'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','deleted'].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=scope==='active'?50:2000,filter=scope==='deleted'?'deleted_at IS NOT NULL':'archived=? AND deleted_at IS NULL',args=scope==='deleted'?[owner]:[owner,archived]; const count=store.db.prepare('SELECT COUNT(*) n FROM conversations WHERE owner=? AND '+filter).get(...args).n,found=[]; // Capture IDs once so a concurrent update cannot shift offsets and skip rows. const rows=store.db.prepare('SELECT id,payload,archived,deleted_at,created_at,updated_at FROM conversations WHERE owner=? AND '+filter+' ORDER BY updated_at DESC,rowid DESC LIMIT ?').all(...args,maximum); for(let offset=0;offsetmaximum}}; } module.exports={search};