Skip to main content

Streaming and reconnect

The backend retains output events with attempt/sequence cursors. A browser observes text, tool phases, approvals, artifact/media references and run status. Disconnecting an observer does not stop its durable run.

import type { CortexRuntimeClient, RuntimeObservation } from '@cortexmemory/sdk/runtime';

export async function reconnect(
client: CortexRuntimeClient,
runId: string,
render: (observation: RuntimeObservation) => void,
onError: (error: unknown) => void,
) {
const handle = await client.attach(runId);
return client.watchObservation(handle, render, onError);
}

The returned function disconnects this observer. Explicit client.cancel(handle) requests run cancellation. Approval uses approveInput(handle) or the exact server RuntimeToolApproval tuple through approveTool; the browser never executes the tool itself.

Cursor resume includes the attempt number, so events from another attempt cannot be mistaken for already delivered output. Final-only output replaces canonical text rather than appending another copy of streamed deltas. Failed/cancelled status remains explicit.

The AI SDK 7 remote transport projects this lifecycle into useChat. Its disconnect() preserves backend work; stop() requests cancellation; state restores bounded references after fresh server authorization.

A response can finish before memory/graph stages. Keep the runtime watch when the host needs later stage updates. Close owned clients at view/session end. Clear protected displays on expiry, sign-out or revocation.

Authentication defaults to a 10-second bound; RPC observation defaults to 12 seconds. A timed-out mutation may have executed: recover its original request/run identity. Local timeout or cancellation does not prove provider terminality or a refund. See error handling.