defineRecoveryChain
KIT · function · Runtime value
Compile an ordered recovery chain.
A handler reports whether it handled the input; handled undefined values remain explicit. The first handler that reports handled ends the walk, so declaration order is the precedence, and { handled: false } is the only way to fall through.
Import
import { defineRecoveryChain } from '@jangts/agent-core/kits/policies';import { defineRecoveryChain } from '@jangts/agent-core/kits/policies';Other public entry points:@jangts/agent-core, @jangts/agent-core/types, @jangts/agent-core/kits/policies, @jangts/agent-core/kits.
Definition
Type parameters
| Type parameter | Constraint | Default type |
|---|---|---|
Input | — | — |
Result | — | — |
Call
defineRecoveryChain<Input, Result>(definitions: readonly RecoveryDefinition<Input, Result>[]): Readonly<{ tryRecover: (input: Input, execution?: Partial<import("../structs/common.js").ExecutionContext>) => RecoveryResult<Result>; definitions: readonly Readonly<{ id: string; description: string; tryRecover(input: Input): RecoveryResult<Result>; }>[]; ids(): readonly string[]; values(): IterableIterator<Readonly<{ id: string; description: string; tryRecover(input: Input): RecoveryResult<Result>; }>>; entries(): IterableIterator<readonly [string, Readonly<{ id: string; description: string; tryRecover(input: Input): RecoveryResult<Result>; }>]>; has(id: string): boolean; get(id: string): Readonly<{ id: string; description: string; tryRecover(input: Input): RecoveryResult<Result>; }> | undefined; require(id: string): Readonly<{ id: string; description: string; tryRecover(input: Input): RecoveryResult<Result>; }>; }>Compile an ordered recovery chain.
A handler reports whether it handled the input; handled undefined values remain explicit. The first handler that reports handled ends the walk, so declaration order is the precedence, and { handled: false } is the only way to fall through.
| Type parameter | Constraint | Default type |
|---|---|---|
Input | — | — |
Result | — | — |
| Parameter | Type | Passing | Description |
|---|---|---|---|
definitions | readonly RecoveryDefinition<Input, Result>[] | Required | — |
Returns: Readonly<{ tryRecover: (input: Input, execution?: Partial<import("../structs/common.js").ExecutionContext>) => RecoveryResult<Result>; definitions: readonly Readonly<{ id: string; description: string; tryRecover(input: Input): RecoveryResult<Result>; }>[]; ids(): readonly string[]; values(): IterableIterator<Readonly<{ id: string; description: string; tryRecover(input: Input): RecoveryResult<Result>; }>>; entries(): IterableIterator<readonly [string, Readonly<{ id: string; description: string; tryRecover(input: Input): RecoveryResult<Result>; }>]>; has(id: string): boolean; get(id: string): Readonly<{ id: string; description: string; tryRecover(input: Input): RecoveryResult<Result>; }> \| undefined; require(id: string): Readonly<{ id: string; description: string; tryRecover(input: Input): RecoveryResult<Result>; }>; }>.
Return value structure
| Field | Type | Required | Description |
|---|---|---|---|
tryRecover | (input: Input, execution?: Partial<import("../structs/common.js").ExecutionContext>) => RecoveryResult<Result> | Required | See the type definition and usage context. |
definitions | readonly Readonly<{ id: string; description: string; tryRecover(input: Input): RecoveryResult<Result>; }>[] | Required | Read-only. Definitions in registration order; each entry is frozen by the implementation. |
ids | () => readonly string[] | Required | Ids in the same order as definitions. |
values | () => IterableIterator<Readonly<{ id: string; description: string; tryRecover(input: Input): RecoveryResult<Result>; }>> | Required | See the type definition and usage context. |
entries | () => IterableIterator<readonly [string, Readonly<{ id: string; description: string; tryRecover(input: Input): RecoveryResult<Result>; }>]> | Required | See the type definition and usage context. |
has | (id: string) => boolean | Required | See the type definition and usage context. |
get | (id: string) => Readonly<{ id: string; description: string; tryRecover(input: Input): RecoveryResult<Result>; }> | undefined | Required | Returns the definition or undefined; the caller decides whether absence is an error. |
require | (id: string) => Readonly<{ id: string; description: string; tryRecover(input: Input): RecoveryResult<Result>; }> | Required | Returns the definition or throws; use when the id is a required reference. |
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
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 }));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
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));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));