Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/lib/openrouter-models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,21 @@ export const OPENROUTER_DEFAULT_MODEL_ID = "anthropic/claude-haiku-4.5";

/**
* OpenRouter slugs are structured `<org>/<model>` (sometimes with more
* path segments). Validate the shape before we accept a user-supplied
* custom model ID — stops empty strings, whitespace, and obvious typos
* without getting in the way of legitimate rare slugs.
* path segments) and may carry ONE trailing `:variant` suffix. Validate
* the shape before we accept a user-supplied custom model ID — stops
* empty strings, whitespace, and obvious typos without getting in the
* way of legitimate rare slugs.
*
* The `:variant` part is not optional pedantry: every free model on
* OpenRouter is addressed as `<org>/<model>:free`, and routing variants
* (`:online`, `:extended`, `:nitro`, `:thinking`) use the same syntax.
* The original pattern's character class had no colon, so it rejected
* ALL of them — a user pasting the correct id for a free model got
* "invalid model id" from us, not from OpenRouter. Reported in Discord
* 2026-07-30 for `nvidia/nemotron-3-ultra-550b-a55b:free`.
*/
const OPENROUTER_SLUG_RE =
/^[a-z0-9][a-z0-9._-]*(?:\/[a-z0-9][a-z0-9._-]*)+$/i;
/^[a-z0-9][a-z0-9._-]*(?:\/[a-z0-9][a-z0-9._-]*)+(?::[a-z0-9][a-z0-9._-]*)?$/i;
Comment on lines 44 to +45

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Align the validator and tests with the exact <org>/<model> contract.

The + quantifier currently accepts org/model/extra; OpenRouter documents the model shape as one author and one slug, with an optional variant suffix. (openrouter.ai)

  • src/lib/openrouter-models.ts#L44-L45: require exactly one slash-delimited model path.
  • src/tests/unit/openrouter-models.test.ts#L57-L61: add org/model/extra as a rejected regression case.
📍 Affects 2 files
  • src/lib/openrouter-models.ts#L44-L45 (this comment)
  • src/tests/unit/openrouter-models.test.ts#L57-L61
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/lib/openrouter-models.ts` around lines 44 - 45, Update OPENROUTER_SLUG_RE
in src/lib/openrouter-models.ts:44-45 to require exactly one slash-delimited
author/model pair while preserving the optional variant suffix. Add
org/model/extra to the rejected cases in
src/tests/unit/openrouter-models.test.ts:57-61 as a regression test.


export function isValidOpenRouterModelId(id: string): boolean {
return OPENROUTER_SLUG_RE.test(id.trim());
Expand Down
13 changes: 13 additions & 0 deletions src/tests/unit/openrouter-models.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ describe("openrouter-models", () => {
"meta-llama/llama-3.3-70b-instruct",
"x-ai/grok-4-fast",
"deepseek/deepseek-chat-v3.1",
// `:variant` suffixes — every FREE model on OpenRouter is addressed
// this way, and routing variants share the syntax. These were all
// rejected before the slug pattern learned about the colon.
"nvidia/nemotron-3-ultra-550b-a55b:free",
"qwen/qwen-2.5-vl-72b-instruct:free",
"perplexity/sonar:online",
"openai/gpt-4o:extended",
"meta-llama/llama-3.3-70b-instruct:nitro",
])("accepts valid slug %s", (slug) => {
expect(isValidOpenRouterModelId(slug)).toBe(true);
});
Expand All @@ -46,6 +54,11 @@ describe("openrouter-models", () => {
"anthropic//claude",
"anthropic/claude/",
"org/model//variant",
// a colon is allowed, but only as ONE non-empty trailing variant
"anthropic/claude-haiku-4.5:",
"anthropic/claude-haiku-4.5:free:extra",
"anthropic:free",
"anthropic/claude haiku:free",
])("rejects invalid slug %s", (slug) => {
expect(isValidOpenRouterModelId(slug)).toBe(false);
});
Expand Down
Loading