@cobia/pi-onward 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +255 -2
- package/example.json +63 -0
- package/package.json +50 -4
- package/src/extension/autocomplete.ts +23 -0
- package/src/extension/command.ts +482 -0
- package/src/extension/index.ts +22 -0
- package/src/extension/install.ts +27 -0
- package/src/extension/onward.ts +208 -0
- package/src/extension/render.ts +22 -0
- package/src/extension/settings.ts +123 -0
- package/src/extension/text.ts +322 -0
- package/src/policy/config.ts +68 -0
- package/src/policy/delay.ts +78 -0
- package/src/policy/failure.ts +40 -0
- package/src/policy/policy.ts +155 -0
- package/src/policy/rules.ts +121 -0
- package/src/policy/tracker.ts +64 -0
- package/src/util.ts +7 -0
|
@@ -0,0 +1,482 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `/onward <动作> [参数]`:动作在前,参数写成 `字段=值`,字段名和设置文件一样。`set` 全有或全无:任何一项
|
|
3
|
+
* 不合法就一项都不写。不写动作时弹出菜单,标题是现在的状态。
|
|
4
|
+
*
|
|
5
|
+
* `--help` 是结束:写在哪都只显示帮助、不执行,跟在动作后面时显示这个动作的帮助。补全在每个动作后面都给它,
|
|
6
|
+
* 写了它之后什么都不再给,后面有没有空格都一样。
|
|
7
|
+
*/
|
|
8
|
+
import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
9
|
+
import { isActive } from "../policy/config.ts";
|
|
10
|
+
import { DEFAULT_POLICY, FIELD_NAMES, FIELDS, type Field, fieldNamed } from "../policy/policy.ts";
|
|
11
|
+
import type { Onward } from "./onward.ts";
|
|
12
|
+
import type { Settings, SettingsChange } from "./settings.ts";
|
|
13
|
+
import {
|
|
14
|
+
COMMAND,
|
|
15
|
+
changedNotice,
|
|
16
|
+
columns,
|
|
17
|
+
commandErrorNotice,
|
|
18
|
+
info,
|
|
19
|
+
type Notice,
|
|
20
|
+
rulesNotice,
|
|
21
|
+
say,
|
|
22
|
+
section,
|
|
23
|
+
showNotice,
|
|
24
|
+
statusHeadline,
|
|
25
|
+
statusNotice,
|
|
26
|
+
table,
|
|
27
|
+
} from "./text.ts";
|
|
28
|
+
|
|
29
|
+
/** `topic`:帮助或出错是关于哪个动作的,没有时是总帮助。`choose`:没写动作,弹菜单。 */
|
|
30
|
+
export type Command =
|
|
31
|
+
| { kind: "choose" }
|
|
32
|
+
| { kind: "status" | "reload" | "reset" }
|
|
33
|
+
| { kind: "help"; topic?: string }
|
|
34
|
+
| { kind: "show"; rules: boolean }
|
|
35
|
+
| { kind: "update"; change: SettingsChange }
|
|
36
|
+
| { kind: "error"; message: string; topic?: string };
|
|
37
|
+
|
|
38
|
+
const HELP = "--help";
|
|
39
|
+
|
|
40
|
+
interface ActionSpec {
|
|
41
|
+
name: string;
|
|
42
|
+
/** 动作后面参数的写法;没有参数时为空。 */
|
|
43
|
+
args: string;
|
|
44
|
+
/** 一句话:总帮助和补全里用。 */
|
|
45
|
+
description: string;
|
|
46
|
+
/** 这个动作的帮助里多说的。 */
|
|
47
|
+
details: readonly string[];
|
|
48
|
+
/** 帮助里列出字段表:set、unset。 */
|
|
49
|
+
fields?: boolean;
|
|
50
|
+
examples?: readonly string[];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** 每个动作:参数的写法和作用。帮助、补全都从这张表来。 */
|
|
54
|
+
const ACTIONS: readonly ActionSpec[] = [
|
|
55
|
+
{
|
|
56
|
+
name: "status",
|
|
57
|
+
args: "",
|
|
58
|
+
description: "现在会不会续跑、规则没写的参数、设置文件",
|
|
59
|
+
details: [
|
|
60
|
+
"第一行说现在会不会续跑;正在续跑时下一行是进度",
|
|
61
|
+
"下面是规则没写的参数(做法、上限、间隔)和设置文件在哪",
|
|
62
|
+
`设置文件有问题时只说有几处,详情用 /${COMMAND} show`,
|
|
63
|
+
],
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
name: "on",
|
|
67
|
+
args: "",
|
|
68
|
+
description: "打开",
|
|
69
|
+
details: ["在设置文件里写 enabled: true", `设置文件有问题时打开了也不续跑:先改好,再 /${COMMAND} reload`],
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
name: "off",
|
|
73
|
+
args: "",
|
|
74
|
+
description: "关掉",
|
|
75
|
+
details: ["在设置文件里写 enabled: false", "正在等的续跑立刻打断,计数清零"],
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
name: "show",
|
|
79
|
+
args: "[rules]",
|
|
80
|
+
description: "各项参数和来源、规则、设置文件里的问题;rules 只看规则",
|
|
81
|
+
details: [
|
|
82
|
+
"每个参数的值,以及它来自设置文件还是内置默认值",
|
|
83
|
+
"规则按顺序列出,第一条命中的生效;停用的标出来",
|
|
84
|
+
"设置文件有问题时不列规则,只列问题",
|
|
85
|
+
],
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
name: "set",
|
|
89
|
+
args: "字段=值 …",
|
|
90
|
+
description: "改设置文件顶层的参数,规则没写的字段用它;一项不合法就都不改",
|
|
91
|
+
details: [
|
|
92
|
+
"可以一次写几个:先全部检查,有一项不合法就一项都不改,问题一起说",
|
|
93
|
+
'值里有空格时加双引号:prompt="接着做,第 {attempt} 次"',
|
|
94
|
+
`rules 只能在设置文件里手写,改完 /${COMMAND} reload;开关用 /${COMMAND} on、off`,
|
|
95
|
+
],
|
|
96
|
+
fields: true,
|
|
97
|
+
examples: [
|
|
98
|
+
`/${COMMAND} set action=resend maxRetries=20 delay=5s,10s,30s`,
|
|
99
|
+
`/${COMMAND} set prompt="接着做 {error}"`,
|
|
100
|
+
],
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
name: "unset",
|
|
104
|
+
args: "字段 …",
|
|
105
|
+
description: "去掉参数,退回内置默认值",
|
|
106
|
+
details: ["可以一次写几个;有不认识或写了两次的字段就一项都不改", "规则里写的同名字段不受影响"],
|
|
107
|
+
fields: true,
|
|
108
|
+
examples: [`/${COMMAND} unset delay maxRetries`],
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
name: "reload",
|
|
112
|
+
args: "",
|
|
113
|
+
description: "重新读设置文件(手改 rules 之后用)",
|
|
114
|
+
details: ["读完显示和 status 一样的状态", "读到关着或有问题的设置时,正在等的续跑立刻打断"],
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
name: "reset",
|
|
118
|
+
args: "",
|
|
119
|
+
description: "去掉开关和参数,保留手写的 rules",
|
|
120
|
+
details: ["开关回到打开,参数回到内置默认值;rules 和其他手写的内容留着", "正在等的续跑立刻打断,计数清零"],
|
|
121
|
+
},
|
|
122
|
+
];
|
|
123
|
+
|
|
124
|
+
const DESCRIPTION = `出错后按规则自动续跑(不写动作时弹出菜单,${HELP} 查看用法)`;
|
|
125
|
+
|
|
126
|
+
export function registerCommand(pi: ExtensionAPI, settings: Settings, onward: Onward): void {
|
|
127
|
+
pi.registerCommand(COMMAND, {
|
|
128
|
+
description: DESCRIPTION,
|
|
129
|
+
getArgumentCompletions: (prefix) => completeArguments(prefix),
|
|
130
|
+
handler: async (args, ctx) => {
|
|
131
|
+
let command = parseCommand(args);
|
|
132
|
+
if (command.kind === "choose") {
|
|
133
|
+
const picked = await choose(ctx, settings);
|
|
134
|
+
if (picked === undefined) return;
|
|
135
|
+
command = parseCommand(picked);
|
|
136
|
+
}
|
|
137
|
+
if (command.kind === "choose") return;
|
|
138
|
+
const notice = execute(command, settings, onward, ctx);
|
|
139
|
+
// 不管是命令关的、还是 reload 读到手改的关或有问题的设置,不生效了就不该还有等着的续跑。
|
|
140
|
+
if (!isActive(settings.config)) onward.stop(ctx);
|
|
141
|
+
say(ctx, notice);
|
|
142
|
+
},
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* `/onward` 不写动作:弹出菜单,标题是现在的状态。返回选中的动作;要写参数的(set、unset)填进输入框接着写,
|
|
148
|
+
* 返回 undefined。没有界面时不弹,什么都不做。
|
|
149
|
+
*/
|
|
150
|
+
async function choose(ctx: ExtensionContext, settings: Settings): Promise<string | undefined> {
|
|
151
|
+
if (!ctx.hasUI) return undefined;
|
|
152
|
+
const items = menuItems();
|
|
153
|
+
const label = await ctx.ui.select(
|
|
154
|
+
statusHeadline(settings),
|
|
155
|
+
items.map((item) => item.label),
|
|
156
|
+
);
|
|
157
|
+
const picked = items.find((item) => item.label === label);
|
|
158
|
+
if (!picked?.fill) return picked?.input;
|
|
159
|
+
ctx.ui.setEditorText(`/${COMMAND} ${picked.input} `);
|
|
160
|
+
return undefined;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** 每个动作一项,最后是 --help;和帮助里的动作表一样只写名字,参数留给各自动作的帮助。`fill`:参数非写不可。 */
|
|
164
|
+
export function menuItems(): { label: string; input: string; fill: boolean }[] {
|
|
165
|
+
const labels = columns([
|
|
166
|
+
...ACTIONS.map((item): [string, string] => [item.name, item.description]),
|
|
167
|
+
[HELP, "显示帮助"],
|
|
168
|
+
]);
|
|
169
|
+
return [
|
|
170
|
+
...ACTIONS.map((item, index) => ({
|
|
171
|
+
label: labels[index]!,
|
|
172
|
+
input: item.name,
|
|
173
|
+
fill: item.args !== "" && !item.args.startsWith("["),
|
|
174
|
+
})),
|
|
175
|
+
{ label: labels.at(-1)!, input: HELP, fill: false },
|
|
176
|
+
];
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function execute(
|
|
180
|
+
command: Exclude<Command, { kind: "choose" }>,
|
|
181
|
+
settings: Settings,
|
|
182
|
+
onward: Onward,
|
|
183
|
+
ctx: ExtensionContext,
|
|
184
|
+
): Notice {
|
|
185
|
+
switch (command.kind) {
|
|
186
|
+
case "status":
|
|
187
|
+
return statusNotice(settings, onward.progress());
|
|
188
|
+
case "help":
|
|
189
|
+
return info(helpText(command.topic));
|
|
190
|
+
case "error":
|
|
191
|
+
return commandErrorNotice(command.message, command.topic);
|
|
192
|
+
case "show":
|
|
193
|
+
return command.rules ? rulesNotice(settings) : showNotice(settings);
|
|
194
|
+
case "reload":
|
|
195
|
+
settings.reload();
|
|
196
|
+
return statusNotice(settings, onward.progress());
|
|
197
|
+
case "reset": {
|
|
198
|
+
onward.stop(ctx);
|
|
199
|
+
const saved = settings.reset();
|
|
200
|
+
const kept = saved.kind === "saved" && Array.isArray(saved.value.rules) && saved.value.rules.length > 0;
|
|
201
|
+
return changedNotice(
|
|
202
|
+
saved,
|
|
203
|
+
settings,
|
|
204
|
+
onward.progress(),
|
|
205
|
+
kept ? "已恢复默认,手写的 rules 保留" : "已恢复默认",
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
case "update":
|
|
209
|
+
return changedNotice(settings.update(command.change), settings, onward.progress());
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export function parseCommand(text: string): Command {
|
|
214
|
+
const tokens = tokenize(text);
|
|
215
|
+
if (typeof tokens === "string") return { kind: "error", message: tokens };
|
|
216
|
+
if (tokens.includes(HELP)) {
|
|
217
|
+
const [first] = tokens.filter((token) => token !== HELP);
|
|
218
|
+
if (first === undefined) return { kind: "help" };
|
|
219
|
+
const spec = actionNamed(first);
|
|
220
|
+
return spec ? { kind: "help", topic: spec.name } : unknownAction(first);
|
|
221
|
+
}
|
|
222
|
+
const [first, ...rest] = tokens;
|
|
223
|
+
if (first === undefined) return { kind: "choose" };
|
|
224
|
+
const action = first.toLowerCase();
|
|
225
|
+
switch (action) {
|
|
226
|
+
case "status":
|
|
227
|
+
case "reload":
|
|
228
|
+
case "reset":
|
|
229
|
+
return none(action, rest) ?? { kind: action };
|
|
230
|
+
case "on":
|
|
231
|
+
case "off":
|
|
232
|
+
return none(action, rest) ?? { kind: "update", change: { enabled: action === "on" } };
|
|
233
|
+
case "show":
|
|
234
|
+
if (!rest.length) return { kind: "show", rules: false };
|
|
235
|
+
if (rest.length === 1 && rest[0].toLowerCase() === "rules") return { kind: "show", rules: true };
|
|
236
|
+
return { kind: "error", message: "show 后面只能写 rules", topic: "show" };
|
|
237
|
+
case "set":
|
|
238
|
+
return parseSet(rest);
|
|
239
|
+
case "unset":
|
|
240
|
+
return parseUnset(rest);
|
|
241
|
+
default:
|
|
242
|
+
return unknownAction(first);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function unknownAction(word: string): Command {
|
|
247
|
+
return { kind: "error", message: `不认识的动作 ${word};可用 ${actionList()}` };
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** 不带参数的动作带了参数。 */
|
|
251
|
+
function none(action: string, rest: string[]): Command | undefined {
|
|
252
|
+
return rest.length ? { kind: "error", message: `${action} 不带参数`, topic: action } : undefined;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function parseSet(tokens: string[]): Command {
|
|
256
|
+
if (!tokens.length)
|
|
257
|
+
return { kind: "error", message: `set 要写 字段=值;可用 ${FIELD_NAMES.join("、")}`, topic: "set" };
|
|
258
|
+
const problems: string[] = [];
|
|
259
|
+
const values: Record<string, unknown> = {};
|
|
260
|
+
for (const token of tokens) {
|
|
261
|
+
const equals = token.indexOf("=");
|
|
262
|
+
if (equals <= 0) {
|
|
263
|
+
problems.push(`「${token}」写成 字段=值`);
|
|
264
|
+
continue;
|
|
265
|
+
}
|
|
266
|
+
const key = token.slice(0, equals);
|
|
267
|
+
const text = token.slice(equals + 1);
|
|
268
|
+
const known = paramField(key);
|
|
269
|
+
if (typeof known === "string") {
|
|
270
|
+
problems.push(known);
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
if (known.name in values) {
|
|
274
|
+
problems.push(`${known.name} 写了两次`);
|
|
275
|
+
continue;
|
|
276
|
+
}
|
|
277
|
+
const value = known.fromText(text);
|
|
278
|
+
const read = known.read(value);
|
|
279
|
+
if ("problem" in read) problems.push(`${known.name} ${read.problem}`);
|
|
280
|
+
else values[known.name] = value;
|
|
281
|
+
}
|
|
282
|
+
return problems.length ? rejected(problems, "set") : { kind: "update", change: { set: values } };
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function parseUnset(tokens: string[]): Command {
|
|
286
|
+
if (!tokens.length)
|
|
287
|
+
return { kind: "error", message: `unset 要写字段名;可用 ${FIELD_NAMES.join("、")}`, topic: "unset" };
|
|
288
|
+
const problems: string[] = [];
|
|
289
|
+
const unset: string[] = [];
|
|
290
|
+
for (const token of tokens) {
|
|
291
|
+
const known = paramField(token);
|
|
292
|
+
if (typeof known === "string") problems.push(known);
|
|
293
|
+
else if (unset.includes(known.name)) problems.push(`${known.name} 写了两次`);
|
|
294
|
+
else unset.push(known.name);
|
|
295
|
+
}
|
|
296
|
+
return problems.length ? rejected(problems, "unset") : { kind: "update", change: { unset } };
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** 参数字段;不是时说明为什么。 */
|
|
300
|
+
function paramField(key: string): Field | string {
|
|
301
|
+
const lower = key.toLowerCase();
|
|
302
|
+
if (lower === "rules") return "rules 要在设置文件里手写,改完 /onward reload";
|
|
303
|
+
if (lower === "enabled") return "开关用 /onward on 或 off";
|
|
304
|
+
return fieldNamed(key) ?? `不认识的字段 ${key};可用 ${FIELD_NAMES.join("、")}`;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
function rejected(problems: string[], topic: string): Command {
|
|
308
|
+
return { kind: "error", message: `${problems.join(";")}。未做任何修改`, topic };
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
function actionList(): string {
|
|
312
|
+
return ACTIONS.map((item) => item.name).join("、");
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function actionNamed(name: string): ActionSpec | undefined {
|
|
316
|
+
const lower = name.toLowerCase();
|
|
317
|
+
return ACTIONS.find((item) => item.name === lower);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
// ── 帮助 ──
|
|
321
|
+
|
|
322
|
+
/** `topic` 是动作名:这个动作的帮助;没有时是总帮助。 */
|
|
323
|
+
export function helpText(topic?: string): string {
|
|
324
|
+
const spec = topic === undefined ? undefined : actionNamed(topic);
|
|
325
|
+
return spec ? actionHelp(spec) : overallHelp();
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
function overallHelp(): string {
|
|
329
|
+
return [
|
|
330
|
+
"onward — 出错后按规则自动续跑",
|
|
331
|
+
`用法:/${COMMAND} [动作] [参数] [${HELP}]`,
|
|
332
|
+
...section("动作", table(ACTIONS.map((item) => [item.name, item.description]))),
|
|
333
|
+
"",
|
|
334
|
+
" 不写动作时弹出菜单,标题是现在的状态",
|
|
335
|
+
` 每个动作的用法:/${COMMAND} <动作> ${HELP}(只显示帮助,不执行)`,
|
|
336
|
+
` 字段和默认值:/${COMMAND} set ${HELP}`,
|
|
337
|
+
].join("\n");
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
function actionHelp(spec: ActionSpec): string {
|
|
341
|
+
return [
|
|
342
|
+
`${spec.name} — ${spec.description}`,
|
|
343
|
+
`用法:/${COMMAND} ${usage(spec)}`,
|
|
344
|
+
...section(
|
|
345
|
+
"说明",
|
|
346
|
+
spec.details.map((line) => ` ${line}`),
|
|
347
|
+
),
|
|
348
|
+
...(spec.fields ? section("字段", table(FIELDS.map((item) => [item.name, fieldHelp(item)]))) : []),
|
|
349
|
+
...section(
|
|
350
|
+
"示例",
|
|
351
|
+
(spec.examples ?? []).map((line) => ` ${line}`),
|
|
352
|
+
),
|
|
353
|
+
].join("\n");
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
function usage(spec: ActionSpec): string {
|
|
357
|
+
return spec.args ? `${spec.name} ${spec.args}` : spec.name;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/** 作用一行,写法和默认值一行;有固定取值的逐个列出,标出默认的那个。 */
|
|
361
|
+
function fieldHelp(item: Field): string {
|
|
362
|
+
const fallback = DEFAULT_POLICY[item.name];
|
|
363
|
+
const values = item.choices
|
|
364
|
+
? item.choices.map(
|
|
365
|
+
(choice) => `${choice.value}:${choice.description}${choice.value === fallback ? "(默认)" : ""}`,
|
|
366
|
+
)
|
|
367
|
+
: [`${item.syntax};默认:${item.show(fallback)}`];
|
|
368
|
+
return [item.description, ...values].join("\n");
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// ── 分词 ──
|
|
372
|
+
|
|
373
|
+
/** 按空白分开,双引号里的空白不分;引号本身去掉。 */
|
|
374
|
+
function tokenize(text: string): string[] | string {
|
|
375
|
+
const { words, quoted } = scan(text);
|
|
376
|
+
return quoted ? "引号没有配对" : words;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/** 执行和补全共用。`start` 是正在写的词在原文里的起点,以空白结尾时就是末尾;`quoted`:引号到末尾还没配对。 */
|
|
380
|
+
function scan(text: string): { words: string[]; start: number; quoted: boolean } {
|
|
381
|
+
const words: string[] = [];
|
|
382
|
+
let word = "";
|
|
383
|
+
let started = false;
|
|
384
|
+
let quoted = false;
|
|
385
|
+
let start = 0;
|
|
386
|
+
for (let i = 0; i < text.length; i++) {
|
|
387
|
+
const char = text[i];
|
|
388
|
+
if (char === '"') {
|
|
389
|
+
quoted = !quoted;
|
|
390
|
+
started = true;
|
|
391
|
+
} else if (!quoted && /\s/.test(char)) {
|
|
392
|
+
if (started) words.push(word);
|
|
393
|
+
word = "";
|
|
394
|
+
started = false;
|
|
395
|
+
start = i + 1;
|
|
396
|
+
} else {
|
|
397
|
+
word += char;
|
|
398
|
+
started = true;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
if (started) words.push(word);
|
|
402
|
+
return { words, start, quoted };
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
// ── 补全 ──
|
|
406
|
+
|
|
407
|
+
interface Candidate {
|
|
408
|
+
value: string;
|
|
409
|
+
description: string;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* 补全:动作、show 的 rules、set 和 unset 的字段名、action 的取值,以及每个动作后面的 --help。返回的 value
|
|
414
|
+
* 换掉「/onward 」后面的全部文字,正在写的词前面原样保留。
|
|
415
|
+
*
|
|
416
|
+
* 选中的词后面还能接着写时补一个空格,下一下 Tab 补下一层;`字段=` 停在 = 后面写值。写了 --help 就结束了,
|
|
417
|
+
* 什么都不再给。写完的词不给列表,回车直接执行;写过的字段和引号里的值不补。
|
|
418
|
+
*/
|
|
419
|
+
export function completeArguments(prefix: string): { value: string; label: string }[] | null {
|
|
420
|
+
const { words, start } = scan(prefix);
|
|
421
|
+
const current = prefix.slice(start);
|
|
422
|
+
if (current.includes('"')) return null;
|
|
423
|
+
const head = prefix.slice(0, start);
|
|
424
|
+
const done = current ? words.slice(0, -1) : words;
|
|
425
|
+
const all = candidates(done, current);
|
|
426
|
+
// 说明写进 label,不另给 description:pi 的参数补全把名字列固定留 32 格,说明会隔开一大截。
|
|
427
|
+
// 列宽按这一层的全部候选算,不按过滤后的:边打字边过滤时说明不左右跳。
|
|
428
|
+
const labels = columns(all.map((item): [string, string] => [item.value, item.description]));
|
|
429
|
+
const shown = all.flatMap((item, index) =>
|
|
430
|
+
item.value.toLowerCase().startsWith(current.toLowerCase()) ? [{ item, label: labels[index]! }] : [],
|
|
431
|
+
);
|
|
432
|
+
if (!shown.length || shown.some(({ item }) => item.value.toLowerCase() === current.toLowerCase())) return null;
|
|
433
|
+
const spaced = (item: Candidate) => !item.value.endsWith("=") && candidates([...done, item.value], "").length > 0;
|
|
434
|
+
return shown.map(({ item, label }) => ({
|
|
435
|
+
value: `${head}${item.value}${spaced(item) ? " " : ""}`,
|
|
436
|
+
label,
|
|
437
|
+
}));
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/** `words` 后面能写的词;--help 之后、写错了的地方都是空的。 */
|
|
441
|
+
function candidates(words: string[], current: string): Candidate[] {
|
|
442
|
+
if (words.includes(HELP)) return [];
|
|
443
|
+
const [first, ...rest] = words;
|
|
444
|
+
if (first === undefined)
|
|
445
|
+
return [
|
|
446
|
+
...ACTIONS.map((item) => ({ value: item.name, description: item.description })),
|
|
447
|
+
{ value: HELP, description: "显示帮助" },
|
|
448
|
+
];
|
|
449
|
+
const spec = actionNamed(first);
|
|
450
|
+
const args = spec && argumentCandidates(spec.name, rest, current);
|
|
451
|
+
return args ? [...args, { value: HELP, description: `显示 ${spec.name} 的帮助,不执行` }] : [];
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/** 动作后面的参数;`undefined`:这里已经写错了,执行时会被拒绝,--help 也不给。 */
|
|
455
|
+
function argumentCandidates(action: string, rest: string[], current: string): Candidate[] | undefined {
|
|
456
|
+
switch (action) {
|
|
457
|
+
case "show":
|
|
458
|
+
if (!rest.length) return [{ value: "rules", description: "只看规则" }];
|
|
459
|
+
return rest.length === 1 && rest[0].toLowerCase() === "rules" ? [] : undefined;
|
|
460
|
+
case "set":
|
|
461
|
+
case "unset":
|
|
462
|
+
return fieldCandidates(action, rest, current);
|
|
463
|
+
default:
|
|
464
|
+
return rest.length ? undefined : [];
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
function fieldCandidates(action: string, rest: string[], current: string): Candidate[] | undefined {
|
|
469
|
+
// 写过的字段不再给:再写一次,执行时会被拒绝。
|
|
470
|
+
const written = new Set(rest.map((word) => fieldNamed(word.split("=")[0])?.name));
|
|
471
|
+
const unwritten = FIELDS.filter((item) => !written.has(item.name));
|
|
472
|
+
if (action === "unset") return unwritten.map((item) => ({ value: item.name, description: item.description }));
|
|
473
|
+
const equals = current.indexOf("=");
|
|
474
|
+
if (equals < 0)
|
|
475
|
+
return unwritten.map((item) => ({ value: `${item.name}=`, description: `${item.syntax}:${item.description}` }));
|
|
476
|
+
const known = fieldNamed(current.slice(0, equals));
|
|
477
|
+
if (!known || written.has(known.name)) return undefined;
|
|
478
|
+
return (known.choices ?? []).map((choice) => ({
|
|
479
|
+
value: `${known.name}=${choice.value}`,
|
|
480
|
+
description: choice.description,
|
|
481
|
+
}));
|
|
482
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* pi-onward:一轮以错误收尾(401、429、502、Connection error. 等)时,按用户写的规则续跑。没有内置规则。
|
|
3
|
+
*
|
|
4
|
+
* - `policy/`:纯逻辑,不碰 pi 的运行时和文件。什么算失败、命中哪条规则、按什么参数、等多久、什么时候停。
|
|
5
|
+
* - `extension/`:接 pi。`onward` 处理事件并续跑,`command` 是 `/onward`,`autocomplete` 让它的参数里按 Tab 补参数,
|
|
6
|
+
* `settings` 读写设置文件,`text` 是给人看的文字,`render` 画会话里的续跑消息,`install` 把它们装起来。
|
|
7
|
+
*
|
|
8
|
+
* 这里是 pi 加载的入口,只决定设置文件的位置。目录用 pi 自己的 getAgentDir(),`PI_CODING_AGENT_DIR` 的写法
|
|
9
|
+
* (包括 `~`)和 pi 一致。
|
|
10
|
+
*/
|
|
11
|
+
import { join } from "node:path";
|
|
12
|
+
import { type ExtensionAPI, getAgentDir } from "@earendil-works/pi-coding-agent";
|
|
13
|
+
import { install } from "./install.ts";
|
|
14
|
+
|
|
15
|
+
/** `PI_ONWARD_CONFIG` 可改路径。 */
|
|
16
|
+
export function configPath(): string {
|
|
17
|
+
return process.env.PI_ONWARD_CONFIG || join(getAgentDir(), "onward.json");
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export default function piOnward(pi: ExtensionAPI): void {
|
|
21
|
+
install(pi, configPath());
|
|
22
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/** 把各部分装到 pi 上。设置文件放在哪由调用方给:入口是 index.ts,测试直接给临时路径。 */
|
|
2
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
import { argumentTabCompletion } from "./autocomplete.ts";
|
|
4
|
+
import { registerCommand } from "./command.ts";
|
|
5
|
+
import { CUSTOM_TYPE, Onward } from "./onward.ts";
|
|
6
|
+
import { renderContinue } from "./render.ts";
|
|
7
|
+
import { Settings } from "./settings.ts";
|
|
8
|
+
import { say, startupNotices } from "./text.ts";
|
|
9
|
+
|
|
10
|
+
export function install(pi: ExtensionAPI, configPath: string): void {
|
|
11
|
+
const settings = new Settings(configPath);
|
|
12
|
+
const onward = new Onward(settings);
|
|
13
|
+
|
|
14
|
+
pi.on("session_start", (_event, ctx) => {
|
|
15
|
+
// reload 和切换会话时 pi 会清掉扩展装的补全,所以每次都装。
|
|
16
|
+
ctx.ui.addAutocompleteProvider(argumentTabCompletion);
|
|
17
|
+
for (const notice of startupNotices(settings)) say(ctx, notice);
|
|
18
|
+
});
|
|
19
|
+
pi.on("agent_start", (_event, ctx) => onward.agentStart(ctx));
|
|
20
|
+
pi.on("message_end", (event, ctx) => onward.messageEnd(event, ctx));
|
|
21
|
+
pi.on("agent_before_settle", (event, ctx) => onward.beforeSettle(event, ctx));
|
|
22
|
+
pi.on("agent_settled", (_event, ctx) => onward.settled(ctx));
|
|
23
|
+
pi.on("session_shutdown", (_event, ctx) => onward.shutdown(ctx));
|
|
24
|
+
|
|
25
|
+
registerCommand(pi, settings, onward);
|
|
26
|
+
pi.registerMessageRenderer(CUSTOM_TYPE, renderContinue);
|
|
27
|
+
}
|