Skip to content
English

ToolCatalogEntry ​

SUPPORT · type · 类型合同

Public declaration surface of one tool, without registry internals.

导入 ​

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

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

定义 ​

类型: ToolCatalogEntry<App, Tool>

类型表达式
ts
type ToolCatalogEntry<App, Tool extends ToolDefinition<unknown, unknown, App>> = Tool extends ToolDefinition<unknown, unknown, App>
    ? Pick<Tool, keyof ToolDefinition<unknown, unknown, App>>
    : never;

泛型参数 ​

类型参数约束默认类型
App——
ToolToolDefinition<unknown, unknown, App>—

字段结构 ​

字段类型必需说明
idstring必需只读。Stable id; also the name the model calls.
descriptionstring必需只读。Model-visible explanation of when to call the tool.
resolve((input: unknown, context: Readonly<{ stageId: string; }>) => ToolProposal) | undefined可省略只读。见类型定义及调用场景。
inputSchemaSchemaAdapter<unknown>必需只读。Schema applied to raw model arguments before authorize and execute.
activeToolCondition<App> | undefined可省略只读。Omitted means active. Authorization remains a separate execution boundary.
asIntentAsIntent<App> | undefined可省略只读。Omitted means this tool is not advertised by withIntent().
kindstring必需只读。Declared grouping used by registries and documentation; not an authorization boundary.
authorize(input: unknown, context: OperationContext<App>) => MaybePromise<void>必需Host permission check. Runs before execution and may be asynchronous.
execute(input: unknown, context: OperationContext<App> & NodeReportingContext) => unknown必需Executes the already-authorized input; must not repeat authorization decisions.
debugSteps((result: unknown, context: import("../debug.js").NodeReportingContext) => void) | undefined可省略Optional result hook; writes through context.createSubFlowLogger() are recorded immediately.
parseOutput(value: unknown) => unknown必需Validates untrusted output (for example a cached replay) before it is returned.

成员目录 ​

成员用途
authorizeHost permission check. Runs before execution and may be asynchronous.
executeExecutes the already-authorized input; must not repeat authorization decisions.
parseOutputValidates untrusted output (for example a cached replay) before it is returned.

成员详情 ​

authorize ​

Host permission check. Runs before execution and may be asynchronous.

ts
ToolCatalogEntry.authorize(input: unknown, context: OperationContext<App>): MaybePromise<void>

Host permission check. Runs before execution and may be asynchronous.

参数类型传递说明
inputunknown必需—
contextOperationContext<App>必需—
context 的字段
字段类型必需说明
appunknown extends App ? App : Readonly<App>必需只读。见类型定义及调用场景。
loggerSubFlowLog | undefined可省略只读。Explicit optional recorder for this operation; never inferred from diagnostics.
signalAbortSignal必需只读。Non-optional cancellation signal; never undefined once execution has started.
diagnosticsDiagnosticContext | undefined可省略只读。Diagnostic scope of the enclosing call, when recording is enabled.

返回: MaybePromise<void>。

等待后的值: void。

execute ​

Executes the already-authorized input; must not repeat authorization decisions.

ts
ToolCatalogEntry.execute(input: unknown, context: OperationContext<App> & NodeReportingContext): unknown

Executes the already-authorized input; must not repeat authorization decisions.

参数类型传递说明
inputunknown必需—
contextOperationContext<App> & NodeReportingContext必需—
context 的字段
字段类型必需说明
appunknown extends App ? App : Readonly<App>必需只读。见类型定义及调用场景。
loggerSubFlowLog | undefined可省略只读。Explicit optional recorder for this operation; never inferred from diagnostics.
signalAbortSignal必需只读。Non-optional cancellation signal; never undefined once execution has started.
diagnosticsDiagnosticContext | undefined可省略只读。Diagnostic scope of the enclosing call, when recording is enabled.
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 ​

Validates untrusted output (for example a cached replay) before it is returned.

ts
ToolCatalogEntry.parseOutput(value: unknown): unknown

Validates untrusted output (for example a cached replay) before it is returned.

参数类型传递说明
valueunknown必需—

返回: unknown。

