forked from github/copilot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtool_results.test.ts
More file actions
101 lines (87 loc) · 3.81 KB
/
Copy pathtool_results.test.ts
File metadata and controls
101 lines (87 loc) · 3.81 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
import { describe, expect, it } from "vitest";
import { z } from "zod";
import type { ToolResultObject } from "../../src/index.js";
import { approveAll, defineTool } from "../../src/index.js";
import { createSdkTestContext } from "./harness/sdkTestContext";
describe("Tool Results", async () => {
const { copilotClient: client } = await createSdkTestContext();
it("should handle structured ToolResultObject from custom tool", async () => {
const session = await client.createSession({
onPermissionRequest: approveAll,
tools: [
defineTool("get_weather", {
description: "Gets weather for a city",
parameters: z.object({
city: z.string(),
}),
handler: ({ city }): ToolResultObject => ({
textResultForLlm: `The weather in ${city} is sunny and 72°F`,
resultType: "success",
}),
}),
],
});
const assistantMessage = await session.sendAndWait({
prompt: "What's the weather in Paris?",
});
const content = assistantMessage?.data.content ?? "";
expect(content).toMatch(/sunny|72/i);
await session.destroy();
});
it("should handle tool result with failure resultType", async () => {
const session = await client.createSession({
onPermissionRequest: approveAll,
tools: [
defineTool("check_status", {
description: "Checks the status of a service",
handler: (): ToolResultObject => ({
textResultForLlm: "Service unavailable",
resultType: "failure",
error: "API timeout",
}),
}),
],
});
const assistantMessage = await session.sendAndWait({
prompt: "Check the status of the service using check_status. If it fails, say 'service is down'.",
});
const failureContent = assistantMessage?.data.content ?? "";
expect(failureContent).toMatch(/service is down/i);
await session.destroy();
});
it("should pass validated Zod parameters to tool handler", async () => {
const session = await client.createSession({
onPermissionRequest: approveAll,
tools: [
defineTool("calculate", {
description: "Calculates a math expression",
parameters: z.object({
operation: z.enum(["add", "subtract", "multiply"]),
a: z.number(),
b: z.number(),
}),
handler: ({ operation, a, b }) => {
expect(typeof a).toBe("number");
expect(typeof b).toBe("number");
switch (operation) {
case "add":
return String(a + b);
case "subtract":
return String(a - b);
case "multiply":
return String(a * b);
}
},
}),
],
});
const assistantMessage = await session.sendAndWait({
prompt: "Use calculate to add 17 and 25",
});
expect(assistantMessage?.data.content).toContain("42");
await session.destroy();
});
});