Skip to content
中文

ModelRunner ​

CORE · class · Runtime value

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.

Import ​

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

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

Definition ​

ts
class ModelRunner<TransportId, Transports>

Type parameters ​

Type parameterConstraintDefault type
TransportIdstringstring
Transportsreadonly ModelTransportDefinition[]readonly ModelTransportDefinition[]

Constructor ​

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.

ParameterTypePassingDescription
optionsModelRunnerOptions<Transports>Required—
Fields of options
FieldTypeRequiredDescription
providerChatProviderRequiredSee the type definition and usage context.
executionExecutionOptions | undefinedOptionalSee the type definition and usage context.
transportsTransports | undefinedOptionalSee the type definition and usage context.
sink{ output?: ModelRunnerPorts["onOutput"]; } | undefinedOptionalSee the type definition and usage context.
errors{ inputBudget?: ModelRunnerPorts["inputBudgetError"]; outputBudget?: ModelRunnerPorts["outputBudgetError"]; } | undefinedOptionalSee the type definition and usage context.
childrenModelChildPolicy | undefinedOptionalRead-only. See the type definition and usage context.
maxInputCharsnumber | undefinedOptionalCharacter budget for the assembled input.
maxSchemaCharsnumber | undefinedOptionalCharacter allowance for registered capability schemas.
tokenBudgetModelTokenBudget | undefinedOptionalSee the type definition and usage context.
maxOutputTokensnumber | undefinedOptionalDefault output reservation; invoke can override it for one call.
maxCallsnumber | undefinedOptionalCalls allowed for the whole request, including descendant runners' reservations.
callTimeoutMsnumber | undefinedOptionalDeadline for one provider call, in milliseconds.
maxOutputCharsnumber | undefinedOptionalCap on streamed output characters; exceeding it fails the call.
usageboolean | undefinedOptionalRequest and record provider usage for each call.

Returns: ModelRunner<TransportId, Transports>.

Instance properties ​

FieldTypeRequiredDescription
journalContextJournalRequiredRead-only. Record of parsed results for this runner; descendants keep their own journals.
callCountnumberRequiredRead-only. Completed calls observed by this runner only.
lastCallReadonly<ModelCallTrace> | undefinedRequiredRead-only. Detached copy of the most recent call trace, or undefined before the first call.
signalAbortSignalRequiredRead-only. See the type definition and usage context.
executionExecutionContextRequiredRead-only. See the type definition and usage context.
childLimitsReadonly<Required<ModelChildPolicy>>RequiredRead-only. Limits shared by every runner of one request; children only read them.
totalCallCountnumberRequiredRead-only. Includes descendant attempts; local callCount and journal remain isolated.
remainingCallsnumberRequiredRead-only. Calls still available to this runner after the parent's reserve is applied.

Member index ​

MemberPurpose
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.

Member details ​

createChild ​

Instance method.

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.

ParameterTypePassingDescription
optionsModelChildOptionsRequired—
Fields of options
FieldTypeRequiredDescription
idstringRequiredRead-only. Child stage id, reported in traces and events.
maxCallsnumberRequiredRead-only. Calls the child may use from the shared request budget.
reserveCallsnumberRequiredRead-only. Calls kept for the parent; the child cannot spend them.
timeoutMsnumberRequiredRead-only. Deadline for the child's own calls, in milliseconds.
transportsreadonly ModelTransportDefinition<unknown>[] | undefinedOptionalRead-only. Additional transports visible to the child.
diagnosticsDiagnosticContext | undefinedOptionalRead-only. See the type definition and usage context.
signalAbortSignal | undefinedOptionalRead-only. Extra cancellation; can only shorten the parent's lifetime.

Returns: ModelRunner<string, readonly ModelTransportDefinition<unknown>[]>.

prepare ​

Instance method.

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.

ParameterTypePassingDescription
requestModelPrepareRequest<never> | undefinedOptional—

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

