import { NextRequest, NextResponse } from "next/server"; import { listJobs, publicJob } from "@/lib/downloads"; export const dynamic = "force-dynamic"; export const runtime = "nodejs"; /** * GET /api/models/download/status * * Returns all download jobs (active + recent history). Useful for the * frontend to render a list of in-progress downloads. * * For SSE push on a single job, see /api/models/download/[id]/stream. * * Returns: { jobs: [...], activeCount: N } */ export async function GET(_req: NextRequest) { const jobs = listJobs().map(publicJob); // Sort: active first (by startedAt asc — oldest first), then finished (by finishedAt desc) const active = jobs .filter((j) => j.status === "downloading" || j.status === "queued") .sort((a, b) => a.startedAt - b.startedAt); const finished = jobs .filter((j) => j.status !== "downloading" && j.status !== "queued") .sort((a, b) => (b.finishedAt || 0) - (a.finishedAt || 0)); return NextResponse.json({ jobs: [...active, ...finished], activeCount: active.length, }); }