-
Notifications
You must be signed in to change notification settings - Fork 631
Simplify Node.js example #221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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
ToolResultObjectstructures - 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.
| 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!"); |
Copilot
AI
Jan 27, 2026
There was a problem hiding this comment.
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.
| 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!"); | |
| } |
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. SummaryThis PR simplifies the Node.js basic example by adopting modern API patterns that are already available and consistent across all SDKs:
Minor Documentation Note (Non-blocking)Node.js has a standalone Recommendation: Approve and merge. The changes improve code quality and demonstrate best practices without creating cross-SDK inconsistencies.
|
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.