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

4.6 KB
Raw Blame
import { existsSync, readFileSync } from "node:fs";
import { join, dirname } from "node:path";
import { fileURLToPath } from "node:url";
import { isAutostartEnabled } from "./autostart.mjs";

const __dirname = dirname(fileURLToPath(import.meta.url));

const MENU_INDEX = { STATUS: 0, DASHBOARD: 1, LOGS: 2, AUTOSTART: 3, QUIT: 4 };

export function isTraySupported() {
  const p = process.platform;
  if (!["darwin", "linux", "win32"].includes(p)) return false;
  if (p === "linux" && !process.env.DISPLAY && !process.env.WAYLAND_DISPLAY) return false;
  return true;
}

// systray2 is NOT a static dependency — it is lazily installed into
// ~/.omniroute/runtime by trayRuntime.ts (loadSystray). The previous inline
// loader called `require("module")`, which throws `ReferenceError: require is
// not defined` in this ESM file (package "type":"module"); the throw was
// silently swallowed, so the tray never appeared on macOS/Linux with no error
// printed (#4605, regressed in v3.8.34). Delegate to the runtime loader, which
// resolves systray2 from the runtime dir and surfaces install/import failures.
async function loadSystray2() {
  const { loadSystray } = await import("../runtime/trayRuntime.ts");
  return loadSystray();
}

function getIconBase64() {
  // Icon ships at bin/cli/tray/icon.png — the previous "icons/icon.png" path
  // never existed, so the tray was created with an empty icon (#4605).
  // systray2 expects an ICO payload on Windows; the PNG asset is used elsewhere.
  // (ported from #13991, credit @prabhtheone)
  const iconPath = join(__dirname, process.platform === "win32" ? "icon.ico" : "icon.png");
  if (existsSync(iconPath)) return readFileSync(iconPath).toString("base64");
  return "";
}

export async function initSystrayUnix(
  { port, onQuit, onOpenDashboard, onShowLogs },
  loadCtor = loadSystray2
) {
  const SysTray = await loadCtor();
  if (!SysTray) return null;

  const autostartEnabled = isAutostartEnabled();
  const items = [
    { title: `OmniRoute  •  port ${port}`, tooltip: "Server running", enabled: false },
    { title: "Open Dashboard", enabled: true },
    { title: "Show Logs", enabled: true },
    {
      title: autostartEnabled ? "✓ Auto-start (click to disable)" : "Enable Auto-start",
      enabled: true,
    },
    { title: "Quit OmniRoute", enabled: true },
  ];

  let tray;
  try {
    tray = new SysTray({
      menu: {
        icon: getIconBase64(),
        // isTemplateIcon must be false: icon.png is a full-color RGBA logo, and
        // macOS template mode uses only the alpha channel → a solid white square
        // (the icon looked "missing" even when the tray loaded). (PR #1080)
        isTemplateIcon: false,
        title: "",
        tooltip: `OmniRoute — port ${port}`,
        items,
      },
      debug: false,
      copyDir: false,
    });
  } catch {
    return null;
  }

  tray.onClick(async (action) => {
    if (action.seq_id === MENU_INDEX.DASHBOARD) {
      onOpenDashboard?.();
    } else if (action.seq_id === MENU_INDEX.LOGS) {
      onShowLogs?.();
    } else if (action.seq_id === MENU_INDEX.AUTOSTART) {
      const { enable, disable, isAutostartEnabled: isEnabled } = await import("./autostart.mjs");
      const wasOn = isEnabled();
      if (wasOn) disable();
      else enable();
      const nowOn = !wasOn;
      tray.sendAction({
        type: "update-item",
        item: {
          title: nowOn ? "✓ Auto-start (click to disable)" : "Enable Auto-start",
          enabled: true,
        },
        seq_id: MENU_INDEX.AUTOSTART,
      });
    } else if (action.seq_id === MENU_INDEX.QUIT) {
      onQuit?.();
    }
  });

  await tray.ready();

  return tray;
}

/**
 * Resolve the Go systray2 child subprocess PID from a tray instance.
 * systray2 exposes the spawned binary either as the `_process` field or via a
 * `process()` accessor depending on version. Returns the numeric PID or null.
 */
export function getSystrayChildPid(tray) {
  if (!tray) return null;
  try {
    const proc = tray._process || (typeof tray.process === "function" ? tray.process() : null);
    if (proc && typeof proc.pid === "number") return proc.pid;
  } catch {}
  return null;
}

export function killSystrayUnix(tray) {
  try {
    // systray2.kill(false) closes the IPC channel but leaves the Go tray binary
    // subprocess running, which keeps an orphan NSStatusItem on macOS and blocks
    // a freshly spawned tray (e.g. on respawn / hide-to-tray) from registering.
    // SIGKILL the child PID directly first, then close IPC.
    const pid = getSystrayChildPid(tray);
    if (pid) {
      try {
        process.kill(pid, "SIGKILL");
      } catch {}
    }
    tray.kill(false);
  } catch {}
}