2025-12-13 15:15:09 +00:00
|
|
|
import type { Server } from "node:http";
|
|
|
|
|
import express from "express";
|
2026-02-01 10:03:47 +09:00
|
|
|
import type { BrowserRouteRegistrar } from "./routes/types.js";
|
2025-12-13 15:15:09 +00:00
|
|
|
import { loadConfig } from "../config/config.js";
|
2026-01-18 23:24:17 +00:00
|
|
|
import { createSubsystemLogger } from "../logging/subsystem.js";
|
2026-01-27 03:23:42 +00:00
|
|
|
import { resolveBrowserConfig, resolveProfile } from "./config.js";
|
2026-01-15 04:50:11 +00:00
|
|
|
import { ensureChromeExtensionRelayServer } from "./extension-relay.js";
|
2025-12-19 23:57:26 +00:00
|
|
|
import { registerBrowserRoutes } from "./routes/index.js";
|
2026-01-14 14:31:43 +00:00
|
|
|
import { type BrowserServerState, createBrowserRouteContext } from "./server-context.js";
|
2025-12-13 15:15:09 +00:00
|
|
|
|
|
|
|
|
let state: BrowserServerState | null = null;
|
2025-12-21 13:23:51 +00:00
|
|
|
const log = createSubsystemLogger("browser");
|
|
|
|
|
const logServer = log.child("server");
|
2025-12-13 15:15:09 +00:00
|
|
|
|
2025-12-21 13:58:27 +00:00
|
|
|
export async function startBrowserControlServerFromConfig(): Promise<BrowserServerState | null> {
|
2026-01-31 16:19:20 +09:00
|
|
|
if (state) {
|
|
|
|
|
return state;
|
|
|
|
|
}
|
2025-12-13 15:15:09 +00:00
|
|
|
|
|
|
|
|
const cfg = loadConfig();
|
2026-01-27 03:23:42 +00:00
|
|
|
const resolved = resolveBrowserConfig(cfg.browser, cfg);
|
2026-01-31 16:19:20 +09:00
|
|
|
if (!resolved.enabled) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2025-12-13 15:15:09 +00:00
|
|
|
|
|
|
|
|
const app = express();
|
|
|
|
|
app.use(express.json({ limit: "1mb" }));
|
|
|
|
|
|
2025-12-19 23:57:26 +00:00
|
|
|
const ctx = createBrowserRouteContext({
|
|
|
|
|
getState: () => state,
|
2025-12-13 18:48:55 +00:00
|
|
|
});
|
2026-01-27 03:23:42 +00:00
|
|
|
registerBrowserRoutes(app as unknown as BrowserRouteRegistrar, ctx);
|
2025-12-13 18:48:55 +00:00
|
|
|
|
2025-12-13 15:15:09 +00:00
|
|
|
const port = resolved.controlPort;
|
|
|
|
|
const server = await new Promise<Server>((resolve, reject) => {
|
|
|
|
|
const s = app.listen(port, "127.0.0.1", () => resolve(s));
|
|
|
|
|
s.once("error", reject);
|
|
|
|
|
}).catch((err) => {
|
2026-01-30 03:15:10 +01:00
|
|
|
logServer.error(`openclaw browser server failed to bind 127.0.0.1:${port}: ${String(err)}`);
|
2025-12-13 15:15:09 +00:00
|
|
|
return null;
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-31 16:19:20 +09:00
|
|
|
if (!server) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2025-12-13 15:15:09 +00:00
|
|
|
|
|
|
|
|
state = {
|
|
|
|
|
server,
|
|
|
|
|
port,
|
|
|
|
|
resolved,
|
2026-01-04 03:32:40 +00:00
|
|
|
profiles: new Map(),
|
2025-12-13 15:15:09 +00:00
|
|
|
};
|
|
|
|
|
|
2026-01-15 04:50:11 +00:00
|
|
|
// If any profile uses the Chrome extension relay, start the local relay server eagerly
|
|
|
|
|
// so the extension can connect before the first browser action.
|
|
|
|
|
for (const name of Object.keys(resolved.profiles)) {
|
|
|
|
|
const profile = resolveProfile(resolved, name);
|
2026-01-31 16:19:20 +09:00
|
|
|
if (!profile || profile.driver !== "extension") {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2026-01-15 05:17:03 +00:00
|
|
|
await ensureChromeExtensionRelayServer({ cdpUrl: profile.cdpUrl }).catch((err) => {
|
|
|
|
|
logServer.warn(`Chrome extension relay init failed for profile "${name}": ${String(err)}`);
|
|
|
|
|
});
|
2026-01-15 04:50:11 +00:00
|
|
|
}
|
|
|
|
|
|
2025-12-21 17:36:24 +00:00
|
|
|
logServer.info(`Browser control listening on http://127.0.0.1:${port}/`);
|
2025-12-13 15:15:09 +00:00
|
|
|
return state;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-21 13:58:27 +00:00
|
|
|
export async function stopBrowserControlServer(): Promise<void> {
|
2025-12-13 15:15:09 +00:00
|
|
|
const current = state;
|
2026-01-31 16:19:20 +09:00
|
|
|
if (!current) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-12-19 23:57:26 +00:00
|
|
|
|
|
|
|
|
const ctx = createBrowserRouteContext({
|
|
|
|
|
getState: () => state,
|
|
|
|
|
});
|
|
|
|
|
|
2025-12-13 15:15:09 +00:00
|
|
|
try {
|
2026-01-04 03:32:40 +00:00
|
|
|
const current = state;
|
|
|
|
|
if (current) {
|
|
|
|
|
for (const name of Object.keys(current.resolved.profiles)) {
|
|
|
|
|
try {
|
|
|
|
|
await ctx.forProfile(name).stopRunningBrowser();
|
|
|
|
|
} catch {
|
|
|
|
|
// ignore
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-12-19 23:57:26 +00:00
|
|
|
} catch (err) {
|
2026-01-30 03:15:10 +01:00
|
|
|
logServer.warn(`openclaw browser stop failed: ${String(err)}`);
|
2025-12-13 15:15:09 +00:00
|
|
|
}
|
2025-12-19 23:57:26 +00:00
|
|
|
|
2026-01-27 03:23:42 +00:00
|
|
|
if (current.server) {
|
|
|
|
|
await new Promise<void>((resolve) => {
|
|
|
|
|
current.server?.close(() => resolve());
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-12-19 23:57:26 +00:00
|
|
|
state = null;
|
2025-12-20 19:16:56 +00:00
|
|
|
|
|
|
|
|
// Optional: Playwright is not always available (e.g. embedded gateway builds).
|
|
|
|
|
try {
|
|
|
|
|
const mod = await import("./pw-ai.js");
|
|
|
|
|
await mod.closePlaywrightBrowserConnection();
|
|
|
|
|
} catch {
|
|
|
|
|
// ignore
|
|
|
|
|
}
|
2025-12-13 15:15:09 +00:00
|
|
|
}
|