Skip to content
中文

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 ​

ts
import { defineRecoveryChain } from '@jangts/agent-core/kits/policies';
js
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 parameterConstraintDefault type
Input——
Result——

Call ​

ts
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 parameterConstraintDefault type
Input——
Result——
ParameterTypePassingDescription
definitionsreadonly 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
FieldTypeRequiredDescription
tryRecover(input: Input, execution?: Partial<import("../structs/common.js").ExecutionContext>) => RecoveryResult<Result>RequiredSee the type definition and usage context.
definitionsreadonly Readonly<{ id: string; description: string; tryRecover(input: Input): RecoveryResult<Result>; }>[]RequiredRead-only. Definitions in registration order; each entry is frozen by the implementation.
ids() => readonly string[]RequiredIds in the same order as definitions.
values() => IterableIterator<Readonly<{ id: string; description: string; tryRecover(input: Input): RecoveryResult<Result>; }>>RequiredSee the type definition and usage context.
entries() => IterableIterator<readonly [string, Readonly<{ id: string; description: string; tryRecover(input: Input): RecoveryResult<Result>; }>]>RequiredSee the type definition and usage context.
has(id: string) => booleanRequiredSee the type definition and usage context.
get(id: string) => Readonly<{ id: string; description: string; tryRecover(input: Input): RecoveryResult<Result>; }> | undefinedRequiredReturns 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>; }>RequiredReturns the definition or throws; use when the id is a required reference.

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.