diff --git a/nodejs/examples/basic-example.ts b/nodejs/examples/basic-example.ts index 2dc47d6d..b0b99313 100644 --- a/nodejs/examples/basic-example.ts +++ b/nodejs/examples/basic-example.ts @@ -2,123 +2,45 @@ * Copyright (c) Microsoft Corporation. All rights reserved. *--------------------------------------------------------------------------------------------*/ -/** - * Example: Basic usage of the Copilot SDK - */ - -import { existsSync } from "node:fs"; -import { CopilotClient, type Tool } from "../src/index.js"; - -async function main() { - console.log("๐Ÿš€ Starting Copilot SDK Example\n"); - - // Create client - will auto-start CLI server - const cliCommand = process.env.COPILOT_CLI_PATH?.trim(); - let cliPath: string | undefined; - let cliArgs: string[] | undefined; - - if (cliCommand) { - if (!cliCommand.includes(" ") || existsSync(cliCommand)) { - cliPath = cliCommand; - } else { - const tokens = cliCommand - .match(/(?:[^\s"]+|"[^"]*")+/g) - ?.map((token) => token.replace(/^"(.*)"$/, "$1")); - if (tokens && tokens.length > 0) { - cliPath = tokens[0]; - if (tokens.length > 1) { - cliArgs = tokens.slice(1); - } - } - } - } - - const client = new CopilotClient({ - logLevel: "info", - ...(cliPath ? { cliPath } : {}), - ...(cliArgs && cliArgs.length > 0 ? { cliArgs } : {}), - }); - - try { - const facts: Record = { - 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 tools: Tool[] = [ - { - name: "lookup_fact", - description: "Returns a fun fact about a given topic.", - parameters: { - type: "object", - properties: { - topic: { - type: "string", - description: "Topic to look up (e.g. 'javascript', 'node')", - }, - }, - required: ["topic"], - }, - handler: async ({ arguments: args }) => { - const topic = String((args as { topic: string }).topic || "").toLowerCase(); - const fact = facts[topic]; - if (!fact) { - return { - textResultForLlm: `No fact stored for ${topic}.`, - resultType: "failure", - sessionLog: `lookup_fact: missing topic ${topic}`, - toolTelemetry: {}, - }; - } - - return { - textResultForLlm: fact, - resultType: "success", - sessionLog: `lookup_fact: served ${topic}`, - toolTelemetry: {}, - }; - }, - }, - ]; - - // Create a session - console.log("๐Ÿ“ Creating session..."); - const session = await client.createSession({ - model: "gpt-5", - tools, - }); - 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..."); - await session.sendAndWait({ - prompt: "You can call the lookup_fact tool. First, please tell me 2+2.", - }); - console.log("โœ… Message completed\n"); - - // Send another message - console.log("\n๐Ÿ’ฌ Sending follow-up message..."); - await session.sendAndWait({ - prompt: "Great. Now use lookup_fact to tell me something about Node.js.", - }); - console.log("โœ… Follow-up completed\n"); - - // Clean up - console.log("\n๐Ÿงน Cleaning up..."); - await session.destroy(); - await client.stop(); - - console.log("โœ… Done!"); - } catch (error) { - console.error("โŒ Error:", error); - await client.stop(); - process.exit(1); - } -} - -main(); +import { z } from "zod"; +import { CopilotClient, defineTool } from "../src/index.js"; + +console.log("๐Ÿš€ Starting Copilot SDK Example\n"); + +const facts: Record = { + 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.destroy(); +await client.stop(); +console.log("โœ… Done!");