Streaming Chat
The SDK provides an async iterator pattern for consuming SSE streams — the most ergonomic way to handle real-time AI responses.
Async Iterator (recommended)
import { Senda } from '@senda/sdk';
const senda = new Senda({
apiKey: 'sk_live_YOUR_KEY',
baseUrl: 'https://senda.telar.ai/api'
});
for await (const event of senda.chat.stream('Explain quantum computing')) {
switch (event.type) {
case 'text':
process.stdout.write(event.text!);
break;
case 'component':
console.log('UI Widget:', event.component_name, event.props);
break;
case 'agent_info':
console.log('Routed to agent:', event.agent_id);
break;
case 'done':
console.log('\n[Stream complete]');
break;
}
}
Non-Streaming (simple)
If you don't need real-time display:
const response = await senda.chat.send('What is 2+2?');
console.log(response); // "2 + 2 = 4"
Continue a Conversation
Pass a chatId to maintain context:
const chatId = 'chat_abc123';
const response1 = await senda.chat.send('My name is Eddie', { chatId });
const response2 = await senda.chat.send('What is my name?', { chatId });
// → "Your name is Eddie"
Event Types
| Type | Fields | Description |
|---|---|---|
text | text | Streaming text token |
agent_info | agent_id | Which agent was selected |
component | component_name, props | Generative UI widget data |
done | — | Stream complete |
error | text | Error message |