Branch 1

undefined

Branch 2
FieldTypeRequiredDescription
stageIdstring | undefinedOptionalRead-only. See the type definition and usage context.
transportundefinedOptionalRead-only. See the type definition and usage context.
maxOutputTokensnumber | undefinedOptionalRead-only. See the type definition and usage context.

Returns: PreparedModelCall<string>.

Return value structure
FieldTypeRequiredDescription
modelIdstring | undefinedOptionalRead-only. Model binding the call was prepared with.
stageIdstringRequiredRead-only. See the type definition and usage context.
transportIdstringRequiredRead-only. See the type definition and usage context.
maxOutputTokensnumberRequiredRead-only. See the type definition and usage context.
remainingCallsnumberRequiredRead-only. Calls still available; a live value, so later spends are visible.
measure(input: string | readonly ModelMessage[] | BuiltContext) => ModelInputMeasurementRequiredPreflight measurement of one input shape. Never marks the call used and never charges the budget.
optionsPick<ModelOptions, "functions" | "json" | "toolChoice" | "parallelToolCalls" | "temperature">RequiredRead-only. See the type definition and usage context.
schemaCharsnumberRequiredRead-only. See the type definition and usage context.
instructionstring | undefinedOptionalRead-only. See the type definition and usage context.
createResponse() => ModelResponseCollector<string>RequiredSee the type definition and usage context.
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.

Type parameterConstraintDefault type
Result——
ParameterTypePassingDescription
requestModelPrepareRequest<ModelTransportDefinition<Result>> & { readonly transport: ModelTransportDefinition<Result>; }Required—
Fields of request
FieldTypeRequiredDescription
stageIdstring | undefinedOptionalRead-only. See the type definition and usage context.
transportModelTransportDefinition<Result>RequiredRead-only. See the type definition and usage context.
maxOutputTokensnumber | undefinedOptionalRead-only. See the type definition and usage context.

Returns: PreparedModelCall<Result>.

Return value structure
FieldTypeRequiredDescription
modelIdstring | undefinedOptionalRead-only. Model binding the call was prepared with.
stageIdstringRequiredRead-only. See the type definition and usage context.
transportIdstringRequiredRead-only. See the type definition and usage context.
maxOutputTokensnumberRequiredRead-only. See the type definition and usage context.
remainingCallsnumberRequiredRead-only. Calls still available; a live value, so later spends are visible.
measure(input: string | readonly ModelMessage[] | BuiltContext) => ModelInputMeasurementRequiredPreflight measurement of one input shape. Never marks the call used and never charges the budget.
optionsPick<ModelOptions, "functions" | "json" | "toolChoice" | "parallelToolCalls" | "temperature">RequiredRead-only. See the type definition and usage context.
schemaCharsnumberRequiredRead-only. See the type definition and usage context.
instructionstring | undefinedOptionalRead-only. See the type definition and usage context.
createResponse() => ModelResponseCollector<Result>RequiredSee the type definition and usage context.
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.

Type parameterConstraintDefault type
Idstring—
ParameterTypePassingDescription
requestModelPrepareRequest<Id> & { readonly transport: Id; }Required—
Fields of request
FieldTypeRequiredDescription
stageIdstring | undefinedOptionalRead-only. See the type definition and usage context.
transportIdRequiredRead-only. See the type definition and usage context.
maxOutputTokensnumber | undefinedOptionalRead-only. See the type definition and usage context.

Returns: PreparedModelCall<RegisteredTransportOutput<Transports, Id>>.

