Skip to content

feat(copilot): add GitHub Copilot reverse proxy support#1449

Open
kunish wants to merge 4 commits intoWei-Shaw:mainfrom
kunish:feat/copilot-reverse-proxy
Open

feat(copilot): add GitHub Copilot reverse proxy support#1449
kunish wants to merge 4 commits intoWei-Shaw:mainfrom
kunish:feat/copilot-reverse-proxy

Conversation

@kunish
Copy link
Copy Markdown
Contributor

@kunish kunish commented Apr 3, 2026

Summary

  • Add GitHub Copilot API reverse proxy with OpenAI-compatible endpoints
  • Two-phase token exchange: GitHub OAuth → Copilot JWT with 25min caching
  • Dedicated /copilot/v1/* routes + automatic /v1/* platform routing

New Components

  • CopilotTokenProvider: GitHub token → Copilot JWT exchange with RWMutex caching
  • CopilotGatewayService: request forwarding with Copilot headers, SSE streaming
  • CopilotGatewayHandler: HTTP handler with failover loop, billing and concurrency

Account Setup

Platform: copilot, Credentials: github_token (required), base_url (optional)

Test plan

  • Create Copilot account and verify token exchange
  • Test /copilot/v1/chat/completions (streaming + non-streaming)
  • Test /copilot/v1/responses and /copilot/v1/models
  • Test auto-routing via /v1/* with group platform = copilot
  • Verify existing routes are unaffected

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)
Copilot AI review requested due to automatic review settings April 3, 2026 14:53
Copy link
Copy Markdown
Contributor

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 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 PlatformCopilot constant 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.

kunish added 3 commits April 4, 2026 02:02
- 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.
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.

2 participants