@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.d.mts +19 -5
- package/dist/index.d.ts +19 -5
- package/dist/index.js +62 -52
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +62 -52
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
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
|
|
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
|
|
57
|
-
* @returns The
|
|
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
|
-
*
|
|
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
|
|
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
|
|
57
|
-
* @returns The
|
|
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
|
-
*
|
|
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
|
@@ -749,11 +749,7 @@ var import_provider = require("@ai-sdk/provider");
|
|
|
749
749
|
function convertBase64ToUint8Array(base64) {
|
|
750
750
|
try {
|
|
751
751
|
const binaryString = atob(base64);
|
|
752
|
-
|
|
753
|
-
for (let i = 0; i < binaryString.length; i++) {
|
|
754
|
-
bytes[i] = binaryString.charCodeAt(i);
|
|
755
|
-
}
|
|
756
|
-
return bytes;
|
|
752
|
+
return Uint8Array.from(binaryString, (c) => c.charCodeAt(0));
|
|
757
753
|
} catch (error) {
|
|
758
754
|
throw new Error(`Failed to convert base64 to Uint8Array: ${error}`);
|
|
759
755
|
}
|
|
@@ -1019,19 +1015,7 @@ function gatherUnsupportedSettingWarnings(options) {
|
|
|
1019
1015
|
}
|
|
1020
1016
|
|
|
1021
1017
|
// src/utils/prompt-utils.ts
|
|
1022
|
-
function
|
|
1023
|
-
for (const message of prompt) {
|
|
1024
|
-
if (message.role === "user") {
|
|
1025
|
-
for (const part of message.content) {
|
|
1026
|
-
if (part.type === "file") {
|
|
1027
|
-
return true;
|
|
1028
|
-
}
|
|
1029
|
-
}
|
|
1030
|
-
}
|
|
1031
|
-
}
|
|
1032
|
-
return false;
|
|
1033
|
-
}
|
|
1034
|
-
function getExpectedInputs(prompt) {
|
|
1018
|
+
function getMultimodalInfo(prompt) {
|
|
1035
1019
|
const inputs = /* @__PURE__ */ new Set();
|
|
1036
1020
|
for (const message of prompt) {
|
|
1037
1021
|
if (message.role === "user") {
|
|
@@ -1046,7 +1030,11 @@ function getExpectedInputs(prompt) {
|
|
|
1046
1030
|
}
|
|
1047
1031
|
}
|
|
1048
1032
|
}
|
|
1049
|
-
|
|
1033
|
+
const hasMultiModalInput = inputs.size > 0;
|
|
1034
|
+
return {
|
|
1035
|
+
hasMultiModalInput,
|
|
1036
|
+
expectedInputs: hasMultiModalInput ? Array.from(inputs, (type) => ({ type })) : void 0
|
|
1037
|
+
};
|
|
1050
1038
|
}
|
|
1051
1039
|
|
|
1052
1040
|
// src/chat/session-manager.ts
|
|
@@ -1096,15 +1084,17 @@ var SessionManager = class {
|
|
|
1096
1084
|
}
|
|
1097
1085
|
const sessionOptions = this.prepareSessionOptions(options);
|
|
1098
1086
|
this.session = await LanguageModel.create(sessionOptions);
|
|
1099
|
-
const
|
|
1100
|
-
|
|
1101
|
-
|
|
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);
|
|
1102
1096
|
} else {
|
|
1103
|
-
|
|
1104
|
-
console.warn(
|
|
1105
|
-
"Model quota exceeded. Consider handling 'quotaoverflow' event."
|
|
1106
|
-
);
|
|
1107
|
-
});
|
|
1097
|
+
session.addEventListener("quotaoverflow", overflowHandler);
|
|
1108
1098
|
}
|
|
1109
1099
|
return this.session;
|
|
1110
1100
|
}
|
|
@@ -1173,18 +1163,34 @@ var SessionManager = class {
|
|
|
1173
1163
|
this.session = null;
|
|
1174
1164
|
}
|
|
1175
1165
|
/**
|
|
1176
|
-
* Gets the
|
|
1177
|
-
* @returns The
|
|
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.
|
|
1178
1185
|
*/
|
|
1179
1186
|
getInputQuota() {
|
|
1180
|
-
return this.
|
|
1187
|
+
return this.getContextWindow();
|
|
1181
1188
|
}
|
|
1182
1189
|
/**
|
|
1183
|
-
*
|
|
1184
|
-
* @returns The input usage or undefined if not available
|
|
1190
|
+
* @deprecated Use {@link getContextUsage} instead.
|
|
1185
1191
|
*/
|
|
1186
1192
|
getInputUsage() {
|
|
1187
|
-
return this.
|
|
1193
|
+
return this.getContextUsage();
|
|
1188
1194
|
}
|
|
1189
1195
|
/**
|
|
1190
1196
|
* Prepares merged session options from base config and request options
|
|
@@ -1200,7 +1206,8 @@ var SessionManager = class {
|
|
|
1200
1206
|
systemMessage,
|
|
1201
1207
|
expectedInputs,
|
|
1202
1208
|
onDownloadProgress,
|
|
1203
|
-
|
|
1209
|
+
onContextOverflow: _onContextOverflow,
|
|
1210
|
+
onQuotaOverflow: _onQuotaOverflow,
|
|
1204
1211
|
...createOptions
|
|
1205
1212
|
} = options;
|
|
1206
1213
|
Object.assign(mergedOptions, createOptions);
|
|
@@ -1220,17 +1227,8 @@ var SessionManager = class {
|
|
|
1220
1227
|
};
|
|
1221
1228
|
}
|
|
1222
1229
|
}
|
|
1223
|
-
this.sanitizeOptions(mergedOptions);
|
|
1224
1230
|
return mergedOptions;
|
|
1225
1231
|
}
|
|
1226
|
-
/**
|
|
1227
|
-
* Removes custom options that aren't part of LanguageModel.create API
|
|
1228
|
-
*
|
|
1229
|
-
* @param options - Options object to sanitize in-place
|
|
1230
|
-
* @private
|
|
1231
|
-
*/
|
|
1232
|
-
sanitizeOptions(options) {
|
|
1233
|
-
}
|
|
1234
1232
|
};
|
|
1235
1233
|
|
|
1236
1234
|
// src/chat/browser-ai-language-model.ts
|
|
@@ -1306,7 +1304,7 @@ var BrowserAIChatLanguageModel = class {
|
|
|
1306
1304
|
)
|
|
1307
1305
|
);
|
|
1308
1306
|
}
|
|
1309
|
-
const hasMultiModalInput =
|
|
1307
|
+
const { hasMultiModalInput, expectedInputs } = getMultimodalInfo(prompt);
|
|
1310
1308
|
const { systemMessage, messages } = convertToBrowserAIMessages(prompt);
|
|
1311
1309
|
const promptOptions = {};
|
|
1312
1310
|
if (responseFormat?.type === "json") {
|
|
@@ -1324,7 +1322,7 @@ var BrowserAIChatLanguageModel = class {
|
|
|
1324
1322
|
warnings,
|
|
1325
1323
|
promptOptions,
|
|
1326
1324
|
hasMultiModalInput,
|
|
1327
|
-
expectedInputs
|
|
1325
|
+
expectedInputs,
|
|
1328
1326
|
functionTools
|
|
1329
1327
|
};
|
|
1330
1328
|
}
|
|
@@ -1423,18 +1421,30 @@ var BrowserAIChatLanguageModel = class {
|
|
|
1423
1421
|
};
|
|
1424
1422
|
}
|
|
1425
1423
|
/**
|
|
1426
|
-
* Gets the
|
|
1427
|
-
* @returns The
|
|
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.
|
|
1428
1439
|
*/
|
|
1429
1440
|
getInputUsage() {
|
|
1430
|
-
return this.
|
|
1441
|
+
return this.getContextUsage();
|
|
1431
1442
|
}
|
|
1432
1443
|
/**
|
|
1433
|
-
*
|
|
1434
|
-
* @returns The input quota or undefined if not available
|
|
1444
|
+
* @deprecated Use {@link getContextWindow} instead.
|
|
1435
1445
|
*/
|
|
1436
1446
|
getInputQuota() {
|
|
1437
|
-
return this.
|
|
1447
|
+
return this.getContextWindow();
|
|
1438
1448
|
}
|
|
1439
1449
|
/**
|
|
1440
1450
|
* Check the availability of the browser AI model
|
|
@@ -1543,7 +1553,7 @@ var BrowserAIChatLanguageModel = class {
|
|
|
1543
1553
|
finishReason,
|
|
1544
1554
|
usage: {
|
|
1545
1555
|
inputTokens: {
|
|
1546
|
-
total: session.inputUsage,
|
|
1556
|
+
total: session.contextUsage ?? session.inputUsage,
|
|
1547
1557
|
noCache: void 0,
|
|
1548
1558
|
cacheRead: void 0,
|
|
1549
1559
|
cacheWrite: void 0
|