Return value structure
FieldTypeRequiredDescription
modelIdstring | undefinedOptionalRead-only. Model binding the call was prepared with.
stageIdstringRequiredRead-only. See the type definition and usage context.
transportIdstringRequiredRead-only. See the type definition and usage context.
maxOutputTokensnumberRequiredRead-only. See the type definition and usage context.
remainingCallsnumberRequiredRead-only. Calls still available; a live value, so later spends are visible.
measure(input: string | readonly ModelMessage[] | BuiltContext) => ModelInputMeasurementRequiredPreflight measurement of one input shape. Never marks the call used and never charges the budget.
optionsPick<ModelOptions, "functions" | "json" | "toolChoice" | "parallelToolCalls" | "temperature">RequiredRead-only. See the type definition and usage context.
schemaCharsnumberRequiredRead-only. See the type definition and usage context.
instructionstring | undefinedOptionalRead-only. See the type definition and usage context.
createResponse() => ModelResponseCollector<RegisteredTransportOutput<Transports, Id>>RequiredSee the type definition and usage context.
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.

ParameterTypePassingDescription
requestModelPrepareRequest<string | ModelTransportDefinition<unknown>>Required—
Fields of request
FieldTypeRequiredDescription
stageIdstring | undefinedOptionalRead-only. See the type definition and usage context.
transportstring | ModelTransportDefinition<unknown> | undefinedOptionalRead-only. See the type definition and usage context.
maxOutputTokensnumber | undefinedOptionalRead-only. See the type definition and usage context.

Returns: PreparedModelCall<unknown>.

Return value structure
FieldTypeRequiredDescription
modelIdstring | undefinedOptionalRead-only. Model binding the call was prepared with.
stageIdstringRequiredRead-only. See the type definition and usage context.
transportIdstringRequiredRead-only. See the type definition and usage context.
maxOutputTokensnumberRequiredRead-only. See the type definition and usage context.
remainingCallsnumberRequiredRead-only. Calls still available; a live value, so later spends are visible.
measure(input: string | readonly ModelMessage[] | BuiltContext) => ModelInputMeasurementRequiredPreflight measurement of one input shape. Never marks the call used and never charges the budget.
optionsPick<ModelOptions, "functions" | "json" | "toolChoice" | "parallelToolCalls" | "temperature">RequiredRead-only. See the type definition and usage context.
schemaCharsnumberRequiredRead-only. See the type definition and usage context.
instructionstring | undefinedOptionalRead-only. See the type definition and usage context.
createResponse() => ModelResponseCollector<unknown>RequiredSee the type definition and usage context.

invoke ​

Instance method.

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.

ParameterTypePassingDescription
requeststringRequired—

Returns: Promise<ModelResult<string>>.

Awaited value: ModelResult<string>.

Return value structure
FieldTypeRequiredDescription
valuestringRequiredRead-only. See the type definition and usage context.
invocationReadonly<ModelCallRecord>RequiredRead-only. See the type definition and usage context.
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.

Type parameterConstraintDefault type
Result——
ParameterTypePassingDescription
requestModelCallRequest<never> & { readonly prepared: import("../structs/model/runtime.js").PreparedModelCall<Result>; }Required—

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

Branch 1
FieldTypeRequiredDescription
loggerSubFlowLogger | undefinedOptionalRead-only. Record this call in the supplied logger; omission adds no sub-flow record.
signalAbortSignal | undefinedOptionalRead-only. Additional cancellation can shorten, never replace, the Runner lifetime.
diagnosticsDiagnosticContext | undefinedOptionalRead-only. Parent observation scope for this invocation; never changes execution budget or signal.
preparedPreparedModelCall<unknown> & PreparedModelCall<Result>RequiredRead-only. Reuse a prepared call; its stage, transport and output reservation then take precedence.
maxOutputTokensnumber | undefinedOptionalRead-only. See the type definition and usage context.
stageIdstring | undefinedOptionalRead-only. See the type definition and usage context.
transportundefinedOptionalRead-only. See the type definition and usage context.
inputstringRequiredSee the type definition and usage context.
instructionsstring | undefinedOptionalSee the type definition and usage context.
Branch 2
FieldTypeRequiredDescription
loggerSubFlowLogger | undefinedOptionalRead-only. Record this call in the supplied logger; omission adds no sub-flow record.
signalAbortSignal | undefinedOptionalRead-only. Additional cancellation can shorten, never replace, the Runner lifetime.
diagnosticsDiagnosticContext | undefinedOptionalRead-only. Parent observation scope for this invocation; never changes execution budget or signal.
preparedPreparedModelCall<unknown> & PreparedModelCall<Result>RequiredRead-only. Reuse a prepared call; its stage, transport and output reservation then take precedence.
maxOutputTokensnumber | undefinedOptionalRead-only. See the type definition and usage context.
stageIdstring | undefinedOptionalRead-only. See the type definition and usage context.
transportundefinedOptionalRead-only. See the type definition and usage context.
inputreadonly ModelMessage[] | BuiltContext | ContextPlanRequiredSee the type definition and usage context.
instructionsundefinedOptionalSee the type definition and usage context.

