Skip to content
English

ModelRunner ​

CORE · class · 运行时值

Execute registered model stages under one request's budgets and lifetime.

Invariants: at most one call is in flight per runner; callCount and journal are local to the runner while totalCallCount includes descendants; every call is charged to the shared budget before I/O and every failure is annotated with its stage before it propagates. No business prompts, domain stages or provider SDKs are built into this runner.

导入 ​

ts
import { ModelRunner } from '@jangts/agent-core/model';
js
import { ModelRunner } from '@jangts/agent-core/model';

其他公开入口:@jangts/agent-core、@jangts/agent-core/model、@jangts/agent-core/types。

定义 ​

ts
class ModelRunner<TransportId, Transports>

泛型参数 ​

类型参数约束默认类型
TransportIdstringstring
Transportsreadonly ModelTransportDefinition[]readonly ModelTransportDefinition[]

构造函数 ​

ts
new ModelRunner<TransportId extends string = string, const Transports extends readonly ModelTransportDefinition[] = readonly ModelTransportDefinition<unknown>[]>(options: ModelRunnerOptions<Transports>): ModelRunner<TransportId, Transports>

Build a runner from App options, or adopt an already compiled binding.

Compiling here rather than per call is what makes configuration immutable for the runner's lifetime. The text transport is always registered first, then App transports in declaration order.

参数类型传递说明
optionsModelRunnerOptions<Transports>必需—
options 的字段
字段类型必需说明
providerChatProvider必需见类型定义及调用场景。
executionExecutionOptions | undefined可省略见类型定义及调用场景。
transportsTransports | undefined可省略见类型定义及调用场景。
sink{ output?: ModelRunnerPorts["onOutput"]; } | undefined可省略见类型定义及调用场景。
errors{ inputBudget?: ModelRunnerPorts["inputBudgetError"]; outputBudget?: ModelRunnerPorts["outputBudgetError"]; } | undefined可省略见类型定义及调用场景。
childrenModelChildPolicy | undefined可省略只读。见类型定义及调用场景。
maxInputCharsnumber | undefined可省略Character budget for the assembled input.
maxSchemaCharsnumber | undefined可省略Character allowance for registered capability schemas.
tokenBudgetModelTokenBudget | undefined可省略见类型定义及调用场景。
maxOutputTokensnumber | undefined可省略Default output reservation; invoke can override it for one call.
maxCallsnumber | undefined可省略Calls allowed for the whole request, including descendant runners' reservations.
callTimeoutMsnumber | undefined可省略Deadline for one provider call, in milliseconds.
maxOutputCharsnumber | undefined可省略Cap on streamed output characters; exceeding it fails the call.
usageboolean | undefined可省略Request and record provider usage for each call.

返回: ModelRunner<TransportId, Transports>。

实例属性 ​

字段类型必需说明
journalContextJournal必需只读。Record of parsed results for this runner; descendants keep their own journals.
callCountnumber必需只读。Completed calls observed by this runner only.
lastCallReadonly<ModelCallTrace> | undefined必需只读。Detached copy of the most recent call trace, or undefined before the first call.
signalAbortSignal必需只读。见类型定义及调用场景。
executionExecutionContext必需只读。见类型定义及调用场景。
childLimitsReadonly<Required<ModelChildPolicy>>必需只读。Limits shared by every runner of one request; children only read them.
totalCallCountnumber必需只读。Includes descendant attempts; local callCount and journal remain isolated.
remainingCallsnumber必需只读。Calls still available to this runner after the parent's reserve is applied.

成员目录 ​

成员用途
createChildCreate a fresh runner for a nested stage. The child never inherits conversation, journal or sinks: only the provider, fixed limits, the parent budget and the child's own lifetime. All ancestors keep their budgets, and the child is charged through them, so nested work cannot overspend.
preparePrepare a call and measure it before invocation. A prepared call is single-use and bound to the stage, output tokens and transport it was measured with; invoke rejects any attempt to replace those settings. Prepare a call for a transport instance supplied at the call site. Prepare a call for a transport id, typed by the runner's registered transports. Prepare a call without narrowing the transport type.
invokeInvoke one model call. Named and shorthand input share the same lifecycle: validation, measurement, budget charge, streaming, commit. A caller-supplied signal only shortens the call lifetime. Invoke a prepared call whose settings cannot be replaced. Invoke with the default transport and a plain-text result. Invoke with a transport instance supplied at the call site. Invoke with a transport id registered on this runner. Invoke without narrowing the transport type.

