@browser-ai/core 2.1.1 → 2.1.3

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
@@ -454,6 +454,91 @@ var ToolCallFenceDetector = class {
454
454
  }
455
455
  };
456
456
 
457
+ // ../shared/src/streaming/tool-call-stream-utils.ts
458
+ function extractToolName(content) {
459
+ const jsonMatch = content.match(/\{\s*"name"\s*:\s*"([^"]+)"/);
460
+ if (jsonMatch) {
461
+ return jsonMatch[1];
462
+ }
463
+ return null;
464
+ }
465
+ var ARGUMENTS_FIELD_REGEX = /"arguments"\s*:\s*/g;
466
+ var ARGUMENTS_SEARCH_OVERLAP = 32;
467
+ function createArgumentsStreamState() {
468
+ return {
469
+ searchFrom: 0,
470
+ valueStartIndex: null,
471
+ parseIndex: 0,
472
+ started: false,
473
+ depth: 0,
474
+ inString: false,
475
+ escaped: false,
476
+ complete: false
477
+ };
478
+ }
479
+ function extractArgumentsDelta(content, state) {
480
+ if (state.complete) {
481
+ return "";
482
+ }
483
+ if (state.valueStartIndex === null) {
484
+ ARGUMENTS_FIELD_REGEX.lastIndex = state.searchFrom;
485
+ const match = ARGUMENTS_FIELD_REGEX.exec(content);
486
+ ARGUMENTS_FIELD_REGEX.lastIndex = 0;
487
+ if (!match || match.index === void 0) {
488
+ state.searchFrom = Math.max(0, content.length - ARGUMENTS_SEARCH_OVERLAP);
489
+ return "";
490
+ }
491
+ state.valueStartIndex = match.index + match[0].length;
492
+ state.parseIndex = state.valueStartIndex;
493
+ state.searchFrom = state.valueStartIndex;
494
+ }
495
+ if (state.parseIndex >= content.length) {
496
+ return "";
497
+ }
498
+ let delta = "";
499
+ for (let i = state.parseIndex; i < content.length; i++) {
500
+ const char = content[i];
501
+ delta += char;
502
+ if (!state.started) {
503
+ if (!/\s/.test(char)) {
504
+ state.started = true;
505
+ if (char === "{" || char === "[") {
506
+ state.depth = 1;
507
+ }
508
+ }
509
+ continue;
510
+ }
511
+ if (state.escaped) {
512
+ state.escaped = false;
513
+ continue;
514
+ }
515
+ if (char === "\\") {
516
+ state.escaped = true;
517
+ continue;
518
+ }
519
+ if (char === '"') {
520
+ state.inString = !state.inString;
521
+ continue;
522
+ }
523
+ if (!state.inString) {
524
+ if (char === "{" || char === "[") {
525
+ state.depth += 1;
526
+ } else if (char === "}" || char === "]") {
527
+ if (state.depth > 0) {
528
+ state.depth -= 1;
529
+ if (state.depth === 0) {
530
+ state.parseIndex = i + 1;
531
+ state.complete = true;
532
+ return delta;
533
+ }
534
+ }
535
+ }
536
+ }
537
+ }
538
+ state.parseIndex = content.length;
539
+ return delta;
540
+ }
541
+
457
542
  // src/convert-to-browser-ai-messages.ts
458
543
  import {
459
544
  UnsupportedFunctionalityError
@@ -884,6 +969,20 @@ var SessionManager = class {
884
969
  }
885
970
  this.session = null;
886
971
  }
