prompt-api-polyfill 1.4.0 → 1.6.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/prompt-api-polyfill.js +302 -274
- package/package.json +3 -3
|
@@ -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), n = e.buffer.slice(
|
|
31
31
|
e.byteOffset,
|
|
32
32
|
e.byteOffset + e.byteLength
|
|
33
|
-
),
|
|
34
|
-
if (!
|
|
33
|
+
), r = this.arrayBufferToBase64(n), o = this.#o(e);
|
|
34
|
+
if (!o)
|
|
35
35
|
throw new DOMException("Invalid image data", "InvalidStateError");
|
|
36
|
-
return { inlineData: { data:
|
|
36
|
+
return { inlineData: { data: r, mimeType: o } };
|
|
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 n = String.fromCharCode(...t.slice(0, 100)).toLowerCase();
|
|
71
|
+
return n.includes("<svg") || n.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", n = ArrayBuffer.isView(t) || t && t.buffer && (t.buffer instanceof ArrayBuffer || t.buffer.constructor.name === "ArrayBuffer");
|
|
84
|
+
if (e || n) {
|
|
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,89 +95,89 @@ 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, n) => {
|
|
99
|
+
const r = new FileReader();
|
|
100
|
+
r.onloadend = () => {
|
|
101
|
+
r.error ? n(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) {
|
|
111
111
|
typeof HTMLImageElement < "u" && t instanceof HTMLImageElement && !t.complete && await t.decode().catch(() => {
|
|
112
112
|
});
|
|
113
|
-
const e = (
|
|
114
|
-
const l = t[
|
|
113
|
+
const e = (c) => {
|
|
114
|
+
const l = t[c];
|
|
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 n = t.displayWidth || t.naturalWidth || t.videoWidth || e("width"), r = t.displayHeight || t.naturalHeight || t.videoHeight || e("height");
|
|
118
|
+
if ((!n || !r) && typeof t.getBBox == "function")
|
|
119
119
|
try {
|
|
120
|
-
const
|
|
121
|
-
|
|
120
|
+
const c = t.getBBox();
|
|
121
|
+
n = n || c.width, r = r || c.height;
|
|
122
122
|
} catch {
|
|
123
123
|
}
|
|
124
|
-
if ((!
|
|
124
|
+
if ((!n || !r) && typeof t.getBoundingClientRect == "function")
|
|
125
125
|
try {
|
|
126
|
-
const
|
|
127
|
-
|
|
126
|
+
const c = t.getBoundingClientRect();
|
|
127
|
+
n = n || c.width, r = r || c.height;
|
|
128
128
|
} catch {
|
|
129
129
|
}
|
|
130
|
-
if (!
|
|
130
|
+
if (!n || !r)
|
|
131
131
|
throw new DOMException("Invalid image dimensions", "InvalidStateError");
|
|
132
|
-
const
|
|
133
|
-
|
|
134
|
-
const i =
|
|
132
|
+
const o = document.createElement("canvas");
|
|
133
|
+
o.width = n, o.height = r;
|
|
134
|
+
const i = o.getContext("2d");
|
|
135
135
|
return typeof ImageData < "u" && t instanceof ImageData ? i.putImageData(t, 0, 0) : i.drawImage(t, 0, 0), {
|
|
136
136
|
inlineData: {
|
|
137
|
-
data:
|
|
137
|
+
data: o.toDataURL("image/png").split(",")[1],
|
|
138
138
|
mimeType: "image/png"
|
|
139
139
|
}
|
|
140
140
|
};
|
|
141
141
|
}
|
|
142
142
|
static arrayBufferToBase64(t) {
|
|
143
143
|
let e = "";
|
|
144
|
-
const
|
|
145
|
-
for (let
|
|
146
|
-
e += String.fromCharCode(
|
|
144
|
+
const n = new Uint8Array(t), r = n.byteLength;
|
|
145
|
+
for (let o = 0; o < r; o++)
|
|
146
|
+
e += String.fromCharCode(n[o]);
|
|
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, n = t.sampleRate, r = 1, o = 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, n, e, o);
|
|
157
157
|
}
|
|
158
158
|
static interleave(t, e) {
|
|
159
|
-
const
|
|
160
|
-
let
|
|
161
|
-
for (;
|
|
162
|
-
o
|
|
163
|
-
return
|
|
159
|
+
const n = t.length + e.length, r = new Float32Array(n);
|
|
160
|
+
let o = 0, i = 0;
|
|
161
|
+
for (; o < n; )
|
|
162
|
+
r[o++] = t[i], r[o++] = e[i], i++;
|
|
163
|
+
return r;
|
|
164
164
|
}
|
|
165
|
-
static encodeWAV(t, e, r, o
|
|
166
|
-
const i =
|
|
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, n, r, o) {
|
|
166
|
+
const i = o / 8, a = r * i, c = new ArrayBuffer(44 + t.length * i), l = new DataView(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, r, !0), l.setUint32(24, n, !0), l.setUint32(28, n * a, !0), l.setUint16(32, a, !0), l.setUint16(34, o, !0), this.writeString(l, 36, "data"), l.setUint32(40, t.length * i, !0), this.floatTo16BitPCM(l, 44, t), c;
|
|
168
168
|
}
|
|
169
|
-
static floatTo16BitPCM(t, e,
|
|
170
|
-
for (let
|
|
171
|
-
const
|
|
172
|
-
t.setInt16(e,
|
|
169
|
+
static floatTo16BitPCM(t, e, n) {
|
|
170
|
+
for (let r = 0; r < n.length; r++, e += 2) {
|
|
171
|
+
const o = Math.max(-1, Math.min(1, n[r]));
|
|
172
|
+
t.setInt16(e, o < 0 ? o * 32768 : o * 32767, !0);
|
|
173
173
|
}
|
|
174
174
|
}
|
|
175
|
-
static writeString(t, e,
|
|
176
|
-
for (let
|
|
177
|
-
t.setUint8(e +
|
|
175
|
+
static writeString(t, e, n) {
|
|
176
|
+
for (let r = 0; r < n.length; r++)
|
|
177
|
+
t.setUint8(e + r, n.charCodeAt(r));
|
|
178
178
|
}
|
|
179
179
|
}
|
|
180
|
-
function
|
|
180
|
+
function O(s) {
|
|
181
181
|
if (!s)
|
|
182
182
|
return;
|
|
183
183
|
const t = {
|
|
@@ -201,22 +201,22 @@ function v(s) {
|
|
|
201
201
|
return T.array({
|
|
202
202
|
...t,
|
|
203
203
|
// Recursively convert the 'items' schema
|
|
204
|
-
items:
|
|
204
|
+
items: O(s.items)
|
|
205
205
|
});
|
|
206
206
|
case "object":
|
|
207
|
-
const e = {},
|
|
208
|
-
|
|
209
|
-
e[i] =
|
|
207
|
+
const e = {}, n = s.properties ? Object.keys(s.properties) : [];
|
|
208
|
+
n.forEach((i) => {
|
|
209
|
+
e[i] = O(
|
|
210
210
|
s.properties[i]
|
|
211
211
|
);
|
|
212
212
|
});
|
|
213
|
-
const
|
|
214
|
-
(i) => !
|
|
213
|
+
const r = s.required || [], o = n.filter(
|
|
214
|
+
(i) => !r.includes(i)
|
|
215
215
|
);
|
|
216
216
|
return T.object({
|
|
217
217
|
...t,
|
|
218
218
|
properties: e,
|
|
219
|
-
optionalProperties:
|
|
219
|
+
optionalProperties: o
|
|
220
220
|
});
|
|
221
221
|
default:
|
|
222
222
|
return console.warn(
|
|
@@ -255,46 +255,46 @@ 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 n of s) {
|
|
259
|
+
const r = n.role === "assistant" ? "model" : "user", o = r === "model";
|
|
260
260
|
let i = [];
|
|
261
|
-
if (Array.isArray(
|
|
262
|
-
for (const
|
|
263
|
-
if (
|
|
264
|
-
const
|
|
265
|
-
if (typeof
|
|
261
|
+
if (Array.isArray(n.content))
|
|
262
|
+
for (const a of n.content)
|
|
263
|
+
if (a.type === "text") {
|
|
264
|
+
const c = a.value || a.text || "";
|
|
265
|
+
if (typeof c != "string")
|
|
266
266
|
throw new (t.DOMException || globalThis.DOMException)(
|
|
267
267
|
'The content type "text" must have a string value.',
|
|
268
268
|
"SyntaxError"
|
|
269
269
|
);
|
|
270
|
-
i.push({ text:
|
|
270
|
+
i.push({ text: c });
|
|
271
271
|
} else {
|
|
272
|
-
if (
|
|
272
|
+
if (o)
|
|
273
273
|
throw new (t.DOMException || globalThis.DOMException)(
|
|
274
274
|
"Assistant messages only support text content.",
|
|
275
275
|
"NotSupportedError"
|
|
276
276
|
);
|
|
277
|
-
const
|
|
278
|
-
i.push(
|
|
277
|
+
const c = await M.convert(a.type, a.value);
|
|
278
|
+
i.push(c);
|
|
279
279
|
}
|
|
280
280
|
else
|
|
281
|
-
i.push({ text:
|
|
282
|
-
e.push({ role:
|
|
281
|
+
i.push({ text: n.content });
|
|
282
|
+
e.push({ role: r, parts: i });
|
|
283
283
|
}
|
|
284
284
|
return e;
|
|
285
285
|
}
|
|
286
286
|
class d extends EventTarget {
|
|
287
287
|
#o;
|
|
288
288
|
#f;
|
|
289
|
-
#r;
|
|
290
|
-
#s;
|
|
291
289
|
#e;
|
|
290
|
+
#s;
|
|
291
|
+
#r;
|
|
292
292
|
#n;
|
|
293
293
|
#i;
|
|
294
294
|
#c;
|
|
295
295
|
#t;
|
|
296
|
-
constructor(t, e,
|
|
297
|
-
super(), this.#o = t, this.#f = e, this.#
|
|
296
|
+
constructor(t, e, n, r = {}, o, i = 0, a = globalThis) {
|
|
297
|
+
super(), this.#o = t, this.#f = e, this.#e = n || [], this.#s = r, this.#r = o, this.#n = !1, this.#i = i, this.#c = {}, this.#t = a;
|
|
298
298
|
}
|
|
299
299
|
get contextUsage() {
|
|
300
300
|
return this.#i;
|
|
@@ -306,10 +306,7 @@ class d extends EventTarget {
|
|
|
306
306
|
return this.#c;
|
|
307
307
|
}
|
|
308
308
|
set oncontextoverflow(t) {
|
|
309
|
-
this.#c && this.removeEventListener(
|
|
310
|
-
"contextoverflow",
|
|
311
|
-
this.#c
|
|
312
|
-
), this.#c = t, typeof t == "function" && this.addEventListener("contextoverflow", t);
|
|
309
|
+
this.#c && this.removeEventListener("contextoverflow", this.#c), this.#c = t, typeof t == "function" && this.addEventListener("contextoverflow", t);
|
|
313
310
|
}
|
|
314
311
|
static #h(t) {
|
|
315
312
|
try {
|
|
@@ -318,8 +315,8 @@ class d extends EventTarget {
|
|
|
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 n = t?.DOMException || globalThis.DOMException;
|
|
319
|
+
throw new n(
|
|
323
320
|
"The execution context is not valid.",
|
|
324
321
|
"InvalidStateError"
|
|
325
322
|
);
|
|
@@ -333,31 +330,31 @@ class d extends EventTarget {
|
|
|
333
330
|
d.#h(e);
|
|
334
331
|
try {
|
|
335
332
|
await d.#w(t, e);
|
|
336
|
-
} catch (
|
|
337
|
-
if (
|
|
338
|
-
if (
|
|
339
|
-
throw
|
|
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
348
|
return (await d.#p(e)).availability(t);
|
|
352
349
|
}
|
|
353
|
-
static #
|
|
350
|
+
static #g = A;
|
|
354
351
|
static #d(t = globalThis) {
|
|
355
|
-
for (const
|
|
356
|
-
const
|
|
357
|
-
if (
|
|
358
|
-
return { ...
|
|
352
|
+
for (const n of d.#g) {
|
|
353
|
+
const r = t[n.config] || globalThis[n.config];
|
|
354
|
+
if (r && r.apiKey)
|
|
355
|
+
return { ...n, configValue: r };
|
|
359
356
|
}
|
|
360
|
-
const e = d.#
|
|
357
|
+
const e = d.#g.map((n) => `window.${n.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"
|
|
@@ -369,43 +366,43 @@ class d extends EventTarget {
|
|
|
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 n = t.expectedInputs ? ["text", ...t.expectedInputs.map((r) => r.type)] : ["text"];
|
|
384
381
|
if (t.initialPrompts && Array.isArray(t.initialPrompts)) {
|
|
385
|
-
let
|
|
386
|
-
for (let
|
|
387
|
-
const i = t.initialPrompts[
|
|
382
|
+
let r = !1;
|
|
383
|
+
for (let o = 0; o < t.initialPrompts.length; o++) {
|
|
384
|
+
const i = t.initialPrompts[o];
|
|
388
385
|
if (i.role === "system") {
|
|
389
|
-
if (
|
|
386
|
+
if (o !== 0)
|
|
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
|
-
for (const
|
|
401
|
-
const
|
|
402
|
-
if (!
|
|
397
|
+
for (const a of i.content) {
|
|
398
|
+
const c = a.type || "text";
|
|
399
|
+
if (!n.includes(c))
|
|
403
400
|
throw new (e.DOMException || globalThis.DOMException)(
|
|
404
|
-
`The content type "${
|
|
401
|
+
`The content type "${c}" is not in the expectedInputs.`,
|
|
405
402
|
"NotSupportedError"
|
|
406
403
|
);
|
|
407
404
|
}
|
|
408
|
-
else if (!
|
|
405
|
+
else if (!n.includes("text"))
|
|
409
406
|
throw new (e.DOMException || globalThis.DOMException)(
|
|
410
407
|
'The content type "text" is not in the expectedInputs.',
|
|
411
408
|
"NotSupportedError"
|
|
@@ -439,13 +436,13 @@ class d extends EventTarget {
|
|
|
439
436
|
"Aborted",
|
|
440
437
|
"AbortError"
|
|
441
438
|
);
|
|
442
|
-
const
|
|
443
|
-
if (
|
|
439
|
+
const n = await this.availability(t);
|
|
440
|
+
if (n === "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 (n === "downloadable" || n === "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,26 +452,26 @@ class d extends EventTarget {
|
|
|
455
452
|
"Aborted",
|
|
456
453
|
"AbortError"
|
|
457
454
|
);
|
|
458
|
-
const
|
|
459
|
-
d.#
|
|
460
|
-
|
|
455
|
+
const r = d.#d(e), o = await d.#p(e), i = new o(r.configValue), a = { ...t };
|
|
456
|
+
d.#x(
|
|
457
|
+
a.responseConstraint,
|
|
461
458
|
e
|
|
462
459
|
);
|
|
463
|
-
const
|
|
460
|
+
const c = {
|
|
464
461
|
model: i.modelName,
|
|
465
462
|
generationConfig: {}
|
|
466
463
|
};
|
|
467
464
|
let l = [], y = 0;
|
|
468
|
-
if (
|
|
469
|
-
const w =
|
|
465
|
+
if (a.initialPrompts && Array.isArray(a.initialPrompts)) {
|
|
466
|
+
const w = a.initialPrompts.filter(
|
|
470
467
|
(f) => f.role === "system"
|
|
471
|
-
), p =
|
|
468
|
+
), p = a.initialPrompts.filter(
|
|
472
469
|
(f) => f.role !== "system"
|
|
473
470
|
);
|
|
474
|
-
w.length > 0 && (
|
|
471
|
+
w.length > 0 && (c.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
|
-
for (const f of
|
|
474
|
+
for (const f of a.initialPrompts) {
|
|
478
475
|
if (typeof f.content != "string")
|
|
479
476
|
continue;
|
|
480
477
|
const h = d.#E([
|
|
@@ -489,35 +486,35 @@ class d 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
|
|
498
|
-
if (typeof
|
|
499
|
-
|
|
494
|
+
let g = null;
|
|
495
|
+
if (typeof a.monitor == "function") {
|
|
496
|
+
g = new EventTarget();
|
|
500
497
|
try {
|
|
501
|
-
|
|
498
|
+
a.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
|
-
),
|
|
517
|
+
), g.__lastProgressLoaded = f;
|
|
521
518
|
} catch (h) {
|
|
522
519
|
console.error("Error dispatching downloadprogress events:", h);
|
|
523
520
|
}
|
|
@@ -529,20 +526,20 @@ class d extends EventTarget {
|
|
|
529
526
|
"AbortError"
|
|
530
527
|
);
|
|
531
528
|
const m = await i.createSession(
|
|
532
|
-
c,
|
|
533
529
|
a,
|
|
534
|
-
|
|
530
|
+
c,
|
|
531
|
+
g
|
|
535
532
|
);
|
|
536
533
|
if (!await b(1))
|
|
537
534
|
throw t.signal.reason || new (e.DOMException || globalThis.DOMException)(
|
|
538
535
|
"Aborted",
|
|
539
536
|
"AbortError"
|
|
540
537
|
);
|
|
541
|
-
if (
|
|
538
|
+
if (a.initialPrompts?.length > 0) {
|
|
542
539
|
const w = [...l];
|
|
543
|
-
if (
|
|
540
|
+
if (c.systemInstruction && w.unshift({
|
|
544
541
|
role: "system",
|
|
545
|
-
parts: [{ text:
|
|
542
|
+
parts: [{ text: c.systemInstruction }]
|
|
546
543
|
}), y = await i.countTokens(w) || 0, y > 1e6) {
|
|
547
544
|
const p = e.QuotaExceededError || e.DOMException || globalThis.QuotaExceededError || globalThis.DOMException, f = new p(
|
|
548
545
|
"The initial prompts are too large, they exceed the quota.",
|
|
@@ -555,8 +552,8 @@ class d extends EventTarget {
|
|
|
555
552
|
i,
|
|
556
553
|
m,
|
|
557
554
|
l,
|
|
558
|
-
c,
|
|
559
555
|
a,
|
|
556
|
+
c,
|
|
560
557
|
y,
|
|
561
558
|
e
|
|
562
559
|
);
|
|
@@ -573,9 +570,9 @@ class d extends EventTarget {
|
|
|
573
570
|
"Aborted",
|
|
574
571
|
"AbortError"
|
|
575
572
|
);
|
|
576
|
-
const e = JSON.parse(JSON.stringify(this.#
|
|
577
|
-
|
|
578
|
-
this.#
|
|
573
|
+
const e = JSON.parse(JSON.stringify(this.#e)), n = { ...this.#s, ...t }, r = await d.#p(this.#t), o = d.#d(this.#t), i = new r(o.configValue), a = await i.createSession(
|
|
574
|
+
n,
|
|
575
|
+
this.#r
|
|
579
576
|
);
|
|
580
577
|
if (t.signal?.aborted)
|
|
581
578
|
throw t.signal.reason || new (this.#t.DOMException || globalThis.DOMException)(
|
|
@@ -584,16 +581,16 @@ class d extends EventTarget {
|
|
|
584
581
|
);
|
|
585
582
|
return new this.constructor(
|
|
586
583
|
i,
|
|
587
|
-
|
|
584
|
+
a,
|
|
588
585
|
e,
|
|
589
|
-
|
|
590
|
-
this.#
|
|
586
|
+
n,
|
|
587
|
+
this.#r,
|
|
591
588
|
this.#i,
|
|
592
589
|
this.#t
|
|
593
590
|
);
|
|
594
591
|
}
|
|
595
592
|
destroy() {
|
|
596
|
-
this.#a(), this.#n = !0, this.#
|
|
593
|
+
this.#a(), this.#n = !0, this.#e = null;
|
|
597
594
|
}
|
|
598
595
|
async prompt(t, e = {}) {
|
|
599
596
|
if (this.#a(), this.#n)
|
|
@@ -609,25 +606,25 @@ class d 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
|
-
d.#
|
|
609
|
+
d.#x(
|
|
613
610
|
e.responseConstraint,
|
|
614
611
|
this.#t
|
|
615
612
|
);
|
|
616
|
-
const
|
|
613
|
+
const c = O(
|
|
617
614
|
e.responseConstraint
|
|
618
615
|
);
|
|
619
|
-
this.#
|
|
616
|
+
this.#r.generationConfig.responseMimeType = "application/json", this.#r.generationConfig.responseSchema = c, this.#f = this.#o.createSession(
|
|
620
617
|
this.#s,
|
|
621
|
-
this.#
|
|
618
|
+
this.#r
|
|
622
619
|
);
|
|
623
620
|
}
|
|
624
|
-
const
|
|
621
|
+
const n = 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
|
|
627
|
+
const o = { role: "user", parts: r }, i = new Promise((c, l) => {
|
|
631
628
|
if (e.signal?.aborted) {
|
|
632
629
|
l(
|
|
633
630
|
e.signal.reason || new (this.#t.DOMException || globalThis.DOMException)(
|
|
@@ -649,9 +646,9 @@ class d extends EventTarget {
|
|
|
649
646
|
},
|
|
650
647
|
{ once: !0 }
|
|
651
648
|
);
|
|
652
|
-
}),
|
|
653
|
-
const
|
|
654
|
-
if (
|
|
649
|
+
}), a = (async () => {
|
|
650
|
+
const c = this.#u(r);
|
|
651
|
+
if (c === "QuotaExceededError") {
|
|
655
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"
|
|
@@ -659,12 +656,12 @@ class d extends EventTarget {
|
|
|
659
656
|
Object.defineProperty(h, "code", { value: 22, configurable: !0 });
|
|
660
657
|
const E = 1e7;
|
|
661
658
|
throw h.requested = E, h.quota = this.contextWindow, h;
|
|
662
|
-
} else if (
|
|
659
|
+
} else if (c === "contextoverflow")
|
|
663
660
|
return this.dispatchEvent(new Event("contextoverflow")), "Mock response for quota overflow test.";
|
|
664
|
-
const l = [...this.#
|
|
665
|
-
this.#
|
|
661
|
+
const l = [...this.#e, o];
|
|
662
|
+
this.#r.systemInstruction && l.unshift({
|
|
666
663
|
role: "system",
|
|
667
|
-
parts: [{ text: this.#
|
|
664
|
+
parts: [{ text: this.#r.systemInstruction }]
|
|
668
665
|
});
|
|
669
666
|
const y = await this.#o.countTokens(
|
|
670
667
|
l
|
|
@@ -677,25 +674,25 @@ class d extends EventTarget {
|
|
|
677
674
|
throw Object.defineProperty(h, "code", { value: 22, configurable: !0 }), h.requested = y, h.quota = this.contextWindow, h;
|
|
678
675
|
}
|
|
679
676
|
y > this.contextWindow && this.dispatchEvent(new Event("contextoverflow"));
|
|
680
|
-
const
|
|
677
|
+
const g = [...this.#e, o];
|
|
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 (n) {
|
|
690
687
|
const f = p.match(/^\s*{\s*"Rating"\s*:\s*/);
|
|
691
688
|
f && (p = p.slice(f[0].length));
|
|
692
689
|
}
|
|
693
|
-
return w && (this.#i = w), this.#
|
|
690
|
+
return w && (this.#i = w), !this.#n && this.#e && (this.#e.push(o), this.#e.push({ role: "model", parts: [{ text: p }] })), p;
|
|
694
691
|
})();
|
|
695
692
|
try {
|
|
696
|
-
return await Promise.race([
|
|
697
|
-
} catch (
|
|
698
|
-
throw
|
|
693
|
+
return await Promise.race([a, i]);
|
|
694
|
+
} catch (c) {
|
|
695
|
+
throw c.name === "AbortError" || console.error("Prompt API Polyfill Error:", c), c;
|
|
699
696
|
}
|
|
700
697
|
}
|
|
701
698
|
promptStreaming(t, e = {}) {
|
|
@@ -711,91 +708,91 @@ class d extends EventTarget {
|
|
|
711
708
|
);
|
|
712
709
|
if (typeof t == "object" && t !== null && !Array.isArray(t) && Object.keys(t).length === 0)
|
|
713
710
|
return new ReadableStream({
|
|
714
|
-
start(
|
|
715
|
-
|
|
711
|
+
start(o) {
|
|
712
|
+
o.enqueue("[object Object]"), o.close();
|
|
716
713
|
}
|
|
717
714
|
});
|
|
718
|
-
const
|
|
715
|
+
const n = this, r = e.signal;
|
|
719
716
|
return new ReadableStream({
|
|
720
|
-
async start(
|
|
717
|
+
async start(o) {
|
|
721
718
|
let i = !1;
|
|
722
|
-
const
|
|
719
|
+
const a = () => {
|
|
723
720
|
i = !0;
|
|
724
721
|
try {
|
|
725
|
-
const
|
|
722
|
+
const c = r?.reason || new (n.#t.DOMException || globalThis.DOMException)(
|
|
726
723
|
"Aborted",
|
|
727
724
|
"AbortError"
|
|
728
725
|
);
|
|
729
|
-
|
|
726
|
+
o.error(c);
|
|
730
727
|
} catch {
|
|
731
728
|
}
|
|
732
729
|
};
|
|
733
|
-
if (
|
|
734
|
-
|
|
730
|
+
if (r?.aborted) {
|
|
731
|
+
a();
|
|
735
732
|
return;
|
|
736
733
|
}
|
|
737
|
-
|
|
734
|
+
r && r.addEventListener("abort", a);
|
|
738
735
|
try {
|
|
739
736
|
if (e.responseConstraint) {
|
|
740
|
-
d.#
|
|
737
|
+
d.#x(
|
|
741
738
|
e.responseConstraint,
|
|
742
|
-
|
|
739
|
+
n.#t
|
|
743
740
|
);
|
|
744
|
-
const u =
|
|
741
|
+
const u = O(
|
|
745
742
|
e.responseConstraint
|
|
746
743
|
);
|
|
747
|
-
r
|
|
748
|
-
|
|
749
|
-
r
|
|
744
|
+
n.#r.generationConfig.responseMimeType = "application/json", n.#r.generationConfig.responseSchema = u, n.#f = n.#o.createSession(
|
|
745
|
+
n.#s,
|
|
746
|
+
n.#r
|
|
750
747
|
);
|
|
751
748
|
}
|
|
752
|
-
const
|
|
753
|
-
if (
|
|
754
|
-
throw new (
|
|
749
|
+
const c = n.#b(t), l = await n.#l(t);
|
|
750
|
+
if (n.#n)
|
|
751
|
+
throw new (n.#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 = n.#u(l);
|
|
756
|
+
if (g === "QuotaExceededError") {
|
|
757
|
+
const u = n.#t && n.#t.QuotaExceededError || n.#t && n.#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
|
-
const
|
|
769
|
-
throw
|
|
770
|
-
} else if (
|
|
771
|
-
|
|
765
|
+
const v = 1e7;
|
|
766
|
+
throw x.requested = v, x.quota = n.contextWindow, x;
|
|
767
|
+
} else if (g === "contextoverflow") {
|
|
768
|
+
n.dispatchEvent(new Event("contextoverflow")), o.enqueue("Mock response for quota overflow test."), o.close();
|
|
772
769
|
return;
|
|
773
770
|
}
|
|
774
|
-
const b = [...
|
|
775
|
-
r
|
|
771
|
+
const b = [...n.#e, y];
|
|
772
|
+
n.#r.systemInstruction && b.unshift({
|
|
776
773
|
role: "system",
|
|
777
|
-
parts: [{ text: r
|
|
774
|
+
parts: [{ text: n.#r.systemInstruction }]
|
|
778
775
|
});
|
|
779
|
-
const m = await
|
|
776
|
+
const m = await n.#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 > n.contextWindow) {
|
|
780
|
+
const u = n.#t && n.#t.QuotaExceededError || n.#t && n.#t.DOMException || globalThis.QuotaExceededError || globalThis.DOMException, x = new u(
|
|
781
|
+
`The prompt is too large (${m} tokens), it exceeds the quota of ${n.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 = n.contextWindow, x;
|
|
791
788
|
}
|
|
792
|
-
m >
|
|
793
|
-
const w = [...
|
|
789
|
+
m > n.contextWindow && n.dispatchEvent(new Event("contextoverflow"));
|
|
790
|
+
const w = [...n.#e, y];
|
|
794
791
|
let p;
|
|
795
792
|
try {
|
|
796
|
-
p = await
|
|
793
|
+
p = await n.#o.generateContentStream(w);
|
|
797
794
|
} catch (u) {
|
|
798
|
-
throw
|
|
795
|
+
throw n.#m(u, l), u;
|
|
799
796
|
}
|
|
800
797
|
let f = "", h = !1, E = "";
|
|
801
798
|
for await (const u of p) {
|
|
@@ -803,27 +800,27 @@ class d extends EventTarget {
|
|
|
803
800
|
typeof p.return == "function" && await p.return();
|
|
804
801
|
return;
|
|
805
802
|
}
|
|
806
|
-
let
|
|
807
|
-
if (
|
|
808
|
-
E +=
|
|
809
|
-
const
|
|
810
|
-
if (
|
|
811
|
-
|
|
803
|
+
let x = u.text();
|
|
804
|
+
if (c && !h) {
|
|
805
|
+
E += x;
|
|
806
|
+
const v = E.match(/^\s*{\s*"Rating"\s*:\s*/);
|
|
807
|
+
if (v)
|
|
808
|
+
x = E.slice(v[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 && (n.#i = u.usageMetadata.totalTokenCount), o.enqueue(x);
|
|
818
815
|
}
|
|
819
|
-
i
|
|
816
|
+
!i && !n.#n && n.#e && (n.#e.push(y), n.#e.push({
|
|
820
817
|
role: "model",
|
|
821
818
|
parts: [{ text: f }]
|
|
822
|
-
}),
|
|
823
|
-
} catch (
|
|
824
|
-
i ||
|
|
819
|
+
})), o.close();
|
|
820
|
+
} catch (c) {
|
|
821
|
+
i || o.error(c);
|
|
825
822
|
} finally {
|
|
826
|
-
|
|
823
|
+
r && r.removeEventListener("abort", a);
|
|
827
824
|
}
|
|
828
825
|
}
|
|
829
826
|
});
|
|
@@ -839,21 +836,21 @@ class d extends EventTarget {
|
|
|
839
836
|
"Aborted",
|
|
840
837
|
"AbortError"
|
|
841
838
|
);
|
|
842
|
-
const
|
|
839
|
+
const n = 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.#
|
|
845
|
+
const r = { role: "user", parts: n };
|
|
846
|
+
this.#e.push(r);
|
|
850
847
|
try {
|
|
851
|
-
const
|
|
852
|
-
this.#
|
|
848
|
+
const o = [...this.#e];
|
|
849
|
+
this.#r.systemInstruction && o.unshift({
|
|
853
850
|
role: "system",
|
|
854
|
-
parts: [{ text: this.#
|
|
851
|
+
parts: [{ text: this.#r.systemInstruction }]
|
|
855
852
|
});
|
|
856
|
-
const i = await this.#o.countTokens(
|
|
853
|
+
const i = await this.#o.countTokens(o);
|
|
857
854
|
this.#i = i || 0;
|
|
858
855
|
} catch {
|
|
859
856
|
}
|
|
@@ -872,8 +869,8 @@ class d extends EventTarget {
|
|
|
872
869
|
"Session is destroyed",
|
|
873
870
|
"InvalidStateError"
|
|
874
871
|
);
|
|
875
|
-
const
|
|
876
|
-
return
|
|
872
|
+
const n = this.#u(e);
|
|
873
|
+
return n === "QuotaExceededError" ? 1e7 : n === "contextoverflow" ? 5e5 : await this.#o.countTokens([
|
|
877
874
|
{ role: "user", parts: e }
|
|
878
875
|
]) || 0;
|
|
879
876
|
} catch {
|
|
@@ -892,7 +889,7 @@ class d extends EventTarget {
|
|
|
892
889
|
const e = t[0].text;
|
|
893
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 d 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,54 +923,85 @@ class d extends EventTarget {
|
|
|
926
923
|
if (t.length === 0)
|
|
927
924
|
return [{ text: " " }];
|
|
928
925
|
if (t.length > 0 && t[0].role) {
|
|
929
|
-
let
|
|
930
|
-
for (const
|
|
931
|
-
const i =
|
|
932
|
-
if (typeof
|
|
926
|
+
let r = [];
|
|
927
|
+
for (const o of t) {
|
|
928
|
+
const i = o.role === "assistant" || o.role === "model";
|
|
929
|
+
if (typeof o.content == "string") {
|
|
933
930
|
if (!e.includes("text"))
|
|
934
931
|
throw new (this.#t.DOMException || globalThis.DOMException)(
|
|
935
932
|
'The content type "text" is not in the expectedInputs.',
|
|
936
933
|
"NotSupportedError"
|
|
937
934
|
);
|
|
938
|
-
|
|
935
|
+
r.push({ text: o.content }), o.prefix && console.warn(
|
|
939
936
|
"The `prefix` flag isn't supported and was ignored."
|
|
940
937
|
);
|
|
941
|
-
} else if (Array.isArray(
|
|
942
|
-
for (const
|
|
943
|
-
const
|
|
944
|
-
if (!e.includes(
|
|
938
|
+
} else if (Array.isArray(o.content))
|
|
939
|
+
for (const a of o.content) {
|
|
940
|
+
const c = a.type || "text";
|
|
941
|
+
if (!e.includes(c))
|
|
945
942
|
throw new (this.#t.DOMException || globalThis.DOMException)(
|
|
946
|
-
`The content type "${
|
|
943
|
+
`The content type "${c}" is not in the expectedInputs.`,
|
|
947
944
|
"NotSupportedError"
|
|
948
945
|
);
|
|
949
|
-
if (
|
|
950
|
-
if (typeof
|
|
946
|
+
if (c === "text") {
|
|
947
|
+
if (typeof a.value != "string")
|
|
951
948
|
throw new (this.#t.DOMException || globalThis.DOMException)(
|
|
952
949
|
'The content type "text" must have a string value.',
|
|
953
950
|
"SyntaxError"
|
|
954
951
|
);
|
|
955
|
-
|
|
952
|
+
r.push({ text: a.value });
|
|
956
953
|
} else {
|
|
957
954
|
if (i)
|
|
958
955
|
throw new (this.#t.DOMException || globalThis.DOMException)(
|
|
959
956
|
"Assistant messages only support text content.",
|
|
960
957
|
"NotSupportedError"
|
|
961
958
|
);
|
|
962
|
-
const l = await M.convert(
|
|
963
|
-
|
|
959
|
+
const l = a.value && a.value.inlineData ? a.value : await M.convert(a.type, a.value);
|
|
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) {
|
|
977
|
+
if (r.inlineData)
|
|
978
|
+
return r;
|
|
979
|
+
if (r.type && r.value) {
|
|
980
|
+
const o = r.type || "text";
|
|
981
|
+
if (!e.includes(o))
|
|
982
|
+
throw new (this.#t.DOMException || globalThis.DOMException)(
|
|
983
|
+
`The content type "${o}" is not in the expectedInputs.`,
|
|
984
|
+
"NotSupportedError"
|
|
985
|
+
);
|
|
986
|
+
if (o === "text") {
|
|
987
|
+
if (typeof r.value != "string")
|
|
988
|
+
throw new (this.#t.DOMException || globalThis.DOMException)(
|
|
989
|
+
'The content type "text" must have a string value.',
|
|
990
|
+
"SyntaxError"
|
|
991
|
+
);
|
|
992
|
+
return { text: r.value };
|
|
993
|
+
}
|
|
994
|
+
return r.value && r.value.inlineData ? r.value : await M.convert(r.type, r.value);
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
if (!e.includes("text"))
|
|
998
|
+
throw new (this.#t.DOMException || globalThis.DOMException)(
|
|
999
|
+
'The content type "text" is not in the expectedInputs.',
|
|
1000
|
+
"NotSupportedError"
|
|
1001
|
+
);
|
|
1002
|
+
return { text: String(r) };
|
|
1003
|
+
})
|
|
1004
|
+
);
|
|
977
1005
|
}
|
|
978
1006
|
if (!e.includes("text"))
|
|
979
1007
|
throw new (this.#t.DOMException || globalThis.DOMException)(
|
|
@@ -984,16 +1012,16 @@ class d extends EventTarget {
|
|
|
984
1012
|
}
|
|
985
1013
|
// Map backend errors to WPT expectations
|
|
986
1014
|
#m(t, e) {
|
|
987
|
-
const
|
|
988
|
-
if (
|
|
989
|
-
const
|
|
990
|
-
(
|
|
991
|
-
),
|
|
992
|
-
(
|
|
1015
|
+
const n = String(t.message || t);
|
|
1016
|
+
if (n.includes("400") || n.toLowerCase().includes("unable to process") || n.toLowerCase().includes("invalid")) {
|
|
1017
|
+
const r = e.some(
|
|
1018
|
+
(a) => a.inlineData?.mimeType.startsWith("audio/")
|
|
1019
|
+
), o = e.some(
|
|
1020
|
+
(a) => a.inlineData?.mimeType.startsWith("image/")
|
|
993
1021
|
), i = this.#t.DOMException || globalThis.DOMException;
|
|
994
|
-
if (
|
|
1022
|
+
if (r)
|
|
995
1023
|
throw new i("Invalid audio data", "DataError");
|
|
996
|
-
if (
|
|
1024
|
+
if (o)
|
|
997
1025
|
throw new i("Invalid image data", "InvalidStateError");
|
|
998
1026
|
}
|
|
999
1027
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "prompt-api-polyfill",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.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",
|
|
@@ -52,9 +52,9 @@
|
|
|
52
52
|
"node-addon-api": "^8.5.0"
|
|
53
53
|
},
|
|
54
54
|
"dependencies": {
|
|
55
|
-
"@google/genai": "^1.
|
|
55
|
+
"@google/genai": "^1.43.0",
|
|
56
56
|
"@huggingface/transformers": "^3.8.1",
|
|
57
57
|
"firebase": "^12.9.0",
|
|
58
|
-
"openai": "^6.
|
|
58
|
+
"openai": "^6.25.0"
|
|
59
59
|
}
|
|
60
60
|
}
|