Skip to content

feat: dynamic model fetching for GitHub Copilot#408

Merged
luispater merged 2 commits intorouter-for-me:mainfrom
xy-host:feat/dynamic-copilot-models
Mar 5, 2026
Merged

feat: dynamic model fetching for GitHub Copilot#408
luispater merged 2 commits intorouter-for-me:mainfrom
xy-host:feat/dynamic-copilot-models

Conversation

@ym-xie
Copy link

@ym-xie ym-xie commented Mar 4, 2026

fix: #37

- Add ListModels/ListModelsWithGitHubToken to CopilotAuth for querying
  the /models endpoint at api.githubcopilot.com
- Add FetchGitHubCopilotModels in executor with static fallback on failure
- Update service.go to use dynamic fetching (15s timeout) instead of
  hardcoded GetGitHubCopilotModels()
- Add GitHubCopilotAliasesFromModels for auto-generating dot-to-hyphen
  model aliases from dynamic model lists
Copilot AI review requested due to automatic review settings March 4, 2026 06:33
@gemini-code-assist
Copy link

Summary of Changes

Hello, 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 introduces a significant enhancement by enabling dynamic fetching of GitHub Copilot models. Instead of relying on hardcoded model lists, the system can now query the Copilot API directly to retrieve available models. This change improves the flexibility and accuracy of model availability, ensuring the application always reflects the latest offerings from GitHub Copilot. It also includes robust error handling and a fallback to static model definitions to maintain stability.

Highlights

  • Dynamic Model Fetching: Implemented new functionality to dynamically fetch available GitHub Copilot models directly from the Copilot API, moving away from static model definitions.
  • Copilot API Integration: Added new Go structs and methods within the copilot_auth.go package to handle requests to the /models endpoint of the Copilot API, including authentication with a Copilot API token.
  • Dynamic Model Aliasing: Introduced a utility function to generate OAuth model aliases on the fly from dynamically fetched model IDs, specifically addressing the . to - conversion for Claude models.
  • Executor Integration and Fallback: Integrated the dynamic model fetching into the GitHub Copilot executor, ensuring that fetched models are enriched with existing static metadata and providing a robust fallback mechanism to static models in case of API fetching failures.
  • Service Layer Update: Updated the cliproxy service to utilize the new dynamic model fetching mechanism for GitHub Copilot, incorporating a context timeout for the API call.

🧠 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
  • internal/auth/copilot/copilot_auth.go
    • Added CopilotModelEntry and CopilotModelsResponse structs for deserializing Copilot model API responses.
    • Implemented ListModels function to fetch models from the Copilot API using a Copilot API token.
    • Added ListModelsWithGitHubToken as a convenience method to fetch models by first obtaining a Copilot API token from a GitHub access token.
  • internal/config/oauth_model_alias_defaults.go
    • Imported the strings package.
    • Added GitHubCopilotAliasesFromModels function to dynamically generate OAuth model aliases from a list of model IDs, specifically converting dots to hyphens for certain model naming conventions.
  • internal/runtime/executor/github_copilot_executor.go
    • Imported the registry package.
    • Implemented FetchGitHubCopilotModels function to dynamically retrieve models from the GitHub Copilot API, enrich them with static metadata, and provide sensible defaults for new models, with a fallback to static models on failure.
  • sdk/cliproxy/service.go
    • Modified the registerModelsForAuth function to call executor.FetchGitHubCopilotModels for GitHub Copilot, replacing the static model retrieval.
    • Added a 15-second context timeout for the dynamic model fetching operation.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces dynamic fetching of GitHub Copilot models, enhancing the current static list. However, several critical security issues have been identified. The most critical is a potential SSRF and token leakage vulnerability in the ListModels function, stemming from unvalidated endpoint URLs provided by the API. Additionally, there's a risk of memory exhaustion (DoS) due to a lack of response size limits, and a logic flaw in model registration could lead to incorrect availability tracking if duplicate model IDs are returned by the API. Beyond security, the review also highlights areas for improving code clarity, maintainability, and correctness, including refactoring opportunities, addressing unused code, and replacing magic numbers with constants.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses issue #37 by making the GitHub Copilot model list update dynamically from the Copilot API instead of relying solely on hardcoded registry entries, while preserving a static fallback for resiliency.

