@browserbridge/bbx 1.7.4 → 1.7.6

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.
Files changed (34) hide show
  1. package/package.json +6 -6
  2. package/packages/agent-client/src/atomic-write.js +99 -0
  3. package/packages/agent-client/src/cli-args.js +152 -0
  4. package/packages/agent-client/src/cli-batch.js +140 -0
  5. package/packages/agent-client/src/cli-helpers.js +20 -3
  6. package/packages/agent-client/src/cli-output.js +57 -0
  7. package/packages/agent-client/src/cli-proxy-remote.js +350 -0
  8. package/packages/agent-client/src/cli-setup-commands.js +404 -0
  9. package/packages/agent-client/src/cli.js +63 -906
  10. package/packages/agent-client/src/command-registry.js +12 -6
  11. package/packages/agent-client/src/install.js +39 -4
  12. package/packages/agent-client/src/mcp-config.js +28 -14
  13. package/packages/agent-client/src/remotes.js +93 -15
  14. package/packages/agent-client/src/runtime.js +6 -4
  15. package/packages/mcp-server/src/bin.js +10 -1
  16. package/packages/mcp-server/src/guidance.js +1 -1
  17. package/packages/mcp-server/src/handlers-capture.js +8 -5
  18. package/packages/mcp-server/src/handlers-dom.js +2 -2
  19. package/packages/mcp-server/src/handlers-navigation.js +20 -9
  20. package/packages/mcp-server/src/handlers-page.js +111 -111
  21. package/packages/mcp-server/src/handlers-utils.js +268 -8
  22. package/packages/mcp-server/src/handlers.js +31 -10
  23. package/packages/mcp-server/src/lifecycle.js +413 -0
  24. package/packages/mcp-server/src/server.js +27 -19
  25. package/packages/native-host/src/auth-token.js +1 -1
  26. package/packages/native-host/src/config.js +9 -4
  27. package/packages/native-host/src/daemon-process.js +8 -1
  28. package/packages/protocol/src/capabilities.js +33 -110
  29. package/packages/protocol/src/capability-values.js +45 -0
  30. package/packages/protocol/src/defaults.js +19 -2
  31. package/packages/protocol/src/protocol.js +106 -5
  32. package/packages/protocol/src/registry.js +299 -86
  33. package/packages/protocol/src/types.ts +19 -1
  34. package/skills/browser-bridge/SKILL.md +1 -1
