@cobia/pi-onward 0.5.0 → 0.5.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/CHANGELOG.md ADDED
@@ -0,0 +1,8 @@
1
+ # Changelog
2
+
3
+ ## [0.5.2]
4
+
5
+ - 提供基于规则与阶段的自动恢复,支持命令和动作调用。
6
+ - 提供 `@cobia/pi-onward/actions` 动作接口。
7
+ - 提供 `onward.json` 配置及 `onward.schema.json` 编辑器校验。
8
+ - 精简发布内容,配置 Schema 生成代码不再随 npm 包分发。
package/LICENSE CHANGED
@@ -1,6 +1,8 @@
1
1
  MIT License
2
2
 
3
+ Copyright (c) 2026 pi-labs contributors
3
4
  Copyright (c) 2026 pi-onward contributors
5
+ Copyright (c) 2026 pi-wecom-bot contributors
4
6
 
5
7
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
8
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -62,6 +62,25 @@ onward 处理两类失败:Pi 明确以错误结束,或者正常结束但没
62
62
 
63
63
  `onward.stop` 只结束本轮恢复;需要持续停用自动恢复时,使用 `/onward off`
64
64
 
65
+ ### 状态栏
66
+
67
+ 状态栏采用 `emoji onward · 失败次数` 的短格式,例如 `⏳ onward · 失败3`。数字表示本轮连续失败次数,规则、动作名和完整原因通过 `/onward status` 查看
68
+
69
+ | Emoji | 含义 |
70
+ | :--- | :--- |
71
+ | ⏳ | 等待间隔、提议等待启动或动作待执行 |
72
+ | ⏩ | 已启动续跑 |
73
+ | 🚀 | 正在执行动作 |
74
+ | 🎉 | 动作完成 |
75
+ | 📤 | 命令已分派,业务结果由目标插件报告 |
76
+ | 🔀 | 已接受接管 |
77
+ | 🛑 | 本轮恢复停止,例如规则停止、未命中规则或达到次数上限 |
78
+ | 🚫 | 恢复已取消 |
79
+ | 💥 | 动作失败,同时显示“动作失败” |
80
+ | 🎲 | 结果未知,同时显示“结果未知” |
81
+
82
+ 空闲时隐藏状态栏;终止结果保留到当前恢复状态被清除。异常提示示例:`🎲 onward 结果未知 · 失败3`
83
+
65
84
  ### 续跑消息与任务提示词
66
85
 
67
86
  `pi.message` 的 `prompt` 默认为 `继续`,可以使用以下变量,也可以与正文拼接:
@@ -108,8 +127,6 @@ onward 处理两类失败:Pi 明确以错误结束,或者正常结束但没
108
127
 
109
128
  结果“已分派”表示命令已交给 Pi,业务是否成功由目标插件报告。onward 不能强制取消已经进入目标命令处理器的操作,也不会自动重放命令。通用命令的跨会话、跨进程防循环由目标插件负责,`maxHandoffs` 不限制这类调用
110
129
 
111
- 需要结构化结果或恢复链传递时,可以使用独立桥接扩展或插件提供的具名动作,详见[动作提供者协议](docs/actions.md)
112
-
113
130
  ### 计数与停止
114
131
 
115
132
  连续失败次数属于整轮恢复,换错误或换规则不会清零。一次失败以 Pi 完成本轮重试后的最终结果为准,不包含 Pi 内建重试期间的每个 HTTP 请求
@@ -159,7 +176,7 @@ onward 处理两类失败:Pi 明确以错误结束,或者正常结束但没
159
176
 
