Repository navigation
Expand file tree
/
Copy pathstructured_output.e2e.test.ts
More file actions
502 lines (491 loc) · 20.6 KB
/
Copy pathstructured_output.e2e.test.ts
File metadata and controls
502 lines (491 loc) · 20.6 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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
import { describe, expect, expectTypeOf, it } from "vitest";
import { z } from "zod";
import {
approveAll,
defineTool,
type AssistantMessageEvent,
type CopilotSession,
type ProviderConfig,
type SessionEvent,
} from "../../src/index.js";
import { createSdkTestContext, DEFAULT_GITHUB_TOKEN, isCI } from "./harness/sdkTestContext";
import { waitForCondition } from "./harness/sdkTestHelper";
describe("Structured output", async () => {
const { copilotClient: client, openAiEndpoint } = await createSdkTestContext({
copilotClientOptions: {
env: { COPILOT_CLI_ENABLED_FEATURE_FLAGS: "HYDRAFUSION,HYDRAFUSION_ROLLOUT" },
},
});
const provider: ProviderConfig = {
type: "openai",
wireApi: "completions",
baseUrl: openAiEndpoint.url,
modelId: "gpt-4.1",
wireModel: "gpt-4.1",
apiKey: isCI ? DEFAULT_GITHUB_TOKEN : (process.env.GITHUB_TOKEN ?? DEFAULT_GITHUB_TOKEN),
headers: {
"Copilot-Integration-Id": "copilot-developer-cli",
"Copilot-Harness-Id": "copilot-sdk",
"X-GitHub-Api-Version": "2026-08-01",
},
};
it("infers_typed_result_after_custom_tool", async () => {
let calls = 0;
const schema = z.object({ count: z.number().int(), color: z.string() });
const session = await client.createSession({
model: "gpt-4.1",
provider,
onPermissionRequest: approveAll,
availableTools: [],
streaming: true,
tools: [
defineTool("get_inventory", {
description: "Get the current widget inventory.",
parameters: z.object({}),
handler: () => {
calls++;
return "The inventory contains 42 red widgets.";
},
}),
],
});
const events: SessionEvent[] = [];
session.on((event) => events.push(event));
const result = await session.sendAndWait(
"Call get_inventory, then report the widget count and color.",
schema
);
expectTypeOf(result).toEqualTypeOf<{ count: number; color: string }>();
expect(result).toEqual({ count: 42, color: "red" });
expect(calls).toBeGreaterThan(0);
expect(events.some((event) => event.type === "assistant.message_delta")).toBe(true);
const ordinary = await session.sendAndWait(
"Now reply with exactly the plain text HELLO, not JSON."
);
expect(ordinary?.data.content.trim()).toBe("HELLO");
const exchanges = await openAiEndpoint.getExchanges();
expect(exchanges.length).toBeGreaterThanOrEqual(3);
for (const exchange of exchanges.slice(0, -1)) {
expect(exchange.request).toHaveProperty(
"response_format.json_schema.schema",
schema.toJSONSchema()
);
}
expect(exchanges.at(-1)!.request).not.toHaveProperty("response_format");
});
it("typed_result_after_terminal_tool_and_steering", async () => {
const events: SessionEvent[] = [];
let calls = 0;
let session: CopilotSession;
session = await client.createSession({
model: "gpt-4.1",
provider,
onPermissionRequest: approveAll,
availableTools: [],
streaming: true,
onEvent: (event) => events.push(event),
tools: [
defineTool("lookup_number", {
description: "Return the number needed for the calculation.",
parameters: z.object({}),
skipPermission: true,
isTerminal: true,
handler: async () => {
calls++;
await session.send({
prompt: "Continue with the original calculation. Do not call any more tools.",
mode: "immediate",
});
return 58;
},
}),
],
});
const schema = z.object({ answer: z.number().int(), contract: z.literal("typed_tool") });
const result = await session.sendAndWait(
"Call lookup_number exactly once, then add 5 to the returned number. Do not guess its result.",
schema
);
expectTypeOf(result).toEqualTypeOf<{ answer: number; contract: "typed_tool" }>();
expect(result).toEqual({ answer: 63, contract: "typed_tool" });
expect(calls).toBe(1);
expect(events.some((event) => event.type === "tool.execution_complete")).toBe(true);
expect(events.some((event) => event.type === "assistant.message_delta")).toBe(true);
const replies = events.filter(
(event) => event.type === "assistant.message" && !event.agentId
);
expect(replies.some((event) => event.data.toolRequests?.length)).toBe(true);
expect(replies.at(-1)?.data.toolRequests ?? []).toEqual([]);
const exchanges = await openAiEndpoint.getExchanges();
expect(exchanges.length).toBeGreaterThanOrEqual(2);
for (const exchange of exchanges.slice(1)) {
expect(exchange.request).toHaveProperty("tool_choice", "none");
}
for (const exchange of exchanges) {
expect(exchange.request).toHaveProperty(
"response_format.json_schema.schema",
schema.toJSONSchema()
);
}
});
it("typed_wait_returns_stop_hook_correction_after_terminal_tool", async () => {
let calls = 0;
let stops = 0;
const replies: AssistantMessageEvent[] = [];
const session = await client.createSession({
model: "gpt-4.1",
provider,
onPermissionRequest: approveAll,
availableTools: [],
tools: [
defineTool("lookup_number", {
description: "Return the number needed for the calculation.",
parameters: z.object({}),
skipPermission: true,
isTerminal: true,
handler: () => {
calls++;
return 58;
},
}),
],
onEvent: (event) => {
if (event.type === "assistant.message" && !event.agentId) replies.push(event);
},
hooks: {
onAgentStop: () =>
++stops === 1
? {
decision: "block",
reason: "Correct the answer to 99, not 63. Do not use tools.",
}
: undefined,
},
});
const schema = z.object({ answer: z.number().int() });
const result = await session.sendAndWait(
"Call lookup_number exactly once, then add 5 to the returned number. Do not guess its result.",
schema
);
expect(result).toEqual({ answer: 99 });
expect(calls).toBe(1);
expect(stops).toBe(2);
const answers = replies.filter((reply) => !reply.data.toolRequests?.length);
expect(answers.map((reply): unknown => JSON.parse(reply.data.content))).toEqual([
{ answer: 63 },
{ answer: 99 },
]);
expect(answers[0].data.originatingMessageId).toBeTruthy();
expect(answers[1].data.originatingMessageId).toBe(answers[0].data.originatingMessageId);
const exchanges = await openAiEndpoint.getExchanges();
expect(exchanges).toHaveLength(3);
expect(exchanges[1].request).toHaveProperty("tool_choice", "none");
for (const exchange of exchanges) {
expect(exchange.request).toHaveProperty(
"response_format.json_schema.schema",
schema.toJSONSchema()
);
}
});
it("rejects_unsupported_or_oversized_schemas_before_admission", async () => {
for (const model of ["gpt-4.1", "hydrafusion"]) {
const session = await client.createSession({
model,
provider,
onPermissionRequest: approveAll,
availableTools: [],
});
await expect(
session.sendAndWait(
{ prompt: "Must not be admitted", mode: "immediate" },
z.object({ answer: z.number().int() })
)
).rejects.toThrow(/immediate/);
const schema = {
type: "object",
description: model === "gpt-4.1" ? "x".repeat(32 * 1024 * 1024) : "Small schema",
};
const message = model === "gpt-4.1" ? /32 MiB/ : /HydraFusion/;
await expect(
session.sendAndWait({
prompt: "Must not be admitted",
responseSchema: schema,
})
).rejects.toThrow(message);
await expect(
session.rpc.sendMessages({
messages: [],
responseFormat: {
type: "json_schema",
jsonSchema: { name: "response", schema },
},
})
).rejects.toThrow(message);
expect((await session.rpc.queue.pendingItems()).items).toEqual([]);
expect(
(await session.getEvents()).filter(
(event) => event.type === "user.message" || event.type === "session.error"
)
).toEqual([]);
}
expect(await openAiEndpoint.getExchanges()).toEqual([]);
});
it("send_selects_correlated_response_after_idle", async () => {
let releaseHook!: () => void;
let hookEntered = false;
const hookReleased = new Promise<void>((resolve) => {
releaseHook = resolve;
});
const session = await client.createSession({
model: "gpt-4.1",
provider,
onPermissionRequest: approveAll,
availableTools: [],
tools: [
defineTool("read_inventory", {
description: "Read the current widget count and color.",
parameters: z.object({}),
skipPermission: true,
handler: () => "The inventory contains 42 red widgets.",
}),
],
hooks: {
onAgentStop: async () => {
hookEntered = true;
await hookReleased;
},
},
});
const replies: AssistantMessageEvent[] = [];
const errors: string[] = [];
let idle = false;
const unsubscribe = session.on((event) => {
if (event.agentId) return;
if (event.type === "assistant.message") {
replies.push(event);
} else if (event.type === "session.error") {
errors.push(event.data.message);
} else if (event.type === "session.idle") {
idle = true;
}
});
const schema = z.object({ count: z.number().int(), color: z.string() });
try {
const messageId = await session.send({
prompt: "Call read_inventory once, then report the current widget count and color.",
responseSchema: schema,
});
await waitForCondition(() => hookEntered || errors.length > 0, {
timeoutMessage: "Stop hook did not start",
});
expect(errors).toEqual([]);
expect(idle).toBe(false);
releaseHook();
await waitForCondition(() => idle || errors.length > 0, {
timeoutMessage: "Session did not become idle",
});
expect(errors).toEqual([]);
const reply = replies.findLast(
(event) => event.data.originatingMessageId === messageId
);
expect(reply).toBeDefined();
if (!reply) throw new Error("No correlated assistant response");
expect(reply.data.originatingMessageId).toBe(messageId);
expect(schema.parse(JSON.parse(reply.data.content))).toEqual({
count: 42,
color: "red",
});
expect(replies.some((event) => event.data.toolRequests?.length)).toBe(true);
expect(reply.data.toolRequests ?? []).toEqual([]);
expect(replies.at(-1)).toBe(reply);
} finally {
releaseHook();
unsubscribe();
}
}, 60_000);
it("typed_wait_returns_stop_hook_correction", async () => {
let stops = 0;
const replies: AssistantMessageEvent[] = [];
const schema = z.object({ answer: z.number().int() });
const session = await client.createSession({
model: "gpt-4.1",
provider,
onPermissionRequest: approveAll,
availableTools: [],
onEvent: (event) => {
if (event.type === "assistant.message" && !event.agentId) replies.push(event);
},
hooks: {
onAgentStop: () =>
++stops === 1
? {
decision: "block",
reason: "Correct the answer to 99, not 42. Do not use tools.",
}
: undefined,
},
});
const result = await session.sendAndWait("What is 19 + 23? Do not use tools.", schema);
expect(result).toEqual({ answer: 99 });
expect(stops).toBe(2);
expect(replies.map((reply): unknown => JSON.parse(reply.data.content))).toEqual([
{ answer: 42 },
{ answer: 99 },
]);
expect(replies[0].data.originatingMessageId).toBeTruthy();
expect(replies[1].data.originatingMessageId).toBe(replies[0].data.originatingMessageId);
const exchanges = await openAiEndpoint.getExchanges();
expect(exchanges).toHaveLength(2);
for (const exchange of exchanges) {
expect(exchange.request).toHaveProperty(
"response_format.json_schema.schema",
schema.toJSONSchema()
);
}
});
it("typed_wait_returns_late_steering_response", async () => {
let stops = 0;
let steeringId: string | undefined;
let session: CopilotSession;
const replies: AssistantMessageEvent[] = [];
const schema = z.object({ answer: z.number().int() });
session = await client.createSession({
model: "gpt-4.1",
provider,
onPermissionRequest: approveAll,
availableTools: [],
onEvent: (event) => {
if (event.type === "assistant.message" && !event.agentId) replies.push(event);
},
hooks: {
onAgentStop: async () => {
if (++stops === 1) {
// The final model request has finished, but this run still admits steering.
steeringId = await session.send({
prompt: "Change the answer to 99. Do not use tools.",
mode: "immediate",
});
}
},
},
});
const result = await session.sendAndWait("What is 19 + 23? Do not use tools.", schema);
expect(result).toEqual({ answer: 99 });
expect(stops).toBe(2);
expect(replies.map((reply): unknown => JSON.parse(reply.data.content))).toEqual([
{ answer: 42 },
{ answer: 99 },
]);
expect(steeringId).toBeTruthy();
expect(replies[0].data.originatingMessageId).toBeTruthy();
expect(replies[0].data.originatingMessageId).not.toBe(steeringId);
expect(replies[1].data.originatingMessageId).toBe(replies[0].data.originatingMessageId);
const exchanges = await openAiEndpoint.getExchanges();
expect(exchanges).toHaveLength(2);
for (const exchange of exchanges) {
expect(exchange.request).toHaveProperty(
"response_format.json_schema.schema",
schema.toJSONSchema()
);
}
});
it("concurrent_typed_sends_return_their_own_results", async () => {
let markToolEntered!: () => void;
let releaseTool!: () => void;
const toolEntered = new Promise<void>((resolve) => {
markToolEntered = resolve;
});
const toolReleased = new Promise<void>((resolve) => {
releaseTool = resolve;
});
const session = await client.createSession({
model: "gpt-4.1",
provider,
onPermissionRequest: approveAll,
availableTools: [],
tools: [
defineTool("first_number", {
description: "Get the number for the first question.",
parameters: z.object({}),
skipPermission: true,
handler: async () => {
markToolEntered();
await toolReleased;
return 42;
},
}),
],
});
const first = session.sendAndWait(
"Call first_number exactly once and report its returned number.",
z.object({ first: z.number().int() })
);
try {
await Promise.race([
toolEntered,
first.then(() => {
throw new Error("First run completed without calling first_number");
}),
]);
const secondPrompt = "What is 30 + 7? Do not use tools.";
const second = session.sendAndWait(
secondPrompt,
z.object({ second: z.number().int() })
);
const results = Promise.all([first, second]);
await Promise.race([
waitForCondition(
async () =>
(await session.rpc.queue.pendingItems()).items.some((item) =>
item.displayText.includes(secondPrompt)
),
{ timeoutMessage: "Second structured send was not queued behind the tool call" }
),
results.then(() => {
throw new Error("Runs completed before the tool was released");
}),
]);
releaseTool();
const [firstResult, secondResult] = await results;
expect(firstResult).toEqual({ first: 42 });
expect(secondResult).toEqual({ second: 37 });
} finally {
releaseTool();
}
});
it("sends_explicit_schema_for_message_and_batch", async () => {
const session = await client.createSession({
model: "gpt-4.1",
provider,
onPermissionRequest: approveAll,
availableTools: [],
});
const schema = z.object({ count: z.number().int(), color: z.string() });
const events: SessionEvent[] = [];
session.on((event) => events.push(event));
const response = await session.rpc.sendMessages({
messages: [
{ prompt: "There are 42 red widgets in stock." },
{ prompt: "Report the widget count and color." },
],
responseFormat: {
type: "json_schema",
jsonSchema: { name: "inventory", strict: true, schema: schema.toJSONSchema() },
},
wait: true,
});
const final = events.findLast((event) => event.type === "assistant.message");
expect(final?.type).toBe("assistant.message");
if (final?.type !== "assistant.message") throw new Error("No assistant response");
expect(schema.parse(JSON.parse(final.data.content))).toEqual({ count: 42, color: "red" });
expect(final.data.originatingMessageId).toBe(response.messageIds.at(-1));
const raw = await session.sendAndWait({
prompt: "The inventory now has 21 blue widgets. Report the new count and color.",
responseSchema: schema.toJSONSchema(),
});
expect(schema.parse(JSON.parse(raw!.data.content))).toEqual({ count: 21, color: "blue" });
});
});