Skip to content
English

ToolsOptions ​

KIT · interface · 类型合同

Construction options for a tool registry.

导入 ​

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

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

定义 ​

ts
interface ToolsOptions<Types, Declaration>

继承或实现: ToolInvocationPorts<Types['context']>。

泛型参数 ​

类型参数约束默认类型
TypesToolRuntimeTypes—
DeclarationToolRegistration<Types>—

字段结构 ​

字段类型必需说明
receiver(() => Types["receiver"]) | undefined可省略只读。Supplies the receiver handed to the finalizer.
executionExecutionOptions | undefined可省略只读。Execution defaults (signal and diagnostics) applied to invocations.
errorsPick<ToolRegistryPorts, "unavailable" | "errorCode"> | undefined可省略只读。Error factories shared by the registry.
registrationPolicy<Declaration>必需只读。Registration policy for every tool.
invocationId(decision: Types["decision"]) => string必需只读。Maps a decision to the invocation id that should handle it.
outputs() => Iterable<readonly [string, ToolProtocol<Declaration>]>必需只读。Output protocols by id, used to validate invocation outputs.
modelOutputs((context: Types["context"]) => Iterable<readonly [string, ToolProtocol<Declaration>]>) | undefined可省略只读。Optional context-dependent output protocols, exposed alongside outputs.
unavailableInvocation((invocationId: string) => Error) | undefined可省略只读。Builds the error for an invocation id with no registered tool.
forbidden(() => Error) | undefined可省略只读。Builds the error for a tool that is currently unavailable.
contextFragment(tool: Declaration, field: ToolModelField<Types["placement"]>) => ContextFragment必需只读。Renders a model field into a context fragment.
goalIds() => readonly string[]必需只读。Current goal ids, used to validate goalIds: 'all' availability.
logger((context: Types["context"]) => import("../debug.js").SubFlowLogger | undefined) | undefined可省略只读。Explicit recorder selected for this invocation only.
diagnostics((context: Types["context"]) => import("../diagnostics.js").DiagnosticContext | undefined) | undefined可省略只读。Diagnostic scope; omitted disables recording for this tool.
signal((context: Types["context"]) => AbortSignal | undefined) | undefined可省略只读。Extra cancellation; must not replace the tool execution signal.
journal((context: Types["context"]) => ToolJournal | undefined) | undefined可省略只读。Journal receiving begin/finish receipts.
checkpoint((context: Types["context"]) => unknown) | undefined可省略只读。Extracts the domain checkpoint recorded with the receipt.
receipt((context: Types["context"], checkpoint: unknown) => unknown) | undefined可省略只读。Builds the domain receipt from a checkpoint after execution.
receiptError((error: unknown) => unknown) | undefined可省略只读。Converts an error into the receipt's replacement value.

成员目录 ​

成员用途
invocationIdMaps a decision to the invocation id that should handle it.
outputsOutput protocols by id, used to validate invocation outputs.
contextFragmentRenders a model field into a context fragment.
goalIdsCurrent goal ids, used to validate goalIds: 'all' availability.

成员详情 ​

invocationId ​

Maps a decision to the invocation id that should handle it.

ts
ToolsOptions.invocationId(decision: Types["decision"]): string
参数类型传递说明
decisionTypes["decision"]必需—

返回: string。

outputs ​

Output protocols by id, used to validate invocation outputs.

ts
ToolsOptions.outputs(): Iterable<readonly [string, ToolProtocol<Declaration>]>

无调用参数。

返回: Iterable<readonly [string, ToolProtocol<Declaration>]>。

contextFragment ​

Renders a model field into a context fragment.

ts
ToolsOptions.contextFragment(tool: Declaration, field: ToolModelField<Types["placement"]>): ContextFragment
参数类型传递说明
toolDeclaration必需—
fieldToolModelField<Types["placement"]>必需—
field 的字段
字段类型必需说明
lifetimeContextLifetime | undefined可省略Validity window of the produced fragment.
maxCharsnumber | undefined可省略Character budget of the produced fragment.
placementTypes["placement"]必需Placement group this field belongs to; only that placement consumes it.
ordernumber必需Ordering key; lower values are emitted first within a placement.
keystring必需Key inside the merged model field record; later entries win.
valueunknown必需Field value, rendered by the registry's fragment factory.

返回: ContextFragment。

返回值结构
字段类型必需说明
idstring必需Unique, stable id matching ^[a-z][a-zA-Z0-9:._-]*$; duplicates are rejected.
sourceIdstring必需Owning source (module or registration) used to explain where the content came from.
trustContextTrust必需Producer trust level. user and tool content cannot become a system message.
lifetimeContextLifetime必需Declared validity window of this content.
maxCharsnumber必需Hard upper bound in JSON characters; must be a positive safe integer.
role"system" | "user"必需Message slot the fragment occupies when assembled.
valuestring | Record<string, unknown>必需Rendered content. A system message accepts a string only, never a record.

goalIds ​

Current goal ids, used to validate goalIds: 'all' availability.

ts
ToolsOptions.goalIds(): readonly string[]

无调用参数。

返回: readonly string[]。

使用示例 ​

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

相关策略源码 ​

相关定义 ​

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