createReplyPreview
SUPPORT · function · 运行时值
Create a model-output event handler that publishes preview bubbles.
The handler resets on model.start, ignores other invocations, disables itself on unexpected output, and clears the preview when a call ends unsuccessfully. Rules are snapshotted at creation, and only explicitly registered prose fields are projected.
导入
import { createReplyPreview } from '@jangts/agent-core/support';import { createReplyPreview } from '@jangts/agent-core/support';其他公开入口:@jangts/agent-core/support。
定义
调用
createReplyPreview(options: ReplyPreviewOptions): (event: ModelOutputEvent) => Promise<void>Create a model-output event handler that publishes preview bubbles.
The handler resets on model.start, ignores other invocations, disables itself on unexpected output, and clears the preview when a call ends unsuccessfully. Rules are snapshotted at creation, and only explicitly registered prose fields are projected.
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
options | ReplyPreviewOptions | 必需 | — |
options 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
sink | (bubbles: string[]) => void | Promise<void> | 必需 | 只读。Receives the current bubble list; rejection propagates to the event caller. |
rules | readonly ReplyPreviewRule[] | 必需 | 见类型定义及调用场景。 |
render | ((text: string) => string) | undefined | 可省略 | Presentation only; must not authorize, persist or execute a proposal. |
maxChars | number | undefined | 可省略 | Streamed characters tolerated before preview is disabled; default 16000. |
返回: (event: ModelOutputEvent) => Promise<void>。
使用示例
- 正文预览:有序输出,不代替最终接受:按已登记的 stage/tool/字段路径提取可展示正文,拒绝把控制提议当作已执行结果。
为纯文本阶段增加预览
入门 · Node.js
import { ModelRunner, Support } from '@jangts/agent-core';
const preview = Support.createReplyPreview({ rules: [{ stageId: 'reply' }],
sink: bubbles => { console.log('preview', bubbles); },
});
const runner = new ModelRunner({ provider: { async *stream() { yield '你好'; yield ',Core'; } }, sink: { output: preview } });
console.log((await runner.invoke({ input: '你好', stageId: 'reply' })).value);import { ModelRunner, Support } from '@jangts/agent-core';
const preview = Support.createReplyPreview({ rules: [{ stageId: 'reply' }],
sink: bubbles => { console.log('preview', bubbles); },
});
const runner = new ModelRunner({ provider: { async *stream() { yield '你好'; yield ',Core'; } }, sink: { output: preview } });
console.log((await runner.invoke({ input: '你好', stageId: 'reply' })).value);只允许明确正文协议的可展示字段
配置 · Node.js
规则必须对应 App 实际登记的 reply Tool 与 Schema,配置预览本身不创建模型 Tool。
import { Support } from '@jangts/agent-core';
const preview = Support.createReplyPreview({
rules: [{ stageId: 'reply', toolId: 'reply', paths: [['answer'], ['bubbles', '*']],
when: { path: ['kind'], values: ['reply'] } }],
sink: async bubbles => { console.log('当前暂态正文', bubbles); },
maxChars: 4000,
});
console.log(typeof preview);import { Support } from '@jangts/agent-core';
const preview = Support.createReplyPreview({
rules: [{ stageId: 'reply', toolId: 'reply', paths: [['answer'], ['bubbles', '*']],
when: { path: ['kind'], values: ['reply'] } }],
sink: async (bubbles) => { console.log('当前暂态正文', bubbles); },
maxChars: 4000,
});
console.log(typeof preview);