Skip to content
中文

ToolRuntime ​

SUPPORT · interface · Type contract

Catalog bound to one App request.

Import ​

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

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

Definition ​

ts
interface ToolRuntime<Tool, App>

Type parameters ​

Type parameterConstraintDefault type
ToolResultDefinitionResultDefinition
App—unknown

Field structure ​

FieldTypeRequiredDescription
toolsToolModelView<App>RequiredRead-only. The catalog's model tools, shared with the catalog instance.
eventsreadonly ToolExecutionEvent<string>[]RequiredRead-only. 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>; }RequiredSynchronous 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>>; }RequiredAsynchronous invocation; still bounded by the bound execution signal and errors.

Member index ​

MemberPurpose
invokeSyncSynchronous invocation; rejects tools declared as asynchronous.
invokeAsynchronous invocation; still bounded by the bound execution signal and errors.

Member details ​

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.

Type parameterConstraintDefault type
Idstring—
ParameterTypePassingDescription
requestToolInvocationRequest<Id>Required—
Fields of request
FieldTypeRequiredDescription
loggerSubFlowLogger | undefinedOptionalRead-only. Explicit recorder for this call, also available to its handler for deliberate use.
toolIdIdRequiredRead-only. See the type definition and usage context.
inputunknownRequiredRead-only. Raw model arguments; parsed by the tool's input schema before authorization.
signalAbortSignal | undefinedOptionalRead-only. Per-call cancellation; can only shorten the surrounding execution lifetime.
diagnosticsDiagnosticContext | undefinedOptionalRead-only. See the type definition and usage context.

Returns: ToolResult<ToolOutput<Tool, Id>>.

Return value structure
FieldTypeRequiredDescription
valueToolOutput<Tool, Id>RequiredRead-only. See the type definition and usage context.
invocationReadonly<ToolCallRecord>RequiredRead-only. See the type definition and usage context.
ts
ToolRuntime.invokeSync(request: ToolInvocationRequest): ToolResult<unknown>
ParameterTypePassingDescription
requestToolInvocationRequest<string>Required—
Fields of request
FieldTypeRequiredDescription
loggerSubFlowLogger | undefinedOptionalRead-only. Explicit recorder for this call, also available to its handler for deliberate use.
toolIdstringRequiredRead-only. See the type definition and usage context.
inputunknownRequiredRead-only. Raw model arguments; parsed by the tool's input schema before authorization.
signalAbortSignal | undefinedOptionalRead-only. Per-call cancellation; can only shorten the surrounding execution lifetime.
diagnosticsDiagnosticContext | undefinedOptionalRead-only. See the type definition and usage context.

Returns: ToolResult<unknown>.

Return value structure
FieldTypeRequiredDescription
valueunknownRequiredRead-only. See the type definition and usage context.
invocationReadonly<ToolCallRecord>RequiredRead-only. See the type definition and usage context.

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.

Type parameterConstraintDefault type
Idstring—
ParameterTypePassingDescription
requestToolInvocationRequest<Id>Required—
Fields of request
FieldTypeRequiredDescription
loggerSubFlowLogger | undefinedOptionalRead-only. Explicit recorder for this call, also available to its handler for deliberate use.
toolIdIdRequiredRead-only. See the type definition and usage context.
inputunknownRequiredRead-only. Raw model arguments; parsed by the tool's input schema before authorization.
signalAbortSignal | undefinedOptionalRead-only. Per-call cancellation; can only shorten the surrounding execution lifetime.
diagnosticsDiagnosticContext | undefinedOptionalRead-only. See the type definition and usage context.

Returns: Promise<ToolResult<ToolOutput<Tool, Id>>>.

Awaited value: ToolResult<ToolOutput<Tool, Id>>.

Return value structure
FieldTypeRequiredDescription
valueToolOutput<Tool, Id>RequiredRead-only. See the type definition and usage context.
invocationReadonly<ToolCallRecord>RequiredRead-only. See the type definition and usage context.
ts
ToolRuntime.invoke(request: ToolInvocationRequest): Promise<ToolResult<unknown>>
ParameterTypePassingDescription
requestToolInvocationRequest<string>Required—
Fields of request
FieldTypeRequiredDescription
loggerSubFlowLogger | undefinedOptionalRead-only. Explicit recorder for this call, also available to its handler for deliberate use.
toolIdstringRequiredRead-only. See the type definition and usage context.
inputunknownRequiredRead-only. Raw model arguments; parsed by the tool's input schema before authorization.
signalAbortSignal | undefinedOptionalRead-only. Per-call cancellation; can only shorten the surrounding execution lifetime.
diagnosticsDiagnosticContext | undefinedOptionalRead-only. See the type definition and usage context.

Returns: Promise<ToolResult<unknown>>.

Awaited value: ToolResult<unknown>.

Return value structure
FieldTypeRequiredDescription
valueunknownRequiredRead-only. See the type definition and usage context.
invocationReadonly<ToolCallRecord>RequiredRead-only. See the type definition and usage context.

Examples ​

Define and use a read-only tool ​

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

Keep the list and add this request's permissions and service dependencies ​

Configuration · 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);

API reference, usage examples and developer guides.