Skip to content
English

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

字段结构 ​

字段类型必需说明
modelIdstring | undefined可省略只读。Model binding the call was prepared with.
stageIdstring必需只读。见类型定义及调用场景。
transportIdstring必需只读。见类型定义及调用场景。
maxOutputTokensnumber必需只读。见类型定义及调用场景。
remainingCallsnumber必需只读。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.
optionsPick<ModelOptions, "functions" | "json" | "toolChoice" | "parallelToolCalls" | "temperature">必需只读。见类型定义及调用场景。
schemaCharsnumber必需只读。见类型定义及调用场景。
instructionstring | undefined可省略只读。见类型定义及调用场景。
createResponse() => ModelResponseCollector<Result>必需见类型定义及调用场景。

成员目录 ​

成员用途
measurePreflight 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): ModelInputMeasurement

Preflight measurement of one input shape.

Never marks the call used and never charges the budget.

参数类型传递说明
inputstring | readonly ModelMessage[] | BuiltContext必需—

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

分支 1

string

分支 2

readonly ModelMessage[]

分支 3
字段类型必需说明
messagesModelMessage[]必需Messages in final model order.
fragmentUsageContextFragmentUsage[]必需Per-fragment accounting in the same order as the fragments were merged.
inputCharsnumber必需Total counted characters across messages and usage.
maxInputCharsnumber必需Budget the assembly was validated against; Infinity means unbounded.

返回: ModelInputMeasurement。

返回值结构
字段类型必需说明
messageCharsnumber必需只读。Characters of the message content.
instructionCharsnumber必需只读。Characters of the instruction text.
schemaCharsnumber必需只读。Characters of the compiled capability schemas.
transportCharsnumber必需只读。Characters the transport itself contributes (for example framing).
inputCharsnumber必需只读。Sum of the character components above.
maxInputCharsnumber必需只读。Character budget the input was compared against.
maxSchemaCharsnumber必需只读。Schema budget the input was compared against.
maxOutputTokensnumber必需只读。Output tokens reserved for this call, which reduces the token input ceiling.
counterIdstring | undefined可省略只读。Tokenizer identity; present only when a token budget applies.
inputTokensnumber | undefined可省略只读。Measured input tokens.
inputTokenLimitnumber | undefined可省略只读。Token ceiling after reserving output and the safety margin.
withinBudgetboolean必需只读。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.
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.

使用示例 ​

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

相关策略源码 ​

相关定义 ​

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