Skip to content

Self-hosted AI workspace.

Python
46.8 KB
Raw Blame
// Model Picker — chatbox model selector dropdown
// Extracted from sessions.js

import { providerLogo, providerLabel } from './providers.js';
import uiModule from './ui.js?v=20260916largetoolscroll1';
import settingsModule from './settings.js?v=20260912writingstyle3';
import { sortModelObjects } from './modelSort.js';
import spinnerModule from './spinner.js';

const API_BASE = window.location.origin;

// ── Recent + Favorites persistence ──
// Recent is auto-tracked (last 5 picks, most-recent-first) and lives in its
// own key. Favorites is the SAME key the sidebar Models section uses, so a
// favorite toggled here shows up there and vice-versa.
const RECENT_KEY = 'odysseus-model-recent';
const FAVORITES_KEY = 'odysseus-model-favorites';
const RECENT_MAX = 5;
// Catalogs at or below this size are small enough that hiding everything
// behind search would be a regression — keep listing them in browse mode.
const BROWSE_ALL_LIMIT = 12;

function _loadList(key) {
  try {
    const a = JSON.parse(localStorage.getItem(key) || '[]');
    return Array.isArray(a) ? a : [];
  } catch { return []; }
}
function _saveList(key, list) {
  try { localStorage.setItem(key, JSON.stringify(list)); } catch { /* quota / private mode */ }
}
function _loadRecent() { return _loadList(RECENT_KEY); }
function _pushRecent(mid) {
  if (!mid) return;
  const next = _loadRecent().filter(x => x !== mid);
  next.unshift(mid);
  _saveList(RECENT_KEY, next.slice(0, RECENT_MAX));
}
function _loadFavorites() { return _loadList(FAVORITES_KEY); }
function _toggleFavorite(mid) {
  const favs = _loadFavorites();
  const i = favs.indexOf(mid);
  if (i >= 0) favs.splice(i, 1);
  else favs.push(mid);
  _saveList(FAVORITES_KEY, favs);
  // Keep the sidebar Models section (same key) in sync if it's mounted.
  try {
    if (window.modelsModule && typeof window.modelsModule.refreshModels === 'function') {
      window.modelsModule.refreshModels();
    }
  } catch { /* sidebar not present */ }
  return i < 0; // true when now favorited
}

function _pickerModelKey(m) {
  if (!m) return '';
  return `${m.endpointId || m.url || m.epName || 'model'}::${m.mid || ''}`;
}

// ── Shared keyboard nav for model pickers ──
function _handlePickerKeydown(e, listEl, itemSelector, closeFn) {
  if (e.key === 'Escape') { closeFn(); return; }
  if (e.key === 'Enter') {
    e.preventDefault();
    const active = listEl.querySelector(itemSelector + '.kb-active') || listEl.querySelector(itemSelector);
    if (active) active.click();
    return;
  }
  if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {
    e.preventDefault();
    const items = [...listEl.querySelectorAll(itemSelector)].filter(el => el.style.display !== 'none');
    if (!items.length) return;
    const cur = items.findIndex(el => el.classList.contains('kb-active'));
    items.forEach(el => el.classList.remove('kb-active'));
    let next;
    if (e.key === 'ArrowDown') next = cur < items.length - 1 ? cur + 1 : 0;
    else next = cur > 0 ? cur - 1 : items.length - 1;
    items[next].classList.add('kb-active');
    items[next].scrollIntoView({ block: 'nearest' });
  }
}

// Dependencies injected via initModelPicker()
let _deps = null;
let _autoSelectingDefault = false;
let _defaultChatPickInFlight = false;
let _defaultPendingSeq = 0;

function _modelExists(modelId, url) {
  if (!modelId || !window.modelsModule || !window.modelsModule.getCachedItems) return false;
  const items = window.modelsModule.getCachedItems() || [];
  if (!items.length) return true;
  const targetUrl = (url || '').replace(/\/+$/, '');
  return items.some(item => {
    if (item.offline) return false;
    const itemUrl = (item.url || '').replace(/\/+$/, '');
    const models = (item.models || []).concat(item.models_extra || []);
    return models.includes(modelId) && (!targetUrl || itemUrl === targetUrl);
  });
}

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() {
  if (!_deps || _defaultChatPickInFlight) return;
  if (_deps.getCurrentSessionId && _deps.getCurrentSessionId()) return;
  const pending = _deps.getPendingChat && _deps.getPendingChat();
  if (pending && pending.modelId) return;
  _defaultChatPickInFlight = true;
  const seq = ++_defaultPendingSeq;
  try {
    let dc = null;
    try {
      dc = window.__odysseusDefaultChat || null;
    } catch (_) {}
    if (!dc || !dc.endpoint_url || !dc.model) {
      try {
        const res = await fetch(`${API_BASE}/api/default-chat`, { credentials: 'same-origin' });
        if (res.ok) dc = await res.json();
      } catch (_) {}
    }
    if (dc && dc.endpoint_url && dc.model) {
      if (seq !== _defaultPendingSeq) return;
      const latest = _deps.getPendingChat && _deps.getPendingChat();
      if (latest && latest.modelId && latest.source !== 'default' && latest.source !== 'fallback') return;
      try {
        window.__odysseusDefaultChat = dc;
        localStorage.setItem('odysseus-default-chat-cache', JSON.stringify(dc));
      } catch (_) {}
      const pendingUrl = String((latest && latest.url) || '').replace(/\/+$/, '');
      const defaultUrl = String(dc.endpoint_url || '').replace(/\/+$/, '');
      _deps.setPendingChat({
        url: dc.endpoint_url,
        modelId: dc.model,
        endpointId: dc.endpoint_id || '',
        source: 'default',
      });
      if (!latest || latest.modelId !== dc.model || pendingUrl !== defaultUrl || latest.source !== 'default') {
        updateModelPicker();
      }
      return;
    }
    if (pending && pending.modelId) return;
    await _ensureModelCacheForFallback();
    // No configured default, or the configured default is gone/offline:
    // preserve the convenience fallback and keep the picker usable.
    const fallback = _firstAvailableModel();
    if (fallback) {
      if (seq !== _defaultPendingSeq) return;
      const latest = _deps.getPendingChat && _deps.getPendingChat();
      if (latest && latest.modelId && latest.source !== 'default' && latest.source !== 'fallback') return;
      _deps.setPendingChat({ ...fallback, source: 'fallback' });
      updateModelPicker();
    }
  } finally {
    _defaultChatPickInFlight = false;
  }
}

