feat(copilot): add GitHub Copilot reverse proxy support#1449
Open
kunish wants to merge 4 commits intoWei-Shaw:mainfrom
Open
feat(copilot): add GitHub Copilot reverse proxy support#1449kunish wants to merge 4 commits intoWei-Shaw:mainfrom
kunish wants to merge 4 commits intoWei-Shaw:mainfrom
Conversation
Add full GitHub Copilot API reverse proxy with OpenAI-compatible endpoints, two-phase token exchange (GitHub OAuth → Copilot JWT), and automatic platform-based request routing. New components: - CopilotTokenProvider: exchanges GitHub tokens for short-lived Copilot API JWTs with 25-minute caching and 5-minute proactive refresh - CopilotGatewayService: forwards requests to api.githubcopilot.com with required Copilot headers (User-Agent, Editor-Version, etc.), SSE streaming support, and vision content detection - CopilotGatewayHandler: HTTP handler with failover loop (up to 5 account switches on 401/429/5xx), billing and concurrency control Routing: - Dedicated /copilot/v1/* routes with ForcePlatform middleware - Automatic routing on /v1/* based on group platform setting Account support: - New "copilot" platform constant - IsCopilot(), GetGitHubToken(), GetCopilotBaseURL() account methods - Credentials: github_token (required), base_url (optional override)
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds a new GitHub Copilot platform to the gateway, including a GitHub-token → Copilot-JWT exchange flow and new routing so Copilot can be used via OpenAI-compatible endpoints.
Changes:
- Add Copilot token exchange + caching (
CopilotTokenProvider) and a forwarding service supporting streaming/non-streaming. - Add a dedicated Copilot gateway handler and register /copilot/v1/ routes, plus auto-routing for Copilot groups on /v1/chat/completions and /v1/responses.
- Introduce
PlatformCopilotconstant and wire the new components into DI.
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
| backend/internal/service/wire.go | Adds Copilot providers/services to Wire set. |
| backend/internal/service/domain_constants.go | Exposes PlatformCopilot in service layer. |
| backend/internal/service/copilot_token_provider.go | Implements GitHub→Copilot token exchange with in-memory caching. |
| backend/internal/service/copilot_gateway_service.go | Implements request forwarding to Copilot upstream (incl. SSE). |
| backend/internal/service/account.go | Adds Copilot helpers (IsCopilot, GetGitHubToken, GetCopilotBaseURL). |
| backend/internal/server/routes/gateway.go | Adds /copilot/v1 routes and Copilot auto-routing for /v1/*. |
| backend/internal/handler/wire.go | Wires the new CopilotGatewayHandler into handlers set. |
| backend/internal/handler/handler.go | Adds CopilotGateway to the global Handlers struct. |
| backend/internal/handler/copilot_gateway_handler.go | Adds Copilot endpoint handler with account failover + user concurrency. |
| backend/internal/domain/constants.go | Adds PlatformCopilot domain constant. |
| backend/cmd/server/wire_gen.go | Updates generated wiring to construct Copilot components. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Remove service-layer c.JSON() calls to prevent double HTTP writes; handler now owns all response writing with c.Writer.Written() guards - Honor account-level base_url override in forward() and ListModels() - Add billing eligibility check and async usage recording in handler - Reject /responses/*subpath for Copilot platform (unsupported upstream) - Use SHA-256 fingerprint as cache key + singleflight to prevent thundering herd + opportunistic eviction of expired entries - Add unit tests for token provider, failover classification, agent detection, and vision content detection
- Check type assertion error on singleflight result (errcheck) - Handle resp.Body.Close() error in ListModels (errcheck) - Fix struct field alignment for gofmt - Remove unused getCalls() test helper (unused)
…or detection Improve isCopilotAgentInitiated() to correctly detect agent-initiated requests that were being misclassified as user-initiated, inflating premium quota consumption. Chat Completions: - Detect assistant tool_use in second-to-last message (covers cases where Claude Code wraps tool results in role:"user" messages) - Correctly mark genuine multi-turn follow-ups as "user" (no false positives on user→assistant(plain)→user patterns) Responses API: - Add function_call_arguments, computer_call, computer_call_output, tool_result type detection - Scan full input history for prior assistant/function items when last item is user-role Tests expanded from 6 to 16 cases covering all edge cases.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
New Components
Account Setup
Platform: copilot, Credentials: github_token (required), base_url (optional)
Test plan