prompt-api-polyfill 1.11.0 → 1.12.1
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/README.md +6 -1
- package/dist/backends/firebase.js +88 -66
- package/dist/backends/gemini.js +22 -1
- package/dist/backends/openai.js +22 -1
- package/dist/backends/transformers.js +87 -173
- package/dist/chunks/{defaults-DD5ZWEDv.js → defaults-j68Er1oH.js} +7 -7
- package/dist/prompt-api-polyfill.js +41 -39
- package/package.json +10 -10
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@ supporting dynamic backends:
|
|
|
7
7
|
- **Firebase AI Logic** (cloud)
|
|
8
8
|
- **Google Gemini API** (cloud)
|
|
9
9
|
- **OpenAI API** (cloud)
|
|
10
|
-
- **Transformers.js** (local after initial model download)
|
|
10
|
+
- **Transformers.js** (local after initial model download, **default backend**)
|
|
11
11
|
|
|
12
12
|
When loaded in the browser, it defines a global:
|
|
13
13
|
|
|
@@ -20,6 +20,9 @@ natively available.
|
|
|
20
20
|
|
|
21
21
|
## Supported Backends
|
|
22
22
|
|
|
23
|
+
You can choose between multiple cloud and local backends. If no backend is
|
|
24
|
+
configured, the polyfill will use Transformers.js with the default model.
|
|
25
|
+
|
|
23
26
|
### Firebase AI Logic (cloud)
|
|
24
27
|
|
|
25
28
|
- **Uses**: `firebase/ai` SDK.
|
|
@@ -49,6 +52,8 @@ natively available.
|
|
|
49
52
|
|
|
50
53
|
- **Uses**: `@huggingface/transformers` SDK.
|
|
51
54
|
- **Select by setting**: `window.TRANSFORMERS_CONFIG`.
|
|
55
|
+
- **Note**: This is the **default backend** if no other configuration is
|
|
56
|
+
provided.
|
|
52
57
|
- **Model**: Uses default if not specified (see
|
|
53
58
|
[`backends/defaults.js`](backends/defaults.js)).
|
|
54
59
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { initializeApp as Fe } from "firebase/app";
|
|
2
2
|
import { VertexAIBackend as ze, GoogleAIBackend as Ue, getAI as je, getGenerativeModel as Ve, InferenceMode as We } from "firebase/ai";
|
|
3
|
-
import { P as Ke, D as Ge } from "../chunks/defaults-
|
|
3
|
+
import { P as Ke, D as Ge } from "../chunks/defaults-j68Er1oH.js";
|
|
4
4
|
const qe = () => {
|
|
5
5
|
};
|
|
6
6
|
const be = function(t) {
|
|
@@ -89,9 +89,9 @@ const be = function(t) {
|
|
|
89
89
|
this.init_();
|
|
90
90
|
const r = e ? this.byteToCharMapWebSafe_ : this.byteToCharMap_, n = [];
|
|
91
91
|
for (let s = 0; s < t.length; s += 3) {
|
|
92
|
-
const o = t[s], i = s + 1 < t.length, c = i ? t[s + 1] : 0, a = s + 2 < t.length, l = a ? t[s + 2] : 0,
|
|
92
|
+
const o = t[s], i = s + 1 < t.length, c = i ? t[s + 1] : 0, a = s + 2 < t.length, l = a ? t[s + 2] : 0, p = o >> 2, f = (o & 3) << 4 | c >> 4;
|
|
93
93
|
let k = (c & 15) << 2 | l >> 6, S = l & 63;
|
|
94
|
-
a || (S = 64, i || (k = 64)), n.push(r[
|
|
94
|
+
a || (S = 64, i || (k = 64)), n.push(r[p], r[f], r[k], r[S]);
|
|
95
95
|
}
|
|
96
96
|
return n.join("");
|
|
97
97
|
},
|
|
@@ -321,7 +321,7 @@ function dt(t, e = at, r = ct) {
|
|
|
321
321
|
function ut(t) {
|
|
322
322
|
return t && t._delegate ? t._delegate : t;
|
|
323
323
|
}
|
|
324
|
-
class
|
|
324
|
+
class y {
|
|
325
325
|
/**
|
|
326
326
|
*
|
|
327
327
|
* @param name The public service name, e.g. app, auth, firestore, database
|
|
@@ -344,7 +344,7 @@ class w {
|
|
|
344
344
|
return this.onInstanceCreated = e, this;
|
|
345
345
|
}
|
|
346
346
|
}
|
|
347
|
-
const
|
|
347
|
+
const _ = "[DEFAULT]";
|
|
348
348
|
class ft {
|
|
349
349
|
constructor(e, r) {
|
|
350
350
|
this.name = e, this.container = r, this.component = null, this.instances = /* @__PURE__ */ new Map(), this.instancesDeferred = /* @__PURE__ */ new Map(), this.instancesOptions = /* @__PURE__ */ new Map(), this.onInitCallbacks = /* @__PURE__ */ new Map();
|
|
@@ -397,7 +397,7 @@ class ft {
|
|
|
397
397
|
if (this.component = e, !!this.shouldAutoInitialize()) {
|
|
398
398
|
if (gt(e))
|
|
399
399
|
try {
|
|
400
|
-
this.getOrInitializeService({ instanceIdentifier:
|
|
400
|
+
this.getOrInitializeService({ instanceIdentifier: _ });
|
|
401
401
|
} catch {
|
|
402
402
|
}
|
|
403
403
|
for (const [r, n] of this.instancesDeferred.entries()) {
|
|
@@ -412,7 +412,7 @@ class ft {
|
|
|
412
412
|
}
|
|
413
413
|
}
|
|
414
414
|
}
|
|
415
|
-
clearInstance(e =
|
|
415
|
+
clearInstance(e = _) {
|
|
416
416
|
this.instancesDeferred.delete(e), this.instancesOptions.delete(e), this.instances.delete(e);
|
|
417
417
|
}
|
|
418
418
|
// app.delete() will call this method on every provider to delete the services
|
|
@@ -427,10 +427,10 @@ class ft {
|
|
|
427
427
|
isComponentSet() {
|
|
428
428
|
return this.component != null;
|
|
429
429
|
}
|
|
430
|
-
isInitialized(e =
|
|
430
|
+
isInitialized(e = _) {
|
|
431
431
|
return this.instances.has(e);
|
|
432
432
|
}
|
|
433
|
-
getOptions(e =
|
|
433
|
+
getOptions(e = _) {
|
|
434
434
|
return this.instancesOptions.get(e) || {};
|
|
435
435
|
}
|
|
436
436
|
initialize(e = {}) {
|
|
@@ -490,15 +490,15 @@ class ft {
|
|
|
490
490
|
}
|
|
491
491
|
return n || null;
|
|
492
492
|
}
|
|
493
|
-
normalizeInstanceIdentifier(e =
|
|
494
|
-
return this.component ? this.component.multipleInstances ? e :
|
|
493
|
+
normalizeInstanceIdentifier(e = _) {
|
|
494
|
+
return this.component ? this.component.multipleInstances ? e : _ : e;
|
|
495
495
|
}
|
|
496
496
|
shouldAutoInitialize() {
|
|
497
497
|
return !!this.component && this.component.instantiationMode !== "EXPLICIT";
|
|
498
498
|
}
|
|
499
499
|
}
|
|
500
500
|
function pt(t) {
|
|
501
|
-
return t ===
|
|
501
|
+
return t === _ ? void 0 : t;
|
|
502
502
|
}
|
|
503
503
|
function gt(t) {
|
|
504
504
|
return t.instantiationMode === "EAGER";
|
|
@@ -647,7 +647,7 @@ function Tt(t) {
|
|
|
647
647
|
const s = () => {
|
|
648
648
|
t.removeEventListener("success", o), t.removeEventListener("error", i);
|
|
649
649
|
}, o = () => {
|
|
650
|
-
r(
|
|
650
|
+
r(b(t.result)), s();
|
|
651
651
|
}, i = () => {
|
|
652
652
|
n(t.error), s();
|
|
653
653
|
};
|
|
@@ -683,7 +683,7 @@ let U = {
|
|
|
683
683
|
if (e === "store")
|
|
684
684
|
return r.objectStoreNames[1] ? void 0 : r.objectStore(r.objectStoreNames[0]);
|
|
685
685
|
}
|
|
686
|
-
return
|
|
686
|
+
return b(t[e]);
|
|
687
687
|
},
|
|
688
688
|
set(t, e, r) {
|
|
689
689
|
return t[e] = r, !0;
|
|
@@ -698,17 +698,17 @@ function Dt(t) {
|
|
|
698
698
|
function kt(t) {
|
|
699
699
|
return t === IDBDatabase.prototype.transaction && !("objectStoreNames" in IDBTransaction.prototype) ? function(e, ...r) {
|
|
700
700
|
const n = t.call(N(this), e, ...r);
|
|
701
|
-
return Ie.set(n, e.sort ? e.sort() : [e]),
|
|
701
|
+
return Ie.set(n, e.sort ? e.sort() : [e]), b(n);
|
|
702
702
|
} : It().includes(t) ? function(...e) {
|
|
703
|
-
return t.apply(N(this), e),
|
|
703
|
+
return t.apply(N(this), e), b(Ae.get(this));
|
|
704
704
|
} : function(...e) {
|
|
705
|
-
return
|
|
705
|
+
return b(t.apply(N(this), e));
|
|
706
706
|
};
|
|
707
707
|
}
|
|
708
708
|
function St(t) {
|
|
709
709
|
return typeof t == "function" ? kt(t) : (t instanceof IDBTransaction && Ct(t), yt(t, At()) ? new Proxy(t, U) : t);
|
|
710
710
|
}
|
|
711
|
-
function
|
|
711
|
+
function b(t) {
|
|
712
712
|
if (t instanceof IDBRequest)
|
|
713
713
|
return Tt(t);
|
|
714
714
|
if (O.has(t))
|
|
@@ -718,9 +718,9 @@ function m(t) {
|
|
|
718
718
|
}
|
|
719
719
|
const N = (t) => J.get(t);
|
|
720
720
|
function vt(t, e, { blocked: r, upgrade: n, blocking: s, terminated: o } = {}) {
|
|
721
|
-
const i = indexedDB.open(t, e), c =
|
|
721
|
+
const i = indexedDB.open(t, e), c = b(i);
|
|
722
722
|
return n && i.addEventListener("upgradeneeded", (a) => {
|
|
723
|
-
n(
|
|
723
|
+
n(b(i.result), a.oldVersion, a.newVersion, b(i.transaction), a);
|
|
724
724
|
}), r && i.addEventListener("blocked", (a) => r(
|
|
725
725
|
// Casting due to https://github.com/microsoft/TypeScript-DOM-lib-generator/pull/1405
|
|
726
726
|
a.oldVersion,
|
|
@@ -777,8 +777,8 @@ class Pt {
|
|
|
777
777
|
function Mt(t) {
|
|
778
778
|
return t.getComponent()?.type === "VERSION";
|
|
779
779
|
}
|
|
780
|
-
const j = "@firebase/app", ae = "0.14.
|
|
781
|
-
const
|
|
780
|
+
const j = "@firebase/app", ae = "0.14.9";
|
|
781
|
+
const g = new ye("@firebase/app"), Ot = "@firebase/app-compat", Nt = "@firebase/analytics-compat", $t = "@firebase/analytics", xt = "@firebase/app-check-compat", Lt = "@firebase/app-check", Ht = "@firebase/auth", Ft = "@firebase/auth-compat", zt = "@firebase/database", Ut = "@firebase/data-connect", jt = "@firebase/database-compat", Vt = "@firebase/functions", Wt = "@firebase/functions-compat", Kt = "@firebase/installations", Gt = "@firebase/installations-compat", qt = "@firebase/messaging", Xt = "@firebase/messaging-compat", Yt = "@firebase/performance", Jt = "@firebase/performance-compat", Zt = "@firebase/remote-config", Qt = "@firebase/remote-config-compat", er = "@firebase/storage", tr = "@firebase/storage-compat", rr = "@firebase/firestore", nr = "@firebase/ai", sr = "@firebase/firestore-compat", or = "firebase";
|
|
782
782
|
const V = "[DEFAULT]", ir = {
|
|
783
783
|
[j]: "fire-core",
|
|
784
784
|
[Ot]: "fire-core-compat",
|
|
@@ -815,13 +815,13 @@ function ce(t, e) {
|
|
|
815
815
|
try {
|
|
816
816
|
t.container.addComponent(e);
|
|
817
817
|
} catch (r) {
|
|
818
|
-
|
|
818
|
+
g.debug(`Component ${e.name} failed to register with FirebaseApp ${t.name}`, r);
|
|
819
819
|
}
|
|
820
820
|
}
|
|
821
821
|
function I(t) {
|
|
822
822
|
const e = t.name;
|
|
823
823
|
if (W.has(e))
|
|
824
|
-
return
|
|
824
|
+
return g.debug(`There were multiple attempts to register component ${e}.`), !1;
|
|
825
825
|
W.set(e, t);
|
|
826
826
|
for (const r of P.values())
|
|
827
827
|
ce(r, t);
|
|
@@ -848,10 +848,10 @@ const cr = {
|
|
|
848
848
|
"idb-delete": "Error thrown when deleting from IndexedDB. Original error: {$originalErrorMessage}.",
|
|
849
849
|
"finalization-registry-not-supported": "FirebaseServerApp deleteOnDeref field defined but the JS runtime does not support FinalizationRegistry.",
|
|
850
850
|
"invalid-server-app-environment": "FirebaseServerApp is not for use in browser environments."
|
|
851
|
-
},
|
|
851
|
+
}, E = new Y("app", "Firebase", cr);
|
|
852
852
|
class lr {
|
|
853
853
|
constructor(e, r, n) {
|
|
854
|
-
this._isDeleted = !1, this._options = { ...e }, this._config = { ...r }, this._name = r.name, this._automaticDataCollectionEnabled = r.automaticDataCollectionEnabled, this._container = n, this.container.addComponent(new
|
|
854
|
+
this._isDeleted = !1, this._options = { ...e }, this._config = { ...r }, this._name = r.name, this._automaticDataCollectionEnabled = r.automaticDataCollectionEnabled, this._container = n, this.container.addComponent(new y(
|
|
855
855
|
"app",
|
|
856
856
|
() => this,
|
|
857
857
|
"PUBLIC"
|
|
@@ -888,7 +888,7 @@ class lr {
|
|
|
888
888
|
*/
|
|
889
889
|
checkDestroyed() {
|
|
890
890
|
if (this.isDeleted)
|
|
891
|
-
throw
|
|
891
|
+
throw E.create("app-deleted", { appName: this._name });
|
|
892
892
|
}
|
|
893
893
|
}
|
|
894
894
|
function hr(t, e = {}) {
|
|
@@ -900,11 +900,11 @@ function hr(t, e = {}) {
|
|
|
900
900
|
...e
|
|
901
901
|
}, s = n.name;
|
|
902
902
|
if (typeof s != "string" || !s)
|
|
903
|
-
throw
|
|
903
|
+
throw E.create("bad-app-name", {
|
|
904
904
|
appName: String(s)
|
|
905
905
|
});
|
|
906
906
|
if (r || (r = we()), !r)
|
|
907
|
-
throw
|
|
907
|
+
throw E.create(
|
|
908
908
|
"no-options"
|
|
909
909
|
/* AppError.NO_OPTIONS */
|
|
910
910
|
);
|
|
@@ -912,7 +912,7 @@ function hr(t, e = {}) {
|
|
|
912
912
|
if (o) {
|
|
913
913
|
if (F(r, o.options) && F(n, o.config))
|
|
914
914
|
return o;
|
|
915
|
-
throw
|
|
915
|
+
throw E.create("duplicate-app", { appName: s });
|
|
916
916
|
}
|
|
917
917
|
const i = new mt(s);
|
|
918
918
|
for (const a of W.values())
|
|
@@ -925,7 +925,7 @@ function dr(t = V) {
|
|
|
925
925
|
if (!e && t === V && we())
|
|
926
926
|
return hr();
|
|
927
927
|
if (!e)
|
|
928
|
-
throw
|
|
928
|
+
throw E.create("no-app", { appName: t });
|
|
929
929
|
return e;
|
|
930
930
|
}
|
|
931
931
|
function B(t, e, r) {
|
|
@@ -936,10 +936,10 @@ function B(t, e, r) {
|
|
|
936
936
|
const i = [
|
|
937
937
|
`Unable to register library "${n}" with version "${e}":`
|
|
938
938
|
];
|
|
939
|
-
s && i.push(`library name "${n}" contains illegal characters (whitespace or "/")`), s && o && i.push("and"), o && i.push(`version name "${e}" contains illegal characters (whitespace or "/")`),
|
|
939
|
+
s && i.push(`library name "${n}" contains illegal characters (whitespace or "/")`), s && o && i.push("and"), o && i.push(`version name "${e}" contains illegal characters (whitespace or "/")`), g.warn(i.join(" "));
|
|
940
940
|
return;
|
|
941
941
|
}
|
|
942
|
-
I(new
|
|
942
|
+
I(new y(
|
|
943
943
|
`${n}-version`,
|
|
944
944
|
() => ({ library: n, version: e }),
|
|
945
945
|
"VERSION"
|
|
@@ -961,7 +961,7 @@ function Ce() {
|
|
|
961
961
|
}
|
|
962
962
|
}
|
|
963
963
|
}).catch((t) => {
|
|
964
|
-
throw
|
|
964
|
+
throw E.create("idb-open", {
|
|
965
965
|
originalErrorMessage: t.message
|
|
966
966
|
});
|
|
967
967
|
})), x;
|
|
@@ -972,12 +972,12 @@ async function pr(t) {
|
|
|
972
972
|
return await r.done, n;
|
|
973
973
|
} catch (e) {
|
|
974
974
|
if (e instanceof D)
|
|
975
|
-
|
|
975
|
+
g.warn(e.message);
|
|
976
976
|
else {
|
|
977
|
-
const r =
|
|
977
|
+
const r = E.create("idb-get", {
|
|
978
978
|
originalErrorMessage: e?.message
|
|
979
979
|
});
|
|
980
|
-
|
|
980
|
+
g.warn(r.message);
|
|
981
981
|
}
|
|
982
982
|
}
|
|
983
983
|
}
|
|
@@ -987,12 +987,12 @@ async function le(t, e) {
|
|
|
987
987
|
await n.objectStore(T).put(e, De(t)), await n.done;
|
|
988
988
|
} catch (r) {
|
|
989
989
|
if (r instanceof D)
|
|
990
|
-
|
|
990
|
+
g.warn(r.message);
|
|
991
991
|
else {
|
|
992
|
-
const n =
|
|
992
|
+
const n = E.create("idb-set", {
|
|
993
993
|
originalErrorMessage: r?.message
|
|
994
994
|
});
|
|
995
|
-
|
|
995
|
+
g.warn(n.message);
|
|
996
996
|
}
|
|
997
997
|
}
|
|
998
998
|
}
|
|
@@ -1024,7 +1024,7 @@ class br {
|
|
|
1024
1024
|
}
|
|
1025
1025
|
return this._storage.overwrite(this._heartbeatsCache);
|
|
1026
1026
|
} catch (e) {
|
|
1027
|
-
|
|
1027
|
+
g.warn(e);
|
|
1028
1028
|
}
|
|
1029
1029
|
}
|
|
1030
1030
|
/**
|
|
@@ -1041,7 +1041,7 @@ class br {
|
|
|
1041
1041
|
const e = he(), { heartbeatsToSend: r, unsentEntries: n } = Er(this._heartbeatsCache.heartbeats), s = Ee(JSON.stringify({ version: 2, heartbeats: r }));
|
|
1042
1042
|
return this._heartbeatsCache.lastSentHeartbeatDate = e, n.length > 0 ? (this._heartbeatsCache.heartbeats = n, await this._storage.overwrite(this._heartbeatsCache)) : (this._heartbeatsCache.heartbeats = [], this._storage.overwrite(this._heartbeatsCache)), s;
|
|
1043
1043
|
} catch (e) {
|
|
1044
|
-
return
|
|
1044
|
+
return g.warn(e), "";
|
|
1045
1045
|
}
|
|
1046
1046
|
}
|
|
1047
1047
|
}
|
|
@@ -1130,12 +1130,12 @@ function wr(t) {
|
|
|
1130
1130
|
return e;
|
|
1131
1131
|
}
|
|
1132
1132
|
function yr(t) {
|
|
1133
|
-
I(new
|
|
1133
|
+
I(new y(
|
|
1134
1134
|
"platform-logger",
|
|
1135
1135
|
(e) => new Pt(e),
|
|
1136
1136
|
"PRIVATE"
|
|
1137
1137
|
/* ComponentType.PRIVATE */
|
|
1138
|
-
)), I(new
|
|
1138
|
+
)), I(new y(
|
|
1139
1139
|
"heartbeat",
|
|
1140
1140
|
(e) => new br(e),
|
|
1141
1141
|
"PRIVATE"
|
|
@@ -1281,11 +1281,11 @@ async function Q({ url: t, body: e }, r) {
|
|
|
1281
1281
|
throw u.create("fetch-parse-error", {
|
|
1282
1282
|
originalErrorMessage: `ttl field (timeToLive) is not in standard Protobuf Duration format: ${c.ttl}`
|
|
1283
1283
|
});
|
|
1284
|
-
const l = Number(a[1]) * 1e3,
|
|
1284
|
+
const l = Number(a[1]) * 1e3, p = Date.now();
|
|
1285
1285
|
return {
|
|
1286
1286
|
token: c.token,
|
|
1287
|
-
expireTimeMillis:
|
|
1288
|
-
issuedAtTimeMillis:
|
|
1287
|
+
expireTimeMillis: p + l,
|
|
1288
|
+
issuedAtTimeMillis: p
|
|
1289
1289
|
};
|
|
1290
1290
|
}
|
|
1291
1291
|
function Rr(t, e) {
|
|
@@ -1375,21 +1375,21 @@ async function Oe(t) {
|
|
|
1375
1375
|
function Ne(t) {
|
|
1376
1376
|
return `${t.options.appId}-${t.name}`;
|
|
1377
1377
|
}
|
|
1378
|
-
const
|
|
1378
|
+
const m = new ye("@firebase/app-check");
|
|
1379
1379
|
async function xr(t) {
|
|
1380
1380
|
if (X()) {
|
|
1381
1381
|
let e;
|
|
1382
1382
|
try {
|
|
1383
1383
|
e = await Mr(t);
|
|
1384
1384
|
} catch (r) {
|
|
1385
|
-
|
|
1385
|
+
m.warn(`Failed to read token from IndexedDB. Error: ${r}`);
|
|
1386
1386
|
}
|
|
1387
1387
|
return e;
|
|
1388
1388
|
}
|
|
1389
1389
|
}
|
|
1390
1390
|
function L(t, e) {
|
|
1391
1391
|
return X() ? Or(t, e).catch((r) => {
|
|
1392
|
-
|
|
1392
|
+
m.warn(`Failed to write token to IndexedDB. Error: ${r}`);
|
|
1393
1393
|
}) : Promise.resolve();
|
|
1394
1394
|
}
|
|
1395
1395
|
async function Lr() {
|
|
@@ -1402,7 +1402,7 @@ async function Lr() {
|
|
|
1402
1402
|
return t;
|
|
1403
1403
|
{
|
|
1404
1404
|
const e = crypto.randomUUID();
|
|
1405
|
-
return Nr(e).catch((r) =>
|
|
1405
|
+
return Nr(e).catch((r) => m.warn(`Failed to persist debug token to IndexedDB. Error: ${r}`)), e;
|
|
1406
1406
|
}
|
|
1407
1407
|
}
|
|
1408
1408
|
function ee() {
|
|
@@ -1437,34 +1437,35 @@ async function G(t, e = !1, r = !1) {
|
|
|
1437
1437
|
Z(n);
|
|
1438
1438
|
const s = d(n);
|
|
1439
1439
|
let o = s.token, i;
|
|
1440
|
-
if (o && !
|
|
1440
|
+
if (o && !w(o) && (s.token = void 0, o = void 0), !o) {
|
|
1441
1441
|
const l = await s.cachedTokenPromise;
|
|
1442
|
-
l && (
|
|
1442
|
+
l && (w(l) ? o = l : await L(n, void 0));
|
|
1443
1443
|
}
|
|
1444
|
-
if (!e && o &&
|
|
1444
|
+
if (!e && o && w(o))
|
|
1445
1445
|
return {
|
|
1446
1446
|
token: o.token
|
|
1447
1447
|
};
|
|
1448
1448
|
let c = !1;
|
|
1449
1449
|
if (ee())
|
|
1450
1450
|
try {
|
|
1451
|
-
|
|
1451
|
+
const l = await te();
|
|
1452
|
+
s.exchangeTokenPromise || (s.exchangeTokenPromise = Q(Re(n, l), t.heartbeatServiceProvider).finally(() => {
|
|
1452
1453
|
s.exchangeTokenPromise = void 0;
|
|
1453
1454
|
}), c = !0);
|
|
1454
|
-
const
|
|
1455
|
-
return await L(n,
|
|
1455
|
+
const p = await s.exchangeTokenPromise;
|
|
1456
|
+
return await L(n, p), s.token = p, { token: p.token };
|
|
1456
1457
|
} catch (l) {
|
|
1457
|
-
return l.code === "appCheck/throttled" || l.code === "appCheck/initial-throttle" ?
|
|
1458
|
+
return l.code === "appCheck/throttled" || l.code === "appCheck/initial-throttle" ? m.warn(l.message) : r && m.error(l), H(l);
|
|
1458
1459
|
}
|
|
1459
1460
|
try {
|
|
1460
1461
|
s.exchangeTokenPromise || (s.exchangeTokenPromise = s.provider.getToken().finally(() => {
|
|
1461
1462
|
s.exchangeTokenPromise = void 0;
|
|
1462
1463
|
}), c = !0), o = await d(n).exchangeTokenPromise;
|
|
1463
1464
|
} catch (l) {
|
|
1464
|
-
l.code === "appCheck/throttled" || l.code === "appCheck/initial-throttle" ?
|
|
1465
|
+
l.code === "appCheck/throttled" || l.code === "appCheck/initial-throttle" ? m.warn(l.message) : r && m.error(l), i = l;
|
|
1465
1466
|
}
|
|
1466
1467
|
let a;
|
|
1467
|
-
return o ? i ?
|
|
1468
|
+
return o ? i ? w(o) ? a = {
|
|
1468
1469
|
token: o.token,
|
|
1469
1470
|
internalError: i
|
|
1470
1471
|
} : a = H(i) : (a = {
|
|
@@ -1489,7 +1490,7 @@ function $e(t, e, r, n) {
|
|
|
1489
1490
|
error: n,
|
|
1490
1491
|
type: e
|
|
1491
1492
|
};
|
|
1492
|
-
if (o.tokenObservers = [...o.tokenObservers, i], o.token &&
|
|
1493
|
+
if (o.tokenObservers = [...o.tokenObservers, i], o.token && w(o.token)) {
|
|
1493
1494
|
const c = o.token;
|
|
1494
1495
|
Promise.resolve().then(() => {
|
|
1495
1496
|
r({ token: c.token }), pe(t);
|
|
@@ -1542,7 +1543,7 @@ function Le(t, e) {
|
|
|
1542
1543
|
} catch {
|
|
1543
1544
|
}
|
|
1544
1545
|
}
|
|
1545
|
-
function
|
|
1546
|
+
function w(t) {
|
|
1546
1547
|
return t.expireTimeMillis - Date.now() > 0;
|
|
1547
1548
|
}
|
|
1548
1549
|
function H(t) {
|
|
@@ -1573,7 +1574,7 @@ function Kr(t) {
|
|
|
1573
1574
|
removeTokenListener: (e) => xe(t.app, e)
|
|
1574
1575
|
};
|
|
1575
1576
|
}
|
|
1576
|
-
const Gr = "@firebase/app-check", qr = "0.11.
|
|
1577
|
+
const Gr = "@firebase/app-check", qr = "0.11.1", Xr = "https://www.google.com/recaptcha/enterprise.js";
|
|
1577
1578
|
function Yr(t, e) {
|
|
1578
1579
|
const r = new A(), n = d(t);
|
|
1579
1580
|
n.reCAPTCHAState = { initialized: r };
|
|
@@ -1728,11 +1729,11 @@ function nn(t = dr(), e) {
|
|
|
1728
1729
|
}
|
|
1729
1730
|
function sn(t, e, r = !1) {
|
|
1730
1731
|
const n = Ir(t, { ...ke });
|
|
1731
|
-
n.activated = !0, n.provider = e, n.cachedTokenPromise = xr(t).then((s) => (s &&
|
|
1732
|
+
n.activated = !0, n.provider = e, n.cachedTokenPromise = xr(t).then((s) => (s && w(s) && (n.token = s, Le(t, { token: s.token })), s)), n.isTokenAutoRefreshEnabled = r && t.automaticDataCollectionEnabled, !t.automaticDataCollectionEnabled && r && m.warn("`isTokenAutoRefreshEnabled` is true but `automaticDataCollectionEnabled` was set to false during `initializeApp()`. This blocks automatic token refresh."), n.provider.initialize(t);
|
|
1732
1733
|
}
|
|
1733
1734
|
const on = "app-check", me = "app-check-internal";
|
|
1734
1735
|
function an() {
|
|
1735
|
-
I(new
|
|
1736
|
+
I(new y(
|
|
1736
1737
|
on,
|
|
1737
1738
|
(t) => {
|
|
1738
1739
|
const e = t.getProvider("app").getImmediate(), r = t.getProvider("heartbeat");
|
|
@@ -1745,7 +1746,7 @@ function an() {
|
|
|
1745
1746
|
/* InstantiationMode.EXPLICIT */
|
|
1746
1747
|
).setInstanceCreatedCallback((t, e, r) => {
|
|
1747
1748
|
t.getProvider(me).initialize();
|
|
1748
|
-
})), I(new
|
|
1749
|
+
})), I(new y(
|
|
1749
1750
|
me,
|
|
1750
1751
|
(t) => {
|
|
1751
1752
|
const e = t.getProvider("app-check").getImmediate();
|
|
@@ -1782,19 +1783,40 @@ class dn extends Ke {
|
|
|
1782
1783
|
useLimitedUseAppCheckTokens: i || !0
|
|
1783
1784
|
});
|
|
1784
1785
|
}
|
|
1786
|
+
/**
|
|
1787
|
+
* Creates a model session and stores it.
|
|
1788
|
+
* @param {Object} _options - LanguageModel options.
|
|
1789
|
+
* @param {Object} sessionParams - Parameters for the cloud or local model.
|
|
1790
|
+
* @returns {any} The created session object.
|
|
1791
|
+
*/
|
|
1785
1792
|
createSession(e, r) {
|
|
1786
1793
|
return this.#e = Ve(this.ai, {
|
|
1787
1794
|
mode: We.ONLY_IN_CLOUD,
|
|
1788
1795
|
inCloudParams: r
|
|
1789
1796
|
}), this.#e;
|
|
1790
1797
|
}
|
|
1798
|
+
/**
|
|
1799
|
+
* Generates content (non-streaming).
|
|
1800
|
+
* @param {Array} contents - The history + new message content.
|
|
1801
|
+
* @returns {Promise<{text: string, usage: number}>}
|
|
1802
|
+
*/
|
|
1791
1803
|
async generateContent(e) {
|
|
1792
1804
|
const r = await this.#e.generateContent({ contents: e }), n = r.response.usageMetadata?.promptTokenCount || 0;
|
|
1793
1805
|
return { text: r.response.text(), usage: n };
|
|
1794
1806
|
}
|
|
1807
|
+
/**
|
|
1808
|
+
* Generates content stream.
|
|
1809
|
+
* @param {Array} contents - The history + new content.
|
|
1810
|
+
* @returns {Promise<AsyncIterable>} Stream of chunks.
|
|
1811
|
+
*/
|
|
1795
1812
|
async generateContentStream(e) {
|
|
1796
1813
|
return (await this.#e.generateContentStream({ contents: e })).stream;
|
|
1797
1814
|
}
|
|
1815
|
+
/**
|
|
1816
|
+
* Counts tokens.
|
|
1817
|
+
* @param {Array} contents - The content to count.
|
|
1818
|
+
* @returns {Promise<number>} Total tokens.
|
|
1819
|
+
*/
|
|
1798
1820
|
async countTokens(e) {
|
|
1799
1821
|
const { totalTokens: r } = await this.#e.countTokens({
|
|
1800
1822
|
contents: e
|
package/dist/backends/gemini.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { GoogleGenAI as s } from "@google/genai";
|
|
2
|
-
import { P as a, D as i } from "../chunks/defaults-
|
|
2
|
+
import { P as a, D as i } from "../chunks/defaults-j68Er1oH.js";
|
|
3
3
|
class u extends a {
|
|
4
4
|
#n;
|
|
5
5
|
#e;
|
|
@@ -7,9 +7,20 @@ class u extends a {
|
|
|
7
7
|
constructor(t) {
|
|
8
8
|
super(t.modelName || i.gemini.modelName), this.#n = new s({ apiKey: t.apiKey });
|
|
9
9
|
}
|
|
10
|
+
/**
|
|
11
|
+
* Creates a model session.
|
|
12
|
+
* @param {Object} options - LanguageModel options.
|
|
13
|
+
* @param {Object} sessionParams - Session parameters.
|
|
14
|
+
* @returns {Object} The session object.
|
|
15
|
+
*/
|
|
10
16
|
createSession(t, e) {
|
|
11
17
|
return this.#t = e, this.#e = t.modelName || this.modelName, { model: this.#e, params: e };
|
|
12
18
|
}
|
|
19
|
+
/**
|
|
20
|
+
* Generates content (non-streaming).
|
|
21
|
+
* @param {Array} contents - The history + new message content.
|
|
22
|
+
* @returns {Promise<{text: string, usage: number}>}
|
|
23
|
+
*/
|
|
13
24
|
async generateContent(t) {
|
|
14
25
|
const e = {
|
|
15
26
|
systemInstruction: this.#t.systemInstruction,
|
|
@@ -22,6 +33,11 @@ class u extends a {
|
|
|
22
33
|
}), o = n.usageMetadata?.promptTokenCount || 0;
|
|
23
34
|
return { text: n.text, usage: o };
|
|
24
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* Generates content stream.
|
|
38
|
+
* @param {Array} contents - The history + new content.
|
|
39
|
+
* @returns {Promise<AsyncIterable>} Stream of chunks.
|
|
40
|
+
*/
|
|
25
41
|
async generateContentStream(t) {
|
|
26
42
|
const e = {
|
|
27
43
|
systemInstruction: this.#t.systemInstruction,
|
|
@@ -42,6 +58,11 @@ class u extends a {
|
|
|
42
58
|
};
|
|
43
59
|
})();
|
|
44
60
|
}
|
|
61
|
+
/**
|
|
62
|
+
* Counts tokens.
|
|
63
|
+
* @param {Array} contents - The content to count.
|
|
64
|
+
* @returns {Promise<number>} Total tokens.
|
|
65
|
+
*/
|
|
45
66
|
async countTokens(t) {
|
|
46
67
|
const { totalTokens: e } = await this.#n.models.countTokens({
|
|
47
68
|
model: this.#e,
|
package/dist/backends/openai.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import u from "openai";
|
|
2
|
-
import { P as m, D as l } from "../chunks/defaults-
|
|
2
|
+
import { P as m, D as l } from "../chunks/defaults-j68Er1oH.js";
|
|
3
3
|
class y extends m {
|
|
4
4
|
#e;
|
|
5
5
|
constructor(t) {
|
|
@@ -21,6 +21,12 @@ class y extends m {
|
|
|
21
21
|
}
|
|
22
22
|
return "available";
|
|
23
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Creates a model session and stores it.
|
|
26
|
+
* @param {Object} options - LanguageModel options.
|
|
27
|
+
* @param {Object} sessionParams - Parameters for the cloud or local model.
|
|
28
|
+
* @returns {any} The created session object.
|
|
29
|
+
*/
|
|
24
30
|
createSession(t, s) {
|
|
25
31
|
this.#e = {
|
|
26
32
|
model: t.modelName || this.modelName,
|
|
@@ -93,6 +99,11 @@ class y extends m {
|
|
|
93
99
|
#s(t) {
|
|
94
100
|
return this.#e.model !== this.modelName ? this.#e.model : t ? `${this.modelName}-audio-preview` : this.modelName;
|
|
95
101
|
}
|
|
102
|
+
/**
|
|
103
|
+
* Generates content (non-streaming).
|
|
104
|
+
* @param {Array} contents - The history + new message content.
|
|
105
|
+
* @returns {Promise<{text: string, usage: number}>}
|
|
106
|
+
*/
|
|
96
107
|
async generateContent(t) {
|
|
97
108
|
const { messages: s } = this.#r(
|
|
98
109
|
t,
|
|
@@ -123,6 +134,11 @@ class y extends m {
|
|
|
123
134
|
throw console.error("OpenAI Generate Content Error:", a), a;
|
|
124
135
|
}
|
|
125
136
|
}
|
|
137
|
+
/**
|
|
138
|
+
* Generates content stream.
|
|
139
|
+
* @param {Array} contents - The history + new content.
|
|
140
|
+
* @returns {Promise<AsyncIterable>} Stream of chunks.
|
|
141
|
+
*/
|
|
126
142
|
async generateContentStream(t) {
|
|
127
143
|
const { messages: s } = this.#r(
|
|
128
144
|
t,
|
|
@@ -154,6 +170,11 @@ class y extends m {
|
|
|
154
170
|
throw console.error("OpenAI Generate Content Stream Error:", a), a;
|
|
155
171
|
}
|
|
156
172
|
}
|
|
173
|
+
/**
|
|
174
|
+
* Counts tokens.
|
|
175
|
+
* @param {Array} contents - The content to count.
|
|
176
|
+
* @returns {Promise<number>} Total tokens.
|
|
177
|
+
*/
|
|
157
178
|
async countTokens(t) {
|
|
158
179
|
let s = "";
|
|
159
180
|
if (Array.isArray(t)) {
|
|
@@ -1,18 +1,20 @@
|
|
|
1
|
-
import { env as
|
|
2
|
-
import { P as
|
|
3
|
-
class
|
|
1
|
+
import { env as p, pipeline as m, TextStreamer as f } from "@huggingface/transformers";
|
|
2
|
+
import { P as h, D as c } from "../chunks/defaults-j68Er1oH.js";
|
|
3
|
+
class w extends h {
|
|
4
4
|
#e;
|
|
5
5
|
#t;
|
|
6
|
-
#i;
|
|
7
6
|
#o;
|
|
7
|
+
#a;
|
|
8
8
|
#n;
|
|
9
|
-
constructor(
|
|
10
|
-
if (super(
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
9
|
+
constructor(s = {}) {
|
|
10
|
+
if (super(s.modelName || c.transformers.modelName), this.#o = s.device || c.transformers.device, this.#a = s.dtype || c.transformers.dtype, s.isDefault && console.log(
|
|
11
|
+
`Polyfill: No backend configuration found. Defaulting to Transformers.js with model: ${this.modelName}`
|
|
12
|
+
), p.experimental_useCrossOriginStorage = !0, s.env) {
|
|
13
|
+
const e = (n, r) => {
|
|
14
|
+
for (const [o, t] of Object.entries(r))
|
|
15
|
+
t && typeof t == "object" && !Array.isArray(t) && n[o] && typeof n[o] == "object" ? e(n[o], t) : n[o] = t;
|
|
14
16
|
};
|
|
15
|
-
|
|
17
|
+
e(p, s.env);
|
|
16
18
|
}
|
|
17
19
|
}
|
|
18
20
|
/**
|
|
@@ -20,56 +22,26 @@ class I extends $ {
|
|
|
20
22
|
* @param {EventTarget} [monitorTarget] - The event target to dispatch download progress events to.
|
|
21
23
|
* @returns {Promise<Object>} The generator.
|
|
22
24
|
*/
|
|
23
|
-
async #s(
|
|
25
|
+
async #s(s) {
|
|
24
26
|
if (!this.#e) {
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
});
|
|
28
|
-
for (const { path: e, size: r } of n)
|
|
29
|
-
t.set(e, { loaded: 0, total: r });
|
|
30
|
-
const a = (e) => {
|
|
31
|
-
if (!o)
|
|
27
|
+
const e = (r) => {
|
|
28
|
+
if (!s)
|
|
32
29
|
return;
|
|
33
|
-
const
|
|
34
|
-
|
|
30
|
+
const o = 1 / 65536, t = Math.floor(r / o) * o;
|
|
31
|
+
t <= s.__lastProgressLoaded || (s.dispatchEvent(
|
|
35
32
|
new ProgressEvent("downloadprogress", {
|
|
36
|
-
loaded:
|
|
33
|
+
loaded: t,
|
|
37
34
|
total: 1,
|
|
38
35
|
lengthComputable: !0
|
|
39
36
|
})
|
|
40
|
-
),
|
|
41
|
-
},
|
|
42
|
-
|
|
43
|
-
if (t.has(e.file)) {
|
|
44
|
-
const r = t.get(e.file);
|
|
45
|
-
e.total && (r.total = e.total);
|
|
46
|
-
} else
|
|
47
|
-
t.set(e.file, { loaded: 0, total: e.total || 0 });
|
|
48
|
-
else if (e.status === "progress")
|
|
49
|
-
t.has(e.file) && (t.get(e.file).loaded = e.loaded);
|
|
50
|
-
else if (e.status === "done") {
|
|
51
|
-
if (t.has(e.file)) {
|
|
52
|
-
const r = t.get(e.file);
|
|
53
|
-
r.loaded = r.total;
|
|
54
|
-
}
|
|
55
|
-
} else if (e.status === "ready") {
|
|
56
|
-
a(1);
|
|
57
|
-
return;
|
|
58
|
-
}
|
|
59
|
-
if (e.status === "progress" || e.status === "done") {
|
|
60
|
-
let r = 0, c = 0;
|
|
61
|
-
for (const { loaded: f, total: d } of t.values())
|
|
62
|
-
r += f, c += d;
|
|
63
|
-
if (c > 0) {
|
|
64
|
-
const f = r / c;
|
|
65
|
-
a(Math.min(f, 0.9999));
|
|
66
|
-
}
|
|
67
|
-
}
|
|
37
|
+
), s.__lastProgressLoaded = t);
|
|
38
|
+
}, n = (r) => {
|
|
39
|
+
r.status === "progress_total" ? e(r.progress / 100) : r.status === "ready" && e(1);
|
|
68
40
|
};
|
|
69
|
-
|
|
70
|
-
device: this.#
|
|
71
|
-
dtype: this.#
|
|
72
|
-
progress_callback:
|
|
41
|
+
e(0), this.#e = await m("text-generation", this.modelName, {
|
|
42
|
+
device: this.#o,
|
|
43
|
+
dtype: this.#a,
|
|
44
|
+
progress_callback: n
|
|
73
45
|
}), this.#t = this.#e.tokenizer;
|
|
74
46
|
}
|
|
75
47
|
return this.#e;
|
|
@@ -79,10 +51,10 @@ class I extends $ {
|
|
|
79
51
|
* @param {Object} options - LanguageModel options.
|
|
80
52
|
* @returns {string} 'available' or 'unavailable'.
|
|
81
53
|
*/
|
|
82
|
-
static availability(
|
|
83
|
-
if (
|
|
84
|
-
for (const
|
|
85
|
-
if (
|
|
54
|
+
static availability(s) {
|
|
55
|
+
if (s?.expectedInputs && Array.isArray(s.expectedInputs)) {
|
|
56
|
+
for (const e of s.expectedInputs)
|
|
57
|
+
if (e.type === "audio" || e.type === "image")
|
|
86
58
|
return "unavailable";
|
|
87
59
|
}
|
|
88
60
|
return "available";
|
|
@@ -94,167 +66,109 @@ class I extends $ {
|
|
|
94
66
|
* @param {EventTarget} [monitorTarget] - The event target to dispatch download progress events to.
|
|
95
67
|
* @returns {Promise<Object>} The generator.
|
|
96
68
|
*/
|
|
97
|
-
async createSession(
|
|
98
|
-
return
|
|
69
|
+
async createSession(s, e, n) {
|
|
70
|
+
return s.responseConstraint && console.warn(
|
|
99
71
|
"The `responseConstraint` flag isn't supported by the Transformers.js backend and was ignored."
|
|
100
72
|
), await this.#s(n), this.generationConfig = {
|
|
101
73
|
max_new_tokens: 512,
|
|
102
74
|
// Default limit
|
|
103
|
-
temperature:
|
|
75
|
+
temperature: e.generationConfig?.temperature ?? 1,
|
|
104
76
|
top_p: 1,
|
|
105
|
-
do_sample:
|
|
77
|
+
do_sample: e.generationConfig?.temperature !== 0,
|
|
106
78
|
return_full_text: !1
|
|
107
|
-
}, this.#n =
|
|
79
|
+
}, this.#n = e.systemInstruction, this.#e;
|
|
108
80
|
}
|
|
109
|
-
|
|
110
|
-
|
|
81
|
+
/**
|
|
82
|
+
* Generates content (non-streaming).
|
|
83
|
+
* @param {Array} contents - The history + new message content.
|
|
84
|
+
* @returns {Promise<{text: string, usage: number}>}
|
|
85
|
+
*/
|
|
86
|
+
async generateContent(s) {
|
|
87
|
+
const e = await this.#s(), n = this.#r(s), r = this.#t.apply_chat_template(n, {
|
|
111
88
|
tokenize: !1,
|
|
112
89
|
add_generation_prompt: !0
|
|
113
|
-
}),
|
|
90
|
+
}), t = (await e(r, {
|
|
114
91
|
...this.generationConfig,
|
|
115
92
|
add_special_tokens: !1
|
|
116
|
-
}))[0].generated_text,
|
|
117
|
-
return { text:
|
|
93
|
+
}))[0].generated_text, i = await this.countTokens(s);
|
|
94
|
+
return { text: t, usage: i };
|
|
118
95
|
}
|
|
119
|
-
|
|
120
|
-
|
|
96
|
+
/**
|
|
97
|
+
* Generates content stream.
|
|
98
|
+
* @param {Array} contents - The history + new content.
|
|
99
|
+
* @returns {Promise<AsyncIterable>} Stream of chunks.
|
|
100
|
+
*/
|
|
101
|
+
async generateContentStream(s) {
|
|
102
|
+
const e = await this.#s(), n = this.#r(s), r = this.#t.apply_chat_template(n, {
|
|
121
103
|
tokenize: !1,
|
|
122
104
|
add_generation_prompt: !0
|
|
123
|
-
}),
|
|
124
|
-
let
|
|
125
|
-
const
|
|
126
|
-
|
|
127
|
-
}, d = new
|
|
105
|
+
}), o = [];
|
|
106
|
+
let t, i = new Promise((a) => t = a), l = !1;
|
|
107
|
+
const u = (a) => {
|
|
108
|
+
o.push(a), t && (t(), t = null);
|
|
109
|
+
}, d = new f(this.#t, {
|
|
128
110
|
skip_prompt: !0,
|
|
129
111
|
skip_special_tokens: !0,
|
|
130
|
-
callback_function:
|
|
112
|
+
callback_function: u
|
|
131
113
|
});
|
|
132
|
-
return
|
|
114
|
+
return e(r, {
|
|
133
115
|
...this.generationConfig,
|
|
134
116
|
add_special_tokens: !1,
|
|
135
117
|
streamer: d
|
|
136
118
|
}).then(() => {
|
|
137
|
-
|
|
138
|
-
}).catch((
|
|
139
|
-
console.error("[Transformers.js] Generation error:",
|
|
119
|
+
l = !0, t && (t(), t = null);
|
|
120
|
+
}).catch((a) => {
|
|
121
|
+
console.error("[Transformers.js] Generation error:", a), l = !0, t && (t(), t = null);
|
|
140
122
|
}), (async function* () {
|
|
141
123
|
for (; ; ) {
|
|
142
|
-
for (
|
|
143
|
-
const
|
|
124
|
+
for (o.length === 0 && !l && (t || (i = new Promise((a) => t = a)), await i); o.length > 0; ) {
|
|
125
|
+
const a = o.shift();
|
|
144
126
|
yield {
|
|
145
|
-
text: () =>
|
|
127
|
+
text: () => a,
|
|
146
128
|
usageMetadata: { totalTokenCount: 0 }
|
|
147
129
|
};
|
|
148
130
|
}
|
|
149
|
-
if (
|
|
131
|
+
if (l)
|
|
150
132
|
break;
|
|
151
133
|
}
|
|
152
134
|
})();
|
|
153
135
|
}
|
|
154
|
-
|
|
136
|
+
/**
|
|
137
|
+
* Counts tokens.
|
|
138
|
+
* @param {Array} contents - The content to count.
|
|
139
|
+
* @returns {Promise<number>} Total tokens.
|
|
140
|
+
*/
|
|
141
|
+
async countTokens(s) {
|
|
155
142
|
await this.#s();
|
|
156
|
-
const
|
|
157
|
-
return this.#t.apply_chat_template(
|
|
143
|
+
const e = this.#r(s);
|
|
144
|
+
return this.#t.apply_chat_template(e, {
|
|
158
145
|
tokenize: !0,
|
|
159
146
|
add_generation_prompt: !1,
|
|
160
147
|
return_tensor: !1
|
|
161
148
|
}).length;
|
|
162
149
|
}
|
|
163
|
-
#r(
|
|
164
|
-
const
|
|
165
|
-
let
|
|
166
|
-
const
|
|
167
|
-
return { role:
|
|
150
|
+
#r(s) {
|
|
151
|
+
const e = s.map((n) => {
|
|
152
|
+
let r = n.role === "model" ? "assistant" : n.role === "system" ? "system" : "user";
|
|
153
|
+
const o = n.parts.map((t) => t.text).join("");
|
|
154
|
+
return { role: r, content: o };
|
|
168
155
|
});
|
|
169
|
-
if (this.#n && !
|
|
170
|
-
const n =
|
|
156
|
+
if (this.#n && !e.some((n) => n.role === "system") && e.unshift({ role: "system", content: this.#n }), this.modelName.toLowerCase().includes("gemma")) {
|
|
157
|
+
const n = e.findIndex((r) => r.role === "system");
|
|
171
158
|
if (n !== -1) {
|
|
172
|
-
const
|
|
173
|
-
(
|
|
159
|
+
const r = e[n], o = e.findIndex(
|
|
160
|
+
(t, i) => t.role === "user" && i > n
|
|
174
161
|
);
|
|
175
|
-
|
|
162
|
+
o !== -1 ? (e[o].content = r.content + `
|
|
176
163
|
|
|
177
|
-
` +
|
|
164
|
+
` + e[o].content, e.splice(n, 1)) : (r.content += `
|
|
178
165
|
|
|
179
|
-
`,
|
|
166
|
+
`, r.role = "user");
|
|
180
167
|
}
|
|
181
168
|
}
|
|
182
|
-
return
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
async function M(g, o = {}) {
|
|
186
|
-
const { dtype: t = "q8", branch: n = "main" } = o;
|
|
187
|
-
let a = null;
|
|
188
|
-
const l = `transformers_model_files_${g}_${t}_${n}`;
|
|
189
|
-
try {
|
|
190
|
-
const s = localStorage.getItem(l);
|
|
191
|
-
if (s) {
|
|
192
|
-
a = JSON.parse(s);
|
|
193
|
-
const { timestamp: p, files: u } = a, v = 1440 * 60 * 1e3;
|
|
194
|
-
if (Date.now() - p < v)
|
|
195
|
-
return u;
|
|
196
|
-
}
|
|
197
|
-
} catch (s) {
|
|
198
|
-
console.warn("Failed to read from localStorage cache:", s);
|
|
199
|
-
}
|
|
200
|
-
const e = `https://huggingface.co/api/models/${g}/tree/${n}?recursive=true`;
|
|
201
|
-
let r;
|
|
202
|
-
try {
|
|
203
|
-
if (r = await fetch(e), !r.ok)
|
|
204
|
-
throw new Error(`Manifest fetch failed: ${r.status}`);
|
|
205
|
-
} catch (s) {
|
|
206
|
-
if (a)
|
|
207
|
-
return console.warn(
|
|
208
|
-
"Failed to fetch manifest from network, falling back to cached data (expired):",
|
|
209
|
-
s
|
|
210
|
-
), a.files;
|
|
211
|
-
throw s;
|
|
212
|
-
}
|
|
213
|
-
const c = await r.json(), f = new Map(c.map((s) => [s.path, s.size])), d = [], h = (s) => f.has(s), i = (s) => h(s) ? (d.push({ path: s, size: f.get(s) }), !0) : !1;
|
|
214
|
-
i("config.json"), i("generation_config.json"), i("preprocessor_config.json"), h("tokenizer.json") ? (i("tokenizer.json"), i("tokenizer_config.json")) : (i("tokenizer_config.json"), i("special_tokens_map.json"), i("vocab.json"), i("merges.txt"), i("vocab.txt"));
|
|
215
|
-
const w = "onnx";
|
|
216
|
-
let m;
|
|
217
|
-
t === "fp32" ? m = [""] : t === "quantized" ? m = ["_quantized"] : (m = [`_${t}`], t === "q8" && m.push(""));
|
|
218
|
-
let k = [
|
|
219
|
-
"model",
|
|
220
|
-
"encoder_model",
|
|
221
|
-
"decoder_model",
|
|
222
|
-
"decoder_model_merged"
|
|
223
|
-
];
|
|
224
|
-
const _ = [];
|
|
225
|
-
for (const s of k)
|
|
226
|
-
for (const p of m) {
|
|
227
|
-
const u = `${w}/${s}${p}.onnx`;
|
|
228
|
-
if (h(u)) {
|
|
229
|
-
_.push(u);
|
|
230
|
-
break;
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
const x = _.some(
|
|
234
|
-
(s) => s.includes("decoder_model_merged")
|
|
235
|
-
), b = _.filter((s) => !(x && s.includes("decoder_model") && !s.includes("merged")));
|
|
236
|
-
for (const s of b) {
|
|
237
|
-
i(s);
|
|
238
|
-
const p = `${s}_data`;
|
|
239
|
-
if (i(p)) {
|
|
240
|
-
let u = 1;
|
|
241
|
-
for (; i(`${p}_${u}`); )
|
|
242
|
-
u++;
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
try {
|
|
246
|
-
localStorage.setItem(
|
|
247
|
-
l,
|
|
248
|
-
JSON.stringify({
|
|
249
|
-
timestamp: Date.now(),
|
|
250
|
-
files: d
|
|
251
|
-
})
|
|
252
|
-
);
|
|
253
|
-
} catch (s) {
|
|
254
|
-
console.warn("Failed to write to localStorage cache:", s);
|
|
169
|
+
return e;
|
|
255
170
|
}
|
|
256
|
-
return d;
|
|
257
171
|
}
|
|
258
172
|
export {
|
|
259
|
-
|
|
173
|
+
w as default
|
|
260
174
|
};
|
|
@@ -7,7 +7,7 @@ class a {
|
|
|
7
7
|
}
|
|
8
8
|
/**
|
|
9
9
|
* Checks if the backend is available given the options.
|
|
10
|
-
* @param {Object}
|
|
10
|
+
* @param {Object} _options - LanguageModel options.
|
|
11
11
|
* @returns {string} 'available', 'unavailable', 'downloadable', or 'downloading'.
|
|
12
12
|
*/
|
|
13
13
|
static availability(e) {
|
|
@@ -15,9 +15,9 @@ class a {
|
|
|
15
15
|
}
|
|
16
16
|
/**
|
|
17
17
|
* Creates a model session and stores it.
|
|
18
|
-
* @param {Object}
|
|
19
|
-
* @param {Object}
|
|
20
|
-
* @param {EventTarget} [
|
|
18
|
+
* @param {Object} _options - LanguageModel options.
|
|
19
|
+
* @param {Object} _sessionParams - Parameters for the cloud or local model.
|
|
20
|
+
* @param {EventTarget} [_monitorTarget] - The event target to dispatch download progress events to.
|
|
21
21
|
* @returns {any} The created session object.
|
|
22
22
|
*/
|
|
23
23
|
createSession(e, n, o) {
|
|
@@ -25,7 +25,7 @@ class a {
|
|
|
25
25
|
}
|
|
26
26
|
/**
|
|
27
27
|
* Generates content (non-streaming).
|
|
28
|
-
* @param {Array}
|
|
28
|
+
* @param {Array} _content - The history + new message content.
|
|
29
29
|
* @returns {Promise<{text: string, usage: number}>}
|
|
30
30
|
*/
|
|
31
31
|
async generateContent(e) {
|
|
@@ -33,7 +33,7 @@ class a {
|
|
|
33
33
|
}
|
|
34
34
|
/**
|
|
35
35
|
* Generates content stream.
|
|
36
|
-
* @param {Array}
|
|
36
|
+
* @param {Array} _content - The history + new content.
|
|
37
37
|
* @returns {Promise<AsyncIterable>} Stream of chunks.
|
|
38
38
|
*/
|
|
39
39
|
async generateContentStream(e) {
|
|
@@ -41,7 +41,7 @@ class a {
|
|
|
41
41
|
}
|
|
42
42
|
/**
|
|
43
43
|
* Counts tokens.
|
|
44
|
-
* @param {Array}
|
|
44
|
+
* @param {Array} _content - The content to count.
|
|
45
45
|
* @returns {Promise<number>} Total tokens.
|
|
46
46
|
*/
|
|
47
47
|
async countTokens(e) {
|
|
@@ -180,7 +180,7 @@ class M {
|
|
|
180
180
|
t.setUint8(e + n, r.charCodeAt(n));
|
|
181
181
|
}
|
|
182
182
|
}
|
|
183
|
-
function
|
|
183
|
+
function v(s) {
|
|
184
184
|
if (!s)
|
|
185
185
|
return;
|
|
186
186
|
const t = {
|
|
@@ -204,12 +204,12 @@ function O(s) {
|
|
|
204
204
|
return T.array({
|
|
205
205
|
...t,
|
|
206
206
|
// Recursively convert the 'items' schema
|
|
207
|
-
items:
|
|
207
|
+
items: v(s.items)
|
|
208
208
|
});
|
|
209
209
|
case "object": {
|
|
210
210
|
const e = {}, r = s.properties ? Object.keys(s.properties) : [];
|
|
211
211
|
r.forEach((i) => {
|
|
212
|
-
e[i] =
|
|
212
|
+
e[i] = v(
|
|
213
213
|
s.properties[i]
|
|
214
214
|
);
|
|
215
215
|
});
|
|
@@ -348,23 +348,31 @@ class h extends EventTarget {
|
|
|
348
348
|
}
|
|
349
349
|
return "unavailable";
|
|
350
350
|
}
|
|
351
|
-
return (await h.#
|
|
351
|
+
return (await h.#p(e)).availability(t);
|
|
352
352
|
}
|
|
353
|
-
static #
|
|
354
|
-
static #
|
|
355
|
-
for (const
|
|
356
|
-
const
|
|
357
|
-
if (
|
|
358
|
-
return { ...
|
|
353
|
+
static #d = A;
|
|
354
|
+
static #h(t = globalThis) {
|
|
355
|
+
for (const n of h.#d) {
|
|
356
|
+
const o = t[n.config] || globalThis[n.config];
|
|
357
|
+
if (o && o.apiKey)
|
|
358
|
+
return { ...n, configValue: o };
|
|
359
359
|
}
|
|
360
|
-
const e = h.#
|
|
360
|
+
const e = h.#d.find(
|
|
361
|
+
(n) => n.config === "TRANSFORMERS_CONFIG"
|
|
362
|
+
);
|
|
363
|
+
if (e)
|
|
364
|
+
return {
|
|
365
|
+
...e,
|
|
366
|
+
configValue: { apiKey: "dummy", isDefault: !0 }
|
|
367
|
+
};
|
|
368
|
+
const r = h.#d.map((n) => `window.${n.config}`).join(", ");
|
|
361
369
|
throw new (t.DOMException || globalThis.DOMException)(
|
|
362
|
-
`Prompt API Polyfill: No backend configuration found. Please set one of: ${
|
|
370
|
+
`Prompt API Polyfill: No backend configuration found. Please set one of: ${r}.`,
|
|
363
371
|
"NotSupportedError"
|
|
364
372
|
);
|
|
365
373
|
}
|
|
366
|
-
static async #
|
|
367
|
-
const e = h.#
|
|
374
|
+
static async #p(t = globalThis) {
|
|
375
|
+
const e = h.#h(t);
|
|
368
376
|
return I(e.path);
|
|
369
377
|
}
|
|
370
378
|
static async #x(t = {}, e = globalThis) {
|
|
@@ -455,8 +463,8 @@ class h extends EventTarget {
|
|
|
455
463
|
"Aborted",
|
|
456
464
|
"AbortError"
|
|
457
465
|
);
|
|
458
|
-
const n = h.#
|
|
459
|
-
h.#
|
|
466
|
+
const n = h.#h(e), o = await h.#p(e), i = new o(n.configValue), a = { ...t };
|
|
467
|
+
h.#g(
|
|
460
468
|
a.responseConstraint,
|
|
461
469
|
e
|
|
462
470
|
);
|
|
@@ -565,7 +573,7 @@ class h extends EventTarget {
|
|
|
565
573
|
"Aborted",
|
|
566
574
|
"AbortError"
|
|
567
575
|
);
|
|
568
|
-
const e = JSON.parse(JSON.stringify(this.#e)), r = { ...this.#a, ...t }, n = await h.#
|
|
576
|
+
const e = JSON.parse(JSON.stringify(this.#e)), r = { ...this.#a, ...t }, n = await h.#p(this.#t), o = h.#h(this.#t), i = new n(o.configValue), a = await i.createSession(
|
|
569
577
|
r,
|
|
570
578
|
this.#n
|
|
571
579
|
);
|
|
@@ -601,17 +609,14 @@ class h extends EventTarget {
|
|
|
601
609
|
if (typeof t == "object" && t !== null && !Array.isArray(t) && Object.keys(t).length === 0)
|
|
602
610
|
return "[object Object]";
|
|
603
611
|
if (e.responseConstraint) {
|
|
604
|
-
h.#
|
|
612
|
+
h.#g(
|
|
605
613
|
e.responseConstraint,
|
|
606
614
|
this.#t
|
|
607
615
|
);
|
|
608
|
-
const l =
|
|
616
|
+
const l = v(
|
|
609
617
|
e.responseConstraint
|
|
610
618
|
);
|
|
611
|
-
this.#n.generationConfig.responseMimeType = "application/json", this.#n.generationConfig.responseSchema = l, this.#o.createSession(
|
|
612
|
-
this.#a,
|
|
613
|
-
this.#n
|
|
614
|
-
);
|
|
619
|
+
this.#n.generationConfig.responseMimeType = "application/json", this.#n.generationConfig.responseSchema = l, this.#o.createSession(this.#a, this.#n);
|
|
615
620
|
}
|
|
616
621
|
const r = this.#E(t), n = await this.#l(t);
|
|
617
622
|
if (this.#r)
|
|
@@ -642,7 +647,7 @@ class h extends EventTarget {
|
|
|
642
647
|
{ once: !0 }
|
|
643
648
|
);
|
|
644
649
|
}), a = (async () => {
|
|
645
|
-
const l = this.#
|
|
650
|
+
const l = this.#u(n);
|
|
646
651
|
if (l === "QuotaExceededError") {
|
|
647
652
|
const f = this.#t && this.#t.QuotaExceededError || this.#t && this.#t.DOMException || globalThis.QuotaExceededError || globalThis.DOMException, d = new f(
|
|
648
653
|
"The prompt is too large, it exceeds the quota.",
|
|
@@ -729,17 +734,14 @@ class h extends EventTarget {
|
|
|
729
734
|
n && n.addEventListener("abort", a);
|
|
730
735
|
try {
|
|
731
736
|
if (e.responseConstraint) {
|
|
732
|
-
h.#
|
|
737
|
+
h.#g(
|
|
733
738
|
e.responseConstraint,
|
|
734
739
|
r.#t
|
|
735
740
|
);
|
|
736
|
-
const g =
|
|
741
|
+
const g = v(
|
|
737
742
|
e.responseConstraint
|
|
738
743
|
);
|
|
739
|
-
r.#n.generationConfig.responseMimeType = "application/json", r.#n.generationConfig.responseSchema = g, r.#o.createSession(
|
|
740
|
-
r.#a,
|
|
741
|
-
r.#n
|
|
742
|
-
);
|
|
744
|
+
r.#n.generationConfig.responseMimeType = "application/json", r.#n.generationConfig.responseSchema = g, r.#o.createSession(r.#a, r.#n);
|
|
743
745
|
}
|
|
744
746
|
const l = r.#E(t), c = await r.#l(t);
|
|
745
747
|
if (r.#r)
|
|
@@ -747,7 +749,7 @@ class h extends EventTarget {
|
|
|
747
749
|
"Session is destroyed",
|
|
748
750
|
"InvalidStateError"
|
|
749
751
|
);
|
|
750
|
-
const p = { role: "user", parts: c }, w = r.#
|
|
752
|
+
const p = { role: "user", parts: c }, w = r.#u(c);
|
|
751
753
|
if (w === "QuotaExceededError") {
|
|
752
754
|
const g = r.#t && r.#t.QuotaExceededError || r.#t && r.#t.DOMException || globalThis.QuotaExceededError || globalThis.DOMException, x = new g(
|
|
753
755
|
"The prompt is too large, it exceeds the quota.",
|
|
@@ -757,8 +759,8 @@ class h extends EventTarget {
|
|
|
757
759
|
value: 22,
|
|
758
760
|
configurable: !0
|
|
759
761
|
});
|
|
760
|
-
const
|
|
761
|
-
throw x.requested =
|
|
762
|
+
const O = 1e7;
|
|
763
|
+
throw x.requested = O, x.quota = r.contextWindow, x;
|
|
762
764
|
} else if (w === "contextoverflow") {
|
|
763
765
|
r.dispatchEvent(new Event("contextoverflow")), o.enqueue("Mock response for quota overflow test."), o.close();
|
|
764
766
|
return;
|
|
@@ -798,9 +800,9 @@ class h extends EventTarget {
|
|
|
798
800
|
let x = g.text();
|
|
799
801
|
if (l && !d) {
|
|
800
802
|
E += x;
|
|
801
|
-
const
|
|
802
|
-
if (
|
|
803
|
-
x = E.slice(
|
|
803
|
+
const O = E.match(/^\s*{\s*"Rating"\s*:\s*/);
|
|
804
|
+
if (O)
|
|
805
|
+
x = E.slice(O[0].length), d = !0, E = "";
|
|
804
806
|
else if (E.length > 50)
|
|
805
807
|
x = E, d = !0, E = "";
|
|
806
808
|
else
|
|
@@ -864,7 +866,7 @@ class h extends EventTarget {
|
|
|
864
866
|
"Session is destroyed",
|
|
865
867
|
"InvalidStateError"
|
|
866
868
|
);
|
|
867
|
-
const r = this.#
|
|
869
|
+
const r = this.#u(e);
|
|
868
870
|
return r === "QuotaExceededError" ? 1e7 : r === "contextoverflow" ? 5e5 : await this.#o.countTokens([
|
|
869
871
|
{ role: "user", parts: e }
|
|
870
872
|
]) || 0;
|
|
@@ -875,7 +877,7 @@ class h extends EventTarget {
|
|
|
875
877
|
}
|
|
876
878
|
}
|
|
877
879
|
// Volkswagen mode detection to avoid cloud costs for WPT tests.
|
|
878
|
-
#
|
|
880
|
+
#u(t) {
|
|
879
881
|
return h.#y(t);
|
|
880
882
|
}
|
|
881
883
|
static #y(t) {
|
|
@@ -884,7 +886,7 @@ class h extends EventTarget {
|
|
|
884
886
|
const e = t[0].text;
|
|
885
887
|
return typeof e != "string" || !e.startsWith("Please write a sentence in English.") ? null : e.length > 1e7 ? "QuotaExceededError" : e.length > 5e4 ? "contextoverflow" : null;
|
|
886
888
|
}
|
|
887
|
-
static #
|
|
889
|
+
static #g(t, e) {
|
|
888
890
|
if (t)
|
|
889
891
|
try {
|
|
890
892
|
JSON.stringify(t);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "prompt-api-polyfill",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.12.1",
|
|
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",
|
|
@@ -47,19 +47,19 @@
|
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
49
|
"@eslint/js": "^10.0.1",
|
|
50
|
-
"eslint": "^10.0.
|
|
50
|
+
"eslint": "^10.0.3",
|
|
51
51
|
"eslint-config-prettier": "^10.1.8",
|
|
52
|
-
"globals": "^17.
|
|
52
|
+
"globals": "^17.4.0",
|
|
53
|
+
"node-addon-api": "^8.6.0",
|
|
54
|
+
"node-gyp": "^12.2.0",
|
|
53
55
|
"prettier": "^3.8.1",
|
|
54
56
|
"prettier-plugin-curly": "^0.4.1",
|
|
55
|
-
"vite": "^7.3.1"
|
|
56
|
-
"node-gyp": "^12.2.0",
|
|
57
|
-
"node-addon-api": "^8.6.0"
|
|
57
|
+
"vite": "^7.3.1"
|
|
58
58
|
},
|
|
59
59
|
"dependencies": {
|
|
60
|
-
"@google/genai": "^1.
|
|
61
|
-
"@huggingface/transformers": "^
|
|
62
|
-
"firebase": "^12.
|
|
63
|
-
"openai": "^6.
|
|
60
|
+
"@google/genai": "^1.44.0",
|
|
61
|
+
"@huggingface/transformers": "^4.0.0-next.6",
|
|
62
|
+
"firebase": "^12.10.0",
|
|
63
|
+
"openai": "^6.27.0"
|
|
64
64
|
}
|
|
65
65
|
}
|