Repository navigation
Expand file tree
/
Copy pathmode_handlers.e2e.test.ts
More file actions
198 lines (179 loc) · 7.88 KB
/
Copy pathmode_handlers.e2e.test.ts
File metadata and controls
198 lines (179 loc) · 7.88 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
import { describe, expect, it } from "vitest";
import type {
AutoModeSwitchRequest,
CopilotSession,
ExitPlanModeRequest,
ExitPlanModeResult,
SessionEvent,
} from "../../src/index.js";
import { approveAll } from "../../src/index.js";
import { createSdkTestContext } from "./harness/sdkTestContext.js";
const EVENT_TIMEOUT_MS = 30_000;
const MODE_HANDLER_TOKEN = "mode-handler-token";
const PLAN_SUMMARY = "Greeting file implementation plan";
const PLAN_PROMPT =
"Create a brief implementation plan for adding a greeting.txt file, then request approval with exit_plan_mode.";
const AUTO_MODE_PROMPT =
"Explain that auto mode recovered from a rate limit in one short sentence.";
function waitForEvent<T extends SessionEvent>(
session: CopilotSession,
predicate: (event: SessionEvent) => event is T,
description: string,
timeoutMs = EVENT_TIMEOUT_MS,
allowRateLimitError = false
): Promise<T> {
return new Promise<T>((resolve, reject) => {
let unsubscribe: () => void = () => {};
const timer = setTimeout(() => {
unsubscribe();
reject(new Error(`Timed out waiting for ${description}`));
}, timeoutMs);
unsubscribe = session.on((event) => {
if (predicate(event)) {
clearTimeout(timer);
unsubscribe();
resolve(event);
} else if (
event.type === "session.error" &&
!(allowRateLimitError && event.data.errorType === "rate_limit")
) {
clearTimeout(timer);
unsubscribe();
reject(new Error(`${event.data.message}\n${event.data.stack ?? ""}`));
}
});
});
}
describe("Mode handlers", async () => {
const { copilotClient: client, openAiEndpoint, env } = await createSdkTestContext();
env.COPILOT_DEBUG_GITHUB_API_URL = env.COPILOT_API_URL;
await openAiEndpoint.setCopilotUserByToken(MODE_HANDLER_TOKEN, {
login: "mode-handler-user",
copilot_plan: "individual_pro",
endpoints: {
api: env.COPILOT_API_URL,
telemetry: "https://localhost:1/telemetry",
},
analytics_tracking_id: "mode-handler-tracking-id",
});
it("should invoke exit plan mode handler when model uses tool", async () => {
const exitPlanModeRequests: ExitPlanModeRequest[] = [];
let session: CopilotSession | undefined;
session = await client.createSession({
gitHubToken: MODE_HANDLER_TOKEN,
onPermissionRequest: approveAll,
onExitPlanModeRequest: async (request, invocation): Promise<ExitPlanModeResult> => {
exitPlanModeRequests.push(request);
expect(invocation.sessionId).toBe(session?.sessionId);
return {
approved: true,
selectedAction: "interactive",
feedback: "Approved by the TypeScript E2E test",
};
},
});
try {
const requestedEvent = waitForEvent(
session,
(event): event is Extract<SessionEvent, { type: "exit_plan_mode.requested" }> =>
event.type === "exit_plan_mode.requested" &&
event.data.summary === PLAN_SUMMARY,
"exit_plan_mode.requested event"
);
const completedEvent = waitForEvent(
session,
(event): event is Extract<SessionEvent, { type: "exit_plan_mode.completed" }> =>
event.type === "exit_plan_mode.completed" &&
event.data.approved === true &&
event.data.selectedAction === "interactive",
"exit_plan_mode.completed event"
);
const response = await session.sendAndWait({
prompt: PLAN_PROMPT,
agentMode: "plan",
});
expect(exitPlanModeRequests).toHaveLength(1);
expect(exitPlanModeRequests[0]).toMatchObject({
summary: PLAN_SUMMARY,
actions: ["autopilot", "interactive", "exit_only"],
recommendedAction: "interactive",
});
expect(exitPlanModeRequests[0].planContent).toBeDefined();
expect((await requestedEvent).data.summary).toBe(PLAN_SUMMARY);
const completed = await completedEvent;
expect(completed.data.approved).toBe(true);
expect(completed.data.selectedAction).toBe("interactive");
expect(completed.data.feedback).toBe("Approved by the TypeScript E2E test");
expect(response).toBeDefined();
} finally {
await session.disconnect();
}
});
it("should invoke auto mode switch handler when rate limited", async () => {
const autoModeSwitchRequests: AutoModeSwitchRequest[] = [];
let session: CopilotSession | undefined;
session = await client.createSession({
gitHubToken: MODE_HANDLER_TOKEN,
onPermissionRequest: approveAll,
onAutoModeSwitchRequest: (request, invocation) => {
autoModeSwitchRequests.push(request);
expect(invocation.sessionId).toBe(session?.sessionId);
return "yes";
},
});
try {
const requestedEvent = waitForEvent(
session,
(event): event is Extract<SessionEvent, { type: "auto_mode_switch.requested" }> =>
event.type === "auto_mode_switch.requested" &&
event.data.errorCode === "user_weekly_rate_limited" &&
event.data.retryAfterSeconds === 1,
"auto_mode_switch.requested event",
EVENT_TIMEOUT_MS,
true
);
const completedEvent = waitForEvent(
session,
(event): event is Extract<SessionEvent, { type: "auto_mode_switch.completed" }> =>
event.type === "auto_mode_switch.completed" && event.data.response === "yes",
"auto_mode_switch.completed event",
EVENT_TIMEOUT_MS,
true
);
const modelChangeEvent = waitForEvent(
session,
(event): event is Extract<SessionEvent, { type: "session.model_change" }> =>
event.type === "session.model_change" &&
event.data.cause === "rate_limit_auto_switch",
"rate-limit auto-mode model change",
EVENT_TIMEOUT_MS,
true
);
const idleEvent = waitForEvent(
session,
(event): event is Extract<SessionEvent, { type: "session.idle" }> =>
event.type === "session.idle",
"session.idle after auto-mode switch",
EVENT_TIMEOUT_MS,
true
);
const messageId = await session.send({ prompt: AUTO_MODE_PROMPT });
expect(messageId).toBeTruthy();
expect((await requestedEvent).data.errorCode).toBe("user_weekly_rate_limited");
const completed = await completedEvent;
expect(completed.data.response).toBe("yes");
expect((await modelChangeEvent).data.cause).toBe("rate_limit_auto_switch");
await idleEvent;
expect(autoModeSwitchRequests).toHaveLength(1);
expect(autoModeSwitchRequests[0]).toMatchObject({
errorCode: "user_weekly_rate_limited",
retryAfterSeconds: 1,
});
} finally {
await session.disconnect();
}
});
});