Reply events: text chunks and bubble boundaries
Turn an accepted reply into awaitable text/bubble events while preserving cancellation and delivery backpressure.
publishReplyEvents({text,delivery?},execution) returns AsyncIterable<AgentOutputEvent>. The defaults are chunkSize=240, intervalMs=0, bubblePauseMs=0, and maxDurationMs=3000; the total timing scales down to the cap, and cancellation stops further output. The internal REPLY_BUBBLE_BOUNDARY only separates bubbles and should not be displayed as body text.
serializeOutputEvents converts to a text stream, filters preview, and re-encodes bubble as the internal boundary. textOutputEvents wraps a plain string stream into text events. replyBubbleTexts only takes text/bubbles entries that are actually strings in the input. These are reply presentation contracts, not Core execution fact observation, and they do not prove the content passed business checks.
Progressive examples for the same business case
First output a reply that is already accepted
Beginner · Node.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);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
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');
}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
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);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);