/**
 * Initialize the model picker dropdown.
 * @param {Object} deps
 * @param {function} deps.getCurrentSessionId - returns current session ID
 * @param {function} deps.getSessions - returns sessions array
 * @param {function} deps.getPendingChat - returns _pendingChat object
 * @param {function} deps.setPendingChat - sets _pendingChat object
 * @param {function} deps.createDirectChat - creates a new direct chat session
 */
export function initModelPicker(deps) {
  _deps = deps;
  _initModelPickerDropdown();
}

function _initModelPickerDropdown() {
  const wrap = document.getElementById('model-picker-wrap');
  const btn = document.getElementById('model-picker-btn');
  const menu = document.getElementById('model-picker-menu');
  const search = document.getElementById('model-picker-search');
  const listEl = document.getElementById('model-picker-list');
  const searchRow = menu ? menu.querySelector('.model-picker-search-row') : null;
  const refreshBtn = document.getElementById('model-picker-refresh-btn');
  _initReasoningEffort();
  if (!wrap || !btn || !menu || !search || !listEl) return;
  if (wrap.dataset.modelPickerBound === '1') return;
  wrap.dataset.modelPickerBound = '1';

  function _close() {
    if (menu.classList.contains('hidden')) return;
    // Restore scroll button
    const _scrollBtn = document.getElementById('scroll-bottom-btn');
    if (_scrollBtn) _scrollBtn.style.display = '';
    menu.classList.add('closing');
    menu.addEventListener('animationend', function _onDone() {
      menu.removeEventListener('animationend', _onDone);
      menu.classList.remove('closing');
      menu.classList.add('hidden');
      search.value = '';
    }, { once: true });
    // Fallback if animationend doesn't fire
    setTimeout(() => {
      if (!menu.classList.contains('hidden')) {
        menu.classList.remove('closing');
        menu.classList.add('hidden');
        search.value = '';
      }
    }, 200);
  }

  function _openPickerShortcut(kind) {
    _close();
    try {
      if (kind === 'cookbook') {
        if (window.cookbookModule && typeof window.cookbookModule.open === 'function') {
          window.cookbookModule.open();
        } else {
          const btn = document.getElementById('tool-cookbook-btn') || document.getElementById('rail-cookbook');
          if (btn) btn.click();
          else location.hash = '#cookbook';
        }
      } else if (kind === 'settings') {
        if (settingsModule && typeof settingsModule.open === 'function') settingsModule.open();
      } else if (window.adminModule && typeof window.adminModule.open === 'function') {
        window.adminModule.open('services');
      } else if (settingsModule && typeof settingsModule.open === 'function') {
        settingsModule.open('services');
      }
    } catch (_) {}
  }

  // Local endpoint health — only probed for LOCAL endpoints, since
  // cloud APIs are essentially always up. Cached briefly on the
  // server side too (8s TTL).
  let _localProbe = {};            // {endpoint_id: {alive, latency_ms, error}}
  let _localProbeFetchedAt = 0;
  const _LOCAL_PROBE_TTL_MS = 5000;
  let _pickerLoading = false;
  let _pickerLoadSeq = 0;

  async function _refreshLocalProbe() {
    try {
      if (window.__odysseusChatBusy || Date.now() < (window.__odysseusChatBusyUntil || 0)) return;
    } catch (_) {}
    const now = Date.now();
    if (now - _localProbeFetchedAt < _LOCAL_PROBE_TTL_MS) return;
    _localProbeFetchedAt = now;
    try {
      const r = await fetch('/api/model-endpoints/probe-local', { credentials: 'same-origin' });
      if (r.ok) _localProbe = (await r.json()) || {};
    } catch (_) { /* leave stale data; picker still works */ }
  }

  function _getAllModels() {
    const items = (window.modelsModule && window.modelsModule.getCachedItems) ? window.modelsModule.getCachedItems() : [];
    const result = [];
    const seen = new Set();
    items.forEach(item => {
      // Previously: offline endpoints were skipped entirely, so a server
      // that briefly went down disappeared from the picker — confusing
      // when the user can still see it (offline-tagged) in Settings.
      // Now: include offline-endpoint models too but flag them
      // `stale: true` so the row renderer dims them + shows the offline
      // pill. The user can still click and try anyway (matches the
      // existing "local server appears offline" path on line 301).
      const epOffline = !!item.offline;
      const allModels = (item.models || []).concat(item.models_extra || []);
      const allDisplay = (item.models_display || []).concat(item.models_extra_display || []);
      // Mark local endpoints whose live probe failed.
      const probeResult = item.endpoint_id ? _localProbe[item.endpoint_id] : null;
      const isLocalDead = !!(probeResult && probeResult.alive === false);
      allModels.forEach((mid, i) => {
        // A registered route is a user choice, including local routes using
        // identical weights with different harness profiles. Never collapse
        // distinct endpoints just because their model IDs match.
        const isApiEndpoint = item.category && item.category !== 'local';
        const seenKey = isApiEndpoint
          ? `${item.endpoint_id || item.url || item.endpoint_name || 'api'}::${mid}`
          : _pickerModelKey({ endpointId: item.endpoint_id, url: item.url, epName: item.endpoint_name, mid }); // const seenKey = _pickerModelKey(
        if (seen.has(seenKey)) return;
        seen.add(seenKey);
        result.push({
          key: seenKey,
          mid,
          display: (allDisplay[i] || mid).split('/').pop(),
          url: item.url,
          endpointId: item.endpoint_id,
          epName: item.endpoint_name || '',
          category: item.category || '',
          modelsMetadata: item.models_metadata || {},
          providerText: [
            item.endpoint_name || '',
            item.category || '',
            item.host || '',
            item.url || '',
          ].filter(Boolean).join(' '),
          stale: isLocalDead || epOffline,
          staleReason: epOffline
            ? (item.ping_error || 'endpoint offline')
            : (isLocalDead ? (probeResult.error || 'not responding') : ''),
          offline: epOffline,
        });
      });
    });
    return sortModelObjects(result);
  }

  function _hasModelCache() {
    try {
      return !!(window.modelsModule && window.modelsModule.getCachedItems && (window.modelsModule.getCachedItems() || []).length);
    } catch (_) {
      return false;
    }
  }

  function _renderLoading(text = 'Loading models…') {
    listEl.innerHTML = '';
    listEl.classList.remove('is-empty');
    listEl.classList.add('is-loading');
    menu.classList.remove('no-models');
    if (search) search.placeholder = text;
    let row = null;
    try {
      row = spinnerModule.createLoadingRow(text, 15);
    } catch (_) {
      row = document.createElement('div');
      row.className = 'model-switch-empty';
      row.textContent = text;
    }
    row.classList.add('model-picker-loading-row');
    listEl.appendChild(row);
  }

  async function _refreshPickerModels({ force = false, showLoading = false } = {}) {
    if (!window.modelsModule || typeof window.modelsModule.refreshModels !== 'function') return false;
    const seq = ++_pickerLoadSeq;
    _pickerLoading = true;
    if (showLoading) _renderLoading(force ? 'Refreshing models…' : 'Loading models…');
    try {
      await window.modelsModule.refreshModels(force, { waitForRefresh: force });
      await _refreshLocalProbe();
    } finally {
      if (seq === _pickerLoadSeq) {
        _pickerLoading = false;
        listEl.classList.remove('is-loading');
      }
    }
    return seq === _pickerLoadSeq;
  }

  // ── Provider display names and grouping ──
  const _PROVIDER_NAMES = {
    '01-ai': 'Yi', 'abacusai': 'Abacus AI', 'adept': 'Adept',
    'ai21': 'AI21 Labs', 'ai21labs': 'AI21 Labs', 'aion-labs': 'Aion Labs',
    'aisingapore': 'AI Singapore', 'allenai': 'Allen AI', 'amazon': 'Amazon',
    'anthracite-org': 'Anthracite', 'anthropic': 'Anthropic', 'arcee-ai': 'Arcee AI',
    'baai': 'BAAI', 'baidu': 'Baidu', 'bigcode': 'BigCode',
    'black-forest-labs': 'Black Forest Labs', 'bytedance': 'ByteDance',
    'bytedance-seed': 'ByteDance', 'cognitivecomputations': 'Cognitive Computations',
    'cohere': 'Cohere', 'databricks': 'Databricks', 'deepcogito': 'DeepCogito',
    'deepseek': 'DeepSeek', 'deepseek-ai': 'DeepSeek', 'essentialai': 'Essential AI',
    'google': 'Google', 'gryphe': 'Gryphe', 'ibm': 'IBM',
    'ibm-granite': 'IBM Granite', 'inception': 'Inception',
    'inclusionai': 'Inclusion AI', 'inflection': 'Inflection',
    'kwaipilot': 'KwaiPilot', 'liquid': 'Liquid AI', 'mancer': 'Mancer',
    'meta': 'Llama', 'meta-llama': 'Llama', 'microsoft': 'Microsoft',
    'minimax': 'MiniMax', 'minimaxai': 'MiniMax', 'mistralai': 'Mistral',
    'moonshotai': 'Moonshot', 'morph': 'Morph', 'nex-agi': 'Nex AGI',
    'nousresearch': 'Nous Research', 'nv-mistralai': 'NVIDIA x Mistral',
    'nvidia': 'NVIDIA', 'openai': 'OpenAI', 'openrouter': 'OpenRouter',
    'perceptron': 'Perceptron', 'perplexity': 'Perplexity', 'poolside': 'Poolside',
    'prime-intellect': 'Prime Intellect', 'qwen': 'Qwen', 'rekaai': 'Reka',
    'relace': 'Relace', 'sao10k': 'Sao10k', 'sarvamai': 'Sarvam AI',
    'snowflake': 'Snowflake', 'stepfun': 'StepFun', 'stepfun-ai': 'StepFun',
    'stockmark': 'Stockmark', 'switchpoint': 'SwitchPoint', 'tencent': 'Tencent',
    'thedrummer': 'TheDrummer', 'undi95': 'Undi95', 'upstage': 'Upstage',
    'writer': 'Writer', 'x-ai': 'xAI', 'xiaomi': 'Xiaomi',
    'z-ai': 'Zhipu', 'zyphra': 'Zyphra',
    '~anthropic': 'Anthropic', '~google': 'Google',
    '~moonshotai': 'Moonshot', '~openai': 'OpenAI',
  };
  const _PROVIDER_ALIAS = {
    'meta-llama': 'meta', 'deepseek': 'deepseek-ai', 'minimaxai': 'minimax',
    'stepfun-ai': 'stepfun', 'ai21labs': 'ai21', 'ibm-granite': 'ibm',
    'bytedance-seed': 'bytedance', '~anthropic': 'anthropic',
    '~google': 'google', '~moonshotai': 'moonshotai', '~openai': 'openai',
  };
  const _endpointGroupNames = new Map();
  function _providerGroupKey(m) {
    if (!m) return 'other';
    // Grouping must be keyed on endpoint_id, falling back to url or epName
    const gid = m.endpointId || m.url || (m.category && m.category !== 'local' && m.epName ? m.epName : '');
    if (gid) {
      const gname = m.epName || (m.endpointId ? m.endpointId : (m.url || 'Other Models'));
      _endpointGroupNames.set(gid, gname);
      return `~endpoint:${gid}`;
    }
    if (m.category && m.category !== 'local' && m.epName) {
      _endpointGroupNames.set(m.epName, m.epName);
      return `~endpoint:${m.epName}`;
    }
    return _providerSlug((m && m.mid) || '');
  }
  function _providerGroupName(provider) {
    if (String(provider || '').startsWith('~endpoint:')) {
      const raw = String(provider).slice('~endpoint:'.length);
      return _endpointGroupNames.get(raw) || raw;
    }
    return _providerDisplayName(provider);
  }
  function _providerSlug(mid) {
    const slash = mid.indexOf('/');
    let slug = slash > 0 ? mid.substring(0, slash) : 'other';
    return _PROVIDER_ALIAS[slug] || slug;
  }
  const _collapsedProviders = new Set(_loadList('odysseus-model-collapsed'));
  let _justExpandedProvider = null;

  function _populate(filter) {
    listEl.innerHTML = '';
    listEl.classList.remove('is-loading');
    const all = _getAllModels();
    const q = (filter || '').trim().toLowerCase();
    const hasAnyModel = all.length > 0;
    listEl.classList.toggle('is-empty', !hasAnyModel);
    menu.classList.toggle('no-models', !hasAnyModel);
    if (search) {
      search.placeholder = hasAnyModel ? 'Search models…' : 'No models connected';
    }
    if (searchRow) {
      searchRow.classList.toggle('searching', !!q);
    }

    if (!hasAnyModel) return; // collapsed empty list — nothing to render

    // Unique lookup so Recent/Favorites (stored as bare model IDs) can be
    // resolved back to full model objects; drops anything no longer offered.
    const byId = new Map();
    const byKey = new Map();
    all.forEach(m => {
      const key = _pickerModelKey(m);
      if (key && !byKey.has(key)) byKey.set(key, m);
      if (!byId.has(m.mid)) byId.set(m.mid, m);
    });

    const favs = _loadFavorites();

    function _addSection(label) {
      const el = document.createElement('div');
      el.className = 'mp-section-label';
      el.textContent = label;
      listEl.appendChild(el);
    }
    function _addEmpty(text) {
      const empty = document.createElement('div');
      empty.className = 'model-switch-empty';
      empty.textContent = text;
      listEl.appendChild(empty);
    }
    function _addRow(m, { inGroup = false } = {}) {
      const row = document.createElement('div');
      row.className = 'model-switch-item';
      if (m.stale) {
        row.classList.add('model-switch-stale');
        row.style.opacity = '0.45';
        row.title = `Local server appears offline: ${m.staleReason}. Click to try anyway, or relaunch in Cookbook.`;
      }
      const _mlogo = providerLogo(m.mid);
      if (_mlogo) {
        const logoSpan = document.createElement('span');
        logoSpan.className = 'provider-logo';
        logoSpan.style.opacity = '0.6';
        logoSpan.innerHTML = _mlogo;
        row.appendChild(logoSpan);
      }
      const nameSpan = document.createElement('span');
      nameSpan.className = 'mp-model-name';
      nameSpan.textContent = m.display;
      // Long model names are clipped with ellipsis — expose the full name on
      // hover so the suffix/variant tag is still discoverable (#1982).
      nameSpan.title = m.display;
      row.appendChild(nameSpan);
      // Offline state is already conveyed by the row's reduced opacity —
      // a redundant "offline" pill on top of that just added clutter.
      // (Class kept on `row` so the opacity rule still applies; the text
      // badge is gone.)
      const epSpan = document.createElement('span');
      epSpan.className = 'model-switch-ep';
      // Don't show endpoint name if it matches the model name (local self-hosted)
      // or if it's already rendered under its endpoint group header
      const _epDisplay = (!inGroup && m.epName && !m.display.toLowerCase().includes(m.epName.toLowerCase().split('/').pop())) ? m.epName : '';
      epSpan.textContent = _epDisplay;
      row.appendChild(epSpan);

      // Inline favorite button — toggles favorite, never picks the model.
      const favDot = document.createElement('button');
      favDot.type = 'button';
      favDot.className = 'mp-fav-dot' + (favs.includes(m.mid) ? ' active' : '');
      const _setFavState = (on) => {
        favDot.classList.toggle('active', on);
        favDot.innerHTML = `<svg width="13" height="13" viewBox="0 0 24 24" fill="${on ? 'currentColor' : 'none'}" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M19 21l-7-5-7 5V5a2 2 0 0 1 2-2h10a2 2 0 0 1 2 2z"/></svg>`;
        favDot.title = on ? 'Remove from favorites' : 'Add to favorites';
        favDot.setAttribute('aria-label', on ? 'Remove from favorites' : 'Add to favorites');
        favDot.setAttribute('aria-pressed', on ? 'true' : 'false');
      };
      _setFavState(favs.includes(m.mid));
      favDot.addEventListener('click', (e) => {
        e.stopPropagation();
        const nowFav = _toggleFavorite(m.mid);
        _setFavState(nowFav);
        favDot.classList.remove('pulse');
        void favDot.offsetWidth;
        favDot.classList.add('pulse');
        // Keep our in-memory copy aligned so a follow-up re-render is correct.
        const idx = favs.indexOf(m.mid);
        if (nowFav && idx < 0) favs.push(m.mid);
        else if (!nowFav && idx >= 0) favs.splice(idx, 1);
        if (uiModule && uiModule.showToast) uiModule.showToast(nowFav ? 'Favorited' : 'Unfavorited');
        // In browse mode the Favorites section membership changed — rebuild
        // (cheap: Recent + Favorites). In search mode the row stays put, so
        // the in-place favorite update above is enough.
        if (!q) {
          const st = listEl.scrollTop;
          _populate('');
          listEl.scrollTop = st;
        }
      });
      row.appendChild(favDot);

      row.addEventListener('click', () => _pick(m));
      listEl.appendChild(row);
    }

    function _renderGroup(provider, models, { isSearch = false } = {}) {
      if (!models || !models.length) return;
      const isCollapsed = !isSearch && _collapsedProviders.has(provider);
      const header = document.createElement('div');
      header.className = 'mp-provider-header';
      header.innerHTML =
        `<svg class="mp-provider-chevron${isCollapsed ? ' collapsed' : ''}" width="10" height="10" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="3" stroke-linecap="round" stroke-linejoin="round"><polyline points="6 9 12 15 18 9"/></svg>`;
      const nameSpan = document.createElement('span');
      nameSpan.className = 'mp-provider-name';
      nameSpan.textContent = _providerGroupName(provider);
      header.appendChild(nameSpan);
      const countSpan = document.createElement('span');
      countSpan.className = 'mp-provider-count';
      countSpan.textContent = `${models.length} model${models.length === 1 ? '' : 's'}`;
      header.appendChild(countSpan);

      header.addEventListener('click', (e) => {
        e.stopPropagation();
        if (_collapsedProviders.has(provider)) {
          _collapsedProviders.delete(provider);
          _justExpandedProvider = provider;
        } else {
          _collapsedProviders.add(provider);
          _justExpandedProvider = null;
        }
        _saveList('odysseus-model-collapsed', [..._collapsedProviders]);
        const st = listEl.scrollTop;
        _populate(search ? search.value : '');
        listEl.scrollTop = st;
      });
      listEl.appendChild(header);

      if (!isCollapsed) {
        const group = document.createElement('div');
        group.className = 'mp-provider-group' + (_justExpandedProvider === provider ? ' mp-just-expanded' : '');
        models.forEach(m => {
          _addRow(m, { inGroup: true });
          // Move the just-appended row into the group container
          group.appendChild(listEl.lastElementChild);
        });
        listEl.appendChild(group);
        if (_justExpandedProvider === provider) _justExpandedProvider = null;
      }
    }

    // ── Search mode: grouped, filtered results across the whole catalog ──
    if (q) {
      const matches = all.filter(m => {
        const groupKey = _providerGroupKey(m);
        const groupName = _providerGroupName(groupKey).toLowerCase();
        return [m.mid, m.display, m.epName, m.providerText, groupName]
          .filter(Boolean).join(' ').toLowerCase().includes(q);
      });
      if (matches.length === 0) {
        _addEmpty('No matching models');
      } else {
        const groups = new Map();
        matches.forEach(m => {
          const key = _providerGroupKey(m);
          if (!groups.has(key)) groups.set(key, []);
          groups.get(key).push(m);
        });
        const sorted = [...groups.keys()].sort((a, b) =>
          _providerGroupName(a).localeCompare(_providerGroupName(b)));
        sorted.forEach(provider => {
          _renderGroup(provider, groups.get(provider), { isSearch: true });
        });
      }
      return;
    }

    // ── Browse mode: Favorites (manual) + Recent (auto), with dedupe. ──
    const shown = new Set();
    const favModels = favs.map(id => byKey.get(id) || byId.get(id)).filter(Boolean);
    if (favModels.length) {
      _addSection('Favorites');
      favModels.forEach(m => { shown.add(_pickerModelKey(m)); _addRow(m, { inGroup: false }); });
    }
    // Recent: only render when the catalog is big enough that surfacing
    // a recency shortlist is actually useful, AND only models that
    // aren't already in Favorites (dedupe).
    if (all.length > BROWSE_ALL_LIMIT) {
      const recentModels = _loadRecent()
        .map(id => byKey.get(id) || byId.get(id))
        .filter(Boolean)
        .filter(m => !shown.has(_pickerModelKey(m)))
        .slice(0, RECENT_MAX);
      if (recentModels.length) {
        _addSection('Recent');
        recentModels.forEach(m => { shown.add(_pickerModelKey(m)); _addRow(m, { inGroup: false }); });
      }
    }

    // Provider / endpoint groups with collapsible sections.
    const rest = all.filter(m => !shown.has(_pickerModelKey(m)));
    const groups = new Map();
    rest.forEach(m => {
      const key = _providerGroupKey(m);
      if (!groups.has(key)) groups.set(key, []);
      groups.get(key).push(m);
    });
    const sorted = [...groups.keys()].sort((a, b) =>
      _providerGroupName(a).localeCompare(_providerGroupName(b)));

    sorted.forEach(provider => {
      _renderGroup(provider, groups.get(provider), { isSearch: false });
    });
  }

async function _pick(m) {
    _defaultPendingSeq++;
    try {
      window.__odysseusLastPickedRoute = {
        model: m.mid || '',
        endpoint_url: m.url || '',
        endpoint_id: m.endpointId || '',
        display: m.display || m.mid || '',
        picked_at: Date.now(),
        session_id: _deps.getCurrentSessionId() || null,
      };
    } catch (_) {}
    let switchDone = null;
    const switchPromise = new Promise(resolve => { switchDone = resolve; });
    try { window.__odysseusModelSwitchPromise = switchPromise; } catch (_) {}
    const finishSwitch = () => {
      try {
        if (switchDone) switchDone();
        if (window.__odysseusModelSwitchPromise === switchPromise) delete window.__odysseusModelSwitchPromise;
      } catch (_) {}
    };
    const currentSessionId = _deps.getCurrentSessionId();
    const _pendingChat = _deps.getPendingChat();

    // Remember this pick so it surfaces under "Recent" next time the picker
    // opens — the whole point of quick-switch.
    if (m && m.mid) _pushRecent(_pickerModelKey(m) || m.mid);

    // Broadcast immediately so listeners (e.g. the tour) can advance without
    // waiting for the async session-create/PATCH that follows.
    try { document.dispatchEvent(new CustomEvent('odysseus:model-picked', { detail: m })); } catch {}

    // Blur search input before closing to dismiss keyboard on mobile
    if (document.activeElement) document.activeElement.blur();
    _close();
    // Refocus main textarea — skip on mobile to avoid keyboard bounce
    if (window.innerWidth >= 768) {
      const _ta = document.getElementById('message');
      if (_ta) setTimeout(() => _ta.focus(), 50);
    }
    if (!currentSessionId && _pendingChat) {
      // Already have a deferred session — just update the model
      _deps.setPendingChat({ url: m.url, modelId: m.mid, endpointId: m.endpointId, source: 'manual' });
      // Header stays as session name — model switch only updates picker
      updateModelPicker();
      uiModule.showToast(`Using ${m.display}`);
      finishSwitch();
      return;
    } else if (!currentSessionId) {
      // No session yet — create one with this model
      try {
        await _deps.createDirectChat(m.url, m.mid, m.endpointId);
      } catch (e) {
        uiModule.showError('Failed to start chat: ' + e);
        finishSwitch();
        return;
      }
    } else {
      // Existing session with no model — PATCH it
      const sessions = _deps.getSessions();
      const s = sessions.find(x => x.id === currentSessionId);
      if (s) { s.model = m.mid; s.endpoint_url = m.url; s.endpoint_id = m.endpointId || ''; s.endpoint_name = m.epName || ''; }
      updateModelPicker();
      const fd = new FormData();
      fd.append('model', m.mid);
      fd.append('endpoint_url', m.url);
      if (m.endpointId) fd.append('endpoint_id', m.endpointId);
      try {
        const res = await fetch(`${API_BASE}/api/session/${currentSessionId}`, { method: 'PATCH', body: fd });
        if (!res.ok) {
          uiModule.showError('Failed to set model');
          finishSwitch();
          return;
        }
        // Header stays as session name — model info shown in picker only
      } catch (e) {
        uiModule.showError('Failed to set model: ' + e);
        finishSwitch();
        return;
      }
    }
    // Update picker visibility — model is now set
    updateModelPicker();
    if (window.refreshChatContextHeader) window.refreshChatContextHeader('model-pick');
    uiModule.showToast(`Using ${m.display}`);
    finishSwitch();
  }

  document.addEventListener('odysseus:auto-select-model', async (e) => {
    const detail = (e && e.detail) || {};
    const currentSessionId = _deps.getCurrentSessionId();
    const sessions = _deps.getSessions();
    const current = sessions.find(x => x.id === currentSessionId);
    const pending = _deps.getPendingChat();
    if ((current && current.model) || (pending && pending.modelId)) return;

    if (window.modelsModule && window.modelsModule.refreshModels) {
      try { await window.modelsModule.refreshModels(false); } catch (_) {}
    }
    const items = window.modelsModule && window.modelsModule.getCachedItems ? window.modelsModule.getCachedItems() : [];
    const targetEndpointId = detail.endpointId ? String(detail.endpointId) : '';
    const targetModel = detail.modelId || '';
    let match = null;
    for (const item of items) {
      if (item.offline) continue;
      if (targetEndpointId && String(item.endpoint_id || '') !== targetEndpointId) continue;
      const models = (item.models || []).concat(item.models_extra || []);
      const displays = (item.models_display || []).concat(item.models_extra_display || []);
      const idx = targetModel ? models.indexOf(targetModel) : (models.length ? 0 : -1);
      if (idx >= 0) {
        match = {
          mid: models[idx],
          display: (displays[idx] || models[idx]).split('/').pop(),
          url: item.url || detail.url || '',
          endpointId: item.endpoint_id || detail.endpointId || '',
          epName: item.endpoint_name || detail.endpointName || '',
          providerText: [item.endpoint_name || detail.endpointName || '', item.url || detail.url || ''].filter(Boolean).join(' '),
        };
        break;
      }
    }
    if (!match && detail.modelId && detail.url) {
      match = {
        mid: detail.modelId,
        display: String(detail.modelId).split('/').pop(),
        url: detail.url,
        endpointId: detail.endpointId || '',
        epName: detail.endpointName || '',
        providerText: [detail.endpointName || '', detail.url || ''].filter(Boolean).join(' '),
      };
    }
    if (match) await _pick(match);
  });

  btn.addEventListener('pointerdown', (e) => {
    e.stopPropagation();
  });
  btn.addEventListener('click', (e) => {
    e.stopPropagation();
    if (menu.classList.contains('hidden') || menu.classList.contains('closing')) {
      // Force-clear any in-progress close animation
      menu.classList.remove('closing', 'hidden');
      const hasCache = _hasModelCache();
      if (hasCache) {
        _populate('');
      } else {
        _renderLoading('Loading models…');
      }
      if (window.modelsModule && window.modelsModule.refreshModels) {
        _refreshPickerModels({ force: true, showLoading: !hasCache }).then(isLatest => {
          if (isLatest && !menu.classList.contains('hidden')) _populate(search.value || '');
          updateModelPicker();
        }).catch(() => {});
      }
      if (window.innerWidth >= 768) search.focus();
      // Hide scroll button so it doesn't overlap
      const _scrollBtn = document.getElementById('scroll-bottom-btn');
      if (_scrollBtn) _scrollBtn.style.display = 'none';
    } else {
      _close();
    }
  });

  search.addEventListener('input', () => {
    if (_pickerLoading) return;
    _populate(search.value);
  });
  search.addEventListener('click', (e) => e.stopPropagation());
  if (refreshBtn) {
    refreshBtn.addEventListener('click', async (e) => {
      e.stopPropagation();
      refreshBtn.disabled = true;
      refreshBtn.classList.add('spinning');
      try {
        await _refreshPickerModels({ force: true, showLoading: true });
        if (!menu.classList.contains('hidden')) _populate(search.value || '');
        updateModelPicker();
      } catch (_) {
        uiModule.showToast('Model refresh failed');
      } finally {
        refreshBtn.disabled = false;
        refreshBtn.classList.remove('spinning');
      }
    });
  }
  search.addEventListener('keydown', (e) => {
    _handlePickerKeydown(e, listEl, '.model-switch-item', _close);
  });
  const addModelsBtn = document.getElementById('model-picker-add-models-btn');
  if (addModelsBtn) {
    addModelsBtn.addEventListener('click', (e) => {
      e.stopPropagation();
      _openPickerShortcut('models');
    });
  }
  document.addEventListener('click', (e) => {
    if (!menu.classList.contains('hidden') && !wrap.contains(e.target)) {
      _close();
    }
  });
}