Changes:

  • Fetch GitHub Copilot models dynamically via Copilot /models (with fallback to static registry data).
  • Add Copilot auth support for /models listing (data structs + request method).
  • Add a helper to derive GitHub Copilot OAuth model aliases from dynamically fetched model IDs.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 7 comments.

File Description
sdk/cliproxy/service.go Switches GitHub Copilot model registration to use the dynamic fetch helper with a timeout.
internal/runtime/executor/github_copilot_executor.go Implements dynamic model fetching + static enrichment/fallback in the executor layer.
internal/auth/copilot/copilot_auth.go Adds /models API types and a method to list models using a Copilot API token.
internal/config/oauth_model_alias_defaults.go Adds helper for generating alias defaults from dynamically fetched model IDs.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +250 to +253
modelsURL := copilotAPIEndpoint + "/models"
if apiToken.Endpoints.API != "" {
modelsURL = apiToken.Endpoints.API + "/models"
}
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When building modelsURL, apiToken.Endpoints.API may already include a trailing slash. Concatenating + "/models" can produce a double-slash URL; consider strings.TrimRight(apiToken.Endpoints.API, "/") + "/models" (similar to how the executor trims endpoints).

Copilot uses AI. Check for mistakes.
- Fix SSRF: validate API endpoint host against allowlist before use
- Limit /models response body to 2MB to prevent memory exhaustion (DoS)
- Use MakeAuthenticatedRequest for consistent headers across API calls
- Trim trailing slash on API endpoint to prevent double-slash URLs
- Use ListModelsWithGitHubToken to simplify token exchange + listing
- Deduplicate model IDs to prevent incorrect registry reference counting
- Remove dead capabilities enrichment code block
- Remove unused ModelExtra field with misleading json:"-" tag
- Extract magic numbers to named constants (defaultCopilotContextLength)
- Remove redundant hyphenID == id check (already filtered by Contains)
- Use defer cancel() for context timeout in service.go
@ym-xie
Copy link
Author

ym-xie commented Mar 4, 2026

@copilot code review[agent]

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +870 to +871
defer cancel()
models = executor.FetchGitHubCopilotModels(ctx, a, s.cfg)
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The timeout context is canceled via defer, so the timer can stay active until registerModelsForAuth returns (which continues doing non-trivial work after this switch). Consider calling cancel() immediately after FetchGitHubCopilotModels returns (as done in the antigravity case) to release resources promptly.

Suggested change
defer cancel()
models = executor.FetchGitHubCopilotModels(ctx, a, s.cfg)
models = executor.FetchGitHubCopilotModels(ctx, a, s.cfg)
cancel()

Copilot uses AI. Check for mistakes.
Comment on lines +255 to +260
// ListModels fetches the list of available models from the Copilot API.
// It requires a valid Copilot API token (not the GitHub access token).
func (c *CopilotAuth) ListModels(ctx context.Context, apiToken *CopilotAPIToken) ([]CopilotModelEntry, error) {
if apiToken == nil || apiToken.Token == "" {
return nil, fmt.Errorf("copilot: api token is required for listing models")
}
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ListModels and the new endpoint validation/response-size limiting logic should have unit tests (similar to internal/auth/copilot/oauth_test.go) to cover: trusted vs untrusted endpoint host handling, non-2xx responses, and successful JSON parsing.

Copilot uses AI. Check for mistakes.
Comment on lines +1276 to +1283
// FetchGitHubCopilotModels dynamically fetches available models from the GitHub Copilot API.
// It exchanges the GitHub access token stored in auth.Metadata for a Copilot API token,
// then queries the /models endpoint. Falls back to the static registry on any failure.
func FetchGitHubCopilotModels(ctx context.Context, auth *cliproxyauth.Auth, cfg *config.Config) []*registry.ModelInfo {
if auth == nil {
log.Debug("github-copilot: auth is nil, using static models")
return registry.GetGitHubCopilotModels()
}
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FetchGitHubCopilotModels introduces new behavior (dynamic model list + merging with static metadata) but there are no tests covering success, empty result fallback, and error fallback paths. Consider adding unit tests (e.g., using an httptest server and injecting/mocking CopilotAuth) to prevent regressions.

Copilot uses AI. Check for mistakes.
@luispater luispater merged commit 88aa1b9 into router-for-me:main Mar 5, 2026
5 of 6 checks passed
@ym-xie ym-xie deleted the feat/dynamic-copilot-models branch March 5, 2026 12:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GitHub Copilot models seem to be hardcoded

4 participants