@meandr/chat-sdk 0.0.2 → 0.0.3
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 +83 -16
- package/dist/api.d.ts +1 -0
- package/dist/errors.d.ts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +547 -359
- package/dist/index.js.map +3 -3
- package/dist/markdown.js +90 -90
- package/dist/markdown.js.map +1 -1
- package/dist/meandr-chat.js +9 -9
- package/dist/meandr-chat.js.map +3 -3
- package/dist/types.d.ts +47 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -108,6 +108,7 @@ function request(fetchFn, base, c) {
|
|
|
108
108
|
if (c.body !== void 0) headers["Content-Type"] = c.type || "application/json";
|
|
109
109
|
if (c.bearer) headers.Authorization = "Bearer " + c.bearer;
|
|
110
110
|
const init = { method: c.method, headers, credentials: "include", cache: "no-store", redirect: "error" };
|
|
111
|
+
if (c.keepalive) init.keepalive = true;
|
|
111
112
|
if (c.body !== void 0) init.body = c.type ? c.body : JSON.stringify(c.body);
|
|
112
113
|
if (!c.type) return timed(fetchFn, base + c.path, init, c.signal);
|
|
113
114
|
if (c.progress && typeof XMLHttpRequest == "function") return upload(base + c.path, init, c.progress, c.signal);
|
|
@@ -258,45 +259,45 @@ var SSETooLarge = class extends Error {
|
|
|
258
259
|
};
|
|
259
260
|
var SSEParser = class {
|
|
260
261
|
constructor(maxEvent_ = 4 << 20) {
|
|
261
|
-
this.
|
|
262
|
-
this.P = "";
|
|
263
|
-
this.B = "";
|
|
262
|
+
this.xa = maxEvent_;
|
|
264
263
|
this.Z = "";
|
|
265
|
-
this.
|
|
266
|
-
this.
|
|
264
|
+
this.I = "";
|
|
265
|
+
this.ia = "";
|
|
266
|
+
this.J = false;
|
|
267
|
+
this.ja = false;
|
|
267
268
|
}
|
|
268
269
|
push(chunk) {
|
|
269
270
|
const out = [];
|
|
270
271
|
let text = chunk;
|
|
271
|
-
if (!this.
|
|
272
|
-
this.
|
|
272
|
+
if (!this.J && text.length > 0) {
|
|
273
|
+
this.J = true;
|
|
273
274
|
if (text.charCodeAt(0) === 65279) text = text.slice(1);
|
|
274
275
|
}
|
|
275
276
|
let start = 0;
|
|
276
|
-
if (this.
|
|
277
|
-
this.
|
|
277
|
+
if (this.ja && text.length > 0) {
|
|
278
|
+
this.ja = false;
|
|
278
279
|
if (text.charCodeAt(0) === 10) start = 1;
|
|
279
280
|
}
|
|
280
281
|
for (let i = start; i < text.length; i++) {
|
|
281
282
|
const c = text.charCodeAt(i);
|
|
282
283
|
if (c !== 10 && c !== 13) continue;
|
|
283
|
-
this.
|
|
284
|
-
this.
|
|
284
|
+
this.bb(this.Z + text.slice(start, i), out);
|
|
285
|
+
this.Z = "";
|
|
285
286
|
if (c === 13) {
|
|
286
|
-
if (i + 1 === text.length) this.
|
|
287
|
+
if (i + 1 === text.length) this.ja = true;
|
|
287
288
|
else if (text.charCodeAt(i + 1) === 10) i++;
|
|
288
289
|
}
|
|
289
290
|
start = i + 1;
|
|
290
291
|
}
|
|
291
|
-
this.
|
|
292
|
-
if (this.
|
|
292
|
+
this.Z += text.slice(start);
|
|
293
|
+
if (this.Z.length + this.I.length > this.xa) throw new SSETooLarge("event too large");
|
|
293
294
|
return out;
|
|
294
295
|
}
|
|
295
|
-
|
|
296
|
+
bb(line, out) {
|
|
296
297
|
if (line === "") {
|
|
297
|
-
if (this.
|
|
298
|
-
this.
|
|
299
|
-
this.
|
|
298
|
+
if (this.I !== "") out.push({ event: this.ia || "message", data: this.I.slice(0, -1) });
|
|
299
|
+
this.I = "";
|
|
300
|
+
this.ia = "";
|
|
300
301
|
return;
|
|
301
302
|
}
|
|
302
303
|
if (line.charCodeAt(0) === 58) return;
|
|
@@ -304,10 +305,10 @@ var SSEParser = class {
|
|
|
304
305
|
const field = colon < 0 ? line : line.slice(0, colon);
|
|
305
306
|
let value = colon < 0 ? "" : line.slice(colon + 1);
|
|
306
307
|
if (value.charCodeAt(0) === 32) value = value.slice(1);
|
|
307
|
-
if (field === "event") this.
|
|
308
|
+
if (field === "event") this.ia = value;
|
|
308
309
|
else if (field === "data") {
|
|
309
|
-
this.
|
|
310
|
-
if (this.
|
|
310
|
+
this.I += value + "\n";
|
|
311
|
+
if (this.I.length > this.xa) throw new SSETooLarge("event too large");
|
|
311
312
|
}
|
|
312
313
|
}
|
|
313
314
|
};
|
|
@@ -410,7 +411,7 @@ function claimsOf(token) {
|
|
|
410
411
|
if (!c || typeof c !== "object" || typeof c.sub !== "string") return null;
|
|
411
412
|
const aud = typeof c.aud === "string" ? [c.aud] : Array.isArray(c.aud) ? c.aud.filter((a) => typeof a === "string") : [];
|
|
412
413
|
const empty = !!c.ext && typeof c.ext === "object" && !Array.isArray(c.ext) && Object.keys(c.ext).length === 0;
|
|
413
|
-
return { aud,
|
|
414
|
+
return { aud, m: "user:" + c.sub + "\n" + canonical(c.ext === void 0 || empty ? null : c.ext) };
|
|
414
415
|
} catch {
|
|
415
416
|
return null;
|
|
416
417
|
}
|
|
@@ -460,13 +461,15 @@ var IDLE = {
|
|
|
460
461
|
anonymous: null,
|
|
461
462
|
conversation: null,
|
|
462
463
|
messages: [],
|
|
464
|
+
hasOlder: false,
|
|
463
465
|
outgoing: null,
|
|
464
466
|
answer: null,
|
|
465
467
|
answering: false,
|
|
466
468
|
waiting: null,
|
|
467
469
|
stop: null,
|
|
468
470
|
error: null,
|
|
469
|
-
files: null
|
|
471
|
+
files: null,
|
|
472
|
+
conversationList: null
|
|
470
473
|
};
|
|
471
474
|
function createChat(options) {
|
|
472
475
|
return new Client(options);
|
|
@@ -474,56 +477,64 @@ function createChat(options) {
|
|
|
474
477
|
var Client = class {
|
|
475
478
|
constructor(o) {
|
|
476
479
|
this.a = IDLE;
|
|
477
|
-
this.
|
|
480
|
+
this._ = /* @__PURE__ */ new Set();
|
|
478
481
|
this.g = { e: "cookie" };
|
|
479
|
-
this.
|
|
482
|
+
this.m = null;
|
|
480
483
|
// the session's user: a token's sub and ext, "anonymous", or "cookie"
|
|
481
|
-
this
|
|
484
|
+
this.$ = false;
|
|
482
485
|
// learned once: this browser doesn't keep the chat's cookie
|
|
483
|
-
this.
|
|
484
|
-
this.
|
|
486
|
+
this.z = null;
|
|
487
|
+
this.r = new AbortController();
|
|
485
488
|
// aborted by reopen, signOut and destroy
|
|
486
|
-
this.
|
|
487
|
-
// aborted with the session, and
|
|
489
|
+
this.A = new AbortController();
|
|
490
|
+
// aborted with the session, and when the conversation shown is dropped
|
|
488
491
|
this.h = 0;
|
|
489
|
-
this.
|
|
490
|
-
//
|
|
491
|
-
this.
|
|
492
|
-
|
|
493
|
-
this.
|
|
492
|
+
this.R = false;
|
|
493
|
+
// openConversation before the session opened: it shows no conversation
|
|
494
|
+
this.S = false;
|
|
495
|
+
// the end the last open sent for endNext_
|
|
496
|
+
this.aa = 0;
|
|
497
|
+
// openConversation, send and start calls: an open outrun by one shows nothing
|
|
498
|
+
this.T = null;
|
|
499
|
+
// the send or start out to start a conversation, until the chat names it
|
|
500
|
+
this.j = null;
|
|
501
|
+
this.u = null;
|
|
502
|
+
this.U = null;
|
|
503
|
+
// loadOlder's request, for those before from_
|
|
504
|
+
this.B = null;
|
|
494
505
|
// a message the chat may have stored: sending it again checks first
|
|
495
|
-
this.
|
|
496
|
-
this.
|
|
497
|
-
this.
|
|
506
|
+
this.K = 0;
|
|
507
|
+
this.Aa = /* @__PURE__ */ new Set();
|
|
508
|
+
this.la = false;
|
|
498
509
|
// a stream's chunk is applied at once: listeners hear it once
|
|
499
|
-
this.
|
|
500
|
-
this.
|
|
510
|
+
this.ma = false;
|
|
511
|
+
this.o = false;
|
|
501
512
|
this.getState = () => this.a;
|
|
502
513
|
this.subscribe = (listener) => {
|
|
503
514
|
if (typeof listener !== "function") throw new TypeError("subscribe needs a function");
|
|
504
|
-
if (!this.
|
|
505
|
-
this.
|
|
515
|
+
if (!this.o) this._.add(listener);
|
|
516
|
+
this.Ba(listener);
|
|
506
517
|
return () => {
|
|
507
|
-
this.
|
|
518
|
+
this._.delete(listener);
|
|
508
519
|
};
|
|
509
520
|
};
|
|
510
521
|
this.open = () => {
|
|
511
|
-
if (this.
|
|
512
|
-
if (!this.
|
|
513
|
-
const p = this.
|
|
514
|
-
this.
|
|
522
|
+
if (this.o || this.a.status === "ready") return Promise.resolve(this.a);
|
|
523
|
+
if (!this.z) {
|
|
524
|
+
const p = this.cb();
|
|
525
|
+
this.z = p;
|
|
515
526
|
const clear = () => {
|
|
516
|
-
if (this.
|
|
527
|
+
if (this.z === p) this.z = null;
|
|
517
528
|
};
|
|
518
529
|
p.then(clear, clear);
|
|
519
530
|
}
|
|
520
|
-
return this.
|
|
531
|
+
return this.z;
|
|
521
532
|
};
|
|
522
533
|
this.send = (text, options) => {
|
|
523
534
|
if (typeof text !== "string") throw new TypeError("send needs text");
|
|
524
535
|
const files = checked(options, ["files"]).files;
|
|
525
536
|
if (files !== void 0 && !Array.isArray(files)) throw new TypeError("files must be an array");
|
|
526
|
-
return this.
|
|
537
|
+
return this.db(text, files ? files.map(idOf) : []);
|
|
527
538
|
};
|
|
528
539
|
this.attach = (file, options) => {
|
|
529
540
|
var _a, _b;
|
|
@@ -535,85 +546,153 @@ var Client = class {
|
|
|
535
546
|
const size = file.size;
|
|
536
547
|
if (size > FILE_CAP) return Promise.resolve({ ok: false, error: new ChatError("too_large", "The file is too large.") });
|
|
537
548
|
const path = "/files?name=" + encodeURIComponent(wellFormed((_b = (_a = o.name) != null ? _a : file.name) != null ? _b : "") || "file");
|
|
538
|
-
return this.
|
|
539
|
-
const l = this
|
|
549
|
+
return this.L(o.signal, async (signal) => {
|
|
550
|
+
const l = this.ka;
|
|
540
551
|
if (l === null) throw new ChatError("attachments_off", "This chat takes no files.");
|
|
541
552
|
if (l && size > l.maxBytes) throw new ChatError("too_large", "The file is too large.");
|
|
542
553
|
const c = { method: "POST", path, body: file, type: file.type || "application/octet-stream" };
|
|
543
|
-
const res = await this.
|
|
554
|
+
const res = await this.V(on ? { ...c, progress: (sent) => on(Math.min(sent, size), size) } : c, signal);
|
|
544
555
|
return { ok: true, file: fileOf(await readJSON(res, signal)) };
|
|
545
556
|
});
|
|
546
557
|
};
|
|
547
558
|
this.readFile = (file, options) => {
|
|
548
559
|
const path = "/files/" + encodeURIComponent(idOf(file));
|
|
549
|
-
return this.
|
|
550
|
-
const res = await this.
|
|
560
|
+
return this.L(checked(options, ["signal"]).signal, async (signal) => {
|
|
561
|
+
const res = await this.V({ method: "GET", path }, signal);
|
|
551
562
|
const t = res.headers.get("Content-Type") || "";
|
|
552
563
|
return { ok: true, blob: new Blob(await chunks(res, signal), { type: IMAGE.test(t) ? t : "application/octet-stream" }) };
|
|
553
564
|
});
|
|
554
565
|
};
|
|
555
|
-
this.start = () => this.
|
|
566
|
+
this.start = () => this.eb();
|
|
567
|
+
this.endConversation = () => {
|
|
568
|
+
if (this.o) return Promise.resolve();
|
|
569
|
+
const c = this.a.conversation;
|
|
570
|
+
const u = c ? null : this.T;
|
|
571
|
+
const opening = this.z;
|
|
572
|
+
if (this.a.status !== "ready") this.S = true;
|
|
573
|
+
const named = u && new Promise((resolve) => u.D = resolve);
|
|
574
|
+
this.T = null;
|
|
575
|
+
this.Ca();
|
|
576
|
+
if (c) return this.na(c.id);
|
|
577
|
+
if (named) return named;
|
|
578
|
+
return opening ? opening.then((st) => st.status === "ready" ? this.ya : void 0) : Promise.resolve();
|
|
579
|
+
};
|
|
556
580
|
this.newConversation = () => {
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
581
|
+
void this.endConversation();
|
|
582
|
+
};
|
|
583
|
+
this.listConversations = (options) => {
|
|
584
|
+
const o = checked(options, ["before", "signal"]);
|
|
585
|
+
const path = "/conversations" + (o.before === void 0 ? "" : "?before=" + encodeURIComponent(idOf(o.before)));
|
|
586
|
+
return this.L(o.signal, async (signal) => {
|
|
587
|
+
if (this.a.conversationList === false) throw listOff();
|
|
588
|
+
const v = await readJSON(await this.V({ method: "GET", path }, signal), signal);
|
|
589
|
+
if (!v || !Array.isArray(v.conversations)) throw bad("list");
|
|
590
|
+
return { ok: true, conversations: v.conversations.map((c) => conversationOf(c, true)), hasMore: v.has_more === true };
|
|
591
|
+
});
|
|
592
|
+
};
|
|
593
|
+
this.openConversation = (conversation) => {
|
|
594
|
+
const id = idOf(conversation);
|
|
595
|
+
if (this.a.status !== "ready") this.R = true;
|
|
596
|
+
const move = ++this.aa;
|
|
597
|
+
const version = this.h;
|
|
598
|
+
return this.L(this.A.signal, async (signal) => {
|
|
599
|
+
const v = await this.Da(id, signal);
|
|
600
|
+
if (move !== this.aa || version !== this.h) throw aborted();
|
|
601
|
+
this.oa();
|
|
602
|
+
this.v(v.messages);
|
|
603
|
+
this.b({ ...this.ba(v.conversation, v.messages, v.w), outgoing: null, answer: null, answering: v.answering, waiting: null, stop: null, error: null });
|
|
604
|
+
if (v.answering) this.Ea(v.conversation.id);
|
|
605
|
+
return { ok: true, conversation: v.conversation };
|
|
606
|
+
});
|
|
607
|
+
};
|
|
608
|
+
this.deleteConversation = (conversation) => {
|
|
609
|
+
const id = idOf(conversation);
|
|
610
|
+
return this.L(null, async (signal) => {
|
|
611
|
+
if (this.a.conversationList === false) throw listOff();
|
|
612
|
+
try {
|
|
613
|
+
discard(await this.V({ method: "DELETE", path: "/conversations/" + encodeURIComponent(id) }, signal));
|
|
614
|
+
} catch (e) {
|
|
615
|
+
if (!isCode(e, "not_found")) throw e;
|
|
616
|
+
}
|
|
617
|
+
if (this.E(id)) this.Ca();
|
|
618
|
+
return { ok: true };
|
|
619
|
+
});
|
|
620
|
+
};
|
|
621
|
+
this.loadOlder = () => {
|
|
622
|
+
const { conversation: c, messages, hasOlder } = this.a;
|
|
623
|
+
const first = messages[0];
|
|
624
|
+
if (!c || !first || !hasOlder) return Promise.resolve({ ok: true, messages: [] });
|
|
625
|
+
const o = this.U;
|
|
626
|
+
if (o && o.fb === first) return o.Fa;
|
|
627
|
+
const p = this.L(this.A.signal, async (signal) => {
|
|
628
|
+
const v = await this.Da(c.id, signal, first.id);
|
|
629
|
+
const shown = this.a.messages;
|
|
630
|
+
if (shown[0] !== first) throw aborted();
|
|
631
|
+
const older = v.messages.filter((m) => m.id < first.id);
|
|
632
|
+
this.b({ messages: older.concat(shown), hasOlder: v.w && older.length > 0 });
|
|
633
|
+
return { ok: true, messages: older };
|
|
634
|
+
});
|
|
635
|
+
this.U = { fb: first, Fa: p };
|
|
636
|
+
p.then(() => {
|
|
637
|
+
if (this.U && this.U.Fa === p) this.U = null;
|
|
638
|
+
});
|
|
639
|
+
return p;
|
|
561
640
|
};
|
|
562
641
|
this.reopen = (options) => {
|
|
563
642
|
if (options !== void 0 && (!options || typeof options !== "object")) throw new TypeError("reopen takes { token }");
|
|
564
643
|
const o = options || {};
|
|
565
644
|
for (const k of Object.keys(o)) if (k !== "token") throw new TypeError("unknown option " + k);
|
|
566
|
-
if ("token" in o) this.
|
|
567
|
-
else if (this.
|
|
568
|
-
if (this.
|
|
645
|
+
if ("token" in o) this.q = tokenWay(o.token);
|
|
646
|
+
else if (this.q.e === "token") throw new TypeError("reopen needs { token }, or { token: null }");
|
|
647
|
+
if (this.o) return Promise.resolve(this.a);
|
|
569
648
|
const idle = this.a.status === "idle";
|
|
570
|
-
this.
|
|
649
|
+
this.Ga();
|
|
571
650
|
return idle ? Promise.resolve(this.a) : this.open();
|
|
572
651
|
};
|
|
573
652
|
this.signOut = async () => {
|
|
574
|
-
if (this.
|
|
653
|
+
if (this.o) return;
|
|
575
654
|
const auth = this.g;
|
|
576
|
-
if (this.
|
|
577
|
-
this.
|
|
655
|
+
if (this.q.e === "token") this.q = { e: "token", token: null };
|
|
656
|
+
this.Ga();
|
|
578
657
|
const stop = new AbortController();
|
|
579
658
|
const t = setTimeout(() => stop.abort(), SIGN_OUT_MS);
|
|
580
659
|
try {
|
|
581
660
|
const bearer = auth.e === "session" ? auth.token() : null;
|
|
582
|
-
discard(await request(this.
|
|
661
|
+
discard(await request(this.i, this.p, { method: "DELETE", path: "/session", bearer, signal: stop.signal }));
|
|
583
662
|
} catch {
|
|
584
663
|
} finally {
|
|
585
664
|
clearTimeout(t);
|
|
586
665
|
}
|
|
587
666
|
};
|
|
588
667
|
this.destroy = () => {
|
|
589
|
-
if (this.
|
|
590
|
-
this.
|
|
591
|
-
this.
|
|
592
|
-
this.
|
|
668
|
+
if (this.o) return;
|
|
669
|
+
this.Ha();
|
|
670
|
+
this.o = true;
|
|
671
|
+
this._.clear();
|
|
593
672
|
};
|
|
594
673
|
if (!o || typeof o !== "object") throw new TypeError("createChat needs options");
|
|
595
674
|
for (const k of Object.keys(o)) if (OPTIONS.indexOf(k) < 0) throw new TypeError("unknown option " + k);
|
|
596
675
|
if (typeof o.url !== "string") throw new TypeError("url is required");
|
|
597
676
|
if (o.fetch !== void 0 && typeof o.fetch !== "function") throw new TypeError("fetch must be a function");
|
|
598
677
|
if (o.context !== void 0 && typeof o.context !== "function") throw new TypeError("context must be a function");
|
|
599
|
-
this.
|
|
600
|
-
this.
|
|
601
|
-
this.
|
|
602
|
-
this.
|
|
678
|
+
this.za = o.context;
|
|
679
|
+
this.p = apiBase(o.url);
|
|
680
|
+
this.Q = originOf(this.p);
|
|
681
|
+
this.q = wayOf(o);
|
|
603
682
|
const f = o.fetch;
|
|
604
|
-
this.
|
|
683
|
+
this.i = f ? (input, init) => f(input, init) : (input, init) => fetch(input, init);
|
|
605
684
|
}
|
|
606
685
|
// --- state
|
|
607
|
-
|
|
686
|
+
b(patch) {
|
|
608
687
|
this.a = { ...this.a, ...patch };
|
|
609
|
-
if (this.
|
|
610
|
-
else this.
|
|
688
|
+
if (this.la) this.ma = true;
|
|
689
|
+
else this.pa();
|
|
611
690
|
}
|
|
612
|
-
|
|
613
|
-
if (this.
|
|
614
|
-
for (const l of Array.from(this.
|
|
691
|
+
pa() {
|
|
692
|
+
if (this.o) return;
|
|
693
|
+
for (const l of Array.from(this._)) this.Ba(l);
|
|
615
694
|
}
|
|
616
|
-
|
|
695
|
+
Ba(listener) {
|
|
617
696
|
try {
|
|
618
697
|
listener(this.a);
|
|
619
698
|
} catch (e) {
|
|
@@ -622,91 +701,117 @@ var Client = class {
|
|
|
622
701
|
});
|
|
623
702
|
}
|
|
624
703
|
}
|
|
625
|
-
|
|
626
|
-
for (const m of messages) if (m.id > this.
|
|
704
|
+
v(messages) {
|
|
705
|
+
for (const m of messages) if (m.id > this.K) this.K = m.id;
|
|
627
706
|
}
|
|
628
|
-
|
|
707
|
+
E(id) {
|
|
629
708
|
return this.a.conversation !== null && this.a.conversation.id === id;
|
|
630
709
|
}
|
|
631
710
|
oa() {
|
|
632
|
-
this.
|
|
633
|
-
this.
|
|
711
|
+
this.A.abort();
|
|
712
|
+
this.A = new AbortController();
|
|
634
713
|
this.h++;
|
|
635
|
-
this.
|
|
636
|
-
this.
|
|
637
|
-
this.
|
|
638
|
-
}
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
this.S = new AbortController();
|
|
642
|
-
this.D = null;
|
|
714
|
+
this.j = null;
|
|
715
|
+
this.u = null;
|
|
716
|
+
this.B = null;
|
|
717
|
+
}
|
|
718
|
+
// clear drops the conversation shown, with everything under way in it, and shows none.
|
|
719
|
+
Ca() {
|
|
643
720
|
this.oa();
|
|
721
|
+
this.b({ conversation: null, messages: [], hasOlder: false, outgoing: null, answer: null, answering: false, waiting: null, stop: null, error: null });
|
|
644
722
|
}
|
|
645
|
-
|
|
646
|
-
|
|
723
|
+
// land stops u being the page's unnamed request: it's named, refused or lost.
|
|
724
|
+
x(u) {
|
|
725
|
+
if (u && this.T === u) this.T = null;
|
|
726
|
+
}
|
|
727
|
+
ca(u) {
|
|
728
|
+
return !!u && !!u.D && !u.r.aborted;
|
|
729
|
+
}
|
|
730
|
+
Ha() {
|
|
731
|
+
this.r.abort();
|
|
732
|
+
this.r = new AbortController();
|
|
733
|
+
this.z = null;
|
|
734
|
+
this.oa();
|
|
735
|
+
}
|
|
736
|
+
Ga() {
|
|
737
|
+
this.Ha();
|
|
647
738
|
this.g = { e: "cookie" };
|
|
648
|
-
this.
|
|
649
|
-
this.
|
|
650
|
-
this.
|
|
651
|
-
this
|
|
739
|
+
this.m = null;
|
|
740
|
+
this.R = this.S = false;
|
|
741
|
+
this.K = 0;
|
|
742
|
+
this.ka = this.C = void 0;
|
|
652
743
|
this.a = IDLE;
|
|
653
|
-
this.
|
|
744
|
+
this.pa();
|
|
654
745
|
}
|
|
655
746
|
// userChanged wipes what the page shows when a reopened session is someone
|
|
656
747
|
// else's, so nobody sees another's conversation or draft, then shows theirs.
|
|
657
|
-
|
|
658
|
-
if (this.
|
|
659
|
-
this.
|
|
748
|
+
gb(o) {
|
|
749
|
+
if (this.u) this.u.abort();
|
|
750
|
+
this.u = null;
|
|
660
751
|
this.h++;
|
|
661
|
-
this.
|
|
662
|
-
this.
|
|
663
|
-
this.
|
|
664
|
-
|
|
665
|
-
this.
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
//
|
|
669
|
-
|
|
670
|
-
|
|
752
|
+
this.B = null;
|
|
753
|
+
this.R = this.S = false;
|
|
754
|
+
this.K = 0;
|
|
755
|
+
const s = this.a;
|
|
756
|
+
this.a = { ...IDLE, status: "ready", files: s.files, conversationList: s.conversationList };
|
|
757
|
+
this.Ia(o, this.h, false);
|
|
758
|
+
}
|
|
759
|
+
// show applies an opened session; hide keeps its conversation off the page, after
|
|
760
|
+
// endConversation or openConversation, while still learning its newest message.
|
|
761
|
+
Ia(o, version, hide2) {
|
|
762
|
+
this.v(o.messages);
|
|
671
763
|
if (version !== this.h) {
|
|
672
|
-
this.
|
|
764
|
+
this.b({ anonymous: o.anonymous });
|
|
673
765
|
return;
|
|
674
766
|
}
|
|
675
|
-
|
|
676
|
-
|
|
767
|
+
const c = o.conversation;
|
|
768
|
+
if (hide2 || !c) {
|
|
769
|
+
this.b({ anonymous: o.anonymous, conversation: null, messages: [], hasOlder: false, answering: false });
|
|
677
770
|
return;
|
|
678
771
|
}
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
772
|
+
this.b({ anonymous: o.anonymous, ...this.ba(c, o.messages, o.w), answering: o.answering });
|
|
773
|
+
if (o.answering) this.Ea(c.id);
|
|
774
|
+
}
|
|
775
|
+
// shown is what a read of c's last messages shows. The conversation shown keeps the
|
|
776
|
+
// older ones it loaded, and hasOlder, when the read reaches back to what it shows.
|
|
777
|
+
ba(c, fresh, more) {
|
|
778
|
+
const old = this.a.messages;
|
|
779
|
+
const at = this.E(c.id) && fresh.length ? old.findIndex((m) => m.id === fresh[0].id) : -1;
|
|
780
|
+
const messages = merge(old, fresh);
|
|
781
|
+
return at < 0 ? { conversation: c, messages, hasOlder: more } : { conversation: c, messages: old.slice(0, at).concat(messages) };
|
|
782
|
+
}
|
|
783
|
+
// forget stops showing conversation id when e says it's missing: ended in another tab
|
|
784
|
+
// where the chat lists none, or deleted. It answers e, to throw.
|
|
785
|
+
qa(id, e) {
|
|
786
|
+
if (isCode(e, "not_found") && this.E(id)) this.b({ conversation: null, messages: [], hasOlder: false, answer: null, answering: false });
|
|
787
|
+
return e;
|
|
685
788
|
}
|
|
686
789
|
// --- opening
|
|
687
|
-
async
|
|
688
|
-
const session = this.
|
|
790
|
+
async cb() {
|
|
791
|
+
const session = this.r;
|
|
689
792
|
const version = this.h;
|
|
690
793
|
const began = Date.now();
|
|
691
|
-
this.
|
|
794
|
+
this.b({ status: "opening", error: null });
|
|
692
795
|
for (let tries = 0; ; tries++) {
|
|
693
796
|
try {
|
|
694
|
-
const o = await this.
|
|
797
|
+
const o = await this.Ja(session.signal);
|
|
695
798
|
if (session.signal.aborted) return this.a;
|
|
696
|
-
const
|
|
697
|
-
|
|
698
|
-
this.
|
|
699
|
-
this.
|
|
799
|
+
const end = this.S;
|
|
800
|
+
const hide2 = end || this.R;
|
|
801
|
+
this.R = this.S = false;
|
|
802
|
+
this.Ia(o, version, hide2);
|
|
803
|
+
this.b({ status: "ready", waiting: null });
|
|
804
|
+
this.ya = end && o.conversation ? this.na(o.conversation.id) : void 0;
|
|
700
805
|
return this.a;
|
|
701
806
|
} catch (e) {
|
|
702
807
|
if (session.signal.aborted) return this.a;
|
|
703
808
|
const err = asChatError(e);
|
|
704
809
|
if (TRANSIENT.indexOf(err.code) < 0 || err.code === "token_failed") {
|
|
705
|
-
this.
|
|
810
|
+
this.b({ status: "error", error: err, waiting: null });
|
|
706
811
|
return this.a;
|
|
707
812
|
}
|
|
708
813
|
const ms = openWait(err, tries, Date.now() - began);
|
|
709
|
-
this.
|
|
814
|
+
this.b({ waiting: { code: err.code, until: Date.now() + ms } });
|
|
710
815
|
try {
|
|
711
816
|
await sleep(ms, session.signal);
|
|
712
817
|
} catch {
|
|
@@ -717,85 +822,86 @@ var Client = class {
|
|
|
717
822
|
}
|
|
718
823
|
// establish opens or resumes the session for the page's user and settles how
|
|
719
824
|
// requests carry it.
|
|
720
|
-
async
|
|
721
|
-
const token = await userToken(this.
|
|
722
|
-
if (typeof token !== "string") return this.
|
|
825
|
+
async Ja(signal) {
|
|
826
|
+
const token = await userToken(this.q, this.i, signal, false);
|
|
827
|
+
if (typeof token !== "string") return this.Ka(token === null, signal);
|
|
723
828
|
try {
|
|
724
|
-
return await this.
|
|
829
|
+
return await this.La(token, signal);
|
|
725
830
|
} catch (e) {
|
|
726
|
-
if (!isCode(e, "token_expired") || !renews(this.
|
|
831
|
+
if (!isCode(e, "token_expired") || !renews(this.q)) throw e;
|
|
727
832
|
}
|
|
728
|
-
const next = await userToken(this.
|
|
729
|
-
if (typeof next !== "string") return this.
|
|
730
|
-
return this.
|
|
833
|
+
const next = await userToken(this.q, this.i, signal, true);
|
|
834
|
+
if (typeof next !== "string") return this.Ka(next === null, signal);
|
|
835
|
+
return this.La(next, signal);
|
|
731
836
|
}
|
|
732
|
-
async
|
|
733
|
-
if (!this.
|
|
837
|
+
async La(token, signal) {
|
|
838
|
+
if (!this.Ma(token)) throw notForThisChat(this.Q);
|
|
734
839
|
const who = whoOf(token);
|
|
735
|
-
if (this
|
|
736
|
-
const o2 = await this.
|
|
840
|
+
if (this.$) {
|
|
841
|
+
const o2 = await this.F({ cookieless: true }, token, signal);
|
|
737
842
|
this.g = { e: "token", token: hide(token) };
|
|
738
|
-
this.
|
|
843
|
+
this.m = who;
|
|
739
844
|
return o2;
|
|
740
845
|
}
|
|
741
|
-
const o = await this.
|
|
742
|
-
if (o.
|
|
743
|
-
this
|
|
846
|
+
const o = await this.F({}, token, signal);
|
|
847
|
+
if (o.J && !await this.Na(signal)) {
|
|
848
|
+
this.$ = true;
|
|
744
849
|
this.g = { e: "token", token: hide(token) };
|
|
745
850
|
} else {
|
|
746
851
|
this.g = { e: "cookie" };
|
|
747
852
|
}
|
|
748
|
-
this.
|
|
853
|
+
this.m = who;
|
|
749
854
|
return o;
|
|
750
855
|
}
|
|
751
856
|
// openWithout opens with no customer's token. When the page says nobody is
|
|
752
857
|
// signed in, a signed-in session the browser still holds is ended first.
|
|
753
|
-
async
|
|
858
|
+
async Ka(nobody, signal) {
|
|
754
859
|
const held = this.g;
|
|
755
860
|
if (held.e === "session") {
|
|
756
861
|
try {
|
|
757
|
-
const o2 = await this.
|
|
758
|
-
this.
|
|
862
|
+
const o2 = await this.F({}, held.token(), signal);
|
|
863
|
+
this.m = "anonymous";
|
|
759
864
|
return o2;
|
|
760
865
|
} catch (e) {
|
|
761
866
|
if (!isCode(e, "session_expired") && !isCode(e, "unauthorized")) throw e;
|
|
762
867
|
}
|
|
763
868
|
}
|
|
764
869
|
this.g = { e: "cookie" };
|
|
765
|
-
if (this
|
|
766
|
-
if (nobody && await this.
|
|
767
|
-
const o = await this.
|
|
870
|
+
if (this.$) return this.Oa(signal);
|
|
871
|
+
if (nobody && await this.hb(signal)) await this.M(signal);
|
|
872
|
+
const o = await this.F({}, null, signal);
|
|
768
873
|
if (nobody && !o.anonymous) {
|
|
769
874
|
throw new ChatError("protocol", "Signed in, though the page says nobody is.");
|
|
770
875
|
}
|
|
771
|
-
if (o.
|
|
772
|
-
this
|
|
773
|
-
return this.
|
|
876
|
+
if (o.J && !await this.Na(signal)) {
|
|
877
|
+
this.$ = true;
|
|
878
|
+
return this.Oa(signal);
|
|
774
879
|
}
|
|
775
|
-
this.
|
|
880
|
+
this.m = o.anonymous ? "anonymous" : "cookie";
|
|
776
881
|
return o;
|
|
777
882
|
}
|
|
778
|
-
async
|
|
779
|
-
const o = await this.
|
|
780
|
-
if (!o.
|
|
781
|
-
this.g = { e: "session", token: hide(o.
|
|
782
|
-
this.
|
|
883
|
+
async Oa(signal) {
|
|
884
|
+
const o = await this.F({ cookieless: true }, null, signal);
|
|
885
|
+
if (!o.Pa) throw new ChatError("protocol", "The chat gave no session token.");
|
|
886
|
+
this.g = { e: "session", token: hide(o.Pa) };
|
|
887
|
+
this.m = "anonymous";
|
|
783
888
|
return o;
|
|
784
889
|
}
|
|
785
|
-
async
|
|
786
|
-
const res = await request(this.
|
|
890
|
+
async F(body2, bearer, signal) {
|
|
891
|
+
const res = await request(this.i, this.p, { method: "POST", path: "/session", body: body2, bearer, signal });
|
|
787
892
|
if (!res.ok) throw await failure(res, signal);
|
|
788
893
|
const o = openedOf(await readJSON(res, signal));
|
|
789
894
|
live(signal);
|
|
790
|
-
this
|
|
791
|
-
this.
|
|
895
|
+
this.ka = o.files;
|
|
896
|
+
this.C = o.C;
|
|
792
897
|
const files = o.files || null;
|
|
793
|
-
if (JSON.stringify(files) !== JSON.stringify(this.a.files)) this.
|
|
898
|
+
if (JSON.stringify(files) !== JSON.stringify(this.a.files)) this.b({ files });
|
|
899
|
+
if (o.Qa !== this.a.conversationList) this.b({ conversationList: o.Qa });
|
|
794
900
|
return o;
|
|
795
901
|
}
|
|
796
902
|
// cookieKept asks, with the cookie alone, whether the browser kept the session it was just given.
|
|
797
|
-
async
|
|
798
|
-
const res = await request(this.
|
|
903
|
+
async Na(signal) {
|
|
904
|
+
const res = await request(this.i, this.p, { method: "GET", path: "/session", signal });
|
|
799
905
|
if (res.ok || res.status === 401) {
|
|
800
906
|
discard(res);
|
|
801
907
|
live(signal);
|
|
@@ -806,8 +912,8 @@ var Client = class {
|
|
|
806
912
|
// signedIn asks, with the cookie alone, whether the browser holds a signed-in
|
|
807
913
|
// session, without fetching that user's conversation. One the chat refuses to
|
|
808
914
|
// serve (403) is ended too: it's of no use to anyone.
|
|
809
|
-
async
|
|
810
|
-
const res = await request(this.
|
|
915
|
+
async hb(signal) {
|
|
916
|
+
const res = await request(this.i, this.p, { method: "GET", path: "/session", signal });
|
|
811
917
|
if (res.status === 401 || res.status === 403) {
|
|
812
918
|
discard(res);
|
|
813
919
|
return res.status === 403;
|
|
@@ -816,74 +922,76 @@ var Client = class {
|
|
|
816
922
|
const v = await readJSON(res, signal);
|
|
817
923
|
return !!v && typeof v === "object" && v.anonymous === false;
|
|
818
924
|
}
|
|
819
|
-
async
|
|
820
|
-
const res = await request(this.
|
|
925
|
+
async M(signal) {
|
|
926
|
+
const res = await request(this.i, this.p, { method: "DELETE", path: "/session", signal });
|
|
821
927
|
if (!res.ok) throw await failure(res, signal);
|
|
822
928
|
discard(res);
|
|
823
929
|
}
|
|
824
930
|
// mayCarry: a token goes only to the chat its aud names, or to the page's own
|
|
825
931
|
// server, which forwards the chat.
|
|
826
|
-
|
|
932
|
+
Ma(token) {
|
|
827
933
|
const page = typeof location === "object" && location ? location.origin : null;
|
|
828
|
-
if (page === this.
|
|
934
|
+
if (page === this.Q) return true;
|
|
829
935
|
const c = claimsOf(token);
|
|
830
|
-
return !!c && c.aud.some((a) => originOf(a) === this.
|
|
936
|
+
return !!c && c.aud.some((a) => originOf(a) === this.Q);
|
|
831
937
|
}
|
|
832
938
|
// --- requests as the session's user
|
|
833
|
-
|
|
939
|
+
ra() {
|
|
834
940
|
return this.g.e === "cookie" ? null : this.g.token();
|
|
835
941
|
}
|
|
836
|
-
// authed makes a request as the session's user; a lapsed token or session is
|
|
837
|
-
//
|
|
838
|
-
async
|
|
942
|
+
// authed makes a request as the session's user; a lapsed token or session is renewed or
|
|
943
|
+
// reopened once, for target's conversation. u, a request starting one, is unnamed while out.
|
|
944
|
+
async da(c, signal, target, u) {
|
|
839
945
|
for (let retried = false; ; retried = true) {
|
|
840
|
-
|
|
841
|
-
|
|
946
|
+
if (u) this.T = u;
|
|
947
|
+
const res = await request(this.i, this.p, { ...c, bearer: this.ra(), signal });
|
|
948
|
+
if (res.status !== 401 || retried || u && u.D) return res;
|
|
949
|
+
this.x(u);
|
|
842
950
|
const err = await failure(res, signal);
|
|
843
|
-
if (!await this.
|
|
951
|
+
if (!await this.Ra(err, signal, target)) throw err;
|
|
844
952
|
}
|
|
845
953
|
}
|
|
846
954
|
// recoverAuth renews a lapsed token or reopens an ended session. It answers
|
|
847
955
|
// whether to try again, and throws session_expired when the user changed.
|
|
848
|
-
async
|
|
956
|
+
async Ra(err, signal, target) {
|
|
849
957
|
if (this.g.e === "token" && (err.code === "token_expired" || err.code === "unauthorized")) {
|
|
850
|
-
if (!renews(this.
|
|
851
|
-
const token = await userToken(this.
|
|
852
|
-
if (typeof token === "string" && whoOf(token) === this.
|
|
853
|
-
if (!this.
|
|
958
|
+
if (!renews(this.q)) return false;
|
|
959
|
+
const token = await userToken(this.q, this.i, signal, err.code === "token_expired");
|
|
960
|
+
if (typeof token === "string" && whoOf(token) === this.m) {
|
|
961
|
+
if (!this.Ma(token)) throw notForThisChat(this.Q);
|
|
854
962
|
this.g = { e: "token", token: hide(token) };
|
|
855
963
|
return true;
|
|
856
964
|
}
|
|
857
965
|
} else if (err.code !== "session_expired" && err.code !== "unauthorized") {
|
|
858
966
|
return false;
|
|
859
967
|
}
|
|
860
|
-
const o = await this.
|
|
861
|
-
this.
|
|
968
|
+
const o = await this.sa(signal);
|
|
969
|
+
this.b({ anonymous: o.anonymous });
|
|
862
970
|
return true;
|
|
863
971
|
}
|
|
864
972
|
// reestablish opens the session again as the page's user, within the flow's
|
|
865
973
|
// signal, so a sign-out meanwhile stops it. Someone else's session wipes the
|
|
866
974
|
// page, and the caller fails with session_expired.
|
|
867
|
-
async
|
|
975
|
+
async sa(signal) {
|
|
868
976
|
live(signal);
|
|
869
|
-
const who = this.
|
|
870
|
-
const o = await this.
|
|
977
|
+
const who = this.m;
|
|
978
|
+
const o = await this.Ja(signal);
|
|
871
979
|
live(signal);
|
|
872
|
-
if (this.
|
|
873
|
-
this.
|
|
980
|
+
if (this.m !== who) {
|
|
981
|
+
this.gb(o);
|
|
874
982
|
throw new ChatError("session_expired", "Someone else is signed in now.", { status: 401 });
|
|
875
983
|
}
|
|
876
|
-
this.
|
|
984
|
+
this.v(o.messages);
|
|
877
985
|
return o;
|
|
878
986
|
}
|
|
879
987
|
// --- files
|
|
880
|
-
// flow runs
|
|
881
|
-
//
|
|
882
|
-
async
|
|
883
|
-
if (this.
|
|
988
|
+
// flow runs a member's request as the session's user, opening it as send
|
|
989
|
+
// does. The page's signal or the session's end cuts it short.
|
|
990
|
+
async L(signal, run) {
|
|
991
|
+
if (this.o) return { ok: false, error: aborted() };
|
|
884
992
|
const ctrl = new AbortController();
|
|
885
993
|
const stop = () => ctrl.abort();
|
|
886
|
-
const session = this.
|
|
994
|
+
const session = this.r.signal;
|
|
887
995
|
session.addEventListener("abort", stop);
|
|
888
996
|
if (signal) signal.addEventListener("abort", stop);
|
|
889
997
|
if (session.aborted || signal && signal.aborted) stop();
|
|
@@ -906,12 +1014,12 @@ var Client = class {
|
|
|
906
1014
|
}
|
|
907
1015
|
// retried makes a request that nothing is stored by when it's refused: a 401
|
|
908
1016
|
// reopens the session once, and 429 and 503 wait as the chat says, up to a minute.
|
|
909
|
-
async
|
|
1017
|
+
async V(c, signal) {
|
|
910
1018
|
const until = Date.now() + WAIT_CAP_MS;
|
|
911
1019
|
for (; ; ) {
|
|
912
1020
|
let err;
|
|
913
1021
|
try {
|
|
914
|
-
const res = await this.
|
|
1022
|
+
const res = await this.da(c, signal, null);
|
|
915
1023
|
if (res.ok) return res;
|
|
916
1024
|
err = await failure(res, signal);
|
|
917
1025
|
} catch (e) {
|
|
@@ -922,229 +1030,273 @@ var Client = class {
|
|
|
922
1030
|
await sleep(wait, signal);
|
|
923
1031
|
}
|
|
924
1032
|
}
|
|
1033
|
+
// sendEnd ends conversation id, trying again through about a minute while the chat can't be
|
|
1034
|
+
// reached or asks to wait. A request under way outlives the page and the session; no retry does.
|
|
1035
|
+
async na(id) {
|
|
1036
|
+
const session = this.r.signal;
|
|
1037
|
+
const until = Date.now() + WAIT_CAP_MS;
|
|
1038
|
+
const path = "/conversations/" + encodeURIComponent(id) + "/end";
|
|
1039
|
+
for (let tries = 0, renewed = false; ; tries++) {
|
|
1040
|
+
let err;
|
|
1041
|
+
try {
|
|
1042
|
+
const res = await request(this.i, this.p, { method: "POST", path, bearer: this.ra(), keepalive: true });
|
|
1043
|
+
if (res.ok || res.status === 404) return discard(res);
|
|
1044
|
+
err = await failure(res);
|
|
1045
|
+
if (res.status === 401 && !renewed && !session.aborted) {
|
|
1046
|
+
renewed = true;
|
|
1047
|
+
if (await this.Ra(err, session, id)) continue;
|
|
1048
|
+
}
|
|
1049
|
+
} catch (e) {
|
|
1050
|
+
err = asChatError(e);
|
|
1051
|
+
}
|
|
1052
|
+
const wait = TRANSIENT.indexOf(err.code) < 0 ? null : openWait(err, tries, 0);
|
|
1053
|
+
if (wait === null || Date.now() + wait > until) return;
|
|
1054
|
+
try {
|
|
1055
|
+
await sleep(wait, session);
|
|
1056
|
+
} catch {
|
|
1057
|
+
return;
|
|
1058
|
+
}
|
|
1059
|
+
}
|
|
1060
|
+
}
|
|
925
1061
|
// --- page context
|
|
926
1062
|
// contextOf is what a message or a start tells of the page: the browser's time zone
|
|
927
1063
|
// under what the context option gives. That part is left out, with a warning, when
|
|
928
1064
|
// the option fails or the chat would refuse it, so it never costs the message.
|
|
929
|
-
async
|
|
1065
|
+
async Sa(signal) {
|
|
930
1066
|
let zone;
|
|
931
1067
|
try {
|
|
932
1068
|
zone = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
933
1069
|
} catch {
|
|
934
1070
|
}
|
|
935
1071
|
const base = zone ? { timezone: zone } : {};
|
|
936
|
-
const fn = this.
|
|
937
|
-
if (fn && this.
|
|
1072
|
+
const fn = this.za;
|
|
1073
|
+
if (fn && this.C !== void 0) {
|
|
938
1074
|
let page;
|
|
939
1075
|
try {
|
|
940
1076
|
page = await within(new Promise((resolve) => resolve(fn())), signal, CONTEXT_MS, () => new Error("timeout"));
|
|
941
1077
|
} catch (e) {
|
|
942
1078
|
if (signal.aborted) throw aborted();
|
|
943
|
-
this.
|
|
1079
|
+
this.Ta("the option failed or took 5 s", e);
|
|
944
1080
|
}
|
|
945
1081
|
if (page != null) {
|
|
946
|
-
const v2 = vetted(base, page, this.
|
|
1082
|
+
const v2 = vetted(base, page, this.C);
|
|
947
1083
|
if (typeof v2 !== "string") return v2;
|
|
948
|
-
this.
|
|
1084
|
+
this.Ta(v2);
|
|
949
1085
|
}
|
|
950
1086
|
}
|
|
951
|
-
const v = vetted(base, {}, this.
|
|
1087
|
+
const v = vetted(base, {}, this.C);
|
|
952
1088
|
return typeof v === "string" ? void 0 : v;
|
|
953
1089
|
}
|
|
954
1090
|
// warn tells the page's developer, once for each reason, that its context was left out.
|
|
955
|
-
|
|
956
|
-
if (this.
|
|
957
|
-
this.
|
|
1091
|
+
Ta(why, ...cause) {
|
|
1092
|
+
if (this.Aa.has(why)) return;
|
|
1093
|
+
this.Aa.add(why);
|
|
958
1094
|
console.warn("@meandr/chat-sdk: context left out: " + why, ...cause);
|
|
959
1095
|
}
|
|
960
1096
|
// --- messages
|
|
961
|
-
async
|
|
1097
|
+
async db(raw, files) {
|
|
962
1098
|
const text = normalize(raw);
|
|
963
|
-
if (this.
|
|
1099
|
+
if (this.o) return { ok: false, error: aborted() };
|
|
964
1100
|
if (text === "" && !files.length) return { ok: false, error: new ChatError("bad_request", "The message is empty.") };
|
|
965
|
-
if (this.
|
|
1101
|
+
if (this.j || this.a.answering) return { ok: false, error: busy() };
|
|
966
1102
|
const mine = {};
|
|
967
|
-
this.
|
|
968
|
-
|
|
969
|
-
const
|
|
1103
|
+
this.j = mine;
|
|
1104
|
+
this.aa++;
|
|
1105
|
+
const scope = this.A;
|
|
1106
|
+
const u = this.B;
|
|
970
1107
|
const recheck = u && u.text === text && String(u.files) === String(files) ? u : null;
|
|
971
|
-
this.
|
|
1108
|
+
this.B = null;
|
|
972
1109
|
let began = -1;
|
|
973
|
-
this.
|
|
1110
|
+
this.b({ outgoing: text, error: null, stop: null });
|
|
974
1111
|
try {
|
|
975
1112
|
if (this.a.status !== "ready") {
|
|
976
1113
|
const st = await this.open();
|
|
977
|
-
if (scope.signal.aborted || this.
|
|
978
|
-
if (st.status !== "ready") return this.
|
|
979
|
-
if (this.a.answering) return this.
|
|
1114
|
+
if (scope.signal.aborted || this.j !== mine) return { ok: false, error: aborted() };
|
|
1115
|
+
if (st.status !== "ready") return this.N(mine, scope, st.error || unopened(), -1);
|
|
1116
|
+
if (this.a.answering) return this.N(mine, scope, busy(), -1);
|
|
980
1117
|
}
|
|
981
|
-
const context = await this.
|
|
1118
|
+
const context = await this.Sa(scope.signal);
|
|
982
1119
|
began = this.h;
|
|
983
|
-
this.
|
|
984
|
-
const r = await this.
|
|
1120
|
+
this.b({ answering: true, answer: { text: "", tools: [] } });
|
|
1121
|
+
const r = await cut(this.ib(text, files, context, scope.signal, recheck), scope.signal);
|
|
985
1122
|
return { ok: true, message: r.message, stop: r.stop };
|
|
986
1123
|
} catch (e) {
|
|
987
|
-
return this.
|
|
1124
|
+
return this.N(mine, scope, asChatError(e), began);
|
|
988
1125
|
} finally {
|
|
989
|
-
if (this.
|
|
1126
|
+
if (this.j === mine) this.j = null;
|
|
990
1127
|
}
|
|
991
1128
|
}
|
|
992
1129
|
// doStart opens if need be, then has the assistant speak first; the chat
|
|
993
1130
|
// supplies the instructions, so the page sends only its context.
|
|
994
|
-
async
|
|
995
|
-
if (this.
|
|
996
|
-
if (this.
|
|
1131
|
+
async eb() {
|
|
1132
|
+
if (this.o) return { ok: false, error: aborted() };
|
|
1133
|
+
if (this.j || this.a.answering) return { ok: false, error: busy() };
|
|
997
1134
|
const mine = {};
|
|
998
|
-
this.
|
|
999
|
-
|
|
1135
|
+
this.j = mine;
|
|
1136
|
+
this.aa++;
|
|
1137
|
+
const scope = this.A;
|
|
1000
1138
|
let began = -1;
|
|
1001
|
-
this.
|
|
1139
|
+
this.b({ error: null, stop: null });
|
|
1002
1140
|
try {
|
|
1003
1141
|
if (this.a.status !== "ready") {
|
|
1004
1142
|
const st = await this.open();
|
|
1005
|
-
if (scope.signal.aborted || this.
|
|
1006
|
-
if (st.status !== "ready") return this.
|
|
1007
|
-
if (this.a.answering) return this.
|
|
1143
|
+
if (scope.signal.aborted || this.j !== mine) return { ok: false, error: aborted() };
|
|
1144
|
+
if (st.status !== "ready") return this.N(mine, scope, st.error || unopened(), -1);
|
|
1145
|
+
if (this.a.answering) return this.N(mine, scope, busy(), -1);
|
|
1008
1146
|
}
|
|
1009
|
-
const context = await this.
|
|
1147
|
+
const context = await this.Sa(scope.signal);
|
|
1010
1148
|
began = this.h;
|
|
1011
|
-
this.
|
|
1012
|
-
const r = await this.
|
|
1149
|
+
this.b({ answering: true, answer: { text: "", tools: [] } });
|
|
1150
|
+
const r = await cut(this.jb(scope.signal, context), scope.signal);
|
|
1013
1151
|
return { ok: true, message: r.message, stop: r.stop };
|
|
1014
1152
|
} catch (e) {
|
|
1015
|
-
return this.
|
|
1153
|
+
return this.N(mine, scope, asChatError(e), began);
|
|
1016
1154
|
} finally {
|
|
1017
|
-
if (this.
|
|
1155
|
+
if (this.j === mine) this.j = null;
|
|
1018
1156
|
}
|
|
1019
1157
|
}
|
|
1020
1158
|
// begin asks for the start and reads its answer. Only refusals before the
|
|
1021
1159
|
// chat starts are tried again; a start never lands twice. With no
|
|
1022
1160
|
// conversation shown, after newConversation say, it asks for a new one.
|
|
1023
|
-
async
|
|
1024
|
-
const sent = { text: "", files: [],
|
|
1025
|
-
const
|
|
1026
|
-
const call = { method: "POST", path: "/start", body:
|
|
1161
|
+
async jb(signal, context) {
|
|
1162
|
+
const sent = { text: "", files: [], l: null, ea: this.K };
|
|
1163
|
+
const fresh = this.a.conversation === null;
|
|
1164
|
+
const call = { method: "POST", path: "/start", body: fresh ? { new: true, context } : { context }, stream: true };
|
|
1027
1165
|
const waitUntil = Date.now() + WAIT_CAP_MS;
|
|
1028
1166
|
const busyUntil = Date.now() + BUSY_CAP_MS;
|
|
1029
1167
|
for (let busyTries = 0; ; ) {
|
|
1030
1168
|
const stream = new AbortController();
|
|
1031
|
-
const
|
|
1169
|
+
const u = fresh ? { r: this.r.signal } : null;
|
|
1170
|
+
const stop = () => {
|
|
1171
|
+
if (!this.ca(u)) stream.abort();
|
|
1172
|
+
};
|
|
1032
1173
|
signal.addEventListener("abort", stop);
|
|
1033
1174
|
try {
|
|
1034
1175
|
let res;
|
|
1035
1176
|
try {
|
|
1036
|
-
res = await this.
|
|
1177
|
+
res = await this.da(call, stream.signal, null, u);
|
|
1037
1178
|
} catch (e) {
|
|
1179
|
+
this.x(u);
|
|
1038
1180
|
if (signal.aborted) throw aborted();
|
|
1039
1181
|
const reopen = isChatError(e) ? waitFor(e) : null;
|
|
1040
1182
|
if (reopen !== null && Date.now() + reopen <= waitUntil) {
|
|
1041
|
-
await this.
|
|
1183
|
+
await this.O(e.code, reopen, signal);
|
|
1042
1184
|
continue;
|
|
1043
1185
|
}
|
|
1044
1186
|
throw e;
|
|
1045
1187
|
}
|
|
1046
|
-
if (res.ok) return await this.
|
|
1188
|
+
if (res.ok) return await this.Ua(res, stream, signal, sent, u);
|
|
1189
|
+
this.x(u);
|
|
1047
1190
|
const err = await failure(res, signal);
|
|
1048
1191
|
const wait = waitFor(err);
|
|
1049
1192
|
if (wait !== null && Date.now() + wait <= waitUntil) {
|
|
1050
|
-
await this.
|
|
1193
|
+
await this.O(err.code, wait, signal);
|
|
1051
1194
|
continue;
|
|
1052
1195
|
}
|
|
1053
1196
|
if (err.code === "busy" && Date.now() < busyUntil) {
|
|
1054
|
-
await this.
|
|
1197
|
+
await this.O("busy", BUSY_STEPS_MS[Math.min(busyTries++, BUSY_STEPS_MS.length - 1)], signal);
|
|
1055
1198
|
continue;
|
|
1056
1199
|
}
|
|
1057
1200
|
throw err;
|
|
1058
1201
|
} finally {
|
|
1059
1202
|
signal.removeEventListener("abort", stop);
|
|
1203
|
+
this.x(u);
|
|
1204
|
+
if (u && u.D) u.D();
|
|
1060
1205
|
}
|
|
1061
1206
|
}
|
|
1062
1207
|
}
|
|
1063
1208
|
// failed shows a failure. A message the page doesn't show as stored stays in
|
|
1064
1209
|
// outgoing, so it can be sent again. began is the conversation version this
|
|
1065
1210
|
// send set answering in; once the page shows another, answering is not its own.
|
|
1066
|
-
|
|
1067
|
-
if (this.
|
|
1068
|
-
if (began < 0) this.
|
|
1069
|
-
else if (began === this.h) this.
|
|
1070
|
-
else this.
|
|
1211
|
+
N(mine, scope, err, began) {
|
|
1212
|
+
if (this.j === mine && !scope.signal.aborted && err.code !== "aborted") {
|
|
1213
|
+
if (began < 0) this.b({ error: err });
|
|
1214
|
+
else if (began === this.h) this.b({ answer: null, answering: false, waiting: null, error: err });
|
|
1215
|
+
else this.b({ answer: null, waiting: null, error: err });
|
|
1071
1216
|
}
|
|
1072
1217
|
return { ok: false, error: err };
|
|
1073
1218
|
}
|
|
1074
1219
|
// deliver sends the message and reads its answer. Only refusals that come
|
|
1075
1220
|
// before the chat stores it are tried again: nothing after a 200 is resent.
|
|
1076
|
-
async
|
|
1221
|
+
async ib(text, files, context, signal, recheck) {
|
|
1077
1222
|
if (recheck) {
|
|
1078
|
-
const id = await this.
|
|
1079
|
-
if (id !== null) return this.
|
|
1223
|
+
const id = await this.ta(recheck, signal, null);
|
|
1224
|
+
if (id !== null) return this.ua(id, recheck, signal);
|
|
1080
1225
|
}
|
|
1081
|
-
const sent = { text, files,
|
|
1082
|
-
const path = sent.
|
|
1226
|
+
const sent = { text, files, l: this.a.conversation ? this.a.conversation.id : null, ea: this.K };
|
|
1227
|
+
const path = sent.l === null ? "/conversations" : "/conversations/" + encodeURIComponent(sent.l) + "/messages";
|
|
1083
1228
|
const waitUntil = Date.now() + WAIT_CAP_MS;
|
|
1084
1229
|
const busyUntil = Date.now() + BUSY_CAP_MS;
|
|
1085
1230
|
for (let busyTries = 0; ; ) {
|
|
1086
1231
|
const stream = new AbortController();
|
|
1087
|
-
const
|
|
1232
|
+
const u = sent.l === null ? { r: this.r.signal } : null;
|
|
1233
|
+
const stop = () => {
|
|
1234
|
+
if (!this.ca(u)) stream.abort();
|
|
1235
|
+
};
|
|
1088
1236
|
signal.addEventListener("abort", stop);
|
|
1089
1237
|
try {
|
|
1090
1238
|
let res;
|
|
1091
1239
|
try {
|
|
1092
|
-
res = await this.
|
|
1240
|
+
res = await this.da({ method: "POST", path, body: files.length ? { text, files, context } : { text, context }, stream: true }, stream.signal, sent.l, u);
|
|
1093
1241
|
} catch (e) {
|
|
1242
|
+
this.x(u);
|
|
1094
1243
|
if (signal.aborted) throw aborted();
|
|
1095
|
-
if (isCode(e, "network")) return await this.
|
|
1244
|
+
if (isCode(e, "network")) return await this.Va(e, sent, signal);
|
|
1096
1245
|
const reopen = isChatError(e) ? waitFor(e) : null;
|
|
1097
1246
|
if (reopen !== null && Date.now() + reopen <= waitUntil) {
|
|
1098
|
-
await this.
|
|
1247
|
+
await this.O(e.code, reopen, signal);
|
|
1099
1248
|
continue;
|
|
1100
1249
|
}
|
|
1101
1250
|
throw e;
|
|
1102
1251
|
}
|
|
1103
|
-
if (res.ok) return await this.
|
|
1252
|
+
if (res.ok) return await this.Ua(res, stream, signal, sent, u);
|
|
1253
|
+
this.x(u);
|
|
1104
1254
|
const err = await failure(res, signal);
|
|
1105
|
-
if (err.code === "internal") return await this.
|
|
1106
|
-
if (
|
|
1255
|
+
if (err.code === "internal") return await this.Va(err, sent, signal);
|
|
1256
|
+
if (sent.l !== null) this.qa(sent.l, err);
|
|
1107
1257
|
const wait = waitFor(err);
|
|
1108
1258
|
if (wait !== null && Date.now() + wait <= waitUntil) {
|
|
1109
|
-
await this.
|
|
1259
|
+
await this.O(err.code, wait, signal);
|
|
1110
1260
|
continue;
|
|
1111
1261
|
}
|
|
1112
1262
|
if (err.code === "busy" && Date.now() < busyUntil) {
|
|
1113
|
-
await this.
|
|
1263
|
+
await this.O("busy", BUSY_STEPS_MS[Math.min(busyTries++, BUSY_STEPS_MS.length - 1)], signal);
|
|
1114
1264
|
continue;
|
|
1115
1265
|
}
|
|
1116
1266
|
throw err;
|
|
1117
1267
|
} finally {
|
|
1118
1268
|
signal.removeEventListener("abort", stop);
|
|
1269
|
+
this.x(u);
|
|
1270
|
+
if (u && u.D) u.D();
|
|
1119
1271
|
}
|
|
1120
1272
|
}
|
|
1121
1273
|
}
|
|
1122
|
-
async
|
|
1274
|
+
async O(code, ms, signal) {
|
|
1123
1275
|
live(signal);
|
|
1124
|
-
this.
|
|
1276
|
+
this.b({ waiting: { code, until: Date.now() + ms } });
|
|
1125
1277
|
try {
|
|
1126
1278
|
await sleep(ms, signal);
|
|
1127
1279
|
} finally {
|
|
1128
|
-
if (!signal.aborted) this.
|
|
1280
|
+
if (!signal.aborted) this.b({ waiting: null });
|
|
1129
1281
|
}
|
|
1130
1282
|
}
|
|
1131
1283
|
// ifLanded settles a request whose outcome is unknown, a lost connection or a
|
|
1132
1284
|
// 500: the message may have been stored, so the conversation decides.
|
|
1133
|
-
async
|
|
1134
|
-
const id = await this.
|
|
1285
|
+
async Va(err, sent, signal) {
|
|
1286
|
+
const id = await this.ta(sent, signal, err);
|
|
1135
1287
|
if (id === null) throw err;
|
|
1136
|
-
return this.
|
|
1288
|
+
return this.ua(id, sent, signal);
|
|
1137
1289
|
}
|
|
1138
1290
|
// landedSoon looks for the message, trying again while the chat can't be
|
|
1139
1291
|
// reached. It answers null only when a lookup said the message isn't there;
|
|
1140
1292
|
// when none could tell, it throws, and sending the text again checks first.
|
|
1141
|
-
async
|
|
1293
|
+
async ta(sent, signal, cause) {
|
|
1142
1294
|
const version = this.h;
|
|
1143
1295
|
let last = null;
|
|
1144
1296
|
for (const ms of LOOKUP_STEPS_MS) {
|
|
1145
1297
|
await sleep(ms, signal);
|
|
1146
1298
|
try {
|
|
1147
|
-
return await this.
|
|
1299
|
+
return await this.kb(sent, signal);
|
|
1148
1300
|
} catch (e) {
|
|
1149
1301
|
if (signal.aborted) throw aborted();
|
|
1150
1302
|
last = asChatError(e);
|
|
@@ -1153,58 +1305,58 @@ var Client = class {
|
|
|
1153
1305
|
if (TRANSIENT.indexOf(last.code) < 0) break;
|
|
1154
1306
|
}
|
|
1155
1307
|
}
|
|
1156
|
-
this.
|
|
1308
|
+
this.B = sent;
|
|
1157
1309
|
if (last && TRANSIENT.indexOf(last.code) < 0) throw last;
|
|
1158
1310
|
throw cause || last || new ChatError("network", "The chat couldn't be reached.");
|
|
1159
1311
|
}
|
|
1160
1312
|
// landed finds the conversation that holds the message sent, if the chat
|
|
1161
1313
|
// stored it, and shows it. A new conversation holds nothing older than known.
|
|
1162
|
-
async
|
|
1314
|
+
async kb(sent, signal) {
|
|
1163
1315
|
const mine = (m) => isSent(m, sent);
|
|
1164
1316
|
let found = null;
|
|
1165
|
-
if (sent.
|
|
1166
|
-
const v = await this.
|
|
1317
|
+
if (sent.l !== null) {
|
|
1318
|
+
const v = await this.Wa(sent.l, signal);
|
|
1167
1319
|
if (v.messages.some(mine)) found = v;
|
|
1168
1320
|
} else {
|
|
1169
|
-
const o = await this.
|
|
1170
|
-
if (o.conversation && o.messages.some(mine) && o.messages.every((m) => m.id > sent.
|
|
1171
|
-
found = { conversation: o.conversation, messages: o.messages };
|
|
1321
|
+
const o = await this.lb(signal);
|
|
1322
|
+
if (o.conversation && o.messages.some(mine) && o.messages.every((m) => m.id > sent.ea)) {
|
|
1323
|
+
found = { conversation: o.conversation, messages: o.messages, w: o.w };
|
|
1172
1324
|
}
|
|
1173
1325
|
}
|
|
1174
1326
|
live(signal);
|
|
1175
1327
|
if (!found) return null;
|
|
1176
|
-
this.
|
|
1177
|
-
this.
|
|
1328
|
+
this.v(found.messages);
|
|
1329
|
+
this.b({ ...this.ba(found.conversation, found.messages, found.w), outgoing: null });
|
|
1178
1330
|
return found.conversation.id;
|
|
1179
1331
|
}
|
|
1180
1332
|
// latest opens the session as it is, for its latest conversation: a live
|
|
1181
1333
|
// cookie needs no token, which may have expired meanwhile. A lapsed session is
|
|
1182
1334
|
// reopened as the page's user, never swapped for an anonymous one.
|
|
1183
|
-
async
|
|
1335
|
+
async lb(signal) {
|
|
1184
1336
|
if (this.g.e === "cookie") {
|
|
1185
|
-
const res = await request(this.
|
|
1337
|
+
const res = await request(this.i, this.p, { method: "GET", path: "/session", signal });
|
|
1186
1338
|
if (res.ok) {
|
|
1187
1339
|
discard(res);
|
|
1188
|
-
const o = await this.
|
|
1189
|
-
if (!o.
|
|
1340
|
+
const o = await this.F({}, null, signal);
|
|
1341
|
+
if (!o.J) return o;
|
|
1190
1342
|
} else if (res.status === 401) {
|
|
1191
1343
|
discard(res);
|
|
1192
1344
|
} else {
|
|
1193
1345
|
throw await failure(res, signal);
|
|
1194
1346
|
}
|
|
1195
|
-
return this.
|
|
1347
|
+
return this.sa(signal);
|
|
1196
1348
|
}
|
|
1197
1349
|
try {
|
|
1198
|
-
return await this.
|
|
1350
|
+
return await this.F(this.g.e === "token" ? { cookieless: true } : {}, this.ra(), signal);
|
|
1199
1351
|
} catch (e) {
|
|
1200
1352
|
if (!isCode(e, "token_expired") && !isCode(e, "session_expired") && !isCode(e, "unauthorized")) throw e;
|
|
1201
|
-
return this.
|
|
1353
|
+
return this.sa(signal);
|
|
1202
1354
|
}
|
|
1203
1355
|
}
|
|
1204
1356
|
// read applies the stream's events. A stream that ends without done or error
|
|
1205
1357
|
// lost the answer, which runs on and is stored, so the conversation is followed.
|
|
1206
|
-
async
|
|
1207
|
-
let conversation = sent.
|
|
1358
|
+
async Ua(res, stream, signal, sent, u) {
|
|
1359
|
+
let conversation = sent.l;
|
|
1208
1360
|
let message = false;
|
|
1209
1361
|
const reader = res.body ? res.body.getReader() : null;
|
|
1210
1362
|
if (reader) {
|
|
@@ -1221,26 +1373,32 @@ var Client = class {
|
|
|
1221
1373
|
try {
|
|
1222
1374
|
for (; ; ) {
|
|
1223
1375
|
const { value, done } = await reader.read();
|
|
1224
|
-
if (signal.aborted) throw aborted();
|
|
1376
|
+
if (signal.aborted && !this.ca(u)) throw aborted();
|
|
1225
1377
|
if (done) break;
|
|
1226
1378
|
clearTimeout(stall);
|
|
1227
1379
|
stall = setTimeout(() => stream.abort(), STALL_MS);
|
|
1228
|
-
this.
|
|
1380
|
+
this.la = true;
|
|
1229
1381
|
try {
|
|
1230
1382
|
for (const ev of parser.push(decoder.decode(value, { stream: true }))) {
|
|
1383
|
+
if (this.ca(u)) {
|
|
1384
|
+
if (ev.event !== "conversation") continue;
|
|
1385
|
+
u.D(this.na(conversationOf(JSON.parse(ev.data)).id));
|
|
1386
|
+
throw aborted();
|
|
1387
|
+
}
|
|
1231
1388
|
if (signal.aborted) throw aborted();
|
|
1232
1389
|
if (ev.event === "message" && message) continue;
|
|
1233
1390
|
if (ev.event === "message") message = true;
|
|
1234
|
-
const out = this.
|
|
1391
|
+
const out = this.mb(ev.event, ev.data);
|
|
1235
1392
|
if (!out) continue;
|
|
1236
|
-
if ("conversation" in out)
|
|
1237
|
-
|
|
1393
|
+
if (!("conversation" in out)) return out;
|
|
1394
|
+
conversation = out.conversation;
|
|
1395
|
+
this.x(u);
|
|
1238
1396
|
}
|
|
1239
1397
|
} finally {
|
|
1240
|
-
this.
|
|
1241
|
-
if (this.
|
|
1242
|
-
this.
|
|
1243
|
-
this.
|
|
1398
|
+
this.la = false;
|
|
1399
|
+
if (this.ma) {
|
|
1400
|
+
this.ma = false;
|
|
1401
|
+
this.pa();
|
|
1244
1402
|
}
|
|
1245
1403
|
}
|
|
1246
1404
|
}
|
|
@@ -1255,11 +1413,11 @@ var Client = class {
|
|
|
1255
1413
|
}
|
|
1256
1414
|
}
|
|
1257
1415
|
if (signal.aborted) throw aborted();
|
|
1258
|
-
return this.
|
|
1416
|
+
return this.ua(conversation, { ...sent, l: conversation }, signal);
|
|
1259
1417
|
}
|
|
1260
1418
|
// event applies one event: it answers the reply when done, the conversation
|
|
1261
1419
|
// when one starts, and throws the answer's error.
|
|
1262
|
-
|
|
1420
|
+
mb(name, data) {
|
|
1263
1421
|
if (EVENTS.indexOf(name) < 0) return null;
|
|
1264
1422
|
let o;
|
|
1265
1423
|
try {
|
|
@@ -1271,40 +1429,41 @@ var Client = class {
|
|
|
1271
1429
|
switch (name) {
|
|
1272
1430
|
case "conversation": {
|
|
1273
1431
|
const c = conversationOf(o);
|
|
1274
|
-
|
|
1275
|
-
else this.c({ conversation: c, messages: [] });
|
|
1432
|
+
this.b(this.E(c.id) ? { conversation: c } : { conversation: c, messages: [], hasOlder: false });
|
|
1276
1433
|
return { conversation: c.id };
|
|
1277
1434
|
}
|
|
1278
1435
|
case "message": {
|
|
1279
1436
|
const m = messageOf(o);
|
|
1280
|
-
this.
|
|
1281
|
-
this.
|
|
1437
|
+
const c = this.a.conversation;
|
|
1438
|
+
this.v([m]);
|
|
1439
|
+
this.b({ messages: withMessage(this.a.messages, m), outgoing: null });
|
|
1440
|
+
if (c && c.endedAt) this.b({ conversation: { ...c, endedAt: null } });
|
|
1282
1441
|
return null;
|
|
1283
1442
|
}
|
|
1284
1443
|
case "delta": {
|
|
1285
1444
|
const a = this.a.answer || { text: "", tools: [] };
|
|
1286
1445
|
if (typeof o.text !== "string" || o.text === "" || a.text.length >= ANSWER_CAP) return null;
|
|
1287
|
-
this.
|
|
1446
|
+
this.b({ answer: { text: a.text + o.text, tools: a.tools } });
|
|
1288
1447
|
return null;
|
|
1289
1448
|
}
|
|
1290
1449
|
case "tool": {
|
|
1291
1450
|
const a = this.a.answer || { text: "", tools: [] };
|
|
1292
1451
|
if (typeof o.name !== "string" || typeof o.status !== "string") return null;
|
|
1293
1452
|
const tools = withTool(a.tools, o.name, o.status, typeof o.title === "string" ? o.title : "");
|
|
1294
|
-
if (tools !== a.tools) this.
|
|
1453
|
+
if (tools !== a.tools) this.b({ answer: { text: a.text, tools } });
|
|
1295
1454
|
return null;
|
|
1296
1455
|
}
|
|
1297
1456
|
case "done": {
|
|
1298
1457
|
const m = messageOf(o.message);
|
|
1299
1458
|
const stop = typeof o.stop === "string" ? o.stop : null;
|
|
1300
|
-
this.
|
|
1301
|
-
this.
|
|
1459
|
+
this.v([m]);
|
|
1460
|
+
this.b({ messages: withMessage(this.a.messages, m), outgoing: null, answer: null, answering: false, waiting: null, stop });
|
|
1302
1461
|
return { message: m, stop };
|
|
1303
1462
|
}
|
|
1304
1463
|
default: {
|
|
1305
1464
|
const partial = o.partial ? messageOf(o.partial) : null;
|
|
1306
|
-
if (partial) this.
|
|
1307
|
-
this.
|
|
1465
|
+
if (partial) this.v([partial]);
|
|
1466
|
+
this.b({ messages: partial ? withMessage(this.a.messages, partial) : this.a.messages, outgoing: null, answer: null, answering: false });
|
|
1308
1467
|
const code = typeof o.error === "string" && o.error !== "" ? o.error : "unavailable";
|
|
1309
1468
|
throw new ChatError(code, typeof o.message === "string" ? o.message : "The answer failed.", { status: 200, partial });
|
|
1310
1469
|
}
|
|
@@ -1313,27 +1472,27 @@ var Client = class {
|
|
|
1313
1472
|
// follow waits for an answer whose stream was lost: the conversation, found
|
|
1314
1473
|
// through the session when its first event never came, is asked for until
|
|
1315
1474
|
// it's answered. The message is stored, so a failure here leaves it unsure.
|
|
1316
|
-
async
|
|
1475
|
+
async ua(id, sent, signal) {
|
|
1317
1476
|
const start = sent.text === "" && !sent.files.length;
|
|
1318
1477
|
if (id === null && start) throw new ChatError("network", "The answer was lost.");
|
|
1319
|
-
const cid = id !== null ? id : await this.
|
|
1478
|
+
const cid = id !== null ? id : await this.ta(sent, signal, null);
|
|
1320
1479
|
if (cid === null) {
|
|
1321
|
-
this.
|
|
1480
|
+
this.B = sent;
|
|
1322
1481
|
throw new ChatError("no_answer", "The answer was lost.");
|
|
1323
1482
|
}
|
|
1324
1483
|
const version = this.h;
|
|
1325
1484
|
let v;
|
|
1326
1485
|
try {
|
|
1327
|
-
v = await this.
|
|
1486
|
+
v = await this.Xa(cid, signal);
|
|
1328
1487
|
} catch (e) {
|
|
1329
|
-
if (!start && !signal.aborted && !isCode(e, "not_found") && version === this.h) this.
|
|
1488
|
+
if (!start && !signal.aborted && !isCode(e, "not_found") && version === this.h) this.B = { ...sent, l: cid };
|
|
1330
1489
|
throw e;
|
|
1331
1490
|
}
|
|
1332
1491
|
live(signal);
|
|
1333
|
-
if (this.
|
|
1492
|
+
if (this.E(cid)) this.b({ outgoing: null, answer: null, waiting: null });
|
|
1334
1493
|
let reply;
|
|
1335
1494
|
if (start) {
|
|
1336
|
-
reply = v.messages.filter((m) => m.role === "assistant" && m.id > sent.
|
|
1495
|
+
reply = v.messages.filter((m) => m.role === "assistant" && m.id > sent.ea).pop();
|
|
1337
1496
|
} else {
|
|
1338
1497
|
let i = v.messages.length - 1;
|
|
1339
1498
|
while (i >= 0 && !isSent(v.messages[i], sent)) i--;
|
|
@@ -1342,14 +1501,24 @@ var Client = class {
|
|
|
1342
1501
|
if (!reply) throw new ChatError("no_answer", "The assistant didn't answer.");
|
|
1343
1502
|
return { message: reply, stop: null };
|
|
1344
1503
|
}
|
|
1345
|
-
async
|
|
1346
|
-
const res = await this.
|
|
1347
|
-
if (!res.ok) throw await failure(res, signal);
|
|
1504
|
+
async Wa(id, signal) {
|
|
1505
|
+
const res = await this.da({ method: "GET", path: "/conversations/" + encodeURIComponent(id) }, signal, id);
|
|
1506
|
+
if (!res.ok) throw this.qa(id, await failure(res, signal));
|
|
1348
1507
|
return viewOf(await readJSON(res, signal));
|
|
1349
1508
|
}
|
|
1509
|
+
// page reads a conversation's last 20 messages, or the 20 before message before,
|
|
1510
|
+
// waiting as the chat says.
|
|
1511
|
+
async Da(id, signal, before) {
|
|
1512
|
+
const path = "/conversations/" + encodeURIComponent(id) + (before ? "?before=" + before : "");
|
|
1513
|
+
try {
|
|
1514
|
+
return viewOf(await readJSON(await this.V({ method: "GET", path }, signal), signal));
|
|
1515
|
+
} catch (e) {
|
|
1516
|
+
throw this.qa(id, e);
|
|
1517
|
+
}
|
|
1518
|
+
}
|
|
1350
1519
|
// poll asks for the conversation every few seconds until no answer is being
|
|
1351
1520
|
// written in it, then shows what was stored.
|
|
1352
|
-
async
|
|
1521
|
+
async Xa(id, signal) {
|
|
1353
1522
|
const started = Date.now();
|
|
1354
1523
|
let failures = 0;
|
|
1355
1524
|
for (; ; ) {
|
|
@@ -1358,42 +1527,41 @@ var Client = class {
|
|
|
1358
1527
|
await sleep(elapsed < 6e4 ? POLL_MS : POLL_SLOW_MS, signal);
|
|
1359
1528
|
let v;
|
|
1360
1529
|
try {
|
|
1361
|
-
v = await this.
|
|
1530
|
+
v = await this.Wa(id, signal);
|
|
1362
1531
|
} catch (e) {
|
|
1363
1532
|
if (signal.aborted) throw aborted();
|
|
1364
1533
|
const err = asChatError(e);
|
|
1365
|
-
if (err.code
|
|
1366
|
-
else if (TRANSIENT.indexOf(err.code) >= 0 && ++failures < POLL_FAILURES) continue;
|
|
1534
|
+
if (TRANSIENT.indexOf(err.code) >= 0 && ++failures < POLL_FAILURES) continue;
|
|
1367
1535
|
throw err;
|
|
1368
1536
|
}
|
|
1369
1537
|
failures = 0;
|
|
1370
1538
|
live(signal);
|
|
1371
1539
|
if (v.answering) continue;
|
|
1372
|
-
if (this.
|
|
1373
|
-
this.
|
|
1374
|
-
this.
|
|
1540
|
+
if (this.E(id)) {
|
|
1541
|
+
this.v(v.messages);
|
|
1542
|
+
this.b({ ...this.ba(v.conversation, v.messages, v.w), answering: false });
|
|
1375
1543
|
}
|
|
1376
1544
|
return v;
|
|
1377
1545
|
}
|
|
1378
1546
|
}
|
|
1379
1547
|
// watch follows an answer this page didn't start, such as one a previous
|
|
1380
1548
|
// page left running; a newer watch replaces it.
|
|
1381
|
-
|
|
1382
|
-
if (this.
|
|
1549
|
+
Ea(id) {
|
|
1550
|
+
if (this.u) this.u.abort();
|
|
1383
1551
|
const w = new AbortController();
|
|
1384
|
-
this.
|
|
1385
|
-
const scope = this.
|
|
1552
|
+
this.u = w;
|
|
1553
|
+
const scope = this.A.signal;
|
|
1386
1554
|
const stop = () => w.abort();
|
|
1387
1555
|
scope.addEventListener("abort", stop, { once: true });
|
|
1388
|
-
this.
|
|
1556
|
+
this.Xa(id, w.signal).then(
|
|
1389
1557
|
() => {
|
|
1390
1558
|
},
|
|
1391
1559
|
() => {
|
|
1392
|
-
if (!w.signal.aborted && this.
|
|
1560
|
+
if (!w.signal.aborted && this.E(id)) this.b({ answering: false });
|
|
1393
1561
|
}
|
|
1394
1562
|
).then(() => {
|
|
1395
1563
|
scope.removeEventListener("abort", stop);
|
|
1396
|
-
if (this.
|
|
1564
|
+
if (this.u === w) this.u = null;
|
|
1397
1565
|
});
|
|
1398
1566
|
}
|
|
1399
1567
|
};
|
|
@@ -1410,6 +1578,9 @@ function waitFor(err) {
|
|
|
1410
1578
|
function busy() {
|
|
1411
1579
|
return new ChatError("busy", "Still answering.");
|
|
1412
1580
|
}
|
|
1581
|
+
function listOff() {
|
|
1582
|
+
return new ChatError("conversation_list_off", "This chat lists no conversations.");
|
|
1583
|
+
}
|
|
1413
1584
|
function unopened() {
|
|
1414
1585
|
return new ChatError("unauthorized", "The chat couldn't be opened.");
|
|
1415
1586
|
}
|
|
@@ -1423,7 +1594,7 @@ function checked(o, keys) {
|
|
|
1423
1594
|
}
|
|
1424
1595
|
function idOf(f) {
|
|
1425
1596
|
const id = typeof f === "string" ? f : f && f.id;
|
|
1426
|
-
if (typeof id !== "string" || id === "") throw new TypeError("
|
|
1597
|
+
if (typeof id !== "string" || id === "") throw new TypeError("expected a ChatFile or Conversation, or its id");
|
|
1427
1598
|
return id;
|
|
1428
1599
|
}
|
|
1429
1600
|
function vetted(base, page, max) {
|
|
@@ -1445,7 +1616,7 @@ function bytesOf(s) {
|
|
|
1445
1616
|
return new TextEncoder().encode(s).length;
|
|
1446
1617
|
}
|
|
1447
1618
|
function isSent(m, sent) {
|
|
1448
|
-
if (m.role !== "user" || m.id <= sent.
|
|
1619
|
+
if (m.role !== "user" || m.id <= sent.ea) return false;
|
|
1449
1620
|
return sent.files.length ? !!m.files && m.files.some((f) => f.id === sent.files[0]) : m.text === sent.text;
|
|
1450
1621
|
}
|
|
1451
1622
|
function notForThisChat(origin) {
|
|
@@ -1454,6 +1625,13 @@ function notForThisChat(origin) {
|
|
|
1454
1625
|
function live(signal) {
|
|
1455
1626
|
if (signal.aborted) throw aborted();
|
|
1456
1627
|
}
|
|
1628
|
+
function cut(p, signal) {
|
|
1629
|
+
return new Promise((resolve, reject) => {
|
|
1630
|
+
const stop = () => reject(aborted());
|
|
1631
|
+
signal.addEventListener("abort", stop, { once: true });
|
|
1632
|
+
p.then(resolve, reject).then(() => signal.removeEventListener("abort", stop));
|
|
1633
|
+
});
|
|
1634
|
+
}
|
|
1457
1635
|
function isCode(e, code) {
|
|
1458
1636
|
return isChatError(e) && e.code === code;
|
|
1459
1637
|
}
|
|
@@ -1463,7 +1641,7 @@ function asChatError(e) {
|
|
|
1463
1641
|
}
|
|
1464
1642
|
function whoOf(token) {
|
|
1465
1643
|
const c = claimsOf(token);
|
|
1466
|
-
return c ? c.
|
|
1644
|
+
return c ? c.m : "token";
|
|
1467
1645
|
}
|
|
1468
1646
|
function wellFormed(s) {
|
|
1469
1647
|
let out = "";
|
|
@@ -1514,11 +1692,19 @@ function withTool(tools, name, status, title) {
|
|
|
1514
1692
|
function bad(what) {
|
|
1515
1693
|
return new ChatError("protocol", "Unreadable " + what + " from the chat.");
|
|
1516
1694
|
}
|
|
1517
|
-
function conversationOf(v) {
|
|
1518
|
-
var _a, _b;
|
|
1695
|
+
function conversationOf(v, titled) {
|
|
1696
|
+
var _a, _b, _c;
|
|
1519
1697
|
const o = v;
|
|
1520
1698
|
if (!o || typeof o !== "object" || typeof o.id !== "string") throw bad("conversation");
|
|
1521
|
-
|
|
1699
|
+
const ended = o.ended_at;
|
|
1700
|
+
const c = {
|
|
1701
|
+
id: o.id,
|
|
1702
|
+
createdAt: String((_a = o.created_at) != null ? _a : ""),
|
|
1703
|
+
updatedAt: String((_b = o.updated_at) != null ? _b : ""),
|
|
1704
|
+
endedAt: typeof ended === "string" && ended ? ended : null
|
|
1705
|
+
};
|
|
1706
|
+
if (titled) c.title = String((_c = o.title) != null ? _c : "");
|
|
1707
|
+
return c;
|
|
1522
1708
|
}
|
|
1523
1709
|
function messageOf(v) {
|
|
1524
1710
|
var _a;
|
|
@@ -1557,13 +1743,15 @@ function openedOf(v) {
|
|
|
1557
1743
|
if (!o || typeof o !== "object") throw bad("session");
|
|
1558
1744
|
return {
|
|
1559
1745
|
anonymous: o.anonymous === true,
|
|
1560
|
-
|
|
1746
|
+
J: o.started === true,
|
|
1561
1747
|
conversation: o.conversation ? conversationOf(o.conversation) : null,
|
|
1562
1748
|
messages: messagesOf(o.messages),
|
|
1749
|
+
w: o.has_more === true,
|
|
1563
1750
|
answering: o.answering === true,
|
|
1564
|
-
|
|
1751
|
+
Pa: typeof o.session_token === "string" && o.session_token !== "" ? o.session_token : null,
|
|
1565
1752
|
files: limitsOf(o.files),
|
|
1566
|
-
|
|
1753
|
+
C: typeof o.context_max_bytes === "number" ? o.context_max_bytes : void 0,
|
|
1754
|
+
Qa: o.conversation_list === true
|
|
1567
1755
|
};
|
|
1568
1756
|
}
|
|
1569
1757
|
function limitsOf(v) {
|
|
@@ -1575,7 +1763,7 @@ function limitsOf(v) {
|
|
|
1575
1763
|
function viewOf(v) {
|
|
1576
1764
|
const o = v;
|
|
1577
1765
|
if (!o || typeof o !== "object") throw bad("conversation");
|
|
1578
|
-
return { conversation: conversationOf(o.conversation), messages: messagesOf(o.messages), answering: o.answering === true };
|
|
1766
|
+
return { conversation: conversationOf(o.conversation), messages: messagesOf(o.messages), w: o.has_more === true, answering: o.answering === true };
|
|
1579
1767
|
}
|
|
1580
1768
|
export {
|
|
1581
1769
|
ChatError,
|