// src/server/tools/background-process/store.ts
var MAX_PER_SESSION = 5;
var processesBySession = /* @__PURE__ */ new Map();
var logsByProcess = /* @__PURE__ */ new Map();
function getMaxPerSession() {
  return MAX_PER_SESSION;
}
function getProcess(processId, sessionId) {
  return processesBySession.get(sessionId)?.get(processId);
}
function getSessionProcesses(sessionId) {
  const sessionProcesses = processesBySession.get(sessionId);
  if (!sessionProcesses) return [];
  return Array.from(sessionProcesses.values());
}
function getSessionProcessCount(sessionId) {
  return getSessionProcesses(sessionId).filter((p) => p.status !== "exited").length;
}
function createProcess(sessionId, name, command, cwd) {
  const count = getSessionProcessCount(sessionId);
  if (count >= MAX_PER_SESSION) {
    return null;
  }
  const process2 = {
    id: crypto.randomUUID(),
    sessionId,
    name,
    command,
    cwd,
    pid: null,
    status: "pending",
    exitCode: null,
    createdAt: Date.now(),
    startedAt: null,
    endedAt: null
  };
  if (!processesBySession.has(sessionId)) {
    processesBySession.set(sessionId, /* @__PURE__ */ new Map());
  }
  processesBySession.get(sessionId).set(process2.id, process2);
  logsByProcess.set(process2.id, []);
  return process2;
}
function startProcess(processId, sessionId, pid) {
  const process2 = getProcess(processId, sessionId);
  if (!process2) return void 0;
  process2.pid = pid;
  process2.status = "running";
  process2.startedAt = Date.now();
  return process2;
}
function updateStatus(processId, sessionId, status, exitCode) {
  const process2 = getProcess(processId, sessionId);
  if (!process2) return void 0;
  process2.status = status;
  if (exitCode !== void 0) {
    process2.exitCode = exitCode;
  }
  if (status === "exited" || status === "stopping") {
    process2.endedAt = Date.now();
  }
  return process2;
}
function removeProcess(processId, sessionId) {
  const sessionProcesses = processesBySession.get(sessionId);
  if (!sessionProcesses) return false;
  const deleted = sessionProcesses.delete(processId);
  logsByProcess.delete(processId);
  return deleted;
}
function appendLog(processId, content, stream = "stdout") {
  const logs = logsByProcess.get(processId);
  if (!logs) return;
  const offset = logs.length;
  logs.push({
    offset,
    content,
    timestamp: Date.now(),
    stream
  });
}
function getLogs(processId, offset = 0, limit) {
  const logs = logsByProcess.get(processId) ?? [];
  const start = Math.min(offset, logs.length);
  const end = limit !== void 0 ? Math.min(start + limit, logs.length) : logs.length;
  return logs.slice(start, end);
}
function getLogsPaginated(processId, since = 0, maxLines = 500) {
  const logs = logsByProcess.get(processId) ?? [];
  const totalLines = logs.length;
  const filteredLogs = logs.filter((_, index) => index >= since);
  const slicedLogs = filteredLogs.slice(0, maxLines);
  const lastLine = slicedLogs[slicedLogs.length - 1];
  const nextOffset = lastLine ? lastLine.offset + 1 : totalLines;
  const hasMore = slicedLogs.length < filteredLogs.length;
  return {
    lines: slicedLogs,
    totalLines,
    nextOffset,
    hasMore
  };
}
function clearLogs(processId) {
  logsByProcess.delete(processId);
  logsByProcess.set(processId, []);
}
function getProcessById(processId) {
  for (const sessionProcesses of processesBySession.values()) {
    const proc = sessionProcesses.get(processId);
    if (proc) return proc;
  }
  return void 0;
}
function cleanupAllProcesses() {
  for (const sessionProcesses of processesBySession.values()) {
    for (const proc of sessionProcesses.values()) {
      if (proc.status === "running" && proc.pid) {
        try {
          process.kill(-proc.pid, "SIGTERM");
        } catch {
          try {
            process.kill(proc.pid, "SIGTERM");
          } catch {
          }
        }
      }
    }
  }
  processesBySession.clear();
  logsByProcess.clear();
}

export {
  getMaxPerSession,
  getProcess,
  getSessionProcesses,
  getSessionProcessCount,
  createProcess,
  startProcess,
  updateStatus,
  removeProcess,
  appendLog,
  getLogs,
  getLogsPaginated,
  clearLogs,
  getProcessById,
  cleanupAllProcesses
};
//# sourceMappingURL=chunk-VRGRAQDG.js.map