Skip to content
English

答复事件:文本分片和气泡边界 ​

把已经接受的答复变成可等待的文本/气泡事件,保留取消和交付背压。

publishReplyEvents({text,delivery?},execution) 返回 AsyncIterable<AgentOutputEvent>。默认 chunkSize=240、intervalMs=0、bubblePauseMs=0、maxDurationMs=3000;时序总量按上限缩放,取消会停止继续输出。内部 REPLY_BUBBLE_BOUNDARY 只用于划分气泡,不应作为正文显示。

serializeOutputEvents 转为文本流、过滤 preview,并将 bubble 重新编码为内部边界。textOutputEvents 将普通字符串流包装为 text 事件。replyBubbleTexts 只取输入中确实为 string 的 text/bubbles。它们是答复展示合同,不是 Core 的执行事实监听,不证明内容已通过业务检查。

由简到繁的同业务示例 ​

先输出已经接受的答复 ​

入门 · 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);

相同事件入口增加分片与气泡 ​

配置 · 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');
}

在同一合同间转换已有文本流 ​

扩展 · 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);

策略与实现 ​

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