Add unified delivery architecture skeleton
This commit is contained in:
1 parent
1dc505c8d8
commit
82c6d27075
3 files changed
+371
No files matched your search
+126
@@ -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,
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user