Policies: all matches and the first recovery
A policy set collects every matching reason; a recovery chain selects the first explicitly handled result in order.
assess(input) returns {required,reasons,kinds}; it does not stop at the first match, and it does not let some fast mode override a mandatory rule. RecoveryResult is {handled:false} or {handled:true,value}; a value of undefined can still count as handled. Unknown IDs, duplicate IDs, empty descriptions, and async handlers are all rejected. Concrete kinds and recovery conditions are defined by the App.
Progressive examples for the same business case
Declare when the same reply must be reviewed
Beginner · 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 }));Add another reason and ordered recovery
Configuration · 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));