aboutsummaryrefslogtreecommitdiff
path: root/src/setup.ts
blob: 839a54058e14b1841a6005a05c6f543eea0fe8f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
 * Setup command — guides the user through installing and connecting
 * browser-harness to their Chrome browser.
 */

import type { ExtensionAPI, ExtensionContext } from "@mariozechner/pi-coding-agent";
import { execSync } from "node:child_process";
import { existsSync } from "node:fs";
import { homedir } from "node:os";
import { join } from "node:path";
import type { BrowserDaemon } from "./daemon";

export function registerSetupCommand(pi: ExtensionAPI, daemon: BrowserDaemon): void {
  pi.registerCommand("browser-setup", {
    description: "Install and connect browser-harness to Chrome",
    handler: async (_args, ctx) => {
      await runSetup(ctx, daemon);
    },
  });
}

async function runSetup(ctx: ExtensionContext, daemon: BrowserDaemon): Promise<void> {
  ctx.ui.notify("browser-harness setup: checking installation...", "info");

  // Step 1: Check for browser-harness installation
  const bhInstalled = await checkBrowserHarnessInstalled();
  if (!bhInstalled) {
    ctx.ui.notify(
      "browser-harness not found. Installing...",
      "warning",
    );
    const installed = await installBrowserHarness(ctx);
    if (!installed) {
      ctx.ui.notify(
        "Installation failed. Please install manually:\n" +
          "  git clone https://github.com/browser-use/browser-harness ~/Developer/browser-harness\n" +
          "  cd ~/Developer/browser-harness && uv sync",
        "error",
      );
      return;
    }
  }

  ctx.ui.notify("browser-harness found ✓", "info");

  // Step 2: Check Chrome is running
  const chromeRunning = checkChromeRunning();
  if (!chromeRunning) {
    ctx.ui.notify(
      "Chrome/Chromium/Edge not detected. Please start your browser and retry /browser-setup.",
      "error",
    );
    return;
  }
  ctx.ui.notify("Chrome is running ✓", "info");

  // Step 3: Try to start the daemon
  ctx.ui.notify("Connecting to Chrome...", "info");
  try {
    await daemon.start();
    ctx.ui.notify("Daemon connected ✓", "info");
  } catch (err) {
    const msg = err instanceof Error ? err.message : String(err);
    const lower = msg.toLowerCase();

    if (
      lower.includes("devtoolsactiveport") ||
      lower.includes("remote debugging")
    ) {
      ctx.ui.notify(
        "Chrome remote debugging needs to be enabled.\n\n" +
          "Start Chrome with: --remote-debugging-port=9222\n" +
          "Then run /browser-setup again.",
        "warning",
      );
      return;
    }

    ctx.ui.notify(`Connection failed: ${msg}`, "error");
    return;
  }

  // Step 4: Verify with test navigation
  ctx.ui.notify("Testing browser control...", "info");
  try {
    await daemon.newTab("https://github.com/browser-use/browser-harness");
    const info = await daemon.getPageInfo();
    if ("dialog" in info) {
      await daemon.cdp("Page.handleJavaScriptDialog", { accept: true });
    }
    ctx.ui.notify(
      `Browser control verified ✓\nNavigated to: ${"url" in info ? info.url : "github.com"}`,
      "info",
    );
  } catch (err) {
    ctx.ui.notify(
      `Browser connected but test navigation failed: ${err instanceof Error ? err.message : String(err)}`,
      "warning",
    );
  }
}

// ── Helpers ──────────────────────────────────────────────────────────────────

async function checkBrowserHarnessInstalled(): Promise<boolean> {
  const searchPaths = [
    join(homedir(), "Developer", "browser-harness"),
    join(homedir(), "src", "browser-harness"),
    join(homedir(), "browser-harness"),
    join(homedir(), "dev", "browser-harness"),
    join(homedir(), "Projects", "browser-harness"),
    process.cwd(),
  ];

  for (const dir of searchPaths) {
    if (
      existsSync(join(dir, "daemon.py")) &&
      existsSync(join(dir, "helpers.py"))
    ) {
      return true;
    }
  }

  // Check if uv tool is installed globally
  try {
    execSync("uv tool list 2>/dev/null | grep browser-harness", { timeout: 5000 });
    return true;
  } catch {
    // not found via uv
  }

  return false;
}

async function installBrowserHarness(ctx: ExtensionContext): Promise<boolean> {
  // Try uv tool install
  try {
    ctx.ui.notify("Installing browser-harness via uv...", "info");
    execSync("uv tool install browser-harness", {
      timeout: 60_000,
      stdio: "pipe",
    });
    return true;
  } catch {
    // uv install failed
  }

  // Try git clone
  try {
    const targetDir = join(homedir(), "Developer", "browser-harness");
    ctx.ui.notify(`Cloning browser-harness to ${targetDir}...`, "info");
    execSync(
      `git clone https://github.com/browser-use/browser-harness "${targetDir}"`,
      { timeout: 30_000, stdio: "pipe" },
    );
    execSync("uv sync", { cwd: targetDir, timeout: 60_000, stdio: "pipe" });
    return true;
  } catch {
    return false;
  }
}

function checkChromeRunning(): boolean {
  try {
    if (process.platform === "darwin" || process.platform === "linux") {
      const out = execSync("ps -A -o comm=", { timeout: 5000 }).toString().toLowerCase();
      return ["google chrome", "chrome", "chromium", "microsoft edge", "msedge"].some(
        (n) => out.includes(n),
      );
    } else if (process.platform === "win32") {
      const out = execSync("tasklist", { timeout: 5000 }).toString().toLowerCase();
      return ["chrome.exe", "msedge.exe"].some((n) => out.includes(n));
    }
  } catch {
    // best-effort
  }
  return false;
}