textOutputEvents
KIT · function · 运行时值
Wrap already-chunked text as text events.
导入
ts
import { textOutputEvents } from '@jangts/agent-core/events';js
import { textOutputEvents } from '@jangts/agent-core/events';其他公开入口:@jangts/agent-core、@jangts/agent-core/types、@jangts/agent-core/events、@jangts/agent-core/kits/presentation、@jangts/agent-core/kits。
定义
调用
ts
textOutputEvents(chunks: AsyncIterable<string>): AsyncIterable<AgentOutputEvent>Wrap already-chunked text as text events.
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
chunks | AsyncIterable<string> | 必需 | — |
返回: AsyncIterable<AgentOutputEvent>。
使用示例
- 答复事件:文本分片和气泡边界:把已经接受的答复变成可等待的文本/气泡事件,保留取消和交付背压。
先输出已经接受的答复
入门 · 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);