ModelResponseCollector
CORE · interface · Type contract
Accumulates streamed parts for one call and resolves them into the transport's result.
Import
import type { ModelResponseCollector } from '@jangts/agent-core/support';/** @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
interface ModelResponseCollector<Result>Type parameters
| Type parameter | Constraint | Default type |
|---|---|---|
Result | — | ModelSelection |
Field structure
| Field | Type | Required | Description |
|---|---|---|---|
call | { id: string; name: string; argumentChars: number; } | undefined | Optional | Read-only. Native function call that carried the selection, when the response used one. |
calls | readonly { id: string; name: string; argumentChars: number; toolId?: string; }[] | undefined | Optional | Read-only. All native calls carried by a session response; omitted by single-selection collectors. |
outputChars | number | Required | Read-only. Output characters accumulated so far, for output-budget accounting. |
push | (part: ModelPart) => void | Required | Appends one streamed part; parts arrive in provider order. |
finish | () => Result | Required | Finalizes the response. Throws when the accumulated content is not a valid selection. |
Member index
| Member | Purpose |
|---|---|
push | Appends one streamed part; parts arrive in provider order. |
finish | Finalizes the response. Throws when the accumulated content is not a valid selection. |
Member details
push
Appends one streamed part; parts arrive in provider order.
ModelResponseCollector.push(part: ModelPart): voidAppends one streamed part; parts arrive in provider order.
| Parameter | Type | Passing | Description |
|---|---|---|---|
part | ModelPart | Required | — |
Optional shapes of Parameter part: choose one shape; required fields for each branch are listed separately.
Branch 1
string
Branch 2
| Field | Type | Required | Description |
|---|---|---|---|
type | "function-call-delta" | Required | See the type definition and usage context. |
index | number | Required | Index of the call within the response; deltas with the same index belong together. |
id | string | undefined | Optional | Provider call id, usually delivered once with the first delta. |
name | string | undefined | Optional | Function name, usually delivered once with the first delta. |
arguments | string | undefined | Optional | Raw 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.
ModelResponseCollector.finish(): ResultFinalizes the response. Throws when the accumulated content is not a valid selection.
No call parameters.
Returns: Result.
Awaited value: Awaited<Result>.
Examples
- Actual request measurement and same-call preparation snapshot: Use prepare only when context must be handled ahead of time; multiple measurements and the final send reuse the same transport preparation.
- Model tools and transports: typed selection only after full validation: A tool definition provides a Schema and resolver; a transport translates wire output into the unified model selection contract.
Ordinary calls still only use invoke
Beginner · Node.js
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);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
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);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);