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 | — | — |
Kind | string | string |
字段结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
id | string | 必需 | 只读。Unique policy id; also reported as a reason when the policy matches. |
description | string | 必需 | 只读。Human-readable explanation; must not be blank. |
kind | Kind | 必需 | 只读。Policy family used to deduplicate reported kinds. |
when | (input: Input) => boolean | 必需 | Returns whether the policy applies; must be synchronous boolean. |
成员目录
| 成员 | 用途 |
|---|---|
when | Returns whether the policy applies; must be synchronous boolean. |
成员详情
when
Returns whether the policy applies; must be synchronous boolean.
ts
PolicyDefinition.when(input: Input): booleanReturns whether the policy applies; must be synchronous boolean.
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
input | Input | 必需 | — |
返回: boolean。
使用示例
- Policies:全部命中与首个恢复:策略集收集所有匹配理由;恢复链按顺序选择第一个显式 handled 结果。
为同一答复声明何时必须复核
入门 · 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));