Skip to content
English

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.
callsreadonly { id: string; name: string; argumentChars: number; toolId?: string; }[] | undefined可省略只读。All native calls carried by a session response; omitted by single-selection collectors.
outputCharsnumber必需只读。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.

成员目录 ​

成员用途
pushAppends one streamed part; parts arrive in provider order.
finishFinalizes 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): void

Appends one streamed part; parts arrive in provider order.

参数类型传递说明
partModelPart必需—

参数 part的可选结构: 选择其中一种结构,各分支的字段要求分别列出。

分支 1

string

分支 2
字段类型必需说明
type"function-call-delta"必需见类型定义及调用场景。
indexnumber必需Index of the call within the response; deltas with the same index belong together.
idstring | undefined可省略Provider call id, usually delivered once with the first delta.
namestring | undefined可省略Function name, usually delivered once with the first delta.
argumentsstring | 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(): Result

Finalizes the response. Throws when the accumulated content is not a valid selection.

无调用参数。

返回: Result。

等待后的值: Awaited<Result>。

使用示例 ​

普通调用仍然只有 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);

相关策略源码 ​

相关定义 ​

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