aboutsummaryrefslogtreecommitdiff
path: root/src/setup.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/setup.ts')
-rw-r--r--src/setup.ts74
1 files changed, 21 insertions, 53 deletions
diff --git a/src/setup.ts b/src/setup.ts
index 40f501c..8c04030 100644
--- a/src/setup.ts
+++ b/src/setup.ts
@@ -4,11 +4,12 @@
* users and a tool (browser_setup) for the agent to self-recover.
*/
-import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
-import { execSync } from "node:child_process";
+import type { ExtensionAPI, ExtensionContext } from "@mariozechner/pi-coding-agent";
import type { BrowserClient } from "./client";
import { ensureDaemon } from "./daemon/spawn";
import { DAEMON_SOCKET_PATH } from "./daemon/protocol";
+import { isBrowserRunning } from "./profile/browser-process";
+import { promptForProfileIfUnset } from "./profile/command";
// ── Public: register the /browser-setup slash command ──────────────────────
@@ -16,7 +17,7 @@ export function registerSetupCommand(pi: ExtensionAPI, client: BrowserClient): v
pi.registerCommand("browser-setup", {
description: "Connect pi to your Chrome browser",
handler: async (_args, ctx) => {
- const result = await performSetup(client);
+ const result = await performSetup(client, ctx);
if (result.success) {
ctx.ui.notify(result.data, "info");
} else {
@@ -32,9 +33,9 @@ export type SetupResult = { success: true; data: string } | { success: false; er
// ── Shared setup logic (used by both the command and the tool) ─────────────
-export async function performSetup(client: BrowserClient): Promise<SetupResult> {
+export async function performSetup(client: BrowserClient, ctx?: ExtensionContext): Promise<SetupResult> {
// Step 1: Check Chrome is running
- const chromeRunning = checkChromeRunning();
+ const chromeRunning = await isBrowserRunning();
if (!chromeRunning) {
return { success: false, error: "No browser instance running. Please open your browser and then run /browser-setup." };
}
@@ -45,7 +46,13 @@ export async function performSetup(client: BrowserClient): Promise<SetupResult>
return { success: false, error: `Could not start the browser daemon. Check ${DAEMON_SOCKET_PATH}.` };
}
- // Step 3: Connect to Chrome DevTools
+ // Step 3: Ask which profile to work in, the first time only. Enumeration
+ // reads DevToolsActivePort + Local State from disk, so it works before the
+ // connection exists. Declining leaves the pre-profile behavior untouched —
+ // this must never block setup.
+ const profileNote = await promptForProfileIfUnset(client, ctx);
+
+ // Step 4: Connect to Chrome DevTools
const startResult = await client.start();
if (!startResult.success) {
const msg = startResult.error.message;
@@ -73,7 +80,7 @@ export async function performSetup(client: BrowserClient): Promise<SetupResult>
return { success: false, error: `Connection failed: ${msg}` };
}
- // Step 4: Verify with test navigation
+ // Step 5: Verify with test navigation
const tabResult = await client.newTab("https://github.com");
if (!tabResult.success) {
return { success: false, error: `Browser connected but test navigation failed: ${tabResult.error.message}` };
@@ -85,52 +92,13 @@ export async function performSetup(client: BrowserClient): Promise<SetupResult>
}
const pageUrl = info.success && !("dialog" in info.data) ? info.data.url : "github.com";
- return { success: true, data: `Browser connected ✓\nNavigated to: ${pageUrl}` };
+ const lines = ["Browser connected ✓", `Navigated to: ${pageUrl}`];
+ if (profileNote) lines.push(profileNote);
+ return { success: true, data: lines.join("\n") };
}
// ── Chrome process detection ───────────────────────────────────────────────
-
-function checkChromeRunning(): boolean {
- try {
- if (process.platform === "darwin") {
- // ps -o comm= returns the full executable path on modern macOS, e.g.
- // "/Applications/Brave Browser.app/Contents/MacOS/Brave Browser". We check
- // for a known Chromium-family name anywhere in the path (case-insensitive),
- // excluding helper/renderer/gpu/crashpad/updater subprocesses that linger
- // after the user quits the browser.
- const out = execSync("ps -A -o comm=", { timeout: 5000 }).toString().toLowerCase();
- const lines = out.split("\n").map((l) => l.trim()).filter(Boolean);
- const browserNames = ["google chrome", "chromium", "microsoft edge", "brave browser"];
- return lines.some((l) => {
- if (
- l.includes("helper") ||
- l.includes("renderer") ||
- l.includes("crashpad") ||
- l.includes(" gpu") ||
- l.includes("updater")
- ) return false;
- return browserNames.some((name) => l.includes(name));
- });
- } else if (process.platform === "linux") {
- const out = execSync("ps -A -o comm=,args=", { timeout: 5000 }).toString().toLowerCase();
- const lines = out.split("\n").map((l) => l.trim()).filter(Boolean);
- const browserComms = [
- "chrome", "chromium", "chromium-browser", "msedge", "microsoft-edge",
- "google-chrome", "brave", "brave-browser",
- ];
- return lines.some((line) => {
- const parts = line.split(/\s+/);
- const comm = parts[0] ?? "";
- if (!comm || !browserComms.includes(comm)) return false;
- const isSubprocess = line.includes("--type=") && !line.includes("--type=browser");
- return !isSubprocess;
- });
- } else if (process.platform === "win32") {
- const out = execSync("tasklist", { timeout: 5000 }).toString().toLowerCase();
- return ["chrome.exe", "msedge.exe", "brave.exe"].some((n) => out.includes(n));
- }
- } catch {
- // best-effort
- }
- return false;
-}
+//
+// Detection moved to src/profile/browser-process.ts, which answers the same
+// "is a browser running?" question per platform and additionally reports the
+// executable path and any explicit --user-data-dir that profile selection needs.