Skip to content
English

PolicyDefinition ​

KIT · interface · 类型合同

One declarative policy.

导入 ​

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

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

定义 ​

ts
interface PolicyDefinition<Input, Kind>

泛型参数 ​

类型参数约束默认类型
Input——
Kindstringstring

字段结构 ​

字段类型必需说明
idstring必需只读。Unique policy id; also reported as a reason when the policy matches.
descriptionstring必需只读。Human-readable explanation; must not be blank.
kindKind必需只读。Policy family used to deduplicate reported kinds.
when(input: Input) => boolean必需Returns whether the policy applies; must be synchronous boolean.

成员目录 ​

成员用途
whenReturns whether the policy applies; must be synchronous boolean.

成员详情 ​

when ​

Returns whether the policy applies; must be synchronous boolean.

ts
PolicyDefinition.when(input: Input): boolean

Returns whether the policy applies; must be synchronous boolean.

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

返回: boolean。

使用示例 ​

为同一答复声明何时必须复核 ​

入门 · Node.js

ts
import { Policies } from '@jangts/agent-core/kits/policies';
const policy = Policies.definePolicySet([
  { id: 'has-write', description: '有写操作就复核', kind: 'review', when: (input: { wrote: boolean }) => input.wrote },
]);
console.log(policy.assess({ wrote: true }));
js
import { Policies } from '@jangts/agent-core/kits/policies';
const policy = Policies.definePolicySet([
    { id: 'has-write', description: '有写操作就复核', kind: 'review', when: (input) => input.wrote },
]);
console.log(policy.assess({ wrote: true }));

增加另一理由及有序恢复 ​

配置 · Node.js

ts
import { Policies } from '@jangts/agent-core/kits/policies';
type Input = { wrote: boolean; uncertain: boolean; cancelled: boolean };
const policy = Policies.definePolicySet<Input, 'review'>([
  { id: 'has-write', description: '有写操作就复核', kind: 'review', when: input => input.wrote },
  { id: 'uncertain', description: '证据不确定就复核', kind: 'review', when: input => input.uncertain },
]);
const recovery = Policies.defineRecoveryChain<Input, string>([
  { id: 'cancelled', description: '取消时不发布答案', tryRecover: input => input.cancelled ? { handled: true, value: '任务已取消' } : { handled: false } },
  { id: 'needs-review', description: '未通过复核时明确说明', tryRecover: input => policy.assess(input).required ? { handled: true, value: '结果还需要复核' } : { handled: false } },
]);
const input = { wrote: true, uncertain: true, cancelled: false };
console.log(policy.assess(input), recovery.tryRecover(input));
js
import { Policies } from '@jangts/agent-core/kits/policies';
const policy = Policies.definePolicySet([
    { id: 'has-write', description: '有写操作就复核', kind: 'review', when: input => input.wrote },
    { id: 'uncertain', description: '证据不确定就复核', kind: 'review', when: input => input.uncertain },
]);
const recovery = Policies.defineRecoveryChain([
    { id: 'cancelled', description: '取消时不发布答案', tryRecover: input => input.cancelled ? { handled: true, value: '任务已取消' } : { handled: false } },
    { id: 'needs-review', description: '未通过复核时明确说明', tryRecover: input => policy.assess(input).required ? { handled: true, value: '结果还需要复核' } : { handled: false } },
]);
const input = { wrote: true, uncertain: true, cancelled: false };
console.log(policy.assess(input), recovery.tryRecover(input));

相关策略源码 ​

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