@knowlearning/agents 0.9.170 → 0.9.172

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.
@@ -0,0 +1,1330 @@
1
+ var ie, je = new Uint8Array(16);
2
+ function Ue() {
3
+ if (!ie && (ie = typeof crypto < "u" && crypto.getRandomValues && crypto.getRandomValues.bind(crypto) || typeof msCrypto < "u" && typeof msCrypto.getRandomValues == "function" && msCrypto.getRandomValues.bind(msCrypto), !ie))
4
+ throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");
5
+ return ie(je);
6
+ }
7
+ const Be = /^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i;
8
+ function k(e) {
9
+ return typeof e == "string" && Be.test(e);
10
+ }
11
+ var j = [];
12
+ for (var he = 0; he < 256; ++he)
13
+ j.push((he + 256).toString(16).substr(1));
14
+ function Me(e) {
15
+ var t = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : 0, r = (j[e[t + 0]] + j[e[t + 1]] + j[e[t + 2]] + j[e[t + 3]] + "-" + j[e[t + 4]] + j[e[t + 5]] + "-" + j[e[t + 6]] + j[e[t + 7]] + "-" + j[e[t + 8]] + j[e[t + 9]] + "-" + j[e[t + 10]] + j[e[t + 11]] + j[e[t + 12]] + j[e[t + 13]] + j[e[t + 14]] + j[e[t + 15]]).toLowerCase();
16
+ if (!k(r))
17
+ throw TypeError("Stringified UUID is invalid");
18
+ return r;
19
+ }
20
+ var Pe, ve, ye = 0, me = 0;
21
+ function ne(e, t, r) {
22
+ var n = t && r || 0, o = t || new Array(16);
23
+ e = e || {};
24
+ var s = e.node || Pe, c = e.clockseq !== void 0 ? e.clockseq : ve;
25
+ if (s == null || c == null) {
26
+ var d = e.random || (e.rng || Ue)();
27
+ s == null && (s = Pe = [d[0] | 1, d[1], d[2], d[3], d[4], d[5]]), c == null && (c = ve = (d[6] << 8 | d[7]) & 16383);
28
+ }
29
+ var w = e.msecs !== void 0 ? e.msecs : Date.now(), u = e.nsecs !== void 0 ? e.nsecs : me + 1, i = w - ye + (u - me) / 1e4;
30
+ if (i < 0 && e.clockseq === void 0 && (c = c + 1 & 16383), (i < 0 || w > ye) && e.nsecs === void 0 && (u = 0), u >= 1e4)
31
+ throw new Error("uuid.v1(): Can't create more than 10M uuids/sec");
32
+ ye = w, me = u, ve = c, w += 122192928e5;
33
+ var a = ((w & 268435455) * 1e4 + u) % 4294967296;
34
+ o[n++] = a >>> 24 & 255, o[n++] = a >>> 16 & 255, o[n++] = a >>> 8 & 255, o[n++] = a & 255;
35
+ var h = w / 4294967296 * 1e4 & 268435455;
36
+ o[n++] = h >>> 8 & 255, o[n++] = h & 255, o[n++] = h >>> 24 & 15 | 16, o[n++] = h >>> 16 & 255, o[n++] = c >>> 8 | 128, o[n++] = c & 255;
37
+ for (var l = 0; l < 6; ++l)
38
+ o[n + l] = s[l];
39
+ return t || Me(o);
40
+ }
41
+ function Fe(e, t, r) {
42
+ e = e || {};
43
+ var n = e.random || (e.rng || Ue)();
44
+ return n[6] = n[6] & 15 | 64, n[8] = n[8] & 63 | 128, Me(n);
45
+ }
46
+ /*!
47
+ * https://github.com/Starcounter-Jack/JSON-Patch
48
+ * (c) 2017-2022 Joachim Wester
49
+ * MIT licensed
50
+ */
51
+ var ze = /* @__PURE__ */ function() {
52
+ var e = function(t, r) {
53
+ return e = Object.setPrototypeOf || { __proto__: [] } instanceof Array && function(n, o) {
54
+ n.__proto__ = o;
55
+ } || function(n, o) {
56
+ for (var s in o) o.hasOwnProperty(s) && (n[s] = o[s]);
57
+ }, e(t, r);
58
+ };
59
+ return function(t, r) {
60
+ e(t, r);
61
+ function n() {
62
+ this.constructor = t;
63
+ }
64
+ t.prototype = r === null ? Object.create(r) : (n.prototype = r.prototype, new n());
65
+ };
66
+ }(), Qe = Object.prototype.hasOwnProperty;
67
+ function ge(e, t) {
68
+ return Qe.call(e, t);
69
+ }
70
+ function Ie(e) {
71
+ if (Array.isArray(e)) {
72
+ for (var t = new Array(e.length), r = 0; r < t.length; r++)
73
+ t[r] = "" + r;
74
+ return t;
75
+ }
76
+ if (Object.keys)
77
+ return Object.keys(e);
78
+ var n = [];
79
+ for (var o in e)
80
+ ge(e, o) && n.push(o);
81
+ return n;
82
+ }
83
+ function F(e) {
84
+ switch (typeof e) {
85
+ case "object":
86
+ return JSON.parse(JSON.stringify(e));
87
+ //Faster than ES5 clone - http://jsperf.com/deep-cloning-of-objects/5
88
+ case "undefined":
89
+ return null;
90
+ //this is how JSON.stringify behaves for array items
91
+ default:
92
+ return e;
93
+ }
94
+ }
95
+ function Te(e) {
96
+ for (var t = 0, r = e.length, n; t < r; ) {
97
+ if (n = e.charCodeAt(t), n >= 48 && n <= 57) {
98
+ t++;
99
+ continue;
100
+ }
101
+ return !1;
102
+ }
103
+ return !0;
104
+ }
105
+ function ee(e) {
106
+ return e.indexOf("/") === -1 && e.indexOf("~") === -1 ? e : e.replace(/~/g, "~0").replace(/\//g, "~1");
107
+ }
108
+ function be(e) {
109
+ return e.replace(/~1/g, "/").replace(/~0/g, "~");
110
+ }
111
+ function Se(e) {
112
+ if (e === void 0)
113
+ return !0;
114
+ if (e) {
115
+ if (Array.isArray(e)) {
116
+ for (var t = 0, r = e.length; t < r; t++)
117
+ if (Se(e[t]))
118
+ return !0;
119
+ } else if (typeof e == "object") {
120
+ for (var n = Ie(e), o = n.length, s = 0; s < o; s++)
121
+ if (Se(e[n[s]]))
122
+ return !0;
123
+ }
124
+ }
125
+ return !1;
126
+ }
127
+ function Ce(e, t) {
128
+ var r = [e];
129
+ for (var n in t) {
130
+ var o = typeof t[n] == "object" ? JSON.stringify(t[n], null, 2) : t[n];
131
+ typeof o < "u" && r.push(n + ": " + o);
132
+ }
133
+ return r.join(`
134
+ `);
135
+ }
136
+ var xe = (
137
+ /** @class */
138
+ function(e) {
139
+ ze(t, e);
140
+ function t(r, n, o, s, c) {
141
+ var d = this.constructor, w = e.call(this, Ce(r, { name: n, index: o, operation: s, tree: c })) || this;
142
+ return w.name = n, w.index = o, w.operation = s, w.tree = c, Object.setPrototypeOf(w, d.prototype), w.message = Ce(r, { name: n, index: o, operation: s, tree: c }), w;
143
+ }
144
+ return t;
145
+ }(Error)
146
+ ), U = xe, Ke = F, re = {
147
+ add: function(e, t, r) {
148
+ return e[t] = this.value, { newDocument: r };
149
+ },
150
+ remove: function(e, t, r) {
151
+ var n = e[t];
152
+ return delete e[t], { newDocument: r, removed: n };
153
+ },
154
+ replace: function(e, t, r) {
155
+ var n = e[t];
156
+ return e[t] = this.value, { newDocument: r, removed: n };
157
+ },
158
+ move: function(e, t, r) {
159
+ var n = ue(r, this.path);
160
+ n && (n = F(n));
161
+ var o = te(r, { op: "remove", path: this.from }).removed;
162
+ return te(r, { op: "add", path: this.path, value: o }), { newDocument: r, removed: n };
163
+ },
164
+ copy: function(e, t, r) {
165
+ var n = ue(r, this.from);
166
+ return te(r, { op: "add", path: this.path, value: F(n) }), { newDocument: r };
167
+ },
168
+ test: function(e, t, r) {
169
+ return { newDocument: r, test: oe(e[t], this.value) };
170
+ },
171
+ _get: function(e, t, r) {
172
+ return this.value = e[t], { newDocument: r };
173
+ }
174
+ }, We = {
175
+ add: function(e, t, r) {
176
+ return Te(t) ? e.splice(t, 0, this.value) : e[t] = this.value, { newDocument: r, index: t };
177
+ },
178
+ remove: function(e, t, r) {
179
+ var n = e.splice(t, 1);
180
+ return { newDocument: r, removed: n[0] };
181
+ },
182
+ replace: function(e, t, r) {
183
+ var n = e[t];
184
+ return e[t] = this.value, { newDocument: r, removed: n };
185
+ },
186
+ move: re.move,
187
+ copy: re.copy,
188
+ test: re.test,
189
+ _get: re._get
190
+ };
191
+ function ue(e, t) {
192
+ if (t == "")
193
+ return e;
194
+ var r = { op: "_get", path: t };
195
+ return te(e, r), r.value;
196
+ }
197
+ function te(e, t, r, n, o, s) {
198
+ if (r === void 0 && (r = !1), n === void 0 && (n = !0), o === void 0 && (o = !0), s === void 0 && (s = 0), r && (typeof r == "function" ? r(t, 0, e, t.path) : le(t, 0)), t.path === "") {
199
+ var c = { newDocument: e };
200
+ if (t.op === "add")
201
+ return c.newDocument = t.value, c;
202
+ if (t.op === "replace")
203
+ return c.newDocument = t.value, c.removed = e, c;
204
+ if (t.op === "move" || t.op === "copy")
205
+ return c.newDocument = ue(e, t.from), t.op === "move" && (c.removed = e), c;
206
+ if (t.op === "test") {
207
+ if (c.test = oe(e, t.value), c.test === !1)
208
+ throw new U("Test operation failed", "TEST_OPERATION_FAILED", s, t, e);
209
+ return c.newDocument = e, c;
210
+ } else {
211
+ if (t.op === "remove")
212
+ return c.removed = e, c.newDocument = null, c;
213
+ if (t.op === "_get")
214
+ return t.value = e, c;
215
+ if (r)
216
+ throw new U("Operation `op` property is not one of operations defined in RFC-6902", "OPERATION_OP_INVALID", s, t, e);
217
+ return c;
218
+ }
219
+ } else {
220
+ n || (e = F(e));
221
+ var d = t.path || "", w = d.split("/"), u = e, i = 1, a = w.length, h = void 0, l = void 0, E = void 0;
222
+ for (typeof r == "function" ? E = r : E = le; ; ) {
223
+ if (l = w[i], l && l.indexOf("~") != -1 && (l = be(l)), o && (l == "__proto__" || l == "prototype" && i > 0 && w[i - 1] == "constructor"))
224
+ throw new TypeError("JSON-Patch: modifying `__proto__` or `constructor/prototype` prop is banned for security reasons, if this was on purpose, please set `banPrototypeModifications` flag false and pass it to this function. More info in fast-json-patch README");
225
+ if (r && h === void 0 && (u[l] === void 0 ? h = w.slice(0, i).join("/") : i == a - 1 && (h = t.path), h !== void 0 && E(t, 0, e, h)), i++, Array.isArray(u)) {
226
+ if (l === "-")
227
+ l = u.length;
228
+ else {
229
+ if (r && !Te(l))
230
+ throw new U("Expected an unsigned base-10 integer value, making the new referenced value the array element with the zero-based index", "OPERATION_PATH_ILLEGAL_ARRAY_INDEX", s, t, e);
231
+ Te(l) && (l = ~~l);
232
+ }
233
+ if (i >= a) {
234
+ if (r && t.op === "add" && l > u.length)
235
+ throw new U("The specified index MUST NOT be greater than the number of elements in the array", "OPERATION_VALUE_OUT_OF_BOUNDS", s, t, e);
236
+ var c = We[t.op].call(t, u, l, e);
237
+ if (c.test === !1)
238
+ throw new U("Test operation failed", "TEST_OPERATION_FAILED", s, t, e);
239
+ return c;
240
+ }
241
+ } else if (i >= a) {
242
+ var c = re[t.op].call(t, u, l, e);
243
+ if (c.test === !1)
244
+ throw new U("Test operation failed", "TEST_OPERATION_FAILED", s, t, e);
245
+ return c;
246
+ }
247
+ if (u = u[l], r && i < a && (!u || typeof u != "object"))
248
+ throw new U("Cannot perform operation at the desired path", "OPERATION_PATH_UNRESOLVABLE", s, t, e);
249
+ }
250
+ }
251
+ }
252
+ function de(e, t, r, n, o) {
253
+ if (n === void 0 && (n = !0), o === void 0 && (o = !0), r && !Array.isArray(t))
254
+ throw new U("Patch sequence must be an array", "SEQUENCE_NOT_AN_ARRAY");
255
+ n || (e = F(e));
256
+ for (var s = new Array(t.length), c = 0, d = t.length; c < d; c++)
257
+ s[c] = te(e, t[c], r, !0, o, c), e = s[c].newDocument;
258
+ return s.newDocument = e, s;
259
+ }
260
+ function Ye(e, t, r) {
261
+ var n = te(e, t);
262
+ if (n.test === !1)
263
+ throw new U("Test operation failed", "TEST_OPERATION_FAILED", r, t, e);
264
+ return n.newDocument;
265
+ }
266
+ function le(e, t, r, n) {
267
+ if (typeof e != "object" || e === null || Array.isArray(e))
268
+ throw new U("Operation is not an object", "OPERATION_NOT_AN_OBJECT", t, e, r);
269
+ if (re[e.op]) {
270
+ if (typeof e.path != "string")
271
+ throw new U("Operation `path` property is not a string", "OPERATION_PATH_INVALID", t, e, r);
272
+ if (e.path.indexOf("/") !== 0 && e.path.length > 0)
273
+ throw new U('Operation `path` property must start with "/"', "OPERATION_PATH_INVALID", t, e, r);
274
+ if ((e.op === "move" || e.op === "copy") && typeof e.from != "string")
275
+ throw new U("Operation `from` property is not present (applicable in `move` and `copy` operations)", "OPERATION_FROM_REQUIRED", t, e, r);
276
+ if ((e.op === "add" || e.op === "replace" || e.op === "test") && e.value === void 0)
277
+ throw new U("Operation `value` property is not present (applicable in `add`, `replace` and `test` operations)", "OPERATION_VALUE_REQUIRED", t, e, r);
278
+ if ((e.op === "add" || e.op === "replace" || e.op === "test") && Se(e.value))
279
+ throw new U("Operation `value` property is not present (applicable in `add`, `replace` and `test` operations)", "OPERATION_VALUE_CANNOT_CONTAIN_UNDEFINED", t, e, r);
280
+ if (r) {
281
+ if (e.op == "add") {
282
+ var o = e.path.split("/").length, s = n.split("/").length;
283
+ if (o !== s + 1 && o !== s)
284
+ throw new U("Cannot perform an `add` operation at the desired path", "OPERATION_PATH_CANNOT_ADD", t, e, r);
285
+ } else if (e.op === "replace" || e.op === "remove" || e.op === "_get") {
286
+ if (e.path !== n)
287
+ throw new U("Cannot perform the operation at a path that does not exist", "OPERATION_PATH_UNRESOLVABLE", t, e, r);
288
+ } else if (e.op === "move" || e.op === "copy") {
289
+ var c = { op: "_get", path: e.from, value: void 0 }, d = $e([c], r);
290
+ if (d && d.name === "OPERATION_PATH_UNRESOLVABLE")
291
+ throw new U("Cannot perform the operation from a path that does not exist", "OPERATION_FROM_UNRESOLVABLE", t, e, r);
292
+ }
293
+ }
294
+ } else throw new U("Operation `op` property is not one of operations defined in RFC-6902", "OPERATION_OP_INVALID", t, e, r);
295
+ }
296
+ function $e(e, t, r) {
297
+ try {
298
+ if (!Array.isArray(e))
299
+ throw new U("Patch sequence must be an array", "SEQUENCE_NOT_AN_ARRAY");
300
+ if (t)
301
+ de(F(t), F(e), r || !0);
302
+ else {
303
+ r = r || le;
304
+ for (var n = 0; n < e.length; n++)
305
+ r(e[n], n, t, void 0);
306
+ }
307
+ } catch (o) {
308
+ if (o instanceof U)
309
+ return o;
310
+ throw o;
311
+ }
312
+ }
313
+ function oe(e, t) {
314
+ if (e === t)
315
+ return !0;
316
+ if (e && t && typeof e == "object" && typeof t == "object") {
317
+ var r = Array.isArray(e), n = Array.isArray(t), o, s, c;
318
+ if (r && n) {
319
+ if (s = e.length, s != t.length)
320
+ return !1;
321
+ for (o = s; o-- !== 0; )
322
+ if (!oe(e[o], t[o]))
323
+ return !1;
324
+ return !0;
325
+ }
326
+ if (r != n)
327
+ return !1;
328
+ var d = Object.keys(e);
329
+ if (s = d.length, s !== Object.keys(t).length)
330
+ return !1;
331
+ for (o = s; o-- !== 0; )
332
+ if (!t.hasOwnProperty(d[o]))
333
+ return !1;
334
+ for (o = s; o-- !== 0; )
335
+ if (c = d[o], !oe(e[c], t[c]))
336
+ return !1;
337
+ return !0;
338
+ }
339
+ return e !== e && t !== t;
340
+ }
341
+ const Ze = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
342
+ __proto__: null,
343
+ JsonPatchError: U,
344
+ _areEquals: oe,
345
+ applyOperation: te,
346
+ applyPatch: de,
347
+ applyReducer: Ye,
348
+ deepClone: Ke,
349
+ getValueByPointer: ue,
350
+ validate: $e,
351
+ validator: le
352
+ }, Symbol.toStringTag, { value: "Module" }));
353
+ /*!
354
+ * https://github.com/Starcounter-Jack/JSON-Patch
355
+ * (c) 2017-2021 Joachim Wester
356
+ * MIT license
357
+ */
358
+ var _e = /* @__PURE__ */ new WeakMap(), ke = (
359
+ /** @class */
360
+ /* @__PURE__ */ function() {
361
+ function e(t) {
362
+ this.observers = /* @__PURE__ */ new Map(), this.obj = t;
363
+ }
364
+ return e;
365
+ }()
366
+ ), Xe = (
367
+ /** @class */
368
+ /* @__PURE__ */ function() {
369
+ function e(t, r) {
370
+ this.callback = t, this.observer = r;
371
+ }
372
+ return e;
373
+ }()
374
+ );
375
+ function Ve(e) {
376
+ return _e.get(e);
377
+ }
378
+ function et(e, t) {
379
+ return e.observers.get(t);
380
+ }
381
+ function tt(e, t) {
382
+ e.observers.delete(t.callback);
383
+ }
384
+ function nt(e, t) {
385
+ t.unobserve();
386
+ }
387
+ function rt(e, t) {
388
+ var r = [], n, o = Ve(e);
389
+ if (!o)
390
+ o = new ke(e), _e.set(e, o);
391
+ else {
392
+ var s = et(o, t);
393
+ n = s && s.observer;
394
+ }
395
+ if (n)
396
+ return n;
397
+ if (n = {}, o.value = F(e), t) {
398
+ n.callback = t, n.next = null;
399
+ var c = function() {
400
+ Ne(n);
401
+ }, d = function() {
402
+ clearTimeout(n.next), n.next = setTimeout(c);
403
+ };
404
+ typeof window < "u" && (window.addEventListener("mouseup", d), window.addEventListener("keyup", d), window.addEventListener("mousedown", d), window.addEventListener("keydown", d), window.addEventListener("change", d));
405
+ }
406
+ return n.patches = r, n.object = e, n.unobserve = function() {
407
+ Ne(n), clearTimeout(n.next), tt(o, n), typeof window < "u" && (window.removeEventListener("mouseup", d), window.removeEventListener("keyup", d), window.removeEventListener("mousedown", d), window.removeEventListener("keydown", d), window.removeEventListener("change", d));
408
+ }, o.observers.set(t, new Xe(t, n)), n;
409
+ }
410
+ function Ne(e, t) {
411
+ t === void 0 && (t = !1);
412
+ var r = _e.get(e.object);
413
+ Re(r.value, e.object, e.patches, "", t), e.patches.length && de(r.value, e.patches);
414
+ var n = e.patches;
415
+ return n.length > 0 && (e.patches = [], e.callback && e.callback(n)), n;
416
+ }
417
+ function Re(e, t, r, n, o) {
418
+ if (t !== e) {
419
+ typeof t.toJSON == "function" && (t = t.toJSON());
420
+ for (var s = Ie(t), c = Ie(e), d = !1, w = c.length - 1; w >= 0; w--) {
421
+ var u = c[w], i = e[u];
422
+ if (ge(t, u) && !(t[u] === void 0 && i !== void 0 && Array.isArray(t) === !1)) {
423
+ var a = t[u];
424
+ typeof i == "object" && i != null && typeof a == "object" && a != null && Array.isArray(i) === Array.isArray(a) ? Re(i, a, r, n + "/" + ee(u), o) : i !== a && (o && r.push({ op: "test", path: n + "/" + ee(u), value: F(i) }), r.push({ op: "replace", path: n + "/" + ee(u), value: F(a) }));
425
+ } else Array.isArray(e) === Array.isArray(t) ? (o && r.push({ op: "test", path: n + "/" + ee(u), value: F(i) }), r.push({ op: "remove", path: n + "/" + ee(u) }), d = !0) : (o && r.push({ op: "test", path: n, value: e }), r.push({ op: "replace", path: n, value: t }));
426
+ }
427
+ if (!(!d && s.length == c.length))
428
+ for (var w = 0; w < s.length; w++) {
429
+ var u = s[w];
430
+ !ge(e, u) && t[u] !== void 0 && r.push({ op: "add", path: n + "/" + ee(u), value: F(t[u]) });
431
+ }
432
+ }
433
+ }
434
+ function ot(e, t, r) {
435
+ r === void 0 && (r = !1);
436
+ var n = [];
437
+ return Re(e, t, n, "", r), n;
438
+ }
439
+ const st = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
440
+ __proto__: null,
441
+ compare: ot,
442
+ generate: Ne,
443
+ observe: rt,
444
+ unobserve: nt
445
+ }, Symbol.toStringTag, { value: "Module" }));
446
+ Object.assign({}, Ze, st, {
447
+ JsonPatchError: xe,
448
+ deepClone: F,
449
+ escapePathComponent: ee,
450
+ unescapePathComponent: be
451
+ });
452
+ const it = "https://auth.knowlearning.systems", at = `-----BEGIN PUBLIC KEY-----
453
+ MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA59Uz6jvBJF3B8/7xMqGo
454
+ XkIhLFvTCHuFIGuCNNZGCJUnSk2ne6Jp1ehUIarliJwzrvfr2HMe0PvzAJyZqQIs
455
+ uz0Lt867TTojCAKJunxbcrwEhzvz0FNjNu1wpgkSHFvd1uTvRSZqauqUmG0HqC17
456
+ HSmBaXivB49B/pviowVJc+mUJJ9MROtOiL4JN5niHnLbt6QVi6NITAJkOwtoRhck
457
+ 5j0KLvfrq18R8QrfDOq3v5hWlrA6j1wPvTW1mzFk8MrOZw935mMDdMivFAm/DltM
458
+ NT5I3YnLZpcl1e/fydC+B6zSz2nZfLb2iDBbADDVj2+i9JUEFomg6ng1DjHUGMYc
459
+ ZQIDAQAB
460
+ -----END PUBLIC KEY-----
461
+ `;
462
+ if (window.location.pathname.startsWith("/auth/")) {
463
+ const e = window.location.pathname.slice(6), [t] = e.split("/", 1), r = window.localStorage.getItem(t);
464
+ if (r) {
465
+ const n = e.slice(t.length + 1);
466
+ window.localStorage.setItem("token", n), window.location.href = r;
467
+ }
468
+ }
469
+ async function ct(e = "google", t) {
470
+ const r = Math.random().toString(36).substring(2);
471
+ if (window.localStorage.setItem(r, window.location.href), e === "code") {
472
+ const n = { code: t, provider: e, domain: window.location.host }, o = await lt(at, JSON.stringify(n));
473
+ window.location.href = `/auth/${r}/${o}`;
474
+ } else {
475
+ const n = encodeURIComponent(window.location.href);
476
+ window.location.href = `${it}/${e}/${r}/${n}`;
477
+ }
478
+ }
479
+ function ft() {
480
+ window.localStorage.setItem("token", Math.random().toString(36).substring(2)), window.location.reload();
481
+ }
482
+ async function ut() {
483
+ const e = localStorage.getItem("token");
484
+ return window.localStorage.removeItem("token"), e;
485
+ }
486
+ async function lt(e, t) {
487
+ const r = await crypto.subtle.importKey(
488
+ "spki",
489
+ dt(e),
490
+ { name: "RSA-OAEP", hash: "SHA-256" },
491
+ !0,
492
+ ["encrypt"]
493
+ ), n = await crypto.subtle.generateKey(
494
+ { name: "AES-GCM", length: 256 },
495
+ !0,
496
+ ["encrypt", "decrypt"]
497
+ ), o = new TextEncoder().encode(t), s = crypto.getRandomValues(new Uint8Array(12)), c = await crypto.subtle.encrypt(
498
+ { name: "AES-GCM", iv: s },
499
+ n,
500
+ o
501
+ ), d = await crypto.subtle.exportKey("raw", n), w = await crypto.subtle.encrypt(
502
+ { name: "RSA-OAEP" },
503
+ r,
504
+ d
505
+ );
506
+ return [
507
+ encodeURIComponent(btoa(String.fromCharCode(...new Uint8Array(s)))),
508
+ encodeURIComponent(btoa(String.fromCharCode(...new Uint8Array(c)))),
509
+ encodeURIComponent(btoa(String.fromCharCode(...new Uint8Array(w))))
510
+ ].join(",");
511
+ }
512
+ function dt(e) {
513
+ const t = e.replace(/-----[^-]+-----/g, "").replace(/\s/g, ""), r = atob(t), n = new Uint8Array(r.length);
514
+ for (let o = 0; o < r.length; o++)
515
+ n[o] = r.charCodeAt(o);
516
+ return n.buffer;
517
+ }
518
+ function pt(e) {
519
+ return e.map((t) => {
520
+ const r = { ...t, path: "/" + t.path.map(De).join("/") };
521
+ return t.from && (r.from = "/" + t.from.map(De).join("/")), r;
522
+ });
523
+ }
524
+ function De(e) {
525
+ return typeof e == "string" ? e.replaceAll("~", "~0").replaceAll("/", "~1") : e;
526
+ }
527
+ const Ee = /* @__PURE__ */ new Set(), ae = /* @__PURE__ */ new Map();
528
+ function ce(e) {
529
+ return `/${e.join("/")}`;
530
+ }
531
+ function Le(e) {
532
+ return JSON.parse(JSON.stringify(e));
533
+ }
534
+ function wt(e, [t, r]) {
535
+ return Math.max(Math.min(parseInt(e), r), t);
536
+ }
537
+ function se(e, t, r = {}, n = [], o) {
538
+ if (r[ce(n)]) return e;
539
+ if (Ee.has(e))
540
+ throw new Error(`Cannot add mutable state to multiple mutable parents. Attempted Path: ${ce(n)}`);
541
+ const s = Array.isArray(e), c = (i, a) => se(
542
+ a,
543
+ t,
544
+ r,
545
+ [...n, i]
546
+ ), d = {
547
+ copyWithin() {
548
+ throw new Error('"copyWithin" not implemented.');
549
+ },
550
+ sort(i) {
551
+ const a = /* @__PURE__ */ new Map();
552
+ e.forEach((l, E) => {
553
+ a.has(l) || a.set(l, []), a.get(l).push(E);
554
+ }), e.sort(i);
555
+ let h = e.map((l) => a.get(l).shift());
556
+ return t(
557
+ e.map((l, E) => {
558
+ const v = ae.get(l);
559
+ v && (v[v.length - 1] = E);
560
+ const g = h[E];
561
+ return h = h.map((O) => O < g ? O + 1 : O), {
562
+ op: "move",
563
+ from: [...n, g],
564
+ path: [...n, E]
565
+ };
566
+ })
567
+ ), u;
568
+ },
569
+ reverse() {
570
+ const i = [];
571
+ return e.forEach((a, h) => {
572
+ i.push({
573
+ op: "move",
574
+ from: [...n, h],
575
+ path: [...n, 0]
576
+ });
577
+ const l = ae.get(a);
578
+ l && (l[l.length - 1] = e.length - 1 - h);
579
+ }), e.reverse(), t(i), u;
580
+ },
581
+ shift() {
582
+ return d.splice(0, 1)[0];
583
+ },
584
+ unshift() {
585
+ return d.splice(0, 0, ...arguments), e.length;
586
+ },
587
+ splice(i, a, ...h) {
588
+ const l = e.length - i;
589
+ if (arguments.length === 0) return;
590
+ arguments.length === 1 || arguments[1] === 1 / 0 ? a = l : a = wt(a, [0, l]) || 0;
591
+ const E = [...n, i], v = Array.from(
592
+ { length: a },
593
+ () => ({ op: "remove", path: E })
594
+ );
595
+ h.forEach((m, y) => {
596
+ v.push({
597
+ op: "add",
598
+ path: [...n, i + y],
599
+ value: Le(m)
600
+ });
601
+ });
602
+ const g = h.length - a;
603
+ g !== 0 && e.slice(i + a).forEach((m) => {
604
+ const y = ae.get(m);
605
+ y && (y[y.length - 1] = y[y.length - 1] + g);
606
+ });
607
+ const O = h.map(
608
+ (m, y) => m instanceof Object ? c(y + i, m) : m
609
+ ), I = e.splice(i, a, ...O);
610
+ return t(v), I;
611
+ }
612
+ };
613
+ Object.entries(e).filter(([, i]) => i instanceof Object).forEach(([i, a]) => {
614
+ s && /^\d+$/.test(i) && (i = parseInt(i)), e[i] = c(i, a);
615
+ });
616
+ const w = {
617
+ set(i, a, h) {
618
+ if (s)
619
+ if (/^\d+$/.test(a))
620
+ a = parseInt(a);
621
+ else return i[a] = h, !0;
622
+ const l = [...n, a], E = ce(l);
623
+ if (r[E])
624
+ return i[a] = h, !0;
625
+ if (h === void 0)
626
+ throw new Error(`Setting properties to undefined is not supported. Please use a delete statement. Attempted to set ${E}`);
627
+ return t([{
628
+ op: i[a] === void 0 ? "add" : "replace",
629
+ value: Le(h),
630
+ // TODO: more efficient sanitization
631
+ path: l
632
+ }]), h instanceof Object ? i[a] = c(a, h) : i[a] = h, !0;
633
+ },
634
+ deleteProperty(i, a) {
635
+ if (a in i) {
636
+ if (Ee.delete(u), delete i[a], s)
637
+ if (/^\d+$/.test(a)) a = parseInt(a);
638
+ else return !0;
639
+ const h = [...n, a];
640
+ r[ce(h)] || t([{ op: "remove", path: h }]);
641
+ }
642
+ return !0;
643
+ },
644
+ get(i, a, h) {
645
+ return s && d[a] ? d[a] : i[a];
646
+ }
647
+ }, u = new Proxy(e, w);
648
+ return Ee.add(u), ae.set(u, n), u;
649
+ }
650
+ const ht = 1e4, vt = { open: !0, mutate: !0, close: !0 };
651
+ function yt(e) {
652
+ return structuredClone(e).filter(({ path: t }) => t.shift() === "active");
653
+ }
654
+ function mt({ token: e, sid: t, domain: r, Connection: n, watchers: o, states: s, applyPatch: c, log: d, login: w, reboot: u, handleDomainMessage: i, trigger: a, variables: h = {} }) {
655
+ let l, E, v = !1, g, O, I = -1, m = 0;
656
+ const y = [];
657
+ let N = -1, _, M = null, $ = null, z = !1, Y = !1;
658
+ const X = [], p = {};
659
+ let A;
660
+ const T = new Promise((f) => A = f), R = {
661
+ loaded: Date.now(),
662
+ connected: null,
663
+ authenticated: null
664
+ };
665
+ async function P() {
666
+ return { variables: h, ...await T, context: [] };
667
+ }
668
+ function H({ scope: f, patch: C, context: b }) {
669
+ if (M === f) {
670
+ const G = y.length - 1;
671
+ y[G].patch = [...y[G].patch, ...C];
672
+ } else
673
+ I += 1, $ = new Promise((G, L) => p[I] = [[G, L]]), y.push({ scope: f, patch: C, context: b, si: I, ts: Date.now() }), M = f, q();
674
+ return $;
675
+ }
676
+ async function q() {
677
+ for (await new Promise((f) => f()), M = null; v && N + 1 < y.length; ) {
678
+ M = null;
679
+ try {
680
+ l.send(y[N + 1]), N += 1, await new Promise((f) => f());
681
+ } catch (f) {
682
+ console.warn("ERROR SENDING OVER CONNECTION", f), B();
683
+ break;
684
+ }
685
+ }
686
+ }
687
+ function Q() {
688
+ const f = Object.keys(p).map(parseInt).sort()[0] || 1 / 0;
689
+ for (; X[0] && X[0].si < f; )
690
+ X.shift().resolve();
691
+ }
692
+ function J() {
693
+ clearTimeout(_), _ = setTimeout(
694
+ () => {
695
+ d("CLOSING DUE TO HEARTBEAT TIMEOUT"), B();
696
+ },
697
+ ht
698
+ );
699
+ }
700
+ async function B() {
701
+ z || (v = !1, l.onmessage = () => {
702
+ }, Y || (await new Promise((f) => setTimeout(f, Math.min(1e3, m * 100))), z = !0, m += 1, K(), z = !1));
703
+ }
704
+ function K() {
705
+ l = new n(), l.onopen = async () => {
706
+ R.connected || (R.connected = Date.now()), d("AUTHORIZING NEWLY OPENED CONNECTION FOR SESSION:", g), m = 0, l.send({ token: await e(), sid: await (t == null ? void 0 : t()), session: g, domain: r });
707
+ }, l.onmessage = async (f) => {
708
+ if (J(), !!f)
709
+ try {
710
+ if (f.error && console.warn("ERROR RESPONSE", f), v)
711
+ if (vt[f.type])
712
+ try {
713
+ i && i(f, a);
714
+ } catch {
715
+ d("ERROR HANDLING DOMAIN MESSAGE", f);
716
+ }
717
+ else if (f.si !== void 0)
718
+ p[f.si] ? (p[f.si].forEach(([C, b]) => f.error ? b(f) : C(f)), delete p[f.si], l.send({ ack: f.si }), Q()) : console.warn("received MULTIPLE responses for message with si", f.si, f);
719
+ else {
720
+ const C = f.domain === r ? "" : f.domain, b = f.user === E ? "" : f.user, G = f.scope, L = k(G) ? G : `${C}/${b}/${G}`;
721
+ if (o[L]) {
722
+ if (s[L] = await s[L], s[L].ii + 1 !== f.ii)
723
+ return;
724
+ s[L].ii = f.ii;
725
+ const W = f.patch.findLastIndex((V) => V.path.length === 0);
726
+ W > -1 && (s[L] = f.patch[W].value), s[L].active === void 0 && (s[L].active = {}), c(s[L], pt(f.patch.slice(W + 1))), o[L].forEach((V) => {
727
+ const we = structuredClone(s[L].active);
728
+ V({ ...f, patch: yt(f.patch), state: we });
729
+ });
730
+ }
731
+ }
732
+ else {
733
+ if (f.error) return w();
734
+ v = !0, E ? O !== f.server ? (console.warn(`REBOOTING DUE TO SERVER SWITCH ${O} -> ${f.server}`, f), u()) : N = f.ack : (console.log("INIT MESSAGE", f), R.authenticated = Date.now(), E = f.auth.user, g = f.session, O = f.server, A(f)), q();
735
+ }
736
+ } catch (C) {
737
+ console.error("ERROR HANDLING CONNECTION MESSAGE", C, f);
738
+ }
739
+ }, l.onerror = async (f) => {
740
+ d("CONNECTION ERROR", f.message);
741
+ }, l.onclose = async (f) => {
742
+ d("CONNECTION CLOSURE", f.message), B();
743
+ }, J();
744
+ }
745
+ async function Z() {
746
+ const f = new Promise((C) => X.push({ si: N, resolve: C }));
747
+ return Q(), f;
748
+ }
749
+ function pe() {
750
+ return new Promise((f, C) => p[I].push([f, C]));
751
+ }
752
+ function S() {
753
+ d("DISCONNECTED AGENT!!!!!!!!!!!!!!!"), Y = !0, l.close({ keepalive: !0 });
754
+ }
755
+ function D() {
756
+ d("RECONNECTED AGENT!!!!!!!!!!!!!!!"), Y = !1, B();
757
+ }
758
+ return K(), [H, pe, S, D, Z, P];
759
+ }
760
+ function Et(e = "[]", t, r, { keyToSubscriptionId: n, watchers: o, states: s, create: c, environment: d, lastMessageResponse: w, lastInteractionResponse: u, interact: i, log: a }) {
761
+ let h, l = new Promise((v) => h = v);
762
+ const E = new Promise(async (v, g) => {
763
+ const { auth: { user: O }, domain: I, session: m } = await d(), y = k(e) ? e : `${!r || r === I ? "" : r}/${!t || t === O ? "" : t}/${e}`;
764
+ if (!n[y]) {
765
+ const N = Fe();
766
+ n[y] = N, o[y] = [], s[y] = new Promise(async (_, M) => {
767
+ await new Promise(($) => setTimeout($)), i("sessions", [{
768
+ op: "add",
769
+ path: ["active", m, "subscriptions", N],
770
+ value: { scope: e, user: t, domain: r, ii: null }
771
+ }], !1);
772
+ try {
773
+ _(await w());
774
+ } catch ($) {
775
+ M($);
776
+ }
777
+ });
778
+ }
779
+ await u[y];
780
+ try {
781
+ const N = structuredClone(await s[y]), _ = N.active;
782
+ delete N.active, h(N), v(new se(_ || {}, (M) => {
783
+ const $ = structuredClone(M);
784
+ $.forEach((z) => z.path.unshift("active")), i(e, $);
785
+ }));
786
+ } catch (N) {
787
+ g(N);
788
+ }
789
+ });
790
+ return E.metadata = l, E;
791
+ }
792
+ const At = "[]";
793
+ function qe({ metadata: e, environment: t, state: r, watchers: n, synced: o, sentUpdates: s }) {
794
+ function c(u = At, i, a, h) {
795
+ if (Array.isArray(u)) return d(u, i, a, h);
796
+ const l = r(u, a, h);
797
+ let E, v = !1;
798
+ return e(u, a, h).then(async ({ ii: g }) => {
799
+ const { auth: { user: O }, domain: I } = await t(), m = await l;
800
+ v || (E = k(u) ? u : `${!h || h === I ? "" : h}/${!a || a === O ? "" : a}/${u}`, i({ scope: u, user: a, domain: h, state: m, patch: null, ii: g }), s && (s[E] = g), !v && (n[E] || (n[E] = []), n[E].push(i)));
801
+ }), () => {
802
+ v = !0, E && w(E, i);
803
+ };
804
+ }
805
+ function d(u, i, a, h) {
806
+ const l = u[0], E = u.slice(1);
807
+ let v = () => {
808
+ };
809
+ const O = c(l, ({ state: I }) => {
810
+ if (E.length === 0) {
811
+ i(I);
812
+ return;
813
+ }
814
+ v();
815
+ let m = I;
816
+ for (let y = 0; y < E.length; y += 1)
817
+ if (m = m[E[y]], m == null || y === E.length - 1) i(m);
818
+ else if (k(m)) {
819
+ v = d([m, ...E.slice(y + 1)], i, a, h);
820
+ return;
821
+ }
822
+ }, a, h);
823
+ return () => {
824
+ O(), v();
825
+ };
826
+ }
827
+ function w(u, i) {
828
+ if (!n[u]) {
829
+ console.warn("NO WATCHERS FOR KEY", u, i);
830
+ return;
831
+ }
832
+ const a = n[u].findIndex((h) => h === i);
833
+ a > -1 && n[u].splice(a, 1);
834
+ }
835
+ return [c, w];
836
+ }
837
+ const Ot = "application/json;type=download";
838
+ function Je(e, { create: t, lastMessageResponse: r, fetch: n, metadata: o }) {
839
+ t({
840
+ active_type: Ot,
841
+ active: { id: e }
842
+ });
843
+ let s = "fetch";
844
+ const c = new Promise(async (d, w) => {
845
+ const { url: u } = await r();
846
+ if (await new Promise((i) => setTimeout(i)), s === "url") d(u);
847
+ else if (s === "fetch") {
848
+ const i = await n(u), { ok: a, statusText: h } = i;
849
+ a ? d(i) : w(h);
850
+ } else if (s === "direct") {
851
+ const i = await Je(e, { create: t, lastMessageResponse: r, fetch: n, metadata: o }), { name: a } = await o(e), h = i.headers.get("Content-Type"), l = new Blob([await i.blob()], { type: h }), E = window.URL.createObjectURL(l), v = document.createElement("a");
852
+ v.style.display = "none", v.href = E, v.download = a, document.body.appendChild(v), v.click(), window.URL.revokeObjectURL(E), d();
853
+ }
854
+ });
855
+ return c.direct = () => (s = "direct", c), c.url = () => (s = "url", c), c;
856
+ }
857
+ const Ae = "[]", gt = "application/json;type=upload", It = "application/json;type=domain-claim";
858
+ function Tt({ Connection: e, domain: t, token: r, sid: n, uuid: o, fetch: s, applyPatch: c, login: d, logout: w, reboot: u, handleDomainMessage: i, log: a = console.log, variables: h = {} }) {
859
+ const l = {}, E = {}, v = {}, g = {};
860
+ A("INITIALIZING AGENT CONNECTION");
861
+ const [
862
+ O,
863
+ I,
864
+ m,
865
+ y,
866
+ N,
867
+ _
868
+ ] = mt({ token: r, sid: n, domain: t, Connection: e, watchers: E, states: l, applyPatch: c, log: A, login: d, interact: P, reboot: u, trigger: pe, handleDomainMessage: i, variables: h });
869
+ _().then(({ session: S }) => {
870
+ P("sessions", [{ op: "add", path: ["active", S], value: { queries: {}, subscriptions: {} } }], !1);
871
+ });
872
+ const M = {
873
+ keyToSubscriptionId: v,
874
+ watchers: E,
875
+ states: l,
876
+ state: Y,
877
+ create: T,
878
+ environment: _,
879
+ lastInteractionResponse: g,
880
+ lastMessageResponse: I,
881
+ interact: P,
882
+ fetch: s,
883
+ synced: N,
884
+ metadata: J,
885
+ log: A
886
+ }, [$, z] = qe(M);
887
+ function Y(S, D, f) {
888
+ return Et(S, D, f, M);
889
+ }
890
+ function X(S) {
891
+ return Je(S, M);
892
+ }
893
+ function p() {
894
+ }
895
+ function A() {
896
+ a(...arguments);
897
+ }
898
+ function T({ id: S = o(), active_type: D, active: f, name: C }) {
899
+ D || (D = "application/json");
900
+ const b = [
901
+ { op: "add", path: ["active_type"], value: D },
902
+ { op: "add", path: ["active"], value: f }
903
+ ];
904
+ return C && b.push({ op: "add", path: ["name"], value: C }), P(S, b), S;
905
+ }
906
+ async function R(S) {
907
+ const { name: D, type: f, data: C, id: b = o() } = S || {};
908
+ T({
909
+ active_type: gt,
910
+ active: { id: b, type: f, name: D },
911
+ name: D
912
+ });
913
+ const { url: G } = await I();
914
+ if (C === void 0) return G;
915
+ {
916
+ const W = await s(G, { method: "PUT", headers: { "Content-Type": f }, body: C }), { ok: V, statusText: we } = W;
917
+ if (V) return b;
918
+ throw new Error(we);
919
+ }
920
+ }
921
+ async function P(S = Ae, D, f = !0, C = []) {
922
+ const b = O({ scope: S, patch: D, context: C }), G = k(S) ? S : `//${S}`;
923
+ if (f && l[G] !== void 0)
924
+ return g[G] = b.then((L) => L.ii), b;
925
+ {
926
+ const { ii: L } = await b;
927
+ return { ii: L };
928
+ }
929
+ }
930
+ async function H(S) {
931
+ const D = o();
932
+ return T({
933
+ id: D,
934
+ active_type: It,
935
+ active: { domain: S }
936
+ }), I();
937
+ }
938
+ function q(S = Ae) {
939
+ return P(S, [{ op: "remove", path: ["active"] }]);
940
+ }
941
+ function Q({ path: S, op: D, value: f }) {
942
+ return ["active_type", "name"].includes(S[0]) && S.length === 1 && typeof f == "string" || D === "remove";
943
+ }
944
+ async function J(S = Ae, D, f) {
945
+ const C = structuredClone(await Y(S, D).metadata);
946
+ return delete C.active, new se(C, (b) => {
947
+ const G = structuredClone(b);
948
+ if (!G.every(Q))
949
+ throw new Error("You may only modify the type or name for a scope's metadata");
950
+ P(S, G);
951
+ });
952
+ }
953
+ async function B(S, D, f, C = []) {
954
+ const b = o(), G = Date.now(), { session: L } = await _();
955
+ await new Promise((W) => setTimeout(W)), P("sessions", [
956
+ {
957
+ op: "add",
958
+ path: ["active", L, "queries", b],
959
+ value: { query: S, params: D, domain: f, context: C }
960
+ }
961
+ ], !1);
962
+ try {
963
+ const W = await I(), { rows: V } = W;
964
+ return P("sessions", [
965
+ {
966
+ op: "add",
967
+ path: ["active", L, "queries", b, "agent_latency"],
968
+ value: Date.now() - G
969
+ },
970
+ {
971
+ op: "remove",
972
+ path: ["active", L, "queries", b]
973
+ }
974
+ ], !1), V;
975
+ } catch (W) {
976
+ throw W;
977
+ }
978
+ }
979
+ const K = { child: [] };
980
+ function Z(S, D) {
981
+ if (!K[S]) throw new Error('Agent can only listen to events of "child"');
982
+ K[S].push(D);
983
+ }
984
+ function pe(S, D) {
985
+ K[S].forEach((f) => f(D));
986
+ }
987
+ return {
988
+ uuid: o,
989
+ environment: _,
990
+ login: d,
991
+ logout: w,
992
+ log: A,
993
+ create: T,
994
+ state: Y,
995
+ watch: $,
996
+ upload: R,
997
+ download: X,
998
+ interact: P,
999
+ claim: H,
1000
+ reset: q,
1001
+ metadata: J,
1002
+ query: B,
1003
+ synced: N,
1004
+ disconnect: m,
1005
+ reconnect: y,
1006
+ debug: p,
1007
+ on: Z
1008
+ };
1009
+ }
1010
+ const St = [...navigator.languages], Ge = localStorage.getItem("API_HOST") || "api.knowlearning.systems";
1011
+ async function Nt() {
1012
+ const e = await fetch(`https://${Ge}/_sid-check`, { method: "GET", credentials: "include" }), t = !!localStorage.getItem("sid");
1013
+ if (e.status === 201) {
1014
+ if (!t) {
1015
+ const r = await e.text();
1016
+ localStorage.setItem("sid", r), location.reload();
1017
+ }
1018
+ } else e.status === 200 ? t && (localStorage.removeItem("sid"), location.reload()) : console.warn("Issue Connecting To the API Server");
1019
+ }
1020
+ const _t = (e) => {
1021
+ Nt();
1022
+ const t = function() {
1023
+ const n = new WebSocket(`wss://${Ge}`);
1024
+ return this.send = (o) => n.send(JSON.stringify(o)), this.close = (o) => {
1025
+ this.send({ type: "close", info: o }), n.close();
1026
+ }, n.onopen = () => this.onopen(), n.onmessage = ({ data: o }) => this.onmessage(o.length === 0 ? null : JSON.parse(o)), n.onerror = (o) => this.onerror && this.onerror(o), n.onclose = (o) => this.onclose && this.onclose(o), this;
1027
+ }, r = Tt({
1028
+ token: e.getToken || ut,
1029
+ sid: () => localStorage.getItem("sid"),
1030
+ domain: window.location.host,
1031
+ Connection: t,
1032
+ uuid: ne,
1033
+ fetch,
1034
+ applyPatch: de,
1035
+ login: ct,
1036
+ logout: ft,
1037
+ variables: { LANGUAGES: St },
1038
+ reboot: () => window.location.reload()
1039
+ });
1040
+ return r.local = () => {
1041
+ localStorage.setItem("api", "local"), location.reload();
1042
+ }, r.remote = (n = "production") => {
1043
+ localStorage.setItem("api", "remote"), localStorage.setItem("mode", n), location.reload();
1044
+ }, r.close = () => {
1045
+ window.close();
1046
+ }, r;
1047
+ };
1048
+ function Rt() {
1049
+ let e = 0, t;
1050
+ const r = new Promise((p) => t = p), n = {}, o = {}, s = {}, [c, d] = qe({ metadata: y, state: E, watchers: o, synced: Y, sentUpdates: s, environment: a });
1051
+ async function w(p) {
1052
+ const A = p.requestId || ne();
1053
+ e += 1;
1054
+ const T = window.opener ? window.opener : window.parent;
1055
+ try {
1056
+ return T.postMessage({
1057
+ ...p,
1058
+ session: await r,
1059
+ requestId: A,
1060
+ index: e
1061
+ }, "*"), new Promise((R, P) => {
1062
+ n[A] = { resolve: R, reject: P };
1063
+ });
1064
+ } catch (R) {
1065
+ console.log("ERROR POSTING MESSAGE UP", p, R);
1066
+ }
1067
+ }
1068
+ let u = !1;
1069
+ addEventListener("message", async ({ data: p }) => {
1070
+ if (p.type === "setup" && !u)
1071
+ u = !0, t(p.session);
1072
+ else {
1073
+ if (!u || p.session !== await r) return;
1074
+ if (n[p.requestId]) {
1075
+ const { resolve: A, reject: T } = n[p.requestId];
1076
+ p.error ? T(p.error) : A(p.response);
1077
+ } else if (p.ii !== void 0) {
1078
+ const { scope: A, user: T, domain: R } = p, { auth: P, domain: H } = await a(), q = !R || R === H ? "" : R, Q = !T || P.user === T ? "" : T, J = k(A) ? A : `${q}/${Q}/${A}`, B = () => {
1079
+ s[J] = p.ii, o[J].forEach((K) => K(p));
1080
+ };
1081
+ o[J] && (s[J] === void 0 || s[J] + 1 === p.ii ? B() : p.ii === s[J] || p.ii < s[J] || B());
1082
+ }
1083
+ }
1084
+ });
1085
+ let i;
1086
+ async function a(p) {
1087
+ const A = await w({ type: "environment", user: p });
1088
+ return i || (i = A.variables), { ...A, variables: i };
1089
+ }
1090
+ function h({ id: p = ne(), active_type: A, active: T }) {
1091
+ return A || (A = "application/json"), g(p, [
1092
+ { op: "add", path: ["active_type"], value: A },
1093
+ { op: "add", path: ["active"], value: T }
1094
+ ]), p;
1095
+ }
1096
+ async function l(p, A) {
1097
+ return w({ type: "patch", root: p, scopes: A });
1098
+ }
1099
+ async function E(p, A, T) {
1100
+ if (p === void 0) {
1101
+ const { context: P } = await a();
1102
+ p = JSON.stringify(P);
1103
+ }
1104
+ const R = await w({ type: "state", scope: p, user: A, domain: T });
1105
+ return new se(R, (P) => {
1106
+ const H = structuredClone(P);
1107
+ H.forEach((q) => q.path.unshift("active")), g(p, H);
1108
+ });
1109
+ }
1110
+ function v(p) {
1111
+ return g(p, [{ op: "add", path: ["active"], value: null }]);
1112
+ }
1113
+ function g(p, A, T, R) {
1114
+ return w({ type: "interact", scope: p, patch: A, context: R });
1115
+ }
1116
+ async function O(p) {
1117
+ let { name: A, type: T, data: R, id: P = ne() } = p || {};
1118
+ const H = await w({ type: "upload", info: { name: A, type: T, id: P } });
1119
+ if (R === void 0) return H;
1120
+ {
1121
+ const Q = await fetch(H, { method: "PUT", headers: { "Content-Type": T }, body: R }), { ok: J, statusText: B } = Q;
1122
+ if (J) return P;
1123
+ throw new Error(B);
1124
+ }
1125
+ }
1126
+ function I(p) {
1127
+ let A = "fetch";
1128
+ const T = new Promise(async (R, P) => {
1129
+ const H = await w({ type: "download", id: p });
1130
+ if (await new Promise((q) => setTimeout(q)), A === "url") R(H);
1131
+ else if (A === "fetch") {
1132
+ const q = await fetch(H), { ok: Q, statusText: J } = q;
1133
+ Q ? R(q) : P(J);
1134
+ } else if (A === "direct") {
1135
+ const q = await I(p), { name: Q } = await y(p), J = q.headers.get("Content-Type"), B = new Blob([await q.blob()], { type: J }), K = window.URL.createObjectURL(B), Z = document.createElement("a");
1136
+ Z.style.display = "none", Z.href = K, Z.download = Q, document.body.appendChild(Z), Z.click(), window.URL.revokeObjectURL(K), R();
1137
+ }
1138
+ });
1139
+ return T.direct = () => (A = "direct", T), T.url = () => (A = "url", T), T;
1140
+ }
1141
+ function m({ path: p, op: A, value: T }) {
1142
+ return ["active_type", "name"].includes(p[0]) && p.length === 1 && typeof T == "string" || A === "remove";
1143
+ }
1144
+ async function y(p, A, T) {
1145
+ const R = await w({ type: "metadata", scope: p, user: A, domain: T });
1146
+ return new se(R, (P) => {
1147
+ const H = structuredClone(P);
1148
+ H.forEach((q) => {
1149
+ if (!m(q)) throw new Error("You may only modify the type or name for a scope's metadata");
1150
+ }), g(p, H);
1151
+ });
1152
+ }
1153
+ function N(p, A, T) {
1154
+ return w({ type: "login", provider: p, username: A, password: T });
1155
+ }
1156
+ function _(p, A, T, R = []) {
1157
+ return w({ type: "query", query: p, params: A, domain: T, context: R });
1158
+ }
1159
+ function M() {
1160
+ return w({ type: "logout" });
1161
+ }
1162
+ function $() {
1163
+ return w({ type: "disconnect" });
1164
+ }
1165
+ function z() {
1166
+ return w({ type: "reconnect" });
1167
+ }
1168
+ function Y() {
1169
+ return w({ type: "synced" });
1170
+ }
1171
+ function X(p) {
1172
+ return w({ type: "close", info: p });
1173
+ }
1174
+ return {
1175
+ embedded: !0,
1176
+ uuid: ne,
1177
+ environment: a,
1178
+ login: N,
1179
+ logout: M,
1180
+ create: h,
1181
+ state: E,
1182
+ watch: c,
1183
+ upload: O,
1184
+ download: I,
1185
+ interact: g,
1186
+ patch: l,
1187
+ reset: v,
1188
+ metadata: y,
1189
+ disconnect: $,
1190
+ reconnect: z,
1191
+ synced: Y,
1192
+ close: X,
1193
+ query: _
1194
+ };
1195
+ }
1196
+ async function Pt({ accept: e }) {
1197
+ return new Promise((t, r) => {
1198
+ const n = document.createElement("input");
1199
+ n.type = "file", n.accept = e, n.addEventListener("change", async (o) => {
1200
+ const s = o.target.files[0];
1201
+ t(s || null);
1202
+ }), n.click();
1203
+ });
1204
+ }
1205
+ let x = window.__default_knowlearning_agent;
1206
+ function Oe(e, t) {
1207
+ const r = (e == null ? void 0 : e.allow) || [], n = typeof e == "string" ? e : e == null ? void 0 : e.prefix;
1208
+ return n && !k(t) && !r.some((o) => t.startsWith(o)) ? `${n}/${t}` : t;
1209
+ }
1210
+ function He(e = {}) {
1211
+ if (x && !e.unique) return x;
1212
+ let t;
1213
+ try {
1214
+ t = window.self !== window.top;
1215
+ } catch {
1216
+ t = !0;
1217
+ }
1218
+ const r = t && !e.root ? Rt() : _t(e);
1219
+ r.embed = Ct;
1220
+ const n = r.upload;
1221
+ return r.upload = async (o) => {
1222
+ if (o != null && o.browser) {
1223
+ const s = await Pt(o);
1224
+ if (!s || o.validate && !await o.validate(s)) return;
1225
+ o.data = await s.arrayBuffer(), o.name || (o.name = s.name), o.type || (o.type = s.type);
1226
+ }
1227
+ return n(o);
1228
+ }, x || (window.__default_knowlearning_agent = x = r), r;
1229
+ }
1230
+ const fe = (e) => JSON.parse(JSON.stringify(e));
1231
+ function Ct(e, t) {
1232
+ const r = {}, n = [], o = {};
1233
+ let s = !1, c = !1;
1234
+ const d = ne(), w = (v) => new Promise((g, O) => {
1235
+ const I = { ...fe(v), session: d };
1236
+ n.push({ message: I, sent: g }), s && u();
1237
+ }), u = () => {
1238
+ for (; t.parentNode && n.length; ) {
1239
+ const { message: v, sent: g } = n.shift();
1240
+ t.contentWindow.postMessage(v, "*"), g();
1241
+ }
1242
+ }, i = async (v) => {
1243
+ const { requestId: g, type: O } = v, I = (m, y) => w({ requestId: g, response: m, error: y });
1244
+ if (O === "error")
1245
+ console.error(v), I({});
1246
+ else if (O === "close")
1247
+ o.close && o.close(v.info);
1248
+ else if (O === "environment") {
1249
+ const { user: m } = v, { mode: y, variables: N = {} } = e, _ = await (o.environment ? o.environment(m) : x.environment(m));
1250
+ I({
1251
+ ..._,
1252
+ context: [
1253
+ ..._.context || [],
1254
+ e.id
1255
+ ],
1256
+ variables: {
1257
+ ..._.variables || {},
1258
+ ...N
1259
+ },
1260
+ mode: y
1261
+ // TODO: deprecate
1262
+ });
1263
+ } else if (O === "interact") {
1264
+ let { scope: m, patch: y, context: N = [] } = v;
1265
+ const _ = Oe(e.namespace, m);
1266
+ let M;
1267
+ if (o.mutate && (M = fe(await x.state(_))), await x.interact(_, y, !0, [e.id, ...N]), o.mutate) {
1268
+ const $ = fe(y);
1269
+ $.forEach((z) => z.path.shift()), o.mutate({
1270
+ scope: _,
1271
+ before: M,
1272
+ after: fe(await x.state(_)),
1273
+ patch: $
1274
+ });
1275
+ }
1276
+ I({});
1277
+ } else if (O === "metadata") {
1278
+ const { scope: m, user: y, domain: N } = v, _ = Oe(e.namespace, m);
1279
+ I(await x.metadata(_, y, N));
1280
+ } else if (O === "state") {
1281
+ const { scope: m, user: y, domain: N } = v, _ = Oe(e.namespace, m), M = x.state(_, y, N), $ = `${N || ""}/${y || ""}/${_}`;
1282
+ r[$] || (r[$] = x.watch(_, (z) => w({ ...z, scope: m }), y, N)), o.state && o.state({ scope: m }), I(await M);
1283
+ } else if (O === "patch") {
1284
+ const { root: m, scopes: y } = v;
1285
+ I(await x.patch(m, y));
1286
+ } else if (O === "query") {
1287
+ const { query: m, params: y, domain: N, context: _ = [] } = v;
1288
+ x.query(m, y, N, [e.id, ..._]).then(I).catch((M) => I(null, M.error));
1289
+ } else if (O === "upload") {
1290
+ const { info: m } = v;
1291
+ I(await x.upload(m));
1292
+ } else if (O === "download")
1293
+ I(await x.download(v.id).url());
1294
+ else if (O === "login") {
1295
+ const { provider: m, username: y, password: N } = v;
1296
+ I(await x.login(m, y, N));
1297
+ } else O === "logout" ? x.logout() : O === "disconnect" ? I(await x.disconnect()) : O === "reconnect" ? I(await x.reconnect()) : O === "synced" ? I(await x.synced()) : (console.log("Unknown message type passed up...", v), I({}));
1298
+ };
1299
+ window.addEventListener("message", ({ data: v }) => {
1300
+ v.session === d && (c = !0, i(v));
1301
+ }), t.onload = () => {
1302
+ s = !0, u();
1303
+ }, a();
1304
+ async function a() {
1305
+ He();
1306
+ const { protocol: v } = window.location, { id: g } = e;
1307
+ if (k(g)) {
1308
+ const { domain: O } = await x.metadata(g);
1309
+ t.src = `${v}//${O}/${g}`;
1310
+ } else t.src = g;
1311
+ for (; !c; )
1312
+ w({ type: "setup", session: d }), await new Promise((O) => setTimeout(O, 100));
1313
+ o.open && o.open();
1314
+ }
1315
+ function h() {
1316
+ t.parentNode && t.parentNode.removeChild(t);
1317
+ }
1318
+ function l(v, g) {
1319
+ o[v] = g;
1320
+ }
1321
+ function E(v, g) {
1322
+ w({ type: "auth", token: v, state: g });
1323
+ }
1324
+ return {
1325
+ auth: E,
1326
+ remove: h,
1327
+ on: l
1328
+ };
1329
+ }
1330
+ window.Agent = He();