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.
@@ -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 s = this.getReader();
3
+ const a = this.getReader();
4
4
  try {
5
5
  for (; ; ) {
6
- const { done: t, value: e } = await s.read();
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
- s.releaseLock();
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), o = e.buffer.slice(
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(o), n = this.#o(e);
34
- if (!n)
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: n } };
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 o = String.fromCharCode(...t.slice(0, 100)).toLowerCase();
71
- return o.includes("<svg") || o.includes("<?xml") ? "image/svg+xml" : null;
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", o = ArrayBuffer.isView(t) || t && t.buffer && (t.buffer instanceof ArrayBuffer || t.buffer.constructor.name === "ArrayBuffer");
84
- if (e || o) {
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, o) => {
98
+ return new Promise((e, n) => {
99
99
  const r = new FileReader();
100
100
  r.onloadend = () => {
101
- r.error ? o(r.error) : e({
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 = (a) => {
114
- const l = t[a];
115
- return typeof l == "object" && l !== null && "baseVal" in l ? l.baseVal.value : typeof l == "number" ? l : 0;
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 o = t.displayWidth || t.naturalWidth || t.videoWidth || e("width"), r = t.displayHeight || t.naturalHeight || t.videoHeight || e("height");
118
- if ((!o || !r) && typeof t.getBBox == "function")
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 a = t.getBBox();
121
- o = o || a.width, r = r || a.height;
124
+ const l = t.getBBox();
125
+ n = n || l.width, r = r || l.height;
122
126
  } catch {
123
127
  }
124
- if ((!o || !r) && typeof t.getBoundingClientRect == "function")
128
+ if ((!n || !r) && typeof t.getBoundingClientRect == "function")
125
129
  try {
126
- const a = t.getBoundingClientRect();
127
- o = o || a.width, r = r || a.height;
130
+ const l = t.getBoundingClientRect();
131
+ n = n || l.width, r = r || l.height;
128
132
  } catch {
129
133
  }
130
- if (!o || !r)
131
- throw new DOMException("Invalid image dimensions", "InvalidStateError");
132
- const n = document.createElement("canvas");
133
- n.width = o, n.height = r;
134
- const i = n.getContext("2d");
135
- return typeof ImageData < "u" && t instanceof ImageData ? i.putImageData(t, 0, 0) : i.drawImage(t, 0, 0), {
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: n.toDataURL("image/png").split(",")[1],
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 o = new Uint8Array(t), r = o.byteLength;
145
- for (let n = 0; n < r; n++)
146
- e += String.fromCharCode(o[n]);
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, o = t.sampleRate, r = 1, n = 16;
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, o, e, n);
165
+ ) : i = t.getChannelData(0), this.encodeWAV(i, r, n, e, o);
157
166
  }
158
167
  static interleave(t, e) {
159
- const o = t.length + e.length, r = new Float32Array(o);
160
- let n = 0, i = 0;
161
- for (; n < o; )
162
- r[n++] = t[i], r[n++] = e[i], i++;
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, o, r, n) {
166
- const i = n / 8, c = r * i, a = new ArrayBuffer(44 + t.length * i), l = new DataView(a);
167
- return this.writeString(l, 0, "RIFF"), l.setUint32(4, 36 + t.length * i, !0), this.writeString(l, 8, "WAVE"), this.writeString(l, 12, "fmt "), l.setUint32(16, 16, !0), l.setUint16(20, e, !0), l.setUint16(22, r, !0), l.setUint32(24, o, !0), l.setUint32(28, o * c, !0), l.setUint16(32, c, !0), l.setUint16(34, n, !0), this.writeString(l, 36, "data"), l.setUint32(40, t.length * i, !0), this.floatTo16BitPCM(l, 44, t), a;
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, o) {
170
- for (let r = 0; r < o.length; r++, e += 2) {
171
- const n = Math.max(-1, Math.min(1, o[r]));
172
- t.setInt16(e, n < 0 ? n * 32768 : n * 32767, !0);
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, o) {
176
- for (let r = 0; r < o.length; r++)
177
- t.setUint8(e + r, o.charCodeAt(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 v(s) {
181
- if (!s)
189
+ function O(a) {
190
+ if (!a)
182
191
  return;
183
192
  const t = {
184
- description: s.description,
185
- nullable: s.nullable || !1,
186
- format: s.format
193
+ description: a.description,
194
+ nullable: a.nullable || !1,
195
+ format: a.format
187
196
  };
188
- switch (Array.isArray(s.type) && s.type.includes("null") && (t.nullable = !0, s.type = s.type.find((e) => e !== "null")), s.type) {
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 s.enum && Array.isArray(s.enum) ? T.enumString({
199
+ return a.enum && Array.isArray(a.enum) ? T.enumString({
191
200
  ...t,
192
- enum: s.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: v(s.items)
213
+ items: O(a.items)
205
214
  });
206
215
  case "object":
207
- const e = {}, o = s.properties ? Object.keys(s.properties) : [];
208
- o.forEach((i) => {
209
- e[i] = v(
210
- s.properties[i]
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 = s.required || [], n = o.filter(
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: n
228
+ optionalProperties: o
220
229
  });
221
230
  default:
222
231
  return console.warn(
223
- `Unsupported type: ${s.type}, defaulting to string.`
232
+ `Unsupported type: ${a.type}, defaulting to string.`
224
233
  ), T.string(t);
225
234
  }
226
235
  }
227
- const A = [
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 I(s) {
246
- if (s === "./backends/firebase.js")
254
+ async function A(a) {
255
+ if (a === "./backends/firebase.js")
247
256
  return (await import("./backends/firebase.js")).default;
248
- if (s === "./backends/gemini.js")
257
+ if (a === "./backends/gemini.js")
249
258
  return (await import("./backends/gemini.js")).default;
250
- if (s === "./backends/openai.js")
259
+ if (a === "./backends/openai.js")
251
260
  return (await import("./backends/openai.js")).default;
252
- if (s === "./backends/transformers.js")
261
+ if (a === "./backends/transformers.js")
253
262
  return (await import("./backends/transformers.js")).default;
254
- throw new Error(`Unknown backend path "${s}"`);
263
+ throw new Error(`Unknown backend path "${a}"`);
255
264
  }
256
- async function C(s, t = globalThis) {
265
+ async function S(a, t = globalThis) {
257
266
  const e = [];
258
- for (const o of s) {
259
- const r = o.role === "assistant" ? "model" : "user", n = r === "model";
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(o.content))
262
- for (const c of o.content)
263
- if (c.type === "text") {
264
- const a = c.value || c.text || "";
265
- if (typeof a != "string")
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: a });
279
+ i.push({ text: c });
271
280
  } else {
272
- if (n)
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 a = await M.convert(c.type, c.value);
278
- i.push(a);
286
+ const c = await M.convert(s.type, s.value);
287
+ i.push(c);
279
288
  }
280
289
  else
281
- i.push({ text: o.content });
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
- #c;
303
+ #l;
295
304
  #t;
296
- constructor(t, e, o, r = {}, n, i = 0, c = globalThis) {
297
- super(), this.#o = t, this.#f = e, this.#r = o || [], this.#s = r, this.#e = n, this.#n = !1, this.#i = i, this.#c = {}, this.#t = c;
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.#c;
315
+ return this.#l;
307
316
  }
308
317
  set oncontextoverflow(t) {
309
- this.#c && this.removeEventListener("contextoverflow", this.#c), this.#c = t, typeof t == "function" && this.addEventListener("contextoverflow", t);
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 o = t?.DOMException || globalThis.DOMException;
319
- throw new o(
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
- #a() {
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 #g = A;
359
+ static #x = I;
351
360
  static #d(t = globalThis) {
352
- for (const o of d.#g) {
353
- const r = t[o.config] || globalThis[o.config];
361
+ for (const n of d.#x) {
362
+ const r = t[n.config] || globalThis[n.config];
354
363
  if (r && r.apiKey)
355
- return { ...o, configValue: r };
364
+ return { ...n, configValue: r };
356
365
  }
357
- const e = d.#g.map((o) => `window.${o.config}`).join(", ");
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 I(e.path);
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 o = t.expectedInputs ? ["text", ...t.expectedInputs.map((r) => r.type)] : ["text"];
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 n = 0; n < t.initialPrompts.length; n++) {
384
- const i = t.initialPrompts[n];
392
+ for (let o = 0; o < t.initialPrompts.length; o++) {
393
+ const i = t.initialPrompts[o];
385
394
  if (i.role === "system") {
386
- if (n !== 0)
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 c of i.content) {
398
- const a = c.type || "text";
399
- if (!o.includes(a))
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 "${a}" is not in the expectedInputs.`,
410
+ `The content type "${c}" is not in the expectedInputs.`,
402
411
  "NotSupportedError"
403
412
  );
404
413
  }
405
- else if (!o.includes("text"))
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 o = await this.availability(t);
440
- if (o === "unavailable")
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 (o === "downloadable" || o === "downloading")
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), n = await d.#p(e), i = new n(r.configValue), c = { ...t };
456
- d.#x(
457
- c.responseConstraint,
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 a = {
469
+ const c = {
461
470
  model: i.modelName,
462
471
  generationConfig: {}
463
472
  };
464
- let l = [], y = 0;
465
- if (c.initialPrompts && Array.isArray(c.initialPrompts)) {
466
- const w = c.initialPrompts.filter(
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
- ), p = c.initialPrompts.filter(
477
+ ), u = s.initialPrompts.filter(
469
478
  (f) => f.role !== "system"
470
479
  );
471
- w.length > 0 && (a.systemInstruction = w.map((f) => typeof f.content == "string" ? f.content : Array.isArray(f.content) ? f.content.filter((h) => h.type === "text").map((h) => h.value || h.text || "").join(`
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 C(p, e);
474
- for (const f of c.initialPrompts) {
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, u = new E(
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(u, "code", {
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 u.requested = x, u.quota = 1e6, u;
499
+ throw g.requested = x, g.quota = 1e6, g;
491
500
  }
492
501
  }
493
502
  }
494
- let g = null;
495
- if (typeof c.monitor == "function") {
496
- g = new EventTarget();
503
+ let w = null;
504
+ if (typeof s.monitor == "function") {
505
+ w = new EventTarget();
497
506
  try {
498
- c.monitor(g);
499
- } catch (w) {
500
- throw w;
507
+ s.monitor(w);
508
+ } catch (y) {
509
+ throw y;
501
510
  }
502
511
  }
503
- g && (g.__lastProgressLoaded = -1);
504
- const b = async (w) => {
505
- if (!g || t.signal?.aborted)
512
+ w && (w.__lastProgressLoaded = -1);
513
+ const m = async (y) => {
514
+ if (!w || t.signal?.aborted)
506
515
  return !t.signal?.aborted;
507
- const p = 1 / 65536, f = Math.floor(w / p) * p;
508
- if (f <= g.__lastProgressLoaded)
516
+ const u = 1 / 65536, f = Math.floor(y / u) * u;
517
+ if (f <= w.__lastProgressLoaded)
509
518
  return !0;
510
519
  try {
511
- g.dispatchEvent(
520
+ w.dispatchEvent(
512
521
  new ProgressEvent("downloadprogress", {
513
522
  loaded: f,
514
523
  total: 1,
515
524
  lengthComputable: !0
516
525
  })
517
- ), g.__lastProgressLoaded = f;
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 b(0))
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 m = await i.createSession(
537
+ const b = await i.createSession(
538
+ s,
529
539
  c,
530
- a,
531
- g
540
+ w
532
541
  );
533
- if (!await b(1))
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 (c.initialPrompts?.length > 0) {
539
- const w = [...l];
540
- if (a.systemInstruction && w.unshift({
547
+ if (s.initialPrompts?.length > 0) {
548
+ const y = [...l];
549
+ if (c.systemInstruction && y.unshift({
541
550
  role: "system",
542
- parts: [{ text: a.systemInstruction }]
543
- }), y = await i.countTokens(w) || 0, y > 1e6) {
544
- const p = e.QuotaExceededError || e.DOMException || globalThis.QuotaExceededError || globalThis.DOMException, f = new p(
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 = y, f.quota = 1e6, f;
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
- m,
562
+ b,
554
563
  l,
564
+ s,
555
565
  c,
556
- a,
557
- y,
566
+ p,
558
567
  e
559
568
  );
560
569
  }
561
570
  // Instance Methods
562
571
  async clone(t = {}) {
563
- if (this.#a(), this.#n)
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.#r)), o = { ...this.#s, ...t }, r = await d.#p(this.#t), n = d.#d(this.#t), i = new r(n.configValue), c = await i.createSession(
574
- o,
575
- this.#e
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
- c,
593
+ s,
585
594
  e,
586
- o,
587
- this.#e,
595
+ n,
596
+ this.#r,
588
597
  this.#i,
589
598
  this.#t
590
599
  );
591
600
  }
592
601
  destroy() {
593
- this.#a(), this.#n = !0, this.#r = null;
602
+ this.#s(), this.#n = !0, this.#e = null;
594
603
  }
595
604
  async prompt(t, e = {}) {
596
- if (this.#a(), this.#n)
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.#x(
618
+ d.#g(
610
619
  e.responseConstraint,
611
620
  this.#t
612
621
  );
613
- const a = v(
622
+ const c = O(
614
623
  e.responseConstraint
615
624
  );
616
- this.#e.generationConfig.responseMimeType = "application/json", this.#e.generationConfig.responseSchema = a, this.#f = this.#o.createSession(
617
- this.#s,
618
- this.#e
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 o = this.#b(t), r = await this.#l(t);
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 n = { role: "user", parts: r }, i = new Promise((a, l) => {
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
- }), c = (async () => {
650
- const a = this.#u(r);
651
- if (a === "QuotaExceededError") {
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 (a === "contextoverflow")
668
+ } else if (c === "contextoverflow")
660
669
  return this.dispatchEvent(new Event("contextoverflow")), "Mock response for quota overflow test.";
661
- const l = [...this.#r, n];
662
- this.#e.systemInstruction && l.unshift({
670
+ const l = [...this.#e, o];
671
+ this.#r.systemInstruction && l.unshift({
663
672
  role: "system",
664
- parts: [{ text: this.#e.systemInstruction }]
673
+ parts: [{ text: this.#r.systemInstruction }]
665
674
  });
666
- const y = await this.#o.countTokens(
675
+ const p = await this.#o.countTokens(
667
676
  l
668
677
  );
669
- if (y > this.contextWindow) {
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 (${y} tokens), it exceeds the quota of ${this.contextWindow} tokens.`,
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 = y, h.quota = this.contextWindow, h;
683
+ throw Object.defineProperty(h, "code", { value: 22, configurable: !0 }), h.requested = p, h.quota = this.contextWindow, h;
675
684
  }
676
- y > this.contextWindow && this.dispatchEvent(new Event("contextoverflow"));
677
- const g = [...this.#r, n];
678
- let b;
685
+ p > this.contextWindow && this.dispatchEvent(new Event("contextoverflow"));
686
+ const w = [...this.#e, o];
687
+ let m;
679
688
  try {
680
- b = await this.#o.generateContent(g);
689
+ m = await this.#o.generateContent(w);
681
690
  } catch (f) {
682
- throw this.#m(f, r), f;
691
+ throw this.#b(f, r), f;
683
692
  }
684
- const { text: m, usage: w } = b;
685
- let p = m;
686
- if (o) {
687
- const f = p.match(/^\s*{\s*"Rating"\s*:\s*/);
688
- f && (p = p.slice(f[0].length));
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 w && (this.#i = w), this.#r.push(n), this.#r.push({ role: "model", parts: [{ text: p }] }), p;
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([c, i]);
694
- } catch (a) {
695
- throw a.name === "AbortError" || console.error("Prompt API Polyfill Error:", a), a;
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.#a(), this.#n)
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(n) {
712
- n.enqueue("[object Object]"), n.close();
720
+ start(o) {
721
+ o.enqueue("[object Object]"), o.close();
713
722
  }
714
723
  });
715
- const o = this, r = e.signal;
724
+ const n = this, r = e.signal;
716
725
  return new ReadableStream({
717
- async start(n) {
726
+ async start(o) {
718
727
  let i = !1;
719
- const c = () => {
728
+ const s = () => {
720
729
  i = !0;
721
730
  try {
722
- const a = r?.reason || new (o.#t.DOMException || globalThis.DOMException)(
731
+ const c = r?.reason || new (n.#t.DOMException || globalThis.DOMException)(
723
732
  "Aborted",
724
733
  "AbortError"
725
734
  );
726
- n.error(a);
735
+ o.error(c);
727
736
  } catch {
728
737
  }
729
738
  };
730
739
  if (r?.aborted) {
731
- c();
740
+ s();
732
741
  return;
733
742
  }
734
- r && r.addEventListener("abort", c);
743
+ r && r.addEventListener("abort", s);
735
744
  try {
736
745
  if (e.responseConstraint) {
737
- d.#x(
746
+ d.#g(
738
747
  e.responseConstraint,
739
- o.#t
748
+ n.#t
740
749
  );
741
- const u = v(
750
+ const g = O(
742
751
  e.responseConstraint
743
752
  );
744
- o.#e.generationConfig.responseMimeType = "application/json", o.#e.generationConfig.responseSchema = u, o.#f = o.#o.createSession(
745
- o.#s,
746
- o.#e
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 a = o.#b(t), l = await o.#l(t);
750
- if (o.#n)
751
- throw new (o.#t.DOMException || globalThis.DOMException)(
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 y = { role: "user", parts: l }, g = o.#u(l);
756
- if (g === "QuotaExceededError") {
757
- const u = o.#t && o.#t.QuotaExceededError || o.#t && o.#t.DOMException || globalThis.QuotaExceededError || globalThis.DOMException, x = new u(
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 O = 1e7;
766
- throw x.requested = O, x.quota = o.contextWindow, x;
767
- } else if (g === "contextoverflow") {
768
- o.dispatchEvent(new Event("contextoverflow")), n.enqueue("Mock response for quota overflow test."), n.close();
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 b = [...o.#r, y];
772
- o.#e.systemInstruction && b.unshift({
780
+ const m = [...n.#e, p];
781
+ n.#r.systemInstruction && m.unshift({
773
782
  role: "system",
774
- parts: [{ text: o.#e.systemInstruction }]
783
+ parts: [{ text: n.#r.systemInstruction }]
775
784
  });
776
- const m = await o.#o.countTokens(
777
- b
785
+ const b = await n.#o.countTokens(
786
+ m
778
787
  );
779
- if (m > o.contextWindow) {
780
- const u = o.#t && o.#t.QuotaExceededError || o.#t && o.#t.DOMException || globalThis.QuotaExceededError || globalThis.DOMException, x = new u(
781
- `The prompt is too large (${m} tokens), it exceeds the quota of ${o.contextWindow} tokens.`,
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 = m, x.quota = o.contextWindow, x;
796
+ }), x.requested = b, x.quota = n.contextWindow, x;
788
797
  }
789
- m > o.contextWindow && o.dispatchEvent(new Event("contextoverflow"));
790
- const w = [...o.#r, y];
791
- let p;
798
+ b > n.contextWindow && n.dispatchEvent(new Event("contextoverflow"));
799
+ const y = [...n.#e, p];
800
+ let u;
792
801
  try {
793
- p = await o.#o.generateContentStream(w);
794
- } catch (u) {
795
- throw o.#m(u, l), u;
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 u of p) {
807
+ for await (const g of u) {
799
808
  if (i) {
800
- typeof p.return == "function" && await p.return();
809
+ typeof u.return == "function" && await u.return();
801
810
  return;
802
811
  }
803
- let x = u.text();
804
- if (a && !h) {
812
+ let x = g.text();
813
+ if (c && !h) {
805
814
  E += x;
806
- const O = E.match(/^\s*{\s*"Rating"\s*:\s*/);
807
- if (O)
808
- x = E.slice(O[0].length), h = !0, E = "";
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, u.usageMetadata?.totalTokenCount && (o.#i = u.usageMetadata.totalTokenCount), n.enqueue(x);
823
+ f += x, g.usageMetadata?.totalTokenCount && (n.#i = g.usageMetadata.totalTokenCount), o.enqueue(x);
815
824
  }
816
- i || (o.#r.push(y), o.#r.push({
825
+ !i && !n.#n && n.#e && (n.#e.push(p), n.#e.push({
817
826
  role: "model",
818
827
  parts: [{ text: f }]
819
- }), n.close());
820
- } catch (a) {
821
- i || n.error(a);
828
+ })), o.close();
829
+ } catch (c) {
830
+ i || o.error(c);
822
831
  } finally {
823
- r && r.removeEventListener("abort", c);
832
+ r && r.removeEventListener("abort", s);
824
833
  }
825
834
  }
826
835
  });
827
836
  }
828
837
  async append(t, e = {}) {
829
- if (this.#a(), this.#n)
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 o = await this.#l(t);
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: o };
846
- this.#r.push(r);
854
+ const r = { role: "user", parts: n };
855
+ this.#e.push(r);
847
856
  try {
848
- const n = [...this.#r];
849
- this.#e.systemInstruction && n.unshift({
857
+ const o = [...this.#e];
858
+ this.#r.systemInstruction && o.unshift({
850
859
  role: "system",
851
- parts: [{ text: this.#e.systemInstruction }]
860
+ parts: [{ text: this.#r.systemInstruction }]
852
861
  });
853
- const i = await this.#o.countTokens(n);
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.#a(), this.#n)
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.#l(t);
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 o = this.#u(e);
873
- return o === "QuotaExceededError" ? 1e7 : o === "contextoverflow" ? 5e5 : await this.#o.countTokens([
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 #x(t, e) {
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
- #b(t) {
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 #l(t) {
913
- const e = this.#s.expectedInputs ? ["text", ...this.#s.expectedInputs.map((r) => r.type)] : ["text"];
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 n of t) {
928
- const i = n.role === "assistant" || n.role === "model";
929
- if (typeof n.content == "string") {
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: n.content }), n.prefix && console.warn(
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(n.content))
939
- for (const c of n.content) {
940
- const a = c.type || "text";
941
- if (!e.includes(a))
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 "${a}" is not in the expectedInputs.`,
952
+ `The content type "${c}" is not in the expectedInputs.`,
944
953
  "NotSupportedError"
945
954
  );
946
- if (a === "text") {
947
- if (typeof c.value != "string")
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: c.value });
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(c.type, c.value);
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 && r.type && r.value) {
977
- const n = r.type || "text";
978
- if (!e.includes(n))
979
- throw new (this.#t.DOMException || globalThis.DOMException)(
980
- `The content type "${n}" is not in the expectedInputs.`,
981
- "NotSupportedError"
982
- );
983
- if (n === "text") {
984
- if (typeof r.value != "string")
985
+ 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
- 'The content type "text" must have a string value.',
987
- "SyntaxError"
992
+ `The content type "${o}" is not in the expectedInputs.`,
993
+ "NotSupportedError"
988
994
  );
989
- return { text: r.value };
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
- #m(t, e) {
1011
- const o = String(t.message || t);
1012
- if (o.includes("400") || o.toLowerCase().includes("unable to process") || o.toLowerCase().includes("invalid")) {
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
- (c) => c.inlineData?.mimeType.startsWith("audio/")
1015
- ), n = e.some(
1016
- (c) => c.inlineData?.mimeType.startsWith("image/")
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 (n)
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 = (s) => {
1039
+ const D = (a) => {
1027
1040
  try {
1028
- if (!s || s.LanguageModel?.__isPolyfill)
1041
+ if (!a || a.LanguageModel?.__isPolyfill)
1029
1042
  return;
1030
1043
  const t = class extends d {
1031
1044
  };
1032
- t.__window = s, t.__isPolyfill = !0, s.LanguageModel = t, s.DOMException && (s.QuotaExceededError = s.DOMException);
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 s = Object.getOwnPropertyDescriptor(
1051
+ const a = Object.getOwnPropertyDescriptor(
1039
1052
  HTMLIFrameElement.prototype,
1040
1053
  "contentWindow"
1041
1054
  );
1042
- s && s.get && Object.defineProperty(HTMLIFrameElement.prototype, "contentWindow", {
1055
+ a && a.get && Object.defineProperty(HTMLIFrameElement.prototype, "contentWindow", {
1043
1056
  get() {
1044
- const t = s.get.call(this);
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((s) => {
1052
- for (const t of s)
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((s) => {
1062
- D(s.contentWindow);
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.5.0",
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",