prompt-api-polyfill 1.4.0 → 1.5.0

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