-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathtool_results.test.ts
More file actions
155 lines (134 loc) · 6.2 KB
/
tool_results.test.ts
File metadata and controls
155 lines (134 loc) · 6.2 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
import { describe, expect, it } from "vitest";
import { z } from "zod";
import type { SessionEvent, ToolResultObject } from "../../src/index.js";
import { approveAll, defineTool } from "../../src/index.js";
import { createSdkTestContext } from "./harness/sdkTestContext";
describe("Tool Results", async () => {
const { copilotClient: client, openAiEndpoint } = 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.disconnect();
});
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.disconnect();
});
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.disconnect();
});
it("should preserve toolTelemetry and not stringify structured results for LLM", async () => {
const session = await client.createSession({
onPermissionRequest: approveAll,
tools: [
defineTool("analyze_code", {
description: "Analyzes code for issues",
parameters: z.object({
file: z.string(),
}),
handler: ({ file }): ToolResultObject => ({
textResultForLlm: `Analysis of ${file}: no issues found`,
resultType: "success",
toolTelemetry: {
metrics: { analysisTimeMs: 150 },
properties: { analyzer: "eslint" },
},
}),
}),
],
});
const events: SessionEvent[] = [];
session.on((event) => events.push(event));
const assistantMessage = await session.sendAndWait({
prompt: "Analyze the file main.ts for issues.",
});
expect(assistantMessage?.data.content).toMatch(/no issues/i);
// Verify the LLM received just textResultForLlm, not stringified JSON
const traffic = await openAiEndpoint.getExchanges();
const lastConversation = traffic[traffic.length - 1]!;
const toolResults = lastConversation.request.messages.filter(
(m: { role: string }) => m.role === "tool"
);
expect(toolResults.length).toBe(1);
expect(toolResults[0]!.content).not.toContain("toolTelemetry");
expect(toolResults[0]!.content).not.toContain("resultType");
// Verify tool.execution_complete event fires for this tool call
const toolCompletes = events.filter((e) => e.type === "tool.execution_complete");
expect(toolCompletes.length).toBeGreaterThanOrEqual(1);
const completeEvent = toolCompletes[0]!;
expect(completeEvent.data.success).toBe(true);
// When the server preserves the structured result, toolTelemetry should
// be present and non-empty (not the {} that results from stringification).
if (completeEvent.data.toolTelemetry) {
expect(Object.keys(completeEvent.data.toolTelemetry).length).toBeGreaterThan(0);
}
await session.disconnect();
});
});