Skip to content
English

WorkflowContext ​

CORE · interface · 类型合同

Execution facts available to every node of a running workflow.

导入 ​

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

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

定义 ​

ts
interface WorkflowContext<Runtime>

继承或实现: OperationContext<Runtime>、NodeReportingContext、CacheContext。

泛型参数 ​

类型参数约束默认类型
Runtime—undefined

字段结构 ​

字段类型必需说明
roundIndexnumber必需只读。Zero-based bounded round; phases within a round do not consume another round.
report(update: WorkflowNodeReport) => void必需Report progress while this node is active; Core supplies its execution identity.
diagnosticsDiagnosticContext | undefined可省略只读。Diagnostic scope of this node visit, when recording is enabled.
modelWorkflowModel必需只读。Model access limited by the workflow's configured calls and budget.
tools{ invoke(request: Omit<import("../capabilities/tools.js").ToolInvocationRequest, "diagnostics">): Promise<import("../invocation.js").ToolResult<unknown>>; }必需只读。见类型定义及调用场景。
errorunknown可省略只读。Failure that led into this node, present only while entering an onErrorNodeId target.
errorEventIdstring | undefined可省略只读。Event id of that failure, for correlation in host logs.
appunknown extends Runtime ? Runtime : Readonly<Runtime>必需只读。见类型定义及调用场景。
loggerSubFlowLog | undefined可省略只读。Explicit optional recorder for this operation; never inferred from diagnostics.
signalAbortSignal必需只读。Non-optional cancellation signal; never undefined once execution has started.
createSubFlowLogger() => SubFlowLogger必需Bound to this context and owned by runtime; repeated calls in one visit return the same instance.
cacheNodeCache必需只读。见类型定义及调用场景。
agentCacheNodeAgentCache | undefined可省略只读。见类型定义及调用场景。

成员目录 ​

成员用途
reportReport progress while this node is active; Core supplies its execution identity.
createSubFlowLoggerBound to this context and owned by runtime; repeated calls in one visit return the same instance.

成员详情 ​

report ​

Report progress while this node is active; Core supplies its execution identity.

ts
WorkflowContext.report(update: WorkflowNodeReport): void

Report progress while this node is active; Core supplies its execution identity.

参数类型传递说明
updateWorkflowNodeReport必需—
update 的字段
字段类型必需说明
usedModelboolean | undefined可省略只读。This visit's own use only; omitted means unknown.
payloadunknown可省略只读。App-defined progress detail; collected only when content logging is enabled.

返回: void。

createSubFlowLogger ​

Bound to this context and owned by runtime; repeated calls in one visit return the same instance.

ts
WorkflowContext.createSubFlowLogger(): SubFlowLogger

Bound to this context and owned by runtime; repeated calls in one visit return the same instance.

无调用参数。

返回: SubFlowLogger。

使用示例 ​

一个节点完成问候 ​

入门 · Node.js

ts
import { AgentCoreEngine, defineWorkflow, route } from '@jangts/agent-core/engine';

type State = { name: string; answer?: string };
const workflow = defineWorkflow<State, string>({
  id: 'greeting',
  nodes: [{
    id: 'greet', description: '根据本次姓名生成问候', startsRound: true,
    run: state => ({ answer: '你好,' + state.name }),
    routes: [route.deliver('answer')],
  }],
});
console.log(workflow.entryNodeId);
console.log(workflow.describe());
js
import { AgentCoreEngine, defineWorkflow, route } from '@jangts/agent-core/engine';
const workflow = defineWorkflow({
    id: 'greeting',
    nodes: [{
            id: 'greet', description: '根据本次姓名生成问候', startsRound: true,
            run: state => ({ answer: '你好,' + state.name }),
            routes: [route.deliver('answer')],
        }],
});
console.log(workflow.entryNodeId);
console.log(workflow.describe());

增加校验阶段,原状态与 run 方式不变 ​

配置 · Node.js

ts
import { AgentCoreEngine, defineWorkflow, route } from '@jangts/agent-core/engine';
type State = { name: string; answer?: string; error?: string };
const workflow = defineWorkflow<State, string>({
  id: 'greeting',
  nodes: [
    { id: 'check', description: '检查姓名', startsRound: true,
      run: state => ({ error: state.name.trim() ? undefined : '姓名不能为空' }),
      routes: [route.toIfError('failure'), route.to('greet')] },
    { id: 'greet', description: '生成问候', provides: ['answer'],
      run: state => ({ answer: '你好,' + state.name }), routes: [route.deliver('answer')] },
    { id: 'failure', description: '返回校验提示', requires: ['error'], routes: [route.deliver('error')] },
  ],
});
console.log(await new AgentCoreEngine({ workflow }).run({ name: 'Ivan' }));
js
import { AgentCoreEngine, defineWorkflow, route } from '@jangts/agent-core/engine';
const workflow = defineWorkflow({
    id: 'greeting',
    nodes: [
        { id: 'check', description: '检查姓名', startsRound: true,
            run: state => ({ error: state.name.trim() ? undefined : '姓名不能为空' }),
            routes: [route.toIfError('failure'), route.to('greet')] },
        { id: 'greet', description: '生成问候', provides: ['answer'],
            run: state => ({ answer: '你好,' + state.name }), routes: [route.deliver('answer')] },
        { id: 'failure', description: '返回校验提示', requires: ['error'], routes: [route.deliver('error')] },
    ],
});
console.log(await new AgentCoreEngine({ workflow }).run({ name: 'Ivan' }));

相关策略源码 ​

相关定义 ​

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