dellweb3's picture
Work OS validated demo local-v0.8
99caad5 verified
Raw History Blame Contribute Delete
7.12 kB
/* Authenticated LLM Gateway administration; no API key ever reaches the browser. */
R.llm=null;R.llmUsage=[];
const previousRtLoad=rtLoad;
rtLoad=async function(){await previousRtLoad();const [settings,usage]=await Promise.all([rtApi('/admin/llm'),rtApi('/admin/llm/usage')]);R.llm=settings;R.llmUsage=usage.items};
const llmTaskLabels={query_rewrite:'查询改写',short_summary:'短摘要',simple_qa:'简单问答',chat:'直接对话',document_extract:'要素提取',knowledge_synthesis:'知识综合',document_draft:'公文起草',complex_review:'复杂审核',complex_rewrite:'复杂重写',code_generation:'代码生成',code_analysis:'代码分析'};
function llmModelOptions(current){const ids=[...new Set([current,...(R.llm.catalog?.items||[]).filter(x=>x.enabled).map(x=>x.model_id)])];return ids.map(id=>'<option value="'+esc(id)+'" '+(id===current?'selected':'')+'>'+esc(id)+'</option>').join('')}
function llmRouteRows(){return Object.entries(R.llm.routing.routes).map(([route,x])=>'<tr><td><b>'+esc(route)+'</b></td><td>'+esc(x.model_id)+'</td><td>'+esc(x.reasoning_effort)+'</td><td>'+x.max_output_tokens+'</td><td>'+x.timeout_seconds+' s</td></tr>')}
views['model-access']=()=>!R.llm?page('LLM 网关','正在加载配置…'):
page('LLM 网关 · 模型接入','只展示服务端可用状态;密钥和完整访问地址不会返回浏览器。',
'<button data-llm-refresh>刷新状态</button> <button class="primary" data-llm-check '+(R.llm.mode==='real'?'':'disabled')+'>测试轻量模型连接</button>',
'<div class="grid"><div class="card"><h3>运行模式</h3><div class="metric">'+esc(R.llm.mode)+'</div><p class="muted">'+(R.llm.mode==='real'?'真实百炼调用':'显式演示模式')+'</p></div><div class="card"><h3>服务端密钥</h3><div class="metric">'+(R.llm.credential_ready?'已配置':'未启用')+'</div><p class="muted">密钥不会显示或由前端保存</p></div><div class="card"><h3>接口域名</h3><div class="small">'+esc(R.llm.endpoint_host||'待配置地域与 Workspace ID')+'</div></div></div><div class="card section"><h3>三档模型</h3>'+table(['档位','模型 ID','推理强度','输出上限','超时'],llmRouteRows())+'</div><div class="card section"><h3>百炼常用模型</h3><p class="muted">目录 '+(R.llm.catalog?.listed_count||0)+' 个;以下为您在百炼开启的模型,可在路由策略中选择。验证时间:'+esc(R.llm.catalog?.checked_at||'未验证')+'</p>'+table(['模型 ID','实测状态'],(R.llm.catalog?.items||[]).filter(x=>x.enabled).map(x=>'<tr><td>'+esc(x.model_id)+'</td><td>'+esc(x.inference_status==='verified'?'已验证':'待验证')+'</td></tr>'))+'</div>');
views['model-route']=()=>!R.llm?page('模型路由','正在加载配置…'):
page('LLM 任务路由','管理员发布版本化映射;客户端不能覆盖模型 ID,数字员工保存自己的发布快照。',
'<button class="primary" data-llm-edit>编辑并发布策略</button>',
'<div class="card"><p>配置版本 '+R.llm.version+' · '+esc(R.llm.routing.routing_revision)+'</p>'+table(['任务类型','路由档位','模型 ID'],Object.entries(R.llm.routing.task_routes).map(([task,route])=>'<tr><td>'+esc(llmTaskLabels[task]||task)+'</td><td>'+esc(route)+'</td><td>'+esc(R.llm.routing.routes[route].model_id)+'</td></tr>'))+'</div>');
views['model-usage']=()=>{const calls=R.llmUsage||[],real=calls.filter(x=>x.mode==='real');return page('LLM 用量','最近 100 次调用的元数据,包含演示调用;不保存提示词或密钥。','<button data-llm-refresh>刷新用量</button>',
'<div class="grid"><div class="card"><h3>最近调用</h3><div class="metric">'+calls.length+'</div></div><div class="card"><h3>真实输入 Token</h3><div class="metric">'+real.reduce((s,x)=>s+x.input_tokens,0)+'</div></div><div class="card"><h3>真实输出 Token</h3><div class="metric">'+real.reduce((s,x)=>s+x.output_tokens,0)+'</div></div></div><div class="card section">'+table(['时间','任务','模型','模式','状态','Token 输入 / 输出'],calls.map(x=>'<tr><td>'+esc(x.created_at)+'</td><td>'+esc(x.task_kind)+'</td><td>'+esc(x.model_id)+'</td><td>'+esc(x.mode)+'</td><td>'+esc(x.status)+'</td><td>'+x.input_tokens+' / '+x.output_tokens+'</td></tr>'))+'</div>')};
function llmPolicyEditor(){const routing=R.llm.routing;
const routes=Object.entries(routing.routes).map(([route,x])=>'<div class="card"><h3>'+esc(route)+' · '+esc(x.model_id)+'</h3><div class="field"><label>模型</label><select name="model-'+route+'">'+llmModelOptions(x.model_id)+'</select></div><div class="field"><label>推理强度</label><select name="effort-'+route+'">'+['none','low','medium','xhigh'].map(v=>'<option '+(v===x.reasoning_effort?'selected':'')+'>'+v+'</option>').join('')+'</select></div><div class="field"><label>最大输出 Token</label><input name="tokens-'+route+'" type="number" min="16" max="32768" value="'+x.max_output_tokens+'"></div><div class="field"><label>超时秒数</label><input name="timeout-'+route+'" type="number" min="1" max="300" value="'+x.timeout_seconds+'"></div></div>').join('');
const tasks=Object.entries(routing.task_routes).map(([task,route])=>'<div class="field"><label>'+esc(llmTaskLabels[task]||task)+'</label><select name="task-'+task+'">'+['light','standard','heavy'].map(x=>'<option '+(route===x?'selected':'')+'>'+x+'</option>').join('')+'</select></div>').join('');
rtDialog('发布 LLM 路由策略','<div class="hint">从服务端开启模型清单选择;更换模型请使用已验证的推理参数(新增可选模型均以 none 实测)。发布新版本后仅影响后续调用。</div><div class="field"><label>新路由版本</label><input name="revision" required pattern="[a-z0-9][a-z0-9.\\-]{2,79}" value="'+esc(routing.routing_revision)+'-r'+(R.llm.version+1)+'"></div>'+routes+tasks,async f=>{
const next=structuredClone(routing);next.routing_revision=f.get('revision');for(const route of ['light','standard','heavy']){next.routes[route].model_id=f.get('model-'+route);next.routes[route].reasoning_effort=f.get('effort-'+route);next.routes[route].max_output_tokens=Number(f.get('tokens-'+route));next.routes[route].timeout_seconds=Number(f.get('timeout-'+route))}for(const task of Object.keys(next.task_routes))next.task_routes[task]=f.get('task-'+task);
R.llm=await rtApi('/admin/llm','PUT',{expected_version:R.llm.version,routing:next});render();toast('LLM 路由策略已发布')});
}
document.addEventListener('click',async e=>{const t=e.target.closest('button');if(!t||!R.token)return;try{if(t.hasAttribute('data-llm-edit'))llmPolicyEditor();else if(t.hasAttribute('data-llm-refresh')){const [settings,usage]=await Promise.all([rtApi('/admin/llm'),rtApi('/admin/llm/usage')]);R.llm=settings;R.llmUsage=usage.items;render()}else if(t.hasAttribute('data-llm-check')){t.disabled=true;const result=await rtApi('/admin/llm/check','POST');toast('连接成功:'+result.model_id+' · '+result.route);R.llmUsage=(await rtApi('/admin/llm/usage')).items;render()}}catch(error){toast(error.message);if(t.isConnected)t.disabled=false}});