PreparedModelCall
CORE · interface · 类型合同
Request-bound, single-use transport snapshot.
Repeated measurements do not prepare or spend calls.
导入
ts
import type { PreparedModelCall } from '@jangts/agent-core/model';js
/** @typedef {import('@jangts/agent-core/model').PreparedModelCall} PreparedModelCall */其他公开入口:@jangts/agent-core、@jangts/agent-core/model、@jangts/agent-core/types、@jangts/agent-core/structs。
定义
ts
interface PreparedModelCall<Result>继承或实现: PreparedModelTransport<Result>。
泛型参数
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
Result | — | unknown |
字段结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
modelId | string | undefined | 可省略 | 只读。Model binding the call was prepared with. |
stageId | string | 必需 | 只读。见类型定义及调用场景。 |
transportId | string | 必需 | 只读。见类型定义及调用场景。 |
maxOutputTokens | number | 必需 | 只读。见类型定义及调用场景。 |
remainingCalls | number | 必需 | 只读。Calls still available; a live value, so later spends are visible. |
measure | (input: string | readonly ModelMessage[] | BuiltContext) => ModelInputMeasurement | 必需 | Preflight measurement of one input shape. Never marks the call used and never charges the budget. |
options | Pick<ModelOptions, "functions" | "json" | "toolChoice" | "parallelToolCalls" | "temperature"> | 必需 | 只读。见类型定义及调用场景。 |
schemaChars | number | 必需 | 只读。见类型定义及调用场景。 |
instruction | string | undefined | 可省略 | 只读。见类型定义及调用场景。 |
createResponse | () => ModelResponseCollector<Result> | 必需 | 见类型定义及调用场景。 |
成员目录
| 成员 | 用途 |
|---|---|
measure | Preflight measurement of one input shape. Never marks the call used and never charges the budget. |
createResponse | 完整输入输出见下方重载。 |
成员详情
measure
Preflight measurement of one input shape.
Never marks the call used and never charges the budget.
ts
PreparedModelCall.measure(input: string | readonly ModelMessage[] | BuiltContext): ModelInputMeasurementPreflight measurement of one input shape.
Never marks the call used and never charges the budget.
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
input | string | readonly ModelMessage[] | BuiltContext | 必需 | — |
参数 input的可选结构: 选择其中一种结构,各分支的字段要求分别列出。
分支 1
string
分支 2
readonly ModelMessage[]
分支 3
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
messages | ModelMessage[] | 必需 | Messages in final model order. |
fragmentUsage | ContextFragmentUsage[] | 必需 | Per-fragment accounting in the same order as the fragments were merged. |
inputChars | number | 必需 | Total counted characters across messages and usage. |
maxInputChars | number | 必需 | Budget the assembly was validated against; Infinity means unbounded. |
返回: ModelInputMeasurement。
返回值结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
messageChars | number | 必需 | 只读。Characters of the message content. |
instructionChars | number | 必需 | 只读。Characters of the instruction text. |
schemaChars | number | 必需 | 只读。Characters of the compiled capability schemas. |
transportChars | number | 必需 | 只读。Characters the transport itself contributes (for example framing). |
inputChars | number | 必需 | 只读。Sum of the character components above. |
maxInputChars | number | 必需 | 只读。Character budget the input was compared against. |
maxSchemaChars | number | 必需 | 只读。Schema budget the input was compared against. |
maxOutputTokens | number | 必需 | 只读。Output tokens reserved for this call, which reduces the token input ceiling. |
counterId | string | undefined | 可省略 | 只读。Tokenizer identity; present only when a token budget applies. |
inputTokens | number | undefined | 可省略 | 只读。Measured input tokens. |
inputTokenLimit | number | undefined | 可省略 | 只读。Token ceiling after reserving output and the safety margin. |
withinBudget | boolean | 必需 | 只读。True when every applicable character and token budget accepted the input. |
createResponse
ts
PreparedModelCall.createResponse(): ModelResponseCollector<Result>无调用参数。
返回: ModelResponseCollector<Result>。
返回值结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
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. |
使用示例
- 实际请求测量与同次准备快照:需要提前处理上下文时才 prepare;多次测量与最终发送复用同一次传输准备。
普通调用仍然只有 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);