成员详情 ​

createChild ​

实例方法。

Create a fresh runner for a nested stage.

The child never inherits conversation, journal or sinks: only the provider, fixed limits, the parent budget and the child's own lifetime. All ancestors keep their budgets, and the child is charged through them, so nested work cannot overspend.

ts
createChild(options: import("../structs/model/runtime.js").ModelChildOptions): ModelRunner

Create a fresh runner for a nested stage.

The child never inherits conversation, journal or sinks: only the provider, fixed limits, the parent budget and the child's own lifetime. All ancestors keep their budgets, and the child is charged through them, so nested work cannot overspend.

参数类型传递说明
optionsModelChildOptions必需—
options 的字段
字段类型必需说明
idstring必需只读。Child stage id, reported in traces and events.
maxCallsnumber必需只读。Calls the child may use from the shared request budget.
reserveCallsnumber必需只读。Calls kept for the parent; the child cannot spend them.
timeoutMsnumber必需只读。Deadline for the child's own calls, in milliseconds.
transportsreadonly ModelTransportDefinition<unknown>[] | undefined可省略只读。Additional transports visible to the child.
diagnosticsDiagnosticContext | undefined可省略只读。见类型定义及调用场景。
signalAbortSignal | undefined可省略只读。Extra cancellation; can only shorten the parent's lifetime.

返回: ModelRunner<string, readonly ModelTransportDefinition<unknown>[]>。

prepare ​

实例方法。

Prepare a call and measure it before invocation.

A prepared call is single-use and bound to the stage, output tokens and transport it was measured with; invoke rejects any attempt to replace those settings. Prepare a call for a transport instance supplied at the call site. Prepare a call for a transport id, typed by the runner's registered transports. Prepare a call without narrowing the transport type.

ts
prepare(request?: import("../structs/model/runtime.js").ModelPrepareRequest<never>): import("../structs/model/runtime.js").PreparedModelCall<string>

Prepare a call and measure it before invocation.

A prepared call is single-use and bound to the stage, output tokens and transport it was measured with; invoke rejects any attempt to replace those settings.

参数类型传递说明
requestModelPrepareRequest<never> | undefined可省略—

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

分支 1

undefined

分支 2
字段类型必需说明
stageIdstring | undefined可省略只读。见类型定义及调用场景。
transportundefined可省略只读。见类型定义及调用场景。
maxOutputTokensnumber | undefined可省略只读。见类型定义及调用场景。

返回: PreparedModelCall<string>。

返回值结构
字段类型必需说明
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<string>必需见类型定义及调用场景。
ts
prepare<Result>(request: import("../structs/model/runtime.js").ModelPrepareRequest<ModelTransportDefinition<Result>> & { readonly transport: ModelTransportDefinition<Result>; }): import("../structs/model/runtime.js").PreparedModelCall<Result>

Prepare a call for a transport instance supplied at the call site.

类型参数约束默认类型
Result——
参数类型传递说明
requestModelPrepareRequest<ModelTransportDefinition<Result>> & { readonly transport: ModelTransportDefinition<Result>; }必需—
request 的字段
字段类型必需说明
stageIdstring | undefined可省略只读。见类型定义及调用场景。
transportModelTransportDefinition<Result>必需只读。见类型定义及调用场景。
maxOutputTokensnumber | undefined可省略只读。见类型定义及调用场景。

返回: PreparedModelCall<Result>。

返回值结构
字段类型必需说明
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>必需见类型定义及调用场景。
ts
prepare<const Id extends string>(request: import("../structs/model/runtime.js").ModelPrepareRequest<Id> & { readonly transport: Id; }): import("../structs/model/runtime.js").PreparedModelCall<import("../structs/model/runtime.js").RegisteredTransportOutput<Transports, Id>>

Prepare a call for a transport id, typed by the runner's registered transports.

