|
| 1 | +// model-discovery.mjs |
| 2 | +// Discover real model names from /api/sessions when /v1/models only returns |
| 3 | +// the synthetic `hermes-agent` alias. Used as a fallback for multi-provider |
| 4 | +// setups where the gateway's OpenAI-compat /v1/models doesn't reflect the |
| 5 | +// real model registry. |
| 6 | +// |
| 7 | +// The actual model used per turn is stored in /api/sessions[*].model, so we |
| 8 | +// can reconstruct a useful model picker from session history. |
| 9 | + |
| 10 | +const SESSION_HISTORY_LIMIT = 100; |
| 11 | + |
| 12 | +const KNOWN_PROVIDER_PREFIXES = [ |
| 13 | + // Order matters: most specific first. |
| 14 | + // Provider names that appear as substrings (e.g. `openai-codex/gpt-5.4`). |
| 15 | + ['openai-codex', 'openai-codex'], |
| 16 | + ['openai', 'openai'], |
| 17 | + ['minimax', 'minimax'], |
| 18 | + ['kimi', 'moonshot'], |
| 19 | + ['moonshot', 'moonshot'], |
| 20 | + ['qwen', 'alibaba'], |
| 21 | + ['glm', 'zhipu'], |
| 22 | + ['deepseek', 'deepseek'], |
| 23 | + ['grok', 'xai'], |
| 24 | + ['gemini', 'google'], |
| 25 | + ['claude', 'anthropic'], |
| 26 | +]; |
| 27 | + |
| 28 | +// Brand prefixes that don't contain the provider name as a substring. |
| 29 | +const KNOWN_PROVIDER_BRANDS = [ |
| 30 | + ['gpt-', 'openai'], |
| 31 | + ['gpt5', 'openai'], |
| 32 | + ['gpt4', 'openai'], |
| 33 | + ['o1', 'openai'], |
| 34 | + ['o3', 'openai'], |
| 35 | + ['o4', 'openai'], |
| 36 | +]; |
| 37 | + |
| 38 | +export function deriveProviderFromModelId(modelId = '') { |
| 39 | + const lower = String(modelId || '').toLowerCase(); |
| 40 | + if (!lower) return ''; |
| 41 | + for (const [prefix, provider] of KNOWN_PROVIDER_PREFIXES) { |
| 42 | + if (lower.startsWith(prefix) || lower.includes(`/${prefix}`) || lower.includes(`-${prefix}`)) { |
| 43 | + return provider; |
| 44 | + } |
| 45 | + } |
| 46 | + for (const [brand, provider] of KNOWN_PROVIDER_BRANDS) { |
| 47 | + if (lower.startsWith(brand) || lower.includes(`-${brand}`) || lower.includes(`/${brand}`)) { |
| 48 | + return provider; |
| 49 | + } |
| 50 | + } |
| 51 | + return ''; |
| 52 | +} |
| 53 | + |
| 54 | +export async function discoverModelsFromSessions({ |
| 55 | + apiFetch, |
| 56 | + readJsonResponse, |
| 57 | + limit = SESSION_HISTORY_LIMIT, |
| 58 | +} = {}) { |
| 59 | + if (typeof apiFetch !== 'function' || typeof readJsonResponse !== 'function') { |
| 60 | + return { ok: false, error: 'no-fetch', models: [] }; |
| 61 | + } |
| 62 | + try { |
| 63 | + const response = await apiFetch(`/api/sessions?limit=${encodeURIComponent(limit)}`, { method: 'GET' }); |
| 64 | + const payload = await readJsonResponse(response); |
| 65 | + if (!response.ok) { |
| 66 | + return { |
| 67 | + ok: false, |
| 68 | + error: payload?.error?.message || `status-${response.status}`, |
| 69 | + models: [], |
| 70 | + }; |
| 71 | + } |
| 72 | + const rows = Array.isArray(payload?.data) ? payload.data : []; |
| 73 | + const buckets = new Map(); |
| 74 | + for (const row of rows) { |
| 75 | + const modelId = String(row?.model || '').trim(); |
| 76 | + if (!modelId || modelId === 'hermes-agent') continue; |
| 77 | + const lastSeen = Number(row?.last_active || row?.started_at || 0); |
| 78 | + const bucket = buckets.get(modelId) || { |
| 79 | + id: modelId, |
| 80 | + label: modelId, |
| 81 | + provider: deriveProviderFromModelId(modelId), |
| 82 | + contextTokens: 0, |
| 83 | + lastSeen: 0, |
| 84 | + sessionCount: 0, |
| 85 | + inputTokens: 0, |
| 86 | + outputTokens: 0, |
| 87 | + }; |
| 88 | + bucket.sessionCount += 1; |
| 89 | + bucket.lastSeen = Math.max(bucket.lastSeen, lastSeen); |
| 90 | + bucket.inputTokens += Number(row?.input_tokens || 0); |
| 91 | + bucket.outputTokens += Number(row?.output_tokens || 0); |
| 92 | + buckets.set(modelId, bucket); |
| 93 | + } |
| 94 | + const models = Array.from(buckets.values()) |
| 95 | + .map((bucket) => ({ |
| 96 | + id: bucket.id, |
| 97 | + label: bucket.label, |
| 98 | + provider: bucket.provider, |
| 99 | + providerLabel: bucket.provider, |
| 100 | + description: `${bucket.sessionCount} session${bucket.sessionCount === 1 ? '' : 's'} · ${(bucket.inputTokens + bucket.outputTokens).toLocaleString()} tokens`, |
| 101 | + contextTokens: 0, |
| 102 | + source: 'sessions', |
| 103 | + lastSeen: bucket.lastSeen, |
| 104 | + sessionCount: bucket.sessionCount, |
| 105 | + })) |
| 106 | + .sort((a, b) => b.lastSeen - a.lastSeen); |
| 107 | + return { ok: true, models, error: '' }; |
| 108 | + } catch (error) { |
| 109 | + return { ok: false, error: error?.message || 'error', models: [] }; |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +export function mergeModelsWithRegistry({ registryModels = [], sessionModels = [] } = {}) { |
| 114 | + const out = []; |
| 115 | + const seen = new Set(); |
| 116 | + // Registry first (these are the gateway-blessed models) |
| 117 | + for (const model of registryModels) { |
| 118 | + if (!model || !model.id || seen.has(model.id)) continue; |
| 119 | + seen.add(model.id); |
| 120 | + out.push({ ...model, source: model.source || 'registry' }); |
| 121 | + } |
| 122 | + // Then session-discovered models, marking them as such |
| 123 | + for (const model of sessionModels) { |
| 124 | + if (!model || !model.id || seen.has(model.id)) continue; |
| 125 | + seen.add(model.id); |
| 126 | + out.push({ ...model, source: 'sessions' }); |
| 127 | + } |
| 128 | + return out; |
| 129 | +} |
| 130 | + |
| 131 | +export const MODEL_DISCOVERY_DEFAULTS = Object.freeze({ |
| 132 | + limit: SESSION_HISTORY_LIMIT, |
| 133 | +}); |
0 commit comments