Returns: Promise<ModelResult<Result>>.

Awaited value: ModelResult<Result>.

Return value structure
FieldTypeRequiredDescription
valueResultRequiredRead-only. See the type definition and usage context.
invocationReadonly<ModelCallRecord>RequiredRead-only. See the type definition and usage context.
ts
invoke(request: ModelCallRequest<never>): Promise<ModelResult<string>>

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

ParameterTypePassingDescription
requestModelCallRequest<never>Required—

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

Branch 1
FieldTypeRequiredDescription
loggerSubFlowLogger | undefinedOptionalRead-only. Record this call in the supplied logger; omission adds no sub-flow record.
signalAbortSignal | undefinedOptionalRead-only. Additional cancellation can shorten, never replace, the Runner lifetime.
diagnosticsDiagnosticContext | undefinedOptionalRead-only. Parent observation scope for this invocation; never changes execution budget or signal.
preparedPreparedModelCall<unknown> | undefinedOptionalRead-only. Reuse a prepared call; its stage, transport and output reservation then take precedence.
maxOutputTokensnumber | undefinedOptionalRead-only. See the type definition and usage context.
stageIdstring | undefinedOptionalRead-only. See the type definition and usage context.
transportundefinedOptionalRead-only. See the type definition and usage context.
inputstringRequiredSee the type definition and usage context.
instructionsstring | undefinedOptionalSee the type definition and usage context.
Branch 2
FieldTypeRequiredDescription
loggerSubFlowLogger | undefinedOptionalRead-only. Record this call in the supplied logger; omission adds no sub-flow record.
signalAbortSignal | undefinedOptionalRead-only. Additional cancellation can shorten, never replace, the Runner lifetime.
diagnosticsDiagnosticContext | undefinedOptionalRead-only. Parent observation scope for this invocation; never changes execution budget or signal.
preparedPreparedModelCall<unknown> | undefinedOptionalRead-only. Reuse a prepared call; its stage, transport and output reservation then take precedence.
maxOutputTokensnumber | undefinedOptionalRead-only. See the type definition and usage context.
stageIdstring | undefinedOptionalRead-only. See the type definition and usage context.
transportundefinedOptionalRead-only. See the type definition and usage context.
inputreadonly ModelMessage[] | BuiltContext | ContextPlanRequiredSee the type definition and usage context.
instructionsundefinedOptionalSee the type definition and usage context.

Returns: Promise<ModelResult<string>>.

Awaited value: ModelResult<string>.

Return value structure
FieldTypeRequiredDescription
valuestringRequiredRead-only. See the type definition and usage context.
invocationReadonly<ModelCallRecord>RequiredRead-only. See the type definition and usage context.
ts
invoke<Result>(request: ModelCallRequest<ModelTransportDefinition<Result>> & { transport: ModelTransportDefinition<Result>; }): Promise<ModelResult<Result>>

Invoke with a transport instance supplied at the call site.

Type parameterConstraintDefault type
Result——
ParameterTypePassingDescription
requestModelCallRequest<ModelTransportDefinition<Result>> & { transport: ModelTransportDefinition<Result>; }Required—

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

