|
| 1 | +import {requestHeaders} from './providers.js'; |
| 2 | + |
| 3 | +const MODELS_TIMEOUT_MS = 20000; |
| 4 | + |
| 5 | +// Gemini's model list includes embedding, image, video and speech models that cannot chat. |
| 6 | +const GEMINI_NON_CHAT = /embedding|imagen|veo|tts|image|audio|live|robotics|aqa|learnlm|computer-use/i; |
| 7 | + |
| 8 | +/** |
| 9 | + * @typedef {object} TorchyModel |
| 10 | + * @property {string} key unique across providers: `${providerId}:${modelId}` |
| 11 | + * @property {string} providerId |
| 12 | + * @property {string} modelId the id sent as `model` in chat requests |
| 13 | + * @property {string} name display name |
| 14 | + * @property {boolean|null} free true/false when the provider publishes pricing, null otherwise |
| 15 | + * @property {string} group dropdown group label within the provider ('' = none) |
| 16 | + */ |
| 17 | + |
| 18 | +const isZeroPrice = value => value === undefined || value === null || Number(value) === 0; |
| 19 | + |
| 20 | +/** |
| 21 | + * Turn one entry of a `/models` response into a TorchyModel, or null if it is not a chat model. |
| 22 | + * @param {import('./providers.js').Provider} provider |
| 23 | + * @param {object} raw |
| 24 | + * @param {string} group |
| 25 | + * @returns {TorchyModel|null} |
| 26 | + */ |
| 27 | +export function normalizeModel (provider, raw, group = '') { |
| 28 | + if (!raw || typeof raw.id !== 'string') return null; |
| 29 | + // Gemini's OpenAI layer lists ids as "models/gemini-…"; chat accepts the bare id. |
| 30 | + const modelId = raw.id.trim().replace(/^models\//, ''); |
| 31 | + if (!modelId) return null; |
| 32 | + if (provider.id === 'gemini') { |
| 33 | + if (!/gemini/i.test(modelId) || GEMINI_NON_CHAT.test(modelId)) return null; |
| 34 | + } |
| 35 | + |
| 36 | + let free = null; |
| 37 | + if (raw.pricing && typeof raw.pricing === 'object') { |
| 38 | + free = isZeroPrice(raw.pricing.prompt) && isZeroPrice(raw.pricing.completion); |
| 39 | + } |
| 40 | + |
| 41 | + const name = typeof raw.name === 'string' && raw.name.trim() && raw.name !== raw.id ? raw.name.trim() : modelId; |
| 42 | + return {key: `${provider.id}:${modelId}`, providerId: provider.id, modelId, name, free, group}; |
| 43 | +} |
| 44 | + |
| 45 | +function withTimeout (promise, ms, message) { |
| 46 | + let timer; |
| 47 | + const timeout = new Promise((_, reject) => { |
| 48 | + timer = setTimeout(() => reject(new Error(message)), ms); |
| 49 | + }); |
| 50 | + return Promise.race([promise, timeout]).finally(() => clearTimeout(timer)); |
| 51 | +} |
| 52 | + |
| 53 | +async function describeFailure (response) { |
| 54 | + let detail = ''; |
| 55 | + try { |
| 56 | + const text = await response.text(); |
| 57 | + try { |
| 58 | + const parsed = JSON.parse(text); |
| 59 | + const value = parsed && (parsed.error || parsed.message); |
| 60 | + detail = typeof value === 'string' ? value : value && value.message ? value.message : ''; |
| 61 | + } catch (e) { |
| 62 | + detail = text; |
| 63 | + } |
| 64 | + } catch (e) { |
| 65 | + detail = ''; |
| 66 | + } |
| 67 | + detail = String(detail || '').trim().slice(0, 200); |
| 68 | + if (response.status === 401 || response.status === 403 || /api key/i.test(detail)) return 'the API key was rejected'; |
| 69 | + return `HTTP ${response.status}${detail ? `: ${detail}` : ''}`; |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * Fetch the models of one provider. Never throws: a failure comes back as `error`. |
| 74 | + * @param {import('./providers.js').Provider} provider |
| 75 | + * @param {typeof fetch} [fetchImpl] |
| 76 | + * @returns {Promise<{models: TorchyModel[], error: string|null}>} |
| 77 | + */ |
| 78 | +export async function fetchProviderModels (provider, fetchImpl = fetch) { |
| 79 | + const models = []; |
| 80 | + const seen = new Set(); |
| 81 | + let error = null; |
| 82 | + |
| 83 | + for (const query of provider.modelQueries) { |
| 84 | + try { |
| 85 | + const response = await withTimeout( |
| 86 | + fetchImpl(`${provider.baseUrl}${query.path}`, {method: 'GET', headers: requestHeaders(provider)}), |
| 87 | + MODELS_TIMEOUT_MS, |
| 88 | + 'the server did not respond in time' |
| 89 | + ); |
| 90 | + if (!response.ok) throw new Error(await describeFailure(response)); |
| 91 | + const body = await response.json(); |
| 92 | + const list = Array.isArray(body) ? body : Array.isArray(body.data) ? body.data : Array.isArray(body.models) ? body.models : []; |
| 93 | + for (const raw of list) { |
| 94 | + const model = normalizeModel(provider, raw, query.group); |
| 95 | + if (!model || seen.has(model.key)) continue; |
| 96 | + seen.add(model.key); |
| 97 | + models.push(model); |
| 98 | + } |
| 99 | + } catch (e) { |
| 100 | + const message = e && e.message ? e.message : String(e); |
| 101 | + error = /Failed to fetch|NetworkError|Load failed/i.test(message) ? |
| 102 | + 'could not reach the server (is it running, and does it allow browser requests?)' : |
| 103 | + message; |
| 104 | + break; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + if (!error && models.length === 0) error = 'the server returned no chat models'; |
| 109 | + return {models, error}; |
| 110 | +} |
| 111 | + |
| 112 | +/** |
| 113 | + * Fetch the models of every active provider in parallel. |
| 114 | + * @param {import('./providers.js').Provider[]} providers |
| 115 | + * @param {typeof fetch} [fetchImpl] |
| 116 | + * @returns {Promise<{models: TorchyModel[], errors: Object<string, string>}>} |
| 117 | + */ |
| 118 | +export async function fetchAllModels (providers, fetchImpl = fetch) { |
| 119 | + const results = await Promise.all(providers.map(provider => fetchProviderModels(provider, fetchImpl))); |
| 120 | + const models = []; |
| 121 | + const errors = {}; |
| 122 | + results.forEach((result, index) => { |
| 123 | + models.push(...result.models); |
| 124 | + if (result.error) errors[providers[index].id] = result.error; |
| 125 | + }); |
| 126 | + return {models, errors}; |
| 127 | +} |
| 128 | + |
| 129 | +/** |
| 130 | + * The model to preselect when a session has none: a Gemini flash model when available (fast, the |
| 131 | + * most tested for Torchy's block syntax), otherwise the first model offered. |
| 132 | + * @param {TorchyModel[]} models |
| 133 | + * @returns {TorchyModel|null} |
| 134 | + */ |
| 135 | +export function pickDefaultModel (models) { |
| 136 | + if (!models.length) return null; |
| 137 | + const flash = models.find(m => |
| 138 | + m.providerId === 'gemini' && /flash/i.test(m.modelId) && !/lite|preview|exp|8b|thinking/i.test(m.modelId) |
| 139 | + ); |
| 140 | + return flash || models.find(m => m.providerId === 'gemini' && /flash/i.test(m.modelId)) || models[0]; |
| 141 | +} |
0 commit comments