PolicyDefinition
KIT · interface · Type contract
One declarative policy.
Import
ts
import type { PolicyDefinition } from '@jangts/agent-core/kits/policies';js
/** @typedef {import('@jangts/agent-core/kits/policies').PolicyDefinition} PolicyDefinition */Other public entry points:@jangts/agent-core, @jangts/agent-core/types, @jangts/agent-core/structs, @jangts/agent-core/kits/policies, @jangts/agent-core/kits.
Definition
ts
interface PolicyDefinition<Input, Kind>Type parameters
| Type parameter | Constraint | Default type |
|---|---|---|
Input | — | — |
Kind | string | string |
Field structure
| Field | Type | Required | Description |
|---|---|---|---|
id | string | Required | Read-only. Unique policy id; also reported as a reason when the policy matches. |
description | string | Required | Read-only. Human-readable explanation; must not be blank. |
kind | Kind | Required | Read-only. Policy family used to deduplicate reported kinds. |
when | (input: Input) => boolean | Required | Returns whether the policy applies; must be synchronous boolean. |
Member index
| Member | Purpose |
|---|---|
when | Returns whether the policy applies; must be synchronous boolean. |
Member details
when
Returns whether the policy applies; must be synchronous boolean.
ts
PolicyDefinition.when(input: Input): booleanReturns whether the policy applies; must be synchronous boolean.
| Parameter | Type | Passing | Description |
|---|---|---|---|
input | Input | Required | — |
Returns: boolean.
Examples
- 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.
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));