|
| 1 | +/** |
| 2 | + * OpenRouter provider — pulls the public OpenAPI 3.1 spec served at |
| 3 | + * openrouter.ai/openapi.json. No auth required for the spec itself. |
| 4 | + * |
| 5 | + * The static spec describes the request/response shape per endpoint |
| 6 | + * (including video generation's frame_images / input_references). Per-model |
| 7 | + * video constraints (supported_durations, resolutions, aspect ratios) live |
| 8 | + * in the separate GET /api/v1/videos/models metadata API — synthesising |
| 9 | + * per-model schemas from that is a candidate follow-up, not covered here. |
| 10 | + */ |
| 11 | + |
| 12 | +import { mkdirSync, readFileSync, writeFileSync } from 'node:fs' |
| 13 | +import { join } from 'node:path' |
| 14 | + |
| 15 | +import { |
| 16 | + applyTransforms, |
| 17 | + mergeOpenAPISpecs, |
| 18 | + toActivitySpecs, |
| 19 | + type MergedSpec, |
| 20 | +} from '../merge-openapi-specs.js' |
| 21 | +import type { |
| 22 | + Activity, |
| 23 | + FetchOptions, |
| 24 | + ProviderCategorySpec, |
| 25 | + ProviderConfig, |
| 26 | +} from '../providers.js' |
| 27 | + |
| 28 | +const OPENROUTER_OPENAPI_URL = 'https://openrouter.ai/openapi.json' |
| 29 | + |
| 30 | +async function fetchOpenRouter({ outDir }: FetchOptions): Promise<void> { |
| 31 | + mkdirSync(outDir, { recursive: true }) |
| 32 | + |
| 33 | + console.log(`Fetching OpenRouter OpenAPI from ${OPENROUTER_OPENAPI_URL}...`) |
| 34 | + const response = await fetch(OPENROUTER_OPENAPI_URL) |
| 35 | + if (!response.ok) { |
| 36 | + throw new Error( |
| 37 | + `OpenRouter fetch failed: ${response.status} ${response.statusText}`, |
| 38 | + ) |
| 39 | + } |
| 40 | + // The spec's key-management examples embed realistic-looking |
| 41 | + // `sk-or-v1-…` API keys that trip GitHub push protection when the spec is |
| 42 | + // committed. Redact them — they only appear in platform endpoints that |
| 43 | + // are dropped from generation anyway. |
| 44 | + const text = (await response.text()).replace( |
| 45 | + /sk-or-v1-[0-9a-f]{16,}/g, |
| 46 | + 'sk-or-v1-REDACTED', |
| 47 | + ) |
| 48 | + const spec = JSON.parse(text) as object |
| 49 | + writeFileSync( |
| 50 | + join(outDir, 'openrouter.openapi.json'), |
| 51 | + JSON.stringify(spec, null, 2), |
| 52 | + ) |
| 53 | + console.log(' Wrote openrouter.openapi.json') |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * Path rules — OpenRouter's generation surface spans the OpenAI-compatible |
| 58 | + * endpoints (chat/completions, completions-style responses) and the |
| 59 | + * Anthropic-compatible /messages. Account management (auth/keys, byok, |
| 60 | + * credits, guardrails, workspaces, observability), the preset-scoped |
| 61 | + * endpoint variants, and rerank (no core activity) drop out of generation. |
| 62 | + */ |
| 63 | +function classifyOpenRouter(path: string): Activity | null { |
| 64 | + if ( |
| 65 | + path === '/chat/completions' || |
| 66 | + path === '/messages' || |
| 67 | + path === '/responses' |
| 68 | + ) { |
| 69 | + return 'chat' |
| 70 | + } |
| 71 | + if (path.startsWith('/audio/')) return 'audio' |
| 72 | + if (path === '/videos') return 'video' |
| 73 | + // NB: /embeddings is intentionally unclassified — OpenRouter declares its |
| 74 | + // request/response schemas inline rather than via $ref, so the endpoint |
| 75 | + // can't be mapped to named schema exports. Revisit if upstream refactors. |
| 76 | + return null |
| 77 | +} |
| 78 | + |
| 79 | +function loadOpenRouter(): Array<ProviderCategorySpec> { |
| 80 | + const specDir = new URL('../specs/openrouter/', import.meta.url).pathname |
| 81 | + let raw: string |
| 82 | + try { |
| 83 | + raw = readFileSync(join(specDir, 'openrouter.openapi.json'), 'utf8') |
| 84 | + } catch { |
| 85 | + return [] |
| 86 | + } |
| 87 | + const spec = JSON.parse(raw) as object |
| 88 | + applyTransforms(spec, { providerId: 'openrouter' }) |
| 89 | + const mergedSpec: MergedSpec = mergeOpenAPISpecs([spec], 'OpenRouter API') |
| 90 | + return toActivitySpecs('openrouter', mergedSpec, classifyOpenRouter) |
| 91 | +} |
| 92 | + |
| 93 | +export const openrouterProvider: ProviderConfig = { |
| 94 | + id: 'openrouter', |
| 95 | + namespace: 'OpenRouter', |
| 96 | + fetch: fetchOpenRouter, |
| 97 | + load: loadOpenRouter, |
| 98 | + requiresAuth: false, |
| 99 | +} |
0 commit comments