@armadra/agent 0.18.0 → 0.19.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.
- package/CHANGELOG.md +20 -0
- package/CHANGELOG.zh-CN.md +20 -0
- package/README.md +4 -0
- package/README.zh-CN.md +4 -0
- package/dist/bundle/ama.cjs +25956 -25161
- package/dist/cli/bootstrap.js +32 -6
- package/dist/cli/compose-agents.js +2 -1
- package/dist/cli/compose-extensions-load.d.ts +20 -0
- package/dist/cli/compose-extensions-load.js +38 -0
- package/dist/cli/compose.js +1 -0
- package/dist/cli/deps.d.ts +11 -0
- package/dist/cli/runtime.d.ts +7 -0
- package/dist/cli/subcommands/doctor.js +33 -0
- package/dist/config/extension-tree-hash.d.ts +31 -0
- package/dist/config/extension-tree-hash.js +88 -0
- package/dist/config/extension-trust.d.ts +70 -0
- package/dist/config/extension-trust.js +112 -0
- package/dist/config/profile.d.ts +2 -0
- package/dist/config/profile.js +1 -0
- package/dist/config/schema-trust.d.ts +7 -0
- package/dist/config/schema-trust.js +46 -0
- package/dist/config/schema.d.ts +4 -5
- package/dist/config/schema.js +3 -25
- package/dist/config/trust.d.ts +2 -0
- package/dist/config/trust.js +5 -1
- package/dist/config/types.d.ts +13 -0
- package/dist/drivers/host-runners.d.ts +15 -4
- package/dist/drivers/host-runners.js +31 -17
- package/dist/errors.d.ts +1 -1
- package/dist/host/api-impl.d.ts +47 -4
- package/dist/host/api-impl.js +181 -20
- package/dist/host/commands.d.ts +23 -0
- package/dist/host/commands.js +76 -0
- package/dist/host/extensions.d.ts +43 -0
- package/dist/host/extensions.js +117 -0
- package/dist/host/resources.d.ts +2 -0
- package/dist/host/resources.js +5 -0
- package/dist/host/types.d.ts +41 -2
- package/dist/host/types.js +4 -2
- package/dist/i18n/catalog.d.ts +31 -0
- package/dist/i18n/messages/cli.d.ts +24 -0
- package/dist/i18n/messages/cli.js +24 -0
- package/dist/i18n/messages/drivers.d.ts +22 -0
- package/dist/i18n/messages/drivers.js +22 -0
- package/dist/i18n/messages/errors.d.ts +1 -1
- package/dist/i18n/messages/interactive-startup.d.ts +4 -0
- package/dist/i18n/messages/interactive-startup.js +4 -0
- package/dist/i18n/messages/interactive.d.ts +4 -0
- package/dist/i18n/messages/report.d.ts +12 -0
- package/dist/i18n/messages/report.js +12 -0
- package/dist/modes/acp/acp-config.d.ts +4 -2
- package/dist/modes/acp/acp-config.js +17 -3
- package/dist/modes/acp/acp-events.d.ts +2 -0
- package/dist/modes/acp/acp-events.js +4 -0
- package/dist/modes/acp/acp-server.js +6 -2
- package/dist/modes/acp/acp-sessions.d.ts +8 -1
- package/dist/modes/acp/acp-sessions.js +18 -0
- package/dist/modes/commands-core.d.ts +14 -1
- package/dist/modes/commands-core.js +34 -2
- package/dist/modes/interactive/startup-ui.js +21 -0
- package/dist/modes/interactive/status-area.js +1 -1
- package/dist/modes/print/print-mode.d.ts +3 -0
- package/dist/modes/print/print-mode.js +35 -8
- package/dist/modes/rpc/commands.d.ts +2 -0
- package/dist/modes/rpc/commands.js +22 -7
- package/dist/modes/rpc/rpc-mode.js +3 -0
- package/dist/modes/startup-ui-text.js +11 -0
- package/dist/rpc.d.ts +2 -1
- package/dist/tools/registry.d.ts +6 -0
- package/dist/tools/registry.js +13 -0
- package/dist/tools/types.d.ts +7 -0
- package/docs/README.md +1 -1
- package/docs/en/README.md +3 -3
- package/docs/en/guides/extensions.md +141 -0
- package/docs/en/reference/acp.md +1 -1
- package/docs/en/reference/host-api.md +55 -27
- package/docs/en/reference/rpc.md +9 -7
- package/docs/guides/extensions.md +136 -0
- package/docs/reference/acp.md +1 -1
- package/docs/reference/host-api.md +55 -27
- package/docs/reference/rpc.md +9 -7
- package/package.json +4 -2
|
@@ -5,7 +5,7 @@ English · [简体中文](../../reference/host-api.md)
|
|
|
5
5
|
> Translated from the Chinese [docs/reference/host-api.md](../../reference/host-api.md) as of commit `979608b`. When the two differ, the Chinese
|
|
6
6
|
> version is authoritative.
|
|
7
7
|
|
|
8
|
-
A host adapter is a local JS module that ama loads at startup and hands a `HostApi`. With it the adapter can register tools, append to the system prompt, observe events, answer approvals, inject user messages and show notifications and status in the interface. The Armadra canvas plugs in as a host adapter (the canvas tools `canvas_*` / `context_*` are all registered by the adapter). The types are defined in `src/host/types.ts` and exported from `@armadra/agent/host`; `HOST_API_VERSION = 1`, `HOST_API_MINOR =
|
|
8
|
+
A host adapter is a local JS module that ama loads at startup and hands a `HostApi`. With it the adapter can register tools, append to the system prompt, observe events, answer approvals, inject user messages and show notifications and status in the interface. The Armadra canvas plugs in as a host adapter (the canvas tools `canvas_*` / `context_*` are all registered by the adapter). The types are defined in `src/host/types.ts` and exported from `@armadra/agent/host`; `HOST_API_VERSION = 1`, `HOST_API_MINOR = 4` (host-api 1.4, see "Versioning"). The design rationale is in [design.md](../../design/design.md) §6.2, §6.3 and §11.1 step 13 (Chinese).
|
|
9
9
|
|
|
10
10
|
## Module shape
|
|
11
11
|
|
|
@@ -61,34 +61,37 @@ export function create(api) {
|
|
|
61
61
|
| ----- | ----------------- | -------------------------------------------------------------------------------------------------- |
|
|
62
62
|
| 0 | — | Every existing member (optional facets such as `cache?` and `runners?.provide` still need a check) |
|
|
63
63
|
| 1 | 0.11.0 | `minor`, `runners?.onDone` (see "Runner done event") |
|
|
64
|
-
| 2 |
|
|
65
|
-
| 3 |
|
|
64
|
+
| 2 | 0.16.0 | `resources?.register` / `resources?.list` (see "Host resources") |
|
|
65
|
+
| 3 | 0.16.0 | `session_start.uncleanEnd` (with `reason: "resume"`, #268) |
|
|
66
|
+
| 4 | Unreleased | `commands?.register` / `commands?.list` (see "Slash commands", #301) |
|
|
66
67
|
|
|
67
68
|
## HostApi
|
|
68
69
|
|
|
69
|
-
| Member | Description
|
|
70
|
-
| ----------------------------------------------- |
|
|
71
|
-
| `version` | `HOST_API_VERSION`
|
|
72
|
-
| `agent` | `{ name: "ama", version }`
|
|
73
|
-
| `env` | A frozen copy of the environment variables at startup
|
|
74
|
-
| `mode` | `interactive` / `line` / `print` / `rpc`
|
|
75
|
-
| `session.id()` / `file()` / `cwd()` / `model()` | The current session (follows the new session after a switch); `file()` is `undefined` until the first request is written to disk
|
|
76
|
-
| `tools.register(tool)` | Register a tool (shape below); the name must match `^[a-z][a-z0-9_]{1,63}$`, and an existing name throws `tool_exists`. A prefix is recommended (`canvas_*`)
|
|
77
|
-
| `tools.disable(name)` | Hide a built-in tool (Armadra disables `task`, for example); the tools section of the system prompt stops listing it
|
|
78
|
-
| `tools.list()` | All current tool names
|
|
79
|
-
| `instructions.add(source)` | Append to the final `host` section of the system prompt; `{ kind: "file", path }` or `{ kind: "text", text, name? }`
|
|
80
|
-
| `events.on(name, handler)` | Observe events (table below); returns an unsubscribe function
|
|
81
|
-
| `approvals.setBroker(broker)` | Set the approval answerer (see "Approvals")
|
|
82
|
-
| `messages.sendUser(text, origin?)` | Inject a user message: when idle it starts a run (`"started"`), while running it is queued as a steer (`"queued"`); `origin` defaults to `"host"`, is persisted on the message and shown as `↳ host`
|
|
83
|
-
| `ui.notify(message, level?)` | Goes to the message area in interactive / line mode; becomes a `notification` event in rpc mode (with a copy on stderr); written to stderr in print mode
|
|
84
|
-
| `ui.setStatus(key, text?)` | A host item in the status bar; an empty or missing `text` removes the key
|
|
85
|
-
| `log(level, message, detail?)` | Logging; `warn` / `error` go to stderr
|
|
86
|
-
| `cache?.onWarmingDecision(handler)` | Veto hook for cache warming (see "Cache warming"); an optional facet missing in older runtimes, so check `api.cache !== undefined` before use
|
|
87
|
-
| `runners?.provide(runner)` | Injects a sub-agent runner: `id` is the name in `task(agent=<id>)` and `description` goes into the task tool description; with a host, ama starts no external CLI of its own and only uses runners injected here; returns an unsubscribe function |
|
|
88
|
-
| `runners?.onDone(handler)` | Called whenever a round of
|
|
89
|
-
| `resources?.register(resource)` | Registers a read-only resource namespace `scheme://…` that the built-in `read` reads (see "Host resources"); only callable inside `create()`. Since host-api 1.2, so check `api.resources !== undefined` before use
|
|
90
|
-
| `resources?.list()` | The registered schemes, sorted by name
|
|
91
|
-
| `
|
|
70
|
+
| Member | Description |
|
|
71
|
+
| ----------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
72
|
+
| `version` | `HOST_API_VERSION` |
|
|
73
|
+
| `agent` | `{ name: "ama", version }` |
|
|
74
|
+
| `env` | A frozen copy of the environment variables at startup |
|
|
75
|
+
| `mode` | `interactive` / `line` / `print` / `rpc` |
|
|
76
|
+
| `session.id()` / `file()` / `cwd()` / `model()` | The current session (follows the new session after a switch); `file()` is `undefined` until the first request is written to disk |
|
|
77
|
+
| `tools.register(tool)` | Register a tool (shape below); the name must match `^[a-z][a-z0-9_]{1,63}$`, and an existing name throws `tool_exists`. A prefix is recommended (`canvas_*`) |
|
|
78
|
+
| `tools.disable(name)` | Hide a built-in tool (Armadra disables `task`, for example); the tools section of the system prompt stops listing it |
|
|
79
|
+
| `tools.list()` | All current tool names |
|
|
80
|
+
| `instructions.add(source)` | Append to the final `host` section of the system prompt; `{ kind: "file", path }` or `{ kind: "text", text, name? }` |
|
|
81
|
+
| `events.on(name, handler)` | Observe events (table below); returns an unsubscribe function |
|
|
82
|
+
| `approvals.setBroker(broker)` | Set the approval answerer (see "Approvals") |
|
|
83
|
+
| `messages.sendUser(text, origin?)` | Inject a user message: when idle it starts a run (`"started"`), while running it is queued as a steer (`"queued"`); `origin` defaults to `"host"`, is persisted on the message and shown as `↳ host` |
|
|
84
|
+
| `ui.notify(message, level?)` | Goes to the message area in interactive / line mode; becomes a `notification` event in rpc mode (with a copy on stderr); written to stderr in print mode |
|
|
85
|
+
| `ui.setStatus(key, text?)` | A host item in the status bar; an empty or missing `text` removes the key |
|
|
86
|
+
| `log(level, message, detail?)` | Logging; `warn` / `error` go to stderr |
|
|
87
|
+
| `cache?.onWarmingDecision(handler)` | Veto hook for cache warming (see "Cache warming"); an optional facet missing in older runtimes, so check `api.cache !== undefined` before use |
|
|
88
|
+
| `runners?.provide(runner)` | Injects a sub-agent runner: `id` is the name in `task(agent=<id>)` and `description` goes into the task tool description; with a host adapter, ama starts no external CLI of its own and only uses runners injected here; the host adapter and local extensions share one table, across modules the first provider of an id wins and a later one throws `runner_exists`, while a module providing the same id again replaces its own (#306); returns an unsubscribe function |
|
|
89
|
+
| `runners?.onDone(handler)` | Called whenever a round of one of this module's own runners ends (see "Runner done event"); returns an unsubscribe function. Since host-api 1.1, so check `typeof api.runners?.onDone === "function"` before use |
|
|
90
|
+
| `resources?.register(resource)` | Registers a read-only resource namespace `scheme://…` that the built-in `read` reads (see "Host resources"); only callable inside `create()`. Since host-api 1.2, so check `api.resources !== undefined` before use |
|
|
91
|
+
| `resources?.list()` | The registered schemes, sorted by name |
|
|
92
|
+
| `commands?.register(command)` | Register a slash command (see "Slash commands"); available to host adapters and local extensions, returns an unregister function; since host-api 1.4, check `api.commands !== undefined` first |
|
|
93
|
+
| `commands?.list()` | Registered command names (all modules), in registration order |
|
|
94
|
+
| `minor?` | `HOST_API_MINOR` (see "Versioning"); missing in older runtimes |
|
|
92
95
|
|
|
93
96
|
During `create()` the session is not assembled yet: `session.*` returns the values fixed at startup, and `sendUser` is rejected with `busy`. To send a message at startup, wait for the `session_start` event.
|
|
94
97
|
|
|
@@ -124,6 +127,31 @@ Host tools take the same path as built-in tools: schema validation → command h
|
|
|
124
127
|
|
|
125
128
|
The exported `ToolPermission` type gains `"network"` (used by the built-in `web_fetch`; judged as execute and asks in plan, see [permissions.md](../guides/permissions.md) "Network class"); tools registered by a host through `tools.register` still accept only `read` / `write` / `execute`, and `HOST_API_VERSION` is unchanged.
|
|
126
129
|
|
|
130
|
+
## Slash commands
|
|
131
|
+
|
|
132
|
+
`api.commands.register({ name, description, args?, run(args, ctx) })` (host-api 1.4): registers a `/name` command and returns an unregister function.
|
|
133
|
+
|
|
134
|
+
```ts
|
|
135
|
+
interface HostCommand {
|
|
136
|
+
name: string; // ^[a-z][a-z0-9-]{0,63}$, no slash
|
|
137
|
+
description: string; // shown in /help and completion
|
|
138
|
+
args?: string; // argument placeholder, e.g. "[day]"
|
|
139
|
+
run(args: string, ctx: HostCommandContext): HostCommandResult | Promise<HostCommandResult>;
|
|
140
|
+
}
|
|
141
|
+
interface HostCommandContext {
|
|
142
|
+
mode: "interactive" | "line" | "print" | "rpc";
|
|
143
|
+
session: { id(): string; cwd(): string };
|
|
144
|
+
}
|
|
145
|
+
type HostCommandResult =
|
|
146
|
+
{ kind: "handled"; message?: string } | { kind: "prompt"; text: string } | undefined;
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
- `register` throws (`command_exists`) for a built-in command, an alias (`quit`, `q`, `?`), an interactive-UI command (`tree`, `permissions`) or an already registered name; a bad name format, `description` or `run` throws `invalid_arguments`.
|
|
150
|
+
- Dispatch is after built-in commands and before prompt templates; `args` is everything after the command name, trimmed.
|
|
151
|
+
- A `handled` `message` is shown to the user (message area in interactive / line, a `notification` event in RPC) and does not enter the conversation; `prompt` sends `text` as the user prompt; `undefined` means `{ kind: "handled" }`; any other shape, or an empty `text`, throws and is shown as a command error.
|
|
152
|
+
- Dispatched in interactive, line and RPC modes; `-p` and ACP do not dispatch slash commands. Listed by `/help`, completion and RPC `get_commands` (`source: "extension"`).
|
|
153
|
+
- Host adapters and local extensions share one table, so command names are globally unique.
|
|
154
|
+
|
|
127
155
|
## Events
|
|
128
156
|
|
|
129
157
|
`events.on` handlers only observe: a throw is just logged and does not affect the run; handlers are called and awaited in order, and `session_shutdown` is awaited (so you can clean up before exit).
|
|
@@ -156,7 +184,7 @@ For token-level streaming content or the full event stream, use RPC or the SDK's
|
|
|
156
184
|
- `ask` answers by returning `"allow"` / `"deny"` / `"allow_session"`; returning `undefined` passes to the next answerer; a throw counts as deny.
|
|
157
185
|
- `request`: `requestId`, `toolName`, `input`, `reason: "mode" | "dangerous" | "hook"`, `hookReason?`, `preview?` (the pre-execution preview, see [rpc.md](rpc.md) "Approvals"), `context?` (`depth > 0` means it comes from a `task` sub-agent; codemode inner calls carry `parentToolCallId`).
|
|
158
186
|
- On timeout (10 minutes by default, `AMA_APPROVAL_TIMEOUT_MS`) or when the run is interrupted, `signal` aborts and the decision is deny. Approvals are serial: only one request waits at a time.
|
|
159
|
-
- Timing: the broker is looked up at each approval, so it can be set in `create()` or set / replaced at any later time; the last `setBroker` wins.
|
|
187
|
+
- Timing: the broker is looked up at each approval, so it can be set in `create()` or set / replaced at any later time; within one module the last `setBroker` wins. With several modules (a host adapter plus [local extensions](../guides/extensions.md)) only the first setter counts; later calls are ignored with a warning.
|
|
160
188
|
- The host broker only decides "who answers an ask"; it cannot loosen deny rules, command hook denies or dangerous-command detection ([design.md](../../design/design.md) §6.3, Chinese).
|
|
161
189
|
|
|
162
190
|
## Cache warming
|
package/docs/en/reference/rpc.md
CHANGED
|
@@ -49,6 +49,8 @@ A command has the shape `{ "id"?: string, "type": <command name>, ...parameters
|
|
|
49
49
|
| `abort` | — | `{}` (answered once idle again; the queue is not cleared) |
|
|
50
50
|
| `clear_queue` | — | `{ steering: string[], followUp: string[] }` (the cleared text) |
|
|
51
51
|
|
|
52
|
+
When the `message` of a `prompt` looks like `/name args` and matches a command registered by a host adapter or local extension (`source: "extension"` in `get_commands`), ama dispatches it first: `handled` emits the command's `message` as a `notification` (`level: "info"`) and answers `disposition: "handled"` without starting a turn; `prompt` sends the text the command returned instead; a command error is answered as a failure.
|
|
53
|
+
|
|
52
54
|
Prompt commands **do not wait for the run to finish**: they are answered as soon as the session starts running (`before_agent_start` / `agent_start`), or the message is queued or handled (for example a slash command, or a hook block); progress arrives as events. Sending `prompt` while running without `streamingBehavior` fails with `code: "busy"`; with `steer` / `followUp` it is queued. Run failures after the response are reported as `{"type":"notification","level":"error","message":…}`.
|
|
53
55
|
|
|
54
56
|
**Interrupt and send now**: with `interrupt: true` on `prompt` / `steer` (it takes precedence over `streamingBehavior`), a running session first takes the queued steers, stops the current turn (the model stream is cut, running tools finish as interrupted so every tool call has exactly one `aborted by user` result, and the interrupted assistant message is persisted with `stopReason: "aborted"`), then immediately starts a new turn with "queued steers… + this message" (joined by blank lines) and answers `{ disposition: "started" }`; the new user message has `origin: "interrupt"`. followUp messages stay queued and are delivered after the new turn; background sub-agents are not affected. When idle it is the same as leaving it out. A non-boolean `interrupt` → `invalid_arguments`; running with both the message and the queued steers empty → `invalid_arguments` (nothing is interrupted). Event order: `queue_update` (steers taken) → the old turn's `message_end` (aborted) → `agent_settled` → `agent_start` → the response → the new user `message_end` … (golden record `test/fixtures/rpc/interrupt.out.jsonl`). The new request starts with every message of the interrupted request, so the cache keeps hitting. In the SDK: `session.prompt(text, { interrupt: true })` / `session.steer(text, { interrupt: true })`.
|
|
@@ -124,13 +126,13 @@ After a session switch the server re-subscribes to events and sends `session_sta
|
|
|
124
126
|
|
|
125
127
|
### Tools, permissions, discovery
|
|
126
128
|
|
|
127
|
-
| Command | Parameters | `data`
|
|
128
|
-
| --------------------- | ---------------------------------------------------------------------- |
|
|
129
|
-
| `get_tools` | — | `{ tools: { name, description, parameters, permission, active }[] }` (every tool in the registry; `active` means the model sees it now)
|
|
130
|
-
| `set_active_tools` | `names: string[]` | `{ names }` (active tool names after the change)
|
|
131
|
-
| `set_permission_mode` | `mode: plan \| allowlist \| default \| auto-edit \| auto \| full-auto` | `{ mode }` (unknown mode → `invalid_arguments`)
|
|
132
|
-
| `get_commands` | — | `{ commands: { name, description?, source: "builtin" \| "template" \| "skill" }[] }`; Skill names are written `skill:<name>`; only built-in commands that are currently available are listed (no `memory` while memory is off, no `tasks` when task cannot be called) |
|
|
133
|
-
| `get_skills` | — | `{ skills: { name, description, location, … }[] }` (discovered Skills; `location` is the SKILL.md path)
|
|
129
|
+
| Command | Parameters | `data` |
|
|
130
|
+
| --------------------- | ---------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
131
|
+
| `get_tools` | — | `{ tools: { name, description, parameters, permission, active }[] }` (every tool in the registry; `active` means the model sees it now) |
|
|
132
|
+
| `set_active_tools` | `names: string[]` | `{ names }` (active tool names after the change) |
|
|
133
|
+
| `set_permission_mode` | `mode: plan \| allowlist \| default \| auto-edit \| auto \| full-auto` | `{ mode }` (unknown mode → `invalid_arguments`) |
|
|
134
|
+
| `get_commands` | — | `{ commands: { name, description?, source: "builtin" \| "template" \| "skill" \| "extension" }[] }`; Skill names are written `skill:<name>`; `extension` is a command registered through `commands.register` by a host adapter or local extension (#301); only built-in commands that are currently available are listed (no `memory` while memory is off, no `tasks` when task cannot be called) |
|
|
135
|
+
| `get_skills` | — | `{ skills: { name, description, location, … }[] }` (discovered Skills; `location` is the SKILL.md path) |
|
|
134
136
|
|
|
135
137
|
### Plans and tasks (wave 5)
|
|
136
138
|
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
# 本地扩展
|
|
2
|
+
|
|
3
|
+
用户自己安装、可以有多个的宿主适配器:在进程内观察事件、注册工具与斜杠命令、在界面上给出通知与状态,而不占用宿主(`--host` / profile `host`,如 Armadra 画布)的位置。命令式 Hook([hooks.md](hooks.md))只能在固定时机跑外部命令,扩展补上进程内的这一层。
|
|
4
|
+
|
|
5
|
+
扩展与宿主适配器是**同一个模块形状**:`HostModule`(`hostApi` + `create(api)`),拿到的是同一个 `HostApi`,类型与各成员的细节见 [host-api.md](../reference/host-api.md)。本文只讲扩展特有的部分:加载、共存规则、斜杠命令、信任与 `ama doctor`。
|
|
6
|
+
|
|
7
|
+
## 写一个扩展
|
|
8
|
+
|
|
9
|
+
```js
|
|
10
|
+
// ~/.config/ama/extensions/standup.mjs
|
|
11
|
+
export const hostApi = 1;
|
|
12
|
+
|
|
13
|
+
export function create(api) {
|
|
14
|
+
api.commands.register({
|
|
15
|
+
name: "standup",
|
|
16
|
+
description: "Summarize today's work",
|
|
17
|
+
args: "[day]",
|
|
18
|
+
run(args, ctx) {
|
|
19
|
+
return {
|
|
20
|
+
kind: "prompt",
|
|
21
|
+
text: `Summarize what we did in ${ctx.session.cwd()} ${args || "today"}.`,
|
|
22
|
+
};
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
api.events.on("agent_settled", () => api.ui.setStatus("standup", "ready"));
|
|
26
|
+
return { id: "standup" };
|
|
27
|
+
}
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
模块可以是 `.mjs`(ESM)或 `.cjs`;导出 `default` 或具名的 `hostApi` + `create` 都行。`create()` 返回的适配器必须有 `id`,可以带 `dispose()`(退出时调用)。返回 `undefined` 表示不启用。
|
|
31
|
+
|
|
32
|
+
## 位置与加载
|
|
33
|
+
|
|
34
|
+
- **用户级目录**:`<configDir>/extensions/` 下的单文件 `*.{mjs,cjs}` 与**目录扩展** `<名>/index.{mjs,cjs}`(`index.mjs` 优先),一起按入口名(文件名或目录名)排序(缺省 `~/.config/ama/extensions/`,`AMA_CONFIG_DIR` 可改)。没有 `index.*` 的子目录忽略。
|
|
35
|
+
- **profile**:`extensions: ["/abs/a.mjs", "/abs/dir-ext", …]`(绝对路径,可以是目录扩展),排在用户级目录之后;同一路径只加载一次。
|
|
36
|
+
- **不支持项目级**:仓库里的 `.ama/extensions/` 不加载。扩展以进程权限运行、能注册工具,等同于执行任意代码,不随仓库分发。
|
|
37
|
+
- **顺序**:宿主适配器最先激活;扩展在它之后、组装会话之前(启动第 13 步)。
|
|
38
|
+
|
|
39
|
+
## 共存规则
|
|
40
|
+
|
|
41
|
+
每个模块有独立的 `HostApi` 实例(各自的工具清单与状态键),共享同一个工具注册表、事件总线、审批 broker 槽、资源注册表与保温处理器。
|
|
42
|
+
|
|
43
|
+
| 面 | 多个模块共存时 |
|
|
44
|
+
| ----------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
45
|
+
| `tools.register` | 先到先得;同名时后来者抛 `tool_exists`(扩展可以自己捕获;不捕获则 `create()` 失败,整个扩展被跳过) |
|
|
46
|
+
| `tools.disable` | 累加 |
|
|
47
|
+
| `instructions.add` | 按加载顺序(宿主适配器在前)追加到系统提示的 `host` 节 |
|
|
48
|
+
| `events.on` | 都收到,按注册顺序依次调用 |
|
|
49
|
+
| `approvals.setBroker` | 只认第一个设置者(通常是宿主适配器);其余调用忽略并 warning。同一个模块可以再次设置覆盖自己 |
|
|
50
|
+
| `ui.notify` / `ui.setStatus` | 都可用;状态栏把各模块的键合并显示,同名键后加载的覆盖先加载的,所以键名建议带自己的前缀 |
|
|
51
|
+
| `cache.onWarmingDecision` | 跨模块最后注册且未注销的处理器生效 |
|
|
52
|
+
| `commands.register` | 见下节;命令名全局唯一,先注册先得 |
|
|
53
|
+
| `runners.provide` / `onDone` | 共享一张 runner 表,模块之间先到先得(同 id 后来者抛 `runner_exists`,同一模块再次提供替换自己的);`onDone` 只回调本模块的 runner;有无宿主适配器都被 `task` 使用 |
|
|
54
|
+
| `create()` 失败 / 超时 | 宿主适配器失败仍是退出码 6;**扩展失败只记 warning 并跳过**,不阻止启动 |
|
|
55
|
+
| 加载失败 / `hostApi` 版本不等 | 宿主适配器仍是退出码 78 / 6;扩展跳过并 warning |
|
|
56
|
+
| 退出 | 按加载的逆序 `dispose`(扩展先、宿主适配器最后),某个失败只记 warning |
|
|
57
|
+
|
|
58
|
+
没有扩展时,系统提示与工具表与以前逐字节相同。Hook 公共字段里的 `host` 仍是宿主适配器的 id,扩展不改它。
|
|
59
|
+
|
|
60
|
+
扩展的 `create()` 抛错、超时或没有返回适配器时,它此前注册的全部内容都会被撤回:工具(禁用的工具恢复)、事件订阅、命令、指令、状态键、保温处理器、自己持有的 broker、runner 与资源。撤回之后该模块再调用注册类方法(例如超时后迟到的注册)一律忽略并写 warning。
|
|
61
|
+
|
|
62
|
+
## 子 Agent runner
|
|
63
|
+
|
|
64
|
+
`api.runners.provide(runner)` 在宿主适配器与扩展之间共用一张表,`task(agent=<id>)`、`/agents` 与 RPC `get_agents` 都读它:
|
|
65
|
+
|
|
66
|
+
- 模块之间同 id 先到先得,后来者抛 `runner_exists`;同一模块再次提供同 id 替换自己的;注销或模块被撤回后 id 可再提供。
|
|
67
|
+
- 没有宿主适配器时扩展的 runner 同样生效,与宿主注入的同等优先(同 id 会替换内置的外部 runner,如 `claude`);“有宿主适配器时 ama 不自起外部 CLI”的判定仍只看宿主适配器,扩展不改变它。
|
|
68
|
+
- `api.runners.onDone(handler)` 只回调本模块自己提供的 runner。
|
|
69
|
+
|
|
70
|
+
## 斜杠命令
|
|
71
|
+
|
|
72
|
+
`api.commands.register({ name, description, args?, run(args, ctx) })`(host-api 1.4,可选成员,用前判断 `api.commands !== undefined`),宿主适配器与扩展都可用,返回注销函数。
|
|
73
|
+
|
|
74
|
+
- **名字**:`^[a-z][a-z0-9-]{0,63}$`,不带斜杠。与内置命令、别名(`quit`、`q`、`?`)、交互界面命令(`tree`、`permissions`)同名,或与已注册的命令同名,注册时抛错。
|
|
75
|
+
- **分派**:内置命令之后、提示模板之前。同名的提示模板被扩展命令遮住。`/skill:<名>` 不受影响。
|
|
76
|
+
- **`args`**:命令名之后的整段文本(已去掉首尾空白);`description` 与 `args`(占位提示,如 `"[day]"`)显示在 `/help` 与补全里。不做参数补全。
|
|
77
|
+
- **`ctx`**:刻意很小——`{ mode, session: { id(), cwd() } }`。要更多能力(发消息、读状态)用注册时拿到的 `api`。
|
|
78
|
+
- **返回值**:`{ kind: "handled", message? }`(`message` 显示给用户,不进对话)或 `{ kind: "prompt", text }`(改发这段文本作为用户提示);返回 `undefined` 等同 `{ kind: "handled" }`;其它形状按错误处理,`run` 自己抛的错也按错误显示,不影响会话。
|
|
79
|
+
- **在哪些模式可用**:交互、line、RPC、`-p`、ACP。提示整段形如 `/名 参数` 且命中扩展命令时在 ama 内分派,`prompt` 一律改发其 `text`;`handled` 按模式给出 `message`,都不开模型回合:
|
|
80
|
+
- RPC:`message` 作为 `notification` 事件发出,返回 `disposition: "handled"`。
|
|
81
|
+
- `-p`:`message` 写到 stdout 后退出 0(`--output-format text` 原样输出,`json` 与 `stream-json` 给一行 `result` 对象,`text` 是 `message`、用量为 0);`run` 抛错时 stderr 一行 `ama: <错误>`,退出码 1。只看命令行给的提示:有管道 stdin 时不分派,整段原样发给模型;`handled` 不读 `--image` 图片。
|
|
82
|
+
- ACP:`message` 作为一条 `agent_message_chunk` 发出,`session/prompt` 以 `end_turn` 结束(用量为 0);`run` 抛错时 `session/prompt` 答复 internal error。`available_commands_update` 列出扩展命令(`args` → `input.hint`),遮住同名提示模板。
|
|
83
|
+
- **列出**:`/help`、交互补全、RPC `get_commands`(`source: "extension"`)。
|
|
84
|
+
|
|
85
|
+
## 信任
|
|
86
|
+
|
|
87
|
+
扩展以进程权限运行,必须先被信任才加载。决定按**文件内容的 sha256**(目录扩展为整棵目录树的 sha256) 记在用户级 `trust.json` 的 `extensions` 段:
|
|
88
|
+
|
|
89
|
+
```json
|
|
90
|
+
{
|
|
91
|
+
"version": 1,
|
|
92
|
+
"entries": [],
|
|
93
|
+
"extensions": [
|
|
94
|
+
{ "path": "/abs/standup.mjs", "sha256": "…", "trusted": true, "at": "2026-11-15T12:00:00.000Z" }
|
|
95
|
+
]
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
决定顺序:
|
|
100
|
+
|
|
101
|
+
1. `--no-trust`:全部扩展都不加载。`--trust` 与 profile 的 `trustProject` **不影响**扩展(它们只管项目级资源)。
|
|
102
|
+
2. 记录里有同路径且哈希相同的条目:按记录的决定(信任则加载,拒绝则跳过)。
|
|
103
|
+
3. 没有记录,或文件内容变了(哈希不符):
|
|
104
|
+
- 交互 / line 模式:启动时**一次性**列出全部待定文件的路径与 sha256 询问(仅本次 / 信任并记住 / 不信任);记住的写入 `trust.json`,内容再变就重新询问。
|
|
105
|
+
- 其它模式(`-p`、RPC、ACP):不加载,写 warning(RPC / `-p` 的 stderr)。要启用,先在交互会话里确认一次。
|
|
106
|
+
|
|
107
|
+
哈希在加载前计算;加载之间文件被替换的竞争不在防护范围内(本地用户目录已是同一信任域)。
|
|
108
|
+
|
|
109
|
+
**目录扩展**的哈希覆盖整棵目录树,`trust.json` 记录形状不变(`path` 是目录,`sha256` 是树哈希),目录里任何文件改了都会重新询问,所以多文件扩展应写成目录,而不是只信任入口:
|
|
110
|
+
|
|
111
|
+
- 参与哈希的是目录下每个常规文件,按「正斜杠相对路径」的字节序排序(与平台、遍历与创建顺序无关),每项依次写入 `F <路径字节数> <路径> <内容字节数>\n` 与内容字节,前面加固定前缀 `armadra-ext-tree-v2\n`。路径与内容都带长度前缀,不会拼出歧义。
|
|
112
|
+
- **不跳过任何项**(包括 `.git` 与 `node_modules`):被跳过的文件照样能被 `import`,跳过就成了绕过信任的口子。
|
|
113
|
+
- 符号链接**跟随**:按目标的内容计入(指向目录外也一样),指向目录的链接递归进去;成环或断开的链接按「读不了」跳过。socket、fifo 等忽略。
|
|
114
|
+
- 上限:最多 2000 个文件、2000 个目录、32 层深、总计 50 MiB。超出时该扩展被跳过,原因是「目录扩展过大」,不会询问也不会加载。
|
|
115
|
+
- 交互询问与 `ama doctor` 都会把目录标出来。
|
|
116
|
+
|
|
117
|
+
单文件扩展照旧:只校验该文件本身,它 `import` 的其它文件不计入哈希;需要多文件时请改用目录扩展,或把依赖打包进单个文件。
|
|
118
|
+
|
|
119
|
+
## `ama doctor`
|
|
120
|
+
|
|
121
|
+
`ama doctor` 有「本地扩展」一节:列出每个文件的路径、sha256 前 12 位与状态——**启用**(记录匹配且已信任),或**跳过**及原因(未信任、内容自信任后已改变、已拒绝、读不了文件、目录扩展过大、`--no-trust`);目录扩展标 `[目录]`,哈希前缀是树哈希。doctor 只读文件与信任记录,**不执行扩展代码**,所以「加载失败 / 版本不等 / `create()` 失败 / 未返回适配器」这类只有加载时才知道的原因不在 doctor 里,它们作为 warning 出现在启动输出里(`-p` 与 RPC 写 stderr,交互模式显示在消息区)。
|
|
122
|
+
|
|
123
|
+
## 典型用例
|
|
124
|
+
|
|
125
|
+
| 用例 | 做法 |
|
|
126
|
+
| ---------- | ------------------------------------------------------------------------------------ |
|
|
127
|
+
| 下一步建议 | `events.on("agent_settled")` 后根据最后的回复给出 2–3 条建议,`ui.notify` 显示 |
|
|
128
|
+
| 桌面通知 | `agent_settled` 与 `tool_approval_requested` 时向终端写 OSC 777 通知序列 |
|
|
129
|
+
| git 检查点 | `turn_start` 时 `git stash create` 记录一个不改工作区的检查点,配合 `/undo` 命令回滚 |
|
|
130
|
+
| 自定义命令 | `commands.register({ name: "standup", … })` 汇总今天的会话 |
|
|
131
|
+
| 保温策略 | `cache.onWarmingDecision` 按时段或预算否决空闲保温 |
|
|
132
|
+
|
|
133
|
+
## 不做的事
|
|
134
|
+
|
|
135
|
+
- **不开放渲染层**:扩展不能替换或插入界面组件、不能接管按键。界面上的表达只有消息区通知、状态栏键值与斜杠命令的输出文本。
|
|
136
|
+
- 不做能力声明(信任对话框不按能力展示)、不做命令参数补全、不做扩展之间的依赖顺序(只有文件名排序)。
|
package/docs/reference/acp.md
CHANGED
|
@@ -131,7 +131,7 @@ Zed 的配置(`settings.json`):
|
|
|
131
131
|
|
|
132
132
|
- `mode` 与 `modes` / `session/set_mode` 是同一状态:规范要求客户端有 `configOptions` 时用它代替 `modes`(Zed 给了就不再看 `modes`),所以模式也放进配置项;`modes` 照给,留给只认 `modes` 的客户端。设 `mode` 与 `session/set_mode` 一样按会话记,变化时 `current_mode_update` 与 `config_option_update` 都发。没有 boolean 型配置项。
|
|
133
133
|
- `session/set_config_option`:`mode` → 改权限模式,`model` → 切模型,`thinking` → 改思考级别,答复是全部配置项的新状态;未知 id、找不到的模型、不认识的级别回 invalid params(-32602)。模型或级别在会话里变了(含 plan 流程自动切换)时发 `config_option_update`。
|
|
134
|
-
- 命令表:Skill 列为 `skill:<名字>`,提示模板列为 `<名字>`(frontmatter 的 `argument-hint` 作 `input.hint`)。这两类在 prompt 文本里本来就会展开(`/skill:<名字> …`、`/<名字>
|
|
134
|
+
- 命令表:Skill 列为 `skill:<名字>`,提示模板列为 `<名字>`(frontmatter 的 `argument-hint` 作 `input.hint`)。这两类在 prompt 文本里本来就会展开(`/skill:<名字> …`、`/<名字> …`)。[#303] 宿主适配器与本地扩展注册的命令也列为 `<名字>`(`args` 作 `input.hint`),遮住同名提示模板;`session/prompt` 的文本整段形如 `/<名字> 参数` 且命中时由 ama 分派:`handled` 把 `message` 作为一条 `agent_message_chunk` 发出并以 `end_turn` 结束(不开模型回合,用量为 0),`prompt` 改发其 `text`,命令抛错则答复 internal error。`/new`、`/compact` 等内置斜杠命令在 ACP 下不执行,不列。
|
|
135
135
|
|
|
136
136
|
### 退出
|
|
137
137
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# 宿主适配器 API(@armadra/agent/host)
|
|
2
2
|
|
|
3
|
-
宿主适配器是一个本地 JS 模块,ama 启动时加载它并交给它一个 `HostApi`:它可以注册工具、追加系统提示、观察事件、回答审批、注入用户消息、在界面上显示通知与状态。Armadra 画布就是以宿主适配器的形式接入的(画布工具 `canvas_*` / `context_*` 都由适配器注册)。类型定义在 `src/host/types.ts`,从 `@armadra/agent/host` 导出,`HOST_API_VERSION = 1`、`HOST_API_MINOR =
|
|
3
|
+
宿主适配器是一个本地 JS 模块,ama 启动时加载它并交给它一个 `HostApi`:它可以注册工具、追加系统提示、观察事件、回答审批、注入用户消息、在界面上显示通知与状态。Armadra 画布就是以宿主适配器的形式接入的(画布工具 `canvas_*` / `context_*` 都由适配器注册)。类型定义在 `src/host/types.ts`,从 `@armadra/agent/host` 导出,`HOST_API_VERSION = 1`、`HOST_API_MINOR = 4`(host-api 1.4,见「版本」)。设计依据见 [design.md](../design/design.md) §6.2、§6.3、§11.1 第 13 步。
|
|
4
4
|
|
|
5
5
|
## 模块形状
|
|
6
6
|
|
|
@@ -56,34 +56,37 @@ export function create(api) {
|
|
|
56
56
|
| ------ | ------------ | ----------------------------------------------------------------- |
|
|
57
57
|
| 0 | — | 现有全部成员(`cache?`、`runners?.provide` 等可选面同样要先判断) |
|
|
58
58
|
| 1 | 0.11.0 | `minor`、`runners?.onDone`(见「runner 完成事件」) |
|
|
59
|
-
| 2 |
|
|
60
|
-
| 3 |
|
|
59
|
+
| 2 | 0.16.0 | `resources?.register` / `resources?.list`(见「宿主资源」) |
|
|
60
|
+
| 3 | 0.16.0 | `session_start.uncleanEnd`(`reason: "resume"` 时,#268) |
|
|
61
|
+
| 4 | 未发布 | `commands?.register` / `commands?.list`(见「斜杠命令」,#301) |
|
|
61
62
|
|
|
62
63
|
## HostApi
|
|
63
64
|
|
|
64
|
-
| 成员 | 说明
|
|
65
|
-
| ----------------------------------------------- |
|
|
66
|
-
| `version` | `HOST_API_VERSION`
|
|
67
|
-
| `agent` | `{ name: "ama", version }`
|
|
68
|
-
| `env` | 启动时环境变量的冻结副本
|
|
69
|
-
| `mode` | `interactive` / `line` / `print` / `rpc`
|
|
70
|
-
| `session.id()` / `file()` / `cwd()` / `model()` | 当前会话(换会话后跟随新会话);`file()` 在首次请求落盘前为 `undefined`
|
|
71
|
-
| `tools.register(tool)` | 注册工具(形状见下);名字须匹配 `^[a-z][a-z0-9_]{1,63}$`,同名已存在抛 `tool_exists`。建议加前缀(`canvas_*`)
|
|
72
|
-
| `tools.disable(name)` | 隐藏内置工具(如 Armadra 禁用 `task`),系统提示的工具节随之不再列它
|
|
73
|
-
| `tools.list()` | 当前全部工具名
|
|
74
|
-
| `instructions.add(source)` | 追加到系统提示最后的 `host` 节;`{ kind: "file", path }` 或 `{ kind: "text", text, name? }`
|
|
75
|
-
| `events.on(name, handler)` | 观察事件(下表),返回注销函数
|
|
76
|
-
| `approvals.setBroker(broker)` | 设置审批回答者(见「审批」)
|
|
77
|
-
| `messages.sendUser(text, origin?)` | 以 user 消息注入:空闲时开始一次运行(`"started"`),运行中按 steer 入队(`"queued"`);`origin` 缺省 `"host"`,落盘在消息上,界面标 `↳ host`
|
|
78
|
-
| `ui.notify(message, level?)` | 交互 / line 模式进消息区;rpc 模式变成 `notification` 事件(stderr 另有一份);print 模式写 stderr
|
|
79
|
-
| `ui.setStatus(key, text?)` | 状态栏的宿主项;`text` 为空或缺省则移除该键
|
|
80
|
-
| `log(level, message, detail?)` | 日志;`warn` / `error` 写 stderr
|
|
81
|
-
| `cache?.onWarmingDecision(handler)` | 缓存保温的否决钩子(见「缓存保温」);可选面,旧版本运行时没有它,用前判断 `api.cache !== undefined`
|
|
82
|
-
| `runners?.provide(runner)` | 注入子 Agent runner:`id` 即 `task(agent=<id>)` 的名字,`description` 进 task
|
|
83
|
-
| `runners?.onDone(handler)` |
|
|
84
|
-
| `resources?.register(resource)` | 注册一个只读资源命名空间 `scheme://…`,内置 `read` 读它(见「宿主资源」);只能在 `create()` 里调用;host-api 1.2 起,用前判断 `api.resources !== undefined`
|
|
85
|
-
| `resources?.list()` | 已注册的 scheme,按名排序
|
|
86
|
-
| `
|
|
65
|
+
| 成员 | 说明 |
|
|
66
|
+
| ----------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
67
|
+
| `version` | `HOST_API_VERSION` |
|
|
68
|
+
| `agent` | `{ name: "ama", version }` |
|
|
69
|
+
| `env` | 启动时环境变量的冻结副本 |
|
|
70
|
+
| `mode` | `interactive` / `line` / `print` / `rpc` |
|
|
71
|
+
| `session.id()` / `file()` / `cwd()` / `model()` | 当前会话(换会话后跟随新会话);`file()` 在首次请求落盘前为 `undefined` |
|
|
72
|
+
| `tools.register(tool)` | 注册工具(形状见下);名字须匹配 `^[a-z][a-z0-9_]{1,63}$`,同名已存在抛 `tool_exists`。建议加前缀(`canvas_*`) |
|
|
73
|
+
| `tools.disable(name)` | 隐藏内置工具(如 Armadra 禁用 `task`),系统提示的工具节随之不再列它 |
|
|
74
|
+
| `tools.list()` | 当前全部工具名 |
|
|
75
|
+
| `instructions.add(source)` | 追加到系统提示最后的 `host` 节;`{ kind: "file", path }` 或 `{ kind: "text", text, name? }` |
|
|
76
|
+
| `events.on(name, handler)` | 观察事件(下表),返回注销函数 |
|
|
77
|
+
| `approvals.setBroker(broker)` | 设置审批回答者(见「审批」) |
|
|
78
|
+
| `messages.sendUser(text, origin?)` | 以 user 消息注入:空闲时开始一次运行(`"started"`),运行中按 steer 入队(`"queued"`);`origin` 缺省 `"host"`,落盘在消息上,界面标 `↳ host` |
|
|
79
|
+
| `ui.notify(message, level?)` | 交互 / line 模式进消息区;rpc 模式变成 `notification` 事件(stderr 另有一份);print 模式写 stderr |
|
|
80
|
+
| `ui.setStatus(key, text?)` | 状态栏的宿主项;`text` 为空或缺省则移除该键 |
|
|
81
|
+
| `log(level, message, detail?)` | 日志;`warn` / `error` 写 stderr |
|
|
82
|
+
| `cache?.onWarmingDecision(handler)` | 缓存保温的否决钩子(见「缓存保温」);可选面,旧版本运行时没有它,用前判断 `api.cache !== undefined` |
|
|
83
|
+
| `runners?.provide(runner)` | 注入子 Agent runner:`id` 即 `task(agent=<id>)` 的名字,`description` 进 task 工具描述;有宿主适配器时 ama 不自起外部 CLI,只认这里注入的;宿主适配器与本地扩展共用一张表,模块之间同 id 先到先得,后来者抛 `runner_exists`,同一模块再次提供替换自己的(#306);返回注销函数 |
|
|
84
|
+
| `runners?.onDone(handler)` | 本模块提供的 runner 每轮结束时回调(见「runner 完成事件」),返回注销函数;host-api 1.1 起,用前判断 `typeof api.runners?.onDone === "function"` |
|
|
85
|
+
| `resources?.register(resource)` | 注册一个只读资源命名空间 `scheme://…`,内置 `read` 读它(见「宿主资源」);只能在 `create()` 里调用;host-api 1.2 起,用前判断 `api.resources !== undefined` |
|
|
86
|
+
| `resources?.list()` | 已注册的 scheme,按名排序 |
|
|
87
|
+
| `commands?.register(command)` | 注册斜杠命令(见「斜杠命令」);宿主适配器与本地扩展都可用,返回注销函数;host-api 1.4 起,用前判断 `api.commands !== undefined` |
|
|
88
|
+
| `commands?.list()` | 已注册的命令名(全部模块),按注册顺序 |
|
|
89
|
+
| `minor?` | `HOST_API_MINOR`(见「版本」);旧运行时缺省 |
|
|
87
90
|
|
|
88
91
|
`create()` 期间会话还没组装完:`session.*` 返回启动时确定的值,`sendUser` 会以 `busy` 拒绝——要在启动时发消息,等 `session_start` 事件之后再调用。
|
|
89
92
|
|
|
@@ -119,6 +122,31 @@ interface ToolResult {
|
|
|
119
122
|
|
|
120
123
|
导出的 `ToolPermission` 类型多了 `"network"`(内置 `web_fetch` 用,按 execute 判定、plan 下询问,见 [permissions.md](../guides/permissions.md)「网络类」);宿主经 `tools.register` 注册的工具仍只接受 `read` / `write` / `execute`,`HOST_API_VERSION` 不变。
|
|
121
124
|
|
|
125
|
+
## 斜杠命令
|
|
126
|
+
|
|
127
|
+
`api.commands.register({ name, description, args?, run(args, ctx) })`(host-api 1.4):注册 `/名` 命令,返回注销函数。
|
|
128
|
+
|
|
129
|
+
```ts
|
|
130
|
+
interface HostCommand {
|
|
131
|
+
name: string; // ^[a-z][a-z0-9-]{0,63}$,不带斜杠
|
|
132
|
+
description: string; // /help 与补全里显示
|
|
133
|
+
args?: string; // 参数占位,如 "[day]"
|
|
134
|
+
run(args: string, ctx: HostCommandContext): HostCommandResult | Promise<HostCommandResult>;
|
|
135
|
+
}
|
|
136
|
+
interface HostCommandContext {
|
|
137
|
+
mode: "interactive" | "line" | "print" | "rpc";
|
|
138
|
+
session: { id(): string; cwd(): string };
|
|
139
|
+
}
|
|
140
|
+
type HostCommandResult =
|
|
141
|
+
{ kind: "handled"; message?: string } | { kind: "prompt"; text: string } | undefined;
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
- 与内置命令、别名(`quit`、`q`、`?`)、交互界面命令(`tree`、`permissions`)或已注册的命令同名时 `register` 抛错(`command_exists`);名字格式、`description`、`run` 不合法抛 `invalid_arguments`。
|
|
145
|
+
- 分派在内置命令之后、提示模板之前;`args` 是命令名之后的整段文本(已去首尾空白)。
|
|
146
|
+
- `handled` 的 `message` 显示给用户(交互 / line 进消息区,RPC 为 `notification` 事件),不进对话;`prompt` 把 `text` 改发为用户提示;返回 `undefined` 等同 `{ kind: "handled" }`;其它形状或 `text` 为空抛错,按命令错误显示。
|
|
147
|
+
- 交互、line、RPC 三种模式分派;`-p` 与 ACP 不分派斜杠命令。`/help`、补全与 RPC `get_commands`(`source: "extension"`)列出。
|
|
148
|
+
- 宿主适配器与本地扩展共用一张表,命令名全局唯一。
|
|
149
|
+
|
|
122
150
|
## 事件
|
|
123
151
|
|
|
124
152
|
`events.on` 的处理器只观察:抛错只记日志,不影响运行;处理器依次调用并等待,`session_shutdown` 被 await(退出前可以做清理)。
|
|
@@ -151,7 +179,7 @@ interface ToolResult {
|
|
|
151
179
|
- `ask` 返回 `"allow"` / `"deny"` / `"allow_session"` 作答;返回 `undefined` 交给下一个回答者;抛错按 deny。
|
|
152
180
|
- `request`:`requestId`、`toolName`、`input`、`reason: "mode" | "dangerous" | "hook"`、`hookReason?`、`preview?`(执行前预览,见 [rpc.md](rpc.md)「审批」)、`context?`(`depth > 0` 表示来自 `task` 子 Agent;codemode 内层调用带 `parentToolCallId`)。
|
|
153
181
|
- 超时(缺省 10 分钟,`AMA_APPROVAL_TIMEOUT_MS`)或运行被中断时 `signal` abort,结论为 deny。审批串行,同一时刻只有一个请求在等。
|
|
154
|
-
- 时机:broker 每次审批时现取,可以在 `create()`
|
|
182
|
+
- 时机:broker 每次审批时现取,可以在 `create()` 里设,也可以之后任何时候设或替换;同一个模块里最后一次 `setBroker` 生效。有多个模块(宿主适配器 + [本地扩展](../guides/extensions.md))时只认第一个设置者,其余的调用被忽略并 warning。
|
|
155
183
|
- 宿主 broker 只决定「谁来回答 ask」,不能放宽 deny 规则、命令式 Hook 的 deny 与危险命令识别([design.md](../design/design.md) §6.3)。
|
|
156
184
|
|
|
157
185
|
## 缓存保温
|
package/docs/reference/rpc.md
CHANGED
|
@@ -44,6 +44,8 @@
|
|
|
44
44
|
| `abort` | — | `{}`(回到空闲后应答;不清队列) |
|
|
45
45
|
| `clear_queue` | — | `{ steering: string[], followUp: string[] }`(被清掉的文本) |
|
|
46
46
|
|
|
47
|
+
`prompt` 的 `message` 形如 `/名 参数` 且命中宿主适配器 / 本地扩展注册的命令(`get_commands` 里 `source: "extension"`)时,ama 先分派它:`handled` 把命令的 `message` 作为 `notification`(`level: "info"`)发出并应答 `disposition: "handled"`,不开回合;`prompt` 改发命令给出的文本;命令出错按失败应答。
|
|
48
|
+
|
|
47
49
|
提示类命令**不等运行结束**:会话开始运行(`before_agent_start` / `agent_start`)、消息入队或被处理(例如斜杠命令、Hook 阻止)后立刻应答,运行进展走事件。运行中发 `prompt` 且不带 `streamingBehavior` → 失败,`code: "busy"`;带上 `steer` / `followUp` 则入队。应答发出之后的运行失败以 `{"type":"notification","level":"error","message":…}` 报告。
|
|
48
50
|
|
|
49
51
|
**打断并立即发送**:`prompt` / `steer` 带 `interrupt: true`(优先于 `streamingBehavior`)时,运行中先取走排队的 steer、中止当前回合(模型流断开,正在执行的工具按中断收尾,每个工具调用恰有一个结果 `aborted by user`,被打断的 assistant 消息以 `stopReason: "aborted"` 落盘),再立刻以「排队的 steer… + 本条」(空行拼接)开新回合,应答 `{ disposition: "started" }`;新回合的 user 消息 `origin: "interrupt"`。排在本轮之后的 followUp 留在队列,新回合结束后照常投递;后台子 Agent 不受影响。空闲时等同不带。`interrupt` 不是布尔 → `invalid_arguments`;运行中但本条与排队的 steer 都为空 → `invalid_arguments`(不中断)。事件顺序:`queue_update`(steer 被取走)→ 旧回合 `message_end`(aborted)→ `agent_settled` → `agent_start` → 应答 → 新的 user `message_end` ……(黄金记录 `test/fixtures/rpc/interrupt.out.jsonl`)。新请求以被打断那次请求的全部消息为前缀,缓存照常命中。SDK 对应 `session.prompt(text, { interrupt: true })` / `session.steer(text, { interrupt: true })`。
|
|
@@ -119,13 +121,13 @@
|
|
|
119
121
|
|
|
120
122
|
### 工具、权限、发现
|
|
121
123
|
|
|
122
|
-
| 命令 | 参数 | `data`
|
|
123
|
-
| --------------------- | ---------------------------------------------------------------------- |
|
|
124
|
-
| `get_tools` | — | `{ tools: { name, description, parameters, permission, active }[] }`(注册表全部工具,`active` 表示模型当前能看到)
|
|
125
|
-
| `set_active_tools` | `names: string[]` | `{ names }`(生效后的活动工具名)
|
|
126
|
-
| `set_permission_mode` | `mode: plan \| allowlist \| default \| auto-edit \| auto \| full-auto` | `{ mode }`(未知模式 → `invalid_arguments`)
|
|
127
|
-
| `get_commands` | — | `{ commands: { name, description?, source: "builtin" \| "template" \| "skill" }[] }`;Skill 名写作 `skill
|
|
128
|
-
| `get_skills` | — | `{ skills: { name, description, location, … }[] }`(已发现的 Skill;`location` 是 SKILL.md 路径)
|
|
124
|
+
| 命令 | 参数 | `data` |
|
|
125
|
+
| --------------------- | ---------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
126
|
+
| `get_tools` | — | `{ tools: { name, description, parameters, permission, active }[] }`(注册表全部工具,`active` 表示模型当前能看到) |
|
|
127
|
+
| `set_active_tools` | `names: string[]` | `{ names }`(生效后的活动工具名) |
|
|
128
|
+
| `set_permission_mode` | `mode: plan \| allowlist \| default \| auto-edit \| auto \| full-auto` | `{ mode }`(未知模式 → `invalid_arguments`) |
|
|
129
|
+
| `get_commands` | — | `{ commands: { name, description?, source: "builtin" \| "template" \| "skill" \| "extension" }[] }`;Skill 名写作 `skill:<名>`;`extension` 是宿主适配器 / 本地扩展经 `commands.register` 注册的命令(#301);只列当前可用的内置命令(记忆关闭时没有 `memory`,task 不可调时没有 `tasks`) |
|
|
130
|
+
| `get_skills` | — | `{ skills: { name, description, location, … }[] }`(已发现的 Skill;`location` 是 SKILL.md 路径) |
|
|
129
131
|
|
|
130
132
|
### 计划与任务(第五波)
|
|
131
133
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@armadra/agent",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.19.0",
|
|
4
4
|
"description": "A coding and coordination agent that runs standalone or embedded in Armadra",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"keywords": [
|
|
@@ -94,10 +94,12 @@
|
|
|
94
94
|
"check:deps": "node scripts/check-no-deps.mjs",
|
|
95
95
|
"check:i18n": "node scripts/check-i18n.mjs",
|
|
96
96
|
"check:docs": "node scripts/check-doc-links.mjs",
|
|
97
|
+
"check:portability": "node scripts/check-portability.mjs",
|
|
97
98
|
"release:check": "node scripts/release-check.mjs",
|
|
99
|
+
"prepare": "node scripts/hooks/install.mjs",
|
|
98
100
|
"bench:presets": "node scripts/bench-presets.mjs",
|
|
99
101
|
"bench:cache": "node scripts/cache-experiment.mjs",
|
|
100
|
-
"ci": "pnpm typecheck && pnpm fmt:check && pnpm check:deps && pnpm check:i18n && pnpm check:docs && pnpm release:check && pnpm test && pnpm build && node dist/bundle/ama.cjs --version"
|
|
102
|
+
"ci": "pnpm typecheck && pnpm fmt:check && pnpm check:deps && pnpm check:i18n && pnpm check:docs && pnpm check:portability && pnpm release:check && pnpm test && pnpm build && node dist/bundle/ama.cjs --version"
|
|
101
103
|
},
|
|
102
104
|
"dependencies": {},
|
|
103
105
|
"devDependencies": {
|