feat(monitoring): dispatch monitoring tasks to desktop via AMQP outbox

Replace SSE /desktop/events with priority AMQP dispatch for monitoring
runs, and add phase1/phase2 infrastructure so desktop workers can lease,
resume, report, skip, and cancel monitoring tasks over the existing
dispatch WebSocket.

- Add monitoring collect outbox worker and phase2 desktop task fields
- Add /desktop/monitoring/tasks/{lease,resume,result,skip,cancel} routes
- Introduce execution-devtools and network-observer on desktop runtime
- Refactor runtime-controller, LoginView, and doubao adapter for the new flow
- Add channelName column to tracking views

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-22 00:24:21 +08:00
parent 749b6b99cd
commit 4142c53fa6
57 changed files with 7897 additions and 985 deletions
+139 -20
View File
@@ -1,4 +1,5 @@
import { hostname } from "node:os";
import { readFileSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { BrowserWindow, WebContentsView, app, ipcMain, nativeTheme } from "electron/main";
@@ -11,6 +12,7 @@ import type { DesktopAccountInfo, DesktopRuntimeSessionSyncRequest } from "@geo/
import { initAccountHealth } from "./account-health";
import { bindPublishAccount, openPublishAccountConsole } from "./account-binder";
import { installObservedGlobalFetch, registerObservedRequestRendererTarget } from "./network-observer";
import { initProcessMetricsSampler } from "./process-metrics";
import { getPlaywrightCDPPort, startHiddenPlaywrightReaper } from "./playwright-cdp";
import { registerRendererDevtoolsProxyTarget } from "./renderer-devtools-proxy";
@@ -38,6 +40,7 @@ app.commandLine.appendSwitch("disable-quic");
app.commandLine.appendSwitch("disable-background-networking");
app.commandLine.appendSwitch("remote-debugging-port", String(getPlaywrightCDPPort()));
app.userAgentFallback = STANDARD_USER_AGENT;
installObservedGlobalFetch();
console.info("[desktop-main] boot", {
electron: process.versions.electron,
@@ -58,6 +61,107 @@ let mainWindow: ElectronBrowserWindow | null = null;
let mainRendererContents: ElectronWebContents | null = null;
let quitReleaseInFlight = false;
type WindowMode = "login" | "main";
const WINDOW_SIZES: Record<WindowMode, { width: number; height: number; resizable: boolean }> = {
login: { width: 340, height: 540, resizable: false },
main: { width: 1320, height: 860, resizable: true },
};
function windowModePath(): string {
return join(app.getPath("userData"), "window-mode.json");
}
function readPersistedWindowMode(): WindowMode {
try {
const raw = readFileSync(windowModePath(), "utf8");
const data = JSON.parse(raw) as { mode?: unknown };
if (data?.mode === "main" || data?.mode === "login") {
return data.mode;
}
} catch {
// First run, missing file, or corrupt — fall through to default.
}
return "login";
}
function persistWindowMode(mode: WindowMode): void {
try {
writeFileSync(windowModePath(), JSON.stringify({ mode }), "utf8");
} catch (err) {
console.warn("[desktop-main] persist window mode failed", err);
}
}
let currentWindowMode: WindowMode = "login";
let windowModeApplied = false;
function applyWindowMode(window: ElectronBrowserWindow, mode: WindowMode): void {
const isFirstApply = !windowModeApplied;
const sameMode = windowModeApplied && currentWindowMode === mode;
if (!sameMode) {
currentWindowMode = mode;
persistWindowMode(mode);
const target = WINDOW_SIZES[mode];
window.setResizable(true);
window.setMinimumSize(1, 1);
window.setMaximumSize(0, 0);
window.setSize(target.width, target.height, false);
window.center();
if (mode === "login") {
window.setMinimumSize(target.width, target.height);
window.setMaximumSize(target.width, target.height);
window.setResizable(false);
} else {
window.setMinimumSize(800, 600);
window.setResizable(true);
}
}
windowModeApplied = true;
if (isFirstApply || !window.isVisible()) {
window.show();
window.focus();
}
}
function currentMainWindow(): ElectronBrowserWindow | null {
if (!mainWindow) {
return null;
}
if (mainWindow.isDestroyed()) {
mainWindow = null;
windowModeApplied = false;
return null;
}
return mainWindow;
}
async function ensureMainWindow(): Promise<ElectronBrowserWindow> {
const existing = currentMainWindow();
if (existing) {
return existing;
}
const window = await createMainWindow();
mainWindow = window;
return window;
}
async function revealMainWindow(): Promise<void> {
const window = await ensureMainWindow();
applyWindowMode(window, currentWindowMode);
}
function revealMainWindowSafely(source: string): void {
void revealMainWindow().catch((error) => {
console.error("[desktop-main] reveal main window failed", { source, error });
});
}
function rendererURL(): string | null {
return process.env.ELECTRON_RENDERER_URL ?? null;
}
@@ -83,6 +187,7 @@ async function mountRendererView(window: ElectronBrowserWindow): Promise<void> {
view.webContents.setWindowOpenHandler(() => ({ action: "deny" }));
registerRendererDevtoolsProxyTarget(view.webContents);
registerObservedRequestRendererTarget(view.webContents);
mainRendererContents = view.webContents;
window.contentView.addChildView(view);
syncViewBounds(window, view);
@@ -104,12 +209,30 @@ async function mountRendererView(window: ElectronBrowserWindow): Promise<void> {
}
async function createMainWindow(): Promise<ElectronBrowserWindow> {
const initial = WINDOW_SIZES[currentWindowMode];
const window = new BrowserWindow({
width: 1320,
height: 860,
width: initial.width,
height: initial.height,
show: false,
title: "GEO Rankly Desktop",
backgroundColor: nativeTheme.shouldUseDarkColors ? "#15191a" : "#f4f1ea",
titleBarStyle: "hiddenInset",
trafficLightPosition: { x: 12, y: 14 },
backgroundColor: nativeTheme.shouldUseDarkColors ? "#15191a" : "#eaf2ff",
});
const fallbackReveal = setTimeout(() => {
if (!windowModeApplied && !window.isDestroyed()) {
applyWindowMode(window, currentWindowMode);
}
}, 1500);
window.once("closed", () => {
clearTimeout(fallbackReveal);
if (mainWindow === window) {
mainWindow = null;
windowModeApplied = false;
}
});
await mountRendererView(window);
return window;
}
@@ -187,18 +310,23 @@ function registerBridgeHandlers(): void {
await releaseRuntimeSession({ revoke: Boolean(revoke) });
return null;
});
ipcMain.handle("desktop:set-window-mode", (_event, mode: WindowMode) => {
const window = currentMainWindow();
if (window && (mode === "login" || mode === "main")) {
applyWindowMode(window, mode);
}
return null;
});
}
if (!initSingleInstance(() => {
if (mainWindow) {
mainWindow.show();
mainWindow.focus();
}
revealMainWindowSafely("single-instance");
})) {
app.quit();
}
app.whenReady().then(async () => {
currentWindowMode = readPersistedWindowMode();
registerBridgeHandlers();
await initSessionRegistry();
initAccountHealth();
@@ -213,25 +341,16 @@ app.whenReady().then(async () => {
}
mainRendererContents.send("desktop:runtime-invalidated", event);
});
mainWindow = await createMainWindow();
await ensureMainWindow();
initTray(() => {
if (!mainWindow) {
return;
}
mainWindow.show();
mainWindow.focus();
revealMainWindowSafely("tray");
});
}).catch((error) => {
console.error("[desktop-main] app.whenReady failed", error);
});
app.on("activate", async () => {
if (mainWindow) {
mainWindow.show();
mainWindow.focus();
return;
}
mainWindow = await createMainWindow();
app.on("activate", () => {
revealMainWindowSafely("activate");
});
app.on("window-all-closed", () => {