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

@@ -38,7 +38,7 @@ targets:
swiftlint lint --config "$SRCROOT/.swiftlint.yml" swiftlint lint --config "$SRCROOT/.swiftlint.yml"
settings: settings:
base: base:
PRODUCT_BUNDLE_IDENTIFIER: com.steipete.clawdis.node PRODUCT_BUNDLE_IDENTIFIER: com.steipete.clawdis.ios
SWIFT_VERSION: "6.0" SWIFT_VERSION: "6.0"
info: info:
path: Sources/Info.plist path: Sources/Info.plist

View File

@@ -62,17 +62,17 @@ enum BrowserCLI {
case "start": case "start":
self.printResult( self.printResult(
jsonOutput: jsonOutput, jsonOutput: jsonOutput,
res: try await self.httpJSON(method: "POST", url: baseURL.appendingPathComponent("/start"))) res: try await self.httpJSON(method: "POST", url: baseURL.appendingPathComponent("/start"), timeoutInterval: 15.0))
return 0 return 0
case "stop": case "stop":
self.printResult( self.printResult(
jsonOutput: jsonOutput, jsonOutput: jsonOutput,
res: try await self.httpJSON(method: "POST", url: baseURL.appendingPathComponent("/stop"))) res: try await self.httpJSON(method: "POST", url: baseURL.appendingPathComponent("/stop"), timeoutInterval: 15.0))
return 0 return 0
case "tabs": case "tabs":
let res = try await self.httpJSON(method: "GET", url: baseURL.appendingPathComponent("/tabs")) let res = try await self.httpJSON(method: "GET", url: baseURL.appendingPathComponent("/tabs"), timeoutInterval: 3.0)
if jsonOutput { if jsonOutput {
self.printJSON(ok: true, result: res) self.printJSON(ok: true, result: res)
} else { } else {
@@ -90,7 +90,8 @@ enum BrowserCLI {
res: try await self.httpJSON( res: try await self.httpJSON(
method: "POST", method: "POST",
url: baseURL.appendingPathComponent("/tabs/open"), url: baseURL.appendingPathComponent("/tabs/open"),
body: ["url": url])) body: ["url": url],
timeoutInterval: 15.0))
return 0 return 0
case "focus": case "focus":
@@ -103,7 +104,8 @@ enum BrowserCLI {
res: try await self.httpJSON( res: try await self.httpJSON(
method: "POST", method: "POST",
url: baseURL.appendingPathComponent("/tabs/focus"), url: baseURL.appendingPathComponent("/tabs/focus"),
body: ["targetId": id])) body: ["targetId": id],
timeoutInterval: 5.0))
return 0 return 0
case "close": case "close":
@@ -115,7 +117,8 @@ enum BrowserCLI {
jsonOutput: jsonOutput, jsonOutput: jsonOutput,
res: try await self.httpJSON( res: try await self.httpJSON(
method: "DELETE", method: "DELETE",
url: baseURL.appendingPathComponent("/tabs/\(id)"))) url: baseURL.appendingPathComponent("/tabs/\(id)"),
timeoutInterval: 5.0))
return 0 return 0
case "screenshot": case "screenshot":
@@ -130,7 +133,7 @@ enum BrowserCLI {
if !items.isEmpty { if !items.isEmpty {
url = self.withQuery(url, items: items) url = self.withQuery(url, items: items)
} }
let res = try await self.httpJSON(method: "GET", url: url) let res = try await self.httpJSON(method: "GET", url: url, timeoutInterval: 20.0)
if jsonOutput { if jsonOutput {
self.printJSON(ok: true, result: res) self.printJSON(ok: true, result: res)
} else if let path = res["path"] as? String, !path.isEmpty { } else if let path = res["path"] as? String, !path.isEmpty {
@@ -173,8 +176,13 @@ enum BrowserCLI {
return components.url ?? url return components.url ?? url
} }
private static func httpJSON(method: String, url: URL, body: [String: Any]? = nil) async throws -> [String: Any] { private static func httpJSON(
var req = URLRequest(url: url, timeoutInterval: 2.0) method: String,
url: URL,
body: [String: Any]? = nil,
timeoutInterval: TimeInterval = 2.0
) async throws -> [String: Any] {
var req = URLRequest(url: url, timeoutInterval: timeoutInterval)
req.httpMethod = method req.httpMethod = method
if let body { if let body {
req.setValue("application/json", forHTTPHeaderField: "Content-Type") req.setValue("application/json", forHTTPHeaderField: "Content-Type")

View File

@@ -40,11 +40,15 @@ function jsonError(res: express.Response, status: number, message: string) {
res.status(status).json({ error: message }); 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 ctrl = new AbortController();
const t = setTimeout(() => ctrl.abort(), timeoutMs); const t = setTimeout(() => ctrl.abort(), timeoutMs);
try { 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}`); if (!res.ok) throw new Error(`HTTP ${res.status}`);
return (await res.json()) as T; return (await res.json()) as T;
} finally { } 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[]> { async function listTabs(cdpPort: number): Promise<BrowserTab[]> {
const raw = await fetchJson< const raw = await fetchJson<
Array<{ Array<{
@@ -75,13 +94,24 @@ async function listTabs(cdpPort: number): Promise<BrowserTab[]> {
async function openTab(cdpPort: number, url: string): Promise<BrowserTab> { async function openTab(cdpPort: number, url: string): Promise<BrowserTab> {
const encoded = encodeURIComponent(url); const encoded = encodeURIComponent(url);
const created = await fetchJson<{
// Chrome changed /json/new to require PUT (older versions allowed GET).
type CdpTarget = {
id?: string; id?: string;
title?: string; title?: string;
url?: string; url?: string;
webSocketDebuggerUrl?: string; webSocketDebuggerUrl?: string;
type?: 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)"); if (!created.id) throw new Error("Failed to open tab (missing id)");
return { return {
@@ -94,11 +124,13 @@ async function openTab(cdpPort: number, url: string): Promise<BrowserTab> {
} }
async function activateTab(cdpPort: number, targetId: string): Promise<void> { 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> { 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> { async function ensureBrowserAvailable(runtime: RuntimeEnv): Promise<void> {

View File

@@ -11,6 +11,7 @@ import {
browserTabs, browserTabs,
resolveBrowserControlUrl, resolveBrowserControlUrl,
} from "../browser/client.js"; } from "../browser/client.js";
import { runClawdisMac } from "../infra/clawdis-mac.js";
import { agentCommand } from "../commands/agent.js"; import { agentCommand } from "../commands/agent.js";
import { healthCommand } from "../commands/health.js"; import { healthCommand } from "../commands/health.js";
import { sendCommand } from "../commands/send.js"; import { sendCommand } from "../commands/send.js";
@@ -223,6 +224,44 @@ Examples:
registerGatewayCli(program); registerGatewayCli(program);
registerNodesCli(program); registerNodesCli(program);
registerCronCli(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 program
.command("status") .command("status")
.description("Show web session health and recent session recipients") .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 };
}