@cobia/pi-onward 0.1.1 → 0.1.2

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 CHANGED
@@ -2,13 +2,13 @@
2
2
 
3
3
  # ⏩ pi-onward
4
4
 
5
- [pi](https://pi.dev) 的一轮以错误结束时,按规则自动续跑:发一条「继续」,或者静默重发同一个请求
5
+ [pi](https://pi.dev) 的一轮以错误结束、或者回了个空回复时,按规则自动续跑:发一条「继续」,或者静默重发同一个请求
6
6
 
7
7
  [📦 安装](#-安装) · [⚙️ 工作方式](#️-工作方式) · [📝 设置文件](#-设置文件) · [📋 常见规则](#-常见规则) · [🧰 命令速查](#-命令速查) · [❓ 常见问题](#-常见问题)
8
8
 
9
9
  </div>
10
10
 
11
- 管的是「一轮已经失败收尾之后怎么办」,例如中转间歇吐出的 `401 Invalid API key`、pi 自带重试用完后仍然失败的 `429`、`502`、`Connection error.`。不改 pi 自带的重试。
11
+ 管的是「一轮已经失败收尾之后怎么办」,例如中转间歇吐出的 `401 Invalid API key`、pi 自带重试用完后仍然失败的 `429`、`502`、`Connection error.`,以及中转偶尔返回的空回复:正常结束,但一个字都没生成。不改 pi 自带的重试。
12
12
 
13
13
  没有内置规则:哪些错误续跑、怎么续跑,全由你在设置文件里写。[常见规则](#-常见规则)给了一份可以直接用的。
14
14
 
@@ -29,7 +29,7 @@ pi install npm:@cobia/pi-onward
29
29
 
30
30
  ## ⚙️ 工作方式
31
31
 
32
- 一轮收尾时,最后一条助手消息是 `stopReason: "error"`、并且命中了一条规则,才处理;按 Esc 结束的(`aborted`)、正常结束的、没命中规则的都不碰。下面的动作、上限、间隔都是参数:设置文件顶层写的是默认值,规则里同名的字段覆盖它。
32
+ 一轮收尾时,最后一条助手消息是 `stopReason: "error"`、并且命中了一条规则,才处理;空回复(`stopReason: "stop"`,但没有文字也没有工具调用)要有 `"when": { "empty": true }` 的规则才处理。按 Esc 结束的(`aborted`)、正常结束且有内容的、没命中规则的都不碰。下面的动作、上限、间隔都是参数:设置文件顶层写的是默认值,规则里同名的字段覆盖它。
33
33
 
34
34
  续跑都在 pi 的收尾边界 `agent_before_settle` 上等一会儿,再用 `continue: true` 让这一轮接着跑。等待和续跑都在这一轮之内,所以交互、`pi -p`、RPC、[subagent 子会话](#-常见问题)里行为一样。
35
35
 
@@ -39,7 +39,7 @@ pi install npm:@cobia/pi-onward
39
39
  | `resend` | 用 `context_edit` 把失败的那条从模型上下文里省略,重发同一个请求 | 不写消息;原始记录里还留着失败的那次和一条不可见的 `context_edit` |
40
40
  | `stop` | 不续跑,只提醒一次;后面的规则也不再看 | 什么都不留 |
41
41
 
42
- 状态栏写着进度:续跑时是「⏩ 续跑 2/10 · auth」(第几次、最多几次、哪条规则),停了是「🛑 续跑已停 · auth」或「🛑 不续跑 · quota」;恢复、或者你发新消息时清掉。会话里的提示只补状态栏没有的:续跑时是「onward:3 秒后静默重发」,停手时说为什么、怎么办,不重复次数和规则名。`/onward status` 和启动提示用同一套图标:⏩ 在管,💤 不会续跑(关了或没有规则),🛑 停了或设置文件要先修。`pi -p` 没有界面,不写状态栏也不提示;RPC 下 pi 把它们发给客户端,显不显示由客户端定。
42
+ 状态栏写着进度:续跑时是「⏩ 续跑 2/10 · auth」(第几次、最多几次、哪条规则),停了是「🛑 续跑已停 · auth」或「🛑 不续跑 · quota」;恢复、或者你发新消息时清掉。会话里的提示只补状态栏没有的:续跑时是「onward:3 秒后静默重发」,空回复时 pi 什么都不显示,所以前面加上「空回复,」;停手时说为什么、怎么办,不重复次数和规则名。`/onward status` 和启动提示用同一套图标:⏩ 在管,💤 不会续跑(关了或没有规则),🛑 停了或设置文件要先修。`pi -p` 没有界面,不写状态栏也不提示;RPC 下 pi 把它们发给客户端,显不显示由客户端定。
43
43
 
44
44
  等待期间这一轮还算忙,和 pi 自带的重试一样:按 Esc 打断等待,不再续跑;等待时输入的消息优先,pi-onward 让路,由 pi 接着处理你的消息;`/onward off` 也会立刻打断。
45
45
 
@@ -54,7 +54,7 @@ pi install npm:@cobia/pi-onward
54
54
  - `3s..15s`(默认):按次数增长并封顶,第 n 次等 `min(3 秒 × n, 15 秒)`,即 3 → 6 → 9 → 12 → 15 → 15 … 秒,10 次合计 2 分钟。
55
55
  - `30s,1m,2m`:按列表来,最后一个值重复。
56
56
 
57
- **计数清零**:续跑之后助手正常返回过一次(包括中间的工具轮)就算恢复,次数和同错计数都从头算;一轮正常结束、按 Esc、用户自己发起新一轮也清零。所以上限管的是「连着失败」,不是一轮里的总次数:只要每次续跑后都有进展,就会一直续下去。
57
+ **计数清零**:续跑之后助手正常返回过一次(包括中间的工具轮;空回复不算)就算恢复,次数和同错计数都从头算;一轮正常结束、按 Esc、用户自己发起新一轮也清零。所以上限管的是「连着失败」,不是一轮里的总次数:只要每次续跑后都有进展,就会一直续下去。
58
58
 
59
59
  ## 📝 设置文件
60
60
 
@@ -95,6 +95,7 @@ pi install npm:@cobia/pi-onward
95
95
 
96
96
  - **没有内置规则**。没写 `rules`,出错时什么都不做。顶层的参数只是规则没写时的默认值,本身不会触发续跑。
97
97
  - 按顺序第一条命中的生效,**没命中的错误不处理**。想兜底就在最后放一条不带 `when` 的规则。
98
+ - 不带 `when` 的兜底只管出错,不管空回复;空回复要单独写一条 `"when": { "empty": true }` 的规则。
98
99
  - `"action": "stop"` 的规则命中就停、只提醒,也挡住后面的兜底。适合额度用完这类重试没用的错误。
99
100
  - `enabled: false` 的规则保留但跳过。
100
101
  - 每条规则有自己的续跑次数;「同一个错误连续几次」是全局的,但用的是当前命中规则的上限。
@@ -103,11 +104,12 @@ pi install npm:@cobia/pi-onward
103
104
  | :-------------------------------------------------------- | :---------------------------------------------------------------- |
104
105
  | `name` | 规则名,出现在通知里,不能重名;不写时按位置叫 `rule-1`、`rule-2` |
105
106
  | `enabled` | `false` 时保留但跳过 |
107
+ | `when.empty` | 写 `true` 时只管空回复,不管出错;不能和 `when.error` 一起写 |
106
108
  | `when.error` | 正则,大小写不敏感,对原始错误文案匹配 |
107
109
  | `when.provider` / `when.model` | 通配,只有 `*` 有特殊含义,大小写不敏感,整串匹配 |
108
110
  | `action`、`prompt`、`maxRetries`、`maxSameError`、`delay` | 覆盖顶层的同名字段 |
109
111
 
110
- `prompt` 里可以写 `{attempt}`(第几次)、`{max}`(最多几次)、`{error}`(错误摘要)、`{rule}`(规则名)。
112
+ `prompt` 里可以写 `{attempt}`(第几次)、`{max}`(最多几次)、`{error}`(错误摘要,空回复时是「空回复」)、`{rule}`(规则名)。
111
113
 
112
114
  **设置文件里有读不懂的地方,整份不生效**,修好之前不会自动续跑,启动时和 `/onward status` 都会提醒。拼错或不认识的键、超出范围的值、不是 `true` / `false` 的 `enabled`、重名的规则都算。不会只丢掉写坏的那部分、其余照跑:那样错误会落到更宽的规则或默认值上,比如写坏了的 `stop` 规则让额度错误落到兜底去续跑,多发真实请求。问题用 `/onward show` 查看,改完 `/onward reload`。设置文件不是合法 JSON、或者顶层不是对象时也一样;这时命令不会改写它,回复「未做任何修改」。
113
115
 
@@ -164,6 +166,14 @@ pi install npm:@cobia/pi-onward
164
166
  },
165
167
  "action": "resend"
166
168
  },
169
+ {
170
+ "name": "empty",
171
+ "when": {
172
+ "empty": true
173
+ },
174
+ "action": "message",
175
+ "maxRetries": 3
176
+ },
167
177
  {
168
178
  "name": "fallback",
169
179
  "action": "message",
@@ -183,6 +193,7 @@ pi install npm:@cobia/pi-onward
183
193
  | `rate-limit` | pi 自带重试用完后仍然 `429` | 静默重发,间隔拉长到 30 秒 → 1 分 → 2 分,最多 5 次 |
184
194
  | `server` | `5xx`、`overloaded`、上游负载饱和 | 静默重发,默认间隔 |
185
195
  | `network` | `Connection error.`、`ECONNRESET`、超时、`fetch failed` | 静默重发,默认间隔 |
196
+ | `empty` | 空回复:正常结束,但没有文字也没有工具调用。中转偶尔这样返回 | 发「继续」让模型接着做;原样重发通常还是空的。最多 3 次 |
186
197
  | `fallback` | 上面都没命中的错误 | 发一条带错误摘要的消息,让模型自己接着做;最多 3 次,同一个错误连续超过 2 次就停。不想兜底就删掉 |
187
198
 
188
199
  - 只想对某个中转生效:在 `when` 里加 `"provider": "你的中转-*"`(只有 `*` 是通配)。
package/example.json CHANGED
@@ -52,6 +52,14 @@
52
52
  },
53
53
  "action": "resend"
54
54
  },
55
+ {
56
+ "name": "empty",
57
+ "when": {
58
+ "empty": true
59
+ },
60
+ "action": "message",
61
+ "maxRetries": 3
62
+ },
55
63
  {
56
64
  "name": "fallback",
57
65
  "action": "message",
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@cobia/pi-onward",
3
- "version": "0.1.1",
4
- "description": "Keep a pi run going after it ends in an error: user-written rules decide whether to send a visible message or silently resend, with per-rule budgets, backoff and a same-error stop.",
3
+ "version": "0.1.2",
4
+ "description": "Keep a pi run going after it ends in an error or an empty reply: user-written rules decide whether to send a visible message or silently resend, with per-rule budgets, backoff and a same-error stop.",
5
5
  "keywords": [
6
6
  "pi",
7
7
  "pi-package",
@@ -1,5 +1,6 @@
1
1
  /**
2
- * pi-onward:一轮以错误收尾(401、429、502、Connection error. 等)时,按用户写的规则续跑。没有内置规则。
2
+ * pi-onward:一轮以错误收尾(401、429、502、Connection error. 等)、或者正常结束却是空回复时,按用户写的规则续跑。
3
+ * 没有内置规则。
3
4
  *
4
5
  * - `policy/`:纯逻辑,不碰 pi 的运行时和文件。什么算失败、命中哪条规则、按什么参数、等多久、什么时候停。
5
6
  * - `extension/`:接 pi。`onward` 处理事件并续跑,`command` 是 `/onward`,`autocomplete` 让它的参数里按 Tab 补参数,
@@ -1,5 +1,5 @@
1
1
  /**
2
- * 一轮以错误收尾、命中规则时,在 `agent_before_settle` 边界上等一会儿,再要 pi 接着跑:
2
+ * 一轮以错误收尾、或者正常结束却是空回复,并且命中规则时,在 `agent_before_settle` 边界上等一会儿,再要 pi 接着跑:
3
3
  * - message:追加一条看得见的消息(默认「继续」),模型照常读到;
4
4
  * - resend:用 `context_edit` 把失败的那条从模型上下文里省略,重发同一个请求。失败的记录仍留在会话里。
5
5
  *
@@ -75,7 +75,7 @@ export class Onward {
75
75
  }
76
76
 
77
77
  messageEnd(event: MessageEndEvent, ctx: Ctx): void {
78
- // 续跑之后只要正常返回过一次就算恢复了:后面再出错是新的一串,次数从头算。
78
+ // 续跑之后只要正常返回过一次就算恢复了:后面再出错是新的一串,次数从头算。空回复不算正常返回。
79
79
  if (this.owns(ctx) && isSuccess(event.message)) this.reset(ctx);
80
80
  }
81
81
 
@@ -103,7 +103,7 @@ export class Onward {
103
103
  display: true,
104
104
  details: { rule: hit.rule.name, attempt: step.attempt, max: hit.policy.maxRetries },
105
105
  };
106
- say(ctx, retryNotice(step, draft));
106
+ say(ctx, retryNotice(step, draft, hit.failure));
107
107
  this.setStatus(ctx, "retrying", retryStatus(hit, step));
108
108
  if (await this.wait(step.delay, ctx.signal)) return undefined;
109
109
  // 等的时候用户发了消息:pi 会接着处理它,这算用户开的新一轮。
@@ -11,7 +11,7 @@
11
11
  import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
12
12
  import type { Config, Hit } from "../policy/config.ts";
13
13
  import { formatDelay, retryDelay, totalDelay } from "../policy/delay.ts";
14
- import { errorSignature, type Failure } from "../policy/failure.ts";
14
+ import { EMPTY_REPLY, errorSignature, type Failure } from "../policy/failure.ts";
15
15
  import { DEFAULT_POLICY, FIELDS, type Field, type Policy, resolvePolicy } from "../policy/policy.ts";
16
16
  import type { Rule } from "../policy/rules.ts";
17
17
  import type { GiveUp, Progress, Retry } from "../policy/tracker.ts";
@@ -231,11 +231,12 @@ export function describeRules(config: Config): string {
231
231
  ...table(rows).map((line) => ` ${line}`),
232
232
  ];
233
233
  });
234
- return ["规则(按顺序,第一条命中的生效;没命中的错误不处理)", ...blocks.flat()].join("\n");
234
+ return ["规则(按顺序,第一条命中的生效;没命中的错误和空回复不处理)", ...blocks.flat()].join("\n");
235
235
  }
236
236
 
237
237
  function describeWhen({ when }: Rule): string {
238
238
  const parts = [
239
+ when.empty && "空回复",
239
240
  when.error && `错误匹配 /${when.error.source}/i`,
240
241
  when.provider && `provider=${when.provider}`,
241
242
  when.model && `model=${when.model}`,
@@ -269,11 +270,14 @@ export function startupNotices(settings: Settings): Notice[] {
269
270
  return [];
270
271
  }
271
272
 
272
- /** 和状态栏同一个写法:`⏩ onward:续跑 1/20 · auth,3 秒后静默重发`。错误本身 pi 已经显示过了。 */
273
- /** 状态栏已经写了第几次、哪条规则,这里只说接下来做什么:`onward:3 秒后静默重发`。 */
274
- export function retryNotice(step: Retry, draft: Continue): Notice {
273
+ /**
274
+ * 状态栏已经写了第几次、哪条规则,这里只说接下来做什么:`onward:3 秒后静默重发`。出错时 pi 已经显示过错误;
275
+ * 空回复 pi 什么都不显示,所以先说一句:`onward:空回复,3 秒后发「继续」`。
276
+ */
277
+ export function retryNotice(step: Retry, draft: Continue, failure: Failure): Notice {
275
278
  const action = draft.type === "context_edit" ? "静默重发" : `发「${truncate(draft.content, 30)}」`;
276
- return info(`onward:${formatDelay(step.delay)}后${action}`);
279
+ const what = failure.stopReason === "stop" ? `${EMPTY_REPLY},` : "";
280
+ return info(`onward:${what}${formatDelay(step.delay)}后${action}`);
277
281
  }
278
282
 
279
283
  /** 状态栏已经写了停了、哪条规则,这里只说为什么停、怎么办。 */
@@ -6,18 +6,36 @@ export type AgentMessage = MessageEndEvent["message"];
6
6
  type AssistantMessage = Extract<AgentMessage, { role: "assistant" }>;
7
7
 
8
8
  /** 以错误结束的助手消息。provider、model 是 pi 必填的;errorMessage 可能没有。 */
9
- export type Failure = AssistantMessage & { stopReason: "error" };
9
+ export type ErrorReply = AssistantMessage & { stopReason: "error" };
10
+
11
+ /** 正常结束(stop),但没有文字也没有工具调用:中转偶尔这样返回,这一轮什么都没做就停了。 */
12
+ export type EmptyReply = AssistantMessage & { stopReason: "stop" };
13
+
14
+ /** 规则要处理的两种收尾:出错,或者空回复。 */
15
+ export type Failure = ErrorReply | EmptyReply;
10
16
 
11
17
  /** 用户按 Esc 是 `aborted`,不算失败。 */
12
- export function isFailure(message: AgentMessage | undefined): message is Failure {
18
+ export function isError(message: AgentMessage | undefined): message is ErrorReply {
13
19
  return message?.role === "assistant" && message.stopReason === "error";
14
20
  }
15
21
 
22
+ /** 只有空白的文字、只有思考也算空:用户什么都没看到。 */
23
+ export function isEmptyReply(message: AgentMessage | undefined): message is EmptyReply {
24
+ if (message?.role !== "assistant" || message.stopReason !== "stop") return false;
25
+ return !message.content.some(
26
+ (part) => part.type === "toolCall" || (part.type === "text" && part.text.trim() !== ""),
27
+ );
28
+ }
29
+
30
+ export function isFailure(message: AgentMessage | undefined): message is Failure {
31
+ return isError(message) || isEmptyReply(message);
32
+ }
33
+
16
34
  const SUCCESS: ReadonlySet<AssistantMessage["stopReason"]> = new Set(["stop", "length", "toolUse", "deferred"]);
17
35
 
18
- /** 助手正常返回了一次(包括中间的工具轮),说明已经恢复。 */
36
+ /** 助手正常返回了一次(包括中间的工具轮),说明已经恢复。空回复不算。 */
19
37
  export function isSuccess(message: AgentMessage): boolean {
20
- return message.role === "assistant" && SUCCESS.has(message.stopReason);
38
+ return message.role === "assistant" && SUCCESS.has(message.stopReason) && !isEmptyReply(message);
21
39
  }
22
40
 
23
41
  /**
@@ -36,5 +54,9 @@ export function normalizeError(text: string): string {
36
54
  }
37
55
 
38
56
  export function errorSignature(failure: Failure): string {
57
+ if (failure.stopReason === "stop") return EMPTY_REPLY;
39
58
  return normalizeError(failure.errorMessage ?? "") || "未知错误";
40
59
  }
60
+
61
+ /** 空回复的「错误摘要」:提示词里的 {error}、同错计数都用它。 */
62
+ export const EMPTY_REPLY = "空回复";
@@ -1,5 +1,6 @@
1
1
  /**
2
2
  * 规则 = 条件(when)+ 参数,按顺序第一条命中的生效。参数和设置文件顶层同名,只写和顶层不同的。
3
+ * 没写 `when.empty` 的规则管出错,写了的只管空回复;不带 when 的兜底因此不会碰空回复。
3
4
  * 没有内置规则:没写或没命中,都不处理。读不懂的内容只记问题,不替用户猜;有问题时整份设置不生效(见 config.ts)。
4
5
  */
5
6
  import { errorText, isRecord } from "../util.ts";
@@ -7,6 +8,8 @@ import type { Failure } from "./failure.ts";
7
8
  import { FIELD_NAMES, type PolicyPatch, readParams } from "./policy.ts";
8
9
 
9
10
  export interface RuleWhen {
11
+ /** 只管空回复;只能是 true,不写就是管出错。 */
12
+ empty?: true;
10
13
  /** 大小写不敏感,对原始错误文案匹配;读设置时编译一次。 */
11
14
  error?: RegExp;
12
15
  /** 通配,只有 `*` 有特殊含义。 */
@@ -29,6 +32,7 @@ export function findRule(rules: readonly Rule[], failure: Failure): Rule | undef
29
32
  }
30
33
 
31
34
  function matches(when: RuleWhen, failure: Failure): boolean {
35
+ if ((when.empty === true) !== (failure.stopReason === "stop")) return false;
32
36
  if (when.error !== undefined && !when.error.test(failure.errorMessage ?? "")) return false;
33
37
  if (when.provider !== undefined && !glob(when.provider, failure.provider)) return false;
34
38
  if (when.model !== undefined && !glob(when.model, failure.model)) return false;
@@ -97,6 +101,10 @@ function parseWhen(raw: unknown, where: string, problems: string[]): RuleWhen {
97
101
  }
98
102
  for (const [key, value] of Object.entries(raw)) {
99
103
  switch (key) {
104
+ case "empty":
105
+ if (value === true) when.empty = true;
106
+ else problems.push(`${where}.empty 只能是 true;不写就是管出错`);
107
+ break;
100
108
  case "error":
101
109
  if (typeof value !== "string" || !value) {
102
110
  problems.push(`${where}.error 需要非空正则字符串`);
@@ -117,5 +125,7 @@ function parseWhen(raw: unknown, where: string, problems: string[]): RuleWhen {
117
125
  problems.push(`${where}.${key} 不认识`);
118
126
  }
119
127
  }
128
+ if (raw.empty === true && raw.error !== undefined)
129
+ problems.push(`${where} 里 empty 和 error 不能一起写:空回复没有错误文案`);
120
130
  return when;
121
131
  }