Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

14.0 KB
Raw Blame
/**
 * GET  /api/system/version  — Returns current version and latest available on npm
 * POST /api/system/version  — Triggers a deployment-aware background update
 *
 * Security: Requires admin authentication (same as other management routes).
 * Safety: Update only runs if a newer version is available on npm.
 */
import { createHash } from "node:crypto";
import { NextRequest, NextResponse } from "next/server";
import { execFile } from "child_process";
import { promisify } from "util";
import { isAuthenticated } from "@/shared/utils/apiAuth";
import {
  ensureGitTagExists,
  getAutoUpdateConfig,
  launchAutoUpdate,
  validateAutoUpdateRuntime,
  PROJECT_ROOT,
} from "@/lib/system/autoUpdate";
import { NEWS_JSON_URL, parseActiveNewsPayload } from "@/shared/utils/releaseNotes";
import {
  clearLatestVersionCache,
  isNewer,
  resolveDistTagsCached,
  resolveLatestVersionCached,
} from "@/lib/system/versionCheck";
import { describeReleaseChannels } from "@/lib/system/releaseChannel";
import { resolveGlobalOmniroutePath } from "@/lib/system/globalPackagePath";
import { restartRunningServer } from "@/lib/system/processManagerRestart";
import { APP_CONFIG } from "@/shared/constants/appConfig";
// #5542 — On Windows npm is `npm.cmd`; Node ≥24 refuses to execFile a `.cmd` without
// a shell (nodejs/node#52554 → "spawn npm ENOENT"). buildNpmExecOptions enables the
// shell on win32 only; SERVICE_VERSION_PATTERN keeps the shell-joined version safe.
import { buildNpmExecOptions, SERVICE_VERSION_PATTERN } from "@/lib/services/installers/utils";

const execFileAsync = promisify(execFile);

export const dynamic = "force-dynamic";

function getCurrentVersion(): string {
  return APP_CONFIG.version;
}

/**
 * Shared restart step for both npm-mode update flows (source-checkout and global-install
 * below). #11885: this used to hardcode `pm2 restart omniroute` in each branch separately
 * and silently report "skipped" — reading like a completed update — whenever pm2 wasn't
 * the process manager. `restartRunningServer()` tries OmniRoute's own PID-file-managed
 * supervisor first, then pm2, and this wrapper turns its honest "restart-required" outcome
 * into an SSE step the dashboard renders as a warning instead of a false "done".
 */
async function sendRestartStep(send: (data: Record<string, unknown>) => void): Promise<void> {
  send({ step: "restart", status: "running", message: "Restarting service..." });
  const outcome = await restartRunningServer();
  send({ step: "restart", status: outcome.status, message: outcome.message });
}

async function getNews() {
  try {
    const res = await fetch(NEWS_JSON_URL, { next: { revalidate: 3600 } });
    if (!res.ok) return null;
    const data = await res.json();
    return parseActiveNewsPayload(data);
  } catch {
    return null;
  }
}

export async function GET(req: NextRequest) {
  if (!(await isAuthenticated(req))) {
    return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  }

  const current = getCurrentVersion();
  const config = getAutoUpdateConfig();

  const cacheControl = req.headers.get("Cache-Control") ?? "";
  const cacheOptions = {
    bypassCache: /(?:^|,)\s*(?:no-cache|no-store)\b/i.test(cacheControl),
    storeResult: !/(?:^|,)\s*no-store\b/i.test(cacheControl),
  };
  const [latest, distTags, news, validation] = await Promise.all([
    resolveLatestVersionCached(cacheOptions),
    resolveDistTagsCached(cacheOptions),
    getNews(),
    validateAutoUpdateRuntime(config),
  ]);
  // Additive (rail 3.8.54): `channel` keeps meaning the deployment mode
  // (npm / source / docker-compose) the dashboard updater relies on; the npm
  // release channel of the running build is `releaseChannel`.
  const { releaseChannel, channels } = describeReleaseChannels(current, distTags, latest);

  const body = {
    current,
    latest: latest ?? "unavailable",
    updateAvailable: isNewer(latest, current),
    channel: config.mode,
    autoUpdateSupported: validation.supported,
    autoUpdateError: validation.reason,
    // Host-side compose command for a published-image container (`docker-image` mode).
    manualUpdateCommand: config.manualUpdateCommand,
    news,
    releaseChannel,
    channels,
  };
  const serialized = JSON.stringify(body);
  const etag = `"${createHash("sha256").update(serialized).digest("base64url")}"`;
  const headers = { "Cache-Control": "private, no-cache, must-revalidate", ETag: etag };
  const validators = req.headers
    .get("If-None-Match")
    ?.split(",")
    .map((value) => value.trim());
  if (validators?.some((value) => value === etag || value === `W/${etag}`)) {
    return new NextResponse(null, { status: 304, headers });
  }
  return new NextResponse(serialized, {
    headers: { ...headers, "Content-Type": "application/json" },
  });
}

