Skip to content
English

SseWriter ​

SUPPORT · interface · 类型合同

Destination that reports backpressure instead of buffering without bound.

导入 ​

ts
import type { SseWriter } from '@jangts/agent-core/sse';
js
/** @typedef {import('@jangts/agent-core/sse').SseWriter} SseWriter */

其他公开入口:@jangts/agent-core、@jangts/agent-core/types、@jangts/agent-core/structs、@jangts/agent-core/sse、@jangts/agent-core/support。

定义 ​

ts
interface SseWriter

字段结构 ​

字段类型必需说明
write(frame: string) => boolean必需Writes one encoded frame; returns false when the consumer needs a drain wait.
waitForDrain(signal?: AbortSignal) => Promise<unknown>必需Resolves once the destination can accept more data, or rejects when signal aborts.

成员目录 ​

成员用途
writeWrites one encoded frame; returns false when the consumer needs a drain wait.
waitForDrainResolves once the destination can accept more data, or rejects when signal aborts.

成员详情 ​

write ​

Writes one encoded frame; returns false when the consumer needs a drain wait.

ts
SseWriter.write(frame: string): boolean

Writes one encoded frame; returns false when the consumer needs a drain wait.

参数类型传递说明
framestring必需—

返回: boolean。

waitForDrain ​

Resolves once the destination can accept more data, or rejects when signal aborts.

ts
SseWriter.waitForDrain(signal?: AbortSignal): Promise<unknown>

Resolves once the destination can accept more data, or rejects when signal aborts.

参数类型传递说明
signalAbortSignal | undefined可省略—

返回: Promise<unknown>。

等待后的值: unknown。

使用示例 ​

在本地解析一帧 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);

相关策略源码 ​

接口参考、使用示例与开发指南。