Skip to content
中文

writeSseFrame ​

SUPPORT · function · Runtime value

Write one encoded frame and wait for drain when the writer reports backpressure.

The signal is re-checked around the write and the drain wait, so cancellation does not leave a pending write considered successful.

Import ​

ts
import { writeSseFrame } from '@jangts/agent-core/sse';
js
import { writeSseFrame } from '@jangts/agent-core/sse';

Other public entry points:@jangts/agent-core, @jangts/agent-core/types, @jangts/agent-core/sse, @jangts/agent-core/support.

Definition ​

Call ​

ts
writeSseFrame({ writer, frame }: { writer: SseWriter; frame: string; }, { signal }: import("../../structs/common.js").ExecutionContext): Promise<void>

Write one encoded frame and wait for drain when the writer reports backpressure.

The signal is re-checked around the write and the drain wait, so cancellation does not leave a pending write considered successful.

ParameterTypePassingDescription
__0{ writer: SseWriter; frame: string; }Required—
__1ExecutionContextRequired—
Fields of __0
FieldTypeRequiredDescription
writerSseWriterRequiredSee the type definition and usage context.
framestringRequiredSee the type definition and usage context.
Fields of __1
FieldTypeRequiredDescription
loggerSubFlowLog | undefinedOptionalRead-only. Explicit optional recorder for this operation; never inferred from diagnostics.
signalAbortSignalRequiredRead-only. Non-optional cancellation signal; never undefined once execution has started.
diagnosticsDiagnosticContext | undefinedOptionalRead-only. Diagnostic scope of the enclosing call, when recording is enabled.

Returns: Promise<void>.

Awaited value: void.

Examples ​

Parse one SSE frame locally ​

Beginner · Node.js

ts
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);
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);

Producer waits for each event delivery ​

Configuration · Node.js

ts
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);
js
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

ts
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);
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 = [];
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);

API reference, usage examples and developer guides.