export async function POST(req: NextRequest) {
  if (!(await isAuthenticated(req))) {
    return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  }

  const current = getCurrentVersion();
  const latest = await resolveLatestVersionCached({ bypassCache: true });

  if (!latest) {
    return NextResponse.json(
      { success: false, error: "Could not reach npm registry" },
      { status: 503 }
    );
  }

  const resolvedTargetTag = latest.startsWith("v") ? latest : `v${latest}`;

  if (!isNewer(latest, current)) {
    return NextResponse.json({
      success: false,
      error: `Already on latest version (${current})`,
      current,
      latest,
    });
  }

  const config = getAutoUpdateConfig();
  const validation = await validateAutoUpdateRuntime(config);

  if (!validation.supported) {
    return NextResponse.json(
      {
        success: false,
        error: validation.reason || "Auto-update is not supported in this environment.",
      },
      { status: 400 }
    );
  }

  // Socket-free Docker image update: drop a request file in DATA_DIR for the host watcher,
  // which pulls the image and recreates the container. The dashboard then polls GET.
  if (config.mode === "docker-trigger") {
    const launched = await launchAutoUpdate({ latest });
    if (!launched.started) {
      return NextResponse.json(
        {
          success: false,
          error: launched.error || "Failed to request the Docker update.",
          channel: launched.channel,
        },
        { status: 500 }
      );
    }

    clearLatestVersionCache();
    return NextResponse.json(
      {
        success: true,
        message: `Update to v${latest} requested. The Docker host is pulling the new image.`,
        from: current,
        to: latest,
        channel: launched.channel,
      },
      { status: 202 }
    );
  }

  // If we are in docker-compose mode, use the detached shell script background updates
  if (config.mode === "docker-compose") {
    const launched = await launchAutoUpdate({ latest });
    if (!launched.started) {
      return NextResponse.json(
        {
          success: false,
          error: launched.error || "Failed to start auto-update.",
          channel: launched.channel,
          logPath: launched.logPath,
        },
        { status: 503 }
      );
    }

    clearLatestVersionCache();
    return NextResponse.json({
      success: true,
      message: `Update to v${latest} started. Docker rebuild is running in the background.`,
      from: current,
      to: latest,
      channel: launched.channel,
      logPath: launched.logPath,
    });
  }

  if (config.mode === "source") {
    const encoder = new TextEncoder();
    const stream = new ReadableStream({
      async start(controller) {
        const send = (data: Record<string, unknown>) => {
          controller.enqueue(encoder.encode(`data: ${JSON.stringify(data)}\n\n`));
        };

        try {
          send({
            step: "install",
            status: "running",
            message: `Fetching latest tags from ${config.gitRemote}...`,
          });
          await execFileAsync("git", ["fetch", "--tags", config.gitRemote], {
            timeout: 60_000,
            cwd: PROJECT_ROOT,
          });
          send({ step: "install", status: "done", message: "Tags fetched" });

          send({
            step: "install",
            status: "running",
            message: `Validating ${resolvedTargetTag}...`,
          });
          await ensureGitTagExists(resolvedTargetTag, execFileAsync, PROJECT_ROOT);
          send({
            step: "install",
            status: "done",
            message: `Validated ${resolvedTargetTag}`,
          });

          send({
            step: "install",
            status: "running",
            message: `Checking out ${resolvedTargetTag}...`,
          });
          try {
            await execFileAsync("git", ["stash", "--include-untracked"], {
              timeout: 30_000,
              cwd: PROJECT_ROOT,
            });
          } catch {
            // No local changes to stash.
          }

          const shortHead = (
            await execFileAsync("git", ["rev-parse", "--short", "HEAD"], {
              timeout: 10_000,
              cwd: PROJECT_ROOT,
            })
          ).stdout.trim();
          const backupBranch = `pre-update/${shortHead}-${new Date().toISOString().replace(/[:.]/g, "-")}`;

          try {
            await execFileAsync("git", ["branch", backupBranch], {
              timeout: 10_000,
              cwd: PROJECT_ROOT,
            });
          } catch {
            // Backup branch is best-effort only.
          }

          await execFileAsync("git", ["checkout", resolvedTargetTag], {
            timeout: 30_000,
            cwd: PROJECT_ROOT,
          });
          send({ step: "install", status: "done", message: `Checked out ${resolvedTargetTag}` });

          send({
            step: "rebuild",
            status: "running",
            message: "Installing dependencies...",
          });
          await execFileAsync(
            "npm",
            ["install", "--legacy-peer-deps"],
            buildNpmExecOptions(process.platform, { cwd: PROJECT_ROOT, timeoutMs: 300_000 })
          );
          send({ step: "rebuild", status: "done", message: "Dependencies installed" });

          try {
            await execFileAsync("node", ["scripts/dev/sync-env.mjs"], {
              timeout: 15_000,
              cwd: PROJECT_ROOT,
            });
          } catch {
            // .env sync is non-fatal during update.
          }

          send({
            step: "rebuild",
            status: "running",
            message: "Building application...",
          });
          await execFileAsync(
            "npm",
            ["run", "build"],
            buildNpmExecOptions(process.platform, { cwd: PROJECT_ROOT, timeoutMs: 600_000 })
          );
          send({ step: "rebuild", status: "done", message: "Build complete" });

          await sendRestartStep(send);

          send({
            step: "complete",
            status: "done",
            from: current,
            to: latest,
            message: `Update to ${resolvedTargetTag} complete!`,
          });
          console.log(`[AutoUpdate] Successfully updated to ${resolvedTargetTag} via source mode`);
        } catch (err: any) {
          const errMsg = err?.stderr || err?.message || String(err);
          send({ step: "error", status: "failed", message: errMsg });
          console.error("[AutoUpdate] Source update failed:", err);
        } finally {
          controller.close();
        }
      },
    });

    return new Response(stream, {
      headers: {
        "Content-Type": "text/event-stream",
        "Cache-Control": "no-cache",
        Connection: "keep-alive",
      },
    });
  }

  // Stream progress events so the frontend can show real-time status for NPM/PM2 mode
  const encoder = new TextEncoder();
  const stream = new ReadableStream({
    async start(controller) {
      const send = (data: Record<string, unknown>) => {
        controller.enqueue(encoder.encode(`data: ${JSON.stringify(data)}\n\n`));
      };

      try {
        // Step 1: Install
        // #5542 — buildNpmExecOptions enables the shell on win32 (npm.cmd), which
        // shell-joins argv, so the version spec must be metacharacter-free before it
        // reaches the command line (Hard Rule #13).
        if (!SERVICE_VERSION_PATTERN.test(latest)) {
          send({ step: "install", status: "error", message: "Invalid version format" });
          controller.close();
          return;
        }
        send({ step: "install", status: "running", message: `Installing omniroute@${latest}...` });
        await execFileAsync(
          "npm",
          ["install", "-g", `omniroute@${latest}`, "--ignore-scripts", "--legacy-peer-deps"],
          buildNpmExecOptions(process.platform, { cwd: PROJECT_ROOT, timeoutMs: 300_000 })
        );
        send({ step: "install", status: "done", message: `Installed omniroute@${latest}` });

        // Step 2: Rebuild native modules (critical for better-sqlite3)
        send({
          step: "rebuild",
          status: "running",
          message: "Rebuilding native modules (better-sqlite3)...",
        });
        const omniPath = await resolveGlobalOmniroutePath();
        await execFileAsync(
          "npm",
          ["rebuild", "better-sqlite3"],
          buildNpmExecOptions(process.platform, { cwd: omniPath, timeoutMs: 120_000 })
        );
        send({ step: "rebuild", status: "done", message: "Native modules rebuilt" });

        // Step 3: Restart
        await sendRestartStep(send);

        clearLatestVersionCache();
        send({
          step: "complete",
          status: "done",
          from: current,
          to: latest,
          message: `Update to v${latest} complete!`,
        });
        console.log(`[AutoUpdate] Successfully updated to v${latest}`);
      } catch (err: any) {
        const errMsg = err?.stderr || err?.message || String(err);
        send({ step: "error", status: "failed", message: errMsg });
        console.error(`[AutoUpdate] Update failed:`, err);
      } finally {
        controller.close();
      }
    },
  });

  return new Response(stream, {
    headers: {
      "Content-Type": "text/event-stream",
      "Cache-Control": "no-cache",
      Connection: "keep-alive",
    },
  });
}