Skip to content
English

defineRecoveryChain ​

KIT · function · 运行时值

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.

导入 ​

ts
import { defineRecoveryChain } from '@jangts/agent-core/kits/policies';
js
import { defineRecoveryChain } from '@jangts/agent-core/kits/policies';

其他公开入口:@jangts/agent-core、@jangts/agent-core/types、@jangts/agent-core/kits/policies、@jangts/agent-core/kits。

定义 ​

泛型参数 ​

类型参数约束默认类型
Input——
Result——

调用 ​

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.

类型参数约束默认类型
Input——
Result——
参数类型传递说明
definitionsreadonly 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>; }>; }>。

返回值结构
字段类型必需说明
tryRecover(input: Input, execution?: Partial<import("../structs/common.js").ExecutionContext>) => RecoveryResult<Result>必需见类型定义及调用场景。
definitionsreadonly Readonly<{ id: string; description: string; tryRecover(input: Input): RecoveryResult<Result>; }>[]必需只读。Definitions in registration order; each entry is frozen by the implementation.
ids() => readonly string[]必需Ids in the same order as definitions.
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必需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>; }>必需Returns the definition or throws; use when the id is a required reference.

使用示例 ​

为同一答复声明何时必须复核 ​

入门 · 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 }));

增加另一理由及有序恢复 ​

配置 · 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));

相关策略源码 ​

相关定义 ​

接口参考、使用示例与开发指南。