Repository navigation
feat(gemini): 支持 tool_choice 参数转换,优化多个渠道错误处理 - #2742
Conversation
# Conflicts: # relay/channel/gemini/relay-gemini.go
|
Warning Rate limit exceeded
⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. WalkthroughAdds OpenAI Changes
Sequence DiagramsequenceDiagram
participant Client
participant Handler as Channel Handler\n(OpenAI / Gemini / Claude)
participant Context as Request Context
participant Compat as Compatible Handler
participant Logger as Log Formatter
participant Frontend
Client->>Handler: Send API request
Handler->>Handler: Process upstream response\n(map finish_reason / check PromptFeedback / detect CSAM/refusal)
Handler->>Context: Set ContextKeyAdminRejectReason\n(e.g. gemini_block_reason / openai_finish_reason / claude_stop_reason)
Handler->>Compat: Return processed response
Compat->>Context: Read ContextKeyAdminRejectReason
Compat->>Logger: Add reject_reason to "other" data (if present)
Logger->>Frontend: Store/display logs (admin view shows 拦截原因)
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 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: 1
🤖 Fix all issues with AI agents
In `@relay/channel/gemini/relay-gemini.go`:
- Around line 1357-1401: When handling the empty-candidates branch
(len(geminiResponse.Candidates) == 0) you only populate PromptTokens; also set
CompletionTokens and TotalTokens: read
geminiResponse.UsageMetadata.CompletionTokenCount into usage.CompletionTokens if
present, otherwise fall back to usage.CompletionTokenDetails.ReasoningTokens
(which you already set from geminiResponse.UsageMetadata.ThoughtsTokenCount);
then compute usage.TotalTokens = usage.PromptTokens + usage.CompletionTokens
and, as a final fallback, if TotalTokens <= 0 set TotalTokens =
usage.PromptTokens. Update the dto.Usage population in that block (the usage
variable created there) to include these fields so billing/telemetry aren’t
undercounted.
| if len(geminiResponse.Candidates) == 0 { | ||
| //return nil, types.NewOpenAIError(errors.New("no candidates returned"), types.ErrorCodeBadResponseBody, http.StatusInternalServerError) | ||
| //if geminiResponse.PromptFeedback != nil && geminiResponse.PromptFeedback.BlockReason != nil { | ||
| // return nil, types.NewOpenAIError(errors.New("request blocked by Gemini API: "+*geminiResponse.PromptFeedback.BlockReason), types.ErrorCodePromptBlocked, http.StatusBadRequest) | ||
| //} else { | ||
| // return nil, types.NewOpenAIError(errors.New("empty response from Gemini API"), types.ErrorCodeEmptyResponse, http.StatusInternalServerError) | ||
| //} | ||
| usage := dto.Usage{ | ||
| PromptTokens: geminiResponse.UsageMetadata.PromptTokenCount, | ||
| } | ||
| usage.CompletionTokenDetails.ReasoningTokens = geminiResponse.UsageMetadata.ThoughtsTokenCount | ||
| for _, detail := range geminiResponse.UsageMetadata.PromptTokensDetails { | ||
| if detail.Modality == "AUDIO" { | ||
| usage.PromptTokensDetails.AudioTokens = detail.TokenCount | ||
| } else if detail.Modality == "TEXT" { | ||
| usage.PromptTokensDetails.TextTokens = detail.TokenCount | ||
| } | ||
| } | ||
| if usage.PromptTokens <= 0 { | ||
| usage.PromptTokens = info.GetEstimatePromptTokens() | ||
| } | ||
|
|
||
| var newAPIError *types.NewAPIError | ||
| if geminiResponse.PromptFeedback != nil && geminiResponse.PromptFeedback.BlockReason != nil { | ||
| newAPIError = types.NewOpenAIError( | ||
| errors.New("request blocked by Gemini API: "+*geminiResponse.PromptFeedback.BlockReason), | ||
| types.ErrorCodePromptBlocked, | ||
| http.StatusBadRequest, | ||
| ) | ||
| } else { | ||
| newAPIError = types.NewOpenAIError( | ||
| errors.New("empty response from Gemini API"), | ||
| types.ErrorCodeEmptyResponse, | ||
| http.StatusInternalServerError, | ||
| ) | ||
| } | ||
|
|
||
| service.ResetStatusCode(newAPIError, c.GetString("status_code_mapping")) | ||
|
|
||
| switch info.RelayFormat { | ||
| case types.RelayFormatClaude: | ||
| c.JSON(newAPIError.StatusCode, gin.H{ | ||
| "type": "error", | ||
| "error": newAPIError.ToClaudeError(), | ||
| }) | ||
| default: | ||
| c.JSON(newAPIError.StatusCode, gin.H{ | ||
| "error": newAPIError.ToOpenAIError(), | ||
| }) | ||
| } | ||
| return &usage, nil |
There was a problem hiding this comment.
Populate TotalTokens/CompletionTokens for empty-candidate responses.
The new usage object only sets PromptTokens; TotalTokens and CompletionTokens stay zero even when Gemini provides usage metadata. This can undercount billing/telemetry. Suggest filling totals and deriving completion tokens from metadata (or at minimum set TotalTokens = PromptTokens when missing).
🔧 Suggested fix
- usage := dto.Usage{
- PromptTokens: geminiResponse.UsageMetadata.PromptTokenCount,
- }
+ usage := dto.Usage{
+ PromptTokens: geminiResponse.UsageMetadata.PromptTokenCount,
+ TotalTokens: geminiResponse.UsageMetadata.TotalTokenCount,
+ }
+ if usage.TotalTokens == 0 && usage.PromptTokens > 0 {
+ usage.TotalTokens = usage.PromptTokens
+ }
+ usage.CompletionTokens = usage.TotalTokens - usage.PromptTokens
+ if usage.CompletionTokens < 0 {
+ usage.CompletionTokens = 0
+ }📝 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(geminiResponse.Candidates) == 0 { | |
| //return nil, types.NewOpenAIError(errors.New("no candidates returned"), types.ErrorCodeBadResponseBody, http.StatusInternalServerError) | |
| //if geminiResponse.PromptFeedback != nil && geminiResponse.PromptFeedback.BlockReason != nil { | |
| // return nil, types.NewOpenAIError(errors.New("request blocked by Gemini API: "+*geminiResponse.PromptFeedback.BlockReason), types.ErrorCodePromptBlocked, http.StatusBadRequest) | |
| //} else { | |
| // return nil, types.NewOpenAIError(errors.New("empty response from Gemini API"), types.ErrorCodeEmptyResponse, http.StatusInternalServerError) | |
| //} | |
| usage := dto.Usage{ | |
| PromptTokens: geminiResponse.UsageMetadata.PromptTokenCount, | |
| } | |
| usage.CompletionTokenDetails.ReasoningTokens = geminiResponse.UsageMetadata.ThoughtsTokenCount | |
| for _, detail := range geminiResponse.UsageMetadata.PromptTokensDetails { | |
| if detail.Modality == "AUDIO" { | |
| usage.PromptTokensDetails.AudioTokens = detail.TokenCount | |
| } else if detail.Modality == "TEXT" { | |
| usage.PromptTokensDetails.TextTokens = detail.TokenCount | |
| } | |
| } | |
| if usage.PromptTokens <= 0 { | |
| usage.PromptTokens = info.GetEstimatePromptTokens() | |
| } | |
| var newAPIError *types.NewAPIError | |
| if geminiResponse.PromptFeedback != nil && geminiResponse.PromptFeedback.BlockReason != nil { | |
| newAPIError = types.NewOpenAIError( | |
| errors.New("request blocked by Gemini API: "+*geminiResponse.PromptFeedback.BlockReason), | |
| types.ErrorCodePromptBlocked, | |
| http.StatusBadRequest, | |
| ) | |
| } else { | |
| newAPIError = types.NewOpenAIError( | |
| errors.New("empty response from Gemini API"), | |
| types.ErrorCodeEmptyResponse, | |
| http.StatusInternalServerError, | |
| ) | |
| } | |
| service.ResetStatusCode(newAPIError, c.GetString("status_code_mapping")) | |
| switch info.RelayFormat { | |
| case types.RelayFormatClaude: | |
| c.JSON(newAPIError.StatusCode, gin.H{ | |
| "type": "error", | |
| "error": newAPIError.ToClaudeError(), | |
| }) | |
| default: | |
| c.JSON(newAPIError.StatusCode, gin.H{ | |
| "error": newAPIError.ToOpenAIError(), | |
| }) | |
| } | |
| return &usage, nil | |
| if len(geminiResponse.Candidates) == 0 { | |
| usage := dto.Usage{ | |
| PromptTokens: geminiResponse.UsageMetadata.PromptTokenCount, | |
| TotalTokens: geminiResponse.UsageMetadata.TotalTokenCount, | |
| } | |
| if usage.TotalTokens == 0 && usage.PromptTokens > 0 { | |
| usage.TotalTokens = usage.PromptTokens | |
| } | |
| usage.CompletionTokens = usage.TotalTokens - usage.PromptTokens | |
| if usage.CompletionTokens < 0 { | |
| usage.CompletionTokens = 0 | |
| } | |
| usage.CompletionTokenDetails.ReasoningTokens = geminiResponse.UsageMetadata.ThoughtsTokenCount | |
| for _, detail := range geminiResponse.UsageMetadata.PromptTokensDetails { | |
| if detail.Modality == "AUDIO" { | |
| usage.PromptTokensDetails.AudioTokens = detail.TokenCount | |
| } else if detail.Modality == "TEXT" { | |
| usage.PromptTokensDetails.TextTokens = detail.TokenCount | |
| } | |
| } | |
| if usage.PromptTokens <= 0 { | |
| usage.PromptTokens = info.GetEstimatePromptTokens() | |
| } | |
| var newAPIError *types.NewAPIError | |
| if geminiResponse.PromptFeedback != nil && geminiResponse.PromptFeedback.BlockReason != nil { | |
| newAPIError = types.NewOpenAIError( | |
| errors.New("request blocked by Gemini API: "+*geminiResponse.PromptFeedback.BlockReason), | |
| types.ErrorCodePromptBlocked, | |
| http.StatusBadRequest, | |
| ) | |
| } else { | |
| newAPIError = types.NewOpenAIError( | |
| errors.New("empty response from Gemini API"), | |
| types.ErrorCodeEmptyResponse, | |
| http.StatusInternalServerError, | |
| ) | |
| } | |
| service.ResetStatusCode(newAPIError, c.GetString("status_code_mapping")) | |
| switch info.RelayFormat { | |
| case types.RelayFormatClaude: | |
| c.JSON(newAPIError.StatusCode, gin.H{ | |
| "type": "error", | |
| "error": newAPIError.ToClaudeError(), | |
| }) | |
| default: | |
| c.JSON(newAPIError.StatusCode, gin.H{ | |
| "error": newAPIError.ToOpenAIError(), | |
| }) | |
| } | |
| return &usage, nil |
🤖 Prompt for AI Agents
In `@relay/channel/gemini/relay-gemini.go` around lines 1357 - 1401, When handling
the empty-candidates branch (len(geminiResponse.Candidates) == 0) you only
populate PromptTokens; also set CompletionTokens and TotalTokens: read
geminiResponse.UsageMetadata.CompletionTokenCount into usage.CompletionTokens if
present, otherwise fall back to usage.CompletionTokenDetails.ReasoningTokens
(which you already set from geminiResponse.UsageMetadata.ThoughtsTokenCount);
then compute usage.TotalTokens = usage.PromptTokens + usage.CompletionTokens
and, as a final fallback, if TotalTokens <= 0 set TotalTokens =
usage.PromptTokens. Update the dto.Usage population in that block (the usage
variable created there) to include these fields so billing/telemetry aren’t
undercounted.
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@web/src/components/table/usage-logs/UsageLogsColumnDefs.jsx`:
- Around line 581-583: Replace the hard-coded "Block reason" label with the i18n
translation API used in this component: when building content in
UsageLogsColumnDefs.jsx (the block that checks isAdminUser &&
other?.reject_reason), call the translation function (e.g., t('Block reason') or
i18n.t('blockReason') consistent with existing keys) and concatenate the
translated label with other.reject_reason instead of the literal string; add the
translation key to your locale files if it doesn't exist and ensure the
component imports/uses the same translation hook or i18n instance as the rest of
the file.
♻️ Duplicate comments (1)
relay/channel/gemini/relay-gemini.go (1)
1361-1375: PopulateTotalTokensandCompletionTokensfor complete usage tracking.The usage object only sets
PromptTokenswhileTotalTokensandCompletionTokensremain at their zero values. For empty candidate responses, completion tokens are typically zero, butTotalTokensshould still equalPromptTokensfor accurate billing/telemetry.🔧 Suggested fix
usage := dto.Usage{ PromptTokens: geminiResponse.UsageMetadata.PromptTokenCount, + TotalTokens: geminiResponse.UsageMetadata.TotalTokenCount, } usage.CompletionTokenDetails.ReasoningTokens = geminiResponse.UsageMetadata.ThoughtsTokenCount for _, detail := range geminiResponse.UsageMetadata.PromptTokensDetails { if detail.Modality == "AUDIO" { usage.PromptTokensDetails.AudioTokens = detail.TokenCount } else if detail.Modality == "TEXT" { usage.PromptTokensDetails.TextTokens = detail.TokenCount } } if usage.PromptTokens <= 0 { usage.PromptTokens = info.GetEstimatePromptTokens() } + if usage.TotalTokens <= 0 { + usage.TotalTokens = usage.PromptTokens + } + usage.CompletionTokens = usage.TotalTokens - usage.PromptTokens + if usage.CompletionTokens < 0 { + usage.CompletionTokens = 0 + }
| if (isAdminUser && other?.reject_reason) { | ||
| content += `\nBlock reason: ${other.reject_reason}`; | ||
| } |
There was a problem hiding this comment.
Use i18n for the "Block reason" label.
This string should be localized like the rest of the column labels.
💡 Suggested change
- if (isAdminUser && other?.reject_reason) {
- content += `\nBlock reason: ${other.reject_reason}`;
- }
+ if (isAdminUser && other?.reject_reason) {
+ content += `\n${t('Block reason')}: ${other.reject_reason}`;
+ }📝 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 (isAdminUser && other?.reject_reason) { | |
| content += `\nBlock reason: ${other.reject_reason}`; | |
| } | |
| if (isAdminUser && other?.reject_reason) { | |
| content += `\n${t('Block reason')}: ${other.reject_reason}`; | |
| } |
🤖 Prompt for AI Agents
In `@web/src/components/table/usage-logs/UsageLogsColumnDefs.jsx` around lines 581
- 583, Replace the hard-coded "Block reason" label with the i18n translation API
used in this component: when building content in UsageLogsColumnDefs.jsx (the
block that checks isAdminUser && other?.reject_reason), call the translation
function (e.g., t('Block reason') or i18n.t('blockReason') consistent with
existing keys) and concatenate the translated label with other.reject_reason
instead of the literal string; add the translation key to your locale files if
it doesn't exist and ensure the component imports/uses the same translation hook
or i18n instance as the rest of the file.
This reverts commit fd25b60.
feat(gemini): 支持 tool_choice 参数转换,优化多个渠道错误处理
feat(gemini): 支持 tool_choice 参数转换,优化多个渠道错误处理
feat(gemini): 支持 tool_choice 参数转换,优化多个渠道错误处理
fix #2540
Summary by CodeRabbit
New Features
Improvements
UI
✏️ Tip: You can customize this high-level summary in your review settings.