@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
@@ -7,7 +7,7 @@ English · [简体中文](../../reference/acp.md)
7
7
 
8
8
  ama works on both sides of ACP:
9
9
 
10
- - **Agent**: `ama --mode acp` exposes ama as an ACP agent for Zed, JetBrains and Armadra's ACP nodes;
10
+ - **Agent**: `ama --mode acp` exposes ama as an ACP agent for Zed, JetBrains and Armadra's ACP nodes; `ama --mode acp --bridge pi` exposes the locally installed Pi as an ACP agent (see below);
11
11
  - **Client**: ama drives external agents over ACP (native ACP agents such as Gemini CLI, OpenCode, Kimi and Copilot, or Claude Code / Codex with an adapter installed); see [agents.md](../../guides/agents.md) (Chinese).
12
12
 
13
13
  The protocol stack is hand-written with no dependencies and maintained in one place: `@armadra/agent/acp` exports the types, framing, client and fake agent, and Armadra reuses them directly. The design rationale is in [wave5-plan.md](../../history/wave5-plan.md) §5 (D14, Chinese).
@@ -130,18 +130,96 @@ Session-open answers (new / load / resume) carry `configOptions`, followed by an
130
130
 
131
131
  After stdin closes, ama waits for the run in progress to finish, then exits (0); from the first start-up step the stdout of the `ama` process carries only the protocol (`console.log` while hosts / hooks load is redirected to stderr); after SIGINT / SIGTERM it exits 130 / 143. Hosts see the mode as `rpc` (`HostApi.mode`). When the SDK calls `bootstrap(--mode acp)` directly it gets a Runtime with `mode: "rpc"` and hands it to `runAcpMode`.
132
132
 
