fix: 修复 buildProxyUrl 重复拼接版本前缀的问题(Gemini)#693
Conversation
Summary of ChangesHello @sunxyw, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! 此拉取请求旨在解决 Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
📝 WalkthroughWalkthrough修改 URL 构建逻辑:用正则检测并提取请求路径中的版本前缀(如 /v1、/v1beta、/v1internal、/v2 等),基于捕获的版本与端点动态构建 requestRoot,替换原先固定拼接 Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 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.
Code Review Summary
No significant issues identified in this PR. The implementation correctly addresses the Gemini version prefix duplication issue using a robust regex-based approach.
PR Size: XS
- Lines changed: 65 (49 additions, 16 deletions)
- Files changed: 2
Analysis Results
The PR introduces a dynamic version detection mechanism using regex to strip API version prefixes (v1, v1beta, v1internal, etc.) from request paths before URL construction. This prevents duplicate path segments when baseUrl already contains the complete endpoint path.
Key Changes:
- Replaced hardcoded
/v1prefix with dynamic regex pattern/(v\d+[a-z0-9]*) - Properly handles version variants: v1, v1beta, v1internal, v2, etc.
- Maintains backward compatibility with existing OpenAI fix (#139)
- Three new test cases cover the main scenarios
Review Coverage
- Logic and correctness - Clean
- Security (OWASP Top 10) - Clean
- Error handling - Clean (existing try-catch preserved)
- Type safety - Clean (proper use of optional chaining and nullish coalescing)
- Documentation accuracy - Clean (inline comments match implementation)
- Test coverage - Adequate (3 new tests covering v1beta, v1internal, v2)
- Code clarity - Good (regex escaping helper improves readability)
Automated review by Claude AI
- Move escapeRegExp helper to module scope to avoid recreation - Remove case-insensitive flag from version endpoint regex
Move endpoint regex compilation outside the loop to avoid repeated regex creation on every request. Pre-compile endpoint patterns at module load time for better performance.
* fix: 修复 buildProxyUrl 重复拼接版本前缀的问题 * refactor(proxy): optimize regex compilation in buildProxyUrl - Move escapeRegExp helper to module scope to avoid recreation - Remove case-insensitive flag from version endpoint regex * refactor(proxy): optimize endpoint regex compilation in URL builder Move endpoint regex compilation outside the loop to avoid repeated regex creation on every request. Pre-compile endpoint patterns at module load time for better performance.
Summary
Fix
buildProxyUrlincorrectly duplicating API version prefixes (like/v1beta,/v1internal) whenbaseUrlalready contains the full endpoint path. This issue primarily affects Gemini endpoints but the fix applies to all providers.修复
buildProxyUrl在baseUrl已包含完整端点路径时错误地重复拼接 API 版本前缀(如/v1beta、/v1internal)的问题。此问题主要影响 Gemini 端点,但修复适用于所有供应商。Problem
Previously addressed for OpenAI in #690 and #139, but custom Gemini endpoints still exhibited the bug.
When
baseUrlcontains a complete path (e.g.,/models),buildProxyUrlincorrectly duplicates the version prefix from the request path.Example (Gemini endpoint):
baseUrl="https://co.yes.vg/gemini/models",requestUrl="/v1beta/models/gemini-1.5-pro:streamGenerateContent"https://co.yes.vg/gemini/models/v1beta/models/gemini-1.5-pro:streamGenerateContent❌https://co.yes.vg/gemini/models/gemini-1.5-pro:streamGenerateContent✅Related Issues:
/v1suffix causing incompatibility (closed as duplicate, this PR provides the complete fix)Solution
Replace the hardcoded
/v1prefix logic with a dynamic regex pattern that:/v1,/v1beta,/v1internal,/v2, etc./v\d+[a-z0-9]*to support current and future version formatsbaseUrlends with the target endpoint (e.g.,/models,/messages)This ensures compatibility with:
/v1beta,/v1internal)/v1)/v1)Changes
Core Changes
src/app/v1/_lib/url.ts: Refactored version prefix detection logic/v1to dynamic regex/v\d+[a-z0-9]*/Supporting Changes
tests/unit/app/v1/url.test.ts: Added regression tests/v1betaprefix stripping/v1internalvariant (used by Gemini 2.5)/v2Testing
Automated Tests
/v1beta,/v1internal,/v2variantsManual Testing
Verified with Gemini custom endpoints:
baseUrl="https://custom-proxy.example.com/gemini/models"/v1beta/models/gemini-1.5-pro:streamGenerateContenthttps://custom-proxy.example.com/gemini/models/gemini-1.5-pro:streamGenerateContent(no duplicate/v1beta)Checklist
bun run test)Description enhanced by Claude AI
Greptile Overview
Greptile Summary
This PR successfully extends the URL builder to support dynamic API version prefixes beyond the hardcoded
/v1pattern. The refactoring replaces string-based prefix detection with a regex pattern that matches/v1,/v1beta,/v1internal,/v2, and future version formats using/v\d+[a-z0-9]*.Key Changes:
escapeRegExputility for safe regex pattern construction/v1logic with dynamic version extraction using named capture groupsIssues Found:
/v\d+[a-z0-9]*is overly permissive and could match unconventional version strings likev1xyz999, though this is unlikely to cause issues in practiceThe core logic correctly handles version prefix stripping when
baseUrlalready contains the target endpoint, preventing URL duplication issues that previously affected Gemini and other providers.Confidence Score: 4/5
Important Files Changed
/v1to dynamic regex pattern supporting multiple version formats (/v1beta,/v1internal,/v2)/v1beta,/v1internal,/v2)Sequence Diagram
sequenceDiagram participant Client participant Proxy as buildProxyUrl participant Regex as endpointRegexes participant Target as Target API Client->>Proxy: Request with /v1beta/models/gemini-1.5-pro Note over Proxy: baseUrl="https://api.example.com/gemini/models" Proxy->>Proxy: Parse baseUrl & requestPath Proxy->>Proxy: Check Case 1: exact prefix match Note over Proxy: Not matched Proxy->>Regex: Loop through endpoint patterns Regex->>Regex: Match /v1beta/models/gemini-1.5-pro against<br/>^/(v\d+[a-z0-9]*)/models(?<suffix>/.*)?$ Regex-->>Proxy: Match found: version="v1beta", suffix="/gemini-1.5-pro" Proxy->>Proxy: Check if basePath ends with "/models" Note over Proxy: Condition satisfied Proxy->>Proxy: Construct: basePath + suffix Note over Proxy: Result: /gemini/models/gemini-1.5-pro Proxy->>Target: Forward to https://api.example.com/gemini/models/gemini-1.5-pro Note over Target: Version prefix stripped ✓