Skip to content
中文

WorkflowModel ​

CORE · interface · Type contract

Model and tool access for one workflow invocation.

Every call is charged to the workflow's shared budget. Overloads select the transport: a prepared call, the default plain-text transport, or one registered transport with its typed result.

Import ​

ts
import type { WorkflowModel } from '@jangts/agent-core/engine';
js
/** @typedef {import('@jangts/agent-core/engine').WorkflowModel} WorkflowModel */

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

Definition ​

ts
interface WorkflowModel

Field structure ​

FieldTypeRequiredDescription
invoke{ <Result>(request: WorkflowModelRequest<never> & { readonly prepared: import("../model/runtime.js").PreparedModelCall<Result>; }): Promise<import("../invocation.js").ModelResult<Result>>; (request: WorkflowModelRequest<never>): Promise<import("../invocation.js").ModelResult<string>>; <Result>(request: WorkflowModelRequest<ModelTransportDefinition<Result>> & { readonly transport: ModelTransportDefinition<Result>; }): Promise<import("../invocation.js").ModelResult<Result>>; (request: WorkflowModelRequest<string | ModelTransportDefinition>): Promise<import("../invocation.js").ModelResult<unknown>>; }RequiredSee the type definition and usage context.
prepare{ (request?: import("../model/runtime.js").ModelPrepareRequest<never> & { readonly modelId?: string; }): import("../model/runtime.js").PreparedModelCall<string>; <Result>(request: import("../model/runtime.js").ModelPrepareRequest<ModelTransportDefinition<Result>> & { readonly transport: ModelTransportDefinition<Result>; readonly modelId?: string; }): import("../model/runtime.js").PreparedModelCall<Result>; (request: import("../model/runtime.js").ModelPrepareRequest<string | ModelTransportDefinition> & { readonly modelId?: string; }): import("../model/runtime.js").PreparedModelCall; }RequiredSee the type definition and usage context.
remainingCallsnumberRequiredRead-only. Calls still available to this workflow after ancestor reservations.
callCountnumberRequiredRead-only. Calls completed by this workflow's own runner.
totalCallCountnumberRequiredRead-only. Calls consumed across this workflow and its descendants.

Member index ​

MemberPurpose
invokeSee the overload below for full inputs and outputs.
prepareSee the overload below for full inputs and outputs.

Member details ​

invoke ​

ts
WorkflowModel.invoke<Result>(request: WorkflowModelRequest<never> & { readonly prepared: import("../model/runtime.js").PreparedModelCall<Result>; }): Promise<import("../invocation.js").ModelResult<Result>>
Type parameterConstraintDefault type
Result——
ParameterTypePassingDescription
requestWorkflowModelRequest<never> & { readonly prepared: import("../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.
diagnosticsundefinedOptionalRead-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.
modelIdstring | undefinedOptionalRead-only. See 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.
diagnosticsundefinedOptionalRead-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.
modelIdstring | undefinedOptionalRead-only. See 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
WorkflowModel.invoke(request: WorkflowModelRequest<never>): Promise<import("../invocation.js").ModelResult<string>>
ParameterTypePassingDescription
requestWorkflowModelRequest<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.
diagnosticsundefinedOptionalRead-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.
modelIdstring | undefinedOptionalRead-only. See 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.
diagnosticsundefinedOptionalRead-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.
modelIdstring | undefinedOptionalRead-only. See 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
WorkflowModel.invoke<Result>(request: WorkflowModelRequest<ModelTransportDefinition<Result>> & { readonly transport: ModelTransportDefinition<Result>; }): Promise<import("../invocation.js").ModelResult<Result>>
Type parameterConstraintDefault type
Result——
ParameterTypePassingDescription
requestWorkflowModelRequest<ModelTransportDefinition<Result>> & { readonly 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.
diagnosticsundefinedOptionalRead-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.
modelIdstring | undefinedOptionalRead-only. See 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.
diagnosticsundefinedOptionalRead-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.
modelIdstring | undefinedOptionalRead-only. See 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
WorkflowModel.invoke(request: WorkflowModelRequest<string | ModelTransportDefinition>): Promise<import("../invocation.js").ModelResult<unknown>>
ParameterTypePassingDescription
requestWorkflowModelRequest<string | ModelTransportDefinition<unknown>>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.
diagnosticsundefinedOptionalRead-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.
transportstring | ModelTransportDefinition<unknown> | undefinedOptionalRead-only. See the type definition and usage context.
inputstringRequiredSee the type definition and usage context.
instructionsstring | undefinedOptionalSee the type definition and usage context.
modelIdstring | undefinedOptionalRead-only. See 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.
diagnosticsundefinedOptionalRead-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.
transportstring | ModelTransportDefinition<unknown> | 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.
modelIdstring | undefinedOptionalRead-only. See 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.

prepare ​

ts
WorkflowModel.prepare(request?: import("../model/runtime.js").ModelPrepareRequest<never> & { readonly modelId?: string; }): import("../model/runtime.js").PreparedModelCall<string>
ParameterTypePassingDescription
request(ModelPrepareRequest<never> & { readonly modelId?: string; }) | 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.
modelIdstring | 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
WorkflowModel.prepare<Result>(request: import("../model/runtime.js").ModelPrepareRequest<ModelTransportDefinition<Result>> & { readonly transport: ModelTransportDefinition<Result>; readonly modelId?: string; }): import("../model/runtime.js").PreparedModelCall<Result>
Type parameterConstraintDefault type
Result——
ParameterTypePassingDescription
requestModelPrepareRequest<ModelTransportDefinition<Result>> & { readonly transport: ModelTransportDefinition<Result>; readonly modelId?: string; }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.
modelIdstring | 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
WorkflowModel.prepare(request: import("../model/runtime.js").ModelPrepareRequest<string | ModelTransportDefinition> & { readonly modelId?: string; }): import("../model/runtime.js").PreparedModelCall
ParameterTypePassingDescription
requestModelPrepareRequest<string | ModelTransportDefinition<unknown>> & { readonly modelId?: string; }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.
modelIdstring | 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.

Examples ​

Ordinary calls still only use invoke ​

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

Prepare once only when measurement is needed ​

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

API reference, usage examples and developer guides.