-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathbasic-example.ts
More file actions
46 lines (37 loc) · 1.81 KB
/
basic-example.ts
File metadata and controls
46 lines (37 loc) · 1.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
import { z } from "zod";
import { CopilotClient, defineTool } from "../src/index.js";
console.log("🚀 Starting Copilot SDK Example\n");
const facts: Record<string, string> = {
javascript: "JavaScript was created in 10 days by Brendan Eich in 1995.",
node: "Node.js lets you run JavaScript outside the browser using the V8 engine.",
};
const lookupFactTool = defineTool("lookup_fact", {
description: "Returns a fun fact about a given topic.",
parameters: z.object({
topic: z.string().describe("Topic to look up (e.g. 'javascript', 'node')"),
}),
handler: ({ topic }) => facts[topic.toLowerCase()] ?? `No fact stored for ${topic}.`,
});
// Create client - will auto-start CLI server (searches PATH for "copilot")
const client = new CopilotClient({ logLevel: "info" });
const session = await client.createSession({ tools: [lookupFactTool] });
console.log(`✅ Session created: ${session.sessionId}\n`);
// Listen to events
session.on((event) => {
console.log(`📢 Event [${event.type}]:`, JSON.stringify(event.data, null, 2));
});
// Send a simple message
console.log("💬 Sending message...");
const result1 = await session.sendAndWait({ prompt: "Tell me 2+2" });
console.log("📝 Response:", result1?.data.content);
// Send another message that uses the tool
console.log("💬 Sending follow-up message...");
const result2 = await session.sendAndWait({ prompt: "Use lookup_fact to tell me about 'node'" });
console.log("📝 Response:", result2?.data.content);
// Clean up
await session.disconnect();
await client.stop();
console.log("✅ Done!");