@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.d.mts CHANGED
@@ -18,7 +18,13 @@ interface BrowserAIChatSettings extends LanguageModelCreateOptions {
18
18
  languages?: string[];
19
19
  }>;
20
20
  /**
21
- * Callback invoked when the model quota is exceeded.
21
+ * Callback invoked when the model context window is exceeded.
22
+ * Replaces the deprecated `onQuotaOverflow`.
23
+ * @see [Prompt API Context Overflow](https://github.com/webmachinelearning/prompt-api?tab=readme-ov-file#tokenization-context-window-length-limits-and-overflow)
24
+ */
25
+ onContextOverflow?: (event: Event) => void;
26
+ /**
27
+ * @deprecated Use `onContextOverflow` instead.
22
28
  * @see [Prompt API Quota Overflow](https://github.com/webmachinelearning/prompt-api?tab=readme-ov-file#tokenization-context-window-length-limits-and-overflow)
23
29
  * @param event
24
30
  */
@@ -53,13 +59,21 @@ declare class BrowserAIChatLanguageModel implements LanguageModelV3 {
53
59
  */
54
60
  doGenerate(options: LanguageModelV3CallOptions): Promise<LanguageModelV3GenerateResult>;
55
61
  /**
56
- * Gets the input usage for the current session, if available
57
- * @returns The input usage or undefined if not available
62
+ * Gets the current context usage (tokens consumed) for the current session, if available
63
+ * @returns The context usage or undefined if not available
64
+ */
65
+ getContextUsage(): number | undefined;
66
+ /**
67
+ * Gets the context window size (token limit) for the current session, if available
68
+ * @returns The context window size or undefined if not available
69
+ */
70
+ getContextWindow(): number | undefined;
71
+ /**
72
+ * @deprecated Use {@link getContextUsage} instead.
58
73
  */
59
74
  getInputUsage(): number | undefined;
60
75
  /**
61
- * Gets the input quota for the current session, if available
62
- * @returns The input quota or undefined if not available
76
+ * @deprecated Use {@link getContextWindow} instead.
63
77
  */
64
78
  getInputQuota(): number | undefined;
65
79
  /**
package/dist/index.d.ts CHANGED
@@ -18,7 +18,13 @@ interface BrowserAIChatSettings extends LanguageModelCreateOptions {
18
18
  languages?: string[];
19
19
  }>;
20
20
  /**
21
- * Callback invoked when the model quota is exceeded.
21
+ * Callback invoked when the model context window is exceeded.
22
+ * Replaces the deprecated `onQuotaOverflow`.
23
+ * @see [Prompt API Context Overflow](https://github.com/webmachinelearning/prompt-api?tab=readme-ov-file#tokenization-context-window-length-limits-and-overflow)
24
+ */
25
+ onContextOverflow?: (event: Event) => void;
26
+ /**
27
+ * @deprecated Use `onContextOverflow` instead.
22
28
  * @see [Prompt API Quota Overflow](https://github.com/webmachinelearning/prompt-api?tab=readme-ov-file#tokenization-context-window-length-limits-and-overflow)
23
29
  * @param event
24
30
  */
@@ -53,13 +59,21 @@ declare class BrowserAIChatLanguageModel implements LanguageModelV3 {
53
59
  */
54
60
  doGenerate(options: LanguageModelV3CallOptions): Promise<LanguageModelV3GenerateResult>;
55
61
  /**
56
- * Gets the input usage for the current session, if available
57
- * @returns The input usage or undefined if not available
62
+ * Gets the current context usage (tokens consumed) for the current session, if available
63
+ * @returns The context usage or undefined if not available
64
+ */
65
+ getContextUsage(): number | undefined;
66
+ /**
67
+ * Gets the context window size (token limit) for the current session, if available
68
+ * @returns The context window size or undefined if not available
69
+ */
70
+ getContextWindow(): number | undefined;
71
+ /**
72
+ * @deprecated Use {@link getContextUsage} instead.
58
73
  */
59
74
  getInputUsage(): number | undefined;
60
75
  /**
61
- * Gets the input quota for the current session, if available
62
- * @returns The input quota or undefined if not available
76
+ * @deprecated Use {@link getContextWindow} instead.
63
77
  */
64
78
  getInputQuota(): number | undefined;
65
79
  /**
package/dist/index.js CHANGED
@@ -1084,15 +1084,17 @@ var SessionManager = class {
1084
1084
  }
1085
1085
  const sessionOptions = this.prepareSessionOptions(options);
1086
1086
  this.session = await LanguageModel.create(sessionOptions);
1087
- const onQuotaOverflow = options?.onQuotaOverflow || this.baseOptions.onQuotaOverflow;
1088
- if (onQuotaOverflow) {
1089
- this.session.addEventListener("quotaoverflow", onQuotaOverflow);
1087
+ const onOverflow = options?.onContextOverflow || this.baseOptions.onContextOverflow || options?.onQuotaOverflow || this.baseOptions.onQuotaOverflow;
1088
+ const overflowHandler = onOverflow ?? (() => {
1089
+ console.warn(
1090
+ "Model context window exceeded. Consider handling the 'contextoverflow' event."
1091
+ );
1092
+ });
1093
+ const session = this.session;
1094
+ if ("oncontextoverflow" in session) {
1095
+ session.addEventListener("contextoverflow", overflowHandler);
1090
1096
  } else {
1091
- this.session.addEventListener("quotaoverflow", () => {
1092
- console.warn(
1093
- "Model quota exceeded. Consider handling 'quotaoverflow' event."
1094
- );
1095
- });
1097
+ session.addEventListener("quotaoverflow", overflowHandler);
1096
1098
  }
1097
1099
  return this.session;
1098
1100
  }
@@ -1161,18 +1163,34 @@ var SessionManager = class {
1161
1163
  this.session = null;
1162
1164
  }
1163
1165
  /**
1164
- * Gets the input quota for the current session, if available
1165
- * @returns The input quota or undefined if not available
1166
+ * Gets the context window size (token limit) for the current session, if available.
1167
+ * @returns The context window size or undefined if not available
1168
+ */
1169
+ getContextWindow() {
1170
+ const session = this.getCurrentSession();
1171
+ if (!session) return void 0;
1172
+ return session.contextWindow ?? session.inputQuota;
1173
+ }
1174
+ /**
1175
+ * Gets the current context usage (tokens consumed) for the current session, if available.
1176
+ * @returns The context usage or undefined if not available
1177
+ */
1178
+ getContextUsage() {
1179
+ const session = this.getCurrentSession();
1180
+ if (!session) return void 0;
1181
+ return session.contextUsage ?? session.inputUsage;
1182
+ }
1183
+ /**
1184
+ * @deprecated Use {@link getContextWindow} instead.
1166
1185
  */
1167
1186
  getInputQuota() {
1168
- return this.getCurrentSession()?.inputQuota;
1187
+ return this.getContextWindow();
1169
1188
  }
1170
1189
  /**
1171
- * Gets the input usage for the current session, if available
1172
- * @returns The input usage or undefined if not available
1190
+ * @deprecated Use {@link getContextUsage} instead.
1173
1191
  */
1174
1192
  getInputUsage() {
1175
- return this.getCurrentSession()?.inputUsage;
1193
+ return this.getContextUsage();
1176
1194
  }
1177
1195
  /**
1178
1196
  * Prepares merged session options from base config and request options
@@ -1188,7 +1206,8 @@ var SessionManager = class {
1188
1206
  systemMessage,
1189
1207
  expectedInputs,
1190
1208
  onDownloadProgress,
1191
- onQuotaOverflow,
1209
+ onContextOverflow: _onContextOverflow,
1210
+ onQuotaOverflow: _onQuotaOverflow,
1192
1211
  ...createOptions
1193
1212
  } = options;
1194
1213
  Object.assign(mergedOptions, createOptions);
@@ -1402,18 +1421,30 @@ var BrowserAIChatLanguageModel = class {
1402
1421
  };
1403
1422
  }
1404
1423
  /**
1405
- * Gets the input usage for the current session, if available
1406
- * @returns The input usage or undefined if not available
1424
+ * Gets the current context usage (tokens consumed) for the current session, if available
1425
+ * @returns The context usage or undefined if not available
1426
+ */
1427
+ getContextUsage() {
1428
+ return this.sessionManager.getContextUsage();
1429
+ }
1430
+ /**
1431
+ * Gets the context window size (token limit) for the current session, if available
1432
+ * @returns The context window size or undefined if not available
1433
+ */
1434
+ getContextWindow() {
1435
+ return this.sessionManager.getContextWindow();
1436
+ }
1437
+ /**
1438
+ * @deprecated Use {@link getContextUsage} instead.
1407
1439
  */
1408
1440
  getInputUsage() {
1409
- return this.sessionManager.getInputUsage();
1441
+ return this.getContextUsage();
1410
1442
  }
1411
1443
  /**
1412
- * Gets the input quota for the current session, if available
1413
- * @returns The input quota or undefined if not available
1444
+ * @deprecated Use {@link getContextWindow} instead.
1414
1445
  */
1415
1446
  getInputQuota() {
1416
- return this.sessionManager.getInputQuota();
1447
+ return this.getContextWindow();
1417
1448
  }
1418
1449
  /**
1419
1450
  * Check the availability of the browser AI model
@@ -1522,7 +1553,7 @@ var BrowserAIChatLanguageModel = class {
1522
1553
  finishReason,
1523
1554
  usage: {
1524
1555
  inputTokens: {
1525
- total: session.inputUsage,
1556
+ total: session.contextUsage ?? session.inputUsage,
1526
1557
  noCache: void 0,
1527
1558
  cacheRead: void 0,
1528
1559
  cacheWrite: void 0