# acde RFC 0.1

| | |
| --- | --- |
| 版本 | 0.1 |
| 状态 | 草案 (0.x): 下一个次版本可以做不兼容的改动 |
| 契约级别 (`api-level`) | 1 |
| 来源 | libacde `acde-api/` (`@acde/api` 0.0.11, 提交 `09cf894`), 原样冻结 |
| 冻结 | 2026-11-21 (最初以 1.0 发布, 当天改号为 0.1; 见 [变化](https://docs.dopame.ai/zh/rfc/overview.md#变化)) |
| 文件 | `ssh://git@gitea.dopamine:2333/AC/acde-RFC.git` 的 `rfc/0.1/` |
| 真运行时上的一致性 | 还没跑过 |

## 1. 范围

本版本规定运行时注入页面的 `acde:*` 内置模块: 有哪些模块, 每个模块导出什么, 导出对象有哪些成员, 成员是什么类型, 哪些行为可以被测试检查. web 标准接口 (DOM, `fetch`, 存储, ...) 不在范围内, 它们照 web 标准, 不许改动.

规范由三部分组成, 冲突时按这个顺序为准:

1. `api/`: TypeScript 声明 (`.d.ts`), 唯一正本.
2. `RFC.md`: 声明表达不了的规则 (导出形状, 可见性, 错误码, 版本), 本页给出摘要.
3. `surface.json`: 由 `api/` 生成 (`bun run extract 0.1`), 给运行时验证器用; 不手改.

## 2. 模块导入

- 只能经 ES 模块说明符 `acde:<名>` 导入, 像 `node:fs`. 全局对象上没有 `acde`.
- 每个模块有一个默认导出和一个同名的具名导出, 二者是**同一个对象** (`===`). 例外: `acde:window` 的具名导出叫 `window`.
- 导出对象由运行时进程内注入, 调用直接进运行时, 不经 RPC.
- 声明为可选 (`?`) 的成员可以没有 (值为 `undefined`); 有就必须符合声明的类型.

## 3. 可见性 (`modules.json`)

| 类别 | 模块 | 谁能导入 |
| --- | --- | --- |
| `capability` | fs, terminal, network, system, bluetooth, audio, devices, power, input, display, office | 清单 `capabilities` 里声明了, 且 `acde:capabilities` 的 `get(名).state` 为 `available` 的应用 |
| `app` | launch, apps, open, openPath, handlers, window, capabilities | 所有应用 |
| `shell` | windows, packages, session, desktops, inspect, desktop, setDefault, permissions | 只有桌面和桌面声明的服务 |

导入不允许的模块必须失败 (`import()` reject).

## 4. 模块

共 26 个模块; `surface.json` 另记录 157 个导出接口. 标 `?` 的成员是可选的.

| 模块 | 类别 | 导出 | 成员 |
| --- | --- | --- | --- |
| `acde:fs` | capability | 对象 | abortWrite, commitWrite, copy, delete, download, list, locate, mkdir, openPath, openWrite, preview, readBytes, readRange, readText, rename, revoke, roots, stat, truncateWrite, writeChunk, writeFile |
| `acde:terminal` | capability | 对象 | close, input, open, resize, subscribe |
| `acde:network` | capability | 对象 | activate, connect, disconnect, networks, setWifiEnabled, state, watch |
| `acde:system` | capability | 对象 | info, setHostname, setNtp, setTimezone, storage, time, timezones, user |
| `acde:bluetooth` | capability | 对象 | answer, connect, disconnect, forget, on, pair, rename, scan, setBlocked, setDiscoverable, setName, setOnLogin, setPowered, setTrusted, state |
| `acde:audio` | capability | 对象 | moveStream, on, setBalance, setDefault, setMuted, setPort, setProfile, setVolume, state |
| `acde:devices` | capability | 对象 | cancelJob, eject, forgetDevice, jobs, mount, on, pausePrinter, resumePrinter, setAutomount, setDefaultPrinter, setDeviceAutomount, state, unmount |
| `acde:power` | capability | 对象 | configure, hibernate, on, setBrightness, setChargeLimit, setProfile, setScreenOff, setSleep, state, suspend |
| `acde:input` | capability | 对象 | catalog, on, set, state |
| `acde:display` | capability | 对象 | configure, on, setBrightness, setNightLight, state |
| `acde:office` | capability | 对象 | editorPage, new, save, server, state, status |
| `acde:launch` | app | 对象 | close, embed, list, on, start |
| `acde:apps` | app | 对象 | list, on?, open |
| `acde:open` | app | 函数 | |
| `acde:openPath` | app | 函数 | |
| `acde:handlers` | app | 函数 | |
| `acde:window` | app | 对象 (具名导出叫 `window`) | close, host, hostControls, id, minimizable, minimize, mount, on, output, releaseGrab, requestActivate, requestAttention, requestGrab, requestMode, requestState, setBackdrop, setGrabHint, setMenus, setTitle, shortcut, state, unshortcut |
| `acde:capabilities` | app | 对象 | get, level, list, mode, platform, request, window |
| `acde:windows` | shell | 对象 | animate?, close, focus?, hide?, invokeMenu?, kill?, launch, list, on, requestState?, shortcut?, show?, stats?, unshortcut? |
| `acde:packages` | shell | 对象 | install, natives, on, refresh, runNative |
| `acde:session` | shell | 对象 | logout, own, poweroff, reboot, setLanguage |
| `acde:desktops` | shell | 对象 | choose, list |
| `acde:inspect` | shell | 对象 | act, open, query, run, snapshot |
| `acde:desktop` | shell | 对象 | apps, on, pin?, setDoubleClick?, setFrameTheme? |
| `acde:setDefault` | shell | 函数 | |
| `acde:permissions` | shell | 对象 | answer, list, on, pending, revoke |

签名在 `rfc/0.1/api/`; 用法见 [acde: 模块](https://docs.dopame.ai/zh/reference/modules.md).

## 5. `acde:capabilities`

- `level` 等于本版本的 `api-level`: 1.
- `platform` ∈ `linux`, `win32`, `darwin`; `mode` ∈ `session`, `desktop`, `standalone`.
- `list()` 的每一项和 `get(同名)` 的 `name`, `state` 一致. `state` 取值见 `api/types/capabilities.d.ts` 的 `CapabilityState`: `available`, `not-asked`, `asking`, `denied`, `not-declared`, `unsupported`, `service-not-running`, `shell-only`.

## 6. `acde:fs` 的错误

失败时 reject 一个 `Error`, 带 `status` (数字) 和 `code`:

| `code` | `status` | 什么时候 |
| --- | --- | --- |
| `EINVAL` | 400 | 参数不对, 路径出 root, 类型不对, 符号链接 |
| `EACCES` | 403 | 清单没给这个 root, 或系统不让 |
| `ENOENT` | 404 | 不存在 |
| `EEXIST` | 409 | 已经有了 (`create: 'new'`) |
| `ECONFLICT` | 409 | `ifModifiedMs` / `ifVersion` 对不上 |
| `ENOTEMPTY` | 409 | 删非空的文件夹 |
| `EFBIG` | 413 | 超过上限 |
| `EILSEQ` | 400 | `readText` 读到的不是 UTF-8 |
| `ENOSPC` | 507 | 磁盘满 |
| `EIO` | 500 | 别的 |

## 7. 一致性

运行时通过 0.1 = 按 `surface.json` 的表面检查通过, 且 `tests.json` 里适用的测试全部通过. 因能力不可用而跳过的不算失败, 但报告里必须列出. 方法的返回值按声明的返回类型深度校验 (`surface.json` 的 `returns` 与 `types`).

标了 `effects` 的测试会改变可见状态 (开窗口, 记住默认应用), 只在明确要求时跑 (`&effects=1`). 不标的测试只读, 或只动自己私有文件夹里的临时文件.

| 测试 | 范围 | 需要 | 检查 |
| --- | --- | --- | --- |
| `surface_app-0.1` | app | | 每个可用的 `app` 和 `capability` 模块对照 `surface.json` |
| `surface_shell-0.1` | shell | | 每个 `shell` 模块对照 `surface.json` |
| `capabilities_level-0.1` | app | | `level` 等于契约级别; `window.backdrop` 是已知取值 |
| `capabilities_status-0.1` | app | | `list()` 和 `get()` 一致; 每个状态是已知取值 |
| `window_self-0.1` | app | | 具名导出 `window` 就是默认导出; `id`, `state`, `output`; `on()` 返回退订函数 |
| `fs_roots-0.1` | app | fs | `roots()`: 来源, 模式, 位置 |
| `fs_text_roundtrip-0.1` | app | fs | 在私有 root 里写文本再读回 |
| `fs_errors-0.1` | app | fs | 每种错误的 `code` 和 `status` |
| `terminal_lifecycle-0.1` | app | terminal | `open`, `input`, `resize`, `close`; 事件类型 |
| `network_state-0.1` | app | network | `state()`, `networks()` |
| `system_info-0.1` | app | system | `info()`, `storage()`, `time()`, `timezones()`, `user()` |
| `bluetooth_state-0.1` | app | bluetooth | `state()`, 事件 `changed` 和 `pairing` |
| `audio_state-0.1` | app | audio | 每个方向最多一个默认设备 |
| `devices_state-0.1` | app | devices | 最多一台默认打印机; 每台打印机的任务 |
| `power_state-0.1` | app | power | `state()`, 事件 |
| `input_state-0.1` | app | input | `state()`, `catalog()` |
| `display_state-0.1` | app | display | 最多一个主显示器 |
| `office_server-0.1` | app | office | `server()` |
| `launch_list-0.1` | app | | `list()`, 每个 launch 事件 |
| `apps_list-0.1` | app | | `list()` 的 id 不重复 |
| `open_link-0.1` | app | | `open({ kind: 'open-link' })` 返回 `Opened` (effects) |
| `handlers_file-0.1` | app | fs | 文本文件的处理程序: 不重复, 最多一个默认 |
| `openPath_file-0.1` | app | fs | 打开一个文件 (effects) |
| `windows_list-0.1` | shell | | `list()` 的 id 不重复; 有 `stats()` 就调; 每个窗口事件 |
| `packages_list-0.1` | shell | | `natives()`, `refresh()`, 事件 `changed` |
| `session_own-0.1` | shell | | `own()` 稳定不变 |
| `desktops_list-0.1` | shell | | `list()` 的 id 不重复 |
| `inspect_snapshot-0.1` | shell | | `snapshot()`, `query()` |
| `desktop_apps-0.1` | shell | | `apps()`, 桌面事件 |
| `setDefault_roundtrip-0.1` | shell | fs | 设默认应用再读回 (effects) |
| `permissions_list-0.1` | shell | | `pending()`, `list()`, 事件 |

## 8. 版本

0.1 是草案: 0.2 可以对这里的任何内容做不兼容的改动, 0.1 的测试不保证在 0.2 上通过. 0.1 的文件 (`api/`, `modules.json`, `RFC.md`) 不再修改, 勘误进 0.2. 见 [版本规则](https://docs.dopame.ai/zh/rfc/overview.md#版本规则).
