复制

传输契约与数据边界

这篇文档说明 contracts 如何定义 renderer ↔ preload/main ↔ runtime 的传输,以及这些 DTO 与本地持久化的关系。

RuntimeRequestInput

Renderer 能交给 main 的 runtime 请求只有:

TypeScript
type RuntimeRequestInput = {
  path: string;
  method?: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
  body?: unknown;
  requestId?: string;
  responseMode?: 'body' | 'feature-operation';
};

requestId 关联 renderer 的取消信号与 main 持有的本地请求;responseMode 让 typed Feature failure 跨 IPC 保留结构化错误。它们都不授予任意网络访问能力。

它故意不包含:

  • Host/port。
  • Authorization。
  • 任意 headers。
  • Redirect/proxy。
  • Raw socket。

Main 的 RuntimeHost 再限制 path 为 /health 或 /v1/*。

DesktopRuntimeClient

http.ts 用方法级 interface 描述 renderer 可用的 runtime 能力。方法按数据域覆盖:

  • Threads、messages、turns、queue、context。
  • Attachments。
  • Core config 与共享模型选择投影。
  • Projects、files、search、workspace status。
  • Goal、Review、Model Provider、Skills、Usage、Memory、MCP、Plugin/Hook 管理、Conversation Debug、Workspace Dependencies 等业务通过独立 typed Feature operations,不扩展统一 client。
  • Approvals。

Renderer 的 services/runtime-client/client.ts 必须完整实现它。纵向 Feature 依赖自己的 operation contract 与宿主 transport,不依赖统一 interface method 或具体 path。

SetsunaDesktopBridge

desktop.ts 和相关领域文件定义 window.setsunaDesktop:

  • Runtime bridge。
  • Data-root。
  • Desktop/system。
  • Feature-owned bridge(Browser、Computer Use、Review、Terminal、Network Proxy、Plugin Management、Updater、Windows Sandbox、Workspace Apps)通过对应 Feature contracts 与 host bridge 显式组合。
  • Window controls。

Bridge 类型约束 preload 和 renderer;main handler 的输入输出也应复用同一 DTO。

事件式方法返回 () => void cleanup,避免暴露 Electron listener。

REST 与 SSE

REST

适合:

  • 初始 snapshot。
  • CRUD。
  • 可重拉列表。
  • 配置/状态。
  • 明确命令响应。

Thread SSE

传输 StoredThreadEvent(Core 事件、Feature envelope 与可读取的历史事件),main/preload 向 renderer 转发 RuntimeEventBatch,必要时携带 canonical snapshot resync。订阅输入:

  • threadId
  • sinceSeq

恢复依赖 store 历史和 reducer。

App-server SSE

发送 SWE/app-server notification,与 thread event SSE 不同。两者不能共享 sequence 或 reducer。

常见 REST 路由组

要找的操作 一个可以跟读的请求 定义与实现
当前会话的运行状态 GET /v1/threads/:id/events 按线程序号续接;POST /v1/threads/:id/queued-turn-inputs 将输入排队 轮次与队列路由
读取模型设置、从供应商发现模型 GET /v1/features/model-provider/settings 读取设置;POST /v1/features/model-provider/models 发现模型 Model Provider 的 operation 契约
读取待处理审批 GET /v1/approvals Core 审批入口
兼容历史客户端 旧客户端仍可通过 GET /v1/memories 读取记忆;新设置界面通过 Memory Feature 操作 兼容路由 · Memory 操作定义

新增 /v1/features/* 操作时,在所属 Feature 的 contracts 定义方法、路径、输入和结果,再从同包的 runtime 入口注册。其他 Core 请求从 REST 分发入口 查找。

按会话或时间查询用量属于 Usage Feature。分页与汇总的输入、返回值由所属契约定义。

Input、State 与 Secret

配置 contract 常有三类:

  • Input:本次创建/保存,可含新 secret。
  • Patch:部分更新,必须区分未提供与清空。
  • State/Summary:返回 UI,隐藏 secret 和私有路径。

例子:

  • Provider API key state 只返回 set/preview。
  • MCP list 只返回 env/header key,不返回 value。
  • Plugin marketplace 不返回 Bundle path、Hook command 或安装目录。
  • Credential vault 不返回 secret list payload。

Runtime store 和 main 必须再次执行脱敏,不能依赖 renderer “不显示”。

哪些磁盘字段可以对外暴露

持久化格式和跨进程返回值分别维护。线程恢复需要保存消息和事件;renderer 读取的是按权限投影后的线程快照。位置指针、所有权标记、文件锁和私有路径留在 main 或存储 adapter。查文件位置、恢复来源与迁移步骤,分别见 Runtime 存储和桌面数据根。

兼容策略

Additive field

  • Type 标为 optional 或 store normalization 提供 default。
  • 旧数据 lazy 读取,不为小字段强制全库 rewrite。
  • Snapshot reducer保留合法未知 additive metadata。

Renamed/removed field

  • Store 读取时兼容旧字段。
  • 对外只返回新 state。
  • 明确何时停止写旧字段。
  • 删除必须考虑旧版本 downgrade 是否仍能忽略。

Event

Event type 是持久化协议,不能随意重命名。必要时新增 event 并让 reducer兼容旧 event。

SQLite

Message/event payload 中的 additive JSON 字段不一定要求 schema version 变化。只有表/索引/约束变化才修改 SQLite migration。

Data-root contract

data-root.ts 定义:

  • Boot/maintenance state。
  • Target scan summary。
  • Manifest category 和 issue。
  • Migration plan/progress。
  • Recovery action。
  • Retained backup inspection/cleanup。
  • Runtime migration readiness。

UI 只根据这些结构化状态展示,不能解析 main 的自由格式日志来推进迁移。

Workspace contract

workspace.ts 区分:

  • Project identity/path summary。
  • Directory entry list。
  • File-name search。
  • Content search。
  • File read/preview。
  • Workspace status。

路径字段的语义要明确是 workspace-relative 还是绝对路径。Renderer 通常只需要 relative path;绝对根用于用户明确添加的 project summary,不得成为任意文件访问授权。

Attachment 与 asset

  • 本地文件由 preload 从 Electron File 提取可信路径,再通过 main-only link contract 登记;renderer 不能提交任意路径。
  • 没有本地路径的图片使用受限 filename/MIME/data input 写入受管存储。
  • Runtime 返回 managed asset ID 和 metadata。
  • Message 只引用已认领 attachment。
  • Generated image 使用独立 managed store;Artifact 成品结果由 packages/features/artifact/src/contracts 定义并始终引用受管 workspace 内的相对路径。
  • Renderer 不能把任意本地路径伪装成 runtime asset ID。

验证

修改传输 contract 后至少:

  1. pnpm typecheck
  2. Contracts unit tests。
  3. Runtime server integration。
  4. Renderer client test。
  5. 涉及 preload 时运行 main 对应 tests。

数据格式变化还要增加旧 fixture 和损坏输入测试。