Skip to content
English

ToolRuntime ​

SUPPORT · interface · 类型合同

Catalog bound to one App request.

导入 ​

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

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

定义 ​

ts
interface ToolRuntime<Tool, App>

泛型参数 ​

类型参数约束默认类型
ToolResultDefinitionResultDefinition
App—unknown

字段结构 ​

字段类型必需说明
toolsToolModelView<App>必需只读。The catalog's model tools, shared with the catalog instance.
eventsreadonly ToolExecutionEvent<string>[]必需只读。Execution events recorded by this runtime so far, in occurrence order.
invokeSync{ <const Id extends Tool["id"]>(request: ToolInvocationRequest<Id>): ToolResult<ToolOutput<Tool, Id>>; (request: ToolInvocationRequest): ToolResult<unknown>; }必需Synchronous invocation; rejects tools declared as asynchronous.
invoke{ <const Id extends Tool["id"]>(request: ToolInvocationRequest<Id>): Promise<ToolResult<ToolOutput<Tool, Id>>>; (request: ToolInvocationRequest): Promise<ToolResult<unknown>>; }必需Asynchronous invocation; still bounded by the bound execution signal and errors.

成员目录 ​

成员用途
invokeSyncSynchronous invocation; rejects tools declared as asynchronous.
invokeAsynchronous invocation; still bounded by the bound execution signal and errors.

成员详情 ​

invokeSync ​

Synchronous invocation; rejects tools declared as asynchronous.

ts
ToolRuntime.invokeSync<const Id extends Tool["id"]>(request: ToolInvocationRequest<Id>): ToolResult<ToolOutput<Tool, Id>>

Synchronous invocation; rejects tools declared as asynchronous.

类型参数约束默认类型
Idstring—
参数类型传递说明
requestToolInvocationRequest<Id>必需—
request 的字段
字段类型必需说明
loggerSubFlowLogger | undefined可省略只读。Explicit recorder for this call, also available to its handler for deliberate use.
toolIdId必需只读。见类型定义及调用场景。
inputunknown必需只读。Raw model arguments; parsed by the tool's input schema before authorization.
signalAbortSignal | undefined可省略只读。Per-call cancellation; can only shorten the surrounding execution lifetime.
diagnosticsDiagnosticContext | undefined可省略只读。见类型定义及调用场景。

返回: ToolResult<ToolOutput<Tool, Id>>。

返回值结构
字段类型必需说明
valueToolOutput<Tool, Id>必需只读。见类型定义及调用场景。
invocationReadonly<ToolCallRecord>必需只读。见类型定义及调用场景。
ts
ToolRuntime.invokeSync(request: ToolInvocationRequest): ToolResult<unknown>
参数类型传递说明
requestToolInvocationRequest<string>必需—
request 的字段
字段类型必需说明
loggerSubFlowLogger | undefined可省略只读。Explicit recorder for this call, also available to its handler for deliberate use.
toolIdstring必需只读。见类型定义及调用场景。
inputunknown必需只读。Raw model arguments; parsed by the tool's input schema before authorization.
signalAbortSignal | undefined可省略只读。Per-call cancellation; can only shorten the surrounding execution lifetime.
diagnosticsDiagnosticContext | undefined可省略只读。见类型定义及调用场景。

返回: ToolResult<unknown>。

返回值结构
字段类型必需说明
valueunknown必需只读。见类型定义及调用场景。
invocationReadonly<ToolCallRecord>必需只读。见类型定义及调用场景。

invoke ​

Asynchronous invocation; still bounded by the bound execution signal and errors.

ts
ToolRuntime.invoke<const Id extends Tool["id"]>(request: ToolInvocationRequest<Id>): Promise<ToolResult<ToolOutput<Tool, Id>>>

Asynchronous invocation; still bounded by the bound execution signal and errors.

类型参数约束默认类型
Idstring—
参数类型传递说明
requestToolInvocationRequest<Id>必需—
request 的字段
字段类型必需说明
loggerSubFlowLogger | undefined可省略只读。Explicit recorder for this call, also available to its handler for deliberate use.
toolIdId必需只读。见类型定义及调用场景。
inputunknown必需只读。Raw model arguments; parsed by the tool's input schema before authorization.
signalAbortSignal | undefined可省略只读。Per-call cancellation; can only shorten the surrounding execution lifetime.
diagnosticsDiagnosticContext | undefined可省略只读。见类型定义及调用场景。

返回: Promise<ToolResult<ToolOutput<Tool, Id>>>。

等待后的值: ToolResult<ToolOutput<Tool, Id>>。

返回值结构
字段类型必需说明
valueToolOutput<Tool, Id>必需只读。见类型定义及调用场景。
invocationReadonly<ToolCallRecord>必需只读。见类型定义及调用场景。
ts
ToolRuntime.invoke(request: ToolInvocationRequest): Promise<ToolResult<unknown>>
参数类型传递说明
requestToolInvocationRequest<string>必需—
request 的字段
字段类型必需说明
loggerSubFlowLogger | undefined可省略只读。Explicit recorder for this call, also available to its handler for deliberate use.
toolIdstring必需只读。见类型定义及调用场景。
inputunknown必需只读。Raw model arguments; parsed by the tool's input schema before authorization.
signalAbortSignal | undefined可省略只读。Per-call cancellation; can only shorten the surrounding execution lifetime.
diagnosticsDiagnosticContext | undefined可省略只读。见类型定义及调用场景。

返回: Promise<ToolResult<unknown>>。

等待后的值: ToolResult<unknown>。

返回值结构
字段类型必需说明
valueunknown必需只读。见类型定义及调用场景。
invocationReadonly<ToolCallRecord>必需只读。见类型定义及调用场景。

使用示例 ​

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

入门 · 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);

相关策略源码 ​

相关定义 ​

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