# Moli.Desktop 本地 API 契约 日期:2026-05-02 ## 目标 Moli.Desktop MVP 内嵌本地 HTTP API 和 WebSocket 事件通道,用于脚本、插件调试工具和后续管理端集成读取运行状态并执行受控命令。 API 只绑定回环地址,默认监听: ```text http://127.0.0.1:8765 ``` 端口来自 `%AppData%\DesktopEnhancer\config\appsettings.json` 的 `runtime.apiPort`。端口占用或启动失败时,桌面应用继续启动,并记录 `LocalApiStartFailed` 诊断。 ## 通用约定 - 请求和响应均使用 JSON,字段命名为 camelCase。 - 枚举值使用 camelCase 字符串。 - 命令类接口通过 `IDesktopRuntime` 执行,不直接操作窗口、插件或配置文件。 - 错误响应格式: ```json { "errorCode": "RuntimeCommandFailed", "message": "Runtime command failed." } ``` ## Endpoints | 方法 | 路径 | 用途 | 成功响应 | | --- | --- | --- | --- | | `GET` | `/api/status` | 查询应用、壁纸、插件、组件、诊断和 API 状态 | `ApiStatusResponse` | | `GET` | `/api/plugins` | 查询插件运行状态列表 | `PluginResponse[]` | | `POST` | `/api/plugins/{id}/reload` | 重载指定插件并刷新运行时插件状态 | `ApiStatusResponse` | | `POST` | `/api/wallpaper/enable` | 启用壁纸 | `ApiStatusResponse` | | `POST` | `/api/wallpaper/disable` | 禁用壁纸 | `ApiStatusResponse` | | `POST` | `/api/edit-mode` | 设置或切换编辑模式 | `ApiStatusResponse` | | `POST` | `/api/safe-input-mode` | 设置或切换安全输入模式 | `ApiStatusResponse` | | `POST` | `/api/widgets/{instanceId}/layout` | 更新组件布局、显示、锁定、透明度或点击穿透 | `WidgetResponse` | | `GET` | `/ws/events` | 订阅运行时事件 | WebSocket text JSON | ## 状态查询 ```powershell Invoke-RestMethod -Method Get -Uri http://127.0.0.1:8765/api/status ``` 响应结构: ```json { "isEditMode": false, "isGlobalLocked": false, "isSafeInputMode": false, "api": { "isListening": true, "host": "127.0.0.1", "port": 8765, "errorCode": null, "message": "Listening on http://127.0.0.1:8765" }, "wallpaper": { "isEnabled": true, "isPaused": false, "isMuted": false, "opacity": 1, "clickThrough": true, "pluginId": "sample.wallpaper" }, "plugins": [], "widgets": [], "diagnostics": [] } ``` ## 模式命令 `/api/edit-mode` 和 `/api/safe-input-mode` 支持两种调用方式。 指定目标状态: ```powershell Invoke-RestMethod ` -Method Post ` -Uri http://127.0.0.1:8765/api/edit-mode ` -ContentType application/json ` -Body '{"enabled":true}' ``` 无 body 调用时切换当前状态: ```powershell Invoke-RestMethod -Method Post -Uri http://127.0.0.1:8765/api/edit-mode ``` 也可以通过 query string 指定: ```text POST /api/edit-mode?enabled=true ``` ## 组件布局更新 请求体字段均为可选字段;只发送需要变更的字段。 ```json { "bounds": { "x": 120, "y": 160, "width": 360, "height": 180 }, "monitorId": "primary", "isVisible": true, "isLocked": false, "opacity": 0.9, "clickThrough": true, "scale": 1 } ``` 示例: ```powershell Invoke-RestMethod ` -Method Post ` -Uri http://127.0.0.1:8765/api/widgets/sample.clock.default/layout ` -ContentType application/json ` -Body '{"opacity":0.85,"isVisible":true}' ``` 布局约束由 `ILayoutManager` 统一执行。MVP 约束包括:`width >= 80`、`height >= 40`、`scale` 范围 `0.5..3.0`、`opacity` 范围 `0.1..1.0`,且组件至少保留 `48x48` DIP 在可见工作区内。 ## WebSocket 事件 连接地址: ```text ws://127.0.0.1:8765/ws/events ``` 连接建立后,服务端立即发送: ```json { "name": "api.connected", "timestamp": "2026-05-02T15:20:00.0000000+00:00", "payload": { "isListening": true, "host": "127.0.0.1", "port": 8765 } } ``` 后续事件来自运行时事件总线,响应结构固定为: ```json { "name": "app.statusChanged", "timestamp": "2026-05-02T15:20:01.0000000+00:00", "payload": {} } ``` 当前 smoke test 已验证可接收 `api.connected` 和编辑模式变化相关事件。后续新增运行时事件时,应保持 `name` 可稳定匹配,`payload` 可向后兼容扩展。 ## 验证 启动 `Moli.Desktop` 后执行: ```powershell .\scripts\api-smoke-test.ps1 ``` 预期覆盖: - `/api/status` 可返回状态。 - `/api/edit-mode` 可切换并恢复。 - `/api/safe-input-mode` 可确认并恢复。 - `/api/plugins` 可返回插件列表。 - `/api/widgets/{instanceId}/layout` 可接受组件局部布局更新。