RegisteredTool
CORE · type · 类型合同
Registered snapshot: public data is retained, implementation helpers are not a contract.
导入
ts
import type { RegisteredTool } from '@jangts/agent-core/tools';js
/** @typedef {import('@jangts/agent-core/tools').RegisteredTool} RegisteredTool */其他公开入口:@jangts/agent-core、@jangts/agent-core/types、@jangts/agent-core/structs、@jangts/agent-core/tools。
定义
类型: RegisteredTool<Tool>
类型表达式
ts
type RegisteredTool<Tool extends ErasedTool = ErasedTool> = RegistrationProjection<
Tool,
ErasedTool
>;泛型参数
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
Tool | ErasedTool | ErasedTool |
字段结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
id | (ErasedTool & RegistrationData<Tool> & Pick<Tool, Extract<keyof Tool, keyof ErasedTool>>)["id"] | 必需 | 只读。见类型定义及调用场景。 |
kind | (ErasedTool & RegistrationData<Tool> & Pick<Tool, Extract<keyof Tool, keyof ErasedTool>>)["kind"] | 必需 | 只读。见类型定义及调用场景。 |
description | (ErasedTool & RegistrationData<Tool> & Pick<Tool, Extract<keyof Tool, keyof ErasedTool>>)["description"] | undefined | 可省略 | 只读。Optional here because the erased view does not require model-facing prose. |
debugSteps | (ErasedTool & RegistrationData<Tool> & Pick<Tool, Extract<keyof Tool, keyof ErasedTool>>)["debugSteps"] | undefined | 可省略 | 只读。见类型定义及调用场景。 |
parseInput | (ErasedTool & RegistrationData<Tool> & Pick<Tool, Extract<keyof Tool, keyof ErasedTool>>)["parseInput"] | 必需 | 见类型定义及调用场景。 |
parseOutput | (ErasedTool & RegistrationData<Tool> & Pick<Tool, Extract<keyof Tool, keyof ErasedTool>>)["parseOutput"] | 必需 | 见类型定义及调用场景。 |
execute | (ErasedTool & RegistrationData<Tool> & Pick<Tool, Extract<keyof Tool, keyof ErasedTool>>)["execute"] | 必需 | 见类型定义及调用场景。 |
成员目录
| 成员 | 用途 |
|---|---|
parseInput | 完整输入输出见下方重载。 |
parseOutput | 完整输入输出见下方重载。 |
execute | 完整输入输出见下方重载。 |
成员详情
parseInput
ts
RegisteredTool.parseInput(value: unknown): unknown| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
value | unknown | 必需 | — |
返回: unknown。
parseOutput
ts
RegisteredTool.parseOutput(value: unknown): unknown| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
value | unknown | 必需 | — |
返回: unknown。
execute
ts
RegisteredTool.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。
使用示例
- 真实工具调用与请求内运行时:Core 注册并执行真实工具,返回带 invocation 的回执;业务授权由 App 注册的实现拥有。
最小本地只读工具
入门 · Node.js
ts
import { ToolRegistry } from '@jangts/agent-core/tools';
const tools = new ToolRegistry().register({
id: 'get_status', kind: 'read', description: '读取演示服务状态',
parseInput(value: unknown) { if (!value || typeof value !== 'object' || Array.isArray(value)) throw new TypeError('需要对象'); return {}; },
execute: () => '服务运行正常',
parseOutput(value: unknown) { if (typeof value !== 'string') throw new TypeError('需要文本'); return value; },
}).seal();
const result = tools.invoke({ toolId: 'get_status', input: {} });
console.log(result.value, result.invocation.invocationId);js
import { ToolRegistry } from '@jangts/agent-core/tools';
const tools = new ToolRegistry().register({
id: 'get_status', kind: 'read', description: '读取演示服务状态',
parseInput(value) { if (!value || typeof value !== 'object' || Array.isArray(value))
throw new TypeError('需要对象'); return {}; },
execute: () => '服务运行正常',
parseOutput(value) { if (typeof value !== 'string')
throw new TypeError('需要文本'); return value; },
}).seal();
const result = tools.invoke({ toolId: 'get_status', input: {} });
console.log(result.value, result.invocation.invocationId);提前绑定,节点不再重复注入工具
配置 · Node.js
第一个示例严格校验外部输入;第二个仅展示绑定关系,真实业务应使用相应 Schema 与授权。
ts
import { AgentCoreEngine, defineWorkflow, route } from '@jangts/agent-core/engine';
import { ToolRegistry } from '@jangts/agent-core/tools';
type State = { answer?: unknown };
const tools = new ToolRegistry().register({ id: 'get_status', kind: 'read',
parseInput: () => ({}), execute: async () => '服务运行正常', parseOutput: value => value,
}).seal();
const workflow = defineWorkflow<State, unknown>({ id: 'status', nodes: [{
id: 'read', description: '读取状态', startsRound: true,
async run(_state, { tools }) { const { value } = await tools.invoke({ toolId: 'get_status', input: {} }); return { answer: value }; },
routes: [route.deliver('answer')],
}] });
const agent = new AgentCoreEngine({ workflow }).bindToolRuntime(tools);
console.log(await agent.run({}));js
import { AgentCoreEngine, defineWorkflow, route } from '@jangts/agent-core/engine';
import { ToolRegistry } from '@jangts/agent-core/tools';
const tools = new ToolRegistry().register({ id: 'get_status', kind: 'read',
parseInput: () => ({}), execute: async () => '服务运行正常', parseOutput: value => value,
}).seal();
const workflow = defineWorkflow({ id: 'status', nodes: [{
id: 'read', description: '读取状态', startsRound: true,
async run(_state, { tools }) { const { value } = await tools.invoke({ toolId: 'get_status', input: {} }); return { answer: value }; },
routes: [route.deliver('answer')],
}] });
const agent = new AgentCoreEngine({ workflow }).bindToolRuntime(tools);
console.log(await agent.run({}));