类型参数约束默认类型
Idstring—
参数类型传递说明
requestModelPrepareRequest<Id> & { readonly transport: Id; }必需—
request 的字段
字段类型必需说明
stageIdstring | undefined可省略只读。见类型定义及调用场景。
transportId必需只读。见类型定义及调用场景。
maxOutputTokensnumber | undefined可省略只读。见类型定义及调用场景。

返回: PreparedModelCall<RegisteredTransportOutput<Transports, Id>>。

返回值结构
字段类型必需说明
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<RegisteredTransportOutput<Transports, Id>>必需见类型定义及调用场景。
ts
prepare(request: import("../structs/model/runtime.js").ModelPrepareRequest<string | ModelTransportDefinition>): import("../structs/model/runtime.js").PreparedModelCall

Prepare a call without narrowing the transport type.

参数类型传递说明
requestModelPrepareRequest<string | ModelTransportDefinition<unknown>>必需—
request 的字段
字段类型必需说明
stageIdstring | undefined可省略只读。见类型定义及调用场景。
transportstring | ModelTransportDefinition<unknown> | undefined可省略只读。见类型定义及调用场景。
maxOutputTokensnumber | undefined可省略只读。见类型定义及调用场景。

返回: PreparedModelCall<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<unknown>必需见类型定义及调用场景。

invoke ​

实例方法。

Invoke one model call.

Named and shorthand input share the same lifecycle: validation, measurement, budget charge, streaming, commit. A caller-supplied signal only shortens the call lifetime. Invoke a prepared call whose settings cannot be replaced. Invoke with the default transport and a plain-text result. Invoke with a transport instance supplied at the call site. Invoke with a transport id registered on this runner. Invoke without narrowing the transport type.

ts
invoke(request: string): Promise<ModelResult<string>>

Invoke one model call.

Named and shorthand input share the same lifecycle: validation, measurement, budget charge, streaming, commit. A caller-supplied signal only shortens the call lifetime.

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

返回: Promise<ModelResult<string>>。

等待后的值: ModelResult<string>。

返回值结构
字段类型必需说明
valuestring必需只读。见类型定义及调用场景。
invocationReadonly<ModelCallRecord>必需只读。见类型定义及调用场景。
ts
invoke<Result>(request: ModelCallRequest<never> & { readonly prepared: import("../structs/model/runtime.js").PreparedModelCall<Result>; }): Promise<ModelResult<Result>>

Invoke a prepared call whose settings cannot be replaced.

类型参数约束默认类型
Result——
参数类型传递说明
requestModelCallRequest<never> & { readonly prepared: import("../structs/model/runtime.js").PreparedModelCall<Result>; }必需—

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

分支 1
字段类型必需说明
loggerSubFlowLogger | undefined可省略只读。Record this call in the supplied logger; omission adds no sub-flow record.
signalAbortSignal | undefined可省略只读。Additional cancellation can shorten, never replace, the Runner lifetime.
diagnosticsDiagnosticContext | undefined可省略只读。Parent observation scope for this invocation; never changes execution budget or signal.
preparedPreparedModelCall<unknown> & PreparedModelCall<Result>必需只读。Reuse a prepared call; its stage, transport and output reservation then take precedence.
maxOutputTokensnumber | undefined可省略只读。见类型定义及调用场景。
stageIdstring | undefined可省略只读。见类型定义及调用场景。
transportundefined可省略只读。见类型定义及调用场景。
inputstring必需见类型定义及调用场景。
instructionsstring | undefined可省略见类型定义及调用场景。
分支 2
字段类型必需说明
loggerSubFlowLogger | undefined可省略只读。Record this call in the supplied logger; omission adds no sub-flow record.
signalAbortSignal | undefined可省略只读。Additional cancellation can shorten, never replace, the Runner lifetime.
diagnosticsDiagnosticContext | undefined可省略只读。Parent observation scope for this invocation; never changes execution budget or signal.
preparedPreparedModelCall<unknown> & PreparedModelCall<Result>必需只读。Reuse a prepared call; its stage, transport and output reservation then take precedence.
maxOutputTokensnumber | undefined可省略只读。见类型定义及调用场景。
stageIdstring | undefined可省略只读。见类型定义及调用场景。
transportundefined可省略只读。见类型定义及调用场景。
inputreadonly ModelMessage[] | BuiltContext | ContextPlan必需见类型定义及调用场景。
instructionsundefined可省略见类型定义及调用场景。

