SseEvent
CORE · interface · 类型合同
One dispatched SSE event, assembled from all lines up to a blank line.
导入
ts
import type { SseEvent } from '@jangts/agent-core/sse';js
/** @typedef {import('@jangts/agent-core/sse').SseEvent} SseEvent */其他公开入口:@jangts/agent-core、@jangts/agent-core/types、@jangts/agent-core/structs、@jangts/agent-core/sse、@jangts/agent-core/support。
定义
ts
interface SseEvent字段结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
event | string | 必需 | Event name from the event: field; message when the stream omits it. |
data | string | 必需 | Payload from the data: fields, joined with \n and without a trailing newline. |
使用示例
- SSE、JSON 帧与有界事件流:复用传输机制,保持取消和背压;协议解析与业务校验仍各自负责。
- Support:推荐用法的聚合入口:Support 聚合推荐的构造、策略与同源配置生成;Core 运行不依赖这个命名空间。
在本地解析一帧 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);