@meandr/chat-sdk 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +96 -10
- package/dist/api.d.ts +4 -1
- package/dist/errors.d.ts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +659 -421
- package/dist/index.js.map +3 -3
- package/dist/markdown.js +112 -112
- package/dist/markdown.js.map +3 -3
- package/dist/meandr-chat.js +9 -9
- package/dist/meandr-chat.js.map +3 -3
- package/dist/sse.d.ts +8 -8
- package/dist/token.d.ts +5 -5
- package/dist/types.d.ts +53 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -38,12 +38,13 @@ var LOCAL = /^(localhost|127\.0\.0\.1|\[::1\])$/;
|
|
|
38
38
|
var ANSWER_MS = 3e4;
|
|
39
39
|
var BODY_CAP = 16 << 20;
|
|
40
40
|
var BODY_MS = 18e4;
|
|
41
|
+
var RATE = 32;
|
|
41
42
|
function apiBase(url) {
|
|
42
43
|
let u;
|
|
43
44
|
try {
|
|
44
45
|
u = new URL(url);
|
|
45
46
|
} catch {
|
|
46
|
-
throw new TypeError("url must be an absolute URL
|
|
47
|
+
throw new TypeError("url must be an absolute URL");
|
|
47
48
|
}
|
|
48
49
|
if (u.protocol !== "https:" && !(u.protocol === "http:" && LOCAL.test(u.hostname))) {
|
|
49
50
|
throw new TypeError("url must be an https: URL");
|
|
@@ -54,7 +55,7 @@ function apiBase(url) {
|
|
|
54
55
|
function aborted() {
|
|
55
56
|
return new ChatError("aborted", "Cut short by the page.");
|
|
56
57
|
}
|
|
57
|
-
function within(p, signal, ms,
|
|
58
|
+
function within(p, signal, ms, late2) {
|
|
58
59
|
return new Promise((resolve, reject) => {
|
|
59
60
|
if (signal && signal.aborted) return reject(aborted());
|
|
60
61
|
const stop = () => {
|
|
@@ -67,7 +68,7 @@ function within(p, signal, ms, late) {
|
|
|
67
68
|
};
|
|
68
69
|
const t = setTimeout(() => {
|
|
69
70
|
stop();
|
|
70
|
-
reject(
|
|
71
|
+
reject(late2());
|
|
71
72
|
}, ms);
|
|
72
73
|
if (signal) signal.addEventListener("abort", onAbort, { once: true });
|
|
73
74
|
p.then(
|
|
@@ -82,7 +83,7 @@ function within(p, signal, ms, late) {
|
|
|
82
83
|
);
|
|
83
84
|
});
|
|
84
85
|
}
|
|
85
|
-
async function timed(fetchFn, url, init, signal) {
|
|
86
|
+
async function timed(fetchFn, url, init, signal, ms = ANSWER_MS) {
|
|
86
87
|
if (signal && signal.aborted) throw aborted();
|
|
87
88
|
const ctrl = new AbortController();
|
|
88
89
|
const p = new Promise((resolve, reject) => {
|
|
@@ -93,7 +94,7 @@ async function timed(fetchFn, url, init, signal) {
|
|
|
93
94
|
}
|
|
94
95
|
});
|
|
95
96
|
try {
|
|
96
|
-
return await within(p, signal,
|
|
97
|
+
return await within(p, signal, ms, late);
|
|
97
98
|
} catch (e) {
|
|
98
99
|
ctrl.abort();
|
|
99
100
|
p.then(discard, () => {
|
|
@@ -104,68 +105,111 @@ async function timed(fetchFn, url, init, signal) {
|
|
|
104
105
|
}
|
|
105
106
|
function request(fetchFn, base, c) {
|
|
106
107
|
const headers = { Accept: c.stream ? "text/event-stream" : "application/json" };
|
|
107
|
-
if (c.body !== void 0) headers["Content-Type"] = "application/json";
|
|
108
|
+
if (c.body !== void 0) headers["Content-Type"] = c.type || "application/json";
|
|
108
109
|
if (c.bearer) headers.Authorization = "Bearer " + c.bearer;
|
|
109
110
|
const init = { method: c.method, headers, credentials: "include", cache: "no-store", redirect: "error" };
|
|
110
|
-
if (c.body !== void 0) init.body = JSON.stringify(c.body);
|
|
111
|
-
return timed(fetchFn, base + c.path, init, c.signal);
|
|
111
|
+
if (c.body !== void 0) init.body = c.type ? c.body : JSON.stringify(c.body);
|
|
112
|
+
if (!c.type) return timed(fetchFn, base + c.path, init, c.signal);
|
|
113
|
+
if (c.progress && typeof XMLHttpRequest == "function") return upload(base + c.path, init, c.progress, c.signal);
|
|
114
|
+
return timed(fetchFn, base + c.path, init, c.signal, ANSWER_MS + c.body.size / RATE);
|
|
115
|
+
}
|
|
116
|
+
function upload(url, init, progress, signal) {
|
|
117
|
+
return new Promise((resolve, reject) => {
|
|
118
|
+
if (signal && signal.aborted) return reject(aborted());
|
|
119
|
+
const x = new XMLHttpRequest();
|
|
120
|
+
let t;
|
|
121
|
+
const end = (e) => {
|
|
122
|
+
clearTimeout(t);
|
|
123
|
+
if (signal) signal.removeEventListener("abort", stop);
|
|
124
|
+
if (e) {
|
|
125
|
+
x.abort();
|
|
126
|
+
reject(e);
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
const stop = () => end(aborted());
|
|
130
|
+
const alive = () => {
|
|
131
|
+
clearTimeout(t);
|
|
132
|
+
t = setTimeout(() => end(late()), ANSWER_MS);
|
|
133
|
+
};
|
|
134
|
+
x.open(init.method, url);
|
|
135
|
+
x.withCredentials = true;
|
|
136
|
+
const h = init.headers;
|
|
137
|
+
for (const k in h) x.setRequestHeader(k, h[k]);
|
|
138
|
+
x.upload.onprogress = (e) => {
|
|
139
|
+
alive();
|
|
140
|
+
progress(e.loaded);
|
|
141
|
+
};
|
|
142
|
+
x.onprogress = alive;
|
|
143
|
+
x.onerror = () => end(new ChatError("network", "Couldn't connect."));
|
|
144
|
+
x.onload = () => {
|
|
145
|
+
end();
|
|
146
|
+
try {
|
|
147
|
+
resolve(new Response(x.responseText, { status: x.status, headers: { "Retry-After": x.getResponseHeader("Retry-After") || "" } }));
|
|
148
|
+
} catch {
|
|
149
|
+
reject(new ChatError("protocol", "The chat's answer couldn't be read.", { status: x.status }));
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
if (signal) signal.addEventListener("abort", stop, { once: true });
|
|
153
|
+
alive();
|
|
154
|
+
x.send(init.body);
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
function late() {
|
|
158
|
+
return new ChatError("network", "No answer came in time.");
|
|
112
159
|
}
|
|
113
160
|
function discard(res) {
|
|
114
161
|
if (res.body && !res.body.locked) res.body.cancel().catch(() => {
|
|
115
162
|
});
|
|
116
163
|
}
|
|
117
|
-
async function
|
|
164
|
+
async function chunks(res, signal) {
|
|
118
165
|
const stream = res.body;
|
|
119
166
|
if (!stream) {
|
|
120
|
-
let
|
|
167
|
+
let b;
|
|
121
168
|
try {
|
|
122
|
-
|
|
169
|
+
b = await within(res.arrayBuffer(), signal, ANSWER_MS, stalled);
|
|
123
170
|
} catch (e) {
|
|
124
171
|
throw isChatError(e) ? e : new ChatError("network", "The answer was cut short.");
|
|
125
172
|
}
|
|
126
|
-
|
|
173
|
+
if (b.byteLength > BODY_CAP) throw new ChatError("protocol", "The chat's answer is too large.", { status: res.status });
|
|
174
|
+
return [new Uint8Array(b)];
|
|
127
175
|
}
|
|
128
176
|
const reader = stream.getReader();
|
|
129
|
-
const
|
|
130
|
-
const
|
|
177
|
+
const out = [];
|
|
178
|
+
const began = Date.now();
|
|
131
179
|
let n = 0;
|
|
132
180
|
try {
|
|
133
181
|
for (; ; ) {
|
|
134
182
|
let r;
|
|
135
183
|
try {
|
|
136
|
-
r = await within(reader.read(), signal, Math.max(0, Math.min(ANSWER_MS,
|
|
184
|
+
r = await within(reader.read(), signal, Math.max(0, Math.min(ANSWER_MS, began + BODY_MS + n / RATE - Date.now())), stalled);
|
|
137
185
|
} catch (e) {
|
|
138
186
|
throw isChatError(e) ? e : new ChatError("network", "The answer was cut short.");
|
|
139
187
|
}
|
|
140
|
-
if (r.done)
|
|
188
|
+
if (r.done) return out;
|
|
141
189
|
n += r.value.length;
|
|
142
190
|
if (n > BODY_CAP) throw new ChatError("protocol", "The chat's answer is too large.", { status: res.status });
|
|
143
|
-
|
|
191
|
+
out.push(r.value);
|
|
144
192
|
}
|
|
145
193
|
} catch (e) {
|
|
146
194
|
reader.cancel().catch(() => {
|
|
147
195
|
});
|
|
148
196
|
throw e;
|
|
149
197
|
}
|
|
150
|
-
const all = new Uint8Array(n);
|
|
151
|
-
let at = 0;
|
|
152
|
-
for (const c of chunks) {
|
|
153
|
-
all.set(c, at);
|
|
154
|
-
at += c.length;
|
|
155
|
-
}
|
|
156
|
-
return parse(new TextDecoder().decode(all), res.status);
|
|
157
198
|
}
|
|
158
|
-
function
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
199
|
+
async function body(res, signal) {
|
|
200
|
+
const d = new TextDecoder();
|
|
201
|
+
let text = "";
|
|
202
|
+
for (const c of await chunks(res, signal)) text += d.decode(c, { stream: true });
|
|
203
|
+
text += d.decode();
|
|
163
204
|
try {
|
|
164
205
|
return JSON.parse(text);
|
|
165
206
|
} catch {
|
|
166
|
-
throw new ChatError("protocol", "The chat's answer couldn't be read.", { status });
|
|
207
|
+
throw new ChatError("protocol", "The chat's answer couldn't be read.", { status: res.status });
|
|
167
208
|
}
|
|
168
209
|
}
|
|
210
|
+
function stalled() {
|
|
211
|
+
return new ChatError("network", "The answer stalled.");
|
|
212
|
+
}
|
|
169
213
|
function readJSON(res, signal) {
|
|
170
214
|
return body(res, signal);
|
|
171
215
|
}
|
|
@@ -213,46 +257,46 @@ function sleep(ms, signal) {
|
|
|
213
257
|
var SSETooLarge = class extends Error {
|
|
214
258
|
};
|
|
215
259
|
var SSEParser = class {
|
|
216
|
-
constructor(
|
|
217
|
-
this.
|
|
218
|
-
this.
|
|
219
|
-
this.
|
|
220
|
-
this.
|
|
221
|
-
this.
|
|
222
|
-
this.
|
|
260
|
+
constructor(maxEvent_ = 4 << 20) {
|
|
261
|
+
this.ia = maxEvent_;
|
|
262
|
+
this.P = "";
|
|
263
|
+
this.B = "";
|
|
264
|
+
this.Z = "";
|
|
265
|
+
this.C = false;
|
|
266
|
+
this._ = false;
|
|
223
267
|
}
|
|
224
268
|
push(chunk) {
|
|
225
269
|
const out = [];
|
|
226
270
|
let text = chunk;
|
|
227
|
-
if (!this.
|
|
228
|
-
this.
|
|
271
|
+
if (!this.C && text.length > 0) {
|
|
272
|
+
this.C = true;
|
|
229
273
|
if (text.charCodeAt(0) === 65279) text = text.slice(1);
|
|
230
274
|
}
|
|
231
275
|
let start = 0;
|
|
232
|
-
if (this.
|
|
233
|
-
this.
|
|
276
|
+
if (this._ && text.length > 0) {
|
|
277
|
+
this._ = false;
|
|
234
278
|
if (text.charCodeAt(0) === 10) start = 1;
|
|
235
279
|
}
|
|
236
280
|
for (let i = start; i < text.length; i++) {
|
|
237
281
|
const c = text.charCodeAt(i);
|
|
238
282
|
if (c !== 10 && c !== 13) continue;
|
|
239
|
-
this.
|
|
240
|
-
this.
|
|
283
|
+
this.Ma(this.P + text.slice(start, i), out);
|
|
284
|
+
this.P = "";
|
|
241
285
|
if (c === 13) {
|
|
242
|
-
if (i + 1 === text.length) this.
|
|
286
|
+
if (i + 1 === text.length) this._ = true;
|
|
243
287
|
else if (text.charCodeAt(i + 1) === 10) i++;
|
|
244
288
|
}
|
|
245
289
|
start = i + 1;
|
|
246
290
|
}
|
|
247
|
-
this.
|
|
248
|
-
if (this.
|
|
291
|
+
this.P += text.slice(start);
|
|
292
|
+
if (this.P.length + this.B.length > this.ia) throw new SSETooLarge("event too large");
|
|
249
293
|
return out;
|
|
250
294
|
}
|
|
251
|
-
|
|
295
|
+
Ma(line, out) {
|
|
252
296
|
if (line === "") {
|
|
253
|
-
if (this.
|
|
254
|
-
this.
|
|
255
|
-
this.
|
|
297
|
+
if (this.B !== "") out.push({ event: this.Z || "message", data: this.B.slice(0, -1) });
|
|
298
|
+
this.B = "";
|
|
299
|
+
this.Z = "";
|
|
256
300
|
return;
|
|
257
301
|
}
|
|
258
302
|
if (line.charCodeAt(0) === 58) return;
|
|
@@ -260,10 +304,10 @@ var SSEParser = class {
|
|
|
260
304
|
const field = colon < 0 ? line : line.slice(0, colon);
|
|
261
305
|
let value = colon < 0 ? "" : line.slice(colon + 1);
|
|
262
306
|
if (value.charCodeAt(0) === 32) value = value.slice(1);
|
|
263
|
-
if (field === "event") this.
|
|
307
|
+
if (field === "event") this.Z = value;
|
|
264
308
|
else if (field === "data") {
|
|
265
|
-
this.
|
|
266
|
-
if (this.
|
|
309
|
+
this.B += value + "\n";
|
|
310
|
+
if (this.B.length > this.ia) throw new SSETooLarge("event too large");
|
|
267
311
|
}
|
|
268
312
|
}
|
|
269
313
|
};
|
|
@@ -275,25 +319,25 @@ function hide(token) {
|
|
|
275
319
|
}
|
|
276
320
|
function wayOf(o) {
|
|
277
321
|
for (const k of ["token", "tokenUrl", "getToken"]) {
|
|
278
|
-
if (k in o && o[k] === void 0) throw new TypeError(k + " is undefined:
|
|
322
|
+
if (k in o && o[k] === void 0) throw new TypeError(k + " is undefined: omit it, or null for nobody");
|
|
279
323
|
}
|
|
280
324
|
const given = [o.token, o.tokenUrl, o.getToken].filter((v) => v !== void 0).length;
|
|
281
|
-
if (given > 1) throw new TypeError("
|
|
325
|
+
if (given > 1) throw new TypeError("at most one of token, tokenUrl, getToken");
|
|
282
326
|
if (o.token !== void 0) return tokenWay(o.token);
|
|
283
327
|
if (o.tokenUrl !== void 0) {
|
|
284
328
|
if (typeof o.tokenUrl !== "string" || o.tokenUrl === "") throw new TypeError("tokenUrl must be an address");
|
|
285
|
-
return {
|
|
329
|
+
return { e: "url", url: o.tokenUrl };
|
|
286
330
|
}
|
|
287
331
|
if (o.getToken !== void 0) {
|
|
288
332
|
if (typeof o.getToken !== "function") throw new TypeError("getToken must be a function");
|
|
289
|
-
return {
|
|
333
|
+
return { e: "fn", fn: o.getToken };
|
|
290
334
|
}
|
|
291
|
-
return {
|
|
335
|
+
return { e: "none" };
|
|
292
336
|
}
|
|
293
337
|
function tokenWay(token) {
|
|
294
|
-
if (token === null) return {
|
|
295
|
-
if (typeof token === "string" && usable(token)) return {
|
|
296
|
-
throw new TypeError("token must be a token
|
|
338
|
+
if (token === null) return { e: "token", token: null };
|
|
339
|
+
if (typeof token === "string" && usable(token)) return { e: "token", token: hide(token) };
|
|
340
|
+
throw new TypeError("token must be a token or null");
|
|
297
341
|
}
|
|
298
342
|
function usable(token) {
|
|
299
343
|
if (token === "") return false;
|
|
@@ -304,10 +348,10 @@ function usable(token) {
|
|
|
304
348
|
return true;
|
|
305
349
|
}
|
|
306
350
|
function renews(way) {
|
|
307
|
-
return way.
|
|
351
|
+
return way.e === "url" || way.e === "fn";
|
|
308
352
|
}
|
|
309
353
|
async function userToken(way, fetchFn, signal, expired) {
|
|
310
|
-
switch (way.
|
|
354
|
+
switch (way.e) {
|
|
311
355
|
case "none":
|
|
312
356
|
return void 0;
|
|
313
357
|
case "token":
|
|
@@ -328,17 +372,17 @@ async function fromURL(url, fetchFn, signal) {
|
|
|
328
372
|
throw new ChatError("token_failed", "The token couldn't be fetched.");
|
|
329
373
|
}
|
|
330
374
|
if (res.status === 401 || res.status === 403) return null;
|
|
331
|
-
if (!res.ok) throw new ChatError("token_failed", "
|
|
375
|
+
if (!res.ok) throw new ChatError("token_failed", "tokenUrl answered " + res.status + ".", { status: res.status });
|
|
332
376
|
let v;
|
|
333
377
|
try {
|
|
334
378
|
v = await body(res, signal);
|
|
335
379
|
} catch {
|
|
336
380
|
if (signal.aborted) throw aborted();
|
|
337
|
-
throw new ChatError("token_failed", "
|
|
381
|
+
throw new ChatError("token_failed", "tokenUrl didn't answer JSON.", { status: res.status });
|
|
338
382
|
}
|
|
339
383
|
const token = v && typeof v === "object" ? v.token : void 0;
|
|
340
384
|
if (token === null || typeof token === "string" && usable(token)) return token;
|
|
341
|
-
throw new ChatError("token_failed", '
|
|
385
|
+
throw new ChatError("token_failed", 'tokenUrl must answer {"token": token or null}.', { status: res.status });
|
|
342
386
|
}
|
|
343
387
|
async function fromFunction(fn, signal, expired) {
|
|
344
388
|
let token;
|
|
@@ -352,7 +396,7 @@ async function fromFunction(fn, signal, expired) {
|
|
|
352
396
|
}
|
|
353
397
|
if (signal.aborted) throw aborted();
|
|
354
398
|
if (token === null || typeof token === "string" && usable(token)) return token;
|
|
355
|
-
throw new ChatError("token_failed", "getToken must return a token
|
|
399
|
+
throw new ChatError("token_failed", "getToken must return a token or null.");
|
|
356
400
|
}
|
|
357
401
|
function claimsOf(token) {
|
|
358
402
|
const parts = token.split(".");
|
|
@@ -366,7 +410,7 @@ function claimsOf(token) {
|
|
|
366
410
|
if (!c || typeof c !== "object" || typeof c.sub !== "string") return null;
|
|
367
411
|
const aud = typeof c.aud === "string" ? [c.aud] : Array.isArray(c.aud) ? c.aud.filter((a) => typeof a === "string") : [];
|
|
368
412
|
const empty = !!c.ext && typeof c.ext === "object" && !Array.isArray(c.ext) && Object.keys(c.ext).length === 0;
|
|
369
|
-
return { aud,
|
|
413
|
+
return { aud, j: "user:" + c.sub + "\n" + canonical(c.ext === void 0 || empty ? null : c.ext) };
|
|
370
414
|
} catch {
|
|
371
415
|
return null;
|
|
372
416
|
}
|
|
@@ -389,7 +433,7 @@ function originOf(url) {
|
|
|
389
433
|
}
|
|
390
434
|
|
|
391
435
|
// src/client.ts
|
|
392
|
-
var OPTIONS = ["url", "token", "tokenUrl", "getToken", "fetch"];
|
|
436
|
+
var OPTIONS = ["url", "token", "tokenUrl", "getToken", "fetch", "context"];
|
|
393
437
|
var EVENTS = ["conversation", "message", "delta", "tool", "done", "error"];
|
|
394
438
|
var TRANSIENT = ["network", "internal", "rate_limited", "overloaded", "not_connected", "token_failed"];
|
|
395
439
|
var ANSWER_CAP = 1 << 20;
|
|
@@ -407,6 +451,10 @@ var POLL_FAILURES = 20;
|
|
|
407
451
|
var OPEN_STEPS_MS = [1e3, 2e3, 4e3, 8e3, 15e3];
|
|
408
452
|
var OPEN_LATER_MS = 15e3;
|
|
409
453
|
var SIGN_OUT_MS = 3e3;
|
|
454
|
+
var FILE_CAP = 10 << 20;
|
|
455
|
+
var IMAGE = /^image\/(png|jpeg|webp|gif)$/i;
|
|
456
|
+
var CONTEXT_MS = 5e3;
|
|
457
|
+
var KNOWN = { url: 2048, title: 512, timezone: 64 };
|
|
410
458
|
var IDLE = {
|
|
411
459
|
status: "idle",
|
|
412
460
|
anonymous: null,
|
|
@@ -417,300 +465,337 @@ var IDLE = {
|
|
|
417
465
|
answering: false,
|
|
418
466
|
waiting: null,
|
|
419
467
|
stop: null,
|
|
420
|
-
error: null
|
|
468
|
+
error: null,
|
|
469
|
+
files: null
|
|
421
470
|
};
|
|
422
471
|
function createChat(options) {
|
|
423
472
|
return new Client(options);
|
|
424
473
|
}
|
|
425
474
|
var Client = class {
|
|
426
475
|
constructor(o) {
|
|
427
|
-
this.
|
|
428
|
-
this.
|
|
429
|
-
this.
|
|
430
|
-
this.
|
|
476
|
+
this.a = IDLE;
|
|
477
|
+
this.Q = /* @__PURE__ */ new Set();
|
|
478
|
+
this.g = { e: "cookie" };
|
|
479
|
+
this.j = null;
|
|
431
480
|
// the session's user: a token's sub and ext, "anonymous", or "cookie"
|
|
432
|
-
this.
|
|
481
|
+
this.R = false;
|
|
433
482
|
// learned once: this browser doesn't keep the chat's cookie
|
|
434
|
-
this.
|
|
435
|
-
this.
|
|
483
|
+
this.D = null;
|
|
484
|
+
this.S = new AbortController();
|
|
436
485
|
// aborted by reopen, signOut and destroy
|
|
437
|
-
this.
|
|
486
|
+
this.K = new AbortController();
|
|
438
487
|
// aborted with the session, and by newConversation
|
|
439
|
-
this.
|
|
440
|
-
this.
|
|
488
|
+
this.h = 0;
|
|
489
|
+
this.L = false;
|
|
441
490
|
// newConversation before the session opened
|
|
442
|
-
this.
|
|
443
|
-
this.
|
|
444
|
-
this.
|
|
491
|
+
this.i = null;
|
|
492
|
+
this.r = null;
|
|
493
|
+
this.v = null;
|
|
445
494
|
// a message the chat may have stored: sending it again checks first
|
|
446
|
-
this.
|
|
447
|
-
|
|
448
|
-
this.
|
|
495
|
+
this.E = 0;
|
|
496
|
+
this.ka = /* @__PURE__ */ new Set();
|
|
497
|
+
this.aa = false;
|
|
449
498
|
// a stream's chunk is applied at once: listeners hear it once
|
|
450
|
-
this.
|
|
451
|
-
this.
|
|
452
|
-
this.getState = () => this.
|
|
499
|
+
this.ba = false;
|
|
500
|
+
this.m = false;
|
|
501
|
+
this.getState = () => this.a;
|
|
453
502
|
this.subscribe = (listener) => {
|
|
454
503
|
if (typeof listener !== "function") throw new TypeError("subscribe needs a function");
|
|
455
|
-
if (!this.
|
|
456
|
-
this.
|
|
504
|
+
if (!this.m) this.Q.add(listener);
|
|
505
|
+
this.la(listener);
|
|
457
506
|
return () => {
|
|
458
|
-
this.
|
|
507
|
+
this.Q.delete(listener);
|
|
459
508
|
};
|
|
460
509
|
};
|
|
461
510
|
this.open = () => {
|
|
462
|
-
if (this.
|
|
463
|
-
if (!this.
|
|
464
|
-
const p = this.
|
|
465
|
-
this.
|
|
511
|
+
if (this.m || this.a.status === "ready") return Promise.resolve(this.a);
|
|
512
|
+
if (!this.D) {
|
|
513
|
+
const p = this.Na();
|
|
514
|
+
this.D = p;
|
|
466
515
|
const clear = () => {
|
|
467
|
-
if (this.
|
|
516
|
+
if (this.D === p) this.D = null;
|
|
468
517
|
};
|
|
469
518
|
p.then(clear, clear);
|
|
470
519
|
}
|
|
471
|
-
return this.
|
|
520
|
+
return this.D;
|
|
521
|
+
};
|
|
522
|
+
this.send = (text, options) => {
|
|
523
|
+
if (typeof text !== "string") throw new TypeError("send needs text");
|
|
524
|
+
const files = checked(options, ["files"]).files;
|
|
525
|
+
if (files !== void 0 && !Array.isArray(files)) throw new TypeError("files must be an array");
|
|
526
|
+
return this.Oa(text, files ? files.map(idOf) : []);
|
|
527
|
+
};
|
|
528
|
+
this.attach = (file, options) => {
|
|
529
|
+
var _a, _b;
|
|
530
|
+
if (!(file instanceof Blob)) throw new TypeError("attach needs a Blob");
|
|
531
|
+
const o = checked(options, ["name", "onProgress", "signal"]);
|
|
532
|
+
const on = o.onProgress;
|
|
533
|
+
if (o.name !== void 0 && typeof o.name !== "string") throw new TypeError("name must be a string");
|
|
534
|
+
if (on !== void 0 && typeof on !== "function") throw new TypeError("onProgress must be a function");
|
|
535
|
+
const size = file.size;
|
|
536
|
+
if (size > FILE_CAP) return Promise.resolve({ ok: false, error: new ChatError("too_large", "The file is too large.") });
|
|
537
|
+
const path = "/files?name=" + encodeURIComponent(wellFormed((_b = (_a = o.name) != null ? _a : file.name) != null ? _b : "") || "file");
|
|
538
|
+
return this.ma(o.signal, async (signal) => {
|
|
539
|
+
const l = this.$;
|
|
540
|
+
if (l === null) throw new ChatError("attachments_off", "This chat takes no files.");
|
|
541
|
+
if (l && size > l.maxBytes) throw new ChatError("too_large", "The file is too large.");
|
|
542
|
+
const c = { method: "POST", path, body: file, type: file.type || "application/octet-stream" };
|
|
543
|
+
const res = await this.na(on ? { ...c, progress: (sent) => on(Math.min(sent, size), size) } : c, signal);
|
|
544
|
+
return { ok: true, file: fileOf(await readJSON(res, signal)) };
|
|
545
|
+
});
|
|
472
546
|
};
|
|
473
|
-
this.
|
|
474
|
-
|
|
475
|
-
return this.
|
|
547
|
+
this.readFile = (file, options) => {
|
|
548
|
+
const path = "/files/" + encodeURIComponent(idOf(file));
|
|
549
|
+
return this.ma(checked(options, ["signal"]).signal, async (signal) => {
|
|
550
|
+
const res = await this.na({ method: "GET", path }, signal);
|
|
551
|
+
const t = res.headers.get("Content-Type") || "";
|
|
552
|
+
return { ok: true, blob: new Blob(await chunks(res, signal), { type: IMAGE.test(t) ? t : "application/octet-stream" }) };
|
|
553
|
+
});
|
|
476
554
|
};
|
|
477
|
-
this.start = () => this.
|
|
555
|
+
this.start = () => this.Pa();
|
|
478
556
|
this.newConversation = () => {
|
|
479
|
-
if (this.
|
|
480
|
-
if (this.
|
|
481
|
-
this.
|
|
482
|
-
this.
|
|
557
|
+
if (this.m) return;
|
|
558
|
+
if (this.a.status !== "ready") this.L = true;
|
|
559
|
+
this.oa();
|
|
560
|
+
this.c({ conversation: null, messages: [], outgoing: null, answer: null, answering: false, waiting: null, stop: null, error: null });
|
|
483
561
|
};
|
|
484
562
|
this.reopen = (options) => {
|
|
485
563
|
if (options !== void 0 && (!options || typeof options !== "object")) throw new TypeError("reopen takes { token }");
|
|
486
564
|
const o = options || {};
|
|
487
565
|
for (const k of Object.keys(o)) if (k !== "token") throw new TypeError("unknown option " + k);
|
|
488
|
-
if ("token" in o) this.
|
|
489
|
-
else if (this.
|
|
490
|
-
if (this.
|
|
491
|
-
const idle = this.
|
|
492
|
-
this.
|
|
493
|
-
return idle ? Promise.resolve(this.
|
|
566
|
+
if ("token" in o) this.p = tokenWay(o.token);
|
|
567
|
+
else if (this.p.e === "token") throw new TypeError("reopen needs { token }, or { token: null }");
|
|
568
|
+
if (this.m) return Promise.resolve(this.a);
|
|
569
|
+
const idle = this.a.status === "idle";
|
|
570
|
+
this.pa();
|
|
571
|
+
return idle ? Promise.resolve(this.a) : this.open();
|
|
494
572
|
};
|
|
495
573
|
this.signOut = async () => {
|
|
496
|
-
if (this.
|
|
497
|
-
const auth = this.
|
|
498
|
-
if (this.
|
|
499
|
-
this.
|
|
574
|
+
if (this.m) return;
|
|
575
|
+
const auth = this.g;
|
|
576
|
+
if (this.p.e === "token") this.p = { e: "token", token: null };
|
|
577
|
+
this.pa();
|
|
500
578
|
const stop = new AbortController();
|
|
501
579
|
const t = setTimeout(() => stop.abort(), SIGN_OUT_MS);
|
|
502
580
|
try {
|
|
503
|
-
const bearer = auth.
|
|
504
|
-
discard(await request(this.
|
|
581
|
+
const bearer = auth.e === "session" ? auth.token() : null;
|
|
582
|
+
discard(await request(this.l, this.q, { method: "DELETE", path: "/session", bearer, signal: stop.signal }));
|
|
505
583
|
} catch {
|
|
506
584
|
} finally {
|
|
507
585
|
clearTimeout(t);
|
|
508
586
|
}
|
|
509
587
|
};
|
|
510
588
|
this.destroy = () => {
|
|
511
|
-
if (this.
|
|
512
|
-
this.
|
|
513
|
-
this.
|
|
514
|
-
this.
|
|
589
|
+
if (this.m) return;
|
|
590
|
+
this.qa();
|
|
591
|
+
this.m = true;
|
|
592
|
+
this.Q.clear();
|
|
515
593
|
};
|
|
516
594
|
if (!o || typeof o !== "object") throw new TypeError("createChat needs options");
|
|
517
595
|
for (const k of Object.keys(o)) if (OPTIONS.indexOf(k) < 0) throw new TypeError("unknown option " + k);
|
|
518
|
-
if (typeof o.url !== "string") throw new TypeError("url is required
|
|
596
|
+
if (typeof o.url !== "string") throw new TypeError("url is required");
|
|
519
597
|
if (o.fetch !== void 0 && typeof o.fetch !== "function") throw new TypeError("fetch must be a function");
|
|
520
|
-
|
|
521
|
-
this.
|
|
522
|
-
this.
|
|
598
|
+
if (o.context !== void 0 && typeof o.context !== "function") throw new TypeError("context must be a function");
|
|
599
|
+
this.ja = o.context;
|
|
600
|
+
this.q = apiBase(o.url);
|
|
601
|
+
this.J = originOf(this.q);
|
|
602
|
+
this.p = wayOf(o);
|
|
523
603
|
const f = o.fetch;
|
|
524
|
-
this.
|
|
604
|
+
this.l = f ? (input, init) => f(input, init) : (input, init) => fetch(input, init);
|
|
525
605
|
}
|
|
526
606
|
// --- state
|
|
527
|
-
|
|
528
|
-
this.
|
|
529
|
-
if (this.
|
|
530
|
-
else this.
|
|
607
|
+
c(patch) {
|
|
608
|
+
this.a = { ...this.a, ...patch };
|
|
609
|
+
if (this.aa) this.ba = true;
|
|
610
|
+
else this.ca();
|
|
531
611
|
}
|
|
532
|
-
|
|
533
|
-
if (this.
|
|
534
|
-
for (const l of Array.from(this.
|
|
612
|
+
ca() {
|
|
613
|
+
if (this.m) return;
|
|
614
|
+
for (const l of Array.from(this.Q)) this.la(l);
|
|
535
615
|
}
|
|
536
|
-
|
|
616
|
+
la(listener) {
|
|
537
617
|
try {
|
|
538
|
-
listener(this.
|
|
618
|
+
listener(this.a);
|
|
539
619
|
} catch (e) {
|
|
540
620
|
setTimeout(() => {
|
|
541
621
|
throw e;
|
|
542
622
|
});
|
|
543
623
|
}
|
|
544
624
|
}
|
|
545
|
-
|
|
546
|
-
for (const m of messages) if (m.id > this.
|
|
547
|
-
}
|
|
548
|
-
|
|
549
|
-
return this.
|
|
550
|
-
}
|
|
551
|
-
|
|
552
|
-
this.
|
|
553
|
-
this.
|
|
554
|
-
this.
|
|
555
|
-
this.
|
|
556
|
-
this.
|
|
557
|
-
this.
|
|
558
|
-
}
|
|
559
|
-
|
|
560
|
-
this.
|
|
561
|
-
this.
|
|
562
|
-
this.
|
|
563
|
-
this.
|
|
564
|
-
}
|
|
565
|
-
|
|
566
|
-
this.
|
|
567
|
-
this.
|
|
568
|
-
this.
|
|
569
|
-
this.
|
|
570
|
-
this.
|
|
571
|
-
this.
|
|
572
|
-
this.
|
|
625
|
+
x(messages) {
|
|
626
|
+
for (const m of messages) if (m.id > this.E) this.E = m.id;
|
|
627
|
+
}
|
|
628
|
+
T(id) {
|
|
629
|
+
return this.a.conversation !== null && this.a.conversation.id === id;
|
|
630
|
+
}
|
|
631
|
+
oa() {
|
|
632
|
+
this.K.abort();
|
|
633
|
+
this.K = new AbortController();
|
|
634
|
+
this.h++;
|
|
635
|
+
this.i = null;
|
|
636
|
+
this.r = null;
|
|
637
|
+
this.v = null;
|
|
638
|
+
}
|
|
639
|
+
qa() {
|
|
640
|
+
this.S.abort();
|
|
641
|
+
this.S = new AbortController();
|
|
642
|
+
this.D = null;
|
|
643
|
+
this.oa();
|
|
644
|
+
}
|
|
645
|
+
pa() {
|
|
646
|
+
this.qa();
|
|
647
|
+
this.g = { e: "cookie" };
|
|
648
|
+
this.j = null;
|
|
649
|
+
this.L = false;
|
|
650
|
+
this.E = 0;
|
|
651
|
+
this.$ = this.w = void 0;
|
|
652
|
+
this.a = IDLE;
|
|
653
|
+
this.ca();
|
|
573
654
|
}
|
|
574
655
|
// userChanged wipes what the page shows when a reopened session is someone
|
|
575
656
|
// else's, so nobody sees another's conversation or draft, then shows theirs.
|
|
576
|
-
|
|
577
|
-
if (this.
|
|
578
|
-
this.
|
|
579
|
-
this.
|
|
580
|
-
this.
|
|
581
|
-
this.
|
|
582
|
-
this.
|
|
583
|
-
this.
|
|
584
|
-
this.
|
|
657
|
+
Qa(o) {
|
|
658
|
+
if (this.r) this.r.abort();
|
|
659
|
+
this.r = null;
|
|
660
|
+
this.h++;
|
|
661
|
+
this.v = null;
|
|
662
|
+
this.L = false;
|
|
663
|
+
this.E = 0;
|
|
664
|
+
this.a = { ...IDLE, status: "ready", files: this.a.files };
|
|
665
|
+
this.ra(o, this.h, false);
|
|
585
666
|
}
|
|
586
667
|
// show applies an opened session; hide keeps its conversation off the page,
|
|
587
668
|
// after newConversation, while still learning its newest message.
|
|
588
|
-
|
|
589
|
-
this.
|
|
590
|
-
if (version !== this.
|
|
591
|
-
this.
|
|
669
|
+
ra(o, version, hide2) {
|
|
670
|
+
this.x(o.messages);
|
|
671
|
+
if (version !== this.h) {
|
|
672
|
+
this.c({ anonymous: o.anonymous });
|
|
592
673
|
return;
|
|
593
674
|
}
|
|
594
675
|
if (hide2) {
|
|
595
|
-
this.
|
|
676
|
+
this.c({ anonymous: o.anonymous, conversation: null, messages: [], answering: false });
|
|
596
677
|
return;
|
|
597
678
|
}
|
|
598
679
|
const answering = o.answering && o.conversation !== null;
|
|
599
|
-
this.
|
|
600
|
-
if (answering && o.conversation) this.
|
|
680
|
+
this.c({ anonymous: o.anonymous, conversation: o.conversation, messages: merge(this.a.messages, o.messages), answering });
|
|
681
|
+
if (answering && o.conversation) this.Ra(o.conversation.id);
|
|
601
682
|
}
|
|
602
|
-
|
|
603
|
-
if (this.
|
|
683
|
+
sa(id) {
|
|
684
|
+
if (this.T(id)) this.c({ conversation: null, messages: [], answer: null, answering: false });
|
|
604
685
|
}
|
|
605
686
|
// --- opening
|
|
606
|
-
async
|
|
607
|
-
const session = this.
|
|
608
|
-
const version = this.
|
|
687
|
+
async Na() {
|
|
688
|
+
const session = this.S;
|
|
689
|
+
const version = this.h;
|
|
609
690
|
const began = Date.now();
|
|
610
|
-
this.
|
|
691
|
+
this.c({ status: "opening", error: null });
|
|
611
692
|
for (let tries = 0; ; tries++) {
|
|
612
693
|
try {
|
|
613
|
-
const o = await this.
|
|
614
|
-
if (session.signal.aborted) return this.
|
|
615
|
-
const hidden = this.
|
|
616
|
-
this.
|
|
617
|
-
this.
|
|
618
|
-
this.
|
|
619
|
-
return this.
|
|
694
|
+
const o = await this.ta(session.signal);
|
|
695
|
+
if (session.signal.aborted) return this.a;
|
|
696
|
+
const hidden = this.L;
|
|
697
|
+
this.L = false;
|
|
698
|
+
this.ra(o, version, hidden);
|
|
699
|
+
this.c({ status: "ready", waiting: null });
|
|
700
|
+
return this.a;
|
|
620
701
|
} catch (e) {
|
|
621
|
-
if (session.signal.aborted) return this.
|
|
702
|
+
if (session.signal.aborted) return this.a;
|
|
622
703
|
const err = asChatError(e);
|
|
623
704
|
if (TRANSIENT.indexOf(err.code) < 0 || err.code === "token_failed") {
|
|
624
|
-
this.
|
|
625
|
-
return this.
|
|
705
|
+
this.c({ status: "error", error: err, waiting: null });
|
|
706
|
+
return this.a;
|
|
626
707
|
}
|
|
627
708
|
const ms = openWait(err, tries, Date.now() - began);
|
|
628
|
-
this.
|
|
709
|
+
this.c({ waiting: { code: err.code, until: Date.now() + ms } });
|
|
629
710
|
try {
|
|
630
711
|
await sleep(ms, session.signal);
|
|
631
712
|
} catch {
|
|
632
|
-
return this.
|
|
713
|
+
return this.a;
|
|
633
714
|
}
|
|
634
715
|
}
|
|
635
716
|
}
|
|
636
717
|
}
|
|
637
718
|
// establish opens or resumes the session for the page's user and settles how
|
|
638
719
|
// requests carry it.
|
|
639
|
-
async
|
|
640
|
-
const token = await userToken(this.
|
|
641
|
-
if (typeof token !== "string") return this.
|
|
720
|
+
async ta(signal) {
|
|
721
|
+
const token = await userToken(this.p, this.l, signal, false);
|
|
722
|
+
if (typeof token !== "string") return this.ua(token === null, signal);
|
|
642
723
|
try {
|
|
643
|
-
return await this.
|
|
724
|
+
return await this.va(token, signal);
|
|
644
725
|
} catch (e) {
|
|
645
|
-
if (!isCode(e, "token_expired") || !renews(this.
|
|
726
|
+
if (!isCode(e, "token_expired") || !renews(this.p)) throw e;
|
|
646
727
|
}
|
|
647
|
-
const next = await userToken(this.
|
|
648
|
-
if (typeof next !== "string") return this.
|
|
649
|
-
return this.
|
|
728
|
+
const next = await userToken(this.p, this.l, signal, true);
|
|
729
|
+
if (typeof next !== "string") return this.ua(next === null, signal);
|
|
730
|
+
return this.va(next, signal);
|
|
650
731
|
}
|
|
651
|
-
async
|
|
652
|
-
if (!this.
|
|
732
|
+
async va(token, signal) {
|
|
733
|
+
if (!this.wa(token)) throw notForThisChat(this.J);
|
|
653
734
|
const who = whoOf(token);
|
|
654
|
-
if (this.
|
|
655
|
-
const o2 = await this.
|
|
656
|
-
this.
|
|
657
|
-
this.
|
|
735
|
+
if (this.R) {
|
|
736
|
+
const o2 = await this.y({ cookieless: true }, token, signal);
|
|
737
|
+
this.g = { e: "token", token: hide(token) };
|
|
738
|
+
this.j = who;
|
|
658
739
|
return o2;
|
|
659
740
|
}
|
|
660
|
-
const o = await this.
|
|
661
|
-
if (o.
|
|
662
|
-
this.
|
|
663
|
-
this.
|
|
741
|
+
const o = await this.y({}, token, signal);
|
|
742
|
+
if (o.C && !await this.xa(signal)) {
|
|
743
|
+
this.R = true;
|
|
744
|
+
this.g = { e: "token", token: hide(token) };
|
|
664
745
|
} else {
|
|
665
|
-
this.
|
|
746
|
+
this.g = { e: "cookie" };
|
|
666
747
|
}
|
|
667
|
-
this.
|
|
748
|
+
this.j = who;
|
|
668
749
|
return o;
|
|
669
750
|
}
|
|
670
751
|
// openWithout opens with no customer's token. When the page says nobody is
|
|
671
752
|
// signed in, a signed-in session the browser still holds is ended first.
|
|
672
|
-
async
|
|
673
|
-
const held = this.
|
|
674
|
-
if (held.
|
|
753
|
+
async ua(nobody, signal) {
|
|
754
|
+
const held = this.g;
|
|
755
|
+
if (held.e === "session") {
|
|
675
756
|
try {
|
|
676
|
-
const o2 = await this.
|
|
677
|
-
this.
|
|
757
|
+
const o2 = await this.y({}, held.token(), signal);
|
|
758
|
+
this.j = "anonymous";
|
|
678
759
|
return o2;
|
|
679
760
|
} catch (e) {
|
|
680
761
|
if (!isCode(e, "session_expired") && !isCode(e, "unauthorized")) throw e;
|
|
681
762
|
}
|
|
682
763
|
}
|
|
683
|
-
this.
|
|
684
|
-
if (this.
|
|
685
|
-
if (nobody && await this.
|
|
686
|
-
const o = await this.
|
|
764
|
+
this.g = { e: "cookie" };
|
|
765
|
+
if (this.R) return this.ya(signal);
|
|
766
|
+
if (nobody && await this.Sa(signal)) await this.F(signal);
|
|
767
|
+
const o = await this.y({}, null, signal);
|
|
687
768
|
if (nobody && !o.anonymous) {
|
|
688
|
-
throw new ChatError("protocol", "
|
|
769
|
+
throw new ChatError("protocol", "Signed in, though the page says nobody is.");
|
|
689
770
|
}
|
|
690
|
-
if (o.
|
|
691
|
-
this.
|
|
692
|
-
return this.
|
|
771
|
+
if (o.C && !await this.xa(signal)) {
|
|
772
|
+
this.R = true;
|
|
773
|
+
return this.ya(signal);
|
|
693
774
|
}
|
|
694
|
-
this.
|
|
775
|
+
this.j = o.anonymous ? "anonymous" : "cookie";
|
|
695
776
|
return o;
|
|
696
777
|
}
|
|
697
|
-
async
|
|
698
|
-
const o = await this.
|
|
699
|
-
if (!o.
|
|
700
|
-
this.
|
|
701
|
-
this.
|
|
778
|
+
async ya(signal) {
|
|
779
|
+
const o = await this.y({ cookieless: true }, null, signal);
|
|
780
|
+
if (!o.za) throw new ChatError("protocol", "The chat gave no session token.");
|
|
781
|
+
this.g = { e: "session", token: hide(o.za) };
|
|
782
|
+
this.j = "anonymous";
|
|
702
783
|
return o;
|
|
703
784
|
}
|
|
704
|
-
async
|
|
705
|
-
const res = await request(this.
|
|
785
|
+
async y(body2, bearer, signal) {
|
|
786
|
+
const res = await request(this.l, this.q, { method: "POST", path: "/session", body: body2, bearer, signal });
|
|
706
787
|
if (!res.ok) throw await failure(res, signal);
|
|
707
788
|
const o = openedOf(await readJSON(res, signal));
|
|
708
789
|
live(signal);
|
|
790
|
+
this.$ = o.files;
|
|
791
|
+
this.w = o.w;
|
|
792
|
+
const files = o.files || null;
|
|
793
|
+
if (JSON.stringify(files) !== JSON.stringify(this.a.files)) this.c({ files });
|
|
709
794
|
return o;
|
|
710
795
|
}
|
|
711
796
|
// cookieKept asks, with the cookie alone, whether the browser kept the session it was just given.
|
|
712
|
-
async
|
|
713
|
-
const res = await request(this.
|
|
797
|
+
async xa(signal) {
|
|
798
|
+
const res = await request(this.l, this.q, { method: "GET", path: "/session", signal });
|
|
714
799
|
if (res.ok || res.status === 401) {
|
|
715
800
|
discard(res);
|
|
716
801
|
live(signal);
|
|
@@ -721,8 +806,8 @@ var Client = class {
|
|
|
721
806
|
// signedIn asks, with the cookie alone, whether the browser holds a signed-in
|
|
722
807
|
// session, without fetching that user's conversation. One the chat refuses to
|
|
723
808
|
// serve (403) is ended too: it's of no use to anyone.
|
|
724
|
-
async
|
|
725
|
-
const res = await request(this.
|
|
809
|
+
async Sa(signal) {
|
|
810
|
+
const res = await request(this.l, this.q, { method: "GET", path: "/session", signal });
|
|
726
811
|
if (res.status === 401 || res.status === 403) {
|
|
727
812
|
discard(res);
|
|
728
813
|
return res.status === 403;
|
|
@@ -731,129 +816,214 @@ var Client = class {
|
|
|
731
816
|
const v = await readJSON(res, signal);
|
|
732
817
|
return !!v && typeof v === "object" && v.anonymous === false;
|
|
733
818
|
}
|
|
734
|
-
async
|
|
735
|
-
const res = await request(this.
|
|
819
|
+
async F(signal) {
|
|
820
|
+
const res = await request(this.l, this.q, { method: "DELETE", path: "/session", signal });
|
|
736
821
|
if (!res.ok) throw await failure(res, signal);
|
|
737
822
|
discard(res);
|
|
738
823
|
}
|
|
739
824
|
// mayCarry: a token goes only to the chat its aud names, or to the page's own
|
|
740
825
|
// server, which forwards the chat.
|
|
741
|
-
|
|
826
|
+
wa(token) {
|
|
742
827
|
const page = typeof location === "object" && location ? location.origin : null;
|
|
743
|
-
if (page === this.
|
|
828
|
+
if (page === this.J) return true;
|
|
744
829
|
const c = claimsOf(token);
|
|
745
|
-
return !!c && c.aud.some((a) => originOf(a) === this.
|
|
830
|
+
return !!c && c.aud.some((a) => originOf(a) === this.J);
|
|
746
831
|
}
|
|
747
832
|
// --- requests as the session's user
|
|
748
|
-
|
|
749
|
-
return this.
|
|
833
|
+
Aa() {
|
|
834
|
+
return this.g.e === "cookie" ? null : this.g.token();
|
|
750
835
|
}
|
|
751
836
|
// authed makes a request as the session's user; a lapsed token or session is
|
|
752
837
|
// renewed or reopened once, for target's conversation.
|
|
753
|
-
async
|
|
838
|
+
async U(c, signal, target) {
|
|
754
839
|
for (let retried = false; ; retried = true) {
|
|
755
|
-
const res = await request(this.
|
|
840
|
+
const res = await request(this.l, this.q, { ...c, bearer: this.Aa(), signal });
|
|
756
841
|
if (res.status !== 401 || retried) return res;
|
|
757
842
|
const err = await failure(res, signal);
|
|
758
|
-
if (!await this.
|
|
843
|
+
if (!await this.Ta(err, signal, target)) throw err;
|
|
759
844
|
}
|
|
760
845
|
}
|
|
761
846
|
// recoverAuth renews a lapsed token or reopens an ended session. It answers
|
|
762
847
|
// whether to try again, and throws session_expired when the user changed.
|
|
763
|
-
async
|
|
764
|
-
if (this.
|
|
765
|
-
if (!renews(this.
|
|
766
|
-
const token = await userToken(this.
|
|
767
|
-
if (typeof token === "string" && whoOf(token) === this.
|
|
768
|
-
if (!this.
|
|
769
|
-
this.
|
|
848
|
+
async Ta(err, signal, target) {
|
|
849
|
+
if (this.g.e === "token" && (err.code === "token_expired" || err.code === "unauthorized")) {
|
|
850
|
+
if (!renews(this.p)) return false;
|
|
851
|
+
const token = await userToken(this.p, this.l, signal, err.code === "token_expired");
|
|
852
|
+
if (typeof token === "string" && whoOf(token) === this.j) {
|
|
853
|
+
if (!this.wa(token)) throw notForThisChat(this.J);
|
|
854
|
+
this.g = { e: "token", token: hide(token) };
|
|
770
855
|
return true;
|
|
771
856
|
}
|
|
772
857
|
} else if (err.code !== "session_expired" && err.code !== "unauthorized") {
|
|
773
858
|
return false;
|
|
774
859
|
}
|
|
775
|
-
const o = await this.
|
|
776
|
-
this.
|
|
860
|
+
const o = await this.da(signal);
|
|
861
|
+
this.c({ anonymous: o.anonymous });
|
|
777
862
|
return true;
|
|
778
863
|
}
|
|
779
864
|
// reestablish opens the session again as the page's user, within the flow's
|
|
780
865
|
// signal, so a sign-out meanwhile stops it. Someone else's session wipes the
|
|
781
866
|
// page, and the caller fails with session_expired.
|
|
782
|
-
async
|
|
867
|
+
async da(signal) {
|
|
783
868
|
live(signal);
|
|
784
|
-
const who = this.
|
|
785
|
-
const o = await this.
|
|
869
|
+
const who = this.j;
|
|
870
|
+
const o = await this.ta(signal);
|
|
786
871
|
live(signal);
|
|
787
|
-
if (this.
|
|
788
|
-
this.
|
|
872
|
+
if (this.j !== who) {
|
|
873
|
+
this.Qa(o);
|
|
789
874
|
throw new ChatError("session_expired", "Someone else is signed in now.", { status: 401 });
|
|
790
875
|
}
|
|
791
|
-
this.
|
|
876
|
+
this.x(o.messages);
|
|
792
877
|
return o;
|
|
793
878
|
}
|
|
879
|
+
// --- files
|
|
880
|
+
// flow runs attach's or readFile's request as the session's user, opening it
|
|
881
|
+
// as send does. The page's signal or the session's end cuts it short.
|
|
882
|
+
async ma(signal, run) {
|
|
883
|
+
if (this.m) return { ok: false, error: aborted() };
|
|
884
|
+
const ctrl = new AbortController();
|
|
885
|
+
const stop = () => ctrl.abort();
|
|
886
|
+
const session = this.S.signal;
|
|
887
|
+
session.addEventListener("abort", stop);
|
|
888
|
+
if (signal) signal.addEventListener("abort", stop);
|
|
889
|
+
if (session.aborted || signal && signal.aborted) stop();
|
|
890
|
+
try {
|
|
891
|
+
live(ctrl.signal);
|
|
892
|
+
if (this.a.status !== "ready") {
|
|
893
|
+
const st = await new Promise((resolve, reject) => {
|
|
894
|
+
ctrl.signal.addEventListener("abort", () => reject(aborted()));
|
|
895
|
+
this.open().then(resolve);
|
|
896
|
+
});
|
|
897
|
+
if (st.status !== "ready") throw st.error || unopened();
|
|
898
|
+
}
|
|
899
|
+
return await run(ctrl.signal);
|
|
900
|
+
} catch (e) {
|
|
901
|
+
return { ok: false, error: ctrl.signal.aborted ? aborted() : asChatError(e) };
|
|
902
|
+
} finally {
|
|
903
|
+
session.removeEventListener("abort", stop);
|
|
904
|
+
if (signal) signal.removeEventListener("abort", stop);
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
// retried makes a request that nothing is stored by when it's refused: a 401
|
|
908
|
+
// reopens the session once, and 429 and 503 wait as the chat says, up to a minute.
|
|
909
|
+
async na(c, signal) {
|
|
910
|
+
const until = Date.now() + WAIT_CAP_MS;
|
|
911
|
+
for (; ; ) {
|
|
912
|
+
let err;
|
|
913
|
+
try {
|
|
914
|
+
const res = await this.U(c, signal, null);
|
|
915
|
+
if (res.ok) return res;
|
|
916
|
+
err = await failure(res, signal);
|
|
917
|
+
} catch (e) {
|
|
918
|
+
err = asChatError(e);
|
|
919
|
+
}
|
|
920
|
+
const wait = waitFor(err);
|
|
921
|
+
if (wait === null || Date.now() + wait > until) throw err;
|
|
922
|
+
await sleep(wait, signal);
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
// --- page context
|
|
926
|
+
// contextOf is what a message or a start tells of the page: the browser's time zone
|
|
927
|
+
// under what the context option gives. That part is left out, with a warning, when
|
|
928
|
+
// the option fails or the chat would refuse it, so it never costs the message.
|
|
929
|
+
async Ba(signal) {
|
|
930
|
+
let zone;
|
|
931
|
+
try {
|
|
932
|
+
zone = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
933
|
+
} catch {
|
|
934
|
+
}
|
|
935
|
+
const base = zone ? { timezone: zone } : {};
|
|
936
|
+
const fn = this.ja;
|
|
937
|
+
if (fn && this.w !== void 0) {
|
|
938
|
+
let page;
|
|
939
|
+
try {
|
|
940
|
+
page = await within(new Promise((resolve) => resolve(fn())), signal, CONTEXT_MS, () => new Error("timeout"));
|
|
941
|
+
} catch (e) {
|
|
942
|
+
if (signal.aborted) throw aborted();
|
|
943
|
+
this.Ca("the option failed or took 5 s", e);
|
|
944
|
+
}
|
|
945
|
+
if (page != null) {
|
|
946
|
+
const v2 = vetted(base, page, this.w);
|
|
947
|
+
if (typeof v2 !== "string") return v2;
|
|
948
|
+
this.Ca(v2);
|
|
949
|
+
}
|
|
950
|
+
}
|
|
951
|
+
const v = vetted(base, {}, this.w);
|
|
952
|
+
return typeof v === "string" ? void 0 : v;
|
|
953
|
+
}
|
|
954
|
+
// warn tells the page's developer, once for each reason, that its context was left out.
|
|
955
|
+
Ca(why, ...cause) {
|
|
956
|
+
if (this.ka.has(why)) return;
|
|
957
|
+
this.ka.add(why);
|
|
958
|
+
console.warn("@meandr/chat-sdk: context left out: " + why, ...cause);
|
|
959
|
+
}
|
|
794
960
|
// --- messages
|
|
795
|
-
async
|
|
961
|
+
async Oa(raw, files) {
|
|
796
962
|
const text = normalize(raw);
|
|
797
|
-
if (this.
|
|
798
|
-
if (text === "") return { ok: false, error: new ChatError("bad_request", "The message is empty.") };
|
|
799
|
-
if (this.
|
|
963
|
+
if (this.m) return { ok: false, error: aborted() };
|
|
964
|
+
if (text === "" && !files.length) return { ok: false, error: new ChatError("bad_request", "The message is empty.") };
|
|
965
|
+
if (this.i || this.a.answering) return { ok: false, error: busy() };
|
|
800
966
|
const mine = {};
|
|
801
|
-
this.
|
|
802
|
-
const scope = this.
|
|
803
|
-
const
|
|
804
|
-
|
|
967
|
+
this.i = mine;
|
|
968
|
+
const scope = this.K;
|
|
969
|
+
const u = this.v;
|
|
970
|
+
const recheck = u && u.text === text && String(u.files) === String(files) ? u : null;
|
|
971
|
+
this.v = null;
|
|
805
972
|
let began = -1;
|
|
806
|
-
this.
|
|
973
|
+
this.c({ outgoing: text, error: null, stop: null });
|
|
807
974
|
try {
|
|
808
|
-
if (this.
|
|
975
|
+
if (this.a.status !== "ready") {
|
|
809
976
|
const st = await this.open();
|
|
810
|
-
if (scope.signal.aborted || this.
|
|
811
|
-
if (st.status !== "ready") return this.
|
|
812
|
-
if (this.
|
|
977
|
+
if (scope.signal.aborted || this.i !== mine) return { ok: false, error: aborted() };
|
|
978
|
+
if (st.status !== "ready") return this.G(mine, scope, st.error || unopened(), -1);
|
|
979
|
+
if (this.a.answering) return this.G(mine, scope, busy(), -1);
|
|
813
980
|
}
|
|
814
|
-
|
|
815
|
-
this.
|
|
816
|
-
|
|
981
|
+
const context = await this.Ba(scope.signal);
|
|
982
|
+
began = this.h;
|
|
983
|
+
this.c({ answering: true, answer: { text: "", tools: [] } });
|
|
984
|
+
const r = await this.Ua(text, files, context, scope.signal, recheck);
|
|
817
985
|
return { ok: true, message: r.message, stop: r.stop };
|
|
818
986
|
} catch (e) {
|
|
819
|
-
return this.
|
|
987
|
+
return this.G(mine, scope, asChatError(e), began);
|
|
820
988
|
} finally {
|
|
821
|
-
if (this.
|
|
989
|
+
if (this.i === mine) this.i = null;
|
|
822
990
|
}
|
|
823
991
|
}
|
|
824
992
|
// doStart opens if need be, then has the assistant speak first; the chat
|
|
825
|
-
// supplies the instructions, so
|
|
826
|
-
async
|
|
827
|
-
if (this.
|
|
828
|
-
if (this.
|
|
993
|
+
// supplies the instructions, so the page sends only its context.
|
|
994
|
+
async Pa() {
|
|
995
|
+
if (this.m) return { ok: false, error: aborted() };
|
|
996
|
+
if (this.i || this.a.answering) return { ok: false, error: busy() };
|
|
829
997
|
const mine = {};
|
|
830
|
-
this.
|
|
831
|
-
const scope = this.
|
|
998
|
+
this.i = mine;
|
|
999
|
+
const scope = this.K;
|
|
832
1000
|
let began = -1;
|
|
833
|
-
this.
|
|
1001
|
+
this.c({ error: null, stop: null });
|
|
834
1002
|
try {
|
|
835
|
-
if (this.
|
|
1003
|
+
if (this.a.status !== "ready") {
|
|
836
1004
|
const st = await this.open();
|
|
837
|
-
if (scope.signal.aborted || this.
|
|
838
|
-
if (st.status !== "ready") return this.
|
|
839
|
-
if (this.
|
|
1005
|
+
if (scope.signal.aborted || this.i !== mine) return { ok: false, error: aborted() };
|
|
1006
|
+
if (st.status !== "ready") return this.G(mine, scope, st.error || unopened(), -1);
|
|
1007
|
+
if (this.a.answering) return this.G(mine, scope, busy(), -1);
|
|
840
1008
|
}
|
|
841
|
-
|
|
842
|
-
this.
|
|
843
|
-
|
|
1009
|
+
const context = await this.Ba(scope.signal);
|
|
1010
|
+
began = this.h;
|
|
1011
|
+
this.c({ answering: true, answer: { text: "", tools: [] } });
|
|
1012
|
+
const r = await this.Va(scope.signal, context);
|
|
844
1013
|
return { ok: true, message: r.message, stop: r.stop };
|
|
845
1014
|
} catch (e) {
|
|
846
|
-
return this.
|
|
1015
|
+
return this.G(mine, scope, asChatError(e), began);
|
|
847
1016
|
} finally {
|
|
848
|
-
if (this.
|
|
1017
|
+
if (this.i === mine) this.i = null;
|
|
849
1018
|
}
|
|
850
1019
|
}
|
|
851
1020
|
// begin asks for the start and reads its answer. Only refusals before the
|
|
852
1021
|
// chat starts are tried again; a start never lands twice. With no
|
|
853
1022
|
// conversation shown, after newConversation say, it asks for a new one.
|
|
854
|
-
async
|
|
855
|
-
const sent = { text: "",
|
|
856
|
-
const
|
|
1023
|
+
async Va(signal, context) {
|
|
1024
|
+
const sent = { text: "", files: [], o: null, V: this.E };
|
|
1025
|
+
const body2 = this.a.conversation === null ? { new: true, context } : { context };
|
|
1026
|
+
const call = { method: "POST", path: "/start", body: body2, stream: true };
|
|
857
1027
|
const waitUntil = Date.now() + WAIT_CAP_MS;
|
|
858
1028
|
const busyUntil = Date.now() + BUSY_CAP_MS;
|
|
859
1029
|
for (let busyTries = 0; ; ) {
|
|
@@ -863,25 +1033,25 @@ var Client = class {
|
|
|
863
1033
|
try {
|
|
864
1034
|
let res;
|
|
865
1035
|
try {
|
|
866
|
-
res = await this.
|
|
1036
|
+
res = await this.U(call, stream.signal, null);
|
|
867
1037
|
} catch (e) {
|
|
868
1038
|
if (signal.aborted) throw aborted();
|
|
869
1039
|
const reopen = isChatError(e) ? waitFor(e) : null;
|
|
870
1040
|
if (reopen !== null && Date.now() + reopen <= waitUntil) {
|
|
871
|
-
await this.
|
|
1041
|
+
await this.H(e.code, reopen, signal);
|
|
872
1042
|
continue;
|
|
873
1043
|
}
|
|
874
1044
|
throw e;
|
|
875
1045
|
}
|
|
876
|
-
if (res.ok) return await this.
|
|
1046
|
+
if (res.ok) return await this.Da(res, stream, signal, sent);
|
|
877
1047
|
const err = await failure(res, signal);
|
|
878
1048
|
const wait = waitFor(err);
|
|
879
1049
|
if (wait !== null && Date.now() + wait <= waitUntil) {
|
|
880
|
-
await this.
|
|
1050
|
+
await this.H(err.code, wait, signal);
|
|
881
1051
|
continue;
|
|
882
1052
|
}
|
|
883
1053
|
if (err.code === "busy" && Date.now() < busyUntil) {
|
|
884
|
-
await this.
|
|
1054
|
+
await this.H("busy", BUSY_STEPS_MS[Math.min(busyTries++, BUSY_STEPS_MS.length - 1)], signal);
|
|
885
1055
|
continue;
|
|
886
1056
|
}
|
|
887
1057
|
throw err;
|
|
@@ -893,23 +1063,23 @@ var Client = class {
|
|
|
893
1063
|
// failed shows a failure. A message the page doesn't show as stored stays in
|
|
894
1064
|
// outgoing, so it can be sent again. began is the conversation version this
|
|
895
1065
|
// send set answering in; once the page shows another, answering is not its own.
|
|
896
|
-
|
|
897
|
-
if (this.
|
|
898
|
-
if (began < 0) this.
|
|
899
|
-
else if (began === this.
|
|
900
|
-
else this.
|
|
1066
|
+
G(mine, scope, err, began) {
|
|
1067
|
+
if (this.i === mine && !scope.signal.aborted && err.code !== "aborted") {
|
|
1068
|
+
if (began < 0) this.c({ error: err });
|
|
1069
|
+
else if (began === this.h) this.c({ answer: null, answering: false, waiting: null, error: err });
|
|
1070
|
+
else this.c({ answer: null, waiting: null, error: err });
|
|
901
1071
|
}
|
|
902
1072
|
return { ok: false, error: err };
|
|
903
1073
|
}
|
|
904
1074
|
// deliver sends the message and reads its answer. Only refusals that come
|
|
905
1075
|
// before the chat stores it are tried again: nothing after a 200 is resent.
|
|
906
|
-
async
|
|
1076
|
+
async Ua(text, files, context, signal, recheck) {
|
|
907
1077
|
if (recheck) {
|
|
908
|
-
const id = await this.
|
|
909
|
-
if (id !== null) return this.
|
|
1078
|
+
const id = await this.ea(recheck, signal, null);
|
|
1079
|
+
if (id !== null) return this.fa(id, recheck, signal);
|
|
910
1080
|
}
|
|
911
|
-
const sent = { text,
|
|
912
|
-
const path = sent.
|
|
1081
|
+
const sent = { text, files, o: this.a.conversation ? this.a.conversation.id : null, V: this.E };
|
|
1082
|
+
const path = sent.o === null ? "/conversations" : "/conversations/" + encodeURIComponent(sent.o) + "/messages";
|
|
913
1083
|
const waitUntil = Date.now() + WAIT_CAP_MS;
|
|
914
1084
|
const busyUntil = Date.now() + BUSY_CAP_MS;
|
|
915
1085
|
for (let busyTries = 0; ; ) {
|
|
@@ -919,28 +1089,28 @@ var Client = class {
|
|
|
919
1089
|
try {
|
|
920
1090
|
let res;
|
|
921
1091
|
try {
|
|
922
|
-
res = await this.
|
|
1092
|
+
res = await this.U({ method: "POST", path, body: files.length ? { text, files, context } : { text, context }, stream: true }, stream.signal, sent.o);
|
|
923
1093
|
} catch (e) {
|
|
924
1094
|
if (signal.aborted) throw aborted();
|
|
925
|
-
if (isCode(e, "network")) return await this.
|
|
1095
|
+
if (isCode(e, "network")) return await this.Ea(e, sent, signal);
|
|
926
1096
|
const reopen = isChatError(e) ? waitFor(e) : null;
|
|
927
1097
|
if (reopen !== null && Date.now() + reopen <= waitUntil) {
|
|
928
|
-
await this.
|
|
1098
|
+
await this.H(e.code, reopen, signal);
|
|
929
1099
|
continue;
|
|
930
1100
|
}
|
|
931
1101
|
throw e;
|
|
932
1102
|
}
|
|
933
|
-
if (res.ok) return await this.
|
|
1103
|
+
if (res.ok) return await this.Da(res, stream, signal, sent);
|
|
934
1104
|
const err = await failure(res, signal);
|
|
935
|
-
if (err.code === "internal") return await this.
|
|
936
|
-
if (err.code === "not_found" && sent.
|
|
1105
|
+
if (err.code === "internal") return await this.Ea(err, sent, signal);
|
|
1106
|
+
if (err.code === "not_found" && sent.o !== null) this.sa(sent.o);
|
|
937
1107
|
const wait = waitFor(err);
|
|
938
1108
|
if (wait !== null && Date.now() + wait <= waitUntil) {
|
|
939
|
-
await this.
|
|
1109
|
+
await this.H(err.code, wait, signal);
|
|
940
1110
|
continue;
|
|
941
1111
|
}
|
|
942
1112
|
if (err.code === "busy" && Date.now() < busyUntil) {
|
|
943
|
-
await this.
|
|
1113
|
+
await this.H("busy", BUSY_STEPS_MS[Math.min(busyTries++, BUSY_STEPS_MS.length - 1)], signal);
|
|
944
1114
|
continue;
|
|
945
1115
|
}
|
|
946
1116
|
throw err;
|
|
@@ -949,92 +1119,92 @@ var Client = class {
|
|
|
949
1119
|
}
|
|
950
1120
|
}
|
|
951
1121
|
}
|
|
952
|
-
async
|
|
1122
|
+
async H(code, ms, signal) {
|
|
953
1123
|
live(signal);
|
|
954
|
-
this.
|
|
1124
|
+
this.c({ waiting: { code, until: Date.now() + ms } });
|
|
955
1125
|
try {
|
|
956
1126
|
await sleep(ms, signal);
|
|
957
1127
|
} finally {
|
|
958
|
-
if (!signal.aborted) this.
|
|
1128
|
+
if (!signal.aborted) this.c({ waiting: null });
|
|
959
1129
|
}
|
|
960
1130
|
}
|
|
961
1131
|
// ifLanded settles a request whose outcome is unknown, a lost connection or a
|
|
962
1132
|
// 500: the message may have been stored, so the conversation decides.
|
|
963
|
-
async
|
|
964
|
-
const id = await this.
|
|
1133
|
+
async Ea(err, sent, signal) {
|
|
1134
|
+
const id = await this.ea(sent, signal, err);
|
|
965
1135
|
if (id === null) throw err;
|
|
966
|
-
return this.
|
|
1136
|
+
return this.fa(id, sent, signal);
|
|
967
1137
|
}
|
|
968
1138
|
// landedSoon looks for the message, trying again while the chat can't be
|
|
969
1139
|
// reached. It answers null only when a lookup said the message isn't there;
|
|
970
1140
|
// when none could tell, it throws, and sending the text again checks first.
|
|
971
|
-
async
|
|
972
|
-
const version = this.
|
|
1141
|
+
async ea(sent, signal, cause) {
|
|
1142
|
+
const version = this.h;
|
|
973
1143
|
let last = null;
|
|
974
1144
|
for (const ms of LOOKUP_STEPS_MS) {
|
|
975
1145
|
await sleep(ms, signal);
|
|
976
1146
|
try {
|
|
977
|
-
return await this.
|
|
1147
|
+
return await this.Wa(sent, signal);
|
|
978
1148
|
} catch (e) {
|
|
979
1149
|
if (signal.aborted) throw aborted();
|
|
980
1150
|
last = asChatError(e);
|
|
981
|
-
if (this.
|
|
1151
|
+
if (this.h !== version) throw last;
|
|
982
1152
|
if (last.code === "not_found") return null;
|
|
983
1153
|
if (TRANSIENT.indexOf(last.code) < 0) break;
|
|
984
1154
|
}
|
|
985
1155
|
}
|
|
986
|
-
this.
|
|
1156
|
+
this.v = sent;
|
|
987
1157
|
if (last && TRANSIENT.indexOf(last.code) < 0) throw last;
|
|
988
1158
|
throw cause || last || new ChatError("network", "The chat couldn't be reached.");
|
|
989
1159
|
}
|
|
990
1160
|
// landed finds the conversation that holds the message sent, if the chat
|
|
991
1161
|
// stored it, and shows it. A new conversation holds nothing older than known.
|
|
992
|
-
async
|
|
993
|
-
const mine = (m) => m
|
|
1162
|
+
async Wa(sent, signal) {
|
|
1163
|
+
const mine = (m) => isSent(m, sent);
|
|
994
1164
|
let found = null;
|
|
995
|
-
if (sent.
|
|
996
|
-
const v = await this.
|
|
1165
|
+
if (sent.o !== null) {
|
|
1166
|
+
const v = await this.Fa(sent.o, signal);
|
|
997
1167
|
if (v.messages.some(mine)) found = v;
|
|
998
1168
|
} else {
|
|
999
|
-
const o = await this.
|
|
1000
|
-
if (o.conversation && o.messages.some(mine) && o.messages.every((m) => m.id > sent.
|
|
1169
|
+
const o = await this.Xa(signal);
|
|
1170
|
+
if (o.conversation && o.messages.some(mine) && o.messages.every((m) => m.id > sent.V)) {
|
|
1001
1171
|
found = { conversation: o.conversation, messages: o.messages };
|
|
1002
1172
|
}
|
|
1003
1173
|
}
|
|
1004
1174
|
live(signal);
|
|
1005
1175
|
if (!found) return null;
|
|
1006
|
-
this.
|
|
1007
|
-
this.
|
|
1176
|
+
this.x(found.messages);
|
|
1177
|
+
this.c({ conversation: found.conversation, messages: merge(this.a.messages, found.messages), outgoing: null });
|
|
1008
1178
|
return found.conversation.id;
|
|
1009
1179
|
}
|
|
1010
1180
|
// latest opens the session as it is, for its latest conversation: a live
|
|
1011
1181
|
// cookie needs no token, which may have expired meanwhile. A lapsed session is
|
|
1012
1182
|
// reopened as the page's user, never swapped for an anonymous one.
|
|
1013
|
-
async
|
|
1014
|
-
if (this.
|
|
1015
|
-
const res = await request(this.
|
|
1183
|
+
async Xa(signal) {
|
|
1184
|
+
if (this.g.e === "cookie") {
|
|
1185
|
+
const res = await request(this.l, this.q, { method: "GET", path: "/session", signal });
|
|
1016
1186
|
if (res.ok) {
|
|
1017
1187
|
discard(res);
|
|
1018
|
-
const o = await this.
|
|
1019
|
-
if (!o.
|
|
1188
|
+
const o = await this.y({}, null, signal);
|
|
1189
|
+
if (!o.C) return o;
|
|
1020
1190
|
} else if (res.status === 401) {
|
|
1021
1191
|
discard(res);
|
|
1022
1192
|
} else {
|
|
1023
1193
|
throw await failure(res, signal);
|
|
1024
1194
|
}
|
|
1025
|
-
return this.
|
|
1195
|
+
return this.da(signal);
|
|
1026
1196
|
}
|
|
1027
1197
|
try {
|
|
1028
|
-
return await this.
|
|
1198
|
+
return await this.y(this.g.e === "token" ? { cookieless: true } : {}, this.Aa(), signal);
|
|
1029
1199
|
} catch (e) {
|
|
1030
1200
|
if (!isCode(e, "token_expired") && !isCode(e, "session_expired") && !isCode(e, "unauthorized")) throw e;
|
|
1031
|
-
return this.
|
|
1201
|
+
return this.da(signal);
|
|
1032
1202
|
}
|
|
1033
1203
|
}
|
|
1034
1204
|
// read applies the stream's events. A stream that ends without done or error
|
|
1035
1205
|
// lost the answer, which runs on and is stored, so the conversation is followed.
|
|
1036
|
-
async
|
|
1037
|
-
let conversation = sent.
|
|
1206
|
+
async Da(res, stream, signal, sent) {
|
|
1207
|
+
let conversation = sent.o;
|
|
1038
1208
|
let message = false;
|
|
1039
1209
|
const reader = res.body ? res.body.getReader() : null;
|
|
1040
1210
|
if (reader) {
|
|
@@ -1055,22 +1225,22 @@ var Client = class {
|
|
|
1055
1225
|
if (done) break;
|
|
1056
1226
|
clearTimeout(stall);
|
|
1057
1227
|
stall = setTimeout(() => stream.abort(), STALL_MS);
|
|
1058
|
-
this.
|
|
1228
|
+
this.aa = true;
|
|
1059
1229
|
try {
|
|
1060
1230
|
for (const ev of parser.push(decoder.decode(value, { stream: true }))) {
|
|
1061
1231
|
if (signal.aborted) throw aborted();
|
|
1062
1232
|
if (ev.event === "message" && message) continue;
|
|
1063
1233
|
if (ev.event === "message") message = true;
|
|
1064
|
-
const out = this.
|
|
1234
|
+
const out = this.Ya(ev.event, ev.data);
|
|
1065
1235
|
if (!out) continue;
|
|
1066
1236
|
if ("conversation" in out) conversation = out.conversation;
|
|
1067
1237
|
else return out;
|
|
1068
1238
|
}
|
|
1069
1239
|
} finally {
|
|
1070
|
-
this.
|
|
1071
|
-
if (this.
|
|
1072
|
-
this.
|
|
1073
|
-
this.
|
|
1240
|
+
this.aa = false;
|
|
1241
|
+
if (this.ba) {
|
|
1242
|
+
this.ba = false;
|
|
1243
|
+
this.ca();
|
|
1074
1244
|
}
|
|
1075
1245
|
}
|
|
1076
1246
|
}
|
|
@@ -1085,56 +1255,56 @@ var Client = class {
|
|
|
1085
1255
|
}
|
|
1086
1256
|
}
|
|
1087
1257
|
if (signal.aborted) throw aborted();
|
|
1088
|
-
return this.
|
|
1258
|
+
return this.fa(conversation, { ...sent, o: conversation }, signal);
|
|
1089
1259
|
}
|
|
1090
1260
|
// event applies one event: it answers the reply when done, the conversation
|
|
1091
1261
|
// when one starts, and throws the answer's error.
|
|
1092
|
-
|
|
1262
|
+
Ya(name, data) {
|
|
1093
1263
|
if (EVENTS.indexOf(name) < 0) return null;
|
|
1094
1264
|
let o;
|
|
1095
1265
|
try {
|
|
1096
1266
|
const v = JSON.parse(data);
|
|
1097
1267
|
o = v && typeof v === "object" ? v : {};
|
|
1098
1268
|
} catch {
|
|
1099
|
-
throw new ChatError("protocol", "The
|
|
1269
|
+
throw new ChatError("protocol", "The stream couldn't be read.");
|
|
1100
1270
|
}
|
|
1101
1271
|
switch (name) {
|
|
1102
1272
|
case "conversation": {
|
|
1103
1273
|
const c = conversationOf(o);
|
|
1104
|
-
if (this.
|
|
1105
|
-
else this.
|
|
1274
|
+
if (this.a.conversation && this.a.conversation.id === c.id) this.c({ conversation: c });
|
|
1275
|
+
else this.c({ conversation: c, messages: [] });
|
|
1106
1276
|
return { conversation: c.id };
|
|
1107
1277
|
}
|
|
1108
1278
|
case "message": {
|
|
1109
1279
|
const m = messageOf(o);
|
|
1110
|
-
this.
|
|
1111
|
-
this.
|
|
1280
|
+
this.x([m]);
|
|
1281
|
+
this.c({ messages: withMessage(this.a.messages, m), outgoing: null });
|
|
1112
1282
|
return null;
|
|
1113
1283
|
}
|
|
1114
1284
|
case "delta": {
|
|
1115
|
-
const a = this.
|
|
1285
|
+
const a = this.a.answer || { text: "", tools: [] };
|
|
1116
1286
|
if (typeof o.text !== "string" || o.text === "" || a.text.length >= ANSWER_CAP) return null;
|
|
1117
|
-
this.
|
|
1287
|
+
this.c({ answer: { text: a.text + o.text, tools: a.tools } });
|
|
1118
1288
|
return null;
|
|
1119
1289
|
}
|
|
1120
1290
|
case "tool": {
|
|
1121
|
-
const a = this.
|
|
1291
|
+
const a = this.a.answer || { text: "", tools: [] };
|
|
1122
1292
|
if (typeof o.name !== "string" || typeof o.status !== "string") return null;
|
|
1123
|
-
const tools = withTool(a.tools, o.name, o.status);
|
|
1124
|
-
if (tools !== a.tools) this.
|
|
1293
|
+
const tools = withTool(a.tools, o.name, o.status, typeof o.title === "string" ? o.title : "");
|
|
1294
|
+
if (tools !== a.tools) this.c({ answer: { text: a.text, tools } });
|
|
1125
1295
|
return null;
|
|
1126
1296
|
}
|
|
1127
1297
|
case "done": {
|
|
1128
1298
|
const m = messageOf(o.message);
|
|
1129
1299
|
const stop = typeof o.stop === "string" ? o.stop : null;
|
|
1130
|
-
this.
|
|
1131
|
-
this.
|
|
1300
|
+
this.x([m]);
|
|
1301
|
+
this.c({ messages: withMessage(this.a.messages, m), outgoing: null, answer: null, answering: false, waiting: null, stop });
|
|
1132
1302
|
return { message: m, stop };
|
|
1133
1303
|
}
|
|
1134
1304
|
default: {
|
|
1135
1305
|
const partial = o.partial ? messageOf(o.partial) : null;
|
|
1136
|
-
if (partial) this.
|
|
1137
|
-
this.
|
|
1306
|
+
if (partial) this.x([partial]);
|
|
1307
|
+
this.c({ messages: partial ? withMessage(this.a.messages, partial) : this.a.messages, outgoing: null, answer: null, answering: false });
|
|
1138
1308
|
const code = typeof o.error === "string" && o.error !== "" ? o.error : "unavailable";
|
|
1139
1309
|
throw new ChatError(code, typeof o.message === "string" ? o.message : "The answer failed.", { status: 200, partial });
|
|
1140
1310
|
}
|
|
@@ -1143,43 +1313,43 @@ var Client = class {
|
|
|
1143
1313
|
// follow waits for an answer whose stream was lost: the conversation, found
|
|
1144
1314
|
// through the session when its first event never came, is asked for until
|
|
1145
1315
|
// it's answered. The message is stored, so a failure here leaves it unsure.
|
|
1146
|
-
async
|
|
1147
|
-
const start = sent.text === "";
|
|
1316
|
+
async fa(id, sent, signal) {
|
|
1317
|
+
const start = sent.text === "" && !sent.files.length;
|
|
1148
1318
|
if (id === null && start) throw new ChatError("network", "The answer was lost.");
|
|
1149
|
-
const cid = id !== null ? id : await this.
|
|
1319
|
+
const cid = id !== null ? id : await this.ea(sent, signal, null);
|
|
1150
1320
|
if (cid === null) {
|
|
1151
|
-
this.
|
|
1321
|
+
this.v = sent;
|
|
1152
1322
|
throw new ChatError("no_answer", "The answer was lost.");
|
|
1153
1323
|
}
|
|
1154
|
-
const version = this.
|
|
1324
|
+
const version = this.h;
|
|
1155
1325
|
let v;
|
|
1156
1326
|
try {
|
|
1157
|
-
v = await this.
|
|
1327
|
+
v = await this.Ga(cid, signal);
|
|
1158
1328
|
} catch (e) {
|
|
1159
|
-
if (!start && !signal.aborted && !isCode(e, "not_found") && version === this.
|
|
1329
|
+
if (!start && !signal.aborted && !isCode(e, "not_found") && version === this.h) this.v = { ...sent, o: cid };
|
|
1160
1330
|
throw e;
|
|
1161
1331
|
}
|
|
1162
1332
|
live(signal);
|
|
1163
|
-
if (this.
|
|
1333
|
+
if (this.T(cid)) this.c({ outgoing: null, answer: null, waiting: null });
|
|
1164
1334
|
let reply;
|
|
1165
1335
|
if (start) {
|
|
1166
|
-
reply = v.messages.filter((m) => m.role === "assistant" && m.id > sent.
|
|
1336
|
+
reply = v.messages.filter((m) => m.role === "assistant" && m.id > sent.V).pop();
|
|
1167
1337
|
} else {
|
|
1168
1338
|
let i = v.messages.length - 1;
|
|
1169
|
-
while (i >= 0 && !(v.messages[i]
|
|
1339
|
+
while (i >= 0 && !isSent(v.messages[i], sent)) i--;
|
|
1170
1340
|
reply = i >= 0 ? v.messages.slice(i + 1).find((m) => m.role === "assistant") : void 0;
|
|
1171
1341
|
}
|
|
1172
1342
|
if (!reply) throw new ChatError("no_answer", "The assistant didn't answer.");
|
|
1173
1343
|
return { message: reply, stop: null };
|
|
1174
1344
|
}
|
|
1175
|
-
async
|
|
1176
|
-
const res = await this.
|
|
1345
|
+
async Fa(id, signal) {
|
|
1346
|
+
const res = await this.U({ method: "GET", path: "/conversations/" + encodeURIComponent(id) }, signal, id);
|
|
1177
1347
|
if (!res.ok) throw await failure(res, signal);
|
|
1178
1348
|
return viewOf(await readJSON(res, signal));
|
|
1179
1349
|
}
|
|
1180
1350
|
// poll asks for the conversation every few seconds until no answer is being
|
|
1181
1351
|
// written in it, then shows what was stored.
|
|
1182
|
-
async
|
|
1352
|
+
async Ga(id, signal) {
|
|
1183
1353
|
const started = Date.now();
|
|
1184
1354
|
let failures = 0;
|
|
1185
1355
|
for (; ; ) {
|
|
@@ -1188,42 +1358,42 @@ var Client = class {
|
|
|
1188
1358
|
await sleep(elapsed < 6e4 ? POLL_MS : POLL_SLOW_MS, signal);
|
|
1189
1359
|
let v;
|
|
1190
1360
|
try {
|
|
1191
|
-
v = await this.
|
|
1361
|
+
v = await this.Fa(id, signal);
|
|
1192
1362
|
} catch (e) {
|
|
1193
1363
|
if (signal.aborted) throw aborted();
|
|
1194
1364
|
const err = asChatError(e);
|
|
1195
|
-
if (err.code === "not_found") this.
|
|
1365
|
+
if (err.code === "not_found") this.sa(id);
|
|
1196
1366
|
else if (TRANSIENT.indexOf(err.code) >= 0 && ++failures < POLL_FAILURES) continue;
|
|
1197
1367
|
throw err;
|
|
1198
1368
|
}
|
|
1199
1369
|
failures = 0;
|
|
1200
1370
|
live(signal);
|
|
1201
1371
|
if (v.answering) continue;
|
|
1202
|
-
if (this.
|
|
1203
|
-
this.
|
|
1204
|
-
this.
|
|
1372
|
+
if (this.T(id)) {
|
|
1373
|
+
this.x(v.messages);
|
|
1374
|
+
this.c({ conversation: v.conversation, messages: merge(this.a.messages, v.messages), answering: false });
|
|
1205
1375
|
}
|
|
1206
1376
|
return v;
|
|
1207
1377
|
}
|
|
1208
1378
|
}
|
|
1209
1379
|
// watch follows an answer this page didn't start, such as one a previous
|
|
1210
1380
|
// page left running; a newer watch replaces it.
|
|
1211
|
-
|
|
1212
|
-
if (this.
|
|
1381
|
+
Ra(id) {
|
|
1382
|
+
if (this.r) this.r.abort();
|
|
1213
1383
|
const w = new AbortController();
|
|
1214
|
-
this.
|
|
1215
|
-
const scope = this.
|
|
1384
|
+
this.r = w;
|
|
1385
|
+
const scope = this.K.signal;
|
|
1216
1386
|
const stop = () => w.abort();
|
|
1217
1387
|
scope.addEventListener("abort", stop, { once: true });
|
|
1218
|
-
this.
|
|
1388
|
+
this.Ga(id, w.signal).then(
|
|
1219
1389
|
() => {
|
|
1220
1390
|
},
|
|
1221
1391
|
() => {
|
|
1222
|
-
if (!w.signal.aborted && this.
|
|
1392
|
+
if (!w.signal.aborted && this.T(id)) this.c({ answering: false });
|
|
1223
1393
|
}
|
|
1224
1394
|
).then(() => {
|
|
1225
1395
|
scope.removeEventListener("abort", stop);
|
|
1226
|
-
if (this.
|
|
1396
|
+
if (this.r === w) this.r = null;
|
|
1227
1397
|
});
|
|
1228
1398
|
}
|
|
1229
1399
|
};
|
|
@@ -1238,10 +1408,48 @@ function waitFor(err) {
|
|
|
1238
1408
|
return secs * 1e3;
|
|
1239
1409
|
}
|
|
1240
1410
|
function busy() {
|
|
1241
|
-
return new ChatError("busy", "Still answering
|
|
1411
|
+
return new ChatError("busy", "Still answering.");
|
|
1412
|
+
}
|
|
1413
|
+
function unopened() {
|
|
1414
|
+
return new ChatError("unauthorized", "The chat couldn't be opened.");
|
|
1415
|
+
}
|
|
1416
|
+
function checked(o, keys) {
|
|
1417
|
+
if (o === void 0) return {};
|
|
1418
|
+
if (!o || typeof o !== "object") throw new TypeError("options must be an object");
|
|
1419
|
+
for (const k of Object.keys(o)) if (keys.indexOf(k) < 0) throw new TypeError("unknown option " + k);
|
|
1420
|
+
const s = o.signal;
|
|
1421
|
+
if (s != null && !(s instanceof AbortSignal)) throw new TypeError("signal must be an AbortSignal");
|
|
1422
|
+
return o;
|
|
1423
|
+
}
|
|
1424
|
+
function idOf(f) {
|
|
1425
|
+
const id = typeof f === "string" ? f : f && f.id;
|
|
1426
|
+
if (typeof id !== "string" || id === "") throw new TypeError("a file is a ChatFile or its id");
|
|
1427
|
+
return id;
|
|
1428
|
+
}
|
|
1429
|
+
function vetted(base, page, max) {
|
|
1430
|
+
let s;
|
|
1431
|
+
try {
|
|
1432
|
+
if (page && typeof page === "object" && !Array.isArray(page)) s = JSON.stringify({ ...base, ...page });
|
|
1433
|
+
} catch {
|
|
1434
|
+
}
|
|
1435
|
+
if (!s || s[0] !== "{") return "no JSON object";
|
|
1436
|
+
const o = JSON.parse(s);
|
|
1437
|
+
for (const k in KNOWN) {
|
|
1438
|
+
const f = o[k];
|
|
1439
|
+
if (f != null && (typeof f !== "string" || bytesOf(f) > KNOWN[k])) return k + " must be a string of at most " + KNOWN[k] + " bytes";
|
|
1440
|
+
}
|
|
1441
|
+
if (max !== void 0 && bytesOf(s) > max) return "over the chat's " + max + " bytes";
|
|
1442
|
+
return s === "{}" ? void 0 : o;
|
|
1443
|
+
}
|
|
1444
|
+
function bytesOf(s) {
|
|
1445
|
+
return new TextEncoder().encode(s).length;
|
|
1446
|
+
}
|
|
1447
|
+
function isSent(m, sent) {
|
|
1448
|
+
if (m.role !== "user" || m.id <= sent.V) return false;
|
|
1449
|
+
return sent.files.length ? !!m.files && m.files.some((f) => f.id === sent.files[0]) : m.text === sent.text;
|
|
1242
1450
|
}
|
|
1243
1451
|
function notForThisChat(origin) {
|
|
1244
|
-
return new ChatError("token_failed", "The token
|
|
1452
|
+
return new ChatError("token_failed", "The token's aud isn't " + origin + ".");
|
|
1245
1453
|
}
|
|
1246
1454
|
function live(signal) {
|
|
1247
1455
|
if (signal.aborted) throw aborted();
|
|
@@ -1255,9 +1463,9 @@ function asChatError(e) {
|
|
|
1255
1463
|
}
|
|
1256
1464
|
function whoOf(token) {
|
|
1257
1465
|
const c = claimsOf(token);
|
|
1258
|
-
return c ? c.
|
|
1466
|
+
return c ? c.j : "token";
|
|
1259
1467
|
}
|
|
1260
|
-
function
|
|
1468
|
+
function wellFormed(s) {
|
|
1261
1469
|
let out = "";
|
|
1262
1470
|
let from = 0;
|
|
1263
1471
|
for (let i = 0; i < s.length; i++) {
|
|
@@ -1269,7 +1477,10 @@ function normalize(s) {
|
|
|
1269
1477
|
from = i + 1;
|
|
1270
1478
|
}
|
|
1271
1479
|
}
|
|
1272
|
-
|
|
1480
|
+
return from === 0 ? s : out + s.slice(from);
|
|
1481
|
+
}
|
|
1482
|
+
function normalize(s) {
|
|
1483
|
+
const t = wellFormed(s);
|
|
1273
1484
|
let a = 0;
|
|
1274
1485
|
let b = t.length;
|
|
1275
1486
|
while (a < b && goSpace(t.charCodeAt(a))) a++;
|
|
@@ -1283,60 +1494,87 @@ function merge(old, fresh) {
|
|
|
1283
1494
|
const byId = new Map(old.map((m) => [m.id, m]));
|
|
1284
1495
|
return fresh.map((m) => {
|
|
1285
1496
|
const o = byId.get(m.id);
|
|
1286
|
-
|
|
1497
|
+
const same = o && o.text === m.text && o.role === m.role && o.createdAt === m.createdAt && String(o.tools) === String(m.tools);
|
|
1498
|
+
return same && JSON.stringify(o.files) === JSON.stringify(m.files) ? o : m;
|
|
1287
1499
|
});
|
|
1288
1500
|
}
|
|
1289
1501
|
function withMessage(list, m) {
|
|
1290
1502
|
return list.some((x) => x.id === m.id) ? list.map((x) => x.id === m.id ? m : x) : list.concat(m);
|
|
1291
1503
|
}
|
|
1292
|
-
function withTool(tools, name, status) {
|
|
1504
|
+
function withTool(tools, name, status, title) {
|
|
1293
1505
|
if (status !== "started") {
|
|
1294
1506
|
for (let i = tools.length - 1; i >= 0; i--) {
|
|
1295
1507
|
if (tools[i].name === name && tools[i].status === "started") {
|
|
1296
|
-
return tools.slice(0, i).concat({ name, status }, tools.slice(i + 1));
|
|
1508
|
+
return tools.slice(0, i).concat({ name, status, title: title || tools[i].title }, tools.slice(i + 1));
|
|
1297
1509
|
}
|
|
1298
1510
|
}
|
|
1299
1511
|
}
|
|
1300
|
-
return tools.length >= TOOLS_CAP ? tools : tools.concat({ name, status });
|
|
1512
|
+
return tools.length >= TOOLS_CAP ? tools : tools.concat({ name, status, title: title || name });
|
|
1301
1513
|
}
|
|
1302
1514
|
function bad(what) {
|
|
1303
|
-
return new ChatError("protocol", "
|
|
1515
|
+
return new ChatError("protocol", "Unreadable " + what + " from the chat.");
|
|
1304
1516
|
}
|
|
1305
1517
|
function conversationOf(v) {
|
|
1306
1518
|
var _a, _b;
|
|
1307
1519
|
const o = v;
|
|
1308
|
-
if (!o || typeof o !== "object" || typeof o.id !== "string") throw bad("
|
|
1520
|
+
if (!o || typeof o !== "object" || typeof o.id !== "string") throw bad("conversation");
|
|
1309
1521
|
return { id: o.id, createdAt: String((_a = o.created_at) != null ? _a : ""), updatedAt: String((_b = o.updated_at) != null ? _b : "") };
|
|
1310
1522
|
}
|
|
1311
1523
|
function messageOf(v) {
|
|
1312
1524
|
var _a;
|
|
1313
1525
|
const o = v;
|
|
1314
1526
|
if (!o || typeof o !== "object" || typeof o.id !== "number" || typeof o.text !== "string" || o.role !== "user" && o.role !== "assistant") {
|
|
1315
|
-
throw bad("
|
|
1527
|
+
throw bad("message");
|
|
1316
1528
|
}
|
|
1317
1529
|
const m = { id: o.id, role: o.role, text: o.text, createdAt: String((_a = o.created_at) != null ? _a : "") };
|
|
1318
|
-
if (Array.isArray(o.tools))
|
|
1530
|
+
if (Array.isArray(o.tools)) {
|
|
1531
|
+
const tools = o.tools.filter((t) => typeof t === "string");
|
|
1532
|
+
const said = o.tool_titles && typeof o.tool_titles === "object" ? o.tool_titles : {};
|
|
1533
|
+
m.tools = tools;
|
|
1534
|
+
m.toolTitles = Object.fromEntries(
|
|
1535
|
+
tools.map((t) => {
|
|
1536
|
+
const title = said[t];
|
|
1537
|
+
return [t, typeof title === "string" && title ? title : t];
|
|
1538
|
+
})
|
|
1539
|
+
);
|
|
1540
|
+
}
|
|
1541
|
+
if (Array.isArray(o.files)) m.files = o.files.filter((f) => f && typeof f.id === "string").map(fileOf);
|
|
1319
1542
|
return m;
|
|
1320
1543
|
}
|
|
1544
|
+
function fileOf(v) {
|
|
1545
|
+
var _a, _b, _c;
|
|
1546
|
+
const o = v;
|
|
1547
|
+
if (!o || typeof o !== "object" || typeof o.id !== "string") throw bad("file");
|
|
1548
|
+
const size = typeof o.size === "number" ? o.size : 0;
|
|
1549
|
+
return { id: o.id, name: String((_a = o.name) != null ? _a : ""), type: String((_b = o.type) != null ? _b : ""), size, createdAt: String((_c = o.created_at) != null ? _c : "") };
|
|
1550
|
+
}
|
|
1321
1551
|
function messagesOf(v) {
|
|
1322
1552
|
if (!Array.isArray(v)) return [];
|
|
1323
1553
|
return v.filter((m) => m && typeof m === "object" && (m.role === "user" || m.role === "assistant")).map(messageOf);
|
|
1324
1554
|
}
|
|
1325
1555
|
function openedOf(v) {
|
|
1326
1556
|
const o = v;
|
|
1327
|
-
if (!o || typeof o !== "object") throw bad("
|
|
1557
|
+
if (!o || typeof o !== "object") throw bad("session");
|
|
1328
1558
|
return {
|
|
1329
1559
|
anonymous: o.anonymous === true,
|
|
1330
|
-
|
|
1560
|
+
C: o.started === true,
|
|
1331
1561
|
conversation: o.conversation ? conversationOf(o.conversation) : null,
|
|
1332
1562
|
messages: messagesOf(o.messages),
|
|
1333
1563
|
answering: o.answering === true,
|
|
1334
|
-
|
|
1564
|
+
za: typeof o.session_token === "string" && o.session_token !== "" ? o.session_token : null,
|
|
1565
|
+
files: limitsOf(o.files),
|
|
1566
|
+
w: typeof o.context_max_bytes === "number" ? o.context_max_bytes : void 0
|
|
1335
1567
|
};
|
|
1336
1568
|
}
|
|
1569
|
+
function limitsOf(v) {
|
|
1570
|
+
const o = v;
|
|
1571
|
+
if (o === null) return null;
|
|
1572
|
+
if (!o || typeof o !== "object" || !Array.isArray(o.types) || typeof o.max_bytes !== "number" || typeof o.per_message !== "number") return void 0;
|
|
1573
|
+
return { types: o.types.filter((t) => typeof t === "string"), maxBytes: o.max_bytes, perMessage: o.per_message };
|
|
1574
|
+
}
|
|
1337
1575
|
function viewOf(v) {
|
|
1338
1576
|
const o = v;
|
|
1339
|
-
if (!o || typeof o !== "object") throw bad("
|
|
1577
|
+
if (!o || typeof o !== "object") throw bad("conversation");
|
|
1340
1578
|
return { conversation: conversationOf(o.conversation), messages: messagesOf(o.messages), answering: o.answering === true };
|
|
1341
1579
|
}
|
|
1342
1580
|
export {
|