@armadra/agent 0.12.0 → 0.14.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.
Files changed (57) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/CHANGELOG.zh-CN.md +16 -0
  3. package/dist/ai/fake/fake-provider.d.ts +6 -0
  4. package/dist/ai/fake/fake-provider.js +46 -0
  5. package/dist/ai/fake/fake-script.d.ts +8 -1
  6. package/dist/ai/fake/fake-script.js +5 -1
  7. package/dist/bridge/pi/args.d.ts +19 -0
  8. package/dist/bridge/pi/args.js +96 -0
  9. package/dist/bridge/pi/capabilities.d.ts +57 -0
  10. package/dist/bridge/pi/capabilities.js +65 -0
  11. package/dist/bridge/pi/config.d.ts +28 -0
  12. package/dist/bridge/pi/config.js +67 -0
  13. package/dist/bridge/pi/elicit.d.ts +23 -0
  14. package/dist/bridge/pi/elicit.js +86 -0
  15. package/dist/bridge/pi/entry.d.ts +14 -0
  16. package/dist/bridge/pi/entry.js +59 -0
  17. package/dist/bridge/pi/events.d.ts +62 -0
  18. package/dist/bridge/pi/events.js +223 -0
  19. package/dist/bridge/pi/gate.d.ts +42 -0
  20. package/dist/bridge/pi/gate.js +69 -0
  21. package/dist/bridge/pi/mcp.d.ts +17 -0
  22. package/dist/bridge/pi/mcp.js +49 -0
  23. package/dist/bridge/pi/prompt.d.ts +8 -0
  24. package/dist/bridge/pi/prompt.js +26 -0
  25. package/dist/bridge/pi/replay.d.ts +10 -0
  26. package/dist/bridge/pi/replay.js +87 -0
  27. package/dist/bridge/pi/server.d.ts +61 -0
  28. package/dist/bridge/pi/server.js +240 -0
  29. package/dist/bridge/pi/session.d.ts +112 -0
  30. package/dist/bridge/pi/session.js +497 -0
  31. package/dist/bridge/pi/tools.d.ts +36 -0
  32. package/dist/bridge/pi/tools.js +129 -0
  33. package/dist/bundle/ama.cjs +2426 -559
  34. package/dist/cli/main.d.ts +1 -0
  35. package/dist/cli/main.js +7 -0
  36. package/dist/codemode/host-side.d.ts +7 -0
  37. package/dist/codemode/host-side.js +7 -2
  38. package/dist/drivers/catalog.js +1 -1
  39. package/dist/drivers/native/pi-extension.d.ts +39 -0
  40. package/dist/drivers/native/pi-extension.js +100 -0
  41. package/dist/drivers/native/pi-rpc-core.d.ts +49 -0
  42. package/dist/drivers/native/pi-rpc-core.js +108 -0
  43. package/dist/drivers/native/pi-rpc.d.ts +4 -1
  44. package/dist/drivers/native/pi-rpc.js +40 -76
  45. package/dist/i18n/catalog.d.ts +56 -0
  46. package/dist/i18n/catalog.js +3 -0
  47. package/dist/i18n/messages/bridge.d.ts +123 -0
  48. package/dist/i18n/messages/bridge.js +123 -0
  49. package/dist/i18n/messages/cli-help.js +4 -0
  50. package/docs/README.md +6 -6
  51. package/docs/en/README.md +1 -1
  52. package/docs/en/reference/acp.md +87 -9
  53. package/docs/guides/agents.md +2 -0
  54. package/docs/reference/acp.md +87 -9
  55. package/package.json +1 -1
  56. package/dist/drivers/native/pi-gate.d.ts +0 -19
  57. package/dist/drivers/native/pi-gate.js +0 -39
package/CHANGELOG.md CHANGED
@@ -5,6 +5,22 @@ English · [简体中文](CHANGELOG.zh-CN.md)
5
5
  > This file is in English starting with 0.6.0. Release notes for 0.1 through 0.5.1 are in Chinese in
6
6
  > [CHANGELOG.zh-CN.md](CHANGELOG.zh-CN.md). New entries go into both files.
7
7
 
