@cobia/pi-onward 0.3.1 → 0.4.1
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 +186 -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 +25 -0
- package/src/actions/pi.ts +70 -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 +10 -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 +470 -0
- package/src/runtime/task-prompt.ts +82 -0
- package/src/extension/onward.ts +0 -208
- package/src/policy/tracker.ts +0 -64
package/src/extension/command.ts
CHANGED
|
@@ -1,22 +1,10 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* `/onward <动作> [参数]`:动作在前,参数写成 `字段=值`,字段名和设置文件一样。动作、字段、取值和选项都区分大小写,
|
|
3
|
-
* 和设置文件、pi 自己的命令名一样。`set` 全有或全无:任何一项不合法就一项都不写。不写动作时弹出菜单,标明当前状态和修改目标。
|
|
4
|
-
*
|
|
5
|
-
* `--help` 是结束:写在哪都只显示帮助、不执行,跟在动作后面时显示这个动作的帮助。补全在每个动作后面都给它,
|
|
6
|
-
* 写了它之后什么都不再给,后面有没有空格都一样。
|
|
7
|
-
*
|
|
8
|
-
* 看或改哪一份设置文件由 `--scope <global|project>` 定,和 wecom-aibot 的 `config --scope <层> <动作>` 一样写在动作前:
|
|
9
|
-
* `/onward --scope project set maxRetries=3`,也可以写成 `--scope=project`。改设置的动作(on、off、set、unset、reset)
|
|
10
|
-
* 不写就是全局,和 pi 的 `install`、`config` 不加 `--local` 时一样:同一条命令总是写同一个文件,仓库里的文件要写明才改。
|
|
11
|
-
* show 不写时看合并后的结果,写了只看那一份写了什么。写在动作后不执行,报错给出正确的写法;只写 `--scope` 不写动作时,
|
|
12
|
-
* 弹出这一份的菜单,`/onward` 的菜单里也有进到每一份的项。
|
|
13
|
-
*/
|
|
1
|
+
/** 修改默认写全局,查询默认看合并结果;set 先全部校验,--help 不执行修改。 */
|
|
14
2
|
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
15
|
-
import {
|
|
16
|
-
import
|
|
17
|
-
import type { Onward } from "./onward.ts";
|
|
3
|
+
import { DEFAULT_LIMITS, FIELD_NAMES, FIELDS, type Field, fieldNamed } from "../policy/policy.ts";
|
|
4
|
+
import type { Recovery } from "../runtime/recovery.ts";
|
|
18
5
|
import { COMMAND_KEYS, SCOPES, type Scope, type Settings, type SettingsChange } from "./settings.ts";
|
|
19
6
|
import {
|
|
7
|
+
actionsNotice,
|
|
20
8
|
COMMAND,
|
|
21
9
|
changedNotice,
|
|
22
10
|
columns,
|
|
@@ -26,26 +14,20 @@ import {
|
|
|
26
14
|
layerNotice,
|
|
27
15
|
menuTitle,
|
|
28
16
|
type Notice,
|
|
29
|
-
|
|
30
|
-
resetLine,
|
|
17
|
+
reloadNotice,
|
|
31
18
|
rulesNotice,
|
|
32
19
|
SCOPE,
|
|
33
20
|
say,
|
|
34
|
-
scopeLine,
|
|
35
21
|
section,
|
|
36
|
-
shadowLine,
|
|
37
22
|
showNotice,
|
|
38
23
|
statusNotice,
|
|
39
24
|
table,
|
|
25
|
+
toggleNotice,
|
|
40
26
|
} from "./text.ts";
|
|
41
27
|
|
|
42
|
-
/**
|
|
43
|
-
* `topic`:帮助或出错是关于哪个动作的,没有时是总帮助。`choose`:没写动作,弹菜单;写了 `--scope` 时只列改这一份的。
|
|
44
|
-
* `scope`:改哪一份设置文件。
|
|
45
|
-
*/
|
|
46
28
|
export type Command =
|
|
47
29
|
| { kind: "choose"; scope?: Scope }
|
|
48
|
-
| { kind: "status" | "reload" }
|
|
30
|
+
| { kind: "status" | "reload" | "actions" }
|
|
49
31
|
| { kind: "reset"; scope: Scope }
|
|
50
32
|
| { kind: "help"; topic?: string }
|
|
51
33
|
| { kind: "show"; rules: boolean; scope?: Scope }
|
|
@@ -53,9 +35,7 @@ export type Command =
|
|
|
53
35
|
| { kind: "error"; message: string; topic?: string };
|
|
54
36
|
|
|
55
37
|
const HELP = "--help";
|
|
56
|
-
/** 启动提示的开关:不是规则的参数,只能写在全局设置。 */
|
|
57
38
|
const QUIET = "quietStartup";
|
|
58
|
-
/** 帮助的字段表里 quietStartup 那一行。 */
|
|
59
39
|
const QUIET_HELP: [string, string] = [
|
|
60
40
|
QUIET,
|
|
61
41
|
[
|
|
@@ -66,61 +46,78 @@ const QUIET_HELP: [string, string] = [
|
|
|
66
46
|
].join("\n"),
|
|
67
47
|
];
|
|
68
48
|
|
|
69
|
-
|
|
70
|
-
const SCOPE_HELP = `${SCOPE} 写在动作前:不写或 global 改全局设置,project 改当前目录的项目级设置;两份在哪见 /${COMMAND} status`;
|
|
71
|
-
/** 去掉参数的动作:去掉之后按什么走。 */
|
|
49
|
+
const SCOPE_HELP = `${SCOPE} 写在动作前:不写或 global 改全局设置,project 改 Pi 已信任的当前项目设置;两份在哪见 /${COMMAND} show`;
|
|
72
50
|
const SCOPE_FALLBACK = "去掉之后继承下一层:项目级的退回全局,全局的退回内置默认值";
|
|
73
51
|
|
|
74
52
|
interface ActionSpec {
|
|
75
53
|
name: string;
|
|
76
|
-
/** 动作后面参数的写法;没有参数时为空。 */
|
|
77
54
|
args: string;
|
|
78
|
-
/** 一句话:总帮助和补全里用。 */
|
|
79
55
|
description: string;
|
|
80
|
-
/** 这个动作的帮助里多说的。 */
|
|
81
56
|
details: readonly string[];
|
|
82
|
-
/**
|
|
57
|
+
/** 是否在帮助中列出配置字段。 */
|
|
83
58
|
fields?: boolean;
|
|
84
|
-
/**
|
|
59
|
+
/** 是否允许 --scope;区分读配置与写配置。 */
|
|
85
60
|
scoped?: "write" | "read";
|
|
86
61
|
examples?: readonly string[];
|
|
87
62
|
}
|
|
88
63
|
|
|
89
|
-
/**
|
|
64
|
+
/** 命令、帮助、菜单与补全共用的动作定义。 */
|
|
90
65
|
const ACTIONS: readonly ActionSpec[] = [
|
|
66
|
+
{
|
|
67
|
+
name: "actions",
|
|
68
|
+
args: "",
|
|
69
|
+
description: "查看已加载的恢复动作",
|
|
70
|
+
details: ["列出动作名称、执行阶段、支持的运行模式和说明", "注册失败的动作另列诊断;未引用的坏动作不阻断当前配置"],
|
|
71
|
+
},
|
|
91
72
|
{
|
|
92
73
|
name: "status",
|
|
93
74
|
args: "",
|
|
94
|
-
description: "
|
|
75
|
+
description: "查看当前状态",
|
|
95
76
|
details: [
|
|
96
|
-
"
|
|
97
|
-
"
|
|
98
|
-
|
|
77
|
+
"显示自动续跑开关和当前运行状态;待命时显示可用规则数,没有续跑记录时不显示零计数",
|
|
78
|
+
"显示连续失败次数、已启动续跑次数、当前恢复阶段和动作结果",
|
|
79
|
+
"从第几次失败进入哪个阶段由 rules[].stages 决定,规则切换不清零",
|
|
80
|
+
"超过 maxFailures 停止;原生接管受 maxHandoffs 限制",
|
|
81
|
+
`默认参数、规则详情和设置文件位置见 /${COMMAND} show`,
|
|
82
|
+
`配置有问题时只显示原因摘要,详情用 /${COMMAND} show;停止时显示原因和处理提示`,
|
|
99
83
|
],
|
|
100
84
|
},
|
|
101
85
|
{
|
|
102
86
|
name: "on",
|
|
103
87
|
args: "",
|
|
104
|
-
description: "
|
|
105
|
-
details: [
|
|
88
|
+
description: "启用自动续跑",
|
|
89
|
+
details: [
|
|
90
|
+
"在设置文件里写 enabled: true",
|
|
91
|
+
"成功后只确认启用结果和配置范围;当前仍不能续跑时补充原因",
|
|
92
|
+
`设置文件有问题时打开了也不续跑:先改好,再 /${COMMAND} reload`,
|
|
93
|
+
"启用开关会取消当前恢复并清零,不立即发起恢复",
|
|
94
|
+
"保存失败时明确提示;本次运行保留配置修改,重新加载或重启后以原文件为准",
|
|
95
|
+
],
|
|
106
96
|
scoped: "write",
|
|
107
97
|
},
|
|
108
98
|
{
|
|
109
99
|
name: "off",
|
|
110
100
|
args: "",
|
|
111
|
-
description: "
|
|
112
|
-
details: [
|
|
101
|
+
description: "停用自动续跑",
|
|
102
|
+
details: [
|
|
103
|
+
"在设置文件里写 enabled: false",
|
|
104
|
+
"成功后只确认停用结果和配置范围;项目级仍启用时说明覆盖关系",
|
|
105
|
+
"成功修改配置会取消等待、待执行动作并清零计数",
|
|
106
|
+
"保存失败时明确提示;本次运行保留配置修改,重新加载或重启后以原文件为准",
|
|
107
|
+
],
|
|
113
108
|
scoped: "write",
|
|
114
109
|
},
|
|
115
110
|
{
|
|
116
111
|
name: "show",
|
|
117
112
|
args: "[rules]",
|
|
118
|
-
description: "
|
|
113
|
+
description: "查看配置参数、来源和规则",
|
|
119
114
|
details: [
|
|
120
|
-
"
|
|
121
|
-
"
|
|
122
|
-
"
|
|
123
|
-
|
|
115
|
+
"查看当前已加载的配置:开关与启动提示、恢复上限、规则及来源、配置文件位置",
|
|
116
|
+
"参数标明来源及未保存的字段;项目级 rules 整份替换全局规则,不拼接",
|
|
117
|
+
"show rules 只显示规则;按顺序列出,第一条命中的生效,停用的标出来",
|
|
118
|
+
"配置无效时只列具体问题和文件位置,不展示部分参数或规则",
|
|
119
|
+
`写了 ${SCOPE} 时只看本层显式配置,不合并、不填默认值;未写 rules 与显式空列表不同`,
|
|
120
|
+
`手改文件后 /${COMMAND} reload;show 本身不重载、不写配置`,
|
|
124
121
|
],
|
|
125
122
|
scoped: "read",
|
|
126
123
|
examples: [`/${COMMAND} show rules`, `/${COMMAND} ${SCOPE} project show`],
|
|
@@ -128,83 +125,98 @@ const ACTIONS: readonly ActionSpec[] = [
|
|
|
128
125
|
{
|
|
129
126
|
name: "set",
|
|
130
127
|
args: "字段=值 …",
|
|
131
|
-
description: "
|
|
128
|
+
description: "修改配置参数",
|
|
132
129
|
details: [
|
|
133
130
|
"可以一次写几个:先全部检查,有一项不合法就一项都不改,问题一起说",
|
|
134
|
-
|
|
131
|
+
"成功后只回显本次修改值和目标范围;项目级覆盖与当前不可用原因另行说明",
|
|
132
|
+
"修改恢复过程的上限、动作执行时限和全局 quietStartup",
|
|
133
|
+
"写入失败时本次运行保留配置修改,重新加载或重启后以原文件为准",
|
|
134
|
+
"修改配置会取消当前恢复过程",
|
|
135
135
|
`rules 只能在设置文件里手写,改完 /${COMMAND} reload;开关用 /${COMMAND} on、off`,
|
|
136
136
|
],
|
|
137
137
|
fields: true,
|
|
138
138
|
scoped: "write",
|
|
139
139
|
examples: [
|
|
140
|
-
`/${COMMAND} set
|
|
141
|
-
`/${COMMAND} set
|
|
142
|
-
`/${COMMAND} ${SCOPE} project set
|
|
140
|
+
`/${COMMAND} set maxFailures=20 actionTimeout=30s`,
|
|
141
|
+
`/${COMMAND} set maxHandoffs=2`,
|
|
142
|
+
`/${COMMAND} ${SCOPE} project set maxFailures=3`,
|
|
143
143
|
],
|
|
144
144
|
},
|
|
145
145
|
{
|
|
146
146
|
name: "unset",
|
|
147
147
|
args: "字段 …",
|
|
148
|
-
description: "
|
|
148
|
+
description: "移除指定配置项",
|
|
149
149
|
details: [
|
|
150
150
|
"可以一次写几个,开关 enabled、启动提示 quietStartup 也能去掉;有不认识或写了两次的字段就一项都不改",
|
|
151
|
-
"
|
|
151
|
+
"规则中的阶段与动作不受影响",
|
|
152
|
+
"成功后说明移除后当前采用的值和来源;配置无效时不展示继承值",
|
|
153
|
+
"写入失败时本次运行保留配置修改,重新加载或重启后以原文件为准",
|
|
152
154
|
SCOPE_FALLBACK,
|
|
153
155
|
],
|
|
154
156
|
fields: true,
|
|
155
157
|
scoped: "write",
|
|
156
|
-
examples: [`/${COMMAND} unset
|
|
158
|
+
examples: [`/${COMMAND} unset maxFailures actionTimeout`, `/${COMMAND} ${SCOPE} project unset enabled`],
|
|
157
159
|
},
|
|
158
160
|
{
|
|
159
161
|
name: "reload",
|
|
160
162
|
args: "",
|
|
161
|
-
description: "
|
|
162
|
-
details: [
|
|
163
|
+
description: "重新加载配置",
|
|
164
|
+
details: [
|
|
165
|
+
"丢弃未保存修改,重读配置;项目级仅在绑定且受信任时读取,会话开始前只读全局",
|
|
166
|
+
`成功时确认读取范围;配置无效时说明原因摘要,详情见 /${COMMAND} show`,
|
|
167
|
+
"重载会取消等待、待执行动作和本轮计数,不主动发起恢复",
|
|
168
|
+
],
|
|
163
169
|
},
|
|
164
170
|
{
|
|
165
171
|
name: "reset",
|
|
166
172
|
args: "",
|
|
167
|
-
description: "
|
|
173
|
+
description: "重置配置参数,保留规则",
|
|
168
174
|
details: [
|
|
169
175
|
"开关、参数和 quietStartup 都去掉;rules 和其他手写的内容留着",
|
|
170
|
-
"
|
|
176
|
+
"反馈确认重置范围和保留内容,不把重置某一层说成整个项目恢复内置默认值",
|
|
177
|
+
"正在等的续跑立刻打断,运行状态和计数清零;即使目标配置无法读取或保存也一样",
|
|
178
|
+
"写入失败时本次运行保留配置修改,重新加载或重启后以原文件为准",
|
|
171
179
|
SCOPE_FALLBACK,
|
|
172
180
|
],
|
|
173
181
|
scoped: "write",
|
|
174
182
|
},
|
|
175
183
|
];
|
|
176
184
|
|
|
177
|
-
const DESCRIPTION =
|
|
185
|
+
const DESCRIPTION = "控制自动续跑,查看状态和配置";
|
|
178
186
|
|
|
179
|
-
export function registerCommand(pi: ExtensionAPI, settings: Settings, onward:
|
|
187
|
+
export function registerCommand(pi: ExtensionAPI, settings: Settings, onward: Recovery): void {
|
|
180
188
|
pi.registerCommand(COMMAND, {
|
|
181
189
|
description: DESCRIPTION,
|
|
182
190
|
getArgumentCompletions: (prefix) => completeArguments(prefix),
|
|
183
191
|
handler: async (args, ctx) => {
|
|
192
|
+
if (args.startsWith("_dispatch ")) {
|
|
193
|
+
await onward.dispatch(args.slice(10), ctx);
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
184
196
|
let command = parseCommand(args);
|
|
185
|
-
//
|
|
197
|
+
// 选中作用域后继续进入该层菜单。
|
|
186
198
|
while (command.kind === "choose") {
|
|
187
|
-
const picked = await choose(ctx, settings, command.scope);
|
|
199
|
+
const picked = await choose(ctx, settings, onward, command.scope);
|
|
188
200
|
if (picked === undefined) return;
|
|
189
201
|
command = parseCommand(picked);
|
|
190
202
|
}
|
|
191
203
|
const notice = execute(command, settings, onward, ctx);
|
|
192
|
-
// 不管是命令关的、还是 reload 读到手改的关或有问题的设置,不生效了就不该还有等着的续跑。
|
|
193
|
-
if (!isActive(settings.config)) onward.stop(ctx);
|
|
194
204
|
say(ctx, notice);
|
|
195
205
|
},
|
|
196
206
|
});
|
|
197
207
|
}
|
|
198
208
|
|
|
199
|
-
/**
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
209
|
+
/** 必填参数的动作只填入编辑器;其余返回可执行命令。 */
|
|
210
|
+
async function choose(
|
|
211
|
+
ctx: ExtensionContext,
|
|
212
|
+
settings: Settings,
|
|
213
|
+
onward: Recovery,
|
|
214
|
+
scope?: Scope,
|
|
215
|
+
): Promise<string | undefined> {
|
|
204
216
|
if (!ctx.hasUI) return undefined;
|
|
205
217
|
const items = menuItems(scope);
|
|
206
218
|
const label = await ctx.ui.select(
|
|
207
|
-
menuTitle(settings, scope),
|
|
219
|
+
menuTitle(settings, scope, onward.status().state),
|
|
208
220
|
items.map((item) => item.label),
|
|
209
221
|
);
|
|
210
222
|
const picked = items.find((item) => item.label === label);
|
|
@@ -213,11 +225,6 @@ async function choose(ctx: ExtensionContext, settings: Settings, scope?: Scope):
|
|
|
213
225
|
return undefined;
|
|
214
226
|
}
|
|
215
227
|
|
|
216
|
-
/**
|
|
217
|
-
* 每个动作一项,再是进到每一份设置的菜单的 `--scope <层> …`,最后是 --help;和帮助里的动作表一样只写名字,参数留给
|
|
218
|
-
* 各自动作的帮助。`fill`:参数非写不可。写了 `scope` 时是这一份的菜单:只列能写 --scope 的动作,`input` 带上
|
|
219
|
-
* `--scope <scope>`。
|
|
220
|
-
*/
|
|
221
228
|
export function menuItems(scope?: Scope): { label: string; input: string; fill: boolean }[] {
|
|
222
229
|
const actions = scope ? ACTIONS.filter((item) => item.scoped) : ACTIONS;
|
|
223
230
|
const layers = scope ? [] : SCOPES;
|
|
@@ -244,12 +251,16 @@ export function menuItems(scope?: Scope): { label: string; input: string; fill:
|
|
|
244
251
|
function execute(
|
|
245
252
|
command: Exclude<Command, { kind: "choose" }>,
|
|
246
253
|
settings: Settings,
|
|
247
|
-
onward:
|
|
254
|
+
onward: Recovery,
|
|
248
255
|
ctx: ExtensionContext,
|
|
249
256
|
): Notice {
|
|
257
|
+
// Read-only queries never change the recovery state.
|
|
258
|
+
const status = () => onward.status();
|
|
250
259
|
switch (command.kind) {
|
|
260
|
+
case "actions":
|
|
261
|
+
return actionsNotice(onward.registry.list(), onward.registry.diagnostics);
|
|
251
262
|
case "status":
|
|
252
|
-
return statusNotice(settings,
|
|
263
|
+
return statusNotice(settings, status());
|
|
253
264
|
case "help":
|
|
254
265
|
return info(helpText(command.topic));
|
|
255
266
|
case "error":
|
|
@@ -259,42 +270,27 @@ function execute(
|
|
|
259
270
|
return command.rules ? rulesNotice(settings) : showNotice(settings);
|
|
260
271
|
case "reload":
|
|
261
272
|
settings.reload();
|
|
262
|
-
return statusNotice(settings, onward.progress());
|
|
263
|
-
case "reset": {
|
|
264
273
|
onward.stop(ctx);
|
|
274
|
+
return reloadNotice(settings, status());
|
|
275
|
+
case "reset": {
|
|
265
276
|
const saved = settings.reset(command.scope);
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
return changedNotice(saved, settings, onward.progress(), done);
|
|
277
|
+
onward.stop(ctx);
|
|
278
|
+
return changedNotice(saved, settings, status(), { action: "reset", scope: command.scope, keys: COMMAND_KEYS });
|
|
269
279
|
}
|
|
270
280
|
case "update": {
|
|
271
281
|
const saved = settings.update(command.scope, command.change);
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
282
|
+
if (saved.kind === "saved" || saved.kind === "unwritten") onward.stop(ctx);
|
|
283
|
+
if (command.change.enabled !== undefined)
|
|
284
|
+
return toggleNotice(saved, settings, status(), command.scope, command.change.enabled);
|
|
285
|
+
return changedNotice(saved, settings, status(), {
|
|
286
|
+
action: command.change.unset ? "unset" : "set",
|
|
287
|
+
scope: command.scope,
|
|
288
|
+
keys: command.change.unset ?? Object.keys(command.change.set ?? {}),
|
|
289
|
+
});
|
|
279
290
|
}
|
|
280
291
|
}
|
|
281
292
|
}
|
|
282
293
|
|
|
283
|
-
/** 跟在标题后面的几行;都没有时是 undefined。 */
|
|
284
|
-
function note(...lines: (string | undefined)[]): string | undefined {
|
|
285
|
-
const kept = lines.filter((line): line is string => line !== undefined);
|
|
286
|
-
return kept.length ? kept.join("\n") : undefined;
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
/** 这次改到的开关和参数。 */
|
|
290
|
-
function changedKeys(change: SettingsChange): string[] {
|
|
291
|
-
return [
|
|
292
|
-
...(change.enabled === undefined ? [] : ["enabled"]),
|
|
293
|
-
...Object.keys(change.set ?? {}),
|
|
294
|
-
...(change.unset ?? []),
|
|
295
|
-
];
|
|
296
|
-
}
|
|
297
|
-
|
|
298
294
|
export function parseCommand(text: string): Command {
|
|
299
295
|
const tokens = tokenize(text);
|
|
300
296
|
if (typeof tokens === "string") return { kind: "error", message: tokens };
|
|
@@ -308,22 +304,21 @@ export function parseCommand(text: string): Command {
|
|
|
308
304
|
const [first, ...rest] = words;
|
|
309
305
|
const spec = first === undefined ? undefined : actionNamed(first);
|
|
310
306
|
const topic = spec && { topic: spec.name };
|
|
311
|
-
if (count > 1) return { kind: "error", message: `${SCOPE}
|
|
307
|
+
if (count > 1) return { kind: "error", message: `${SCOPE} may only be specified once`, ...topic };
|
|
312
308
|
const scope = count ? scopeNamed(value) : "global";
|
|
313
309
|
// 先看 --scope 的值:`--scope set …` 是漏写了值,不是动作写错了。
|
|
314
310
|
if (scope === undefined)
|
|
315
311
|
return {
|
|
316
312
|
kind: "error",
|
|
317
|
-
message: `${SCOPE} ${value === undefined ? "
|
|
313
|
+
message: `${SCOPE} ${value === undefined ? "requires" : "must be"} ${SCOPES.join(" or ")}`,
|
|
318
314
|
...topic,
|
|
319
315
|
};
|
|
320
316
|
if (first !== undefined && !spec) return unknownAction(first);
|
|
321
317
|
if (count && spec && !spec.scoped) return { kind: "error", message: scopedOnly(), topic: spec.name };
|
|
322
|
-
// --scope 是命令的目标,写在动作前;写在别处不执行,给出正确的写法。
|
|
323
318
|
if (count && at !== 0)
|
|
324
319
|
return {
|
|
325
320
|
kind: "error",
|
|
326
|
-
message: `${SCOPE}
|
|
321
|
+
message: `${SCOPE} must precede the action: /${COMMAND} ${[SCOPE, scope, ...words.map(quoted)].join(" ")}`,
|
|
327
322
|
...topic,
|
|
328
323
|
};
|
|
329
324
|
if (!spec) return count ? { kind: "choose", scope } : { kind: "choose" };
|
|
@@ -331,6 +326,7 @@ export function parseCommand(text: string): Command {
|
|
|
331
326
|
switch (action) {
|
|
332
327
|
case "status":
|
|
333
328
|
case "reload":
|
|
329
|
+
case "actions":
|
|
334
330
|
return none(action, rest) ?? { kind: action };
|
|
335
331
|
case "reset":
|
|
336
332
|
return none(action, rest) ?? { kind: "reset", scope };
|
|
@@ -338,11 +334,10 @@ export function parseCommand(text: string): Command {
|
|
|
338
334
|
case "off":
|
|
339
335
|
return none(action, rest) ?? { kind: "update", scope, change: { enabled: action === "on" } };
|
|
340
336
|
case "show": {
|
|
341
|
-
// 不写 --scope 是合并后的结果,不是全局那一份。
|
|
342
337
|
const only = count ? { scope } : {};
|
|
343
338
|
if (!rest.length) return { kind: "show", rules: false, ...only };
|
|
344
339
|
if (rest.length === 1 && rest[0] === "rules") return { kind: "show", rules: true, ...only };
|
|
345
|
-
return { kind: "error", message: "show
|
|
340
|
+
return { kind: "error", message: "show only accepts the optional argument 'rules'", topic: "show" };
|
|
346
341
|
}
|
|
347
342
|
case "set":
|
|
348
343
|
return parseSet(rest, scope);
|
|
@@ -353,10 +348,7 @@ export function parseCommand(text: string): Command {
|
|
|
353
348
|
}
|
|
354
349
|
}
|
|
355
350
|
|
|
356
|
-
/**
|
|
357
|
-
* 把 `--scope <值>`、`--scope=<值>` 从词里拿出来。`at` 是第一个 `--scope` 前面有几个别的词:
|
|
358
|
-
* 只有 0 是写在动作前;`value` 是它的值,没写时是 undefined;`count` 是写了几次。`-` 开头的词(如 --help)不算它的值。
|
|
359
|
-
*/
|
|
351
|
+
/** at 是首个 --scope 前的其他词数;只有 0 合法。支持 --scope=x 与 --scope x。 */
|
|
360
352
|
function splitScope(tokens: readonly string[]): { words: string[]; at: number; value?: string; count: number } {
|
|
361
353
|
const words: string[] = [];
|
|
362
354
|
let at = -1;
|
|
@@ -385,7 +377,7 @@ function splitScope(tokens: readonly string[]): { words: string[]; at: number; v
|
|
|
385
377
|
return { words, at, ...(value !== undefined && { value }), count };
|
|
386
378
|
}
|
|
387
379
|
|
|
388
|
-
/**
|
|
380
|
+
/** inline 区分 --scope 与 --scope=(包括空值)。 */
|
|
389
381
|
function scopeOption(token: string): { inline: boolean; value?: string } | undefined {
|
|
390
382
|
const match = /^--scope(?:=(.*))?$/.exec(token);
|
|
391
383
|
if (!match) return undefined;
|
|
@@ -396,7 +388,6 @@ function scopeNamed(value: string | undefined): Scope | undefined {
|
|
|
396
388
|
return SCOPES.find((scope) => scope === value);
|
|
397
389
|
}
|
|
398
390
|
|
|
399
|
-
/** 给出正确写法时,含空白的词重新加上引号。 */
|
|
400
391
|
function quoted(word: string): string {
|
|
401
392
|
if (!/\s/.test(word)) return word;
|
|
402
393
|
const equals = word.indexOf("=");
|
|
@@ -404,40 +395,43 @@ function quoted(word: string): string {
|
|
|
404
395
|
}
|
|
405
396
|
|
|
406
397
|
function scopedOnly(): string {
|
|
407
|
-
return `${SCOPE}
|
|
398
|
+
return `${SCOPE} is only supported by ${scopedActions()}`;
|
|
408
399
|
}
|
|
409
400
|
|
|
410
|
-
/** 能写 --scope 的动作;`write`:只要改设置的。 */
|
|
411
401
|
function scopedActions(only?: "write"): string {
|
|
412
402
|
return ACTIONS.filter((item) => (only ? item.scoped === only : item.scoped))
|
|
413
403
|
.map((item) => item.name)
|
|
414
|
-
.join("
|
|
404
|
+
.join(", ");
|
|
415
405
|
}
|
|
416
406
|
|
|
417
407
|
function unknownAction(word: string): Command {
|
|
418
|
-
return { kind: "error", message:
|
|
408
|
+
return { kind: "error", message: `Unknown action '${word}'; available actions: ${actionList()}` };
|
|
419
409
|
}
|
|
420
410
|
|
|
421
|
-
/**
|
|
411
|
+
/** 拒绝无参动作后多写的参数。 */
|
|
422
412
|
function none(action: string, rest: string[]): Command | undefined {
|
|
423
|
-
return rest.length ? { kind: "error", message: `${action}
|
|
413
|
+
return rest.length ? { kind: "error", message: `${action} does not accept arguments`, topic: action } : undefined;
|
|
424
414
|
}
|
|
425
415
|
|
|
426
416
|
function parseSet(tokens: string[], scope: Scope): Command {
|
|
427
417
|
if (!tokens.length)
|
|
428
|
-
return {
|
|
418
|
+
return {
|
|
419
|
+
kind: "error",
|
|
420
|
+
message: `set requires field=value; available fields: ${settable(scope).join(", ")}`,
|
|
421
|
+
topic: "set",
|
|
422
|
+
};
|
|
429
423
|
const problems: string[] = [];
|
|
430
424
|
const values: Record<string, unknown> = {};
|
|
431
425
|
for (const token of tokens) {
|
|
432
426
|
const equals = token.indexOf("=");
|
|
433
427
|
if (equals <= 0) {
|
|
434
|
-
problems.push(
|
|
428
|
+
problems.push(`'${token}' must use field=value`);
|
|
435
429
|
continue;
|
|
436
430
|
}
|
|
437
431
|
const key = token.slice(0, equals);
|
|
438
432
|
const text = token.slice(equals + 1);
|
|
439
433
|
if (Object.hasOwn(values, key)) {
|
|
440
|
-
problems.push(`${key}
|
|
434
|
+
problems.push(`${key} may only be specified once`);
|
|
441
435
|
continue;
|
|
442
436
|
}
|
|
443
437
|
const parsed = key === QUIET ? quietValue(text, scope) : paramValue(key, text, scope);
|
|
@@ -457,57 +451,56 @@ function paramValue(key: string, text: string, scope: Scope): Parsed {
|
|
|
457
451
|
return "problem" in read ? { problem: `${known.name} ${read.problem}` } : { name: known.name, value };
|
|
458
452
|
}
|
|
459
453
|
|
|
460
|
-
/**
|
|
454
|
+
/** 项目不能自行隐藏启用项目配置的提示。 */
|
|
461
455
|
function quietValue(text: string, scope: Scope): Parsed {
|
|
462
|
-
if (scope !== "global")
|
|
463
|
-
|
|
456
|
+
if (scope !== "global")
|
|
457
|
+
return { problem: `${QUIET} is only allowed in global settings; omit ${SCOPE} to use global scope` };
|
|
458
|
+
if (text !== "true" && text !== "false") return { problem: `${QUIET} must be true or false` };
|
|
464
459
|
return { name: QUIET, value: text === "true" };
|
|
465
460
|
}
|
|
466
461
|
|
|
467
|
-
/** set 能写的字段:参数,全局还有 quietStartup。 */
|
|
468
462
|
function settable(scope: Scope): readonly string[] {
|
|
469
463
|
return scope === "global" ? [...FIELD_NAMES, QUIET] : FIELD_NAMES;
|
|
470
464
|
}
|
|
471
465
|
|
|
472
|
-
/**
|
|
473
|
-
* unset 能去掉的:参数,和开关 enabled。开关有两份设置时也要能去掉:去掉项目级的才继承全局的。
|
|
474
|
-
*/
|
|
475
466
|
function parseUnset(tokens: string[], scope: Scope): Command {
|
|
476
467
|
if (!tokens.length)
|
|
477
|
-
return {
|
|
468
|
+
return {
|
|
469
|
+
kind: "error",
|
|
470
|
+
message: `unset requires field names; available fields: ${COMMAND_KEYS.join(", ")}`,
|
|
471
|
+
topic: "unset",
|
|
472
|
+
};
|
|
478
473
|
const problems: string[] = [];
|
|
479
474
|
const unset: string[] = [];
|
|
480
475
|
for (const token of tokens) {
|
|
481
476
|
const key = unsetKey(token, scope);
|
|
482
477
|
if ("problem" in key) problems.push(key.problem);
|
|
483
|
-
else if (unset.includes(key.name)) problems.push(`${key.name}
|
|
478
|
+
else if (unset.includes(key.name)) problems.push(`${key.name} may only be specified once`);
|
|
484
479
|
else unset.push(key.name);
|
|
485
480
|
}
|
|
486
481
|
return problems.length ? rejected(problems, "unset") : { kind: "update", scope, change: { unset } };
|
|
487
482
|
}
|
|
488
483
|
|
|
489
484
|
function unsetKey(token: string, scope: Scope): { name: string } | { problem: string } {
|
|
490
|
-
// quietStartup
|
|
485
|
+
// 允许删除项目层误写的 quietStartup,以修复配置。
|
|
491
486
|
if (token === "enabled" || token === QUIET) return { name: token };
|
|
492
487
|
const known = paramField(token, scope, COMMAND_KEYS);
|
|
493
488
|
return typeof known === "string" ? { problem: known } : { name: known.name };
|
|
494
489
|
}
|
|
495
490
|
|
|
496
|
-
/**
|
|
497
|
-
* 参数字段;不是时说明为什么。开关的提示写同一份的写法。`names` 是这个动作能写的字段,不认识时列出来。
|
|
498
|
-
*/
|
|
499
491
|
function paramField(key: string, scope: Scope, names: readonly string[] = FIELD_NAMES): Field | string {
|
|
500
|
-
if (key === "rules") return `rules
|
|
501
|
-
if (key === "enabled")
|
|
502
|
-
|
|
492
|
+
if (key === "rules") return `rules must be edited in the settings file; then run /${COMMAND} reload`;
|
|
493
|
+
if (key === "enabled")
|
|
494
|
+
return `use /${COMMAND} ${scope === "project" ? `${SCOPE} project ` : ""}on or off to change enabled`;
|
|
495
|
+
return fieldNamed(key) ?? `Unknown field '${key}'; available fields: ${names.join(", ")}`;
|
|
503
496
|
}
|
|
504
497
|
|
|
505
498
|
function rejected(problems: string[], topic: string): Command {
|
|
506
|
-
return { kind: "error", message: `${problems.join("
|
|
499
|
+
return { kind: "error", message: `${problems.join("; ")}. No changes made`, topic };
|
|
507
500
|
}
|
|
508
501
|
|
|
509
502
|
function actionList(): string {
|
|
510
|
-
return ACTIONS.map((item) => item.name).join("
|
|
503
|
+
return ACTIONS.map((item) => item.name).join(", ");
|
|
511
504
|
}
|
|
512
505
|
|
|
513
506
|
function actionNamed(name: string): ActionSpec | undefined {
|
|
@@ -516,7 +509,6 @@ function actionNamed(name: string): ActionSpec | undefined {
|
|
|
516
509
|
|
|
517
510
|
// ── 帮助 ──
|
|
518
511
|
|
|
519
|
-
/** `topic` 是动作名:这个动作的帮助;没有时是总帮助。 */
|
|
520
512
|
export function helpText(topic?: string): string {
|
|
521
513
|
const spec = topic === undefined ? undefined : actionNamed(topic);
|
|
522
514
|
return spec ? actionHelp(spec) : overallHelp();
|
|
@@ -524,7 +516,7 @@ export function helpText(topic?: string): string {
|
|
|
524
516
|
|
|
525
517
|
function overallHelp(): string {
|
|
526
518
|
return [
|
|
527
|
-
|
|
519
|
+
`${COMMAND} — ${DESCRIPTION}`,
|
|
528
520
|
`用法:/${COMMAND} [${SCOPE} <${SCOPES.join("|")}>] [动作] [参数] [${HELP}]`,
|
|
529
521
|
...section("动作", table(ACTIONS.map((item) => [item.name, item.description]))),
|
|
530
522
|
"",
|
|
@@ -545,7 +537,14 @@ function actionHelp(spec: ActionSpec): string {
|
|
|
545
537
|
[...spec.details, ...(spec.scoped === "write" ? [SCOPE_HELP] : [])].map((line) => ` ${line}`),
|
|
546
538
|
),
|
|
547
539
|
...(spec.fields
|
|
548
|
-
? section(
|
|
540
|
+
? section(
|
|
541
|
+
"字段",
|
|
542
|
+
table([
|
|
543
|
+
...(spec.name === "unset" ? [["enabled", "自动续跑开关;去掉后继承下一层"] as [string, string]] : []),
|
|
544
|
+
...FIELDS.map((item): [string, string] => [item.name, fieldHelp(item)]),
|
|
545
|
+
QUIET_HELP,
|
|
546
|
+
]),
|
|
547
|
+
)
|
|
549
548
|
: []),
|
|
550
549
|
...section(
|
|
551
550
|
"示例",
|
|
@@ -554,32 +553,24 @@ function actionHelp(spec: ActionSpec): string {
|
|
|
554
553
|
].join("\n");
|
|
555
554
|
}
|
|
556
555
|
|
|
557
|
-
/** 能写 --scope 的动作前面写着 `[--scope <global|project>]`。 */
|
|
558
556
|
function usage(spec: ActionSpec): string {
|
|
559
557
|
const words = [spec.scoped ? `[${SCOPE} <${SCOPES.join("|")}>]` : "", spec.name, spec.args];
|
|
560
558
|
return words.filter(Boolean).join(" ");
|
|
561
559
|
}
|
|
562
560
|
|
|
563
|
-
/** 作用一行,写法和默认值一行;有固定取值的逐个列出,标出默认的那个。 */
|
|
564
561
|
function fieldHelp(item: Field): string {
|
|
565
|
-
|
|
566
|
-
const values = item.choices
|
|
567
|
-
? item.choices.map(
|
|
568
|
-
(choice) => `${choice.value}:${choice.description}${choice.value === fallback ? "(默认)" : ""}`,
|
|
569
|
-
)
|
|
570
|
-
: [`${item.syntax};默认:${item.show(fallback)}`];
|
|
571
|
-
return [item.description, ...values].join("\n");
|
|
562
|
+
return [item.description, `${item.syntax};默认:${item.show(DEFAULT_LIMITS[item.name])}`].join("\n");
|
|
572
563
|
}
|
|
573
564
|
|
|
574
565
|
// ── 分词 ──
|
|
575
566
|
|
|
576
|
-
/**
|
|
567
|
+
/** 与补全共用分词,未闭合引号只在执行时拒绝。 */
|
|
577
568
|
function tokenize(text: string): string[] | string {
|
|
578
569
|
const { words, quoted } = scan(text);
|
|
579
|
-
return quoted ? "
|
|
570
|
+
return quoted ? "Unmatched double quote" : words;
|
|
580
571
|
}
|
|
581
572
|
|
|
582
|
-
/**
|
|
573
|
+
/** start 为当前词的起点;quoted 表示引号未闭合,补全时允许这种中间状态。 */
|
|
583
574
|
function scan(text: string): { words: string[]; start: number; quoted: boolean } {
|
|
584
575
|
const words: string[] = [];
|
|
585
576
|
let word = "";
|
|
@@ -612,13 +603,7 @@ interface Candidate {
|
|
|
612
603
|
description: string;
|
|
613
604
|
}
|
|
614
605
|
|
|
615
|
-
/**
|
|
616
|
-
* 补全:动作前的 --scope 和它的取值、动作、show 的 rules、set 和 unset 的字段名、action 的取值,以及每个动作后面的
|
|
617
|
-
* --help。返回的 value 换掉「/onward 」后面的全部文字,正在写的词前面原样保留。
|
|
618
|
-
*
|
|
619
|
-
* 选中的词后面还能接着写时补一个空格,下一下 Tab 补下一层;`字段=` 停在 = 后面写值。写了 --help 就结束了,
|
|
620
|
-
* 什么都不再给。写完的词不给列表,回车直接执行;写过的字段和引号里的值不补。
|
|
621
|
-
*/
|
|
606
|
+
/** value 替换 /onward 后的全部参数;保留已输入前缀,仅补全当前词。 */
|
|
622
607
|
export function completeArguments(prefix: string): { value: string; label: string }[] | null {
|
|
623
608
|
const { words, start } = scan(prefix);
|
|
624
609
|
const current = prefix.slice(start);
|
|
@@ -640,10 +625,6 @@ export function completeArguments(prefix: string): { value: string; label: strin
|
|
|
640
625
|
}));
|
|
641
626
|
}
|
|
642
627
|
|
|
643
|
-
/**
|
|
644
|
-
* `words` 后面能写的词;--help 之后、写错了的地方都是空的。--scope 只在动作前给:写在别处执行时会被拒绝;写了它之后
|
|
645
|
-
* 只给能写 --scope 的动作。`--scope=<值>` 和 `--scope <值>` 一样看。
|
|
646
|
-
*/
|
|
647
628
|
function candidates(typed: string[], current: string): Candidate[] {
|
|
648
629
|
if (typed.includes(HELP)) return [];
|
|
649
630
|
const words = typed.flatMap((word) => {
|
|
@@ -662,7 +643,7 @@ function candidates(typed: string[], current: string): Candidate[] {
|
|
|
662
643
|
const [first, ...rest] = scoped ? words.slice(2) : words;
|
|
663
644
|
if (first === undefined)
|
|
664
645
|
return [
|
|
665
|
-
...(scoped ? [] : [{ value: SCOPE, description: "
|
|
646
|
+
...(scoped ? [] : [{ value: SCOPE, description: "指定查看或修改的配置范围" }]),
|
|
666
647
|
...ACTIONS.filter((item) => !scoped || item.scoped).map((item) => ({
|
|
667
648
|
value: item.name,
|
|
668
649
|
description: item.description,
|
|
@@ -675,7 +656,7 @@ function candidates(typed: string[], current: string): Candidate[] {
|
|
|
675
656
|
return args ? [...args, { value: HELP, description: `显示 ${spec.name} 的帮助,不执行` }] : [];
|
|
676
657
|
}
|
|
677
658
|
|
|
678
|
-
/**
|
|
659
|
+
/** undefined 表示输入已无效,不再补全 --help。 */
|
|
679
660
|
function argumentCandidates(action: string, rest: string[], current: string, scope: Scope): Candidate[] | undefined {
|
|
680
661
|
switch (action) {
|
|
681
662
|
case "show":
|
|
@@ -689,25 +670,22 @@ function argumentCandidates(action: string, rest: string[], current: string, sco
|
|
|
689
670
|
}
|
|
690
671
|
}
|
|
691
672
|
|
|
692
|
-
/** quietStartup= 后面能写的。 */
|
|
693
673
|
const QUIET_CHOICES: readonly Candidate[] = [
|
|
694
674
|
{ value: "true", description: "启动时不显示 onward 的提示;设置有问题时照样警告" },
|
|
695
675
|
{ value: "false", description: "照常显示,不管 pi 的 quietStartup" },
|
|
696
676
|
];
|
|
697
677
|
|
|
698
678
|
function fieldCandidates(action: string, rest: string[], current: string, scope: Scope): Candidate[] | undefined {
|
|
699
|
-
// quietStartup
|
|
679
|
+
// 项目层不提示 quietStartup,但允许手写 unset 来修复误配置。
|
|
700
680
|
const offered = scope === "global";
|
|
701
681
|
const named = (name: string) =>
|
|
702
682
|
fieldNamed(name) !== undefined || (name === QUIET && (offered || action === "unset"));
|
|
703
|
-
// 前面有执行时会被拒绝的词(不认识的字段、大小写不对的、没写 = 或值的),什么都不给。
|
|
704
683
|
const valid = (word: string) => {
|
|
705
684
|
if (action === "unset") return word === "enabled" || named(word);
|
|
706
685
|
const equals = word.indexOf("=");
|
|
707
686
|
return equals > 0 && equals < word.length - 1 && named(word.slice(0, equals));
|
|
708
687
|
};
|
|
709
688
|
if (!rest.every(valid)) return undefined;
|
|
710
|
-
// 写过的字段不再给:再写一次,执行时会被拒绝。
|
|
711
689
|
const written = new Set(rest.map((word) => word.split("=")[0]));
|
|
712
690
|
const unwritten = FIELDS.filter((item) => !written.has(item.name));
|
|
713
691
|
const quiet = offered && !written.has(QUIET);
|
|
@@ -729,10 +707,5 @@ function fieldCandidates(action: string, rest: string[], current: string, scope:
|
|
|
729
707
|
if (written.has(name)) return undefined;
|
|
730
708
|
if (name === QUIET)
|
|
731
709
|
return quiet ? QUIET_CHOICES.map((choice) => ({ ...choice, value: `${QUIET}=${choice.value}` })) : undefined;
|
|
732
|
-
|
|
733
|
-
if (!known) return undefined;
|
|
734
|
-
return (known.choices ?? []).map((choice) => ({
|
|
735
|
-
value: `${known.name}=${choice.value}`,
|
|
736
|
-
description: choice.description,
|
|
737
|
-
}));
|
|
710
|
+
return fieldNamed(name) ? [] : undefined;
|
|
738
711
|
}
|