How OpenCode Integrates with GitHub Copilot as an AI Provider
OpenCode integrates GitHub Copilot by registering the @ai-sdk/github-copilot SDK as a first-class provider, supporting both standard and enterprise endpoints with automatic authentication handling and unified configuration transformation.
OpenCode, developed in the anomalyco/opencode repository, treats GitHub Copilot as a native AI provider alongside OpenAI and Anthropic. This deep integration allows developers to use Copilot models through the same CLI, SDK, and web interface without manual configuration of endpoints or tokens.
Provider Registration and SDK Integration
The integration begins in packages/opencode/src/provider/provider.ts, where OpenCode registers the Copilot SDK under the generic @ai-sdk/github-copilot key. The factory function createGitHubCopilotOpenAICompatible instantiates the provider instance, mapping it to two concrete provider IDs:
"github-copilot"– the standard consumer endpoint"github-copilot-enterprise"– the enterprise-managed endpoint
This dual-ID architecture allows the system to route requests to the appropriate Copilot tier based on the user's subscription level.
Model Resolution and Enterprise Endpoint Handling
When resolving models, OpenCode forces the SDK identifier to @ai-sdk/github-copilot in packages/opencode/src/plugin/copilot.ts. This ensures that all Copilot-specific logic flows through the dedicated plugin layer.
The plugin dynamically switches between endpoints based on authentication status. If the user has valid enterprise credentials, the plugin routes requests to the github-copilot-enterprise endpoint; otherwise, it falls back to the standard github-copilot endpoint. This automatic tier detection removes the need for manual configuration files.
Configuration Transformation and Option Normalization
OpenCode normalizes provider-specific options through packages/opencode/src/provider/transform.ts. When the system detects the @ai-sdk/github-copilot SDK identifier, it rewrites provider-specific options to the unified copilot key.
This transformation layer ensures that flags like temperature, top-p, or custom headers are correctly mapped to the underlying Copilot API format, maintaining compatibility with OpenCode's unified configuration schema across all AI providers.
Authentication and Credential Management
Authentication handling resides in packages/opencode/src/provider/provider.ts. The system implements a fallback chain: it first searches for stored credentials under "github-copilot", and if none exist, checks "github-copilot-enterprise" (lines 911–918).
After a successful Copilot login, OpenCode automatically registers the enterprise credential, making both provider IDs usable simultaneously (lines 933–935). This seamless dual-credential management ensures that users can switch between personal and enterprise Copilot tiers without re-authenticating.
Error Handling and Re-authentication Flows
OpenCode maps Copilot-specific HTTP errors to generic re-authentication prompts in packages/opencode/src/provider/error.ts. When the system intercepts a 403 response from the Copilot API, it translates the error into a user-friendly "reauthenticate" message (line 43).
This abstraction allows the CLI and UI to handle token expiration consistently across all providers, presenting a unified interface for credential refresh regardless of the underlying AI service.
UI Integration and Documentation
The integration extends to the user interface layer. The popular providers list in packages/app/src/hooks/use-providers.ts includes "github-copilot", ensuring Copilot appears in the default provider selection UI.
Visual assets are defined in packages/ui/src/components/provider-icons/types.ts (line 57) and rendered via the SVG sprite in packages/ui/src/components/provider-icons/sprite.svg (line 635), displaying the official Copilot logo alongside other AI providers.
Documentation in packages/web/src/content/docs/providers.mdx outlines the optional Pro+ subscription requirements for certain Copilot models and links to the official GitHub Copilot documentation (lines 793–795).
Summary
OpenCode integrates GitHub Copilot as a first-class AI provider through a multi-layered architecture:
- Provider Registration: Registers the
@ai-sdk/github-copilotSDK with dual provider IDs for standard and enterprise tiers inpackages/opencode/src/provider/provider.ts - Dynamic Routing: Automatically switches between
github-copilotandgithub-copilot-enterpriseendpoints based on authentication status inpackages/opencode/src/plugin/copilot.ts - Configuration Normalization: Transforms provider-specific options to the unified
copilotkey inpackages/opencode/src/provider/transform.ts - Seamless Authentication: Manages dual credentials with automatic enterprise registration after login in
packages/opencode/src/provider/provider.ts - Unified Error Handling: Maps 403 responses to re-authentication prompts in
packages/opencode/src/provider/error.ts - Full UI Support: Renders Copilot in provider lists, icons, and documentation across the application
Frequently Asked Questions
How does OpenCode handle authentication for both standard and enterprise GitHub Copilot accounts?
OpenCode checks for stored credentials under both "github-copilot" and "github-copilot-enterprise" provider IDs, falling back from standard to enterprise automatically. After a successful login, the system registers the enterprise credential in packages/opencode/src/provider/provider.ts (lines 933–935), making both endpoints available without requiring separate authentication flows.
What happens when a GitHub Copilot API request returns a 403 error?
When the Copilot API returns a 403 response, OpenCode intercepts the error in packages/opencode/src/provider/error.ts (line 43) and translates it into a generic "reauthenticate" message. This abstraction ensures that the CLI and UI present a consistent credential refresh experience across all AI providers, regardless of the underlying HTTP error codes.
Can OpenCode automatically switch between standard and enterprise Copilot endpoints?
Yes, OpenCode dynamically routes requests to the appropriate endpoint based on the user's authentication status. In packages/opencode/src/plugin/copilot.ts (lines 187–192), the plugin checks for valid enterprise credentials and switches the provider ID from github-copilot to github-copilot-enterprise automatically, eliminating the need for manual configuration changes when upgrading to an enterprise subscription.
Where is the GitHub Copilot provider configured in the OpenCode UI?
The Copilot provider appears in the default provider selection list defined in packages/app/src/hooks/use-providers.ts, which includes "github-copilot" among the popular providers. The visual representation uses the Copilot icon defined in packages/ui/src/components/provider-icons/types.ts (line 57) and rendered via the SVG sprite in packages/ui/src/components/provider-icons/sprite.svg (line 635), ensuring consistent branding across the application interface.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →