streamEvents
SUPPORT · function · Runtime value
Bridge a push-style producer to a pull-style async iterable with a one-slot rendezvous.
The producer must await every emit call before emitting again; concurrent emissions are rejected rather than buffered. The slot is released only after the consumer receives the event, so backpressure is exact.
Import
import { streamEvents } from '@jangts/agent-core/support';import { streamEvents } from '@jangts/agent-core/support';Other public entry points:@jangts/agent-core/support.
Definition
Type parameters
| Type parameter | Constraint | Default type |
|---|---|---|
Event | — | — |
Call
streamEvents<Event>(operation: (emit: (event: Event) => Promise<void>) => Promise<void>, { signal }: import("../structs/common.js").ExecutionContext): AsyncIterable<Event>Bridge a push-style producer to a pull-style async iterable with a one-slot rendezvous.
The producer must await every emit call before emitting again; concurrent emissions are rejected rather than buffered. The slot is released only after the consumer receives the event, so backpressure is exact.
| Type parameter | Constraint | Default type |
|---|---|---|
Event | — | — |
| Parameter | Type | Passing | Description |
|---|---|---|---|
operation | (emit: (event: Event) => Promise<void>) => Promise<void> | Required | — |
__1 | ExecutionContext | Required | — |
Fields of __1
| Field | Type | Required | Description |
|---|---|---|---|
logger | SubFlowLog | undefined | Optional | Read-only. Explicit optional recorder for this operation; never inferred from diagnostics. |
signal | AbortSignal | Required | Read-only. Non-optional cancellation signal; never undefined once execution has started. |
diagnostics | DiagnosticContext | undefined | Optional | Read-only. Diagnostic scope of the enclosing call, when recording is enabled. |
Returns: AsyncIterable<Event>.
Examples
- SSE, JSON frames, and bounded event streams: Reuse transport mechanics while preserving cancellation and backpressure; protocol parsing and business validation remain separate.
Parse one SSE frame locally
Beginner · Node.js
import { Support } from '@jangts/agent-core';
const frame = Support.encodeSseEvent({ event: 'status', data: JSON.stringify({ phase: 'thinking' }) });
const body = new Response(frame).body!;
for await (const event of Support.readSse(body)) console.log(event.event, event.data);import { Support } from '@jangts/agent-core';
const frame = Support.encodeSseEvent({ event: 'status', data: JSON.stringify({ phase: 'thinking' }) });
const body = new Response(frame).body;
for await (const event of Support.readSse(body))
console.log(event.event, event.data);Producer waits for each event delivery
Configuration · Node.js
import { Support } from '@jangts/agent-core';
const signal = new AbortController().signal;
const stream = Support.streamEvents<{ phase: string }>(async emit => {
await emit({ phase: 'preparing' });
await emit({ phase: 'complete' });
}, { signal });
for await (const event of stream) console.log(event.phase);import { Support } from '@jangts/agent-core';
const signal = new AbortController().signal;
const stream = Support.streamEvents(async (emit) => {
await emit({ phase: 'preparing' });
await emit({ phase: 'complete' });
}, { signal });
for await (const event of stream)
console.log(event.phase);Explicitly limit JSON frame count and surrounding content
Extension · Node.js
import { Support } from '@jangts/agent-core';
const text = '{"kind":"reply","answer":"服务正常"}';
console.log(Support.scanJsonObjectFrames(text));
console.log(Support.parseJsonFrames(text, { maxFrames: 1, maxExtraClosings: 0 }));
const sent: string[] = [];
await Support.writeSseFrame({
writer: { write(frame) { sent.push(frame); return true; }, async waitForDrain() {} },
frame: Support.encodeSseEvent({ event: 'answer', data: text }),
}, { signal: new AbortController().signal });
console.log(sent);import { Support } from '@jangts/agent-core';
const text = '{"kind":"reply","answer":"服务正常"}';
console.log(Support.scanJsonObjectFrames(text));
console.log(Support.parseJsonFrames(text, { maxFrames: 1, maxExtraClosings: 0 }));
const sent = [];
await Support.writeSseFrame({
writer: { write(frame) { sent.push(frame); return true; }, async waitForDrain() { } },
frame: Support.encodeSseEvent({ event: 'answer', data: text }),
}, { signal: new AbortController().signal });
console.log(sent);