/*--------------------------------------------------------------------------------------------- * 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..."); const messageId = await session.send({ prompt: "You can call the lookup_fact tool. First, please tell me 2+2.", }); console.log(`โœ… Message sent: ${messageId}\n`); // Wait a bit for events to arrive await new Promise((resolve) => setTimeout(resolve, 5000)); // Send another message console.log("\n๐Ÿ’ฌ Sending follow-up message..."); await session.send({ prompt: "Great. Now use lookup_fact to tell me something about Node.js.", }); // Wait for response await new Promise((resolve) => setTimeout(resolve, 5000)); // 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();