Fallback model picker to available model

This commit is contained in:
pewdiepie-archdaemon
2026-06-30 11:56:36 +00:00
parent baecea2681
commit 0cdbdf4186
3 changed files with 37 additions and 14 deletions
+3 -3
View File
@@ -219,8 +219,8 @@
}, { once: true }); }, { once: true });
})(); })();
</script> </script>
<link rel="stylesheet" href="/static/style.css?v=20260630emailrace"> <link rel="stylesheet" href="/static/style.css?v=20260630modelfallback">
<link rel="modulepreload" href="/static/app.js?v=20260630emailrace"> <link rel="modulepreload" href="/static/app.js?v=20260630modelfallback">
<link rel="modulepreload" href="/static/js/chat.js"> <link rel="modulepreload" href="/static/js/chat.js">
<link rel="modulepreload" href="/static/js/ui.js"> <link rel="modulepreload" href="/static/js/ui.js">
<link rel="modulepreload" href="/static/js/sessions.js"> <link rel="modulepreload" href="/static/js/sessions.js">
@@ -2512,7 +2512,7 @@
<script type="module" src="/static/js/settings.js"></script> <script type="module" src="/static/js/settings.js"></script>
<script type="module" src="/static/js/admin.js"></script> <script type="module" src="/static/js/admin.js"></script>
<script type="module" src="/static/js/assistant.js"></script> <script type="module" src="/static/js/assistant.js"></script>
<script type="module" src="/static/app.js?v=20260630emailrace"></script> <!-- app.js must be LAST --> <script type="module" src="/static/app.js?v=20260630modelfallback"></script> <!-- app.js must be LAST -->
<script type="module" src="/static/js/init.js"></script> <script type="module" src="/static/js/init.js"></script>
<script type="module" src="/static/js/a11y.js"></script> <script type="module" src="/static/js/a11y.js"></script>
<script nonce="{{CSP_NONCE}}">if('serviceWorker' in navigator){navigator.serviceWorker.register('/static/sw.js').catch(()=>{});}</script> <script nonce="{{CSP_NONCE}}">if('serviceWorker' in navigator){navigator.serviceWorker.register('/static/sw.js').catch(()=>{});}</script>
+32 -9
View File
@@ -92,6 +92,31 @@ function _modelExists(modelId, url) {
}); });
} }
function _firstAvailableModel() {
if (!window.modelsModule || !window.modelsModule.getCachedItems) return null;
const items = window.modelsModule.getCachedItems() || [];
for (const item of items) {
if (item.offline) continue;
const models = (item.models || []).concat(item.models_extra || []);
if (!models.length) continue;
return {
url: item.url,
modelId: models[0],
endpointId: item.endpoint_id || '',
};
}
return null;
}
async function _ensureModelCacheForFallback() {
if (!window.modelsModule || !window.modelsModule.getCachedItems) return;
const items = window.modelsModule.getCachedItems() || [];
if (items.length) return;
if (typeof window.modelsModule.refreshModels === 'function') {
try { await window.modelsModule.refreshModels(false); } catch (_) {}
}
}
async function _ensureDefaultPendingChat() { async function _ensureDefaultPendingChat() {
if (!_deps || _defaultChatPickInFlight) return; if (!_deps || _defaultChatPickInFlight) return;
if (_deps.getCurrentSessionId && _deps.getCurrentSessionId()) return; if (_deps.getCurrentSessionId && _deps.getCurrentSessionId()) return;
@@ -99,12 +124,13 @@ async function _ensureDefaultPendingChat() {
if (pending && pending.modelId) return; if (pending && pending.modelId) return;
_defaultChatPickInFlight = true; _defaultChatPickInFlight = true;
try { try {
await _ensureModelCacheForFallback();
let dc = null; let dc = null;
try { try {
const res = await fetch(`${API_BASE}/api/default-chat`, { credentials: 'same-origin' }); const res = await fetch(`${API_BASE}/api/default-chat`, { credentials: 'same-origin' });
if (res.ok) dc = await res.json(); if (res.ok) dc = await res.json();
} catch (_) {} } catch (_) {}
if (dc && dc.endpoint_url && dc.model) { if (dc && dc.endpoint_url && dc.model && _modelExists(dc.model, dc.endpoint_url)) {
_deps.setPendingChat({ _deps.setPendingChat({
url: dc.endpoint_url, url: dc.endpoint_url,
modelId: dc.model, modelId: dc.model,
@@ -114,16 +140,13 @@ async function _ensureDefaultPendingChat() {
updateModelPicker(); updateModelPicker();
return; return;
} }
// No configured default: preserve the old convenience fallback. // No configured default, or the configured default is gone/offline:
if (window.modelsModule && window.modelsModule.getCachedItems) { // preserve the convenience fallback and keep the picker usable.
const items = window.modelsModule.getCachedItems(); const fallback = _firstAvailableModel();
const first = items.find(item => !item.offline && ((item.models || []).length || (item.models_extra || []).length)); if (fallback) {
if (first) { _deps.setPendingChat(fallback);
const models = (first.models || []).concat(first.models_extra || []);
_deps.setPendingChat({ url: first.url, modelId: models[0], endpointId: first.endpoint_id });
updateModelPicker(); updateModelPicker();
} }
}
} finally { } finally {
_defaultChatPickInFlight = false; _defaultChatPickInFlight = false;
} }
+1 -1
View File
@@ -7,7 +7,7 @@
// - Other static assets (images/fonts/libs): cache-first with bg refresh. // - Other static assets (images/fonts/libs): cache-first with bg refresh.
// - API / non-GET: never cached. // - API / non-GET: never cached.
// Bump CACHE_NAME whenever the precache list or SW logic changes. // Bump CACHE_NAME whenever the precache list or SW logic changes.
const CACHE_NAME = 'odysseus-v333'; const CACHE_NAME = 'odysseus-v334';
// Core shell precached on install so repeat opens are instant without any // Core shell precached on install so repeat opens are instant without any
// network wait. Keep this list in sync with the <script type="module"> tags // network wait. Keep this list in sync with the <script type="module"> tags