feat: align streaming architecture with v2 (memory-first persistence)#347
Open
chengcheng84 wants to merge 12 commits intoCherryHQ:mainfrom
Open
feat: align streaming architecture with v2 (memory-first persistence)#347chengcheng84 wants to merge 12 commits intoCherryHQ:mainfrom
chengcheng84 wants to merge 12 commits intoCherryHQ:mainfrom
Conversation
…esService Other files include fully copying the V2 design and making some necessary fixes
…e subscription mechanism - Add cache service subscription feature to support real-time monitoring of message and block changes - Refactor message components by removing React.memo optimization, relying instead on streaming updates - Implement real-time merging and display of messages and blocks, supporting synchronization between database and streaming data - Add message state change listeners to ensure UI responds promptly to streaming updates - Optimize message grouping and rendering logic to improve streaming message processing performance
When the thinking details page is visible, subscribe to the message stream service to update the thinking block status in real time. Remove unnecessary global state comments.
Collaborator
|
还是有点问题,这个里面东西有点复杂,还是我后面找时间弄吧 |
Contributor
Author
|
OK |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
总结
将流式处理从实时数据库写入改为两段式落库
修改细节
finalize()一次性写入,把最终data.blocks、status、stats等写回 SQLite,见 src/services/messageStreaming/StreamingService.ts#L158-L160src/services/messageStreaming/callbacks/baseCallbacks.tssrc/services/messageStreaming/callbacks/baseCallbacks.ts#L217流式输出架构重构
Before
Now
Question
这个是要之后改吧,还是这个PR直接改
这个是不考虑的吗
Deepseek的diff总结
根据你提供的
git diff文件内容,这个 PR 涉及一次大规模的重构和优化,主要集中在消息流式处理架构的重构、引入缓存服务以及UI 交互的优化。核心变更总结
本次重构的核心目标是将消息流式处理从实时数据库写入模式,改为内存缓存 + 最终持久化的两阶段提交模式,以提升性能和数据一致性。
主要变更点
1. 架构与核心服务重构
CacheService:src/services/CacheService.ts),作为基础设施。Map的键值存储、TTL(生存时间)过期、自动垃圾回收以及订阅/通知机制。StreamingService提供了内存存储支持,并允许UI组件实时响应缓存变化。StreamingService:src/services/messageStreaming/StreamingService.ts),专门管理消息流式生成的生命周期。CacheService的内存中;流式结束时,通过finalize()方法一次性批量写入 SQLite 数据库。blockId到messageId的映射,并提供了subscribeToMessage方法供UI层监听消息状态变化。2. 业务逻辑层重构
MessagesService.ts:pendingBlockUpdates、flushTimer等节流逻辑)。streamingService.startTask()启动一个流式任务。fetchAndProcessAssistantResponseImpl和fetchTranslateThunk函数,移除了对旧版数据库保存函数的依赖。BlockManager:BlockManager的功能被简化,不再直接操作数据库或 Redux。_activeBlockInfo),并通过依赖注入的throttledBlockUpdate和cancelThrottledBlockUpdate方法,间接调用streamingService来更新内存中的块数据。messageBlockDatabase的代码。3. 回调处理器更新
baseCallbacks、textCallbacks、thinkingCallbacks等):saveUpdatesToDB和数据库直接操作的依赖。streamingService的updateBlock、updateMessage、addBlock等方法来更新内存状态。onError和onComplete等生命周期回调现在通过调用streamingService.finalize()来触发最终的数据库持久化。4. UI/数据层适配
useCacheHook:src/hooks/useCache.ts),用于让UI组件订阅CacheService中特定键的变化,实现实时更新。useMessages和useMessageBlocks:useLiveQuery)读取数据,还会通过streamingService和cacheService订阅流式消息的变化。MessageBlockRenderer组件现在会检查消息是否正在流式处理。如果是,则优先从streamingService获取最新的块数据并渲染,从而实现了实时更新。5. 其他小优化
logger.info和logger.silly调整为logger.debug,减少了生产环境的日志输出。memo,调整了recycleItems行为)。总结
这是一个基础性、高价值的重构 PR。它将消息流式处理的架构从“实时写库”模式转变为“内存缓存 + 最终一致性”模式。这带来了以下好处:
finalize机制确保了数据要么全部成功写入,要么失败回滚(或由 TTL 清理),避免了部分数据落库导致的脏数据问题。CacheService作为专门的内存管理层,StreamingService作为业务编排层,职责划分更明确。graph TB subgraph Input[输入层] A[流式Chunk] --> B[BlockManager] end subgraph Memory[内存层] B --> C[StreamingService] C --> D[CacheService<br/>TTL+订阅] end subgraph Storage[存储层] C -->|finalize| E[(SQLite)] end subgraph UI[UI层] D -.->|订阅| F[useMessages] D -.->|订阅| G[useMessageBlocks] E -->|useLiveQuery| F E -->|useLiveQuery| G F --> H[实时渲染] G --> H end style Memory fill:#e1f5ff style Storage fill:#fff4e1