fix(scripts): sanitize tilde prefix in OpenRouter alias model constant names#574
fix(scripts): sanitize tilde prefix in OpenRouter alias model constant names#574AlemTuzlak wants to merge 2 commits into
Conversation
📝 WalkthroughWalkthroughgenerateModelMetaString now maps OpenRouter routing-alias ChangesOpenRouter Routing-Alias Handling
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
View your CI Pipeline Execution ↗ for commit b0ae8f6
☁️ Nx Cloud last updated this comment at |
🚀 Changeset Version PreviewNo changeset entries found. Merging this PR will not cause a version bump for any packages. |
@tanstack/ai
@tanstack/ai-anthropic
@tanstack/ai-client
@tanstack/ai-code-mode
@tanstack/ai-code-mode-skills
@tanstack/ai-devtools-core
@tanstack/ai-elevenlabs
@tanstack/ai-event-client
@tanstack/ai-fal
@tanstack/ai-gemini
@tanstack/ai-grok
@tanstack/ai-groq
@tanstack/ai-isolate-cloudflare
@tanstack/ai-isolate-node
@tanstack/ai-isolate-quickjs
@tanstack/ai-ollama
@tanstack/ai-openai
@tanstack/ai-openrouter
@tanstack/ai-preact
@tanstack/ai-react
@tanstack/ai-react-ui
@tanstack/ai-solid
@tanstack/ai-solid-ui
@tanstack/ai-svelte
@tanstack/ai-utils
@tanstack/ai-vue
@tanstack/ai-vue-ui
@tanstack/openai-base
@tanstack/preact-ai-devtools
@tanstack/react-ai-devtools
@tanstack/solid-ai-devtools
commit: |
…t names The daily sync-models workflow has been failing for several weeks. Example: https://github.com/TanStack/ai/actions/runs/26025311706/job/76496996066 OpenRouter started returning model IDs prefixed with `~` to denote routing aliases (e.g. `~anthropic/claude-haiku-latest`, `~anthropic/claude-opus-latest`, ~16 in the current snapshot). These point to whatever the current canonical model is for that slot and are valid models users can call. The generator transformed the IDs into JavaScript identifiers verbatim, producing `const ~ANTHROPIC_CLAUDE_HAIKU_LATEST = {…}` — not a valid identifier. Prettier then bailed with `SyntaxError: Variable declaration expected. (4:7)` and the workflow failed. Fix: - Map a leading `~` in the model ID to `_` only when deriving the constant name. The original ID is still emitted as a string literal on the meta object's `id` field (`id: '~anthropic/claude-haiku-latest'`), so users can pass the exact ID to `chat({ model: '~anthropic/claude-haiku-latest' })` and the constant list (`OPENROUTER_CHAT_MODELS`) references the alias by that same ID. - Add a regex check inside `generateModelMetaString` that throws with a clear, actionable error if a generated constant name isn't a valid JS identifier. Future OpenRouter ID quirks will fail loudly at generation time instead of writing invalid TS that breaks later in the pipeline. Verified locally: `pnpm regenerate:models` now produces valid identifiers like `_ANTHROPIC_CLAUDE_HAIKU_LATEST` with the original `'~anthropic/...'` strings intact, and `pnpm exec prettier --write` on the generated file succeeds.
07cd914 to
f1bd255
Compare
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
scripts/convert-openrouter-models.ts (1)
284-286:⚠️ Potential issue | 🟠 Major | ⚡ Quick winRouting-alias models are still included in generated output.
convertModelsstill processes every model, so~-prefixed routing aliases will remain in staticmodel-meta.ts(just renamed), which defeats the “skip unstable aliases” objective.Suggested fix
function convertModels(models: Array<OpenRouterModel>): string { - const modelStrings = models.map(generateModelMetaString) + const modelStrings = models + .filter((model) => !model.id.startsWith('~')) + .map(generateModelMetaString) return modelStrings.join('\n') }🤖 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 `@scripts/convert-openrouter-models.ts` around lines 284 - 286, convertModels is still including routing-alias models (those prefixed with '~'), so filter them out before mapping; update convertModels to first filter the input models array (e.g., models.filter(m => !(m.id?.startsWith('~') || m.name?.startsWith('~')))) and then map the remaining models with generateModelMetaString so the generated output omits unstable routing-alias entries; reference convertModels and generateModelMetaString and ensure the predicate checks the model identifier fields (id/name) that carry the '~' prefix.
🤖 Prompt for all review comments with 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.
Outside diff comments:
In `@scripts/convert-openrouter-models.ts`:
- Around line 284-286: convertModels is still including routing-alias models
(those prefixed with '~'), so filter them out before mapping; update
convertModels to first filter the input models array (e.g., models.filter(m =>
!(m.id?.startsWith('~') || m.name?.startsWith('~')))) and then map the remaining
models with generateModelMetaString so the generated output omits unstable
routing-alias entries; reference convertModels and generateModelMetaString and
ensure the predicate checks the model identifier fields (id/name) that carry the
'~' prefix.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: ab2f9af4-5d38-43d3-aa0c-2f7ea9d70bb3
📒 Files selected for processing (1)
scripts/convert-openrouter-models.ts
Summary
The daily Sync Model Metadata workflow has been failing for several weeks.
Example: https://github.com/TanStack/ai/actions/runs/26025311706/job/76496996066
Root cause
OpenRouter started returning ~16 model IDs prefixed with
~to denote unstable routing aliases (e.g.~anthropic/claude-haiku-latest). The model-meta generator (scripts/convert-openrouter-models.ts) transformed those IDs into JS identifiers verbatim, producing:Not a valid identifier. The file got written, then the next workflow step (
pnpm format→ prettier) bailed withSyntaxError: Variable declaration expected. (4:7)and the whole sync step failed.Fix
isRoutingAlias(id)recognises the~prefix.convertModelsfilters routing aliases out before they reach the generator. They don't belong in staticmodel-meta.tsanyway — the alias resolves to whatever the current canonical model is, and we'd never know if it changed between sync runs.generateModelMetaStringnow throws a clear, actionable error if a generated constant name isn't a valid JS identifier. Future OpenRouter ID quirks will fail loudly at generation time instead of writing invalid TS that breaks later in the pipeline.The provider-specific sync (
sync-provider-models.ts) was unaffected because it filters byprefix/(e.g.anthropic/) which doesn't match~anthropic/.Test plan
Sync Model Metadataworkflow (workflow_dispatch) on this branch — should complete green and either no-op or open the automated/sync-models PR.pnpm regenerate:models && pnpm exec prettier --check packages/typescript/ai-openrouter/src/model-meta.tsboth succeed. The script logsSkipped N OpenRouter routing-alias model(s)so the filter's behaviour is visible.Summary by CodeRabbit