fix: custom provider API key handling and OpenRouter support#847
fix: custom provider API key handling and OpenRouter support#847seoeaa wants to merge 6 commits intoagentscope-ai:mainfrom
Conversation
feat(console): add Russian language support
Summary of ChangesHello, 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! This pull request significantly enhances the platform's flexibility and user experience by rectifying a critical issue with custom provider API key management, enabling seamless integration with OpenRouter, and expanding language support to include Russian. These changes collectively improve the reliability of external model connections and make the application more accessible to a broader user base. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
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
|
There was a problem hiding this comment.
Code Review
This pull request correctly addresses an issue with custom provider API key handling and introduces support for OpenRouter, along with adding Russian language localization. The changes are generally well-implemented. I've identified a couple of areas for improvement: one regarding inefficient data fetching in the frontend, and another related to duplicated code for OpenRouter header injection in the backend. My review includes suggestions to address these points to enhance efficiency and maintainability.
| # OpenRouter requires special headers for identification | ||
| if base_url and "openrouter.ai" in base_url.lower(): | ||
| client_kwargs["default_headers"] = { | ||
| "HTTP-Referer": "https://github.com/copaw-ai/CoPaw", | ||
| "X-Title": "CoPaw", | ||
| } |
There was a problem hiding this comment.
The logic to add special headers for OpenRouter is duplicated in multiple places. This exact block is one instance. You can also find similar logic in _build_remote_provider_headers in this file (lines 204-207), and in _create_remote_model_instance in src/copaw/agents/model_factory.py (lines 408-412).
This duplication makes the code harder to maintain. If the headers need to be changed in the future, it will have to be done in three different places.
Additionally, the implementations are inconsistent. This one overwrites default_headers, while the one in model_factory.py correctly appends to it. This could lead to bugs if other default headers are needed.
I recommend centralizing this logic into a single helper function that returns the OpenRouter headers, and then call this function from all three places, ensuring you are updating the default_headers dictionary rather than overwriting it.
# OpenRouter requires special headers for identification
if base_url and "openrouter.ai" in base_url.lower():
if "default_headers" not in client_kwargs:
client_kwargs["default_headers"] = {}
client_kwargs["default_headers"].update({
"HTTP-Referer": "https://github.com/copaw-ai/CoPaw",
"X-Title": "CoPaw",
})
Changes
Custom Provider API Key Fixes
OpenRouter Support
Localization
Root Cause
The original code incorrectly used api_key_prefix field (meant for validation prefixes like sk-) to store the actual API key. This caused authentication failures.