CoreStep
CORE · type · 类型合同
One loop iteration. Must be re-entrant: the same step object runs every round.
导入
ts
import type { CoreStep } from '@jangts/agent-core/engine';js
/** @typedef {import('@jangts/agent-core/engine').CoreStep} CoreStep */其他公开入口:@jangts/agent-core、@jangts/agent-core/engine、@jangts/agent-core/types、@jangts/agent-core/structs。
定义
ts
type CoreStep<Result> = (
roundIndex: number,
context: ExecutionContext & import('../debug.js').NodeReportingContext & CacheContext,
) => MaybePromise<StepOutcome<Result>>;泛型参数
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
Result | — | — |
调用
ts
CoreStep(roundIndex: number, context: ExecutionContext & import("../debug.js").NodeReportingContext & CacheContext): MaybePromise<StepOutcome<Result>>| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
roundIndex | number | 必需 | — |
context | ExecutionContext & NodeReportingContext & CacheContext | 必需 | — |
context 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
logger | SubFlowLog | undefined | 可省略 | 只读。Explicit optional recorder for this operation; never inferred from diagnostics. |
signal | AbortSignal | 必需 | 只读。Non-optional cancellation signal; never undefined once execution has started. |
diagnostics | DiagnosticContext | undefined | 可省略 | 只读。Diagnostic scope of the enclosing call, when recording is enabled. |
report | (update: import("./execution/workflow.js").WorkflowNodeReport) => void | 必需 | Reports progress for the active node; Core supplies the execution identity. |
createSubFlowLogger | () => SubFlowLogger | 必需 | Bound to this context and owned by runtime; repeated calls in one visit return the same instance. |
cache | NodeCache | 必需 | 只读。见类型定义及调用场景。 |
agentCache | NodeAgentCache | undefined | 可省略 | 只读。见类型定义及调用场景。 |
返回: MaybePromise<StepOutcome<Result>>。
等待后的值: StepOutcome<Result>。
返回值的可选结构: 选择其中一种结构,各分支的字段要求分别列出。
分支 1
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
type | "continue" | 必需 | 只读。见类型定义及调用场景。 |
分支 2
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
type | "complete" | 必需 | 只读。见类型定义及调用场景。 |
value | Result | 必需 | 只读。见类型定义及调用场景。 |
使用示例
- Agent:先运行,再逐步绑定 Tool 与依赖:Engine 执行有界循环;绑定 Workflow 后,每次只传状态中变化的数据。
一个普通步骤即可运行
入门 · Node.js
ts
import { AgentCoreEngine } from '@jangts/agent-core/engine';
const agent = new AgentCoreEngine<string>();
const answer = await agent.run(() => ({ type: 'complete', value: '你好,Core' }));
console.log(answer);js
import { AgentCoreEngine } from '@jangts/agent-core/engine';
const agent = new AgentCoreEngine();
const answer = await agent.run(() => ({ type: 'complete', value: '你好,Core' }));
console.log(answer);增加显式 Workflow,仍然调用 run
配置 · 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 agent = new AgentCoreEngine({ workflow });
console.log(await agent.run({ name: 'Ivan' }));
console.log(await agent.run({ name: '访客' }));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 agent = new AgentCoreEngine({ workflow });
console.log(await agent.run({ name: 'Ivan' }));
console.log(await agent.run({ name: '访客' }));