Durable streaming.
Resume an interrupted stream from the last received event.
Durable streaming lets a client disconnect mid-stream and resume from where it left off. The server buffers events in a Redis stream, keyed by a streamId advertised at stream start.
Durable streaming lifecycle
- Start a stream —
chat.stream()emits aresponse.createdevent with astreamId. - Persist the
streamIdandconversationIdimmediately. - If disconnected, call
chat.activeStream(conversationId)to check if the stream is still live. - Call
chat.resume({ streamId, lastEventId })to replay buffered events fromlastEventIdonwards.
chat.resume(params, options?)
Resumes a previously detached stream by replaying buffered events from lastEventId. Returns an async generator of MatildaChatStreamEvent.
for await (const event of client.chat.resume({
streamId: savedStreamId,
lastEventId: savedLastEventId,
})) {
if (event.type === 'response.output_text.delta') {
process.stdout.write(event.delta);
}
}ChatResumeParams
| Field | type | description |
|---|---|---|
| streamId | string | The stream ID from response.created. |
| lastEventId | string | The last Redis stream entry ID received. Omit to replay from the start. |
chat.activeStream(conversationId, options?)
Checks whether a conversation has an active stream.
const result = await client.chat.activeStream('conv-123');
// { stream_id: 'abc-123' | null, status: 'active' | 'done' | 'error' | null }Returns Promise<ActiveStreamLookup>:
interface ActiveStreamLookup {
stream_id: string | null;
status: 'active' | 'done' | 'error' | null;
}chat.notifyOnCompletion(streamId, enabled?, options?)
Request a push notification when a backgrounded stream completes.
await client.chat.notifyOnCompletion(streamId, true);| Field | type | description |
|---|---|---|
| streamId | string | The stream to watch. |
| enabled | boolean | Enable or disable the notification. Defaults to true. |
Returns Promise<{ status: string }>.
Full resume example
import Matilda from '@maincode-ai/matilda-client-sdk';
const client = new Matilda({
baseUrl: 'https://matilda.maincode.com/api',
accessToken: process.env.MATILDA_ACCESS_TOKEN!,
});
let streamId: string | null = null;
let lastEventId: string | undefined;
// Start streaming
for await (const event of client.chat.stream({
input: 'Write a long essay about Australia.',
conversationId: 'conv-123',
})) {
if (event.type === 'response.created') {
streamId = event.streamId;
}
if (event.type === 'response.cursor') {
lastEventId = event.lastEventId;
}
if (event.type === 'response.output_text.delta') {
process.stdout.write(event.delta);
}
}
// Later — check if the stream is still active, then resume
const active = await client.chat.activeStream('conv-123');
if (active.status === 'active' && streamId) {
console.log('\n--- Resuming ---');
for await (const event of client.chat.resume({ streamId, lastEventId })) {
if (event.type === 'response.output_text.delta') {
process.stdout.write(event.delta);
}
}
}