ErasedTool
CORE · interface · 类型合同
Type-erased tool shape the registry executes after parsing at the boundary.
导入
ts
import type { ErasedTool } from '@jangts/agent-core/tools';js
/** @typedef {import('@jangts/agent-core/tools').ErasedTool} ErasedTool */其他公开入口:@jangts/agent-core、@jangts/agent-core/types、@jangts/agent-core/structs、@jangts/agent-core/tools。
定义
ts
interface ErasedTool字段结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
id | string | 必需 | 只读。见类型定义及调用场景。 |
kind | string | 必需 | 只读。见类型定义及调用场景。 |
description | string | undefined | 可省略 | 只读。Optional here because the erased view does not require model-facing prose. |
debugSteps | ((result: unknown, context: NodeReportingContext) => void) | undefined | 可省略 | 只读。见类型定义及调用场景。 |
parseInput | (value: unknown) => unknown | 必需 | 见类型定义及调用场景。 |
parseOutput | (value: unknown) => unknown | 必需 | 见类型定义及调用场景。 |
execute | (input: unknown, context: ToolExecutionContext) => unknown | 必需 | 见类型定义及调用场景。 |
成员目录
| 成员 | 用途 |
|---|---|
parseInput | 完整输入输出见下方重载。 |
parseOutput | 完整输入输出见下方重载。 |
execute | 完整输入输出见下方重载。 |
成员详情
parseInput
ts
ErasedTool.parseInput(value: unknown): unknown| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
value | unknown | 必需 | — |
返回: unknown。
parseOutput
ts
ErasedTool.parseOutput(value: unknown): unknown| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
value | unknown | 必需 | — |
返回: unknown。
execute
ts
ErasedTool.execute(input: unknown, context: ToolExecutionContext): unknown| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
input | unknown | 必需 | — |
context | ToolExecutionContext | 必需 | — |
context 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
logger | ScopedSubFlowLogger | undefined | 可省略 | 只读。Delegate bound by Core to this actual invocation. |
diagnostics | DiagnosticContext | undefined | 可省略 | 只读。Diagnostic scope of the invocation, when recording is enabled. |
mode | "sync" | "async" | 必需 | 只读。Declared execution discipline; mirrors whether the registry accepted an async tool. |
invocationId | string | 必需 | 只读。Opaque invocation id shared by receipt, events and diagnostics. |
signal | AbortSignal | 必需 | 只读。Non-optional cancellation for this invocation. |
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. |
返回: unknown。
使用示例
- Agent:先运行,再逐步绑定 Tool 与依赖:Engine 执行有界循环;绑定 Workflow 后,每次只传状态中变化的数据。
- 复用基础 Workflow 与 Agent 分支:add 保留旧 Workflow,生成组合后的新 Workflow;fork 继承已有绑定及分支来源。
- 一次绑定,单次多模型与跨次切换:构造时提供稳定模型配置;请求用配置 ID 选择,不反复注入 Provider。
- 真实工具调用与请求内运行时:Core 注册并执行真实工具,返回带 invocation 的回执;业务授权由 App 注册的实现拥有。
- 装饰器:处理类与已有实例方法:类装饰代办定义实例化,方法装饰绑定原对象;输出仍走原 define、构造和 bind。
一个普通步骤即可运行
入门 · 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: '访客' }));