@cobia/pi-onward 0.1.2 → 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.
@@ -13,6 +13,13 @@ export type Delay = { kind: "list"; steps: readonly number[] } | { kind: "ramp";
13
13
  export const DELAY_SYNTAX = "5s,10s,30s 或 3s..15s";
14
14
  const MALFORMED = `写成 ${DELAY_SYNTAX},每个值带单位 ms、s 或 m`;
15
15
 
16
+ /**
17
+ * JSON Schema 里的写法:单个值、逗号分隔的列表,或者 `起步..封顶`,每个值带单位。只管写法:封顶不小于起步、最多
18
+ * 几个值、每个值不超过一小时,由 parseDelay 检查。
19
+ */
20
+ const DURATION = "\\d+(\\.\\d+)?(ms|s|m)";
21
+ export const DELAY_PATTERN = `^\\s*(${DURATION}\\.\\.${DURATION}|${DURATION}(\\s*,\\s*${DURATION})*)\\s*$`;
22
+
16
23
  /** 合法时返回间隔,否则返回原因。 */
17
24
  export function parseDelay(text: string): Delay | string {
18
25
  const value = text.trim();
@@ -69,10 +76,10 @@ export function formatDelay(ms: number): string {
69
76
 
70
77
  const UNIT_MS = { ms: 1, s: 1000, m: 60_000 } as const;
71
78
 
72
- /** `500ms` / `1.5s` / `2m` → 毫秒;必须带单位,超过一小时的不收。 */
79
+ /** `500ms` / `1.5s` / `2m` → 毫秒;必须带单位,单位小写,超过一小时的不收。 */
73
80
  function parseDuration(text: string): number | undefined {
74
- const match = /^(\d+(?:\.\d+)?)(ms|s|m)$/i.exec(text);
81
+ const match = /^(\d+(?:\.\d+)?)(ms|s|m)$/.exec(text);
75
82
  if (!match) return undefined;
76
- const ms = Math.round(Number(match[1]) * UNIT_MS[match[2]!.toLowerCase() as keyof typeof UNIT_MS]);
83
+ const ms = Math.round(Number(match[1]) * UNIT_MS[match[2] as keyof typeof UNIT_MS]);
77
84
  return ms <= MAX_DELAY_MS ? ms : undefined;
78
85
  }
@@ -1,8 +1,8 @@
1
1
  /**
2
- * 续跑的参数:一张字段表,设置文件、规则、命令、帮助和补全都用它。设置文件顶层写的就是每条规则的默认值,
3
- * 规则里同名的字段覆盖它。
2
+ * 续跑的参数:一张字段表,设置文件、规则、命令、帮助、补全和 JSON Schema 都用它。设置文件顶层写的就是每条规则的
3
+ * 默认值,规则里同名的字段覆盖它。
4
4
  */
5
- import { DELAY_SYNTAX, type Delay, delayText, parseDelay } from "./delay.ts";
5
+ import { DELAY_PATTERN, DELAY_SYNTAX, type Delay, delayText, parseDelay } from "./delay.ts";
6
6
 
7
7
  /** message 发一条看得见的消息再接着跑;resend 从上下文里省略失败的那条,重发同一个请求;stop 不续跑,只提醒。 */
8
8
  export type Action = "message" | "resend" | "stop";
@@ -33,8 +33,14 @@ export interface Field<K extends keyof Policy = keyof Policy> {
33
33
  fromText(text: string): unknown;
34
34
  /** 给人看的值。 */
35
35
  show(value: Policy[K]): string;
36
+ /** 设置文件里这个字段的 JSON Schema:和 `read` 收下的一致,只管写法,含义上的检查留给 `read`。 */
37
+ schema: Readonly<Record<string, unknown>>;
36
38
  }
37
39
 
40
+ /** 两个次数上限的范围。一条规则最多续跑 maxRetries 次,同错上限比它大时用不上,所以范围一样。 */
41
+ const RETRIES = { min: 1, max: 50 } as const;
42
+ const PROMPT_MAX = 500;
43
+
38
44
  const ACTIONS: readonly { value: Action; description: string }[] = [
39
45
  { value: "message", description: "发一条看得见的消息再接着跑" },
40
46
  { value: "resend", description: "静默重发同一个请求" },
@@ -55,33 +61,37 @@ export const FIELDS: readonly Field[] = [
55
61
  : { problem: "只能是 message、resend 或 stop" },
56
62
  fromText: (text) => text,
57
63
  show: String,
64
+ schema: { enum: ACTIONS.map((item) => item.value) },
58
65
  }),
59
66
  field({
60
67
  name: "prompt",
61
68
  syntax: "文字",
62
69
  description: "message 发的消息,可含 {attempt} {max} {error} {rule}",
63
70
  read: (v) =>
64
- typeof v === "string" && v.trim() && v.trim().length <= 500
71
+ typeof v === "string" && v.trim() && v.trim().length <= PROMPT_MAX
65
72
  ? { value: v.trim() }
66
- : { problem: "需要 1–500 字的文字" },
73
+ : { problem: `需要 1–${PROMPT_MAX} 字的文字` },
67
74
  fromText: (text) => text,
68
75
  show: (prompt) => `「${prompt}」`,
76
+ schema: { type: "string", pattern: "\\S", maxLength: PROMPT_MAX },
69
77
  }),
70
78
  field({
71
79
  name: "maxRetries",
72
- syntax: "1–50",
80
+ syntax: `${RETRIES.min}–${RETRIES.max}`,
73
81
  description: "连续失败时最多续跑几次",
74
- read: (v) => integer(v, 1, 50),
82
+ read: (v) => integer(v, RETRIES.min, RETRIES.max),
75
83
  fromText: number,
76
84
  show: String,
85
+ schema: { type: "integer", minimum: RETRIES.min, maximum: RETRIES.max },
77
86
  }),
78
87
  field({
79
88
  name: "maxSameError",
80
- syntax: "1–100",
89
+ syntax: `${RETRIES.min}–${RETRIES.max}`,
81
90
  description: "同一个错误最多续跑几次,再出现就彻底停",
82
- read: (v) => integer(v, 1, 100),
91
+ read: (v) => integer(v, RETRIES.min, RETRIES.max),
83
92
  fromText: number,
84
93
  show: String,
94
+ schema: { type: "integer", minimum: RETRIES.min, maximum: RETRIES.max },
85
95
  }),
86
96
  field({
87
97
  name: "delay",
@@ -93,13 +103,15 @@ export const FIELDS: readonly Field[] = [
93
103
  },
94
104
  fromText: (text) => text,
95
105
  show: delayText,
106
+ schema: { type: "string", pattern: DELAY_PATTERN },
96
107
  }),
97
108
  ];
98
109
 
99
110
  export const FIELD_NAMES: readonly string[] = FIELDS.map((item) => item.name);
100
111
 
112
+ /** 字段名区分大小写,和设置文件一样。 */
101
113
  export function fieldNamed(name: string): Field | undefined {
102
- return FIELDS.find((item) => item.name.toLowerCase() === name.toLowerCase());
114
+ return FIELDS.find((item) => item.name === name);
103
115
  }
104
116
 
105
117
  export const DEFAULT_POLICY: Readonly<Policy> = Object.freeze<Policy>({
@@ -25,7 +25,7 @@ export interface Rule {
25
25
  patch: PolicyPatch;
26
26
  }
27
27
 
28
- const RULE_KEYS = new Set(["name", "enabled", "when", ...FIELD_NAMES]);
28
+ export const RULE_KEYS: ReadonlySet<string> = new Set(["name", "enabled", "when", ...FIELD_NAMES]);
29
29
 
30
30
  export function findRule(rules: readonly Rule[], failure: Failure): Rule | undefined {
31
31
  return rules.find((rule) => rule.enabled && matches(rule.when, failure));
@@ -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";