Skip to content

feat(llm): add OpenRouter to list of providers - #1522

Merged
lizhengfeng101 merged 3 commits into
alibaba:mainfrom
gadri-07:feat/openrouter-provider
Sep 24, 2026
Merged

lizhengfeng101 merged 3 commits into
alibaba:mainfrom
gadri-07:feat/openrouter-provider

Conversation

@gadri-07

@gadri-07 gadri-07 commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Description

Add OpenRouter to the shared provider registry in internal/llm/providers.go so users can select it through the existing CLI menus and switch between models from different vendors using one API key.

OpenRouter already works through custom-provider setup; this change makes it available directly in the CLI provider menu, with the endpoint and protocol preconfigured.

The integration reuses the existing OpenAI-compatible Chat Completions client and supports OPENROUTER_API_KEY. It provides static model suggestions, and users can enter other compatible models using their exact model IDs through the existing custom model option.

The change includes provider configuration and mock HTTP tests, OpenRouter entries in all five localized provider tables, and English configuration documentation covering setup and model switching.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Refactoring (no functional changes)
  • Documentation update
  • CI / Build / Tooling

How Has This Been Tested?

  • make test passed locally with race detection.
  • make check and make build passed; the rebuilt CLI's version command succeeded.
  • Shared frontend typechecking, tests, and production builds for VS Code and IntelliJ passed.
  • make coverage passed with 92.8% statement coverage, above the 90% threshold.
  • Manual testing confirmed OpenRouter appears in the CLI provider menu, with preset suggestions and custom model entry available.
  • Mock HTTP tests verified request formatting, API key handling, and preservation of preset and manually entered model IDs.
  • Selected google/gemini-3.1-flash-lite through the CLI and completed an OCR review using OpenRouter.

Full IDE runtime testing, the VS Code host suite, and the IntelliJ Gradle suite were not run.

Checklist

  • My code follows the project's coding style (go fmt, go vet)
  • I have performed a self-review of my code
  • I have added tests that prove my fix is effective or my feature works
  • New and existing unit tests pass locally with my changes
  • I have updated the documentation accordingly (if applicable)
  • I have signed the CLA
  • I did not use AI/LLM to create this PR, or I disclosed the tool/model below and reviewed its output; I did not attribute commits to AI and will answer maintainer questions and review comments myself without AI/LLM.

I used Codex (GPT-6) for implementation and drafting, and Open Code Review with DeepSeek V4.1 Flash, GLM 5.3 Flash, and Gemini 3.1 Flash Lite through OpenRouter for automated review. I personally reviewed and manually tested the code.

Related Issues

Related issue: #1540 — Add OpenRouter as a built-in provider.

Related discussion: #17 — Preset Model Providers & Multi-Model Switching.

This PR builds on the provider configuration and model-switching workflow discussed there by adding OpenRouter as a built-in provider, allowing users to switch between compatible models from different vendors with one API key.

@CLAassistant

CLAassistant commented Sep 21, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@github-actions

github-actions Bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

🔍 OpenCodeReview found 3 issue(s) in this PR.

  • ✅ Successfully posted inline: 3 comment(s)

