ModelResponseCollector
CORE · interface · 类型合同
Accumulates streamed parts for one call and resolves them into the transport's result.
导入
ts
import type { ModelResponseCollector } from '@jangts/agent-core/support';js
/** @typedef {import('@jangts/agent-core/support').ModelResponseCollector} ModelResponseCollector */其他公开入口:@jangts/agent-core、@jangts/agent-core/types、@jangts/agent-core/structs、@jangts/agent-core/support、@jangts/agent-core/support/model。
定义
ts
interface ModelResponseCollector<Result>泛型参数
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
Result | — | ModelSelection |
字段结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
call | { id: string; name: string; argumentChars: number; } | undefined | 可省略 | 只读。Native function call that carried the selection, when the response used one. |
calls | readonly { id: string; name: string; argumentChars: number; toolId?: string; }[] | undefined | 可省略 | 只读。All native calls carried by a session response; omitted by single-selection collectors. |
outputChars | number | 必需 | 只读。Output characters accumulated so far, for output-budget accounting. |
push | (part: ModelPart) => void | 必需 | Appends one streamed part; parts arrive in provider order. |
finish | () => Result | 必需 | Finalizes the response. Throws when the accumulated content is not a valid selection. |
成员目录
| 成员 | 用途 |
|---|---|
push | Appends one streamed part; parts arrive in provider order. |
finish | Finalizes the response. Throws when the accumulated content is not a valid selection. |
成员详情
push
Appends one streamed part; parts arrive in provider order.
ts
ModelResponseCollector.push(part: ModelPart): voidAppends one streamed part; parts arrive in provider order.
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
part | ModelPart | 必需 | — |
参数 part的可选结构: 选择其中一种结构,各分支的字段要求分别列出。
分支 1
string
分支 2
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
type | "function-call-delta" | 必需 | 见类型定义及调用场景。 |
index | number | 必需 | Index of the call within the response; deltas with the same index belong together. |
id | string | undefined | 可省略 | Provider call id, usually delivered once with the first delta. |
name | string | undefined | 可省略 | Function name, usually delivered once with the first delta. |
arguments | string | undefined | 可省略 | Raw JSON argument fragment; concatenating all fragments yields the full arguments. |
返回: void。
finish
Finalizes the response. Throws when the accumulated content is not a valid selection.
ts
ModelResponseCollector.finish(): ResultFinalizes the response. Throws when the accumulated content is not a valid selection.
无调用参数。
返回: Result。
等待后的值: Awaited<Result>。
使用示例
- 实际请求测量与同次准备快照:需要提前处理上下文时才 prepare;多次测量与最终发送复用同一次传输准备。
- 模型 Tool 与传输:完整校验后才得到类型化选择:Tool 定义提供 Schema 与 resolver;传输将 wire 输出翻译为统一的模型选择合同。
普通调用仍然只有 invoke
入门 · Node.js
ts
import { ModelRunner } from '@jangts/agent-core/model';
const provider = {
async *stream(messages: readonly { content: string }[]) {
yield '收到:' + messages.at(-1)!.content;
},
};
const runner = new ModelRunner({ provider });
console.log((await runner.invoke('你好')).value);js
import { ModelRunner } from '@jangts/agent-core/model';
const provider = {
async *stream(messages) {
yield '收到:' + messages.at(-1).content;
},
};
const runner = new ModelRunner({ provider });
console.log((await runner.invoke('你好')).value);只在需要测量时准备一次
配置 · Node.js
ts
import { ModelRunner } from '@jangts/agent-core/model';
const provider = {
async *stream(messages: readonly { content: string }[]) {
yield '收到:' + messages.at(-1)!.content;
},
};
const runner = new ModelRunner({ provider, maxInputChars: 1000 });
const prepared = runner.prepare({ stageId: 'reply', maxOutputTokens: 400 });
const input = '你好,解释这次服务状态';
console.log(prepared.measure(input));
const result = await runner.invoke({ input, prepared });
console.log(result.value);js
import { ModelRunner } from '@jangts/agent-core/model';
const provider = {
async *stream(messages) {
yield '收到:' + messages.at(-1).content;
},
};
const runner = new ModelRunner({ provider, maxInputChars: 1000 });
const prepared = runner.prepare({ stageId: 'reply', maxOutputTokens: 400 });
const input = '你好,解释这次服务状态';
console.log(prepared.measure(input));
const result = await runner.invoke({ input, prepared });
console.log(result.value);