@cobia/pi-onward 0.1.1 → 0.2.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.
@@ -0,0 +1,94 @@
1
+ /**
2
+ * 设置文件 `onward.json` 的 JSON Schema,给编辑器补全和检查用。参数来自 policy.ts 的同一张字段表,和读设置时
3
+ * 收下的写法一致。生成的文件是仓库根目录的 onward.schema.json:`npm run gen:schema` 重新生成,测试检查它没有过期。
4
+ *
5
+ * Schema 只管写法,而且不拒绝读设置时收下的:正则对不对、重名的规则、封顶小于起步这类问题,仍由 config.ts
6
+ * 记成问题。`error` 不写 `format: "regex"`:校验器按 `u` 模式检查,会拒绝读设置时收下的写法,如 `[\w-.]`。
7
+ */
8
+ import { delayText } from "./delay.ts";
9
+ import { DEFAULT_POLICY, FIELDS, type Field } from "./policy.ts";
10
+
11
+ /** 发布的位置:同步到 GitHub 的仓库根目录。设置文件的 `$schema` 写它。 */
12
+ export const SCHEMA_URL = "https://raw.githubusercontent.com/cobia68/pi-onward/main/onward.schema.json";
13
+
14
+ /** 一个参数:作用和取值说明。顶层的带内置默认值 `fallback`;规则里的不带,说它覆盖顶层的。 */
15
+ function param(item: Field, fallback?: unknown): Record<string, unknown> {
16
+ const choices = item.choices?.map((choice) => `${choice.value}:${choice.description}`).join(";");
17
+ // 写法是「文字」时不重复:类型已经是 string。
18
+ const syntax = item.syntax === "文字" ? "" : `(${item.syntax})`;
19
+ const what = choices ? `${item.description}。${choices}` : `${item.description}${syntax}`;
20
+ return {
21
+ description: fallback === undefined ? `${what}。覆盖顶层的同名字段` : what,
22
+ ...item.schema,
23
+ ...(fallback !== undefined && { default: fallback }),
24
+ };
25
+ }
26
+
27
+ /** 内置默认值写成设置文件里的样子:间隔是 `3s..15s` 这样的文字。 */
28
+ function fileValue(name: string): unknown {
29
+ const value = DEFAULT_POLICY[name as keyof typeof DEFAULT_POLICY];
30
+ return name === "delay" ? delayText(DEFAULT_POLICY.delay) : value;
31
+ }
32
+
33
+ export function configSchema(): Record<string, unknown> {
34
+ return {
35
+ $schema: "https://json-schema.org/draft/2020-12/schema",
36
+ $id: SCHEMA_URL,
37
+ title: "pi-onward 设置",
38
+ description:
39
+ "~/.pi/agent/onward.json(全局)或 .pi/onward.json(项目级)。顶层的参数是每条规则的默认值,规则里同名的字段覆盖它",
40
+ type: "object",
41
+ additionalProperties: false,
42
+ properties: {
43
+ $schema: { description: "这份 JSON Schema 的地址,给编辑器用", type: "string" },
44
+ enabled: { description: "插件开关", type: "boolean", default: true },
45
+ ...Object.fromEntries(FIELDS.map((item) => [item.name, param(item, fileValue(item.name))])),
46
+ rules: {
47
+ description: "按顺序第一条命中的生效;没命中的错误和空回复不处理",
48
+ type: "array",
49
+ items: { $ref: "#/$defs/rule" },
50
+ },
51
+ },
52
+ $defs: {
53
+ rule: {
54
+ type: "object",
55
+ additionalProperties: false,
56
+ properties: {
57
+ name: {
58
+ description: "规则名,出现在通知里,不能重名;不写时按位置叫 rule-1、rule-2",
59
+ type: "string",
60
+ pattern: "\\S",
61
+ },
62
+ enabled: { description: "false 时保留但跳过", type: "boolean", default: true },
63
+ when: { $ref: "#/$defs/when" },
64
+ ...Object.fromEntries(FIELDS.map((item) => [item.name, param(item)])),
65
+ },
66
+ },
67
+ when: {
68
+ description: "命中条件,几项都要满足;不写 when 的规则管所有错误,不管空回复",
69
+ type: "object",
70
+ additionalProperties: false,
71
+ properties: {
72
+ empty: { description: "只管空回复,不管出错;不能和 error 一起写", const: true },
73
+ error: {
74
+ description: "正则,大小写不敏感,对原始错误文案匹配",
75
+ type: "string",
76
+ minLength: 1,
77
+ },
78
+ provider: {
79
+ description: "通配,只有 * 有特殊含义,大小写不敏感,整串匹配",
80
+ type: "string",
81
+ pattern: "\\S",
82
+ },
83
+ model: {
84
+ description: "通配,只有 * 有特殊含义,大小写不敏感,整串匹配",
85
+ type: "string",
86
+ pattern: "\\S",
87
+ },
88
+ },
89
+ // 写了 empty 就不能写 error:编辑器把问题标在 error 上。
90
+ dependentSchemas: { empty: { properties: { error: false } } },
91
+ },
92
+ },
93
+ };
94
+ }
@@ -1,5 +1,5 @@
1
1
  /**
2
- * 上次正常返回之后的连续失败计数。每条规则各自计次数,预算互不挤占;同一个错误的连续次数是全局的:
2
+ * 上次正常返回之后的连续失败计数。每条规则各自计次数,预算互不挤占;同一个错误的连续次数不分规则:
3
3
  * 不管落到哪条规则,同一个错误连着出现都说明没恢复。扩展在正常返回或用户开新一轮时 reset。
4
4
  */
5
5
  import { retryDelay } from "./delay.ts";