feat(core): support custom base URL via env vars#21561
feat(core): support custom base URL via env vars#21561spencer426 merged 5 commits intogoogle-gemini:mainfrom
Conversation
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 enhances the core content generation module by allowing users to specify custom base URLs for Gemini and Vertex AI APIs through environment variables. This provides greater flexibility for routing API traffic through proxies or custom gateways without modifying the application code, while also incorporating robust URL validation to maintain security by enforcing HTTPS for external connections. 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
|
There was a problem hiding this comment.
Code Review
This pull request introduces support for configuring the Gemini and Vertex AI API base URLs via environment variables, which is a valuable feature for routing traffic through proxies. The implementation includes security validation to ensure custom URLs are well-formed and use HTTPS for non-local hosts. The accompanying tests are comprehensive and cover the new functionality well.
spencer426
left a comment
There was a problem hiding this comment.
Thank you for the PR! I have a few suggestions to align with our development guidelines.
Co-authored-by: Spencer <spencertang@google.com>
Co-authored-by: Spencer <spencertang@google.com>
|
Thanks for the update |
Co-authored-by: Spencer <spencertang@google.com>
Co-authored-by: Spencer <spencertang@google.com>
Summary
Adds support for configuring the Gemini and Vertex AI API base URLs via environment variables, while enforcing safe URL usage. This enables routing CLI traffic through proxies or custom gateways without changing code, and keeps the default behavior unchanged when env vars are not set.
Details
AuthType.USE_GEMINIandAuthType.USE_VERTEX_AI(andGATEWAY), we now resolve abaseUrlas:config.baseUrlif provided (e.g., from gateway / higher-level config)GOOGLE_GEMINI_BASE_URLwhen not using Vertex AIGOOGLE_VERTEX_BASE_URLwhen using Vertex AIvalidateBaseUrl(baseUrl: string)incontentGenerator.tshttps:for non-local hostshttp:only forlocalhost,127.0.0.1, and[::1]to support local developmentbaseUrlis passed intoGoogleGenAIviahttpOptions.baseUrlTouched files:
packages/core/src/core/contentGenerator.tspackages/core/src/core/contentGenerator.test.tsRelated Issues
Fixes #6746
How to Validate
npx vitest run packages/core/src/core/contentGenerator.test.tsExpected: all tests pass, including new cases for env-based baseUrl and validateBaseUrl.
2. Manual sanity check – default behavior
npm run build GEMINI_API_KEY=<your_key> npm start3. Manual sanity check – custom Gemini base URL
npm run build GOOGLE_GEMINI_BASE_URL=https://generativelanguage.googleapis.com \ GEMINI_API_KEY=<your_key> \ npm start4. Optional: local proxy test
# run a local HTTP server on 8080 that logs requests GOOGLE_GEMINI_BASE_URL=http://localhost:8080 \ GEMINI_API_KEY=<your_key> \ npm start5. Negative check: insecure non-local URL
Set
GOOGLE_GEMINI_BASE_URL=http://evil-proxy.example.comand start the CLI.Expected: startup fails with Custom base URL must use HTTPS unless it is localhost.
Pre-Merge Checklist