agentic-browser 2.6.1 → 2.7.0

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/index.mjs CHANGED
@@ -1,3 +1,2 @@
1
1
  #!/usr/bin/env node
2
- import { i as createMockAgenticBrowserCore, n as createAgenticBrowserCore, t as AgenticBrowserCore } from "./runtime-BzdbtFF4.mjs";
3
- export { AgenticBrowserCore, createAgenticBrowserCore, createMockAgenticBrowserCore };
2
+ export { t as AgenticBrowserCore, n as createAgenticBrowserCore, i as createMockAgenticBrowserCore } from "./runtime-CfQ0xGAm.mjs";
@@ -1,5 +1,2 @@
1
1
 
2
- //#region src/mcp/index.d.ts
3
- declare function main(): Promise<void>;
4
- //#endregion
5
- export { main };
2
+ export declare function main(): Promise<void>;
@@ -1,12 +1,12 @@
1
1
  #!/usr/bin/env node
2
- import { n as createAgenticBrowserCore } from "../runtime-BzdbtFF4.mjs";
2
+ import { n as createAgenticBrowserCore } from "../runtime-CfQ0xGAm.mjs";
3
3
  import { readFileSync } from "node:fs";
4
4
  import { dirname, join } from "node:path";
5
5
  import { fileURLToPath } from "node:url";
6
6
  import { z } from "zod";
7
7
  import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
8
8
  import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
9
- //#region src/mcp/response-helpers.ts
9
+ // src/mcp/response-helpers.ts
10
10
  function uniqueLines(content, maxLineLength = 160) {
11
11
  const seen = /* @__PURE__ */ new Set();
12
12
  const lines = [];
@@ -204,8 +204,7 @@ function compactMemoryResults(results) {
204
204
  return compact;
205
205
  }) };
206
206
  }
207
- //#endregion
208
- //#region src/mcp/element-refs.ts
207
+ // src/mcp/element-refs.ts
209
208
  var ElementRefRegistry = class {
210
209
  bySession = /* @__PURE__ */ new Map();
211
210
  /**
@@ -245,8 +244,41 @@ var ElementRefRegistry = class {
245
244
  this.bySession.delete(sessionId);
246
245
  }
247
246
  };
248
- //#endregion
249
- //#region src/mcp/index.ts
247
+ // src/mcp/session-resolver.ts
248
+ /**
249
+ * Tracks the active session and auto-starts one on demand. Concurrent callers
250
+ * share a single in-flight start so only one session is launched.
251
+ */
252
+ function createSessionResolver(start) {
253
+ let activeSessionId;
254
+ let pendingSession;
255
+ return {
256
+ async resolve(sessionId) {
257
+ if (sessionId) return sessionId;
258
+ if (activeSessionId) return activeSessionId;
259
+ if (pendingSession) return pendingSession;
260
+ const p = (async () => {
261
+ const session = await start();
262
+ activeSessionId = session.sessionId;
263
+ return session.sessionId;
264
+ })();
265
+ pendingSession = p;
266
+ try {
267
+ return await p;
268
+ } finally {
269
+ pendingSession = void 0;
270
+ }
271
+ },
272
+ current: () => activeSessionId,
273
+ set(sessionId) {
274
+ activeSessionId = sessionId;
275
+ },
276
+ clear() {
277
+ activeSessionId = void 0;
278
+ }
279
+ };
280
+ }
281
+ // src/mcp/index.ts
250
282
  function readPackageVersion() {
251
283
  try {
252
284
  const dir = dirname(fileURLToPath(import.meta.url));
@@ -271,7 +303,7 @@ function classifyError(message) {
271
303
  return "UNKNOWN";
272
304
  }
273
305
  let core;
274
- let activeSessionId;
306
+ const sessionResolver = createSessionResolver(() => getCore().startSession());
275
307
  const DEFAULT_MCP_MAX_CHARS = {
276
308
  summary: void 0,
277
309
  title: void 0,
@@ -313,10 +345,7 @@ async function snapshotElements(sessionId, opts = {}) {
313
345
  * This means the LLM never has to call browser_start_session explicitly.
314
346
  */
315
347
  async function resolveSession(sessionId) {
316
- if (sessionId) return sessionId;
317
- if (activeSessionId) return activeSessionId;
318
- activeSessionId = (await getCore().startSession()).sessionId;
319
- return activeSessionId;
348
+ return sessionResolver.resolve(sessionId);
320
349
  }
321
350
  const server = new McpServer({
322
351
  name: "agentic-browser",
@@ -324,7 +353,7 @@ const server = new McpServer({
324
353
  });
325
354
  server.tool("browser_start_session", "Start a new Chrome browser session (or return the existing one if healthy). Sessions auto-start when you call any other browser tool, so you rarely need to call this explicitly. Use this to force a fresh session after stopping the previous one.", {}, async () => {
326
355
  const session = await getCore().startSession();
327
- activeSessionId = session.sessionId;
356
+ sessionResolver.set(session.sessionId);
328
357
  const { authTokenRef: _, ...compactSession } = session;
329
358
  return { content: [{
330
359
  type: "text",
@@ -536,7 +565,7 @@ server.tool("browser_screenshot", "Capture a screenshot of the current page. Ret
536
565
  }] };
537
566
  });
538
567
  server.tool("browser_stop_session", "Stop the browser session and terminate Chrome. The next browser tool call will auto-start a fresh session.", { sessionId: z.string().optional().describe("Session ID (uses active session if omitted)") }, async ({ sessionId }) => {
539
- const sid = sessionId ?? activeSessionId;
568
+ const sid = sessionId ?? sessionResolver.current();
540
569
  if (!sid) return { content: [{
541
570
  type: "text",
542
571
  text: JSON.stringify({
@@ -546,7 +575,7 @@ server.tool("browser_stop_session", "Stop the browser session and terminate Chro
546
575
  }] };
547
576
  await getCore().stopSession(sid);
548
577
  refs.clear(sid);
549
- if (activeSessionId === sid) activeSessionId = void 0;
578
+ if (sessionResolver.current() === sid) sessionResolver.clear();
550
579
  return { content: [{
551
580
  type: "text",
552
581
  text: JSON.stringify({
@@ -558,11 +587,12 @@ server.tool("browser_stop_session", "Stop the browser session and terminate Chro
558
587
  async function main() {
559
588
  const transport = new StdioServerTransport();
560
589
  transport.onclose = async () => {
561
- if (activeSessionId) {
590
+ const activeId = sessionResolver.current();
591
+ if (activeId) {
562
592
  try {
563
- await getCore().stopSession(activeSessionId);
593
+ await getCore().stopSession(activeId);
564
594
  } catch {}
565
- activeSessionId = void 0;
595
+ sessionResolver.clear();
566
596
  }
567
597
  try {
568
598
  getCore().context.memoryService.flushSync();
@@ -573,5 +603,4 @@ async function main() {
573
603
  };
574
604
  await server.connect(transport);
575
605
  }
576
- //#endregion
577
606
  export { main };