Skip to content
English

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.

导入 ​

ts
import { createReplyPreview } from '@jangts/agent-core/support';
js
import { createReplyPreview } from '@jangts/agent-core/support';

其他公开入口:@jangts/agent-core/support。

定义 ​

调用 ​

ts
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.

参数类型传递说明
optionsReplyPreviewOptions必需—
options 的字段
字段类型必需说明
sink(bubbles: string[]) => void | Promise<void>必需只读。Receives the current bubble list; rejection propagates to the event caller.
rulesreadonly ReplyPreviewRule[]必需见类型定义及调用场景。
render((text: string) => string) | undefined可省略Presentation only; must not authorize, persist or execute a proposal.
maxCharsnumber | undefined可省略Streamed characters tolerated before preview is disabled; default 16000.

返回: (event: ModelOutputEvent) => Promise<void>。

使用示例 ​

为纯文本阶段增加预览 ​

入门 · Node.js

ts
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);
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);

只允许明确正文协议的可展示字段 ​

配置 · Node.js

规则必须对应 App 实际登记的 reply Tool 与 Schema,配置预览本身不创建模型 Tool。

ts
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);
js
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);

相关定义 ​

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