Skip to content

feat: add Custom OpenAI Provider support (BYOK) #204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jmcombs
Copy link

@jmcombs jmcombs commented Jul 10, 2025

Summary

This PR implements comprehensive Custom OpenAI Provider support (Bring Your Own Key - BYOK) for GitHub Copilot Chat, enabling users to integrate any OpenAI-compatible API endpoint with their own API keys.

Features Added

🔧 Core Functionality

  • Custom API Endpoint Support: Connect to any OpenAI-compatible API (Grok/X.AI, self-hosted OpenAI, etc.)
  • Secure Credential Storage: API keys stored securely using VS Code's SecretStorage API
  • Real-time Connectivity Testing: Validate endpoints and credentials before saving
  • Model Auto-discovery: Automatically detect available models from custom endpoints
  • Comprehensive Validation: Input validation for URLs, API keys, and model configurations

🎨 User Interface

  • Intuitive Configuration UI: Easy-to-use interface for setting up custom providers
  • Model Management: Add, edit, and remove custom model configurations
  • Connection Status: Real-time feedback on endpoint connectivity
  • Error Handling: Clear error messages and troubleshooting guidance

🛠️ Technical Implementation

  • Modular Architecture: Clean separation between UI, storage, and provider logic
  • Type Safety: Full TypeScript implementation with comprehensive type definitions
  • Error Recovery: Robust error handling and graceful degradation
  • Performance Optimized: Efficient caching and minimal API calls

🎯 Edit Mode Support Fix

  • Agent Mode Capability: Custom models now properly calculate agentMode based on tool calling support and token limits
  • Edit Mode v2 Compatibility: Custom OpenAI models with tool calling can now use Edit Mode when chat.edits2.enabled: true
  • Capability Detection: Automatic detection of model capabilities (tool calling, vision, token limits)

Technical Details

Files Added/Modified

  • src/extension/byok/ - Complete BYOK implementation
    • common/byokProvider.ts - Core provider logic and model metadata
    • vscode-node/byokContribution.ts - VS Code integration and lifecycle
    • vscode-node/byokStorageService.ts - Secure credential storage
    • vscode-node/byokUIService.ts - Configuration UI and user interaction
    • vscode-node/customOpenAIProvider.ts - OpenAI-compatible API integration
    • node/openAIEndpoint.ts - HTTP client and API communication
    • vscode-node/test/ - Comprehensive test coverage

Key Fix: Edit Mode Support

The critical fix for Edit Mode support was in byokProvider.ts:

// Before: agentMode was hardcoded to false
agentMode: false, // Disable Agent mode for custom models

// After: agentMode calculated based on capabilities
const supportsToolCalls = chatModelInfo.capabilities.supports.tool_calls;
const hasEnoughTokens = inputTokens > 40000;
const agentModeEnabled = supportsToolCalls && hasEnoughTokens;

capabilities: {
  agentMode: agentModeEnabled,
  toolCalling: supportsToolCalls,
  vision: chatModelInfo.capabilities.supports.vision,
}

This ensures custom models with tool calling support and sufficient context window (>40k tokens) can use Edit Mode v2, matching the behavior of built-in OpenAI models.

Testing

  • Unit Tests: Comprehensive test coverage for all components
  • Integration Tests: End-to-end testing of provider registration and model usage
  • Manual Testing: Verified with multiple OpenAI-compatible endpoints
  • Edit Mode Testing: Confirmed Edit Mode works with custom models that support tool calling

Fixes

Closes microsoft/vscode-copilot-release#7518

Review Notes

@lramos15 This implements the Custom OpenAI Provider feature requested in the issue. The implementation includes:

  1. Complete BYOK Infrastructure: Secure storage, validation, and UI
  2. Edit Mode Fix: The key issue where custom models couldn't use Edit Mode is resolved
  3. Extensible Design: Easy to add support for other OpenAI-compatible providers
  4. Production Ready: Comprehensive error handling, validation, and testing

The Edit Mode regression mentioned in the issue is fixed by properly calculating the agentMode capability for custom models based on their actual capabilities (tool calling + token limits) rather than hardcoding it to false.

Testing was completed using xAI with grok3 and grok4. I do not have access to any other OpenAI compatible language models. Testing with other OpenAI compatible endpoints is highly advised.

I will note that I felt it was a little slower on Edit mode than with Ask mode. I don't know if it's due to running v2 of edit mode and on insiders edition of VS Code.

- Add comprehensive Custom OpenAI Provider implementation
- Support for custom API endpoints with OpenAI-compatible APIs
- Secure credential storage with VS Code SecretStorage
- Real-time connectivity testing and validation
- UI for model configuration and management
- Fix Edit Mode support for custom models with tool calling
- Add comprehensive test coverage

Fixes microsoft/vscode-copilot-release#7518
@jmcombs
Copy link
Author

jmcombs commented Jul 10, 2025

@microsoft-github-policy-service agree

@Igorgro
Copy link

Igorgro commented Jul 10, 2025

How is this different from #38 ?

@jmcombs
Copy link
Author

jmcombs commented Jul 10, 2025

@Igorgro 🤷‍♂️ I submitted the PR following their documented process and attached the issue to it. Had the other been attached to the issue properly, I would have not bothered. I can’t speak to the differences in code.

@rasyidrafi
Copy link

rasyidrafi commented Jul 12, 2025

@isidorn @lramos15, could you please review this and #38? I think this feature has been requested for over a week and hasn't been implemented. Community already make great PR, I get it if your team is busy or something, just genuinely asking, all peace, no hate. 😊 thanks for the great work

@bartlettroscoe
Copy link

The ability to run arbitrary local models is obviously one of the first features the community was going to request and implement PRs for once co-pilot was open sourced. The fact that multiple PRs are being submitted for this key feature shouldn't come as any surprise if it is not being addressed.

Microsoft/GitHub should have anticipated this before and they even posted this GitHub repository in 6/30/2025.

1 similar comment
@bartlettroscoe
Copy link

The ability to run arbitrary local models is obviously one of the first features the community was going to request and implement PRs for once co-pilot was open sourced. The fact that multiple PRs are being submitted for this key feature shouldn't come as any surprise if it is not being addressed.

Microsoft/GitHub should have anticipated this before and they even posted this GitHub repository in 6/30/2025.

@jmcombs
Copy link
Author

jmcombs commented Jul 14, 2025

@bartlettroscoe I believe the way that I have this plugin structured, you can test with locally hosted, as well. As long as it has an OpenAI compatible endpoint and is reachable by the machine running it, it should work.

@bartlettroscoe
Copy link

@jmcombs, does this PR remove the need to log into your GitHub account to use Copilot locally?

@jmcombs
Copy link
Author

jmcombs commented Jul 14, 2025

@bartlettroscoe No, it does not. I know exactly where the few lines of code are that do the check because I commented it out for testing but, I suspect they'd reject the PR if I kept it out because I am pretty sure they require an active Copilot subscription for the privilege to use this extension.

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.

Add custom OpenAI endpoint configuration (Base URL & Model Parameter) of copilot chat settings
6 participants