使用示例 ​

定义并使用一项只读工具 ​

入门 · Node.js

ts
import { Support } from '@jangts/agent-core';
const inputSchema = Support.defineSchema({
  jsonSchema: { type: 'object', properties: {}, additionalProperties: false },
  parse(value: unknown) {
    if (!value || typeof value !== 'object' || Array.isArray(value) || Object.keys(value).length) throw new TypeError('需要空对象');
    return {};
  },
});
const catalog = Support.defineToolCatalog([{ id: 'get_status', kind: 'read', description: '读取服务状态', inputSchema,
  authorize() {},
  execute() { return '服务运行正常'; },
  parseOutput(value: unknown) { if (typeof value !== 'string') throw new TypeError('需要字符串'); return value; },
}]);
const tools = catalog.bind({ app: undefined });
console.log((await tools.invoke({ toolId: 'get_status', input: {} })).value);
console.log(catalog.tools.ids());
js
import { Support } from '@jangts/agent-core';
const inputSchema = Support.defineSchema({
    jsonSchema: { type: 'object', properties: {}, additionalProperties: false },
    parse(value) {
        if (!value || typeof value !== 'object' || Array.isArray(value) || Object.keys(value).length)
            throw new TypeError('需要空对象');
        return {};
    },
});
const catalog = Support.defineToolCatalog([{ id: 'get_status', kind: 'read', description: '读取服务状态', inputSchema,
        authorize() { },
        execute() { return '服务运行正常'; },
        parseOutput(value) { if (typeof value !== 'string')
            throw new TypeError('需要字符串'); return value; },
    }]);
const tools = catalog.bind({ app: undefined });
console.log((await tools.invoke({ toolId: 'get_status', input: {} })).value);
console.log(catalog.tools.ids());

保留清单,增加本次权限与服务依赖 ​

配置 · Node.js

ts
import { Support } from '@jangts/agent-core';
import type { OperationContext } from '@jangts/agent-core/types';
type App = { allowed: boolean; readStatus(): Promise<string> };
const inputSchema = Support.defineSchema({
  jsonSchema: { type: 'object', properties: {}, additionalProperties: false },
  parse(value: unknown) {
    if (!value || typeof value !== 'object' || Array.isArray(value) || Object.keys(value).length) throw new TypeError('需要空对象');
    return {};
  },
});
const catalog = Support.defineToolCatalog<App>([{ id: 'get_status', kind: 'read', description: '读取状态', inputSchema,
  authorize(_input: {}, { app }: OperationContext<App>) { if (!app.allowed) throw new Error('当前请求没有读取权限'); },
  execute(_input: {}, { app }: OperationContext<App>) { return app.readStatus(); },
  parseOutput(value: unknown) { if (typeof value !== 'string') throw new TypeError('需要字符串'); return value; },
}]);
const tools = catalog.bind({ app: { allowed: true, readStatus: async () => '服务运行正常' },
  execution: { signal: AbortSignal.timeout(10000) },
});
console.log((await tools.invoke({ toolId: 'get_status', input: {} })).value);
js
import { Support } from '@jangts/agent-core';
const inputSchema = Support.defineSchema({
    jsonSchema: { type: 'object', properties: {}, additionalProperties: false },
    parse(value) {
        if (!value || typeof value !== 'object' || Array.isArray(value) || Object.keys(value).length)
            throw new TypeError('需要空对象');
        return {};
    },
});
const catalog = Support.defineToolCatalog([{ id: 'get_status', kind: 'read', description: '读取状态', inputSchema,
        authorize(_input, { app }) { if (!app.allowed)
            throw new Error('当前请求没有读取权限'); },
        execute(_input, { app }) { return app.readStatus(); },
        parseOutput(value) { if (typeof value !== 'string')
            throw new TypeError('需要字符串'); return value; },
    }]);
const tools = catalog.bind({ app: { allowed: true, readStatus: async () => '服务运行正常' },
    execution: { signal: AbortSignal.timeout(10000) },
});
console.log((await tools.invoke({ toolId: 'get_status', input: {} })).value);

相关策略源码 ​

相关定义 ​

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