Repository navigation
fix(tokenhub): wire Go driver and harden requests - #15224
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
✅ Files skipped from review due to trivial changes (1)
📝 WalkthroughWalkthroughThis PR registers TokenHub in the model factory, tightens input validation and defensive parsing in the TokenHub model (chat, streaming, embed, list), removes per-line SSE logging, and adds a comprehensive HTTP/SSE test suite covering these behaviors. ChangesTokenHub Provider Registration and Implementation
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 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: 2
🤖 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/tokenhub_test.go`:
- Around line 216-219: In the httptest.NewServer(http.HandlerFunc(...)) handler
where you parse body["input"] into inputs and currently call
t.Fatalf("input=%#v", body["input"]) (around the inputs, ok check), replace that
t.Fatalf with t.Errorf to record the failure without aborting the test
goroutine, write an appropriate HTTP error response (e.g., http.Error or setting
status and body), and then return immediately from the handler so the request
completes; this change affects the handler closure that reads body and the
inputs variable check.
In `@internal/entity/models/tokenhub.go`:
- Around line 44-47: Both ChatWithMessages and ChatStreamlyWithSender are
missing validation for an empty modelName; add a guard at the start of
TokenHubModel.ChatWithMessages and TokenHubModel.ChatStreamlyWithSender that
checks modelName is not nil/empty (use strings.TrimSpace(modelName) to catch
whitespace) and return a clear error (e.g., fmt.Errorf("modelName is required"))
if invalid, so bad requests are rejected locally before calling TokenHub.
🪄 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: d312d2fe-849d-4f4e-b5d5-e4d2fb562231
📒 Files selected for processing (3)
internal/entity/models/factory.gointernal/entity/models/tokenhub.gointernal/entity/models/tokenhub_test.go
| inputs, ok := body["input"].([]interface{}) | ||
| if !ok || len(inputs) != 2 { | ||
| t.Fatalf("input=%#v", body["input"]) | ||
| } |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
rg -n --type=go -C3 'httptest\.NewServer|http\.HandlerFunc|t\.Fatalf\(' internal/entity/models/tokenhub_test.goRepository: infiniflow/ragflow
Length of output: 5041
Replace t.Fatalf inside the httptest HTTP handler with t.Errorf (+ early return)
internal/entity/models/tokenhub_test.go uses t.Fatalf inside the httptest.NewServer(http.HandlerFunc(...)) closure (around lines 216-219). Change it to t.Errorf and return after writing an error response so the test fails reliably.
💡 Suggested fix
inputs, ok := body["input"].([]interface{})
if !ok || len(inputs) != 2 {
- t.Fatalf("input=%#v", body["input"])
+ t.Errorf("input=%#v", body["input"])
+ http.Error(w, "invalid input", http.StatusBadRequest)
+ return
}📝 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.
| inputs, ok := body["input"].([]interface{}) | |
| if !ok || len(inputs) != 2 { | |
| t.Fatalf("input=%#v", body["input"]) | |
| } | |
| inputs, ok := body["input"].([]interface{}) | |
| if !ok || len(inputs) != 2 { | |
| t.Errorf("input=%#v", body["input"]) | |
| http.Error(w, "invalid input", http.StatusBadRequest) | |
| return | |
| } |
🤖 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/tokenhub_test.go` around lines 216 - 219, In the
httptest.NewServer(http.HandlerFunc(...)) handler where you parse body["input"]
into inputs and currently call t.Fatalf("input=%#v", body["input"]) (around the
inputs, ok check), replace that t.Fatalf with t.Errorf to record the failure
without aborting the test goroutine, write an appropriate HTTP error response
(e.g., http.Error or setting status and body), and then return immediately from
the handler so the request completes; this change affects the handler closure
that reads body and the inputs variable check.
| func (t *TokenHubModel) ChatWithMessages(modelName string, messages []Message, apiConfig *APIConfig, chatModelConfig *ChatConfig) (*ChatResponse, error) { | ||
| if apiConfig == nil || apiConfig.ApiKey == nil || *apiConfig.ApiKey == "" { | ||
| return nil, fmt.Errorf("api key is required") | ||
| } |
There was a problem hiding this comment.
Validate modelName before sending chat requests.
ChatWithMessages and ChatStreamlyWithSender now fail fast for API key/sender, but still allow empty modelName, which pushes avoidable bad requests to TokenHub instead of returning a local validation error.
💡 Suggested fix
func (t *TokenHubModel) ChatWithMessages(modelName string, messages []Message, apiConfig *APIConfig, chatModelConfig *ChatConfig) (*ChatResponse, error) {
+ if strings.TrimSpace(modelName) == "" {
+ return nil, fmt.Errorf("model name is required")
+ }
if apiConfig == nil || apiConfig.ApiKey == nil || *apiConfig.ApiKey == "" {
return nil, fmt.Errorf("api key is required")
}
@@
func (t *TokenHubModel) ChatStreamlyWithSender(modelName string, messages []Message, apiConfig *APIConfig, modelConfig *ChatConfig, sender func(*string, *string) error) error {
+ if strings.TrimSpace(modelName) == "" {
+ return fmt.Errorf("model name is required")
+ }
if sender == nil {
return fmt.Errorf("sender is required")
}Also applies to: 169-175
🤖 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/tokenhub.go` around lines 44 - 47, Both
ChatWithMessages and ChatStreamlyWithSender are missing validation for an empty
modelName; add a guard at the start of TokenHubModel.ChatWithMessages and
TokenHubModel.ChatStreamlyWithSender that checks modelName is not nil/empty (use
strings.TrimSpace(modelName) to catch whitespace) and return a clear error
(e.g., fmt.Errorf("modelName is required")) if invalid, so bad requests are
rejected locally before calling TokenHub.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #15224 +/- ##
=======================================
Coverage 93.12% 93.12%
=======================================
Files 10 10
Lines 713 713
Branches 116 116
=======================================
Hits 664 664
Misses 29 29
Partials 20 20 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Summary
What changed
tokenhubinModelFactoryso configured TokenHub instances createTokenHubModelinstead of falling through to the dummy driver.ChatWithMessageson the non-streaming path even when a shared chat config containsstream: true.Why
TokenHub provider configuration and the driver were added in #15159, but the factory path still did not instantiate the TokenHub driver. This completes that wiring and adds regression coverage for the Go provider work tracked under #14736.
Validation
go test ./internal/entity/models -run TestTokenHub -count=1 -vgit diff --checkNotes