Comment on lines +40 to +42
const response = await fetch(CATALOG_URL, {
signal, credentials: 'omit', redirect: 'error', referrerPolicy: 'no-referrer', cache: 'no-store',
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security · medium
Missing response size limit: The Go CLI counterpart (openrouter_models.go) caps the response body at 1 MiB via io.LimitReader, but this webview fetch has no equivalent protection. A malicious or misconfigured server could return an arbitrarily large response body, potentially causing memory pressure or UI jank in the webview.

Consider adding a size check after reading the response, e.g. using response.body.getReader() with a byte counter, or checking Content-Length header as a preliminary guard.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 97ff97a. The webview now counts bytes as it reads the response and cancels if it exceeds 1 MiB, then falls back to saved or built-in models. Added tests for the limit and fallback behavior too.

Comment on lines +55 to +57
} catch {
return savedOpenRouterModels();
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maintainability · medium
Silent error swallowing: All errors (network failures, invalid JSON, no compatible models) are caught and silently replaced with cached/fallback models. The user receives no indication that the live catalog fetch failed, which can be confusing when expected models are missing from the dropdown.

Consider returning a result type that distinguishes success from fallback (e.g., { models: string[], stale: boolean }) so the UI can optionally show a subtle warning like "Showing cached models" when the live fetch fails. Alternatively, at minimum log the error to the console for debugging purposes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is intentional, to match the pattern of how the models are displayed from the other providers. The fact that it tries to fetch the latest popular models is a bonus, not an expectation from the user. Therefore, surfacing messaging would introduce a new UI/UX pattern is disruptive to the current provider-model flow. At the same time, the reason I fetch it dynamically is to reduce the manual workload of having to open PRs to update the default model options. OpenRouter's model catalogue is 100+ models, and we can't display them all. So this is the best option.

I will add a line to log the error to the console for debugging purposes.

<html lang="${lang}"><head>
<meta charset="UTF-8">
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; style-src ${webview.cspSource} 'unsafe-inline'; script-src 'nonce-${nonce}';">
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; connect-src ${OPENROUTER_CATALOG_ENDPOINT}; style-src ${webview.cspSource} 'unsafe-inline'; script-src 'nonce-${nonce}';">

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security · medium
Using a full URL with path (https://openrouter.ai/api/v1/models) as a CSP connect-src source expression is unconventional and fragile. CSP source expressions are typically specified as origins (e.g., https://openrouter.ai). While browsers do support path-based matching, this tightly couples the security policy to a specific endpoint constant that serves a dual purpose (also used for fetching). If additional OpenRouter API endpoints are ever needed from the webview, or if the path changes, the CSP will silently break or need separate maintenance.

Consider extracting just the origin for the CSP directive, e.g.:

const OPENROUTER_ORIGIN = 'https://openrouter.ai';

and using that in the CSP instead.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’d prefer to keep this scoped to the models endpoint since that’s all the webview needs, and the shared constant already keeps the fetch URL and CSP in sync.

@wu21-web

Copy link
Copy Markdown
Contributor

@gadri-07 Can you open an issue for this?

@gadri-07

Copy link
Copy Markdown
Contributor Author

@gadri-07 Can you open an issue for this?

Done! #1540

@lizhengfeng101

Copy link
Copy Markdown
Contributor

@gadri-07 Nice work, but I'd like to remove the changes under cmd/opencodereview/ and extensions/frontend/, because we're planning to maintain only one internal/llm/providers.go configuration, and other plugins such as VSCode and JetBrains will automatically follow this configuration in a different way. Looking forward to your update.

@wu21-web wu21-web mentioned this pull request Sep 23, 2026
9 of 15 tasks
@gadri-07
gadri-07 force-pushed the feat/openrouter-provider branch from 97ff97a to d0ca4b8 Compare September 23, 2026 15:46
@gadri-07

Copy link
Copy Markdown
Contributor Author

@lizhengfeng101 Thanks for the feedback, I removed those changes. I also removed the dynamic fetching of the latest model names, in order to reduce scope. The stale model names is a feature of all the providers currently listed, so a dynamic model fetching mechanism could be standardized for all of them in a future PR.

Also squashed commits to follow commit convention specified in CONTRIBUTING.md, and updated PR.

@lizhengfeng101 lizhengfeng101 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gadri-07 openrouter_test.go can be dropped. OpenRouter is a standard ProtocolOpenAIChatCompletions entry — no new protocol, no special auth, no new code path. Every previous provider addition in the same category (gemini, xAI, kimi-global, siliconflow, novita) only touched providers_test.go, which this PR already does.

  • TestResolveEndpoint_OpenRouter re-tests generic resolver logic (key precedence, env fallback, missing-key errors) already covered by resolver_test.go.
  • TestOpenRouter_ToolCallingRequest re-tests the OpenAI chat completions client, already covered by client_test.go.

The TestLookupProvider_OpenRouterDetails + sorted-list update in providers_test.go is sufficient.

OpenRouter is a standard ProtocolOpenAIChatCompletions entry — no new
protocol, no special auth, no new code path.  Every previous provider
addition in the same category (gemini, xAI, kimi-global, siliconflow,
novita) only touched providers_test.go, which this PR already does.

TestResolveEndpoint_OpenRouter re-tests generic resolver logic already
covered by resolver_test.go.  TestOpenRouter_ToolCallingRequest re-tests
the OpenAI chat completions client already covered by client_test.go.
The TestLookupProvider_OpenRouterDetails + sorted-list update in
providers_test.go is sufficient.

@lizhengfeng101 lizhengfeng101 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@lizhengfeng101
lizhengfeng101 merged commit a290633 into alibaba:main Sep 24, 2026
15 checks passed
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.

4 participants