browse 0.8.4-alpha-3ecf09e → 0.8.4-alpha-4ee8d99

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.
@@ -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
+ }
@@ -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-3ecf09e",
3
+ "version": "0.8.4-alpha-4ee8d99",
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,7 @@
91
92
  "archiver": "^7.0.1",
92
93
  "deepmerge": "^4.3.1",
93
94
  "dotenv": "^16.5.0",
95
+ "fastest-levenshtein": "^1.0.16",
94
96
  "ignore": "^7.0.5",
95
97
  "node-html-markdown": "^1.3.0",
96
98
  "semver": "^7.7.4",
@@ -112,7 +114,7 @@
112
114
  "prettier": "3.5.3",
113
115
  "typescript": "^5.8.3",
114
116
  "typescript-eslint": "^8.59.3",
115
- "vitest": "^4.1.6"
117
+ "vitest": "^4.1.9"
116
118
  },
117
119
  "scripts": {
118
120
  "build": "node -e \"const fs = require('fs'); fs.rmSync('dist', { recursive: true, force: true }); fs.rmSync('oclif.manifest.json', { force: true })\" && tsc -p tsconfig.json && pnpm manifest",