Branch 1
FieldTypeRequiredDescription
loggerSubFlowLogger | undefinedOptionalRead-only. Record this call in the supplied logger; omission adds no sub-flow record.
signalAbortSignal | undefinedOptionalRead-only. Additional cancellation can shorten, never replace, the Runner lifetime.
diagnosticsDiagnosticContext | undefinedOptionalRead-only. Parent observation scope for this invocation; never changes execution budget or signal.
preparedPreparedModelCall<unknown> | undefinedOptionalRead-only. Reuse a prepared call; its stage, transport and output reservation then take precedence.
maxOutputTokensnumber | undefinedOptionalRead-only. See the type definition and usage context.
stageIdstring | undefinedOptionalRead-only. See the type definition and usage context.
transportModelTransportDefinition<Result>RequiredRead-only. See the type definition and usage context.
inputstringRequiredSee the type definition and usage context.
instructionsstring | undefinedOptionalSee the type definition and usage context.
Branch 2
FieldTypeRequiredDescription
loggerSubFlowLogger | undefinedOptionalRead-only. Record this call in the supplied logger; omission adds no sub-flow record.
signalAbortSignal | undefinedOptionalRead-only. Additional cancellation can shorten, never replace, the Runner lifetime.
diagnosticsDiagnosticContext | undefinedOptionalRead-only. Parent observation scope for this invocation; never changes execution budget or signal.
preparedPreparedModelCall<unknown> | undefinedOptionalRead-only. Reuse a prepared call; its stage, transport and output reservation then take precedence.
maxOutputTokensnumber | undefinedOptionalRead-only. See the type definition and usage context.
stageIdstring | undefinedOptionalRead-only. See the type definition and usage context.
transportModelTransportDefinition<Result>RequiredRead-only. See the type definition and usage context.
inputreadonly ModelMessage[] | BuiltContext | ContextPlanRequiredSee the type definition and usage context.
instructionsundefinedOptionalSee the type definition and usage context.

Returns: Promise<ModelResult<Result>>.

Awaited value: ModelResult<Result>.

Return value structure
FieldTypeRequiredDescription
valueResultRequiredRead-only. See the type definition and usage context.
invocationReadonly<ModelCallRecord>RequiredRead-only. See the type definition and usage context.
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.

Type parameterConstraintDefault type
Idstring—
ParameterTypePassingDescription
requestModelCallRequest<Id> & { readonly transport: Id; }Required—

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

Branch 1
FieldTypeRequiredDescription
loggerSubFlowLogger | undefinedOptionalRead-only. Record this call in the supplied logger; omission adds no sub-flow record.
signalAbortSignal | undefinedOptionalRead-only. Additional cancellation can shorten, never replace, the Runner lifetime.
diagnosticsDiagnosticContext | undefinedOptionalRead-only. Parent observation scope for this invocation; never changes execution budget or signal.
preparedPreparedModelCall<unknown> | undefinedOptionalRead-only. Reuse a prepared call; its stage, transport and output reservation then take precedence.
maxOutputTokensnumber | undefinedOptionalRead-only. See the type definition and usage context.
stageIdstring | undefinedOptionalRead-only. See the type definition and usage context.
transportIdRequiredRead-only. See the type definition and usage context.
inputstringRequiredSee the type definition and usage context.
instructionsstring | undefinedOptionalSee the type definition and usage context.
Branch 2
FieldTypeRequiredDescription
loggerSubFlowLogger | undefinedOptionalRead-only. Record this call in the supplied logger; omission adds no sub-flow record.
signalAbortSignal | undefinedOptionalRead-only. Additional cancellation can shorten, never replace, the Runner lifetime.
diagnosticsDiagnosticContext | undefinedOptionalRead-only. Parent observation scope for this invocation; never changes execution budget or signal.
preparedPreparedModelCall<unknown> | undefinedOptionalRead-only. Reuse a prepared call; its stage, transport and output reservation then take precedence.
maxOutputTokensnumber | undefinedOptionalRead-only. See the type definition and usage context.
stageIdstring | undefinedOptionalRead-only. See the type definition and usage context.
transportIdRequiredRead-only. See the type definition and usage context.
inputreadonly ModelMessage[] | BuiltContext | ContextPlanRequiredSee the type definition and usage context.
instructionsundefinedOptionalSee the type definition and usage context.