/**
 * Update the model picker label to show the current model.
 * Always visible — shows current model name or "Select model" if none.
 * Called after selectSession, createDirectChat, and model switch.
 */
export function updateModelPicker() {
  if (!_deps) return;
  const label = document.getElementById('model-picker-label');
  if (!label) return;
  // Hide model picker when group chat is active
  const wrap = document.getElementById('model-picker-wrap');
  if (window.groupModule && window.groupModule.isActive()) {
    if (wrap) { wrap.style.display = 'none'; }
    return;
  }
  // Reset inline visibility (may have been hidden by typing in previous session)
  if (wrap) {
    wrap.style.display = '';
    wrap.style.opacity = '';
    wrap.style.pointerEvents = '';
  }
  const currentSessionId = _deps.getCurrentSessionId();
  const sessions = _deps.getSessions();
  const _pendingChat = _deps.getPendingChat();
  const s = sessions.find(x => x.id === currentSessionId);
  let modelId = null;
  if (s && s.model) {
    modelId = s.model;
  } else if (_pendingChat && _pendingChat.modelId) {
    modelId = _pendingChat.modelId;
    if (_pendingChat.source === 'fallback' && !_modelExists(modelId, _pendingChat.url || '')) {
      _deps.setPendingChat(null);
      modelId = null;
    }
  }
  if (!modelId && !currentSessionId && !_pendingChat && _deps.setPendingChat) {
    let cachedDefault = null;
    try {
      cachedDefault = window.__odysseusDefaultChat || null;
    } catch (_) {}
    if (!cachedDefault || !cachedDefault.endpoint_url || !cachedDefault.model) {
      try {
        cachedDefault = JSON.parse(localStorage.getItem('odysseus-default-chat-cache') || 'null');
      } catch (_) {}
    }
    if (cachedDefault && cachedDefault.endpoint_url && cachedDefault.model) {
      modelId = cachedDefault.model;
      _deps.setPendingChat({
        url: cachedDefault.endpoint_url,
        modelId,
        endpointId: cachedDefault.endpoint_id || '',
        source: 'default',
      });
    }
  }
  // SECURITY: deliberately NOT auto-injecting `odysseus-model-favorites[0]`
  // here. localStorage favorites are per-browser, not per-user, so on a
  // shared browser the previous account's first favorited model would
  // silently pre-populate the chatbox of the next user that signed in. If
  // we have no session model and no pending-chat pick, fall through to
  // the "Select model" placeholder below.
  //
  // Check if selected model is still available — fall back ONLY for pending chats with no user selection
  // Never override an existing session's model — the user explicitly chose it
  if (
    modelId &&
    !currentSessionId &&
    _pendingChat &&
    _pendingChat.source !== 'manual' &&
    window.modelsModule &&
    window.modelsModule.getCachedItems
  ) {
    const items = window.modelsModule.getCachedItems();
    const allAvailable = [];
    items.forEach(item => {
      if (item.offline) return;
      (item.models || []).concat(item.models_extra || []).forEach(m => allAvailable.push(m));
    });
    if (allAvailable.length > 0 && !allAvailable.includes(modelId)) {
      // Model no longer available — switch to first available
      const fallback = items.find(item => !item.offline && (item.models || []).length > 0);
      if (fallback) {
        modelId = fallback.models[0];
        _deps.setPendingChat({ url: fallback.url, modelId, endpointId: fallback.endpoint_id, source: 'fallback' });
      }
    }
  }
  const latestPending = _deps.getPendingChat && _deps.getPendingChat();
  if (
    !currentSessionId &&
    !_autoSelectingDefault &&
    window.modelsModule &&
    window.modelsModule.getCachedItems &&
    (!modelId || (latestPending && latestPending.source === 'fallback'))
  ) {
    _ensureDefaultPendingChat();
  }

  let displayName = modelId ? modelId.split('/').pop() : 'Select model';
  const routeItems = window.modelsModule?.getCachedItems?.() || [];
  const candidates = routeItems.filter(item => (item.models || []).concat(item.models_extra || []).includes(modelId));
  const selectedId = s?.endpoint_id || latestPending?.endpointId;
  const selectedUrl = s?.endpoint_url || latestPending?.url || '';
  const normalizeRouteUrl = url => String(url || '').replace(/\/chat\/completions\/?$/, '').replace(/\/$/, '');
  const selectedEndpoint = candidates.find(item => selectedId ? item.endpoint_id === selectedId
    : normalizeRouteUrl(item.url) === normalizeRouteUrl(selectedUrl));
  const routeName = s?.endpoint_name || selectedEndpoint?.endpoint_name || '';
  const endpointUrl = selectedUrl || selectedEndpoint?.url || '';
  const detectedProvider = selectedEndpoint?.category === 'local'
    ? 'Local' : providerLabel(endpointUrl || routeName);
  let addressLabel = !routeName;
  try {
    const endpoint = new URL(endpointUrl);
    addressLabel = addressLabel || [endpoint.host, endpoint.hostname, endpointUrl].includes(routeName);
  } catch (_) {}
  const provider = addressLabel ? detectedProvider : routeName;
  if (modelId && provider) displayName = `${provider} · ${displayName}`;
  // Keep route details and the full model ID available without putting raw
  // local addresses in the compact picker label.
  label.title = modelId ? [...new Set([modelId, routeName, endpointUrl].filter(Boolean))].join(' · ') : '';
  const logo = modelId ? providerLogo(modelId) : null;
  if (logo) {
    label.innerHTML = '<span class="model-picker-logo">' + logo + '</span> ';
    label.appendChild(document.createTextNode(displayName));
  } else {
    label.textContent = displayName;
  }
  _updateReasoningEffortUI(modelId, s, latestPending, selectedEndpoint);
}