8
+ ## 0.14.0 (2026-11-05)
9
+
10
+ ### ACP
11
+
12
+ - **A Pi bridge, `ama --mode acp --bridge pi`**: ama itself exposes the locally installed Pi as an ACP agent, with no ama key or model needed (it branches off before bootstrap). Each ACP session is one `pi --mode rpc --session-id <id>` child; `session/new` / `load` / `resume` / `close` / `prompt` / `cancel`, the `model` and `thought_level` config options, and `usage_update` with cost (from 1.5.0 it says whether the cost is a billed total). The four permission modes (`default` / `auto-edit` / `plan` / `full-auto`) are implemented by the approval gate and `session/set_mode` takes effect at once; requests that need a human go out as `session/request_permission` and the bridge never answers for the user. The client's `mcpServers` (stdio and http) are handed to Pi's built-in MCP, and the client's own tools carry `_meta.is_mcp_tool_call`. Arguments after `--` pass through to pi (flags the bridge manages are refused). Todos become `plan`, subagent calls carry `_meta["armadra.subagent"]`, and other extensions' dialogs become `elicitation/create`. It adapts to the Pi version: floor 1.1.0 (older answers `pi_outdated`), verified range `>=1.1.0 <2.0.0`, newer or unreadable versions run with a notice (`_meta["armadra.pi"].verified: false`), and only commands in the capability table are sent. The bridge registers no todo or questionnaire tools of its own; when Pi lacks them the gap shows in `_meta["armadra.pi"].capabilities`. See [docs/reference/acp.md](docs/reference/acp.md) (#254).
13
+
14
+ ### External agents
15
+
16
+ - **The pi driver and the bridge share one extension**: `pi-gate` is merged into `pi-extension` (besides the approval gate it hands over MCP servers, reports capabilities and returns refusal reasons); request correlation, the command table and the 10 s deadline for non-prompt commands of `pi-rpc` moved into `pi-rpc-core`. Driver behaviour is unchanged (#254).
17
+
18
+ ## 0.13.0 (2026-11-04)
19
+
20
+ ### Maintenance
21
+
22
+ - **Maintenance release aligned with the unified version**: no user-visible changes. Timing-sensitive tests (scripted provider, background approval docking, the code-mode sandbox timeout, session stats and trace generation) now use gates and manual clocks instead of wall-clock sleeps and absolute time limits; the absolute limits are asserted only with `AMA_PERF=1` (#249, #250).
23
+
8
24
  ## 0.12.0 (2026-11-02)
9
25
 
10
26
  ### Tools and permissions
@@ -4,6 +4,22 @@
4
4
 
5
5
  > 从 0.6.0 起 [CHANGELOG.md](CHANGELOG.md) 为英文,本文件保留中文记录(0.1–0.5.1 的完整历史在此)。新条目两份都要加。
6
6
 
7
+ ## 0.14.0(2026-11-05)
8
+
9
+ ### ACP
10
+
11
+ - **Pi 的 ACP 桥 `ama --mode acp --bridge pi`**:ama 自己把本机装的 Pi 暴露为 ACP Agent,不需要 ama 的 key 与模型(bootstrap 之前分流)。一个 ACP 会话对应一个 `pi --mode rpc --session-id <id>` 子进程;`session/new` / `load` / `resume` / `close` / `prompt` / `cancel`,`model` 与 `thought_level` 配置项,带费用的 `usage_update`(≥1.5.0 标费用是不是账单值);四个权限模式(`default` / `auto-edit` / `plan` / `full-auto`)经审批闸实现、`session/set_mode` 立即生效,需要人的请求发 `session/request_permission`,桥从不代答;客户端的 `mcpServers`(stdio 与 http)交给 Pi 内置的 MCP,自家工具带 `_meta.is_mcp_tool_call`;`--` 之后的参数原样交给 pi(桥自管的旗标拒绝);待办 → `plan`、子 Agent 的 `_meta["armadra.subagent"]`、其它扩展的对话框 → `elicitation/create`。按 Pi 版本适配:下限 1.1.0(更低答 `pi_outdated`)、已验证区间 `>=1.1.0 <2.0.0`、更高或读不出版本照跑并提示(`_meta["armadra.pi"].verified: false`),只发能力表里有的命令。桥不注册自己的 todo / 问卷工具,Pi 里没有对应工具时缺口体现在 `_meta["armadra.pi"].capabilities`。见 [docs/reference/acp.md](docs/reference/acp.md)(#254)。
12
+
13
+ ### 外部 Agent
14
+
15
+ - **pi 驱动与桥共用一份扩展**:`pi-gate` 并入 `pi-extension`(审批闸之外加 MCP 透传、能力报告与拒绝理由);`pi-rpc` 的请求关联、命令表与非 prompt 命令的 10 秒截止拆成 `pi-rpc-core`。驱动的行为不变(#254)。
16
+
17
+ ## 0.13.0(2026-11-04)
18
+
19
+ ### 维护
20
+
21
+ - **与统一版本对齐的维护版**:没有用户可见的变化。对时间敏感的测试(脚本化供应商、后台审批停靠、代码模式沙箱超时、会话统计与轨迹生成)改用闸门与手动时钟,不再靠墙钟睡眠与绝对时限;绝对时限只在 `AMA_PERF=1` 时断言(#249、#250)。
22
+
7
23
  ## 0.12.0(2026-11-02)
8
24
 
9
25
  ### 工具与权限
@@ -54,12 +54,18 @@ export declare class FakeProvider {
54
54
  private readonly recordFile;
55
55
  private readonly keepCalls;
56
56
  private count;
57
+ private readonly gates;
57
58
  constructor(script?: FakeScript | FakeResponse[] | (() => FakeScript | undefined), options?: FakeProviderOptions);
58
59
  readonly api: ApiImplementation;
59
60
  get callCount(): number;
60
61
  setScript(script: FakeScript | FakeResponse[]): void;
61
62
  append(...responses: FakeResponse[]): void;
62
63
  reset(): void;
64
+ private gate;
65
+ /** 放行名为 `name` 的闸门(`FakeResponse.gate`):正在等的调用继续,之后到达的也直接通过。 */
66
+ release(name: string): void;
67
+ /** 正停在闸门 `name` 上的调用个数(测试确认「请求已发出、正在等」)。 */
68
+ waitingAt(name: string): number;
63
69
  private ensureLoaded;
64
70
  /** 第 index 次调用的响应;undefined = 回显。 */
65
71
  private responseFor;
@@ -96,6 +96,21 @@ function sleep(ms, signal) {
96
96
  function tick() {
97
97
  return new Promise((resolve) => setImmediate(resolve));
98
98
  }
99
+ /** 等闸门放行;abort 时提前返回(随后由 checkAbort 收尾)。 */
100
+ function waitOpen(open, signal) {
101
+ return new Promise((resolve) => {
102
+ if (signal.aborted) {
103
+ resolve();
104
+ return;
105
+ }
106
+ const done = () => {
107
+ signal.removeEventListener("abort", done);
108
+ resolve();
109
+ };
110
+ signal.addEventListener("abort", done, { once: true });
111
+ void open.then(done);
112
+ });
113
+ }
99
114
  class AbortedError extends Error {
100
115
  }
101
116
  export class FakeProvider {
@@ -106,6 +121,7 @@ export class FakeProvider {
106
121
  recordFile;
107
122
  keepCalls;
108
123
  count = 0;
124
+ gates = new Map();
109
125
  constructor(script, options = {}) {
110
126
  if (typeof script === "function")
111
127
  this.scriptLoader = script;
@@ -134,6 +150,25 @@ export class FakeProvider {
134
150
  reset() {
135
151
  this.calls.length = 0;
136
152
  this.count = 0;
153
+ this.gates.clear();
154
+ }
155
+ gate(name) {
156
+ let gate = this.gates.get(name);
157
+ if (gate === undefined) {
158
+ let release;
159
+ const open = new Promise((resolve) => (release = resolve));
160
+ gate = { open, release, waiting: 0 };
161
+ this.gates.set(name, gate);
162
+ }
163
+ return gate;
164
+ }
165
+ /** 放行名为 `name` 的闸门(`FakeResponse.gate`):正在等的调用继续,之后到达的也直接通过。 */
166
+ release(name) {
167
+ this.gate(name).release();
168
+ }
169
+ /** 正停在闸门 `name` 上的调用个数(测试确认「请求已发出、正在等」)。 */
170
+ waitingAt(name) {
171
+ return this.gates.get(name)?.waiting ?? 0;
137
172
  }
138
173
  ensureLoaded() {
139
174
  if (this.loaded)
@@ -202,6 +237,17 @@ export class FakeProvider {
202
237
  };
203
238
  try {
204
239
  options.onPayload?.({ fake: true, model: model.id, messages: context.messages.length });
240
+ if (response.gate !== undefined) {
241
+ const gate = this.gate(response.gate);
242
+ gate.waiting++;
243
+ try {
244
+ await waitOpen(gate.open, signal);
245
+ }
246
+ finally {
247
+ gate.waiting--;
248
+ }
249
+ checkAbort();
250
+ }
205
251
  if (response.delayMs)
206
252
  await sleep(response.delayMs, signal);
207
253
  checkAbort();
@@ -9,7 +9,8 @@
9
9
  * { "error": { "kind": "rate_limit" } },
10
10
  * { "text": "完成", "usage": { "input": 1200, "output": 30 } },
11
11
  * { "steps": [{ "text": "写到一半" }], "error": { "kind": "disconnect" } },
12
- * { "delayMs": 500, "text": "慢响应", "stopReason": "length" }
12
+ * { "delayMs": 500, "text": "慢响应", "stopReason": "length" },
13
+ * { "gate": "reply", "text": "等放行(进程内测试 provider.release('reply'))" }
13
14
  * ] }
14
15
  * ```
15
16
  *
@@ -57,6 +58,12 @@ export interface FakeResponse {
57
58
  error?: FakeError;
58
59
  /** start 之前的延迟(毫秒,可被 abort 打断)。 */
59
60
  delayMs?: number;
61
+ /**
62
+ * start 之前的闸门:这次调用停在闸门上,直到进程内的 `FakeProvider.release(gate)` 放行(可被 abort 打断;
63
+ * 先放行再调用也立即通过)。测试用它显式控制「谁先回」,不靠 `delayMs` 的墙钟。脚本文件里没人放行,
64
+ * 仅用于进程内测试;与 `delayMs` 同给时先过闸门再延迟。
65
+ */
66
+ gate?: string;
60
67
  }
61
68
  export type FakeExhaustedBehavior = "echo" | "repeat-last" | "error";
62
69
  export interface FakeScript {
@@ -9,7 +9,8 @@
9
9
  * { "error": { "kind": "rate_limit" } },
10
10
  * { "text": "完成", "usage": { "input": 1200, "output": 30 } },
11
11
  * { "steps": [{ "text": "写到一半" }], "error": { "kind": "disconnect" } },
12
- * { "delayMs": 500, "text": "慢响应", "stopReason": "length" }
12
+ * { "delayMs": 500, "text": "慢响应", "stopReason": "length" },
13
+ * { "gate": "reply", "text": "等放行(进程内测试 provider.release('reply'))" }
13
14
  * ] }
14
15
  * ```
15
16
  *
@@ -65,6 +66,9 @@ function checkResponse(value, path) {
65
66
  if (value["text"] !== undefined && typeof value["text"] !== "string") {
66
67
  fail(`${path}.text`, "must be a string");
67
68
  }
69
+ if (value["gate"] !== undefined && typeof value["gate"] !== "string") {
70
+ fail(`${path}.gate`, "must be a string");
71
+ }
68
72
  const stop = value["stopReason"];
69
73
  if (stop !== undefined &&
70
74
  stop !== "stop" &&
@@ -0,0 +1,19 @@
1
+ /**
2
+ * `ama --mode acp --bridge pi [--lang zh|en] [-- <pi 的参数>]` 的参数解析(#254)。
3
+ *
4
+ * - `--bridge` 只认 `pi`,且必须配 `--mode acp`;除 `--lang` 外不收别的选项(桥不起 ama 的模型与会话);
5
+ * - `--` 之后的参数原样交给 pi(宿主的 `--skill`、`--append-system-prompt` 注入照用),但桥自己管的旗标
6
+ * 一律拒绝:模式、会话选择与存储、`--tools`、`--no-mcp`、`-p`——放行它们会让审批闸、会话 id 或 MCP 透传失效。
7
+ */
8
+ import { type Locale } from "../../i18n/index.js";
9
+ export interface BridgeArgs {
10
+ bridge: "pi";
11
+ passthrough: string[];
12
+ lang?: Locale;
13
+ }
14
+ /** `--` 之前出现 `--bridge`(含 `--bridge=pi`)。 */
15
+ export declare function hasBridgeFlag(argv: readonly string[]): boolean;
16
+ /** 宽松预扫 `--lang`(`--` 之前,无效值忽略):用法错误信息也要按界面语言出。 */
17
+ export declare function bridgeLangOf(argv: readonly string[]): Locale | undefined;
18
+ /** 解析;用法错误抛 {@link UsageError}(退出码 2)。 */
19
+ export declare function parseBridgeArgs(argv: readonly string[]): BridgeArgs;
@@ -0,0 +1,96 @@
1
+ /**
2
+ * `ama --mode acp --bridge pi [--lang zh|en] [-- <pi 的参数>]` 的参数解析(#254)。
3
+ *
4
+ * - `--bridge` 只认 `pi`,且必须配 `--mode acp`;除 `--lang` 外不收别的选项(桥不起 ama 的模型与会话);
5
+ * - `--` 之后的参数原样交给 pi(宿主的 `--skill`、`--append-system-prompt` 注入照用),但桥自己管的旗标
6
+ * 一律拒绝:模式、会话选择与存储、`--tools`、`--no-mcp`、`-p`——放行它们会让审批闸、会话 id 或 MCP 透传失效。
7
+ */
8
+ import { UsageError } from "../../cli/args.js";
9
+ import { LOCALES, msg } from "../../i18n/index.js";
10
+ /** 桥自己管的 pi 旗标(长名与短名;`--名=值` 形式同样命中)。 */
11
+ const OWNED_FLAGS = new Set([
12
+ "--mode",
13
+ "--session",
14
+ "--session-id",
15
+ "--session-dir",
16
+ "--continue",
17
+ "-c",
18
+ "--resume",
19
+ "-r",
20
+ "--fork",
21
+ "--no-session",
22
+ "--tools",
23
+ "-t",
24
+ "--no-mcp",
25
+ "--print",
26
+ "-p",
27
+ ]);
28
+ /** `--` 之前出现 `--bridge`(含 `--bridge=pi`)。 */
29
+ export function hasBridgeFlag(argv) {
30
+ for (const token of argv) {
31
+ if (token === "--")
32
+ return false;
33
+ if (token === "--bridge" || token.startsWith("--bridge="))
34
+ return true;
35
+ }
36
+ return false;
37
+ }
38
+ /** 宽松预扫 `--lang`(`--` 之前,无效值忽略):用法错误信息也要按界面语言出。 */
39
+ export function bridgeLangOf(argv) {
40
+ let lang;
41
+ for (let i = 0; i < argv.length && argv[i] !== "--"; i++) {
42
+ const token = argv[i] ?? "";
43
+ const value = token === "--lang" ? argv[i + 1] : token.startsWith("--lang=") ? token.slice(7) : undefined;
44
+ if (value !== undefined && LOCALES.includes(value))
45
+ lang = value;
46
+ }
47
+ return lang;
48
+ }
49
+ /** 解析;用法错误抛 {@link UsageError}(退出码 2)。 */
50
+ export function parseBridgeArgs(argv) {
51
+ const m = msg().bridge.args;
52
+ let bridge;
53
+ let mode;
54
+ let lang;
55
+ let passthrough = [];
56
+ const valueOf = (name, inline, at) => {
57
+ if (inline !== undefined)
58
+ return [inline, at];
59
+ const next = argv[at + 1];
60
+ if (next === undefined)
61
+ throw new UsageError(m.needsValue(name));
62
+ return [next, at + 1];
63
+ };
64
+ for (let i = 0; i < argv.length; i++) {
65
+ const token = argv[i] ?? "";
66
+ if (token === "--") {
67
+ passthrough = argv.slice(i + 1).map(String);
68
+ break;
69
+ }
70
+ const eq = token.indexOf("=");
71
+ const name = token.startsWith("--") && eq !== -1 ? token.slice(0, eq) : token;
72
+ const inline = token.startsWith("--") && eq !== -1 ? token.slice(eq + 1) : undefined;
73
+ if (name !== "--bridge" && name !== "--mode" && name !== "--lang")
74
+ throw new UsageError(m.unknownOption(token));
75
+ const [value, next] = valueOf(name, inline, i);
76
+ i = next;
77
+ if (name === "--bridge")
78
+ bridge = value;
79
+ else if (name === "--mode")
80
+ mode = value;
81
+ else if (LOCALES.includes(value))
82
+ lang = value;
83
+ else
84
+ throw new UsageError(m.unknownOption(`--lang ${value}`));
85
+ }
86
+ if (bridge !== "pi")
87
+ throw new UsageError(m.unsupportedBridge(bridge ?? ""));
88
+ if (mode !== "acp")
89
+ throw new UsageError(m.needsAcpMode);
90
+ for (const token of passthrough) {
91
+ const flag = token.startsWith("--") ? token.split("=")[0] : token;
92
+ if (OWNED_FLAGS.has(flag))
93
+ throw new UsageError(m.ownedFlag(flag));
94
+ }
95
+ return { bridge: "pi", passthrough, ...(lang !== undefined ? { lang } : {}) };
96
+ }
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Pi 版本能力表(#254):桥只发表里有的 RPC 命令、只开表里起始版本不高于已装版本的可选能力。
3
+ *
4
+ * - 下限 {@link PI_FLOOR}:必需能力里起始版本最晚的一项(`agent_settled.aborted`,1.1.0),低于它拒绝;
5
+ * - 已验证区间 {@link PI_VERIFIED}:区间内按表开关可选能力;高于区间或读不出版本:照跑,按最高已知能力处理,
6
+ * 由调用方给出提示;
7
+ * - 永不发表外命令:旧版 Pi 对未知命令的应答不带 id,请求无从关联(Armadra#710 的根因)。
8
+ */
9
+ export declare const PI_FLOOR = "1.1.0";
10
+ export declare const PI_VERIFIED = ">=1.1.0 <2.0.0";
11
+ export declare const PI_PACKAGE = "@earendil-works/pi-coding-agent";
12
+ /** 能力 id → 起始版本(含)。 */
13
+ export declare const PI_CAPABILITY_SINCE: {
14
+ /** `--session-id`:新开与接回都用它。 */
15
+ readonly sessionId: "0.76.0";
16
+ /** `agent_settled` 事件:判定回合结束。 */
17
+ readonly agentSettled: "0.80.4";
18
+ /** `agent_settled.aborted`:判定取消。 */
19
+ readonly settledAborted: "1.1.0";
20
+ /** `get_available_thinking_levels`:thought_level 配置项。 */
21
+ readonly thinkingLevels: "0.81.0";
22
+ /** `prompt` 应答里的 `disposition`:`handled` 时不等回合。 */
23
+ readonly disposition: "0.99.0";
24
+ /** 内置 MCP、`pi.registerMcpServer`、工具 `annotations`。 */
25
+ readonly mcp: "0.99.0";
26
+ /** `get_session_stats` 的 `cost` / `contextUsage`。 */
27
+ readonly sessionStats: "0.63.0";
28
+ /** `Usage.cost.totalFromProvider`:区分账单值与价目表估算。 */
29
+ readonly costFromProvider: "1.5.0";
30
+ };
31
+ export type PiCapabilityId = keyof typeof PI_CAPABILITY_SINCE;
32
+ export type PiCapabilities = Readonly<Record<PiCapabilityId, boolean>>;
33
+ /** 桥会发的 RPC 命令 → 起始版本(都不晚于下限,除 thinking 外)。 */
34
+ export declare const PI_COMMAND_SINCE: {
35
+ readonly get_state: "0.76.0";
36
+ readonly get_messages: "0.76.0";
37
+ readonly prompt: "0.76.0";
38
+ readonly steer: "0.76.0";
39
+ readonly abort: "0.76.0";
40
+ readonly set_model: "0.76.0";
41
+ readonly get_available_models: "0.76.0";
42
+ readonly set_thinking_level: "0.76.0";
43
+ readonly get_available_thinking_levels: "0.81.0";
44
+ readonly get_session_stats: "0.63.0";
45
+ };
46
+ export type PiCommandName = keyof typeof PI_COMMAND_SINCE;
47
+ /** 版本 → 能力判定的结果。 */
48
+ export interface PiVersionInfo {
49
+ /** `unsupported` 低于下限;`verified` 在已验证区间内;`newer` 高于区间;`unknown` 读不出版本。 */
50
+ status: "unsupported" | "verified" | "newer" | "unknown";
51
+ version?: string;
52
+ capabilities: PiCapabilities;
53
+ /** 可发的命令。 */
54
+ commands: ReadonlySet<string>;
55
+ }
56
+ /** 按版本求能力与可发命令;`undefined`(读不出)与高于区间一样按最高已知能力。 */
57
+ export declare function piVersionInfo(version: string | undefined): PiVersionInfo;
@@ -0,0 +1,65 @@
1
+ /**
2
+ * Pi 版本能力表(#254):桥只发表里有的 RPC 命令、只开表里起始版本不高于已装版本的可选能力。
3
+ *
4
+ * - 下限 {@link PI_FLOOR}:必需能力里起始版本最晚的一项(`agent_settled.aborted`,1.1.0),低于它拒绝;
5
+ * - 已验证区间 {@link PI_VERIFIED}:区间内按表开关可选能力;高于区间或读不出版本:照跑,按最高已知能力处理,
6
+ * 由调用方给出提示;
7
+ * - 永不发表外命令:旧版 Pi 对未知命令的应答不带 id,请求无从关联(Armadra#710 的根因)。
8
+ */
9
+ import { versionSatisfies } from "../../drivers/probe.js";
10
+ export const PI_FLOOR = "1.1.0";
11
+ export const PI_VERIFIED = ">=1.1.0 <2.0.0";
12
+ export const PI_PACKAGE = "@earendil-works/pi-coding-agent";
13
+ /** 能力 id → 起始版本(含)。 */
14
+ export const PI_CAPABILITY_SINCE = {
15
+ /** `--session-id`:新开与接回都用它。 */
16
+ sessionId: "0.76.0",
17
+ /** `agent_settled` 事件:判定回合结束。 */
18
+ agentSettled: "0.80.4",
19
+ /** `agent_settled.aborted`:判定取消。 */
20
+ settledAborted: "1.1.0",
21
+ /** `get_available_thinking_levels`:thought_level 配置项。 */
22
+ thinkingLevels: "0.81.0",
23
+ /** `prompt` 应答里的 `disposition`:`handled` 时不等回合。 */
24
+ disposition: "0.99.0",
25
+ /** 内置 MCP、`pi.registerMcpServer`、工具 `annotations`。 */
26
+ mcp: "0.99.0",
27
+ /** `get_session_stats` 的 `cost` / `contextUsage`。 */
28
+ sessionStats: "0.63.0",
29
+ /** `Usage.cost.totalFromProvider`:区分账单值与价目表估算。 */
30
+ costFromProvider: "1.5.0",
31
+ };
32
+ /** 桥会发的 RPC 命令 → 起始版本(都不晚于下限,除 thinking 外)。 */
33
+ export const PI_COMMAND_SINCE = {
34
+ get_state: "0.76.0",
35
+ get_messages: "0.76.0",
36
+ prompt: "0.76.0",
37
+ steer: "0.76.0",
38
+ abort: "0.76.0",
39
+ set_model: "0.76.0",
40
+ get_available_models: "0.76.0",
41
+ set_thinking_level: "0.76.0",
42
+ get_available_thinking_levels: PI_CAPABILITY_SINCE.thinkingLevels,
43
+ get_session_stats: PI_CAPABILITY_SINCE.sessionStats,
44
+ };
45
+ function atLeast(version, since) {
46
+ return versionSatisfies(version, `>=${since}`);
47
+ }
48
+ /** 按版本求能力与可发命令;`undefined`(读不出)与高于区间一样按最高已知能力。 */
49
+ export function piVersionInfo(version) {
50
+ const known = version !== undefined;
51
+ const has = (since) => !known || atLeast(version, since);
52
+ const capabilities = Object.fromEntries(Object.keys(PI_CAPABILITY_SINCE).map((id) => [
53
+ id,
54
+ has(PI_CAPABILITY_SINCE[id]),
55
+ ]));
56
+ const commands = new Set(Object.keys(PI_COMMAND_SINCE).filter((c) => has(PI_COMMAND_SINCE[c])));
57
+ const status = !known
58
+ ? "unknown"
59
+ : !atLeast(version, PI_FLOOR)
60
+ ? "unsupported"
61
+ : versionSatisfies(version, PI_VERIFIED)
62
+ ? "verified"
63
+ : "newer";
64
+ return { status, ...(known ? { version } : {}), capabilities, commands };
65
+ }
@@ -0,0 +1,28 @@
1
+ /**
2
+ * 桥的会话配置项(#254):`model` 与 `thought_level` 两个 select。
3
+ *
4
+ * - `model`:值 `provider/id`(`get_available_models`),按供应商分组;当前模型不在列表里时补一项;
5
+ * - `thought_level`:值是 Pi 的级别(`get_available_thinking_levels`,`off`…`max`),是唯一的推理强度控件,
6
+ * 不进 `modes`;当前模型只支持 `off`(不支持推理)时不给这一项。
7
+ */
8
+ import type { AcpSessionConfigOption } from "../../drivers/acp/types.js";
9
+ export declare const CONFIG_IDS: {
10
+ readonly model: "model";
11
+ readonly thoughtLevel: "thought_level";
12
+ };
13
+ export interface PiModelRef {
14
+ provider: string;
15
+ id: string;
16
+ name?: string;
17
+ }
18
+ /** 配置项依赖的 Pi 状态快照。 */
19
+ export interface ConfigState {
20
+ models: readonly PiModelRef[];
21
+ current?: PiModelRef;
22
+ /** `get_available_thinking_levels`(读不到为空)。 */
23
+ levels: readonly string[];
24
+ level?: string;
25
+ }
26
+ export declare function modelRefOf(value: unknown): PiModelRef | undefined;
27
+ export declare const modelValue: (m: PiModelRef) => string;
28
+ export declare function buildConfigOptions(state: ConfigState): AcpSessionConfigOption[];
@@ -0,0 +1,67 @@
1
+ /**
2
+ * 桥的会话配置项(#254):`model` 与 `thought_level` 两个 select。
3
+ *
4
+ * - `model`:值 `provider/id`(`get_available_models`),按供应商分组;当前模型不在列表里时补一项;
5
+ * - `thought_level`:值是 Pi 的级别(`get_available_thinking_levels`,`off`…`max`),是唯一的推理强度控件,
6
+ * 不进 `modes`;当前模型只支持 `off`(不支持推理)时不给这一项。
7
+ */
8
+ import { msg } from "../../i18n/index.js";
9
+ export const CONFIG_IDS = { model: "model", thoughtLevel: "thought_level" };
10
+ export function modelRefOf(value) {
11
+ if (value === null || typeof value !== "object")
12
+ return undefined;
13
+ const m = value;
14
+ if (typeof m["id"] !== "string" || typeof m["provider"] !== "string")
15
+ return undefined;
16
+ return {
17
+ provider: m["provider"],
18
+ id: m["id"],
19
+ ...(typeof m["name"] === "string" && m["name"] !== "" ? { name: m["name"] } : {}),
20
+ };
21
+ }
22
+ export const modelValue = (m) => `${m.provider}/${m.id}`;
23
+ function levelName(level) {
24
+ const names = msg().bridge.config.levels;
25
+ return names[level] ?? level;
26
+ }
27
+ export function buildConfigOptions(state) {
28
+ const m = msg().bridge.config;
29
+ const options = [];
30
+ const models = [...state.models];
31
+ if (state.current !== undefined &&
32
+ !models.some((x) => modelValue(x) === modelValue(state.current)))
33
+ models.unshift(state.current);
34
+ if (state.current !== undefined) {
35
+ const groups = new Map();
36
+ for (const model of models) {
37
+ let group = groups.get(model.provider);
38
+ if (group === undefined) {
39
+ group = { group: model.provider, name: model.provider, options: [] };
40
+ groups.set(model.provider, group);
41
+ }
42
+ group.options.push({ value: modelValue(model), name: model.name ?? model.id });
43
+ }
44
+ options.push({
45
+ id: CONFIG_IDS.model,
46
+ name: m.model,
47
+ category: "model",
48
+ type: "select",
49
+ currentValue: modelValue(state.current),
50
+ options: [...groups.values()],
51
+ });
52
+ }
53
+ const levels = state.levels.filter((l) => l !== "");
54
+ if (levels.some((l) => l !== "off")) {
55
+ options.push({
56
+ id: CONFIG_IDS.thoughtLevel,
57
+ name: m.thoughtLevel,
58
+ category: "thought_level",
59
+ type: "select",
60
+ currentValue: state.level !== undefined && levels.includes(state.level)
61
+ ? state.level
62
+ : (levels[0] ?? "off"),
63
+ options: levels.map((level) => ({ value: level, name: levelName(level) })),
64
+ });
65
+ }
66
+ return options;
67
+ }
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Pi 扩展的对话框 → ACP `elicitation/create`(#254)。
3
+ *
4
+ * 客户端声明了 `elicitation` 时,其它扩展经 RPC 发来的 select / confirm / input / editor 转成 form 型征询:
5
+ * - select → 带标题的枚举(`oneOf`);confirm → boolean;input / editor → string(editor 的预填作默认值);
6
+ * - `accept` → 把值回给 Pi;`decline` / `cancel` / 出错 → Pi 一侧 `cancelled: true`;
7
+ * - 桥从不替人填表,也不挑选项。
8
+ *
9
+ * ama 自己的对话框(审批闸、拒绝理由、能力报告)不走这里;桥也不注册自己的 todo / 问卷工具——Pi 里没有对应
10
+ * 扩展时,什么也不加,缺口体现在 `_meta["armadra.pi"].capabilities`。
11
+ */
12
+ import type { AcpElicitationSchema } from "../../drivers/acp/types.js";
13
+ import type { Json } from "./tools.js";
14
+ export interface ElicitationPlan {
15
+ /** 给人看的说明(Pi 扩展给的标题,confirm 附上正文)。 */
16
+ message: string;
17
+ schema: AcpElicitationSchema;
18
+ /** `accept` 的内容 → 回给 Pi 的 `extension_ui_response` 正文;内容不合就取消。 */
19
+ toResponse(content: Record<string, unknown> | null | undefined): Json;
20
+ }
21
+ /** 为一个 Pi 对话框请求建征询;不是四种对话框、或没有可选项的 select 返回 undefined(按取消处理)。 */
22
+ export declare function planElicitation(record: Json): ElicitationPlan | undefined;
23
+ export declare const CANCELLED_RESPONSE: Json;
@@ -0,0 +1,86 @@
1
+ /**
2
+ * Pi 扩展的对话框 → ACP `elicitation/create`(#254)。
3
+ *
4
+ * 客户端声明了 `elicitation` 时,其它扩展经 RPC 发来的 select / confirm / input / editor 转成 form 型征询:
5
+ * - select → 带标题的枚举(`oneOf`);confirm → boolean;input / editor → string(editor 的预填作默认值);
6
+ * - `accept` → 把值回给 Pi;`decline` / `cancel` / 出错 → Pi 一侧 `cancelled: true`;
7
+ * - 桥从不替人填表,也不挑选项。
8
+ *
9
+ * ama 自己的对话框(审批闸、拒绝理由、能力报告)不走这里;桥也不注册自己的 todo / 问卷工具——Pi 里没有对应
10
+ * 扩展时,什么也不加,缺口体现在 `_meta["armadra.pi"].capabilities`。
11
+ */
12
+ import { oneLine } from "../../drivers/turn.js";
13
+ const CANCELLED = { cancelled: true };
14
+ function label(record, fallback) {
15
+ const title = record["title"];
16
+ return typeof title === "string" && title !== "" ? oneLine(title, 200) : fallback;
17
+ }
18
+ /** 为一个 Pi 对话框请求建征询;不是四种对话框、或没有可选项的 select 返回 undefined(按取消处理)。 */
19
+ export function planElicitation(record) {
20
+ const method = String(record["method"] ?? "");
21
+ const title = label(record, method);
22
+ const string = (v) => (typeof v === "string" ? v : undefined);
23
+ switch (method) {
24
+ case "select": {
25
+ const options = Array.isArray(record["options"])
26
+ ? record["options"].filter((o) => typeof o === "string")
27
+ : [];
28
+ if (options.length === 0)
29
+ return undefined;
30
+ return {
31
+ message: title,
32
+ schema: {
33
+ type: "object",
34
+ properties: {
35
+ value: { type: "string", title, oneOf: options.map((o) => ({ const: o, title: o })) },
36
+ },
37
+ required: ["value"],
38
+ },
39
+ toResponse: (content) => {
40
+ const value = content?.["value"];
41
+ return typeof value === "string" && options.includes(value) ? { value } : CANCELLED;
42
+ },
43
+ };
44
+ }
45
+ case "confirm": {
46
+ const body = string(record["message"]);
47
+ return {
48
+ message: body !== undefined && body !== "" ? `${title}\n${oneLine(body, 400)}` : title,
49
+ schema: {
50
+ type: "object",
51
+ properties: { confirmed: { type: "boolean", title, default: false } },
52
+ required: ["confirmed"],
53
+ },
54
+ toResponse: (content) => ({ confirmed: content?.["confirmed"] === true }),
55
+ };
56
+ }
57
+ case "input":
58
+ case "editor": {
59
+ const hint = string(record[method === "input" ? "placeholder" : "prefill"]);
60
+ return {
61
+ message: title,
62
+ schema: {
63
+ type: "object",
64
+ properties: {
65
+ value: {
66
+ type: "string",
67
+ title,
68
+ ...(method === "input" && hint !== undefined && hint !== ""
69
+ ? { description: oneLine(hint, 200) }
70
+ : {}),
71
+ ...(method === "editor" && hint !== undefined ? { default: hint } : {}),
72
+ },
73
+ },
74
+ required: ["value"],
75
+ },
76
+ toResponse: (content) => {
77
+ const value = content?.["value"];
78
+ return typeof value === "string" ? { value } : CANCELLED;
79
+ },
80
+ };
81
+ }
82
+ default:
83
+ return undefined;
84
+ }
85
+ }
86
+ export const CANCELLED_RESPONSE = CANCELLED;
@@ -0,0 +1,14 @@
1
+ /**
2
+ * `ama --mode acp --bridge pi` 的进程入口(#254)。
3
+ *
4
+ * bootstrap 之前分流:不解析供应商与模型,不需要 ama 的 key 或会话;stdin / stdout 是 ACP(NDJSON),
5
+ * 诊断写 stderr。退出语义同 `--mode acp`:stdin 关闭 → 收掉全部 Pi 子进程 → 退出 0;SIGINT / SIGTERM → 130 / 143。
6
+ */
7
+ import type { CliIo } from "../../cli/deps.js";
8
+ import { type BridgeDeps } from "./server.js";
9
+ export interface BridgeRunOptions {
10
+ stdin?: NodeJS.ReadableStream;
11
+ stdout?: NodeJS.WritableStream;
12
+ deps?: BridgeDeps;
13
+ }
14
+ export declare function runBridge(argv: readonly string[], io: Pick<CliIo, "stderr" | "env">, options?: BridgeRunOptions): Promise<number>;