返回: Promise<ModelResult<Result>>。

等待后的值: ModelResult<Result>。

返回值结构
字段类型必需说明
valueResult必需只读。见类型定义及调用场景。
invocationReadonly<ModelCallRecord>必需只读。见类型定义及调用场景。
ts
invoke(request: ModelCallRequest<never>): Promise<ModelResult<string>>

Invoke with the default transport and a plain-text result.

参数类型传递说明
requestModelCallRequest<never>必需—

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

分支 1
字段类型必需说明
loggerSubFlowLogger | undefined可省略只读。Record this call in the supplied logger; omission adds no sub-flow record.
signalAbortSignal | undefined可省略只读。Additional cancellation can shorten, never replace, the Runner lifetime.
diagnosticsDiagnosticContext | undefined可省略只读。Parent observation scope for this invocation; never changes execution budget or signal.
preparedPreparedModelCall<unknown> | undefined可省略只读。Reuse a prepared call; its stage, transport and output reservation then take precedence.
maxOutputTokensnumber | undefined可省略只读。见类型定义及调用场景。
stageIdstring | undefined可省略只读。见类型定义及调用场景。
transportundefined可省略只读。见类型定义及调用场景。
inputstring必需见类型定义及调用场景。
instructionsstring | undefined可省略见类型定义及调用场景。
分支 2
字段类型必需说明
loggerSubFlowLogger | undefined可省略只读。Record this call in the supplied logger; omission adds no sub-flow record.
signalAbortSignal | undefined可省略只读。Additional cancellation can shorten, never replace, the Runner lifetime.
diagnosticsDiagnosticContext | undefined可省略只读。Parent observation scope for this invocation; never changes execution budget or signal.
preparedPreparedModelCall<unknown> | undefined可省略只读。Reuse a prepared call; its stage, transport and output reservation then take precedence.
maxOutputTokensnumber | undefined可省略只读。见类型定义及调用场景。
stageIdstring | undefined可省略只读。见类型定义及调用场景。
transportundefined可省略只读。见类型定义及调用场景。
inputreadonly ModelMessage[] | BuiltContext | ContextPlan必需见类型定义及调用场景。
instructionsundefined可省略见类型定义及调用场景。

返回: Promise<ModelResult<string>>。

等待后的值: ModelResult<string>。

返回值结构
字段类型必需说明
valuestring必需只读。见类型定义及调用场景。
invocationReadonly<ModelCallRecord>必需只读。见类型定义及调用场景。
ts
invoke<Result>(request: ModelCallRequest<ModelTransportDefinition<Result>> & { transport: ModelTransportDefinition<Result>; }): Promise<ModelResult<Result>>

Invoke with a transport instance supplied at the call site.

类型参数约束默认类型
Result——
参数类型传递说明
requestModelCallRequest<ModelTransportDefinition<Result>> & { transport: ModelTransportDefinition<Result>; }必需—

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

分支 1
字段类型必需说明
loggerSubFlowLogger | undefined可省略只读。Record this call in the supplied logger; omission adds no sub-flow record.
signalAbortSignal | undefined可省略只读。Additional cancellation can shorten, never replace, the Runner lifetime.
diagnosticsDiagnosticContext | undefined可省略只读。Parent observation scope for this invocation; never changes execution budget or signal.
preparedPreparedModelCall<unknown> | undefined可省略只读。Reuse a prepared call; its stage, transport and output reservation then take precedence.
maxOutputTokensnumber | undefined可省略只读。见类型定义及调用场景。
stageIdstring | undefined可省略只读。见类型定义及调用场景。
transportModelTransportDefinition<Result>必需只读。见类型定义及调用场景。
inputstring必需见类型定义及调用场景。
instructionsstring | undefined可省略见类型定义及调用场景。
分支 2
字段类型必需说明
loggerSubFlowLogger | undefined可省略只读。Record this call in the supplied logger; omission adds no sub-flow record.
signalAbortSignal | undefined可省略只读。Additional cancellation can shorten, never replace, the Runner lifetime.
diagnosticsDiagnosticContext | undefined可省略只读。Parent observation scope for this invocation; never changes execution budget or signal.
preparedPreparedModelCall<unknown> | undefined可省略只读。Reuse a prepared call; its stage, transport and output reservation then take precedence.
maxOutputTokensnumber | undefined可省略只读。见类型定义及调用场景。
stageIdstring | undefined可省略只读。见类型定义及调用场景。
transportModelTransportDefinition<Result>必需只读。见类型定义及调用场景。
inputreadonly ModelMessage[] | BuiltContext | ContextPlan必需见类型定义及调用场景。
instructionsundefined可省略见类型定义及调用场景。

