import { createRequire } from "node:module";
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 = /* @__PURE__ */ createRequire(import.meta.url);

// src/memory/manager.ts
import { randomUUID } from "node:crypto";
function extractMessageContent(message) {
  const content = message.content;
  if (typeof content === "string") {
    return content;
  }
  if (Array.isArray(content)) {
    return content.map((part) => {
      if (typeof part === "string")
        return part;
      if ("text" in part && typeof part.text === "string")
        return part.text;
      return "";
    }).filter(Boolean).join("");
  }
  return "";
}
function extractAgentMessageContent(message) {
  const content = message.content;
  if (typeof content === "string") {
    return content;
  }
  if (Array.isArray(content)) {
    return content.map((part) => {
      if (typeof part === "string")
        return part;
      if ("text" in part && typeof part.text === "string")
        return part.text;
      return "";
    }).filter(Boolean).join("");
  }
  return "";
}
function roleToEntryType(role) {
  switch (role) {
    case "assistant":
      return "assistant";
    case "system":
      return "system";
    case "tool":
      return "tool";
    case "user":
    default:
      return "user";
  }
}

class BaseAgentMemoryManager {
  async append(session, entry) {
    const current = await this.load(session.sessionId) ?? this.bootstrapMemory(session);
    const finalEntry = {
      id: randomUUID(),
      createdAt: entry.createdAt ?? new Date,
      ...entry
    };
    current.memory.entries.push(finalEntry);
    await this.save(current);
    return current;
  }
  async summarize(_session) {
    return;
  }
  async prune(_session) {}
  bootstrapMemory(session) {
    return {
      session,
      memory: {
        entries: session.messages.map((message) => ({
          id: randomUUID(),
          createdAt: new Date,
          type: roleToEntryType(message.role),
          content: extractMessageContent(message)
        }))
      }
    };
  }
}
function trackMessageInMemory(manager, session, message) {
  if (!manager)
    return;
  manager.append(session, {
    type: message.role,
    content: extractAgentMessageContent(message),
    metadata: message.metadata
  });
}

// src/memory/in-memory.ts
class InMemoryAgentMemory extends BaseAgentMemoryManager {
  ttlMs;
  maxEntries;
  store = new Map;
  constructor(options) {
    super();
    this.ttlMs = (options?.ttlMinutes ?? 60) * 60 * 1000;
    this.maxEntries = options?.maxEntries ?? 250;
  }
  async load(sessionId) {
    this.evictExpired();
    const stored = this.store.get(sessionId);
    if (!stored)
      return null;
    stored.data.session.updatedAt = new Date;
    stored.expiresAt = Date.now() + this.ttlMs;
    return stored.data;
  }
  async save(snapshot) {
    this.trim(snapshot.memory.entries);
    this.store.set(snapshot.session.sessionId, {
      data: snapshot,
      expiresAt: Date.now() + this.ttlMs
    });
  }
  async summarize(session) {
    const current = await this.load(session.sessionId) ?? this.bootstrapMemory(session);
    const content = current.memory.entries.slice(-10).map((entry) => `- ${entry.type}: ${entry.content}`).join(`
`);
    current.memory.summary = content;
    current.memory.lastSummarizedAt = new Date;
    await this.save(current);
    return current.memory;
  }
  trim(entries) {
    if (entries.length <= this.maxEntries)
      return;
    entries.splice(0, entries.length - this.maxEntries);
  }
  evictExpired() {
    const now = Date.now();
    for (const [sessionId, stored] of this.store.entries()) {
      if (stored.expiresAt <= now) {
        this.store.delete(sessionId);
      }
    }
  }
}
export {
  trackMessageInMemory,
  InMemoryAgentMemory,
  BaseAgentMemoryManager
};
