Add unified delivery architecture skeleton

This commit is contained in:
OpenClaw committed 2026-03-07 22:39:13 +08:00
1 parent 1dc505c8d8
commit 82c6d27075
3 files changed
+371

No files matched your search

@@ -0,0 +1,126 @@
package cn.xiaobai.openclawapp.data
sealed interface DeliveryOperationType {
data object SendText : DeliveryOperationType
data object SendAttachment : DeliveryOperationType
}
enum class DeliveryOperationState {
Accepted,
Persisted,
Dispatching,
AwaitingRemoteEcho,
Materialized,
FailedRetryable,
FailedTerminal,
Cancelled,
}
data class DeliveryOperation(
val opId: String,
val sessionId: String,
val type: DeliveryOperationType,
val state: DeliveryOperationState,
val createdAtMs: Long,
val clientRequestId: String,
val payloadSummary: String,
val attemptCount: Int = 0,
val lastError: String? = null,
val correlationToken: String? = null,
val materializedMessageIds: List<String> = emptyList(),
)
enum class SessionSyncState {
Idle,
Bootstrapping,
Refreshing,
Failed,
}
enum class SessionStreamState {
Idle,
Connecting,
Live,
Recovering,
Failed,
}
sealed interface MessageTimelineItem {
data class RemoteMessage(val message: ChatMessage) : MessageTimelineItem
data class PendingOperation(val operation: DeliveryOperation) : MessageTimelineItem
data class SystemNotice(val id: String, val text: String) : MessageTimelineItem
}
data class SessionDetailState(
val sessionId: String,
val timeline: List<MessageTimelineItem> = emptyList(),
val operations: List<DeliveryOperation> = emptyList(),
val syncState: SessionSyncState = SessionSyncState.Idle,
val streamState: SessionStreamState = SessionStreamState.Idle,
val lastBootstrapAtMs: Long? = null,
val lastError: String? = null,
)
sealed interface SessionDetailEvent {
data class BootstrapLoaded(
val sessionId: String,
val timeline: List<MessageTimelineItem>,
val operations: List<DeliveryOperation>,
val loadedAtMs: Long,
) : SessionDetailEvent
data class OperationUpserted(val operation: DeliveryOperation) : SessionDetailEvent
data class OperationRemoved(val opId: String) : SessionDetailEvent
data class MessageUpserted(val message: ChatMessage) : SessionDetailEvent
data class MessageRemoved(val messageId: String) : SessionDetailEvent
data class SyncStateChanged(val syncState: SessionSyncState, val error: String? = null) : SessionDetailEvent
data class StreamStateChanged(val streamState: SessionStreamState) : SessionDetailEvent
}
fun reduceSessionDetailState(
previous: SessionDetailState,
event: SessionDetailEvent,
): SessionDetailState {
return when (event) {
is SessionDetailEvent.BootstrapLoaded -> previous.copy(
sessionId = event.sessionId,
timeline = event.timeline,
operations = event.operations,
syncState = SessionSyncState.Idle,
lastBootstrapAtMs = event.loadedAtMs,
lastError = null,
)
is SessionDetailEvent.OperationUpserted -> {
val nextOps = previous.operations
.filterNot { it.opId == event.operation.opId } + event.operation
previous.copy(operations = nextOps)
}
is SessionDetailEvent.OperationRemoved -> previous.copy(
operations = previous.operations.filterNot { it.opId == event.opId }
)
is SessionDetailEvent.MessageUpserted -> {
val nextTimeline = previous.timeline.filterNot {
it is MessageTimelineItem.RemoteMessage && it.message.id == event.message.id
} + MessageTimelineItem.RemoteMessage(event.message)
previous.copy(timeline = nextTimeline)
}
is SessionDetailEvent.MessageRemoved -> previous.copy(
timeline = previous.timeline.filterNot {
it is MessageTimelineItem.RemoteMessage && it.message.id == event.messageId
}
)
is SessionDetailEvent.SyncStateChanged -> previous.copy(
syncState = event.syncState,
lastError = event.error,
)
is SessionDetailEvent.StreamStateChanged -> previous.copy(
streamState = event.streamState,
)
}
}
+1
View File
@@ -462,3 +462,4 @@ Android/iOS App
- 2026-03-07:聊天页也补上下拉刷新:沿用同一套 `PullToRefreshBox + onRetry` 机制,让会话详情除了顶栏刷新/重连外,也支持直接下拉拉取最新消息与状态。
- 2026-03-07:同步反馈开始区分触发来源:把远端同步拆成 `Initial / Manual / Background` 三种触发语义,用户主动刷新成功时会明确提示“已同步最新状态”,后台静默刷新则尽量不打扰,避免轮询也频繁弹反馈,同时让手动操作的结果更可感知。
- 2026-03-07:开始统一发送反馈语言:聊天时间线里把文本/附件/工具的状态从泛化的“处理中 / 失败”细化成用户更能理解的产品文案,如“发送中 / 上传中 / 发送队列中 / 自动重试中 / 执行中 / 发送失败 / 上传失败 / 生成失败”,让用户更清楚当前到底卡在哪一环。
- 2026-03-07:正式启动“统一机制”重构 Phase 1:新增 `docs/UNIFIED_DELIVERY_ARCHITECTURE.md` 作为设计稿;App 新增 `UnifiedDeliveryModels.kt`,定义 `DeliveryOperation / SessionDetailState / SessionDetailEvent / reducer` 骨架;BFF 新增 `unified_models.py` 并预留 `/api/sessions/{session_id}/bootstrap` 响应模型与空接口,先把新主干立起来,后续再逐步把旧 pending/SSE/refresh 逻辑迁过去。
+244
View File
@@ -0,0 +1,244 @@
# 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
- 后续再逐步接真实数据流