返回: Promise<ModelResult<Result>>。

等待后的值: ModelResult<Result>。

返回值结构
字段类型必需说明
valueResult必需只读。见类型定义及调用场景。
invocationReadonly<ModelCallRecord>必需只读。见类型定义及调用场景。
ts
invoke<const Id extends string>(request: ModelCallRequest<Id> & { readonly transport: Id; }): Promise<ModelResult<import("../structs/model/runtime.js").RegisteredTransportOutput<Transports, Id>>>

Invoke with a transport id registered on this runner.

类型参数约束默认类型
Idstring—
参数类型传递说明
requestModelCallRequest<Id> & { readonly transport: Id; }必需—

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

分支 1
字段类型必需说明
loggerSubFlowLogger | undefined可省略只读。Record this call in the supplied logger; omission adds no sub-flow record.
signalAbortSignal | undefined可省略只读。Additional cancellation can shorten, never replace, the Runner lifetime.
diagnosticsDiagnosticContext | undefined可省略只读。Parent observation scope for this invocation; never changes execution budget or signal.
preparedPreparedModelCall<unknown> | undefined可省略只读。Reuse a prepared call; its stage, transport and output reservation then take precedence.
maxOutputTokensnumber | undefined可省略只读。见类型定义及调用场景。
stageIdstring | undefined可省略只读。见类型定义及调用场景。
transportId必需只读。见类型定义及调用场景。
inputstring必需见类型定义及调用场景。
instructionsstring | undefined可省略见类型定义及调用场景。
分支 2
字段类型必需说明
loggerSubFlowLogger | undefined可省略只读。Record this call in the supplied logger; omission adds no sub-flow record.
signalAbortSignal | undefined可省略只读。Additional cancellation can shorten, never replace, the Runner lifetime.
diagnosticsDiagnosticContext | undefined可省略只读。Parent observation scope for this invocation; never changes execution budget or signal.
preparedPreparedModelCall<unknown> | undefined可省略只读。Reuse a prepared call; its stage, transport and output reservation then take precedence.
maxOutputTokensnumber | undefined可省略只读。见类型定义及调用场景。
stageIdstring | undefined可省略只读。见类型定义及调用场景。
transportId必需只读。见类型定义及调用场景。
inputreadonly ModelMessage[] | BuiltContext | ContextPlan必需见类型定义及调用场景。
instructionsundefined可省略见类型定义及调用场景。

返回: Promise<ModelResult<RegisteredTransportOutput<Transports, Id>>>。

等待后的值: ModelResult<RegisteredTransportOutput<Transports, Id>>。

返回值结构
字段类型必需说明
valueRegisteredTransportOutput<Transports, Id>必需只读。见类型定义及调用场景。
invocationReadonly<ModelCallRecord>必需只读。见类型定义及调用场景。
ts
invoke(request: ModelCallRequest<TransportId | ModelTransportDefinition>): Promise<ModelResult<unknown>>

Invoke without narrowing the transport type.

参数类型传递说明
requestModelCallRequest<ModelTransportDefinition<unknown> | TransportId>必需—

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

