Stream responses with messageStream()
Stream partial tokens for responsive UIs or progressive server responses. The async generator yields successive string fragments.
Signature
1messageStream(content: string, useContext?: boolean, saveToMessageHistory?: boolean): AsyncGenerator<string>
Basic streaming
1let full = '';
2
3for await (const chunk of agent.messageStream('Explain vector embeddings simply')) {
4 full += chunk;
5}
6
7console.log(full);
Attach to in-flight assistant message
You can pre-create an assistant placeholder and append as chunks arrive.
1const assistantIndex = agent.addMessage('', 'assistant');
2
3let full = '';
4
5for await (const part of agent.messageStream('Draft tweet about launch', true, false)) {
6 full += part;
7 agent.modifyMessage(assistantIndex, full);
8}
Error handling
Wrap iteration in try/catch to capture mid-stream failures (network resets, quota, etc.). Partial content collected so far can still be rendered.
1let accumulation = '';
2
3try {
4 for await (const part of agent.messageStream('Long answer about reliability patterns')) {
5 accumulation += part;
6 }
7} catch (e) {
8 console.error('Stream failed after partial:', accumulation);
9}