node
SUPPORT · function · 运行时值
Instantiate a node definition class once, injecting its id; graph validation stays in Workflow. Bind a named host method as run without renaming or manually binding the original object.
导入
ts
import { node } from '@jangts/agent-core/support/decorators';js
import { node } from '@jangts/agent-core/support/decorators';其他公开入口:@jangts/agent-core/support/decorators。
定义
泛型参数
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
State | — | any |
Result | — | unknown |
App | — | any |
重载 1
ts
node<State = any, Result = unknown, App = any>(id: string, options?: DecorationOptions): ClassDecoration<WorkflowNode<State, Result, App>>Instantiate a node definition class once, injecting its id; graph validation stays in Workflow.
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
State | — | any |
Result | — | unknown |
App | — | any |
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
id | string | 必需 | — |
options | DecorationOptions | undefined | 可省略 | — |
参数 options的可选结构: 选择其中一种结构,各分支的字段要求分别列出。
分支 1
undefined
分支 2
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
ns | string | readonly string[] | undefined | 可省略 | 只读。见类型定义及调用场景。 |
返回: ClassDecoration<WorkflowNode<State, Result, App>>。
重载 2
ts
node<State = any, Result = unknown, App = any>(id: string, options: MethodOptions<WorkflowNode<State, Result, App>, "run">): MethodDecoration<NonNullable<WorkflowNode<State, Result, App>["run"]>>Bind a named host method as run without renaming or manually binding the original object.
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
State | — | any |
Result | — | unknown |
App | — | any |
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
id | string | 必需 | — |
options | MethodOptions<WorkflowNode<State, Result, App>, "run"> | 必需 | — |
options 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
description | string | 必需 | 只读。Required explanation of what the node does; also used in diagnostics. |
name | string | undefined | 可省略 | 只读。Display name; defaults to id when omitted. |
requires | readonly (keyof State & string)[] | undefined | 可省略 | 只读。State keys that must be present before this node runs; enforced by the engine. |
provides | readonly (keyof State & string)[] | undefined | 可省略 | 只读。State keys this node is expected to produce; validated after run. |
routes | readonly WorkflowRoute<State, Result, App>[] | 必需 | 只读。Candidate successors, evaluated in order. A getter must be pure; registration reads it once. |
report | ((update: void | Readonly<Partial<State>>, context: NodeReportingContext) => WorkflowNodeReport) | undefined | 可省略 | 只读。Optional application-reported detail, collected only when content logging is enabled. |
startsRound | boolean | undefined | 可省略 | 只读。Reserves a fresh round on entry. Use it for steps that may legitimately repeat; entering such a node when the round budget is exhausted fails with the configured exhausted error. |
onErrorNodeId | string | undefined | 可省略 | 只读。Node entered when run/provides/route fails; contract errors always propagate instead. |
onErrorRouteId | string | undefined | 可省略 | 只读。Route id reported with the recovery transition. |
ns | string | readonly string[] | undefined | 可省略 | 只读。见类型定义及调用场景。 |
返回: MethodDecoration<(state: Readonly<State>, context: WorkflowContext<App>) => void \| Partial<State> \| Promise<void \| Partial<State>>>。
使用示例
- Workflow:节点、路由与局部状态更新:用一份真实参与运行的节点定义,集中展示执行顺序、分支、错误去向和结束条件。
- 装饰器:处理类与已有实例方法:类装饰代办定义实例化,方法装饰绑定原对象;输出仍走原 define、构造和 bind。
一个节点完成问候
入门 · 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' }));同一节点合同可由类实现
扩展 · Node.js
ts
import { AgentCoreEngine, defineWorkflow, route } from '@jangts/agent-core/engine';
type State = { name: string; answer?: string };
class GreetingNode {
readonly id = 'greet';
readonly description = '根据姓名生成问候';
readonly startsRound = true;
readonly routes = [route.deliver('answer')];
#prefix = '你好,';
run(state: Readonly<State>) { return { answer: this.#prefix + state.name }; }
}
const workflow = defineWorkflow<State, string>({ id: 'greeting', nodes: [new GreetingNode()] });
console.log(await new AgentCoreEngine({ workflow }).run({ name: 'Ivan' }));js
import { AgentCoreEngine, defineWorkflow, route } from '@jangts/agent-core/engine';
class GreetingNode {
id = 'greet';
description = '根据姓名生成问候';
startsRound = true;
routes = [route.deliver('answer')];
#prefix = '你好,';
run(state) { return { answer: this.#prefix + state.name }; }
}
const workflow = defineWorkflow({ id: 'greeting', nodes: [new GreetingNode()] });
console.log(await new AgentCoreEngine({ workflow }).run({ name: 'Ivan' }));