Skip to content
中文

WorkflowContext ​

CORE · interface · Type contract

Execution facts available to every node of a running workflow.

Import ​

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

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

Definition ​

ts
interface WorkflowContext<Runtime>

Extends or implements: OperationContext<Runtime>, NodeReportingContext, CacheContext.

Type parameters ​

Type parameterConstraintDefault type
Runtime—undefined

Field structure ​

FieldTypeRequiredDescription
roundIndexnumberRequiredRead-only. Zero-based bounded round; phases within a round do not consume another round.
report(update: WorkflowNodeReport) => voidRequiredReport progress while this node is active; Core supplies its execution identity.
diagnosticsDiagnosticContext | undefinedOptionalRead-only. Diagnostic scope of this node visit, when recording is enabled.
modelWorkflowModelRequiredRead-only. 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>>; }RequiredRead-only. See the type definition and usage context.
errorunknownOptionalRead-only. Failure that led into this node, present only while entering an onErrorNodeId target.
errorEventIdstring | undefinedOptionalRead-only. Event id of that failure, for correlation in host logs.
appunknown extends Runtime ? Runtime : Readonly<Runtime>RequiredRead-only. See the type definition and usage context.
loggerSubFlowLog | undefinedOptionalRead-only. Explicit optional recorder for this operation; never inferred from diagnostics.
signalAbortSignalRequiredRead-only. Non-optional cancellation signal; never undefined once execution has started.
createSubFlowLogger() => SubFlowLoggerRequiredBound to this context and owned by runtime; repeated calls in one visit return the same instance.
cacheNodeCacheRequiredRead-only. See the type definition and usage context.
agentCacheNodeAgentCache | undefinedOptionalRead-only. See the type definition and usage context.

Member index ​

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

Member details ​

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.

ParameterTypePassingDescription
updateWorkflowNodeReportRequired—
Fields of update
FieldTypeRequiredDescription
usedModelboolean | undefinedOptionalRead-only. This visit's own use only; omitted means unknown.
payloadunknownOptionalRead-only. App-defined progress detail; collected only when content logging is enabled.

Returns: 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.

No call parameters.

Returns: SubFlowLogger.

Examples ​

A single node completes the greeting ​

Beginner · 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());

Add a validation stage without changing state or the run call ​

Configuration · 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' }));

API reference, usage examples and developer guides.