Repository navigation
Conversation
WalkthroughThe PR refactors token text building across multiple DTO files to use a dedicated Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 1 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (1 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (2)
dto/openai_request.go (1)
447-461:⚠️ Potential issue | 🟡 MinorDirect
json.Unmarshalandjson.Marshalusage violates coding guidelines.Lines 452 and 459 use
encoding/jsondirectly instead ofcommon.Unmarshalandcommon.Marshal. As per coding guidelines, all JSON marshal/unmarshal operations must use wrapper functions fromcommon/json.go.🔧 Proposed fix
func (m *Message) ParseToolCalls() []ToolCallRequest { if m.ToolCalls == nil { return nil } var toolCalls []ToolCallRequest - if err := json.Unmarshal(m.ToolCalls, &toolCalls); err == nil { + if err := common.Unmarshal(m.ToolCalls, &toolCalls); err == nil { return toolCalls } return toolCalls } func (m *Message) SetToolCalls(toolCalls any) { - toolCallsJson, _ := json.Marshal(toolCalls) + toolCallsJson, _ := common.Marshal(toolCalls) m.ToolCalls = toolCallsJson }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@dto/openai_request.go` around lines 447 - 461, Replace direct encoding/json calls in Message.ParseToolCalls and Message.SetToolCalls with the project's JSON wrappers: use common.Unmarshal instead of json.Unmarshal when decoding m.ToolCalls in ParseToolCalls and use common.Marshal instead of json.Marshal when encoding in SetToolCalls; preserve current behavior (returning nil or the decoded slice on error) but call the wrapper functions and handle their returned (value, error) results consistently, referencing the Message.ParseToolCalls and Message.SetToolCalls methods to locate the changes.dto/claude.go (1)
410-417:⚠️ Potential issue | 🟡 MinorDirect
json.Unmarshalusage violates coding guidelines.Line 412 uses
json.Unmarshaldirectly instead ofcommon.Unmarshal. As per coding guidelines, all JSON marshal/unmarshal operations must use wrapper functions fromcommon/json.go.🔧 Proposed fix
func (c *ClaudeRequest) GetEfforts() string { var OutputConfig OutputConfigForEffort - if err := json.Unmarshal(c.OutputConfig, &OutputConfig); err == nil { + if err := common.Unmarshal(c.OutputConfig, &OutputConfig); err == nil { effort := OutputConfig.Effort return effort } return "" }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@dto/claude.go` around lines 410 - 417, The GetEfforts method in ClaudeRequest uses json.Unmarshal directly; replace that call with the project's JSON wrapper (common.Unmarshal) to comply with guidelines: call common.Unmarshal(c.OutputConfig, &OutputConfig) inside ClaudeRequest.GetEfforts, keep the existing error handling (i.e., only return effort when unmarshal succeeds), and reference the OutputConfigForEffort struct and the GetEfforts method when making the change.
🧹 Nitpick comments (2)
dto/gemini.go (1)
519-530: Inconsistent refactoring:GeminiEmbeddingRequest.GetTokenCountMeta()still uses the old pattern.This method still uses
[]string+strings.JoinwhileGeminiChatRequest.GetTokenCountMeta()was refactored to usetokenTextBuilder. Consider applying the same refactor for consistency.♻️ Suggested refactor for consistency
func (r *GeminiEmbeddingRequest) GetTokenCountMeta() *types.TokenCountMeta { - var inputTexts []string + var textBuilder tokenTextBuilder for _, part := range r.Content.Parts { if part.Text != "" { - inputTexts = append(inputTexts, part.Text) + textBuilder.Add(part.Text) } } - inputText := strings.Join(inputTexts, "\n") return &types.TokenCountMeta{ - CombineText: inputText, + CombineText: textBuilder.String(), } }The same applies to
GeminiBatchEmbeddingRequest.GetTokenCountMeta()at lines 547-559.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@dto/gemini.go` around lines 519 - 530, Replace the old []string + strings.Join pattern in GeminiEmbeddingRequest.GetTokenCountMeta() with the same tokenTextBuilder utility used by GeminiChatRequest: instantiate a tokenTextBuilder, iterate r.Content.Parts and call builder.Add(part.Text) for non-empty texts, then call builder.String() (or builder.Build()) to produce CombineText and return it in the TokenCountMeta; apply the identical change to GeminiBatchEmbeddingRequest.GetTokenCountMeta() so both methods use tokenTextBuilder consistently.middleware/distributor_test.go (1)
41-65: Tests cover key scenarios appropriately.The type mismatch and non-object tests validate the fallback/skip behavior correctly. Consider adding a test for
groupbeing non-string whilemodelis valid to ensure both fields are validated.💡 Additional test case suggestion
func TestExtractModelRequestFromJSONBodySkipsNonStringGroup(t *testing.T) { gin.SetMode(gin.TestMode) body := `{"model":"gpt-4.1","group":123}` ctx, _ := gin.CreateTestContext(httptest.NewRecorder()) ctx.Request = httptest.NewRequest("POST", "/v1/chat/completions", bytes.NewBufferString(body)) ctx.Request.Header.Set("Content-Type", "application/json") modelRequest, ok, err := extractModelRequestFromJSONBody(ctx) require.NoError(t, err) require.Nil(t, modelRequest) require.False(t, ok) }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@middleware/distributor_test.go` around lines 41 - 65, Add a unit test that verifies extractModelRequestFromJSONBody rejects payloads where "group" is not a string even if "model" is valid: create a Gin test context and request with body `{"model":"gpt-4.1","group":123}`, call extractModelRequestFromJSONBody(ctx), and assert no error, returned modelRequest is nil, and ok is false; place it alongside TestExtractModelRequestFromJSONBodySkipsNonJSONObject and reference extractModelRequestFromJSONBody to locate the logic to validate.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@common/gin.go`:
- Around line 119-130: The bug is variable shadowing: the inner `err` declared
with `:=` when calling `storage.Bytes()` in the branches for
`strings.Contains(contentType, gin.MIMEPOSTForm)` and
`strings.Contains(contentType, gin.MIMEMultipartPOSTForm)` hides the outer
`err`, so errors from `parseFormData` and `parseMultipartFormData` are lost; fix
by using assignment (`=`) instead of short-declare (`:=`) for `requestBody, err`
(or split into two statements: `requestBody, err = storage.Bytes()` or
`requestBody := ...; if err := ...;` avoided) and ensure the same `err` variable
is used so that returned errors from `parseFormData` and
`parseMultipartFormData` propagate correctly from the surrounding function
(update the branches around `storage.Bytes()`, `parseFormData`, and
`parseMultipartFormData` accordingly).
---
Outside diff comments:
In `@dto/claude.go`:
- Around line 410-417: The GetEfforts method in ClaudeRequest uses
json.Unmarshal directly; replace that call with the project's JSON wrapper
(common.Unmarshal) to comply with guidelines: call
common.Unmarshal(c.OutputConfig, &OutputConfig) inside ClaudeRequest.GetEfforts,
keep the existing error handling (i.e., only return effort when unmarshal
succeeds), and reference the OutputConfigForEffort struct and the GetEfforts
method when making the change.
In `@dto/openai_request.go`:
- Around line 447-461: Replace direct encoding/json calls in
Message.ParseToolCalls and Message.SetToolCalls with the project's JSON
wrappers: use common.Unmarshal instead of json.Unmarshal when decoding
m.ToolCalls in ParseToolCalls and use common.Marshal instead of json.Marshal
when encoding in SetToolCalls; preserve current behavior (returning nil or the
decoded slice on error) but call the wrapper functions and handle their returned
(value, error) results consistently, referencing the Message.ParseToolCalls and
Message.SetToolCalls methods to locate the changes.
---
Nitpick comments:
In `@dto/gemini.go`:
- Around line 519-530: Replace the old []string + strings.Join pattern in
GeminiEmbeddingRequest.GetTokenCountMeta() with the same tokenTextBuilder
utility used by GeminiChatRequest: instantiate a tokenTextBuilder, iterate
r.Content.Parts and call builder.Add(part.Text) for non-empty texts, then call
builder.String() (or builder.Build()) to produce CombineText and return it in
the TokenCountMeta; apply the identical change to
GeminiBatchEmbeddingRequest.GetTokenCountMeta() so both methods use
tokenTextBuilder consistently.
In `@middleware/distributor_test.go`:
- Around line 41-65: Add a unit test that verifies
extractModelRequestFromJSONBody rejects payloads where "group" is not a string
even if "model" is valid: create a Gin test context and request with body
`{"model":"gpt-4.1","group":123}`, call extractModelRequestFromJSONBody(ctx),
and assert no error, returned modelRequest is nil, and ok is false; place it
alongside TestExtractModelRequestFromJSONBodySkipsNonJSONObject and reference
extractModelRequestFromJSONBody to locate the logic to validate.
🪄 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: 469100c4-8bb6-48e0-b4cf-20a5baf5f380
📒 Files selected for processing (12)
common/gin.godto/claude.godto/embedding.godto/gemini.godto/openai_request.godto/rerank.godto/token_text_builder.godto/token_text_builder_test.gomiddleware/distributor.gomiddleware/distributor_test.gorelay/common/override.gorelay/common/override_test.go
| } else if strings.Contains(contentType, gin.MIMEPOSTForm) { | ||
| requestBody, err := storage.Bytes() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| err = parseFormData(requestBody, v) | ||
| } else if strings.Contains(contentType, gin.MIMEMultipartPOSTForm) { | ||
| requestBody, err := storage.Bytes() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| err = parseMultipartFormData(c, requestBody, v) |
There was a problem hiding this comment.
Critical bug: variable shadowing causes parsing errors to be silently ignored.
The := on lines 120 and 126 declares new err variables in the inner scope that shadow the outer err from line 109. When parseFormData (line 124) or parseMultipartFormData (line 130) returns an error, it's assigned to the inner err, which goes out of scope immediately. The check on line 135 then reads the outer err (which is nil), causing the function to return nil instead of the parsing error.
🐛 Proposed fix to prevent variable shadowing
} else if strings.Contains(contentType, gin.MIMEPOSTForm) {
- requestBody, err := storage.Bytes()
- if err != nil {
- return err
+ requestBody, bytesErr := storage.Bytes()
+ if bytesErr != nil {
+ return bytesErr
}
err = parseFormData(requestBody, v)
} else if strings.Contains(contentType, gin.MIMEMultipartPOSTForm) {
- requestBody, err := storage.Bytes()
- if err != nil {
- return err
+ requestBody, bytesErr := storage.Bytes()
+ if bytesErr != nil {
+ return bytesErr
}
err = parseMultipartFormData(c, requestBody, v)
} else {📝 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.
| } else if strings.Contains(contentType, gin.MIMEPOSTForm) { | |
| requestBody, err := storage.Bytes() | |
| if err != nil { | |
| return err | |
| } | |
| err = parseFormData(requestBody, v) | |
| } else if strings.Contains(contentType, gin.MIMEMultipartPOSTForm) { | |
| requestBody, err := storage.Bytes() | |
| if err != nil { | |
| return err | |
| } | |
| err = parseMultipartFormData(c, requestBody, v) | |
| } else if strings.Contains(contentType, gin.MIMEPOSTForm) { | |
| requestBody, bytesErr := storage.Bytes() | |
| if bytesErr != nil { | |
| return bytesErr | |
| } | |
| err = parseFormData(requestBody, v) | |
| } else if strings.Contains(contentType, gin.MIMEMultipartPOSTForm) { | |
| requestBody, bytesErr := storage.Bytes() | |
| if bytesErr != nil { | |
| return bytesErr | |
| } | |
| err = parseMultipartFormData(c, requestBody, v) |
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@common/gin.go` around lines 119 - 130, The bug is variable shadowing: the
inner `err` declared with `:=` when calling `storage.Bytes()` in the branches
for `strings.Contains(contentType, gin.MIMEPOSTForm)` and
`strings.Contains(contentType, gin.MIMEMultipartPOSTForm)` hides the outer
`err`, so errors from `parseFormData` and `parseMultipartFormData` are lost; fix
by using assignment (`=`) instead of short-declare (`:=`) for `requestBody, err`
(or split into two statements: `requestBody, err = storage.Bytes()` or
`requestBody := ...; if err := ...;` avoided) and ensure the same `err` variable
is used so that returned errors from `parseFormData` and
`parseMultipartFormData` propagate correctly from the surrounding function
(update the branches around `storage.Bytes()`, `parseFormData`, and
`parseMultipartFormData` accordingly).
51fdfc5 to
2b6f1df
Compare
AI Generated:
Summary by CodeRabbit
Refactor
Tests