Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 42 additions & 120 deletions nodejs/examples/basic-example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<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 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<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.destroy();
await client.stop();
console.log("✅ Done!");
Comment on lines +25 to +46
Copy link

Copilot AI Jan 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a try-finally block to ensure cleanup (session.destroy and client.stop) occurs even when errors happen. While this matches the style in the README examples, adding error handling would make this a more robust example that users can safely copy for production use. At minimum, wrapping the code in a try-finally would ensure resources are properly cleaned up.

Suggested change
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!");
let session: any;
try {
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);
} finally {
if (session) {
await session.destroy();
}
await client.stop();
console.log("✅ Done!");
}

Copilot uses AI. Check for mistakes.
Loading