Repository navigation
Go: implement ASR and TTS for xiaomi - #15765
Conversation
📝 WalkthroughWalkthroughThis PR implements a complete Xiaomi model driver supporting chat, speech-to-text (ASR), and text-to-speech (TTS) across both non-stream and SSE streaming modes. Configuration endpoints are updated with ChangesXiaomi Model Driver: Chat, ASR, and TTS Implementation
Estimated code review effort🎯 4 (Complex) | ⏱️ ~50 minutes Possibly related PRs
Suggested labels
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@internal/entity/models/xiaomi.go`:
- Around line 311-313: The loop that reads SSE lines currently calls
common.Info(line) and will log potentially sensitive SSE content; modify the
code around scanner.Scan() to stop unguarded logging by either removing the
common.Info(line) call or wrapping it behind a runtime-configurable debug flag
(e.g., an application DEBUG/LOG_LEVEL check or a function like isDebugEnabled())
so that common.Info(line) only executes when debugging is explicitly enabled;
locate and update the scanner.Scan() loop and the common.Info usage to implement
the guard.
- Around line 710-718: The TTS stream handler readXiaomiTTSStream currently
returns without emitting the same "[DONE]" end-of-stream marker that ASR streams
send; update readXiaomiTTSStream (and the fallback branch that returns
sender(&chunk, nil)) to call sender with a "[DONE]" marker (e.g., create a done
string and call sender(&done, nil)) after all audio chunks are delivered so
callers receive a consistent end signal; ensure the sender invocation for the
final chunk remains unchanged and the done emission happens once after the
stream/body processing completes.
- Around line 99-101: The ChatWithMessages non-streaming path currently respects
chatModelConfig.Stream and can set reqBody["stream"] to true which will break
JSON parsing; change ChatWithMessages to always set reqBody["stream"] = false
(or omit any use of chatModelConfig.Stream) so the request is never sent as
SSE—locate the block that checks chatModelConfig.Stream and replace it with a
hardcoded false assignment or remove the conditional, mirroring how
ChatStreamlyWithSender hardcodes stream: true.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 021f23db-58ee-47f6-98e1-069e3bf32c3f
📒 Files selected for processing (2)
conf/models/xiaomi.jsoninternal/entity/models/xiaomi.go
| if chatModelConfig.Stream != nil { | ||
| reqBody["stream"] = *chatModelConfig.Stream | ||
| } |
There was a problem hiding this comment.
Non-streaming method allows stream override that would break response parsing.
ChatWithMessages is a non-streaming method that parses the response as a single JSON object (lines 163-212). If chatModelConfig.Stream is true, the API returns SSE chunks, causing JSON unmarshal failures.
Consider forcing stream: false regardless of config, consistent with ChatStreamlyWithSender which hardcodes stream: true without allowing override.
Proposed fix
if chatModelConfig != nil {
- if chatModelConfig.Stream != nil {
- reqBody["stream"] = *chatModelConfig.Stream
- }
+ // Note: Stream is intentionally not overridden here.
+ // This is a non-streaming method; use ChatStreamlyWithSender for streaming.
if chatModelConfig.MaxTokens != nil {🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@internal/entity/models/xiaomi.go` around lines 99 - 101, The ChatWithMessages
non-streaming path currently respects chatModelConfig.Stream and can set
reqBody["stream"] to true which will break JSON parsing; change ChatWithMessages
to always set reqBody["stream"] = false (or omit any use of
chatModelConfig.Stream) so the request is never sent as SSE—locate the block
that checks chatModelConfig.Stream and replace it with a hardcoded false
assignment or remove the conditional, mirroring how ChatStreamlyWithSender
hardcodes stream: true.
| for scanner.Scan() { | ||
| line := scanner.Text() | ||
| common.Info(line) |
There was a problem hiding this comment.
Logging every SSE line may expose sensitive content in production.
common.Info(line) logs all SSE data including user messages and model reasoning. Consider removing this debug logging or guarding it with a debug flag.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@internal/entity/models/xiaomi.go` around lines 311 - 313, The loop that reads
SSE lines currently calls common.Info(line) and will log potentially sensitive
SSE content; modify the code around scanner.Scan() to stop unguarded logging by
either removing the common.Info(line) call or wrapping it behind a
runtime-configurable debug flag (e.g., an application DEBUG/LOG_LEVEL check or a
function like isDebugEnabled()) so that common.Info(line) only executes when
debugging is explicitly enabled; locate and update the scanner.Scan() loop and
the common.Info usage to implement the guard.
| if len(audio.Audio) > 0 { | ||
| chunk := base64.StdEncoding.EncodeToString(audio.Audio) | ||
| return sender(&chunk, nil) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| return readXiaomiTTSStream(resp.Body, sender) | ||
| } |
There was a problem hiding this comment.
Inconsistent [DONE] marker handling between ASR and TTS streams.
ASR streaming (both SSE and fallback) sends a [DONE] marker to signal completion, but TTS streaming does not. This inconsistency could confuse callers expecting uniform end-of-stream signaling.
Consider adding [DONE] emission to TTS for consistency, or document the difference if intentional.
Proposed fix for non-stream fallback
if len(audio.Audio) > 0 {
chunk := base64.StdEncoding.EncodeToString(audio.Audio)
- return sender(&chunk, nil)
+ if err := sender(&chunk, nil); err != nil {
+ return err
+ }
}
- return nil
+ done := "[DONE]"
+ return sender(&done, nil)
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| if len(audio.Audio) > 0 { | |
| chunk := base64.StdEncoding.EncodeToString(audio.Audio) | |
| return sender(&chunk, nil) | |
| } | |
| return nil | |
| } | |
| return readXiaomiTTSStream(resp.Body, sender) | |
| } | |
| if len(audio.Audio) > 0 { | |
| chunk := base64.StdEncoding.EncodeToString(audio.Audio) | |
| if err := sender(&chunk, nil); err != nil { | |
| return err | |
| } | |
| } | |
| done := "[DONE]" | |
| return sender(&done, nil) | |
| } | |
| return readXiaomiTTSStream(resp.Body, sender) | |
| } |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@internal/entity/models/xiaomi.go` around lines 710 - 718, The TTS stream
handler readXiaomiTTSStream currently returns without emitting the same "[DONE]"
end-of-stream marker that ASR streams send; update readXiaomiTTSStream (and the
fallback branch that returns sender(&chunk, nil)) to call sender with a "[DONE]"
marker (e.g., create a done string and call sender(&done, nil)) after all audio
chunks are delivered so callers receive a consistent end signal; ensure the
sender invocation for the final chunk remains unchanged and the done emission
happens once after the stream/body processing completes.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #15765 +/- ##
=======================================
Coverage 93.16% 93.16%
=======================================
Files 10 10
Lines 717 717
Branches 118 118
=======================================
Hits 668 668
Misses 29 29
Partials 20 20 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
What problem does this PR solve?
Verified from CLI
Type of change