feat: add X-Conversation-Id header to API requests#463
Merged
Chevey339 merged 1 commit intoChevey339:masterfrom Apr 13, 2026
Merged
feat: add X-Conversation-Id header to API requests#463Chevey339 merged 1 commit intoChevey339:masterfrom
Chevey339 merged 1 commit intoChevey339:masterfrom
Conversation
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.
关联 Issue: #412
改动目标
在发送 API 请求时,将当前对话的本地会话 ID 透传到 HTTP 请求 header(X-Conversation-Id),使中间网关能够识别和区分不同对话窗口。
改动内容
1.在 message_generation_service.dart 新增 buildConversationRequestHeaders 函数,在构建 GenerationContext 时将 assistantMessage.conversationId 注入到 extraHeaders 中
2.修复 google_common.dart 中 Gemini 非流式分支未合并 extraHeaders 的遗漏
3.新增测试覆盖 header 合并规则及 OpenAI / Claude / Gemini 三种 provider 的透传验证
改动范围
・lib/features/home/services/message_generation_service.dart — header 合并 helper + 注入逻辑
・lib/core/services/api/providers/google_common.dart — Gemini 非流式分支补全 extraHeaders
・test/conversation_request_headers_test.dart — header 合并单测
・test/chat_api_extra_headers_test.dart — 各 provider 透传单测
设计说明
・会话 ID 来源为 ChatMessage.conversationId(关联 Conversation.id,UUID v4),每个对话窗口唯一且稳定
・header 注入放在统一组装点,不散落到各 provider 实现中
・合并逻辑保留用户已有的自定义 header,仅覆盖大小写变体的旧 conversation-id 值
・翻译、OCR 等非对话请求路径不经过此组装点,不会携带该 header,这是预期行为
・该 header 对所有 API provider 无副作用,不认识的 provider 会直接忽略
验证
・dart format 通过
・flutter analyze --no-fatal-infos 通过(1 个 warning 为项目原有,非本次引入)
・flutter test 相关子集全绿