@cobia/pi-onward 0.5.1 → 0.5.3
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/CHANGELOG.md +16 -0
- package/LICENSE +2 -0
- package/README.md +25 -7
- package/example.json +1 -1
- package/onward.schema.json +1 -1
- package/package.json +17 -14
- package/src/actions/pi.ts +3 -3
- package/src/actions/registry.ts +7 -7
- package/src/extension/command-display.ts +217 -0
- package/src/extension/command.ts +210 -226
- package/src/extension/settings.ts +2 -4
- package/src/extension/text.ts +96 -85
- package/src/policy/config.ts +3 -3
- package/src/policy/delay.ts +4 -4
- package/src/policy/policy.ts +2 -2
- package/src/policy/rules.ts +20 -22
- package/src/runtime/recovery.ts +32 -29
- package/docs/actions.md +0 -97
- package/src/policy/schema.ts +0 -94
|
@@ -195,11 +195,9 @@ function readJson(path: string): ReadResult {
|
|
|
195
195
|
try {
|
|
196
196
|
value = JSON.parse(text);
|
|
197
197
|
} catch (error: unknown) {
|
|
198
|
-
return { kind: "broken", error:
|
|
198
|
+
return { kind: "broken", error: `配置文件不是有效的 JSON:${errorText(error)}` };
|
|
199
199
|
}
|
|
200
|
-
return isRecord(value)
|
|
201
|
-
? { kind: "ok", value }
|
|
202
|
-
: { kind: "broken", error: "Top-level configuration must be a JSON object" };
|
|
200
|
+
return isRecord(value) ? { kind: "ok", value } : { kind: "broken", error: "配置文件顶层必须是 JSON 对象" };
|
|
203
201
|
}
|
|
204
202
|
|
|
205
203
|
/** 临时文件写完再原子改名,避免留下半写的配置。 */
|
package/src/extension/text.ts
CHANGED
|
@@ -5,51 +5,32 @@ import { type Config, configWarnings } from "../policy/config.ts";
|
|
|
5
5
|
import { delayText } from "../policy/delay.ts";
|
|
6
6
|
import { FIELDS, fieldNamed, type LimitsPatch, resolveLimits } from "../policy/policy.ts";
|
|
7
7
|
import type { Rule } from "../policy/rules.ts";
|
|
8
|
-
import type
|
|
8
|
+
import { type OnwardStatus, type RunState, stateIcon } from "../runtime/recovery.ts";
|
|
9
|
+
import { commandError, notify, type Row, report, table, title } from "./command-display.ts";
|
|
9
10
|
import type { Saved, Scope, Settings, SettingsFile } from "./settings.ts";
|
|
10
11
|
|
|
11
12
|
export const COMMAND = "onward";
|
|
12
13
|
export const SCOPE = "--scope";
|
|
13
14
|
export interface Notice {
|
|
14
15
|
message: string;
|
|
15
|
-
level: "info" | "warning";
|
|
16
|
+
level: "info" | "warning" | "error";
|
|
16
17
|
}
|
|
17
18
|
export const info = (message: string): Notice => ({ message, level: "info" });
|
|
18
19
|
const warning = (message: string): Notice => ({ message, level: "warning" });
|
|
19
20
|
const NAMES = { global: "全局", project: "项目级" } as const;
|
|
20
|
-
|
|
21
|
-
|
|
21
|
+
/** 没有界面时不发;多行内容由 notify 按通知宽度预先折行。 */
|
|
22
|
+
export function say(ctx: Pick<ExtensionContext, "hasUI" | "ui" | "mode">, notice: Notice): void {
|
|
23
|
+
if (ctx.hasUI) notify(ctx, notice.message, notice.level);
|
|
22
24
|
}
|
|
23
|
-
|
|
24
|
-
return [...
|
|
25
|
-
(sum, char) =>
|
|
26
|
-
sum + (/[\u1100-\u115f\u2e80-\ua4cf\uac00-\ud7a3\uf900-\ufaff\uff00-\uff60\uffe0-\uffe6]/.test(char) ? 2 : 1),
|
|
27
|
-
0,
|
|
28
|
-
);
|
|
29
|
-
}
|
|
30
|
-
export function table(rows: [string, string][]): string[] {
|
|
31
|
-
const width = Math.max(0, ...rows.map(([key]) => cells(key)));
|
|
32
|
-
return rows.flatMap(([key, value]) => {
|
|
33
|
-
const [first, ...rest] = value.split("\n");
|
|
34
|
-
return [
|
|
35
|
-
` ${key}${" ".repeat(width - cells(key))} ${first}`,
|
|
36
|
-
...rest.map((line) => ` ${" ".repeat(width)} ${line}`),
|
|
37
|
-
];
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
export function columns(rows: [string, string][]): string[] {
|
|
41
|
-
const width = Math.max(0, ...rows.map(([key]) => cells(key)));
|
|
42
|
-
return rows.map(([key, value]) => `${key}${" ".repeat(width - cells(key))} ${value.replace(/\s*\n\s*/g, " ")}`);
|
|
43
|
-
}
|
|
44
|
-
export function section(title: string, lines: string[]): string[] {
|
|
45
|
-
return lines.length ? ["", title, ...lines] : [];
|
|
25
|
+
function section(heading: string, lines: string[]): string[] {
|
|
26
|
+
return lines.length ? ["", heading, ...lines] : [];
|
|
46
27
|
}
|
|
47
|
-
export function commandErrorNotice(
|
|
48
|
-
return
|
|
28
|
+
export function commandErrorNotice(problems: readonly string[], topic?: string): Notice {
|
|
29
|
+
return { message: commandError(COMMAND, problems, `/${COMMAND}${topic ? ` ${topic}` : ""} --help`), level: "error" };
|
|
49
30
|
}
|
|
50
|
-
export const layerMenuText = (scope: Scope): string => `只看、只改${NAMES[scope]}设置`;
|
|
51
31
|
export function menuTitle(settings: Settings, scope?: Scope, state?: RunState): string {
|
|
52
|
-
|
|
32
|
+
const { icon, text } = statusOf(settings, state);
|
|
33
|
+
return scope ? title(icon, `${COMMAND} ${SCOPE} ${scope}`, text) : title(icon, COMMAND, text, "默认改全局配置");
|
|
53
34
|
}
|
|
54
35
|
const PHASES: Record<RunState["phase"], string> = {
|
|
55
36
|
idle: "待命",
|
|
@@ -72,18 +53,28 @@ const RESULTS: Record<string, string> = {
|
|
|
72
53
|
function usable(config: Config): number {
|
|
73
54
|
return config.rules.filter((rule) => rule.enabled).length;
|
|
74
55
|
}
|
|
75
|
-
|
|
56
|
+
/** 插件当前状态:菜单、status 和查看输出的标题都用它;恢复进行中时图标与状态栏相同。 */
|
|
57
|
+
export function statusOf(settings: Settings, state?: RunState): { icon: string; text: string } {
|
|
76
58
|
const config = settings.config;
|
|
77
|
-
if (config.problems.length) return "🛑
|
|
78
|
-
if (!config.enabled) return "💤
|
|
59
|
+
if (config.problems.length) return { icon: "🛑", text: "配置无效,修好之前不会自动恢复" };
|
|
60
|
+
if (!config.enabled) return { icon: "💤", text: "关,出错时不会自动恢复" };
|
|
79
61
|
if (state && state.phase !== "idle")
|
|
80
|
-
return
|
|
81
|
-
|
|
62
|
+
return {
|
|
63
|
+
icon: stateIcon(state),
|
|
64
|
+
text: state.result ? (RESULTS[state.result] ?? state.result) : PHASES[state.phase],
|
|
65
|
+
};
|
|
66
|
+
return usable(config)
|
|
67
|
+
? { icon: "⏩", text: `开,${usable(config)} 条规则生效` }
|
|
68
|
+
: { icon: "💤", text: "开,但没有可用的规则" };
|
|
69
|
+
}
|
|
70
|
+
function headline(settings: Settings, state: RunState | undefined, path: string, subject?: string): string {
|
|
71
|
+
const { icon, text } = statusOf(settings, state);
|
|
72
|
+
return title(icon, path, subject ?? text);
|
|
82
73
|
}
|
|
83
74
|
export function statusNotice(settings: Settings, status: OnwardStatus): Notice {
|
|
84
75
|
const config = settings.config;
|
|
85
76
|
const { state } = status;
|
|
86
|
-
const rows: [
|
|
77
|
+
const rows: Row[] = [
|
|
87
78
|
["自动恢复", config.enabled ? "已启用" : "已停用"],
|
|
88
79
|
[
|
|
89
80
|
"当前状态",
|
|
@@ -111,24 +102,30 @@ export function statusNotice(settings: Settings, status: OnwardStatus): Notice {
|
|
|
111
102
|
if (state.result) rows.push(["动作结果", RESULTS[state.result] ?? state.result]);
|
|
112
103
|
if (state.message) rows.push(["说明", state.message]);
|
|
113
104
|
}
|
|
114
|
-
const
|
|
105
|
+
const hints: string[] = [];
|
|
115
106
|
const attention = state.phase === "stopped" && !["completed", "dispatched", "handoff"].includes(state.result ?? "");
|
|
116
|
-
if (state.phase === "stopped")
|
|
117
|
-
|
|
107
|
+
if (state.phase === "stopped")
|
|
108
|
+
hints.push(
|
|
118
109
|
state.result === "dispatched"
|
|
119
|
-
? "后续执行与结果由目标插件负责;onward
|
|
110
|
+
? "后续执行与结果由目标插件负责;onward 不会自动重放命令"
|
|
120
111
|
: state.result === "handoff"
|
|
121
|
-
? "
|
|
112
|
+
? "后续恢复由动作提供者负责"
|
|
122
113
|
: state.result === "completed"
|
|
123
|
-
? "
|
|
124
|
-
: "
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
return attention || config.problems.length ? warning(
|
|
114
|
+
? "本次动作已完成;没有自动追加模型请求"
|
|
115
|
+
: "处理原因后发消息开启新一轮;未自动重放动作",
|
|
116
|
+
);
|
|
117
|
+
const text = report(headline(settings, state, COMMAND), [rows, hints]);
|
|
118
|
+
return attention || config.problems.length ? warning(text) : info(text);
|
|
128
119
|
}
|
|
129
|
-
export function actionsNotice(
|
|
120
|
+
export function actionsNotice(
|
|
121
|
+
actions: readonly ActionDefinition[],
|
|
122
|
+
diagnostics: readonly string[],
|
|
123
|
+
settings: Settings,
|
|
124
|
+
state: RunState,
|
|
125
|
+
): Notice {
|
|
130
126
|
const text = [
|
|
131
|
-
|
|
127
|
+
headline(settings, state, `${COMMAND} actions`, `${actions.length} 个已加载`),
|
|
128
|
+
"",
|
|
132
129
|
...table(
|
|
133
130
|
actions.map((action) => [
|
|
134
131
|
action.name,
|
|
@@ -143,21 +140,22 @@ export function actionsNotice(actions: readonly ActionDefinition[], diagnostics:
|
|
|
143
140
|
return diagnostics.length ? warning(text) : info(text);
|
|
144
141
|
}
|
|
145
142
|
|
|
146
|
-
function fileRows(files: readonly SettingsFile[]): [
|
|
143
|
+
function fileRows(files: readonly SettingsFile[]): Row[] {
|
|
147
144
|
const sources = { file: "", none: "(未创建)", broken: "(无法读取)", untrusted: "(未受信任,未读取)" };
|
|
148
145
|
return files.map((file) => [
|
|
149
146
|
NAMES[file.scope],
|
|
150
147
|
`${file.path}${sources[file.source.kind]}${file.unsaved.length ? `(未保存:${file.unsaved.join("、")})` : ""}`,
|
|
151
148
|
]);
|
|
152
149
|
}
|
|
153
|
-
function showProblems(settings: Settings): Notice {
|
|
150
|
+
function showProblems(settings: Settings, path: string): Notice {
|
|
154
151
|
return warning(
|
|
155
152
|
[
|
|
156
|
-
"
|
|
153
|
+
title("🛑", path, "配置无效,修好之前不会自动恢复"),
|
|
154
|
+
"",
|
|
157
155
|
...settings.config.problems.map((problem) => `- ${problem}`),
|
|
158
156
|
...section("配置文件", table(fileRows(settings.files))),
|
|
159
157
|
"",
|
|
160
|
-
"修复后使用 Pi 的 /reload
|
|
158
|
+
"修复后使用 Pi 的 /reload 重新加载配置",
|
|
161
159
|
].join("\n"),
|
|
162
160
|
);
|
|
163
161
|
}
|
|
@@ -176,11 +174,12 @@ function shown(key: string, values: LimitsPatch): string {
|
|
|
176
174
|
const field = fieldNamed(key)!;
|
|
177
175
|
return field.show(values[field.name]!);
|
|
178
176
|
}
|
|
179
|
-
export function showNotice(settings: Settings): Notice {
|
|
180
|
-
|
|
177
|
+
export function showNotice(settings: Settings, state: RunState): Notice {
|
|
178
|
+
const path = `${COMMAND} show`;
|
|
179
|
+
if (settings.config.problems.length) return showProblems(settings, path);
|
|
181
180
|
return info(
|
|
182
181
|
[
|
|
183
|
-
|
|
182
|
+
headline(settings, state, path, "合并结果"),
|
|
184
183
|
...section("开关与启动提示", table(["enabled", "quietStartup"].map((key) => [key, effective(settings, key)]))),
|
|
185
184
|
...section("恢复上限", table(FIELDS.map((field) => [field.name, effective(settings, field.name)]))),
|
|
186
185
|
"",
|
|
@@ -189,20 +188,24 @@ export function showNotice(settings: Settings): Notice {
|
|
|
189
188
|
].join("\n"),
|
|
190
189
|
);
|
|
191
190
|
}
|
|
192
|
-
export function rulesNotice(settings: Settings): Notice {
|
|
193
|
-
|
|
191
|
+
export function rulesNotice(settings: Settings, state: RunState): Notice {
|
|
192
|
+
const path = `${COMMAND} show rules`;
|
|
193
|
+
if (settings.config.problems.length) return showProblems(settings, path);
|
|
194
|
+
return info(
|
|
195
|
+
[headline(settings, state, path, `${settings.config.rules.length} 条规则`), "", rulesText(settings)].join("\n"),
|
|
196
|
+
);
|
|
194
197
|
}
|
|
195
198
|
function rulesText(settings: Settings): string {
|
|
196
199
|
const origin = settings.origin("rules");
|
|
197
200
|
return describeRules(
|
|
198
201
|
settings.config,
|
|
199
|
-
`规则(${settings.config.rules.length} 条,${origin ? `来源:${NAMES[origin]}配置` : "未配置"})${origin === "project" && settings.has("global", "rules") ? "\n项目级 rules 整份替换全局 rules
|
|
202
|
+
`规则(${settings.config.rules.length} 条,${origin ? `来源:${NAMES[origin]}配置` : "未配置"})${origin === "project" && settings.has("global", "rules") ? "\n项目级 rules 整份替换全局 rules" : ""}`,
|
|
200
203
|
);
|
|
201
204
|
}
|
|
202
|
-
export function describeRules(config: Config,
|
|
205
|
+
export function describeRules(config: Config, heading = `规则(${config.rules.length} 条)`): string {
|
|
203
206
|
if (config.problems.length) return config.problems.join("\n");
|
|
204
|
-
const lines = [
|
|
205
|
-
if (!config.rules.length) lines.push("
|
|
207
|
+
const lines = [heading, "按顺序匹配第一条规则;按连续失败次数选择已达到阈值的最后一个阶段"];
|
|
208
|
+
if (!config.rules.length) lines.push("没有可用的规则;在配置文件的 rules 里写规则,再用 Pi 的 /reload 加载");
|
|
206
209
|
for (const [index, rule] of config.rules.entries()) {
|
|
207
210
|
lines.push(`${index + 1}. ${rule.name}${rule.enabled ? "" : "(已停用)"}:${describeWhen(rule)}`);
|
|
208
211
|
for (const stage of rule.stages)
|
|
@@ -210,7 +213,7 @@ export function describeRules(config: Config, title = `规则(${config.rules.l
|
|
|
210
213
|
` 从第 ${stage.minFailures} 次失败:${stage.action.use};间隔 ${delayText(stage.delay)}${stage.action.with ? `;参数 ${JSON.stringify(stage.action.with)}` : ""}`,
|
|
211
214
|
);
|
|
212
215
|
}
|
|
213
|
-
lines.push(...section("
|
|
216
|
+
lines.push(...section("规则提醒(不影响配置生效)", configWarnings(config)));
|
|
214
217
|
return lines.join("\n");
|
|
215
218
|
}
|
|
216
219
|
function describeWhen({ when }: Rule): string {
|
|
@@ -224,18 +227,26 @@ function describeWhen({ when }: Rule): string {
|
|
|
224
227
|
.join(",") || "任何失败"
|
|
225
228
|
);
|
|
226
229
|
}
|
|
227
|
-
export function layerNotice(settings: Settings, scope: Scope, rulesOnly: boolean): Notice {
|
|
230
|
+
export function layerNotice(settings: Settings, scope: Scope, rulesOnly: boolean, state: RunState): Notice {
|
|
228
231
|
const file = settings.file(scope);
|
|
229
|
-
if (!file) return warning("onward
|
|
230
|
-
const lines = [
|
|
232
|
+
if (!file) return warning("onward:当前目录尚未确定,请在会话开始后重试");
|
|
233
|
+
const lines = [
|
|
234
|
+
headline(
|
|
235
|
+
settings,
|
|
236
|
+
state,
|
|
237
|
+
`${COMMAND} ${SCOPE} ${scope} show${rulesOnly ? " rules" : ""}`,
|
|
238
|
+
`${NAMES[scope]}配置,只看本层`,
|
|
239
|
+
),
|
|
240
|
+
"",
|
|
241
|
+
];
|
|
231
242
|
if (file.source.kind === "untrusted")
|
|
232
|
-
return warning([...lines, "
|
|
243
|
+
return warning([...lines, "项目未受信任,未读取项目配置", ...table(fileRows([file]))].join("\n"));
|
|
233
244
|
if (file.layer.problems.length)
|
|
234
245
|
return warning([...lines, ...file.layer.problems, ...section("配置文件", table(fileRows([file])))].join("\n"));
|
|
235
|
-
if (file.unsaved.length) lines.push(`未保存的配置项:${file.unsaved.join("、")}
|
|
236
|
-
if (file.source.kind === "none") lines.push("
|
|
246
|
+
if (file.unsaved.length) lines.push(`未保存的配置项:${file.unsaved.join("、")};重载后以磁盘为准`);
|
|
247
|
+
if (file.source.kind === "none") lines.push("配置文件未创建");
|
|
237
248
|
if (!rulesOnly) {
|
|
238
|
-
const rows: [
|
|
249
|
+
const rows: Row[] = [];
|
|
239
250
|
if (file.layer.enabled !== undefined) rows.push(["enabled", String(file.layer.enabled)]);
|
|
240
251
|
if (file.layer.quietStartup !== undefined) rows.push(["quietStartup", String(file.layer.quietStartup)]);
|
|
241
252
|
for (const field of FIELDS)
|
|
@@ -244,18 +255,18 @@ export function layerNotice(settings: Settings, scope: Scope, rulesOnly: boolean
|
|
|
244
255
|
lines.push(...table(rows));
|
|
245
256
|
}
|
|
246
257
|
if (file.layer.rules === undefined)
|
|
247
|
-
lines.push(`未写 rules,${scope === "project" ? "继承全局配置的规则" : "没有规则"}
|
|
258
|
+
lines.push(`未写 rules,${scope === "project" ? "继承全局配置的规则" : "没有规则"}`);
|
|
248
259
|
else if (!file.layer.rules.length)
|
|
249
|
-
lines.push(`规则(0 条,显式空列表);${scope === "project" ? "不使用全局规则" : "没有规则"}
|
|
260
|
+
lines.push(`规则(0 条,显式空列表);${scope === "project" ? "不使用全局规则" : "没有规则"}`);
|
|
250
261
|
else lines.push(describeRules({ enabled: true, limits: file.layer.limits, rules: file.layer.rules, problems: [] }));
|
|
251
262
|
if (!rulesOnly) lines.push(...section("配置文件", table(fileRows([file]))));
|
|
252
263
|
return info(lines.join("\n"));
|
|
253
264
|
}
|
|
254
265
|
function restriction(settings: Settings, state: RunState): string | undefined {
|
|
255
|
-
if (settings.config.problems.length) return "配置无效,暂时无法自动恢复;详情见 /onward show
|
|
256
|
-
if (!settings.config.enabled) return "
|
|
257
|
-
if (!usable(settings.config)) return "
|
|
258
|
-
if (state.phase === "stopped") return "
|
|
266
|
+
if (settings.config.problems.length) return "配置无效,暂时无法自动恢复;详情见 /onward show";
|
|
267
|
+
if (!settings.config.enabled) return "当前自动恢复已停用";
|
|
268
|
+
if (!usable(settings.config)) return "当前没有可用规则,暂时不会自动恢复";
|
|
269
|
+
if (state.phase === "stopped") return "本轮恢复已停止,发消息开启新一轮";
|
|
259
270
|
return undefined;
|
|
260
271
|
}
|
|
261
272
|
function saveBlocker(saved: Saved): string | undefined {
|
|
@@ -281,14 +292,14 @@ export function toggleNotice(
|
|
|
281
292
|
const verb = enabled ? "启用" : "停用";
|
|
282
293
|
const target = `${NAMES[scope]}配置`;
|
|
283
294
|
const blocker = saveBlocker(saved);
|
|
284
|
-
if (blocker) return warning(`onward:未能${verb}自动恢复(${target})\n${blocker}
|
|
295
|
+
if (blocker) return warning(`onward:未能${verb}自动恢复(${target})\n${blocker},配置未修改`);
|
|
285
296
|
const lines = [
|
|
286
297
|
saved.kind === "saved"
|
|
287
298
|
? `onward:已${verb}自动恢复(${target})`
|
|
288
|
-
: `onward
|
|
299
|
+
: `onward:已${verb}自动恢复,但保存失败(${target})\n本次运行保留了配置修改;重新加载或重启后以原文件为准`,
|
|
289
300
|
];
|
|
290
301
|
if (scope === "global" && settings.config.enabled !== enabled && settings.origin("enabled") === "project")
|
|
291
|
-
lines.push(`当前项目仍${settings.config.enabled ? "启用" : "停用"}
|
|
302
|
+
lines.push(`当前项目仍${settings.config.enabled ? "启用" : "停用"}自动恢复:项目级配置覆盖了全局配置`);
|
|
292
303
|
else if (enabled) {
|
|
293
304
|
const reason = restriction(settings, status.state);
|
|
294
305
|
if (reason) lines.push(reason);
|
|
@@ -307,12 +318,12 @@ export function changedNotice(
|
|
|
307
318
|
const blocker = saveBlocker(saved);
|
|
308
319
|
if (blocker)
|
|
309
320
|
return warning(
|
|
310
|
-
`onward:未能${label}(${target})\n${blocker}
|
|
321
|
+
`onward:未能${label}(${target})\n${blocker},配置未修改${action === "reset" ? "\n本轮恢复状态和计数已重置" : ""}`,
|
|
311
322
|
);
|
|
312
323
|
const lines = [
|
|
313
324
|
saved.kind === "saved"
|
|
314
325
|
? `onward:已${label}(${target})`
|
|
315
|
-
: `onward:配置修改保存失败(${target})\n
|
|
326
|
+
: `onward:配置修改保存失败(${target})\n本次运行保留了配置修改;重新加载或重启后以原文件为准`,
|
|
316
327
|
];
|
|
317
328
|
if (action === "set") {
|
|
318
329
|
const layer = settings.file(scope)!.layer;
|
|
@@ -324,9 +335,9 @@ export function changedNotice(
|
|
|
324
335
|
} else if (action === "unset") {
|
|
325
336
|
if (settings.config.problems.length) lines.push(`配置项:${keys.join("、")}`);
|
|
326
337
|
else lines.push("当前采用", ...table(keys.map((key) => [key, effective(settings, key)])));
|
|
327
|
-
} else lines.push("已移除开关、恢复上限和 quietStartup;rules
|
|
338
|
+
} else lines.push("已移除开关、恢复上限和 quietStartup;rules 及其他手写内容未改动");
|
|
328
339
|
const covered = scope === "global" ? keys.filter((key) => settings.has("project", key)) : [];
|
|
329
|
-
if (covered.length) lines.push(`当前项目的 ${covered.join("、")} 仍由项目级配置覆盖;详情见 /onward show
|
|
340
|
+
if (covered.length) lines.push(`当前项目的 ${covered.join("、")} 仍由项目级配置覆盖;详情见 /onward show`);
|
|
330
341
|
const reason = restriction(settings, status.state);
|
|
331
342
|
if (reason) lines.push(reason);
|
|
332
343
|
return saved.kind === "unwritten" || reason ? warning(lines.join("\n")) : info(lines.join("\n"));
|
|
@@ -340,11 +351,11 @@ export function startupNotice(settings: Settings, quiet: () => boolean, actionPr
|
|
|
340
351
|
const warnings = configWarnings(settings.config);
|
|
341
352
|
if (warnings.length) notices.push(`规则有 ${warnings.length} 处提醒(/onward show 查看)`);
|
|
342
353
|
if (actionProblems) notices.push(`动作注册有 ${actionProblems} 处问题(/onward actions 查看)`);
|
|
343
|
-
if (notices.length) return warning(`onward:${notices.join(";")}
|
|
354
|
+
if (notices.length) return warning(`onward:${notices.join(";")};不影响当前配置生效`);
|
|
344
355
|
const project = settings.file("project")?.source.kind === "file";
|
|
345
356
|
const idle = settings.config.enabled && !usable(settings.config);
|
|
346
357
|
if ((!project && !idle) || quiet()) return;
|
|
347
358
|
return info(
|
|
348
|
-
`${idle ? "💤 " : ""}onward:${project ? "
|
|
359
|
+
`${idle ? "💤 " : ""}onward:${project ? "用上了项目级配置;" : ""}${idle ? "没有可用的规则,出错时不会自动恢复" : "配置已加载"}(/onward show 查看)`,
|
|
349
360
|
);
|
|
350
361
|
}
|
package/src/policy/config.ts
CHANGED
|
@@ -21,10 +21,10 @@ export const CONFIG_KEYS = new Set(["$schema", "enabled", "quietStartup", "rules
|
|
|
21
21
|
export function parseLayer(raw: Record<string, unknown>, project = false): ConfigLayer {
|
|
22
22
|
const problems: string[] = [];
|
|
23
23
|
unknownKeys(raw, CONFIG_KEYS, "", problems);
|
|
24
|
-
if (raw.enabled !== undefined && typeof raw.enabled !== "boolean") problems.push("enabled
|
|
24
|
+
if (raw.enabled !== undefined && typeof raw.enabled !== "boolean") problems.push("enabled 必须是 true 或 false");
|
|
25
25
|
if (raw.quietStartup !== undefined) {
|
|
26
|
-
if (project) problems.push("quietStartup
|
|
27
|
-
else if (typeof raw.quietStartup !== "boolean") problems.push("quietStartup
|
|
26
|
+
if (project) problems.push("quietStartup 只能写在全局配置");
|
|
27
|
+
else if (typeof raw.quietStartup !== "boolean") problems.push("quietStartup 必须是 true 或 false");
|
|
28
28
|
}
|
|
29
29
|
const limits = readLimits(raw, problems);
|
|
30
30
|
const rules = raw.rules === undefined ? undefined : parseRules(raw.rules, problems);
|
package/src/policy/delay.ts
CHANGED
|
@@ -4,8 +4,8 @@ const MAX_STEPS = 20;
|
|
|
4
4
|
|
|
5
5
|
export type Delay = { kind: "list"; steps: readonly number[] } | { kind: "ramp"; step: number; cap: number };
|
|
6
6
|
|
|
7
|
-
export const DELAY_SYNTAX = "5s,10s,30s
|
|
8
|
-
const MALFORMED =
|
|
7
|
+
export const DELAY_SYNTAX = "5s,10s,30s 或 3s..15s";
|
|
8
|
+
const MALFORMED = `必须写成 ${DELAY_SYNTAX},每个值带 ms、s 或 m`;
|
|
9
9
|
|
|
10
10
|
/** Schema 只校验格式;时长、列表长度与区间关系由 parseDelay 校验。 */
|
|
11
11
|
const DURATION = "\\d+(\\.\\d+)?(ms|s|m)";
|
|
@@ -19,13 +19,13 @@ export function parseDelay(text: string): Delay | string {
|
|
|
19
19
|
const step = parseDuration(range[1]!);
|
|
20
20
|
const cap = parseDuration(range[2]!);
|
|
21
21
|
if (step === undefined || cap === undefined) return MALFORMED;
|
|
22
|
-
if (step <= 0 || cap < step) return "
|
|
22
|
+
if (step <= 0 || cap < step) return "递增间隔的起始值必须大于 0,上限不能小于起始值";
|
|
23
23
|
return { kind: "ramp", step, cap };
|
|
24
24
|
}
|
|
25
25
|
const parts = value.split(",").map((part) => part.trim());
|
|
26
26
|
const steps = parts.map(parseDuration);
|
|
27
27
|
if (!value || steps.some((ms) => ms === undefined)) return MALFORMED;
|
|
28
|
-
if (steps.length > MAX_STEPS) return
|
|
28
|
+
if (steps.length > MAX_STEPS) return `最多只能写 ${MAX_STEPS} 个间隔`;
|
|
29
29
|
return { kind: "list", steps: steps as number[] };
|
|
30
30
|
}
|
|
31
31
|
|
package/src/policy/policy.ts
CHANGED
|
@@ -28,7 +28,7 @@ const integer =
|
|
|
28
28
|
(value: unknown): { value: number } | { problem: string } =>
|
|
29
29
|
Number.isSafeInteger(value) && (value as number) >= min && (value as number) <= max
|
|
30
30
|
? { value: value as number }
|
|
31
|
-
: { problem:
|
|
31
|
+
: { problem: `必须是 ${min}~${max} 的整数` };
|
|
32
32
|
export const FIELDS: readonly Field[] = [
|
|
33
33
|
{
|
|
34
34
|
name: "maxFailures",
|
|
@@ -56,7 +56,7 @@ export const FIELDS: readonly Field[] = [
|
|
|
56
56
|
const ms = typeof value === "string" ? parseDuration(value) : undefined;
|
|
57
57
|
return ms !== undefined && ms > 0
|
|
58
58
|
? { value: ms }
|
|
59
|
-
: { problem: "
|
|
59
|
+
: { problem: "必须是不超过 60m 的正时长,单位用 ms、s 或 m" };
|
|
60
60
|
},
|
|
61
61
|
fromText: String,
|
|
62
62
|
show: formatDelay,
|
package/src/policy/rules.ts
CHANGED
|
@@ -43,49 +43,47 @@ export function selectStage(rule: Rule, failures: number): Stage {
|
|
|
43
43
|
const stage = rule.stages[index]!;
|
|
44
44
|
if (stage.minFailures <= failures) return stage;
|
|
45
45
|
}
|
|
46
|
-
throw new Error("
|
|
46
|
+
throw new Error("没有恢复阶段覆盖当前失败次数");
|
|
47
47
|
}
|
|
48
48
|
export function ruleWarnings(rules: readonly Rule[]): string[] {
|
|
49
49
|
let fallback: string | undefined;
|
|
50
50
|
const warnings: string[] = [];
|
|
51
51
|
for (const rule of rules) {
|
|
52
52
|
if (!rule.enabled) continue;
|
|
53
|
-
if (fallback)
|
|
54
|
-
warnings.push(`Rule "${rule.name}" is shadowed by earlier catch-all rule "${fallback}" and cannot match`);
|
|
53
|
+
if (fallback) warnings.push(`规则 "${rule.name}" 排在兜底规则 "${fallback}" 后面,不会命中`);
|
|
55
54
|
if (!fallback && !Object.keys(rule.when).length) fallback = rule.name;
|
|
56
55
|
}
|
|
57
56
|
return warnings;
|
|
58
57
|
}
|
|
59
58
|
export function parseRules(raw: unknown, problems: string[]): Rule[] {
|
|
60
59
|
if (!Array.isArray(raw)) {
|
|
61
|
-
problems.push("rules
|
|
60
|
+
problems.push("rules 必须是数组");
|
|
62
61
|
return [];
|
|
63
62
|
}
|
|
64
63
|
const rules: Rule[] = [];
|
|
65
64
|
for (const [index, value] of raw.entries()) {
|
|
66
65
|
const label = `rules[${index}]`;
|
|
67
66
|
if (!isRecord(value)) {
|
|
68
|
-
problems.push(`${label}
|
|
67
|
+
problems.push(`${label} 必须是对象`);
|
|
69
68
|
continue;
|
|
70
69
|
}
|
|
71
70
|
const before = problems.length;
|
|
72
71
|
unknownKeys(value, RULE_KEYS, label, problems);
|
|
73
72
|
const name = value.name === undefined ? `rule-${index + 1}` : value.name;
|
|
74
|
-
if (typeof name !== "string" || !name.trim()) problems.push(`${label}.name
|
|
73
|
+
if (typeof name !== "string" || !name.trim()) problems.push(`${label}.name 必须是非空字符串`);
|
|
75
74
|
if (value.enabled !== undefined && typeof value.enabled !== "boolean")
|
|
76
|
-
problems.push(`${label}.enabled
|
|
75
|
+
problems.push(`${label}.enabled 必须是 true 或 false`);
|
|
77
76
|
const when = parseWhen(value.when, `${label}.when`, problems);
|
|
78
77
|
const stages = parseStages(value.stages, `${label}.stages`, problems);
|
|
79
78
|
if (problems.length !== before || typeof name !== "string") continue;
|
|
80
|
-
if (rules.some((rule) => rule.name === name.trim()))
|
|
81
|
-
problems.push(`${label}.name duplicates an earlier rule name`);
|
|
79
|
+
if (rules.some((rule) => rule.name === name.trim())) problems.push(`${label}.name 与前面的规则重名`);
|
|
82
80
|
else rules.push({ name: name.trim(), enabled: value.enabled !== false, when, stages });
|
|
83
81
|
}
|
|
84
82
|
return rules;
|
|
85
83
|
}
|
|
86
84
|
function parseStages(raw: unknown, label: string, problems: string[]): Stage[] {
|
|
87
85
|
if (!Array.isArray(raw) || !raw.length) {
|
|
88
|
-
problems.push(`${label}
|
|
86
|
+
problems.push(`${label} 必须是非空数组`);
|
|
89
87
|
return [];
|
|
90
88
|
}
|
|
91
89
|
const stages: Stage[] = [];
|
|
@@ -93,17 +91,17 @@ function parseStages(raw: unknown, label: string, problems: string[]): Stage[] {
|
|
|
93
91
|
for (const [index, value] of raw.entries()) {
|
|
94
92
|
const at = `${label}[${index}]`;
|
|
95
93
|
if (!isRecord(value)) {
|
|
96
|
-
problems.push(`${at}
|
|
94
|
+
problems.push(`${at} 必须是对象`);
|
|
97
95
|
continue;
|
|
98
96
|
}
|
|
99
97
|
const before = problems.length;
|
|
100
98
|
unknownKeys(value, new Set(["minFailures", "delay", "action"]), at, problems);
|
|
101
99
|
const minFailures = value.minFailures;
|
|
102
100
|
if (!Number.isSafeInteger(minFailures) || (minFailures as number) < 1 || (minFailures as number) > 1000)
|
|
103
|
-
problems.push(`${at}.minFailures
|
|
101
|
+
problems.push(`${at}.minFailures 必须是 1~1000 的整数`);
|
|
104
102
|
else {
|
|
105
|
-
if (index === 0 && minFailures !== 1) problems.push(`${at}.minFailures
|
|
106
|
-
if ((minFailures as number) <= previous) problems.push(`${at}.minFailures
|
|
103
|
+
if (index === 0 && minFailures !== 1) problems.push(`${at}.minFailures 在第一个阶段必须是 1`);
|
|
104
|
+
if ((minFailures as number) <= previous) problems.push(`${at}.minFailures 必须严格递增`);
|
|
107
105
|
previous = minFailures as number;
|
|
108
106
|
}
|
|
109
107
|
const delay =
|
|
@@ -111,17 +109,17 @@ function parseStages(raw: unknown, label: string, problems: string[]): Stage[] {
|
|
|
111
109
|
? parseDelay("0s")
|
|
112
110
|
: typeof value.delay === "string"
|
|
113
111
|
? parseDelay(value.delay)
|
|
114
|
-
: "
|
|
112
|
+
: "必须是带单位的间隔字符串";
|
|
115
113
|
if (typeof delay === "string") problems.push(`${at}.delay ${delay}`);
|
|
116
114
|
const action = value.action;
|
|
117
115
|
if (!isRecord(action)) {
|
|
118
|
-
problems.push(`${at}.action
|
|
116
|
+
problems.push(`${at}.action 必须是对象,包含 "use",可选 "with"`);
|
|
119
117
|
continue;
|
|
120
118
|
}
|
|
121
119
|
unknownKeys(action, new Set(["use", "with"]), `${at}.action`, problems);
|
|
122
120
|
if (typeof action.use !== "string" || !new RegExp(ACTION_NAME_PATTERN).test(action.use))
|
|
123
|
-
problems.push(`${at}.action.use
|
|
124
|
-
if (action.with !== undefined && !isRecord(action.with)) problems.push(`${at}.action.with
|
|
121
|
+
problems.push(`${at}.action.use 必须是带命名空间的动作名,例如 pi.message`);
|
|
122
|
+
if (action.with !== undefined && !isRecord(action.with)) problems.push(`${at}.action.with 必须是对象`);
|
|
125
123
|
if (problems.length === before && typeof delay !== "string")
|
|
126
124
|
stages.push({
|
|
127
125
|
minFailures: minFailures as number,
|
|
@@ -137,7 +135,7 @@ function parseStages(raw: unknown, label: string, problems: string[]): Stage[] {
|
|
|
137
135
|
function parseWhen(raw: unknown, label: string, problems: string[]): RuleWhen {
|
|
138
136
|
if (raw === undefined) return {};
|
|
139
137
|
if (!isRecord(raw)) {
|
|
140
|
-
problems.push(`${label}
|
|
138
|
+
problems.push(`${label} 必须是对象`);
|
|
141
139
|
return {};
|
|
142
140
|
}
|
|
143
141
|
const when: RuleWhen = {};
|
|
@@ -146,7 +144,7 @@ function parseWhen(raw: unknown, label: string, problems: string[]): RuleWhen {
|
|
|
146
144
|
const value = raw[key];
|
|
147
145
|
if (value === undefined) continue;
|
|
148
146
|
if (typeof value !== "string" || !value.trim()) {
|
|
149
|
-
problems.push(`${label}.${key}
|
|
147
|
+
problems.push(`${label}.${key} 必须是非空字符串`);
|
|
150
148
|
continue;
|
|
151
149
|
}
|
|
152
150
|
if (key !== "failure") when[key] = value.trim();
|
|
@@ -154,7 +152,7 @@ function parseWhen(raw: unknown, label: string, problems: string[]): RuleWhen {
|
|
|
154
152
|
try {
|
|
155
153
|
when.failure = new RegExp(value, "i");
|
|
156
154
|
} catch (error) {
|
|
157
|
-
problems.push(`${label}.failure
|
|
155
|
+
problems.push(`${label}.failure 不是有效的正则表达式(${errorText(error)})`);
|
|
158
156
|
}
|
|
159
157
|
}
|
|
160
158
|
}
|
|
@@ -167,5 +165,5 @@ export function unknownKeys(
|
|
|
167
165
|
problems: string[],
|
|
168
166
|
): void {
|
|
169
167
|
for (const key of Object.keys(raw))
|
|
170
|
-
if (!allowed.has(key)) problems.push(
|
|
168
|
+
if (!allowed.has(key)) problems.push(`未知字段 ${label ? `${label}.` : ""}${key}`);
|
|
171
169
|
}
|