ToolExecutionEvent
CORE · interface · 类型合同
Fact emitted when one tool call settles, correlated by invocationId.
导入
ts
import type { ToolExecutionEvent } from '@jangts/agent-core/tools';js
/** @typedef {import('@jangts/agent-core/tools').ToolExecutionEvent} ToolExecutionEvent */其他公开入口:@jangts/agent-core、@jangts/agent-core/types、@jangts/agent-core/structs、@jangts/agent-core/tools。
定义
ts
interface ToolExecutionEvent<Kind>继承或实现: InvocationMetadata。
泛型参数
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
Kind | string | string |
字段结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
toolId | string | 必需 | 见类型定义及调用场景。 |
invocationId | string | 必需 | Opaque identity shared by result and diagnostic records; not an App message ID. |
kind | Kind | 必需 | 见类型定义及调用场景。 |
durationMs | number | 必需 | 见类型定义及调用场景。 |
status | InvocationStatus | 必需 | 见类型定义及调用场景。 |
errorCode | string | undefined | 可省略 | Error classification when the call failed. |
runId | string | undefined | 可省略 | 只读。Root run this invocation belongs to, when the host runs inside a diagnostics scope. |
spanId | string | undefined | 可省略 | 只读。Diagnostics span of this invocation. |
parentSpanId | string | undefined | 可省略 | 只读。Span that caused this invocation; absent for a root invocation. |
parentInvocationId | string | undefined | 可省略 | 只读。Enclosing invocation when one call triggers another. |
startedAtMs | number | 必需 | 只读。Start instant in epoch milliseconds. |
使用示例
- 真实工具调用与请求内运行时: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({}));