OpenRouter Client (TypeScript)
item / server / ai
The TypeScript twin of openrouter, on http-retry-ts: fallback models, strict json_schema output, usage.cost, served-model drift warning, fails closed without a key.
- type
- item
- files
- 2
- registry deps
- https://components.automindz.app/r/http-retry-ts.json
- npm deps
- none
- env vars
- OPENROUTER_API_KEY, OPENROUTER_BASE_URL, OPENROUTER_APP_URL, OPENROUTER_APP_NAME
installs
- ~/lib/automindz/openrouter.ts
- ~/tests/openrouter.test.mts
- Same contract as the Python twin, camelCased, on fetchWithRetry
- Strict json_schema output; a parse failure is a typed { error } on result.parsed, never a throw
- Injectable fetch, so the shipped test runs with zero network
import { chat } from "@/lib/automindz/openrouter" (the file imports @/lib/automindz/http-retry, so the app needs the standard @/ path alias); const result = await chat([{ role: "user", content: "Say hi" }]) — same contract as the Python twin, camelCased: result.usage is { promptTokens, completionTokens, cost }, result.model versus result.requestedModel. fallbackModels: [...] reroutes server-side; a drift between served and requested model logs a console.warn naming both. jsonSchema: { name, schema } gives strict structured output in result.parsed (or { error } on a parse failure, never a throw). retries and timeoutMs go straight to fetchWithRetry; a transport failure throws OpenRouterError with status: 0. Requires OPENROUTER_API_KEY; without it chat() throws before any request, and the key never appears in a message or a log. OPENROUTER_BASE_URL, OPENROUTER_APP_URL / OPENROUTER_APP_NAME as in the Python twin. Run the shipped test with node --test tests/openrouter.test.mts.
Variations
one state per section
No previews for this item yet.
