import { ChuckyClient, tool, getAssistantText } from '@chucky.cloud/sdk';
// Create client
const client = new ChuckyClient({
token: process.env.CHUCKY_TOKEN,
debug: true,
});
// Define a tool
const calculatorTool = tool(
'calculate',
'Perform math calculations',
{
type: 'object',
properties: {
expression: { type: 'string' },
},
required: ['expression'],
},
async ({ expression }) => {
const result = eval(expression); // Use a proper math lib in production
return { content: [{ type: 'text', text: String(result) }] };
}
);
// Create a session with tools
const session = client.createSession({
model: 'claude-sonnet-4-5-20250929',
systemPrompt: 'You are a math tutor.',
tools: [calculatorTool],
});
// Send message and stream response
await session.send('What is 15% of 230?');
for await (const msg of session.stream()) {
if (msg.type === 'assistant') {
const text = getAssistantText(msg);
if (text) process.stdout.write(text);
}
if (msg.type === 'result' && msg.subtype === 'success') {
console.log('\nCost:', msg.total_cost_usd);
}
}
// Multi-turn conversation
await session.send('Explain how you calculated that');
for await (const msg of session.stream()) {
if (msg.type === 'assistant') {
console.log(getAssistantText(msg));
}
}
// Clean up
session.close();
client.close();