@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/src/policy/policy.ts
CHANGED
|
@@ -25,6 +25,8 @@ export interface Field<K extends keyof Policy = keyof Policy> {
|
|
|
25
25
|
/** 帮助里的写法。 */
|
|
26
26
|
syntax: string;
|
|
27
27
|
description: string;
|
|
28
|
+
/** 只在这些动作下使用;不写表示都使用。规则显式写了不适用的参数时只提醒。 */
|
|
29
|
+
appliesTo?: readonly Action[];
|
|
28
30
|
/** 补全的候选值。 */
|
|
29
31
|
choices?: readonly { value: string; description: string }[];
|
|
30
32
|
/** 设置文件里的值;合法时返回 value,否则返回原因。 */
|
|
@@ -65,6 +67,7 @@ export const FIELDS: readonly Field[] = [
|
|
|
65
67
|
}),
|
|
66
68
|
field({
|
|
67
69
|
name: "prompt",
|
|
70
|
+
appliesTo: ["message"],
|
|
68
71
|
syntax: "文字",
|
|
69
72
|
description: "message 发的消息,可含 {attempt} {max} {error} {rule}",
|
|
70
73
|
read: (v) =>
|
|
@@ -77,6 +80,7 @@ export const FIELDS: readonly Field[] = [
|
|
|
77
80
|
}),
|
|
78
81
|
field({
|
|
79
82
|
name: "maxRetries",
|
|
83
|
+
appliesTo: ["message", "resend"],
|
|
80
84
|
syntax: `${RETRIES.min}–${RETRIES.max}`,
|
|
81
85
|
description: "连续失败时最多续跑几次",
|
|
82
86
|
read: (v) => integer(v, RETRIES.min, RETRIES.max),
|
|
@@ -86,6 +90,7 @@ export const FIELDS: readonly Field[] = [
|
|
|
86
90
|
}),
|
|
87
91
|
field({
|
|
88
92
|
name: "maxSameError",
|
|
93
|
+
appliesTo: ["message", "resend"],
|
|
89
94
|
syntax: `${RETRIES.min}–${RETRIES.max}`,
|
|
90
95
|
description: "同一个错误最多续跑几次,再出现就彻底停",
|
|
91
96
|
read: (v) => integer(v, RETRIES.min, RETRIES.max),
|
|
@@ -95,6 +100,7 @@ export const FIELDS: readonly Field[] = [
|
|
|
95
100
|
}),
|
|
96
101
|
field({
|
|
97
102
|
name: "delay",
|
|
103
|
+
appliesTo: ["message", "resend"],
|
|
98
104
|
syntax: DELAY_SYNTAX,
|
|
99
105
|
description: "列表按顺序等,最后一个值重复;起步..封顶按次数增长",
|
|
100
106
|
read: (v) => {
|
package/src/policy/rules.ts
CHANGED
|
@@ -1,17 +1,16 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* 规则 = 条件(when)+ 参数,按顺序第一条命中的生效。参数和设置文件顶层同名,只写和顶层不同的。
|
|
3
|
-
*
|
|
4
|
-
*
|
|
3
|
+
* when.failure 统一用正则匹配失败文本:普通错误用原文,空回复用空串;不写就不限制失败类型。
|
|
4
|
+
* 没有内置规则:没写或没命中,都不处理。无条件兜底必须显式写 action;读不懂的内容让整份设置不生效(见 config.ts)。
|
|
5
|
+
* 遮挡和不适用的显式参数单独提醒,不影响设置生效,也不改变规则顺序。
|
|
5
6
|
*/
|
|
6
7
|
import { errorText, isRecord } from "../util.ts";
|
|
7
|
-
import type
|
|
8
|
-
import { FIELD_NAMES, type PolicyPatch, readParams } from "./policy.ts";
|
|
8
|
+
import { type Failure, failureText } from "./failure.ts";
|
|
9
|
+
import { FIELD_NAMES, FIELDS, type PolicyPatch, readParams, resolvePolicy } from "./policy.ts";
|
|
9
10
|
|
|
10
11
|
export interface RuleWhen {
|
|
11
|
-
/**
|
|
12
|
-
|
|
13
|
-
/** 大小写不敏感,对原始错误文案匹配;读设置时编译一次。 */
|
|
14
|
-
error?: RegExp;
|
|
12
|
+
/** 大小写不敏感,对统一的失败文本匹配;读设置时编译一次。 */
|
|
13
|
+
failure?: RegExp;
|
|
15
14
|
/** 通配,只有 `*` 有特殊含义。 */
|
|
16
15
|
provider?: string;
|
|
17
16
|
model?: string;
|
|
@@ -32,8 +31,7 @@ export function findRule(rules: readonly Rule[], failure: Failure): Rule | undef
|
|
|
32
31
|
}
|
|
33
32
|
|
|
34
33
|
function matches(when: RuleWhen, failure: Failure): boolean {
|
|
35
|
-
if (
|
|
36
|
-
if (when.error !== undefined && !when.error.test(failure.errorMessage ?? "")) return false;
|
|
34
|
+
if (when.failure !== undefined && !when.failure.test(failureText(failure))) return false;
|
|
37
35
|
if (when.provider !== undefined && !glob(when.provider, failure.provider)) return false;
|
|
38
36
|
if (when.model !== undefined && !glob(when.model, failure.model)) return false;
|
|
39
37
|
return true;
|
|
@@ -48,6 +46,29 @@ export function glob(pattern: string, value: string): boolean {
|
|
|
48
46
|
return new RegExp(`^${source}$`, "i").test(value);
|
|
49
47
|
}
|
|
50
48
|
|
|
49
|
+
/** 省略或空的 when 是无条件失败兜底;不猜测正则或通配条件的覆盖关系。 */
|
|
50
|
+
function isFallbackWhen(when: unknown): boolean {
|
|
51
|
+
return when === undefined || (isRecord(when) && Object.keys(when).length === 0);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** 按合并后的参数诊断启用的规则;提醒不进入 problems,调用方不因此停用设置。 */
|
|
55
|
+
export function ruleWarnings(rules: readonly Rule[], defaults: PolicyPatch): string[] {
|
|
56
|
+
const warnings: string[] = [];
|
|
57
|
+
let fallback: string | undefined;
|
|
58
|
+
for (const [index, rule] of rules.entries()) {
|
|
59
|
+
if (!rule.enabled) continue;
|
|
60
|
+
const label = `rules[${index}](${rule.name})`;
|
|
61
|
+
if (fallback !== undefined) warnings.push(`${label}被前面的兜底规则「${fallback}」挡住,不会命中`);
|
|
62
|
+
if (fallback === undefined && isFallbackWhen(rule.when)) fallback = rule.name;
|
|
63
|
+
const { action } = resolvePolicy(defaults, rule.patch);
|
|
64
|
+
const unused = FIELDS.filter(
|
|
65
|
+
(item) => Object.hasOwn(rule.patch, item.name) && item.appliesTo && !item.appliesTo.includes(action),
|
|
66
|
+
).map((item) => item.name);
|
|
67
|
+
if (unused.length) warnings.push(`${label}的 ${unused.join("、")} 在 action=${action} 时不使用`);
|
|
68
|
+
}
|
|
69
|
+
return warnings;
|
|
70
|
+
}
|
|
71
|
+
|
|
51
72
|
/** 校验规则列表:读得懂的规则返回,读不懂的地方逐条记进 problems。 */
|
|
52
73
|
export function parseRules(raw: unknown, problems: string[]): Rule[] {
|
|
53
74
|
if (raw === undefined) return [];
|
|
@@ -79,6 +100,8 @@ function parseRule(raw: unknown, index: number, problems: string[]): Rule | unde
|
|
|
79
100
|
problems.push(`${label}enabled 需要 true 或 false`);
|
|
80
101
|
const when = parseWhen(raw.when, `${label}when`, problems);
|
|
81
102
|
const patch = readParams(raw, label, problems);
|
|
103
|
+
if (isFallbackWhen(raw.when) && raw.action === undefined)
|
|
104
|
+
problems.push(`${label}action 不能省略:兜底规则需要明确写 message、resend 或 stop`);
|
|
82
105
|
for (const key of Object.keys(raw)) if (!RULE_KEYS.has(key)) problems.push(`${label}${key} 不认识`);
|
|
83
106
|
// name 是 undefined 时已经记过问题,后一个条件只为收窄类型。
|
|
84
107
|
if (problems.length > before || name === undefined) return undefined;
|
|
@@ -101,19 +124,15 @@ function parseWhen(raw: unknown, where: string, problems: string[]): RuleWhen {
|
|
|
101
124
|
}
|
|
102
125
|
for (const [key, value] of Object.entries(raw)) {
|
|
103
126
|
switch (key) {
|
|
104
|
-
case "
|
|
105
|
-
if (value === true) when.empty = true;
|
|
106
|
-
else problems.push(`${where}.empty 只能是 true;不写就是管出错`);
|
|
107
|
-
break;
|
|
108
|
-
case "error":
|
|
127
|
+
case "failure":
|
|
109
128
|
if (typeof value !== "string" || !value) {
|
|
110
|
-
problems.push(`${where}.
|
|
129
|
+
problems.push(`${where}.failure 需要非空正则字符串;空回复用 ^$,所有失败用 .*`);
|
|
111
130
|
break;
|
|
112
131
|
}
|
|
113
132
|
try {
|
|
114
|
-
when.
|
|
133
|
+
when.failure = new RegExp(value, "i");
|
|
115
134
|
} catch (error: unknown) {
|
|
116
|
-
problems.push(`${where}.
|
|
135
|
+
problems.push(`${where}.failure 不是合法正则(${errorText(error)})`);
|
|
117
136
|
}
|
|
118
137
|
break;
|
|
119
138
|
case "provider":
|
|
@@ -125,7 +144,5 @@ function parseWhen(raw: unknown, where: string, problems: string[]): RuleWhen {
|
|
|
125
144
|
problems.push(`${where}.${key} 不认识`);
|
|
126
145
|
}
|
|
127
146
|
}
|
|
128
|
-
if (raw.empty === true && raw.error !== undefined)
|
|
129
|
-
problems.push(`${where} 里 empty 和 error 不能一起写:空回复没有错误文案`);
|
|
130
147
|
return when;
|
|
131
148
|
}
|
package/src/policy/schema.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* 收下的写法一致。生成的文件是仓库根目录的 onward.schema.json:`npm run gen:schema` 重新生成,测试检查它没有过期。
|
|
4
4
|
*
|
|
5
5
|
* Schema 只管写法,而且不拒绝读设置时收下的:正则对不对、重名的规则、封顶小于起步这类问题,仍由 config.ts
|
|
6
|
-
* 记成问题。`
|
|
6
|
+
* 记成问题。`failure` 不写 `format: "regex"`:校验器按 `u` 模式检查,会拒绝读设置时收下的写法,如 `[\w-.]`。
|
|
7
7
|
*/
|
|
8
8
|
import { delayText } from "./delay.ts";
|
|
9
9
|
import { DEFAULT_POLICY, FIELDS, type Field } from "./policy.ts";
|
|
@@ -16,7 +16,8 @@ function param(item: Field, fallback?: unknown): Record<string, unknown> {
|
|
|
16
16
|
const choices = item.choices?.map((choice) => `${choice.value}:${choice.description}`).join(";");
|
|
17
17
|
// 写法是「文字」时不重复:类型已经是 string。
|
|
18
18
|
const syntax = item.syntax === "文字" ? "" : `(${item.syntax})`;
|
|
19
|
-
const
|
|
19
|
+
const usage = item.appliesTo ? `。仅在 action 为 ${item.appliesTo.join(" 或 ")} 时使用` : "";
|
|
20
|
+
const what = (choices ? `${item.description}。${choices}` : `${item.description}${syntax}`) + usage;
|
|
20
21
|
return {
|
|
21
22
|
description: fallback === undefined ? `${what}。覆盖顶层的同名字段` : what,
|
|
22
23
|
...item.schema,
|
|
@@ -43,6 +44,11 @@ export function configSchema(): Record<string, unknown> {
|
|
|
43
44
|
$schema: { description: "这份 JSON Schema 的地址,给编辑器用", type: "string" },
|
|
44
45
|
enabled: { description: "插件开关", type: "boolean", default: true },
|
|
45
46
|
...Object.fromEntries(FIELDS.map((item) => [item.name, param(item, fileValue(item.name))])),
|
|
47
|
+
quietStartup: {
|
|
48
|
+
description:
|
|
49
|
+
"启动时的提示(用上了项目级设置、没有可用的规则):true 不显示,false 照常显示,不写时跟着 pi 的 quietStartup;设置有问题时照样警告。只能写在全局设置",
|
|
50
|
+
type: "boolean",
|
|
51
|
+
},
|
|
46
52
|
rules: {
|
|
47
53
|
description: "按顺序第一条命中的生效;没命中的错误和空回复不处理",
|
|
48
54
|
type: "array",
|
|
@@ -51,8 +57,10 @@ export function configSchema(): Record<string, unknown> {
|
|
|
51
57
|
},
|
|
52
58
|
$defs: {
|
|
53
59
|
rule: {
|
|
60
|
+
description: "规则需要匹配条件或显式动作;不写 when 或写成 {} 的失败兜底必须显式写 action",
|
|
54
61
|
type: "object",
|
|
55
62
|
additionalProperties: false,
|
|
63
|
+
anyOf: [{ required: ["action"] }, { required: ["when"], properties: { when: { minProperties: 1 } } }],
|
|
56
64
|
properties: {
|
|
57
65
|
name: {
|
|
58
66
|
description: "规则名,出现在通知里,不能重名;不写时按位置叫 rule-1、rule-2",
|
|
@@ -65,13 +73,14 @@ export function configSchema(): Record<string, unknown> {
|
|
|
65
73
|
},
|
|
66
74
|
},
|
|
67
75
|
when: {
|
|
68
|
-
description:
|
|
76
|
+
description:
|
|
77
|
+
"命中条件,几项都要满足;不写 failure 就不限制失败类型;不写 when 或写成 {} 时必须显式写 action",
|
|
69
78
|
type: "object",
|
|
70
79
|
additionalProperties: false,
|
|
71
80
|
properties: {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
81
|
+
failure: {
|
|
82
|
+
description:
|
|
83
|
+
"非空正则,大小写不敏感;普通错误匹配原始文案,缺少文案时匹配「未知错误」,空回复匹配空串。^$ 匹配空回复,.+ 匹配普通错误,.* 匹配所有失败",
|
|
75
84
|
type: "string",
|
|
76
85
|
minLength: 1,
|
|
77
86
|
},
|
|
@@ -86,8 +95,6 @@ export function configSchema(): Record<string, unknown> {
|
|
|
86
95
|
pattern: "\\S",
|
|
87
96
|
},
|
|
88
97
|
},
|
|
89
|
-
// 写了 empty 就不能写 error:编辑器把问题标在 error 上。
|
|
90
|
-
dependentSchemas: { empty: { properties: { error: false } } },
|
|
91
98
|
},
|
|
92
99
|
},
|
|
93
100
|
};
|