CompiledTool
SUPPORT · type · 类型合同
Tool definition type derived from a declaration literal, preserving input/output/id.
导入
import type { CompiledTool } from '@jangts/agent-core/support';/** @typedef {import('@jangts/agent-core/support').CompiledTool} CompiledTool */其他公开入口:@jangts/agent-core/types、@jangts/agent-core/structs、@jangts/agent-core/support。
定义
类型: CompiledTool<App, Definition>
类型表达式
type CompiledTool<App, Definition> = Definition extends {
parseOutput(value: unknown): infer Output;
}
? Definition extends { readonly id: infer Id extends string }
? ExecutableTool<
Definition extends { inputSchema: SchemaAdapter<infer Input> } ? Input : unknown,
Output,
App,
Id
>
: never
: never;泛型参数
| 类型参数 | 约束 | 默认类型 |
|---|---|---|
App | — | — |
Definition | — | — |
字段结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
id | string | 必需 | 只读。Stable id; also the name the model calls. |
kind | string | 必需 | 只读。Declared grouping used by registries and documentation; not an authorization boundary. |
description | string | 必需 | 只读。Model-visible explanation of when to call the tool. |
inputSchema | SchemaAdapter<Definition extends { inputSchema: SchemaAdapter<infer Input>; } ? Input : unknown> | 必需 | 只读。Schema applied to raw model arguments before authorize and execute. |
authorize | (input: Definition extends { inputSchema: SchemaAdapter<infer Input>; } ? Input : unknown, context: OperationContext<App>) => MaybePromise<void> | 必需 | Host permission check. Runs before execution and may be asynchronous. |
execute | (input: Definition extends { inputSchema: SchemaAdapter<infer Input>; } ? 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. |
active | ToolCondition<App> | undefined | 可省略 | 只读。Omitted means active. Authorization remains a separate execution boundary. |
asIntent | AsIntent<App> | undefined | 可省略 | 只读。Omitted means this tool is not advertised by withIntent(). |
resolve | (input: Definition extends { inputSchema: SchemaAdapter<infer Input>; } ? Input : unknown, context: Readonly<{ stageId: string; }>) => ToolProposal | 必需 | Converts validated arguments into the value handed back to the caller. Runs inside the model call, so it must return synchronously; throwing rejects the selection. |
成员目录
| 成员 | 用途 |
|---|---|
authorize | Host permission check. Runs before execution and may be asynchronous. |
execute | Executes the already-authorized input; must not repeat authorization decisions. |
parseOutput | Validates untrusted output (for example a cached replay) before it is returned. |
resolve | Converts validated arguments into the value handed back to the caller. Runs inside the model call, so it must return synchronously; throwing rejects the selection. |
成员详情
authorize
Host permission check. Runs before execution and may be asynchronous.
CompiledTool.authorize(input: Definition extends { inputSchema: SchemaAdapter<infer Input>; } ? Input : unknown, context: OperationContext<App>): MaybePromise<void>Host permission check. Runs before execution and may be asynchronous.
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
input | Definition extends { inputSchema: SchemaAdapter<infer Input>; } ? Input : unknown | 必需 | — |
context | OperationContext<App> | 必需 | — |
context 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
app | unknown extends App ? App : Readonly<App> | 必需 | 只读。见类型定义及调用场景。 |
logger | SubFlowLog | undefined | 可省略 | 只读。Explicit optional recorder for this operation; never inferred from diagnostics. |
signal | AbortSignal | 必需 | 只读。Non-optional cancellation signal; never undefined once execution has started. |
diagnostics | DiagnosticContext | 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.
CompiledTool.execute(input: Definition extends { inputSchema: SchemaAdapter<infer Input>; } ? Input : unknown, context: OperationContext<App> & NodeReportingContext): unknownExecutes the already-authorized input; must not repeat authorization decisions.
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
input | Definition extends { inputSchema: SchemaAdapter<infer Input>; } ? Input : unknown | 必需 | — |
context | OperationContext<App> & NodeReportingContext | 必需 | — |
context 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
app | unknown extends App ? App : Readonly<App> | 必需 | 只读。见类型定义及调用场景。 |
logger | SubFlowLog | undefined | 可省略 | 只读。Explicit optional recorder for this operation; never inferred from diagnostics. |
signal | AbortSignal | 必需 | 只读。Non-optional cancellation signal; never undefined once execution has started. |
diagnostics | DiagnosticContext | 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.
CompiledTool.parseOutput(value: unknown): unknownValidates untrusted output (for example a cached replay) before it is returned.
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
value | unknown | 必需 | — |
返回: unknown。
resolve
Converts validated arguments into the value handed back to the caller.
Runs inside the model call, so it must return synchronously; throwing rejects the selection.
CompiledTool.resolve(input: Definition extends { inputSchema: SchemaAdapter<infer Input>; } ? Input : unknown, context: Readonly<{ stageId: string; }>): ToolProposalConverts validated arguments into the value handed back to the caller.
Runs inside the model call, so it must return synchronously; throwing rejects the selection.
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
input | Definition extends { inputSchema: SchemaAdapter<infer Input>; } ? Input : unknown | 必需 | — |
context | Readonly<{ stageId: string; }> | 必需 | — |
context 的字段
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
stageId | string | 必需 | 见类型定义及调用场景。 |
返回: ToolProposal。
返回值结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
toolId | string | 必需 | 只读。见类型定义及调用场景。 |
input | unknown | 必需 | 只读。见类型定义及调用场景。 |
使用示例
- 工具最佳实践:一份定义生成模型 Tool 与执行运行时:把 Schema、授权、执行和输出校验登记一次,按请求绑定 App,避免维护两份工具清单。
- Support:推荐用法的聚合入口:Support 聚合推荐的构造、策略与同源配置生成;Core 运行不依赖这个命名空间。
定义并使用一项只读工具
入门 · Node.js
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());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
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);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);