Skip to content
中文

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 parameterConstraintDefault type
Input——
Kindstringstring

Field structure ​

FieldTypeRequiredDescription
idstringRequiredRead-only. Unique policy id; also reported as a reason when the policy matches.
descriptionstringRequiredRead-only. Human-readable explanation; must not be blank.
kindKindRequiredRead-only. Policy family used to deduplicate reported kinds.
when(input: Input) => booleanRequiredReturns whether the policy applies; must be synchronous boolean.

Member index ​

MemberPurpose
whenReturns 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): boolean

Returns whether the policy applies; must be synchronous boolean.

ParameterTypePassingDescription
inputInputRequired—

Returns: boolean.

Examples ​

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

API reference, usage examples and developer guides.