import { v as validateAuth } from './auth-DuunT7Cg.js';
import { g as getProviderConversation, l as listAllProviderProjects } from './registry-D4J_CuzW.js';
import { j as json } from './index-CoD1IJuy.js';
import './shared-server-DaWdgxVh.js';
import 'crypto';
import './qwen-reader-DGfUbKaJ.js';
import 'fs';
import 'os';
import 'path';
import 'better-sqlite3';

let cachedProjects = null;
let cacheTimestamp = 0;
const CACHE_TTL_MS = 5e3;
function getMergedProjects() {
  const now = Date.now();
  if (cachedProjects && now - cacheTimestamp < CACHE_TTL_MS) {
    return cachedProjects;
  }
  cachedProjects = listAllProviderProjects();
  cacheTimestamp = now;
  return cachedProjects;
}
const GET = ({ request, url }) => {
  const authError = validateAuth(request);
  if (authError) {
    return authError;
  }
  if (url.searchParams.get("refresh") === "true") {
    cachedProjects = null;
    cacheTimestamp = 0;
  }
  const sessionId = url.searchParams.get("id");
  if (sessionId) {
    const projectId = url.searchParams.get("project") || "";
    const rawOffset = parseInt(url.searchParams.get("offset") || "0");
    const rawLimit = parseInt(url.searchParams.get("limit") || "200");
    const offset2 = Number.isFinite(rawOffset) && rawOffset >= 0 ? rawOffset : 0;
    const limit2 = Number.isFinite(rawLimit) && rawLimit >= 1 ? Math.min(rawLimit, 500) : 200;
    const allProjects2 = getMergedProjects();
    const matchesProject = (p) => p.id === projectId || p.fullPath === projectId;
    const matchedProject = projectId ? allProjects2.find(matchesProject) : void 0;
    const claudeProjectDir = matchedProject ? matchedProject.fullPath.replace(/\//g, "-") : void 0;
    const messages = getProviderConversation(sessionId, offset2, limit2, claudeProjectDir);
    let sessionInfo = matchedProject?.sessions.find((s) => s.id === sessionId) ?? null;
    if (!sessionInfo) {
      for (const project of allProjects2) {
        if (projectId && !matchesProject(project)) {
          continue;
        }
        const found = project.sessions.find((s) => s.id === sessionId);
        if (found) {
          sessionInfo = found;
          break;
        }
      }
    }
    if (!sessionInfo) {
      return json({ error: "Session not found" }, { status: 404 });
    }
    return json({
      messages,
      session: sessionInfo,
      timestamp: (/* @__PURE__ */ new Date()).toISOString()
    });
  }
  const allProjects = getMergedProjects();
  const limit = parseInt(url.searchParams.get("limit") || "0");
  const offset = parseInt(url.searchParams.get("offset") || "0");
  const total = allProjects.length;
  const paginatedProjects = limit > 0 ? allProjects.slice(offset, offset + limit) : allProjects;
  return json({
    count: allProjects.reduce((sum, p) => sum + p.sessionCount, 0),
    projects: paginatedProjects,
    timestamp: (/* @__PURE__ */ new Date()).toISOString(),
    total
  });
};

export { GET };
//# sourceMappingURL=_server.ts-D9ir7u24.js.map