160
177
  ```json
161
178
  {
162
- "$schema": "https://raw.githubusercontent.com/cobia68/pi-onward/main/onward.schema.json",
179
+ "$schema": "https://raw.githubusercontent.com/cobia68/pi-labs/main/packages/pi-onward/onward.schema.json",
163
180
  "enabled": true,
164
181
  "maxFailures": 20,
165
182
  "maxHandoffs": 3,
package/example.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "$schema": "https://raw.githubusercontent.com/cobia68/pi-onward/main/onward.schema.json",
2
+ "$schema": "https://raw.githubusercontent.com/cobia68/pi-labs/main/packages/pi-onward/onward.schema.json",
3
3
  "enabled": true,
4
4
  "maxFailures": 20,
5
5
  "maxHandoffs": 3,
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "$schema": "https://json-schema.org/draft/2020-12/schema",
3
- "$id": "https://raw.githubusercontent.com/cobia68/pi-onward/main/onward.schema.json",
3
+ "$id": "https://raw.githubusercontent.com/cobia68/pi-labs/main/packages/pi-onward/onward.schema.json",
4
4
  "title": "pi-onward 阶段恢复策略",
5
5
  "type": "object",
6
6
  "additionalProperties": false,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cobia/pi-onward",
3
- "version": "0.5.0",
3
+ "version": "0.5.2",
4
4
  "description": "pi 出错或空回复时,按阶段策略恢复并调用扩展命令或具名动作",
5
5
  "keywords": [
6
6
  "pi",
@@ -16,7 +16,16 @@
16
16
  },
17
17
  "repository": {
18
18
  "type": "git",
19
- "url": "git+https://github.com/cobia68/pi-onward.git"
19
+ "url": "git+https://github.com/cobia68/pi-labs.git",
20
+ "directory": "packages/pi-onward"
21
+ },
22
+ "homepage": "https://github.com/cobia68/pi-labs/tree/main/packages/pi-onward#readme",
23
+ "bugs": {
24
+ "url": "https://github.com/cobia68/pi-labs/issues"
25
+ },
26
+ "publishConfig": {
27
+ "access": "public",
28
+ "registry": "https://registry.npmjs.org/"
20
29
  },
21
30
  "pi": {
22
31
  "extensions": [
@@ -28,32 +37,26 @@
28
37
  },
29
38
  "files": [
30
39
  "src/",
31
- "docs/",
32
40
  "example.json",
33
- "onward.schema.json"
41
+ "onward.schema.json",
42
+ "CHANGELOG.md"
34
43
  ],
35
44
  "scripts": {
36
- "test": "node scripts/run-tests.mjs",
37
- "typecheck": "node node_modules/typescript/bin/tsc --noEmit",
45
+ "test": "node ../../scripts/run-tests.mjs",
38
46
  "check:extension": "node scripts/check-extension.mjs",
39
47
  "gen:schema": "node --import tsx scripts/generate-schema.ts",
40
- "check": "npm run check:biome && npm run typecheck && npm test && npm run check:extension",
41
- "prepublishOnly": "npm run check",
42
- "lint": "node scripts/biome-check.mjs --lint",
43
- "format": "node scripts/biome-check.mjs --write",
44
- "check:biome": "node scripts/biome-check.mjs"
48
+ "check": "node ../../scripts/packages.mjs check pi-onward",
49
+ "prepublishOnly": "npm run check"
45
50
  },
46
51
  "peerDependencies": {
47
52
  "@earendil-works/pi-coding-agent": "*",
48
53
  "@earendil-works/pi-tui": "*"
49
54
  },
50
55
  "devDependencies": {
51
- "@biomejs/biome": "^2.6.4",
52
56
  "@earendil-works/pi-coding-agent": "^1.4.0",
53
57
  "@earendil-works/pi-tui": "^1.4.0",
54
58
  "@types/node": "^22.20.5",
55
- "tsx": "^4.23.15",
56
- "typescript": "^7.0.2"
59
+ "tsx": "^4.23.15"
57
60
  },
58
61
  "dependencies": {
59
62
  "ajv": "^8.20.0"
@@ -415,14 +415,27 @@ export class Recovery {
415
415
  export function stateText(state: RunState, failures: number): string | undefined {
416
416
  if (state.phase === "idle") return undefined;
417
417
  const phases = {
418
- waiting: "等待",
419
- submitted: "提议已提交",
420
- retrying: "续跑中",
421
- queued: "待执行",
422
- executing: "执行中",
423
- stopped: "已停止",
418
+ waiting: "⏳",
419
+ submitted: "⏳",
420
+ retrying: "⏩",
421
+ queued: "⏳",
422
+ executing: "🚀",
423
+ stopped: "🛑",
424
424
  };
425
- return `${state.phase === "stopped" ? "🛑" : "⏩"} onward · ${phases[state.phase]} · 失败 ${failures} 次${state.hit ? ` · ${state.hit.stage.action.use}` : ""}`;
425
+ const results: Record<ActionResult["kind"], string> = {
426
+ continue: "⏩",
427
+ completed: "🎉",
428
+ dispatched: "📤",
429
+ handoff: "🔀",
430
+ stopped: "🛑",
431
+ failed: "💥",
432
+ cancelled: "🚫",
433
+ unknown: "🎲",
434
+ };
435
+ const result = state.phase === "stopped" ? state.result : undefined;
436
+ const icon = result ? results[result] : phases[state.phase];
437
+ const detail = result === "failed" ? " 动作失败" : result === "unknown" ? " 结果未知" : "";
438
+ return `${icon} onward${detail} · 失败${failures}`;
426
439
  }
427
440
  function validResult(value: unknown, definition: ActionDefinition): value is ActionResult {
428
441
  if (!isRecord(value) || typeof value.kind !== "string") return false;
package/docs/actions.md DELETED
@@ -1,97 +0,0 @@
1
- # 动作提供者协议
2
-
3
- 普通插件命令使用 `pi.command`,目标插件只需已在 Pi 中注册命令,不需要实现本文的协议。`pi.message` 和 `pi.command` 由 onward 内置的 Pi 适配模块注册,无需额外安装;这两个名称属于 onward 的动作命名空间,并非 Pi 官方 API。本文用于独立桥接扩展或插件主动提供结构化能力。
4
-
5
- ## 发现与加载
6
-
7
- Pi 负责安装、加载和销毁扩展。动作提供者在扩展工厂里监听 `onward:discover-actions`,同步调用事件参数的 `add(definition)`。onward 在 `session_start` 中发起发现;此时所有扩展工厂已完成,因此与加载顺序无关。异步发现结果不会纳入该次注册表,耗时准备工作应由扩展工厂完成后再声明动作。
8
-
9
- 可从 `@cobia/pi-onward/actions` 导入 `provideAction` 和类型,也可以按同样的结构直接实现事件约定。该导出不依赖全局单例,不能依赖两个包共享模块缓存。插件在没有 onward 时可以继续独立工作。
10
-
11
- ```ts
12
- import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
13
- import { provideAction } from "@cobia/pi-onward/actions";
14
-
15
- export default function (pi: ExtensionAPI) {
16
- provideAction(pi, {
17
- name: "example.notify",
18
- description: "通知当前失败已交给人工处理",
19
- phase: "settled",
20
- modes: ["tui", "rpc"],
21
- parameters: {
22
- type: "object",
23
- additionalProperties: false,
24
- required: ["text"],
25
- properties: { text: { type: "string", minLength: 1 } }
26
- },
27
- execute(params, context) {
28
- if (context.signal.aborted) return { kind: "cancelled", message: "已取消" };
29
- context.host.ui.notify(String(params.text), "warning");
30
- return { kind: "completed", message: "通知已显示,本轮停止" };
31
- }
32
- });
33
- }
34
- ```
35
-
36
- 名称采用 `namespace.action`,每段为小写字母起首,后续允许小写字母、数字和连字符。重复名称使该名称不可用,不按加载顺序覆盖。注册诊断与配置校验分开:未被启用规则引用的注册失败只警告;引用了缺失、冲突或参数非法的动作才阻断整份配置。`/onward actions` 显示包含动作名称的注册失败原因。
37
-
38
- `parameters` 使用同步 JSON Schema 2020-12,由 AJV 严格校验;禁止异步 Schema,不转换参数类型,也不写入默认值。提供者在执行时自行采用可选参数的默认值。`format` 注释不验证,额外限制可以在动作中检查并返回明确失败。
39
-
40
- `provideAction` 返回注销函数;session_shutdown 时自动注销。注销后保留的旧执行句柄也会返回取消。独立实现协议时同样要防止旧句柄在卸载后继续操作。
41
-
42
- ## 调用接口
43
-
44
- 配置只引用 `{"use":"namespace.action","with":{...}}`。调用前把参数复制,提供者修改参数不会影响已加载配置。
45
-
46
- 通用执行上下文包含:
47
-
48
- | 字段 | 含义 |
49
- | --- | --- |
50
- | `invocationId` | 本次调用唯一 ID,用于提供者去重 |
51
- | `episodeId` | 本次恢复过程 ID |
52
- | `sessionId` | 调用时的会话 ID |
53
- | `rule` | 当前规则名 |
54
- | `failures` | 本次恢复过程中的连续失败次数 |
55
- | `minFailures` | 所选阶段的连续失败次数下限,包含当前失败 |
56
- | `failure` | 只读失败快照:kind、text、provider、model |
57
- | `taskPrompt` | 可选的已接收任务文本快照;用于 `{taskPrompt}`,自动消息保留来源,重载后按会话记录重建 |
58
- | `chain` | 只读恢复链:id、handoffs、maxHandoffs |
59
- | `signal` | 合并取消及动作执行时限的信号 |
60
-
61
- `boundary` 动作额外得到 `targetId`,用来生成 `context_edit`,并可以返回 `{kind:"continue",entries:[...]}`。条目由 Pi 验证和提交;在此阶段不能等待当前轮次结束,也不能直接调用需要空闲命令上下文的操作。
62
-
63
- `settled` 动作额外得到 `host: ExtensionCommandContext`。执行器通过 Pi 的命令桥接,确保所有 `agent_settled` 观察者结束后再执行。调用前重新检查会话、取消、配置、空闲状态和排队消息。不要把 `host` 保存到另一会话或进程;Pi 重载或替换会话后它会失效。
64
-
65
- ## 结果和副作用
66
-
67
- | kind | 含义 |
68
- | --- | --- |
69
- | `continue` | 边界动作提出下一轮;只有 Pi 真正启动才记已启动续跑 |
70
- | `completed` | 动作自身完成,本轮结束;不自动追加模型请求 |
71
- | `dispatched` | 已提交给另一个系统,业务结果未知;通用命令返回此结果 |
72
- | `handoff` | 提供者接受后续恢复责任,需要声明 `handoff:true` |
73
- | `stopped` | 策略要求停止 |
74
- | `failed` | 提供者明确报告未成功执行 |
75
- | `cancelled` | 明确取消 |
76
- | `unknown` | 可能已产生副作用,最终结果未知 |
77
-
78
- 失败、停止、取消和未知必须带 `message`;其他结果可选。意外异常、超时及非法返回均转为 `unknown`,不自动重放、不自动进入下一阶段。提供者应在副作用前检查 `signal` 并把它传给可取消操作。超时不能强行终止进程内 JavaScript,也不能撤销已发生的副作用。
79
-
80
- 落定动作执行前会写入 `onward.action` 自定义记录,结束后写入结果。记录不包含提示词或失败原文,也不进入模型上下文。重载不重放记录。当前运行时对一次调用最多分派一次,跨崩溃的去重仍由提供者使用 invocationId 处理。
81
-
82
- ## 可选的跨会话接管
83
-
84
- 仅有跨会话责任的原生动作声明 `handoff:true`。调用前执行器预留一次预算,传给提供者的 `chain.handoffs` 已包含本次。明确 failed/cancelled/stopped/completed 会退回预留;unknown 保留。新会话的限制只允许维持或收紧链的上限。
85
-
86
- 提供者负责把 `chain` 存进自己的一次性、可验证、可过期的交接记录。校验新会话来源并成功认领记录后,在发送自动提示之前发出:
87
-
88
- ```ts
89
- pi.events.emit("onward:resume-recovery", {
90
- sessionId: ctx.sessionManager.getSessionId(),
91
- chain: savedChain
92
- });
93
- ```
94
-
95
- onward 在工厂初始化时已监听此事件,支持在它自己的 session_start 之前或之后收到。错误的会话 ID 不生效。已接收的人工新任务或正常模型进展结束恢复链;自动注入的消息保留当前链。
96
-
97
- 通用 slash 命令没有上述约定,也没有业务结果回执,因此不能推断恢复链已传递。跨会话或跨进程的防循环由目标扩展负责。独立桥接扩展也不能通过包装命令声称获得了命令没有公开的确认、取消或去重能力。
@@ -1,94 +0,0 @@
1
- /** Structural JSON Schema. Loaded action providers validate their own parameters at runtime. */
2
- import { ACTION_NAME_PATTERN } from "../actions/protocol.ts";
3
- import { DELAY_PATTERN } from "./delay.ts";
4
- import { DEFAULT_LIMITS, FIELDS } from "./policy.ts";
5
- export const SCHEMA_URL = "https://raw.githubusercontent.com/cobia68/pi-onward/main/onward.schema.json";
6
- export function configSchema(): Record<string, unknown> {
7
- return {
8
- $schema: "https://json-schema.org/draft/2020-12/schema",
9
- $id: SCHEMA_URL,
10
- title: "pi-onward 阶段恢复策略",
11
- type: "object",
12
- additionalProperties: false,
13
- properties: {
14
- $schema: { type: "string" },
15
- enabled: { type: "boolean", default: true },
16
- quietStartup: { type: "boolean", description: "仅允许写在全局;未配置时跟随 Pi" },
17
- ...Object.fromEntries(
18
- FIELDS.map((field) => [
19
- field.name,
20
- {
21
- ...field.schema,
22
- description: field.description,
23
- default: field.name === "actionTimeout" ? "30s" : DEFAULT_LIMITS[field.name],
24
- },
25
- ]),
26
- ),
27
- rules: { type: "array", items: { $ref: "#/$defs/rule" } },
28
- },
29
- $defs: {
30
- rule: {
31
- type: "object",
32
- additionalProperties: false,
33
- required: ["stages"],
34
- properties: {
35
- name: { type: "string", pattern: "\\S" },
36
- enabled: { type: "boolean", default: true },
37
- when: { $ref: "#/$defs/when" },
38
- stages: {
39
- type: "array",
40
- minItems: 1,
41
- description: "minFailures 严格递增;每次失败选择达到下限的最后一个阶段",
42
- prefixItems: [
43
- {
44
- allOf: [
45
- { $ref: "#/$defs/stage" },
46
- { type: "object", properties: { minFailures: { const: 1 } } },
47
- ],
48
- },
49
- ],
50
- items: { $ref: "#/$defs/stage" },
51
- },
52
- },
53
- },
54
- when: {
55
- type: "object",
56
- additionalProperties: false,
57
- properties: {
58
- failure: { type: "string", minLength: 1, description: "大小写不敏感的正则;空回复用 ^$,所有失败用 .*" },
59
- provider: { type: "string", pattern: "\\S" },
60
- model: { type: "string", pattern: "\\S" },
61
- },
62
- },
63
- stage: {
64
- type: "object",
65
- additionalProperties: false,
66
- required: ["minFailures", "action"],
67
- properties: {
68
- minFailures: {
69
- type: "integer",
70
- minimum: 1,
71
- maximum: 1000,
72
- description: "阶段生效的连续失败次数下限,包含当前失败",
73
- },
74
- delay: {
75
- type: "string",
76
- pattern: DELAY_PATTERN,
77
- default: "0s",
78
- description: "在当前阶段内按失败次数推进;省略时不等待",
79
- },
80
- action: { $ref: "#/$defs/action" },
81
- },
82
- },
83
- action: {
84
- type: "object",
85
- additionalProperties: false,
86
- required: ["use"],
87
- properties: {
88
- use: { type: "string", pattern: ACTION_NAME_PATTERN },
89
- with: { type: "object", description: "由已加载动作提供的 JSON Schema 校验;不执行隐式类型转换" },
90
- },
91
- },
92
- },
93
- };
94
- }