feat(assistant): manage providers models and personal conversation recycle bin

This commit is contained in:
abaishu0730 committed 2026-10-28 09:58:11 +08:00
1 parent 506e6fbfad
commit 301cdf669d
29 files changed
+267 -83

No files matched your search

+11 -1
View File
@@ -4,6 +4,14 @@ function bodyKeys(body,keys){if(!body||Array.isArray(body)||Object.keys(body).so
async function assistantApi({assistant,user,sessionId,route,method,body,request,response,streams,authenticateStream,reply,workspace,trustedProxy=false}){
if(!route.startsWith('/api/assistant'))return false;if(!assistant)throw fail('DSH 助手服务未启动。',503);const principal={user,sessionId};
if(route==='/api/assistant'&&method==='GET'){const settings=assistant.store.settings();reply(200,{settings:user.role==='admin'?settings:{enabled:settings.enabled,allowActions:settings.allowActions,model:settings.model},skills:require('./skills.cjs').projectSkills(),usage:assistant.store.usage(),conversations:assistant.store.conversations(user.id)});return true;}
if(route==='/api/assistant/providers'&&method==='GET'){const list=assistant.store.providers.list();if(user.role==='admin')reply(200,list);else reply(200,{version:list.version,defaultSelection:list.defaultSelection,providers:list.providers.filter(p=>p.enabled&&p.hasApiKey&&p.models.length).map(({id,name,version,models})=>({id,name,version,models}))});return true;}
if((route==='/api/assistant/providers'||route==='/api/assistant/providers/default'||/^\/api\/assistant\/providers\/[\w-]+\/(delete|models)$/.test(route))&&method==='POST'){
if(user.role!=='admin')throw fail('仅管理员可以维护模型供应商。',403);if(!secureBpmTransport(request,trustedProxy))throw fail('请在部署机本地或 HTTPS 页面维护模型供应商。',403);
if(route==='/api/assistant/providers'){const saved=assistant.store.providers.save(body);workspace?.record(user,'维护模型供应商','已保存供应商配置');reply(body.version===0?201:200,saved);return true;}
if(route.endsWith('/default')){bodyKeys(body,['version','providerId','modelId']);reply(200,assistant.store.providers.chooseDefault(body));return true;}
const providerRoute=/^\/api\/assistant\/providers\/([\w-]+)\/(delete|models)$/.exec(route);bodyKeys(body,['version']);
if(providerRoute[2]==='delete'){if([...assistant.running.values()].some(run=>run.providerId===providerRoute[1]))throw fail('此供应商仍在回复,请停止并等待完成后删除。',409);const result=assistant.store.providers.remove(providerRoute[1],body);workspace?.record(user,'删除模型供应商',providerRoute[1]);reply(200,result);}else{const controller=new AbortController(),abort=()=>{if(!response.writableEnded)controller.abort();};response.once('close',abort);try{reply(200,await assistant.store.providers.discover(providerRoute[1],{version:body.version,signal:controller.signal}));}finally{response.removeListener('close',abort);}}return true;
}
if(route==='/api/assistant/context'&&method==='GET'){reply(200,require('./diagnostic-context.cjs').diagnosticContext(assistant.deps,user));return true;}
const eventRoute=/^\/api\/assistant\/conversations\/([\w-]+)\/(events|stream)$/.exec(route);
if(eventRoute&&method==='GET'){
@@ -24,11 +32,13 @@ async function assistantApi({assistant,user,sessionId,route,method,body,request,
if(route==='/api/assistant/operations'&&method==='GET'){const params=new URL(request.url,'http://localhost').searchParams;if([...params.keys()].some(k=>k!=='beforeId')||params.getAll('beforeId').length>1)throw fail('操作记录查询参数无效。');const beforeId=params.has('beforeId')?Number(params.get('beforeId')):undefined;if(beforeId!==undefined&&(!Number.isSafeInteger(beforeId)||beforeId<1))throw fail('操作记录游标无效。');reply(200,{events:assistant.operations(principal,{beforeId})});return true;}
if(route==='/api/assistant/conversations'&&method==='POST'){bodyKeys(body,[]);reply(201,assistant.store.create(user.id));return true;}
if(route==='/api/assistant/conversations'&&method==='GET'){const params=new URL(request.url,'http://localhost').searchParams,filters={};for(const [key,value]of params){if(!['scope','page','pageSize','query'].includes(key)||params.getAll(key).length!==1)throw fail('会话查询参数无效。');filters[key]=['scope','query'].includes(key)?value:Number(value);}reply(200,filters.query?await require('./conversation-search.cjs').search(assistant.store,user.id,filters):assistant.store.listConversations(user.id,filters));return true;}
const management=/^\/api\/assistant\/conversations\/([\w-]+)\/(delete|restore-deleted|purge|model)$/.exec(route);
if(management&&method==='POST'){const [,id,action]=management;bodyKeys(body,action==='purge'?['version','confirm']:action==='model'?['version','providerId','modelId']:['version']);const result=assistant.store[{delete:'trash','restore-deleted':'restoreDeleted',purge:'purge',model:'setModel'}[action]](id,user.id,body);workspace?.record(user,action==='model'?'选择个人对话模型':'管理个人会话',action+' '+id);reply(200,result);return true;}
const history=/^\/api\/assistant\/conversations\/([\w-]+)\/(archive|restore|continue)$/.exec(route);
if(history&&method==='POST'){bodyKeys(body,['version']);const result=assistant.store[{archive:'archive',restore:'restore',continue:'continueConversation'}[history[2]]](history[1],user.id,body);workspace?.record(user,'管理个人助手会话',history[2]);reply(200,result);return true;}
const match=/^\/api\/assistant\/conversations\/([\w-]+)(?:\/(messages|repair-messages|cancel)|\/actions\/([\w-]+)\/execute)?$/.exec(route);
if(match){const [,id,action,actionId]=match;
if(!action&&!actionId&&method==='GET'){reply(200,assistant.store.conversation(id,user.id));return true;}
if(!action&&!actionId&&method==='GET'){const params=new URL(request.url,'http://localhost').searchParams;if([...params.keys()].some(k=>k!=='scope')||params.getAll('scope').length>1||params.has('scope')&&params.get('scope')!=='deleted')throw fail('会话读取参数无效。');reply(200,assistant.store.conversation(id,user.id,{includeDeleted:params.get('scope')==='deleted'}));return true;}
if(method==='POST'){
if(action==='repair-messages'){bodyKeys(body,['text','paths']);reply(202,assistant.sendRepair(id,principal,body.text,body.paths));return true;}
if(action==='messages'){bodyKeys(body,['text','diagnostic','requestId']);const diagnostic=require('./diagnostic-context.cjs').validateDiagnostic(body.diagnostic,assistant.deps,user);reply(202,assistant.send(id,principal,body.text,{diagnostic,requestId:body.requestId}));return true;}