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