Skip to content

feat: add OpenRouter adapter#27

Merged
ChiragAgg5k merged 5 commits intomainfrom
feat/openrouter-adapter
Mar 26, 2026
Merged

feat: add OpenRouter adapter#27
ChiragAgg5k merged 5 commits intomainfrom
feat/openrouter-adapter

Conversation

@ChiragAgg5k
Copy link
Copy Markdown
Member

Summary

  • Adds OpenRouter adapter extending OpenAI with support for OpenRouter's API endpoint, custom headers (HTTP-Referer, X-Title), and provider-prefixed model ID normalization
  • Introduces OpenRouter\Models class with named constants for major providers (OpenAI, Anthropic, Google, Meta, DeepSeek, Mistral, xAI) and the full 346-model catalog
  • Adds scripts/sync-openrouter-models.php to regenerate model constants from the live OpenRouter API
  • Refactors OpenAI adapter to extract createClient() and normalizeModelForCompatibilityChecks() for clean subclassing

Test plan

  • Verify OpenRouter adapter constructs with default model and endpoint
  • Verify arbitrary provider-prefixed model IDs (e.g. meta-llama/llama-3.3-70b-instruct) are accepted
  • Run ConversationOpenRouterTest with LLM_KEY_OPENROUTER set to confirm live API connectivity
  • Run php scripts/sync-openrouter-models.php to verify model sync generates valid PHP
  • Confirm existing OpenAI adapter tests still pass after the refactor

@ChiragAgg5k ChiragAgg5k force-pushed the feat/openrouter-adapter branch from 1b66bb2 to 188241d Compare March 26, 2026 03:17
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Mar 26, 2026

Greptile Summary

This PR adds an OpenRouter adapter that extends the OpenAI adapter, routing requests through OpenRouter's unified API endpoint. The core adapter logic is clean and well-structured: createClient() is overridden to inject the optional HTTP-Referer and X-Title attribution headers, and normalizeModelForCompatibilityChecks() correctly strips provider prefixes (e.g. openai/gpt-4ogpt-4o) so that OpenAI-specific token-limit and temperature constraints are applied only when routing to compatible OpenAI models.\n\nKey additions:\n- OpenRouter adapter with clean subclassing of the refactored OpenAI base\n- OpenRouter\\Models class with 115 named constants for curated providers and a full 346-entry MODELS catalog\n- scripts/sync-openrouter-models.php to regenerate Models.php from the live OpenRouter API\n- Integration test, env/compose wiring, and README documentation\n\nThe main concerns are all in the sync script: the DEFAULT_MODEL constant reference is hardcoded with no validation guard (could silently produce broken PHP on a future sync run), toConstantName() has a double-prefix latent bug, and generated string literals are not single-quote-escaped.

Confidence Score: 4/5

Safe to merge; the current generated Models.php is correct and the adapter logic is sound — the main fix needed is a validation guard in the sync script.

The adapter and test code are well-implemented with no runtime issues in the current state. The one targeted fix is adding a guard in sync-openrouter-models.php so future runs cannot silently produce a Models.php with an unresolvable constant reference.

scripts/sync-openrouter-models.php — hardcoded DEFAULT_MODEL reference and toConstantName double-prefix bug.

Important Files Changed

Filename Overview
src/Agents/Adapters/OpenRouter.php New adapter extending OpenAI; cleanly overrides createClient() to inject HTTP-Referer/X-Title headers and normalizeModelForCompatibilityChecks() to strip provider prefixes before OpenAI-specific token/temperature constraint checks.
src/Agents/Adapters/OpenAI.php Refactored to extract createClient() and normalizeModelForCompatibilityChecks() as protected overridable methods; no behavioral changes to the existing OpenAI adapter.
scripts/sync-openrouter-models.php New code-generation script; has a hardcoded DEFAULT_MODEL reference with no validation guard, a double-prefix bug in toConstantName(), and no single-quote escaping for generated string literals.
src/Agents/Adapters/OpenRouter/Models.php Generated model catalog with 115 named constants and a 346-entry MODELS array; currently correct and MODEL_OPENAI_GPT_4O is present as required by DEFAULT_MODEL.
tests/Agents/Conversation/ConversationOpenRouterTest.php Simple integration test extending ConversationBase; correctly reads LLM_KEY_OPENROUTER from env and constructs the adapter with a named model constant.
.env.example Adds LLM_KEY_OPENROUTER placeholder and fixes missing newline at end of file.
docker-compose.yml Passes LLM_KEY_OPENROUTER env var into the test container, consistent with other adapter keys.
README.md Adds OpenRouter usage section with code example and feature notes; straightforward documentation update.

Reviews (1): Last reviewed commit: "feat: add OpenRouter adapter" | Re-trigger Greptile

@ChiragAgg5k ChiragAgg5k force-pushed the feat/openrouter-adapter branch from 188241d to b573c1b Compare March 26, 2026 03:22
Adds OpenRouter as a new AI provider, extending the OpenAI adapter with
custom HTTP headers (HTTP-Referer, X-Title) and provider-prefixed model
ID normalization. Includes a PHP sync script to generate model constants
from the live OpenRouter API catalog.
@ChiragAgg5k ChiragAgg5k force-pushed the feat/openrouter-adapter branch from b573c1b to 8df9473 Compare March 26, 2026 03:24
@ChiragAgg5k ChiragAgg5k force-pushed the feat/openrouter-adapter branch from 2ced0ce to a3858ec Compare March 26, 2026 03:26
@ChiragAgg5k ChiragAgg5k merged commit 06064fd into main Mar 26, 2026
11 checks passed
@ChiragAgg5k ChiragAgg5k deleted the feat/openrouter-adapter branch March 26, 2026 03:51
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