forked from openai/openai-agents-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_tool_context.py
More file actions
418 lines (346 loc) · 12.2 KB
/
test_tool_context.py
File metadata and controls
418 lines (346 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
from typing import Annotated, Any, cast
import pytest
from openai.types.responses import ResponseFunctionToolCall
from agents import Agent
from agents.run_config import RunConfig
from agents.run_context import RunContextWrapper
from agents.tool import FunctionTool, invoke_function_tool
from agents.tool_context import ToolContext
from agents.usage import Usage
from tests.utils.hitl import make_context_wrapper
def test_tool_context_requires_fields() -> None:
ctx: RunContextWrapper[dict[str, object]] = RunContextWrapper(context={})
with pytest.raises(ValueError):
ToolContext.from_agent_context(ctx, tool_call_id="call-1")
def test_tool_context_missing_defaults_raise() -> None:
base_ctx: RunContextWrapper[dict[str, object]] = RunContextWrapper(context={})
with pytest.raises(ValueError):
ToolContext(context=base_ctx.context, tool_call_id="call-1", tool_arguments="")
with pytest.raises(ValueError):
ToolContext(context=base_ctx.context, tool_name="name", tool_arguments="")
with pytest.raises(ValueError):
ToolContext(context=base_ctx.context, tool_name="name", tool_call_id="call-1")
def test_tool_context_from_agent_context_populates_fields() -> None:
tool_call = ResponseFunctionToolCall(
type="function_call",
name="test_tool",
call_id="call-123",
arguments='{"a": 1}',
)
ctx = make_context_wrapper()
agent = Agent(name="agent")
tool_ctx = ToolContext.from_agent_context(
ctx,
tool_call_id="call-123",
tool_call=tool_call,
agent=agent,
)
assert tool_ctx.tool_name == "test_tool"
assert tool_ctx.tool_call_id == "call-123"
assert tool_ctx.tool_arguments == '{"a": 1}'
assert tool_ctx.agent is agent
def test_tool_context_agent_none_by_default() -> None:
tool_call = ResponseFunctionToolCall(
type="function_call",
name="test_tool",
call_id="call-1",
arguments="{}",
)
ctx = make_context_wrapper()
tool_ctx = ToolContext.from_agent_context(ctx, tool_call_id="call-1", tool_call=tool_call)
assert tool_ctx.agent is None
def test_tool_context_constructor_accepts_agent_keyword() -> None:
agent = Agent(name="direct-agent")
tool_ctx: ToolContext[dict[str, object]] = ToolContext(
context={},
tool_name="my_tool",
tool_call_id="call-2",
tool_arguments="{}",
agent=agent,
)
assert tool_ctx.agent is agent
def test_tool_context_constructor_infers_namespace_from_tool_call() -> None:
tool_call = ResponseFunctionToolCall(
type="function_call",
name="lookup_account",
call_id="call-2",
arguments="{}",
namespace="billing",
)
tool_ctx: ToolContext[dict[str, object]] = ToolContext(
context={},
tool_name="lookup_account",
tool_call_id="call-2",
tool_arguments="{}",
tool_call=tool_call,
)
assert tool_ctx.tool_namespace == "billing"
assert tool_ctx.qualified_tool_name == "billing.lookup_account"
def test_tool_context_qualified_tool_name_collapses_synthetic_namespace() -> None:
tool_call = ResponseFunctionToolCall(
type="function_call",
name="get_weather",
call_id="call-weather",
arguments="{}",
namespace="get_weather",
)
tool_ctx: ToolContext[dict[str, object]] = ToolContext(
context={},
tool_name="get_weather",
tool_call_id="call-weather",
tool_arguments="{}",
tool_call=tool_call,
)
assert tool_ctx.tool_namespace == "get_weather"
assert tool_ctx.qualified_tool_name == "get_weather"
def test_tool_context_from_tool_context_inherits_agent() -> None:
original_call = ResponseFunctionToolCall(
type="function_call",
name="test_tool",
call_id="call-3",
arguments="{}",
)
derived_call = ResponseFunctionToolCall(
type="function_call",
name="test_tool",
call_id="call-4",
arguments="{}",
)
agent = Agent(name="origin-agent")
parent_context: ToolContext[dict[str, object]] = ToolContext(
context={},
tool_name="test_tool",
tool_call_id="call-3",
tool_arguments="{}",
tool_call=original_call,
agent=agent,
)
derived_context = ToolContext.from_agent_context(
parent_context,
tool_call_id="call-4",
tool_call=derived_call,
)
assert derived_context.agent is agent
def test_tool_context_from_tool_context_inherits_run_config() -> None:
original_call = ResponseFunctionToolCall(
type="function_call",
name="test_tool",
call_id="call-3",
arguments="{}",
)
derived_call = ResponseFunctionToolCall(
type="function_call",
name="test_tool",
call_id="call-4",
arguments="{}",
)
parent_run_config = RunConfig(model="gpt-4.1-mini")
parent_context: ToolContext[dict[str, object]] = ToolContext(
context={},
tool_name="test_tool",
tool_call_id="call-3",
tool_arguments="{}",
tool_call=original_call,
run_config=parent_run_config,
)
derived_context = ToolContext.from_agent_context(
parent_context,
tool_call_id="call-4",
tool_call=derived_call,
)
assert derived_context.run_config is parent_run_config
def test_tool_context_from_agent_context_prefers_explicit_run_config() -> None:
tool_call = ResponseFunctionToolCall(
type="function_call",
name="test_tool",
call_id="call-1",
arguments="{}",
)
ctx = make_context_wrapper()
explicit_run_config = RunConfig(model="gpt-4.1")
tool_ctx = ToolContext.from_agent_context(
ctx,
tool_call_id="call-1",
tool_call=tool_call,
run_config=explicit_run_config,
)
assert tool_ctx.run_config is explicit_run_config
@pytest.mark.asyncio
async def test_invoke_function_tool_passes_plain_run_context_when_requested() -> None:
captured_context: RunContextWrapper[str] | None = None
async def on_invoke_tool(ctx: RunContextWrapper[str], _input: str) -> str:
nonlocal captured_context
captured_context = ctx
return ctx.context
function_tool = FunctionTool(
name="plain_context_tool",
description="test",
params_json_schema={"type": "object", "properties": {}},
on_invoke_tool=on_invoke_tool,
)
tool_context = ToolContext(
context="Stormy",
usage=Usage(),
tool_name="plain_context_tool",
tool_call_id="call-1",
tool_arguments="{}",
agent=Agent(name="agent"),
run_config=RunConfig(model="gpt-4.1-mini"),
tool_input={"city": "Tokyo"},
)
result = await invoke_function_tool(
function_tool=function_tool,
context=tool_context,
arguments="{}",
)
assert result == "Stormy"
assert captured_context is not None
assert not isinstance(captured_context, ToolContext)
assert captured_context.context == "Stormy"
assert captured_context.usage is tool_context.usage
assert captured_context.tool_input == {"city": "Tokyo"}
@pytest.mark.asyncio
async def test_invoke_function_tool_preserves_tool_context_when_requested() -> None:
captured_context: ToolContext[str] | None = None
async def on_invoke_tool(ctx: ToolContext[str], _input: str) -> str:
nonlocal captured_context
captured_context = ctx
return ctx.tool_name
function_tool = FunctionTool(
name="tool_context_tool",
description="test",
params_json_schema={"type": "object", "properties": {}},
on_invoke_tool=on_invoke_tool,
)
tool_context = ToolContext(
context="Stormy",
usage=Usage(),
tool_name="tool_context_tool",
tool_call_id="call-2",
tool_arguments="{}",
agent=Agent(name="agent"),
run_config=RunConfig(model="gpt-4.1-mini"),
)
result = await invoke_function_tool(
function_tool=function_tool,
context=tool_context,
arguments="{}",
)
assert result == "tool_context_tool"
assert captured_context is tool_context
@pytest.mark.asyncio
async def test_invoke_function_tool_ignores_context_name_substrings_in_string_annotations() -> None:
captured_context: object | None = None
class MyRunContextWrapper:
pass
async def on_invoke_tool(ctx: "MyRunContextWrapper", _input: str) -> str:
nonlocal captured_context
captured_context = ctx
return "ok"
function_tool = FunctionTool(
name="substring_context_tool",
description="test",
params_json_schema={"type": "object", "properties": {}},
on_invoke_tool=cast(Any, on_invoke_tool),
)
tool_context = ToolContext(
context="Stormy",
usage=Usage(),
tool_name="substring_context_tool",
tool_call_id="call-3",
tool_arguments="{}",
)
result = await invoke_function_tool(
function_tool=function_tool,
context=tool_context,
arguments="{}",
)
assert result == "ok"
assert captured_context is tool_context
@pytest.mark.asyncio
async def test_invoke_function_tool_ignores_annotated_string_metadata_when_matching_context() -> (
None
):
captured_context: ToolContext[str] | RunContextWrapper[str] | None = None
async def on_invoke_tool(
ctx: Annotated[RunContextWrapper[str], "ToolContext note"], _input: str
) -> str:
nonlocal captured_context
captured_context = ctx
return ctx.context
function_tool = FunctionTool(
name="annotated_string_context_tool",
description="test",
params_json_schema={"type": "object", "properties": {}},
on_invoke_tool=on_invoke_tool,
)
tool_context = ToolContext(
context="Stormy",
usage=Usage(),
tool_name="annotated_string_context_tool",
tool_call_id="call-4",
tool_arguments="{}",
tool_input={"city": "Tokyo"},
)
result = await invoke_function_tool(
function_tool=function_tool,
context=tool_context,
arguments="{}",
)
assert result == "Stormy"
assert captured_context is not None
assert not isinstance(captured_context, ToolContext)
assert captured_context.tool_input == {"city": "Tokyo"}
def test_conversation_history_defaults_to_empty() -> None:
"""conversation_history is empty when no generated items are set."""
tool_ctx: ToolContext[None] = ToolContext(
context=None,
tool_name="tool",
tool_call_id="call-1",
tool_arguments="{}",
)
assert tool_ctx.conversation_history == []
def test_conversation_history_populated_via_wrapper() -> None:
"""conversation_history is populated from RunContextWrapper._generated_items."""
wrapper: RunContextWrapper[None] = RunContextWrapper(context=None)
wrapper._generated_items = ["item_a", "item_b"]
tool_call = ResponseFunctionToolCall(
type="function_call",
name="my_tool",
call_id="call-10",
arguments="{}",
)
tool_ctx = ToolContext.from_agent_context(
wrapper,
tool_call_id="call-10",
tool_call=tool_call,
)
assert tool_ctx.conversation_history == ["item_a", "item_b"]
def test_conversation_history_returns_copy() -> None:
"""Mutating the returned list must not affect the underlying state."""
wrapper: RunContextWrapper[None] = RunContextWrapper(context=None)
wrapper._generated_items = ["item_a"]
tool_call = ResponseFunctionToolCall(
type="function_call",
name="my_tool",
call_id="call-11",
arguments="{}",
)
tool_ctx = ToolContext.from_agent_context(
wrapper,
tool_call_id="call-11",
tool_call=tool_call,
)
history = tool_ctx.conversation_history
history.append("extra")
assert tool_ctx.conversation_history == ["item_a"]
def test_conversation_history_via_constructor() -> None:
"""conversation_history can be set via _generated_items kwarg."""
tool_ctx: ToolContext[None] = ToolContext(
context=None,
tool_name="tool",
tool_call_id="call-12",
tool_arguments="{}",
_generated_items=["x", "y"],
)
assert tool_ctx.conversation_history == ["x", "y"]