// ── Reasoning effort control for models supporting reasoning levels ──
let _reasoningEffortBound = false;
let _pendingReasoningEffort = null;

export function getSelectedReasoningEffort() {
  if (!_deps) return _pendingReasoningEffort;
  const currentSessionId = _deps.getCurrentSessionId ? _deps.getCurrentSessionId() : null;
  if (!currentSessionId) return _pendingReasoningEffort;
  const sessions = _deps.getSessions ? _deps.getSessions() : [];
  const s = sessions.find(x => x.id === currentSessionId);
  const mode = s?.thinking_mode || '';
  if (mode.startsWith('effort:')) {
    return mode.slice('effort:'.length).trim().toLowerCase();
  }
  return null;
}
try { window.__odysseusGetReasoningEffort = getSelectedReasoningEffort; } catch (_) {}

function _initReasoningEffort() {
  if (_reasoningEffortBound) return;
  const wrap = document.getElementById('reasoning-effort-wrap');
  const btn = document.getElementById('reasoning-effort-btn');
  const menu = document.getElementById('reasoning-effort-menu');
  if (!wrap || !btn || !menu) return;
  _reasoningEffortBound = true;

  btn.addEventListener('click', (e) => {
    e.stopPropagation();
    const isHidden = menu.classList.contains('hidden');
    if (isHidden) {
      menu.classList.remove('hidden');
      btn.setAttribute('aria-expanded', 'true');
    } else {
      menu.classList.add('hidden');
      btn.setAttribute('aria-expanded', 'false');
    }
  });

  document.addEventListener('click', (e) => {
    if (!wrap.contains(e.target) && !menu.classList.contains('hidden')) {
      menu.classList.add('hidden');
      btn.setAttribute('aria-expanded', 'false');
    }
  });
}

