import{createRequire as O}from"node:module";var K=Object.defineProperty;var L=(j)=>j;function N(j,C){this[j]=L.bind(null,C)}var R=(j,C)=>{for(var z in C)K(j,z,{get:C[z],enumerable:!0,configurable:!0,set:N.bind(C,z)})};var V=(j,C)=>()=>(j&&(C=j(j=0)),C);var X=O(import.meta.url);class H{sessions=new Map;maxSessions;maxMessagesPerSession;maxStepsPerSession;constructor(j={}){this.maxSessions=j.maxSessions??500,this.maxMessagesPerSession=j.maxMessagesPerSession??200,this.maxStepsPerSession=j.maxStepsPerSession??200}async get(j){return this.sessions.get(j)??null}async create(j){this.evictIfNeeded();let C=new Date,z={...j,createdAt:C,updatedAt:C};return this.sessions.set(j.sessionId,z),z}async appendStep(j,C){let z=this.sessions.get(j);if(z)z.steps.push(C),this.trimArray(z.steps,this.maxStepsPerSession),z.updatedAt=new Date}async appendMessage(j,C){let z=this.sessions.get(j);if(z)z.messages.push(C),this.trimArray(z.messages,this.maxMessagesPerSession),z.updatedAt=new Date}async update(j,C){let z=this.sessions.get(j);if(z)Object.assign(z,C,{updatedAt:new Date})}async delete(j){return this.sessions.delete(j)}async listByAgent(j,C=100){let z=[];for(let E of this.sessions.values())if(E.agentId===j){if(z.push(E),z.length>=C)break}return z.sort((E,F)=>F.updatedAt.getTime()-E.updatedAt.getTime())}async listByTenant(j,C=100){let z=[];for(let E of this.sessions.values())if(E.tenantId===j){if(z.push(E),z.length>=C)break}return z.sort((E,F)=>F.updatedAt.getTime()-E.updatedAt.getTime())}clear(){this.sessions.clear()}evictIfNeeded(){if(this.sessions.size<this.maxSessions)return;let j=null,C=Number.POSITIVE_INFINITY;for(let[z,E]of this.sessions.entries()){let F=E.updatedAt.getTime();if(F<C)C=F,j=z}if(j)this.sessions.delete(j)}trimArray(j,C){if(j.length<=C)return;let z=j.length-C;j.splice(0,z)}}function Z(j){return new H(j)}function Q(){let j=globalThis.crypto;if(typeof j?.randomUUID==="function")return j.randomUUID();if(typeof j?.getRandomValues==="function"){let C=j.getRandomValues(new Uint8Array(16)),z=C.at(6),E=C.at(8);if(z===void 0||E===void 0)throw Error("Secure session token generation requires 16 random bytes.");C[6]=z&15|64,C[8]=E&63|128;let F=[...C].map((J)=>J.toString(16).padStart(2,"0"));return[F.slice(0,4).join(""),F.slice(4,6).join(""),F.slice(6,8).join(""),F.slice(8,10).join(""),F.slice(10,16).join("")].join("-")}throw Error("Secure session IDs require Web Crypto support.")}function _(){return`sess_${Q()}`}export{_ as generateSessionId,Z as createInMemorySessionStore,H as InMemorySessionStore};
