prompt-api-polyfill 1.7.0 → 1.8.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 +286 -285
- package/package.json +1 -1
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import { Schema as T } from "firebase/ai";
|
|
2
2
|
ReadableStream.prototype[Symbol.asyncIterator] || (ReadableStream.prototype[Symbol.asyncIterator] = async function* () {
|
|
3
|
-
const
|
|
3
|
+
const s = this.getReader();
|
|
4
4
|
try {
|
|
5
5
|
for (; ; ) {
|
|
6
|
-
const { done: t, value: e } = await
|
|
6
|
+
const { done: t, value: e } = await s.read();
|
|
7
7
|
if (t)
|
|
8
8
|
return;
|
|
9
9
|
yield e;
|
|
10
10
|
}
|
|
11
11
|
} finally {
|
|
12
|
-
|
|
12
|
+
s.releaseLock();
|
|
13
13
|
}
|
|
14
14
|
});
|
|
15
15
|
class M {
|
|
@@ -24,16 +24,17 @@ class M {
|
|
|
24
24
|
);
|
|
25
25
|
}
|
|
26
26
|
static async processImage(t) {
|
|
27
|
-
if (t instanceof Blob)
|
|
27
|
+
if (t instanceof Blob || t && typeof t == "object" && t.constructor && t.constructor.name === "Blob")
|
|
28
28
|
return this.blobToInlineData(t);
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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
|
+
if (r || n) {
|
|
31
|
+
const o = r ? new Uint8Array(t) : new Uint8Array(t.buffer, t.byteOffset, t.byteLength), i = o.buffer.slice(
|
|
32
|
+
o.byteOffset,
|
|
33
|
+
o.byteOffset + o.byteLength
|
|
34
|
+
), a = this.arrayBufferToBase64(i), l = this.#o(o);
|
|
35
|
+
if (!l)
|
|
35
36
|
throw new DOMException("Invalid image data", "InvalidStateError");
|
|
36
|
-
return { inlineData: { data:
|
|
37
|
+
return { inlineData: { data: a, mimeType: l } };
|
|
37
38
|
}
|
|
38
39
|
return this.canvasSourceToInlineData(t);
|
|
39
40
|
}
|
|
@@ -55,37 +56,37 @@ class M {
|
|
|
55
56
|
if (t[0] === 73 && t[1] === 73 && t[2] === 42 || t[0] === 77 && t[1] === 77 && t[2] === 42)
|
|
56
57
|
return "image/tiff";
|
|
57
58
|
if (t[4] === 102 && t[5] === 116 && t[6] === 121 && t[7] === 112) {
|
|
58
|
-
const
|
|
59
|
-
if (
|
|
59
|
+
const n = String.fromCharCode(t[8], t[9], t[10], t[11]);
|
|
60
|
+
if (n === "avif" || n === "avis")
|
|
60
61
|
return "image/avif";
|
|
61
|
-
if (
|
|
62
|
+
if (n === "heic" || n === "heix" || n === "hevc" || n === "hevx")
|
|
62
63
|
return "image/heic";
|
|
63
|
-
if (
|
|
64
|
+
if (n === "mif1" || n === "msf1")
|
|
64
65
|
return "image/heif";
|
|
65
66
|
}
|
|
66
67
|
if (t[0] === 255 && t[1] === 10 || t[0] === 0 && t[4] === 74 && t[5] === 88 && t[6] === 76)
|
|
67
68
|
return "image/jxl";
|
|
68
69
|
if (t[0] === 0 && t[4] === 106 && t[5] === 80 && t[6] === 32)
|
|
69
70
|
return "image/jp2";
|
|
70
|
-
const
|
|
71
|
-
return
|
|
71
|
+
const r = String.fromCharCode(...t.slice(0, 100)).toLowerCase();
|
|
72
|
+
return r.includes("<svg") || r.includes("<?xml") ? "image/svg+xml" : null;
|
|
72
73
|
}
|
|
73
74
|
static async processAudio(t) {
|
|
74
|
-
if (t instanceof Blob) {
|
|
75
|
+
if (t instanceof Blob || t && typeof t == "object" && t.constructor && t.constructor.name === "Blob") {
|
|
75
76
|
if (t.type && !t.type.startsWith("audio/") && t.type !== "application/ogg")
|
|
76
77
|
throw new DOMException("Invalid audio mime type", "DataError");
|
|
77
78
|
return this.blobToInlineData(t);
|
|
78
79
|
}
|
|
79
|
-
if (t instanceof AudioBuffer) {
|
|
80
|
-
const
|
|
81
|
-
return { inlineData: { data: this.arrayBufferToBase64(
|
|
80
|
+
if (t instanceof AudioBuffer || t && t.constructor && t.constructor.name === "AudioBuffer") {
|
|
81
|
+
const i = this.audioBufferToWav(t);
|
|
82
|
+
return { inlineData: { data: this.arrayBufferToBase64(i), mimeType: "audio/wav" } };
|
|
82
83
|
}
|
|
83
|
-
const
|
|
84
|
-
if (
|
|
85
|
-
const
|
|
84
|
+
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
|
+
if (n || o) {
|
|
86
|
+
const i = n ? t : t.buffer;
|
|
86
87
|
return {
|
|
87
88
|
inlineData: {
|
|
88
|
-
data: this.arrayBufferToBase64(
|
|
89
|
+
data: this.arrayBufferToBase64(i),
|
|
89
90
|
mimeType: "audio/wav"
|
|
90
91
|
// Fallback assumption
|
|
91
92
|
}
|
|
@@ -95,53 +96,53 @@ class M {
|
|
|
95
96
|
}
|
|
96
97
|
// Low Level Converters
|
|
97
98
|
static blobToInlineData(t) {
|
|
98
|
-
return new Promise((e,
|
|
99
|
-
const
|
|
100
|
-
|
|
101
|
-
|
|
99
|
+
return new Promise((e, r) => {
|
|
100
|
+
const n = new FileReader();
|
|
101
|
+
n.onloadend = () => {
|
|
102
|
+
n.error ? r(n.error) : e({
|
|
102
103
|
inlineData: {
|
|
103
|
-
data:
|
|
104
|
+
data: n.result.split(",")[1],
|
|
104
105
|
mimeType: t.type
|
|
105
106
|
}
|
|
106
107
|
});
|
|
107
|
-
},
|
|
108
|
+
}, n.readAsDataURL(t);
|
|
108
109
|
});
|
|
109
110
|
}
|
|
110
111
|
static async canvasSourceToInlineData(t) {
|
|
111
112
|
if (!t)
|
|
112
113
|
throw new DOMException("Invalid image source", "InvalidStateError");
|
|
113
114
|
typeof HTMLImageElement < "u" && t instanceof HTMLImageElement && !t.complete && await t.decode().catch(() => {
|
|
114
|
-
}), typeof HTMLVideoElement < "u" && t instanceof HTMLVideoElement && t.readyState < 2 && await new Promise((
|
|
115
|
-
t.addEventListener("loadeddata",
|
|
115
|
+
}), typeof HTMLVideoElement < "u" && t instanceof HTMLVideoElement && t.readyState < 2 && await new Promise((c) => {
|
|
116
|
+
t.addEventListener("loadeddata", c, { once: !0 }), t.readyState >= 2 && c(), setTimeout(c, 1e3);
|
|
116
117
|
});
|
|
117
|
-
const e = (
|
|
118
|
-
const p = t[
|
|
118
|
+
const e = (c) => {
|
|
119
|
+
const p = t[c];
|
|
119
120
|
return typeof p == "object" && p !== null && "baseVal" in p ? p.baseVal.value : typeof p == "number" ? p : 0;
|
|
120
121
|
};
|
|
121
|
-
let
|
|
122
|
-
if ((!
|
|
122
|
+
let r = t.displayWidth || t.naturalWidth || t.videoWidth || t.width || e("width"), n = t.displayHeight || t.naturalHeight || t.videoHeight || t.height || e("height");
|
|
123
|
+
if ((!r || !n) && typeof t.getBBox == "function")
|
|
123
124
|
try {
|
|
124
|
-
const
|
|
125
|
-
|
|
125
|
+
const c = t.getBBox();
|
|
126
|
+
r = r || c.width, n = n || c.height;
|
|
126
127
|
} catch {
|
|
127
128
|
}
|
|
128
|
-
if ((!
|
|
129
|
+
if ((!r || !n) && typeof t.getBoundingClientRect == "function")
|
|
129
130
|
try {
|
|
130
|
-
const
|
|
131
|
-
|
|
131
|
+
const c = t.getBoundingClientRect();
|
|
132
|
+
r = r || c.width, n = n || c.height;
|
|
132
133
|
} catch {
|
|
133
134
|
}
|
|
134
|
-
if (!
|
|
135
|
-
const
|
|
135
|
+
if (!r || !n) {
|
|
136
|
+
const c = t.constructor && t.constructor.name ? t.constructor.name : typeof t;
|
|
136
137
|
throw new DOMException(
|
|
137
|
-
`Invalid image dimensions (${
|
|
138
|
+
`Invalid image dimensions (${r}x${n}) for source type ${c}`,
|
|
138
139
|
"InvalidStateError"
|
|
139
140
|
);
|
|
140
141
|
}
|
|
141
142
|
const o = document.createElement("canvas");
|
|
142
|
-
o.width =
|
|
143
|
+
o.width = r, o.height = n;
|
|
143
144
|
const i = o.getContext("2d");
|
|
144
|
-
return typeof ImageData < "u" && t instanceof ImageData || t && typeof t.width == "number" && typeof t.height == "number" && t.data && t.data.buffer ? i.putImageData(t, 0, 0) : i.drawImage(t, 0, 0), {
|
|
145
|
+
return typeof ImageData < "u" && t instanceof ImageData || t && t.constructor && t.constructor.name === "ImageData" || t && typeof t.width == "number" && typeof t.height == "number" && t.data && t.data.buffer ? i.putImageData(t, 0, 0) : i.drawImage(t, 0, 0), {
|
|
145
146
|
inlineData: {
|
|
146
147
|
data: o.toDataURL("image/png").split(",")[1],
|
|
147
148
|
mimeType: "image/png"
|
|
@@ -150,55 +151,55 @@ class M {
|
|
|
150
151
|
}
|
|
151
152
|
static arrayBufferToBase64(t) {
|
|
152
153
|
let e = "";
|
|
153
|
-
const
|
|
154
|
-
for (let o = 0; o <
|
|
155
|
-
e += String.fromCharCode(
|
|
154
|
+
const r = new Uint8Array(t), n = r.byteLength;
|
|
155
|
+
for (let o = 0; o < n; o++)
|
|
156
|
+
e += String.fromCharCode(r[o]);
|
|
156
157
|
return window.btoa(e);
|
|
157
158
|
}
|
|
158
159
|
// Simple WAV Encoder for AudioBuffer
|
|
159
160
|
static audioBufferToWav(t) {
|
|
160
|
-
const e = t.numberOfChannels,
|
|
161
|
+
const e = t.numberOfChannels, r = t.sampleRate, n = 1, o = 16;
|
|
161
162
|
let i;
|
|
162
163
|
return e === 2 ? i = this.interleave(
|
|
163
164
|
t.getChannelData(0),
|
|
164
165
|
t.getChannelData(1)
|
|
165
|
-
) : i = t.getChannelData(0), this.encodeWAV(i,
|
|
166
|
+
) : i = t.getChannelData(0), this.encodeWAV(i, n, r, e, o);
|
|
166
167
|
}
|
|
167
168
|
static interleave(t, e) {
|
|
168
|
-
const
|
|
169
|
+
const r = t.length + e.length, n = new Float32Array(r);
|
|
169
170
|
let o = 0, i = 0;
|
|
170
|
-
for (; o <
|
|
171
|
-
|
|
172
|
-
return
|
|
171
|
+
for (; o < r; )
|
|
172
|
+
n[o++] = t[i], n[o++] = e[i], i++;
|
|
173
|
+
return n;
|
|
173
174
|
}
|
|
174
|
-
static encodeWAV(t, e,
|
|
175
|
-
const i = o / 8,
|
|
176
|
-
return this.writeString(
|
|
175
|
+
static encodeWAV(t, e, r, n, o) {
|
|
176
|
+
const i = o / 8, a = n * i, l = new ArrayBuffer(44 + t.length * i), c = new DataView(l);
|
|
177
|
+
return this.writeString(c, 0, "RIFF"), c.setUint32(4, 36 + t.length * i, !0), this.writeString(c, 8, "WAVE"), this.writeString(c, 12, "fmt "), c.setUint32(16, 16, !0), c.setUint16(20, e, !0), c.setUint16(22, n, !0), c.setUint32(24, r, !0), c.setUint32(28, r * a, !0), c.setUint16(32, a, !0), c.setUint16(34, o, !0), this.writeString(c, 36, "data"), c.setUint32(40, t.length * i, !0), this.floatTo16BitPCM(c, 44, t), l;
|
|
177
178
|
}
|
|
178
|
-
static floatTo16BitPCM(t, e,
|
|
179
|
-
for (let
|
|
180
|
-
const o = Math.max(-1, Math.min(1, n
|
|
179
|
+
static floatTo16BitPCM(t, e, r) {
|
|
180
|
+
for (let n = 0; n < r.length; n++, e += 2) {
|
|
181
|
+
const o = Math.max(-1, Math.min(1, r[n]));
|
|
181
182
|
t.setInt16(e, o < 0 ? o * 32768 : o * 32767, !0);
|
|
182
183
|
}
|
|
183
184
|
}
|
|
184
|
-
static writeString(t, e,
|
|
185
|
-
for (let
|
|
186
|
-
t.setUint8(e +
|
|
185
|
+
static writeString(t, e, r) {
|
|
186
|
+
for (let n = 0; n < r.length; n++)
|
|
187
|
+
t.setUint8(e + n, r.charCodeAt(n));
|
|
187
188
|
}
|
|
188
189
|
}
|
|
189
|
-
function O(
|
|
190
|
-
if (!
|
|
190
|
+
function O(s) {
|
|
191
|
+
if (!s)
|
|
191
192
|
return;
|
|
192
193
|
const t = {
|
|
193
|
-
description:
|
|
194
|
-
nullable:
|
|
195
|
-
format:
|
|
194
|
+
description: s.description,
|
|
195
|
+
nullable: s.nullable || !1,
|
|
196
|
+
format: s.format
|
|
196
197
|
};
|
|
197
|
-
switch (Array.isArray(
|
|
198
|
+
switch (Array.isArray(s.type) && s.type.includes("null") && (t.nullable = !0, s.type = s.type.find((e) => e !== "null")), s.type) {
|
|
198
199
|
case "string":
|
|
199
|
-
return
|
|
200
|
+
return s.enum && Array.isArray(s.enum) ? T.enumString({
|
|
200
201
|
...t,
|
|
201
|
-
enum:
|
|
202
|
+
enum: s.enum
|
|
202
203
|
}) : T.string(t);
|
|
203
204
|
case "number":
|
|
204
205
|
return T.number(t);
|
|
@@ -210,17 +211,17 @@ function O(a) {
|
|
|
210
211
|
return T.array({
|
|
211
212
|
...t,
|
|
212
213
|
// Recursively convert the 'items' schema
|
|
213
|
-
items: O(
|
|
214
|
+
items: O(s.items)
|
|
214
215
|
});
|
|
215
216
|
case "object":
|
|
216
|
-
const e = {},
|
|
217
|
-
|
|
217
|
+
const e = {}, r = s.properties ? Object.keys(s.properties) : [];
|
|
218
|
+
r.forEach((i) => {
|
|
218
219
|
e[i] = O(
|
|
219
|
-
|
|
220
|
+
s.properties[i]
|
|
220
221
|
);
|
|
221
222
|
});
|
|
222
|
-
const
|
|
223
|
-
(i) => !
|
|
223
|
+
const n = s.required || [], o = r.filter(
|
|
224
|
+
(i) => !n.includes(i)
|
|
224
225
|
);
|
|
225
226
|
return T.object({
|
|
226
227
|
...t,
|
|
@@ -229,11 +230,11 @@ function O(a) {
|
|
|
229
230
|
});
|
|
230
231
|
default:
|
|
231
232
|
return console.warn(
|
|
232
|
-
`Unsupported type: ${
|
|
233
|
+
`Unsupported type: ${s.type}, defaulting to string.`
|
|
233
234
|
), T.string(t);
|
|
234
235
|
}
|
|
235
236
|
}
|
|
236
|
-
const
|
|
237
|
+
const A = [
|
|
237
238
|
{
|
|
238
239
|
config: "FIREBASE_CONFIG",
|
|
239
240
|
path: "./backends/firebase.js"
|
|
@@ -251,44 +252,44 @@ const I = [
|
|
|
251
252
|
path: "./backends/transformers.js"
|
|
252
253
|
}
|
|
253
254
|
];
|
|
254
|
-
async function
|
|
255
|
-
if (
|
|
255
|
+
async function I(s) {
|
|
256
|
+
if (s === "./backends/firebase.js")
|
|
256
257
|
return (await import("./backends/firebase.js")).default;
|
|
257
|
-
if (
|
|
258
|
+
if (s === "./backends/gemini.js")
|
|
258
259
|
return (await import("./backends/gemini.js")).default;
|
|
259
|
-
if (
|
|
260
|
+
if (s === "./backends/openai.js")
|
|
260
261
|
return (await import("./backends/openai.js")).default;
|
|
261
|
-
if (
|
|
262
|
+
if (s === "./backends/transformers.js")
|
|
262
263
|
return (await import("./backends/transformers.js")).default;
|
|
263
|
-
throw new Error(`Unknown backend path "${
|
|
264
|
+
throw new Error(`Unknown backend path "${s}"`);
|
|
264
265
|
}
|
|
265
|
-
async function S(
|
|
266
|
+
async function S(s, t = globalThis) {
|
|
266
267
|
const e = [];
|
|
267
|
-
for (const
|
|
268
|
-
const
|
|
268
|
+
for (const r of s) {
|
|
269
|
+
const n = r.role === "assistant" ? "model" : "user", o = n === "model";
|
|
269
270
|
let i = [];
|
|
270
|
-
if (Array.isArray(
|
|
271
|
-
for (const
|
|
272
|
-
if (
|
|
273
|
-
const
|
|
274
|
-
if (typeof
|
|
271
|
+
if (Array.isArray(r.content))
|
|
272
|
+
for (const a of r.content)
|
|
273
|
+
if (a.type === "text") {
|
|
274
|
+
const l = a.value || a.text || "";
|
|
275
|
+
if (typeof l != "string")
|
|
275
276
|
throw new (t.DOMException || globalThis.DOMException)(
|
|
276
277
|
'The content type "text" must have a string value.',
|
|
277
278
|
"SyntaxError"
|
|
278
279
|
);
|
|
279
|
-
i.push({ text:
|
|
280
|
+
i.push({ text: l });
|
|
280
281
|
} else {
|
|
281
282
|
if (o)
|
|
282
283
|
throw new (t.DOMException || globalThis.DOMException)(
|
|
283
284
|
"Assistant messages only support text content.",
|
|
284
285
|
"NotSupportedError"
|
|
285
286
|
);
|
|
286
|
-
const
|
|
287
|
-
i.push(
|
|
287
|
+
const l = await M.convert(a.type, a.value);
|
|
288
|
+
i.push(l);
|
|
288
289
|
}
|
|
289
290
|
else
|
|
290
|
-
i.push({ text:
|
|
291
|
-
e.push({ role:
|
|
291
|
+
i.push({ text: r.content });
|
|
292
|
+
e.push({ role: n, parts: i });
|
|
292
293
|
}
|
|
293
294
|
return e;
|
|
294
295
|
}
|
|
@@ -297,13 +298,13 @@ class d extends EventTarget {
|
|
|
297
298
|
#f;
|
|
298
299
|
#e;
|
|
299
300
|
#a;
|
|
300
|
-
#r;
|
|
301
301
|
#n;
|
|
302
|
+
#r;
|
|
302
303
|
#i;
|
|
303
|
-
#
|
|
304
|
+
#c;
|
|
304
305
|
#t;
|
|
305
|
-
constructor(t, e,
|
|
306
|
-
super(), this.#o = t, this.#f = e, this.#e =
|
|
306
|
+
constructor(t, e, r, n = {}, o, i = 0, a = globalThis) {
|
|
307
|
+
super(), this.#o = t, this.#f = e, this.#e = r || [], this.#a = n, this.#n = o, this.#r = !1, this.#i = i, this.#c = {}, this.#t = a;
|
|
307
308
|
}
|
|
308
309
|
get contextUsage() {
|
|
309
310
|
return this.#i;
|
|
@@ -312,10 +313,10 @@ class d extends EventTarget {
|
|
|
312
313
|
return 1e6;
|
|
313
314
|
}
|
|
314
315
|
get oncontextoverflow() {
|
|
315
|
-
return this.#
|
|
316
|
+
return this.#c;
|
|
316
317
|
}
|
|
317
318
|
set oncontextoverflow(t) {
|
|
318
|
-
this.#
|
|
319
|
+
this.#c && this.removeEventListener("contextoverflow", this.#c), this.#c = t, typeof t == "function" && this.addEventListener("contextoverflow", t);
|
|
319
320
|
}
|
|
320
321
|
static #h(t) {
|
|
321
322
|
try {
|
|
@@ -324,8 +325,8 @@ class d extends EventTarget {
|
|
|
324
325
|
if (t !== globalThis && t !== t.top && (!t.frameElement || !t.frameElement.isConnected))
|
|
325
326
|
throw new Error();
|
|
326
327
|
} catch {
|
|
327
|
-
const
|
|
328
|
-
throw new
|
|
328
|
+
const r = t?.DOMException || globalThis.DOMException;
|
|
329
|
+
throw new r(
|
|
329
330
|
"The execution context is not valid.",
|
|
330
331
|
"InvalidStateError"
|
|
331
332
|
);
|
|
@@ -339,31 +340,31 @@ class d extends EventTarget {
|
|
|
339
340
|
d.#h(e);
|
|
340
341
|
try {
|
|
341
342
|
await d.#w(t, e);
|
|
342
|
-
} catch (
|
|
343
|
-
if (
|
|
344
|
-
if (
|
|
345
|
-
throw
|
|
343
|
+
} catch (n) {
|
|
344
|
+
if (n instanceof RangeError) {
|
|
345
|
+
if (n.message.includes("language tag"))
|
|
346
|
+
throw n;
|
|
346
347
|
return "unavailable";
|
|
347
348
|
}
|
|
348
|
-
if (
|
|
349
|
+
if (n.name === "NotSupportedError")
|
|
349
350
|
return "unavailable";
|
|
350
|
-
if (
|
|
351
|
-
if (/system/i.test(
|
|
351
|
+
if (n instanceof TypeError) {
|
|
352
|
+
if (/system/i.test(n.message))
|
|
352
353
|
return "unavailable";
|
|
353
|
-
throw
|
|
354
|
+
throw n;
|
|
354
355
|
}
|
|
355
356
|
return "unavailable";
|
|
356
357
|
}
|
|
357
358
|
return (await d.#p(e)).availability(t);
|
|
358
359
|
}
|
|
359
|
-
static #x =
|
|
360
|
+
static #x = A;
|
|
360
361
|
static #d(t = globalThis) {
|
|
361
|
-
for (const
|
|
362
|
-
const
|
|
363
|
-
if (
|
|
364
|
-
return { ...
|
|
362
|
+
for (const r of d.#x) {
|
|
363
|
+
const n = t[r.config] || globalThis[r.config];
|
|
364
|
+
if (n && n.apiKey)
|
|
365
|
+
return { ...r, configValue: n };
|
|
365
366
|
}
|
|
366
|
-
const e = d.#x.map((
|
|
367
|
+
const e = d.#x.map((r) => `window.${r.config}`).join(", ");
|
|
367
368
|
throw new (t.DOMException || globalThis.DOMException)(
|
|
368
369
|
`Prompt API Polyfill: No backend configuration found. Please set one of: ${e}.`,
|
|
369
370
|
"NotSupportedError"
|
|
@@ -371,24 +372,24 @@ class d extends EventTarget {
|
|
|
371
372
|
}
|
|
372
373
|
static async #p(t = globalThis) {
|
|
373
374
|
const e = d.#d(t);
|
|
374
|
-
return
|
|
375
|
+
return I(e.path);
|
|
375
376
|
}
|
|
376
377
|
static async #w(t = {}, e = globalThis) {
|
|
377
378
|
if (t.expectedInputs)
|
|
378
|
-
for (const
|
|
379
|
-
if (
|
|
380
|
-
throw new TypeError(`Invalid input type: ${
|
|
381
|
-
|
|
379
|
+
for (const n of t.expectedInputs) {
|
|
380
|
+
if (n.type !== "text" && n.type !== "image" && n.type !== "audio")
|
|
381
|
+
throw new TypeError(`Invalid input type: ${n.type}`);
|
|
382
|
+
n.languages && d.#y(n.languages);
|
|
382
383
|
}
|
|
383
384
|
if (t.expectedOutputs)
|
|
384
|
-
for (const
|
|
385
|
-
if (
|
|
386
|
-
throw new RangeError(`Unsupported output type: ${
|
|
387
|
-
|
|
385
|
+
for (const n of t.expectedOutputs) {
|
|
386
|
+
if (n.type !== "text")
|
|
387
|
+
throw new RangeError(`Unsupported output type: ${n.type}`);
|
|
388
|
+
n.languages && d.#y(n.languages);
|
|
388
389
|
}
|
|
389
|
-
const
|
|
390
|
+
const r = t.expectedInputs ? ["text", ...t.expectedInputs.map((n) => n.type)] : ["text"];
|
|
390
391
|
if (t.initialPrompts && Array.isArray(t.initialPrompts)) {
|
|
391
|
-
let
|
|
392
|
+
let n = !1;
|
|
392
393
|
for (let o = 0; o < t.initialPrompts.length; o++) {
|
|
393
394
|
const i = t.initialPrompts[o];
|
|
394
395
|
if (i.role === "system") {
|
|
@@ -396,22 +397,22 @@ class d extends EventTarget {
|
|
|
396
397
|
throw new TypeError(
|
|
397
398
|
"The prompt with 'system' role must be placed at the first entry of initialPrompts."
|
|
398
399
|
);
|
|
399
|
-
if (
|
|
400
|
+
if (n)
|
|
400
401
|
throw new TypeError(
|
|
401
402
|
"The prompt with 'system' role must be placed at the first entry of initialPrompts."
|
|
402
403
|
);
|
|
403
|
-
|
|
404
|
+
n = !0;
|
|
404
405
|
}
|
|
405
406
|
if (Array.isArray(i.content))
|
|
406
|
-
for (const
|
|
407
|
-
const
|
|
408
|
-
if (!
|
|
407
|
+
for (const a of i.content) {
|
|
408
|
+
const l = a.type || "text";
|
|
409
|
+
if (!r.includes(l))
|
|
409
410
|
throw new (e.DOMException || globalThis.DOMException)(
|
|
410
|
-
`The content type "${
|
|
411
|
+
`The content type "${l}" is not in the expectedInputs.`,
|
|
411
412
|
"NotSupportedError"
|
|
412
413
|
);
|
|
413
414
|
}
|
|
414
|
-
else if (!
|
|
415
|
+
else if (!r.includes("text"))
|
|
415
416
|
throw new (e.DOMException || globalThis.DOMException)(
|
|
416
417
|
'The content type "text" is not in the expectedInputs.',
|
|
417
418
|
"NotSupportedError"
|
|
@@ -445,13 +446,13 @@ class d extends EventTarget {
|
|
|
445
446
|
"Aborted",
|
|
446
447
|
"AbortError"
|
|
447
448
|
);
|
|
448
|
-
const
|
|
449
|
-
if (
|
|
449
|
+
const r = await this.availability(t);
|
|
450
|
+
if (r === "unavailable")
|
|
450
451
|
throw new (e.DOMException || globalThis.DOMException)(
|
|
451
452
|
"The model is not available for the given options.",
|
|
452
453
|
"NotSupportedError"
|
|
453
454
|
);
|
|
454
|
-
if (
|
|
455
|
+
if (r === "downloadable" || r === "downloading")
|
|
455
456
|
throw new (e.DOMException || globalThis.DOMException)(
|
|
456
457
|
'Requires a user gesture when availability is "downloading" or "downloadable".',
|
|
457
458
|
"NotAllowedError"
|
|
@@ -461,26 +462,26 @@ class d extends EventTarget {
|
|
|
461
462
|
"Aborted",
|
|
462
463
|
"AbortError"
|
|
463
464
|
);
|
|
464
|
-
const
|
|
465
|
+
const n = d.#d(e), o = await d.#p(e), i = new o(n.configValue), a = { ...t };
|
|
465
466
|
d.#g(
|
|
466
|
-
|
|
467
|
+
a.responseConstraint,
|
|
467
468
|
e
|
|
468
469
|
);
|
|
469
|
-
const
|
|
470
|
+
const l = {
|
|
470
471
|
model: i.modelName,
|
|
471
472
|
generationConfig: {}
|
|
472
473
|
};
|
|
473
|
-
let
|
|
474
|
-
if (
|
|
475
|
-
const y =
|
|
474
|
+
let c = [], p = 0;
|
|
475
|
+
if (a.initialPrompts && Array.isArray(a.initialPrompts)) {
|
|
476
|
+
const y = a.initialPrompts.filter(
|
|
476
477
|
(f) => f.role === "system"
|
|
477
|
-
), u =
|
|
478
|
+
), u = a.initialPrompts.filter(
|
|
478
479
|
(f) => f.role !== "system"
|
|
479
480
|
);
|
|
480
|
-
y.length > 0 && (
|
|
481
|
+
y.length > 0 && (l.systemInstruction = y.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(`
|
|
481
482
|
`) : "").join(`
|
|
482
|
-
`)),
|
|
483
|
-
for (const f of
|
|
483
|
+
`)), c = await S(u, e);
|
|
484
|
+
for (const f of a.initialPrompts) {
|
|
484
485
|
if (typeof f.content != "string")
|
|
485
486
|
continue;
|
|
486
487
|
const h = d.#E([
|
|
@@ -501,10 +502,10 @@ class d extends EventTarget {
|
|
|
501
502
|
}
|
|
502
503
|
}
|
|
503
504
|
let w = null;
|
|
504
|
-
if (typeof
|
|
505
|
+
if (typeof a.monitor == "function") {
|
|
505
506
|
w = new EventTarget();
|
|
506
507
|
try {
|
|
507
|
-
|
|
508
|
+
a.monitor(w);
|
|
508
509
|
} catch (y) {
|
|
509
510
|
throw y;
|
|
510
511
|
}
|
|
@@ -535,8 +536,8 @@ class d extends EventTarget {
|
|
|
535
536
|
"AbortError"
|
|
536
537
|
);
|
|
537
538
|
const b = await i.createSession(
|
|
538
|
-
|
|
539
|
-
|
|
539
|
+
a,
|
|
540
|
+
l,
|
|
540
541
|
w
|
|
541
542
|
);
|
|
542
543
|
if (!await m(1))
|
|
@@ -544,11 +545,11 @@ class d extends EventTarget {
|
|
|
544
545
|
"Aborted",
|
|
545
546
|
"AbortError"
|
|
546
547
|
);
|
|
547
|
-
if (
|
|
548
|
-
const y = [...
|
|
549
|
-
if (
|
|
548
|
+
if (a.initialPrompts?.length > 0) {
|
|
549
|
+
const y = [...c];
|
|
550
|
+
if (l.systemInstruction && y.unshift({
|
|
550
551
|
role: "system",
|
|
551
|
-
parts: [{ text:
|
|
552
|
+
parts: [{ text: l.systemInstruction }]
|
|
552
553
|
}), p = await i.countTokens(y) || 0, p > 1e6) {
|
|
553
554
|
const u = e.QuotaExceededError || e.DOMException || globalThis.QuotaExceededError || globalThis.DOMException, f = new u(
|
|
554
555
|
"The initial prompts are too large, they exceed the quota.",
|
|
@@ -560,16 +561,16 @@ class d extends EventTarget {
|
|
|
560
561
|
return new this(
|
|
561
562
|
i,
|
|
562
563
|
b,
|
|
563
|
-
l,
|
|
564
|
-
s,
|
|
565
564
|
c,
|
|
565
|
+
a,
|
|
566
|
+
l,
|
|
566
567
|
p,
|
|
567
568
|
e
|
|
568
569
|
);
|
|
569
570
|
}
|
|
570
571
|
// Instance Methods
|
|
571
572
|
async clone(t = {}) {
|
|
572
|
-
if (this.#s(), this.#
|
|
573
|
+
if (this.#s(), this.#r)
|
|
573
574
|
throw new (this.#t.DOMException || globalThis.DOMException)(
|
|
574
575
|
"Session is destroyed",
|
|
575
576
|
"InvalidStateError"
|
|
@@ -579,9 +580,9 @@ class d extends EventTarget {
|
|
|
579
580
|
"Aborted",
|
|
580
581
|
"AbortError"
|
|
581
582
|
);
|
|
582
|
-
const e = JSON.parse(JSON.stringify(this.#e)),
|
|
583
|
-
|
|
584
|
-
this.#
|
|
583
|
+
const e = JSON.parse(JSON.stringify(this.#e)), r = { ...this.#a, ...t }, n = await d.#p(this.#t), o = d.#d(this.#t), i = new n(o.configValue), a = await i.createSession(
|
|
584
|
+
r,
|
|
585
|
+
this.#n
|
|
585
586
|
);
|
|
586
587
|
if (t.signal?.aborted)
|
|
587
588
|
throw t.signal.reason || new (this.#t.DOMException || globalThis.DOMException)(
|
|
@@ -590,19 +591,19 @@ class d extends EventTarget {
|
|
|
590
591
|
);
|
|
591
592
|
return new this.constructor(
|
|
592
593
|
i,
|
|
593
|
-
|
|
594
|
+
a,
|
|
594
595
|
e,
|
|
595
|
-
|
|
596
|
-
this.#
|
|
596
|
+
r,
|
|
597
|
+
this.#n,
|
|
597
598
|
this.#i,
|
|
598
599
|
this.#t
|
|
599
600
|
);
|
|
600
601
|
}
|
|
601
602
|
destroy() {
|
|
602
|
-
this.#s(), this.#
|
|
603
|
+
this.#s(), this.#r = !0, this.#e = null;
|
|
603
604
|
}
|
|
604
605
|
async prompt(t, e = {}) {
|
|
605
|
-
if (this.#s(), this.#
|
|
606
|
+
if (this.#s(), this.#r)
|
|
606
607
|
throw new (this.#t.DOMException || globalThis.DOMException)(
|
|
607
608
|
"Session is destroyed",
|
|
608
609
|
"InvalidStateError"
|
|
@@ -619,23 +620,23 @@ class d extends EventTarget {
|
|
|
619
620
|
e.responseConstraint,
|
|
620
621
|
this.#t
|
|
621
622
|
);
|
|
622
|
-
const
|
|
623
|
+
const l = O(
|
|
623
624
|
e.responseConstraint
|
|
624
625
|
);
|
|
625
|
-
this.#
|
|
626
|
+
this.#n.generationConfig.responseMimeType = "application/json", this.#n.generationConfig.responseSchema = l, this.#f = this.#o.createSession(
|
|
626
627
|
this.#a,
|
|
627
|
-
this.#
|
|
628
|
+
this.#n
|
|
628
629
|
);
|
|
629
630
|
}
|
|
630
|
-
const
|
|
631
|
-
if (this.#
|
|
631
|
+
const r = this.#m(t), n = await this.#l(t);
|
|
632
|
+
if (this.#r)
|
|
632
633
|
throw new (this.#t.DOMException || globalThis.DOMException)(
|
|
633
634
|
"Session is destroyed",
|
|
634
635
|
"InvalidStateError"
|
|
635
636
|
);
|
|
636
|
-
const o = { role: "user", parts:
|
|
637
|
+
const o = { role: "user", parts: n }, i = new Promise((l, c) => {
|
|
637
638
|
if (e.signal?.aborted) {
|
|
638
|
-
|
|
639
|
+
c(
|
|
639
640
|
e.signal.reason || new (this.#t.DOMException || globalThis.DOMException)(
|
|
640
641
|
"Aborted",
|
|
641
642
|
"AbortError"
|
|
@@ -646,7 +647,7 @@ class d extends EventTarget {
|
|
|
646
647
|
e.signal?.addEventListener(
|
|
647
648
|
"abort",
|
|
648
649
|
() => {
|
|
649
|
-
|
|
650
|
+
c(
|
|
650
651
|
e.signal.reason || new (this.#t.DOMException || globalThis.DOMException)(
|
|
651
652
|
"Aborted",
|
|
652
653
|
"AbortError"
|
|
@@ -655,9 +656,9 @@ class d extends EventTarget {
|
|
|
655
656
|
},
|
|
656
657
|
{ once: !0 }
|
|
657
658
|
);
|
|
658
|
-
}),
|
|
659
|
-
const
|
|
660
|
-
if (
|
|
659
|
+
}), a = (async () => {
|
|
660
|
+
const l = this.#u(n);
|
|
661
|
+
if (l === "QuotaExceededError") {
|
|
661
662
|
const f = this.#t && this.#t.QuotaExceededError || this.#t && this.#t.DOMException || globalThis.QuotaExceededError || globalThis.DOMException, h = new f(
|
|
662
663
|
"The prompt is too large, it exceeds the quota.",
|
|
663
664
|
"QuotaExceededError"
|
|
@@ -665,15 +666,15 @@ class d extends EventTarget {
|
|
|
665
666
|
Object.defineProperty(h, "code", { value: 22, configurable: !0 });
|
|
666
667
|
const E = 1e7;
|
|
667
668
|
throw h.requested = E, h.quota = this.contextWindow, h;
|
|
668
|
-
} else if (
|
|
669
|
+
} else if (l === "contextoverflow")
|
|
669
670
|
return this.dispatchEvent(new Event("contextoverflow")), "Mock response for quota overflow test.";
|
|
670
|
-
const
|
|
671
|
-
this.#
|
|
671
|
+
const c = [...this.#e, o];
|
|
672
|
+
this.#n.systemInstruction && c.unshift({
|
|
672
673
|
role: "system",
|
|
673
|
-
parts: [{ text: this.#
|
|
674
|
+
parts: [{ text: this.#n.systemInstruction }]
|
|
674
675
|
});
|
|
675
676
|
const p = await this.#o.countTokens(
|
|
676
|
-
|
|
677
|
+
c
|
|
677
678
|
);
|
|
678
679
|
if (p > this.contextWindow) {
|
|
679
680
|
const f = this.#t && this.#t.QuotaExceededError || this.#t && this.#t.DOMException || globalThis.QuotaExceededError || globalThis.DOMException, h = new f(
|
|
@@ -688,24 +689,24 @@ class d extends EventTarget {
|
|
|
688
689
|
try {
|
|
689
690
|
m = await this.#o.generateContent(w);
|
|
690
691
|
} catch (f) {
|
|
691
|
-
throw this.#b(f,
|
|
692
|
+
throw this.#b(f, n), f;
|
|
692
693
|
}
|
|
693
694
|
const { text: b, usage: y } = m;
|
|
694
695
|
let u = b;
|
|
695
|
-
if (
|
|
696
|
+
if (r) {
|
|
696
697
|
const f = u.match(/^\s*{\s*"Rating"\s*:\s*/);
|
|
697
698
|
f && (u = u.slice(f[0].length));
|
|
698
699
|
}
|
|
699
|
-
return y && (this.#i = y), !this.#
|
|
700
|
+
return y && (this.#i = y), !this.#r && this.#e && (this.#e.push(o), this.#e.push({ role: "model", parts: [{ text: u }] })), u;
|
|
700
701
|
})();
|
|
701
702
|
try {
|
|
702
|
-
return await Promise.race([
|
|
703
|
-
} catch (
|
|
704
|
-
throw
|
|
703
|
+
return await Promise.race([a, i]);
|
|
704
|
+
} catch (l) {
|
|
705
|
+
throw l.name === "AbortError" || console.error("Prompt API Polyfill Error:", l), l;
|
|
705
706
|
}
|
|
706
707
|
}
|
|
707
708
|
promptStreaming(t, e = {}) {
|
|
708
|
-
if (this.#s(), this.#
|
|
709
|
+
if (this.#s(), this.#r)
|
|
709
710
|
throw new (this.#t.DOMException || globalThis.DOMException)(
|
|
710
711
|
"Session is destroyed",
|
|
711
712
|
"InvalidStateError"
|
|
@@ -721,49 +722,49 @@ class d extends EventTarget {
|
|
|
721
722
|
o.enqueue("[object Object]"), o.close();
|
|
722
723
|
}
|
|
723
724
|
});
|
|
724
|
-
const
|
|
725
|
+
const r = this, n = e.signal;
|
|
725
726
|
return new ReadableStream({
|
|
726
727
|
async start(o) {
|
|
727
728
|
let i = !1;
|
|
728
|
-
const
|
|
729
|
+
const a = () => {
|
|
729
730
|
i = !0;
|
|
730
731
|
try {
|
|
731
|
-
const
|
|
732
|
+
const l = n?.reason || new (r.#t.DOMException || globalThis.DOMException)(
|
|
732
733
|
"Aborted",
|
|
733
734
|
"AbortError"
|
|
734
735
|
);
|
|
735
|
-
o.error(
|
|
736
|
+
o.error(l);
|
|
736
737
|
} catch {
|
|
737
738
|
}
|
|
738
739
|
};
|
|
739
|
-
if (
|
|
740
|
-
|
|
740
|
+
if (n?.aborted) {
|
|
741
|
+
a();
|
|
741
742
|
return;
|
|
742
743
|
}
|
|
743
|
-
|
|
744
|
+
n && n.addEventListener("abort", a);
|
|
744
745
|
try {
|
|
745
746
|
if (e.responseConstraint) {
|
|
746
747
|
d.#g(
|
|
747
748
|
e.responseConstraint,
|
|
748
|
-
|
|
749
|
+
r.#t
|
|
749
750
|
);
|
|
750
751
|
const g = O(
|
|
751
752
|
e.responseConstraint
|
|
752
753
|
);
|
|
753
|
-
n
|
|
754
|
-
|
|
755
|
-
n
|
|
754
|
+
r.#n.generationConfig.responseMimeType = "application/json", r.#n.generationConfig.responseSchema = g, r.#f = r.#o.createSession(
|
|
755
|
+
r.#a,
|
|
756
|
+
r.#n
|
|
756
757
|
);
|
|
757
758
|
}
|
|
758
|
-
const
|
|
759
|
-
if (
|
|
760
|
-
throw new (
|
|
759
|
+
const l = r.#m(t), c = await r.#l(t);
|
|
760
|
+
if (r.#r)
|
|
761
|
+
throw new (r.#t.DOMException || globalThis.DOMException)(
|
|
761
762
|
"Session is destroyed",
|
|
762
763
|
"InvalidStateError"
|
|
763
764
|
);
|
|
764
|
-
const p = { role: "user", parts:
|
|
765
|
+
const p = { role: "user", parts: c }, w = r.#u(c);
|
|
765
766
|
if (w === "QuotaExceededError") {
|
|
766
|
-
const g =
|
|
767
|
+
const g = r.#t && r.#t.QuotaExceededError || r.#t && r.#t.DOMException || globalThis.QuotaExceededError || globalThis.DOMException, x = new g(
|
|
767
768
|
"The prompt is too large, it exceeds the quota.",
|
|
768
769
|
"QuotaExceededError"
|
|
769
770
|
);
|
|
@@ -772,36 +773,36 @@ class d extends EventTarget {
|
|
|
772
773
|
configurable: !0
|
|
773
774
|
});
|
|
774
775
|
const v = 1e7;
|
|
775
|
-
throw x.requested = v, x.quota =
|
|
776
|
+
throw x.requested = v, x.quota = r.contextWindow, x;
|
|
776
777
|
} else if (w === "contextoverflow") {
|
|
777
|
-
|
|
778
|
+
r.dispatchEvent(new Event("contextoverflow")), o.enqueue("Mock response for quota overflow test."), o.close();
|
|
778
779
|
return;
|
|
779
780
|
}
|
|
780
|
-
const m = [...
|
|
781
|
-
n
|
|
781
|
+
const m = [...r.#e, p];
|
|
782
|
+
r.#n.systemInstruction && m.unshift({
|
|
782
783
|
role: "system",
|
|
783
|
-
parts: [{ text: n
|
|
784
|
+
parts: [{ text: r.#n.systemInstruction }]
|
|
784
785
|
});
|
|
785
|
-
const b = await
|
|
786
|
+
const b = await r.#o.countTokens(
|
|
786
787
|
m
|
|
787
788
|
);
|
|
788
|
-
if (b >
|
|
789
|
-
const g =
|
|
790
|
-
`The prompt is too large (${b} tokens), it exceeds the quota of ${
|
|
789
|
+
if (b > r.contextWindow) {
|
|
790
|
+
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 (${b} tokens), it exceeds the quota of ${r.contextWindow} tokens.`,
|
|
791
792
|
"QuotaExceededError"
|
|
792
793
|
);
|
|
793
794
|
throw Object.defineProperty(x, "code", {
|
|
794
795
|
value: 22,
|
|
795
796
|
configurable: !0
|
|
796
|
-
}), x.requested = b, x.quota =
|
|
797
|
+
}), x.requested = b, x.quota = r.contextWindow, x;
|
|
797
798
|
}
|
|
798
|
-
b >
|
|
799
|
-
const y = [...
|
|
799
|
+
b > r.contextWindow && r.dispatchEvent(new Event("contextoverflow"));
|
|
800
|
+
const y = [...r.#e, p];
|
|
800
801
|
let u;
|
|
801
802
|
try {
|
|
802
|
-
u = await
|
|
803
|
+
u = await r.#o.generateContentStream(y);
|
|
803
804
|
} catch (g) {
|
|
804
|
-
throw
|
|
805
|
+
throw r.#b(g, c), g;
|
|
805
806
|
}
|
|
806
807
|
let f = "", h = !1, E = "";
|
|
807
808
|
for await (const g of u) {
|
|
@@ -810,7 +811,7 @@ class d extends EventTarget {
|
|
|
810
811
|
return;
|
|
811
812
|
}
|
|
812
813
|
let x = g.text();
|
|
813
|
-
if (
|
|
814
|
+
if (l && !h) {
|
|
814
815
|
E += x;
|
|
815
816
|
const v = E.match(/^\s*{\s*"Rating"\s*:\s*/);
|
|
816
817
|
if (v)
|
|
@@ -820,22 +821,22 @@ class d extends EventTarget {
|
|
|
820
821
|
else
|
|
821
822
|
continue;
|
|
822
823
|
}
|
|
823
|
-
f += x, g.usageMetadata?.totalTokenCount && (
|
|
824
|
+
f += x, g.usageMetadata?.totalTokenCount && (r.#i = g.usageMetadata.totalTokenCount), o.enqueue(x);
|
|
824
825
|
}
|
|
825
|
-
!i && !
|
|
826
|
+
!i && !r.#r && r.#e && (r.#e.push(p), r.#e.push({
|
|
826
827
|
role: "model",
|
|
827
828
|
parts: [{ text: f }]
|
|
828
829
|
})), o.close();
|
|
829
|
-
} catch (
|
|
830
|
-
i || o.error(
|
|
830
|
+
} catch (l) {
|
|
831
|
+
i || o.error(l);
|
|
831
832
|
} finally {
|
|
832
|
-
|
|
833
|
+
n && n.removeEventListener("abort", a);
|
|
833
834
|
}
|
|
834
835
|
}
|
|
835
836
|
});
|
|
836
837
|
}
|
|
837
838
|
async append(t, e = {}) {
|
|
838
|
-
if (this.#s(), this.#
|
|
839
|
+
if (this.#s(), this.#r)
|
|
839
840
|
throw new (this.#t.DOMException || globalThis.DOMException)(
|
|
840
841
|
"Session is destroyed",
|
|
841
842
|
"InvalidStateError"
|
|
@@ -845,19 +846,19 @@ class d extends EventTarget {
|
|
|
845
846
|
"Aborted",
|
|
846
847
|
"AbortError"
|
|
847
848
|
);
|
|
848
|
-
const
|
|
849
|
-
if (this.#
|
|
849
|
+
const r = await this.#l(t);
|
|
850
|
+
if (this.#r)
|
|
850
851
|
throw new (this.#t.DOMException || globalThis.DOMException)(
|
|
851
852
|
"Session is destroyed",
|
|
852
853
|
"InvalidStateError"
|
|
853
854
|
);
|
|
854
|
-
const
|
|
855
|
-
this.#e.push(
|
|
855
|
+
const n = { role: "user", parts: r };
|
|
856
|
+
this.#e.push(n);
|
|
856
857
|
try {
|
|
857
858
|
const o = [...this.#e];
|
|
858
|
-
this.#
|
|
859
|
+
this.#n.systemInstruction && o.unshift({
|
|
859
860
|
role: "system",
|
|
860
|
-
parts: [{ text: this.#
|
|
861
|
+
parts: [{ text: this.#n.systemInstruction }]
|
|
861
862
|
});
|
|
862
863
|
const i = await this.#o.countTokens(o);
|
|
863
864
|
this.#i = i || 0;
|
|
@@ -866,20 +867,20 @@ class d extends EventTarget {
|
|
|
866
867
|
this.#i > this.contextWindow && this.dispatchEvent(new Event("contextoverflow"));
|
|
867
868
|
}
|
|
868
869
|
async measureContextUsage(t) {
|
|
869
|
-
if (this.#s(), this.#
|
|
870
|
+
if (this.#s(), this.#r)
|
|
870
871
|
throw new (this.#t.DOMException || globalThis.DOMException)(
|
|
871
872
|
"Session is destroyed",
|
|
872
873
|
"InvalidStateError"
|
|
873
874
|
);
|
|
874
875
|
try {
|
|
875
|
-
const e = await this.#
|
|
876
|
-
if (this.#
|
|
876
|
+
const e = await this.#l(t);
|
|
877
|
+
if (this.#r)
|
|
877
878
|
throw new (this.#t.DOMException || globalThis.DOMException)(
|
|
878
879
|
"Session is destroyed",
|
|
879
880
|
"InvalidStateError"
|
|
880
881
|
);
|
|
881
|
-
const
|
|
882
|
-
return
|
|
882
|
+
const r = this.#u(e);
|
|
883
|
+
return r === "QuotaExceededError" ? 1e7 : r === "contextoverflow" ? 5e5 : await this.#o.countTokens([
|
|
883
884
|
{ role: "user", parts: e }
|
|
884
885
|
]) || 0;
|
|
885
886
|
} catch {
|
|
@@ -918,8 +919,8 @@ class d extends EventTarget {
|
|
|
918
919
|
return null;
|
|
919
920
|
}
|
|
920
921
|
// Private Helper to process diverse input types
|
|
921
|
-
async #
|
|
922
|
-
const e = this.#a.expectedInputs ? ["text", ...this.#a.expectedInputs.map((
|
|
922
|
+
async #l(t) {
|
|
923
|
+
const e = this.#a.expectedInputs ? ["text", ...this.#a.expectedInputs.map((n) => n.type)] : ["text"];
|
|
923
924
|
if (typeof t == "string") {
|
|
924
925
|
if (!e.includes("text"))
|
|
925
926
|
throw new (this.#t.DOMException || globalThis.DOMException)(
|
|
@@ -932,7 +933,7 @@ class d extends EventTarget {
|
|
|
932
933
|
if (t.length === 0)
|
|
933
934
|
return [{ text: " " }];
|
|
934
935
|
if (t.length > 0 && t[0].role) {
|
|
935
|
-
let
|
|
936
|
+
let n = [];
|
|
936
937
|
for (const o of t) {
|
|
937
938
|
const i = o.role === "assistant" || o.role === "model";
|
|
938
939
|
if (typeof o.content == "string") {
|
|
@@ -941,66 +942,66 @@ class d extends EventTarget {
|
|
|
941
942
|
'The content type "text" is not in the expectedInputs.',
|
|
942
943
|
"NotSupportedError"
|
|
943
944
|
);
|
|
944
|
-
|
|
945
|
+
n.push({ text: o.content }), o.prefix && console.warn(
|
|
945
946
|
"The `prefix` flag isn't supported and was ignored."
|
|
946
947
|
);
|
|
947
948
|
} else if (Array.isArray(o.content))
|
|
948
|
-
for (const
|
|
949
|
-
const
|
|
950
|
-
if (!e.includes(
|
|
949
|
+
for (const a of o.content) {
|
|
950
|
+
const l = a.type || "text";
|
|
951
|
+
if (!e.includes(l))
|
|
951
952
|
throw new (this.#t.DOMException || globalThis.DOMException)(
|
|
952
|
-
`The content type "${
|
|
953
|
+
`The content type "${l}" is not in the expectedInputs.`,
|
|
953
954
|
"NotSupportedError"
|
|
954
955
|
);
|
|
955
|
-
if (
|
|
956
|
-
if (typeof
|
|
956
|
+
if (l === "text") {
|
|
957
|
+
if (typeof a.value != "string")
|
|
957
958
|
throw new (this.#t.DOMException || globalThis.DOMException)(
|
|
958
959
|
'The content type "text" must have a string value.',
|
|
959
960
|
"SyntaxError"
|
|
960
961
|
);
|
|
961
|
-
|
|
962
|
+
n.push({ text: a.value });
|
|
962
963
|
} else {
|
|
963
964
|
if (i)
|
|
964
965
|
throw new (this.#t.DOMException || globalThis.DOMException)(
|
|
965
966
|
"Assistant messages only support text content.",
|
|
966
967
|
"NotSupportedError"
|
|
967
968
|
);
|
|
968
|
-
const
|
|
969
|
-
|
|
969
|
+
const c = a.value && a.value.inlineData ? a.value : await M.convert(a.type, a.value);
|
|
970
|
+
n.push(c);
|
|
970
971
|
}
|
|
971
972
|
}
|
|
972
973
|
}
|
|
973
|
-
return
|
|
974
|
+
return n;
|
|
974
975
|
}
|
|
975
976
|
return Promise.all(
|
|
976
|
-
t.map(async (
|
|
977
|
-
if (typeof
|
|
977
|
+
t.map(async (n) => {
|
|
978
|
+
if (typeof n == "string") {
|
|
978
979
|
if (!e.includes("text"))
|
|
979
980
|
throw new (this.#t.DOMException || globalThis.DOMException)(
|
|
980
981
|
'The content type "text" is not in the expectedInputs.',
|
|
981
982
|
"NotSupportedError"
|
|
982
983
|
);
|
|
983
|
-
return { text:
|
|
984
|
+
return { text: n === "" ? " " : n };
|
|
984
985
|
}
|
|
985
|
-
if (typeof
|
|
986
|
-
if (
|
|
987
|
-
return
|
|
988
|
-
if (
|
|
989
|
-
const o =
|
|
986
|
+
if (typeof n == "object" && n !== null) {
|
|
987
|
+
if (n.inlineData)
|
|
988
|
+
return n;
|
|
989
|
+
if (n.type && n.value) {
|
|
990
|
+
const o = n.type || "text";
|
|
990
991
|
if (!e.includes(o))
|
|
991
992
|
throw new (this.#t.DOMException || globalThis.DOMException)(
|
|
992
993
|
`The content type "${o}" is not in the expectedInputs.`,
|
|
993
994
|
"NotSupportedError"
|
|
994
995
|
);
|
|
995
996
|
if (o === "text") {
|
|
996
|
-
if (typeof
|
|
997
|
+
if (typeof n.value != "string")
|
|
997
998
|
throw new (this.#t.DOMException || globalThis.DOMException)(
|
|
998
999
|
'The content type "text" must have a string value.',
|
|
999
1000
|
"SyntaxError"
|
|
1000
1001
|
);
|
|
1001
|
-
return { text:
|
|
1002
|
+
return { text: n.value };
|
|
1002
1003
|
}
|
|
1003
|
-
return
|
|
1004
|
+
return n.value && n.value.inlineData ? n.value : await M.convert(n.type, n.value);
|
|
1004
1005
|
}
|
|
1005
1006
|
}
|
|
1006
1007
|
if (!e.includes("text"))
|
|
@@ -1008,7 +1009,7 @@ class d extends EventTarget {
|
|
|
1008
1009
|
'The content type "text" is not in the expectedInputs.',
|
|
1009
1010
|
"NotSupportedError"
|
|
1010
1011
|
);
|
|
1011
|
-
return { text: String(
|
|
1012
|
+
return { text: String(n) };
|
|
1012
1013
|
})
|
|
1013
1014
|
);
|
|
1014
1015
|
}
|
|
@@ -1021,14 +1022,14 @@ class d extends EventTarget {
|
|
|
1021
1022
|
}
|
|
1022
1023
|
// Map backend errors to WPT expectations
|
|
1023
1024
|
#b(t, e) {
|
|
1024
|
-
const
|
|
1025
|
-
if (
|
|
1026
|
-
const
|
|
1027
|
-
(
|
|
1025
|
+
const r = String(t.message || t);
|
|
1026
|
+
if (r.includes("400") || r.toLowerCase().includes("unable to process") || r.toLowerCase().includes("invalid")) {
|
|
1027
|
+
const n = e.some(
|
|
1028
|
+
(a) => a.inlineData?.mimeType.startsWith("audio/")
|
|
1028
1029
|
), o = e.some(
|
|
1029
|
-
(
|
|
1030
|
+
(a) => a.inlineData?.mimeType.startsWith("image/")
|
|
1030
1031
|
), i = this.#t.DOMException || globalThis.DOMException;
|
|
1031
|
-
if (
|
|
1032
|
+
if (n)
|
|
1032
1033
|
throw new i("Invalid audio data", "DataError");
|
|
1033
1034
|
if (o)
|
|
1034
1035
|
throw new i("Invalid image data", "InvalidStateError");
|
|
@@ -1036,33 +1037,33 @@ class d extends EventTarget {
|
|
|
1036
1037
|
}
|
|
1037
1038
|
}
|
|
1038
1039
|
globalThis.DOMException && (globalThis.QuotaExceededError = globalThis.DOMException);
|
|
1039
|
-
const D = (
|
|
1040
|
+
const D = (s) => {
|
|
1040
1041
|
try {
|
|
1041
|
-
if (!
|
|
1042
|
+
if (!s || s.LanguageModel?.__isPolyfill)
|
|
1042
1043
|
return;
|
|
1043
1044
|
const t = class extends d {
|
|
1044
1045
|
};
|
|
1045
|
-
t.__window =
|
|
1046
|
+
t.__window = s, t.__isPolyfill = !0, s.LanguageModel = t, s.DOMException && (s.QuotaExceededError = s.DOMException);
|
|
1046
1047
|
} catch {
|
|
1047
1048
|
}
|
|
1048
1049
|
};
|
|
1049
1050
|
if (typeof HTMLIFrameElement < "u")
|
|
1050
1051
|
try {
|
|
1051
|
-
const
|
|
1052
|
+
const s = Object.getOwnPropertyDescriptor(
|
|
1052
1053
|
HTMLIFrameElement.prototype,
|
|
1053
1054
|
"contentWindow"
|
|
1054
1055
|
);
|
|
1055
|
-
|
|
1056
|
+
s && s.get && Object.defineProperty(HTMLIFrameElement.prototype, "contentWindow", {
|
|
1056
1057
|
get() {
|
|
1057
|
-
const t =
|
|
1058
|
+
const t = s.get.call(this);
|
|
1058
1059
|
return t && D(t), t;
|
|
1059
1060
|
},
|
|
1060
1061
|
configurable: !0
|
|
1061
1062
|
});
|
|
1062
1063
|
} catch {
|
|
1063
1064
|
}
|
|
1064
|
-
const P = new MutationObserver((
|
|
1065
|
-
for (const t of
|
|
1065
|
+
const P = new MutationObserver((s) => {
|
|
1066
|
+
for (const t of s)
|
|
1066
1067
|
for (const e of t.addedNodes)
|
|
1067
1068
|
e.tagName === "IFRAME" && (D(e.contentWindow), e.addEventListener("load", () => D(e.contentWindow), {
|
|
1068
1069
|
once: !1
|
|
@@ -1071,8 +1072,8 @@ const P = new MutationObserver((a) => {
|
|
|
1071
1072
|
globalThis.document?.documentElement && (P.observe(globalThis.document.documentElement, {
|
|
1072
1073
|
childList: !0,
|
|
1073
1074
|
subtree: !0
|
|
1074
|
-
}), globalThis.document.querySelectorAll("iframe").forEach((
|
|
1075
|
-
D(
|
|
1075
|
+
}), globalThis.document.querySelectorAll("iframe").forEach((s) => {
|
|
1076
|
+
D(s.contentWindow);
|
|
1076
1077
|
}));
|
|
1077
1078
|
(!("LanguageModel" in globalThis) || globalThis.__FORCE_PROMPT_API_POLYFILL__) && (globalThis.LanguageModel = d, d.__isPolyfill = !0, console.log(
|
|
1078
1079
|
"Polyfill: window.LanguageModel is now backed by the Prompt API polyfill."
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "prompt-api-polyfill",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.8.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",
|