WorkflowRoute
CORE · type · 类型合同
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.
导入
ts
import type { WorkflowRoute } from '@jangts/agent-core/engine';js
/** @typedef {import('@jangts/agent-core/engine').WorkflowRoute} WorkflowRoute */其他公开入口:@jangts/agent-core、@jangts/agent-core/engine、@jangts/agent-core/types、@jangts/agent-core/structs。
定义
类型: WorkflowRoute<State, Result, Runtime>
泛型参数
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
State | — | — |
Result | — | — |
Runtime | — | undefined |
联合分支结构
WorkflowRoute的可选结构: 选择其中一种结构,各分支的字段要求分别列出。
分支 1
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
id | string | undefined | 可省略 | 只读。Stable route id reported in events; required only when hosts need to audit the choice. |
description | string | undefined | 可省略 | 只读。见类型定义及调用场景。 |
conditionId | string | undefined | 可省略 | 只读。Stable condition id reported when this route was selected. |
when | ((state: Readonly<State>, context: WorkflowContext<Runtime>) => boolean) | undefined | 可省略 | 只读。Omission means unconditional selection at this position; otherwise must return boolean now. |
targetNodeId | string | 必需 | 只读。见类型定义及调用场景。 |
complete | undefined | 可省略 | 只读。见类型定义及调用场景。 |
分支 2
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
id | string | undefined | 可省略 | 只读。Stable route id reported in events; required only when hosts need to audit the choice. |
description | string | undefined | 可省略 | 只读。见类型定义及调用场景。 |
conditionId | string | undefined | 可省略 | 只读。Stable condition id reported when this route was selected. |
when | ((state: Readonly<State>, context: WorkflowContext<Runtime>) => boolean) | undefined | 可省略 | 只读。Omission means unconditional selection at this position; otherwise must return boolean now. |
targetNodeId | undefined | 可省略 | 只读。见类型定义及调用场景。 |
complete | (state: Readonly<State>, context: WorkflowContext<Runtime>) => Result | 必需 | 只读。见类型定义及调用场景。 |
使用示例
- Workflow:节点、路由与局部状态更新:用一份真实参与运行的节点定义,集中展示执行顺序、分支、错误去向和结束条件。
一个节点完成问候
入门 · 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' }));