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 | — | — |
字段结构
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
id | string | 必需 | 只读。Unique handler id. |
description | string | 必需 | 只读。见类型定义及调用场景。 |
tryRecover | (input: Input) => RecoveryResult<Result> | 必需 | Attempts recovery; must be synchronous and the first handled result wins. |
成员目录
| 成员 | 用途 |
|---|---|
tryRecover | Attempts 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.
| 参数 | 类型 | 传递 | 说明 |
|---|---|---|---|
input | Input | 必需 | — |
返回: RecoveryResult<Result>。
返回值的可选结构: 选择其中一种结构,各分支的字段要求分别列出。
分支 1
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
handled | false | 必需 | 只读。见类型定义及调用场景。 |
分支 2
| 字段 | 类型 | 必需 | 说明 |
|---|---|---|---|
handled | true | 必需 | 只读。见类型定义及调用场景。 |
value | Result | 必需 | 只读。见类型定义及调用场景。 |
使用示例
- Policies:全部命中与首个恢复:策略集收集所有匹配理由;恢复链按顺序选择第一个显式 handled 结果。
为同一答复声明何时必须复核
入门 · 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));