-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy path_copilot_request_helpers.py
More file actions
291 lines (255 loc) · 9.74 KB
/
Copy path_copilot_request_helpers.py
File metadata and controls
291 lines (255 loc) · 9.74 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
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# --------------------------------------------------------------------------------------------
"""Shared fixtures and response-builder helpers for the CopilotRequestHandler e2e tests.
The ``copilot_request_*`` tests have no recorded snapshots: the registered
handler fabricates well-formed model responses and the runtime routes all of
its model-layer HTTP/WebSocket traffic through that handler instead of the
CAPI proxy. These helpers centralise the synthetic CAPI shapes (model catalog,
policy, ``/responses`` SSE, ``/chat/completions``) so each test file can focus
on the behaviour it is exercising.
The leading underscore keeps pytest from collecting this module as a test.
"""
from __future__ import annotations
import json
import os
import re
import httpx
import pytest_asyncio
from copilot import CopilotClient, CopilotRequestHandler, RuntimeConnection
from copilot.generated.session_events import AssistantMessageData
from .testharness import E2ETestContext
SYNTHETIC_TEXT = "OK from the synthetic stream."
def sse(event: str, data: dict) -> str:
"""Frame a single Server-Sent Events message: ``event:``/``data:`` + blank line."""
return f"event: {event}\ndata: {json.dumps(data)}\n\n"
def is_inference_url(url: str) -> bool:
"""Return True if ``url`` is a model inference endpoint.
Strips query parameters before matching so URLs like
``/chat/completions?api-version=2024-02`` are handled correctly.
"""
path = url.lower().split("?", 1)[0]
return (
path.endswith("/chat/completions")
or path.endswith("/responses")
or path.endswith("/v1/messages")
or path.endswith("/messages")
)
def _wants_stream(body: bytes) -> bool:
return re.search(rb'"stream"\s*:\s*true', body) is not None
def model_catalog(supported_endpoints: list[str] | None = None) -> dict:
"""The synthetic ``/models`` catalog payload."""
model: dict = {
"id": "claude-sonnet-4.5",
"name": "Claude Sonnet 4.5",
"object": "model",
"vendor": "Anthropic",
"version": "1",
"preview": False,
"model_picker_enabled": True,
"capabilities": {
"type": "chat",
"family": "claude-sonnet-4.5",
"tokenizer": "o200k_base",
"limits": {"max_context_window_tokens": 200000, "max_output_tokens": 8192},
"supports": {
"streaming": True,
"tool_calls": True,
"parallel_tool_calls": True,
"vision": True,
},
},
}
if supported_endpoints is not None:
model["supported_endpoints"] = supported_endpoints
return {"data": [model]}
def responses_events(text: str, resp_id: str = "resp_stub_1") -> list[dict]:
"""The ordered ``/responses`` event objects the runtime's reducer expects."""
return [
{
"type": "response.created",
"response": {
"id": resp_id,
"object": "response",
"status": "in_progress",
"output": [],
},
},
{
"type": "response.output_item.added",
"output_index": 0,
"item": {"id": "msg_1", "type": "message", "role": "assistant", "content": []},
},
{
"type": "response.content_part.added",
"output_index": 0,
"content_index": 0,
"part": {"type": "output_text", "text": ""},
},
{
"type": "response.output_text.delta",
"output_index": 0,
"content_index": 0,
"delta": text,
},
{
"type": "response.output_text.done",
"output_index": 0,
"content_index": 0,
"text": text,
},
{
"type": "response.completed",
"response": {
"id": resp_id,
"object": "response",
"status": "completed",
"output": [
{
"id": "msg_1",
"type": "message",
"role": "assistant",
"content": [{"type": "output_text", "text": text}],
}
],
"usage": {"input_tokens": 5, "output_tokens": 7, "total_tokens": 12},
},
},
]
def build_non_inference_response(
url: str, supported_endpoints: list[str] | None = None
) -> httpx.Response:
"""Build a minimal ``httpx.Response`` for non-inference model-layer requests."""
path = url.lower().split("?", 1)[0] # strip query params before matching
if path.endswith("/models"):
return httpx.Response(
200,
headers={"content-type": "application/json"},
content=json.dumps(model_catalog(supported_endpoints)).encode(),
)
if "/models/session" in path:
return httpx.Response(200, headers={"content-type": "application/json"}, content=b"{}")
if "/policy" in path:
return httpx.Response(
200,
headers={"content-type": "application/json"},
content=json.dumps({"state": "enabled"}).encode(),
)
return httpx.Response(200, headers={"content-type": "application/json"}, content=b"{}")
def build_inference_response(request: httpx.Request, text: str = SYNTHETIC_TEXT) -> httpx.Response:
"""Build a synthetic inference response for ``/responses`` or ``/chat/completions``.
Dispatches by URL and the request body's ``stream`` flag: ``/responses``
streams an SSE event sequence (or returns a buffered Responses object when
``stream`` is false), ``/chat/completions`` streams chat-completion chunks
(or returns a buffered completion).
"""
body = request.content # already drained when send_request is called
wants_stream = _wants_stream(body)
url = str(request.url).lower()
if "/responses" in url:
if not wants_stream:
return httpx.Response(
200,
headers={"content-type": "application/json"},
content=json.dumps(responses_events(text)[-1]["response"]).encode(),
)
stream_body = "".join(sse(e["type"], e) for e in responses_events(text))
return httpx.Response(
200,
headers={"content-type": "text/event-stream"},
content=stream_body.encode(),
)
if "/chat/completions" in url and wants_stream:
base = {
"id": "chatcmpl-stub-1",
"object": "chat.completion.chunk",
"created": 1,
"model": "claude-sonnet-4.5",
}
chunks = [
{
**base,
"choices": [
{
"index": 0,
"delta": {"role": "assistant", "content": ""},
"finish_reason": None,
}
],
},
{
**base,
"choices": [{"index": 0, "delta": {"content": text}, "finish_reason": None}],
},
{
**base,
"choices": [{"index": 0, "delta": {}, "finish_reason": "stop"}],
"usage": {"prompt_tokens": 5, "completion_tokens": 7, "total_tokens": 12},
},
]
stream_body = (
"".join("data: " + json.dumps(c) + "\n\n" for c in chunks) + "data: [DONE]\n\n"
)
return httpx.Response(
200,
headers={"content-type": "text/event-stream"},
content=stream_body.encode(),
)
return httpx.Response(
200,
headers={"content-type": "application/json"},
content=json.dumps(
{
"id": "chatcmpl-stub-1",
"object": "chat.completion",
"created": 1,
"model": "claude-sonnet-4.5",
"choices": [
{
"index": 0,
"message": {"role": "assistant", "content": text},
"finish_reason": "stop",
}
],
"usage": {"prompt_tokens": 5, "completion_tokens": 7, "total_tokens": 12},
}
).encode(),
)
def assistant_text(event) -> str:
if event is not None and isinstance(event.data, AssistantMessageData):
return event.data.content
return ""
def build_isolated_client(
ctx: E2ETestContext,
handler: CopilotRequestHandler,
extra_env: dict[str, str] | None = None,
) -> CopilotClient:
"""Build a CopilotClient wired to ``handler`` via ``request_handler``."""
github_token = (
"fake-token-for-e2e-tests" if os.environ.get("GITHUB_ACTIONS") == "true" else None
)
env = ctx.get_env()
if extra_env:
env = {**env, **extra_env}
return CopilotClient(
connection=RuntimeConnection.for_stdio(path=ctx.cli_path),
working_directory=ctx.work_dir,
env=env,
github_token=github_token,
request_handler=handler,
)
def isolated_client_fixture(make_handler, extra_env: dict[str, str] | None = None):
"""Build a module-scoped pytest-asyncio fixture yielding ``(client, handler)``."""
@pytest_asyncio.fixture(loop_scope="module")
async def _fixture(ctx: E2ETestContext):
handler = make_handler()
client = build_isolated_client(ctx, handler, extra_env)
try:
yield client, handler
finally:
try:
await client.stop()
except Exception:
# Best-effort teardown during fixture cleanup.
pass
return _fixture