Skip to content
English

WorkflowModel ​

CORE · interface · 类型合同

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.

导入 ​

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

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

定义 ​

ts
interface WorkflowModel

字段结构 ​

字段类型必需说明
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>>; }必需见类型定义及调用场景。
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; }必需见类型定义及调用场景。
remainingCallsnumber必需只读。Calls still available to this workflow after ancestor reservations.
callCountnumber必需只读。Calls completed by this workflow's own runner.
totalCallCountnumber必需只读。Calls consumed across this workflow and its descendants.

成员目录 ​

成员用途
invoke完整输入输出见下方重载。
prepare完整输入输出见下方重载。

成员详情 ​

invoke ​

ts
WorkflowModel.invoke<Result>(request: WorkflowModelRequest<never> & { readonly prepared: import("../model/runtime.js").PreparedModelCall<Result>; }): Promise<import("../invocation.js").ModelResult<Result>>
类型参数约束默认类型
Result——
参数类型传递说明
requestWorkflowModelRequest<never> & { readonly prepared: import("../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.
diagnosticsundefined可省略只读。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可省略见类型定义及调用场景。
modelIdstring | 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.
diagnosticsundefined可省略只读。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可省略见类型定义及调用场景。
modelIdstring | undefined可省略只读。见类型定义及调用场景。

返回: Promise<ModelResult<Result>>。

等待后的值: ModelResult<Result>。

返回值结构
字段类型必需说明
valueResult必需只读。见类型定义及调用场景。
invocationReadonly<ModelCallRecord>必需只读。见类型定义及调用场景。
ts
WorkflowModel.invoke(request: WorkflowModelRequest<never>): Promise<import("../invocation.js").ModelResult<string>>
参数类型传递说明
requestWorkflowModelRequest<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.
diagnosticsundefined可省略只读。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可省略见类型定义及调用场景。
modelIdstring | 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.
diagnosticsundefined可省略只读。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可省略见类型定义及调用场景。
modelIdstring | undefined可省略只读。见类型定义及调用场景。

返回: Promise<ModelResult<string>>。

等待后的值: ModelResult<string>。

返回值结构
字段类型必需说明
valuestring必需只读。见类型定义及调用场景。
invocationReadonly<ModelCallRecord>必需只读。见类型定义及调用场景。
ts
WorkflowModel.invoke<Result>(request: WorkflowModelRequest<ModelTransportDefinition<Result>> & { readonly transport: ModelTransportDefinition<Result>; }): Promise<import("../invocation.js").ModelResult<Result>>
类型参数约束默认类型
Result——
参数类型传递说明
requestWorkflowModelRequest<ModelTransportDefinition<Result>> & { readonly 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.
diagnosticsundefined可省略只读。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可省略见类型定义及调用场景。
modelIdstring | 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.
diagnosticsundefined可省略只读。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可省略见类型定义及调用场景。
modelIdstring | undefined可省略只读。见类型定义及调用场景。

返回: Promise<ModelResult<Result>>。

等待后的值: ModelResult<Result>。

返回值结构
字段类型必需说明
valueResult必需只读。见类型定义及调用场景。
invocationReadonly<ModelCallRecord>必需只读。见类型定义及调用场景。
ts
WorkflowModel.invoke(request: WorkflowModelRequest<string | ModelTransportDefinition>): Promise<import("../invocation.js").ModelResult<unknown>>
参数类型传递说明
requestWorkflowModelRequest<string | ModelTransportDefinition<unknown>>必需—

参数 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.
diagnosticsundefined可省略只读。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可省略只读。见类型定义及调用场景。
transportstring | ModelTransportDefinition<unknown> | undefined可省略只读。见类型定义及调用场景。
inputstring必需见类型定义及调用场景。
instructionsstring | undefined可省略见类型定义及调用场景。
modelIdstring | 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.
diagnosticsundefined可省略只读。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可省略只读。见类型定义及调用场景。
transportstring | ModelTransportDefinition<unknown> | undefined可省略只读。见类型定义及调用场景。
inputreadonly ModelMessage[] | BuiltContext | ContextPlan必需见类型定义及调用场景。
instructionsundefined可省略见类型定义及调用场景。
modelIdstring | undefined可省略只读。见类型定义及调用场景。

返回: Promise<ModelResult<unknown>>。

等待后的值: ModelResult<unknown>。

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

prepare ​

ts
WorkflowModel.prepare(request?: import("../model/runtime.js").ModelPrepareRequest<never> & { readonly modelId?: string; }): import("../model/runtime.js").PreparedModelCall<string>
参数类型传递说明
request(ModelPrepareRequest<never> & { readonly modelId?: string; }) | undefined可省略—

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

分支 1

undefined

分支 2
字段类型必需说明
stageIdstring | undefined可省略只读。见类型定义及调用场景。
transportundefined可省略只读。见类型定义及调用场景。
maxOutputTokensnumber | undefined可省略只读。见类型定义及调用场景。
modelIdstring | 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
WorkflowModel.prepare<Result>(request: import("../model/runtime.js").ModelPrepareRequest<ModelTransportDefinition<Result>> & { readonly transport: ModelTransportDefinition<Result>; readonly modelId?: string; }): import("../model/runtime.js").PreparedModelCall<Result>
类型参数约束默认类型
Result——
参数类型传递说明
requestModelPrepareRequest<ModelTransportDefinition<Result>> & { readonly transport: ModelTransportDefinition<Result>; readonly modelId?: string; }必需—
request 的字段
字段类型必需说明
stageIdstring | undefined可省略只读。见类型定义及调用场景。
transportModelTransportDefinition<Result>必需只读。见类型定义及调用场景。
maxOutputTokensnumber | undefined可省略只读。见类型定义及调用场景。
modelIdstring | 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
WorkflowModel.prepare(request: import("../model/runtime.js").ModelPrepareRequest<string | ModelTransportDefinition> & { readonly modelId?: string; }): import("../model/runtime.js").PreparedModelCall
参数类型传递说明
requestModelPrepareRequest<string | ModelTransportDefinition<unknown>> & { readonly modelId?: string; }必需—
request 的字段
字段类型必需说明
stageIdstring | undefined可省略只读。见类型定义及调用场景。
transportstring | ModelTransportDefinition<unknown> | undefined可省略只读。见类型定义及调用场景。
maxOutputTokensnumber | undefined可省略只读。见类型定义及调用场景。
modelIdstring | 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 ​

入门 · 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);

相关策略源码 ​

相关定义 ​

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