-
Notifications
You must be signed in to change notification settings - Fork 7
Add OpenRouter as a setup connection with searchable model pickers #77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f806a81
Add OpenRouter as a setup connection with searchable model pickers
claude 5349a0d
Allow only OpenRouter transcription models that return word timestamps
claude a77f4d7
Use the Improved heading for the release notes
claude a4f4438
Fix review findings on the OpenRouter connection
claude fbb4cd3
Check the route's key live, and keep the guard on when it has none
claude File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import { readFile } from 'node:fs/promises' | ||
| import { | ||
| OPENROUTER_API_BASE, | ||
| parseOpenRouterModels, | ||
| suggestedAsModels, | ||
| SUGGESTED_OPENROUTER_MODELS, | ||
| OPENROUTER_TRANSCRIPTION_MODELS, | ||
| isSupportedTranscriptionModel, | ||
| type OpenRouterCatalog | ||
| } from '@shared/openrouter' | ||
| import { getOpenRouterKey } from './settings' | ||
|
|
||
| const CATALOG_TTL_MS = 10 * 60_000 | ||
| const REQUEST_TIMEOUT_MS = 12_000 | ||
|
|
||
| let cached: { at: number; catalog: OpenRouterCatalog } | null = null | ||
|
|
||
| async function getJson(path: string, key?: string): Promise<{ status: number; body: unknown }> { | ||
| const res = await fetch(`${OPENROUTER_API_BASE}${path}`, { | ||
| headers: key ? { Authorization: `Bearer ${key}` } : {}, | ||
| signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS) | ||
| }) | ||
| let body: unknown = null | ||
| try { body = await res.json() } catch { /* non-JSON body */ } | ||
| return { status: res.status, body } | ||
| } | ||
|
|
||
| /** | ||
| * The public model catalogue (no key needed): chat models, and the | ||
| * transcription models OpenRouter serves at /audio/transcriptions that | ||
| * Cutawan can use (word timestamps required). Offline, | ||
| * the suggested models stand in so setup still works. | ||
| * `CUTAWAN_OPENROUTER_CATALOG` points at a saved `/models` response, for | ||
| * headless checks without network access. | ||
| */ | ||
| export async function listOpenRouterModels(refresh = false): Promise<OpenRouterCatalog> { | ||
| if (!refresh && cached && Date.now() - cached.at < CATALOG_TTL_MS) return cached.catalog | ||
| const fixture = process.env.CUTAWAN_OPENROUTER_CATALOG | ||
| try { | ||
| let llm, transcription | ||
| if (fixture) { | ||
| const saved = JSON.parse(await readFile(fixture, 'utf8')) as { models: unknown; transcription: unknown } | ||
| llm = parseOpenRouterModels(saved.models, 'llm') | ||
| transcription = parseOpenRouterModels(saved.transcription, 'transcription') | ||
| } else { | ||
| const [chat, speech] = await Promise.all([ | ||
| getJson('/models'), | ||
| getJson('/models?output_modalities=transcription').catch(() => ({ status: 0, body: null })) | ||
| ]) | ||
| if (chat.status !== 200) throw new Error(`OpenRouter returned HTTP ${chat.status}`) | ||
| llm = parseOpenRouterModels(chat.body, 'llm') | ||
| transcription = parseOpenRouterModels(speech.body, 'transcription') | ||
| } | ||
| if (!llm.length) throw new Error('OpenRouter returned no models') | ||
| // Only models that return word timestamps; see OPENROUTER_TRANSCRIPTION_MODELS. | ||
| // A short or failed transcription listing keeps the whole allowlist. | ||
| const supported = transcription.filter(m => isSupportedTranscriptionModel(m.id)) | ||
| const catalog: OpenRouterCatalog = { | ||
| llm, | ||
| transcription: supported.length ? supported : suggestedAsModels(OPENROUTER_TRANSCRIPTION_MODELS), | ||
| live: true | ||
| } | ||
| cached = { at: Date.now(), catalog } | ||
| return catalog | ||
| } catch (error) { | ||
| return { | ||
| llm: suggestedAsModels(SUGGESTED_OPENROUTER_MODELS), | ||
| transcription: suggestedAsModels(OPENROUTER_TRANSCRIPTION_MODELS), | ||
| live: false, | ||
| error: error instanceof Error ? error.message : String(error) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Validate a key (the typed one, else the stored one) with OpenRouter's | ||
| * key-info endpoint. Makes no model request and costs nothing. | ||
| */ | ||
| export async function checkOpenRouterKey(typed?: string): Promise<{ message: string }> { | ||
| const key = typed?.trim() || getOpenRouterKey() | ||
| if (!key) throw new Error('Enter an OpenRouter API key first.') | ||
| let res: { status: number; body: unknown } | ||
| try { | ||
| res = await getJson('/key', key) | ||
| } catch (error) { | ||
| throw new Error(`Could not reach OpenRouter: ${error instanceof Error ? error.message : String(error)}`, { cause: error }) | ||
| } | ||
| if (res.status === 401 || res.status === 403) throw new Error('OpenRouter rejected this key. Copy it again from openrouter.ai/keys.') | ||
| if (res.status !== 200) throw new Error(`OpenRouter key check failed (HTTP ${res.status}).`) | ||
| const data = (res.body as { data?: { limit_remaining?: unknown; is_free_tier?: unknown } } | null)?.data | ||
| const remaining = typeof data?.limit_remaining === 'number' ? ` $${data.limit_remaining.toFixed(2)} of this key’s limit remains.` : '' | ||
| const free = data?.is_free_tier === true ? ' This account is on the free tier; add credits to use paid models.' : '' | ||
| return { message: `OpenRouter accepted the key.${remaining}${free}` } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.