browse 0.8.4-alpha-75df86a → 0.8.4-alpha-cffcc91
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/hooks/command-not-found.js +45 -0
- package/dist/lib/command-suggestions.js +112 -0
- package/dist/lib/driver/commands/selectors.js +2 -1
- package/dist/lib/driver/daemon/client.js +13 -6
- package/dist/lib/driver/daemon/protocol.js +2 -0
- package/dist/lib/driver/daemon/server.js +14 -3
- package/dist/lib/driver/errors.js +16 -0
- package/dist/lib/driver/remote.disabled.js +15 -0
- package/dist/lib/driver/remote.js +35 -0
- package/dist/lib/driver/session-manager.js +68 -6
- package/dist/lib/telemetry.js +19 -0
- package/package.json +5 -2
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { Errors } from "@oclif/core";
|
|
2
|
+
import { suggestCommand } from "../lib/command-suggestions.js";
|
|
3
|
+
import { captureCommandNotFound } from "../lib/telemetry.js";
|
|
4
|
+
function toSpaced(commandId) {
|
|
5
|
+
return commandId.replaceAll(":", " ");
|
|
6
|
+
}
|
|
7
|
+
const hook = async function ({ config, id }) {
|
|
8
|
+
let attempted = "";
|
|
9
|
+
let suggestion = null;
|
|
10
|
+
try {
|
|
11
|
+
const commandIds = config.commands
|
|
12
|
+
.filter((command) => !command.hidden)
|
|
13
|
+
.map((command) => command.id);
|
|
14
|
+
const result = suggestCommand(id, commandIds);
|
|
15
|
+
attempted = result.attempted;
|
|
16
|
+
suggestion = result.suggestion;
|
|
17
|
+
if (suggestion &&
|
|
18
|
+
!config.findCommand(suggestion) &&
|
|
19
|
+
!config.findTopic(suggestion)) {
|
|
20
|
+
// Guards against alias targets drifting out of the command tree.
|
|
21
|
+
suggestion = null;
|
|
22
|
+
}
|
|
23
|
+
const displayAttempted = toSpaced(attempted || (id.split(":")[0] ?? id));
|
|
24
|
+
const didYouMean = suggestion
|
|
25
|
+
? ` Did you mean "${config.bin} ${toSpaced(suggestion)}"?`
|
|
26
|
+
: "";
|
|
27
|
+
process.stderr.write(`"${config.bin} ${displayAttempted}" is not a ${config.bin} command.${didYouMean} Run ${config.bin} --help for all commands.\n`);
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
// Suggestions are best-effort and must never mask the not-found error.
|
|
31
|
+
}
|
|
32
|
+
try {
|
|
33
|
+
// Awaited so the event is delivered before the process exits; the
|
|
34
|
+
// transport aborts after a short timeout, so this cannot hang the CLI.
|
|
35
|
+
await captureCommandNotFound(config.version, attempted || null, suggestion);
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
// Best-effort telemetry should never affect CLI behavior.
|
|
39
|
+
}
|
|
40
|
+
// Re-throw oclif's standard not-found error. Returning normally from a
|
|
41
|
+
// command_not_found hook makes oclif treat the invocation as handled
|
|
42
|
+
// (exit 0), so this throw preserves the default error and exit code 2.
|
|
43
|
+
throw new Errors.CLIError(`command ${id} not found`);
|
|
44
|
+
};
|
|
45
|
+
export default hook;
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Suggestion engine for unknown commands.
|
|
3
|
+
*
|
|
4
|
+
* oclif's spaced-topic parsing glues unknown leading argv tokens into the
|
|
5
|
+
* attempted command id (e.g. `browse opne https://example.com` arrives as
|
|
6
|
+
* `opne:https://example.com`), so the id may contain user-provided values.
|
|
7
|
+
* Everything here works on a sanitized token prefix and never returns raw
|
|
8
|
+
* argv content beyond the tokens that matched a known command shape.
|
|
9
|
+
*/
|
|
10
|
+
import { distance } from "fastest-levenshtein";
|
|
11
|
+
/**
|
|
12
|
+
* Old Commander-era syntax (and common agent guesses) mapped to the current
|
|
13
|
+
* command tree. Keys and values are colon-separated oclif ids; values may
|
|
14
|
+
* also be topics (e.g. `cloud:contexts`, which prints topic help).
|
|
15
|
+
*/
|
|
16
|
+
export const aliasSuggestions = new Map([
|
|
17
|
+
["sessions", "cloud:sessions:list"],
|
|
18
|
+
["sessions:list", "cloud:sessions:list"],
|
|
19
|
+
["sessions:create", "cloud:sessions:create"],
|
|
20
|
+
["sessions:get", "cloud:sessions:get"],
|
|
21
|
+
["projects", "cloud:projects:list"],
|
|
22
|
+
["projects:list", "cloud:projects:list"],
|
|
23
|
+
["contexts", "cloud:contexts"],
|
|
24
|
+
["extensions", "cloud:extensions"],
|
|
25
|
+
["search", "cloud:search"],
|
|
26
|
+
["fetch", "cloud:fetch"],
|
|
27
|
+
["goto", "open"],
|
|
28
|
+
["navigate", "open"],
|
|
29
|
+
]);
|
|
30
|
+
const safeTokenPattern = /^[A-Za-z][A-Za-z0-9_-]*$/;
|
|
31
|
+
const maxCommandTokens = 4;
|
|
32
|
+
const maxSuggestionDistance = 5;
|
|
33
|
+
/**
|
|
34
|
+
* Extracts the leading command-shaped tokens from an attempted id, stopping
|
|
35
|
+
* at the first token that does not look like a command word (URLs, selectors,
|
|
36
|
+
* flags, and other argument-like values).
|
|
37
|
+
*/
|
|
38
|
+
export function extractCommandTokens(id) {
|
|
39
|
+
const tokens = [];
|
|
40
|
+
for (const token of id.split(":")) {
|
|
41
|
+
if (tokens.length >= maxCommandTokens || !safeTokenPattern.test(token)) {
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
tokens.push(token.toLowerCase());
|
|
45
|
+
}
|
|
46
|
+
return tokens;
|
|
47
|
+
}
|
|
48
|
+
function tokenThreshold(token) {
|
|
49
|
+
return Math.max(2, Math.floor(token.length / 3));
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Segment-aligned fuzzy distance between a token prefix and a command id.
|
|
53
|
+
* Only command ids with the same number of segments are considered, and every
|
|
54
|
+
* token must be within its own edit-distance threshold of the corresponding
|
|
55
|
+
* segment. This means a trailing token can only ever be retained in the
|
|
56
|
+
* attempted command when it itself looks like a typo of a real command
|
|
57
|
+
* segment — free-form user values can never ride along.
|
|
58
|
+
*/
|
|
59
|
+
function prefixDistance(tokens, commandId) {
|
|
60
|
+
const segments = commandId.split(":");
|
|
61
|
+
if (segments.length !== tokens.length) {
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
let total = 0;
|
|
65
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
66
|
+
const token = tokens[i] ?? "";
|
|
67
|
+
const segmentDistance = distance(token, segments[i] ?? "");
|
|
68
|
+
if (segmentDistance > tokenThreshold(token)) {
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
total += segmentDistance;
|
|
72
|
+
}
|
|
73
|
+
return total > maxSuggestionDistance ? null : total;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Computes a suggestion for an unknown command id. Explicit aliases win over
|
|
77
|
+
* fuzzy matches, and longer token prefixes win over shorter ones so that
|
|
78
|
+
* `auth status` resolves before `auth`. Returns only the matched prefix as
|
|
79
|
+
* `attempted` (or the first token when nothing matches) so user-provided
|
|
80
|
+
* values never escape into messaging or telemetry.
|
|
81
|
+
*/
|
|
82
|
+
export function suggestCommand(id, commandIds) {
|
|
83
|
+
const tokens = extractCommandTokens(id);
|
|
84
|
+
if (tokens.length === 0) {
|
|
85
|
+
return { attempted: "", suggestion: null };
|
|
86
|
+
}
|
|
87
|
+
for (let length = tokens.length; length >= 1; length--) {
|
|
88
|
+
const attempted = tokens.slice(0, length).join(":");
|
|
89
|
+
const alias = aliasSuggestions.get(attempted);
|
|
90
|
+
if (alias) {
|
|
91
|
+
return { attempted, suggestion: alias };
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
let best;
|
|
95
|
+
for (let length = tokens.length; length >= 1; length--) {
|
|
96
|
+
const prefix = tokens.slice(0, length);
|
|
97
|
+
for (const commandId of commandIds) {
|
|
98
|
+
const prefixDist = prefixDistance(prefix, commandId);
|
|
99
|
+
if (prefixDist !== null && (!best || prefixDist < best.distance)) {
|
|
100
|
+
best = {
|
|
101
|
+
attempted: prefix.join(":"),
|
|
102
|
+
suggestion: commandId,
|
|
103
|
+
distance: prefixDist,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
if (best) {
|
|
109
|
+
return { attempted: best.attempted, suggestion: best.suggestion };
|
|
110
|
+
}
|
|
111
|
+
return { attempted: tokens[0] ?? "", suggestion: null };
|
|
112
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { DriverError } from "../errors.js";
|
|
1
2
|
export function emptyRefMaps() {
|
|
2
3
|
return { urlMap: {}, xpathMap: {} };
|
|
3
4
|
}
|
|
@@ -24,7 +25,7 @@ export function resolveSelector(selector, refMaps) {
|
|
|
24
25
|
return selector;
|
|
25
26
|
const xpath = refMaps.xpathMap[ref];
|
|
26
27
|
if (!xpath) {
|
|
27
|
-
throw new
|
|
28
|
+
throw new DriverError(`Unknown ref "${ref}" - run browse snapshot first to populate refs (have ${Object.keys(refMaps.xpathMap).length} refs).`, { code: "stale_ref" });
|
|
28
29
|
}
|
|
29
30
|
return xpath;
|
|
30
31
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { spawn } from "node:child_process";
|
|
2
2
|
import { promises as fs } from "node:fs";
|
|
3
3
|
import net from "node:net";
|
|
4
|
-
import { fail } from "../../errors.js";
|
|
4
|
+
import { CommandFailure, fail } from "../../errors.js";
|
|
5
5
|
import { targetsCompatible } from "../mode.js";
|
|
6
6
|
import { cleanupDaemonFiles, ensureRuntimeDir, getLockPath, getPidPath, getSocketPath, PRIVATE_FILE_MODE, } from "./paths.js";
|
|
7
7
|
import { isProcessAlive } from "./process.js";
|
|
@@ -15,7 +15,7 @@ export async function ensureDriverDaemon({ session, target, }) {
|
|
|
15
15
|
}
|
|
16
16
|
const locked = await acquireLock(session);
|
|
17
17
|
if (!locked) {
|
|
18
|
-
fail(`Timed out waiting for driver daemon lock for session "${session}"
|
|
18
|
+
fail(`Timed out waiting for driver daemon lock for session "${session}".`, 1, { resultCode: "daemon_lock_timeout" });
|
|
19
19
|
}
|
|
20
20
|
try {
|
|
21
21
|
const afterLock = await tryDriverStatus(session);
|
|
@@ -24,7 +24,7 @@ export async function ensureDriverDaemon({ session, target, }) {
|
|
|
24
24
|
return;
|
|
25
25
|
}
|
|
26
26
|
if (await isDaemonPidAlive(session)) {
|
|
27
|
-
fail(`Driver daemon session "${session}" is running but not responding. Run browse stop --session ${session} --force to clean it up
|
|
27
|
+
fail(`Driver daemon session "${session}" is running but not responding. Run browse stop --session ${session} --force to clean it up.`, 1, { resultCode: "daemon_unresponsive" });
|
|
28
28
|
}
|
|
29
29
|
spawnDaemon(session, target);
|
|
30
30
|
await waitForSocketReady(getSocketPath(session), 30_000);
|
|
@@ -123,7 +123,7 @@ async function sendDriverRequest(session, request) {
|
|
|
123
123
|
return new Error(`Driver daemon session "${session}" closed ${detail}.`);
|
|
124
124
|
};
|
|
125
125
|
const timeout = setTimeout(() => {
|
|
126
|
-
failRequest(new
|
|
126
|
+
failRequest(new CommandFailure(`Timed out waiting for driver daemon session "${session}".`, 1, { resultCode: "daemon_socket_timeout" }));
|
|
127
127
|
}, 35_000);
|
|
128
128
|
socket.on("connect", () => {
|
|
129
129
|
socket.write(`${JSON.stringify(request)}\n`);
|
|
@@ -136,7 +136,12 @@ async function sendDriverRequest(session, request) {
|
|
|
136
136
|
try {
|
|
137
137
|
const response = ResponseSchema.parse(JSON.parse(buffer.slice(0, newline)));
|
|
138
138
|
if (response.type === "error") {
|
|
139
|
-
failRequest(new
|
|
139
|
+
failRequest(new CommandFailure(response.error, 1, {
|
|
140
|
+
...(response.code ? { resultCode: response.code } : {}),
|
|
141
|
+
...(response.httpStatus !== undefined
|
|
142
|
+
? { httpStatus: response.httpStatus }
|
|
143
|
+
: {}),
|
|
144
|
+
}));
|
|
140
145
|
return;
|
|
141
146
|
}
|
|
142
147
|
completeRequest(response.data);
|
|
@@ -161,7 +166,9 @@ async function sendDriverRequest(session, request) {
|
|
|
161
166
|
function spawnDaemon(session, target) {
|
|
162
167
|
const entrypoint = process.argv[1];
|
|
163
168
|
if (!entrypoint) {
|
|
164
|
-
fail("Unable to locate browse CLI entrypoint for daemon startup."
|
|
169
|
+
fail("Unable to locate browse CLI entrypoint for daemon startup.", 1, {
|
|
170
|
+
resultCode: "daemon_spawn_failed",
|
|
171
|
+
});
|
|
165
172
|
}
|
|
166
173
|
const child = spawn(process.execPath, [
|
|
167
174
|
entrypoint,
|
|
@@ -32,7 +32,9 @@ export const SuccessResponseSchema = z.object({
|
|
|
32
32
|
type: z.literal("success"),
|
|
33
33
|
});
|
|
34
34
|
export const ErrorResponseSchema = z.object({
|
|
35
|
+
code: z.string().optional(),
|
|
35
36
|
error: z.string(),
|
|
37
|
+
httpStatus: z.number().optional(),
|
|
36
38
|
id: z.string().optional(),
|
|
37
39
|
type: z.literal("error"),
|
|
38
40
|
});
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import net from "node:net";
|
|
2
2
|
import readline from "node:readline";
|
|
3
|
+
import { DriverError } from "../errors.js";
|
|
3
4
|
import { DriverSessionManager } from "../session-manager.js";
|
|
4
5
|
import { cleanupDaemonFiles, ensureRuntimeDir, getPidPath, getSocketPath, writePrivateFile, } from "./paths.js";
|
|
5
6
|
import { parseRequest, serializeResponse, } from "./protocol.js";
|
|
@@ -83,7 +84,7 @@ async function handleLine(line, socket, manager) {
|
|
|
83
84
|
request = parseRequest(line);
|
|
84
85
|
}
|
|
85
86
|
catch (error) {
|
|
86
|
-
await writeResponse(socket, {
|
|
87
|
+
await writeResponse(socket, { ...formatError(error), type: "error" });
|
|
87
88
|
return false;
|
|
88
89
|
}
|
|
89
90
|
try {
|
|
@@ -124,7 +125,7 @@ async function handleLine(line, socket, manager) {
|
|
|
124
125
|
}
|
|
125
126
|
catch (error) {
|
|
126
127
|
await writeResponse(socket, {
|
|
127
|
-
|
|
128
|
+
...formatError(error),
|
|
128
129
|
id: request.id,
|
|
129
130
|
type: "error",
|
|
130
131
|
});
|
|
@@ -161,5 +162,15 @@ function endSocket(socket) {
|
|
|
161
162
|
});
|
|
162
163
|
}
|
|
163
164
|
function formatError(error) {
|
|
164
|
-
|
|
165
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
166
|
+
if (error instanceof DriverError) {
|
|
167
|
+
return {
|
|
168
|
+
code: error.code,
|
|
169
|
+
error: message,
|
|
170
|
+
...(error.httpStatus !== undefined
|
|
171
|
+
? { httpStatus: error.httpStatus }
|
|
172
|
+
: {}),
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
return { error: message };
|
|
165
176
|
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Typed driver error. The daemon serializes `code`/`httpStatus` into error
|
|
3
|
+
* responses so the client can record a telemetry result code and agents get
|
|
4
|
+
* a stable, machine-readable failure reason alongside the human message.
|
|
5
|
+
*/
|
|
6
|
+
export class DriverError extends Error {
|
|
7
|
+
code;
|
|
8
|
+
httpStatus;
|
|
9
|
+
constructor(message, options) {
|
|
10
|
+
super(message, options.cause === undefined ? {} : { cause: options.cause });
|
|
11
|
+
this.name = "DriverError";
|
|
12
|
+
this.code = options.code;
|
|
13
|
+
if (options.httpStatus !== undefined)
|
|
14
|
+
this.httpStatus = options.httpStatus;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -13,6 +13,21 @@ export function autoSelectRemoteTarget() {
|
|
|
13
13
|
export function remoteStagehandOptions() {
|
|
14
14
|
throw new Error(DISABLED_MESSAGE);
|
|
15
15
|
}
|
|
16
|
+
export function classifyRemoteInitError(error) {
|
|
17
|
+
// Remote targets cannot be selected in a local-only build, so this is only
|
|
18
|
+
// reachable if a remote error somehow surfaces anyway; preserve it as-is.
|
|
19
|
+
return {
|
|
20
|
+
code: "remote_session_create_failed",
|
|
21
|
+
message: error instanceof Error ? error.message : String(error),
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
export function driverInitHints() {
|
|
25
|
+
// Key-free variants: a local-only artifact must not mention the API key.
|
|
26
|
+
return {
|
|
27
|
+
chromeNotFound: "No Chrome or Chromium found on this machine. Install one (Linux: apt install chromium \u00b7 macOS: brew install --cask google-chrome, or Chromium with CHROME_PATH set) or attach to a running browser with --cdp <port>.",
|
|
28
|
+
repeatedInitFailure: " (failing repeatedly — check your browser setup or run browse doctor)",
|
|
29
|
+
};
|
|
30
|
+
}
|
|
16
31
|
export function remoteDoctorCheck() {
|
|
17
32
|
return { ok: true, message: "remote disabled (local-only build)" };
|
|
18
33
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { StatusCodes } from "http-status-codes";
|
|
1
2
|
/**
|
|
2
3
|
* Real Browserbase capability. This is the ONLY module that reads
|
|
3
4
|
* `BROWSERBASE_API_KEY`; it is excluded from `build:local-only` so that
|
|
@@ -25,6 +26,40 @@ export function remoteStagehandOptions() {
|
|
|
25
26
|
verbose: 0,
|
|
26
27
|
};
|
|
27
28
|
}
|
|
29
|
+
/**
|
|
30
|
+
* Map a failed remote `stagehand.init()` to an actionable message and a
|
|
31
|
+
* stable result code. Browserbase SDK errors carry an HTTP `status`.
|
|
32
|
+
*/
|
|
33
|
+
export function classifyRemoteInitError(error) {
|
|
34
|
+
const status = error?.status;
|
|
35
|
+
const httpStatus = typeof status === "number" ? status : undefined;
|
|
36
|
+
const original = error instanceof Error ? error.message : String(error);
|
|
37
|
+
if (httpStatus === StatusCodes.UNAUTHORIZED) {
|
|
38
|
+
return {
|
|
39
|
+
code: "remote_auth_401",
|
|
40
|
+
httpStatus,
|
|
41
|
+
message: "Browserbase rejected your BROWSERBASE_API_KEY (401 Unauthorized). A set key makes browse default to remote mode. Check the key at https://browserbase.com/settings, run without one using --local (browse open <url> --local), or diagnose with browse doctor.",
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
if (httpStatus === StatusCodes.FORBIDDEN) {
|
|
45
|
+
return {
|
|
46
|
+
code: "remote_auth_403",
|
|
47
|
+
httpStatus,
|
|
48
|
+
message: "Browserbase refused this request (403 Forbidden). Your BROWSERBASE_API_KEY may lack access to this project, or your plan may not allow this session type. Check the key at https://browserbase.com/settings, run without one using --local (browse open <url> --local), or diagnose with browse doctor.",
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
code: "remote_session_create_failed",
|
|
53
|
+
...(httpStatus !== undefined ? { httpStatus } : {}),
|
|
54
|
+
message: `Failed to start a remote (Browserbase) session: ${original}\nRun browse doctor to diagnose remote connectivity.`,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
export function driverInitHints() {
|
|
58
|
+
return {
|
|
59
|
+
chromeNotFound: "No Chrome or Chromium found on this machine. Install one (Linux: apt install chromium \u00b7 macOS: brew install --cask google-chrome, or Chromium with CHROME_PATH set), attach to a running browser with --cdp <port>, or set BROWSERBASE_API_KEY to use a remote browser.",
|
|
60
|
+
repeatedInitFailure: " (failing repeatedly — fix BROWSERBASE_API_KEY, use --local, or run browse doctor)",
|
|
61
|
+
};
|
|
62
|
+
}
|
|
28
63
|
export function remoteDoctorCheck(env) {
|
|
29
64
|
if (env.BROWSERBASE_API_KEY) {
|
|
30
65
|
return { ok: true, message: "BROWSERBASE_API_KEY is set" };
|
|
@@ -1,14 +1,41 @@
|
|
|
1
1
|
import { Stagehand } from "@browserbasehq/stagehand";
|
|
2
2
|
import { emptyRefMaps, resolveSelector as resolveCachedSelector, } from "./commands/selectors.js";
|
|
3
3
|
import { executeDriverCommand } from "./commands/registry.js";
|
|
4
|
+
import { DriverError } from "./errors.js";
|
|
4
5
|
import { discoverLocalCdp } from "./local-cdp-discovery.js";
|
|
5
6
|
import { NetworkCapture } from "./network-capture.js";
|
|
6
7
|
import { getRemote } from "./remote-binding.js";
|
|
7
8
|
const INIT_FAILURE_RETRY_MS = 5_000;
|
|
9
|
+
const INIT_FAILURE_RETRY_MAX_MS = 60_000;
|
|
10
|
+
// chrome-launcher reports "no Chrome on this machine" with these codes (its
|
|
11
|
+
// LaunchErrorCodes const enum, which can't be imported directly: const enums
|
|
12
|
+
// are erased at build time and isolatedModules forbids cross-module access).
|
|
13
|
+
const CHROME_NOT_FOUND_ERROR_CODES = new Set([
|
|
14
|
+
"ERR_LAUNCHER_NOT_INSTALLED",
|
|
15
|
+
"ERR_LAUNCHER_PATH_NOT_SET",
|
|
16
|
+
]);
|
|
17
|
+
/**
|
|
18
|
+
* Exponential backoff for cached init failures: 5s, 10s, 20s, ... capped at
|
|
19
|
+
* 1 minute. Prevents agents stuck in retry loops from hammering init while
|
|
20
|
+
* still allowing a quick retry after the first failure.
|
|
21
|
+
*/
|
|
22
|
+
export function initFailureBackoffMs(consecutiveFailures) {
|
|
23
|
+
const attempt = Math.max(1, consecutiveFailures);
|
|
24
|
+
return Math.min(INIT_FAILURE_RETRY_MS * 2 ** (attempt - 1), INIT_FAILURE_RETRY_MAX_MS);
|
|
25
|
+
}
|
|
26
|
+
export function isChromeNotFoundError(error) {
|
|
27
|
+
const code = error?.code;
|
|
28
|
+
if (typeof code === "string" && CHROME_NOT_FOUND_ERROR_CODES.has(code)) {
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
return (error instanceof Error &&
|
|
32
|
+
error.message.includes("No Chrome installations found"));
|
|
33
|
+
}
|
|
8
34
|
export class DriverSessionManager {
|
|
9
35
|
session;
|
|
10
36
|
target;
|
|
11
37
|
network;
|
|
38
|
+
consecutiveInitFailures = 0;
|
|
12
39
|
context = null;
|
|
13
40
|
initFailure = null;
|
|
14
41
|
initPromise = null;
|
|
@@ -79,6 +106,7 @@ export class DriverSessionManager {
|
|
|
79
106
|
this.stagehand = null;
|
|
80
107
|
this.context = null;
|
|
81
108
|
this.initFailure = null;
|
|
109
|
+
this.consecutiveInitFailures = 0;
|
|
82
110
|
await this.network.disable().catch(() => undefined);
|
|
83
111
|
if (stagehand) {
|
|
84
112
|
await stagehand.close().catch(() => undefined);
|
|
@@ -144,7 +172,7 @@ export class DriverSessionManager {
|
|
|
144
172
|
this.selectedTargetId = page.targetId();
|
|
145
173
|
return page;
|
|
146
174
|
}
|
|
147
|
-
throw new
|
|
175
|
+
throw new DriverError(`No active page in session "${this.session}". Run browse open <url> --session ${this.session} or browse tab new <url> --session ${this.session}.`, { code: "no_active_page" });
|
|
148
176
|
}
|
|
149
177
|
activePageIfPresent() {
|
|
150
178
|
try {
|
|
@@ -168,19 +196,32 @@ export class DriverSessionManager {
|
|
|
168
196
|
this.initPromise = this.initialize()
|
|
169
197
|
.then(() => {
|
|
170
198
|
this.initFailure = null;
|
|
199
|
+
this.consecutiveInitFailures = 0;
|
|
171
200
|
})
|
|
172
|
-
.catch((error) => {
|
|
201
|
+
.catch(async (error) => {
|
|
202
|
+
this.consecutiveInitFailures += 1;
|
|
203
|
+
const failure = await this.markRepeatedInitFailure(error);
|
|
173
204
|
this.initFailure = {
|
|
174
|
-
error,
|
|
175
|
-
retryAt: Date.now() +
|
|
205
|
+
error: failure,
|
|
206
|
+
retryAt: Date.now() + initFailureBackoffMs(this.consecutiveInitFailures),
|
|
176
207
|
};
|
|
177
|
-
throw
|
|
208
|
+
throw failure;
|
|
178
209
|
})
|
|
179
210
|
.finally(() => {
|
|
180
211
|
this.initPromise = null;
|
|
181
212
|
});
|
|
182
213
|
return this.initPromise;
|
|
183
214
|
}
|
|
215
|
+
async markRepeatedInitFailure(error) {
|
|
216
|
+
if (this.consecutiveInitFailures < 3 || !(error instanceof Error)) {
|
|
217
|
+
return error;
|
|
218
|
+
}
|
|
219
|
+
const hint = (await getRemote()).driverInitHints().repeatedInitFailure;
|
|
220
|
+
if (!error.message.includes(hint)) {
|
|
221
|
+
error.message += hint;
|
|
222
|
+
}
|
|
223
|
+
return error;
|
|
224
|
+
}
|
|
184
225
|
async initialize() {
|
|
185
226
|
const resolvedTarget = await this.resolveTarget();
|
|
186
227
|
const options = await this.stagehandOptions(resolvedTarget);
|
|
@@ -190,7 +231,7 @@ export class DriverSessionManager {
|
|
|
190
231
|
}
|
|
191
232
|
catch (error) {
|
|
192
233
|
await stagehand.close().catch(() => undefined);
|
|
193
|
-
throw error;
|
|
234
|
+
throw await describeInitError(error, resolvedTarget);
|
|
194
235
|
}
|
|
195
236
|
this.stagehand = stagehand;
|
|
196
237
|
this.context = stagehand.context;
|
|
@@ -243,3 +284,24 @@ async function safeTitle(page) {
|
|
|
243
284
|
return "";
|
|
244
285
|
}
|
|
245
286
|
}
|
|
287
|
+
/**
|
|
288
|
+
* Turn raw `stagehand.init()` failures into typed, actionable errors. Remote
|
|
289
|
+
* failures are classified by the remote capability (401/403/etc.); a missing
|
|
290
|
+
* local Chrome gets install/escape-hatch guidance. Anything else is rethrown
|
|
291
|
+
* unchanged.
|
|
292
|
+
*/
|
|
293
|
+
async function describeInitError(error, target) {
|
|
294
|
+
if (error instanceof DriverError)
|
|
295
|
+
return error;
|
|
296
|
+
if (target.kind === "remote") {
|
|
297
|
+
const { code, httpStatus, message } = (await getRemote()).classifyRemoteInitError(error);
|
|
298
|
+
return new DriverError(message, { cause: error, code, httpStatus });
|
|
299
|
+
}
|
|
300
|
+
if (target.kind === "managed-local" && isChromeNotFoundError(error)) {
|
|
301
|
+
return new DriverError((await getRemote()).driverInitHints().chromeNotFound, {
|
|
302
|
+
cause: error,
|
|
303
|
+
code: "no_chrome_found",
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
return error;
|
|
307
|
+
}
|
package/dist/lib/telemetry.js
CHANGED
|
@@ -52,6 +52,25 @@ export async function captureCommandCompleted(cliVersion, error) {
|
|
|
52
52
|
completionCapture,
|
|
53
53
|
]);
|
|
54
54
|
}
|
|
55
|
+
/**
|
|
56
|
+
* Captures a `cli.command_not_found` event. Privacy: only the sanitized
|
|
57
|
+
* attempted command id and the computed suggestion are sent — never raw argv,
|
|
58
|
+
* which can contain URLs, selectors, or secrets.
|
|
59
|
+
*/
|
|
60
|
+
export async function captureCommandNotFound(cliVersion, attemptedCommand, suggestedCommand) {
|
|
61
|
+
await getCliTelemetry(cliVersion)
|
|
62
|
+
.capture("cli.command_not_found", {
|
|
63
|
+
attempted_command: attemptedCommand
|
|
64
|
+
? resolveCommandPath(attemptedCommand)
|
|
65
|
+
: null,
|
|
66
|
+
suggested_command: suggestedCommand
|
|
67
|
+
? resolveCommandPath(suggestedCommand)
|
|
68
|
+
: null,
|
|
69
|
+
})
|
|
70
|
+
.catch(() => {
|
|
71
|
+
// Best-effort telemetry should never affect CLI behavior.
|
|
72
|
+
});
|
|
73
|
+
}
|
|
55
74
|
function createCliTelemetry(options) {
|
|
56
75
|
const env = options.env ?? process.env;
|
|
57
76
|
const transport = resolveTransportConfig(env);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "browse",
|
|
3
|
-
"version": "0.8.4-alpha-
|
|
3
|
+
"version": "0.8.4-alpha-cffcc91",
|
|
4
4
|
"description": "Unified Browserbase CLI for browser automation and cloud APIs.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"private": false,
|
|
@@ -27,7 +27,8 @@
|
|
|
27
27
|
"hooks": {
|
|
28
28
|
"init": "./dist/hooks/init.js",
|
|
29
29
|
"prerun": "./dist/hooks/prerun.js",
|
|
30
|
-
"finally": "./dist/hooks/finally.js"
|
|
30
|
+
"finally": "./dist/hooks/finally.js",
|
|
31
|
+
"command_not_found": "./dist/hooks/command-not-found.js"
|
|
31
32
|
},
|
|
32
33
|
"topicSeparator": " ",
|
|
33
34
|
"topics": {
|
|
@@ -91,6 +92,8 @@
|
|
|
91
92
|
"archiver": "^7.0.1",
|
|
92
93
|
"deepmerge": "^4.3.1",
|
|
93
94
|
"dotenv": "^16.5.0",
|
|
95
|
+
"fastest-levenshtein": "^1.0.16",
|
|
96
|
+
"http-status-codes": "^2.3.0",
|
|
94
97
|
"ignore": "^7.0.5",
|
|
95
98
|
"node-html-markdown": "^1.3.0",
|
|
96
99
|
"semver": "^7.7.4",
|