Skip to content

armgit / ODS

Public Imported from GitHub
0

ODS V3 Pre-Release: Public testing and refinement ahead of the official V3 launch. Turn your PC, Mac, or Linux box into a private AI server.

15.6 KB
Raw Blame
import {useEffect,useId,useRef,useState} from 'react'
import {Check,ChevronDown,Loader2,SlidersHorizontal} from 'lucide-react'
import {Link} from 'react-router-dom'
import {useModels} from '../hooks/useModels'
import PortalModelRecovery from './PortalModelRecovery'
import './portal-model-selector.css'

export function modelDisplayName(model,compact=false) {
  let value=String(typeof model==='string'?model:model?.name || model?.id || '').trim()
  if(compact && /^extra\./i.test(value)) {
    const family=value.match(/\b([a-z]+[\d.]*-\d+(?:\.\d+)?[bm])\b/i)
    if(family)value=family[1]
  }
  value=value.replace(/^extra\.(?:hf[-.])?/i,'').replace(/\.gguf$/i,'').replace(/[-_.][a-f\d]{8,64}$/i,'')
    .replace(/\s*·\s*(?:I?Q\d[\w.]*|[BF]F?16|AWQ|GPTQ).*$/i,'')
    .replace(/(?:^|[-\s])(?:I?Q\d(?:_[A-Z\d]+)*)(?=$|[-\s])/gi,' ')
    .replace(/\bGGUF\b/gi,'').replace(/[-_]+/g,' ').replace(/([a-z])(\d)/gi,'$1 $2').replace(/\s+/g,' ').trim()
  if(compact)value=value.match(/^.*?\b\d+(?:\.\d+)?[BM]\b/i)?.[0] || value
  return value || 'Choose model'
}

function details(model) {
  const context=Number(model.contextLength)
  return [model.quantization,Number.isFinite(context) && context>0?`${Number((context/1024).toFixed(1))}K context`:null].filter(Boolean).join(' · ')
}

function verifiedNativeProfile(model) {
  const support=model.activationSupport
  return support?.available===true && support.source==='measured-native' && support.mode==='native-profile'
    && Number.isInteger(support.contextLength) && support.contextLength===model.contextLength
}

function quickSwitchProfile(model) {
  if(model.fitsVram===true || verifiedNativeProfile(model))return model
  const limit=Number(model.maxContextLength || model.contextLength || 0)
  const fitting=(Array.isArray(model.contextOptions)?model.contextOptions:[])
    .filter(option=>option?.fitsVram===true && Number.isSafeInteger(option.contextLength)
      && option.contextLength>=4096 && option.contextLength<=limit)
    .sort((a,b)=>b.contextLength-a.contextLength)[0]
  return fitting?{...model,contextLength:fitting.contextLength,fitsVram:true}:model
}