972
+ /**
973
+ * Gets the input quota for the current session, if available
974
+ * @returns The input quota or undefined if not available
975
+ */
976
+ getInputQuota() {
977
+ return this.getCurrentSession()?.inputQuota;
978
+ }
979
+ /**
980
+ * Gets the input usage for the current session, if available
981
+ * @returns The input usage or undefined if not available
982
+ */
983
+ getInputUsage() {
984
+ return this.getCurrentSession()?.inputUsage;
985
+ }
887
986
  /**
888
987
  * Prepares merged session options from base config and request options
889
988
  *
@@ -935,63 +1034,6 @@ var SessionManager = class {
935
1034
  function doesBrowserSupportBrowserAI() {
936
1035
  return typeof LanguageModel !== "undefined";
937
1036
  }
938
- function extractToolName(content) {
939
- const jsonMatch = content.match(/\{\s*"name"\s*:\s*"([^"]+)"/);
940
- if (jsonMatch) {
941
- return jsonMatch[1];
942
- }
943
- return null;
944
- }
945
- function extractArgumentsContent(content) {
946
- const match = content.match(/"arguments"\s*:\s*/);
947
- if (!match || match.index === void 0) {
948
- return "";
949
- }
950
- const startIndex = match.index + match[0].length;
951
- let result = "";
952
- let depth = 0;
953
- let inString = false;
954
- let escaped = false;
955
- let started = false;
956
- for (let i = startIndex; i < content.length; i++) {
957
- const char = content[i];
958
- result += char;
959
- if (!started) {
960
- if (!/\s/.test(char)) {
961
- started = true;
962
- if (char === "{" || char === "[") {
963
- depth = 1;
964
- }
965
- }
966
- continue;
967
- }
968
- if (escaped) {
969
- escaped = false;
970
- continue;
971
- }
972
- if (char === "\\") {
973
- escaped = true;
974
- continue;
975
- }
976
- if (char === '"') {
977
- inString = !inString;
978
- continue;
979
- }
980
- if (!inString) {
981
- if (char === "{" || char === "[") {
982
- depth += 1;
983
- } else if (char === "}" || char === "]") {
984
- if (depth > 0) {
985
- depth -= 1;
986
- if (depth === 0) {
987
- break;
988
- }
989
- }
990
- }
991
- }
992
- }
993
- return result;
994
- }
995
1037
  var BrowserAIChatLanguageModel = class {
996
1038
  constructor(modelId, options = {}) {
997
1039
  this.specificationVersion = "v3";
@@ -1177,6 +1219,20 @@ var BrowserAIChatLanguageModel = class {
1177
1219
  warnings
1178
1220
  };
1179
1221
  }
1222
+ /**
1223
+ * Gets the input usage for the current session, if available
1224
+ * @returns The input usage or undefined if not available
1225
+ */
1226
+ getInputUsage() {
1227
+ return this.sessionManager.getInputUsage();
1228
+ }
1229
+ /**
1230
+ * Gets the input quota for the current session, if available
1231
+ * @returns The input quota or undefined if not available
1232
+ */
1233
+ getInputQuota() {
1234
+ return this.sessionManager.getInputQuota();
1235
+ }
1180
1236
  /**
1181
1237
  * Check the availability of the browser AI model
1182
1238
  * @returns Promise resolving to "unavailable", "available", or "available-after-download"
@@ -1328,7 +1384,7 @@ var BrowserAIChatLanguageModel = class {
1328
1384
  let currentToolCallId = null;
1329
1385
  let toolInputStartEmitted = false;
1330
1386
  let accumulatedFenceContent = "";
1331
- let streamedArgumentsLength = 0;
1387
+ let argumentsStreamState = createArgumentsStreamState();
1332
1388
  let insideFence = false;
1333
1389
  while (!aborted) {
1334
1390
  const { done, value } = await currentReader.read();
@@ -1349,7 +1405,7 @@ var BrowserAIChatLanguageModel = class {
1349
1405
  currentToolCallId = `call_${Date.now()}_${Math.random().toString(36).slice(2, 9)}`;
1350
1406
  toolInputStartEmitted = false;
1351
1407
  accumulatedFenceContent = "";
1352
- streamedArgumentsLength = 0;
1408
+ argumentsStreamState = createArgumentsStreamState();
1353
1409
  insideFence = true;
1354
1410
  continue;
1355
1411
  }
@@ -1359,19 +1415,16 @@ var BrowserAIChatLanguageModel = class {
1359
1415
  accumulatedFenceContent += result.safeContent;
1360
1416
  }
1361
1417
  if (toolInputStartEmitted && currentToolCallId) {
1362
- const argsContent = extractArgumentsContent(
1363
- accumulatedFenceContent
1418
+ const delta = extractArgumentsDelta(
1419
+ accumulatedFenceContent,
1420
+ argumentsStreamState
1364
1421
  );
1365
- if (argsContent.length > streamedArgumentsLength) {
1366
- const delta = argsContent.slice(streamedArgumentsLength);
1367
- streamedArgumentsLength = argsContent.length;
1368
- if (delta.length > 0) {
1369
- controller.enqueue({
1370
- type: "tool-input-delta",
1371
- id: currentToolCallId,
1372
- delta
1373
- });
1374
- }
1422
+ if (delta.length > 0) {
1423
+ controller.enqueue({
1424
+ type: "tool-input-delta",
1425
+ id: currentToolCallId,
1426
+ delta
1427
+ });
1375
1428
  }
1376
1429
  }
1377
1430
  const parsed = parseJsonFunctionCalls(result.completeFence);
@@ -1387,7 +1440,7 @@ var BrowserAIChatLanguageModel = class {
1387
1440
  currentToolCallId = null;
1388
1441
  toolInputStartEmitted = false;
1389
1442
  accumulatedFenceContent = "";
1390
- streamedArgumentsLength = 0;
1443
+ argumentsStreamState = createArgumentsStreamState();
1391
1444
  insideFence = false;
1392
1445
  continue;
1393
1446
  }
@@ -1409,21 +1462,16 @@ var BrowserAIChatLanguageModel = class {
1409
1462
  });
1410
1463
  toolInputStartEmitted = true;
1411
1464
  }
1412
- const argsContent = extractArgumentsContent(
1413
- accumulatedFenceContent
1465
+ const delta = extractArgumentsDelta(
1466
+ accumulatedFenceContent,
1467
+ argumentsStreamState
1414
1468
  );
1415
- if (argsContent.length > streamedArgumentsLength) {
1416
- const delta = argsContent.slice(
1417
- streamedArgumentsLength
1418
- );
1419
- streamedArgumentsLength = argsContent.length;
1420
- if (delta.length > 0) {
1421
- controller.enqueue({
1422
- type: "tool-input-delta",
1423
- id: toolCallId,
1424
- delta
1425
- });
1426
- }
1469
+ if (delta.length > 0) {
1470
+ controller.enqueue({
1471
+ type: "tool-input-delta",
1472
+ id: toolCallId,
1473
+ delta
1474
+ });
1427
1475
  }
1428
1476
  } else {
1429
1477
  controller.enqueue({
@@ -1460,7 +1508,7 @@ var BrowserAIChatLanguageModel = class {
1460
1508
  currentToolCallId = null;
1461
1509
  toolInputStartEmitted = false;
1462
1510
  accumulatedFenceContent = "";
1463
- streamedArgumentsLength = 0;
1511
+ argumentsStreamState = createArgumentsStreamState();
1464
1512
  insideFence = false;
1465
1513
  continue;
1466
1514
  }
@@ -1478,21 +1526,16 @@ var BrowserAIChatLanguageModel = class {
1478
1526
  toolInputStartEmitted = true;
1479
1527
  }
1480
1528
  if (toolInputStartEmitted && currentToolCallId) {
1481
- const argsContent = extractArgumentsContent(
1482
- accumulatedFenceContent
1529
+ const delta = extractArgumentsDelta(
1530
+ accumulatedFenceContent,
1531
+ argumentsStreamState
1483
1532
  );
1484
- if (argsContent.length > streamedArgumentsLength) {
1485
- const delta = argsContent.slice(
1486
- streamedArgumentsLength
1487
- );
1488
- streamedArgumentsLength = argsContent.length;
1489
- if (delta.length > 0) {
1490
- controller.enqueue({
1491
- type: "tool-input-delta",
1492
- id: currentToolCallId,
1493
- delta
1494
- });
1495
- }
1533
+ if (delta.length > 0) {
1534
+ controller.enqueue({
1535
+ type: "tool-input-delta",
1536
+ id: currentToolCallId,
1537
+ delta
1538
+ });
1496
1539
  }
1497
1540
  }
1498
1541
  }