AgentBranch
CORE · interface · 类型合同
Immutable identity of one branch of an agent.
The three required ids are generated per engine unless the host restores a branch; the optional ids are only present on a fork and record the exact origin, so a forked run can be attributed without consulting external storage.
导入
ts
import type { AgentBranch } from '@jangts/agent-core/engine';js
/** @typedef {import('@jangts/agent-core/engine').AgentBranch} AgentBranch */其他公开入口:@jangts/agent-core、@jangts/agent-core/engine、@jangts/agent-core/types。
定义
ts
interface AgentBranch字段结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
agentId | string | 必需 | 只读。Agent the branch belongs to. |
branchId | string | 必需 | 只读。Branch identifier, unique within the agent. |
agentVersionId | string | 必需 | 只读。Agent version the branch was created from. |
parentBranchId | string | undefined | 可省略 | 只读。Parent branch id; present only for a fork. |
parentAgentVersionId | string | undefined | 可省略 | 只读。Agent version of the parent branch; present only for a fork. |
forkPointId | string | undefined | 可省略 | 只读。Checkpoint the branch was forked at; present only for a fork. |
使用示例
- 复用基础 Workflow 与 Agent 分支:add 保留旧 Workflow,生成组合后的新 Workflow;fork 继承已有绑定及分支来源。
基础 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')],
}],
});
const extended = workflow.add({
nodes: [{ id: 'format', description: '为现有回答增加结尾',
run: state => ({ answer: state.answer + '!' }), routes: [route.deliver('answer')] }],
routes: { greet: [route.to('format')] },
});
console.log(await new AgentCoreEngine({ workflow: extended }).run({ name: 'Ivan' }));
console.log(workflow.ids()); // 基础定义仍然只有 greetjs
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')],
}],
});
const extended = workflow.add({
nodes: [{ id: 'format', description: '为现有回答增加结尾',
run: state => ({ answer: state.answer + '!' }), routes: [route.deliver('answer')] }],
routes: { greet: [route.to('format')] },
});
console.log(await new AgentCoreEngine({ workflow: extended }).run({ name: 'Ivan' }));
console.log(workflow.ids()); // 基础定义仍然只有 greet继承基础 Agent 的绑定与分支来源
配置 · 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')],
}],
});
const base = new AgentCoreEngine({ workflow });
const checkpoint = base.checkpoint({ name: 'Ivan' });
const branch = base.fork({ at: checkpoint });
console.log(await branch.run(checkpoint.read()));
console.log(branch.branch.parentBranchId === base.branch.branchId);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')],
}],
});
const base = new AgentCoreEngine({ workflow });
const checkpoint = base.checkpoint({ name: 'Ivan' });
const branch = base.fork({ at: checkpoint });
console.log(await branch.run(checkpoint.read()));
console.log(branch.branch.parentBranchId === base.branch.branchId);