Skip to content
中文

ToolsOptions ​

KIT · interface · Type contract

Construction options for a tool registry.

Import ​

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

Other public entry points:@jangts/agent-core, @jangts/agent-core/types, @jangts/agent-core/structs, @jangts/agent-core/kits/tools, @jangts/agent-core/kits.

Definition ​

ts
interface ToolsOptions<Types, Declaration>

Extends or implements: ToolInvocationPorts<Types['context']>.

Type parameters ​

Type parameterConstraintDefault type
TypesToolRuntimeTypes—
DeclarationToolRegistration<Types>—

Field structure ​

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

Member index ​

MemberPurpose
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.

Member details ​

invocationId ​

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

ts
ToolsOptions.invocationId(decision: Types["decision"]): string
ParameterTypePassingDescription
decisionTypes["decision"]Required—

Returns: string.

outputs ​

Output protocols by id, used to validate invocation outputs.

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

No call parameters.

Returns: Iterable<readonly [string, ToolProtocol<Declaration>]>.

contextFragment ​

Renders a model field into a context fragment.

ts
ToolsOptions.contextFragment(tool: Declaration, field: ToolModelField<Types["placement"]>): ContextFragment
ParameterTypePassingDescription
toolDeclarationRequired—
fieldToolModelField<Types["placement"]>Required—
Fields of field
FieldTypeRequiredDescription
lifetimeContextLifetime | undefinedOptionalValidity window of the produced fragment.
maxCharsnumber | undefinedOptionalCharacter budget of the produced fragment.
placementTypes["placement"]RequiredPlacement group this field belongs to; only that placement consumes it.
ordernumberRequiredOrdering key; lower values are emitted first within a placement.
keystringRequiredKey inside the merged model field record; later entries win.
valueunknownRequiredField value, rendered by the registry's fragment factory.

Returns: ContextFragment.

Return value structure
FieldTypeRequiredDescription
idstringRequiredUnique, stable id matching ^[a-z][a-zA-Z0-9:._-]*$; duplicates are rejected.
sourceIdstringRequiredOwning source (module or registration) used to explain where the content came from.
trustContextTrustRequiredProducer trust level. user and tool content cannot become a system message.
lifetimeContextLifetimeRequiredDeclared validity window of this content.
maxCharsnumberRequiredHard upper bound in JSON characters; must be a positive safe integer.
role"system" | "user"RequiredMessage slot the fragment occupies when assembled.
valuestring | Record<string, unknown>RequiredRendered 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[]

No call parameters.

Returns: readonly string[].

Examples ​

Register one read-only tool for an existing App ​

Beginner · 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 }));

Add App rules and presentation contribution to the same registration ​

Configuration · Node.js

active affects real tool availability and description; business authorization must still verify server-side identity at the actual invocation boundary.

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 }));

API reference, usage examples and developer guides.