returnjdbc.queryForList("SELECT c.id,c.title,c.version,c.created_at,c.updated_at FROM conversation c WHERE c.user_id=? AND (c.title LIKE ? ESCAPE '!' OR EXISTS (SELECT 1 FROM conversation_message m WHERE m.conversation_id=c.id AND m.content LIKE ? ESCAPE '!')) ORDER BY c.updated_at DESC,c.id LIMIT 50 OFFSET ?",user.id(auth),pattern,pattern,offset);
jdbc.update("INSERT INTO conversation(id,user_id,title,auto_title) VALUES(?,?,?,?)",id,owner,request.title().isBlank()?"新对话":request.title().trim(),request.title().isBlank());
if(jdbc.update("UPDATE conversation SET title=?,auto_title=FALSE,version=version+1,updated_at=CURRENT_TIMESTAMP WHERE id=? AND version=?",request.title().trim(),id,request.version())!=1)
varhistory=jdbc.queryForList("SELECT t.id,t.request_json FROM conversation_message m JOIN agent_task t ON t.id=m.task_id WHERE m.conversation_id=? AND t.user_id=? ORDER BY m.created_at DESC,m.id DESC LIMIT 1",id,owner);
varprevious=jdbc.queryForList("SELECT m.conversation_id FROM conversation_message m JOIN agent_task t ON t.id=m.task_id WHERE t.user_id=? AND t.idempotency_key=?",String.class,owner,key);
jdbc.update("INSERT INTO conversation_message(id,conversation_id,role,content,task_id) VALUES(?,?,'user',?,?)",UUID.randomUUID().toString(),conversation,content,task);
jdbc.update("UPDATE conversation SET title=CASE WHEN auto_title=TRUE THEN ? ELSE title END,auto_title=FALSE,version=version+1,updated_at=CURRENT_TIMESTAMP WHERE id=?",title,conversation);
varrows=jdbc.queryForList("SELECT id,title,version,created_at,updated_at FROM conversation WHERE id=? AND user_id=?"+(lock?" FOR UPDATE":""),id,owner);
varbatches=jdbc.queryForList("SELECT s.batch_id FROM search_item s JOIN search_batch b ON b.id=s.batch_id WHERE b.user_id=? AND s.project_id=? ORDER BY b.created_at DESC,b.id LIMIT 1",String.class,owner,scope.projectId());
vartasks=jdbc.queryForList("SELECT status,request_json FROM agent_task WHERE id=? AND user_id=? AND operation='OPPORTUNITY_SEARCH' FOR UPDATE",batch,owner);
varsteps=jdbc.queryForList(market?"SELECT tool_name,evidence_id FROM market_snapshot WHERE task_id=? ORDER BY tool_name":
"SELECT step_key,tool_name,evidence_id FROM research_step WHERE task_id=? AND step_key NOT IN ('synthesis','file','notice:search','buyer:entity') AND step_key NOT LIKE 'entity:%' AND step_key NOT LIKE 'vision:%' ORDER BY step_key",sourceTask);
jdbc.update("INSERT INTO evidence(id,task_id,tool_call_id,source_url,locator,excerpt,source_title,source_type,content_hash,source_snapshot) VALUES(?,?,?,?,?,?,?,'PUBLIC_RECORD',?,?)",
returnjdbc.query("SELECT e.*,t.status AS call_status,t.task_id AS call_task FROM evidence e JOIN tool_call t ON t.id=e.tool_call_id WHERE e.task_id=? AND (e.source_type IS NULL OR e.source_type<>'GENERATED_DRAFT') ORDER BY e.created_at,e.id",(rs,n)->{
jdbc.update("INSERT INTO temporary_file(id,user_id,purpose,media_type,sha256,status,expires_at,filename,original_bytes,segments_json) VALUES(?,?,'BID_PROJECT',?,?,'READY',?,?,?,?)",
jdbc.update("INSERT INTO tool_call(id,task_id,provider,tool_name,client_version,status,cost_units) VALUES(?,?,'LOCAL','FILE_PARSE','D3','SUCCEEDED',0)",call,task);
for(varrow:jdbc.queryForList("SELECT id,user_id FROM temporary_file WHERE status='READY' AND expires_at<=CURRENT_TIMESTAMP ORDER BY expires_at,id LIMIT 100"))
if(jdbc.update("UPDATE temporary_file SET status='DELETED',original_bytes=NULL,segments_json=NULL,storage_path=NULL,deleted_at=CURRENT_TIMESTAMP WHERE id=? AND status<>'DELETED'",id)>0)
jdbc.update("INSERT INTO file_deletion_audit(id,file_id,user_id,reason) VALUES(?,?,?,?)",UUID.randomUUID().toString(),id,owner,reason);
returnjdbc.query("SELECT t.id,t.status,t.created_at,r.scheme_id,r.scheme_name,r.scheme_version FROM agent_task t LEFT JOIN search_run r ON r.task_id=t.id WHERE t.user_id=? AND t.operation='OPPORTUNITY_SEARCH' ORDER BY t.created_at DESC,t.id LIMIT 50",(rs,n)->{
jdbc.queryForObject("SELECT id FROM app_user WHERE id=? FOR UPDATE",String.class,owner);
varprevious=jdbc.queryForList("SELECT t.id,r.scheme_id FROM agent_task t LEFT JOIN search_run r ON r.task_id=t.id WHERE t.user_id=? AND t.idempotency_key=?",owner,key);
jdbc.update("INSERT INTO search_run(task_id,scheme_id,scheme_name,scheme_version) VALUES(?,?,?,?)",task.get("id"),id,scheme.get("name"),scheme.get("version"));
returnjdbc.queryForList("SELECT t.id FROM search_run r JOIN agent_task t ON t.id=r.task_id WHERE r.scheme_id=? AND t.user_id=? ORDER BY t.created_at DESC,t.id",String.class,id,owner);
varsnapshots=jdbc.queryForList("SELECT s.snapshot FROM search_item s JOIN search_batch b ON b.id=s.batch_id WHERE s.project_id=? AND b.user_id=? ORDER BY b.created_at DESC LIMIT 1",String.class,id,owner);
vardocs=jdbc.queryForList("SELECT p.document FROM project p JOIN watch_record w ON w.project_id=p.id WHERE p.id=? AND w.user_id=?",String.class,id,owner);