Returns: Promise<ModelResult<RegisteredTransportOutput<Transports, Id>>>.

Awaited value: ModelResult<RegisteredTransportOutput<Transports, Id>>.

Return value structure
FieldTypeRequiredDescription
valueRegisteredTransportOutput<Transports, Id>RequiredRead-only. See the type definition and usage context.
invocationReadonly<ModelCallRecord>RequiredRead-only. See the type definition and usage context.
ts
invoke(request: ModelCallRequest<TransportId | ModelTransportDefinition>): Promise<ModelResult<unknown>>

Invoke without narrowing the transport type.

ParameterTypePassingDescription
requestModelCallRequest<ModelTransportDefinition<unknown> | TransportId>Required—

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

Branch 1
FieldTypeRequiredDescription
loggerSubFlowLogger | undefinedOptionalRead-only. Record this call in the supplied logger; omission adds no sub-flow record.
signalAbortSignal | undefinedOptionalRead-only. Additional cancellation can shorten, never replace, the Runner lifetime.
diagnosticsDiagnosticContext | undefinedOptionalRead-only. Parent observation scope for this invocation; never changes execution budget or signal.
preparedPreparedModelCall<unknown> | undefinedOptionalRead-only. Reuse a prepared call; its stage, transport and output reservation then take precedence.
maxOutputTokensnumber | undefinedOptionalRead-only. See the type definition and usage context.
stageIdstring | undefinedOptionalRead-only. See the type definition and usage context.
transportModelTransportDefinition<unknown> | TransportId | undefinedOptionalRead-only. See the type definition and usage context.
inputstringRequiredSee the type definition and usage context.
instructionsstring | undefinedOptionalSee the type definition and usage context.
Branch 2
FieldTypeRequiredDescription
loggerSubFlowLogger | undefinedOptionalRead-only. Record this call in the supplied logger; omission adds no sub-flow record.
signalAbortSignal | undefinedOptionalRead-only. Additional cancellation can shorten, never replace, the Runner lifetime.
diagnosticsDiagnosticContext | undefinedOptionalRead-only. Parent observation scope for this invocation; never changes execution budget or signal.
preparedPreparedModelCall<unknown> | undefinedOptionalRead-only. Reuse a prepared call; its stage, transport and output reservation then take precedence.
maxOutputTokensnumber | undefinedOptionalRead-only. See the type definition and usage context.
stageIdstring | undefinedOptionalRead-only. See the type definition and usage context.
transportModelTransportDefinition<unknown> | TransportId | undefinedOptionalRead-only. See the type definition and usage context.
inputreadonly ModelMessage[] | BuiltContext | ContextPlanRequiredSee the type definition and usage context.
instructionsundefinedOptionalSee the type definition and usage context.

Returns: Promise<ModelResult<unknown>>.

Awaited value: ModelResult<unknown>.

Return value structure
FieldTypeRequiredDescription
valueunknownRequiredRead-only. See the type definition and usage context.
invocationReadonly<ModelCallRecord>RequiredRead-only. See the type definition and usage context.

Examples ​

Two inputs for the same output ​

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 });
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);

Add budget and request cancellation ​

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, 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);

Add an independent ordered output receiver ​

Extension · Node.js

sink.output is awaited in order. model.output is a proposal that has not been fully validated, so tool operations proposed by the model must not be executed here.

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

API reference, usage examples and developer guides.