Skip to content
English

Policy ​

KIT · interface · 类型合同

Registration policy shared by every tool in the registry.

导入 ​

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

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

定义 ​

ts
interface Policy<Tool>

泛型参数 ​

类型参数约束默认类型
Tool——

字段结构 ​

字段类型必需说明
kindsreadonly string[]必需只读。Accepted values of a tool's kind.
isValidId(tool: Tool) => boolean必需只读。Identity and shape predicate; must be synchronous boolean.
audiencesreadonly string[]必需只读。Accepted audience values.
effectsreadonly string[]必需只读。Accepted effect values.
goalExists(goal: string) => boolean必需只读。Reports whether a goal id exists, for validating goalIds references.
protocolVersionnumber | undefined可省略只读。When set, every lifecycle protocol must declare this version.
tagsreadonly string[] | undefined可省略只读。Diagnostic tags attached to registration rejections.
invalidToolBreakReason | undefined可省略只读。Rejection wording for an invalid tool definition.
invalidInvocationBreakReason | undefined可省略只读。Rejection wording for an invalid or duplicate invocation id.

成员目录 ​

成员用途
isValidIdIdentity and shape predicate; must be synchronous boolean.
goalExistsReports whether a goal id exists, for validating goalIds references.

成员详情 ​

isValidId ​

Identity and shape predicate; must be synchronous boolean.

ts
Policy.isValidId(tool: Tool): boolean
参数类型传递说明
toolTool必需—

返回: boolean。

goalExists ​

Reports whether a goal id exists, for validating goalIds references.

ts
Policy.goalExists(goal: string): boolean
参数类型传递说明
goalstring必需—

返回: boolean。

使用示例 ​

为已有 App 登记一个只读工具 ​

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

同一注册方式增加 App 规则和展示贡献 ​

配置 · Node.js

active 影响真实工具可用性及描述;业务授权仍必须在实际调用边界核对服务端身份。

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

相关策略源码 ​

相关定义 ​

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