-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat: 支持仅使用x-api-key获取anthropic格式的模型列表 #2422 #2425
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
WalkthroughThe TokenAuth middleware in Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
📜 Recent review detailsConfiguration used: Organization UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (1)
middleware/auth.go (1)
197-202: Consider using more precise path matching.The change successfully extends x-api-key handling to
/v1/modelsas intended. However, usingstrings.Containsmay match unintended paths like/api/v1/modelsor/v1/models-legacy.Additionally, paths like
/v1/models/model-namewill match both this Anthropic block and the Gemini handling block below (lines 204-216). If bothx-api-keyand Gemini keys are present, the Gemini block will overwrite the Authorization header.Consider using more precise matching:
- if strings.Contains(c.Request.URL.Path, "/v1/messages") || strings.Contains(c.Request.URL.Path, "/v1/models") { + if strings.HasPrefix(c.Request.URL.Path, "/v1/messages") || c.Request.URL.Path == "/v1/models" || strings.HasPrefix(c.Request.URL.Path, "/v1/models/") { anthropicKey := c.Request.Header.Get("x-api-key") if anthropicKey != "" { c.Request.Header.Set("Authorization", "Bearer "+anthropicKey) } }Note: The
/v1/messagescheck may also benefit from this pattern, though that's outside the scope of this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
middleware/auth.go (1)
198-204: UseHasPrefixinstead ofContainsfor more precise path matching.The current implementation uses
strings.Contains()to match paths, which is overly broad and inconsistent with the Gemini logic (line 208) that usesHasPrefix. While the current codebase has no routes that would be unintended matches, usingContainsis less defensive against future route additions like/v1/models_adminor/v1/messages_internal.- // 检查path包含/v1/messages 或 /v1/models - if strings.Contains(c.Request.URL.Path, "/v1/messages") || strings.Contains(c.Request.URL.Path, "/v1/models") { + // 检查path是/v1/messages 或 /v1/models 开头 + if strings.HasPrefix(c.Request.URL.Path, "/v1/messages") || strings.HasPrefix(c.Request.URL.Path, "/v1/models") { anthropicKey := c.Request.Header.Get("x-api-key") if anthropicKey != "" { c.Request.Header.Set("Authorization", "Bearer "+anthropicKey) } }
对于/v1/models请求也尝试从x-api-key 获取Bearer Token
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.