import{i as e,o as t,t as n}from"./home-dir-C44RaPME-B5iLWwCq.mjs";import{o as r}from"./paths-D92DjaZ--DdtJ-zYp.mjs";import{i}from"./session-key-Cn2QoOyX-QZeYmbxd.mjs";import a from"node:fs";import o from"node:path";import s from"node:os";const c=/^\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}(?:\.\d{3})?Z$/,l=/^sessions\.json\.bak\.\d+$/;function u(e,t){let n=`.${t}.`,r=e.lastIndexOf(n);if(r<0)return!1;let i=e.slice(r+n.length);return c.test(i)}function d(e){return l.test(e)?!0:u(e,`deleted`)||u(e,`reset`)||u(e,`bak`)}function f(e){return e===`sessions.json`||!e.endsWith(`.jsonl`)?!1:!d(e)}function p(e=Date.now()){return new Date(e).toISOString().replaceAll(`:`,`-`)}function m(e){let[t,n]=e.split(`T`);return!t||!n?e:`${t}T${n.replace(/-/g,`:`)}`}function h(e,t){let n=`.${t}.`,r=e.lastIndexOf(n);if(r<0)return null;let i=e.slice(r+n.length);if(!i||!c.test(i))return null;let a=Date.parse(m(i));return Number.isNaN(a)?null:a}function g(t,n=process.env,a=()=>e(n,s.homedir)){let c=r(n,a),l=i(t??`main`);return o.join(c,`agents`,l,`sessions`)}function _(e){return o.join(g(e),`sessions.json`)}const v=/^[a-z0-9][a-z0-9._-]{0,127}$/i;function y(e){let t=e.trim();if(!v.test(t))throw Error(`Invalid session ID: ${e}`);return t}function b(e){let t=e?.sessionsDir?.trim();return t?o.resolve(t):g(e?.agentId)}function x(e,t){let n=E(o.resolve(e))??o.resolve(e),r=E(t)??t,i=o.relative(n,r);if(!(!i||i.startsWith(`..`)||o.isAbsolute(i)))return o.resolve(n,i)}function S(e,t){let n=o.resolve(e);if(o.basename(n)!==`sessions`)return;let r=o.dirname(n),a=o.dirname(r);if(o.basename(a)!==`agents`)return;let s=o.dirname(a);return o.join(s,`agents`,i(t),`sessions`)}function C(e){let t=o.normalize(o.resolve(e)).split(o.sep).filter(Boolean),n=t.lastIndexOf(`sessions`);return n<2||t[n-2]!==`agents`?null:{parts:t,sessionsIndex:n}}function w(e){let t=C(e);if(!t)return;let{parts:n,sessionsIndex:r}=t;return n[r-1]||void 0}function T(e,n){let r=C(e);if(!r)return;let{parts:a,sessionsIndex:s}=r,c=a[s-1];if(!c)return;let l=i(c);if(l!==t(c)||l!==i(n))return;let u=a.slice(s+1);if(u.length!==1)return;let d=u[0];if(!(!d||d===`.`||d===`..`))return o.normalize(o.resolve(e))}function E(e){try{return a.realpathSync(e)}catch{return}}function D(e,t,n){let r=t.trim();if(!r)throw Error(`Session file path must not be empty`);let a=o.resolve(e),s=E(a)??a,c=o.isAbsolute(r)?E(r)??r:r,l=o.isAbsolute(c)?o.relative(s,c):c;if(l.startsWith(`..`)&&o.isAbsolute(c)){let e=e=>{let t=i(e),n=S(s,t);if(n){let e=x(n,c);if(e)return e}return x(g(t),c)},t=n?.agentId?.trim();if(t){let n=e(t);if(n)return n}let r=w(c);if(r){let t=e(r);if(t)return t;let n=T(c,r);if(n)return n}}if(!l||l.startsWith(`..`)||o.isAbsolute(l))throw Error(`Session file path must be within sessions directory`);return o.resolve(s,l)}function O(e,t,n){let r=y(e),i=typeof n==`string`?encodeURIComponent(n):typeof n==`number`?String(n):void 0;return D(t,i===void 0?`${r}.jsonl`:`${r}-topic-${i}.jsonl`)}function k(e,t,n){return O(e,g(t),n)}function A(e,t,n){let r=b(n),i=t?.sessionFile?.trim();if(i)try{return D(r,i,{agentId:n?.agentId})}catch{}return O(e,r)}function j(t,r){let a=i(r?.agentId??`main`),c=r?.env??process.env,l=()=>e(c,s.homedir);if(!t)return o.join(g(a,c,l),`sessions.json`);if(t.includes(`{agentId}`)){let r=t.replaceAll(`{agentId}`,a);return r.startsWith(`~`)?o.resolve(n(r,{home:e(c,l),env:c,homedir:l})):o.resolve(r)}return t.startsWith(`~`)?o.resolve(n(t,{home:e(c,l),env:c,homedir:l})):o.resolve(t)}export{j as a,d as c,O as i,h as l,A as n,p as o,k as r,f as s,_ as t};