serializeOutputEvents
KIT · function · Runtime value
Serialize the event protocol for callers using a text stream.
preview events are dropped because they are not final text; bubbles become the boundary character again, so a consumer can re-split the stream.
Import
ts
import { serializeOutputEvents } from '@jangts/agent-core/events';js
import { serializeOutputEvents } from '@jangts/agent-core/events';Other public entry points:@jangts/agent-core, @jangts/agent-core/types, @jangts/agent-core/events, @jangts/agent-core/kits/presentation, @jangts/agent-core/kits.
Definition
Call
ts
serializeOutputEvents(events: AsyncIterable<AgentOutputEvent>): AsyncIterable<string>Serialize the event protocol for callers using a text stream.
preview events are dropped because they are not final text; bubbles become the boundary character again, so a consumer can re-split the stream.
| Parameter | Type | Passing | Description |
|---|---|---|---|
events | AsyncIterable<AgentOutputEvent> | Required | — |
Returns: AsyncIterable<string>.
Examples
- Reply events: text chunks and bubble boundaries: Turn an accepted reply into awaitable text/bubble events while preserving cancellation and delivery backpressure.
First output a reply that is already accepted
Beginner · Node.js
ts
import { Presentation } from '@jangts/agent-core/kits/presentation';
const context = { signal: new AbortController().signal };
for await (const event of Presentation.publishReplyEvents({ text: '这是已经接受的答复。' }, context)) console.log(event);js
import { Presentation } from '@jangts/agent-core/kits/presentation';
const context = { signal: new AbortController().signal };
for await (const event of Presentation.publishReplyEvents({ text: '这是已经接受的答复。' }, context))
console.log(event);Add chunking and bubbles to the same event entry
Configuration · Node.js
ts
import { Presentation } from '@jangts/agent-core/kits/presentation';
const text = Presentation.replyBubbleTexts({ text: '先说明定义。', bubbles: ['再解释输入输出。'] }).join(Presentation.REPLY_BUBBLE_BOUNDARY);
const events = Presentation.publishReplyEvents({ text, delivery: { chunkSize: 6, intervalMs: 10, bubblePauseMs: 30, maxDurationMs: 1000 } }, { signal: new AbortController().signal });
for await (const event of events) {
if (event.type === 'text') process.stdout.write(event.text);
else if (event.type === 'bubble') process.stdout.write('\n');
}js
import { Presentation } from '@jangts/agent-core/kits/presentation';
const text = Presentation.replyBubbleTexts({ text: '先说明定义。', bubbles: ['再解释输入输出。'] }).join(Presentation.REPLY_BUBBLE_BOUNDARY);
const events = Presentation.publishReplyEvents({ text, delivery: { chunkSize: 6, intervalMs: 10, bubblePauseMs: 30, maxDurationMs: 1000 } }, { signal: new AbortController().signal });
for await (const event of events) {
if (event.type === 'text')
process.stdout.write(event.text);
else if (event.type === 'bubble')
process.stdout.write('\n');
}Convert an existing text stream within the same contract
Extension · Node.js
ts
import { Presentation } from '@jangts/agent-core/kits/presentation';
async function* chunks() { yield '已接受的'; yield '正文。'; }
const events = Presentation.textOutputEvents(chunks());
for await (const text of Presentation.serializeOutputEvents(events)) console.log(text);js
import { Presentation } from '@jangts/agent-core/kits/presentation';
async function* chunks() { yield '已接受的'; yield '正文。'; }
const events = Presentation.textOutputEvents(chunks());
for await (const text of Presentation.serializeOutputEvents(events))
console.log(text);