writeSseFrame
SUPPORT · function · 运行时值
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.
导入
ts
import { writeSseFrame } from '@jangts/agent-core/sse';js
import { writeSseFrame } from '@jangts/agent-core/sse';其他公开入口:@jangts/agent-core、@jangts/agent-core/types、@jangts/agent-core/sse、@jangts/agent-core/support。
定义
调用
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.
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
__0 | { writer: SseWriter; frame: string; } | 必需 | — |
__1 | ExecutionContext | 必需 | — |
__0 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
writer | SseWriter | 必需 | 见类型定义及调用场景。 |
frame | string | 必需 | 见类型定义及调用场景。 |
__1 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
logger | SubFlowLog | undefined | 可省略 | 只读。Explicit optional recorder for this operation; never inferred from diagnostics. |
signal | AbortSignal | 必需 | 只读。Non-optional cancellation signal; never undefined once execution has started. |
diagnostics | DiagnosticContext | undefined | 可省略 | 只读。Diagnostic scope of the enclosing call, when recording is enabled. |
返回: Promise<void>。
等待后的值: void。
使用示例
- SSE、JSON 帧与有界事件流:复用传输机制,保持取消和背压;协议解析与业务校验仍各自负责。
在本地解析一帧 SSE
入门 · 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);生产者等待每一次事件交付
配置 · 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);显式限制 JSON 帧数量和外围内容
扩展 · 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);