ExecutableTool
CORE · interface · 类型合同
One registered tool, typed by its input, output, host context and id literal.
导入
import type { ExecutableTool } from '@jangts/agent-core/types';/** @typedef {import('@jangts/agent-core/types').ExecutableTool} ExecutableTool */其他公开入口:@jangts/agent-core/types、@jangts/agent-core/structs。
定义
interface ExecutableTool<Input, Output, App, Id>继承或实现: ModelTool<Input, ToolProposal, App>。
泛型参数
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
Input | — | unknown |
Output | — | unknown |
App | — | unknown |
Id | string | string |
字段结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
id | Id | 必需 | 只读。Stable id; also the name the model calls. |
kind | string | 必需 | 只读。Declared grouping used by registries and documentation; not an authorization boundary. |
description | string | 必需 | 只读。Model-visible explanation of when to call the tool. |
inputSchema | SchemaAdapter<Input> | 必需 | 只读。Schema applied to raw model arguments before authorize and execute. |
authorize | (input: Input, context: OperationContext<App>) => MaybePromise<void> | 必需 | Host permission check. Runs before execution and may be asynchronous. |
execute | (input: Input, context: OperationContext<App> & import("../debug.js").NodeReportingContext) => MaybePromise<Output> | 必需 | Executes the already-authorized input; must not repeat authorization decisions. |
debugSteps | ((result: Output, context: import("../debug.js").NodeReportingContext) => void) | undefined | 可省略 | Optional result hook; writes through context.createSubFlowLogger() are recorded immediately. |
parseOutput | (value: unknown) => Output | 必需 | Validates untrusted output (for example a cached replay) before it is returned. |
active | ToolCondition<App> | undefined | 可省略 | 只读。Omitted means active. Authorization remains a separate execution boundary. |
asIntent | AsIntent<App> | undefined | 可省略 | 只读。Omitted means this tool is not advertised by withIntent(). |
resolve | (input: Input, context: Readonly<{ stageId: string; }>) => ToolProposal | 必需 | Converts validated arguments into the value handed back to the caller. Runs inside the model call, so it must return synchronously; throwing rejects the selection. |
成员目录
| 成员 | 用途 |
|---|---|
authorize | Host permission check. Runs before execution and may be asynchronous. |
execute | Executes the already-authorized input; must not repeat authorization decisions. |
parseOutput | Validates untrusted output (for example a cached replay) before it is returned. |
resolve | Converts validated arguments into the value handed back to the caller. Runs inside the model call, so it must return synchronously; throwing rejects the selection. |
成员详情
authorize
Host permission check. Runs before execution and may be asynchronous.
ExecutableTool.authorize(input: Input, context: OperationContext<App>): MaybePromise<void>Host permission check. Runs before execution and may be asynchronous.
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
input | Input | 必需 | — |
context | OperationContext<App> | 必需 | — |
context 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
app | unknown extends App ? App : Readonly<App> | 必需 | 只读。见类型定义及调用场景。 |
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. |
返回: MaybePromise<void>。
等待后的值: void。
execute
Executes the already-authorized input; must not repeat authorization decisions.
ExecutableTool.execute(input: Input, context: OperationContext<App> & import("../debug.js").NodeReportingContext): MaybePromise<Output>Executes the already-authorized input; must not repeat authorization decisions.
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
input | Input | 必需 | — |
context | OperationContext<App> & NodeReportingContext | 必需 | — |
context 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
app | unknown extends App ? App : Readonly<App> | 必需 | 只读。见类型定义及调用场景。 |
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. |
返回: MaybePromise<Output>。
等待后的值: Awaited<Output>。
parseOutput
Validates untrusted output (for example a cached replay) before it is returned.
ExecutableTool.parseOutput(value: unknown): OutputValidates untrusted output (for example a cached replay) before it is returned.
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
value | unknown | 必需 | — |
返回: Output。
等待后的值: Awaited<Output>。
resolve
Converts validated arguments into the value handed back to the caller.
Runs inside the model call, so it must return synchronously; throwing rejects the selection.
ExecutableTool.resolve(input: Input, context: Readonly<{ stageId: string; }>): ToolProposalConverts validated arguments into the value handed back to the caller.
Runs inside the model call, so it must return synchronously; throwing rejects the selection.
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
input | Input | 必需 | — |
context | Readonly<{ stageId: string; }> | 必需 | — |
context 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
stageId | string | 必需 | 见类型定义及调用场景。 |
返回: ToolProposal。
返回值结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
toolId | string | 必需 | 只读。见类型定义及调用场景。 |
input | unknown | 必需 | 只读。见类型定义及调用场景。 |
使用示例
- 真实工具调用与请求内运行时:Core 注册并执行真实工具,返回带 invocation 的回执;业务授权由 App 注册的实现拥有。
最小本地只读工具
入门 · Node.js
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);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 与授权。
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({}));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({}));