Chat Stream
ngx-prompt-kit original — not part of ibelick/prompt-kit
readChatStream() reduces an SSE chat stream — tokens, tool calls/results, done/error — into handler callbacks and resolves with the terminal result. You supply an adapt() from your backend's payload shape, so it works over any tool-calling backend.
Examples
Reduce a stream
Runs readChatStream() over a simulated HttpClient event stream: tokens accumulate, tool calls/results are recorded, and the done payload resolves the promise.
import { readChatStream } from 'ngx-prompt-kit/streaming';
const result = await readChatStream(events$, (data) => {
const p = JSON.parse(data);
if (p.type === 'chunk') return { kind: 'token', text: p.text };
if (p.type === 'done') return { kind: 'done', result: p.result };
return null;
}, { onToken: (t) => stream.append(t) });Installation
readChatStream() ships with the streaming utility.
ng generate ngx-prompt-kit:streamingStopping a stream
readChatStream() takes an optional AbortSignal after the handlers. Aborting it cancels the request and rejects with an AbortError; check for it with isAbortError() so a stop button (e.g. pk-composer's stopped output) isn't reported as a failure.
import { isAbortError, readChatStream } from 'ngx-prompt-kit/streaming';
const controller = new AbortController();
// <pk-composer [busy]="busy()" (stopped)="controller.abort()" />
try {
const result = await readChatStream(events$, adapt, handlers, controller.signal);
} catch (error) {
if (!isAbortError(error)) throw error; // stopped on purpose: keep the partial reply
}