feat(assistant): add authorized diagnostic context and resumable event streams

This commit is contained in:
abaishu0730 committed 2026-10-27 16:30:23 +08:00
1 parent 9d51676d7b
commit e0860afeb3
9 files changed
+167 -10

No files matched your search

@@ -0,0 +1,17 @@
'use strict';
const {setImmediate:yieldLoop}=require('node:timers/promises');
const fail=message=>Object.assign(new Error(message),{status:400});
async function search(store,owner,{scope='active',page=1,pageSize=25,query}){
if(typeof query!=='string'||query.length>100||!['active','archived'].includes(scope)||!Number.isSafeInteger(page)||page<1||!Number.isSafeInteger(pageSize)||pageSize<1||pageSize>50)throw fail('会话搜索条件无效。');
const term=query.trim().toLocaleLowerCase(),archived=Number(scope==='archived'),maximum=archived?2000:50;
const count=store.db.prepare('SELECT COUNT(*) n FROM conversations WHERE owner=? AND archived=?').get(owner,archived).n,found=[];
// Capture IDs once so a concurrent update cannot shift offsets and skip rows.
const rows=store.db.prepare('SELECT id,payload,created_at,updated_at FROM conversations WHERE owner=? AND archived=? ORDER BY updated_at DESC,rowid DESC LIMIT ?').all(owner,archived,maximum);
for(let offset=0;offset<rows.length;offset+=100){
for(const row of rows.slice(offset,offset+100)){const metadata=store.decode(row.id,row.payload);if(String(metadata.title||'').toLocaleLowerCase().includes(term))found.push({id:row.id,...metadata,archived:!!archived,createdAt:row.created_at,updatedAt:row.updated_at});}
await yieldLoop();
}
const actual=Math.min(page,Math.max(1,Math.ceil(found.length/pageSize)));
return {items:found.slice((actual-1)*pageSize,actual*pageSize),total:found.length,page:actual,pageSize,search:{scanned:rows.length,maximum,truncated:count>maximum}};
}
module.exports={search};