-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_openai_adapter.py
More file actions
314 lines (268 loc) · 10.3 KB
/
Copy pathtest_openai_adapter.py
File metadata and controls
314 lines (268 loc) · 10.3 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
from __future__ import annotations
import asyncio
from dataclasses import dataclass
from types import SimpleNamespace
from typing import Any
from zcp import CanonicalValidator, HandleStore, SessionState, ToolDefinition, ToolRegistry, decode_tool_output
from zcp.adapters.openai import AgentLoop, OpenAIResponsesAdapter
from zcp.canonical_runtime import RuntimeExecutor
@dataclass
class FakeResponse:
id: str
output: list[dict]
output_text: str | None = None
class FakeResponsesAPI:
def __init__(
self,
responses: list[FakeResponse],
stream_events: list[dict] | None = None,
create_error: Exception | None = None,
) -> None:
self._responses = list(responses)
self._stream_events = stream_events or []
self._create_error = create_error
self.create_calls: list[dict] = []
def create(self, **kwargs):
self.create_calls.append(kwargs)
if self._create_error is not None:
raise self._create_error
return self._responses.pop(0)
def stream(self, **kwargs):
self.create_calls.append(kwargs)
return FakeStream(self._stream_events)
class FakeStream:
def __init__(self, events: list[dict]) -> None:
self._events = events
def __enter__(self):
return iter(self._events)
def __exit__(self, exc_type, exc, tb):
return False
class FakeClient:
def __init__(
self,
responses: list[FakeResponse],
stream_events: list[dict] | None = None,
response_error: Exception | None = None,
chat_responses: list[Any] | None = None,
chat_stream_events: list[Any] | None = None,
) -> None:
self.responses = FakeResponsesAPI(responses, stream_events=stream_events, create_error=response_error)
self.chat = SimpleNamespace(
completions=FakeChatCompletionsAPI(chat_responses or [], stream_events=chat_stream_events or [])
)
class FakeChatCompletionsAPI:
def __init__(self, responses: list[Any], stream_events: list[Any]) -> None:
self._responses = list(responses)
self._stream_events = stream_events
self.create_calls: list[dict] = []
def create(self, **kwargs):
self.create_calls.append(kwargs)
if kwargs.get("stream"):
return iter(self._stream_events)
return self._responses.pop(0)
def make_chat_response(
*,
message_content: str | None = None,
tool_calls: list[dict] | None = None,
response_id: str = "chat_1",
):
message = SimpleNamespace(
role="assistant",
content=message_content,
tool_calls=tool_calls or [],
model_dump=lambda exclude_none=True: {
key: value
for key, value in {
"role": "assistant",
"content": message_content,
"tool_calls": tool_calls,
}.items()
if value is not None
},
)
choice = SimpleNamespace(message=message)
return SimpleNamespace(id=response_id, choices=[choice])
def build_adapter() -> tuple[OpenAIResponsesAdapter, SessionState]:
session = SessionState(session_id="s1")
registry = ToolRegistry()
registry.register(
ToolDefinition(
tool_id="17",
alias="web.search",
description_short="Search documents.",
input_schema={
"type": "object",
"properties": {
"q": {"type": "string"},
},
"required": ["q"],
"additionalProperties": False,
},
handler=lambda arguments: [{"title": arguments["q"], "url": "https://example.com"}],
handle_kind="web_results",
)
)
registry.register(
ToolDefinition(
tool_id="31",
alias="mail.send",
description_short="Send email.",
input_schema={
"type": "object",
"properties": {
"to": {"type": "string"},
},
"required": ["to"],
"additionalProperties": False,
},
handler=lambda arguments: arguments,
flags=frozenset({"approval"}),
)
)
executor = RuntimeExecutor(registry, CanonicalValidator(), HandleStore(session))
return OpenAIResponsesAdapter(registry, executor), session
def test_run_turn_executes_function_calls_and_submits_outputs() -> None:
adapter, session = build_adapter()
client = FakeClient(
responses=[
FakeResponse(
id="resp_1",
output=[
{
"type": "function_call",
"name": "web_search",
"arguments": '{"q":"compact tools"}',
"call_id": "call_1",
}
],
)
]
)
result = asyncio.run(
adapter.run_turn(client, "gpt-4.1", [{"role": "user", "content": "find docs"}], session)
)
assert result.has_function_calls is True
assert len(result.call_results) == 1
payload = decode_tool_output(result.submitted_outputs[0]["output"])
assert payload["ok"] is True
assert payload["handle"].startswith("#W")
assert client.responses.create_calls[0]["tools"][0]["name"] == "web_search"
assert result.endpoint_used == "responses"
def test_agent_loop_continues_until_final_text() -> None:
adapter, session = build_adapter()
client = FakeClient(
responses=[
FakeResponse(
id="resp_1",
output=[
{
"type": "function_call",
"name": "web_search",
"arguments": '{"q":"compact tools"}',
"call_id": "call_1",
}
],
),
FakeResponse(
id="resp_2",
output=[
{
"type": "message",
"content": [{"type": "output_text", "text": "Here are the results."}],
}
],
output_text="Here are the results.",
),
]
)
loop = AgentLoop(adapter)
result = asyncio.run(loop.run(client, "gpt-4.1", [{"role": "user", "content": "find docs"}], session))
assert result.final_output_text == "Here are the results."
assert client.responses.create_calls[1]["previous_response_id"] == "resp_1"
assert client.responses.create_calls[1]["input"][0]["type"] == "function_call_output"
def test_stream_turn_aggregates_argument_deltas_and_emits_results() -> None:
adapter, session = build_adapter()
client = FakeClient(
responses=[],
stream_events=[
{
"type": "response.output_item.added",
"item": {"type": "function_call", "name": "web_search", "call_id": "call_1", "arguments": ""},
},
{"type": "response.function_call_arguments.delta", "call_id": "call_1", "delta": '{"q":"compact'},
{"type": "response.function_call_arguments.done", "call_id": "call_1", "arguments": '{"q":"compact tools"}'},
],
)
async def collect():
return [event async for event in adapter.stream_turn(client, "gpt-4.1", [], session)]
events = asyncio.run(collect())
assert events[0].kind == "call"
assert events[0].payload["arguments"] == {"q": "compact tools"}
assert events[1].kind == "result"
assert events[-1].kind == "done"
def test_approval_tool_returns_error_envelope() -> None:
adapter, session = build_adapter()
client = FakeClient(
responses=[
FakeResponse(
id="resp_1",
output=[
{
"type": "function_call",
"name": "mail_send",
"arguments": '{"to":"alice@example.com"}',
"call_id": "call_1",
}
],
)
]
)
result = asyncio.run(adapter.run_turn(client, "gpt-4.1", [{"role": "user", "content": "send"}], session))
payload = decode_tool_output(result.submitted_outputs[0]["output"])
assert payload == {"ok": False, "error": "approval_required"}
def test_auto_falls_back_to_chat_completions_for_non_responses_base_url() -> None:
adapter, session = build_adapter()
client = FakeClient(
responses=[],
response_error=RuntimeError("404 not found: /responses"),
chat_responses=[
make_chat_response(
tool_calls=[
SimpleNamespace(
id="call_1",
function=SimpleNamespace(name="web_search", arguments='{"q":"compact tools"}'),
)
],
response_id="chat_1",
)
],
)
result = asyncio.run(adapter.run_turn(client, "deepseek-chat", [{"role": "user", "content": "find docs"}], session))
assert result.endpoint_used == "chat_completions"
assert result.submitted_outputs[0]["role"] == "tool"
assert client.chat.completions.create_calls[0]["tools"][0]["function"]["name"] == "web_search"
def test_chat_agent_loop_appends_assistant_and_tool_messages() -> None:
adapter, session = build_adapter()
client = FakeClient(
responses=[],
response_error=RuntimeError("404 not found: /responses"),
chat_responses=[
make_chat_response(
tool_calls=[
SimpleNamespace(
id="call_1",
function=SimpleNamespace(name="web_search", arguments='{"q":"compact tools"}'),
)
],
response_id="chat_1",
),
make_chat_response(message_content="Here are the results.", response_id="chat_2"),
],
)
loop = AgentLoop(adapter)
result = asyncio.run(loop.run(client, "deepseek-chat", [{"role": "user", "content": "find docs"}], session))
assert result.endpoint_used == "chat_completions"
assert result.final_output_text == "Here are the results."
second_messages = client.chat.completions.create_calls[1]["messages"]
assert second_messages[-2]["role"] == "assistant"
assert second_messages[-1]["role"] == "tool"