feat(assistant): add authorized diagnostic context and resumable event streams
This commit is contained in:
1 parent
9d51676d7b
commit
e0860afeb3
9 files changed
+167
-10
No files matched your search
@@ -1,16 +1,29 @@
|
||||
'use strict';
|
||||
const {fail}=require('./store.cjs'),{secureBpmTransport}=require('../bpm/api.cjs');
|
||||
function bodyKeys(body,keys){if(!body||Array.isArray(body)||Object.keys(body).some(k=>!keys.includes(k)))throw fail('助手请求包含不支持的字段。');}
|
||||
async function assistantApi({assistant,user,sessionId,route,method,body,request,reply,workspace,trustedProxy=false}){
|
||||
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/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'){
|
||||
const params=new URL(request.url,'http://localhost').searchParams;
|
||||
if([...params.keys()].some(k=>k!=='after')||params.getAll('after').length>1)throw fail('事件查询参数无效。');
|
||||
const raw=eventRoute[2]==='stream'&&request.headers['last-event-id']!==undefined?request.headers['last-event-id']:params.get('after')??'0';
|
||||
if(typeof raw!=='string'||!/^\d+$/.test(raw)||!Number.isSafeInteger(Number(raw)))throw fail('事件游标无效。');
|
||||
const after=Number(raw),[_,id,kind]=eventRoute;
|
||||
assistant.store.row(id,user.id);if(after>assistant.store.eventCursor(id,user.id))throw fail('事件游标超过会话范围。');
|
||||
if(kind==='events')reply(200,assistant.store.events(id,user.id,{after}));
|
||||
else streams.open({request,response,id,user,after,authenticate:authenticateStream});
|
||||
return true;
|
||||
}
|
||||
if((route==='/api/assistant/settings'||route==='/api/assistant/check')&&method==='POST'){
|
||||
if(user.role!=='admin')throw fail('仅管理员可以配置或检查 DSH。',403);if(!secureBpmTransport(request,trustedProxy))throw fail('请在部署机本地或 HTTPS 页面维护模型配置。',403);
|
||||
if(route.endsWith('/settings')){const saved=assistant.store.saveSettings(body);workspace?.record(user,'配置 DSH 助手',saved.enabled?'已启用':'已暂停');reply(200,saved);}else{bodyKeys(body,[]);reply(200,await assistant.check());}return true;
|
||||
}
|
||||
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'].includes(key))throw fail('会话查询参数无效。');filters[key]=key==='scope'?value:Number(value);}reply(200,assistant.store.listConversations(user.id,filters));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 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);
|
||||
@@ -18,7 +31,7 @@ async function assistantApi({assistant,user,sessionId,route,method,body,request,
|
||||
if(!action&&!actionId&&method==='GET'){reply(200,assistant.store.conversation(id,user.id));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']);reply(202,assistant.send(id,principal,body.text));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;}
|
||||
bodyKeys(body,[]);if(action==='cancel'){reply(200,assistant.cancel(id,principal));return true;}
|
||||
if(actionId){reply(200,await assistant.executeAction(id,actionId,principal));return true;}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user