Skip to content
English

RecoveryDefinition ​

KIT · interface · 类型合同

One recovery handler in an ordered chain.

导入 ​

ts
import type { RecoveryDefinition } from '@jangts/agent-core/kits/policies';
js
/** @typedef {import('@jangts/agent-core/kits/policies').RecoveryDefinition} RecoveryDefinition */

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

定义 ​

ts
interface RecoveryDefinition<Input, Result>

泛型参数 ​

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

字段结构 ​

字段类型必需说明
idstring必需只读。Unique handler id.
descriptionstring必需只读。见类型定义及调用场景。
tryRecover(input: Input) => RecoveryResult<Result>必需Attempts recovery; must be synchronous and the first handled result wins.

成员目录 ​

成员用途
tryRecoverAttempts recovery; must be synchronous and the first handled result wins.

成员详情 ​

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.

参数类型传递说明
inputInput必需—

返回: RecoveryResult<Result>。

返回值的可选结构: 选择其中一种结构,各分支的字段要求分别列出。

分支 1
字段类型必需说明
handledfalse必需只读。见类型定义及调用场景。
分支 2
字段类型必需说明
handledtrue必需只读。见类型定义及调用场景。
valueResult必需只读。见类型定义及调用场景。

使用示例 ​

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

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

相关策略源码 ​

相关定义 ​

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