@cobia/pi-onward 0.5.1 → 0.5.3

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.
@@ -161,11 +161,11 @@ export class Recovery {
161
161
  const definition = this.registry.get(hit.stage.action.use)?.definition;
162
162
  const problem = this.registry.check(hit.stage.action);
163
163
  if (problem || !definition) {
164
- this.finish(ctx, { kind: "failed", message: problem ?? "Action is not loaded" }, hit);
164
+ this.finish(ctx, { kind: "failed", message: problem ?? "动作未加载" }, hit);
165
165
  return;
166
166
  }
167
167
  if (!definition.modes.includes(ctx.mode)) {
168
- this.finish(ctx, { kind: "failed", message: `${definition.name} does not support ${ctx.mode} mode` }, hit);
168
+ this.finish(ctx, { kind: "failed", message: `${definition.name} 不支持 ${ctx.mode} 模式` }, hit);
169
169
  return;
170
170
  }
171
171
  const chain = this.episode.chain;
@@ -242,7 +242,7 @@ export class Recovery {
242
242
  );
243
243
  if (!command) {
244
244
  this.pending = undefined;
245
- this.finish(ctx, { kind: "failed", message: "Onward dispatch command is unavailable" }, invocation.hit);
245
+ this.finish(ctx, { kind: "failed", message: "onward 的分派命令不可用" }, invocation.hit);
246
246
  return;
247
247
  }
248
248
  // Pi defers this command until ALL agent_settled handlers finish.
@@ -345,21 +345,19 @@ export class Recovery {
345
345
  return definition.execute(params, { ...context, signal });
346
346
  if (definition.phase === "settled" && context.phase === "settled")
347
347
  return definition.execute(params, { ...context, signal });
348
- return { kind: "failed", message: "Action execution phase does not match its context" };
348
+ return { kind: "failed", message: "动作的执行阶段与当前上下文不符" };
349
349
  };
350
350
  const result = await raceAbort(execute(), signal);
351
351
  if (signal.aborted)
352
352
  return {
353
353
  kind: "unknown",
354
- message: timeout.signal.aborted
355
- ? "Action timed out; outcome unknown; not replayed"
356
- : "Execution cancelled; side-effect outcome unknown",
354
+ message: timeout.signal.aborted ? "动作执行超时,结果未知,不会重放" : "执行已取消,副作用是否发生未知",
357
355
  };
358
356
  if (!validResult(result, invocation.definition))
359
- return { kind: "unknown", message: "Action returned an invalid result; not replayed" };
357
+ return { kind: "unknown", message: "动作返回了无效的结果,不会重放" };
360
358
  return result;
361
359
  } catch (error) {
362
- return { kind: "unknown", message: `Action threw an exception; outcome unknown: ${errorText(error)}` };
360
+ return { kind: "unknown", message: `动作抛出异常,结果未知:${errorText(error)}` };
363
361
  } finally {
364
362
  clearTimeout(timer);
365
363
  }
@@ -414,28 +412,33 @@ export class Recovery {
414
412
  }
415
413
  export function stateText(state: RunState, failures: number): string | undefined {
416
414
  if (state.phase === "idle") return undefined;
417
- const phases = {
418
- waiting: "⏳",
419
- submitted: "⏳",
420
- retrying: "⏩",
421
- queued: "⏳",
422
- executing: "🚀",
423
- stopped: "🛑",
424
- };
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
415
  const result = state.phase === "stopped" ? state.result : undefined;
436
- const icon = result ? results[result] : phases[state.phase];
437
416
  const detail = result === "failed" ? " 动作失败" : result === "unknown" ? " 结果未知" : "";
438
- return `${icon} onward${detail} · 失败${failures}`;
417
+ return `${stateIcon(state)} onward${detail} · 失败${failures}`;
418
+ }
419
+ const PHASE_ICONS: Record<Exclude<RunState["phase"], "idle">, string> = {
420
+ waiting: "⏳",
421
+ submitted: "⏳",
422
+ retrying: "⏩",
423
+ queued: "⏳",
424
+ executing: "🚀",
425
+ stopped: "🛑",
426
+ };
427
+ const RESULT_ICONS: Record<ActionResult["kind"], string> = {
428
+ continue: "⏩",
429
+ completed: "🎉",
430
+ dispatched: "📤",
431
+ handoff: "🔀",
432
+ stopped: "🛑",
433
+ failed: "💥",
434
+ cancelled: "🚫",
435
+ unknown: "🎲",
436
+ };
437
+ /** 恢复进行中的图标:状态栏和菜单、status 标题共用,同一状态只有一个图标。 */
438
+ export function stateIcon(state: RunState): string {
439
+ if (state.phase === "idle") return "⏩";
440
+ const result = state.phase === "stopped" ? state.result : undefined;
441
+ return result ? RESULT_ICONS[result] : PHASE_ICONS[state.phase];
439
442
  }
440
443
  function validResult(value: unknown, definition: ActionDefinition): value is ActionResult {
441
444
  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
- }