@cobia/pi-onward 0.2.0 → 0.3.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 +122 -174
- package/example.json +12 -9
- package/onward.schema.json +33 -22
- package/package.json +3 -3
- package/src/extension/command.ts +89 -29
- package/src/extension/install.ts +11 -2
- package/src/extension/settings.ts +7 -6
- package/src/extension/text.ts +59 -44
- package/src/policy/config.ts +31 -8
- package/src/policy/failure.ts +10 -1
- package/src/policy/policy.ts +6 -0
- package/src/policy/rules.ts +37 -20
- package/src/policy/schema.ts +15 -8
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cobia/pi-onward",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "pi 出错或空回停止时,按规则自动续跑",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pi",
|
|
7
7
|
"pi-package",
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
"@earendil-works/pi-tui": "*"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
|
-
"@biomejs/biome": "^2.6.
|
|
47
|
+
"@biomejs/biome": "^2.6.4",
|
|
48
48
|
"@earendil-works/pi-coding-agent": "^1.4.0",
|
|
49
49
|
"@earendil-works/pi-tui": "^1.4.0",
|
|
50
50
|
"@types/node": "^22.20.5",
|
package/src/extension/command.ts
CHANGED
|
@@ -26,6 +26,7 @@ import {
|
|
|
26
26
|
layerNotice,
|
|
27
27
|
menuTitle,
|
|
28
28
|
type Notice,
|
|
29
|
+
quietLine,
|
|
29
30
|
resetLine,
|
|
30
31
|
rulesNotice,
|
|
31
32
|
SCOPE,
|
|
@@ -52,6 +53,18 @@ export type Command =
|
|
|
52
53
|
| { kind: "error"; message: string; topic?: string };
|
|
53
54
|
|
|
54
55
|
const HELP = "--help";
|
|
56
|
+
/** 启动提示的开关:不是规则的参数,只能写在全局设置。 */
|
|
57
|
+
const QUIET = "quietStartup";
|
|
58
|
+
/** 帮助的字段表里 quietStartup 那一行。 */
|
|
59
|
+
const QUIET_HELP: [string, string] = [
|
|
60
|
+
QUIET,
|
|
61
|
+
[
|
|
62
|
+
"启动时的提示(用上了项目级设置、没有可用的规则);设置有问题时照样警告。只能写在全局设置",
|
|
63
|
+
"true:不显示",
|
|
64
|
+
"false:照常显示,不管 pi 的 quietStartup",
|
|
65
|
+
"不写:跟着 pi 的 quietStartup(默认)",
|
|
66
|
+
].join("\n"),
|
|
67
|
+
];
|
|
55
68
|
|
|
56
69
|
/** 改设置的动作的帮助里都有这一行。不写路径:设置文件放在哪只由 index.ts 决定,实际的路径在 status 里。 */
|
|
57
70
|
const SCOPE_HELP = `${SCOPE} 写在动作前:不写或 global 改全局设置,project 改当前目录的项目级设置;两份在哪见 /${COMMAND} status`;
|
|
@@ -134,7 +147,7 @@ const ACTIONS: readonly ActionSpec[] = [
|
|
|
134
147
|
args: "字段 …",
|
|
135
148
|
description: "去掉开关或参数,继承下一层",
|
|
136
149
|
details: [
|
|
137
|
-
"可以一次写几个,开关 enabled 也能去掉;有不认识或写了两次的字段就一项都不改",
|
|
150
|
+
"可以一次写几个,开关 enabled、启动提示 quietStartup 也能去掉;有不认识或写了两次的字段就一项都不改",
|
|
138
151
|
"规则里写的同名字段不受影响",
|
|
139
152
|
SCOPE_FALLBACK,
|
|
140
153
|
],
|
|
@@ -152,7 +165,11 @@ const ACTIONS: readonly ActionSpec[] = [
|
|
|
152
165
|
name: "reset",
|
|
153
166
|
args: "",
|
|
154
167
|
description: "去掉开关和参数,保留手写的 rules",
|
|
155
|
-
details: [
|
|
168
|
+
details: [
|
|
169
|
+
"开关、参数和 quietStartup 都去掉;rules 和其他手写的内容留着",
|
|
170
|
+
"正在等的续跑立刻打断,计数清零",
|
|
171
|
+
SCOPE_FALLBACK,
|
|
172
|
+
],
|
|
156
173
|
scoped: "write",
|
|
157
174
|
},
|
|
158
175
|
];
|
|
@@ -252,9 +269,11 @@ function execute(
|
|
|
252
269
|
}
|
|
253
270
|
case "update": {
|
|
254
271
|
const saved = settings.update(command.scope, command.change);
|
|
272
|
+
const keys = changedKeys(command.change);
|
|
255
273
|
const done = note(
|
|
256
274
|
scopeLine(settings, command.scope),
|
|
257
|
-
|
|
275
|
+
keys.includes(QUIET) ? quietLine(settings.config.quietStartup) : undefined,
|
|
276
|
+
shadowLine(settings, command.scope, keys),
|
|
258
277
|
);
|
|
259
278
|
return changedNotice(saved, settings, onward.progress(), done);
|
|
260
279
|
}
|
|
@@ -406,7 +425,7 @@ function none(action: string, rest: string[]): Command | undefined {
|
|
|
406
425
|
|
|
407
426
|
function parseSet(tokens: string[], scope: Scope): Command {
|
|
408
427
|
if (!tokens.length)
|
|
409
|
-
return { kind: "error", message: `set 要写 字段=值;可用 ${
|
|
428
|
+
return { kind: "error", message: `set 要写 字段=值;可用 ${settable(scope).join("、")}`, topic: "set" };
|
|
410
429
|
const problems: string[] = [];
|
|
411
430
|
const values: Record<string, unknown> = {};
|
|
412
431
|
for (const token of tokens) {
|
|
@@ -417,23 +436,39 @@ function parseSet(tokens: string[], scope: Scope): Command {
|
|
|
417
436
|
}
|
|
418
437
|
const key = token.slice(0, equals);
|
|
419
438
|
const text = token.slice(equals + 1);
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
problems.push(known);
|
|
423
|
-
continue;
|
|
424
|
-
}
|
|
425
|
-
if (known.name in values) {
|
|
426
|
-
problems.push(`${known.name} 写了两次`);
|
|
439
|
+
if (Object.hasOwn(values, key)) {
|
|
440
|
+
problems.push(`${key} 写了两次`);
|
|
427
441
|
continue;
|
|
428
442
|
}
|
|
429
|
-
const
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
else values[known.name] = value;
|
|
443
|
+
const parsed = key === QUIET ? quietValue(text, scope) : paramValue(key, text, scope);
|
|
444
|
+
if ("problem" in parsed) problems.push(parsed.problem);
|
|
445
|
+
else values[parsed.name] = parsed.value;
|
|
433
446
|
}
|
|
434
447
|
return problems.length ? rejected(problems, "set") : { kind: "update", scope, change: { set: values } };
|
|
435
448
|
}
|
|
436
449
|
|
|
450
|
+
type Parsed = { name: string; value: unknown } | { problem: string };
|
|
451
|
+
|
|
452
|
+
function paramValue(key: string, text: string, scope: Scope): Parsed {
|
|
453
|
+
const known = paramField(key, scope, settable(scope));
|
|
454
|
+
if (typeof known === "string") return { problem: known };
|
|
455
|
+
const value = known.fromText(text);
|
|
456
|
+
const read = known.read(value);
|
|
457
|
+
return "problem" in read ? { problem: `${known.name} ${read.problem}` } : { name: known.name, value };
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
/** quietStartup 只认 true、false,只能写在全局:项目级的能关掉「用上了项目级设置」这句提醒。 */
|
|
461
|
+
function quietValue(text: string, scope: Scope): Parsed {
|
|
462
|
+
if (scope !== "global") return { problem: `${QUIET} 只能写在全局设置,不写 ${SCOPE} 就是全局` };
|
|
463
|
+
if (text !== "true" && text !== "false") return { problem: `${QUIET} 需要 true 或 false` };
|
|
464
|
+
return { name: QUIET, value: text === "true" };
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
/** set 能写的字段:参数,全局还有 quietStartup。 */
|
|
468
|
+
function settable(scope: Scope): readonly string[] {
|
|
469
|
+
return scope === "global" ? [...FIELD_NAMES, QUIET] : FIELD_NAMES;
|
|
470
|
+
}
|
|
471
|
+
|
|
437
472
|
/**
|
|
438
473
|
* unset 能去掉的:参数,和开关 enabled。开关有两份设置时也要能去掉:去掉项目级的才继承全局的。
|
|
439
474
|
*/
|
|
@@ -452,7 +487,8 @@ function parseUnset(tokens: string[], scope: Scope): Command {
|
|
|
452
487
|
}
|
|
453
488
|
|
|
454
489
|
function unsetKey(token: string, scope: Scope): { name: string } | { problem: string } {
|
|
455
|
-
|
|
490
|
+
// quietStartup 在哪一份都能去掉:项目级写了它是一处问题,去掉就好了。
|
|
491
|
+
if (token === "enabled" || token === QUIET) return { name: token };
|
|
456
492
|
const known = paramField(token, scope, COMMAND_KEYS);
|
|
457
493
|
return typeof known === "string" ? { problem: known } : { name: known.name };
|
|
458
494
|
}
|
|
@@ -508,7 +544,9 @@ function actionHelp(spec: ActionSpec): string {
|
|
|
508
544
|
"说明",
|
|
509
545
|
[...spec.details, ...(spec.scoped === "write" ? [SCOPE_HELP] : [])].map((line) => ` ${line}`),
|
|
510
546
|
),
|
|
511
|
-
...(spec.fields
|
|
547
|
+
...(spec.fields
|
|
548
|
+
? section("字段", table([...FIELDS.map((item): [string, string] => [item.name, fieldHelp(item)]), QUIET_HELP]))
|
|
549
|
+
: []),
|
|
512
550
|
...section(
|
|
513
551
|
"示例",
|
|
514
552
|
(spec.examples ?? []).map((line) => ` ${line}`),
|
|
@@ -633,44 +671,66 @@ function candidates(typed: string[], current: string): Candidate[] {
|
|
|
633
671
|
];
|
|
634
672
|
const spec = actionNamed(first);
|
|
635
673
|
if (!spec || (scoped && !spec.scoped)) return [];
|
|
636
|
-
const args = argumentCandidates(spec.name, rest, current);
|
|
674
|
+
const args = argumentCandidates(spec.name, rest, current, scoped ? scopeNamed(words[1])! : "global");
|
|
637
675
|
return args ? [...args, { value: HELP, description: `显示 ${spec.name} 的帮助,不执行` }] : [];
|
|
638
676
|
}
|
|
639
677
|
|
|
640
678
|
/** 动作后面的参数;`undefined`:这里已经写错了,执行时会被拒绝,--help 也不给。 */
|
|
641
|
-
function argumentCandidates(action: string, rest: string[], current: string): Candidate[] | undefined {
|
|
679
|
+
function argumentCandidates(action: string, rest: string[], current: string, scope: Scope): Candidate[] | undefined {
|
|
642
680
|
switch (action) {
|
|
643
681
|
case "show":
|
|
644
682
|
if (!rest.length) return [{ value: "rules", description: "只看规则" }];
|
|
645
683
|
return rest.length === 1 && rest[0] === "rules" ? [] : undefined;
|
|
646
684
|
case "set":
|
|
647
685
|
case "unset":
|
|
648
|
-
return fieldCandidates(action, rest, current);
|
|
686
|
+
return fieldCandidates(action, rest, current, scope);
|
|
649
687
|
default:
|
|
650
688
|
return rest.length ? undefined : [];
|
|
651
689
|
}
|
|
652
690
|
}
|
|
653
691
|
|
|
654
|
-
|
|
692
|
+
/** quietStartup= 后面能写的。 */
|
|
693
|
+
const QUIET_CHOICES: readonly Candidate[] = [
|
|
694
|
+
{ value: "true", description: "启动时不显示 onward 的提示;设置有问题时照样警告" },
|
|
695
|
+
{ value: "false", description: "照常显示,不管 pi 的 quietStartup" },
|
|
696
|
+
];
|
|
697
|
+
|
|
698
|
+
function fieldCandidates(action: string, rest: string[], current: string, scope: Scope): Candidate[] | undefined {
|
|
699
|
+
// quietStartup 只能写在全局:项目级的 set 不收它,也不给;unset 哪一份都收,项目级的不给。
|
|
700
|
+
const offered = scope === "global";
|
|
701
|
+
const named = (name: string) =>
|
|
702
|
+
fieldNamed(name) !== undefined || (name === QUIET && (offered || action === "unset"));
|
|
655
703
|
// 前面有执行时会被拒绝的词(不认识的字段、大小写不对的、没写 = 或值的),什么都不给。
|
|
656
704
|
const valid = (word: string) => {
|
|
657
|
-
if (action === "unset") return word === "enabled" ||
|
|
705
|
+
if (action === "unset") return word === "enabled" || named(word);
|
|
658
706
|
const equals = word.indexOf("=");
|
|
659
|
-
return equals > 0 && equals < word.length - 1 &&
|
|
707
|
+
return equals > 0 && equals < word.length - 1 && named(word.slice(0, equals));
|
|
660
708
|
};
|
|
661
709
|
if (!rest.every(valid)) return undefined;
|
|
662
710
|
// 写过的字段不再给:再写一次,执行时会被拒绝。
|
|
663
|
-
const written = new Set(rest.map((word) =>
|
|
711
|
+
const written = new Set(rest.map((word) => word.split("=")[0]));
|
|
664
712
|
const unwritten = FIELDS.filter((item) => !written.has(item.name));
|
|
713
|
+
const quiet = offered && !written.has(QUIET);
|
|
665
714
|
if (action === "unset") {
|
|
666
|
-
const toggle =
|
|
667
|
-
return [
|
|
715
|
+
const toggle = written.has("enabled") ? [] : [{ value: "enabled", description: "开关:去掉之后继承下一层" }];
|
|
716
|
+
return [
|
|
717
|
+
...toggle,
|
|
718
|
+
...unwritten.map((item) => ({ value: item.name, description: item.description })),
|
|
719
|
+
...(quiet ? [{ value: QUIET, description: "启动提示:去掉之后跟着 pi 的 quietStartup" }] : []),
|
|
720
|
+
];
|
|
668
721
|
}
|
|
669
722
|
const equals = current.indexOf("=");
|
|
670
723
|
if (equals < 0)
|
|
671
|
-
return
|
|
672
|
-
|
|
673
|
-
|
|
724
|
+
return [
|
|
725
|
+
...unwritten.map((item) => ({ value: `${item.name}=`, description: `${item.syntax}:${item.description}` })),
|
|
726
|
+
...(quiet ? [{ value: `${QUIET}=`, description: "true 或 false:启动时的提示显示不显示" }] : []),
|
|
727
|
+
];
|
|
728
|
+
const name = current.slice(0, equals);
|
|
729
|
+
if (written.has(name)) return undefined;
|
|
730
|
+
if (name === QUIET)
|
|
731
|
+
return quiet ? QUIET_CHOICES.map((choice) => ({ ...choice, value: `${QUIET}=${choice.value}` })) : undefined;
|
|
732
|
+
const known = fieldNamed(name);
|
|
733
|
+
if (!known) return undefined;
|
|
674
734
|
return (known.choices ?? []).map((choice) => ({
|
|
675
735
|
value: `${known.name}=${choice.value}`,
|
|
676
736
|
description: choice.description,
|
package/src/extension/install.ts
CHANGED
|
@@ -5,7 +5,7 @@ import { registerCommand } from "./command.ts";
|
|
|
5
5
|
import { CUSTOM_TYPE, Onward } from "./onward.ts";
|
|
6
6
|
import { renderContinue } from "./render.ts";
|
|
7
7
|
import { type ConfigPaths, Settings } from "./settings.ts";
|
|
8
|
-
import { say,
|
|
8
|
+
import { say, startupNotice } from "./text.ts";
|
|
9
9
|
|
|
10
10
|
export function install(pi: ExtensionAPI, paths: ConfigPaths): void {
|
|
11
11
|
const settings = new Settings(paths);
|
|
@@ -16,7 +16,8 @@ export function install(pi: ExtensionAPI, paths: ConfigPaths): void {
|
|
|
16
16
|
settings.bind(ctx.cwd);
|
|
17
17
|
// reload 和切换会话时 pi 会清掉扩展装的补全,所以每次都装。
|
|
18
18
|
ctx.ui.addAutocompleteProvider(argumentTabCompletion);
|
|
19
|
-
|
|
19
|
+
const notice = startupNotice(settings, () => quietStartup(pi, settings));
|
|
20
|
+
if (notice) say(ctx, notice);
|
|
20
21
|
});
|
|
21
22
|
pi.on("agent_start", (_event, ctx) => onward.agentStart(ctx));
|
|
22
23
|
pi.on("message_end", (event, ctx) => onward.messageEnd(event, ctx));
|
|
@@ -27,3 +28,11 @@ export function install(pi: ExtensionAPI, paths: ConfigPaths): void {
|
|
|
27
28
|
registerCommand(pi, settings, onward);
|
|
28
29
|
pi.registerMessageRenderer(CUSTOM_TYPE, renderContinue);
|
|
29
30
|
}
|
|
31
|
+
|
|
32
|
+
/** 启动时的提示收不收起:onward 自己的 quietStartup 写了就按它,没写跟着 pi 的,pi 的 true 和 "header" 都收起。 */
|
|
33
|
+
function quietStartup(pi: ExtensionAPI, settings: Settings): boolean {
|
|
34
|
+
const own = settings.config.quietStartup;
|
|
35
|
+
if (own !== undefined) return own;
|
|
36
|
+
const fromPi = pi.getSettings().quietStartup;
|
|
37
|
+
return fromPi === true || fromPi === "header";
|
|
38
|
+
}
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* 项目级要等 `session_start` 才知道:pi 那时才定下当前目录。不看 pi 对项目的信任:有这个文件就读,和它旁边有没有
|
|
6
6
|
* `.pi/settings.json` 无关。
|
|
7
7
|
*
|
|
8
|
-
* `rules` 只能手写;命令只改 `enabled` 和顶层的参数字段,改哪一份由 `--scope` 定,不写就是全局。每次写之前重读
|
|
8
|
+
* `rules` 只能手写;命令只改 `enabled`、`quietStartup` 和顶层的参数字段,改哪一份由 `--scope` 定,不写就是全局。每次写之前重读
|
|
9
9
|
* 要改的那份,在它当前的内容上合并,免得盖掉加载之后才手改的内容;文件读不了(不是合法 JSON、顶层不是对象)
|
|
10
10
|
* 时拒绝写,让用户自己修。
|
|
11
11
|
*/
|
|
@@ -53,8 +53,8 @@ export type Saved =
|
|
|
53
53
|
| { kind: "unbound" }
|
|
54
54
|
| { kind: "unwritten"; error: string };
|
|
55
55
|
|
|
56
|
-
/** reset
|
|
57
|
-
export const COMMAND_KEYS: readonly string[] = ["enabled", ...FIELD_NAMES];
|
|
56
|
+
/** 命令能改、reset 去掉的键:开关、顶层的参数和 quietStartup。 */
|
|
57
|
+
export const COMMAND_KEYS: readonly string[] = ["enabled", ...FIELD_NAMES, "quietStartup"];
|
|
58
58
|
|
|
59
59
|
const EMPTY: ConfigLayer = { defaults: {}, problems: [] };
|
|
60
60
|
|
|
@@ -120,7 +120,7 @@ export class Settings {
|
|
|
120
120
|
// 两份都重读:显示的状态和磁盘上的一致。
|
|
121
121
|
this.reload();
|
|
122
122
|
if (current.kind === "missing" && Object.keys(next).length === 0) return { kind: "saved", value: next };
|
|
123
|
-
this.loaded[scope] = { scope, path, source: { kind: "file" }, layer: parseLayer(next) };
|
|
123
|
+
this.loaded[scope] = { scope, path, source: { kind: "file" }, layer: parseLayer(next, scope === "project") };
|
|
124
124
|
this.remerge();
|
|
125
125
|
try {
|
|
126
126
|
writeJson(path, next);
|
|
@@ -130,7 +130,7 @@ export class Settings {
|
|
|
130
130
|
return { kind: "saved", value: next };
|
|
131
131
|
}
|
|
132
132
|
|
|
133
|
-
/**
|
|
133
|
+
/** 去掉开关、顶层的参数和 quietStartup,手写的 rules 和其他内容留着。 */
|
|
134
134
|
reset(scope: Scope): Saved {
|
|
135
135
|
return this.update(scope, { unset: COMMAND_KEYS });
|
|
136
136
|
}
|
|
@@ -142,6 +142,7 @@ export class Settings {
|
|
|
142
142
|
const layer = this.loaded[scope]?.layer;
|
|
143
143
|
if (!layer) return false;
|
|
144
144
|
if (key === "enabled") return layer.enabled !== undefined;
|
|
145
|
+
if (key === "quietStartup") return layer.quietStartup !== undefined;
|
|
145
146
|
if (key === "rules") return layer.rules !== undefined;
|
|
146
147
|
return Object.hasOwn(layer.defaults, key);
|
|
147
148
|
}
|
|
@@ -164,7 +165,7 @@ function readFile(scope: Scope, path: string): SettingsFile {
|
|
|
164
165
|
const current = readJson(path);
|
|
165
166
|
switch (current.kind) {
|
|
166
167
|
case "ok":
|
|
167
|
-
return { scope, path, source: { kind: "file" }, layer: parseLayer(current.value) };
|
|
168
|
+
return { scope, path, source: { kind: "file" }, layer: parseLayer(current.value, scope === "project") };
|
|
168
169
|
case "missing":
|
|
169
170
|
return { scope, path, source: { kind: "none" }, layer: EMPTY };
|
|
170
171
|
case "broken":
|
package/src/extension/text.ts
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* 停手说为什么、怎么办,不重复图标、次数和规则名。错误本身 pi 已经在上面用红字显示,提示里也不再摘。
|
|
10
10
|
*/
|
|
11
11
|
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
12
|
-
import type
|
|
12
|
+
import { type Config, configWarnings, type Hit } from "../policy/config.ts";
|
|
13
13
|
import { formatDelay, retryDelay, totalDelay } from "../policy/delay.ts";
|
|
14
14
|
import { EMPTY_REPLY, errorSignature, type Failure } from "../policy/failure.ts";
|
|
15
15
|
import { DEFAULT_POLICY, FIELDS, type Field, type Policy, type PolicyPatch, resolvePolicy } from "../policy/policy.ts";
|
|
@@ -41,7 +41,8 @@ const IDLE = "💤";
|
|
|
41
41
|
const STOPPED = "🛑";
|
|
42
42
|
|
|
43
43
|
const UNTIL_FIXED = "修好之前不会自动续跑";
|
|
44
|
-
const addRules = (
|
|
44
|
+
const addRules = () => `在设置文件的 rules 里写规则(常见规则见 README),再 /${COMMAND} reload`;
|
|
45
|
+
const SEE_SHOW = `/${COMMAND} show 查看`;
|
|
45
46
|
|
|
46
47
|
/** 两份设置文件给人看的名字:表格、来源和问题前面都用它。 */
|
|
47
48
|
const SCOPE_NAMES: Readonly<Record<Scope, string>> = { global: "全局", project: "项目级" };
|
|
@@ -116,7 +117,7 @@ export function statusHeadline(settings: Settings): string {
|
|
|
116
117
|
}
|
|
117
118
|
|
|
118
119
|
export function statusNotice(settings: Settings, progress: Progress, note?: string): Notice {
|
|
119
|
-
return
|
|
120
|
+
return settingsNotice(settings, statusText(settings, progress, note));
|
|
120
121
|
}
|
|
121
122
|
|
|
122
123
|
/**
|
|
@@ -130,6 +131,8 @@ function statusText(settings: Settings, { used, streak }: Progress, note?: strin
|
|
|
130
131
|
// 读不了的文件也记成一处问题,所以这里不用另外看。
|
|
131
132
|
const usable = !config.problems.length;
|
|
132
133
|
if (usable) lines.push(...section("规则没写的参数", table(policyRows(resolvePolicy(config.defaults)))));
|
|
134
|
+
const reminders = ruleWarningSummary(config);
|
|
135
|
+
if (reminders) lines.push("", `${reminders}(${SEE_SHOW})`);
|
|
133
136
|
lines.push(...section("设置文件", table(fileRows(settings.files))), "");
|
|
134
137
|
if (!usable) lines.push(`改好之后 /${COMMAND} reload;问题见 /${COMMAND} show`);
|
|
135
138
|
else if (!config.enabled)
|
|
@@ -171,7 +174,8 @@ export function showNotice(settings: Settings): Notice {
|
|
|
171
174
|
const where = from ? `${SCOPE_NAMES[from]}设置` : "内置默认值";
|
|
172
175
|
return [item.name, `${(item as Field<typeof item.name>).show(value)}(${where})`];
|
|
173
176
|
});
|
|
174
|
-
return
|
|
177
|
+
return settingsNotice(
|
|
178
|
+
settings,
|
|
175
179
|
[
|
|
176
180
|
"规则没写的参数",
|
|
177
181
|
...table(rows),
|
|
@@ -207,6 +211,7 @@ export function layerNotice(settings: Settings, scope: Scope, rulesOnly: boolean
|
|
|
207
211
|
item.name,
|
|
208
212
|
shown(item, layer.defaults),
|
|
209
213
|
]),
|
|
214
|
+
...(layer.quietStartup === undefined ? [] : [["quietStartup", String(layer.quietStartup)] as [string, string]]),
|
|
210
215
|
];
|
|
211
216
|
const rules =
|
|
212
217
|
layer.rules === undefined
|
|
@@ -238,8 +243,9 @@ function shown(item: Field, values: PolicyPatch): string {
|
|
|
238
243
|
/** `/onward show rules`。设置文件读不了时说读不了:规则一条也没读到,不是没写。 */
|
|
239
244
|
export function rulesNotice(settings: Settings): Notice {
|
|
240
245
|
const broken = brokenFiles(settings);
|
|
241
|
-
if (!broken.length) return
|
|
242
|
-
return
|
|
246
|
+
if (!broken.length) return settingsNotice(settings, rulesText(settings));
|
|
247
|
+
return settingsNotice(
|
|
248
|
+
settings,
|
|
243
249
|
[`设置文件读不了,${UNTIL_FIXED}`, ...table(fileRows(broken)), "", `改好之后 /${COMMAND} reload`].join("\n"),
|
|
244
250
|
);
|
|
245
251
|
}
|
|
@@ -261,6 +267,14 @@ function rulesFrom(settings: Settings): string | undefined {
|
|
|
261
267
|
return `规则来自${SCOPE_NAMES[origin]}设置${replaced ? ",全局的 rules 不生效" : ""}`;
|
|
262
268
|
}
|
|
263
269
|
|
|
270
|
+
/** 改了 quietStartup 之后:onward 的启动提示现在怎么显示。 */
|
|
271
|
+
export function quietLine(quiet: boolean | undefined): string {
|
|
272
|
+
if (quiet === undefined) return "onward 的启动提示跟着 pi 的 quietStartup";
|
|
273
|
+
return quiet
|
|
274
|
+
? "onward 的启动提示不显示;设置有问题时照样警告"
|
|
275
|
+
: "onward 的启动提示照常显示,不管 pi 的 quietStartup";
|
|
276
|
+
}
|
|
277
|
+
|
|
264
278
|
/** 改了开关或参数之后:有项目级设置在用、或者改的就是项目级时,说改的是哪一份;只有一份时说了也多余。 */
|
|
265
279
|
export function scopeLine(settings: Settings, scope: Scope): string | undefined {
|
|
266
280
|
return scope === "project" || settings.projectFile !== undefined ? `改的是${SCOPE_NAMES[scope]}设置` : undefined;
|
|
@@ -362,22 +376,35 @@ export function describeRules(config: Config, from?: string): string {
|
|
|
362
376
|
...table(rows).map((line) => ` ${line}`),
|
|
363
377
|
];
|
|
364
378
|
});
|
|
365
|
-
return [
|
|
379
|
+
return [
|
|
380
|
+
...source,
|
|
381
|
+
"规则(按顺序,第一条命中的生效;没命中的错误和空回复不处理)",
|
|
382
|
+
...blocks.flat(),
|
|
383
|
+
...section(
|
|
384
|
+
"规则提醒(不影响设置生效)",
|
|
385
|
+
configWarnings(config).map((text) => `- ${text}`),
|
|
386
|
+
),
|
|
387
|
+
].join("\n");
|
|
366
388
|
}
|
|
367
389
|
|
|
368
390
|
function describeWhen({ when }: Rule): string {
|
|
369
391
|
const parts = [
|
|
370
|
-
when.
|
|
371
|
-
when.error && `错误匹配 /${when.error.source}/i`,
|
|
392
|
+
when.failure && `失败匹配 /${when.failure.source}/i`,
|
|
372
393
|
when.provider && `provider=${when.provider}`,
|
|
373
394
|
when.model && `model=${when.model}`,
|
|
374
395
|
].filter(Boolean);
|
|
375
|
-
return parts.length ? parts.join(",") : "
|
|
396
|
+
return parts.length ? parts.join(",") : "任何失败";
|
|
376
397
|
}
|
|
377
398
|
|
|
378
|
-
/**
|
|
379
|
-
function
|
|
380
|
-
|
|
399
|
+
/** 配置错误和非阻断的规则提醒都用 warning;是否能续跑仍由 config.problems 决定。 */
|
|
400
|
+
function settingsNotice(settings: Settings, message: string): Notice {
|
|
401
|
+
const attention = settings.config.problems.length > 0 || configWarnings(settings.config).length > 0;
|
|
402
|
+
return { message, level: attention ? "warning" : "info" };
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
function ruleWarningSummary(config: Config): string | undefined {
|
|
406
|
+
const count = configWarnings(config).length;
|
|
407
|
+
return count ? `规则有 ${count} 处提醒,不影响设置生效` : undefined;
|
|
381
408
|
}
|
|
382
409
|
|
|
383
410
|
function usableRules(config: Config): number {
|
|
@@ -387,39 +414,27 @@ function usableRules(config: Config): number {
|
|
|
387
414
|
// ── 运行中的通知 ──
|
|
388
415
|
|
|
389
416
|
/**
|
|
390
|
-
*
|
|
391
|
-
*
|
|
417
|
+
* 启动时最多一条:配置错误说是哪一份(关着也说,可能就是 enabled 写错了);规则提醒不阻止续跑。
|
|
418
|
+
* 两者都不受 quietStartup 影响。用上了项目级设置、没有可用规则这两种平常提示才问 quiet()。
|
|
419
|
+
* 不写路径和报错原文,细节用 `/onward show` 查看;pi 只留下连着的提示里的最后一条,分开发会互相顶掉。
|
|
392
420
|
*/
|
|
393
|
-
export function
|
|
421
|
+
export function startupNotice(settings: Settings, quiet: () => boolean): Notice | undefined {
|
|
394
422
|
const { config } = settings;
|
|
395
|
-
const
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
if (
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
...settings.files
|
|
411
|
-
.filter((file) => file.layer.problems.length)
|
|
412
|
-
.map((file) =>
|
|
413
|
-
warning(
|
|
414
|
-
`${STOPPED} onward:${file.path} 有 ${file.layer.problems.length} 处问题,${UNTIL_FIXED}(/${COMMAND} show 查看)`,
|
|
415
|
-
),
|
|
416
|
-
),
|
|
417
|
-
];
|
|
418
|
-
if (config.enabled && !usableRules(config)) {
|
|
419
|
-
const where = (settings.origin("rules") === "project" && project) || settings.file("global");
|
|
420
|
-
notices.push(info(`${IDLE} onward:没有可用的规则,出错时不会自动续跑;${addRules(` ${where.path} `)}`));
|
|
421
|
-
}
|
|
422
|
-
return notices;
|
|
423
|
+
const bad = settings.files.flatMap((file) => {
|
|
424
|
+
const name = `${SCOPE_NAMES[file.scope]}设置`;
|
|
425
|
+
if (file.source.kind === "broken") return [`${name}读不了`];
|
|
426
|
+
return file.layer.problems.length ? [`${name}有 ${file.layer.problems.length} 处问题`] : [];
|
|
427
|
+
});
|
|
428
|
+
if (bad.length) return warning(`${STOPPED} onward:${bad.join(",")},${UNTIL_FIXED}(${SEE_SHOW})`);
|
|
429
|
+
const project = settings.file("project")?.source.kind === "file";
|
|
430
|
+
const reminders = ruleWarningSummary(config);
|
|
431
|
+
if (reminders) return warning(`onward:${project ? "用上了项目级设置;" : ""}${reminders}(${SEE_SHOW})`);
|
|
432
|
+
const idle = config.enabled && !usableRules(config);
|
|
433
|
+
if (!(project || idle) || quiet()) return undefined;
|
|
434
|
+
if (project && idle)
|
|
435
|
+
return info(`${IDLE} onward:用上了项目级设置;没有可用的规则,出错时不会自动续跑(${SEE_SHOW})`);
|
|
436
|
+
if (project) return info(`onward:用上了项目级设置(${SEE_SHOW})`);
|
|
437
|
+
return info(`${IDLE} onward:没有可用的规则,出错时不会自动续跑(${SEE_SHOW})`);
|
|
423
438
|
}
|
|
424
439
|
|
|
425
440
|
/**
|
package/src/policy/config.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* {
|
|
6
6
|
* "enabled": true,
|
|
7
7
|
* "action": "message", "maxRetries": 10,
|
|
8
|
-
* "rules": [{ "name": "auth", "when": { "
|
|
8
|
+
* "rules": [{ "name": "auth", "when": { "failure": "401" }, "action": "resend", "maxRetries": 20 }]
|
|
9
9
|
* }
|
|
10
10
|
* ```
|
|
11
11
|
*
|
|
@@ -17,10 +17,12 @@
|
|
|
17
17
|
*/
|
|
18
18
|
import { type AgentMessage, type Failure, isFailure } from "./failure.ts";
|
|
19
19
|
import { FIELD_NAMES, type Policy, type PolicyPatch, readParams, resolvePolicy } from "./policy.ts";
|
|
20
|
-
import { findRule, parseRules, type Rule } from "./rules.ts";
|
|
20
|
+
import { findRule, parseRules, type Rule, ruleWarnings } from "./rules.ts";
|
|
21
21
|
|
|
22
22
|
export interface Config {
|
|
23
23
|
enabled: boolean;
|
|
24
|
+
/** 启动时的提示显示不显示;只来自全局设置,没写时跟着 pi 的 quietStartup。 */
|
|
25
|
+
quietStartup?: boolean;
|
|
24
26
|
/** 顶层的参数:规则没写的字段用它。 */
|
|
25
27
|
defaults: PolicyPatch;
|
|
26
28
|
rules: Rule[];
|
|
@@ -28,26 +30,40 @@ export interface Config {
|
|
|
28
30
|
problems: string[];
|
|
29
31
|
}
|
|
30
32
|
|
|
31
|
-
export const CONFIG_KEYS: ReadonlySet<string> = new Set([
|
|
33
|
+
export const CONFIG_KEYS: ReadonlySet<string> = new Set([
|
|
34
|
+
"$schema",
|
|
35
|
+
"enabled",
|
|
36
|
+
"quietStartup",
|
|
37
|
+
"rules",
|
|
38
|
+
...FIELD_NAMES,
|
|
39
|
+
]);
|
|
32
40
|
|
|
33
41
|
/** 一份设置文件读出来的内容:只有写了的字段。 */
|
|
34
42
|
export interface ConfigLayer {
|
|
35
43
|
enabled?: boolean;
|
|
44
|
+
quietStartup?: boolean;
|
|
36
45
|
defaults: PolicyPatch;
|
|
37
46
|
/** 写了 `rules` 才有,空数组也算写了:合并时整份替换前面的。 */
|
|
38
47
|
rules?: Rule[];
|
|
39
48
|
problems: string[];
|
|
40
49
|
}
|
|
41
50
|
|
|
42
|
-
/**
|
|
43
|
-
|
|
51
|
+
/**
|
|
52
|
+
* `raw` 是一份设置文件的内容。不抛,读不懂的记进 problems。`project`:这是项目级的一份,不能写 quietStartup:
|
|
53
|
+
* 不然别人仓库里的设置能关掉「用上了项目级设置」这句提醒。
|
|
54
|
+
*/
|
|
55
|
+
export function parseLayer(raw: Record<string, unknown>, project = false): ConfigLayer {
|
|
44
56
|
const problems: string[] = [];
|
|
45
57
|
if (raw.enabled !== undefined && typeof raw.enabled !== "boolean") problems.push("enabled 需要 true 或 false");
|
|
58
|
+
const quiet = raw.quietStartup;
|
|
59
|
+
if (quiet !== undefined && project) problems.push("quietStartup 只能写在全局设置");
|
|
60
|
+
else if (quiet !== undefined && typeof quiet !== "boolean") problems.push("quietStartup 需要 true 或 false");
|
|
46
61
|
const defaults = readParams(raw, "", problems);
|
|
47
62
|
for (const key of Object.keys(raw)) if (!CONFIG_KEYS.has(key)) problems.push(`${key} 不认识`);
|
|
48
63
|
const rules = raw.rules === undefined ? undefined : parseRules(raw.rules, problems);
|
|
49
64
|
return {
|
|
50
65
|
...(typeof raw.enabled === "boolean" && { enabled: raw.enabled }),
|
|
66
|
+
...(!project && typeof quiet === "boolean" && { quietStartup: quiet }),
|
|
51
67
|
defaults,
|
|
52
68
|
...(rules && { rules }),
|
|
53
69
|
problems,
|
|
@@ -57,16 +73,18 @@ export function parseLayer(raw: Record<string, unknown>): ConfigLayer {
|
|
|
57
73
|
/** 后面的层覆盖前面的:顶层字段逐个覆盖,`rules` 整份替换;问题都留着,任何一层有问题整份设置都不生效。 */
|
|
58
74
|
export function mergeLayers(layers: readonly ConfigLayer[]): Config {
|
|
59
75
|
let enabled = true;
|
|
76
|
+
let quietStartup: boolean | undefined;
|
|
60
77
|
let defaults: PolicyPatch = {};
|
|
61
78
|
let rules: Rule[] = [];
|
|
62
79
|
const problems: string[] = [];
|
|
63
80
|
for (const layer of layers) {
|
|
64
81
|
if (layer.enabled !== undefined) enabled = layer.enabled;
|
|
82
|
+
if (layer.quietStartup !== undefined) quietStartup = layer.quietStartup;
|
|
65
83
|
defaults = { ...defaults, ...layer.defaults };
|
|
66
84
|
if (layer.rules) rules = layer.rules;
|
|
67
85
|
problems.push(...layer.problems);
|
|
68
86
|
}
|
|
69
|
-
return { enabled, defaults, rules, problems };
|
|
87
|
+
return { enabled, ...(quietStartup !== undefined && { quietStartup }), defaults, rules, problems };
|
|
70
88
|
}
|
|
71
89
|
|
|
72
90
|
/** 开着,并且设置里没有读不懂的内容。 */
|
|
@@ -74,9 +92,14 @@ export function isActive(config: Config): boolean {
|
|
|
74
92
|
return config.enabled && config.problems.length === 0;
|
|
75
93
|
}
|
|
76
94
|
|
|
77
|
-
/**
|
|
95
|
+
/** 只诊断合并后实际采用的规则;有配置错误时先修错,不对部分解析结果给提醒。 */
|
|
96
|
+
export function configWarnings(config: Config): string[] {
|
|
97
|
+
return config.problems.length ? [] : ruleWarnings(config.rules, config.defaults);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** 写回设置文件时的键顺序:`$schema`、`enabled`、参数、`quietStartup`、其他、`rules`。 */
|
|
78
101
|
export function fileOrder(raw: Record<string, unknown>): Record<string, unknown> {
|
|
79
|
-
const first = ["$schema", "enabled", ...FIELD_NAMES];
|
|
102
|
+
const first = ["$schema", "enabled", ...FIELD_NAMES, "quietStartup"];
|
|
80
103
|
const keys = [
|
|
81
104
|
...first.filter((key) => key in raw),
|
|
82
105
|
...Object.keys(raw).filter((key) => !first.includes(key) && key !== "rules"),
|
package/src/policy/failure.ts
CHANGED
|
@@ -31,6 +31,13 @@ export function isFailure(message: AgentMessage | undefined): message is Failure
|
|
|
31
31
|
return isError(message) || isEmptyReply(message);
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
+
/** 匹配用的失败文本,不是展示摘要:空回复用空串;普通错误缺少有效文案时用非空占位,其他原文不动。 */
|
|
35
|
+
export function failureText(failure: Failure): string {
|
|
36
|
+
if (failure.stopReason === "stop") return "";
|
|
37
|
+
const text = failure.errorMessage ?? "";
|
|
38
|
+
return text.trim() ? text : UNKNOWN_ERROR;
|
|
39
|
+
}
|
|
40
|
+
|
|
34
41
|
const SUCCESS: ReadonlySet<AssistantMessage["stopReason"]> = new Set(["stop", "length", "toolUse", "deferred"]);
|
|
35
42
|
|
|
36
43
|
/** 助手正常返回了一次(包括中间的工具轮),说明已经恢复。空回复不算。 */
|
|
@@ -55,8 +62,10 @@ export function normalizeError(text: string): string {
|
|
|
55
62
|
|
|
56
63
|
export function errorSignature(failure: Failure): string {
|
|
57
64
|
if (failure.stopReason === "stop") return EMPTY_REPLY;
|
|
58
|
-
return normalizeError(failure.errorMessage ?? "") ||
|
|
65
|
+
return normalizeError(failure.errorMessage ?? "") || UNKNOWN_ERROR;
|
|
59
66
|
}
|
|
60
67
|
|
|
68
|
+
const UNKNOWN_ERROR = "未知错误";
|
|
69
|
+
|
|
61
70
|
/** 空回复的「错误摘要」:提示词里的 {error}、同错计数都用它。 */
|
|
62
71
|
export const EMPTY_REPLY = "空回复";
|