Repository navigation
feat(llm): add OpenRouter to list of providers - #1522
Conversation
|
🔍 OpenCodeReview found 3 issue(s) in this PR.
|
| const response = await fetch(CATALOG_URL, { | ||
| signal, credentials: 'omit', redirect: 'error', referrerPolicy: 'no-referrer', cache: 'no-store', | ||
| }); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| } catch { | ||
| return savedOpenRouterModels(); | ||
| } |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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}';"> |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
@gadri-07 Can you open an issue for this? |
|
@gadri-07 Nice work, but I'd like to remove the changes under |
97ff97a to
d0ca4b8
Compare
|
@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
left a comment
There was a problem hiding this comment.
@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_OpenRouterre-tests generic resolver logic (key precedence, env fallback, missing-key errors) already covered byresolver_test.go.TestOpenRouter_ToolCallingRequestre-tests the OpenAI chat completions client, already covered byclient_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.
Description
Add OpenRouter to the shared provider registry in
internal/llm/providers.goso 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
How Has This Been Tested?
make testpassed locally with race detection.make checkandmake buildpassed; the rebuilt CLI's version command succeeded.make coveragepassed with 92.8% statement coverage, above the 90% threshold.google/gemini-3.1-flash-litethrough 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
go fmt,go vet)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.