Skip to content

Conversation

Copilot
Copy link

@Copilot Copilot AI commented Aug 11, 2025

This PR adds OpenRouter as a first-class LLM provider, enabling users to route requests through OpenRouter's unified API to access multiple upstream models (OpenAI, Anthropic, Google, open models, etc.) with a single API key.

What's Added

New OpenRouter Provider: Implements OpenRouterProvider extending BaseLLMProvider using native fetch for explicit control and lighter footprint. Features include:

  • Default model: openai/gpt-4o-mini (cost-effective choice)
  • OpenAI-compatible API format with proper error handling
  • Optional branding headers (HTTP-Referer, X-Title) via environment variables
  • Comprehensive error normalization for HTTP errors, malformed responses, and network issues

Configuration Support:

  • Added openrouter to provider union type in schema
  • Environment variable support: CONTEXT_OPT_LLM_PROVIDER=openrouter and CONTEXT_OPT_OPENROUTER_KEY
  • Configuration validation ensuring API key is required when provider is selected
  • Sanitized config output includes hasOpenrouterKey boolean

Factory Integration: Updated LLMProviderFactory to create OpenRouter provider instances seamlessly alongside existing providers (gemini, claude, openai).

Comprehensive Testing: Added 11 unit tests covering:

  • Provider properties and instantiation
  • API key validation
  • Successful requests with proper headers and body format
  • HTTP error handling with and without error JSON
  • Malformed response handling (missing/empty choices)
  • Network error handling
  • Optional branding header inclusion

Usage

# Required
CONTEXT_OPT_LLM_PROVIDER=openrouter
CONTEXT_OPT_OPENROUTER_KEY=<your-openrouter-api-key>
CONTEXT_OPT_ALLOWED_PATHS=<your-allowed-paths>

# Optional branding headers
CONTEXT_OPT_APP_URL=<your-site-url>
CONTEXT_OPT_APP_NAME=<your-app-name>

Technical Details

  • API Endpoint: https://openrouter.ai/api/v1/chat/completions
  • Request Format: OpenAI-compatible with model, messages, temperature, max_tokens
  • Error Handling: Structured error responses matching existing provider patterns
  • Dependencies: No additional dependencies required (uses native fetch)

All existing functionality remains unchanged. The implementation follows the same patterns as other providers and maintains backward compatibility.

Fixes #1.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • generativelanguage.googleapis.com

If you need me to access, download, or install something from one of these locations, you can either:


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copy link
Author

Copilot AI commented Aug 11, 2025

@malaksedarous 👋 This repository doesn't have Copilot instructions. With Copilot instructions, I can understand the repository better, work faster and produce higher quality PRs.

I can generate a .github/copilot-instructions.md file for you automatically. Click here to open a pre-filled issue and assign it to me. I'll write the instructions, and then tag you for review.

Copilot AI and others added 2 commits August 11, 2025 18:12
Co-authored-by: malaksedarous <18029995+malaksedarous@users.noreply.github.com>
Co-authored-by: malaksedarous <18029995+malaksedarous@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Add support for OpenRouter Add OpenRouter provider support for unified LLM API access Aug 11, 2025
@Copilot Copilot AI requested a review from malaksedarous August 11, 2025 18:22
Copilot finished work on behalf of malaksedarous August 11, 2025 18:22
@malaksedarous malaksedarous requested a review from Copilot August 11, 2025 18:38
Copy link
Contributor

@Copilot 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 OpenRouter as a new LLM provider to enable unified API access to multiple upstream models through a single API key. OpenRouter acts as a router that provides access to OpenAI, Anthropic, Google, and other models via an OpenAI-compatible API format.

  • Implements OpenRouterProvider class with native fetch for lightweight HTTP requests
  • Adds OpenRouter configuration support with environment variables and validation
  • Integrates OpenRouter into the existing provider factory system

Reviewed Changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/providers/openrouter.ts New OpenRouter provider implementation with error handling and optional branding headers
src/providers/factory.ts Added OpenRouter case to provider factory for instantiation
src/config/schema.ts Extended provider union type and config interface to include OpenRouter
src/config/manager.ts Added OpenRouter environment variable support and validation logic
test/openrouter.test.ts Comprehensive test suite covering all provider functionality and error cases

- Added VALID_LLM_PROVIDERS constant to avoid code duplication
- Updated getLLMProvider() and validateConfiguration() to use the constant
- Error messages now dynamically generate from the constant
- Maintains same functionality while improving maintainability
@malaksedarous malaksedarous deleted the branch main August 27, 2025 18:09
@malaksedarous malaksedarous reopened this Aug 27, 2025
@malaksedarous malaksedarous changed the base branch from main-old to main August 27, 2025 18:14
@malaksedarous malaksedarous marked this pull request as ready for review August 27, 2025 18:18
@malaksedarous malaksedarous changed the base branch from ms/fix-tests-on-non-windows to main August 29, 2025 16:25
@malaksedarous malaksedarous changed the base branch from ms/Implement-npm-Trusted-Publishing-with-OIDC to main August 30, 2025 10:14
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 support for OpenRouter
2 participants