feat(gateway): support bun-compiled embedded gateway

This commit is contained in:
Peter Steinberger
2025-12-19 19:20:41 +01:00
parent bd63b5a231
commit bb7f4abd4b
8 changed files with 380 additions and 55 deletions

View File

@@ -3,7 +3,7 @@ import type { IncomingMessage, ServerResponse } from "node:http";
import path from "node:path";
import { fileURLToPath } from "node:url";
const UI_PREFIX = "/ui/";
const _UI_PREFIX = "/ui/";
const ROOT_PREFIX = "/";
function resolveControlUiRoot(): string | null {

View File

@@ -14,10 +14,6 @@ import {
normalizeThinkLevel,
normalizeVerboseLevel,
} from "../auto-reply/thinking.js";
import {
startBrowserControlServerFromConfig,
stopBrowserControlServer,
} from "../browser/server.js";
import {
type CanvasHostServer,
startCanvasHost,
@@ -99,6 +95,23 @@ import { sendMessageWhatsApp } from "../web/outbound.js";
import { requestReplyHeartbeatNow } from "../web/reply-heartbeat-wake.js";
import { buildMessageWithAttachments } from "./chat-attachments.js";
import { handleControlUiHttpRequest } from "./control-ui.js";
let stopBrowserControlServerIfStarted: (() => Promise<void>) | null = null;
async function startBrowserControlServerIfEnabled(): Promise<void> {
if (process.env.CLAWDIS_SKIP_BROWSER_CONTROL_SERVER === "1") return;
// Lazy import to keep optional heavyweight deps (playwright/electron) out of
// embedded/daemon builds.
const spec =
process.env.CLAWDIS_BROWSER_CONTROL_MODULE ??
// Intentionally not a static string literal so bun bundling can omit it.
// (The embedded gateway sets CLAWDIS_SKIP_BROWSER_CONTROL_SERVER=1.)
["..", "browser", "server.js"].join("/");
const mod = await import(spec);
stopBrowserControlServerIfStarted = mod.stopBrowserControlServer;
await mod.startBrowserControlServerFromConfig(defaultRuntime);
}
import {
type ConnectParams,
ErrorCodes,
@@ -4040,7 +4053,7 @@ export async function startGatewayServer(
defaultRuntime.log(`gateway log file: ${getResolvedLoggerSettings().file}`);
// Start clawd browser control server (unless disabled via config).
void startBrowserControlServerFromConfig(defaultRuntime).catch((err) => {
void startBrowserControlServerIfEnabled().catch((err) => {
logError(`gateway: clawd browser server failed to start: ${String(err)}`);
});
@@ -4110,7 +4123,9 @@ export async function startGatewayServer(
}
}
clients.clear();
await stopBrowserControlServer().catch(() => {});
if (stopBrowserControlServerIfStarted) {
await stopBrowserControlServerIfStarted().catch(() => {});
}
await Promise.allSettled(providerTasks);
await new Promise<void>((resolve) => wss.close(() => resolve()));
await new Promise<void>((resolve, reject) =>