19 lines
4.5 KiB
JavaScript
19 lines
4.5 KiB
JavaScript
'use strict';
|
|
window.AssistantModelPicker={mount(root,{api,isActive,getConversation,onSelect,onError}){
|
|
const host=root.querySelector('.aw-compose-tools'),button=document.createElement('button');button.type='button';button.id='assistant-model-picker';button.className='btn btn-sm aw-model-picker';button.setAttribute('aria-haspopup','listbox');button.textContent='请选择模型';host.querySelector('#assistant-commands').after(button);const popup=document.createElement('div');popup.className='aw-model-popup';popup.hidden=true;host.append(popup);let data={providers:[],defaultSelection:null},conversation=null,disposed=false,generation=0;
|
|
const alive=()=>!disposed&&isActive();
|
|
function available(){return data.providers.filter(p=>p.enabled!==false&&p.hasApiKey!==false).flatMap(p=>p.models.map(m=>({provider:p,model:m})));}
|
|
function selected(){return conversation?.modelSelection||data.defaultSelection;}
|
|
function current(){const choice=selected();return available().find(row=>row.provider.id===choice?.providerId&&row.model.id===choice?.modelId);}
|
|
function label(){const row=current();const text=row?row.provider.name+' / '+(row.model.name||row.model.id):'请选择模型';button.textContent=text;button.setAttribute('aria-label','选择模型:'+text);button.disabled=!!conversation?.archived||!!conversation?.deletedAt;const header=root.querySelector('.aw-model');if(header)header.textContent=row?(row.model.name||row.model.id):'模型未选择';}
|
|
function close(){generation++;popup.hidden=true;popup.replaceChildren();button.setAttribute('aria-expanded','false');}
|
|
async function refresh(){const next=await api('/assistant/providers');if(!alive())return;data=next;label();return next;}
|
|
async function open(){const at=++generation;try{await refresh();if(!alive()||generation!==at)return;popup.hidden=false;button.setAttribute('aria-expanded','true');popup.innerHTML='<label class="visually-hidden" for="assistant-model-search">搜索模型</label><input id="assistant-model-search" class="form-control" placeholder="搜索供应商或模型"><div class="aw-model-options" role="listbox" aria-label="可用模型"></div>';const search=popup.querySelector('input'),options=popup.querySelector('[role=listbox]');let index=0,rows=[];
|
|
function draw(){const query=search.value.trim().toLowerCase();rows=available().filter(row=>(row.provider.name+' '+row.model.name+' '+row.model.id).toLowerCase().includes(query));options.replaceChildren();let previous=null;for(const [i,row]of rows.entries()){if(previous!==row.provider.id){const heading=document.createElement('div');heading.className='small text-secondary px-2 py-1';heading.textContent=row.provider.name;options.append(heading);previous=row.provider.id;}const item=document.createElement('button');item.type='button';item.className='aw-model-option';item.role='option';item.setAttribute('aria-label',row.model.name||row.model.id);item.setAttribute('aria-selected',String(i===index));item.textContent=(row.model.name||row.model.id)+(selected()?.providerId===row.provider.id&&selected()?.modelId===row.model.id?' ✓':'');item.title=row.model.id;item.onclick=()=>choose(row);options.append(item);}if(!rows.length){options.innerHTML='<p class="small text-secondary p-2">暂无匹配的可用模型。</p><a class="btn btn-sm" href="#assistant-settings">前往助手设置</a>';}}
|
|
async function choose(row){if(!alive()||generation!==at)return;const buttons=[...options.querySelectorAll('button')];buttons.forEach(b=>b.disabled=true);try{await onSelect({providerId:row.provider.id,modelId:row.model.id});if(alive()&&generation===at){conversation=getConversation();label();close();button.focus();}}catch(error){if(alive()&&generation===at){onError(error);buttons.forEach(b=>b.disabled=false);}}}
|
|
search.oninput=()=>{index=0;draw();};search.onkeydown=event=>{if(event.isComposing)return;if(event.key==='Escape'){event.preventDefault();event.stopPropagation();close();button.focus();}else if(['ArrowDown','ArrowUp'].includes(event.key)){event.preventDefault();index=(index+(event.key==='ArrowDown'?1:-1)+Math.max(1,rows.length))%Math.max(1,rows.length);draw();}else if(['Enter','Tab'].includes(event.key)&&!event.shiftKey&&rows[index]){event.preventDefault();void choose(rows[index]);}};draw();search.focus();
|
|
}catch(error){if(alive()&&generation===at)onError(error);}}
|
|
button.onclick=()=>{if(popup.hidden)void open();else close();};
|
|
return {refresh,open,close,get usable(){return !!current();},update(c){if(conversation?.id!==c?.id)close();conversation=c;label();},dispose(){disposed=true;close();button.remove();popup.remove();}};
|
|
}};
|