@browser-ai/core 2.1.4 → 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
@@ -721,11 +721,7 @@ import {
721
721
  function convertBase64ToUint8Array(base64) {
722
722
  try {
723
723
  const binaryString = atob(base64);
724
- const bytes = new Uint8Array(binaryString.length);
725
- for (let i = 0; i < binaryString.length; i++) {
726
- bytes[i] = binaryString.charCodeAt(i);
727
- }
728
- return bytes;
724
+ return Uint8Array.from(binaryString, (c) => c.charCodeAt(0));
729
725
  } catch (error) {
730
726
  throw new Error(`Failed to convert base64 to Uint8Array: ${error}`);
731
727
  }
@@ -991,19 +987,7 @@ function gatherUnsupportedSettingWarnings(options) {
991
987
  }
992
988
 
993
989
  // src/utils/prompt-utils.ts
994
- function hasMultimodalContent(prompt) {
995
- for (const message of prompt) {
996
- if (message.role === "user") {
997
- for (const part of message.content) {
998
- if (part.type === "file") {
999
- return true;
1000
- }
1001
- }
1002
- }
1003
- }
1004
- return false;
1005
- }
1006
- function getExpectedInputs(prompt) {
990
+ function getMultimodalInfo(prompt) {
1007
991
  const inputs = /* @__PURE__ */ new Set();
1008
992
  for (const message of prompt) {
1009
993
  if (message.role === "user") {
@@ -1018,7 +1002,11 @@ function getExpectedInputs(prompt) {
1018
1002
  }
1019
1003
  }
1020
1004
  }
1021
- return Array.from(inputs).map((type) => ({ type }));
1005
+ const hasMultiModalInput = inputs.size > 0;
1006
+ return {
1007
+ hasMultiModalInput,
1008
+ expectedInputs: hasMultiModalInput ? Array.from(inputs, (type) => ({ type })) : void 0
1009
+ };
1022
1010
  }
1023
1011
 
1024
1012
  // src/chat/session-manager.ts
@@ -1068,15 +1056,17 @@ var SessionManager = class {
1068
1056
  }
1069
1057
  const sessionOptions = this.prepareSessionOptions(options);
1070
1058
  this.session = await LanguageModel.create(sessionOptions);
1071
- const onQuotaOverflow = options?.onQuotaOverflow || this.baseOptions.onQuotaOverflow;
1072
- if (onQuotaOverflow) {
1073
- 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);
1074
1068
  } else {
1075
- this.session.addEventListener("quotaoverflow", () => {
1076
- console.warn(
1077
- "Model quota exceeded. Consider handling 'quotaoverflow' event."
1078
- );
1079
- });
1069
+ session.addEventListener("quotaoverflow", overflowHandler);
1080
1070
  }
1081
1071
  return this.session;
1082
1072
  }
@@ -1145,18 +1135,34 @@ var SessionManager = class {
1145
1135
  this.session = null;
1146
1136
  }
1147
1137
  /**
1148
- * Gets the input quota for the current session, if available
1149
- * @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.
1150
1157
  */
1151
1158
  getInputQuota() {
1152
- return this.getCurrentSession()?.inputQuota;
1159
+ return this.getContextWindow();
1153
1160
  }
1154
1161
  /**
1155
- * Gets the input usage for the current session, if available
1156
- * @returns The input usage or undefined if not available
1162
+ * @deprecated Use {@link getContextUsage} instead.
1157
1163
  */
1158
1164
  getInputUsage() {
1159
- return this.getCurrentSession()?.inputUsage;
1165
+ return this.getContextUsage();
1160
1166
  }
1161
1167
  /**
1162
1168
  * Prepares merged session options from base config and request options
@@ -1172,7 +1178,8 @@ var SessionManager = class {
1172
1178
  systemMessage,
1173
1179
  expectedInputs,
1174
1180
  onDownloadProgress,
1175
- onQuotaOverflow,
1181
+ onContextOverflow: _onContextOverflow,
1182
+ onQuotaOverflow: _onQuotaOverflow,
1176
1183
  ...createOptions
1177
1184
  } = options;
1178
1185
  Object.assign(mergedOptions, createOptions);
@@ -1192,17 +1199,8 @@ var SessionManager = class {
1192
1199
  };
1193
1200
  }
1194
1201
  }
1195
- this.sanitizeOptions(mergedOptions);
1196
1202
  return mergedOptions;
1197
1203
  }
1198
- /**
1199
- * Removes custom options that aren't part of LanguageModel.create API
1200
- *
1201
- * @param options - Options object to sanitize in-place
1202
- * @private
1203
- */
1204
- sanitizeOptions(options) {
1205
- }
1206
1204
  };
1207
1205
 
1208
1206
  // src/chat/browser-ai-language-model.ts
@@ -1278,7 +1276,7 @@ var BrowserAIChatLanguageModel = class {
1278
1276
  )
1279
1277
  );
1280
1278
  }
1281
- const hasMultiModalInput = hasMultimodalContent(prompt);
1279
+ const { hasMultiModalInput, expectedInputs } = getMultimodalInfo(prompt);
1282
1280
  const { systemMessage, messages } = convertToBrowserAIMessages(prompt);
1283
1281
  const promptOptions = {};
1284
1282
  if (responseFormat?.type === "json") {
@@ -1296,7 +1294,7 @@ var BrowserAIChatLanguageModel = class {
1296
1294
  warnings,
1297
1295
  promptOptions,
1298
1296
  hasMultiModalInput,
1299
- expectedInputs: hasMultiModalInput ? getExpectedInputs(prompt) : void 0,
1297
+ expectedInputs,
1300
1298
  functionTools
1301
1299
  };
1302
1300
  }
@@ -1395,18 +1393,30 @@ var BrowserAIChatLanguageModel = class {
1395
1393
  };
1396
1394
  }
1397
1395
  /**
1398
- * Gets the input usage for the current session, if available
1399
- * @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.
1400
1411
  */
1401
1412
  getInputUsage() {
1402
- return this.sessionManager.getInputUsage();
1413
+ return this.getContextUsage();
1403
1414
  }
1404
1415
  /**
1405
- * Gets the input quota for the current session, if available
1406
- * @returns The input quota or undefined if not available
1416
+ * @deprecated Use {@link getContextWindow} instead.
1407
1417
  */
1408
1418
  getInputQuota() {
1409
- return this.sessionManager.getInputQuota();
1419
+ return this.getContextWindow();
1410
1420
  }
1411
1421
  /**
1412
1422
  * Check the availability of the browser AI model
@@ -1515,7 +1525,7 @@ var BrowserAIChatLanguageModel = class {
1515
1525
  finishReason,
1516
1526
  usage: {
1517
1527
  inputTokens: {
1518
- total: session.inputUsage,
1528
+ total: session.contextUsage ?? session.inputUsage,
1519
1529
  noCache: void 0,
1520
1530
  cacheRead: void 0,
1521
1531
  cacheWrite: void 0