分支 1
字段类型必需说明
loggerSubFlowLogger | undefined可省略只读。Record this call in the supplied logger; omission adds no sub-flow record.
signalAbortSignal | undefined可省略只读。Additional cancellation can shorten, never replace, the Runner lifetime.
diagnosticsDiagnosticContext | undefined可省略只读。Parent observation scope for this invocation; never changes execution budget or signal.
preparedPreparedModelCall<unknown> | undefined可省略只读。Reuse a prepared call; its stage, transport and output reservation then take precedence.
maxOutputTokensnumber | undefined可省略只读。见类型定义及调用场景。
stageIdstring | undefined可省略只读。见类型定义及调用场景。
transportModelTransportDefinition<unknown> | TransportId | undefined可省略只读。见类型定义及调用场景。
inputstring必需见类型定义及调用场景。
instructionsstring | undefined可省略见类型定义及调用场景。
分支 2
字段类型必需说明
loggerSubFlowLogger | undefined可省略只读。Record this call in the supplied logger; omission adds no sub-flow record.
signalAbortSignal | undefined可省略只读。Additional cancellation can shorten, never replace, the Runner lifetime.
diagnosticsDiagnosticContext | undefined可省略只读。Parent observation scope for this invocation; never changes execution budget or signal.
preparedPreparedModelCall<unknown> | undefined可省略只读。Reuse a prepared call; its stage, transport and output reservation then take precedence.
maxOutputTokensnumber | undefined可省略只读。见类型定义及调用场景。
stageIdstring | undefined可省略只读。见类型定义及调用场景。
transportModelTransportDefinition<unknown> | TransportId | undefined可省略只读。见类型定义及调用场景。
inputreadonly ModelMessage[] | BuiltContext | ContextPlan必需见类型定义及调用场景。
instructionsundefined可省略见类型定义及调用场景。

返回: Promise<ModelResult<unknown>>。

等待后的值: ModelResult<unknown>。

返回值结构
字段类型必需说明
valueunknown必需只读。见类型定义及调用场景。
invocationReadonly<ModelCallRecord>必需只读。见类型定义及调用场景。

使用示例 ​

同一输出的两种输入 ​

入门 · 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 });
const first = await runner.invoke('你好');
const second = await runner.invoke({ input: '你好' });
console.log(first.value, second.value);
console.log(first.invocation.invocationId);
js
import { ModelRunner } from '@jangts/agent-core/model';
const provider = {
    async *stream(messages) {
        yield '收到:' + messages.at(-1).content;
    },
};
const runner = new ModelRunner({ provider });
const first = await runner.invoke('你好');
const second = await runner.invoke({ input: '你好' });
console.log(first.value, second.value);
console.log(first.invocation.invocationId);

增加预算与请求取消 ​

配置 · 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, maxCalls: 2, maxInputChars: 4000,
  maxOutputChars: 2000, callTimeoutMs: 15000,
  execution: { signal: AbortSignal.timeout(20000) },
});
const result = await runner.invoke({ input: '你好', stageId: 'reply', maxOutputTokens: 600 });
console.log(result.value, runner.remainingCalls);
js
import { ModelRunner } from '@jangts/agent-core/model';
const provider = {
    async *stream(messages) {
        yield '收到:' + messages.at(-1).content;
    },
};
const runner = new ModelRunner({ provider, maxCalls: 2, maxInputChars: 4000,
    maxOutputChars: 2000, callTimeoutMs: 15000,
    execution: { signal: AbortSignal.timeout(20000) },
});
const result = await runner.invoke({ input: '你好', stageId: 'reply', maxOutputTokens: 600 });
console.log(result.value, runner.remainingCalls);

增加独立的有序输出接收器 ​

扩展 · Node.js

sink.output 按序等待。model.output 是尚未完整校验的提议,不能在此执行模型提出的工具操作。

ts
import { ModelRunner } from '@jangts/agent-core/model';
const provider = {
  async *stream(messages: readonly { content: string }[]) {
    yield '收到:' + messages.at(-1)!.content;
  },
};
const events: string[] = [];
const runner = new ModelRunner({ provider, sink: {
  async output(event) { events.push(event.type); },
} });
const result = await runner.invoke('你好');
console.log(result.value, events);
js
import { ModelRunner } from '@jangts/agent-core/model';
const provider = {
    async *stream(messages) {
        yield '收到:' + messages.at(-1).content;
    },
};
const events = [];
const runner = new ModelRunner({ provider, sink: {
        async output(event) { events.push(event.type); },
    } });
const result = await runner.invoke('你好');
console.log(result.value, events);

相关策略源码 ​

相关定义 ​

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