forked from github/copilot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.test.ts
More file actions
237 lines (209 loc) · 9.42 KB
/
Copy pathtools.test.ts
File metadata and controls
237 lines (209 loc) · 9.42 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
import { writeFile } from "fs/promises";
import { join } from "path";
import { assert, describe, expect, it } from "vitest";
import { z } from "zod";
import { defineTool, approveAll } from "../../src/index.js";
import type { PermissionRequest } from "../../src/index.js";
import { createSdkTestContext } from "./harness/sdkTestContext";
describe("Custom tools", async () => {
const { copilotClient: client, openAiEndpoint, workDir } = await createSdkTestContext();
it("invokes built-in tools", async () => {
await writeFile(join(workDir, "README.md"), "# ELIZA, the only chatbot you'll ever need");
const session = await client.createSession({
onPermissionRequest: approveAll,
});
const assistantMessage = await session.sendAndWait({
prompt: "What's the first line of README.md in this directory?",
});
expect(assistantMessage?.data.content).toContain("ELIZA");
});
it("invokes custom tool", async () => {
const session = await client.createSession({
onPermissionRequest: approveAll,
tools: [
defineTool("encrypt_string", {
description: "Encrypts a string",
parameters: z.object({
input: z.string().describe("String to encrypt"),
}),
handler: ({ input }) => input.toUpperCase(),
}),
],
});
const assistantMessage = await session.sendAndWait({
prompt: "Use encrypt_string to encrypt this string: Hello",
});
expect(assistantMessage?.data.content).toContain("HELLO");
});
it("handles tool calling errors", async () => {
const session = await client.createSession({
onPermissionRequest: approveAll,
tools: [
defineTool("get_user_location", {
description: "Gets the user's location",
handler: () => {
throw new Error("Melbourne");
},
}),
],
});
const answer = await session.sendAndWait({
prompt: "What is my location? If you can't find out, just say 'unknown'.",
});
// Check the underlying traffic
const traffic = await openAiEndpoint.getExchanges();
const lastConversation = traffic[traffic.length - 1];
const toolCalls = lastConversation.request.messages.flatMap((m) =>
m.role === "assistant" ? m.tool_calls : []
);
expect(toolCalls.length).toBe(1);
const toolCall = toolCalls[0]!;
assert(toolCall.type === "function");
expect(toolCall.function.name).toBe("get_user_location");
const toolResults = lastConversation.request.messages.filter((m) => m.role === "tool");
expect(toolResults.length).toBe(1);
const toolResult = toolResults[0]!;
expect(toolResult.tool_call_id).toBe(toolCall.id);
expect(toolResult.content).not.toContain("Melbourne");
// Importantly, we're checking that the assistant does not see the
// exception information as if it was the tool's output.
expect(answer?.data.content).not.toContain("Melbourne");
expect(answer?.data.content?.toLowerCase()).toContain("unknown");
});
it("can receive and return complex types", async () => {
const session = await client.createSession({
onPermissionRequest: approveAll,
tools: [
defineTool("db_query", {
description: "Performs a database query",
parameters: z.object({
query: z.object({
table: z.string(),
ids: z.array(z.number()),
sortAscending: z.boolean(),
}),
}),
handler: ({ query }, invocation) => {
expect(query.table).toBe("cities");
expect(query.ids).toEqual([12, 19]);
expect(query.sortAscending).toBe(true);
expect(invocation.sessionId).toBe(session.sessionId);
return [
{ countryId: 19, cityName: "Passos", population: 135460 },
{ countryId: 12, cityName: "San Lorenzo", population: 204356 },
];
},
}),
],
});
const assistantMessage = await session.sendAndWait({
prompt:
"Perform a DB query for the 'cities' table using IDs 12 and 19, sorting ascending. " +
"Reply only with lines of the form: [cityname] [population]",
});
const responseContent = assistantMessage?.data.content!;
expect(assistantMessage).not.toBeNull();
expect(responseContent).not.toBe("");
expect(responseContent).toContain("Passos");
expect(responseContent).toContain("San Lorenzo");
expect(responseContent.replace(/,/g, "")).toContain("135460");
expect(responseContent.replace(/,/g, "")).toContain("204356");
});
it("invokes custom tool with permission handler", async () => {
const permissionRequests: PermissionRequest[] = [];
const session = await client.createSession({
tools: [
defineTool("encrypt_string", {
description: "Encrypts a string",
parameters: z.object({
input: z.string().describe("String to encrypt"),
}),
handler: ({ input }) => input.toUpperCase(),
}),
],
onPermissionRequest: (request) => {
permissionRequests.push(request);
return { kind: "approved" };
},
});
const assistantMessage = await session.sendAndWait({
prompt: "Use encrypt_string to encrypt this string: Hello",
});
expect(assistantMessage?.data.content).toContain("HELLO");
// Should have received a custom-tool permission request
const customToolRequests = permissionRequests.filter((req) => req.kind === "custom-tool");
expect(customToolRequests.length).toBeGreaterThan(0);
expect(customToolRequests[0].toolName).toBe("encrypt_string");
});
it("skipPermission sent in tool definition", async () => {
let didRunPermissionRequest = false;
const session = await client.createSession({
onPermissionRequest: () => {
didRunPermissionRequest = true;
return { kind: "no-result" };
},
tools: [
defineTool("safe_lookup", {
description: "A safe lookup that skips permission",
parameters: z.object({
id: z.string().describe("ID to look up"),
}),
handler: ({ id }) => `RESULT: ${id}`,
skipPermission: true,
}),
],
});
const assistantMessage = await session.sendAndWait({
prompt: "Use safe_lookup to look up 'test123'",
});
expect(assistantMessage?.data.content).toContain("RESULT: test123");
expect(didRunPermissionRequest).toBe(false);
});
it("overrides built-in tool with custom tool", async () => {
const session = await client.createSession({
onPermissionRequest: approveAll,
tools: [
defineTool("grep", {
description: "A custom grep implementation that overrides the built-in",
parameters: z.object({
query: z.string().describe("Search query"),
}),
handler: ({ query }) => `CUSTOM_GREP_RESULT: ${query}`,
overridesBuiltInTool: true,
}),
],
});
const assistantMessage = await session.sendAndWait({
prompt: "Use grep to search for the word 'hello'",
});
expect(assistantMessage?.data.content).toContain("CUSTOM_GREP_RESULT");
});
it("denies custom tool when permission denied", async () => {
let toolHandlerCalled = false;
const session = await client.createSession({
tools: [
defineTool("encrypt_string", {
description: "Encrypts a string",
parameters: z.object({
input: z.string().describe("String to encrypt"),
}),
handler: ({ input }) => {
toolHandlerCalled = true;
return input.toUpperCase();
},
}),
],
onPermissionRequest: () => {
return { kind: "denied-interactively-by-user" };
},
});
await session.sendAndWait({
prompt: "Use encrypt_string to encrypt this string: Hello",
});
// The tool handler should NOT have been called since permission was denied
expect(toolHandlerCalled).toBe(false);
});
});