Skip to content

A desktop coding agent with free access to frontier models.

TypeScript
4.5 KB
Raw Blame
import { execFileSync } from "node:child_process";
import { existsSync, readFileSync } from "node:fs";
import os from "node:os";
import { resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import tailwindcss from "@tailwindcss/vite";

const portValue = Number.parseInt(process.env.PORT ?? "", 10);
const devPort = Number.isFinite(portValue) && portValue > 0 ? portValue : 5173;
const allowedHosts = new Set<string>();
const envAllowedHosts = process.env.VITE_ALLOWED_HOSTS ?? "";

const addHost = (value?: string | null) => {
  const trimmed = value?.trim();
  if (!trimmed) return;
  allowedHosts.add(trimmed);
};

envAllowedHosts.split(",").forEach(addHost);
addHost(process.env.OMNIRUSH_PUBLIC_HOST ?? null);
const hostname = os.hostname();
addHost(hostname);
const shortHostname = hostname.split(".")[0];
if (shortHostname && shortHostname !== hostname) {
  addHost(shortHostname);
}
const appRoot = resolve(fileURLToPath(new URL(".", import.meta.url)));
const appPackagePath = resolve(appRoot, "package.json");
const desktopPackagePath = resolve(appRoot, "..", "desktop", "package.json");

function firstNonEmpty(values: Array<string | null | undefined>): string | null {
  for (const value of values) {
    const trimmed = value?.trim();
    if (trimmed) return trimmed;
  }

  return null;
}

function readLocalGitSha(): string | null {
  try {
    const output = execFileSync("git", ["rev-parse", "HEAD"], {
      cwd: appRoot,
      encoding: "utf8",
      stdio: "pipe",
    });
    return output.trim() || null;
  } catch {
    return null;
  }
}

function readPackageVersion(packagePath: string): string | null {
  if (!existsSync(packagePath)) return null;

  const parsed = JSON.parse(readFileSync(packagePath, "utf8")) as { version?: string };
  return parsed.version?.trim() || null;
}

const buildAppVersion =
  process.env.VITE_OMNIRUSH_APP_VERSION?.trim() ||
  readPackageVersion(desktopPackagePath) ||
  readPackageVersion(appPackagePath) ||
  "0.0.0";
const buildSha = firstNonEmpty([
  process.env.VITE_OMNIRUSH_BUILD_SHA,
  process.env.OMNIRUSH_GIT_SHA,
  process.env.GITHUB_SHA,
]) ?? readLocalGitSha();
const shortBuildSha = buildSha ? buildSha.slice(0, 7) : "";

// Electron packaged builds load index.html via `file://`, so asset URLs
// must be relative. Tauri serves via its own protocol so absolute paths
// work there. Gate on an env var the electron build script sets.
const isElectronPackagedBuild = process.env.OMNIRUSH_ELECTRON_BUILD === "1";

// Headless-web dev (scripts/dev-headless-web.ts): serve /api/den same-origin
// from the dev server, proxied to the Den control plane, so the browser never
// issues cross-origin Den API calls (Den does not serve CORS). The app is
// pointed here via VITE_DEN_API_BASE_URL; sign-in still opens the real Den
// web app. Inert unless the launcher sets the target env. No gateway marker:
// that runtime implies a provisioned cloud instance, which local dev lacks.
const headlessDenTarget = (process.env.OMNIRUSH_DEV_HEADLESS_DEN_TARGET ?? "").trim();

export default defineConfig({
  base: isElectronPackagedBuild ? "./" : "/",
  define: {
    "import.meta.env.VITE_OMNIRUSH_APP_VERSION": JSON.stringify(buildAppVersion),
    "import.meta.env.VITE_OMNIRUSH_BUILD_SHA": JSON.stringify(shortBuildSha),
    // Analytics send only from a packaged release build (analytics-key.ts).
    "import.meta.env.VITE_OMNIRUSH_PACKAGED_BUILD": JSON.stringify(isElectronPackagedBuild ? "1" : ""),
  },
  plugins: [
    {
      name: "omnirush-dev-server-id",
      configureServer(server) {
        server.middlewares.use("/__omnirush_dev_server_id", (_req, res) => {
          res.setHeader("Content-Type", "application/json");
          res.end(JSON.stringify({ appRoot }));
        });
      },
    },
    tailwindcss(),
    react({
      babel: {
        plugins: [["babel-plugin-react-compiler", { compilationMode: "annotation" }]],
      },
    }),
  ],
  server: {
    port: devPort,
    strictPort: true,
    ...(allowedHosts.size > 0 ? { allowedHosts: Array.from(allowedHosts) } : {}),
    ...(headlessDenTarget
      ? {
          proxy: {
            "/api/den": { target: headlessDenTarget, changeOrigin: true },
          },
        }
      : {}),
  },
  build: {
    target: "esnext",
    rollupOptions: {
      input: {
        app: resolve(appRoot, "index.html"),
        overlay: resolve(appRoot, "overlay.html"),
      },
    },
  },
  resolve: {
    alias: {
      "@": resolve(appRoot, "src"),
    },
  },
});