feat(browser): add clawdis-mac browser controls

This commit is contained in:
Peter Steinberger
2025-12-13 17:05:58 +00:00
parent acf035d848
commit 86ed3de1c1
6 changed files with 162 additions and 17 deletions

View File

@@ -40,11 +40,15 @@ function jsonError(res: express.Response, status: number, message: string) {
res.status(status).json({ error: message });
}
async function fetchJson<T>(url: string, timeoutMs = 1500): Promise<T> {
async function fetchJson<T>(
url: string,
timeoutMs = 1500,
init?: RequestInit,
): Promise<T> {
const ctrl = new AbortController();
const t = setTimeout(() => ctrl.abort(), timeoutMs);
try {
const res = await fetch(url, { signal: ctrl.signal });
const res = await fetch(url, { ...init, signal: ctrl.signal });
if (!res.ok) throw new Error(`HTTP ${res.status}`);
return (await res.json()) as T;
} finally {
@@ -52,6 +56,21 @@ async function fetchJson<T>(url: string, timeoutMs = 1500): Promise<T> {
}
}
async function fetchOk(
url: string,
timeoutMs = 1500,
init?: RequestInit,
): Promise<void> {
const ctrl = new AbortController();
const t = setTimeout(() => ctrl.abort(), timeoutMs);
try {
const res = await fetch(url, { ...init, signal: ctrl.signal });
if (!res.ok) throw new Error(`HTTP ${res.status}`);
} finally {
clearTimeout(t);
}
}
async function listTabs(cdpPort: number): Promise<BrowserTab[]> {
const raw = await fetchJson<
Array<{
@@ -75,13 +94,24 @@ async function listTabs(cdpPort: number): Promise<BrowserTab[]> {
async function openTab(cdpPort: number, url: string): Promise<BrowserTab> {
const encoded = encodeURIComponent(url);
const created = await fetchJson<{
// Chrome changed /json/new to require PUT (older versions allowed GET).
type CdpTarget = {
id?: string;
title?: string;
url?: string;
webSocketDebuggerUrl?: string;
type?: string;
}>(`http://127.0.0.1:${cdpPort}/json/new?${encoded}`);
};
const endpoint = `http://127.0.0.1:${cdpPort}/json/new?${encoded}`;
const created = await fetchJson<CdpTarget>(endpoint, 1500, {
method: "PUT",
}).catch(async (err) => {
if (String(err).includes("HTTP 405")) {
return await fetchJson<CdpTarget>(endpoint, 1500);
}
throw err;
});
if (!created.id) throw new Error("Failed to open tab (missing id)");
return {
@@ -94,11 +124,13 @@ async function openTab(cdpPort: number, url: string): Promise<BrowserTab> {
}
async function activateTab(cdpPort: number, targetId: string): Promise<void> {
await fetchJson(`http://127.0.0.1:${cdpPort}/json/activate/${targetId}`);
// Chrome returns plain text ("Target activated") with an application/json content-type.
await fetchOk(`http://127.0.0.1:${cdpPort}/json/activate/${targetId}`);
}
async function closeTab(cdpPort: number, targetId: string): Promise<void> {
await fetchJson(`http://127.0.0.1:${cdpPort}/json/close/${targetId}`);
// Chrome returns plain text ("Target is closing") with an application/json content-type.
await fetchOk(`http://127.0.0.1:${cdpPort}/json/close/${targetId}`);
}
async function ensureBrowserAvailable(runtime: RuntimeEnv): Promise<void> {

View File

@@ -11,6 +11,7 @@ import {
browserTabs,
resolveBrowserControlUrl,
} from "../browser/client.js";
import { runClawdisMac } from "../infra/clawdis-mac.js";
import { agentCommand } from "../commands/agent.js";
import { healthCommand } from "../commands/health.js";
import { sendCommand } from "../commands/send.js";
@@ -223,6 +224,44 @@ Examples:
registerGatewayCli(program);
registerNodesCli(program);
registerCronCli(program);
program
.command("ui")
.description("macOS UI automation via Clawdis.app (PeekabooBridge)")
.option("--json", "Output JSON (passthrough from clawdis-mac)", false)
.allowUnknownOption(true)
.passThroughOptions()
.argument(
"<uiArgs...>",
"Args passed through to: clawdis-mac ui <command> ...",
)
.addHelpText(
"after",
`
Examples:
clawdis ui permissions status
clawdis ui frontmost
clawdis ui screenshot
clawdis ui see --bundle-id com.apple.Safari
clawdis ui click --bundle-id com.apple.Safari --on B1
clawdis ui --json see --bundle-id com.apple.Safari
`,
)
.action(async (uiArgs: string[], opts) => {
try {
const res = await runClawdisMac(["ui", ...uiArgs], {
json: Boolean(opts.json),
timeoutMs: 45_000,
});
if (res.stdout) process.stdout.write(res.stdout);
if (res.stderr) process.stderr.write(res.stderr);
defaultRuntime.exit(res.code ?? 1);
} catch (err) {
defaultRuntime.error(danger(String(err)));
defaultRuntime.exit(1);
}
});
program
.command("status")
.description("Show web session health and recent session recipients")

66
src/infra/clawdis-mac.ts Normal file
View File

@@ -0,0 +1,66 @@
import fs from "node:fs";
import path from "node:path";
import { runCommandWithTimeout, runExec } from "../process/exec.js";
import { defaultRuntime, type RuntimeEnv } from "../runtime.js";
export type ClawdisMacExecResult = {
stdout: string;
stderr: string;
code: number | null;
};
function isFileExecutable(p: string): boolean {
try {
const stat = fs.statSync(p);
if (!stat.isFile()) return false;
fs.accessSync(p, fs.constants.X_OK);
return true;
} catch {
return false;
}
}
export async function resolveClawdisMacBinary(
runtime: RuntimeEnv = defaultRuntime,
): Promise<string> {
if (process.platform !== "darwin") {
runtime.error("clawdis-mac is only available on macOS.");
runtime.exit(1);
}
const override = process.env.CLAWDIS_MAC_BIN?.trim();
if (override) return override;
try {
const { stdout } = await runExec("which", ["clawdis-mac"], 2000);
const resolved = stdout.trim();
if (resolved) return resolved;
} catch {
// fall through
}
const local = path.resolve(process.cwd(), "bin", "clawdis-mac");
if (isFileExecutable(local)) return local;
runtime.error(
"Missing required binary: clawdis-mac. Install the Clawdis mac app/CLI helper (or set CLAWDIS_MAC_BIN).",
);
runtime.exit(1);
}
export async function runClawdisMac(
args: string[],
opts?: { json?: boolean; timeoutMs?: number; runtime?: RuntimeEnv },
): Promise<ClawdisMacExecResult> {
const runtime = opts?.runtime ?? defaultRuntime;
const cmd = await resolveClawdisMacBinary(runtime);
const argv: string[] = [cmd];
if (opts?.json) argv.push("--json");
argv.push(...args);
const res = await runCommandWithTimeout(argv, opts?.timeoutMs ?? 30_000);
return { stdout: res.stdout, stderr: res.stderr, code: res.code };
}