/** A closed, unused selector must not start model-catalog requests or polling. */
export default function PortalModelSelector(props) {
  const [started,setStarted]=useState(false)
  const lastCatalogSource=useRef(props.runtimeSource)
  const confirmedSource=['remote-provider','local-switchboard','external-host'].includes(props.runtimeSource)
  useEffect(()=>{if(confirmedSource)lastCatalogSource.current=props.runtimeSource},[props.runtimeSource,confirmedSource])
  const lastLabel=useRef({scope:props.displayScope,value:null})
  // A fresh validated poll can confirm the new chat's label even when its
  // primitive fields are unchanged. Availability/source still gate the hint.
  useEffect(()=>{
    if(lastLabel.current.scope!==props.displayScope){lastLabel.current={scope:props.displayScope,value:null};return}
    if(!props.availability || props.availability==='switching'
      || confirmedSource && props.runtimeSource!=='remote-provider'
      || props.runtimeFingerprint && props.runtimeFingerprint!==lastLabel.current.value?.fingerprint)lastLabel.current.value=null
    if(props.availability==='available' && props.runtimeSource==='remote-provider' && props.activeModel)
      lastLabel.current.value={model:props.activeModel,fingerprint:props.runtimeFingerprint}
  },[props.availability,props.displayScope,props.runtimeSource,props.runtimeFingerprint,props.activeModel,props.runtimeObservation,confirmedSource])
  // This is a diagnostic label only. Never pass it as an active model/source.
  const remembered=lastLabel.current.scope===props.displayScope
    && (!confirmedSource || props.runtimeSource==='remote-provider')
    && (!props.runtimeFingerprint || props.runtimeFingerprint===lastLabel.current.value?.fingerprint)
      ?lastLabel.current.value:null
  const unverified=props.availability==='available' && !confirmedSource && !props.activeModel
  const qualifier=unverified?'unverified':'unavailable'
  const notice=unverified?'Model selection is not currently verified.':'Portal is unavailable. Model selection is not currently verified.'
  const unavailableLabel=props.availability==='unavailable' || unverified?{
    text:remembered?`${modelDisplayName(remembered.model,true)} · ${qualifier}`:`Model ${qualifier}`,
    model:remembered?modelDisplayName(remembered.model,true):null,
    qualifier,notice,
    label:remembered?`Last confirmed model: ${modelDisplayName(remembered.model,true)}; ${unverified?'model unverified':'Portal unavailable'}`:`Model ${qualifier}`,
    title:remembered?`Last confirmed model: ${modelDisplayName(remembered.model)}. ${notice}`:notice,
  }:null
  // Remember only which reads to suppress, even before the first menu opening.
  // Selection and mutation authorization still require the current source.
  const observeCatalog=(confirmedSource?props.runtimeSource:lastCatalogSource.current)!=='remote-provider'
  if(started)return <LoadedModelSelector {...props} observeCatalog={observeCatalog} unavailableLabel={unavailableLabel}/>
  return <div className="portal-model-selector"><button type="button" className="portal-model-trigger"
    aria-label={unavailableLabel?.label || `Choose model: ${modelDisplayName(props.activeModel,true)}`} aria-haspopup="dialog" aria-expanded="false"
    title={unavailableLabel?.title || modelDisplayName(props.activeModel)} onClick={()=>setStarted(true)}>
    <span>{unavailableLabel?.model || unavailableLabel?.text || modelDisplayName(props.activeModel,true)}</span>{unavailableLabel?.model && <span className="portal-model-unavailable"> · {unavailableLabel.qualifier}</span>}<ChevronDown size={12} aria-hidden="true"/>
  </button></div>
}

