Skip to content
中文

ModelResponseCollector ​

CORE · interface · Type contract

Accumulates streamed parts for one call and resolves them into the transport's result.

Import ​

ts
import type { ModelResponseCollector } from '@jangts/agent-core/support';
js
/** @typedef {import('@jangts/agent-core/support').ModelResponseCollector} ModelResponseCollector */

Other public entry points:@jangts/agent-core, @jangts/agent-core/types, @jangts/agent-core/structs, @jangts/agent-core/support, @jangts/agent-core/support/model.

Definition ​

ts
interface ModelResponseCollector<Result>

Type parameters ​

Type parameterConstraintDefault type
Result—ModelSelection

Field structure ​

FieldTypeRequiredDescription
call{ id: string; name: string; argumentChars: number; } | undefinedOptionalRead-only. Native function call that carried the selection, when the response used one.
callsreadonly { id: string; name: string; argumentChars: number; toolId?: string; }[] | undefinedOptionalRead-only. All native calls carried by a session response; omitted by single-selection collectors.
outputCharsnumberRequiredRead-only. Output characters accumulated so far, for output-budget accounting.
push(part: ModelPart) => voidRequiredAppends one streamed part; parts arrive in provider order.
finish() => ResultRequiredFinalizes the response. Throws when the accumulated content is not a valid selection.

Member index ​

MemberPurpose
pushAppends one streamed part; parts arrive in provider order.
finishFinalizes the response. Throws when the accumulated content is not a valid selection.

Member details ​

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.

ParameterTypePassingDescription
partModelPartRequired—

Optional shapes of Parameter part: choose one shape; required fields for each branch are listed separately.

Branch 1

string

Branch 2
FieldTypeRequiredDescription
type"function-call-delta"RequiredSee the type definition and usage context.
indexnumberRequiredIndex of the call within the response; deltas with the same index belong together.
idstring | undefinedOptionalProvider call id, usually delivered once with the first delta.
namestring | undefinedOptionalFunction name, usually delivered once with the first delta.
argumentsstring | undefinedOptionalRaw JSON argument fragment; concatenating all fragments yields the full arguments.

Returns: 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.

No call parameters.

Returns: Result.

Awaited value: Awaited<Result>.

Examples ​

Ordinary calls still only use invoke ​

Beginner · 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);

Prepare once only when measurement is needed ​

Configuration · 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);

API reference, usage examples and developer guides.