-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathstructured-output.test.ts
More file actions
307 lines (281 loc) · 13.3 KB
/
Copy pathstructured-output.test.ts
File metadata and controls
307 lines (281 loc) · 13.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
import { afterEach, describe, expect, expectTypeOf, it, vi } from "vitest";
import type { MessageConnection } from "vscode-jsonrpc/node.js";
import { z } from "zod";
import { CopilotSession } from "../src/session.js";
import type { SessionEvent } from "../src/generated/session-events.js";
import type { MessageOptions } from "../src/types.js";
const answer = z.object({ answer: z.number().int() });
function event(type: SessionEvent["type"], data: unknown, agentId?: string): SessionEvent {
return {
type,
data,
agentId,
id: crypto.randomUUID(),
timestamp: new Date().toISOString(),
parentId: null,
} as SessionEvent;
}
function user(messageId: string): SessionEvent {
return event("user.message", { messageId, content: "question", turnId: "0" });
}
function assistant(originatingMessageId: string, content: string, agentId?: string): SessionEvent {
return event(
"assistant.message",
{ messageId: crypto.randomUUID(), originatingMessageId, content, turnId: "1" },
agentId
);
}
function controlledSession() {
const sends: Array<{
params: Record<string, unknown>;
resolve: (value: { messageId: string }) => void;
reject: (error: Error) => void;
}> = [];
const sendRequest = vi.fn((_method: string, params: Record<string, unknown>) => {
return new Promise<{ messageId: string }>((resolve, reject) => {
sends.push({ params, resolve, reject });
});
});
const session = new CopilotSession("session", { sendRequest } as unknown as MessageConnection);
return { session, sends, sendRequest };
}
async function sent(sends: unknown[], count = 1) {
await vi.waitFor(() => expect(sends).toHaveLength(count));
}
describe("structured output", () => {
afterEach(() => vi.useRealTimers());
it("infers TResult from a Zod schema and forwards its JSON Schema", async () => {
const { session, sends } = controlledSession();
const pending = session.sendAndWait("What is 19 + 23?", answer);
expectTypeOf(pending).toEqualTypeOf<Promise<{ answer: number }>>();
await sent(sends);
expect(sends[0].params.responseFormat).toEqual({
type: "json_schema",
jsonSchema: { name: "response", strict: true, schema: answer.toJSONSchema() },
});
session._dispatchEvent(user("one"));
session._dispatchEvent(assistant("one", '{"answer":42}'));
session._dispatchEvent(event("session.idle", {}));
sends[0].resolve({ messageId: "one" });
await expect(pending).resolves.toEqual({ answer: 42 });
});
it("keeps raw schema sends and options-based Zod sends event-shaped", async () => {
for (const schema of [answer.toJSONSchema(), answer]) {
const { session, sends } = controlledSession();
const options: MessageOptions = { prompt: "question", responseSchema: schema };
const pending = session.sendAndWait(options);
await sent(sends);
const final = assistant("one", '{"answer":42}');
sends[0].resolve({ messageId: "one" });
session._dispatchEvent(user("one"));
session._dispatchEvent(final);
session._dispatchEvent(event("session.idle", {}));
await expect(pending).resolves.toEqual(final);
}
});
it("isolates queued concurrent sends and excludes subagent messages", async () => {
const { session, sends } = controlledSession();
const first = session.sendAndWait("first", answer);
const second = session.sendAndWait("second", answer);
await sent(sends, 2);
session._dispatchEvent(event("session.idle", {}));
session._dispatchEvent(user("one"));
session._dispatchEvent(assistant("one", "intermediate tool-call text"));
session._dispatchEvent(assistant("one", '{"answer":42}'));
session._dispatchEvent(user("two"));
session._dispatchEvent(assistant("two", '{"answer":37}'));
session._dispatchEvent(assistant("one", '{"answer":999}', "subagent"));
session._dispatchEvent(assistant("unrelated", '{"answer":123}'));
session._dispatchEvent(event("session.idle", {}));
sends[1].resolve({ messageId: "two" });
sends[0].resolve({ messageId: "one" });
await expect(first).resolves.toEqual({ answer: 42 });
await expect(second).resolves.toEqual({ answer: 37 });
});
it("freezes the final message at idle even when more events precede send acknowledgement", async () => {
const { session, sends } = controlledSession();
const pending = session.sendAndWait("question", answer);
await sent(sends);
session._dispatchEvent(user("one"));
session._dispatchEvent(assistant("one", '{"answer":42}'));
session._dispatchEvent(event("session.idle", {}));
session._dispatchEvent(assistant("one", '{"answer":999}'));
sends[0].resolve({ messageId: "one" });
await expect(pending).resolves.toEqual({ answer: 42 });
});
it("ignores autopilot idle boundaries until a final idle", async () => {
const { session, sends } = controlledSession();
const pending = session.sendAndWait("question", answer);
await sent(sends);
session._dispatchEvent(user("one"));
session._dispatchEvent(event("session.idle", { mode: "autopilot" }));
session._dispatchEvent(assistant("one", '{"answer":42}'));
session._dispatchEvent(event("session.idle", {}));
sends[0].resolve({ messageId: "one" });
await expect(pending).resolves.toEqual({ answer: 42 });
});
it.each(["refusal", '{"answer":"not a number"}', "null"])(
"rejects invalid final output: %s",
async (content) => {
const { session, sends } = controlledSession();
const pending = session.sendAndWait("question", answer);
const assertion = expect(pending).rejects.toThrow();
await sent(sends);
session._dispatchEvent(user("one"));
session._dispatchEvent(assistant("one", content));
session._dispatchEvent(event("session.idle", {}));
sends[0].resolve({ messageId: "one" });
await assertion;
}
);
it("rejects missing or uncorrelated output rather than borrowing another message", async () => {
const { session, sends } = controlledSession();
const pending = session.sendAndWait("question", answer);
const assertion = expect(pending).rejects.toThrow(
"without a structured assistant response"
);
await sent(sends);
session._dispatchEvent(user("one"));
session._dispatchEvent(assistant("two", '{"answer":42}'));
session._dispatchEvent(event("session.idle", {}));
sends[0].resolve({ messageId: "one" });
await assertion;
});
it("rejects conflicting explicit and inferred schemas before sending", async () => {
const { session, sendRequest } = controlledSession();
await expect(
session.sendAndWait({ prompt: "question", responseSchema: answer }, answer)
).rejects.toThrow("Do not specify responseSchema");
expect(sendRequest).not.toHaveBeenCalled();
});
it("rejects typed immediate steering before sending", async () => {
const { session, sendRequest } = controlledSession();
await expect(
session.sendAndWait({ prompt: "question", mode: "immediate" }, answer)
).rejects.toThrow(
"Structured output cannot be requested on an immediate steering message."
);
expect(sendRequest).not.toHaveBeenCalled();
});
it.each([{ type: "object" }, { toJSONSchema: () => ({ type: "object" }) }, null])(
"rejects an invalid second argument instead of sending an unformatted request: %j",
async (schema) => {
const { session, sendRequest } = controlledSession();
await expect(
// @ts-expect-error Exercise malformed arguments from JavaScript callers.
session.sendAndWait("question", schema)
).rejects.toThrow("Pass raw JSON Schema in options.responseSchema instead.");
expect(sendRequest).not.toHaveBeenCalled();
}
);
it("does not return a partial result after abort", async () => {
const { session, sends } = controlledSession();
const pending = session.sendAndWait("question", answer);
const assertion = expect(pending).rejects.toThrow("aborted");
await sent(sends);
session._dispatchEvent(user("one"));
session._dispatchEvent(assistant("one", '{"answer":42}'));
session._dispatchEvent(event("session.idle", { aborted: true }));
sends[0].resolve({ messageId: "one" });
await assertion;
});
it("does not parse a tool-call message as the final result", async () => {
const { session, sends } = controlledSession();
const pending = session.sendAndWait("question", answer);
const assertion = expect(pending).rejects.toThrow(
"without a structured assistant response"
);
await sent(sends);
session._dispatchEvent(user("one"));
session._dispatchEvent(
event("assistant.message", {
messageId: "assistant-one",
originatingMessageId: "one",
content: '{"answer":42}',
toolRequests: [{ toolCallId: "tool-one", name: "lookup" }],
})
);
session._dispatchEvent(event("session.idle", {}));
sends[0].resolve({ messageId: "one" });
await assertion;
});
it("propagates send and model failures", async () => {
const first = controlledSession();
const sendFailure = first.session.sendAndWait("question", answer);
const sendAssertion = expect(sendFailure).rejects.toThrow("admission failed");
await sent(first.sends);
first.sends[0].reject(new Error("admission failed"));
await sendAssertion;
const second = controlledSession();
const modelFailure = second.session.sendAndWait("question", answer);
const modelAssertion = expect(modelFailure).rejects.toThrow("provider rejected");
await sent(second.sends);
second.session._dispatchEvent(user("one"));
second.session._dispatchEvent(
event("session.error", { message: "provider rejected", errorType: "query" })
);
second.sends[0].resolve({ messageId: "one" });
await modelAssertion;
});
it("times out even while send acknowledgement is pending", async () => {
vi.useFakeTimers();
const { session } = controlledSession();
const pending = session.sendAndWait("question", answer, 100);
const assertion = expect(pending).rejects.toThrow("Timeout after 100ms");
await vi.advanceTimersByTimeAsync(100);
await assertion;
});
it("does not treat an assistant response as successful completion", async () => {
const { session, sends } = controlledSession();
const pending = session.sendAndWait("question", answer);
const assertion = expect(pending).rejects.toThrow("post-response failure");
await sent(sends);
session._dispatchEvent(user("one"));
session._dispatchEvent(
event("assistant.message", {
messageId: "final-reply",
originatingMessageId: "one",
content: '{"answer":42}',
})
);
session._dispatchEvent(
event("session.error", {
errorType: "query",
message: "post-response failure",
})
);
session._dispatchEvent(event("session.idle", {}));
sends[0].resolve({ messageId: "one" });
await assertion;
});
it("waits for idle and returns a correlated hook correction instead of the original answer", async () => {
const { session, sends } = controlledSession();
const pending = session.sendAndWait("question", answer);
const completed = vi.fn();
void pending.then(completed);
await sent(sends);
sends[0].resolve({ messageId: "one" });
session._dispatchEvent(user("one"));
session._dispatchEvent(assistant("one", '{"answer":42}'));
await Promise.resolve();
expect(completed).not.toHaveBeenCalled();
session._dispatchEvent(user("hook-correction"));
session._dispatchEvent(assistant("one", '{"answer":99}'));
session._dispatchEvent(assistant("unrelated", '{"answer":123}'));
await Promise.resolve();
expect(completed).not.toHaveBeenCalled();
session._dispatchEvent(event("session.idle", {}));
await expect(pending).resolves.toEqual({ answer: 99 });
});
it("rejects promptly when the session disconnects", async () => {
const { session, sends } = controlledSession();
const pending = session.sendAndWait("question", answer);
const assertion = expect(pending).rejects.toThrow("Session disconnected");
await sent(sends);
session._markDisconnected();
await assertion;
});
});