RecoveryDefinition
KIT · interface · Type contract
One recovery handler in an ordered chain.
Import
ts
import type { RecoveryDefinition } from '@jangts/agent-core/kits/policies';js
/** @typedef {import('@jangts/agent-core/kits/policies').RecoveryDefinition} RecoveryDefinition */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 RecoveryDefinition<Input, Result>Type parameters
| Type parameter | Constraint | Default type |
|---|---|---|
Input | — | — |
Result | — | — |
Field structure
| Field | Type | Required | Description |
|---|---|---|---|
id | string | Required | Read-only. Unique handler id. |
description | string | Required | Read-only. See the type definition and usage context. |
tryRecover | (input: Input) => RecoveryResult<Result> | Required | Attempts recovery; must be synchronous and the first handled result wins. |
Member index
| Member | Purpose |
|---|---|
tryRecover | Attempts recovery; must be synchronous and the first handled result wins. |
Member details
tryRecover
Attempts recovery; must be synchronous and the first handled result wins.
ts
RecoveryDefinition.tryRecover(input: Input): RecoveryResult<Result>Attempts recovery; must be synchronous and the first handled result wins.
| Parameter | Type | Passing | Description |
|---|---|---|---|
input | Input | Required | — |
Returns: RecoveryResult<Result>.
Optional shapes of Return value: choose one shape; required fields for each branch are listed separately.
Branch 1
| Field | Type | Required | Description |
|---|---|---|---|
handled | false | Required | Read-only. See the type definition and usage context. |
Branch 2
| Field | Type | Required | Description |
|---|---|---|---|
handled | true | Required | Read-only. See the type definition and usage context. |
value | Result | Required | Read-only. See the type definition and usage context. |
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));