@cobia/pi-onward 0.3.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +178 -171
- package/docs/actions.md +97 -0
- package/example.json +18 -57
- package/onward.schema.json +86 -90
- package/package.json +9 -2
- package/src/actions/builtins.ts +98 -0
- package/src/actions/protocol.ts +136 -0
- package/src/actions/registry.ts +112 -0
- package/src/extension/autocomplete.ts +1 -3
- package/src/extension/command.ts +157 -184
- package/src/extension/index.ts +1 -12
- package/src/extension/install.ts +9 -7
- package/src/extension/render.ts +2 -10
- package/src/extension/settings.ts +81 -77
- package/src/extension/text.ts +309 -438
- package/src/policy/config.ts +35 -85
- package/src/policy/delay.ts +10 -27
- package/src/policy/failure.ts +3 -31
- package/src/policy/policy.ts +63 -157
- package/src/policy/rules.ts +124 -101
- package/src/policy/schema.ts +65 -72
- package/src/runtime/episode.ts +19 -0
- package/src/runtime/recovery.ts +473 -0
- package/src/extension/onward.ts +0 -208
- package/src/policy/tracker.ts +0 -64
package/src/policy/tracker.ts
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 上次正常返回之后的连续失败计数。每条规则各自计次数,预算互不挤占;同一个错误的连续次数不分规则:
|
|
3
|
-
* 不管落到哪条规则,同一个错误连着出现都说明没恢复。扩展在正常返回或用户开新一轮时 reset。
|
|
4
|
-
*/
|
|
5
|
-
import { retryDelay } from "./delay.ts";
|
|
6
|
-
import { errorSignature, type Failure } from "./failure.ts";
|
|
7
|
-
import type { Policy } from "./policy.ts";
|
|
8
|
-
|
|
9
|
-
/** 为什么不再续跑:规则说停、同一个错误太多次、次数用完。 */
|
|
10
|
-
export type GiveUpReason = "stop" | "same-error" | "budget";
|
|
11
|
-
|
|
12
|
-
export type Retry = { retry: true; attempt: number; delay: number };
|
|
13
|
-
export type GiveUp = { retry: false; reason: GiveUpReason; attempt: number; streak: number; notify: boolean };
|
|
14
|
-
export type Step = Retry | GiveUp;
|
|
15
|
-
|
|
16
|
-
/** 这一串失败里一共续跑了几次,同一个错误连续几次。 */
|
|
17
|
-
export interface Progress {
|
|
18
|
-
used: number;
|
|
19
|
-
streak: number;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export class Tracker {
|
|
23
|
-
private readonly attempts = new Map<string, number>();
|
|
24
|
-
private streak = 0;
|
|
25
|
-
private signature: string | undefined;
|
|
26
|
-
private warned = false;
|
|
27
|
-
|
|
28
|
-
/** 记一次失败,给出下一步:等多久再试,还是停。 */
|
|
29
|
-
next(ruleName: string, failure: Failure, policy: Policy): Step {
|
|
30
|
-
const signature = errorSignature(failure);
|
|
31
|
-
this.streak = signature === this.signature ? this.streak + 1 : 1;
|
|
32
|
-
this.signature = signature;
|
|
33
|
-
const used = this.attempts.get(ruleName) ?? 0;
|
|
34
|
-
const reason = giveUpReason(used, this.streak, policy);
|
|
35
|
-
if (reason) {
|
|
36
|
-
// 停手只提醒一次,恢复之前再撞到也不刷屏。
|
|
37
|
-
const notify = !this.warned;
|
|
38
|
-
this.warned = true;
|
|
39
|
-
return { retry: false, reason, attempt: used, streak: this.streak, notify };
|
|
40
|
-
}
|
|
41
|
-
this.attempts.set(ruleName, used + 1);
|
|
42
|
-
return { retry: true, attempt: used + 1, delay: retryDelay(used + 1, policy.delay) };
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
reset(): void {
|
|
46
|
-
this.attempts.clear();
|
|
47
|
-
this.streak = 0;
|
|
48
|
-
this.signature = undefined;
|
|
49
|
-
this.warned = false;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
progress(): Progress {
|
|
53
|
-
let used = 0;
|
|
54
|
-
for (const n of this.attempts.values()) used += n;
|
|
55
|
-
return { used, streak: this.streak };
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
function giveUpReason(used: number, streak: number, policy: Policy): GiveUpReason | undefined {
|
|
60
|
-
if (policy.action === "stop") return "stop";
|
|
61
|
-
if (streak > policy.maxSameError) return "same-error";
|
|
62
|
-
if (used >= policy.maxRetries) return "budget";
|
|
63
|
-
return undefined;
|
|
64
|
-
}
|