import{i as e}from"./home-dir-C44RaPME-B5iLWwCq.mjs";import{i as t,l as n,n as r,o as i,r as a}from"./paths-D6msg0S1-BO4XgM1u.mjs";import o from"node:fs";import s from"node:path";import c from"node:os";function l(e,t){let n=u(t);return n?n===e?`current`:`stale`:`custom`}function u(e){let t=e?.trim();if(!t)return;let n=s.basename(t);if(!n.endsWith(`.jsonl`))return;let r=n.slice(0,-6),i=r.indexOf(`-topic-`);if(i>0){let e=r.slice(0,i);return d(e)?e:void 0}let a=r.match(/^(\d{4}-\d{2}-\d{2}T[\w-]+(?:Z|[+-]\d{2}(?:-\d{2})?)?)_(.+)$/);return a?.[2]?d(a[2])?a[2]:void 0:d(r)?r:void 0}function d(e){return/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(e)}function f(e){let t=s.resolve(e);try{return o.realpathSync(t)}catch{return t}}function p(n,i,o,u){let d=[],f=l(n,o),p=e=>{try{d.push(e())}catch{}};if(i){let e=s.dirname(i);o&&f!==`stale`&&p(()=>r(n,{sessionFile:o},{sessionsDir:e,agentId:u})),p(()=>t(n,e)),o&&f===`stale`&&p(()=>r(n,{sessionFile:o},{sessionsDir:e,agentId:u}))}else if(o)if(u)f!==`stale`&&p(()=>r(n,{sessionFile:o},{agentId:u}));else{let e=o.trim();e&&d.push(s.resolve(e))}u&&(p(()=>a(n,u)),o&&f===`stale`&&p(()=>r(n,{sessionFile:o},{agentId:u})));let m=e(process.env,c.homedir),h=s.join(m,`.openclaw`,`sessions`);return p(()=>t(n,h)),Array.from(new Set(d))}function m(e,t){let n=`${e}.${t}.${i()}`;return o.renameSync(e,n),n}function h(e){return g(e).map(e=>e.archivedPath)}function g(e){let t=[],n=e.restrictToStoreDir&&e.storePath?f(s.dirname(e.storePath)):null;for(let r of p(e.sessionId,e.storePath,e.sessionFile,e.agentId)){let i=f(r);if(n){let e=s.relative(n,i);if(!e||e.startsWith(`..`)||s.isAbsolute(e))continue}if(o.existsSync(i))try{t.push({sourcePath:i,archivedPath:m(i,e.reason)})}catch{}}return t}function _(e){let t=e.archivedTranscripts??[];if(t.length>0){let n=e.sessionFile?.trim()?f(e.sessionFile):void 0,r=(n==null?void 0:t.find(e=>f(e.sourcePath)===n))?.archivedPath??t[0]?.archivedPath;if(r)return{sessionFile:r,transcriptArchived:!0}}for(let t of p(e.sessionId,e.storePath,e.sessionFile,e.agentId)){let e=f(t);if(o.existsSync(e))return{sessionFile:e,transcriptArchived:!1}}return{}}async function v(e){if(!Number.isFinite(e.olderThanMs)||e.olderThanMs<0)return{removed:0,scanned:0};let t=e.nowMs??Date.now(),r=e.reason??`deleted`,i=Array.from(new Set(e.directories.map(e=>s.resolve(e)))),a=0,c=0;for(let l of i){let i=await o.promises.readdir(l).catch(()=>[]);for(let u of i){let i=n(u,r);if(i==null||(c+=1,t-i<=e.olderThanMs))continue;let d=s.join(l,u);(await o.promises.stat(d).catch(()=>null))?.isFile()&&(await o.promises.rm(d).catch(()=>void 0),a+=1)}}return{removed:a,scanned:c}}export{_ as i,g as n,v as r,h as t};