/** Once opened, retain the hook even while closed so an accepted swap stays observed. */
function LoadedModelSelector({activeModel='',runtimeSource,observeCatalog,unavailableLabel,busy=false,onSwitchingChange,onSettled}) {
  const catalog=useModels({observe:observeCatalog})
  const {currentModel,activationReadyModel,loading,error,canActivateModels,activationModeError,activationLoading,modelLifecycle,hostRuntime,modelManagement,runtimeActionLoading,actionLoadingModels=[],loadModel,refresh,clearMutationError}=catalog
  const models=Array.isArray(catalog.models)?catalog.models:[]
  const installed=models.filter(model=>observeCatalog && model && typeof model.id==='string' && ['loaded','downloaded'].includes(model.status))
    .map(quickSwitchProfile)
  const [open,setOpen]=useState(true),[confirmId,setConfirmId]=useState(null),[pending,setPending]=useState(false),[localError,setLocalError]=useState('')
  const [recoveryPending,setRecoveryPending]=useState(false),[recoveryBusy,setRecoveryBusy]=useState(false)
  const root=useRef(null),trigger=useRef(null),list=useRef(null),mounted=useRef(true),submitLock=useRef(false)
  const managementUnavailable=hostRuntime===true && modelManagement?.managed==null
  const id=useId(),remote=runtimeSource==='remote-provider',external=runtimeSource==='external-host' && !managementUnavailable && modelManagement?.managed!==true,local=runtimeSource==='local-switchboard' || (runtimeSource==='external-host' && modelManagement?.managed===true)
  // A confirmed cloud route is independent of the local catalog's lifecycle,
  // which may remain stale after a catalog failure. Keep mutations initiated
  // here blocking until their own completion, even if the route changes.
  const switching=pending || recoveryBusy || Boolean(runtimeActionLoading)
    || (!remote && (Boolean(activationLoading) || Boolean(modelLifecycle?.active && modelLifecycle.operation==='model_activation')))
  const current=local?models.find(model=>model.id===currentModel):null
  const selectedId=local && activationReadyModel===currentModel?currentModel:null
  const activeName=current || activeModel
  const confirmation=installed.find(model=>model.id===confirmId)
  const switchingCallback=useRef(onSwitchingChange)
  switchingCallback.current=onSwitchingChange
  useEffect(()=>{onSwitchingChange?.(switching)},[switching,onSwitchingChange])
  useEffect(()=>{mounted.current=true;return ()=>{mounted.current=false;switchingCallback.current?.(false)}},[])
  function close(focus=false) {setOpen(false);setConfirmId(null);if(focus)trigger.current?.focus()}
  useEffect(()=>{
    if(!open)return
    const outside=event=>{if(!root.current?.contains(event.target))close()}
    const escape=event=>{if(event.key==='Escape'){event.preventDefault();close(true)}}
    window.addEventListener('pointerdown',outside);window.addEventListener('keydown',escape)
    return ()=>{window.removeEventListener('pointerdown',outside);window.removeEventListener('keydown',escape)}
  },[open])
  useEffect(()=>{
    if(!open)return
    if(confirmId)root.current?.querySelector('.portal-model-confirmation button')?.focus()
    else list.current?.querySelector('[aria-checked=true],button:not(:disabled)')?.focus()
  },[open,confirmId,loading])
  function unavailable(model) {
    if(switching || modelLifecycle?.active || actionLoadingModels.length)return 'A model operation is in progress.'
    if(recoveryPending)return 'Recover the interrupted model switch before loading another model.'
    if(remote)return 'This conversation uses a remote provider. Choose its model in provider settings.'
    if(managementUnavailable)return activationModeError || 'Runtime management could not be verified. Refresh the model list.'
    if(external)return 'This model is managed on the external host. Switch it there.'
    if(!local)return 'The conversation’s model source is not confirmed. Review Models before switching.'
    if(busy)return 'Wait for the active task to finish before switching models.'
    if(!canActivateModels)return activationModeError || 'Model switching is unavailable for this runtime.'
    if(model.fitsVram!==true && !model.recommended && !verifiedNativeProfile(model))return 'Review this model’s memory requirements in Models before loading it.'
    return ''
  }
  async function activate() {
    if(!confirmation || unavailable(confirmation) || submitLock.current)return
    submitLock.current=true;setPending(true);setLocalError('');setConfirmId(null);onSwitchingChange?.(true)
    try {
      const contextLength=Number(confirmation.contextLength)
      if(!Number.isSafeInteger(contextLength) || contextLength<4096 || contextLength>10_000_000)
        throw new Error('The model context is unavailable. Refresh the model list before switching.')
      // Activate exactly the context shown in the confirmation, not a catalog default.
      await loadModel(confirmation.id,{contextLength})
    }
    catch (failure) {if(mounted.current)setLocalError(failure?.message || 'The model could not be activated.')}
    finally {
      submitLock.current=false
      if(mounted.current){setPending(false);onSettled?.()}
    }
  }
  const reason=switching?'':confirmation?unavailable(confirmation):remote?'This conversation uses a remote provider.':managementUnavailable?activationModeError || 'Runtime management could not be verified. Refresh the model list.':external?'This model is managed on the external host.':!local?'The conversation’s model source is not confirmed.':busy?'The current task is still running.':!canActivateModels && !loading?activationModeError:''
  const showActiveFallback=!unavailableLabel && activeModel && !current
  return <div ref={root} className="portal-model-selector">
    <button ref={trigger} type="button" className="portal-model-trigger" aria-label={unavailableLabel?.label || `Choose model: ${modelDisplayName(activeName,true)}`} aria-haspopup="dialog" aria-expanded={open} aria-controls={open?id:undefined} title={unavailableLabel?.title || modelDisplayName(activeName)} onClick={()=>{if(open)close();else {setOpen(true);if(observeCatalog)void refresh()}}}>
      {switching && <Loader2 size={12} className="portal-model-loading" aria-hidden="true"/>}<span>{unavailableLabel?.model || unavailableLabel?.text || modelDisplayName(activeName,true)}</span>{unavailableLabel?.model && <span className="portal-model-unavailable"> · {unavailableLabel.qualifier}</span>}<ChevronDown size={12} aria-hidden="true"/>
    </button>
    <section id={id} role="dialog" aria-label="Choose model" className="portal-model-menu" hidden={!open} style={!open?{display:'none'}:undefined}>
      <header><strong>{confirmation?'Switch model':'Model'}</strong>{switching && <span role="status">Switching…</span>}</header>
      {confirmation?<div className="portal-model-confirmation">
        <p>Switch to <strong>{modelDisplayName(confirmation)}</strong>?</p><small>This changes the active model across ODS.</small>
        {details(confirmation) && <small>{details(confirmation)}</small>}
        {reason && <p role="status">{reason}</p>}
        <div><button type="button" onClick={()=>setConfirmId(null)}>Cancel</button><button type="button" className="portal-model-confirm" disabled={Boolean(reason)} onClick={activate}>Switch model</button></div>
      </div>:<>
        <div ref={list} role="menu" aria-label="Installed models" className="portal-model-options" onKeyDown={event=>{
          if(!['ArrowDown','ArrowUp','Home','End'].includes(event.key))return
          event.preventDefault()
          const options=[...event.currentTarget.querySelectorAll('button:not(:disabled)')],index=options.indexOf(document.activeElement)
          const next=event.key==='Home'?0:event.key==='End'?options.length-1:(index+(event.key==='ArrowDown'?1:-1)+options.length)%options.length
          options[next]?.focus()
        }}>
          {showActiveFallback && <div role="menuitemradio" aria-checked="true" className="portal-model-option"><span><strong>{modelDisplayName(activeModel)}</strong><small>{remote?'Remote provider':external?'External host':'Active model'}</small></span><Check size={15} aria-hidden="true"/></div>}
          {installed.map(model=>{const selected=model.id===selectedId,disabled=unavailable(model);return <button key={model.id} role="menuitemradio" aria-checked={selected} type="button" className="portal-model-option" disabled={!selected && Boolean(disabled)} title={disabled || modelDisplayName(model)} onClick={()=>{if(selected)close(true);else if(!disabled)setConfirmId(model.id)}}><span><strong>{modelDisplayName(model)}</strong><small>{details(model)}</small></span>{model.id===activationLoading?<Loader2 size={15} className="portal-model-loading" aria-hidden="true"/>:selected?<Check size={15} aria-hidden="true"/>:null}</button>})}
        </div>
        {loading && observeCatalog && <p role="status" className="portal-model-notice">Loading models…</p>}
        {unavailableLabel && <p role="status" className="portal-model-notice">{unavailableLabel.notice}</p>}
        {!unavailableLabel && !loading && !installed.length && !showActiveFallback && <p className="portal-model-notice">No installed models found.</p>}
        {reason && <p className="portal-model-notice" role="status">{reason}</p>}
        {(localError || error) && <p role="alert" className="portal-model-notice">{localError || error} {observeCatalog && <button type="button" onClick={()=>void refresh()}>Refresh</button>}</p>}
        {external
          ? <p className="portal-model-notice">Change this model on its external host.</p>
          : <Link className="portal-model-manage" to={remote?'/pixel/settings?section=connections':'/models'}><SlidersHorizontal size={14} aria-hidden="true"/>{remote?'Provider settings':'Manage models'}</Link>}
      </>}
      <PortalModelRecovery active={open && observeCatalog} operationActive={pending || Boolean(modelLifecycle?.active)} refreshKey={`${pending}:${Boolean(activationLoading)}`} onPendingChange={setRecoveryPending} onBusyChange={setRecoveryBusy} onRecovered={()=>{setLocalError('');clearMutationError();void refresh();onSettled?.()}}/>
    </section>
  </div>
}