Files
openclawapp/docs/UNIFIED_DELIVERY_ARCHITECTURE.md

5.3 KiB
Raw Permalink Blame History

Unified Delivery Architecture

背景

当前 openclawapp 已具备可用主链路,但消息、pending queue、SSE、refresh、附件、重试之间的机制耦合严重:

  • ChatMessage.status 混合承载本地待发、上传、重试、远端生成、工具执行、流失败等多种维度
  • App 侧同时维护 messageStore、pendingTextQueue、pendingAttachmentQueue、retry stores、stream status stores
  • BFF 侧同时维护 history diff SSE、attachment overlay、alias/redirect、stub message
  • refresh / SSE / local pending / retry 都会直接影响时间线,导致没有单一真相源

核心问题不是某个 reconnect 按钮不够好,而是:

消息结果(Message)与待发操作(Operation)没有分层,App 与 BFF 又同时在做 reconciliation。

目标

建立统一机制:

  1. Message 是结果,不是过程
  2. Operation 是过程,不是消息
  3. BFF 成为 operation/outbox 与 canonical message projection 的唯一协调层
  4. App 只消费 bootstrap + event stream,并用 reducer 应用状态

目标模型

Message

表示会话时间线里的正式实体。

建议字段:

  • messageId
  • sessionId
  • role
  • kind
  • text
  • createdAtMs
  • orderKey
  • remoteState:streaming | final | failed
  • toolName
  • toolCallId
  • attachmentRef

Operation

表示本地/边车待发操作。文本和附件统一纳入同一个 outbox 模型。

建议字段:

  • opId
  • sessionId
  • type: send_text | send_attachment
  • clientRequestId
  • createdAtMs
  • state
  • attemptCount
  • lastError
  • payloadRef
  • correlationToken
  • materializedMessageIds

建议状态:

  • accepted
  • persisted
  • dispatching
  • awaiting_remote_echo
  • materialized
  • failed_retryable
  • failed_terminal
  • cancelled

SessionDetailState

App 侧以会话为单位维护聚合状态,不再让 UI 直接消费多套散落 store。

字段建议:

  • sessionId
  • messages: List<MessageTimelineItem>
  • operations: List<DeliveryOperation>
  • syncState
  • streamState
  • lastBootstrapAtMs
  • lastError

Timeline 投影

UI 时间线不直接渲染底层 message/op,而是渲染投影:

  • MessageTimelineItem.RemoteMessage
  • MessageTimelineItem.PendingOperation
  • MessageTimelineItem.SystemNotice

这样 “发送中/上传中/失败重试” 就是 operation card,不再伪装成正式 message。


协议建议

Bootstrap API

新增或演进:

GET /api/sessions/{sessionId}/bootstrap

返回:

  • messages[]
  • operations[]
  • sessionMeta
  • streamState
  • serverTs

SSE v2

不再暴露 history diff 细碎补丁,而是统一实体事件:

  • session.bootstrap
  • message.upsert
  • message.removed
  • operation.upsert
  • operation.removed
  • session.state
  • stream.health

如果有文本流式增量,可以作为 message.patch,但不再强依赖 started/delta/completed 三件套分散落地。


边界划分

App

负责:

  • UI 渲染
  • 本地缓存 bootstrap 结果
  • 用户命令发起
  • reducer 应用事件

不再负责:

  • pending/retry 真相判定
  • heuristics 匹配远端是否承认本地消息
  • overlay/alias 语义理解

BFF

负责:

  • operation 持久化
  • 附件 payload 生命周期
  • 与 OpenClaw 的消息/附件对接
  • canonical timeline 投影
  • SSE 事件生成

迁移计划

Phase 1:立模型,不切旧逻辑

App

  • 新增 DeliveryOperation / SessionDetailState / reducer skeleton
  • 先不替换现有 UI,只把新模型放进代码库

BFF

  • 新增 OperationRecord / OperationPayloadRef / SessionBootstrapResponse 模型草案
  • 新增 bootstrap / operation DTO 草案(可先不对外开放)

收益

  • 统一机制正式有“落脚点”
  • 后续不再继续给旧字符串状态扩容

风险

  • 这阶段功能收益不明显,主要是架构铺路

Phase 2:BFF 接管 outbox

  • 文本发送、附件上传都先创建 operation record
  • App 保留现有接口,但 response 里开始带 operationId
  • retry/recovery 由 BFF 驱动

Phase 3:App 详情页切到 reducer 投影

  • 会话详情改读 SessionDetailState
  • operation card 替代 local-thinking / fake uploaded message
  • refresh 与 SSE 都只喂 reducer

Phase 4:删旧逻辑

App 待删

  • pendingTextQueue
  • pendingAttachmentQueue
  • textRetryStore
  • attachmentRetryStore
  • autoRetryMessageIds
  • mergeSnapshotWithLocalPending()
  • retainedLocalMessages()
  • matchesPendingLocalText()/Attachment()
  • local-thinking-*

BFF 待删/冻结

  • _session_overlays
  • _attachment_aliases
  • stub-* uploaded message 过渡语义
  • history diff 补丁事件继续扩张

当前阶段的执行原则

从现在开始:

  1. 冻结 ChatMessage.status 新字符串扩张
  2. 冻结新的 pending/retry heuristic
  3. 新能力优先落在新模型上,而不是继续补旧模型
  4. UI 小修只做阻塞性 bug,不再继续围绕旧机制做大量打补丁优化

当前这一轮要落地的最小骨架

App

  • 定义 DeliveryOperation
  • 定义 SessionDetailState
  • 定义 SessionDetailEvent
  • 定义 reduceSessionDetailState()

BFF

  • 定义 OperationRecord
  • 定义 SessionBootstrapResponse
  • 预留 /api/sessions/{session_id}/bootstrap 的 response model
  • 后续再逐步接真实数据流