@browser-ai/core 2.1.5 → 2.1.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.
package/dist/index.mjs CHANGED
@@ -1056,15 +1056,17 @@ var SessionManager = class {
1056
1056
  }
1057
1057
  const sessionOptions = this.prepareSessionOptions(options);
1058
1058
  this.session = await LanguageModel.create(sessionOptions);
1059
- const onQuotaOverflow = options?.onQuotaOverflow || this.baseOptions.onQuotaOverflow;
1060
- if (onQuotaOverflow) {
1061
- this.session.addEventListener("quotaoverflow", onQuotaOverflow);
1059
+ const onOverflow = options?.onContextOverflow || this.baseOptions.onContextOverflow || options?.onQuotaOverflow || this.baseOptions.onQuotaOverflow;
1060
+ const overflowHandler = onOverflow ?? (() => {
1061
+ console.warn(
1062
+ "Model context window exceeded. Consider handling the 'contextoverflow' event."
1063
+ );
1064
+ });
1065
+ const session = this.session;
1066
+ if ("oncontextoverflow" in session) {
1067
+ session.addEventListener("contextoverflow", overflowHandler);
1062
1068
  } else {
1063
- this.session.addEventListener("quotaoverflow", () => {
1064
- console.warn(
1065
- "Model quota exceeded. Consider handling 'quotaoverflow' event."
1066
- );
1067
- });
1069
+ session.addEventListener("quotaoverflow", overflowHandler);
1068
1070
  }
1069
1071
  return this.session;
1070
1072
  }
@@ -1133,18 +1135,34 @@ var SessionManager = class {
1133
1135
  this.session = null;
1134
1136
  }
1135
1137
  /**
1136
- * Gets the input quota for the current session, if available
1137
- * @returns The input quota or undefined if not available
1138
+ * Gets the context window size (token limit) for the current session, if available.
1139
+ * @returns The context window size or undefined if not available
1140
+ */
1141
+ getContextWindow() {
1142
+ const session = this.getCurrentSession();
1143
+ if (!session) return void 0;
1144
+ return session.contextWindow ?? session.inputQuota;
1145
+ }
1146
+ /**
1147
+ * Gets the current context usage (tokens consumed) for the current session, if available.
1148
+ * @returns The context usage or undefined if not available
1149
+ */
1150
+ getContextUsage() {
1151
+ const session = this.getCurrentSession();
1152
+ if (!session) return void 0;
1153
+ return session.contextUsage ?? session.inputUsage;
1154
+ }
1155
+ /**
1156
+ * @deprecated Use {@link getContextWindow} instead.
1138
1157
  */
1139
1158
  getInputQuota() {
1140
- return this.getCurrentSession()?.inputQuota;
1159
+ return this.getContextWindow();
1141
1160
  }
1142
1161
  /**
1143
- * Gets the input usage for the current session, if available
1144
- * @returns The input usage or undefined if not available
1162
+ * @deprecated Use {@link getContextUsage} instead.
1145
1163
  */
1146
1164
  getInputUsage() {
1147
- return this.getCurrentSession()?.inputUsage;
1165
+ return this.getContextUsage();
1148
1166
  }
1149
1167
  /**
1150
1168
  * Prepares merged session options from base config and request options
@@ -1160,7 +1178,8 @@ var SessionManager = class {
1160
1178
  systemMessage,
1161
1179
  expectedInputs,
1162
1180
  onDownloadProgress,
1163
- onQuotaOverflow,
1181
+ onContextOverflow: _onContextOverflow,
1182
+ onQuotaOverflow: _onQuotaOverflow,
1164
1183
  ...createOptions
1165
1184
  } = options;
1166
1185
  Object.assign(mergedOptions, createOptions);
@@ -1374,18 +1393,30 @@ var BrowserAIChatLanguageModel = class {
1374
1393
  };
1375
1394
  }
1376
1395
  /**
1377
- * Gets the input usage for the current session, if available
1378
- * @returns The input usage or undefined if not available
1396
+ * Gets the current context usage (tokens consumed) for the current session, if available
1397
+ * @returns The context usage or undefined if not available
1398
+ */
1399
+ getContextUsage() {
1400
+ return this.sessionManager.getContextUsage();
1401
+ }
1402
+ /**
1403
+ * Gets the context window size (token limit) for the current session, if available
1404
+ * @returns The context window size or undefined if not available
1405
+ */
1406
+ getContextWindow() {
1407
+ return this.sessionManager.getContextWindow();
1408
+ }
1409
+ /**
1410
+ * @deprecated Use {@link getContextUsage} instead.
1379
1411
  */
1380
1412
  getInputUsage() {
1381
- return this.sessionManager.getInputUsage();
1413
+ return this.getContextUsage();
1382
1414
  }
1383
1415
  /**
1384
- * Gets the input quota for the current session, if available
1385
- * @returns The input quota or undefined if not available
1416
+ * @deprecated Use {@link getContextWindow} instead.
1386
1417
  */
1387
1418
  getInputQuota() {
1388
- return this.sessionManager.getInputQuota();
1419
+ return this.getContextWindow();
1389
1420
  }
1390
1421
  /**
1391
1422
  * Check the availability of the browser AI model
@@ -1494,7 +1525,7 @@ var BrowserAIChatLanguageModel = class {
1494
1525
  finishReason,
1495
1526
  usage: {
1496
1527
  inputTokens: {
1497
- total: session.inputUsage,
1528
+ total: session.contextUsage ?? session.inputUsage,
1498
1529
  noCache: void 0,
1499
1530
  cacheRead: void 0,
1500
1531
  cacheWrite: void 0