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.
导入
import { defineRecoveryChain } from '@jangts/agent-core/kits/policies';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 | — | — |
调用
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 | — | — |
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
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>; }>; }>。
返回值结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
tryRecover | (input: Input, execution?: Partial<import("../structs/common.js").ExecutionContext>) => RecoveryResult<Result> | 必需 | 见类型定义及调用场景。 |
definitions | readonly 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. |
使用示例
- Policies:全部命中与首个恢复:策略集收集所有匹配理由;恢复链按顺序选择第一个显式 handled 结果。
为同一答复声明何时必须复核
入门 · 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 }));增加另一理由及有序恢复
配置 · 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));