bb-browser 0.4.0 → 0.4.2

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/daemon.js CHANGED
@@ -4,7 +4,7 @@ import {
4
4
  DAEMON_HOST,
5
5
  DAEMON_PORT,
6
6
  SSE_HEARTBEAT_INTERVAL
7
- } from "./chunk-CWLDHQGR.js";
7
+ } from "./chunk-H7M4J4CW.js";
8
8
 
9
9
  // packages/daemon/src/index.ts
10
10
  import { parseArgs } from "util";
package/dist/mcp.js CHANGED
@@ -6,7 +6,7 @@ import {
6
6
  __export,
7
7
  __toESM,
8
8
  generateId
9
- } from "./chunk-CWLDHQGR.js";
9
+ } from "./chunk-H7M4J4CW.js";
10
10
 
11
11
  // node_modules/.pnpm/ajv@8.18.0/node_modules/ajv/dist/compile/codegen/code.js
12
12
  var require_code = __commonJS({
@@ -2959,7 +2959,7 @@ var require_compile = __commonJS({
2959
2959
  const schOrFunc = root.refs[ref];
2960
2960
  if (schOrFunc)
2961
2961
  return schOrFunc;
2962
- let _sch = resolve.call(this, root, ref);
2962
+ let _sch = resolve2.call(this, root, ref);
2963
2963
  if (_sch === void 0) {
2964
2964
  const schema = (_a = root.localRefs) === null || _a === void 0 ? void 0 : _a[ref];
2965
2965
  const { schemaId } = this.opts;
@@ -2986,7 +2986,7 @@ var require_compile = __commonJS({
2986
2986
  function sameSchemaEnv(s1, s2) {
2987
2987
  return s1.schema === s2.schema && s1.root === s2.root && s1.baseId === s2.baseId;
2988
2988
  }
2989
- function resolve(root, ref) {
2989
+ function resolve2(root, ref) {
2990
2990
  let sch;
2991
2991
  while (typeof (sch = this.refs[ref]) == "string")
2992
2992
  ref = sch;
@@ -3561,7 +3561,7 @@ var require_fast_uri = __commonJS({
3561
3561
  }
3562
3562
  return uri;
3563
3563
  }
3564
- function resolve(baseURI, relativeURI, options) {
3564
+ function resolve2(baseURI, relativeURI, options) {
3565
3565
  const schemelessOptions = options ? Object.assign({ scheme: "null" }, options) : { scheme: "null" };
3566
3566
  const resolved = resolveComponent(parse3(baseURI, schemelessOptions), parse3(relativeURI, schemelessOptions), schemelessOptions, true);
3567
3567
  schemelessOptions.skipEscape = true;
@@ -3788,7 +3788,7 @@ var require_fast_uri = __commonJS({
3788
3788
  var fastUri = {
3789
3789
  SCHEMES,
3790
3790
  normalize,
3791
- resolve,
3791
+ resolve: resolve2,
3792
3792
  resolveComponent,
3793
3793
  equal,
3794
3794
  serialize,
@@ -18262,13 +18262,13 @@ var zodToJsonSchema = (schema, options) => {
18262
18262
  }, true) ?? parseAnyDef(refs)
18263
18263
  }), {}) : void 0;
18264
18264
  const name = typeof options === "string" ? options : options?.nameStrategy === "title" ? void 0 : options?.name;
18265
- const main2 = parseDef(schema._def, name === void 0 ? refs : {
18265
+ const main = parseDef(schema._def, name === void 0 ? refs : {
18266
18266
  ...refs,
18267
18267
  currentPath: [...refs.basePath, refs.definitionPath, name]
18268
18268
  }, false) ?? parseAnyDef(refs);
18269
18269
  const title = typeof options === "object" && options.name !== void 0 && options.nameStrategy === "title" ? options.name : void 0;
18270
18270
  if (title !== void 0) {
18271
- main2.title = title;
18271
+ main.title = title;
18272
18272
  }
18273
18273
  if (refs.flags.hasReferencedOpenAiAnyType) {
18274
18274
  if (!definitions) {
@@ -18289,9 +18289,9 @@ var zodToJsonSchema = (schema, options) => {
18289
18289
  }
18290
18290
  }
18291
18291
  const combined = name === void 0 ? definitions ? {
18292
- ...main2,
18292
+ ...main,
18293
18293
  [refs.definitionPath]: definitions
18294
- } : main2 : {
18294
+ } : main : {
18295
18295
  $ref: [
18296
18296
  ...refs.$refStrategy === "relative" ? [] : refs.basePath,
18297
18297
  refs.definitionPath,
@@ -18299,7 +18299,7 @@ var zodToJsonSchema = (schema, options) => {
18299
18299
  ].join("/"),
18300
18300
  [refs.definitionPath]: {
18301
18301
  ...definitions,
18302
- [name]: main2
18302
+ [name]: main
18303
18303
  }
18304
18304
  };
18305
18305
  if (refs.target === "jsonSchema7") {
@@ -18849,7 +18849,7 @@ var Protocol = class {
18849
18849
  return;
18850
18850
  }
18851
18851
  const pollInterval = task2.pollInterval ?? this._options?.defaultTaskPollInterval ?? 1e3;
18852
- await new Promise((resolve) => setTimeout(resolve, pollInterval));
18852
+ await new Promise((resolve2) => setTimeout(resolve2, pollInterval));
18853
18853
  options?.signal?.throwIfAborted();
18854
18854
  }
18855
18855
  } catch (error2) {
@@ -18866,7 +18866,7 @@ var Protocol = class {
18866
18866
  */
18867
18867
  request(request, resultSchema, options) {
18868
18868
  const { relatedRequestId, resumptionToken, onresumptiontoken, task, relatedTask } = options ?? {};
18869
- return new Promise((resolve, reject) => {
18869
+ return new Promise((resolve2, reject) => {
18870
18870
  const earlyReject = (error2) => {
18871
18871
  reject(error2);
18872
18872
  };
@@ -18944,7 +18944,7 @@ var Protocol = class {
18944
18944
  if (!parseResult.success) {
18945
18945
  reject(parseResult.error);
18946
18946
  } else {
18947
- resolve(parseResult.data);
18947
+ resolve2(parseResult.data);
18948
18948
  }
18949
18949
  } catch (error2) {
18950
18950
  reject(error2);
@@ -19205,12 +19205,12 @@ var Protocol = class {
19205
19205
  }
19206
19206
  } catch {
19207
19207
  }
19208
- return new Promise((resolve, reject) => {
19208
+ return new Promise((resolve2, reject) => {
19209
19209
  if (signal.aborted) {
19210
19210
  reject(new McpError(ErrorCode.InvalidRequest, "Request cancelled"));
19211
19211
  return;
19212
19212
  }
19213
- const timeoutId = setTimeout(resolve, interval);
19213
+ const timeoutId = setTimeout(resolve2, interval);
19214
19214
  signal.addEventListener("abort", () => {
19215
19215
  clearTimeout(timeoutId);
19216
19216
  reject(new McpError(ErrorCode.InvalidRequest, "Request cancelled"));
@@ -20310,7 +20310,7 @@ var McpServer = class {
20310
20310
  let task = createTaskResult.task;
20311
20311
  const pollInterval = task.pollInterval ?? 5e3;
20312
20312
  while (task.status !== "completed" && task.status !== "failed" && task.status !== "cancelled") {
20313
- await new Promise((resolve) => setTimeout(resolve, pollInterval));
20313
+ await new Promise((resolve2) => setTimeout(resolve2, pollInterval));
20314
20314
  const updatedTask = await extra.taskStore.getTask(taskId);
20315
20315
  if (!updatedTask) {
20316
20316
  throw new McpError(ErrorCode.InternalError, `Task ${taskId} not found during polling`);
@@ -20953,19 +20953,62 @@ var StdioServerTransport = class {
20953
20953
  this.onclose?.();
20954
20954
  }
20955
20955
  send(message) {
20956
- return new Promise((resolve) => {
20956
+ return new Promise((resolve2) => {
20957
20957
  const json = serializeMessage(message);
20958
20958
  if (this._stdout.write(json)) {
20959
- resolve();
20959
+ resolve2();
20960
20960
  } else {
20961
- this._stdout.once("drain", resolve);
20961
+ this._stdout.once("drain", resolve2);
20962
20962
  }
20963
20963
  });
20964
20964
  }
20965
20965
  };
20966
20966
 
20967
20967
  // packages/mcp/src/index.ts
20968
+ import { spawn } from "child_process";
20969
+ import { existsSync } from "fs";
20970
+ import { fileURLToPath } from "url";
20971
+ import { dirname, resolve } from "path";
20972
+ var EXT_HINT = [
20973
+ "Chrome extension not connected.",
20974
+ "",
20975
+ "1. Download extension: https://github.com/epiral/bb-browser/releases/latest",
20976
+ "2. Unzip the downloaded file",
20977
+ "3. Open chrome://extensions/ \u2192 Enable Developer Mode",
20978
+ '4. Click "Load unpacked" \u2192 select the unzipped folder'
20979
+ ].join("\n");
20980
+ function getDaemonPath() {
20981
+ const currentDir = dirname(fileURLToPath(import.meta.url));
20982
+ const sameDirPath = resolve(currentDir, "daemon.js");
20983
+ if (existsSync(sameDirPath)) return sameDirPath;
20984
+ return resolve(currentDir, "../../daemon/dist/index.js");
20985
+ }
20986
+ async function isDaemonRunning() {
20987
+ try {
20988
+ const controller = new AbortController();
20989
+ const t = setTimeout(() => controller.abort(), 2e3);
20990
+ const res = await fetch(`${DAEMON_BASE_URL}/status`, { signal: controller.signal });
20991
+ clearTimeout(t);
20992
+ return res.ok;
20993
+ } catch {
20994
+ return false;
20995
+ }
20996
+ }
20997
+ async function ensureDaemon() {
20998
+ if (await isDaemonRunning()) return;
20999
+ const child = spawn(process.execPath, [getDaemonPath()], {
21000
+ detached: true,
21001
+ stdio: "ignore",
21002
+ env: { ...process.env }
21003
+ });
21004
+ child.unref();
21005
+ for (let i = 0; i < 25; i++) {
21006
+ await new Promise((r) => setTimeout(r, 200));
21007
+ if (await isDaemonRunning()) return;
21008
+ }
21009
+ }
20968
21010
  async function sendCommand(request) {
21011
+ await ensureDaemon();
20969
21012
  const controller = new AbortController();
20970
21013
  const timeoutId = setTimeout(() => controller.abort(), COMMAND_TIMEOUT);
20971
21014
  try {
@@ -20976,10 +21019,13 @@ async function sendCommand(request) {
20976
21019
  signal: controller.signal
20977
21020
  });
20978
21021
  clearTimeout(timeoutId);
21022
+ if (response.status === 503) {
21023
+ return { id: request.id, success: false, error: EXT_HINT };
21024
+ }
20979
21025
  return await response.json();
20980
21026
  } catch {
20981
21027
  clearTimeout(timeoutId);
20982
- return { id: request.id, success: false, error: "Cannot connect to daemon" };
21028
+ return { id: request.id, success: false, error: "Failed to start daemon. Run manually: bb-browser daemon" };
20983
21029
  }
20984
21030
  }
20985
21031
  function errorResult(message) {
@@ -21229,12 +21275,15 @@ server.tool(
21229
21275
  return textResult(resp.data || `Waited ${time3}ms`);
21230
21276
  }
21231
21277
  );
21232
- async function main() {
21278
+ async function startMcpServer() {
21233
21279
  const transport = new StdioServerTransport();
21234
21280
  await server.connect(transport);
21235
21281
  }
21236
- main().catch((error2) => {
21282
+ startMcpServer().catch((error2) => {
21237
21283
  console.error(error2);
21238
21284
  process.exit(1);
21239
21285
  });
21286
+ export {
21287
+ startMcpServer
21288
+ };
21240
21289
  //# sourceMappingURL=mcp.js.map