prompt-api-polyfill 1.8.0 → 1.11.0
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/backends/firebase.js +2 -3
- package/dist/backends/gemini.js +1 -1
- package/dist/backends/openai.js +16 -17
- package/dist/backends/transformers.js +2 -2
- package/dist/chunks/{defaults-_qJIFiOb.js → defaults-DD5ZWEDv.js} +1 -2
- package/dist/prompt-api-polyfill.js +111 -126
- package/package.json +7 -2
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { initializeApp as Fe } from "firebase/app";
|
|
2
2
|
import { VertexAIBackend as ze, GoogleAIBackend as Ue, getAI as je, getGenerativeModel as Ve, InferenceMode as We } from "firebase/ai";
|
|
3
|
-
import { P as Ke, D as Ge } from "../chunks/defaults-
|
|
3
|
+
import { P as Ke, D as Ge } from "../chunks/defaults-DD5ZWEDv.js";
|
|
4
4
|
const qe = () => {
|
|
5
5
|
};
|
|
6
6
|
const be = function(t) {
|
|
@@ -1761,7 +1761,6 @@ function an() {
|
|
|
1761
1761
|
an();
|
|
1762
1762
|
class dn extends Ke {
|
|
1763
1763
|
#e;
|
|
1764
|
-
#t;
|
|
1765
1764
|
constructor(e) {
|
|
1766
1765
|
const {
|
|
1767
1766
|
geminiApiProvider: r,
|
|
@@ -1784,7 +1783,7 @@ class dn extends Ke {
|
|
|
1784
1783
|
});
|
|
1785
1784
|
}
|
|
1786
1785
|
createSession(e, r) {
|
|
1787
|
-
return this.#
|
|
1786
|
+
return this.#e = Ve(this.ai, {
|
|
1788
1787
|
mode: We.ONLY_IN_CLOUD,
|
|
1789
1788
|
inCloudParams: r
|
|
1790
1789
|
}), this.#e;
|
package/dist/backends/gemini.js
CHANGED
package/dist/backends/openai.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import u from "openai";
|
|
2
|
-
import { P as m, D as l } from "../chunks/defaults-
|
|
2
|
+
import { P as m, D as l } from "../chunks/defaults-DD5ZWEDv.js";
|
|
3
3
|
class y extends m {
|
|
4
4
|
#e;
|
|
5
5
|
constructor(t) {
|
|
@@ -117,8 +117,8 @@ class y extends m {
|
|
|
117
117
|
c && typeof c == "object" && "value" in c && (i = JSON.stringify(c.value));
|
|
118
118
|
} catch {
|
|
119
119
|
}
|
|
120
|
-
const
|
|
121
|
-
return { text: i, usage:
|
|
120
|
+
const p = a.usage?.prompt_tokens || 0;
|
|
121
|
+
return { text: i, usage: p };
|
|
122
122
|
} catch (a) {
|
|
123
123
|
throw console.error("OpenAI Generate Content Error:", a), a;
|
|
124
124
|
}
|
|
@@ -142,11 +142,10 @@ class y extends m {
|
|
|
142
142
|
try {
|
|
143
143
|
const a = await this.openai.chat.completions.create(o);
|
|
144
144
|
return (async function* () {
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
text: () => n,
|
|
145
|
+
for await (const n of a) {
|
|
146
|
+
let i = n.choices[0]?.delta?.content;
|
|
147
|
+
i && (yield {
|
|
148
|
+
text: () => i,
|
|
150
149
|
usageMetadata: { totalTokenCount: 0 }
|
|
151
150
|
});
|
|
152
151
|
}
|
|
@@ -173,19 +172,19 @@ class y extends m {
|
|
|
173
172
|
});
|
|
174
173
|
for (const e of t) {
|
|
175
174
|
const o = e.role === "model" ? "assistant" : "user", a = [];
|
|
176
|
-
for (const
|
|
177
|
-
if (
|
|
178
|
-
a.push({ type: "text", text:
|
|
179
|
-
else if (
|
|
180
|
-
const { data: i, mimeType:
|
|
181
|
-
|
|
175
|
+
for (const n of e.parts)
|
|
176
|
+
if (n.text)
|
|
177
|
+
a.push({ type: "text", text: n.text });
|
|
178
|
+
else if (n.inlineData) {
|
|
179
|
+
const { data: i, mimeType: p } = n.inlineData;
|
|
180
|
+
p.startsWith("image/") ? a.push({
|
|
182
181
|
type: "image_url",
|
|
183
|
-
image_url: { url: `data:${
|
|
184
|
-
}) :
|
|
182
|
+
image_url: { url: `data:${p};base64,${i}` }
|
|
183
|
+
}) : p.startsWith("audio/") && a.push({
|
|
185
184
|
type: "input_audio",
|
|
186
185
|
input_audio: {
|
|
187
186
|
data: i,
|
|
188
|
-
format:
|
|
187
|
+
format: p.split("/")[1] === "mpeg" ? "mp3" : "wav"
|
|
189
188
|
}
|
|
190
189
|
});
|
|
191
190
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { env as j, pipeline as z, TextStreamer as C } from "@huggingface/transformers";
|
|
2
|
-
import { P as $, D as y } from "../chunks/defaults-
|
|
2
|
+
import { P as $, D as y } from "../chunks/defaults-DD5ZWEDv.js";
|
|
3
3
|
class I extends $ {
|
|
4
4
|
#e;
|
|
5
5
|
#t;
|
|
@@ -213,7 +213,7 @@ async function M(g, o = {}) {
|
|
|
213
213
|
const c = await r.json(), f = new Map(c.map((s) => [s.path, s.size])), d = [], h = (s) => f.has(s), i = (s) => h(s) ? (d.push({ path: s, size: f.get(s) }), !0) : !1;
|
|
214
214
|
i("config.json"), i("generation_config.json"), i("preprocessor_config.json"), h("tokenizer.json") ? (i("tokenizer.json"), i("tokenizer_config.json")) : (i("tokenizer_config.json"), i("special_tokens_map.json"), i("vocab.json"), i("merges.txt"), i("vocab.txt"));
|
|
215
215
|
const w = "onnx";
|
|
216
|
-
let m
|
|
216
|
+
let m;
|
|
217
217
|
t === "fp32" ? m = [""] : t === "quantized" ? m = ["_quantized"] : (m = [`_${t}`], t === "q8" && m.push(""));
|
|
218
218
|
let k = [
|
|
219
219
|
"model",
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
class a {
|
|
2
|
-
#e;
|
|
3
2
|
/**
|
|
4
3
|
* @param {string} modelName - The name of the model.
|
|
5
4
|
*/
|
|
@@ -21,7 +20,7 @@ class a {
|
|
|
21
20
|
* @param {EventTarget} [monitorTarget] - The event target to dispatch download progress events to.
|
|
22
21
|
* @returns {any} The created session object.
|
|
23
22
|
*/
|
|
24
|
-
createSession(e,
|
|
23
|
+
createSession(e, n, o) {
|
|
25
24
|
throw new Error("Not implemented");
|
|
26
25
|
}
|
|
27
26
|
/**
|
|
@@ -25,51 +25,45 @@ class M {
|
|
|
25
25
|
}
|
|
26
26
|
static async processImage(t) {
|
|
27
27
|
if (t instanceof Blob || t && typeof t == "object" && t.constructor && t.constructor.name === "Blob")
|
|
28
|
-
return this.blobToInlineData(t);
|
|
28
|
+
return t.type === "image/png" || t.type === "image/jpeg" ? this.blobToInlineData(t) : this.#o(t);
|
|
29
29
|
const r = t instanceof ArrayBuffer || t && t.constructor && t.constructor.name === "ArrayBuffer", n = ArrayBuffer.isView(t) || t && t.buffer && (t.buffer instanceof ArrayBuffer || t.buffer.constructor.name === "ArrayBuffer");
|
|
30
30
|
if (r || n) {
|
|
31
31
|
const o = r ? new Uint8Array(t) : new Uint8Array(t.buffer, t.byteOffset, t.byteLength), i = o.buffer.slice(
|
|
32
32
|
o.byteOffset,
|
|
33
33
|
o.byteOffset + o.byteLength
|
|
34
|
-
), a = this
|
|
35
|
-
if (!
|
|
34
|
+
), a = this.#e(o);
|
|
35
|
+
if (!a)
|
|
36
36
|
throw new DOMException("Invalid image data", "InvalidStateError");
|
|
37
|
-
|
|
37
|
+
if (a === "image/png" || a === "image/jpeg")
|
|
38
|
+
return { inlineData: { data: await this.arrayBufferToBase64(i), mimeType: a } };
|
|
39
|
+
const l = new Blob([i], { type: a });
|
|
40
|
+
return this.#o(l);
|
|
38
41
|
}
|
|
39
42
|
return this.canvasSourceToInlineData(t);
|
|
40
43
|
}
|
|
41
|
-
static #o(t) {
|
|
44
|
+
static async #o(t) {
|
|
45
|
+
const e = URL.createObjectURL(t);
|
|
46
|
+
try {
|
|
47
|
+
const r = new Image();
|
|
48
|
+
return r.src = e, await r.decode().catch((n) => {
|
|
49
|
+
throw new DOMException(
|
|
50
|
+
`The source image cannot be decoded: ${n.message}`,
|
|
51
|
+
"InvalidStateError"
|
|
52
|
+
);
|
|
53
|
+
}), await this.canvasSourceToInlineData(r);
|
|
54
|
+
} finally {
|
|
55
|
+
URL.revokeObjectURL(e);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
static #e(t) {
|
|
42
59
|
if (t.length < 4)
|
|
43
60
|
return null;
|
|
44
61
|
if (t[0] === 255 && t[1] === 216 && t[2] === 255)
|
|
45
62
|
return "image/jpeg";
|
|
46
63
|
if (t[0] === 137 && t[1] === 80 && t[2] === 78 && t[3] === 71 && t[4] === 13 && t[5] === 10 && t[6] === 26 && t[7] === 10)
|
|
47
64
|
return "image/png";
|
|
48
|
-
if (t[0] === 71 && t[1] === 73 && t[2] === 70 && t[3] === 56)
|
|
49
|
-
return "image/gif";
|
|
50
|
-
if (t[0] === 82 && t[1] === 73 && t[2] === 70 && t[3] === 70 && t[8] === 87 && t[9] === 69 && t[10] === 66 && t[11] === 80)
|
|
51
|
-
return "image/webp";
|
|
52
|
-
if (t[0] === 66 && t[1] === 77)
|
|
53
|
-
return "image/bmp";
|
|
54
|
-
if (t[0] === 0 && t[1] === 0 && t[2] === 1 && t[3] === 0)
|
|
55
|
-
return "image/x-icon";
|
|
56
|
-
if (t[0] === 73 && t[1] === 73 && t[2] === 42 || t[0] === 77 && t[1] === 77 && t[2] === 42)
|
|
57
|
-
return "image/tiff";
|
|
58
|
-
if (t[4] === 102 && t[5] === 116 && t[6] === 121 && t[7] === 112) {
|
|
59
|
-
const n = String.fromCharCode(t[8], t[9], t[10], t[11]);
|
|
60
|
-
if (n === "avif" || n === "avis")
|
|
61
|
-
return "image/avif";
|
|
62
|
-
if (n === "heic" || n === "heix" || n === "hevc" || n === "hevx")
|
|
63
|
-
return "image/heic";
|
|
64
|
-
if (n === "mif1" || n === "msf1")
|
|
65
|
-
return "image/heif";
|
|
66
|
-
}
|
|
67
|
-
if (t[0] === 255 && t[1] === 10 || t[0] === 0 && t[4] === 74 && t[5] === 88 && t[6] === 76)
|
|
68
|
-
return "image/jxl";
|
|
69
|
-
if (t[0] === 0 && t[4] === 106 && t[5] === 80 && t[6] === 32)
|
|
70
|
-
return "image/jp2";
|
|
71
65
|
const r = String.fromCharCode(...t.slice(0, 100)).toLowerCase();
|
|
72
|
-
return r.includes("<svg") || r.includes("<?xml") ? "image/svg+xml" : null;
|
|
66
|
+
return r.includes("<svg") || r.includes("<?xml") ? "image/svg+xml" : t[0] === 71 && t[1] === 73 && t[2] === 70 ? "image/gif" : t[0] === 82 && t[1] === 73 && t[2] === 70 && t[3] === 70 ? "image/webp" : t[4] === 102 && t[5] === 116 && t[6] === 121 && t[7] === 112 ? "image/avif" : null;
|
|
73
67
|
}
|
|
74
68
|
static async processAudio(t) {
|
|
75
69
|
if (t instanceof Blob || t && typeof t == "object" && t.constructor && t.constructor.name === "Blob") {
|
|
@@ -79,14 +73,14 @@ class M {
|
|
|
79
73
|
}
|
|
80
74
|
if (t instanceof AudioBuffer || t && t.constructor && t.constructor.name === "AudioBuffer") {
|
|
81
75
|
const i = this.audioBufferToWav(t);
|
|
82
|
-
return { inlineData: { data: this.arrayBufferToBase64(i), mimeType: "audio/wav" } };
|
|
76
|
+
return { inlineData: { data: await this.arrayBufferToBase64(i), mimeType: "audio/wav" } };
|
|
83
77
|
}
|
|
84
78
|
const n = t instanceof ArrayBuffer || t && t.constructor && t.constructor.name === "ArrayBuffer", o = ArrayBuffer.isView(t) || t && t.buffer && (t.buffer instanceof ArrayBuffer || t.buffer.constructor.name === "ArrayBuffer");
|
|
85
79
|
if (n || o) {
|
|
86
80
|
const i = n ? t : t.buffer;
|
|
87
81
|
return {
|
|
88
82
|
inlineData: {
|
|
89
|
-
data: this.arrayBufferToBase64(i),
|
|
83
|
+
data: await this.arrayBufferToBase64(i),
|
|
90
84
|
mimeType: "audio/wav"
|
|
91
85
|
// Fallback assumption
|
|
92
86
|
}
|
|
@@ -117,9 +111,9 @@ class M {
|
|
|
117
111
|
});
|
|
118
112
|
const e = (c) => {
|
|
119
113
|
const p = t[c];
|
|
120
|
-
return typeof p == "object" && p !== null && "baseVal" in p ? p.baseVal.value :
|
|
114
|
+
return typeof p == "number" ? p : typeof p == "object" && p !== null && "baseVal" in p ? p.baseVal.value : 0;
|
|
121
115
|
};
|
|
122
|
-
let r = t.displayWidth || t.naturalWidth || t.videoWidth ||
|
|
116
|
+
let r = t.displayWidth || t.naturalWidth || t.videoWidth || e("width"), n = t.displayHeight || t.naturalHeight || t.videoHeight || e("height");
|
|
123
117
|
if ((!r || !n) && typeof t.getBBox == "function")
|
|
124
118
|
try {
|
|
125
119
|
const c = t.getBBox();
|
|
@@ -149,12 +143,11 @@ class M {
|
|
|
149
143
|
}
|
|
150
144
|
};
|
|
151
145
|
}
|
|
152
|
-
static arrayBufferToBase64(t) {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
return window.btoa(e);
|
|
146
|
+
static async arrayBufferToBase64(t) {
|
|
147
|
+
const e = new Blob([t]), r = new FileReader();
|
|
148
|
+
return new Promise((n, o) => {
|
|
149
|
+
r.onload = () => n(r.result.split(",")[1]), r.onerror = o, r.readAsDataURL(e);
|
|
150
|
+
});
|
|
158
151
|
}
|
|
159
152
|
// Simple WAV Encoder for AudioBuffer
|
|
160
153
|
static audioBufferToWav(t) {
|
|
@@ -213,7 +206,7 @@ function O(s) {
|
|
|
213
206
|
// Recursively convert the 'items' schema
|
|
214
207
|
items: O(s.items)
|
|
215
208
|
});
|
|
216
|
-
case "object":
|
|
209
|
+
case "object": {
|
|
217
210
|
const e = {}, r = s.properties ? Object.keys(s.properties) : [];
|
|
218
211
|
r.forEach((i) => {
|
|
219
212
|
e[i] = O(
|
|
@@ -228,6 +221,7 @@ function O(s) {
|
|
|
228
221
|
properties: e,
|
|
229
222
|
optionalProperties: o
|
|
230
223
|
});
|
|
224
|
+
}
|
|
231
225
|
default:
|
|
232
226
|
return console.warn(
|
|
233
227
|
`Unsupported type: ${s.type}, defaulting to string.`
|
|
@@ -263,7 +257,7 @@ async function I(s) {
|
|
|
263
257
|
return (await import("./backends/transformers.js")).default;
|
|
264
258
|
throw new Error(`Unknown backend path "${s}"`);
|
|
265
259
|
}
|
|
266
|
-
async function
|
|
260
|
+
async function P(s, t = globalThis) {
|
|
267
261
|
const e = [];
|
|
268
262
|
for (const r of s) {
|
|
269
263
|
const n = r.role === "assistant" ? "model" : "user", o = n === "model";
|
|
@@ -293,9 +287,8 @@ async function S(s, t = globalThis) {
|
|
|
293
287
|
}
|
|
294
288
|
return e;
|
|
295
289
|
}
|
|
296
|
-
class
|
|
290
|
+
class h extends EventTarget {
|
|
297
291
|
#o;
|
|
298
|
-
#f;
|
|
299
292
|
#e;
|
|
300
293
|
#a;
|
|
301
294
|
#n;
|
|
@@ -304,7 +297,7 @@ class d extends EventTarget {
|
|
|
304
297
|
#c;
|
|
305
298
|
#t;
|
|
306
299
|
constructor(t, e, r, n = {}, o, i = 0, a = globalThis) {
|
|
307
|
-
super(), this.#o = t, this.#
|
|
300
|
+
super(), this.#o = t, this.#e = r || [], this.#a = n, this.#n = o, this.#r = !1, this.#i = i, this.#c = {}, this.#t = a;
|
|
308
301
|
}
|
|
309
302
|
get contextUsage() {
|
|
310
303
|
return this.#i;
|
|
@@ -318,28 +311,28 @@ class d extends EventTarget {
|
|
|
318
311
|
set oncontextoverflow(t) {
|
|
319
312
|
this.#c && this.removeEventListener("contextoverflow", this.#c), this.#c = t, typeof t == "function" && this.addEventListener("contextoverflow", t);
|
|
320
313
|
}
|
|
321
|
-
static #
|
|
314
|
+
static #f(t) {
|
|
322
315
|
try {
|
|
323
316
|
if (!t || !t.document || t.document.defaultView !== t)
|
|
324
317
|
throw new Error();
|
|
325
318
|
if (t !== globalThis && t !== t.top && (!t.frameElement || !t.frameElement.isConnected))
|
|
326
319
|
throw new Error();
|
|
327
320
|
} catch {
|
|
328
|
-
const
|
|
329
|
-
throw new
|
|
321
|
+
const e = t?.DOMException || globalThis.DOMException;
|
|
322
|
+
throw new e(
|
|
330
323
|
"The execution context is not valid.",
|
|
331
324
|
"InvalidStateError"
|
|
332
325
|
);
|
|
333
326
|
}
|
|
334
327
|
}
|
|
335
328
|
#s() {
|
|
336
|
-
|
|
329
|
+
h.#f(this.#t);
|
|
337
330
|
}
|
|
338
331
|
static async availability(t = {}) {
|
|
339
332
|
const e = this.__window || globalThis;
|
|
340
|
-
|
|
333
|
+
h.#f(e);
|
|
341
334
|
try {
|
|
342
|
-
await
|
|
335
|
+
await h.#x(t, e);
|
|
343
336
|
} catch (n) {
|
|
344
337
|
if (n instanceof RangeError) {
|
|
345
338
|
if (n.message.includes("language tag"))
|
|
@@ -355,37 +348,37 @@ class d extends EventTarget {
|
|
|
355
348
|
}
|
|
356
349
|
return "unavailable";
|
|
357
350
|
}
|
|
358
|
-
return (await
|
|
351
|
+
return (await h.#h(e)).availability(t);
|
|
359
352
|
}
|
|
360
|
-
static #
|
|
353
|
+
static #g = A;
|
|
361
354
|
static #d(t = globalThis) {
|
|
362
|
-
for (const r of
|
|
355
|
+
for (const r of h.#g) {
|
|
363
356
|
const n = t[r.config] || globalThis[r.config];
|
|
364
357
|
if (n && n.apiKey)
|
|
365
358
|
return { ...r, configValue: n };
|
|
366
359
|
}
|
|
367
|
-
const e =
|
|
360
|
+
const e = h.#g.map((r) => `window.${r.config}`).join(", ");
|
|
368
361
|
throw new (t.DOMException || globalThis.DOMException)(
|
|
369
362
|
`Prompt API Polyfill: No backend configuration found. Please set one of: ${e}.`,
|
|
370
363
|
"NotSupportedError"
|
|
371
364
|
);
|
|
372
365
|
}
|
|
373
|
-
static async #
|
|
374
|
-
const e =
|
|
366
|
+
static async #h(t = globalThis) {
|
|
367
|
+
const e = h.#d(t);
|
|
375
368
|
return I(e.path);
|
|
376
369
|
}
|
|
377
|
-
static async #
|
|
370
|
+
static async #x(t = {}, e = globalThis) {
|
|
378
371
|
if (t.expectedInputs)
|
|
379
372
|
for (const n of t.expectedInputs) {
|
|
380
373
|
if (n.type !== "text" && n.type !== "image" && n.type !== "audio")
|
|
381
374
|
throw new TypeError(`Invalid input type: ${n.type}`);
|
|
382
|
-
n.languages &&
|
|
375
|
+
n.languages && h.#w(n.languages);
|
|
383
376
|
}
|
|
384
377
|
if (t.expectedOutputs)
|
|
385
378
|
for (const n of t.expectedOutputs) {
|
|
386
379
|
if (n.type !== "text")
|
|
387
380
|
throw new RangeError(`Unsupported output type: ${n.type}`);
|
|
388
|
-
n.languages &&
|
|
381
|
+
n.languages && h.#w(n.languages);
|
|
389
382
|
}
|
|
390
383
|
const r = t.expectedInputs ? ["text", ...t.expectedInputs.map((n) => n.type)] : ["text"];
|
|
391
384
|
if (t.initialPrompts && Array.isArray(t.initialPrompts)) {
|
|
@@ -420,7 +413,7 @@ class d extends EventTarget {
|
|
|
420
413
|
}
|
|
421
414
|
}
|
|
422
415
|
}
|
|
423
|
-
static #
|
|
416
|
+
static #w(t) {
|
|
424
417
|
if (!Array.isArray(t))
|
|
425
418
|
throw new RangeError("The `languages` option must be an array.");
|
|
426
419
|
for (const e of t) {
|
|
@@ -441,7 +434,7 @@ class d extends EventTarget {
|
|
|
441
434
|
}
|
|
442
435
|
static async create(t = {}) {
|
|
443
436
|
const e = this.__window || globalThis;
|
|
444
|
-
if (
|
|
437
|
+
if (h.#f(e), await h.#x(t, e), t.signal?.aborted)
|
|
445
438
|
throw t.signal.reason || new (e.DOMException || globalThis.DOMException)(
|
|
446
439
|
"Aborted",
|
|
447
440
|
"AbortError"
|
|
@@ -462,8 +455,8 @@ class d extends EventTarget {
|
|
|
462
455
|
"Aborted",
|
|
463
456
|
"AbortError"
|
|
464
457
|
);
|
|
465
|
-
const n =
|
|
466
|
-
|
|
458
|
+
const n = h.#d(e), o = await h.#h(e), i = new o(n.configValue), a = { ...t };
|
|
459
|
+
h.#u(
|
|
467
460
|
a.responseConstraint,
|
|
468
461
|
e
|
|
469
462
|
);
|
|
@@ -478,16 +471,16 @@ class d extends EventTarget {
|
|
|
478
471
|
), u = a.initialPrompts.filter(
|
|
479
472
|
(f) => f.role !== "system"
|
|
480
473
|
);
|
|
481
|
-
y.length > 0 && (l.systemInstruction = y.map((f) => typeof f.content == "string" ? f.content : Array.isArray(f.content) ? f.content.filter((
|
|
474
|
+
y.length > 0 && (l.systemInstruction = y.map((f) => typeof f.content == "string" ? f.content : Array.isArray(f.content) ? f.content.filter((d) => d.type === "text").map((d) => d.value || d.text || "").join(`
|
|
482
475
|
`) : "").join(`
|
|
483
|
-
`)), c = await
|
|
476
|
+
`)), c = await P(u, e);
|
|
484
477
|
for (const f of a.initialPrompts) {
|
|
485
478
|
if (typeof f.content != "string")
|
|
486
479
|
continue;
|
|
487
|
-
const
|
|
480
|
+
const d = h.#y([
|
|
488
481
|
{ text: f.content }
|
|
489
482
|
]);
|
|
490
|
-
if (
|
|
483
|
+
if (d === "QuotaExceededError" || d === "contextoverflow") {
|
|
491
484
|
const E = e.QuotaExceededError || e.DOMException || globalThis.QuotaExceededError || globalThis.DOMException, g = new E(
|
|
492
485
|
"The initial prompts are too large, they exceed the quota.",
|
|
493
486
|
"QuotaExceededError"
|
|
@@ -496,22 +489,14 @@ class d extends EventTarget {
|
|
|
496
489
|
value: 22,
|
|
497
490
|
configurable: !0
|
|
498
491
|
});
|
|
499
|
-
const x =
|
|
492
|
+
const x = d === "QuotaExceededError" ? 1e7 : 5e5;
|
|
500
493
|
throw g.requested = x, g.quota = 1e6, g;
|
|
501
494
|
}
|
|
502
495
|
}
|
|
503
496
|
}
|
|
504
497
|
let w = null;
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
try {
|
|
508
|
-
a.monitor(w);
|
|
509
|
-
} catch (y) {
|
|
510
|
-
throw y;
|
|
511
|
-
}
|
|
512
|
-
}
|
|
513
|
-
w && (w.__lastProgressLoaded = -1);
|
|
514
|
-
const m = async (y) => {
|
|
498
|
+
typeof a.monitor == "function" && (w = new EventTarget(), a.monitor(w)), w && (w.__lastProgressLoaded = -1);
|
|
499
|
+
const b = async (y) => {
|
|
515
500
|
if (!w || t.signal?.aborted)
|
|
516
501
|
return !t.signal?.aborted;
|
|
517
502
|
const u = 1 / 65536, f = Math.floor(y / u) * u;
|
|
@@ -525,22 +510,22 @@ class d extends EventTarget {
|
|
|
525
510
|
lengthComputable: !0
|
|
526
511
|
})
|
|
527
512
|
), w.__lastProgressLoaded = f;
|
|
528
|
-
} catch (
|
|
529
|
-
console.error("Error dispatching downloadprogress events:",
|
|
513
|
+
} catch (d) {
|
|
514
|
+
console.error("Error dispatching downloadprogress events:", d);
|
|
530
515
|
}
|
|
531
|
-
return await new Promise((
|
|
516
|
+
return await new Promise((d) => setTimeout(d, 0)), !t.signal?.aborted;
|
|
532
517
|
};
|
|
533
|
-
if (!await
|
|
518
|
+
if (!await b(0))
|
|
534
519
|
throw t.signal.reason || new (e.DOMException || globalThis.DOMException)(
|
|
535
520
|
"Aborted",
|
|
536
521
|
"AbortError"
|
|
537
522
|
);
|
|
538
|
-
const
|
|
523
|
+
const m = await i.createSession(
|
|
539
524
|
a,
|
|
540
525
|
l,
|
|
541
526
|
w
|
|
542
527
|
);
|
|
543
|
-
if (!await
|
|
528
|
+
if (!await b(1))
|
|
544
529
|
throw t.signal.reason || new (e.DOMException || globalThis.DOMException)(
|
|
545
530
|
"Aborted",
|
|
546
531
|
"AbortError"
|
|
@@ -560,7 +545,7 @@ class d extends EventTarget {
|
|
|
560
545
|
}
|
|
561
546
|
return new this(
|
|
562
547
|
i,
|
|
563
|
-
|
|
548
|
+
m,
|
|
564
549
|
c,
|
|
565
550
|
a,
|
|
566
551
|
l,
|
|
@@ -580,7 +565,7 @@ class d extends EventTarget {
|
|
|
580
565
|
"Aborted",
|
|
581
566
|
"AbortError"
|
|
582
567
|
);
|
|
583
|
-
const e = JSON.parse(JSON.stringify(this.#e)), r = { ...this.#a, ...t }, n = await
|
|
568
|
+
const e = JSON.parse(JSON.stringify(this.#e)), r = { ...this.#a, ...t }, n = await h.#h(this.#t), o = h.#d(this.#t), i = new n(o.configValue), a = await i.createSession(
|
|
584
569
|
r,
|
|
585
570
|
this.#n
|
|
586
571
|
);
|
|
@@ -616,19 +601,19 @@ class d extends EventTarget {
|
|
|
616
601
|
if (typeof t == "object" && t !== null && !Array.isArray(t) && Object.keys(t).length === 0)
|
|
617
602
|
return "[object Object]";
|
|
618
603
|
if (e.responseConstraint) {
|
|
619
|
-
|
|
604
|
+
h.#u(
|
|
620
605
|
e.responseConstraint,
|
|
621
606
|
this.#t
|
|
622
607
|
);
|
|
623
608
|
const l = O(
|
|
624
609
|
e.responseConstraint
|
|
625
610
|
);
|
|
626
|
-
this.#n.generationConfig.responseMimeType = "application/json", this.#n.generationConfig.responseSchema = l, this.#
|
|
611
|
+
this.#n.generationConfig.responseMimeType = "application/json", this.#n.generationConfig.responseSchema = l, this.#o.createSession(
|
|
627
612
|
this.#a,
|
|
628
613
|
this.#n
|
|
629
614
|
);
|
|
630
615
|
}
|
|
631
|
-
const r = this.#
|
|
616
|
+
const r = this.#E(t), n = await this.#l(t);
|
|
632
617
|
if (this.#r)
|
|
633
618
|
throw new (this.#t.DOMException || globalThis.DOMException)(
|
|
634
619
|
"Session is destroyed",
|
|
@@ -657,15 +642,15 @@ class d extends EventTarget {
|
|
|
657
642
|
{ once: !0 }
|
|
658
643
|
);
|
|
659
644
|
}), a = (async () => {
|
|
660
|
-
const l = this.#
|
|
645
|
+
const l = this.#p(n);
|
|
661
646
|
if (l === "QuotaExceededError") {
|
|
662
|
-
const f = this.#t && this.#t.QuotaExceededError || this.#t && this.#t.DOMException || globalThis.QuotaExceededError || globalThis.DOMException,
|
|
647
|
+
const f = this.#t && this.#t.QuotaExceededError || this.#t && this.#t.DOMException || globalThis.QuotaExceededError || globalThis.DOMException, d = new f(
|
|
663
648
|
"The prompt is too large, it exceeds the quota.",
|
|
664
649
|
"QuotaExceededError"
|
|
665
650
|
);
|
|
666
|
-
Object.defineProperty(
|
|
651
|
+
Object.defineProperty(d, "code", { value: 22, configurable: !0 });
|
|
667
652
|
const E = 1e7;
|
|
668
|
-
throw
|
|
653
|
+
throw d.requested = E, d.quota = this.contextWindow, d;
|
|
669
654
|
} else if (l === "contextoverflow")
|
|
670
655
|
return this.dispatchEvent(new Event("contextoverflow")), "Mock response for quota overflow test.";
|
|
671
656
|
const c = [...this.#e, o];
|
|
@@ -677,22 +662,22 @@ class d extends EventTarget {
|
|
|
677
662
|
c
|
|
678
663
|
);
|
|
679
664
|
if (p > this.contextWindow) {
|
|
680
|
-
const f = this.#t && this.#t.QuotaExceededError || this.#t && this.#t.DOMException || globalThis.QuotaExceededError || globalThis.DOMException,
|
|
665
|
+
const f = this.#t && this.#t.QuotaExceededError || this.#t && this.#t.DOMException || globalThis.QuotaExceededError || globalThis.DOMException, d = new f(
|
|
681
666
|
`The prompt is too large (${p} tokens), it exceeds the quota of ${this.contextWindow} tokens.`,
|
|
682
667
|
"QuotaExceededError"
|
|
683
668
|
);
|
|
684
|
-
throw Object.defineProperty(
|
|
669
|
+
throw Object.defineProperty(d, "code", { value: 22, configurable: !0 }), d.requested = p, d.quota = this.contextWindow, d;
|
|
685
670
|
}
|
|
686
671
|
p > this.contextWindow && this.dispatchEvent(new Event("contextoverflow"));
|
|
687
672
|
const w = [...this.#e, o];
|
|
688
|
-
let
|
|
673
|
+
let b;
|
|
689
674
|
try {
|
|
690
|
-
|
|
675
|
+
b = await this.#o.generateContent(w);
|
|
691
676
|
} catch (f) {
|
|
692
677
|
throw this.#b(f, n), f;
|
|
693
678
|
}
|
|
694
|
-
const { text:
|
|
695
|
-
let u =
|
|
679
|
+
const { text: m, usage: y } = b;
|
|
680
|
+
let u = m;
|
|
696
681
|
if (r) {
|
|
697
682
|
const f = u.match(/^\s*{\s*"Rating"\s*:\s*/);
|
|
698
683
|
f && (u = u.slice(f[0].length));
|
|
@@ -744,25 +729,25 @@ class d extends EventTarget {
|
|
|
744
729
|
n && n.addEventListener("abort", a);
|
|
745
730
|
try {
|
|
746
731
|
if (e.responseConstraint) {
|
|
747
|
-
|
|
732
|
+
h.#u(
|
|
748
733
|
e.responseConstraint,
|
|
749
734
|
r.#t
|
|
750
735
|
);
|
|
751
736
|
const g = O(
|
|
752
737
|
e.responseConstraint
|
|
753
738
|
);
|
|
754
|
-
r.#n.generationConfig.responseMimeType = "application/json", r.#n.generationConfig.responseSchema = g, r.#
|
|
739
|
+
r.#n.generationConfig.responseMimeType = "application/json", r.#n.generationConfig.responseSchema = g, r.#o.createSession(
|
|
755
740
|
r.#a,
|
|
756
741
|
r.#n
|
|
757
742
|
);
|
|
758
743
|
}
|
|
759
|
-
const l = r.#
|
|
744
|
+
const l = r.#E(t), c = await r.#l(t);
|
|
760
745
|
if (r.#r)
|
|
761
746
|
throw new (r.#t.DOMException || globalThis.DOMException)(
|
|
762
747
|
"Session is destroyed",
|
|
763
748
|
"InvalidStateError"
|
|
764
749
|
);
|
|
765
|
-
const p = { role: "user", parts: c }, w = r.#
|
|
750
|
+
const p = { role: "user", parts: c }, w = r.#p(c);
|
|
766
751
|
if (w === "QuotaExceededError") {
|
|
767
752
|
const g = r.#t && r.#t.QuotaExceededError || r.#t && r.#t.DOMException || globalThis.QuotaExceededError || globalThis.DOMException, x = new g(
|
|
768
753
|
"The prompt is too large, it exceeds the quota.",
|
|
@@ -778,25 +763,25 @@ class d extends EventTarget {
|
|
|
778
763
|
r.dispatchEvent(new Event("contextoverflow")), o.enqueue("Mock response for quota overflow test."), o.close();
|
|
779
764
|
return;
|
|
780
765
|
}
|
|
781
|
-
const
|
|
782
|
-
r.#n.systemInstruction &&
|
|
766
|
+
const b = [...r.#e, p];
|
|
767
|
+
r.#n.systemInstruction && b.unshift({
|
|
783
768
|
role: "system",
|
|
784
769
|
parts: [{ text: r.#n.systemInstruction }]
|
|
785
770
|
});
|
|
786
|
-
const
|
|
787
|
-
|
|
771
|
+
const m = await r.#o.countTokens(
|
|
772
|
+
b
|
|
788
773
|
);
|
|
789
|
-
if (
|
|
774
|
+
if (m > r.contextWindow) {
|
|
790
775
|
const g = r.#t && r.#t.QuotaExceededError || r.#t && r.#t.DOMException || globalThis.QuotaExceededError || globalThis.DOMException, x = new g(
|
|
791
|
-
`The prompt is too large (${
|
|
776
|
+
`The prompt is too large (${m} tokens), it exceeds the quota of ${r.contextWindow} tokens.`,
|
|
792
777
|
"QuotaExceededError"
|
|
793
778
|
);
|
|
794
779
|
throw Object.defineProperty(x, "code", {
|
|
795
780
|
value: 22,
|
|
796
781
|
configurable: !0
|
|
797
|
-
}), x.requested =
|
|
782
|
+
}), x.requested = m, x.quota = r.contextWindow, x;
|
|
798
783
|
}
|
|
799
|
-
|
|
784
|
+
m > r.contextWindow && r.dispatchEvent(new Event("contextoverflow"));
|
|
800
785
|
const y = [...r.#e, p];
|
|
801
786
|
let u;
|
|
802
787
|
try {
|
|
@@ -804,20 +789,20 @@ class d extends EventTarget {
|
|
|
804
789
|
} catch (g) {
|
|
805
790
|
throw r.#b(g, c), g;
|
|
806
791
|
}
|
|
807
|
-
let f = "",
|
|
792
|
+
let f = "", d = !1, E = "";
|
|
808
793
|
for await (const g of u) {
|
|
809
794
|
if (i) {
|
|
810
795
|
typeof u.return == "function" && await u.return();
|
|
811
796
|
return;
|
|
812
797
|
}
|
|
813
798
|
let x = g.text();
|
|
814
|
-
if (l && !
|
|
799
|
+
if (l && !d) {
|
|
815
800
|
E += x;
|
|
816
801
|
const v = E.match(/^\s*{\s*"Rating"\s*:\s*/);
|
|
817
802
|
if (v)
|
|
818
|
-
x = E.slice(v[0].length),
|
|
803
|
+
x = E.slice(v[0].length), d = !0, E = "";
|
|
819
804
|
else if (E.length > 50)
|
|
820
|
-
x = E,
|
|
805
|
+
x = E, d = !0, E = "";
|
|
821
806
|
else
|
|
822
807
|
continue;
|
|
823
808
|
}
|
|
@@ -879,7 +864,7 @@ class d extends EventTarget {
|
|
|
879
864
|
"Session is destroyed",
|
|
880
865
|
"InvalidStateError"
|
|
881
866
|
);
|
|
882
|
-
const r = this.#
|
|
867
|
+
const r = this.#p(e);
|
|
883
868
|
return r === "QuotaExceededError" ? 1e7 : r === "contextoverflow" ? 5e5 : await this.#o.countTokens([
|
|
884
869
|
{ role: "user", parts: e }
|
|
885
870
|
]) || 0;
|
|
@@ -890,16 +875,16 @@ class d extends EventTarget {
|
|
|
890
875
|
}
|
|
891
876
|
}
|
|
892
877
|
// Volkswagen mode detection to avoid cloud costs for WPT tests.
|
|
893
|
-
#
|
|
894
|
-
return
|
|
878
|
+
#p(t) {
|
|
879
|
+
return h.#y(t);
|
|
895
880
|
}
|
|
896
|
-
static #
|
|
881
|
+
static #y(t) {
|
|
897
882
|
if (t.length !== 1 || !t[0].text)
|
|
898
883
|
return null;
|
|
899
884
|
const e = t[0].text;
|
|
900
885
|
return typeof e != "string" || !e.startsWith("Please write a sentence in English.") ? null : e.length > 1e7 ? "QuotaExceededError" : e.length > 5e4 ? "contextoverflow" : null;
|
|
901
886
|
}
|
|
902
|
-
static #
|
|
887
|
+
static #u(t, e) {
|
|
903
888
|
if (t)
|
|
904
889
|
try {
|
|
905
890
|
JSON.stringify(t);
|
|
@@ -910,7 +895,7 @@ class d extends EventTarget {
|
|
|
910
895
|
);
|
|
911
896
|
}
|
|
912
897
|
}
|
|
913
|
-
#
|
|
898
|
+
#E(t) {
|
|
914
899
|
if (Array.isArray(t)) {
|
|
915
900
|
for (const e of t)
|
|
916
901
|
if (e.prefix && (e.role === "assistant" || e.role === "model") && typeof e.content == "string" && e.content.includes('"Rating":'))
|
|
@@ -1041,7 +1026,7 @@ const D = (s) => {
|
|
|
1041
1026
|
try {
|
|
1042
1027
|
if (!s || s.LanguageModel?.__isPolyfill)
|
|
1043
1028
|
return;
|
|
1044
|
-
const t = class extends
|
|
1029
|
+
const t = class extends h {
|
|
1045
1030
|
};
|
|
1046
1031
|
t.__window = s, t.__isPolyfill = !0, s.LanguageModel = t, s.DOMException && (s.QuotaExceededError = s.DOMException);
|
|
1047
1032
|
} catch {
|
|
@@ -1062,22 +1047,22 @@ if (typeof HTMLIFrameElement < "u")
|
|
|
1062
1047
|
});
|
|
1063
1048
|
} catch {
|
|
1064
1049
|
}
|
|
1065
|
-
const
|
|
1050
|
+
const S = new MutationObserver((s) => {
|
|
1066
1051
|
for (const t of s)
|
|
1067
1052
|
for (const e of t.addedNodes)
|
|
1068
1053
|
e.tagName === "IFRAME" && (D(e.contentWindow), e.addEventListener("load", () => D(e.contentWindow), {
|
|
1069
1054
|
once: !1
|
|
1070
1055
|
}));
|
|
1071
1056
|
});
|
|
1072
|
-
globalThis.document?.documentElement && (
|
|
1057
|
+
globalThis.document?.documentElement && (S.observe(globalThis.document.documentElement, {
|
|
1073
1058
|
childList: !0,
|
|
1074
1059
|
subtree: !0
|
|
1075
1060
|
}), globalThis.document.querySelectorAll("iframe").forEach((s) => {
|
|
1076
1061
|
D(s.contentWindow);
|
|
1077
1062
|
}));
|
|
1078
|
-
(!("LanguageModel" in globalThis) || globalThis.__FORCE_PROMPT_API_POLYFILL__) && (globalThis.LanguageModel =
|
|
1063
|
+
(!("LanguageModel" in globalThis) || globalThis.__FORCE_PROMPT_API_POLYFILL__) && (globalThis.LanguageModel = h, h.__isPolyfill = !0, console.log(
|
|
1079
1064
|
"Polyfill: window.LanguageModel is now backed by the Prompt API polyfill."
|
|
1080
1065
|
));
|
|
1081
1066
|
export {
|
|
1082
|
-
|
|
1067
|
+
h as LanguageModel
|
|
1083
1068
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "prompt-api-polyfill",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.11.0",
|
|
4
4
|
"description": "Polyfill for the Prompt API (`LanguageModel`) backed by Firebase AI Logic, Gemini API, OpenAI API, or Transformers.js.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/prompt-api-polyfill.js",
|
|
@@ -42,14 +42,19 @@
|
|
|
42
42
|
"sync:wpt": "node scripts/sync-wpt.js",
|
|
43
43
|
"generate:wpt": "npm run generate:registry && node scripts/backend-discovery.js && node scripts/generate-wpt-wrappers.js",
|
|
44
44
|
"test:wpt": "npm run generate:registry && npm run sync:wpt && npm run generate:wpt && npx vite --open /tests/wpt/index.html",
|
|
45
|
+
"lint": "eslint .",
|
|
45
46
|
"fix": "npx prettier --write ."
|
|
46
47
|
},
|
|
47
48
|
"devDependencies": {
|
|
49
|
+
"@eslint/js": "^10.0.1",
|
|
50
|
+
"eslint": "^10.0.2",
|
|
51
|
+
"eslint-config-prettier": "^10.1.8",
|
|
52
|
+
"globals": "^17.3.0",
|
|
48
53
|
"prettier": "^3.8.1",
|
|
49
54
|
"prettier-plugin-curly": "^0.4.1",
|
|
50
55
|
"vite": "^7.3.1",
|
|
51
56
|
"node-gyp": "^12.2.0",
|
|
52
|
-
"node-addon-api": "^8.
|
|
57
|
+
"node-addon-api": "^8.6.0"
|
|
53
58
|
},
|
|
54
59
|
"dependencies": {
|
|
55
60
|
"@google/genai": "^1.43.0",
|