ToolProtocol
KIT · type · 类型合同
Protocol type of a tool's lifecycle invocation.
导入
ts
import type { ToolProtocol } from '@jangts/agent-core/kits/tools';js
/** @typedef {import('@jangts/agent-core/kits/tools').ToolProtocol} ToolProtocol */其他公开入口:@jangts/agent-core、@jangts/agent-core/types、@jangts/agent-core/structs、@jangts/agent-core/kits/tools、@jangts/agent-core/kits。
定义
类型: ToolProtocol<Tool>
类型表达式
ts
type ToolProtocol<Tool extends { lifecycle?: { protocol?: LifecycleProtocol } }> = NonNullable<NonNullable<Tool['lifecycle']>['protocol']>;泛型参数
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
Tool | { lifecycle?: { protocol?: LifecycleProtocol } } | — |
字段结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
version | number | undefined | 可省略 | 只读。Protocol version; compared with the configured protocolVersion when present. |
terminal | boolean | undefined | 可省略 | 只读。Marks an output that ends the interaction. |
modelJsonSchema | Record<string, unknown> | undefined | 可省略 | 只读。Optional schema for model output. |
inputJsonSchema | Record<string, unknown> | undefined | 可省略 | 只读。Optional schema for runtime input. |
fieldSchemas | Record<string, unknown> | undefined | 可省略 | 只读。Optional per-field schemas. |
keys | (value: Record<string, unknown>) => string[] | 必需 | Selector values carried by value. |
成员目录
| 成员 | 用途 |
|---|---|
keys | Selector values carried by value. |
成员详情
keys
Selector values carried by value.
ts
ToolProtocol.keys(value: Record<string, unknown>): string[]Selector values carried by value.
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
value | Record<string, unknown> | 必需 | — |
返回: string[]。
使用示例
- Tools:App 工具生命周期与内容贡献:对已有复杂 App 工具统一登记工具调用、输出规则、上下文贡献和交付处理。
为已有 App 登记一个只读工具
入门 · Node.js
ts
import { Tools } from '@jangts/agent-core/kits/tools';
type Runtime = { decision: { type: string }; context: { canRead: boolean }; composition: string; phase: string; placement: string; normalizerArgs: []; result: string; receiver: undefined };
const tools = new Tools<Runtime>({
registration: { kinds: ['read'], audiences: ['model'], effects: ['read'], isValidId: tool => !!tool.id, goalExists: id => id === 'explain' },
invocationId: decision => decision.type, outputs: () => [], goalIds: () => ['explain'],
contextFragment() { throw new Error('这个工具没有登记上下文字段'); },
});
tools.registerAll([{ id: 'read-source', kind: 'read', description: '读取已允许说明', audience: 'model', effect: 'read', goalIds: ['explain'], parseInput: value => value, parseOutput: value => value, execute: () => '接口说明' }]).seal();
console.log(tools.invoke({ toolId: 'read-source', input: {} }).value);
console.log(tools.describe('model', { canRead: true }));js
import { Tools } from '@jangts/agent-core/kits/tools';
const tools = new Tools({
registration: { kinds: ['read'], audiences: ['model'], effects: ['read'], isValidId: tool => !!tool.id, goalExists: id => id === 'explain' },
invocationId: decision => decision.type, outputs: () => [], goalIds: () => ['explain'],
contextFragment() { throw new Error('这个工具没有登记上下文字段'); },
});
tools.registerAll([{ id: 'read-source', kind: 'read', description: '读取已允许说明', audience: 'model', effect: 'read', goalIds: ['explain'], parseInput: value => value, parseOutput: value => value, execute: () => '接口说明' }]).seal();
console.log(tools.invoke({ toolId: 'read-source', input: {} }).value);
console.log(tools.describe('model', { canRead: true }));同一注册方式增加 App 规则和展示贡献
配置 · Node.js
active 影响真实工具可用性及描述;业务授权仍必须在实际调用边界核对服务端身份。
ts
import { Tools } from '@jangts/agent-core/kits/tools';
type Runtime = { decision: { type: string }; context: { canRead: boolean }; composition: string; phase: string; placement: string; normalizerArgs: []; result: string; receiver: undefined };
const tools = new Tools<Runtime>({
registration: { kinds: ['read'], audiences: ['model'], effects: ['read'], isValidId: tool => !!tool.id, goalExists: id => id === 'explain' },
invocationId: decision => decision.type, outputs: () => [], goalIds: () => ['explain'],
contextFragment() { throw new Error('这个工具没有登记上下文字段'); },
});
tools.registerAll([{ id: 'read-source', kind: 'read', description: '读取已允许说明', audience: 'model', effect: 'read', goalIds: ['explain'], active: app => app.canRead,
parseInput: value => value, parseOutput: value => value, execute: () => '接口说明',
lifecycle: { rules: () => [{ order: 10, text: '只根据真实读取结果回答。' }], visibleText: text => text.trim() },
}]).seal();
console.log(tools.taskRules({ canRead: true }));
console.log(tools.visibleText(' 已确认说明 ', { canRead: true }));
console.log(tools.describe('model', { canRead: false }));js
import { Tools } from '@jangts/agent-core/kits/tools';
const tools = new Tools({
registration: { kinds: ['read'], audiences: ['model'], effects: ['read'], isValidId: tool => !!tool.id, goalExists: id => id === 'explain' },
invocationId: decision => decision.type, outputs: () => [], goalIds: () => ['explain'],
contextFragment() { throw new Error('这个工具没有登记上下文字段'); },
});
tools.registerAll([{ id: 'read-source', kind: 'read', description: '读取已允许说明', audience: 'model', effect: 'read', goalIds: ['explain'], active: app => app.canRead,
parseInput: value => value, parseOutput: value => value, execute: () => '接口说明',
lifecycle: { rules: () => [{ order: 10, text: '只根据真实读取结果回答。' }], visibleText: text => text.trim() },
}]).seal();
console.log(tools.taskRules({ canRead: true }));
console.log(tools.visibleText(' 已确认说明 ', { canRead: true }));
console.log(tools.describe('model', { canRead: false }));