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