function _findModelMetadata(modelId, selectedEndpoint) {
  if (selectedEndpoint?.models_metadata?.[modelId]) {
    return selectedEndpoint.models_metadata[modelId];
  }
  const routeItems = window.modelsModule?.getCachedItems?.() || [];
  for (const ep of routeItems) {
    if (ep.models_metadata && ep.models_metadata[modelId]) {
      return ep.models_metadata[modelId];
    }
  }
  return null;
}

async function _updateReasoningEffortUI(modelId, s, latestPending, selectedEndpoint) {
  _initReasoningEffort();
  const wrap = document.getElementById('reasoning-effort-wrap');
  const btn = document.getElementById('reasoning-effort-btn');
  const currentSpan = document.getElementById('reasoning-effort-current');
  const menu = document.getElementById('reasoning-effort-menu');
  if (!wrap || !btn || !currentSpan || !menu) return;

  if (!modelId) {
    wrap.style.display = 'none';
    return;
  }

  const metadata = _findModelMetadata(modelId, selectedEndpoint);
  const levels = metadata?.supported_reasoning_levels;
  if (!Array.isArray(levels) || levels.length === 0) {
    wrap.style.display = 'none';
    return;
  }

  wrap.style.display = 'inline-flex';

  const supportedEffortNames = levels.map(l => (typeof l === 'string' ? l : l.effort).toLowerCase());
  let activeLevel = 'default';
  const sessionMode = s?.thinking_mode || '';

  if (s && s.id) {
    if (sessionMode.startsWith('effort:')) {
      const parsed = sessionMode.slice('effort:'.length).trim().toLowerCase();
      if (supportedEffortNames.includes(parsed)) {
        activeLevel = parsed;
      } else {
        activeLevel = 'default';
        s.thinking_mode = 'off';
        try {
          fetch(`${API_BASE}/api/session/${encodeURIComponent(s.id)}/generation-settings`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            credentials: 'same-origin',
            body: JSON.stringify({ thinking_mode: 'off', reasoning_effort: null }),
          }).catch(() => {});
        } catch (_) {}
      }
    }
  } else if (_pendingReasoningEffort) {
    if (supportedEffortNames.includes(_pendingReasoningEffort)) {
      activeLevel = _pendingReasoningEffort;
    } else {
      activeLevel = 'default';
      _pendingReasoningEffort = null;
    }
  }

  currentSpan.textContent = activeLevel === 'default' ? 'Default' : (activeLevel.charAt(0).toUpperCase() + activeLevel.slice(1));
  btn.title = 'Reasoning effort';

  menu.innerHTML = '';
  const options = [{ effort: 'default', label: 'Default', desc: `Model default (${metadata.default_reasoning_level || 'standard'})` }];
  for (const l of levels) {
    const eff = (typeof l === 'string' ? l : l.effort).toLowerCase();
    const desc = (typeof l === 'object' && l.description) ? l.description : '';
    options.push({ effort: eff, label: eff.charAt(0).toUpperCase() + eff.slice(1), desc });
  }

  for (const opt of options) {
    const optBtn = document.createElement('button');
    optBtn.type = 'button';
    optBtn.className = 'reasoning-effort-option' + (opt.effort === activeLevel ? ' active' : '');
    optBtn.setAttribute('role', 'option');
    optBtn.setAttribute('aria-selected', opt.effort === activeLevel ? 'true' : 'false');
    if (opt.desc) optBtn.title = opt.desc;

    const lbl = document.createElement('span');
    lbl.textContent = opt.label;
    optBtn.appendChild(lbl);

    if (opt.effort === activeLevel) {
      const check = document.createElement('span');
      check.textContent = '✓';
      check.style.fontSize = '11px';
      optBtn.appendChild(check);
    }

    optBtn.addEventListener('click', async (e) => {
      e.stopPropagation();
      menu.classList.add('hidden');
      btn.setAttribute('aria-expanded', 'false');
      const newEffort = opt.effort;
      const thinkingModeVal = newEffort === 'default' ? 'off' : `effort:${newEffort}`;
      const effortVal = newEffort === 'default' ? null : newEffort;

      if (s && s.id) {
        s.thinking_mode = thinkingModeVal;
        try {
          await fetch(`${API_BASE}/api/session/${encodeURIComponent(s.id)}/generation-settings`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            credentials: 'same-origin',
            body: JSON.stringify({ thinking_mode: thinkingModeVal, reasoning_effort: effortVal }),
          });
        } catch (_) {}
      } else {
        _pendingReasoningEffort = effortVal;
      }
      _updateReasoningEffortUI(modelId, s, latestPending, selectedEndpoint);
    });

    menu.appendChild(optBtn);
  }
}