@@ -0,0 +1,350 @@
1
+ // @ts-check
2
+
3
+ import fs from 'node:fs';
4
+ import os from 'node:os';
5
+ import { randomUUID } from 'node:crypto';
6
+
7
+ import { getBridgeAuthTokenPath } from '../../native-host/src/auth-token.js';
8
+ import {
9
+ createTcpBridgeTransport,
10
+ getProxyConfigPath,
11
+ readProxyConfig,
12
+ } from '../../native-host/src/config.js';
13
+ import { pingExistingDaemon } from '../../native-host/src/daemon.js';
14
+ import { restartBridgeDaemon } from '../../native-host/src/daemon-process.js';
15
+ import { atomicWriteFile } from './atomic-write.js';
16
+ import { parseIntArg } from './cli-helpers.js';
17
+ import {
18
+ addRemoteDestination,
19
+ assertProxyBindSafety,
20
+ createBridgeClientForDestination,
21
+ isLoopbackHost,
22
+ listBridgeDestinations,
23
+ normalizeDestinationId,
24
+ parseRemoteEndpoint,
25
+ readRemoteConfig,
26
+ removeRemoteDestination,
27
+ resolveProxyEnableSettings,
28
+ } from './remotes.js';
29
+
30
+ /**
31
+ * Handle `bbx proxy <enable|disable|status>`.
32
+ *
33
+ * @param {string[]} args
34
+ * @returns {Promise<void>}
35
+ */
36
+ export async function handleProxyCommand(args) {
37
+ const [subcommand, ...restArgs] = args;
38
+ if (subcommand === 'enable') {
39
+ const options = parseProxyEnableArgs(restArgs);
40
+ const existing = readProxyConfig();
41
+ const { port, bindHost, token, tokenSource } = resolveProxyEnableSettings(
42
+ existing,
43
+ options,
44
+ randomUUID
45
+ );
46
+ assertProxyBindSafety(existing, options, bindHost);
47
+ await atomicWriteFile(getBridgeAuthTokenPath(), `${token}\n`, {
48
+ encoding: 'utf8',
49
+ mode: 0o600,
50
+ });
51
+ const configPath = getProxyConfigPath();
52
+ await atomicWriteFile(
53
+ configPath,
54
+ `${JSON.stringify(
55
+ {
56
+ enabled: true,
57
+ port,
58
+ bindHost,
59
+ token,
60
+ },
61
+ null,
62
+ 2
63
+ )}\n`,
64
+ { encoding: 'utf8', mode: 0o600 }
65
+ );
66
+ const result = await restartBridgeDaemon();
67
+ const tokenNote =
68
+ tokenSource === 'existing'
69
+ ? ' (unchanged - already-configured clients keep working; pass --rotate-token to generate a new secret)'
70
+ : tokenSource === 'generated' && existing
71
+ ? ' (rotated - update every configured client with `bbx remote add`)'
72
+ : '';
73
+ const lines = [
74
+ `Browser Bridge proxy enabled on ${bindHost}:${port}.`,
75
+ '',
76
+ `Token: ${token}${tokenNote}`,
77
+ `Config: ${configPath}`,
78
+ `Daemon: ${result.previouslyRunning ? 'restarted' : 'started'} (pid ${result.pid})`,
79
+ ];
80
+ if (tokenSource !== 'existing') {
81
+ if (isLoopbackHost(bindHost)) {
82
+ lines.push(
83
+ '',
84
+ 'On your dev machine, open an SSH local-forward:',
85
+ '',
86
+ `ssh -N -L ${port}:127.0.0.1:${port} <user>@<browser-host>`,
87
+ '',
88
+ 'Save the token to a private file, then add the tunneled remote:',
89
+ '',
90
+ `bbx remote add remote-bbx 127.0.0.1:${port} --token-file <token-file>`
91
+ );
92
+ } else {
93
+ lines.push(
94
+ '',
95
+ 'WARNING: raw TCP is exposed without transport encryption.',
96
+ 'On your dev machine, add this direct remote:',
97
+ '',
98
+ `bbx remote add remote-bbx ${getProxyExampleHost(bindHost)}:${port} --token-file <token-file>`
99
+ );
100
+ }
101
+ }
102
+ lines.push('');
103
+ process.stdout.write(lines.join('\n'));
104
+ return;
105
+ }
106
+
107
+ if (subcommand === 'disable') {
108
+ const configPath = getProxyConfigPath();
109
+ await fs.promises.rm(configPath, { force: true });
110
+ const result = await restartBridgeDaemon();
111
+ process.stdout.write(
112
+ `Browser Bridge proxy disabled. Daemon ${result.previouslyRunning ? 'restarted' : 'started'}.\n`
113
+ );
114
+ return;
115
+ }
116
+
117
+ if (subcommand === 'status') {
118
+ const config = readProxyConfig();
119
+ if (!config) {
120
+ process.stdout.write('Browser Bridge proxy is disabled.\n');
121
+ return;
122
+ }
123
+ const reachable = await pingExistingDaemon(createTcpBridgeTransport(config.port, '127.0.0.1'));
124
+ process.stdout.write(
125
+ [
126
+ `Browser Bridge proxy is enabled on ${config.bindHost}:${config.port}.`,
127
+ `Config: ${getProxyConfigPath()}`,
128
+ `Daemon: ${reachable ? 'reachable' : 'not reachable'} on 127.0.0.1:${config.port}`,
129
+ '',
130
+ ].join('\n')
131
+ );
132
+ return;
133
+ }
134
+
135
+ throw new Error('Usage: bbx proxy <enable|disable|status>');
136
+ }
137
+
138
+ /**
139
+ * Parse `bbx proxy enable` flags. Values stay undefined when not passed so an
140
+ * existing proxy config can be reused instead of silently reset to defaults.
141
+ *
142
+ * @param {string[]} args
143
+ * @returns {import('./remotes.js').ProxyEnableOptions}
144
+ */
145
+ function parseProxyEnableArgs(args) {
146
+ /** @type {import('./remotes.js').ProxyEnableOptions} */
147
+ const options = {};
148
+ for (let index = 0; index < args.length; index += 1) {
149
+ const arg = args[index];
150
+ if (arg === '--port') {
151
+ options.port = parseIntArg(args[++index] || '', 'port');
152
+ continue;
153
+ }
154
+ if (arg === '--bind-host') {
155
+ options.bindHost = args[++index] || '';
156
+ if (!options.bindHost.trim()) {
157
+ throw new Error('--bind-host requires a value.');
158
+ }
159
+ continue;
160
+ }
161
+ if (arg === '--token') {
162
+ options.token = args[++index] || '';
163
+ if (!options.token.trim()) {
164
+ throw new Error('--token requires a value.');
165
+ }
166
+ continue;
167
+ }
168
+ if (arg === '--rotate-token') {
169
+ options.rotateToken = true;
170
+ continue;
171
+ }
172
+ if (arg === '--unsafe-plaintext') {
173
+ options.unsafePlaintext = true;
174
+ continue;
175
+ }
176
+ throw new Error(`Unknown proxy enable option "${arg}".`);
177
+ }
178
+ if (options.token && options.rotateToken) {
179
+ throw new Error('Use either --token or --rotate-token, not both.');
180
+ }
181
+ if (options.port !== undefined && (options.port < 1 || options.port > 65535)) {
182
+ throw new Error('port must be an integer between 1 and 65535.');
183
+ }
184
+ return options;
185
+ }
186
+
187
+ /**
188
+ * @param {string} bindHost
189
+ * @returns {string}
190
+ */
191
+ function getProxyExampleHost(bindHost) {
192
+ if (bindHost !== '0.0.0.0' && bindHost !== '::') {
193
+ return bindHost;
194
+ }
195
+
196
+ /** @type {string | null} */
197
+ let firstIpv4 = null;
198
+ for (const [name, entries] of Object.entries(os.networkInterfaces())) {
199
+ for (const entry of entries ?? []) {
200
+ if (!entry.internal && entry.family === 'IPv4') {
201
+ if (name.toLowerCase() === 'ethernet 0') {
202
+ return entry.address;
203
+ }
204
+ firstIpv4 ??= entry.address;
205
+ }
206
+ }
207
+ }
208
+
209
+ return firstIpv4 ?? '<host>';
210
+ }
211
+
212
+ /**
213
+ * Handle `bbx remote <add|remove|list|test|destinations>`.
214
+ *
215
+ * @param {string[]} args
216
+ * @returns {Promise<void>}
217
+ */
218
+ export async function handleRemoteCommand(args) {
219
+ const [subcommand, ...restArgs] = args;
220
+ if (subcommand === 'add') {
221
+ const [name, endpoint, ...optionArgs] = restArgs;
222
+ if (!name || !endpoint) {
223
+ throw new Error(
224
+ 'Usage: bbx remote add <name> <host:port> (--token <token>|--token-file <path>)'
225
+ );
226
+ }
227
+ const token = await parseRemoteTokenOption(optionArgs);
228
+ const { host, port } = parseRemoteEndpoint(endpoint);
229
+ const remote = await addRemoteDestination({
230
+ id: normalizeDestinationId(name),
231
+ host,
232
+ port,
233
+ token,
234
+ });
235
+ process.stdout.write(
236
+ `Remote destination "${remote.id}" saved (${remote.host}:${remote.port}).\n`
237
+ );
238
+ return;
239
+ }
240
+
241
+ if (subcommand === 'remove') {
242
+ const [name] = restArgs;
243
+ if (!name) {
244
+ throw new Error('Usage: bbx remote remove <name>');
245
+ }
246
+ const removed = await removeRemoteDestination(name);
247
+ process.stdout.write(
248
+ removed
249
+ ? `Remote destination "${name}" removed.\n`
250
+ : `Remote destination "${name}" was not configured.\n`
251
+ );
252
+ return;
253
+ }
254
+
255
+ if (subcommand === 'list') {
256
+ const config = await readRemoteConfig();
257
+ if (config.remotes.length === 0) {
258
+ process.stdout.write('No remote destinations configured.\n');
259
+ return;
260
+ }
261
+ process.stdout.write(
262
+ `${config.remotes.map((remote) => `${remote.id}\t${remote.host}:${remote.port}`).join('\n')}\n`
263
+ );
264
+ return;
265
+ }
266
+
267
+ if (subcommand === 'test') {
268
+ const [name] = restArgs;
269
+ if (!name) {
270
+ throw new Error('Usage: bbx remote test <name>');
271
+ }
272
+ const remoteClient = await createBridgeClientForDestination(name);
273
+ try {
274
+ await remoteClient.connect();
275
+ const response = await remoteClient.request({ method: 'health.ping' });
276
+ if (response.ok) {
277
+ const health = /** @type {{ extensionConnected?: boolean }} */ (response.result);
278
+ process.stdout.write(
279
+ `Remote destination "${name}" is reachable (extension ${
280
+ health.extensionConnected === true ? 'connected' : 'not connected'
281
+ }).\n`
282
+ );
283
+ } else {
284
+ process.exitCode = 1;
285
+ process.stdout.write(`Remote destination "${name}" failed: ${response.error.message}\n`);
286
+ }
287
+ } catch (error) {
288
+ process.exitCode = 1;
289
+ process.stdout.write(
290
+ `Remote destination "${name}" is not reachable: ${
291
+ error instanceof Error ? error.message : String(error)
292
+ }\n`
293
+ );
294
+ } finally {
295
+ await remoteClient.close();
296
+ }
297
+ return;
298
+ }
299
+
300
+ if (subcommand === 'destinations') {
301
+ const destinations = await listBridgeDestinations();
302
+ process.stdout.write(
303
+ `${destinations
304
+ .map((destination) =>
305
+ destination.local
306
+ ? `${destination.id}\tlocal`
307
+ : `${destination.id}\t${destination.host}:${destination.port}`
308
+ )
309
+ .join('\n')}\n`
310
+ );
311
+ return;
312
+ }
313
+
314
+ throw new Error('Usage: bbx remote <add|remove|list|test|destinations>');
315
+ }
316
+
317
+ /**
318
+ * @param {string[]} args
319
+ * @returns {Promise<string>}
320
+ */
321
+ async function parseRemoteTokenOption(args) {
322
+ let token = '';
323
+ let tokenFile = '';
324
+ for (let index = 0; index < args.length; index += 1) {
325
+ const arg = args[index];
326
+ if (arg === '--token') {
327
+ const value = args[++index];
328
+ if (!value || value.startsWith('--')) {
329
+ throw new Error('--token requires a value.');
330
+ }
331
+ token = value;
332
+ continue;
333
+ }
334
+ if (arg === '--token-file') {
335
+ const value = args[++index];
336
+ if (!value || value.startsWith('--')) {
337
+ throw new Error('--token-file requires a path.');
338
+ }
339
+ tokenFile = value;
340
+ continue;
341
+ }
342
+ throw new Error(`Unknown remote add option "${arg}".`);
343
+ }
344
+ if ((token && tokenFile) || (!token.trim() && !tokenFile.trim())) {
345
+ throw new Error(
346
+ 'Usage: bbx remote add <name> <host:port> (--token <token>|--token-file <path>)'
347
+ );
348
+ }
349
+ return tokenFile ? (await fs.promises.readFile(tokenFile, 'utf8')).trim() : token.trim();
350
+ }