WorkflowRoute
CORE · type · Type contract
One route out of a node.
Routes are evaluated in array order and the first match wins. A route either continues at targetNodeId or completes the workflow through complete; declaring both is invalid.
Import
ts
import type { WorkflowRoute } from '@jangts/agent-core/engine';js
/** @typedef {import('@jangts/agent-core/engine').WorkflowRoute} WorkflowRoute */Other public entry points:@jangts/agent-core, @jangts/agent-core/engine, @jangts/agent-core/types, @jangts/agent-core/structs.
Definition
Type: WorkflowRoute<State, Result, Runtime>
Type parameters
| Type parameter | Constraint | Default type |
|---|---|---|
State | — | — |
Result | — | — |
Runtime | — | undefined |
Union branch structure
Optional shapes of WorkflowRoute: choose one shape; required fields for each branch are listed separately.
Branch 1
| Field | Type | Required | Description |
|---|---|---|---|
id | string | undefined | Optional | Read-only. Stable route id reported in events; required only when hosts need to audit the choice. |
description | string | undefined | Optional | Read-only. See the type definition and usage context. |
conditionId | string | undefined | Optional | Read-only. Stable condition id reported when this route was selected. |
when | ((state: Readonly<State>, context: WorkflowContext<Runtime>) => boolean) | undefined | Optional | Read-only. Omission means unconditional selection at this position; otherwise must return boolean now. |
targetNodeId | string | Required | Read-only. See the type definition and usage context. |
complete | undefined | Optional | Read-only. See the type definition and usage context. |
Branch 2
| Field | Type | Required | Description |
|---|---|---|---|
id | string | undefined | Optional | Read-only. Stable route id reported in events; required only when hosts need to audit the choice. |
description | string | undefined | Optional | Read-only. See the type definition and usage context. |
conditionId | string | undefined | Optional | Read-only. Stable condition id reported when this route was selected. |
when | ((state: Readonly<State>, context: WorkflowContext<Runtime>) => boolean) | undefined | Optional | Read-only. Omission means unconditional selection at this position; otherwise must return boolean now. |
targetNodeId | undefined | Optional | Read-only. See the type definition and usage context. |
complete | (state: Readonly<State>, context: WorkflowContext<Runtime>) => Result | Required | Read-only. See the type definition and usage context. |
Examples
- Workflow: nodes, routes, and partial state updates: Use a single node definition that actually participates in execution to show order, branches, error destinations, and end conditions together.
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' }));