Skip to content
English

ToolRegistration ​

KIT · type · 类型合同

Declaration form; the registry derives the ModelTool projection from existing handlers.

导入 ​

ts
import type { ToolRegistration } from '@jangts/agent-core/kits/tools';
js
/** @typedef {import('@jangts/agent-core/kits/tools').ToolRegistration} ToolRegistration */

其他公开入口:@jangts/agent-core、@jangts/agent-core/types、@jangts/agent-core/structs、@jangts/agent-core/kits/tools、@jangts/agent-core/kits。

定义 ​

类型: ToolRegistration<Types>

类型表达式
ts
type ToolRegistration<Types extends ToolRuntimeTypes> = Omit<
  Tool<Types>,
  'inputSchema' | 'resolve'
> &
  Partial<Pick<Tool<Types>, 'inputSchema' | 'resolve'>>;

泛型参数 ​

类型参数约束默认类型
TypesToolRuntimeTypes—

字段结构 ​

字段类型必需说明
idstring必需只读。见类型定义及调用场景。
descriptionstring必需只读。见类型定义及调用场景。
activeToolCondition<Types["context"]> | undefined可省略只读。Omitted means active. Authorization remains a separate execution boundary.
asIntentAsIntent<Types["context"]> | undefined可省略只读。Omitted means this tool is not advertised by withIntent().
kindstring必需只读。见类型定义及调用场景。
execute(input: unknown, context: ToolExecutionContext) => unknown必需见类型定义及调用场景。
debugSteps((result: unknown, context: NodeReportingContext) => void) | undefined可省略只读。见类型定义及调用场景。
parseOutput(value: unknown) => unknown必需见类型定义及调用场景。
parseInput(value: unknown) => unknown必需见类型定义及调用场景。
goalIdsreadonly string[] | "all"必需只读。Goal ids this tool serves, or 'all' when it is always available.
audiencestring必需只读。Audience this tool is described to; validated against the registry policy.
effectstring必需只读。Declared effect class, for example read-only or mutating.
delegatestring | undefined可省略只读。Delegate id this tool answers, when a node delegates execution by name.
lifecycleLifecycleHooks<Types, LifecycleProtocol> | undefined可省略只读。Lifecycle hooks this tool declares for a request.
resolve((input: unknown, context: Readonly<{ stageId: string; }>) => unknown) | undefined可省略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.
inputSchemaSchemaAdapter<unknown> | undefined可省略只读。Schema describing the arguments; must be an object schema.

成员目录 ​

成员用途
execute完整输入输出见下方重载。
parseOutput完整输入输出见下方重载。
parseInput完整输入输出见下方重载。

成员详情 ​

execute ​

ts
ToolRegistration.execute(input: unknown, context: ToolExecutionContext): unknown
参数类型传递说明
inputunknown必需—
contextToolExecutionContext必需—
context 的字段
字段类型必需说明
loggerScopedSubFlowLogger | undefined可省略只读。Delegate bound by Core to this actual invocation.
diagnosticsDiagnosticContext | undefined可省略只读。Diagnostic scope of the invocation, when recording is enabled.
mode"sync" | "async"必需只读。Declared execution discipline; mirrors whether the registry accepted an async tool.
invocationIdstring必需只读。Opaque invocation id shared by receipt, events and diagnostics.
signalAbortSignal必需只读。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。

parseOutput ​

ts
ToolRegistration.parseOutput(value: unknown): unknown
参数类型传递说明
valueunknown必需—

返回: unknown。

parseInput ​

ts
ToolRegistration.parseInput(value: unknown): unknown
参数类型传递说明
valueunknown必需—

返回: unknown。

使用示例 ​

为已有 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 }));

相关策略源码 ​

相关定义 ​

接口参考、使用示例与开发指南。