// @bun
var __defProp = Object.defineProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, {
      get: all[name],
      enumerable: true,
      configurable: true,
      set: (newValue) => all[name] = () => newValue
    });
};
var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
var __require = import.meta.require;

// src/session/store.ts
class InMemorySessionStore {
  sessions = new Map;
  maxSessions;
  maxMessagesPerSession;
  maxStepsPerSession;
  constructor(options = {}) {
    this.maxSessions = options.maxSessions ?? 500;
    this.maxMessagesPerSession = options.maxMessagesPerSession ?? 200;
    this.maxStepsPerSession = options.maxStepsPerSession ?? 200;
  }
  async get(sessionId) {
    return this.sessions.get(sessionId) ?? null;
  }
  async create(session) {
    this.evictIfNeeded();
    const now = new Date;
    const fullSession = {
      ...session,
      createdAt: now,
      updatedAt: now
    };
    this.sessions.set(session.sessionId, fullSession);
    return fullSession;
  }
  async appendStep(sessionId, step) {
    const session = this.sessions.get(sessionId);
    if (session) {
      session.steps.push(step);
      this.trimArray(session.steps, this.maxStepsPerSession);
      session.updatedAt = new Date;
    }
  }
  async appendMessage(sessionId, message) {
    const session = this.sessions.get(sessionId);
    if (session) {
      session.messages.push(message);
      this.trimArray(session.messages, this.maxMessagesPerSession);
      session.updatedAt = new Date;
    }
  }
  async update(sessionId, updates) {
    const session = this.sessions.get(sessionId);
    if (session) {
      Object.assign(session, updates, { updatedAt: new Date });
    }
  }
  async delete(sessionId) {
    return this.sessions.delete(sessionId);
  }
  async listByAgent(agentId, limit = 100) {
    const results = [];
    for (const session of this.sessions.values()) {
      if (session.agentId === agentId) {
        results.push(session);
        if (results.length >= limit)
          break;
      }
    }
    return results.sort((a, b) => b.updatedAt.getTime() - a.updatedAt.getTime());
  }
  async listByTenant(tenantId, limit = 100) {
    const results = [];
    for (const session of this.sessions.values()) {
      if (session.tenantId === tenantId) {
        results.push(session);
        if (results.length >= limit)
          break;
      }
    }
    return results.sort((a, b) => b.updatedAt.getTime() - a.updatedAt.getTime());
  }
  clear() {
    this.sessions.clear();
  }
  evictIfNeeded() {
    if (this.sessions.size < this.maxSessions) {
      return;
    }
    let oldestSessionId = null;
    let oldestUpdatedAt = Number.POSITIVE_INFINITY;
    for (const [sessionId, session] of this.sessions.entries()) {
      const updatedAt = session.updatedAt.getTime();
      if (updatedAt < oldestUpdatedAt) {
        oldestUpdatedAt = updatedAt;
        oldestSessionId = sessionId;
      }
    }
    if (oldestSessionId) {
      this.sessions.delete(oldestSessionId);
    }
  }
  trimArray(items, maxItems) {
    if (items.length <= maxItems) {
      return;
    }
    const overflow = items.length - maxItems;
    items.splice(0, overflow);
  }
}
function createInMemorySessionStore(options) {
  return new InMemorySessionStore(options);
}
function generateSessionId() {
  return `sess_${Date.now()}_${Math.random().toString(36).slice(2, 11)}`;
}
export {
  generateSessionId,
  createInMemorySessionStore,
  InMemorySessionStore
};