133
+ ## `ama --mode acp --bridge pi`
134
+
135
+ ```sh
136
+ ama --mode acp --bridge pi [--lang zh|en] [-- <pi arguments>]
137
+ ```
138
+
139
+ Exposes the locally installed [Pi](https://github.com/earendil-works/pi) (`@earendil-works/pi-coding-agent`) as an ACP agent for Armadra and other ACP clients. The entry point branches off **before** bootstrap: it resolves no provider or model, needs no ama key and reads or writes no ama sessions. `--bridge` accepts only `pi` and requires `--mode acp`. The bridge sends no model requests itself, so the prompt-byte rules are unaffected.
140
+
141
+ **Processes and sessions**: each ACP session is one `pi --mode rpc --session-id <id> -e <bridge extension>` child; the ACP `sessionId` is Pi's session id, and the child's working directory is the `cwd` of `session/new` (it must be absolute). Arguments after `--` are passed to pi unchanged (`--skill`, `--append-system-prompt` and so on keep working), but flags the bridge manages are always refused (exit code 2): `--mode`, `--session*`, `-c` / `--continue`, `-r` / `--resume`, `--fork`, `--no-session`, `--tools` / `-t`, `--no-mcp`, `-p` / `--print`. When stdin closes, all pi children are closed and the bridge exits 0. The bridge neither reads nor writes Pi's configuration or login (`auth.json`, `mcp.json`, `settings.json`); sign in with `/login` inside Pi.
142
+
143
+ | Method | What the bridge does |
144
+ | --------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
145
+ | `initialize` | `loadSession: true`, `sessionCapabilities: { resume, close }` (no `list`: Pi's RPC has no command to list sessions and the bridge does not read Pi's session directory), `mcpCapabilities: { http: true, sse: false }`, `promptCapabilities: { image: true, embeddedContext: true }`, `authMethods: []` |
146
+ | `session/new` | Starts a child with a fresh uuid; the result carries `modes`, `configOptions` and `_meta`. If Pi has no usable model, the error has `error.data.code = "pi_no_model"` and asks the user to sign in inside Pi |
147
+ | `session/load` | The same, with the requested id. A session with no messages in Pi (Pi silently creates an empty one) counts as not found: -32002 and the child is closed. Otherwise history is replayed as `user_message_chunk`, `agent_*_chunk` and final-state `tool_call`s, followed by the current plan |
148
+ | `session/resume` | Like load, without the replay |
149
+ | `session/close` | Interrupts, closes the child and deletes the temporary directory |
150
+ | `session/prompt` | Text and embedded resources become the message; images go in `images`. `disposition: handled` does not wait for a turn; `agent_settled.aborted` → `cancelled`, `length` → `max_tokens`, an error → a JSON-RPC error. The result's `usage` is the sum for this turn. A prompt arriving while one runs gets invalid request (busy) |
151
+ | `session/cancel` | Withdraws pending approvals and elicitations, sends `abort` and arms a 15 s watchdog (then closes stdin and ends the process tree); the turn ends with `cancelled` |
152
+ | `session/set_mode` | The four modes below; takes effect immediately |
153
+ | `session/set_config_option` | `model` and `thought_level`, below |
154
+
155
+ ### Pi versions
156
+
157
+ The bridge only sends RPC commands that its capability table lists (old Pi answers an unknown command without an id, so the request cannot be correlated), and every non-prompt command has a 10 s deadline.
158
+
159
+ | Capability | Since | Used for |
160
+ | -------------------------------------------------------- | -------------- | ------------------------------------- |
161
+ | `--session-id` | 0.76.0 | New and resumed sessions |
162
+ | `agent_settled` / its `aborted` | 0.80.4 / 1.1.0 | Detecting turn end and cancel |
163
+ | `get_available_thinking_levels` | 0.81.0 | The `thought_level` option |
164
+ | `disposition` in the `prompt` response | 0.99.0 | Not waiting for a turn when `handled` |
165
+ | Built-in MCP, `pi.registerMcpServer`, tool `annotations` | 0.99.0 | Canvas tools; read-only check in plan |
166
+ | `cost` / `contextUsage` of `get_session_stats` | 0.63.0 | `usage_update` |
167
+ | `Usage.cost.totalFromProvider` | 1.5.0 | Billed cost versus catalog estimate |
168
+
169
+ - **Floor 1.1.0** (the latest `since` among the required capabilities). Older Pi: `session/new` answers `error.data = { code: "pi_outdated", installed, minimum, package }`; a missing `pi` answers `{ code: "pi_missing", package }`. No child is started in either case.
170
+ - **Verified range `>=1.1.0 <2.0.0`**: optional capabilities follow the table (below 1.5.0, for example, cost is not marked as billed). Tested on Pi 1.5.0.
171
+ - **Above the range, or version unreadable**: runs with the newest known capabilities; one stderr line, and `_meta["armadra.pi"].verified` is `false` in the result so the client can show a notice.
172
+ - The result's `_meta["armadra.pi"] = { version?, verified, capabilities? }`; `capabilities = { todo, subagent, questionnaire }` says whether Pi has a tool with that name (see "Todos, subagents and elicitation").
173
+
174
+ ### Modes and approval
175
+
176
+ Pi has neither permission modes nor an approval channel, so the bridge loads an extension into every run (`-e`, in the session's own `mkdtemp` directory with mode 0600, deleted when the session closes; shared with the external-agent driver, see [agents.md](../../guides/agents.md), Chinese). Before every tool call the extension hands the request to the bridge, and it asks the bridge **every time**, so `session/set_mode` takes effect immediately.
177
+
178
+ | Mode (ids as in ama) | Behaviour |
179
+ | -------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- |
180
+ | `default` | Read-only calls (built-in `read` / `grep` / `find` / `ls`, or MCP tools declaring `readOnlyHint`) pass; everything else goes to the human |
181
+ | `auto-edit` | Also allows `edit` / `write`; the rest goes to the human |
182
+ | `plan` | Read-only passes; everything else is refused and the model is told it is in plan mode (describe the change instead of making it) |
183
+ | `full-auto` | Everything passes, except bash commands matching ama's dangerous-command rules, which still go to the human |
184
+
185
+ Going to the human means `session/request_permission` (`allow_once` / `reject_once`, with `toolCallId`, title, kind, `rawInput` and locations). A call that must pass the gate is announced as `pending` first and becomes `in_progress` once allowed. A `cancelled` outcome, a failing request or a cancelled session all count as a refusal; **the bridge never answers on the human's behalf**.
186
+
187
+ ### Config options and usage
188
+
189
+ - `model`: values are `provider/id` (grouped by provider); after a model change the thinking levels are re-read and a `config_option_update` is sent;
190
+ - `thought_level`: Pi's `off` … `max`; omitted when the current model has no reasoning. It is the only thinking control and is not part of `modes`;
191
+ - `usage_update`: sent when a session opens, after each turn and after a model change; `used` = `contextUsage.tokens`, `size` = the context window, `cost = { amount, currency: "USD" }`. From 1.5.0, `_meta["armadra.pi"].fromProvider` says whether the cost is a billed total.
192
+
193
+ ### MCP
194
+
195
+ The client's `mcpServers` (stdio and http; sse is skipped with one stderr line) are written to `bridge.json` in the session's temporary directory. When the extension loads it calls `pi.registerMcpServer(name, { …, exposure: "direct" })` for each one: ama itself speaks no MCP, Pi's built-in MCP makes the connections. Pi server names may only contain letters, digits, `_` and `-`; anything else becomes `_`. A server of the same name in the user's `mcp.json` wins under Pi's own rules.
196
+
197
+ `tool_call`s and permission requests for `mcp__<server>__<tool>` (only servers the bridge registered are recognised) carry `_meta.is_mcp_tool_call: true` and `rawInput { server, tool, arguments }`, so the client can recognise its own tools. Inner calls made from code mode carry `_meta.ama.parentToolCallId`.
198
+
199
+ ### Todos, subagents and elicitation
200
+
201
+ The bridge **registers no todo or questionnaire tools of its own**: these appear only when the installed Pi already has the matching tool or extension. When it does not, nothing is added and the gap shows up in `_meta["armadra.pi"].capabilities`.
202
+
203
+ - **Todos → `plan`**: every tool result with `details.todos` (`{text, done}` or `{content, status}`) sends the whole list as a `plan`; the `todo` tool has kind `think`;
204
+ - **Subagents**: Pi's `subagent` extension tool is one `tool_call` of kind `other` with `_meta["armadra.subagent"] = { agent, task }` (parallel and chain calls use `{ mode, tasks }`); streamed progress goes into content, and the cost is already part of `get_session_stats`;
205
+ - **Dialogs → `elicitation/create`**: when the client declared `elicitation`, other extensions' select (titled enum), confirm (boolean) and input / editor (string; the editor's prefill is the default) become form elicitations. `accept` hands the value to Pi; `decline`, `cancel` and a value outside the options all end as a cancel on Pi's side, and the bridge never chooses for the user. When Pi gives a `timeout`, the request is withdrawn (`$/cancel_request`) when it expires; a cancelled session withdraws it too. Without the client capability the dialog is cancelled with one stderr line. ama's own dialogs (approval gate, refusal reason, capability report) are not included.
206
+
207
+ ### Tests
208
+
209
+ A scripted fake Pi (`test/helpers/fake-pi.ts`) covers versions 1.0.4 / 1.1.0 / 1.4.0 / 1.5.0 / 2.0.0 / unreadable, the four modes, cancel, MCP, plan, subagents and elicitation. Like old Pi it answers unknown commands without an id, and the tests assert that the bridge never sends one. Every wire message passes the official schema (`test/helpers/acp-schema.ts`). Bundle level: `AMA_E2E=1 pnpm test:e2e` (`test/e2e/bridge.e2e.test.ts`, a `pi` shim on PATH that runs the fake).
210
+
133
211
  ## Deviations and non-goals
134
212
 
135
213
  These are deliberate choices, not omissions:
136
214
 
137
- | Item | What ama does and why |
138
- | ------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
139
- | The client's `fs/*` and `terminal/*` | Not used (even when the client declares them). ama reads and writes files and runs commands itself, all through its own permission pipeline, sandbox and checkpoints; borrowing the client's file system or terminal would bypass them and make TUI / RPC / ACP behave differently. |
140
- | MCP (`mcpServers`, including stdio, which the spec requires) | Not connected; `mcpCapabilities.http` / `sse` are false; a non-empty `mcpServers` gets one stderr line and the session opens as usual. **This deviates from the spec's MUST "agents must support stdio MCP"**: ama's tools come only from itself and the host (profile), and extensions are skills; MCP would put external tool descriptions into the request prefix, breaking the byte-stable prompt cache, and bypass how the permission pipeline classifies tools. |
141
- | elicitation (the agent asking a human for structured input) | As an agent, ama never sends `elicitation/create`: the only thing it needs a human to decide is approvals, which go through `session/request_permission`. As a client it is supported (see below). |
142
- | `session/delete` | Not implemented (-32601). Clean up session files with `ama sessions prune`. |
143
- | `logout` | Not implemented, and `agentCapabilities.auth.logout` is not declared (-32601). Sign out with `ama auth logout chatgpt` / `ama auth remove <provider>`. |
144
- | Session `cwd` | Fixed to ama's start directory: `session/new` with another directory answers -32602 and `additionalDirectories` is ignored. Trust, project config, the session directory and the sandbox are all decided by the start directory, and changing directories inside one process would invalidate them. To work in another directory, start another `ama --mode acp` there. |
215
+ | Item | What ama does and why |
216
+ | ------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
217
+ | The client's `fs/*` and `terminal/*` | Not used (even when the client declares them). ama reads and writes files and runs commands itself, all through its own permission pipeline, sandbox and checkpoints; borrowing the client's file system or terminal would bypass them and make TUI / RPC / ACP behave differently. |
218
+ | MCP (`mcpServers`, including stdio, which the spec requires) | Not connected; `mcpCapabilities.http` / `sse` are false; a non-empty `mcpServers` gets one stderr line and the session opens as usual (the Pi bridge is the exception: it hands `mcpServers` to Pi's built-in MCP, see above). **This deviates from the spec's MUST "agents must support stdio MCP"**: ama's tools come only from itself and the host (profile), and extensions are skills; MCP would put external tool descriptions into the request prefix, breaking the byte-stable prompt cache, and bypass how the permission pipeline classifies tools. |
219
+ | elicitation (the agent asking a human for structured input) | As an agent, ama never sends `elicitation/create`: the only thing it needs a human to decide is approvals, which go through `session/request_permission`. As a client it is supported (see below). |
220
+ | `session/delete` | Not implemented (-32601). Clean up session files with `ama sessions prune`. |
221
+ | `logout` | Not implemented, and `agentCapabilities.auth.logout` is not declared (-32601). Sign out with `ama auth logout chatgpt` / `ama auth remove <provider>`. |
222
+ | Session `cwd` | Fixed to ama's start directory: `session/new` with another directory answers -32602 and `additionalDirectories` is ignored. Trust, project config, the session directory and the sandbox are all decided by the start directory, and changing directories inside one process would invalidate them. To work in another directory, start another `ama --mode acp` there. |
145
223
 
146
224
  ## As a client
147
225
 
@@ -257,6 +257,8 @@ pi 的社区 ACP 适配器 `pi-acp` 不收录:pi 本身没有审批通道,
257
257
  ama 改为直接驱动 `pi --mode rpc`,并以 `-e` 给这一次运行加载一个最小扩展(写在每个会话自己的临时目录,关会话即删,不碰 pi 的配置):
258
258
  只读内置工具(`read` / `grep` / `find` / `ls`)以外的调用都经 RPC 的扩展对话框交给 ama——`plan` / `allowlist` 直接拒绝(并以
259
259
  `--tools` 只开只读工具),`auto-edit` 及更宽的模式放行 `edit` / `write`,其余交给人。用户自己的扩展弹出的对话框一律取消、不代答。
260
+ 这份扩展(`src/drivers/native/pi-extension.ts`)与 ACP 桥共用;Armadra 等 ACP 客户端想直接驱动 pi 时用 `ama --mode acp --bridge pi`
261
+ (四个权限模式、MCP 透传、待办与征询,见 [acp.md](../reference/acp.md)),不必经 `task`。
260
262
 
261
263
  #### 实测能力矩阵(2026-10-10)
262
264
 
@@ -4,7 +4,7 @@
4
4
 
5
5
  ama 在 ACP 两侧都能用:
6
6
 
7
- - **服务端**:`ama --mode acp` 把 ama 暴露为 ACP Agent,供 Zed、JetBrains、Armadra 的 ACP 节点驱动;
7
+ - **服务端**:`ama --mode acp` 把 ama 暴露为 ACP Agent,供 Zed、JetBrains、Armadra 的 ACP 节点驱动;`ama --mode acp --bridge pi` 把本机的 Pi 暴露为 ACP Agent(见下文);
8
8
  - **客户端**:ama 经 ACP 驱动外部 Agent(Gemini CLI、OpenCode、Kimi、Copilot 等原生 ACP,或装了适配器的 Claude Code / Codex),见 [agents.md](../guides/agents.md)。
9
9
 
10
10
  协议栈零依赖、手写,只维护一份:`@armadra/agent/acp` 导出类型、分帧、客户端与假 Agent,Armadra 直接复用。设计依据见 [wave5-plan.md](../history/wave5-plan.md) §5(D14)。
@@ -136,18 +136,96 @@ Zed 的配置(`settings.json`):
136
136
 
137
137
  stdin 关闭后等已开始的运行结束再退出(0);`ama` 进程的 stdout 从启动第一步起只给协议(宿主 / Hook 加载期的 `console.log` 改写到 stderr);SIGINT / SIGTERM 中断后退出 130 / 143。宿主看到的模式是 `rpc`(`HostApi.mode`)。SDK 直接 `bootstrap(--mode acp)` 时得到 `mode: "rpc"` 的 Runtime,再交给 `runAcpMode`。
138
138
 
139
+ ## `ama --mode acp --bridge pi`
140
+
141
+ ```sh
142
+ ama --mode acp --bridge pi [--lang zh|en] [-- <pi 的参数>]
143
+ ```
144
+
145
+ 把本机装的 [Pi](https://github.com/earendil-works/pi)(`@earendil-works/pi-coding-agent`)暴露为 ACP Agent,供 Armadra 与别的 ACP 客户端使用。入口在 bootstrap **之前**分流:不解析供应商与模型,不需要 ama 的 key,也不读写 ama 的会话;`--bridge` 只认 `pi`,且必须配 `--mode acp`。桥自己不发模型请求,提示字节规则不受影响。
146
+
147
+ **进程与会话**:一个 ACP 会话对应一个 `pi --mode rpc --session-id <id> -e <桥扩展>` 子进程,ACP `sessionId` 就是 Pi 的会话 id,进程的工作目录是 `session/new` 的 `cwd`(必须是绝对路径)。`-- ` 之后的参数原样交给 pi(`--skill`、`--append-system-prompt` 等照用),但桥自己管的旗标一律拒绝(退出码 2):`--mode`、`--session*`、`-c` / `--continue`、`-r` / `--resume`、`--fork`、`--no-session`、`--tools` / `-t`、`--no-mcp`、`-p` / `--print`。stdin 关闭时关掉全部 pi 子进程后退出 0。桥不读写 Pi 的配置与登录(`auth.json`、`mcp.json`、`settings.json`);在 Pi 里用 `/login` 登录。
148
+
149
+ | 方法 | 桥的行为 |
150
+ | --------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
151
+ | `initialize` | `loadSession: true`,`sessionCapabilities: { resume, close }`(不报 `list`:Pi 的 RPC 没有列会话的命令,桥也不读 Pi 的会话目录),`mcpCapabilities: { http: true, sse: false }`,`promptCapabilities: { image: true, embeddedContext: true }`,`authMethods: []` |
152
+ | `session/new` | 用新 uuid 起子进程,答复带 `modes`、`configOptions` 与 `_meta`;Pi 没有可用模型时答 `error.data.code = "pi_no_model"`,提示去 Pi 里登录 |
153
+ | `session/load` | 同上,用请求里的 id;Pi 里这个会话没有消息(Pi 会静默新建一个空的)就当不存在:-32002 并收掉进程。通过后把历史回放成 `user_message_chunk`、`agent_*_chunk` 与终态的 `tool_call`,末尾补一份当前计划 |
154
+ | `session/resume` | 同 load,不回放 |
155
+ | `session/close` | 中断、关子进程、删临时目录 |
156
+ | `session/prompt` | 文本与嵌入资源拼成 message,图片走 `images`。`disposition: handled` 不等回合;`agent_settled.aborted` → `cancelled`,`length` → `max_tokens`,出错 → JSON-RPC 错误。答复的 `usage` 是本回合累加。运行中再来的 prompt 答 invalid request(busy) |
157
+ | `session/cancel` | 撤回挂起的审批与征询,发 `abort`,15 秒看门狗(过了关 stdin 再结束进程树),回合以 `cancelled` 结束 |
158
+ | `session/set_mode` | 四个模式,见下;立即生效 |
159
+ | `session/set_config_option` | `model` 与 `thought_level`,见下 |
160
+
161
+ ### Pi 版本
162
+
163
+ 桥只发能力表里有的 RPC 命令(旧版 Pi 对未知命令的应答不带 id,请求无从关联),非 prompt 命令有 10 秒截止。
164
+
165
+ | 能力 | 起始版本 | 用在哪 |
166
+ | ---------------------------------------------------- | -------------- | ------------------------------ |
167
+ | `--session-id` | 0.76.0 | 新开 / 接回 |
168
+ | `agent_settled` / 其 `aborted` | 0.80.4 / 1.1.0 | 判定回合结束与取消 |
169
+ | `get_available_thinking_levels` | 0.81.0 | `thought_level` 配置项 |
170
+ | `prompt` 应答的 `disposition` | 0.99.0 | `handled` 时不等回合 |
171
+ | 内置 MCP、`pi.registerMcpServer`、工具 `annotations` | 0.99.0 | 画布工具;plan 模式判定只读 |
172
+ | `get_session_stats` 的 `cost` / `contextUsage` | 0.63.0 | `usage_update` |
173
+ | `Usage.cost.totalFromProvider` | 1.5.0 | 区分费用是账单值还是价目表估算 |
174
+
175
+ - **下限 1.1.0**(必需能力里起始版本最晚的一项)。更低的 Pi:`session/new` 答 `error.data = { code: "pi_outdated", installed, minimum, package }`,找不到 `pi` 答 `{ code: "pi_missing", package }`,都不起进程。
176
+ - **已验证区间 `>=1.1.0 <2.0.0`**:按能力表开关可选能力(例如 <1.5.0 时费用不标账单值)。已在 Pi 1.5.0 上实测。
177
+ - **高于区间或读不出版本**:照跑,按最高已知能力处理;stderr 一行,答复里 `_meta["armadra.pi"].verified` 为 `false`,客户端据此提示。
178
+ - 答复的 `_meta["armadra.pi"] = { version?, verified, capabilities? }`;`capabilities = { todo, subagent, questionnaire }` 是 Pi 里有没有同名工具(见「待办、子 Agent 与征询」)。
179
+
180
+ ### 模式与审批
181
+
182
+ Pi 没有权限模式,也没有审批通道,所以桥给每次运行加载一个扩展(`-e`,写在会话自己的 `mkdtemp` 目录、权限 0600,关会话即删;与外部 Agent 驱动共用,见 [agents.md](../guides/agents.md))。扩展在每次工具调用前把请求交给桥,**每次都问桥**,所以 `session/set_mode` 立即生效。
183
+
184
+ | 模式(id 同 ama) | 行为 |
185
+ | ----------------- | ------------------------------------------------------------------------------------------------------- |
186
+ | `default` | 只读(内置 `read` / `grep` / `find` / `ls`,或工具声明 `readOnlyHint` 的 MCP 工具)直接放行,其余交给人 |
187
+ | `auto-edit` | 再放行 `edit` / `write`,其余交给人 |
188
+ | `plan` | 只读放行,其余拒绝,并告诉模型当前在 plan 模式(描述改动而不是去做) |
189
+ | `full-auto` | 都放行,但命中 ama 危险命令规则的 bash 仍交给人 |
190
+
191
+ 交给人 = `session/request_permission`(`allow_once` / `reject_once`,带 `toolCallId`、标题、类别、`rawInput`、位置)。要过闸的调用先以 `pending` 公布,放行后转 `in_progress`。客户端回 `cancelled`、请求出错或会话取消都按拒绝;**桥从不代答**。
192
+
193
+ ### 配置项与用量
194
+
195
+ - `model`:值 `provider/id`(按供应商分组),换模型后重取推理强度并发 `config_option_update`;
196
+ - `thought_level`:Pi 的 `off`…`max`,当前模型不支持推理时不给这一项;是唯一的推理强度控件,不进 `modes`;
197
+ - `usage_update`:开会话、回合结束与换模型之后发,`used` = `contextUsage.tokens`,`size` = 上下文窗口,`cost = { amount, currency: "USD" }`;≥1.5.0 时 `_meta["armadra.pi"].fromProvider` 标费用是不是账单值。Pi 在会话里改了模型(`set_model` 会写 Pi 自己的默认模型设置)是 Pi 的行为,桥不另存。
198
+
199
+ ### MCP
200
+
201
+ 客户端的 `mcpServers`(stdio 与 http;sse 跳过并在 stderr 说一行)写进会话临时目录的 `bridge.json`,扩展加载时对每一项调用 `pi.registerMcpServer(name, { …, exposure: "direct" })`——ama 自己不说 MCP,连接由 Pi 内置的 MCP 完成。Pi 的服务器名只允许字母、数字、`_`、`-`,其余换成 `_`;用户 `mcp.json` 里的同名服务器按 Pi 的规则优先。
202
+
203
+ `mcp__<server>__<tool>`(只认桥注册过的服务器)的 `tool_call` 与权限请求带 `_meta.is_mcp_tool_call: true` 与 `rawInput { server, tool, arguments }`,客户端据此识别自家工具。codemode 里发起的内层调用带 `_meta.ama.parentToolCallId`。
204
+
205
+ ### 待办、子 Agent 与征询
206
+
207
+ 桥**不注册自己的 todo 或问卷工具**:这些只在已装的 Pi 本来就有对应工具或扩展时出现;没有就什么也不加,缺口体现在 `_meta["armadra.pi"].capabilities`。
208
+
209
+ - **待办 → `plan`**:工具结果里的 `details.todos`(`{text, done}` 或 `{content, status}`)每来一次整表发一次 `plan`;`todo` 工具的类别是 `think`;
210
+ - **子 Agent**:Pi 的 `subagent` 扩展工具是一条 kind 为 `other` 的 `tool_call`,`_meta["armadra.subagent"] = { agent, task }`(并行 / 链式为 `{ mode, tasks }`),流式进度写进 content,费用已计入 `get_session_stats`;
211
+ - **对话框 → `elicitation/create`**:客户端声明了 `elicitation` 时,其它扩展的 select(带标题的枚举)、confirm(boolean)、input / editor(string,editor 的预填作默认值)转成 form 型征询;`accept` 把值回给 Pi,`decline` / `cancel` / 选项之外的值一律是 Pi 一侧的取消,桥不替人选。Pi 给的 `timeout` 到点时撤回请求(`$/cancel_request`);会话取消同样撤回。客户端没声明时取消并在 stderr 说一行。ama 自己的对话框(审批闸、拒绝理由、能力报告)不在此列。
212
+
213
+ ### 测试
214
+
215
+ 脚本化的假 Pi(`test/helpers/fake-pi.ts`)覆盖版本 1.0.4 / 1.1.0 / 1.4.0 / 1.5.0 / 2.0.0 / 读不出版本、四个模式、取消、MCP、plan、子 Agent、征询,对表外命令像旧版 Pi 那样答不带 id 的应答,测试断言桥从不发;每条线上消息过 [官方 schema](../../test/helpers/acp-schema.ts)。bundle 级:`AMA_E2E=1 pnpm test:e2e`(`test/e2e/bridge.e2e.test.ts`,PATH 上的 `pi` 垫片转到假 pi)。
216
+
139
217
  ## 偏离与不做
140
218
 
141
219
  以下是有意的取舍,不是遗漏:
142
220
 
143
- | 项目 | ama 的做法与理由 |
144
- | ----------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
145
- | 客户端的 `fs/*`、`terminal/*` | 不使用(客户端声明了也不用)。ama 自己读写文件、自己跑命令,全部经自己的权限管线、沙箱与检查点;借客户端的文件系统或终端会绕开这些,也会让 TUI / RPC / ACP 三处行为不一致。 |
146
- | MCP(`mcpServers`,含规范要求必须支持的 stdio) | 不连接,`mcpCapabilities` 的 `http` / `sse` 为 false;收到非空 `mcpServers` 时 stderr 记一行后照常开会话。**这偏离了规范「Agent 必须支持 stdio MCP」的 MUST**:ama 的工具只来自自身与宿主(profile),扩展走 Skill;接 MCP 会把外部工具描述放进请求前缀,破坏逐字节稳定的提示词缓存,也绕开权限管线对工具的分类。 |
147
- | elicitation(Agent 向人要结构化输入) | 作服务端时不发 `elicitation/create`:ama 需要人决定的只有审批,走 `session/request_permission`。作客户端时支持(见下文)。 |
148
- | `session/delete` | 不实现(-32601)。会话文件的清理走 `ama sessions prune`。 |
149
- | `logout` | 不实现、不声明 `agentCapabilities.auth.logout`(-32601)。退出登录用 `ama auth logout chatgpt` / `ama auth remove <供应商>`。 |
150
- | 会话 `cwd` | 固定为 ama 的启动目录,`session/new` 给了别的目录回 -32602,`additionalDirectories` 忽略:信任、项目配置、会话目录与沙箱都按启动目录判定,同一进程里换目录会让它们失效。要换目录就在那个目录另起一个 `ama --mode acp`。 |
221
+ | 项目 | ama 的做法与理由 |
222
+ | ----------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
223
+ | 客户端的 `fs/*`、`terminal/*` | 不使用(客户端声明了也不用)。ama 自己读写文件、自己跑命令,全部经自己的权限管线、沙箱与检查点;借客户端的文件系统或终端会绕开这些,也会让 TUI / RPC / ACP 三处行为不一致。 |
224
+ | MCP(`mcpServers`,含规范要求必须支持的 stdio) | 不连接,`mcpCapabilities` 的 `http` / `sse` 为 false;收到非空 `mcpServers` 时 stderr 记一行后照常开会话(Pi 的桥除外:它把 `mcpServers` 交给 Pi 内置的 MCP,见上文)。**这偏离了规范「Agent 必须支持 stdio MCP」的 MUST**:ama 的工具只来自自身与宿主(profile),扩展走 Skill;接 MCP 会把外部工具描述放进请求前缀,破坏逐字节稳定的提示词缓存,也绕开权限管线对工具的分类。 |
225
+ | elicitation(Agent 向人要结构化输入) | 作服务端时不发 `elicitation/create`:ama 需要人决定的只有审批,走 `session/request_permission`。作客户端时支持(见下文)。 |
226
+ | `session/delete` | 不实现(-32601)。会话文件的清理走 `ama sessions prune`。 |
227
+ | `logout` | 不实现、不声明 `agentCapabilities.auth.logout`(-32601)。退出登录用 `ama auth logout chatgpt` / `ama auth remove <供应商>`。 |
228
+ | 会话 `cwd` | 固定为 ama 的启动目录,`session/new` 给了别的目录回 -32602,`additionalDirectories` 忽略:信任、项目配置、会话目录与沙箱都按启动目录判定,同一进程里换目录会让它们失效。要换目录就在那个目录另起一个 `ama --mode acp`。 |
151
229
 
152
230
  ## 作为客户端
153
231
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@armadra/agent",
3
- "version": "0.12.0",
3
+ "version": "0.14.0",
4
4
  "description": "A coding and coordination agent that runs standalone or embedded in Armadra",
5
5
  "type": "module",
6
6
  "keywords": [
@@ -1,19 +0,0 @@
1
- /**
2
- * pi 的审批闸(#198):pi 自己没有审批通道,ama 以 `-e <文件>` 给这一次运行加载一个最小扩展,
3
- * 把只读内置工具以外的每次工具调用经 RPC 的扩展对话框(`extension_ui_request{method: confirm}`)
4
- * 交给 ama——ama 再按模式决定直接拒绝、直接放行(auto-edit 下的文件编辑)或交给人。
5
- *
6
- * 扩展文件写在每个会话自己的临时目录(`mkdtemp`,只有当前用户可写),会话关闭时删除;
7
- * 不写 pi 的配置目录,不改用户的扩展设置。
8
- */
9
- /** 对话框标题:ama 只认这个标题的 confirm,其它扩展的对话框一律取消(不代答)。 */
10
- export declare const PI_GATE_TITLE = "ama-approval";
11
- /** pi 不经审批就放行的内置只读工具(也用于只读模式的 `--tools`)。 */
12
- export declare const PI_READ_ONLY_TOOLS: readonly string[];
13
- /** 扩展源码(pi 用 jiti 加载 TypeScript;这里只用纯 JS 语法)。 */
14
- export declare const PI_GATE_SOURCE: string;
15
- /** 写一份闸扩展,返回路径与清理函数。 */
16
- export declare function writePiGate(): {
17
- path: string;
18
- dispose(): void;
19
- };
@@ -1,39 +0,0 @@
1
- /**
2
- * pi 的审批闸(#198):pi 自己没有审批通道,ama 以 `-e <文件>` 给这一次运行加载一个最小扩展,
3
- * 把只读内置工具以外的每次工具调用经 RPC 的扩展对话框(`extension_ui_request{method: confirm}`)
4
- * 交给 ama——ama 再按模式决定直接拒绝、直接放行(auto-edit 下的文件编辑)或交给人。
5
- *
6
- * 扩展文件写在每个会话自己的临时目录(`mkdtemp`,只有当前用户可写),会话关闭时删除;
7
- * 不写 pi 的配置目录,不改用户的扩展设置。
8
- */
9
- import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
10
- import { tmpdir } from "node:os";
11
- import { join } from "node:path";
12
- /** 对话框标题:ama 只认这个标题的 confirm,其它扩展的对话框一律取消(不代答)。 */
13
- export const PI_GATE_TITLE = "ama-approval";
14
- /** pi 不经审批就放行的内置只读工具(也用于只读模式的 `--tools`)。 */
15
- export const PI_READ_ONLY_TOOLS = ["read", "grep", "find", "ls"];
16
- /** 扩展源码(pi 用 jiti 加载 TypeScript;这里只用纯 JS 语法)。 */
17
- export const PI_GATE_SOURCE = `// Generated by ama for one pi run: routes non-read-only tool calls to ama for approval.
18
- const READ_ONLY = new Set(${JSON.stringify(PI_READ_ONLY_TOOLS)});
19
- export default function (pi) {
20
- pi.on("tool_call", async (event, ctx) => {
21
- if (READ_ONLY.has(event.toolName)) return undefined;
22
- const ok = await ctx.ui.confirm(
23
- ${JSON.stringify(PI_GATE_TITLE)},
24
- JSON.stringify({ id: event.toolCallId, tool: event.toolName, input: event.input }),
25
- );
26
- return ok ? undefined : { block: true, reason: event.toolName + " was not approved by the user" };
27
- });
28
- }
29
- `;
30
- /** 写一份闸扩展,返回路径与清理函数。 */
31
- export function writePiGate() {
32
- const dir = mkdtempSync(join(tmpdir(), "ama-pi-"));
33
- const path = join(dir, "ama-approval-gate.ts");
34
- writeFileSync(path, PI_GATE_SOURCE, { mode: 0o600 });
35
- return {
36
- path,
37
- dispose: () => rmSync(dir, { recursive: true, force: true }),
38
- };
39
- }