prompt-api-polyfill 1.3.1 → 1.5.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/README.md +1 -1
- package/dist/prompt-api-polyfill.js +239 -215
- package/dot_env.json +11 -1
- package/package.json +6 -4
package/README.md
CHANGED
|
@@ -180,7 +180,7 @@ including:
|
|
|
180
180
|
- `prompt()` and `promptStreaming()`
|
|
181
181
|
- Multimodal inputs (text, image, audio)
|
|
182
182
|
- `append()` and `measureContextUsage()`
|
|
183
|
-
- Quota handling via `
|
|
183
|
+
- Quota handling via `oncontextoverflow`
|
|
184
184
|
- `clone()` and `destroy()`
|
|
185
185
|
|
|
186
186
|
A simplified version of how it is wired up:
|
|
@@ -27,13 +27,13 @@ class M {
|
|
|
27
27
|
if (t instanceof Blob)
|
|
28
28
|
return this.blobToInlineData(t);
|
|
29
29
|
if (ArrayBuffer.isView(t) || t instanceof ArrayBuffer) {
|
|
30
|
-
const e = t instanceof ArrayBuffer ? new Uint8Array(t) : new Uint8Array(t.buffer, t.byteOffset, t.byteLength),
|
|
30
|
+
const e = t instanceof ArrayBuffer ? new Uint8Array(t) : new Uint8Array(t.buffer, t.byteOffset, t.byteLength), o = e.buffer.slice(
|
|
31
31
|
e.byteOffset,
|
|
32
32
|
e.byteOffset + e.byteLength
|
|
33
|
-
),
|
|
33
|
+
), r = this.arrayBufferToBase64(o), n = this.#o(e);
|
|
34
34
|
if (!n)
|
|
35
35
|
throw new DOMException("Invalid image data", "InvalidStateError");
|
|
36
|
-
return { inlineData: { data:
|
|
36
|
+
return { inlineData: { data: r, mimeType: n } };
|
|
37
37
|
}
|
|
38
38
|
return this.canvasSourceToInlineData(t);
|
|
39
39
|
}
|
|
@@ -55,20 +55,20 @@ class M {
|
|
|
55
55
|
if (t[0] === 73 && t[1] === 73 && t[2] === 42 || t[0] === 77 && t[1] === 77 && t[2] === 42)
|
|
56
56
|
return "image/tiff";
|
|
57
57
|
if (t[4] === 102 && t[5] === 116 && t[6] === 121 && t[7] === 112) {
|
|
58
|
-
const
|
|
59
|
-
if (
|
|
58
|
+
const r = String.fromCharCode(t[8], t[9], t[10], t[11]);
|
|
59
|
+
if (r === "avif" || r === "avis")
|
|
60
60
|
return "image/avif";
|
|
61
|
-
if (
|
|
61
|
+
if (r === "heic" || r === "heix" || r === "hevc" || r === "hevx")
|
|
62
62
|
return "image/heic";
|
|
63
|
-
if (
|
|
63
|
+
if (r === "mif1" || r === "msf1")
|
|
64
64
|
return "image/heif";
|
|
65
65
|
}
|
|
66
66
|
if (t[0] === 255 && t[1] === 10 || t[0] === 0 && t[4] === 74 && t[5] === 88 && t[6] === 76)
|
|
67
67
|
return "image/jxl";
|
|
68
68
|
if (t[0] === 0 && t[4] === 106 && t[5] === 80 && t[6] === 32)
|
|
69
69
|
return "image/jp2";
|
|
70
|
-
const
|
|
71
|
-
return
|
|
70
|
+
const o = String.fromCharCode(...t.slice(0, 100)).toLowerCase();
|
|
71
|
+
return o.includes("<svg") || o.includes("<?xml") ? "image/svg+xml" : null;
|
|
72
72
|
}
|
|
73
73
|
static async processAudio(t) {
|
|
74
74
|
if (t instanceof Blob) {
|
|
@@ -77,15 +77,15 @@ class M {
|
|
|
77
77
|
return this.blobToInlineData(t);
|
|
78
78
|
}
|
|
79
79
|
if (t instanceof AudioBuffer) {
|
|
80
|
-
const
|
|
81
|
-
return { inlineData: { data: this.arrayBufferToBase64(
|
|
80
|
+
const r = this.audioBufferToWav(t);
|
|
81
|
+
return { inlineData: { data: this.arrayBufferToBase64(r), mimeType: "audio/wav" } };
|
|
82
82
|
}
|
|
83
|
-
const e = t instanceof ArrayBuffer || t && t.constructor && t.constructor.name === "ArrayBuffer",
|
|
84
|
-
if (e ||
|
|
85
|
-
const
|
|
83
|
+
const e = 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");
|
|
84
|
+
if (e || o) {
|
|
85
|
+
const r = e ? t : t.buffer;
|
|
86
86
|
return {
|
|
87
87
|
inlineData: {
|
|
88
|
-
data: this.arrayBufferToBase64(
|
|
88
|
+
data: this.arrayBufferToBase64(r),
|
|
89
89
|
mimeType: "audio/wav"
|
|
90
90
|
// Fallback assumption
|
|
91
91
|
}
|
|
@@ -95,16 +95,16 @@ class M {
|
|
|
95
95
|
}
|
|
96
96
|
// Low Level Converters
|
|
97
97
|
static blobToInlineData(t) {
|
|
98
|
-
return new Promise((e,
|
|
99
|
-
const
|
|
100
|
-
|
|
101
|
-
|
|
98
|
+
return new Promise((e, o) => {
|
|
99
|
+
const r = new FileReader();
|
|
100
|
+
r.onloadend = () => {
|
|
101
|
+
r.error ? o(r.error) : e({
|
|
102
102
|
inlineData: {
|
|
103
|
-
data:
|
|
103
|
+
data: r.result.split(",")[1],
|
|
104
104
|
mimeType: t.type
|
|
105
105
|
}
|
|
106
106
|
});
|
|
107
|
-
},
|
|
107
|
+
}, r.readAsDataURL(t);
|
|
108
108
|
});
|
|
109
109
|
}
|
|
110
110
|
static async canvasSourceToInlineData(t) {
|
|
@@ -114,23 +114,23 @@ class M {
|
|
|
114
114
|
const l = t[a];
|
|
115
115
|
return typeof l == "object" && l !== null && "baseVal" in l ? l.baseVal.value : typeof l == "number" ? l : 0;
|
|
116
116
|
};
|
|
117
|
-
let
|
|
118
|
-
if ((!
|
|
117
|
+
let o = t.displayWidth || t.naturalWidth || t.videoWidth || e("width"), r = t.displayHeight || t.naturalHeight || t.videoHeight || e("height");
|
|
118
|
+
if ((!o || !r) && typeof t.getBBox == "function")
|
|
119
119
|
try {
|
|
120
120
|
const a = t.getBBox();
|
|
121
|
-
|
|
121
|
+
o = o || a.width, r = r || a.height;
|
|
122
122
|
} catch {
|
|
123
123
|
}
|
|
124
|
-
if ((!
|
|
124
|
+
if ((!o || !r) && typeof t.getBoundingClientRect == "function")
|
|
125
125
|
try {
|
|
126
126
|
const a = t.getBoundingClientRect();
|
|
127
|
-
|
|
127
|
+
o = o || a.width, r = r || a.height;
|
|
128
128
|
} catch {
|
|
129
129
|
}
|
|
130
|
-
if (!
|
|
130
|
+
if (!o || !r)
|
|
131
131
|
throw new DOMException("Invalid image dimensions", "InvalidStateError");
|
|
132
132
|
const n = document.createElement("canvas");
|
|
133
|
-
n.width =
|
|
133
|
+
n.width = o, n.height = r;
|
|
134
134
|
const i = n.getContext("2d");
|
|
135
135
|
return typeof ImageData < "u" && t instanceof ImageData ? i.putImageData(t, 0, 0) : i.drawImage(t, 0, 0), {
|
|
136
136
|
inlineData: {
|
|
@@ -141,40 +141,40 @@ class M {
|
|
|
141
141
|
}
|
|
142
142
|
static arrayBufferToBase64(t) {
|
|
143
143
|
let e = "";
|
|
144
|
-
const
|
|
145
|
-
for (let n = 0; n <
|
|
146
|
-
e += String.fromCharCode(
|
|
144
|
+
const o = new Uint8Array(t), r = o.byteLength;
|
|
145
|
+
for (let n = 0; n < r; n++)
|
|
146
|
+
e += String.fromCharCode(o[n]);
|
|
147
147
|
return window.btoa(e);
|
|
148
148
|
}
|
|
149
149
|
// Simple WAV Encoder for AudioBuffer
|
|
150
150
|
static audioBufferToWav(t) {
|
|
151
|
-
const e = t.numberOfChannels,
|
|
151
|
+
const e = t.numberOfChannels, o = t.sampleRate, r = 1, n = 16;
|
|
152
152
|
let i;
|
|
153
153
|
return e === 2 ? i = this.interleave(
|
|
154
154
|
t.getChannelData(0),
|
|
155
155
|
t.getChannelData(1)
|
|
156
|
-
) : i = t.getChannelData(0), this.encodeWAV(i,
|
|
156
|
+
) : i = t.getChannelData(0), this.encodeWAV(i, r, o, e, n);
|
|
157
157
|
}
|
|
158
158
|
static interleave(t, e) {
|
|
159
|
-
const
|
|
159
|
+
const o = t.length + e.length, r = new Float32Array(o);
|
|
160
160
|
let n = 0, i = 0;
|
|
161
|
-
for (; n <
|
|
162
|
-
|
|
163
|
-
return
|
|
161
|
+
for (; n < o; )
|
|
162
|
+
r[n++] = t[i], r[n++] = e[i], i++;
|
|
163
|
+
return r;
|
|
164
164
|
}
|
|
165
|
-
static encodeWAV(t, e,
|
|
166
|
-
const i = n / 8, c =
|
|
167
|
-
return this.writeString(l, 0, "RIFF"), l.setUint32(4, 36 + t.length * i, !0), this.writeString(l, 8, "WAVE"), this.writeString(l, 12, "fmt "), l.setUint32(16, 16, !0), l.setUint16(20, e, !0), l.setUint16(22,
|
|
165
|
+
static encodeWAV(t, e, o, r, n) {
|
|
166
|
+
const i = n / 8, c = r * i, a = new ArrayBuffer(44 + t.length * i), l = new DataView(a);
|
|
167
|
+
return this.writeString(l, 0, "RIFF"), l.setUint32(4, 36 + t.length * i, !0), this.writeString(l, 8, "WAVE"), this.writeString(l, 12, "fmt "), l.setUint32(16, 16, !0), l.setUint16(20, e, !0), l.setUint16(22, r, !0), l.setUint32(24, o, !0), l.setUint32(28, o * c, !0), l.setUint16(32, c, !0), l.setUint16(34, n, !0), this.writeString(l, 36, "data"), l.setUint32(40, t.length * i, !0), this.floatTo16BitPCM(l, 44, t), a;
|
|
168
168
|
}
|
|
169
|
-
static floatTo16BitPCM(t, e,
|
|
170
|
-
for (let
|
|
171
|
-
const n = Math.max(-1, Math.min(1, r
|
|
169
|
+
static floatTo16BitPCM(t, e, o) {
|
|
170
|
+
for (let r = 0; r < o.length; r++, e += 2) {
|
|
171
|
+
const n = Math.max(-1, Math.min(1, o[r]));
|
|
172
172
|
t.setInt16(e, n < 0 ? n * 32768 : n * 32767, !0);
|
|
173
173
|
}
|
|
174
174
|
}
|
|
175
|
-
static writeString(t, e,
|
|
176
|
-
for (let
|
|
177
|
-
t.setUint8(e +
|
|
175
|
+
static writeString(t, e, o) {
|
|
176
|
+
for (let r = 0; r < o.length; r++)
|
|
177
|
+
t.setUint8(e + r, o.charCodeAt(r));
|
|
178
178
|
}
|
|
179
179
|
}
|
|
180
180
|
function v(s) {
|
|
@@ -204,14 +204,14 @@ function v(s) {
|
|
|
204
204
|
items: v(s.items)
|
|
205
205
|
});
|
|
206
206
|
case "object":
|
|
207
|
-
const e = {},
|
|
208
|
-
|
|
207
|
+
const e = {}, o = s.properties ? Object.keys(s.properties) : [];
|
|
208
|
+
o.forEach((i) => {
|
|
209
209
|
e[i] = v(
|
|
210
210
|
s.properties[i]
|
|
211
211
|
);
|
|
212
212
|
});
|
|
213
|
-
const
|
|
214
|
-
(i) => !
|
|
213
|
+
const r = s.required || [], n = o.filter(
|
|
214
|
+
(i) => !r.includes(i)
|
|
215
215
|
);
|
|
216
216
|
return T.object({
|
|
217
217
|
...t,
|
|
@@ -255,11 +255,11 @@ async function I(s) {
|
|
|
255
255
|
}
|
|
256
256
|
async function C(s, t = globalThis) {
|
|
257
257
|
const e = [];
|
|
258
|
-
for (const
|
|
259
|
-
const
|
|
258
|
+
for (const o of s) {
|
|
259
|
+
const r = o.role === "assistant" ? "model" : "user", n = r === "model";
|
|
260
260
|
let i = [];
|
|
261
|
-
if (Array.isArray(
|
|
262
|
-
for (const c of
|
|
261
|
+
if (Array.isArray(o.content))
|
|
262
|
+
for (const c of o.content)
|
|
263
263
|
if (c.type === "text") {
|
|
264
264
|
const a = c.value || c.text || "";
|
|
265
265
|
if (typeof a != "string")
|
|
@@ -278,12 +278,12 @@ async function C(s, t = globalThis) {
|
|
|
278
278
|
i.push(a);
|
|
279
279
|
}
|
|
280
280
|
else
|
|
281
|
-
i.push({ text:
|
|
282
|
-
e.push({ role:
|
|
281
|
+
i.push({ text: o.content });
|
|
282
|
+
e.push({ role: r, parts: i });
|
|
283
283
|
}
|
|
284
284
|
return e;
|
|
285
285
|
}
|
|
286
|
-
class
|
|
286
|
+
class d extends EventTarget {
|
|
287
287
|
#o;
|
|
288
288
|
#f;
|
|
289
289
|
#r;
|
|
@@ -293,8 +293,8 @@ class h extends EventTarget {
|
|
|
293
293
|
#i;
|
|
294
294
|
#c;
|
|
295
295
|
#t;
|
|
296
|
-
constructor(t, e,
|
|
297
|
-
super(), this.#o = t, this.#f = e, this.#r =
|
|
296
|
+
constructor(t, e, o, r = {}, n, i = 0, c = globalThis) {
|
|
297
|
+
super(), this.#o = t, this.#f = e, this.#r = o || [], this.#s = r, this.#e = n, this.#n = !1, this.#i = i, this.#c = {}, this.#t = c;
|
|
298
298
|
}
|
|
299
299
|
get contextUsage() {
|
|
300
300
|
return this.#i;
|
|
@@ -302,87 +302,84 @@ class h extends EventTarget {
|
|
|
302
302
|
get contextWindow() {
|
|
303
303
|
return 1e6;
|
|
304
304
|
}
|
|
305
|
-
get
|
|
305
|
+
get oncontextoverflow() {
|
|
306
306
|
return this.#c;
|
|
307
307
|
}
|
|
308
|
-
set
|
|
309
|
-
this.#c && this.removeEventListener(
|
|
310
|
-
"contextwindowoverflow",
|
|
311
|
-
this.#c
|
|
312
|
-
), this.#c = t, typeof t == "function" && this.addEventListener("contextwindowoverflow", t);
|
|
308
|
+
set oncontextoverflow(t) {
|
|
309
|
+
this.#c && this.removeEventListener("contextoverflow", this.#c), this.#c = t, typeof t == "function" && this.addEventListener("contextoverflow", t);
|
|
313
310
|
}
|
|
314
|
-
static #
|
|
311
|
+
static #h(t) {
|
|
315
312
|
try {
|
|
316
313
|
if (!t || !t.document || t.document.defaultView !== t)
|
|
317
314
|
throw new Error();
|
|
318
315
|
if (t !== globalThis && t !== t.top && (!t.frameElement || !t.frameElement.isConnected))
|
|
319
316
|
throw new Error();
|
|
320
317
|
} catch {
|
|
321
|
-
const
|
|
322
|
-
throw new
|
|
318
|
+
const o = t?.DOMException || globalThis.DOMException;
|
|
319
|
+
throw new o(
|
|
323
320
|
"The execution context is not valid.",
|
|
324
321
|
"InvalidStateError"
|
|
325
322
|
);
|
|
326
323
|
}
|
|
327
324
|
}
|
|
328
325
|
#a() {
|
|
329
|
-
h
|
|
326
|
+
d.#h(this.#t);
|
|
330
327
|
}
|
|
331
328
|
static async availability(t = {}) {
|
|
332
329
|
const e = this.__window || globalThis;
|
|
333
|
-
h
|
|
330
|
+
d.#h(e);
|
|
334
331
|
try {
|
|
335
|
-
await
|
|
336
|
-
} catch (
|
|
337
|
-
if (
|
|
338
|
-
if (
|
|
339
|
-
throw
|
|
332
|
+
await d.#w(t, e);
|
|
333
|
+
} catch (r) {
|
|
334
|
+
if (r instanceof RangeError) {
|
|
335
|
+
if (r.message.includes("language tag"))
|
|
336
|
+
throw r;
|
|
340
337
|
return "unavailable";
|
|
341
338
|
}
|
|
342
|
-
if (
|
|
339
|
+
if (r.name === "NotSupportedError")
|
|
343
340
|
return "unavailable";
|
|
344
|
-
if (
|
|
345
|
-
if (/system/i.test(
|
|
341
|
+
if (r instanceof TypeError) {
|
|
342
|
+
if (/system/i.test(r.message))
|
|
346
343
|
return "unavailable";
|
|
347
|
-
throw
|
|
344
|
+
throw r;
|
|
348
345
|
}
|
|
349
346
|
return "unavailable";
|
|
350
347
|
}
|
|
351
|
-
return (await
|
|
348
|
+
return (await d.#p(e)).availability(t);
|
|
352
349
|
}
|
|
353
|
-
static #
|
|
354
|
-
static #
|
|
355
|
-
for (const
|
|
356
|
-
const
|
|
357
|
-
if (
|
|
358
|
-
return { ...
|
|
350
|
+
static #g = A;
|
|
351
|
+
static #d(t = globalThis) {
|
|
352
|
+
for (const o of d.#g) {
|
|
353
|
+
const r = t[o.config] || globalThis[o.config];
|
|
354
|
+
if (r && r.apiKey)
|
|
355
|
+
return { ...o, configValue: r };
|
|
359
356
|
}
|
|
360
|
-
const e =
|
|
357
|
+
const e = d.#g.map((o) => `window.${o.config}`).join(", ");
|
|
361
358
|
throw new (t.DOMException || globalThis.DOMException)(
|
|
362
359
|
`Prompt API Polyfill: No backend configuration found. Please set one of: ${e}.`,
|
|
363
360
|
"NotSupportedError"
|
|
364
361
|
);
|
|
365
362
|
}
|
|
366
363
|
static async #p(t = globalThis) {
|
|
367
|
-
const e =
|
|
364
|
+
const e = d.#d(t);
|
|
368
365
|
return I(e.path);
|
|
369
366
|
}
|
|
370
367
|
static async #w(t = {}, e = globalThis) {
|
|
371
368
|
if (t.expectedInputs)
|
|
372
|
-
for (const
|
|
373
|
-
if (
|
|
374
|
-
throw new TypeError(`Invalid input type: ${
|
|
375
|
-
|
|
369
|
+
for (const r of t.expectedInputs) {
|
|
370
|
+
if (r.type !== "text" && r.type !== "image" && r.type !== "audio")
|
|
371
|
+
throw new TypeError(`Invalid input type: ${r.type}`);
|
|
372
|
+
r.languages && d.#y(r.languages);
|
|
376
373
|
}
|
|
377
374
|
if (t.expectedOutputs)
|
|
378
|
-
for (const
|
|
379
|
-
if (
|
|
380
|
-
throw new RangeError(`Unsupported output type: ${
|
|
381
|
-
|
|
375
|
+
for (const r of t.expectedOutputs) {
|
|
376
|
+
if (r.type !== "text")
|
|
377
|
+
throw new RangeError(`Unsupported output type: ${r.type}`);
|
|
378
|
+
r.languages && d.#y(r.languages);
|
|
382
379
|
}
|
|
383
|
-
const
|
|
380
|
+
const o = t.expectedInputs ? ["text", ...t.expectedInputs.map((r) => r.type)] : ["text"];
|
|
384
381
|
if (t.initialPrompts && Array.isArray(t.initialPrompts)) {
|
|
385
|
-
let
|
|
382
|
+
let r = !1;
|
|
386
383
|
for (let n = 0; n < t.initialPrompts.length; n++) {
|
|
387
384
|
const i = t.initialPrompts[n];
|
|
388
385
|
if (i.role === "system") {
|
|
@@ -390,22 +387,22 @@ class h extends EventTarget {
|
|
|
390
387
|
throw new TypeError(
|
|
391
388
|
"The prompt with 'system' role must be placed at the first entry of initialPrompts."
|
|
392
389
|
);
|
|
393
|
-
if (
|
|
390
|
+
if (r)
|
|
394
391
|
throw new TypeError(
|
|
395
392
|
"The prompt with 'system' role must be placed at the first entry of initialPrompts."
|
|
396
393
|
);
|
|
397
|
-
|
|
394
|
+
r = !0;
|
|
398
395
|
}
|
|
399
396
|
if (Array.isArray(i.content))
|
|
400
397
|
for (const c of i.content) {
|
|
401
398
|
const a = c.type || "text";
|
|
402
|
-
if (!
|
|
399
|
+
if (!o.includes(a))
|
|
403
400
|
throw new (e.DOMException || globalThis.DOMException)(
|
|
404
401
|
`The content type "${a}" is not in the expectedInputs.`,
|
|
405
402
|
"NotSupportedError"
|
|
406
403
|
);
|
|
407
404
|
}
|
|
408
|
-
else if (!
|
|
405
|
+
else if (!o.includes("text"))
|
|
409
406
|
throw new (e.DOMException || globalThis.DOMException)(
|
|
410
407
|
'The content type "text" is not in the expectedInputs.',
|
|
411
408
|
"NotSupportedError"
|
|
@@ -434,18 +431,18 @@ class h extends EventTarget {
|
|
|
434
431
|
}
|
|
435
432
|
static async create(t = {}) {
|
|
436
433
|
const e = this.__window || globalThis;
|
|
437
|
-
if (h
|
|
434
|
+
if (d.#h(e), await d.#w(t, e), t.signal?.aborted)
|
|
438
435
|
throw t.signal.reason || new (e.DOMException || globalThis.DOMException)(
|
|
439
436
|
"Aborted",
|
|
440
437
|
"AbortError"
|
|
441
438
|
);
|
|
442
|
-
const
|
|
443
|
-
if (
|
|
439
|
+
const o = await this.availability(t);
|
|
440
|
+
if (o === "unavailable")
|
|
444
441
|
throw new (e.DOMException || globalThis.DOMException)(
|
|
445
442
|
"The model is not available for the given options.",
|
|
446
443
|
"NotSupportedError"
|
|
447
444
|
);
|
|
448
|
-
if (
|
|
445
|
+
if (o === "downloadable" || o === "downloading")
|
|
449
446
|
throw new (e.DOMException || globalThis.DOMException)(
|
|
450
447
|
'Requires a user gesture when availability is "downloading" or "downloadable".',
|
|
451
448
|
"NotAllowedError"
|
|
@@ -455,8 +452,8 @@ class h extends EventTarget {
|
|
|
455
452
|
"Aborted",
|
|
456
453
|
"AbortError"
|
|
457
454
|
);
|
|
458
|
-
const
|
|
459
|
-
|
|
455
|
+
const r = d.#d(e), n = await d.#p(e), i = new n(r.configValue), c = { ...t };
|
|
456
|
+
d.#x(
|
|
460
457
|
c.responseConstraint,
|
|
461
458
|
e
|
|
462
459
|
);
|
|
@@ -471,16 +468,16 @@ class h extends EventTarget {
|
|
|
471
468
|
), p = c.initialPrompts.filter(
|
|
472
469
|
(f) => f.role !== "system"
|
|
473
470
|
);
|
|
474
|
-
w.length > 0 && (a.systemInstruction = w.map((f) => typeof f.content == "string" ? f.content : Array.isArray(f.content) ? f.content.filter((
|
|
471
|
+
w.length > 0 && (a.systemInstruction = w.map((f) => typeof f.content == "string" ? f.content : Array.isArray(f.content) ? f.content.filter((h) => h.type === "text").map((h) => h.value || h.text || "").join(`
|
|
475
472
|
`) : "").join(`
|
|
476
473
|
`)), l = await C(p, e);
|
|
477
474
|
for (const f of c.initialPrompts) {
|
|
478
475
|
if (typeof f.content != "string")
|
|
479
476
|
continue;
|
|
480
|
-
const
|
|
477
|
+
const h = d.#E([
|
|
481
478
|
{ text: f.content }
|
|
482
479
|
]);
|
|
483
|
-
if (
|
|
480
|
+
if (h === "QuotaExceededError" || h === "contextoverflow") {
|
|
484
481
|
const E = e.QuotaExceededError || e.DOMException || globalThis.QuotaExceededError || globalThis.DOMException, u = new E(
|
|
485
482
|
"The initial prompts are too large, they exceed the quota.",
|
|
486
483
|
"QuotaExceededError"
|
|
@@ -489,39 +486,39 @@ class h extends EventTarget {
|
|
|
489
486
|
value: 22,
|
|
490
487
|
configurable: !0
|
|
491
488
|
});
|
|
492
|
-
const
|
|
493
|
-
throw u.requested =
|
|
489
|
+
const x = h === "QuotaExceededError" ? 1e7 : 5e5;
|
|
490
|
+
throw u.requested = x, u.quota = 1e6, u;
|
|
494
491
|
}
|
|
495
492
|
}
|
|
496
493
|
}
|
|
497
|
-
let
|
|
494
|
+
let g = null;
|
|
498
495
|
if (typeof c.monitor == "function") {
|
|
499
|
-
|
|
496
|
+
g = new EventTarget();
|
|
500
497
|
try {
|
|
501
|
-
c.monitor(
|
|
498
|
+
c.monitor(g);
|
|
502
499
|
} catch (w) {
|
|
503
500
|
throw w;
|
|
504
501
|
}
|
|
505
502
|
}
|
|
506
|
-
|
|
503
|
+
g && (g.__lastProgressLoaded = -1);
|
|
507
504
|
const b = async (w) => {
|
|
508
|
-
if (!
|
|
505
|
+
if (!g || t.signal?.aborted)
|
|
509
506
|
return !t.signal?.aborted;
|
|
510
507
|
const p = 1 / 65536, f = Math.floor(w / p) * p;
|
|
511
|
-
if (f <=
|
|
508
|
+
if (f <= g.__lastProgressLoaded)
|
|
512
509
|
return !0;
|
|
513
510
|
try {
|
|
514
|
-
|
|
511
|
+
g.dispatchEvent(
|
|
515
512
|
new ProgressEvent("downloadprogress", {
|
|
516
513
|
loaded: f,
|
|
517
514
|
total: 1,
|
|
518
515
|
lengthComputable: !0
|
|
519
516
|
})
|
|
520
|
-
),
|
|
521
|
-
} catch (
|
|
522
|
-
console.error("Error dispatching downloadprogress events:",
|
|
517
|
+
), g.__lastProgressLoaded = f;
|
|
518
|
+
} catch (h) {
|
|
519
|
+
console.error("Error dispatching downloadprogress events:", h);
|
|
523
520
|
}
|
|
524
|
-
return await new Promise((
|
|
521
|
+
return await new Promise((h) => setTimeout(h, 0)), !t.signal?.aborted;
|
|
525
522
|
};
|
|
526
523
|
if (!await b(0))
|
|
527
524
|
throw t.signal.reason || new (e.DOMException || globalThis.DOMException)(
|
|
@@ -531,7 +528,7 @@ class h extends EventTarget {
|
|
|
531
528
|
const m = await i.createSession(
|
|
532
529
|
c,
|
|
533
530
|
a,
|
|
534
|
-
|
|
531
|
+
g
|
|
535
532
|
);
|
|
536
533
|
if (!await b(1))
|
|
537
534
|
throw t.signal.reason || new (e.DOMException || globalThis.DOMException)(
|
|
@@ -573,8 +570,8 @@ class h extends EventTarget {
|
|
|
573
570
|
"Aborted",
|
|
574
571
|
"AbortError"
|
|
575
572
|
);
|
|
576
|
-
const e = JSON.parse(JSON.stringify(this.#r)),
|
|
577
|
-
|
|
573
|
+
const e = JSON.parse(JSON.stringify(this.#r)), o = { ...this.#s, ...t }, r = await d.#p(this.#t), n = d.#d(this.#t), i = new r(n.configValue), c = await i.createSession(
|
|
574
|
+
o,
|
|
578
575
|
this.#e
|
|
579
576
|
);
|
|
580
577
|
if (t.signal?.aborted)
|
|
@@ -586,7 +583,7 @@ class h extends EventTarget {
|
|
|
586
583
|
i,
|
|
587
584
|
c,
|
|
588
585
|
e,
|
|
589
|
-
|
|
586
|
+
o,
|
|
590
587
|
this.#e,
|
|
591
588
|
this.#i,
|
|
592
589
|
this.#t
|
|
@@ -609,7 +606,7 @@ class h extends EventTarget {
|
|
|
609
606
|
if (typeof t == "object" && t !== null && !Array.isArray(t) && Object.keys(t).length === 0)
|
|
610
607
|
return "[object Object]";
|
|
611
608
|
if (e.responseConstraint) {
|
|
612
|
-
|
|
609
|
+
d.#x(
|
|
613
610
|
e.responseConstraint,
|
|
614
611
|
this.#t
|
|
615
612
|
);
|
|
@@ -621,13 +618,13 @@ class h extends EventTarget {
|
|
|
621
618
|
this.#e
|
|
622
619
|
);
|
|
623
620
|
}
|
|
624
|
-
const
|
|
621
|
+
const o = this.#b(t), r = await this.#l(t);
|
|
625
622
|
if (this.#n)
|
|
626
623
|
throw new (this.#t.DOMException || globalThis.DOMException)(
|
|
627
624
|
"Session is destroyed",
|
|
628
625
|
"InvalidStateError"
|
|
629
626
|
);
|
|
630
|
-
const n = { role: "user", parts:
|
|
627
|
+
const n = { role: "user", parts: r }, i = new Promise((a, l) => {
|
|
631
628
|
if (e.signal?.aborted) {
|
|
632
629
|
l(
|
|
633
630
|
e.signal.reason || new (this.#t.DOMException || globalThis.DOMException)(
|
|
@@ -650,17 +647,17 @@ class h extends EventTarget {
|
|
|
650
647
|
{ once: !0 }
|
|
651
648
|
);
|
|
652
649
|
}), c = (async () => {
|
|
653
|
-
const a = this.#u(
|
|
650
|
+
const a = this.#u(r);
|
|
654
651
|
if (a === "QuotaExceededError") {
|
|
655
|
-
const f = this.#t && this.#t.QuotaExceededError || this.#t && this.#t.DOMException || globalThis.QuotaExceededError || globalThis.DOMException,
|
|
652
|
+
const f = this.#t && this.#t.QuotaExceededError || this.#t && this.#t.DOMException || globalThis.QuotaExceededError || globalThis.DOMException, h = new f(
|
|
656
653
|
"The prompt is too large, it exceeds the quota.",
|
|
657
654
|
"QuotaExceededError"
|
|
658
655
|
);
|
|
659
|
-
Object.defineProperty(
|
|
656
|
+
Object.defineProperty(h, "code", { value: 22, configurable: !0 });
|
|
660
657
|
const E = 1e7;
|
|
661
|
-
throw
|
|
662
|
-
} else if (a === "
|
|
663
|
-
return this.dispatchEvent(new Event("
|
|
658
|
+
throw h.requested = E, h.quota = this.contextWindow, h;
|
|
659
|
+
} else if (a === "contextoverflow")
|
|
660
|
+
return this.dispatchEvent(new Event("contextoverflow")), "Mock response for quota overflow test.";
|
|
664
661
|
const l = [...this.#r, n];
|
|
665
662
|
this.#e.systemInstruction && l.unshift({
|
|
666
663
|
role: "system",
|
|
@@ -670,23 +667,23 @@ class h extends EventTarget {
|
|
|
670
667
|
l
|
|
671
668
|
);
|
|
672
669
|
if (y > this.contextWindow) {
|
|
673
|
-
const f = this.#t && this.#t.QuotaExceededError || this.#t && this.#t.DOMException || globalThis.QuotaExceededError || globalThis.DOMException,
|
|
670
|
+
const f = this.#t && this.#t.QuotaExceededError || this.#t && this.#t.DOMException || globalThis.QuotaExceededError || globalThis.DOMException, h = new f(
|
|
674
671
|
`The prompt is too large (${y} tokens), it exceeds the quota of ${this.contextWindow} tokens.`,
|
|
675
672
|
"QuotaExceededError"
|
|
676
673
|
);
|
|
677
|
-
throw Object.defineProperty(
|
|
674
|
+
throw Object.defineProperty(h, "code", { value: 22, configurable: !0 }), h.requested = y, h.quota = this.contextWindow, h;
|
|
678
675
|
}
|
|
679
|
-
y > this.contextWindow && this.dispatchEvent(new Event("
|
|
680
|
-
const
|
|
676
|
+
y > this.contextWindow && this.dispatchEvent(new Event("contextoverflow"));
|
|
677
|
+
const g = [...this.#r, n];
|
|
681
678
|
let b;
|
|
682
679
|
try {
|
|
683
|
-
b = await this.#o.generateContent(
|
|
680
|
+
b = await this.#o.generateContent(g);
|
|
684
681
|
} catch (f) {
|
|
685
|
-
throw this.#m(f,
|
|
682
|
+
throw this.#m(f, r), f;
|
|
686
683
|
}
|
|
687
684
|
const { text: m, usage: w } = b;
|
|
688
685
|
let p = m;
|
|
689
|
-
if (
|
|
686
|
+
if (o) {
|
|
690
687
|
const f = p.match(/^\s*{\s*"Rating"\s*:\s*/);
|
|
691
688
|
f && (p = p.slice(f[0].length));
|
|
692
689
|
}
|
|
@@ -715,14 +712,14 @@ class h extends EventTarget {
|
|
|
715
712
|
n.enqueue("[object Object]"), n.close();
|
|
716
713
|
}
|
|
717
714
|
});
|
|
718
|
-
const
|
|
715
|
+
const o = this, r = e.signal;
|
|
719
716
|
return new ReadableStream({
|
|
720
717
|
async start(n) {
|
|
721
718
|
let i = !1;
|
|
722
719
|
const c = () => {
|
|
723
720
|
i = !0;
|
|
724
721
|
try {
|
|
725
|
-
const a =
|
|
722
|
+
const a = r?.reason || new (o.#t.DOMException || globalThis.DOMException)(
|
|
726
723
|
"Aborted",
|
|
727
724
|
"AbortError"
|
|
728
725
|
);
|
|
@@ -730,100 +727,100 @@ class h extends EventTarget {
|
|
|
730
727
|
} catch {
|
|
731
728
|
}
|
|
732
729
|
};
|
|
733
|
-
if (
|
|
730
|
+
if (r?.aborted) {
|
|
734
731
|
c();
|
|
735
732
|
return;
|
|
736
733
|
}
|
|
737
|
-
|
|
734
|
+
r && r.addEventListener("abort", c);
|
|
738
735
|
try {
|
|
739
736
|
if (e.responseConstraint) {
|
|
740
|
-
|
|
737
|
+
d.#x(
|
|
741
738
|
e.responseConstraint,
|
|
742
|
-
|
|
739
|
+
o.#t
|
|
743
740
|
);
|
|
744
741
|
const u = v(
|
|
745
742
|
e.responseConstraint
|
|
746
743
|
);
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
744
|
+
o.#e.generationConfig.responseMimeType = "application/json", o.#e.generationConfig.responseSchema = u, o.#f = o.#o.createSession(
|
|
745
|
+
o.#s,
|
|
746
|
+
o.#e
|
|
750
747
|
);
|
|
751
748
|
}
|
|
752
|
-
const a =
|
|
753
|
-
if (
|
|
754
|
-
throw new (
|
|
749
|
+
const a = o.#b(t), l = await o.#l(t);
|
|
750
|
+
if (o.#n)
|
|
751
|
+
throw new (o.#t.DOMException || globalThis.DOMException)(
|
|
755
752
|
"Session is destroyed",
|
|
756
753
|
"InvalidStateError"
|
|
757
754
|
);
|
|
758
|
-
const y = { role: "user", parts: l },
|
|
759
|
-
if (
|
|
760
|
-
const u =
|
|
755
|
+
const y = { role: "user", parts: l }, g = o.#u(l);
|
|
756
|
+
if (g === "QuotaExceededError") {
|
|
757
|
+
const u = o.#t && o.#t.QuotaExceededError || o.#t && o.#t.DOMException || globalThis.QuotaExceededError || globalThis.DOMException, x = new u(
|
|
761
758
|
"The prompt is too large, it exceeds the quota.",
|
|
762
759
|
"QuotaExceededError"
|
|
763
760
|
);
|
|
764
|
-
Object.defineProperty(
|
|
761
|
+
Object.defineProperty(x, "code", {
|
|
765
762
|
value: 22,
|
|
766
763
|
configurable: !0
|
|
767
764
|
});
|
|
768
765
|
const O = 1e7;
|
|
769
|
-
throw
|
|
770
|
-
} else if (
|
|
771
|
-
|
|
766
|
+
throw x.requested = O, x.quota = o.contextWindow, x;
|
|
767
|
+
} else if (g === "contextoverflow") {
|
|
768
|
+
o.dispatchEvent(new Event("contextoverflow")), n.enqueue("Mock response for quota overflow test."), n.close();
|
|
772
769
|
return;
|
|
773
770
|
}
|
|
774
|
-
const b = [...
|
|
775
|
-
|
|
771
|
+
const b = [...o.#r, y];
|
|
772
|
+
o.#e.systemInstruction && b.unshift({
|
|
776
773
|
role: "system",
|
|
777
|
-
parts: [{ text:
|
|
774
|
+
parts: [{ text: o.#e.systemInstruction }]
|
|
778
775
|
});
|
|
779
|
-
const m = await
|
|
776
|
+
const m = await o.#o.countTokens(
|
|
780
777
|
b
|
|
781
778
|
);
|
|
782
|
-
if (m >
|
|
783
|
-
const u =
|
|
784
|
-
`The prompt is too large (${m} tokens), it exceeds the quota of ${
|
|
779
|
+
if (m > o.contextWindow) {
|
|
780
|
+
const u = o.#t && o.#t.QuotaExceededError || o.#t && o.#t.DOMException || globalThis.QuotaExceededError || globalThis.DOMException, x = new u(
|
|
781
|
+
`The prompt is too large (${m} tokens), it exceeds the quota of ${o.contextWindow} tokens.`,
|
|
785
782
|
"QuotaExceededError"
|
|
786
783
|
);
|
|
787
|
-
throw Object.defineProperty(
|
|
784
|
+
throw Object.defineProperty(x, "code", {
|
|
788
785
|
value: 22,
|
|
789
786
|
configurable: !0
|
|
790
|
-
}),
|
|
787
|
+
}), x.requested = m, x.quota = o.contextWindow, x;
|
|
791
788
|
}
|
|
792
|
-
m >
|
|
793
|
-
const w = [...
|
|
789
|
+
m > o.contextWindow && o.dispatchEvent(new Event("contextoverflow"));
|
|
790
|
+
const w = [...o.#r, y];
|
|
794
791
|
let p;
|
|
795
792
|
try {
|
|
796
|
-
p = await
|
|
793
|
+
p = await o.#o.generateContentStream(w);
|
|
797
794
|
} catch (u) {
|
|
798
|
-
throw
|
|
795
|
+
throw o.#m(u, l), u;
|
|
799
796
|
}
|
|
800
|
-
let f = "",
|
|
797
|
+
let f = "", h = !1, E = "";
|
|
801
798
|
for await (const u of p) {
|
|
802
799
|
if (i) {
|
|
803
800
|
typeof p.return == "function" && await p.return();
|
|
804
801
|
return;
|
|
805
802
|
}
|
|
806
|
-
let
|
|
807
|
-
if (a && !
|
|
808
|
-
E +=
|
|
803
|
+
let x = u.text();
|
|
804
|
+
if (a && !h) {
|
|
805
|
+
E += x;
|
|
809
806
|
const O = E.match(/^\s*{\s*"Rating"\s*:\s*/);
|
|
810
807
|
if (O)
|
|
811
|
-
|
|
808
|
+
x = E.slice(O[0].length), h = !0, E = "";
|
|
812
809
|
else if (E.length > 50)
|
|
813
|
-
|
|
810
|
+
x = E, h = !0, E = "";
|
|
814
811
|
else
|
|
815
812
|
continue;
|
|
816
813
|
}
|
|
817
|
-
f +=
|
|
814
|
+
f += x, u.usageMetadata?.totalTokenCount && (o.#i = u.usageMetadata.totalTokenCount), n.enqueue(x);
|
|
818
815
|
}
|
|
819
|
-
i || (
|
|
816
|
+
i || (o.#r.push(y), o.#r.push({
|
|
820
817
|
role: "model",
|
|
821
818
|
parts: [{ text: f }]
|
|
822
819
|
}), n.close());
|
|
823
820
|
} catch (a) {
|
|
824
821
|
i || n.error(a);
|
|
825
822
|
} finally {
|
|
826
|
-
|
|
823
|
+
r && r.removeEventListener("abort", c);
|
|
827
824
|
}
|
|
828
825
|
}
|
|
829
826
|
});
|
|
@@ -839,14 +836,14 @@ class h extends EventTarget {
|
|
|
839
836
|
"Aborted",
|
|
840
837
|
"AbortError"
|
|
841
838
|
);
|
|
842
|
-
const
|
|
839
|
+
const o = await this.#l(t);
|
|
843
840
|
if (this.#n)
|
|
844
841
|
throw new (this.#t.DOMException || globalThis.DOMException)(
|
|
845
842
|
"Session is destroyed",
|
|
846
843
|
"InvalidStateError"
|
|
847
844
|
);
|
|
848
|
-
const
|
|
849
|
-
this.#r.push(
|
|
845
|
+
const r = { role: "user", parts: o };
|
|
846
|
+
this.#r.push(r);
|
|
850
847
|
try {
|
|
851
848
|
const n = [...this.#r];
|
|
852
849
|
this.#e.systemInstruction && n.unshift({
|
|
@@ -857,7 +854,7 @@ class h extends EventTarget {
|
|
|
857
854
|
this.#i = i || 0;
|
|
858
855
|
} catch {
|
|
859
856
|
}
|
|
860
|
-
this.#i > this.contextWindow && this.dispatchEvent(new Event("
|
|
857
|
+
this.#i > this.contextWindow && this.dispatchEvent(new Event("contextoverflow"));
|
|
861
858
|
}
|
|
862
859
|
async measureContextUsage(t) {
|
|
863
860
|
if (this.#a(), this.#n)
|
|
@@ -872,8 +869,8 @@ class h extends EventTarget {
|
|
|
872
869
|
"Session is destroyed",
|
|
873
870
|
"InvalidStateError"
|
|
874
871
|
);
|
|
875
|
-
const
|
|
876
|
-
return
|
|
872
|
+
const o = this.#u(e);
|
|
873
|
+
return o === "QuotaExceededError" ? 1e7 : o === "contextoverflow" ? 5e5 : await this.#o.countTokens([
|
|
877
874
|
{ role: "user", parts: e }
|
|
878
875
|
]) || 0;
|
|
879
876
|
} catch {
|
|
@@ -884,15 +881,15 @@ class h extends EventTarget {
|
|
|
884
881
|
}
|
|
885
882
|
// Volkswagen mode detection to avoid cloud costs for WPT tests.
|
|
886
883
|
#u(t) {
|
|
887
|
-
return
|
|
884
|
+
return d.#E(t);
|
|
888
885
|
}
|
|
889
886
|
static #E(t) {
|
|
890
887
|
if (t.length !== 1 || !t[0].text)
|
|
891
888
|
return null;
|
|
892
889
|
const e = t[0].text;
|
|
893
|
-
return typeof e != "string" || !e.startsWith("Please write a sentence in English.") ? null : e.length > 1e7 ? "QuotaExceededError" : e.length > 5e4 ? "
|
|
890
|
+
return typeof e != "string" || !e.startsWith("Please write a sentence in English.") ? null : e.length > 1e7 ? "QuotaExceededError" : e.length > 5e4 ? "contextoverflow" : null;
|
|
894
891
|
}
|
|
895
|
-
static #
|
|
892
|
+
static #x(t, e) {
|
|
896
893
|
if (t)
|
|
897
894
|
try {
|
|
898
895
|
JSON.stringify(t);
|
|
@@ -913,7 +910,7 @@ class h extends EventTarget {
|
|
|
913
910
|
}
|
|
914
911
|
// Private Helper to process diverse input types
|
|
915
912
|
async #l(t) {
|
|
916
|
-
const e = this.#s.expectedInputs ? ["text", ...this.#s.expectedInputs.map((
|
|
913
|
+
const e = this.#s.expectedInputs ? ["text", ...this.#s.expectedInputs.map((r) => r.type)] : ["text"];
|
|
917
914
|
if (typeof t == "string") {
|
|
918
915
|
if (!e.includes("text"))
|
|
919
916
|
throw new (this.#t.DOMException || globalThis.DOMException)(
|
|
@@ -926,7 +923,7 @@ class h extends EventTarget {
|
|
|
926
923
|
if (t.length === 0)
|
|
927
924
|
return [{ text: " " }];
|
|
928
925
|
if (t.length > 0 && t[0].role) {
|
|
929
|
-
let
|
|
926
|
+
let r = [];
|
|
930
927
|
for (const n of t) {
|
|
931
928
|
const i = n.role === "assistant" || n.role === "model";
|
|
932
929
|
if (typeof n.content == "string") {
|
|
@@ -935,7 +932,7 @@ class h extends EventTarget {
|
|
|
935
932
|
'The content type "text" is not in the expectedInputs.',
|
|
936
933
|
"NotSupportedError"
|
|
937
934
|
);
|
|
938
|
-
|
|
935
|
+
r.push({ text: n.content }), n.prefix && console.warn(
|
|
939
936
|
"The `prefix` flag isn't supported and was ignored."
|
|
940
937
|
);
|
|
941
938
|
} else if (Array.isArray(n.content))
|
|
@@ -952,7 +949,7 @@ class h extends EventTarget {
|
|
|
952
949
|
'The content type "text" must have a string value.',
|
|
953
950
|
"SyntaxError"
|
|
954
951
|
);
|
|
955
|
-
|
|
952
|
+
r.push({ text: c.value });
|
|
956
953
|
} else {
|
|
957
954
|
if (i)
|
|
958
955
|
throw new (this.#t.DOMException || globalThis.DOMException)(
|
|
@@ -960,20 +957,47 @@ class h extends EventTarget {
|
|
|
960
957
|
"NotSupportedError"
|
|
961
958
|
);
|
|
962
959
|
const l = await M.convert(c.type, c.value);
|
|
963
|
-
|
|
960
|
+
r.push(l);
|
|
964
961
|
}
|
|
965
962
|
}
|
|
966
963
|
}
|
|
967
|
-
return
|
|
964
|
+
return r;
|
|
968
965
|
}
|
|
969
|
-
return
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
966
|
+
return Promise.all(
|
|
967
|
+
t.map(async (r) => {
|
|
968
|
+
if (typeof r == "string") {
|
|
969
|
+
if (!e.includes("text"))
|
|
970
|
+
throw new (this.#t.DOMException || globalThis.DOMException)(
|
|
971
|
+
'The content type "text" is not in the expectedInputs.',
|
|
972
|
+
"NotSupportedError"
|
|
973
|
+
);
|
|
974
|
+
return { text: r === "" ? " " : r };
|
|
975
|
+
}
|
|
976
|
+
if (typeof r == "object" && r !== null && r.type && r.value) {
|
|
977
|
+
const n = r.type || "text";
|
|
978
|
+
if (!e.includes(n))
|
|
979
|
+
throw new (this.#t.DOMException || globalThis.DOMException)(
|
|
980
|
+
`The content type "${n}" is not in the expectedInputs.`,
|
|
981
|
+
"NotSupportedError"
|
|
982
|
+
);
|
|
983
|
+
if (n === "text") {
|
|
984
|
+
if (typeof r.value != "string")
|
|
985
|
+
throw new (this.#t.DOMException || globalThis.DOMException)(
|
|
986
|
+
'The content type "text" must have a string value.',
|
|
987
|
+
"SyntaxError"
|
|
988
|
+
);
|
|
989
|
+
return { text: r.value };
|
|
990
|
+
}
|
|
991
|
+
return await M.convert(r.type, r.value);
|
|
992
|
+
}
|
|
993
|
+
if (!e.includes("text"))
|
|
994
|
+
throw new (this.#t.DOMException || globalThis.DOMException)(
|
|
995
|
+
'The content type "text" is not in the expectedInputs.',
|
|
996
|
+
"NotSupportedError"
|
|
997
|
+
);
|
|
998
|
+
return { text: String(r) };
|
|
999
|
+
})
|
|
1000
|
+
);
|
|
977
1001
|
}
|
|
978
1002
|
if (!e.includes("text"))
|
|
979
1003
|
throw new (this.#t.DOMException || globalThis.DOMException)(
|
|
@@ -984,14 +1008,14 @@ class h extends EventTarget {
|
|
|
984
1008
|
}
|
|
985
1009
|
// Map backend errors to WPT expectations
|
|
986
1010
|
#m(t, e) {
|
|
987
|
-
const
|
|
988
|
-
if (
|
|
989
|
-
const
|
|
1011
|
+
const o = String(t.message || t);
|
|
1012
|
+
if (o.includes("400") || o.toLowerCase().includes("unable to process") || o.toLowerCase().includes("invalid")) {
|
|
1013
|
+
const r = e.some(
|
|
990
1014
|
(c) => c.inlineData?.mimeType.startsWith("audio/")
|
|
991
1015
|
), n = e.some(
|
|
992
1016
|
(c) => c.inlineData?.mimeType.startsWith("image/")
|
|
993
1017
|
), i = this.#t.DOMException || globalThis.DOMException;
|
|
994
|
-
if (
|
|
1018
|
+
if (r)
|
|
995
1019
|
throw new i("Invalid audio data", "DataError");
|
|
996
1020
|
if (n)
|
|
997
1021
|
throw new i("Invalid image data", "InvalidStateError");
|
|
@@ -1003,7 +1027,7 @@ const D = (s) => {
|
|
|
1003
1027
|
try {
|
|
1004
1028
|
if (!s || s.LanguageModel?.__isPolyfill)
|
|
1005
1029
|
return;
|
|
1006
|
-
const t = class extends
|
|
1030
|
+
const t = class extends d {
|
|
1007
1031
|
};
|
|
1008
1032
|
t.__window = s, t.__isPolyfill = !0, s.LanguageModel = t, s.DOMException && (s.QuotaExceededError = s.DOMException);
|
|
1009
1033
|
} catch {
|
|
@@ -1037,9 +1061,9 @@ globalThis.document?.documentElement && (P.observe(globalThis.document.documentE
|
|
|
1037
1061
|
}), globalThis.document.querySelectorAll("iframe").forEach((s) => {
|
|
1038
1062
|
D(s.contentWindow);
|
|
1039
1063
|
}));
|
|
1040
|
-
(!("LanguageModel" in globalThis) || globalThis.__FORCE_PROMPT_API_POLYFILL__) && (globalThis.LanguageModel =
|
|
1064
|
+
(!("LanguageModel" in globalThis) || globalThis.__FORCE_PROMPT_API_POLYFILL__) && (globalThis.LanguageModel = d, d.__isPolyfill = !0, console.log(
|
|
1041
1065
|
"Polyfill: window.LanguageModel is now backed by the Prompt API polyfill."
|
|
1042
1066
|
));
|
|
1043
1067
|
export {
|
|
1044
|
-
|
|
1068
|
+
d as LanguageModel
|
|
1045
1069
|
};
|
package/dot_env.json
CHANGED
|
@@ -7,5 +7,15 @@
|
|
|
7
7
|
"reCaptchaSiteKey": "",
|
|
8
8
|
"useLimitedUseAppCheckTokens": true,
|
|
9
9
|
"device": "webgpu",
|
|
10
|
-
"dtype": "q4f16"
|
|
10
|
+
"dtype": "q4f16",
|
|
11
|
+
"env": {
|
|
12
|
+
"allowRemoteModels": true,
|
|
13
|
+
"backends": {
|
|
14
|
+
"onnx": {
|
|
15
|
+
"wasm": {
|
|
16
|
+
"wasmPaths": ""
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
11
21
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "prompt-api-polyfill",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.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",
|
|
@@ -47,12 +47,14 @@
|
|
|
47
47
|
"devDependencies": {
|
|
48
48
|
"prettier": "^3.8.1",
|
|
49
49
|
"prettier-plugin-curly": "^0.4.1",
|
|
50
|
-
"vite": "^7.3.1"
|
|
50
|
+
"vite": "^7.3.1",
|
|
51
|
+
"node-gyp": "^12.2.0",
|
|
52
|
+
"node-addon-api": "^8.5.0"
|
|
51
53
|
},
|
|
52
54
|
"dependencies": {
|
|
53
|
-
"@google/genai": "^1.
|
|
55
|
+
"@google/genai": "^1.43.0",
|
|
54
56
|
"@huggingface/transformers": "^3.8.1",
|
|
55
57
|
"firebase": "^12.9.0",
|
|
56
|
-
"openai": "^6.
|
|
58
|
+
"openai": "^6.25.0"
|
|
57
59
|
}
|
|
58
60
|
}
|