|
2 | 2 | * Copyright (c) Microsoft Corporation. All rights reserved. |
3 | 3 | *--------------------------------------------------------------------------------------------*/ |
4 | 4 |
|
5 | | -/** |
6 | | - * Example: Basic usage of the Copilot SDK |
7 | | - */ |
8 | | - |
9 | | -import { existsSync } from "node:fs"; |
10 | | -import { CopilotClient, type Tool } from "../src/index.js"; |
11 | | - |
12 | | -async function main() { |
13 | | - console.log("🚀 Starting Copilot SDK Example\n"); |
14 | | - |
15 | | - // Create client - will auto-start CLI server |
16 | | - const cliCommand = process.env.COPILOT_CLI_PATH?.trim(); |
17 | | - let cliPath: string | undefined; |
18 | | - let cliArgs: string[] | undefined; |
19 | | - |
20 | | - if (cliCommand) { |
21 | | - if (!cliCommand.includes(" ") || existsSync(cliCommand)) { |
22 | | - cliPath = cliCommand; |
23 | | - } else { |
24 | | - const tokens = cliCommand |
25 | | - .match(/(?:[^\s"]+|"[^"]*")+/g) |
26 | | - ?.map((token) => token.replace(/^"(.*)"$/, "$1")); |
27 | | - if (tokens && tokens.length > 0) { |
28 | | - cliPath = tokens[0]; |
29 | | - if (tokens.length > 1) { |
30 | | - cliArgs = tokens.slice(1); |
31 | | - } |
32 | | - } |
33 | | - } |
34 | | - } |
35 | | - |
36 | | - const client = new CopilotClient({ |
37 | | - logLevel: "info", |
38 | | - ...(cliPath ? { cliPath } : {}), |
39 | | - ...(cliArgs && cliArgs.length > 0 ? { cliArgs } : {}), |
40 | | - }); |
41 | | - |
42 | | - try { |
43 | | - const facts: Record<string, string> = { |
44 | | - javascript: "JavaScript was created in 10 days by Brendan Eich in 1995.", |
45 | | - node: "Node.js lets you run JavaScript outside the browser using the V8 engine.", |
46 | | - }; |
47 | | - |
48 | | - const tools: Tool[] = [ |
49 | | - { |
50 | | - name: "lookup_fact", |
51 | | - description: "Returns a fun fact about a given topic.", |
52 | | - parameters: { |
53 | | - type: "object", |
54 | | - properties: { |
55 | | - topic: { |
56 | | - type: "string", |
57 | | - description: "Topic to look up (e.g. 'javascript', 'node')", |
58 | | - }, |
59 | | - }, |
60 | | - required: ["topic"], |
61 | | - }, |
62 | | - handler: async ({ arguments: args }) => { |
63 | | - const topic = String((args as { topic: string }).topic || "").toLowerCase(); |
64 | | - const fact = facts[topic]; |
65 | | - if (!fact) { |
66 | | - return { |
67 | | - textResultForLlm: `No fact stored for ${topic}.`, |
68 | | - resultType: "failure", |
69 | | - sessionLog: `lookup_fact: missing topic ${topic}`, |
70 | | - toolTelemetry: {}, |
71 | | - }; |
72 | | - } |
73 | | - |
74 | | - return { |
75 | | - textResultForLlm: fact, |
76 | | - resultType: "success", |
77 | | - sessionLog: `lookup_fact: served ${topic}`, |
78 | | - toolTelemetry: {}, |
79 | | - }; |
80 | | - }, |
81 | | - }, |
82 | | - ]; |
83 | | - |
84 | | - // Create a session |
85 | | - console.log("📝 Creating session..."); |
86 | | - const session = await client.createSession({ |
87 | | - model: "gpt-5", |
88 | | - tools, |
89 | | - }); |
90 | | - console.log(`✅ Session created: ${session.sessionId}\n`); |
91 | | - |
92 | | - // Listen to events |
93 | | - session.on((event) => { |
94 | | - console.log(`📢 Event [${event.type}]:`, JSON.stringify(event.data, null, 2)); |
95 | | - }); |
96 | | - |
97 | | - // Send a simple message |
98 | | - console.log("💬 Sending message..."); |
99 | | - await session.sendAndWait({ |
100 | | - prompt: "You can call the lookup_fact tool. First, please tell me 2+2.", |
101 | | - }); |
102 | | - console.log("✅ Message completed\n"); |
103 | | - |
104 | | - // Send another message |
105 | | - console.log("\n💬 Sending follow-up message..."); |
106 | | - await session.sendAndWait({ |
107 | | - prompt: "Great. Now use lookup_fact to tell me something about Node.js.", |
108 | | - }); |
109 | | - console.log("✅ Follow-up completed\n"); |
110 | | - |
111 | | - // Clean up |
112 | | - console.log("\n🧹 Cleaning up..."); |
113 | | - await session.destroy(); |
114 | | - await client.stop(); |
115 | | - |
116 | | - console.log("✅ Done!"); |
117 | | - } catch (error) { |
118 | | - console.error("❌ Error:", error); |
119 | | - await client.stop(); |
120 | | - process.exit(1); |
121 | | - } |
122 | | -} |
123 | | - |
124 | | -main(); |
| 5 | +import { z } from "zod"; |
| 6 | +import { CopilotClient, defineTool } from "../src/index.js"; |
| 7 | + |
| 8 | +console.log("🚀 Starting Copilot SDK Example\n"); |
| 9 | + |
| 10 | +const facts: Record<string, string> = { |
| 11 | + javascript: "JavaScript was created in 10 days by Brendan Eich in 1995.", |
| 12 | + node: "Node.js lets you run JavaScript outside the browser using the V8 engine.", |
| 13 | +}; |
| 14 | + |
| 15 | +const lookupFactTool = defineTool("lookup_fact", { |
| 16 | + description: "Returns a fun fact about a given topic.", |
| 17 | + parameters: z.object({ |
| 18 | + topic: z.string().describe("Topic to look up (e.g. 'javascript', 'node')"), |
| 19 | + }), |
| 20 | + handler: ({ topic }) => facts[topic.toLowerCase()] ?? `No fact stored for ${topic}.`, |
| 21 | +}); |
| 22 | + |
| 23 | +// Create client - will auto-start CLI server (searches PATH for "copilot") |
| 24 | +const client = new CopilotClient({ logLevel: "info" }); |
| 25 | +const session = await client.createSession({ tools: [lookupFactTool] }); |
| 26 | +console.log(`✅ Session created: ${session.sessionId}\n`); |
| 27 | + |
| 28 | +// Listen to events |
| 29 | +session.on((event) => { |
| 30 | + console.log(`📢 Event [${event.type}]:`, JSON.stringify(event.data, null, 2)); |
| 31 | +}); |
| 32 | + |
| 33 | +// Send a simple message |
| 34 | +console.log("💬 Sending message..."); |
| 35 | +const result1 = await session.sendAndWait({ prompt: "Tell me 2+2" }); |
| 36 | +console.log("📝 Response:", result1?.data.content); |
| 37 | + |
| 38 | +// Send another message that uses the tool |
| 39 | +console.log("💬 Sending follow-up message..."); |
| 40 | +const result2 = await session.sendAndWait({ prompt: "Use lookup_fact to tell me about 'node'" }); |
| 41 | +console.log("📝 Response:", result2?.data.content); |
| 42 | + |
| 43 | +// Clean up |
| 44 | +await session.destroy(); |
| 45 | +await client.stop(); |
| 46 | +console.log("✅ Done!"); |
0 commit comments