Skip to content

Conversation

@SteveSandersonMS
Copy link
Contributor

@SteveSandersonMS SteveSandersonMS commented Jan 27, 2026

Reduces the Node.js "simple" example from 124 lines to 46 lines just by using the updated APIs and eliminating some unnecessary CLI arg parsing logic.

We don't necessarily need to have this example, but if we are going to have it, we should make it use current APIs and look nice.

@SteveSandersonMS SteveSandersonMS requested a review from a team as a code owner January 27, 2026 10:06
Copilot AI review requested due to automatic review settings January 27, 2026 10:06
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR simplifies the Node.js basic example from 124 lines to 44 lines by adopting the modern defineTool API with Zod schemas and removing unnecessary CLI argument parsing logic. The streamlined example demonstrates core SDK functionality in a more concise and readable format.

Changes:

  • Replaced manual tool definition with defineTool() helper and Zod schema validation
  • Removed complex CLI path parsing logic in favor of simple default client initialization
  • Simplified tool handler to return strings directly instead of verbose ToolResultObject structures
  • Removed async main() wrapper in favor of top-level await
  • Removed the non-existent model: "gpt-5" parameter from session creation

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +25 to +46
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!");
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.
@github-actions
Copy link

Cross-SDK Consistency Review ✅

I've reviewed this PR for cross-language consistency. No consistency issues found - this change maintains alignment with other SDK implementations.

Summary

This PR simplifies the Node.js basic example by adopting modern API patterns that are already available and consistent across all SDKs:

  • defineTool() helper: All SDKs provide similar utilities:

    • Node.js: defineTool() with Zod schemas
    • Python: define_tool() with Pydantic models
    • Go: DefineTool() with typed structs
    • .NET: AIFunctionFactory.Create() (Microsoft.Extensions.AI pattern)
  • Simplified tool handlers: The pattern of returning strings directly (auto-wrapped in ToolResult) is supported across all SDKs

  • Modern async patterns: Using sendAndWait() and top-level await matches current best practices shown in the Node.js README

Minor Documentation Note (Non-blocking)

Node.js has a standalone examples/ directory with this basic example, while Python, Go, and .NET only have cookbook recipes. This isn't an inconsistency requiring action - the cookbook structure is consistent across all languages. The Node.js example serves as a quick-start reference that complements the cookbook, and this PR improves its quality by demonstrating current API patterns.

Recommendation: Approve and merge. The changes improve code quality and demonstrate best practices without creating cross-SDK inconsistencies.

AI generated by SDK Consistency Review Agent

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants