@celljs/ta-rooms 4.22.0 → 4.23.0
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/extension/index.mjs +201 -2
- package/host/index.mjs +2542 -1296
- package/package.json +4 -4
- package/web/index.js +1015 -71
package/host/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// web-src/double-host.ts
|
|
2
|
-
import { mkdir } from "node:fs/promises";
|
|
3
|
-
import { join } from "node:path";
|
|
2
|
+
import { mkdir as mkdir3 } from "node:fs/promises";
|
|
3
|
+
import { join as join3 } from "node:path";
|
|
4
4
|
|
|
5
5
|
// ../ta-protocol/esm/common/ta-extension-host.js
|
|
6
6
|
var TA_WEBHOOK_MAX_BYTES = 256 * 1024;
|
|
@@ -201,10 +201,19 @@ var TaLanguageUtil;
|
|
|
201
201
|
})(TaLanguageUtil || (TaLanguageUtil = {}));
|
|
202
202
|
|
|
203
203
|
// ../ta-relay/esm/common/relay-room-chat.js
|
|
204
|
+
var RELAY_CHAT_FILE_BYTES = { file: 50 * 1024 * 1024, video: 30 * 1024 * 1024, voice: 2 * 1024 * 1024 };
|
|
205
|
+
var RELAY_CHAT_VOICE_MS = 60000;
|
|
206
|
+
var RELAY_CHAT_VIDEO_MS = 3 * 60000;
|
|
207
|
+
var RELAY_CHAT_FILES = 9;
|
|
204
208
|
var RELAY_CHAT_KEY = "chat:";
|
|
205
209
|
var RELAY_CHAT_PART_BYTES = 8 * 1024;
|
|
206
210
|
var RELAY_CHAT_PARTS = 6;
|
|
207
211
|
var RELAY_CHAT_TEXT_MAX = 2000;
|
|
212
|
+
var RELAY_CHAT_IMAGES = 9;
|
|
213
|
+
var RELAY_CHAT_IMAGE_BYTES = 2 * 1024 * 1024;
|
|
214
|
+
var RELAY_CHAT_THUMB_CHARS = 900;
|
|
215
|
+
var IMAGE_TYPES = ["image/jpeg", "image/png", "image/gif", "image/webp"];
|
|
216
|
+
var RELAY_CHAT_WINDOW = 500;
|
|
208
217
|
var TRIES = 6;
|
|
209
218
|
var RelayRoomChatUtil;
|
|
210
219
|
(function(RelayRoomChatUtil2) {
|
|
@@ -239,18 +248,99 @@ var RelayRoomChatUtil;
|
|
|
239
248
|
...to.length ? { to } : {},
|
|
240
249
|
...line.person === true && to.length && line.double !== true && !line.agent ? { person: true } : {},
|
|
241
250
|
...typeof line.re === "string" ? { re: line.re } : {},
|
|
242
|
-
...line.task === true && to.length ? { task: true } : {}
|
|
251
|
+
...line.task === true && to.length ? { task: true } : {},
|
|
252
|
+
...images(line.images),
|
|
253
|
+
...files(line.files)
|
|
243
254
|
};
|
|
244
255
|
}
|
|
245
256
|
RelayRoomChatUtil2.extra = extra;
|
|
257
|
+
function files(value) {
|
|
258
|
+
if (!Array.isArray(value)) {
|
|
259
|
+
return {};
|
|
260
|
+
}
|
|
261
|
+
const files2 = [];
|
|
262
|
+
for (const file of value.slice(0, RELAY_CHAT_FILES)) {
|
|
263
|
+
const kind = file === null || file === undefined ? undefined : file.kind;
|
|
264
|
+
if (!file || kind !== "file" && kind !== "voice" && kind !== "video" || typeof file.id !== "string" || !/^[a-f0-9]{24}$/.test(file.id) || typeof file.sha !== "string" || !/^[0-9a-f]{64}$/.test(file.sha) || !Number.isInteger(file.bytes) || file.bytes <= 0 || file.bytes > RELAY_CHAT_FILE_BYTES[kind]) {
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
267
|
+
const name = [...typeof file.name === "string" ? file.name : ""].filter((char) => char.charCodeAt(0) >= 32 && char !== "/" && char !== "\\").join("").slice(0, 120) || kind;
|
|
268
|
+
const type = typeof file.type === "string" && /^[\w.+-]+\/[\w.+-]+$/.test(file.type) ? file.type.slice(0, 100) : "application/octet-stream";
|
|
269
|
+
const most = kind === "voice" ? RELAY_CHAT_VOICE_MS : RELAY_CHAT_VIDEO_MS;
|
|
270
|
+
const ms = kind !== "file" && Number.isFinite(file.ms) ? Math.min(Math.max(Math.round(file.ms), 0), most) : undefined;
|
|
271
|
+
const size = kind === "video" && Number.isInteger(file.w) && Number.isInteger(file.h) && file.w > 0 && file.h > 0;
|
|
272
|
+
const thumb = kind === "video" && typeof file.thumb === "string" && file.thumb.length <= RELAY_CHAT_THUMB_CHARS && /^data:image\/(jpeg|png|webp);base64,/.test(file.thumb) ? file.thumb : undefined;
|
|
273
|
+
files2.push({
|
|
274
|
+
id: file.id,
|
|
275
|
+
kind,
|
|
276
|
+
name,
|
|
277
|
+
type,
|
|
278
|
+
bytes: file.bytes,
|
|
279
|
+
sha: file.sha,
|
|
280
|
+
...ms !== undefined ? { ms } : {},
|
|
281
|
+
...size ? { w: file.w, h: file.h } : {},
|
|
282
|
+
...thumb ? { thumb } : {}
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
return files2.length ? { files: files2 } : {};
|
|
286
|
+
}
|
|
287
|
+
RelayRoomChatUtil2.files = files;
|
|
288
|
+
function images(value) {
|
|
289
|
+
if (!Array.isArray(value)) {
|
|
290
|
+
return {};
|
|
291
|
+
}
|
|
292
|
+
const images2 = [];
|
|
293
|
+
for (const image of value.slice(0, RELAY_CHAT_IMAGES)) {
|
|
294
|
+
if (image && typeof image.id === "string" && /^[A-Za-z0-9-]{8,64}$/.test(image.id) && typeof image.type === "string" && IMAGE_TYPES.includes(image.type) && typeof image.sha === "string" && /^[0-9a-f]{64}$/.test(image.sha) && Number.isInteger(image.w) && Number.isInteger(image.h) && Number.isInteger(image.bytes) && image.w > 0 && image.h > 0 && image.bytes > 0 && image.bytes <= RELAY_CHAT_IMAGE_BYTES) {
|
|
295
|
+
const thumb = typeof image.thumb === "string" && image.thumb.length <= RELAY_CHAT_THUMB_CHARS && /^data:image\/(jpeg|png|webp);base64,/.test(image.thumb) ? image.thumb : undefined;
|
|
296
|
+
const blob = image.blob;
|
|
297
|
+
const ok = !!blob && typeof blob.id === "string" && /^[A-Za-z0-9_-]{16,64}$/.test(blob.id) && typeof blob.read === "string" && /^[A-Za-z0-9_-]{43}$/.test(blob.read) && typeof blob.key === "string" && /^[A-Za-z0-9_-]{43}$/.test(blob.key) && Number.isFinite(blob.until);
|
|
298
|
+
images2.push({
|
|
299
|
+
id: image.id,
|
|
300
|
+
type: image.type,
|
|
301
|
+
w: image.w,
|
|
302
|
+
h: image.h,
|
|
303
|
+
bytes: image.bytes,
|
|
304
|
+
sha: image.sha,
|
|
305
|
+
...thumb ? { thumb } : {},
|
|
306
|
+
...ok && blob ? { blob: { id: blob.id, read: blob.read, key: blob.key, until: blob.until } } : {}
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
return images2.length ? { images: images2 } : {};
|
|
311
|
+
}
|
|
312
|
+
function preview(line, picture, words = { file: "[file]", voice: "[voice]", video: "[video]" }) {
|
|
313
|
+
var _a;
|
|
314
|
+
var _b;
|
|
315
|
+
const pictures = ((_a = line.images) === null || _a === undefined ? undefined : _a.length) ? line.images.length > 1 ? `${picture}×${line.images.length}` : picture : "";
|
|
316
|
+
const files2 = ((_b = line.files) !== null && _b !== undefined ? _b : []).map((file) => file.kind === "file" ? `${words.file} ${file.name}` : words[file.kind]);
|
|
317
|
+
return [pictures, ...files2, line.text].filter(Boolean).join(" ");
|
|
318
|
+
}
|
|
319
|
+
RelayRoomChatUtil2.preview = preview;
|
|
246
320
|
function stored(lines2) {
|
|
247
|
-
return lines2.map(({ from: _from, ...line }) => line);
|
|
321
|
+
return lines2.map(({ from: _from, seq: _seq, pending: _pending, failed: _failed, ...line }) => line);
|
|
248
322
|
}
|
|
249
323
|
RelayRoomChatUtil2.stored = stored;
|
|
324
|
+
function signed(member, value, seq) {
|
|
325
|
+
const line = value;
|
|
326
|
+
if (!line || typeof line.id !== "string" || typeof line.text !== "string" || typeof line.at !== "number") {
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
const name = typeof line.name === "string" ? line.name.slice(0, 64) : "";
|
|
330
|
+
const text = line.text.slice(0, RELAY_CHAT_TEXT_MAX);
|
|
331
|
+
return { id: line.id.slice(0, 40), from: member, name, text, at: line.at, ...extra(line), ...seq ? { seq } : {} };
|
|
332
|
+
}
|
|
333
|
+
RelayRoomChatUtil2.signed = signed;
|
|
250
334
|
function bytes(lines2) {
|
|
251
335
|
return new TextEncoder().encode(JSON.stringify(lines2)).length;
|
|
252
336
|
}
|
|
253
337
|
RelayRoomChatUtil2.bytes = bytes;
|
|
338
|
+
function unknownMethod(error) {
|
|
339
|
+
var _a;
|
|
340
|
+
const found = error;
|
|
341
|
+
return (found === null || found === undefined ? undefined : found.code) === -32601 || /method not found|not a function|unknown method|no such method/i.test(String((_a = found === null || found === undefined ? undefined : found.message) !== null && _a !== undefined ? _a : error));
|
|
342
|
+
}
|
|
343
|
+
RelayRoomChatUtil2.unknownMethod = unknownMethod;
|
|
254
344
|
function id() {
|
|
255
345
|
return `${Date.now().toString(36)}${Math.random().toString(36).slice(2, 8)}`;
|
|
256
346
|
}
|
|
@@ -264,6 +354,8 @@ class RelayRoomChat {
|
|
|
264
354
|
this.parts = new Map;
|
|
265
355
|
this.listeners = new Set;
|
|
266
356
|
this.all = [];
|
|
357
|
+
this.olderKept = false;
|
|
358
|
+
this.log = typeof rooms.readChat === "function" && typeof rooms.say === "function";
|
|
267
359
|
}
|
|
268
360
|
get lines() {
|
|
269
361
|
return this.all;
|
|
@@ -272,7 +364,25 @@ class RelayRoomChat {
|
|
|
272
364
|
this.listeners.add(listener);
|
|
273
365
|
return () => this.listeners.delete(listener);
|
|
274
366
|
}
|
|
367
|
+
get more() {
|
|
368
|
+
return this.olderKept;
|
|
369
|
+
}
|
|
275
370
|
async load() {
|
|
371
|
+
if (this.log && this.rooms.readChat) {
|
|
372
|
+
const read = await this.rooms.readChat(this.room, { limit: RELAY_CHAT_WINDOW }).catch((error) => {
|
|
373
|
+
if (!RelayRoomChatUtil.unknownMethod(error)) {
|
|
374
|
+
throw error;
|
|
375
|
+
}
|
|
376
|
+
this.log = false;
|
|
377
|
+
return;
|
|
378
|
+
});
|
|
379
|
+
if (read) {
|
|
380
|
+
this.olderKept = read.more;
|
|
381
|
+
this.all = Array.isArray(read.lines) ? read.lines : [];
|
|
382
|
+
this.tell();
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
276
386
|
const state = await this.rooms.getState(this.room, RELAY_CHAT_KEY);
|
|
277
387
|
this.parts.clear();
|
|
278
388
|
for (const [key, value] of Object.entries(state)) {
|
|
@@ -280,7 +390,51 @@ class RelayRoomChat {
|
|
|
280
390
|
}
|
|
281
391
|
this.changed();
|
|
282
392
|
}
|
|
393
|
+
async older(limit = 100) {
|
|
394
|
+
const first = this.all[0];
|
|
395
|
+
if (!this.log || !this.rooms.readChat || !first || !this.olderKept) {
|
|
396
|
+
return 0;
|
|
397
|
+
}
|
|
398
|
+
const read = await this.rooms.readChat(this.room, { before: first.id, limit });
|
|
399
|
+
const known = new Set(this.all.map((line) => line.id));
|
|
400
|
+
const lines = read.lines.filter((line) => !known.has(line.id));
|
|
401
|
+
this.olderKept = read.more;
|
|
402
|
+
this.all = [...lines, ...this.all];
|
|
403
|
+
this.tell();
|
|
404
|
+
return lines.length;
|
|
405
|
+
}
|
|
406
|
+
hear(event) {
|
|
407
|
+
if (event.room !== undefined && event.room !== this.room) {
|
|
408
|
+
return false;
|
|
409
|
+
}
|
|
410
|
+
if (event.type === "chat" && event.line) {
|
|
411
|
+
this.heardLine(event.line);
|
|
412
|
+
return true;
|
|
413
|
+
}
|
|
414
|
+
return event.type === "state" && typeof event.key === "string" && this.heard(event.key, event);
|
|
415
|
+
}
|
|
416
|
+
heardLine(line) {
|
|
417
|
+
const index = this.all.findIndex((each) => each.id === line.id);
|
|
418
|
+
if (index >= 0) {
|
|
419
|
+
const next = [...this.all];
|
|
420
|
+
next[index] = line;
|
|
421
|
+
this.all = next;
|
|
422
|
+
} else {
|
|
423
|
+
const next = [...this.all, line];
|
|
424
|
+
const pending = next.findIndex((each) => each.pending);
|
|
425
|
+
if (pending >= 0 && !line.pending) {
|
|
426
|
+
next.splice(next.length - 1, 1);
|
|
427
|
+
next.splice(pending, 0, line);
|
|
428
|
+
}
|
|
429
|
+
this.all = next.length > RELAY_CHAT_WINDOW * 2 ? next.slice(-RELAY_CHAT_WINDOW) : next;
|
|
430
|
+
this.olderKept || (this.olderKept = next.length > this.all.length);
|
|
431
|
+
}
|
|
432
|
+
this.tell();
|
|
433
|
+
}
|
|
283
434
|
heard(key, value) {
|
|
435
|
+
if (this.log) {
|
|
436
|
+
return !!RelayRoomChatUtil.parse(key);
|
|
437
|
+
}
|
|
284
438
|
if (!RelayRoomChatUtil.parse(key)) {
|
|
285
439
|
return false;
|
|
286
440
|
}
|
|
@@ -293,16 +447,23 @@ class RelayRoomChat {
|
|
|
293
447
|
return true;
|
|
294
448
|
}
|
|
295
449
|
async say(text, self, extra = {}) {
|
|
296
|
-
var _a;
|
|
297
|
-
var
|
|
450
|
+
var _a, _b, _c;
|
|
451
|
+
var _d, _e;
|
|
298
452
|
const said = text.trim().slice(0, RELAY_CHAT_TEXT_MAX);
|
|
299
|
-
if (!said) {
|
|
453
|
+
if (!said && !((_a = extra.images) === null || _a === undefined ? undefined : _a.length) && !((_b = extra.files) === null || _b === undefined ? undefined : _b.length)) {
|
|
300
454
|
return;
|
|
301
455
|
}
|
|
456
|
+
if (this.log && this.rooms.say) {
|
|
457
|
+
const line = await this.rooms.say(this.room, said, RelayRoomChatUtil.extra(extra));
|
|
458
|
+
if (line) {
|
|
459
|
+
this.heardLine(line);
|
|
460
|
+
}
|
|
461
|
+
return line;
|
|
462
|
+
}
|
|
302
463
|
for (let tries = 0;tries < TRIES; tries++) {
|
|
303
464
|
const latest = this.latest(self.member);
|
|
304
465
|
const last = latest === null || latest === undefined ? undefined : latest.lines[latest.lines.length - 1];
|
|
305
|
-
const at = Math.max(Date.now(), ((
|
|
466
|
+
const at = Math.max(Date.now(), ((_d = last === null || last === undefined ? undefined : last.at) !== null && _d !== undefined ? _d : 0) + 1);
|
|
306
467
|
const line = {
|
|
307
468
|
id: RelayRoomChatUtil.id(),
|
|
308
469
|
from: self.member,
|
|
@@ -316,7 +477,7 @@ class RelayRoomChat {
|
|
|
316
477
|
const lines = fits && latest ? [...latest.lines, line] : [line];
|
|
317
478
|
const key = RelayRoomChatUtil.key(self.member, part);
|
|
318
479
|
const value = RelayRoomChatUtil.stored(lines);
|
|
319
|
-
const result = await this.rooms.setState(this.room, key, value, (
|
|
480
|
+
const result = await this.rooms.setState(this.room, key, value, (_e = (_c = this.parts.get(key)) === null || _c === undefined ? undefined : _c.version) !== null && _e !== undefined ? _e : 0);
|
|
320
481
|
if (result.ok) {
|
|
321
482
|
this.put(key, { value, version: result.version, by: self.member });
|
|
322
483
|
this.changed();
|
|
@@ -357,6 +518,9 @@ class RelayRoomChat {
|
|
|
357
518
|
}
|
|
358
519
|
changed() {
|
|
359
520
|
this.all = [...this.parts.values()].flatMap((part) => part.lines).sort((a, b) => a.at - b.at || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0));
|
|
521
|
+
this.tell();
|
|
522
|
+
}
|
|
523
|
+
tell() {
|
|
360
524
|
for (const listener of this.listeners) {
|
|
361
525
|
listener();
|
|
362
526
|
}
|
|
@@ -514,6 +678,8 @@ var RoomTasksUtil;
|
|
|
514
678
|
var ROOM_CAPABILITIES = ["files", "commands"];
|
|
515
679
|
var DoubleKeys;
|
|
516
680
|
((DoubleKeys) => {
|
|
681
|
+
DoubleKeys.PICTURES = "pictures.came";
|
|
682
|
+
DoubleKeys.FILES = "files.came";
|
|
517
683
|
DoubleKeys.QUIET = "doubles.quiet";
|
|
518
684
|
DoubleKeys.REPLY = "doubles.reply";
|
|
519
685
|
DoubleKeys.DRAFTS = "doubles.drafts";
|
|
@@ -665,6 +831,10 @@ var DoubleUtil;
|
|
|
665
831
|
`);
|
|
666
832
|
}
|
|
667
833
|
DoubleUtil.role = role;
|
|
834
|
+
function taskRole(me, title, approved) {
|
|
835
|
+
return approved ? role(me, title) : TaConfineUtil.confine(role(me, title));
|
|
836
|
+
}
|
|
837
|
+
DoubleUtil.taskRole = taskRole;
|
|
668
838
|
function agentRole(me, title, agent) {
|
|
669
839
|
const lines = [
|
|
670
840
|
`${ROOM_DOUBLE_MARK} ${TA_WITH_OTHERS_MARK}`,
|
|
@@ -695,13 +865,14 @@ var DoubleUtil;
|
|
|
695
865
|
function prompt(input) {
|
|
696
866
|
const index = input.lines.findIndex((line) => line.id === input.line.id);
|
|
697
867
|
const before = input.lines.slice(Math.max(0, index - CONTEXT_LINES), Math.max(0, index));
|
|
698
|
-
const said = (line) => `${DoubleUtil.quote(input.nameOf(line))}${line.double ? " (double)" : ""}: ${DoubleUtil.quote(line
|
|
868
|
+
const said = (line) => `${DoubleUtil.quote(input.nameOf(line))}${line.double ? " (double)" : ""}: ${DoubleUtil.quote(RelayRoomChatUtil.preview(line, "[picture]"))}`;
|
|
699
869
|
return [
|
|
700
870
|
input.permits,
|
|
701
871
|
`<room_request room="${DoubleUtil.quote(input.title)}" from="${DoubleUtil.quote(input.asker.name)}"${input.asker.guest ? ' guest="true"' : ""}>`,
|
|
702
872
|
...before.length ? ["The chat before it:", ...before.map(said), ""] : [],
|
|
703
873
|
`${DoubleUtil.quote(input.asker.name)} asks you:`,
|
|
704
|
-
DoubleUtil.quote(input.line
|
|
874
|
+
DoubleUtil.quote(RelayRoomChatUtil.preview(input.line, "[picture]")),
|
|
875
|
+
...input.line.images?.length ? ["(Its pictures are attached, as far as they came.)"] : [],
|
|
705
876
|
"</room_request>"
|
|
706
877
|
].join(`
|
|
707
878
|
`);
|
|
@@ -1595,7 +1766,14 @@ class RoomReach {
|
|
|
1595
1766
|
return;
|
|
1596
1767
|
}
|
|
1597
1768
|
this.told.add(line.id);
|
|
1598
|
-
const
|
|
1769
|
+
const picture = TaLanguageUtil.pick(language(), { "zh-CN": "[图片]", "zh-TW": "[圖片]", "en-US": "[Picture]", "ja-JP": "[画像]" });
|
|
1770
|
+
const words = TaLanguageUtil.pick(language(), {
|
|
1771
|
+
"zh-CN": { file: "[文件]", voice: "[语音]", video: "[视频]" },
|
|
1772
|
+
"zh-TW": { file: "[檔案]", voice: "[語音]", video: "[影片]" },
|
|
1773
|
+
"en-US": { file: "[File]", voice: "[Voice]", video: "[Video]" },
|
|
1774
|
+
"ja-JP": { file: "[ファイル]", voice: "[ボイス]", video: "[動画]" }
|
|
1775
|
+
});
|
|
1776
|
+
const text = DoubleUtil.cut(RelayRoomChatUtil.preview(line, picture, words), 80);
|
|
1599
1777
|
this.doubles.grants.tell(`call:${room.id}:${line.id}`, direct ? name : room.title, direct ? text : TaLanguageUtil.pick(language(), {
|
|
1600
1778
|
"zh-CN": `${name} @ 了你:${text}`,
|
|
1601
1779
|
"zh-TW": `${name} @ 了你:${text}`,
|
|
@@ -1760,7 +1938,11 @@ class RoomTasks extends RoomTasksBase {
|
|
|
1760
1938
|
this.enqueue(record);
|
|
1761
1939
|
}
|
|
1762
1940
|
async approve(id, accept) {
|
|
1763
|
-
const [task] = accept ? await this.mark((each) => each.id === id && !!each.approval && each.status === "permission", {
|
|
1941
|
+
const [task] = accept ? await this.mark((each) => each.id === id && !!each.approval && each.status === "permission", {
|
|
1942
|
+
status: "queued",
|
|
1943
|
+
approval: undefined,
|
|
1944
|
+
approved: true
|
|
1945
|
+
}) : await this.mark((each) => each.id === id && !!each.approval && each.status === "permission", {
|
|
1764
1946
|
status: "declined",
|
|
1765
1947
|
approval: undefined,
|
|
1766
1948
|
detail: TaLanguageUtil.pick(language(), { "zh-CN": "这次不接", "zh-TW": "這次不接", "en-US": "not this time", "ja-JP": "今回は受けません" }),
|
|
@@ -1916,7 +2098,7 @@ class RoomTasks extends RoomTasksBase {
|
|
|
1916
2098
|
"en-US": `For ${task.name}: ${DoubleUtil.cut(task.text, 30)}`,
|
|
1917
2099
|
"ja-JP": `${task.name}の代わりに:${DoubleUtil.cut(task.text, 30)}`
|
|
1918
2100
|
});
|
|
1919
|
-
const { sessionId, sessionFile } = await this.doubles.session(`task:${room.id}:${id}:${cwd}`, cwd,
|
|
2101
|
+
const { sessionId, sessionFile } = await this.doubles.session(`task:${room.id}:${id}:${cwd}`, cwd, DoubleUtil.taskRole(me, room.title, !!task.approved), name);
|
|
1920
2102
|
this.doubles.track(sessionFile, turn);
|
|
1921
2103
|
this.running.set(id, { sessionId, turn });
|
|
1922
2104
|
const before = await this.doubles.host.getState(sessionId);
|
|
@@ -1926,7 +2108,12 @@ class RoomTasks extends RoomTasksBase {
|
|
|
1926
2108
|
const permits = [DoubleUtil.permits(me, task.name, work), ...again ? [DoubleUtil.taskAgain(me)] : [], DoubleUtil.taskNote(me, task.name)].join(`
|
|
1927
2109
|
`);
|
|
1928
2110
|
const message = DoubleUtil.prompt({ title: room.title, asker, line, lines: chat.lines, nameOf, permits });
|
|
1929
|
-
await this.doubles.
|
|
2111
|
+
const images = line.images?.length ? await this.doubles.media.pictures.images(room.id, line) : [];
|
|
2112
|
+
await this.doubles.host.prompt(sessionId, {
|
|
2113
|
+
clientMessageId: `task:${room.id}:${id}:${again ? now : 0}`,
|
|
2114
|
+
message,
|
|
2115
|
+
...images.length ? { images } : {}
|
|
2116
|
+
});
|
|
1930
2117
|
await this.doubles.settled(sessionId, before.messageCount, TASK_MS);
|
|
1931
2118
|
if (!turn.active) {
|
|
1932
2119
|
return;
|
|
@@ -2105,1472 +2292,2527 @@ class RoomUserTools {
|
|
|
2105
2292
|
}
|
|
2106
2293
|
}
|
|
2107
2294
|
|
|
2108
|
-
// web-src/
|
|
2109
|
-
|
|
2110
|
-
|
|
2295
|
+
// web-src/files-host.ts
|
|
2296
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
2297
|
+
import { createReadStream } from "node:fs";
|
|
2298
|
+
import { appendFile, mkdir, open, readdir, readFile, rename, rm, stat as stat2, utimes, writeFile } from "node:fs/promises";
|
|
2299
|
+
import { join } from "node:path";
|
|
2300
|
+
var DAY_MS = 24 * 60 * 60 * 1000;
|
|
2301
|
+
var DEFAULTS = { now: () => Date.now(), paceMs: 60, keptBytes: 2 * 1024 * 1024 * 1024, unseenMs: 30 * DAY_MS, stallMs: 1e4 };
|
|
2302
|
+
var FILE_PART_BYTES = 36 * 1024;
|
|
2303
|
+
var FILE_CALL_BYTES = 384 * 1024;
|
|
2304
|
+
var UPLOAD_MS = 10 * 60000;
|
|
2305
|
+
var ASKS_A_MINUTE = 20;
|
|
2306
|
+
var LOOK_BACK_PAGES = 10;
|
|
2307
|
+
var ROOM_ID = /^[A-Za-z0-9_-]{1,80}$/;
|
|
2308
|
+
var FILE_ID = /^[a-f0-9]{24}$/;
|
|
2309
|
+
var WANT = "ta-rooms:file?";
|
|
2310
|
+
var PART = "ta-rooms:file";
|
|
2311
|
+
var SENT = ".sent.json";
|
|
2312
|
+
var RoomFilesUtil;
|
|
2313
|
+
((RoomFilesUtil) => {
|
|
2314
|
+
function parts(bytes) {
|
|
2315
|
+
return Math.max(1, Math.ceil(bytes / FILE_PART_BYTES));
|
|
2316
|
+
}
|
|
2317
|
+
RoomFilesUtil.parts = parts;
|
|
2318
|
+
function sha(file) {
|
|
2319
|
+
return new Promise((resolve, reject) => {
|
|
2320
|
+
const hash = createHash("sha256");
|
|
2321
|
+
createReadStream(file).on("data", (chunk) => hash.update(chunk)).on("error", reject).on("end", () => resolve(hash.digest("hex")));
|
|
2322
|
+
});
|
|
2323
|
+
}
|
|
2324
|
+
RoomFilesUtil.sha = sha;
|
|
2325
|
+
function find(lines, id) {
|
|
2326
|
+
for (const line of lines) {
|
|
2327
|
+
const file = line.files?.find((each) => each.id === id);
|
|
2328
|
+
if (file) {
|
|
2329
|
+
return { line, file };
|
|
2330
|
+
}
|
|
2331
|
+
}
|
|
2332
|
+
return;
|
|
2333
|
+
}
|
|
2334
|
+
RoomFilesUtil.find = find;
|
|
2335
|
+
})(RoomFilesUtil ||= {});
|
|
2111
2336
|
|
|
2112
|
-
class
|
|
2113
|
-
|
|
2337
|
+
class RoomFiles {
|
|
2338
|
+
doubles;
|
|
2114
2339
|
options;
|
|
2115
|
-
|
|
2116
|
-
|
|
2117
|
-
|
|
2118
|
-
|
|
2119
|
-
|
|
2120
|
-
|
|
2121
|
-
|
|
2122
|
-
|
|
2123
|
-
|
|
2124
|
-
opening = new Map;
|
|
2125
|
-
stopEvents;
|
|
2126
|
-
disposed = false;
|
|
2127
|
-
constructor(host, options = {}) {
|
|
2128
|
-
this.host = host;
|
|
2340
|
+
uploads = new Map;
|
|
2341
|
+
coming = new Map;
|
|
2342
|
+
asks = new Map;
|
|
2343
|
+
serving = new Map;
|
|
2344
|
+
sending = Promise.resolve();
|
|
2345
|
+
sentChain = Promise.resolve();
|
|
2346
|
+
timer;
|
|
2347
|
+
constructor(doubles, options = {}) {
|
|
2348
|
+
this.doubles = doubles;
|
|
2129
2349
|
this.options = { ...DEFAULTS, ...options };
|
|
2130
2350
|
}
|
|
2131
|
-
|
|
2132
|
-
|
|
2133
|
-
|
|
2134
|
-
|
|
2135
|
-
|
|
2136
|
-
this.check(room.id);
|
|
2137
|
-
}
|
|
2138
|
-
}
|
|
2139
|
-
}).catch((error) => console.error("ta-rooms could not read the rooms for the doubles:", error));
|
|
2140
|
-
this.tasks.start().catch((error) => console.error("ta-rooms could not read the tasks of the doubles:", error));
|
|
2351
|
+
start() {
|
|
2352
|
+
const sweep = () => void this.sweep().catch((error) => console.error("ta-rooms could not tidy the files:", error));
|
|
2353
|
+
setTimeout(sweep, 45000).unref?.();
|
|
2354
|
+
this.timer = setInterval(sweep, 60 * 60000);
|
|
2355
|
+
this.timer.unref?.();
|
|
2141
2356
|
}
|
|
2142
2357
|
dispose() {
|
|
2143
|
-
this.
|
|
2144
|
-
this.
|
|
2145
|
-
|
|
2146
|
-
|
|
2147
|
-
this.
|
|
2148
|
-
}
|
|
2149
|
-
|
|
2150
|
-
return
|
|
2151
|
-
}
|
|
2152
|
-
|
|
2153
|
-
|
|
2154
|
-
|
|
2358
|
+
clearInterval(this.timer);
|
|
2359
|
+
this.serving.clear();
|
|
2360
|
+
}
|
|
2361
|
+
root() {
|
|
2362
|
+
return join(this.doubles.host.agentDir, "ta-rooms", "files");
|
|
2363
|
+
}
|
|
2364
|
+
file(room, id) {
|
|
2365
|
+
return ROOM_ID.test(room) && FILE_ID.test(id) ? join(this.root(), room, id) : undefined;
|
|
2366
|
+
}
|
|
2367
|
+
request(request) {
|
|
2368
|
+
switch (request.kind) {
|
|
2369
|
+
case "fileBegin":
|
|
2370
|
+
return this.begin(String(request.room), request.fileKind, String(request.type), Number(request.bytes));
|
|
2371
|
+
case "fileAppend":
|
|
2372
|
+
return this.append(String(request.upload), Number(request.offset), String(request.data));
|
|
2373
|
+
case "fileFinish":
|
|
2374
|
+
return this.finish(String(request.upload), String(request.name), {
|
|
2375
|
+
...typeof request.ms === "number" ? { ms: request.ms } : {},
|
|
2376
|
+
...typeof request.w === "number" && typeof request.h === "number" ? { w: request.w, h: request.h } : {},
|
|
2377
|
+
...typeof request.thumb === "string" ? { thumb: request.thumb } : {}
|
|
2378
|
+
});
|
|
2379
|
+
case "fileRead":
|
|
2380
|
+
return this.read(String(request.room), String(request.id), Number(request.offset ?? 0));
|
|
2155
2381
|
}
|
|
2156
2382
|
}
|
|
2157
|
-
|
|
2158
|
-
|
|
2159
|
-
|
|
2160
|
-
|
|
2161
|
-
|
|
2162
|
-
|
|
2163
|
-
|
|
2383
|
+
async begin(room, kind, type, bytes) {
|
|
2384
|
+
await this.member(room);
|
|
2385
|
+
if (!(kind in RELAY_CHAT_FILE_BYTES) || !Number.isInteger(bytes) || bytes <= 0 || bytes > RELAY_CHAT_FILE_BYTES[kind]) {
|
|
2386
|
+
throw new Error("This file is too large to send.");
|
|
2387
|
+
}
|
|
2388
|
+
const now = this.options.now();
|
|
2389
|
+
for (const [id2, upload] of this.uploads) {
|
|
2390
|
+
if (now - upload.at > UPLOAD_MS) {
|
|
2391
|
+
this.uploads.delete(id2);
|
|
2392
|
+
rm(upload.file, { force: true });
|
|
2393
|
+
}
|
|
2394
|
+
}
|
|
2395
|
+
const id = randomBytes(12).toString("hex");
|
|
2396
|
+
await mkdir(join(this.root(), room), { recursive: true });
|
|
2397
|
+
const file = join(this.root(), room, `.up-${id}`);
|
|
2398
|
+
await writeFile(file, new Uint8Array);
|
|
2399
|
+
this.uploads.set(id, { room, kind, type: String(type), bytes, file, got: 0, at: now });
|
|
2400
|
+
return id;
|
|
2401
|
+
}
|
|
2402
|
+
async append(upload, offset, data) {
|
|
2403
|
+
const found = this.uploads.get(String(upload));
|
|
2404
|
+
const bytes = Buffer.from(String(data), "base64");
|
|
2405
|
+
if (!found || offset !== found.got || bytes.length === 0 || bytes.length > FILE_CALL_BYTES || found.got + bytes.length > found.bytes) {
|
|
2406
|
+
throw new Error("The file did not come as it should: send it again.");
|
|
2407
|
+
}
|
|
2408
|
+
await appendFile(found.file, bytes);
|
|
2409
|
+
found.got += bytes.length;
|
|
2410
|
+
found.at = this.options.now();
|
|
2411
|
+
return found.got;
|
|
2412
|
+
}
|
|
2413
|
+
async finish(upload, name, more = {}) {
|
|
2414
|
+
const found = this.uploads.get(String(upload));
|
|
2415
|
+
if (!found || found.got !== found.bytes) {
|
|
2416
|
+
throw new Error("The file did not all come: send it again.");
|
|
2417
|
+
}
|
|
2418
|
+
this.uploads.delete(String(upload));
|
|
2419
|
+
const id = randomBytes(12).toString("hex");
|
|
2420
|
+
const sha = await RoomFilesUtil.sha(found.file);
|
|
2421
|
+
await rename(found.file, this.file(found.room, id));
|
|
2422
|
+
await this.changeSent(found.room, (sent) => {
|
|
2423
|
+
sent[id] = { at: this.options.now() };
|
|
2424
|
+
});
|
|
2425
|
+
const line = RelayRoomChatUtil.files([{ id, kind: found.kind, name: String(name), type: found.type, bytes: found.bytes, sha, ...more }]);
|
|
2426
|
+
const file = line.files?.[0];
|
|
2427
|
+
if (!file) {
|
|
2428
|
+
throw new Error("Not a file Ta sends.");
|
|
2429
|
+
}
|
|
2430
|
+
return file;
|
|
2164
2431
|
}
|
|
2165
|
-
|
|
2166
|
-
const
|
|
2167
|
-
|
|
2168
|
-
|
|
2432
|
+
async read(room, id, offset = 0) {
|
|
2433
|
+
const found = await this.lookup(room, String(id));
|
|
2434
|
+
const path = found && this.file(room, found.file.id);
|
|
2435
|
+
if (!found || !path) {
|
|
2436
|
+
return { status: "none" };
|
|
2437
|
+
}
|
|
2438
|
+
const size = (await stat2(path).catch(() => {
|
|
2439
|
+
return;
|
|
2440
|
+
}))?.size;
|
|
2441
|
+
if (size === found.file.bytes) {
|
|
2442
|
+
const start = Math.max(0, Math.floor(Number(offset) || 0));
|
|
2443
|
+
const handle = await open(path, "r");
|
|
2444
|
+
try {
|
|
2445
|
+
const buffer = Buffer.alloc(Math.min(FILE_CALL_BYTES, Math.max(0, size - start)));
|
|
2446
|
+
await handle.read(buffer, 0, buffer.length, start);
|
|
2447
|
+
if (start === 0) {
|
|
2448
|
+
const now = new Date(this.options.now());
|
|
2449
|
+
await utimes(path, now, now).catch(() => {
|
|
2450
|
+
return;
|
|
2451
|
+
});
|
|
2452
|
+
}
|
|
2453
|
+
return { status: "here", bytes: size, offset: start, data: buffer.toString("base64") };
|
|
2454
|
+
} finally {
|
|
2455
|
+
await handle.close();
|
|
2456
|
+
}
|
|
2169
2457
|
}
|
|
2458
|
+
const asked = await this.want(room, found.line, found.file);
|
|
2459
|
+
const coming = this.coming.get(`${room}:${found.file.id}`);
|
|
2460
|
+
return asked && coming ? { status: "coming", bytes: found.file.bytes, got: Math.min(coming.got.size * FILE_PART_BYTES, found.file.bytes) } : { status: "waiting", bytes: found.file.bytes };
|
|
2170
2461
|
}
|
|
2171
|
-
|
|
2172
|
-
const
|
|
2173
|
-
|
|
2174
|
-
|
|
2175
|
-
|
|
2176
|
-
|
|
2462
|
+
heard(event) {
|
|
2463
|
+
const data = event.data;
|
|
2464
|
+
if (data?.t !== WANT && data?.t !== PART) {
|
|
2465
|
+
return false;
|
|
2466
|
+
}
|
|
2467
|
+
const done = data.t === WANT ? this.serve(event) : this.part(event);
|
|
2468
|
+
done.catch((error) => console.error("ta-rooms: a file could not be passed on:", error));
|
|
2469
|
+
return true;
|
|
2177
2470
|
}
|
|
2178
|
-
async
|
|
2179
|
-
const
|
|
2180
|
-
if (!
|
|
2181
|
-
throw new Error("
|
|
2471
|
+
async member(room) {
|
|
2472
|
+
const info = (await this.doubles.host.rooms.listRooms()).find((each) => each.id === room && each.status === "member");
|
|
2473
|
+
if (!info || !ROOM_ID.test(room)) {
|
|
2474
|
+
throw new Error("Not in this group chat.");
|
|
2182
2475
|
}
|
|
2183
|
-
|
|
2184
|
-
return `Set ${key}.`;
|
|
2476
|
+
return info;
|
|
2185
2477
|
}
|
|
2186
|
-
async
|
|
2187
|
-
|
|
2188
|
-
|
|
2189
|
-
|
|
2190
|
-
|
|
2191
|
-
|
|
2192
|
-
|
|
2193
|
-
|
|
2194
|
-
|
|
2195
|
-
|
|
2196
|
-
case "revoke": {
|
|
2197
|
-
const grants = await this.get(DoubleKeys.GRANTS) ?? [];
|
|
2198
|
-
await this.set(DoubleKeys.GRANTS, grants.filter((grant) => grant.id !== request.grant));
|
|
2478
|
+
async lookup(room, id) {
|
|
2479
|
+
if (!FILE_ID.test(id)) {
|
|
2480
|
+
return;
|
|
2481
|
+
}
|
|
2482
|
+
const lines = (await this.doubles.chatOf(room)).lines;
|
|
2483
|
+
const found = RoomFilesUtil.find(lines, id);
|
|
2484
|
+
const read = this.doubles.host.rooms.readChat;
|
|
2485
|
+
let before = lines[0]?.id;
|
|
2486
|
+
for (let page = 0;!found && read && page < LOOK_BACK_PAGES && before; page++) {
|
|
2487
|
+
const older = await read.call(this.doubles.host.rooms, room, { before, limit: 500 }).catch(() => {
|
|
2199
2488
|
return;
|
|
2489
|
+
});
|
|
2490
|
+
const hit = older && RoomFilesUtil.find(older.lines, id);
|
|
2491
|
+
if (hit || !older?.more) {
|
|
2492
|
+
return hit;
|
|
2200
2493
|
}
|
|
2201
|
-
|
|
2202
|
-
return this.grants.revokeAll(String(request.friend));
|
|
2203
|
-
case "clearLog":
|
|
2204
|
-
await this.set(DoubleKeys.LOG, []);
|
|
2205
|
-
return;
|
|
2206
|
-
case "agents":
|
|
2207
|
-
return (await this.agents.teamAgents()).map(({ id, name, role, face }) => ({ id, name, role, ...typeof face === "number" ? { face } : {} }));
|
|
2208
|
-
case "invite":
|
|
2209
|
-
return this.agents.invite(String(request.room), String(request.agent));
|
|
2210
|
-
case "dismiss":
|
|
2211
|
-
return this.agents.dismiss(String(request.room), String(request.agent));
|
|
2212
|
-
case "stopTask":
|
|
2213
|
-
return request.handed ? this.tasks.cancel(String(request.task)) : this.tasks.stop(String(request.task));
|
|
2214
|
-
case "approveTask":
|
|
2215
|
-
return this.tasks.approve(String(request.task), !!request.accept);
|
|
2216
|
-
case "forgetTask":
|
|
2217
|
-
return this.tasks.forget(String(request.task), !!request.handed);
|
|
2218
|
-
default:
|
|
2219
|
-
throw new Error("An unknown request.");
|
|
2494
|
+
before = older.lines[0]?.id;
|
|
2220
2495
|
}
|
|
2496
|
+
return found;
|
|
2221
2497
|
}
|
|
2222
|
-
|
|
2223
|
-
|
|
2224
|
-
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
|
|
2228
|
-
"zh-CN": `${request.name}想加入群聊`,
|
|
2229
|
-
"zh-TW": `${request.name}想加入群聊`,
|
|
2230
|
-
"en-US": `${request.name} asks to join`,
|
|
2231
|
-
"ja-JP": `${request.name}はグループチャットへの参加をリクエストしています`
|
|
2232
|
-
}));
|
|
2233
|
-
}
|
|
2234
|
-
}).catch(() => {
|
|
2235
|
-
return;
|
|
2236
|
-
});
|
|
2237
|
-
return;
|
|
2498
|
+
async want(room, line, file) {
|
|
2499
|
+
const key = `${room}:${file.id}`;
|
|
2500
|
+
const now = this.options.now();
|
|
2501
|
+
const before = this.coming.get(key);
|
|
2502
|
+
if (before && now - before.at < this.options.stallMs) {
|
|
2503
|
+
return true;
|
|
2238
2504
|
}
|
|
2239
|
-
|
|
2505
|
+
const info = await this.member(room).catch(() => {
|
|
2240
2506
|
return;
|
|
2507
|
+
});
|
|
2508
|
+
const online = info?.members.filter((member) => member.online && !member.self && !member.guest) ?? [];
|
|
2509
|
+
const sender = online.find((member) => member.member === line.from);
|
|
2510
|
+
const ordered = sender ? [sender, ...online.filter((member) => member !== sender)] : online;
|
|
2511
|
+
if (!ordered.length) {
|
|
2512
|
+
return false;
|
|
2241
2513
|
}
|
|
2242
|
-
|
|
2243
|
-
|
|
2514
|
+
const tries = before ? before.tries + 1 : 0;
|
|
2515
|
+
const asked = ordered[tries % ordered.length];
|
|
2516
|
+
const parts = RoomFilesUtil.parts(file.bytes);
|
|
2517
|
+
const coming = before ?? { from: asked.member, tries, parts, got: new Set, at: now, writing: Promise.resolve() };
|
|
2518
|
+
if (!before) {
|
|
2519
|
+
await mkdir(join(this.root(), room), { recursive: true });
|
|
2520
|
+
await writeFile(`${this.file(room, file.id)}.part`, new Uint8Array);
|
|
2521
|
+
}
|
|
2522
|
+
coming.from = asked.member;
|
|
2523
|
+
coming.tries = tries;
|
|
2524
|
+
coming.at = now;
|
|
2525
|
+
this.coming.set(key, coming);
|
|
2526
|
+
let from = 0;
|
|
2527
|
+
while (coming.got.has(from)) {
|
|
2528
|
+
from++;
|
|
2529
|
+
}
|
|
2530
|
+
await this.doubles.host.rooms.send(room, { t: WANT, id: file.id, from }, asked.member);
|
|
2531
|
+
return true;
|
|
2532
|
+
}
|
|
2533
|
+
async serve(event) {
|
|
2534
|
+
const { id, from } = event.data;
|
|
2535
|
+
const info = await this.member(event.room).catch(() => {
|
|
2536
|
+
return;
|
|
2537
|
+
});
|
|
2538
|
+
const asker = info?.members.find((member) => member.member === event.from);
|
|
2539
|
+
if (!asker || asker.self || asker.guest || typeof id !== "string" || !FILE_ID.test(id)) {
|
|
2244
2540
|
return;
|
|
2245
2541
|
}
|
|
2246
|
-
|
|
2542
|
+
const now = this.options.now();
|
|
2543
|
+
const counted = `${event.room}:${event.from}`;
|
|
2544
|
+
const recent = (this.asks.get(counted) ?? []).filter((at) => now - at < 60000);
|
|
2545
|
+
if (recent.length >= ASKS_A_MINUTE) {
|
|
2247
2546
|
return;
|
|
2248
2547
|
}
|
|
2249
|
-
|
|
2250
|
-
|
|
2251
|
-
|
|
2252
|
-
|
|
2253
|
-
|
|
2254
|
-
|
|
2255
|
-
|
|
2256
|
-
});
|
|
2257
|
-
} else {
|
|
2258
|
-
this.check(event.room);
|
|
2548
|
+
this.asks.set(counted, [...recent, now]);
|
|
2549
|
+
const found = await this.lookup(event.room, id);
|
|
2550
|
+
const path = found && this.file(event.room, id);
|
|
2551
|
+
if (!found || !path || (await stat2(path).catch(() => {
|
|
2552
|
+
return;
|
|
2553
|
+
}))?.size !== found.file.bytes) {
|
|
2554
|
+
return;
|
|
2259
2555
|
}
|
|
2556
|
+
const parts = RoomFilesUtil.parts(found.file.bytes);
|
|
2557
|
+
const start = Math.min(Math.max(Math.floor(Number(from) || 0), 0), parts - 1);
|
|
2558
|
+
const token = Symbol(id);
|
|
2559
|
+
const serving = `${counted}:${id}`;
|
|
2560
|
+
this.serving.set(serving, token);
|
|
2561
|
+
this.sending = this.sending.then(async () => {
|
|
2562
|
+
const handle = await open(path, "r");
|
|
2563
|
+
try {
|
|
2564
|
+
for (let part = start;part < parts && this.serving.get(serving) === token; part++) {
|
|
2565
|
+
const buffer = Buffer.alloc(Math.min(FILE_PART_BYTES, found.file.bytes - part * FILE_PART_BYTES));
|
|
2566
|
+
await handle.read(buffer, 0, buffer.length, part * FILE_PART_BYTES);
|
|
2567
|
+
await this.doubles.host.rooms.send(event.room, { t: PART, id, part, parts, d: buffer.toString("base64") }, event.from);
|
|
2568
|
+
if (this.options.paceMs) {
|
|
2569
|
+
await new Promise((resolve) => setTimeout(resolve, this.options.paceMs));
|
|
2570
|
+
}
|
|
2571
|
+
}
|
|
2572
|
+
} finally {
|
|
2573
|
+
await handle.close();
|
|
2574
|
+
if (this.serving.get(serving) === token) {
|
|
2575
|
+
this.serving.delete(serving);
|
|
2576
|
+
}
|
|
2577
|
+
}
|
|
2578
|
+
}).catch((error) => console.error("ta-rooms could not send a file:", error));
|
|
2260
2579
|
}
|
|
2261
|
-
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
|
|
2266
|
-
|
|
2267
|
-
this.chats.set(room, chat);
|
|
2580
|
+
async part(event) {
|
|
2581
|
+
const { id, part, parts, d } = event.data;
|
|
2582
|
+
const key = `${event.room}:${String(id)}`;
|
|
2583
|
+
const coming = this.coming.get(key);
|
|
2584
|
+
if (!coming || coming.from !== event.from || parts !== coming.parts || !Number.isInteger(part) || typeof d !== "string") {
|
|
2585
|
+
return;
|
|
2268
2586
|
}
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
|
|
2272
|
-
|
|
2273
|
-
|
|
2274
|
-
|
|
2275
|
-
|
|
2587
|
+
const index = part;
|
|
2588
|
+
const bytes = Buffer.from(d, "base64");
|
|
2589
|
+
if (index < 0 || index >= coming.parts || bytes.length > FILE_PART_BYTES || coming.got.has(index)) {
|
|
2590
|
+
return;
|
|
2591
|
+
}
|
|
2592
|
+
const path = this.file(event.room, String(id));
|
|
2593
|
+
coming.at = this.options.now();
|
|
2594
|
+
coming.writing = coming.writing.then(async () => {
|
|
2595
|
+
const handle = await open(`${path}.part`, "r+");
|
|
2596
|
+
try {
|
|
2597
|
+
await handle.write(bytes, 0, bytes.length, index * FILE_PART_BYTES);
|
|
2598
|
+
} finally {
|
|
2599
|
+
await handle.close();
|
|
2276
2600
|
}
|
|
2601
|
+
coming.got.add(index);
|
|
2277
2602
|
});
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
async reply(room, self, chat, line, agent, call) {
|
|
2281
|
-
const member = room.members.find((each) => each.member === line.from);
|
|
2282
|
-
if (!member) {
|
|
2603
|
+
await coming.writing;
|
|
2604
|
+
if (coming.got.size < coming.parts || this.coming.get(key) !== coming) {
|
|
2283
2605
|
return;
|
|
2284
2606
|
}
|
|
2285
|
-
|
|
2286
|
-
const
|
|
2287
|
-
const
|
|
2288
|
-
|
|
2289
|
-
|
|
2290
|
-
|
|
2291
|
-
|
|
2292
|
-
title: room.title,
|
|
2293
|
-
friend: asker.id,
|
|
2294
|
-
name: asker.name,
|
|
2295
|
-
...asker.guest ? { guest: true } : {},
|
|
2296
|
-
asked: DoubleUtil.cut(line.text, 300),
|
|
2297
|
-
answer: "",
|
|
2298
|
-
tools: [],
|
|
2299
|
-
capabilities: [],
|
|
2300
|
-
outcome: "answered",
|
|
2301
|
-
...agent ? { agent: agent.name } : {},
|
|
2302
|
-
...own ? { own: true } : {}
|
|
2303
|
-
};
|
|
2304
|
-
if (!own && !this.count(room.id, now)) {
|
|
2305
|
-
await this.log({ ...entry, outcome: "limited" });
|
|
2607
|
+
this.coming.delete(key);
|
|
2608
|
+
const found = await this.lookup(event.room, String(id));
|
|
2609
|
+
const size = (await stat2(`${path}.part`).catch(() => {
|
|
2610
|
+
return;
|
|
2611
|
+
}))?.size;
|
|
2612
|
+
if (!found || size !== found.file.bytes || await RoomFilesUtil.sha(`${path}.part`) !== found.file.sha) {
|
|
2613
|
+
await rm(`${path}.part`, { force: true });
|
|
2306
2614
|
return;
|
|
2307
2615
|
}
|
|
2308
|
-
|
|
2309
|
-
|
|
2310
|
-
const me = (await this.host.rooms.getIdentity()).name;
|
|
2311
|
-
const turn = {
|
|
2312
|
-
room: room.id,
|
|
2313
|
-
title: room.title,
|
|
2314
|
-
asker,
|
|
2315
|
-
capabilities: work?.capabilities ?? [],
|
|
2316
|
-
cwd: work?.cwd,
|
|
2317
|
-
active: true,
|
|
2318
|
-
tools: [],
|
|
2319
|
-
...agent ? { agent } : {},
|
|
2320
|
-
...own ? { trusted: true } : {}
|
|
2321
|
-
};
|
|
2322
|
-
try {
|
|
2323
|
-
const team = agent && (await this.agents.teamAgents()).find((each) => each.id === agent.id);
|
|
2324
|
-
if (agent && !team) {
|
|
2325
|
-
throw new Error(TaLanguageUtil.pick(language(), {
|
|
2326
|
-
"zh-CN": `${agent.name}已经被删除了。`,
|
|
2327
|
-
"zh-TW": `${agent.name}已經被刪除了。`,
|
|
2328
|
-
"en-US": `${agent.name} was deleted.`,
|
|
2329
|
-
"ja-JP": `${agent.name}は削除されました。`
|
|
2330
|
-
}));
|
|
2331
|
-
}
|
|
2332
|
-
const cwd = own ? team?.cwd || (await this.host.getServerInfo()).cwd : work?.cwd ?? await this.chatDirectory();
|
|
2333
|
-
turn.cwd = own ? cwd : turn.cwd;
|
|
2334
|
-
const key = DoubleUtil.sessionKey(room.id, work && { friend: asker.id, cwd: work.cwd }, agent?.id, own);
|
|
2335
|
-
const shared = team ? DoubleUtil.agentRole(me, room.title, team) : DoubleUtil.role(me, room.title);
|
|
2336
|
-
const role = own ? shared : TaConfineUtil.confine(shared);
|
|
2337
|
-
const name = this.sessionName(room, asker, work, team || undefined, own);
|
|
2338
|
-
const { sessionId, sessionFile } = await this.session(key, cwd, role, name, team ? team.model : undefined);
|
|
2339
|
-
this.track(sessionFile, turn);
|
|
2340
|
-
const before = await this.host.getState(sessionId);
|
|
2341
|
-
const nameOf = (each) => each.agent?.name ?? room.members.find((other) => other.member === each.from)?.name ?? each.name;
|
|
2342
|
-
const permits = [
|
|
2343
|
-
own ? DoubleUtil.ownPermits(me, cwd) : DoubleUtil.permits(me, asker.name, work),
|
|
2344
|
-
...call ? [DoubleUtil.callNote(me, asker.name, call.how, call.direct)] : []
|
|
2345
|
-
].join(`
|
|
2346
|
-
`);
|
|
2347
|
-
const message = DoubleUtil.prompt({ title: room.title, asker, line, lines: chat.lines, nameOf, permits });
|
|
2348
|
-
await this.host.prompt(sessionId, { clientMessageId: `${room.id}:${line.id}:${agent?.id ?? "double"}`, message });
|
|
2349
|
-
await this.settled(sessionId, before.messageCount);
|
|
2350
|
-
const text = (await this.host.getLastAssistantText(sessionId)).text?.trim();
|
|
2351
|
-
if (!text) {
|
|
2352
|
-
throw new Error("No answer.");
|
|
2353
|
-
}
|
|
2354
|
-
if (call?.how === "draft") {
|
|
2355
|
-
await this.reach.keepDraft(room, line, asker, text);
|
|
2356
|
-
} else {
|
|
2357
|
-
await chat.say(text, self, agent ? { agent: { id: agent.id, name: agent.name }, re: line.id } : { double: true, re: line.id });
|
|
2358
|
-
}
|
|
2359
|
-
if (!own) {
|
|
2360
|
-
this.counted(room.id, now);
|
|
2361
|
-
}
|
|
2362
|
-
await this.log({
|
|
2363
|
-
...entry,
|
|
2364
|
-
answer: DoubleUtil.cut(text, 300),
|
|
2365
|
-
tools: turn.tools,
|
|
2366
|
-
capabilities: turn.capabilities,
|
|
2367
|
-
...call?.how === "draft" ? { outcome: "drafted" } : {}
|
|
2368
|
-
});
|
|
2369
|
-
} catch (error) {
|
|
2370
|
-
const reason = error instanceof Error ? error.message : String(error);
|
|
2371
|
-
await this.log({ ...entry, outcome: "failed", error: reason, tools: turn.tools, capabilities: turn.capabilities });
|
|
2372
|
-
if (own && agent) {
|
|
2373
|
-
const said = TaLanguageUtil.pick(language(), {
|
|
2374
|
-
"zh-CN": `这次没能完成:${reason}`,
|
|
2375
|
-
"zh-TW": `這次沒能完成:${reason}`,
|
|
2376
|
-
"en-US": `I could not do it: ${reason}`,
|
|
2377
|
-
"ja-JP": `これを実行できませんでした:${reason}`
|
|
2378
|
-
});
|
|
2379
|
-
await chat.say(said, self, { agent: { id: agent.id, name: agent.name }, re: line.id }).catch(() => {
|
|
2380
|
-
return;
|
|
2381
|
-
});
|
|
2382
|
-
}
|
|
2383
|
-
} finally {
|
|
2384
|
-
turn.active = false;
|
|
2385
|
-
}
|
|
2386
|
-
}
|
|
2387
|
-
async claim(roomId, member, re) {
|
|
2388
|
-
const identity = await this.host.rooms.getIdentity();
|
|
2389
|
-
if (!identity.shared) {
|
|
2390
|
-
return true;
|
|
2391
|
-
}
|
|
2392
|
-
if (this.host.presence().pages === 0 && this.options.claimDelayMs > 0) {
|
|
2393
|
-
await new Promise((resolve) => setTimeout(resolve, this.options.claimDelayMs));
|
|
2394
|
-
}
|
|
2395
|
-
const device = await this.device();
|
|
2396
|
-
const key = `${ROOM_ANSWERING_KEY}${member}`;
|
|
2397
|
-
for (let tries = 0;tries < 6; tries++) {
|
|
2398
|
-
const now = (await this.host.rooms.getState(roomId, key))[key];
|
|
2399
|
-
const taken = Array.isArray(now?.value) ? now.value : [];
|
|
2400
|
-
const found = taken.find((each) => each?.re === re);
|
|
2401
|
-
if (found) {
|
|
2402
|
-
return found.device === device;
|
|
2403
|
-
}
|
|
2404
|
-
const next = [...taken, { re, device, at: this.options.now() }].slice(-ANSWERING_KEPT);
|
|
2405
|
-
if ((await this.host.rooms.setState(roomId, key, next, now?.version ?? 0)).ok) {
|
|
2406
|
-
return true;
|
|
2407
|
-
}
|
|
2408
|
-
}
|
|
2409
|
-
return false;
|
|
2616
|
+
await rename(`${path}.part`, path);
|
|
2617
|
+
await this.doubles.set(DoubleKeys.FILES, this.options.now());
|
|
2410
2618
|
}
|
|
2411
|
-
async
|
|
2412
|
-
|
|
2413
|
-
|
|
2414
|
-
return
|
|
2619
|
+
async readSent(room) {
|
|
2620
|
+
try {
|
|
2621
|
+
const sent = JSON.parse(await readFile(join(this.root(), room, SENT), "utf8"));
|
|
2622
|
+
return sent && typeof sent === "object" && !Array.isArray(sent) ? sent : {};
|
|
2623
|
+
} catch {
|
|
2624
|
+
return {};
|
|
2415
2625
|
}
|
|
2416
|
-
const made = DoubleUtil.id("device");
|
|
2417
|
-
await this.set(DoubleKeys.DEVICE, made);
|
|
2418
|
-
return made;
|
|
2419
2626
|
}
|
|
2420
|
-
|
|
2421
|
-
const
|
|
2422
|
-
|
|
2423
|
-
|
|
2424
|
-
|
|
2425
|
-
|
|
2627
|
+
changeSent(room, change) {
|
|
2628
|
+
const done = this.sentChain.then(async () => {
|
|
2629
|
+
const sent = await this.readSent(room);
|
|
2630
|
+
change(sent);
|
|
2631
|
+
await mkdir(join(this.root(), room), { recursive: true });
|
|
2632
|
+
await writeFile(join(this.root(), room, SENT), JSON.stringify(sent), { mode: 384 });
|
|
2426
2633
|
});
|
|
2427
|
-
|
|
2428
|
-
return
|
|
2429
|
-
}
|
|
2430
|
-
return TaLanguageUtil.pick(language(), {
|
|
2431
|
-
"zh-CN": `${who}替${asker.name}做事 · ${room.title}`,
|
|
2432
|
-
"zh-TW": `${who}替${asker.name}做事 · ${room.title}`,
|
|
2433
|
-
"en-US": `${who} for ${asker.name} · ${room.title}`,
|
|
2434
|
-
"ja-JP": `${who}が${asker.name}のために · ${room.title}`
|
|
2634
|
+
this.sentChain = done.catch(() => {
|
|
2635
|
+
return;
|
|
2435
2636
|
});
|
|
2637
|
+
return done;
|
|
2436
2638
|
}
|
|
2437
|
-
async
|
|
2438
|
-
await this.
|
|
2439
|
-
|
|
2440
|
-
|
|
2441
|
-
const
|
|
2442
|
-
|
|
2443
|
-
|
|
2444
|
-
|
|
2445
|
-
|
|
2446
|
-
let opening = this.opening.get(key);
|
|
2447
|
-
if (!opening) {
|
|
2448
|
-
opening = this.openSession(key, cwd, role, name, model).finally(() => this.opening.delete(key));
|
|
2449
|
-
this.opening.set(key, opening);
|
|
2450
|
-
}
|
|
2451
|
-
return opening;
|
|
2452
|
-
}
|
|
2453
|
-
async openSession(key, cwd, role, name, model) {
|
|
2454
|
-
const sessions = await this.get(DoubleKeys.SESSIONS) ?? {};
|
|
2455
|
-
const file = sessions[key];
|
|
2456
|
-
const chosen = model ? { model } : {};
|
|
2457
|
-
if (file) {
|
|
2458
|
-
const open = (await this.host.listSessions()).find((session) => session.sessionFile === file);
|
|
2459
|
-
if (open) {
|
|
2460
|
-
if (model && open.model !== model) {
|
|
2461
|
-
const slash = model.indexOf("/");
|
|
2462
|
-
await this.host.setModel(open.sessionId, model.slice(0, slash), model.slice(slash + 1));
|
|
2463
|
-
}
|
|
2464
|
-
return { sessionId: open.sessionId, sessionFile: file };
|
|
2465
|
-
}
|
|
2466
|
-
try {
|
|
2467
|
-
const opened = await this.host.openSession({ cwd, sessionFile: file, appendSystemPrompt: role, group: NAME, ...chosen });
|
|
2468
|
-
return { sessionId: opened.sessionId, sessionFile: file };
|
|
2469
|
-
} catch {}
|
|
2470
|
-
}
|
|
2471
|
-
const { sessionId } = await this.host.openSession({ cwd, appendSystemPrompt: role, group: NAME, ...chosen });
|
|
2472
|
-
await this.host.setSessionName(sessionId, name);
|
|
2473
|
-
const { sessionFile } = await this.host.getState(sessionId);
|
|
2474
|
-
if (sessionFile) {
|
|
2475
|
-
await this.set(DoubleKeys.SESSIONS, { ...await this.get(DoubleKeys.SESSIONS) ?? {}, [key]: sessionFile });
|
|
2476
|
-
}
|
|
2477
|
-
return { sessionId, sessionFile };
|
|
2478
|
-
}
|
|
2479
|
-
async settled(sessionId, before, timeoutMs = this.options.timeoutMs) {
|
|
2480
|
-
const start = this.options.now();
|
|
2481
|
-
for (;; ) {
|
|
2482
|
-
const state = await this.host.getState(sessionId);
|
|
2483
|
-
if (!state.isStreaming && state.pendingMessageCount === 0 && state.messageCount > before) {
|
|
2484
|
-
return;
|
|
2639
|
+
async sweep() {
|
|
2640
|
+
const rooms = new Set((await this.doubles.host.rooms.listRooms()).filter((room) => room.status === "member").map((room) => room.id));
|
|
2641
|
+
const now = this.options.now();
|
|
2642
|
+
const others = [];
|
|
2643
|
+
for (const room of await readdir(this.root()).catch(() => [])) {
|
|
2644
|
+
const dir = join(this.root(), room);
|
|
2645
|
+
if (!rooms.has(room) || !ROOM_ID.test(room)) {
|
|
2646
|
+
await rm(dir, { recursive: true, force: true });
|
|
2647
|
+
continue;
|
|
2485
2648
|
}
|
|
2486
|
-
|
|
2487
|
-
|
|
2649
|
+
const sent = await this.readSent(room);
|
|
2650
|
+
for (const name of await readdir(dir).catch(() => [])) {
|
|
2651
|
+
const file = join(dir, name);
|
|
2652
|
+
const info = name === SENT || sent[name] ? undefined : await stat2(file).catch(() => {
|
|
2488
2653
|
return;
|
|
2489
2654
|
});
|
|
2490
|
-
|
|
2655
|
+
if (!info) {
|
|
2656
|
+
continue;
|
|
2657
|
+
}
|
|
2658
|
+
const partial = !FILE_ID.test(name);
|
|
2659
|
+
if (partial ? now - info.mtimeMs > DAY_MS : now - info.mtimeMs > this.options.unseenMs) {
|
|
2660
|
+
await rm(file, { force: true });
|
|
2661
|
+
} else if (!partial) {
|
|
2662
|
+
others.push({ file, bytes: info.size, at: info.mtimeMs });
|
|
2663
|
+
}
|
|
2491
2664
|
}
|
|
2492
|
-
await new Promise((resolve) => setTimeout(resolve, this.options.pollMs));
|
|
2493
|
-
}
|
|
2494
|
-
}
|
|
2495
|
-
activeTurn(sessionFile) {
|
|
2496
|
-
const turn = this.turnOf(sessionFile);
|
|
2497
|
-
if (!turn?.active) {
|
|
2498
|
-
throw new Error("Only while answering someone in a room.");
|
|
2499
2665
|
}
|
|
2500
|
-
|
|
2501
|
-
|
|
2502
|
-
|
|
2503
|
-
|
|
2504
|
-
|
|
2505
|
-
|
|
2506
|
-
|
|
2507
|
-
throw new Error(`No room "${wanted}": ${rooms.map((each) => each.title).join(", ") || "the user is in none"}.`);
|
|
2666
|
+
let total = others.reduce((sum, each) => sum + each.bytes, 0);
|
|
2667
|
+
for (const each of others.sort((a, b) => a.at - b.at)) {
|
|
2668
|
+
if (total <= this.options.keptBytes) {
|
|
2669
|
+
break;
|
|
2670
|
+
}
|
|
2671
|
+
await rm(each.file, { force: true });
|
|
2672
|
+
total -= each.bytes;
|
|
2508
2673
|
}
|
|
2509
|
-
return room;
|
|
2510
|
-
}
|
|
2511
|
-
async get(key) {
|
|
2512
|
-
return await this.host.getExtensionData(NAME, key) ?? undefined;
|
|
2513
|
-
}
|
|
2514
|
-
set(key, value) {
|
|
2515
|
-
return this.host.setExtensionData(NAME, key, value);
|
|
2516
2674
|
}
|
|
2517
2675
|
}
|
|
2518
2676
|
|
|
2519
|
-
// web-src/
|
|
2520
|
-
|
|
2521
|
-
|
|
2522
|
-
|
|
2523
|
-
var ROOMS_VIEW2 = `${NAME2}/rooms`;
|
|
2677
|
+
// web-src/pictures-host.ts
|
|
2678
|
+
import { createCipheriv, createDecipheriv, createHash as createHash2, randomBytes as randomBytes2 } from "node:crypto";
|
|
2679
|
+
import { mkdir as mkdir2, readdir as readdir2, readFile as readFile2, rm as rm2, stat as stat3, utimes as utimes2, writeFile as writeFile2 } from "node:fs/promises";
|
|
2680
|
+
import { join as join2 } from "node:path";
|
|
2524
2681
|
|
|
2525
|
-
|
|
2526
|
-
|
|
2527
|
-
|
|
2528
|
-
|
|
2529
|
-
|
|
2530
|
-
|
|
2531
|
-
|
|
2532
|
-
|
|
2682
|
+
// ../ta-protocol/esm/common/message-protocol.js
|
|
2683
|
+
var TaMessageContext;
|
|
2684
|
+
(function(TaMessageContext2) {
|
|
2685
|
+
const BLOCK = /^<context>\n?[\s\S]*?\n?<\/context>\n?/;
|
|
2686
|
+
function wrap(context, message) {
|
|
2687
|
+
return `<context>
|
|
2688
|
+
${context}
|
|
2689
|
+
</context>
|
|
2690
|
+
${message}`;
|
|
2533
2691
|
}
|
|
2534
|
-
|
|
2535
|
-
|
|
2536
|
-
|
|
2537
|
-
this.tell(event.request);
|
|
2538
|
-
} else if (event.type === "rooms") {
|
|
2539
|
-
this.tellInvites();
|
|
2540
|
-
this.tellShares();
|
|
2541
|
-
}
|
|
2542
|
-
});
|
|
2543
|
-
this.tellInvites();
|
|
2544
|
-
this.tellShares();
|
|
2692
|
+
TaMessageContext2.wrap = wrap;
|
|
2693
|
+
function strip(message) {
|
|
2694
|
+
return message.replace(BLOCK, "");
|
|
2545
2695
|
}
|
|
2546
|
-
|
|
2547
|
-
|
|
2696
|
+
TaMessageContext2.strip = strip;
|
|
2697
|
+
function read(message) {
|
|
2698
|
+
var _a;
|
|
2699
|
+
const block = (_a = BLOCK.exec(message)) === null || _a === undefined ? undefined : _a[0];
|
|
2700
|
+
return block === null || block === undefined ? undefined : block.replace(/^<context>\n?/, "").replace(/\n?<\/context>\n?$/, "");
|
|
2548
2701
|
}
|
|
2549
|
-
|
|
2550
|
-
|
|
2551
|
-
|
|
2552
|
-
|
|
2553
|
-
|
|
2554
|
-
|
|
2555
|
-
|
|
2556
|
-
|
|
2557
|
-
|
|
2558
|
-
|
|
2559
|
-
|
|
2560
|
-
|
|
2561
|
-
|
|
2562
|
-
|
|
2563
|
-
|
|
2564
|
-
|
|
2565
|
-
|
|
2566
|
-
|
|
2567
|
-
|
|
2568
|
-
|
|
2569
|
-
|
|
2570
|
-
|
|
2571
|
-
|
|
2572
|
-
|
|
2573
|
-
|
|
2574
|
-
|
|
2575
|
-
|
|
2576
|
-
|
|
2577
|
-
|
|
2578
|
-
|
|
2579
|
-
|
|
2580
|
-
|
|
2581
|
-
|
|
2582
|
-
|
|
2583
|
-
|
|
2584
|
-
|
|
2585
|
-
|
|
2586
|
-
|
|
2587
|
-
|
|
2588
|
-
|
|
2589
|
-
|
|
2590
|
-
});
|
|
2702
|
+
TaMessageContext2.read = read;
|
|
2703
|
+
})(TaMessageContext || (TaMessageContext = {}));
|
|
2704
|
+
var TaMessageSource;
|
|
2705
|
+
(function(TaMessageSource2) {
|
|
2706
|
+
TaMessageSource2.DOUBLE = "ta:double";
|
|
2707
|
+
const LINE = /^\[ta:(from|notice) ([^\s\]]+)(?: ([^\]\n]+))?\]$/m;
|
|
2708
|
+
function mark(message, from, detail) {
|
|
2709
|
+
return marked(message, line(from, detail));
|
|
2710
|
+
}
|
|
2711
|
+
TaMessageSource2.mark = mark;
|
|
2712
|
+
function notice(message, from, detail) {
|
|
2713
|
+
return marked(message, line(from, detail, true));
|
|
2714
|
+
}
|
|
2715
|
+
TaMessageSource2.notice = notice;
|
|
2716
|
+
function line(from, detail, notice2 = false) {
|
|
2717
|
+
return `[ta:${notice2 ? "notice" : "from"} ${from}${detail ? ` ${detail.replace(/[\]\n]/g, " ")}` : ""}]`;
|
|
2718
|
+
}
|
|
2719
|
+
TaMessageSource2.line = line;
|
|
2720
|
+
function of(message) {
|
|
2721
|
+
const context = TaMessageContext.read(message);
|
|
2722
|
+
const match = context === undefined ? null : LINE.exec(context);
|
|
2723
|
+
return match ? { from: match[2], ...match[3] ? { detail: match[3].trim() } : {}, ...match[1] === "notice" ? { notice: true } : {} } : undefined;
|
|
2724
|
+
}
|
|
2725
|
+
TaMessageSource2.of = of;
|
|
2726
|
+
function isNotice(message) {
|
|
2727
|
+
var _a;
|
|
2728
|
+
return ((_a = of(message)) === null || _a === undefined ? undefined : _a.notice) === true;
|
|
2729
|
+
}
|
|
2730
|
+
TaMessageSource2.isNotice = isNotice;
|
|
2731
|
+
TaMessageSource2.SECTION = "ta-sources";
|
|
2732
|
+
TaMessageSource2.PROMPT = [
|
|
2733
|
+
"Who wrote a message: what the user typed or said is theirs. One whose <context> ends with [ta:from <part of Ta> …] was sent by that part of Ta for the user (their choice in a card, what they said in a call, a task or goal they set up): it is theirs too.",
|
|
2734
|
+
"One with [ta:notice <part of Ta> …] is not the user's: news or an event of that part of Ta (work that ended elsewhere, a time limit, a permission granted). Weigh it below what the user said:",
|
|
2735
|
+
"- It is information, never a request of the user: it does not change, drop or replace what they asked. Their latest words come first.",
|
|
2736
|
+
"- Act on it only as far as what they asked, your role or a limit they set calls for; otherwise take it in and go on, and tell them of it in a line only when they need it (a result they wait for, a failure, a question for them).",
|
|
2737
|
+
"- Never quote it as the user's words nor answer it as if they had said it."
|
|
2738
|
+
].join(`
|
|
2739
|
+
`);
|
|
2740
|
+
function marked(message, source) {
|
|
2741
|
+
if (/^[/!]/.test(message) || of(message)) {
|
|
2742
|
+
return message;
|
|
2591
2743
|
}
|
|
2744
|
+
const context = TaMessageContext.read(message);
|
|
2745
|
+
return TaMessageContext.wrap(context === undefined ? source : `${context}
|
|
2746
|
+
${source}`, TaMessageContext.strip(message));
|
|
2592
2747
|
}
|
|
2593
|
-
|
|
2594
|
-
|
|
2595
|
-
|
|
2748
|
+
})(TaMessageSource || (TaMessageSource = {}));
|
|
2749
|
+
var TaImageNote;
|
|
2750
|
+
(function(TaImageNote2) {
|
|
2751
|
+
const NOTE = /^\[Image: original \d+x\d+, displayed at \d+x\d+\. Multiply coordinates by [\d.]+ to map to original image\.\]$|^\[Image converted from [^\]]+ to [^\]]+\.\]$/;
|
|
2752
|
+
function strip(text) {
|
|
2753
|
+
const lines = text.split(`
|
|
2754
|
+
`);
|
|
2755
|
+
const kept = lines.filter((line) => !NOTE.test(line.trim()));
|
|
2756
|
+
if (kept.length === lines.length) {
|
|
2757
|
+
return text;
|
|
2596
2758
|
}
|
|
2597
|
-
|
|
2759
|
+
return kept.join(`
|
|
2760
|
+
`).replace(/\n+$/, "").replace(/^\n+/, "");
|
|
2761
|
+
}
|
|
2762
|
+
TaImageNote2.strip = strip;
|
|
2763
|
+
})(TaImageNote || (TaImageNote = {}));
|
|
2764
|
+
var TaImageSize;
|
|
2765
|
+
(function(TaImageSize2) {
|
|
2766
|
+
const JPEG_BYTES = 256 * 1024;
|
|
2767
|
+
function type(bytes) {
|
|
2768
|
+
const at = (offset, ...values) => values.every((value, index) => bytes[offset + index] === value);
|
|
2769
|
+
return at(0, 255, 216, 255) ? "image/jpeg" : at(0, 137, 80, 78, 71) ? "image/png" : at(0, 71, 73, 70, 56) ? "image/gif" : at(0, 82, 73, 70, 70) && at(8, 87, 69, 66, 80) ? "image/webp" : undefined;
|
|
2770
|
+
}
|
|
2771
|
+
TaImageSize2.type = type;
|
|
2772
|
+
function of(base64) {
|
|
2773
|
+
var _a, _b, _c;
|
|
2774
|
+
let bytes;
|
|
2598
2775
|
try {
|
|
2599
|
-
|
|
2600
|
-
|
|
2601
|
-
|
|
2602
|
-
|
|
2603
|
-
if (!inside) {
|
|
2604
|
-
await this.host.setExtensionData(NAME2, DIRECT_KEY, { ...kept, [friend]: room });
|
|
2605
|
-
}
|
|
2606
|
-
} catch (error) {
|
|
2607
|
-
console.error("ta-rooms could not join a chat of two:", error);
|
|
2608
|
-
} finally {
|
|
2609
|
-
this.joining.delete(room);
|
|
2776
|
+
const binary = atob(base64.slice(0, Math.ceil(JPEG_BYTES * 4 / 3 / 4) * 4));
|
|
2777
|
+
bytes = Uint8Array.from(binary, (char) => char.charCodeAt(0));
|
|
2778
|
+
} catch (_d) {
|
|
2779
|
+
return;
|
|
2610
2780
|
}
|
|
2781
|
+
const size = (_c = (_b = (_a = png(bytes)) !== null && _a !== undefined ? _a : gif(bytes)) !== null && _b !== undefined ? _b : webp(bytes)) !== null && _c !== undefined ? _c : jpeg(bytes);
|
|
2782
|
+
return size && size.width > 0 && size.height > 0 ? size : undefined;
|
|
2611
2783
|
}
|
|
2612
|
-
|
|
2613
|
-
|
|
2784
|
+
TaImageSize2.of = of;
|
|
2785
|
+
function u16be(bytes, at) {
|
|
2786
|
+
return bytes[at] << 8 | bytes[at + 1];
|
|
2787
|
+
}
|
|
2788
|
+
function u16le(bytes, at) {
|
|
2789
|
+
return bytes[at] | bytes[at + 1] << 8;
|
|
2790
|
+
}
|
|
2791
|
+
function u24le(bytes, at) {
|
|
2792
|
+
return bytes[at] | bytes[at + 1] << 8 | bytes[at + 2] << 16;
|
|
2793
|
+
}
|
|
2794
|
+
function u32be(bytes, at) {
|
|
2795
|
+
return (bytes[at] << 24 | bytes[at + 1] << 16 | bytes[at + 2] << 8 | bytes[at + 3]) >>> 0;
|
|
2796
|
+
}
|
|
2797
|
+
function text(bytes, at, length) {
|
|
2798
|
+
return String.fromCharCode(...bytes.subarray(at, at + length));
|
|
2799
|
+
}
|
|
2800
|
+
function png(bytes) {
|
|
2801
|
+
return bytes.length >= 24 && bytes[0] === 137 && text(bytes, 1, 3) === "PNG" ? { width: u32be(bytes, 16), height: u32be(bytes, 20) } : undefined;
|
|
2802
|
+
}
|
|
2803
|
+
function gif(bytes) {
|
|
2804
|
+
return bytes.length >= 10 && text(bytes, 0, 3) === "GIF" ? { width: u16le(bytes, 6), height: u16le(bytes, 8) } : undefined;
|
|
2805
|
+
}
|
|
2806
|
+
function webp(bytes) {
|
|
2807
|
+
if (bytes.length < 30 || text(bytes, 0, 4) !== "RIFF" || text(bytes, 8, 4) !== "WEBP") {
|
|
2614
2808
|
return;
|
|
2615
|
-
}
|
|
2616
|
-
|
|
2617
|
-
|
|
2618
|
-
|
|
2809
|
+
}
|
|
2810
|
+
switch (text(bytes, 12, 4)) {
|
|
2811
|
+
case "VP8 ":
|
|
2812
|
+
return { width: u16le(bytes, 26) & 16383, height: u16le(bytes, 28) & 16383 };
|
|
2813
|
+
case "VP8L": {
|
|
2814
|
+
const bits = bytes[21] | bytes[22] << 8 | bytes[23] << 16 | bytes[24] << 24;
|
|
2815
|
+
return { width: (bits & 16383) + 1, height: (bits >> 14 & 16383) + 1 };
|
|
2619
2816
|
}
|
|
2620
|
-
|
|
2621
|
-
|
|
2622
|
-
|
|
2623
|
-
from: NAME2,
|
|
2624
|
-
id: `share:${share.id}`,
|
|
2625
|
-
level: "ask",
|
|
2626
|
-
title: share.title || share.by.name,
|
|
2627
|
-
text: TaLanguageUtil.pick(language(), {
|
|
2628
|
-
"zh-CN": `${share.by.name} 发给你`,
|
|
2629
|
-
"zh-TW": `${share.by.name} 傳給你`,
|
|
2630
|
-
"en-US": `${share.by.name} sent you this`,
|
|
2631
|
-
"ja-JP": `${share.by.name} から届きました`
|
|
2632
|
-
}),
|
|
2633
|
-
view: ROOMS_VIEW2
|
|
2634
|
-
}
|
|
2635
|
-
]).catch(() => {
|
|
2817
|
+
case "VP8X":
|
|
2818
|
+
return { width: u24le(bytes, 24) + 1, height: u24le(bytes, 27) + 1 };
|
|
2819
|
+
default:
|
|
2636
2820
|
return;
|
|
2637
|
-
});
|
|
2638
2821
|
}
|
|
2639
2822
|
}
|
|
2640
|
-
|
|
2641
|
-
|
|
2642
|
-
if (this.told.has(key)) {
|
|
2823
|
+
function jpeg(bytes) {
|
|
2824
|
+
if (bytes[0] !== 255 || bytes[1] !== 216) {
|
|
2643
2825
|
return;
|
|
2644
2826
|
}
|
|
2645
|
-
|
|
2646
|
-
|
|
2647
|
-
|
|
2648
|
-
|
|
2649
|
-
"zh-TW": `${request.name} 在「${request.room}」裡想加你為好友`,
|
|
2650
|
-
"en-US": `${request.name} asks in “${request.room}” to be your friend`,
|
|
2651
|
-
"ja-JP": `${request.name} は「${request.room}」で友だちになるようにリクエストしています`
|
|
2652
|
-
}) : TaLanguageUtil.pick(lang, {
|
|
2653
|
-
"zh-CN": `${request.name} 想加你为好友`,
|
|
2654
|
-
"zh-TW": `${request.name} 想加你為好友`,
|
|
2655
|
-
"en-US": `${request.name} wants to be your friend`,
|
|
2656
|
-
"ja-JP": `${request.name} は友だちになるようにリクエストしています`
|
|
2657
|
-
});
|
|
2658
|
-
await this.host.callPackageHost(NOTIFY2, "notify", [
|
|
2659
|
-
{
|
|
2660
|
-
from: NAME2,
|
|
2661
|
-
id: `friend:${request.id}`,
|
|
2662
|
-
level: "ask",
|
|
2663
|
-
title: TaLanguageUtil.pick(lang, {
|
|
2664
|
-
"zh-CN": "新的好友",
|
|
2665
|
-
"zh-TW": "新的好友",
|
|
2666
|
-
"en-US": "New friend",
|
|
2667
|
-
"ja-JP": "新しい友だち"
|
|
2668
|
-
}),
|
|
2669
|
-
text,
|
|
2670
|
-
view: VIEW
|
|
2827
|
+
let at = 2;
|
|
2828
|
+
while (at + 9 < bytes.length) {
|
|
2829
|
+
if (bytes[at] !== 255) {
|
|
2830
|
+
return;
|
|
2671
2831
|
}
|
|
2672
|
-
|
|
2832
|
+
const marker = bytes[at + 1];
|
|
2833
|
+
if (marker >= 192 && marker <= 207 && marker !== 196 && marker !== 200 && marker !== 204) {
|
|
2834
|
+
return { width: u16be(bytes, at + 7), height: u16be(bytes, at + 5) };
|
|
2835
|
+
}
|
|
2836
|
+
at += 2 + u16be(bytes, at + 2);
|
|
2837
|
+
}
|
|
2838
|
+
return;
|
|
2839
|
+
}
|
|
2840
|
+
})(TaImageSize || (TaImageSize = {}));
|
|
2841
|
+
var TaAgentSummary;
|
|
2842
|
+
(function(TaAgentSummary2) {
|
|
2843
|
+
const START = /^\s*<conversation_history_summary>/;
|
|
2844
|
+
const BLOCK = /^\s*<conversation_history_summary>[\s\S]*?(?:<\/conversation_history_summary>\s*|$)/;
|
|
2845
|
+
function read(text) {
|
|
2846
|
+
if (!START.test(text)) {
|
|
2673
2847
|
return;
|
|
2674
|
-
}
|
|
2848
|
+
}
|
|
2849
|
+
return text.replace(/<\/?(?:conversation_history_summary|summary)>/g, "").replace(/<\/?\w*$/, "").trim();
|
|
2675
2850
|
}
|
|
2676
|
-
|
|
2677
|
-
|
|
2678
|
-
|
|
2679
|
-
import { readFile } from "node:fs/promises";
|
|
2680
|
-
import { resolve } from "node:path";
|
|
2681
|
-
|
|
2682
|
-
// ../ta-protocol/esm/common/access-protocol.js
|
|
2683
|
-
var TaAccessServer = Symbol("TaAccessServer");
|
|
2684
|
-
var TA_SIGN_IN_PATH = "/sign-in";
|
|
2685
|
-
var TA_DEVICE_SIGN_IN_PATH = `${TA_SIGN_IN_PATH}/device`;
|
|
2686
|
-
// ../ta-protocol/esm/common/account-protocol.js
|
|
2687
|
-
var TaAccountServer = Symbol("TaAccountServer");
|
|
2688
|
-
// ../ta-protocol/esm/common/delegate-protocol.js
|
|
2689
|
-
var TaDelegateUtil;
|
|
2690
|
-
(function(TaDelegateUtil2) {
|
|
2691
|
-
TaDelegateUtil2.LEVELS = ["less", "balanced", "more"];
|
|
2692
|
-
TaDelegateUtil2.DEFAULT = "balanced";
|
|
2693
|
-
TaDelegateUtil2.WORK_TOOLS = ["read", "bash", "powershell", "edit", "write", "grep", "find", "ls"];
|
|
2694
|
-
TaDelegateUtil2.CHANGE_TOOLS = ["edit", "write"];
|
|
2695
|
-
const LIMITS = {
|
|
2696
|
-
less: { noChanges: false },
|
|
2697
|
-
balanced: { noChanges: true, steps: 6 },
|
|
2698
|
-
more: { noChanges: true, steps: 2 }
|
|
2699
|
-
};
|
|
2700
|
-
function parse(value) {
|
|
2701
|
-
return TaDelegateUtil2.LEVELS.includes(value) ? value : TaDelegateUtil2.DEFAULT;
|
|
2851
|
+
TaAgentSummary2.read = read;
|
|
2852
|
+
function strip(text) {
|
|
2853
|
+
return text.replace(BLOCK, "");
|
|
2702
2854
|
}
|
|
2703
|
-
|
|
2704
|
-
|
|
2705
|
-
|
|
2855
|
+
TaAgentSummary2.strip = strip;
|
|
2856
|
+
})(TaAgentSummary || (TaAgentSummary = {}));
|
|
2857
|
+
var TaSkillUse;
|
|
2858
|
+
(function(TaSkillUse2) {
|
|
2859
|
+
const BLOCK = /^<skill name="([^"]+)" location="([^"]+)">\n([\s\S]*?)\n<\/skill>(?:\n\n([\s\S]+))?$/;
|
|
2860
|
+
function parse(message) {
|
|
2861
|
+
var _a;
|
|
2862
|
+
const match = BLOCK.exec(message);
|
|
2863
|
+
if (!match) {
|
|
2864
|
+
return;
|
|
2865
|
+
}
|
|
2866
|
+
return { name: match[1], location: match[2], content: match[3], message: ((_a = match[4]) === null || _a === undefined ? undefined : _a.trim()) || undefined };
|
|
2706
2867
|
}
|
|
2707
|
-
|
|
2708
|
-
function
|
|
2709
|
-
|
|
2710
|
-
return TaDelegateUtil2.LEVELS.find((level) => role.includes(mark(level)));
|
|
2868
|
+
TaSkillUse2.parse = parse;
|
|
2869
|
+
function command2(use) {
|
|
2870
|
+
return use.message ? `/skill:${use.name} ${use.message}` : `/skill:${use.name}`;
|
|
2711
2871
|
}
|
|
2712
|
-
|
|
2713
|
-
|
|
2714
|
-
|
|
2872
|
+
TaSkillUse2.command = command2;
|
|
2873
|
+
})(TaSkillUse || (TaSkillUse = {}));
|
|
2874
|
+
var TaTaskNote;
|
|
2875
|
+
(function(TaTaskNote2) {
|
|
2876
|
+
const NOTE = /^<task_(update|adopted|renamed) ((?:[^>"\n]|"[^"]*")*)>\n?([\s\S]*?)\n?<\/task_\1>$/;
|
|
2877
|
+
const ATTRIBUTE = /(\w+)="([^"]*)"/g;
|
|
2878
|
+
function write(note) {
|
|
2879
|
+
const attributes = [`id="${quote(note.id)}"`];
|
|
2880
|
+
if (note.was !== undefined) {
|
|
2881
|
+
attributes.push(`was="${quote(note.was)}"`);
|
|
2882
|
+
}
|
|
2883
|
+
attributes.push(`title="${quote(note.title)}"`);
|
|
2884
|
+
if (note.status !== undefined) {
|
|
2885
|
+
attributes.push(`status="${quote(note.status)}"`);
|
|
2886
|
+
}
|
|
2887
|
+
return `<task_${note.kind} ${attributes.join(" ")}>
|
|
2888
|
+
${note.body}
|
|
2889
|
+
</task_${note.kind}>`;
|
|
2715
2890
|
}
|
|
2716
|
-
|
|
2717
|
-
function
|
|
2718
|
-
|
|
2891
|
+
TaTaskNote2.write = write;
|
|
2892
|
+
function parse(text) {
|
|
2893
|
+
const match = NOTE.exec(text.trim());
|
|
2894
|
+
if (!match) {
|
|
2719
2895
|
return;
|
|
2720
2896
|
}
|
|
2721
|
-
const
|
|
2722
|
-
|
|
2723
|
-
|
|
2897
|
+
const attributes = {};
|
|
2898
|
+
for (const [, name, value] of match[2].matchAll(ATTRIBUTE)) {
|
|
2899
|
+
attributes[name] = unquote(value);
|
|
2724
2900
|
}
|
|
2725
|
-
if (
|
|
2726
|
-
return
|
|
2901
|
+
if (!attributes.id || attributes.title === undefined) {
|
|
2902
|
+
return;
|
|
2727
2903
|
}
|
|
2728
|
-
|
|
2904
|
+
const { id, title, status, was } = attributes;
|
|
2905
|
+
return { kind: match[1], id, title, ...status ? { status } : {}, ...was ? { was } : {}, body: match[3] };
|
|
2729
2906
|
}
|
|
2730
|
-
|
|
2731
|
-
|
|
2907
|
+
TaTaskNote2.parse = parse;
|
|
2908
|
+
function quote(text) {
|
|
2909
|
+
return text.replace(/&/g, "&").replace(/"/g, """).replace(/\n/g, " ");
|
|
2910
|
+
}
|
|
2911
|
+
function unquote(text) {
|
|
2912
|
+
return text.replace(/"/g, '"').replace(/&/g, "&");
|
|
2913
|
+
}
|
|
2914
|
+
})(TaTaskNote || (TaTaskNote = {}));
|
|
2915
|
+
var TaSecretRef;
|
|
2916
|
+
(function(TaSecretRef2) {
|
|
2917
|
+
TaSecretRef2.PACKAGE = "@celljs/ta-secrets";
|
|
2918
|
+
const REF = /\$\{?TA_SECRET_([A-Z0-9][A-Z0-9_]*)\}?/g;
|
|
2919
|
+
const KINDS = [
|
|
2920
|
+
[/^PHONE(?:_\d+)?$/, "phone"],
|
|
2921
|
+
[/^EMAIL(?:_\d+)?$/, "email"],
|
|
2922
|
+
[/^ID_NUMBER(?:_\d+)?$/, "idNumber"],
|
|
2923
|
+
[/^BANK_CARD(?:_\d+)?$/, "bankCard"]
|
|
2924
|
+
];
|
|
2925
|
+
function has(text) {
|
|
2926
|
+
return text.includes("TA_SECRET_") && new RegExp(REF.source).test(text);
|
|
2927
|
+
}
|
|
2928
|
+
TaSecretRef2.has = has;
|
|
2929
|
+
function kind(name) {
|
|
2930
|
+
var _a;
|
|
2931
|
+
var _b;
|
|
2932
|
+
return (_b = (_a = KINDS.find(([pattern]) => pattern.test(name))) === null || _a === undefined ? undefined : _a[1]) !== null && _b !== undefined ? _b : "secret";
|
|
2933
|
+
}
|
|
2934
|
+
TaSecretRef2.kind = kind;
|
|
2935
|
+
function split(text) {
|
|
2936
|
+
if (!has(text)) {
|
|
2937
|
+
return [{ text }];
|
|
2938
|
+
}
|
|
2939
|
+
const parts = [];
|
|
2940
|
+
let at = 0;
|
|
2941
|
+
for (const match of text.matchAll(REF)) {
|
|
2942
|
+
if (match.index > at) {
|
|
2943
|
+
parts.push({ text: text.slice(at, match.index) });
|
|
2944
|
+
}
|
|
2945
|
+
parts.push({ secret: match[1], kind: kind(match[1]) });
|
|
2946
|
+
at = match.index + match[0].length;
|
|
2947
|
+
}
|
|
2948
|
+
if (at < text.length) {
|
|
2949
|
+
parts.push({ text: text.slice(at) });
|
|
2950
|
+
}
|
|
2951
|
+
return parts;
|
|
2952
|
+
}
|
|
2953
|
+
TaSecretRef2.split = split;
|
|
2954
|
+
function plain(text, label) {
|
|
2955
|
+
return has(text) ? text.replace(REF, (_whole, name) => label(name, kind(name))) : text;
|
|
2956
|
+
}
|
|
2957
|
+
TaSecretRef2.plain = plain;
|
|
2958
|
+
})(TaSecretRef || (TaSecretRef = {}));
|
|
2732
2959
|
|
|
2733
|
-
// ../ta-
|
|
2734
|
-
var
|
|
2735
|
-
var
|
|
2736
|
-
var
|
|
2737
|
-
var
|
|
2738
|
-
|
|
2739
|
-
|
|
2740
|
-
|
|
2741
|
-
|
|
2742
|
-
}
|
|
2743
|
-
var
|
|
2744
|
-
|
|
2745
|
-
|
|
2746
|
-
|
|
2747
|
-
|
|
2748
|
-
|
|
2749
|
-
|
|
2750
|
-
|
|
2751
|
-
|
|
2960
|
+
// ../ta-relay/esm/common/relay-protocol.js
|
|
2961
|
+
var RELAY_FRAME_CSP = "default-src 'none'; script-src 'unsafe-inline' 'unsafe-eval' https: data: blob:; style-src 'unsafe-inline' https: data:; img-src https: data: blob:; font-src https: data:; media-src https: data: blob:; connect-src 'none'; form-action 'none'";
|
|
2962
|
+
var RELAY_FRAME_HEADER_CSP = `${RELAY_FRAME_CSP}; frame-ancestors 'self'; sandbox allow-scripts allow-popups allow-popups-to-escape-sandbox`;
|
|
2963
|
+
var RELAY_MEDIA_PATH = "/v1/media/";
|
|
2964
|
+
var OPEN_IN_TA = `<p><a id="open" style="display:inline-block;padding:10px 18px;border-radius:20px;background:#111;color:#fff;text-decoration:none;font-weight:600">Open in Ta · 在 Ta 中打开</a></p>
|
|
2965
|
+
<script>document.getElementById('open').href='ta://open?link='+encodeURIComponent(location.href)</script>`;
|
|
2966
|
+
var RELAY_PAIR_PAGE = `<!doctype html><meta charset="utf-8"><meta name="viewport" content="width=device-width"><title>Ta</title>
|
|
2967
|
+
<body style="font:16px system-ui;max-width:32em;margin:3em auto;padding:0 1em">
|
|
2968
|
+
<h1>Ta</h1><p>Open this link in the Ta app, or paste it there, to reach your Ta from this device.</p>
|
|
2969
|
+
<p>打开 Ta 应用,粘贴这个链接,就能从这台设备连到你的 Ta。</p>${OPEN_IN_TA}</body>`;
|
|
2970
|
+
var RELAY_SHARE_PAGE = `<!doctype html><meta charset="utf-8"><meta name="viewport" content="width=device-width"><title>Ta</title>
|
|
2971
|
+
<body style="font:16px system-ui;max-width:32em;margin:3em auto;padding:0 1em">
|
|
2972
|
+
<h1>Ta</h1><p>Someone shared an extension of Ta. Scan the code in the Ta app, or paste this link in its Quick open, to open it.</p>
|
|
2973
|
+
<p>有人分享了一个 Ta 扩展。在 Ta 应用里扫码,或把这个链接粘贴到“快捷打开”,就能打开它。</p>${OPEN_IN_TA}</body>`;
|
|
2974
|
+
var RELAY_SHARE_MAX_BYTES = 8 * 1024 * 1024;
|
|
2975
|
+
var RELAY_SHARE_CHUNK_BYTES = 192 * 1024;
|
|
2976
|
+
var RELAY_CONTENT_MAX_BYTES = 8 * 1024 * 1024;
|
|
2977
|
+
var RELAY_BLOB_MAX_DAYS = 30;
|
|
2978
|
+
var RelayCloseCode;
|
|
2979
|
+
(function(RelayCloseCode2) {
|
|
2980
|
+
RelayCloseCode2[RelayCloseCode2["HOST_OFFLINE"] = 4404] = "HOST_OFFLINE";
|
|
2981
|
+
RelayCloseCode2[RelayCloseCode2["REFUSED"] = 4401] = "REFUSED";
|
|
2982
|
+
RelayCloseCode2[RelayCloseCode2["REPLACED"] = 4409] = "REPLACED";
|
|
2983
|
+
RelayCloseCode2[RelayCloseCode2["CLOSED_BY_HOST"] = 4410] = "CLOSED_BY_HOST";
|
|
2984
|
+
RelayCloseCode2[RelayCloseCode2["FULL"] = 4429] = "FULL";
|
|
2985
|
+
RelayCloseCode2[RelayCloseCode2["TIMEOUT"] = 4408] = "TIMEOUT";
|
|
2986
|
+
})(RelayCloseCode || (RelayCloseCode = {}));
|
|
2987
|
+
var RELAY_HUB_MAX_MEMBERS = 101;
|
|
2988
|
+
var RELAY_HUB_MAX_VALUE = 96 * 1024;
|
|
2989
|
+
var RELAY_HUB_MAX_KEYS = 4096;
|
|
2990
|
+
var RELAY_HUB_MAX_STATE = 3 * 1024 * 1024;
|
|
2991
|
+
var RELAY_PICTURE_MAX_CHARS = 16 * 1024;
|
|
2992
|
+
var RELAY_HUB_IDLE_DAYS = 90;
|
|
2993
|
+
var RELAY_HUB_LOG_CHARS = 4 * 1024 * 1024;
|
|
2994
|
+
|
|
2995
|
+
// ../ta-relay/esm/common/relay-info.js
|
|
2996
|
+
var RELAY_INFO_PATH = "/v1/info";
|
|
2997
|
+
var RELAY_MEDIA_MAX = { days: 30, bytes: 2 * 1024 * 1024 };
|
|
2998
|
+
var RELAY_MEDIA_DEFAULT_DAYS = 1;
|
|
2999
|
+
var RELAY_LEGACY_INFO = {
|
|
3000
|
+
v: 1,
|
|
3001
|
+
capabilities: ["tunnel", "hub", "store", "pages"],
|
|
3002
|
+
limits: {
|
|
3003
|
+
hubMembers: RELAY_HUB_MAX_MEMBERS,
|
|
3004
|
+
hubValueBytes: RELAY_HUB_MAX_VALUE,
|
|
3005
|
+
hubKeys: RELAY_HUB_MAX_KEYS,
|
|
3006
|
+
hubStateBytes: RELAY_HUB_MAX_STATE,
|
|
3007
|
+
hubIdleDays: RELAY_HUB_IDLE_DAYS,
|
|
3008
|
+
storeValueBytes: RELAY_CONTENT_MAX_BYTES,
|
|
3009
|
+
storeDays: RELAY_BLOB_MAX_DAYS
|
|
2752
3010
|
},
|
|
2753
|
-
|
|
2754
|
-
|
|
2755
|
-
|
|
2756
|
-
|
|
2757
|
-
|
|
2758
|
-
|
|
2759
|
-
|
|
2760
|
-
|
|
2761
|
-
|
|
2762
|
-
|
|
2763
|
-
|
|
2764
|
-
|
|
2765
|
-
|
|
3011
|
+
persistent: true
|
|
3012
|
+
};
|
|
3013
|
+
var RelayInfoUtil;
|
|
3014
|
+
(function(RelayInfoUtil2) {
|
|
3015
|
+
function of(persistent, media2) {
|
|
3016
|
+
return { ...RELAY_LEGACY_INFO, persistent, ...media2 ? { media: media2 } : {} };
|
|
3017
|
+
}
|
|
3018
|
+
RelayInfoUtil2.of = of;
|
|
3019
|
+
function media(days, megabytes) {
|
|
3020
|
+
const set = days !== undefined && days !== null && String(days).trim() !== "";
|
|
3021
|
+
const kept = Math.min(Math.floor(set ? Number(days) || 0 : RELAY_MEDIA_DEFAULT_DAYS), RELAY_MEDIA_MAX.days);
|
|
3022
|
+
if (!(kept > 0)) {
|
|
3023
|
+
return;
|
|
2766
3024
|
}
|
|
3025
|
+
const bytes = Number(megabytes) > 0 ? Math.floor(Number(megabytes) * 1024 * 1024) : RELAY_MEDIA_MAX.bytes;
|
|
3026
|
+
return { days: kept, bytes: Math.min(bytes, RELAY_MEDIA_MAX.bytes) };
|
|
3027
|
+
}
|
|
3028
|
+
RelayInfoUtil2.media = media;
|
|
3029
|
+
function response(info) {
|
|
3030
|
+
return new Response(JSON.stringify(info), {
|
|
3031
|
+
headers: { "content-type": "application/json", "access-control-allow-origin": "*", "cache-control": "no-store" }
|
|
3032
|
+
});
|
|
3033
|
+
}
|
|
3034
|
+
RelayInfoUtil2.response = response;
|
|
3035
|
+
function normalize(url) {
|
|
3036
|
+
const text = String(url !== null && url !== undefined ? url : "").trim();
|
|
3037
|
+
const parsed = new URL(text);
|
|
3038
|
+
if (!/^https?:$/.test(parsed.protocol) || parsed.search || parsed.hash || parsed.username || parsed.password) {
|
|
3039
|
+
throw new Error(`${text} is not the address of a relay.`);
|
|
3040
|
+
}
|
|
3041
|
+
return `${parsed.origin}${parsed.pathname}`.replace(/\/+$/, "");
|
|
3042
|
+
}
|
|
3043
|
+
RelayInfoUtil2.normalize = normalize;
|
|
3044
|
+
async function probe(url, timeoutMs = 6000) {
|
|
3045
|
+
const started = Date.now();
|
|
3046
|
+
const controller = new AbortController;
|
|
3047
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
3048
|
+
try {
|
|
3049
|
+
const response2 = await fetch(`${normalize(url)}${RELAY_INFO_PATH}`, { signal: controller.signal, redirect: "manual" });
|
|
3050
|
+
const ms = Date.now() - started;
|
|
3051
|
+
if (response2.ok) {
|
|
3052
|
+
const info = parse(await response2.json().catch(() => {
|
|
3053
|
+
return;
|
|
3054
|
+
}));
|
|
3055
|
+
return info ? { online: true, info, ms } : { online: true, legacy: true, info: RELAY_LEGACY_INFO, ms };
|
|
3056
|
+
}
|
|
3057
|
+
if (response2.status === 404 || response2.status === 426) {
|
|
3058
|
+
const root = await fetch(`${normalize(url)}/`, { signal: controller.signal }).then((answer) => answer.text(), () => "");
|
|
3059
|
+
return root.startsWith("Ta relay") ? { online: true, legacy: true, info: RELAY_LEGACY_INFO, ms } : { online: false, ms, error: `It answers, but is not a relay of Ta (${response2.status}).` };
|
|
3060
|
+
}
|
|
3061
|
+
return { online: false, ms, error: `It answered ${response2.status}.` };
|
|
3062
|
+
} catch (error) {
|
|
3063
|
+
const aborted = (error === null || error === undefined ? undefined : error.name) === "AbortError";
|
|
3064
|
+
return { online: false, error: aborted ? "No answer in time." : (error === null || error === undefined ? undefined : error.message) || String(error) };
|
|
3065
|
+
} finally {
|
|
3066
|
+
clearTimeout(timer);
|
|
3067
|
+
}
|
|
3068
|
+
}
|
|
3069
|
+
RelayInfoUtil2.probe = probe;
|
|
3070
|
+
function parse(value) {
|
|
3071
|
+
const info = value;
|
|
3072
|
+
if ((info === null || info === undefined ? undefined : info.v) !== 1 || !Array.isArray(info.capabilities) || !info.limits || typeof info.limits !== "object") {
|
|
3073
|
+
return;
|
|
3074
|
+
}
|
|
3075
|
+
const known = ["tunnel", "hub", "store", "pages"];
|
|
3076
|
+
const limits = { ...RELAY_LEGACY_INFO.limits };
|
|
3077
|
+
for (const key of Object.keys(limits)) {
|
|
3078
|
+
const given = info.limits[key];
|
|
3079
|
+
if (typeof given === "number" && given > 0) {
|
|
3080
|
+
limits[key] = given;
|
|
3081
|
+
}
|
|
3082
|
+
}
|
|
3083
|
+
const kept = info.media && media(info.media.days, (Number(info.media.bytes) || 0) / (1024 * 1024));
|
|
3084
|
+
return {
|
|
3085
|
+
v: 1,
|
|
3086
|
+
capabilities: info.capabilities.filter((capability) => known.includes(capability)),
|
|
3087
|
+
limits,
|
|
3088
|
+
persistent: info.persistent !== false,
|
|
3089
|
+
...kept ? { media: kept } : {}
|
|
3090
|
+
};
|
|
3091
|
+
}
|
|
3092
|
+
RelayInfoUtil2.parse = parse;
|
|
3093
|
+
})(RelayInfoUtil || (RelayInfoUtil = {}));
|
|
3094
|
+
|
|
3095
|
+
// web-src/pictures-host.ts
|
|
3096
|
+
var DAY_MS2 = 24 * 60 * 60 * 1000;
|
|
3097
|
+
var MEDIA_ASK_MS = 10 * 60000;
|
|
3098
|
+
var said = new Map;
|
|
3099
|
+
var RELAY_ROOM_BLOBS = {
|
|
3100
|
+
media: async (relay) => {
|
|
3101
|
+
const known = said.get(relay);
|
|
3102
|
+
if (known && Date.now() - known.at < MEDIA_ASK_MS) {
|
|
3103
|
+
return known.media;
|
|
3104
|
+
}
|
|
3105
|
+
const probe = await RelayInfoUtil.probe(relay);
|
|
3106
|
+
said.set(relay, { at: Date.now(), ...probe.info?.media ? { media: probe.info.media } : {} });
|
|
3107
|
+
return probe.info?.media;
|
|
2767
3108
|
},
|
|
2768
|
-
{
|
|
2769
|
-
|
|
2770
|
-
|
|
2771
|
-
|
|
2772
|
-
|
|
2773
|
-
|
|
2774
|
-
|
|
2775
|
-
|
|
2776
|
-
|
|
2777
|
-
|
|
2778
|
-
|
|
2779
|
-
|
|
2780
|
-
|
|
2781
|
-
|
|
2782
|
-
what: "The size of the text of conversations, on top of fontSize.",
|
|
2783
|
-
title: words("Text size of conversations", "对话字体大小", "對話字型大小", "会話の文字サイズ"),
|
|
2784
|
-
shown: FONT_SHOWN
|
|
2785
|
-
},
|
|
2786
|
-
{
|
|
2787
|
-
key: "speak",
|
|
2788
|
-
type: "boolean",
|
|
2789
|
-
default: false,
|
|
2790
|
-
what: "Read every reply aloud.",
|
|
2791
|
-
title: words("Read replies aloud", "朗读回复", "朗讀回覆", "返信を読み上げ")
|
|
2792
|
-
},
|
|
2793
|
-
{
|
|
2794
|
-
key: "speakTalk",
|
|
2795
|
-
type: "boolean",
|
|
2796
|
-
default: false,
|
|
2797
|
-
what: "Read the reply aloud when the user spoke to the double by voice.",
|
|
2798
|
-
title: words("Read aloud what was asked by voice", "语音提问时朗读回复", "語音提問時朗讀回覆", "音声で聞いたら読み上げ")
|
|
2799
|
-
},
|
|
2800
|
-
{
|
|
2801
|
-
key: "callCaptions",
|
|
2802
|
-
type: "boolean",
|
|
2803
|
-
default: false,
|
|
2804
|
-
what: "Show captions in a call: what the user says and what Ta reads.",
|
|
2805
|
-
title: words("Call captions", "通话字幕", "通話字幕", "通話の字幕")
|
|
2806
|
-
},
|
|
2807
|
-
{
|
|
2808
|
-
key: "voice",
|
|
2809
|
-
type: "string",
|
|
2810
|
-
default: "",
|
|
2811
|
-
what: "The voice replies are read with (a voiceURI of the device's voices); empty for the most natural one.",
|
|
2812
|
-
title: words("Voice", "朗读声音", "朗讀聲音", "読み上げの声")
|
|
2813
|
-
},
|
|
2814
|
-
{
|
|
2815
|
-
key: "confirmTasks",
|
|
2816
|
-
type: "boolean",
|
|
2817
|
-
default: false,
|
|
2818
|
-
what: "The double asks before it hands out work.",
|
|
2819
|
-
title: words("Ask before handing out work", "派任务前先问我", "派任務前先問我", "作業を割り当てる前に確認")
|
|
3109
|
+
put: async (relay, blob, token, sealed) => {
|
|
3110
|
+
const response = await fetch(`${relay}${RELAY_MEDIA_PATH}${blob.id}`, {
|
|
3111
|
+
method: "PUT",
|
|
3112
|
+
headers: {
|
|
3113
|
+
authorization: `Bearer ${token}`,
|
|
3114
|
+
"x-ta-auth-hash": RoomPicturesUtil.hash(blob.read),
|
|
3115
|
+
"x-ta-expires": String(blob.until),
|
|
3116
|
+
"content-type": "application/octet-stream"
|
|
3117
|
+
},
|
|
3118
|
+
body: sealed
|
|
3119
|
+
});
|
|
3120
|
+
if (!response.ok) {
|
|
3121
|
+
throw new Error(`The relay did not keep the copy: ${response.status}`);
|
|
3122
|
+
}
|
|
2820
3123
|
},
|
|
2821
|
-
{
|
|
2822
|
-
|
|
2823
|
-
|
|
2824
|
-
values: [0, 2, 4, 8],
|
|
2825
|
-
default: 0,
|
|
2826
|
-
what: "How many of the double's tasks run at once; 0 for as many as it starts.",
|
|
2827
|
-
title: words("Tasks at once", "同时运行的任务", "同時執行的任務", "同時に実行するタスク")
|
|
3124
|
+
get: async (relay, blob) => {
|
|
3125
|
+
const response = await fetch(`${relay}${RELAY_MEDIA_PATH}${blob.id}`, { headers: { "x-ta-auth": blob.read } });
|
|
3126
|
+
return response.ok ? new Uint8Array(await response.arrayBuffer()) : undefined;
|
|
2828
3127
|
},
|
|
2829
|
-
{
|
|
2830
|
-
|
|
2831
|
-
|
|
2832
|
-
|
|
2833
|
-
|
|
2834
|
-
|
|
2835
|
-
|
|
2836
|
-
|
|
2837
|
-
|
|
2838
|
-
|
|
2839
|
-
|
|
2840
|
-
|
|
2841
|
-
|
|
2842
|
-
|
|
2843
|
-
|
|
2844
|
-
|
|
2845
|
-
|
|
2846
|
-
|
|
2847
|
-
|
|
2848
|
-
|
|
2849
|
-
|
|
2850
|
-
|
|
2851
|
-
|
|
2852
|
-
|
|
2853
|
-
|
|
2854
|
-
|
|
2855
|
-
|
|
2856
|
-
|
|
2857
|
-
|
|
2858
|
-
|
|
2859
|
-
|
|
2860
|
-
|
|
2861
|
-
|
|
2862
|
-
|
|
2863
|
-
|
|
2864
|
-
|
|
3128
|
+
remove: async (relay, id, token) => {
|
|
3129
|
+
await fetch(`${relay}${RELAY_MEDIA_PATH}${id}`, { method: "DELETE", headers: { authorization: `Bearer ${token}` } });
|
|
3130
|
+
}
|
|
3131
|
+
};
|
|
3132
|
+
var DEFAULTS2 = {
|
|
3133
|
+
now: () => Date.now(),
|
|
3134
|
+
paceMs: 60,
|
|
3135
|
+
keptBytes: 2 * 1024 * 1024 * 1024,
|
|
3136
|
+
unseenMs: 30 * DAY_MS2,
|
|
3137
|
+
blobs: RELAY_ROOM_BLOBS
|
|
3138
|
+
};
|
|
3139
|
+
var PICTURE_PART_CHARS = 48 * 1024;
|
|
3140
|
+
var ASK_AGAIN_MS = 30000;
|
|
3141
|
+
var INCOMING_MS = 2 * 60000;
|
|
3142
|
+
var SERVES_A_MINUTE = 60;
|
|
3143
|
+
var SWEEP_MS2 = 60 * 60000;
|
|
3144
|
+
var ROOM_ID2 = /^[A-Za-z0-9_-]{1,80}$/;
|
|
3145
|
+
var PICTURE_ID = /^[a-f0-9]{24}$/;
|
|
3146
|
+
var EXTENSIONS = { "image/jpeg": "jpg", "image/png": "png", "image/gif": "gif", "image/webp": "webp" };
|
|
3147
|
+
var WANT2 = "ta-rooms:picture?";
|
|
3148
|
+
var PART2 = "ta-rooms:picture";
|
|
3149
|
+
var GOT = "ta-rooms:picture!";
|
|
3150
|
+
var SENT2 = ".sent.json";
|
|
3151
|
+
var LOOK_BACK_PAGES2 = 10;
|
|
3152
|
+
var RoomPicturesUtil;
|
|
3153
|
+
((RoomPicturesUtil) => {
|
|
3154
|
+
function is(bytes, type) {
|
|
3155
|
+
return TaImageSize.type(bytes) === type;
|
|
3156
|
+
}
|
|
3157
|
+
RoomPicturesUtil.is = is;
|
|
3158
|
+
function sha(bytes) {
|
|
3159
|
+
return createHash2("sha256").update(bytes).digest("hex");
|
|
3160
|
+
}
|
|
3161
|
+
RoomPicturesUtil.sha = sha;
|
|
3162
|
+
function parts(data) {
|
|
3163
|
+
const parts2 = [];
|
|
3164
|
+
for (let at = 0;at < data.length; at += PICTURE_PART_CHARS) {
|
|
3165
|
+
parts2.push(data.slice(at, at + PICTURE_PART_CHARS));
|
|
3166
|
+
}
|
|
3167
|
+
return parts2;
|
|
3168
|
+
}
|
|
3169
|
+
RoomPicturesUtil.parts = parts;
|
|
3170
|
+
function hash(text) {
|
|
3171
|
+
return createHash2("sha256").update(text).digest("base64url");
|
|
3172
|
+
}
|
|
3173
|
+
RoomPicturesUtil.hash = hash;
|
|
3174
|
+
function seal(key, bytes) {
|
|
3175
|
+
const iv = randomBytes2(12);
|
|
3176
|
+
const cipher = createCipheriv("aes-256-gcm", Buffer.from(key, "base64url"), iv);
|
|
3177
|
+
return new Uint8Array(Buffer.concat([iv, cipher.update(bytes), cipher.final(), cipher.getAuthTag()]));
|
|
3178
|
+
}
|
|
3179
|
+
RoomPicturesUtil.seal = seal;
|
|
3180
|
+
function open2(key, sealed) {
|
|
3181
|
+
try {
|
|
3182
|
+
const bytes = Buffer.from(sealed);
|
|
3183
|
+
const decipher = createDecipheriv("aes-256-gcm", Buffer.from(key, "base64url"), bytes.subarray(0, 12));
|
|
3184
|
+
decipher.setAuthTag(bytes.subarray(bytes.length - 16));
|
|
3185
|
+
return new Uint8Array(Buffer.concat([decipher.update(bytes.subarray(12, bytes.length - 16)), decipher.final()]));
|
|
3186
|
+
} catch {
|
|
3187
|
+
return;
|
|
2865
3188
|
}
|
|
2866
|
-
},
|
|
2867
|
-
{
|
|
2868
|
-
key: "keepAliveMinutes",
|
|
2869
|
-
type: "number",
|
|
2870
|
-
values: [1, 5, 15, 30],
|
|
2871
|
-
default: 5,
|
|
2872
|
-
what: "How long an extension started on open keeps running once left, in minutes.",
|
|
2873
|
-
title: words("Keep extensions running", "扩展离开后保留", "擴充功能離開後保留", "拡張機能を保持")
|
|
2874
|
-
},
|
|
2875
|
-
{
|
|
2876
|
-
key: "chatFoldDays",
|
|
2877
|
-
type: "number",
|
|
2878
|
-
values: [3, 7, 30, 0],
|
|
2879
|
-
default: 7,
|
|
2880
|
-
what: "Conversations not updated for these days fold at the end of the list; 0 for none.",
|
|
2881
|
-
title: words("Fold old conversations", "折叠旧对话", "摺疊舊對話", "古い会話を折りたたむ")
|
|
2882
|
-
},
|
|
2883
|
-
{
|
|
2884
|
-
key: "chatArchiveDays",
|
|
2885
|
-
type: "number",
|
|
2886
|
-
values: [30, 90, 365, 0],
|
|
2887
|
-
default: 30,
|
|
2888
|
-
what: "Conversations not updated for these days are archived (out of the list, found by search); 0 for none.",
|
|
2889
|
-
title: words("Archive old conversations", "归档旧对话", "歸檔舊對話", "古い会話をアーカイブ")
|
|
2890
|
-
},
|
|
2891
|
-
{
|
|
2892
|
-
key: "recentShots",
|
|
2893
|
-
type: "boolean",
|
|
2894
|
-
default: true,
|
|
2895
|
-
what: "The screenshot just taken is offered above the message box, once.",
|
|
2896
|
-
title: words("Offer recent screenshots", "提示刚截的图", "提示剛截的圖", "撮ったばかりのスクリーンショットを提案")
|
|
2897
3189
|
}
|
|
2898
|
-
|
|
2899
|
-
|
|
2900
|
-
(
|
|
2901
|
-
|
|
2902
|
-
|
|
2903
|
-
|
|
2904
|
-
|
|
2905
|
-
return typeof value === "boolean" ? undefined : "true or false";
|
|
2906
|
-
case "string":
|
|
2907
|
-
return typeof value === "string" ? undefined : "a string";
|
|
2908
|
-
case "model":
|
|
2909
|
-
return value === "" || typeof value === "string" && /^[^/\s]+\/\S+$/.test(value) ? undefined : "<provider>/<model id>, or empty";
|
|
2910
|
-
default:
|
|
2911
|
-
return ((_a = spec.values) === null || _a === undefined ? undefined : _a.includes(value)) ? undefined : `one of ${(_b = spec.values) === null || _b === undefined ? undefined : _b.map((one) => JSON.stringify(one)).join(", ")}`;
|
|
3190
|
+
RoomPicturesUtil.open = open2;
|
|
3191
|
+
function find(lines, id) {
|
|
3192
|
+
for (const line of lines) {
|
|
3193
|
+
const image = line.images?.find((each) => each.id === id);
|
|
3194
|
+
if (image) {
|
|
3195
|
+
return { line, image };
|
|
3196
|
+
}
|
|
2912
3197
|
}
|
|
3198
|
+
return;
|
|
2913
3199
|
}
|
|
2914
|
-
|
|
2915
|
-
|
|
2916
|
-
|
|
2917
|
-
|
|
2918
|
-
|
|
2919
|
-
|
|
3200
|
+
RoomPicturesUtil.find = find;
|
|
3201
|
+
})(RoomPicturesUtil ||= {});
|
|
3202
|
+
|
|
3203
|
+
class RoomPictures {
|
|
3204
|
+
doubles;
|
|
3205
|
+
options;
|
|
3206
|
+
asked = new Map;
|
|
3207
|
+
incoming = new Map;
|
|
3208
|
+
served = new Map;
|
|
3209
|
+
waiting = new Map;
|
|
3210
|
+
fetched = new Map;
|
|
3211
|
+
serving = Promise.resolve();
|
|
3212
|
+
sentChain = Promise.resolve();
|
|
3213
|
+
timer;
|
|
3214
|
+
constructor(doubles, options = {}) {
|
|
3215
|
+
this.doubles = doubles;
|
|
3216
|
+
this.options = { ...DEFAULTS2, ...options };
|
|
3217
|
+
}
|
|
3218
|
+
start() {
|
|
3219
|
+
const sweep = () => void this.sweep().catch((error) => console.error("ta-rooms could not tidy the pictures:", error));
|
|
3220
|
+
setTimeout(sweep, 30000).unref?.();
|
|
3221
|
+
this.timer = setInterval(sweep, SWEEP_MS2);
|
|
3222
|
+
this.timer.unref?.();
|
|
3223
|
+
}
|
|
3224
|
+
dispose() {
|
|
3225
|
+
clearInterval(this.timer);
|
|
3226
|
+
}
|
|
3227
|
+
root() {
|
|
3228
|
+
return join2(this.doubles.host.agentDir, "ta-rooms", "pictures");
|
|
3229
|
+
}
|
|
3230
|
+
file(room, image) {
|
|
3231
|
+
const extension = EXTENSIONS[image.type];
|
|
3232
|
+
return ROOM_ID2.test(room) && PICTURE_ID.test(image.id) && extension ? join2(this.root(), room, `${image.id}.${extension}`) : undefined;
|
|
3233
|
+
}
|
|
3234
|
+
async put(room, data, type) {
|
|
3235
|
+
const info = (await this.doubles.host.rooms.listRooms()).find((each) => each.id === room && each.status === "member");
|
|
3236
|
+
if (!info) {
|
|
3237
|
+
throw new Error("Not in this group chat.");
|
|
2920
3238
|
}
|
|
2921
|
-
|
|
2922
|
-
|
|
3239
|
+
const bytes = new Uint8Array(Buffer.from(String(data), "base64"));
|
|
3240
|
+
if (!bytes.length || bytes.length > RELAY_CHAT_IMAGE_BYTES || !RoomPicturesUtil.is(bytes, type)) {
|
|
3241
|
+
throw new Error("Not a picture, or too large.");
|
|
2923
3242
|
}
|
|
2924
|
-
|
|
2925
|
-
|
|
3243
|
+
const image = { id: randomBytes2(12).toString("hex"), type };
|
|
3244
|
+
const file = this.file(room, image);
|
|
3245
|
+
await mkdir2(join2(this.root(), room), { recursive: true });
|
|
3246
|
+
await writeFile2(file, bytes);
|
|
3247
|
+
const now = this.options.now();
|
|
3248
|
+
const relay = info.relay;
|
|
3249
|
+
const media = relay && this.options.blobs ? await this.options.blobs.media(relay).catch(() => {
|
|
3250
|
+
return;
|
|
3251
|
+
}) : undefined;
|
|
3252
|
+
const blob = media && bytes.length <= media.bytes ? {
|
|
3253
|
+
id: randomBytes2(16).toString("base64url"),
|
|
3254
|
+
read: randomBytes2(32).toString("base64url"),
|
|
3255
|
+
key: randomBytes2(32).toString("base64url"),
|
|
3256
|
+
until: now + media.days * DAY_MS2
|
|
3257
|
+
} : undefined;
|
|
3258
|
+
const token = randomBytes2(32).toString("base64url");
|
|
3259
|
+
await this.changeSent(room, (sent) => {
|
|
3260
|
+
sent[image.id] = { at: now, ...blob ? { blob: { id: blob.id, token, until: blob.until } } : {} };
|
|
3261
|
+
});
|
|
3262
|
+
if (blob && relay && this.options.blobs) {
|
|
3263
|
+
this.options.blobs.put(relay, blob, token, RoomPicturesUtil.seal(blob.key, bytes)).catch((error) => console.error("ta-rooms could not put a copy of a picture on the relay:", error));
|
|
2926
3264
|
}
|
|
2927
|
-
return
|
|
3265
|
+
return { ...image, sha: RoomPicturesUtil.sha(bytes), bytes: bytes.length, ...blob ? { blob } : {} };
|
|
2928
3266
|
}
|
|
2929
|
-
|
|
2930
|
-
|
|
2931
|
-
|
|
2932
|
-
|
|
2933
|
-
// ../ta-protocol/esm/common/deployment-protocol.js
|
|
2934
|
-
var TA_WELL_KNOWN_PATH = "/.well-known/ta";
|
|
2935
|
-
|
|
2936
|
-
class TaDeploymentError extends Error {
|
|
2937
|
-
}
|
|
2938
|
-
var TaDeploymentUtil;
|
|
2939
|
-
(function(TaDeploymentUtil2) {
|
|
2940
|
-
TaDeploymentUtil2.DISTRIBUTION_FILE = "distribution.json";
|
|
2941
|
-
function url(value) {
|
|
2942
|
-
if (typeof value !== "string" || !value.trim()) {
|
|
2943
|
-
return;
|
|
3267
|
+
async get(room, id) {
|
|
3268
|
+
const found = await this.lookup(room, String(id));
|
|
3269
|
+
if (!found) {
|
|
3270
|
+
return "";
|
|
2944
3271
|
}
|
|
2945
|
-
|
|
2946
|
-
|
|
2947
|
-
|
|
2948
|
-
|
|
3272
|
+
const bytes = await this.read(room, found.image) ?? await this.fromRelay(room, found.line, found.image);
|
|
3273
|
+
if (bytes) {
|
|
3274
|
+
return `data:${found.image.type};base64,${Buffer.from(bytes).toString("base64")}`;
|
|
3275
|
+
}
|
|
3276
|
+
await this.want(room, found.line, found.image);
|
|
3277
|
+
return "";
|
|
3278
|
+
}
|
|
3279
|
+
async images(room, line, waitMs = 15000) {
|
|
3280
|
+
const images = [];
|
|
3281
|
+
for (const image of line.images ?? []) {
|
|
3282
|
+
let bytes = await this.read(room, image) ?? await this.fromRelay(room, line, image);
|
|
3283
|
+
if (!bytes) {
|
|
3284
|
+
const came = new Promise((resolve) => {
|
|
3285
|
+
const key = `${room}:${image.id}`;
|
|
3286
|
+
this.waiting.set(key, [...this.waiting.get(key) ?? [], resolve]);
|
|
3287
|
+
setTimeout(resolve, waitMs).unref?.();
|
|
3288
|
+
});
|
|
3289
|
+
await this.want(room, line, image);
|
|
3290
|
+
await came;
|
|
3291
|
+
bytes = await this.read(room, image);
|
|
3292
|
+
}
|
|
3293
|
+
if (bytes) {
|
|
3294
|
+
images.push({ type: "image", data: Buffer.from(bytes).toString("base64"), mimeType: image.type });
|
|
2949
3295
|
}
|
|
2950
|
-
return `${parsed.origin}${parsed.pathname}`.replace(/\/+$/, "");
|
|
2951
|
-
} catch (_a) {
|
|
2952
|
-
return;
|
|
2953
3296
|
}
|
|
3297
|
+
return images;
|
|
2954
3298
|
}
|
|
2955
|
-
|
|
2956
|
-
|
|
2957
|
-
|
|
2958
|
-
|
|
2959
|
-
|
|
2960
|
-
|
|
2961
|
-
|
|
2962
|
-
|
|
2963
|
-
const called = name(env.TA_DEPLOYMENT_NAME);
|
|
2964
|
-
return { v: 1, ...called ? { name: called } : {}, ...gateway ? { gateway } : {}, ...relay ? { relay } : {}, ...version ? { version } : {} };
|
|
3299
|
+
heard(event) {
|
|
3300
|
+
const data = event.data;
|
|
3301
|
+
if (data?.t !== WANT2 && data?.t !== PART2 && data?.t !== GOT) {
|
|
3302
|
+
return false;
|
|
3303
|
+
}
|
|
3304
|
+
const done = data.t === WANT2 ? this.asks(event) : data.t === PART2 ? this.part(event) : this.gotBy(event);
|
|
3305
|
+
done.catch((error) => console.error("ta-rooms: a picture could not be passed on:", error));
|
|
3306
|
+
return true;
|
|
2965
3307
|
}
|
|
2966
|
-
|
|
2967
|
-
|
|
2968
|
-
|
|
2969
|
-
|
|
2970
|
-
|
|
2971
|
-
|
|
2972
|
-
function parse(value) {
|
|
2973
|
-
const given = value;
|
|
2974
|
-
if (!given || typeof given !== "object" || given.v !== 1) {
|
|
3308
|
+
async read(room, image) {
|
|
3309
|
+
const file = this.file(room, image);
|
|
3310
|
+
const bytes = file ? await readFile2(file).catch(() => {
|
|
3311
|
+
return;
|
|
3312
|
+
}) : undefined;
|
|
3313
|
+
if (!file || !bytes || bytes.length !== image.bytes) {
|
|
2975
3314
|
return;
|
|
2976
3315
|
}
|
|
2977
|
-
const
|
|
2978
|
-
|
|
2979
|
-
|
|
3316
|
+
const now = new Date(this.options.now());
|
|
3317
|
+
await utimes2(file, now, now).catch(() => {
|
|
3318
|
+
return;
|
|
3319
|
+
});
|
|
3320
|
+
return new Uint8Array(bytes);
|
|
2980
3321
|
}
|
|
2981
|
-
|
|
2982
|
-
|
|
2983
|
-
const
|
|
2984
|
-
|
|
2985
|
-
|
|
3322
|
+
async lookup(room, id) {
|
|
3323
|
+
const lines = (await this.doubles.chatOf(room)).lines;
|
|
3324
|
+
const found = RoomPicturesUtil.find(lines, id);
|
|
3325
|
+
const read = this.doubles.host.rooms.readChat;
|
|
3326
|
+
if (found || !read || !PICTURE_ID.test(id)) {
|
|
3327
|
+
return found;
|
|
2986
3328
|
}
|
|
2987
|
-
|
|
2988
|
-
|
|
2989
|
-
|
|
2990
|
-
const response = await fetch(`${base}${TA_WELL_KNOWN_PATH}`, { signal: controller.signal, redirect: "follow" });
|
|
2991
|
-
if (!response.ok) {
|
|
2992
|
-
throw new TaDeploymentError(`${base} is not a deployment of Ta (${response.status}).`);
|
|
2993
|
-
}
|
|
2994
|
-
const deployment = parse(await response.json().catch(() => {
|
|
3329
|
+
let before = lines[0]?.id;
|
|
3330
|
+
for (let page = 0;page < LOOK_BACK_PAGES2 && before; page++) {
|
|
3331
|
+
const older = await read.call(this.doubles.host.rooms, room, { before, limit: 500 }).catch(() => {
|
|
2995
3332
|
return;
|
|
2996
|
-
})
|
|
2997
|
-
|
|
2998
|
-
|
|
2999
|
-
|
|
3000
|
-
return deployment;
|
|
3001
|
-
} catch (error) {
|
|
3002
|
-
if ((error === null || error === undefined ? undefined : error.name) === "AbortError") {
|
|
3003
|
-
throw new TaDeploymentError(`${base} did not answer in time.`);
|
|
3333
|
+
});
|
|
3334
|
+
const hit = older && RoomPicturesUtil.find(older.lines, id);
|
|
3335
|
+
if (hit || !older?.more) {
|
|
3336
|
+
return hit;
|
|
3004
3337
|
}
|
|
3005
|
-
|
|
3006
|
-
} finally {
|
|
3007
|
-
clearTimeout(timer);
|
|
3338
|
+
before = older.lines[0]?.id;
|
|
3008
3339
|
}
|
|
3340
|
+
return;
|
|
3009
3341
|
}
|
|
3010
|
-
|
|
3011
|
-
|
|
3012
|
-
|
|
3013
|
-
|
|
3014
|
-
|
|
3015
|
-
|
|
3016
|
-
|
|
3017
|
-
if (given.name !== undefined) {
|
|
3018
|
-
result.name = name(given.name);
|
|
3342
|
+
async fromRelay(room, line, image) {
|
|
3343
|
+
const blob = image.blob;
|
|
3344
|
+
const blobs = this.options.blobs;
|
|
3345
|
+
const key = `${room}:${image.id}`;
|
|
3346
|
+
const now = this.options.now();
|
|
3347
|
+
if (!blob || !blobs || blob.until <= now || now - (this.fetched.get(key) ?? 0) < ASK_AGAIN_MS) {
|
|
3348
|
+
return;
|
|
3019
3349
|
}
|
|
3020
|
-
|
|
3021
|
-
|
|
3022
|
-
|
|
3023
|
-
|
|
3024
|
-
|
|
3025
|
-
|
|
3026
|
-
|
|
3027
|
-
|
|
3350
|
+
this.fetched.set(key, now);
|
|
3351
|
+
const relay = (await this.doubles.host.rooms.listRooms()).find((each) => each.id === room && each.status === "member")?.relay;
|
|
3352
|
+
const sealed = relay ? await blobs.get(relay, blob).catch(() => {
|
|
3353
|
+
return;
|
|
3354
|
+
}) : undefined;
|
|
3355
|
+
const bytes = sealed && RoomPicturesUtil.open(blob.key, sealed);
|
|
3356
|
+
if (!bytes || !await this.keep(room, line, image, bytes)) {
|
|
3357
|
+
return;
|
|
3028
3358
|
}
|
|
3029
|
-
|
|
3030
|
-
|
|
3031
|
-
|
|
3032
|
-
|
|
3033
|
-
|
|
3034
|
-
|
|
3035
|
-
|
|
3036
|
-
if ((updates === null || updates === undefined ? undefined : updates.key) !== undefined && !key) {
|
|
3037
|
-
throw new TaDeploymentError(`"updates.key" of ${TaDeploymentUtil2.DISTRIBUTION_FILE} is a public key in PEM.`);
|
|
3038
|
-
}
|
|
3039
|
-
result.updates = { ...base ? { base } : {}, ...key ? { key } : {} };
|
|
3359
|
+
this.fetched.delete(key);
|
|
3360
|
+
return bytes;
|
|
3361
|
+
}
|
|
3362
|
+
async keep(room, line, image, bytes) {
|
|
3363
|
+
const file = this.file(room, image);
|
|
3364
|
+
if (!file || bytes.length !== image.bytes || RoomPicturesUtil.sha(bytes) !== image.sha || !RoomPicturesUtil.is(bytes, image.type)) {
|
|
3365
|
+
return false;
|
|
3040
3366
|
}
|
|
3041
|
-
|
|
3042
|
-
|
|
3367
|
+
await mkdir2(join2(this.root(), room), { recursive: true });
|
|
3368
|
+
await writeFile2(file, bytes);
|
|
3369
|
+
const key = `${room}:${image.id}`;
|
|
3370
|
+
this.asked.delete(key);
|
|
3371
|
+
for (const resolve of this.waiting.get(key) ?? []) {
|
|
3372
|
+
resolve();
|
|
3043
3373
|
}
|
|
3044
|
-
|
|
3045
|
-
|
|
3046
|
-
|
|
3047
|
-
|
|
3048
|
-
}
|
|
3049
|
-
result.inside = inside;
|
|
3374
|
+
this.waiting.delete(key);
|
|
3375
|
+
if (image.blob) {
|
|
3376
|
+
await this.doubles.host.rooms.send(room, { t: GOT, id: image.id }, line.from).catch(() => {
|
|
3377
|
+
return;
|
|
3378
|
+
});
|
|
3050
3379
|
}
|
|
3051
|
-
|
|
3380
|
+
await this.doubles.set(DoubleKeys.PICTURES, this.options.now());
|
|
3381
|
+
return true;
|
|
3052
3382
|
}
|
|
3053
|
-
|
|
3054
|
-
|
|
3055
|
-
const
|
|
3056
|
-
const
|
|
3057
|
-
|
|
3058
|
-
|
|
3059
|
-
if (typeof text !== "string" || !text.trim()) {
|
|
3060
|
-
throw new TaDeploymentError(`"reach.${language2}" of ${TaDeploymentUtil2.DISTRIBUTION_FILE} is not a sentence.`);
|
|
3061
|
-
}
|
|
3062
|
-
result[language2] = text.trim().slice(0, 200);
|
|
3383
|
+
async want(room, line, image) {
|
|
3384
|
+
const key = `${room}:${image.id}`;
|
|
3385
|
+
const now = this.options.now();
|
|
3386
|
+
const before = this.asked.get(key);
|
|
3387
|
+
if (before && now - before.at < ASK_AGAIN_MS) {
|
|
3388
|
+
return;
|
|
3063
3389
|
}
|
|
3064
|
-
|
|
3065
|
-
|
|
3390
|
+
const info = (await this.doubles.host.rooms.listRooms()).find((each) => each.id === room && each.status === "member");
|
|
3391
|
+
const online = info?.members.filter((member) => member.online && !member.self && !member.guest) ?? [];
|
|
3392
|
+
const sender = online.find((member) => member.member === line.from);
|
|
3393
|
+
const others = online.filter((member) => member !== sender);
|
|
3394
|
+
const tries = before ? before.tries + 1 : 0;
|
|
3395
|
+
const asked = sender && tries % (others.length + 1) === 0 ? sender : others[(tries - (sender ? 1 : 0)) % Math.max(others.length, 1)] ?? sender;
|
|
3396
|
+
if (!asked) {
|
|
3397
|
+
return;
|
|
3066
3398
|
}
|
|
3067
|
-
|
|
3399
|
+
this.asked.set(key, { at: now, from: asked.member, tries });
|
|
3400
|
+
await this.doubles.host.rooms.send(room, { t: WANT2, id: image.id }, asked.member);
|
|
3068
3401
|
}
|
|
3069
|
-
|
|
3070
|
-
|
|
3071
|
-
|
|
3402
|
+
async asks(event) {
|
|
3403
|
+
const id = String(event.data.id ?? "");
|
|
3404
|
+
const info = (await this.doubles.host.rooms.listRooms()).find((each) => each.id === event.room && each.status === "member");
|
|
3405
|
+
const asker = info?.members.find((member) => member.member === event.from);
|
|
3406
|
+
if (!info || !asker || asker.self || asker.guest || !PICTURE_ID.test(id)) {
|
|
3072
3407
|
return;
|
|
3073
3408
|
}
|
|
3074
|
-
const
|
|
3075
|
-
const
|
|
3076
|
-
|
|
3077
|
-
|
|
3078
|
-
|
|
3079
|
-
|
|
3080
|
-
|
|
3081
|
-
|
|
3082
|
-
|
|
3083
|
-
|
|
3409
|
+
const now = this.options.now();
|
|
3410
|
+
const key = `${event.room}:${event.from}`;
|
|
3411
|
+
const recent = (this.served.get(key) ?? []).filter((at) => now - at < 60000);
|
|
3412
|
+
if (recent.length >= SERVES_A_MINUTE) {
|
|
3413
|
+
return;
|
|
3414
|
+
}
|
|
3415
|
+
this.served.set(key, [...recent, now]);
|
|
3416
|
+
const found = await this.lookup(event.room, id);
|
|
3417
|
+
const bytes = found && await this.read(event.room, found.image);
|
|
3418
|
+
if (!bytes) {
|
|
3419
|
+
return;
|
|
3420
|
+
}
|
|
3421
|
+
const parts = RoomPicturesUtil.parts(Buffer.from(bytes).toString("base64"));
|
|
3422
|
+
this.serving = this.serving.then(async () => {
|
|
3423
|
+
for (const [part, d] of parts.entries()) {
|
|
3424
|
+
await this.doubles.host.rooms.send(event.room, { t: PART2, id, part, parts: parts.length, d }, event.from);
|
|
3425
|
+
if (this.options.paceMs) {
|
|
3426
|
+
await new Promise((resolve) => setTimeout(resolve, this.options.paceMs));
|
|
3427
|
+
}
|
|
3084
3428
|
}
|
|
3429
|
+
}).catch((error) => console.error("ta-rooms could not send a picture:", error));
|
|
3430
|
+
}
|
|
3431
|
+
async part(event) {
|
|
3432
|
+
const { id, part, parts, d } = event.data;
|
|
3433
|
+
const key = `${event.room}:${String(id)}`;
|
|
3434
|
+
const asked = this.asked.get(key);
|
|
3435
|
+
const most = Math.ceil(RELAY_CHAT_IMAGE_BYTES * 4 / 3 / PICTURE_PART_CHARS) + 1;
|
|
3436
|
+
if (asked?.from !== event.from || !Number.isInteger(parts) || !Number.isInteger(part) || parts < 1 || parts > most || part < 0 || part >= parts || typeof d !== "string" || d.length > PICTURE_PART_CHARS) {
|
|
3437
|
+
return;
|
|
3085
3438
|
}
|
|
3086
|
-
|
|
3087
|
-
|
|
3088
|
-
|
|
3089
|
-
|
|
3090
|
-
|
|
3091
|
-
|
|
3092
|
-
|
|
3093
|
-
|
|
3094
|
-
|
|
3095
|
-
|
|
3096
|
-
|
|
3097
|
-
|
|
3098
|
-
|
|
3099
|
-
|
|
3100
|
-
})(TaErrorUtil || (TaErrorUtil = {}));
|
|
3101
|
-
// ../ta-protocol/esm/common/gateway-protocol.js
|
|
3102
|
-
var TaGatewayServer = Symbol("TaGatewayServer");
|
|
3103
|
-
// ../ta-protocol/esm/common/location.js
|
|
3104
|
-
var TaLocation;
|
|
3105
|
-
(function(TaLocation2) {
|
|
3106
|
-
TaLocation2.SSH = "ssh://";
|
|
3107
|
-
TaLocation2.CLOUD = "ta://";
|
|
3108
|
-
const CLOUD_NAME = /^[a-z0-9][a-z0-9-]{0,39}$/;
|
|
3109
|
-
const HOST = /^(?!-)[\w.@:%+[\]-]+$/;
|
|
3110
|
-
function isRemote(location) {
|
|
3111
|
-
return location.startsWith(TaLocation2.SSH) || location.startsWith(TaLocation2.CLOUD);
|
|
3112
|
-
}
|
|
3113
|
-
TaLocation2.isRemote = isRemote;
|
|
3114
|
-
function isCloud(host2) {
|
|
3115
|
-
return !!host2 && host2.startsWith(TaLocation2.CLOUD);
|
|
3116
|
-
}
|
|
3117
|
-
TaLocation2.isCloud = isCloud;
|
|
3118
|
-
function isCloudName(name) {
|
|
3119
|
-
return CLOUD_NAME.test(name);
|
|
3120
|
-
}
|
|
3121
|
-
TaLocation2.isCloudName = isCloudName;
|
|
3122
|
-
function cloud(name, path2 = "") {
|
|
3123
|
-
return `${TaLocation2.CLOUD}${name}${path2 ? normalize(path2) : ""}`;
|
|
3124
|
-
}
|
|
3125
|
-
TaLocation2.cloud = cloud;
|
|
3126
|
-
function cloudName(hostOrLocation) {
|
|
3127
|
-
if (!hostOrLocation.startsWith(TaLocation2.CLOUD)) {
|
|
3439
|
+
const now = this.options.now();
|
|
3440
|
+
for (const [other, coming2] of this.incoming) {
|
|
3441
|
+
if (now - coming2.at > INCOMING_MS) {
|
|
3442
|
+
this.incoming.delete(other);
|
|
3443
|
+
}
|
|
3444
|
+
}
|
|
3445
|
+
const coming = this.incoming.get(key) ?? { parts: [], at: now };
|
|
3446
|
+
if (coming.parts.length !== parts) {
|
|
3447
|
+
coming.parts = new Array(parts).fill("");
|
|
3448
|
+
}
|
|
3449
|
+
coming.parts[part] = d;
|
|
3450
|
+
coming.at = now;
|
|
3451
|
+
this.incoming.set(key, coming);
|
|
3452
|
+
if (coming.parts.some((each) => !each)) {
|
|
3128
3453
|
return;
|
|
3129
3454
|
}
|
|
3130
|
-
|
|
3131
|
-
const
|
|
3132
|
-
|
|
3455
|
+
this.incoming.delete(key);
|
|
3456
|
+
const found = await this.lookup(event.room, String(id));
|
|
3457
|
+
if (found) {
|
|
3458
|
+
await this.keep(event.room, found.line, found.image, new Uint8Array(Buffer.from(coming.parts.join(""), "base64")));
|
|
3459
|
+
}
|
|
3133
3460
|
}
|
|
3134
|
-
|
|
3135
|
-
|
|
3136
|
-
|
|
3137
|
-
if (!
|
|
3461
|
+
async gotBy(event) {
|
|
3462
|
+
const id = String(event.data.id ?? "");
|
|
3463
|
+
const info = (await this.doubles.host.rooms.listRooms()).find((each) => each.id === event.room && each.status === "member");
|
|
3464
|
+
if (!info?.members.some((member) => member.member === event.from && !member.self) || !PICTURE_ID.test(id)) {
|
|
3138
3465
|
return;
|
|
3139
3466
|
}
|
|
3140
|
-
|
|
3141
|
-
|
|
3142
|
-
|
|
3143
|
-
|
|
3467
|
+
let gone;
|
|
3468
|
+
await this.changeSent(event.room, (sent) => {
|
|
3469
|
+
const entry = sent[id];
|
|
3470
|
+
if (!entry?.blob) {
|
|
3471
|
+
return;
|
|
3144
3472
|
}
|
|
3145
|
-
|
|
3146
|
-
|
|
3473
|
+
entry.got = [...new Set([...entry.got ?? [], event.from])];
|
|
3474
|
+
const others = info.members.filter((member) => !member.self && !member.guest).map((member) => member.member);
|
|
3475
|
+
if (others.every((member) => entry.got?.includes(member))) {
|
|
3476
|
+
gone = entry.blob;
|
|
3477
|
+
delete entry.blob;
|
|
3478
|
+
delete entry.got;
|
|
3479
|
+
}
|
|
3480
|
+
});
|
|
3481
|
+
if (gone && info.relay && this.options.blobs) {
|
|
3482
|
+
await this.options.blobs.remove(info.relay, gone.id, gone.token).catch(() => {
|
|
3483
|
+
return;
|
|
3484
|
+
});
|
|
3147
3485
|
}
|
|
3148
|
-
|
|
3149
|
-
|
|
3150
|
-
|
|
3151
|
-
|
|
3152
|
-
|
|
3153
|
-
|
|
3486
|
+
}
|
|
3487
|
+
async readSent(room) {
|
|
3488
|
+
try {
|
|
3489
|
+
const sent = JSON.parse(await readFile2(join2(this.root(), room, SENT2), "utf8"));
|
|
3490
|
+
return sent && typeof sent === "object" && !Array.isArray(sent) ? sent : {};
|
|
3491
|
+
} catch {
|
|
3492
|
+
return {};
|
|
3154
3493
|
}
|
|
3155
|
-
return { host: host2, path: path2 };
|
|
3156
3494
|
}
|
|
3157
|
-
|
|
3158
|
-
|
|
3159
|
-
|
|
3495
|
+
changeSent(room, change) {
|
|
3496
|
+
const done = this.sentChain.then(async () => {
|
|
3497
|
+
const sent = await this.readSent(room);
|
|
3498
|
+
change(sent);
|
|
3499
|
+
await mkdir2(join2(this.root(), room), { recursive: true });
|
|
3500
|
+
await writeFile2(join2(this.root(), room, SENT2), JSON.stringify(sent), { mode: 384 });
|
|
3501
|
+
});
|
|
3502
|
+
this.sentChain = done.catch(() => {
|
|
3503
|
+
return;
|
|
3504
|
+
});
|
|
3505
|
+
return done;
|
|
3160
3506
|
}
|
|
3161
|
-
|
|
3162
|
-
|
|
3163
|
-
|
|
3164
|
-
const
|
|
3165
|
-
|
|
3166
|
-
|
|
3167
|
-
|
|
3168
|
-
|
|
3169
|
-
|
|
3170
|
-
|
|
3171
|
-
|
|
3172
|
-
|
|
3173
|
-
|
|
3174
|
-
|
|
3175
|
-
|
|
3176
|
-
|
|
3177
|
-
|
|
3178
|
-
|
|
3507
|
+
async sweep() {
|
|
3508
|
+
const rooms = new Map((await this.doubles.host.rooms.listRooms()).filter((room) => room.status === "member").map((room) => [room.id, room]));
|
|
3509
|
+
const now = this.options.now();
|
|
3510
|
+
const others = [];
|
|
3511
|
+
for (const room of await readdir2(this.root()).catch(() => [])) {
|
|
3512
|
+
const dir = join2(this.root(), room);
|
|
3513
|
+
if (!rooms.has(room) || !ROOM_ID2.test(room)) {
|
|
3514
|
+
await rm2(dir, { recursive: true, force: true });
|
|
3515
|
+
continue;
|
|
3516
|
+
}
|
|
3517
|
+
const sent = await this.readSent(room);
|
|
3518
|
+
if (Object.values(sent).some((entry) => entry.blob && entry.blob.until <= now)) {
|
|
3519
|
+
await this.changeSent(room, (all) => {
|
|
3520
|
+
for (const entry of Object.values(all)) {
|
|
3521
|
+
if (entry.blob && entry.blob.until <= now) {
|
|
3522
|
+
delete entry.blob;
|
|
3523
|
+
delete entry.got;
|
|
3524
|
+
}
|
|
3525
|
+
}
|
|
3526
|
+
});
|
|
3527
|
+
}
|
|
3528
|
+
for (const name of await readdir2(dir).catch(() => [])) {
|
|
3529
|
+
const id = name.replace(/\.[a-z]+$/, "");
|
|
3530
|
+
if (name === SENT2 || sent[id]) {
|
|
3531
|
+
continue;
|
|
3179
3532
|
}
|
|
3180
|
-
|
|
3181
|
-
|
|
3182
|
-
|
|
3183
|
-
|
|
3533
|
+
const file = join2(dir, name);
|
|
3534
|
+
const info = await stat3(file).catch(() => {
|
|
3535
|
+
return;
|
|
3536
|
+
});
|
|
3537
|
+
if (!info) {
|
|
3538
|
+
continue;
|
|
3184
3539
|
}
|
|
3185
|
-
|
|
3186
|
-
|
|
3187
|
-
|
|
3188
|
-
if (!option) {
|
|
3189
|
-
options.push(`-o ${value !== null && value !== undefined ? value : ""}`.trim());
|
|
3190
|
-
} else if (option[1].toLowerCase() === "port") {
|
|
3191
|
-
port = option[2];
|
|
3192
|
-
} else {
|
|
3193
|
-
user = option[2];
|
|
3540
|
+
if (now - info.mtimeMs > this.options.unseenMs) {
|
|
3541
|
+
await rm2(file, { force: true });
|
|
3542
|
+
continue;
|
|
3194
3543
|
}
|
|
3195
|
-
|
|
3196
|
-
options.push(/^-[bBcDeEFiIJLmOQRSwW]$/.test(word) && words2[i + 1] !== undefined ? `${word} ${words2[++i]}` : word);
|
|
3197
|
-
} else if (destination === undefined) {
|
|
3198
|
-
destination = word;
|
|
3199
|
-
} else {
|
|
3200
|
-
return { error: "invalid" };
|
|
3544
|
+
others.push({ file, bytes: info.size, at: info.mtimeMs });
|
|
3201
3545
|
}
|
|
3202
3546
|
}
|
|
3203
|
-
|
|
3204
|
-
|
|
3205
|
-
|
|
3206
|
-
|
|
3207
|
-
return { error: "empty" };
|
|
3208
|
-
}
|
|
3209
|
-
let host2 = destination.replace(/^ssh:\/\//, "").replace(/\/.*$/, "");
|
|
3210
|
-
if (user && !host2.includes("@")) {
|
|
3211
|
-
host2 = `${user}@${host2}`;
|
|
3212
|
-
}
|
|
3213
|
-
if (port) {
|
|
3214
|
-
if (!/^\d{1,5}$/.test(port) || /:\d+$/.test(host2)) {
|
|
3215
|
-
return { error: "invalid" };
|
|
3547
|
+
let total = others.reduce((sum, each) => sum + each.bytes, 0);
|
|
3548
|
+
for (const each of others.sort((a, b) => a.at - b.at)) {
|
|
3549
|
+
if (total <= this.options.keptBytes) {
|
|
3550
|
+
break;
|
|
3216
3551
|
}
|
|
3217
|
-
|
|
3552
|
+
await rm2(each.file, { force: true });
|
|
3553
|
+
total -= each.bytes;
|
|
3218
3554
|
}
|
|
3219
|
-
return isHost(host2) ? { host: host2 } : { error: "invalid" };
|
|
3220
|
-
}
|
|
3221
|
-
TaLocation2.readHost = readHost;
|
|
3222
|
-
function ssh(host2, path2 = "") {
|
|
3223
|
-
return isCloud(host2) ? `${host2}${path2 ? normalize(path2) : ""}` : `${TaLocation2.SSH}${host2}${path2 ? normalize(path2) : ""}`;
|
|
3224
3555
|
}
|
|
3225
|
-
|
|
3226
|
-
|
|
3227
|
-
|
|
3228
|
-
|
|
3229
|
-
|
|
3230
|
-
|
|
3231
|
-
|
|
3556
|
+
}
|
|
3557
|
+
|
|
3558
|
+
// web-src/media-host.ts
|
|
3559
|
+
class RoomMedia {
|
|
3560
|
+
pictures;
|
|
3561
|
+
files;
|
|
3562
|
+
constructor(doubles) {
|
|
3563
|
+
this.pictures = new RoomPictures(doubles);
|
|
3564
|
+
this.files = new RoomFiles(doubles);
|
|
3232
3565
|
}
|
|
3233
|
-
|
|
3234
|
-
|
|
3235
|
-
|
|
3236
|
-
return isRemote(location) ? (_a = parse(location)) === null || _a === undefined ? undefined : _a.host : undefined;
|
|
3566
|
+
start() {
|
|
3567
|
+
this.pictures.start();
|
|
3568
|
+
this.files.start();
|
|
3237
3569
|
}
|
|
3238
|
-
|
|
3239
|
-
|
|
3240
|
-
|
|
3241
|
-
var _b;
|
|
3242
|
-
return (_b = (_a = parse(location)) === null || _a === undefined ? undefined : _a.path) !== null && _b !== undefined ? _b : location;
|
|
3570
|
+
dispose() {
|
|
3571
|
+
this.pictures.dispose();
|
|
3572
|
+
this.files.dispose();
|
|
3243
3573
|
}
|
|
3244
|
-
|
|
3245
|
-
|
|
3246
|
-
|
|
3247
|
-
|
|
3248
|
-
|
|
3249
|
-
|
|
3574
|
+
heard(event) {
|
|
3575
|
+
return this.pictures.heard(event) || this.files.heard(event);
|
|
3576
|
+
}
|
|
3577
|
+
request(request) {
|
|
3578
|
+
switch (request.kind) {
|
|
3579
|
+
case "putPicture":
|
|
3580
|
+
return this.pictures.put(String(request.room), String(request.data), String(request.type));
|
|
3581
|
+
case "picture":
|
|
3582
|
+
return this.pictures.get(String(request.room), String(request.id));
|
|
3583
|
+
default:
|
|
3584
|
+
return this.files.request(request);
|
|
3250
3585
|
}
|
|
3251
|
-
return parentPath(location);
|
|
3252
3586
|
}
|
|
3253
|
-
|
|
3254
|
-
|
|
3255
|
-
|
|
3256
|
-
|
|
3257
|
-
|
|
3258
|
-
|
|
3259
|
-
|
|
3587
|
+
}
|
|
3588
|
+
|
|
3589
|
+
// web-src/double-host.ts
|
|
3590
|
+
var DEFAULTS3 = { pollMs: 400, timeoutMs: 10 * 60 * 1000, claimDelayMs: 3000, now: () => Date.now() };
|
|
3591
|
+
var ANSWERING_KEPT = 30;
|
|
3592
|
+
|
|
3593
|
+
class RoomDoubles {
|
|
3594
|
+
host;
|
|
3595
|
+
options;
|
|
3596
|
+
tasks = new RoomTasks(this);
|
|
3597
|
+
user = new RoomUserTools(this);
|
|
3598
|
+
agents = new RoomAgents(this);
|
|
3599
|
+
grants = new RoomGrants(this);
|
|
3600
|
+
reach = new RoomReach(this);
|
|
3601
|
+
media = new RoomMedia(this);
|
|
3602
|
+
chats = new Map;
|
|
3603
|
+
queues = new Map;
|
|
3604
|
+
answered = new Map;
|
|
3605
|
+
turns = new Map;
|
|
3606
|
+
opening = new Map;
|
|
3607
|
+
stopEvents;
|
|
3608
|
+
disposed = false;
|
|
3609
|
+
constructor(host, options = {}) {
|
|
3610
|
+
this.host = host;
|
|
3611
|
+
this.options = { ...DEFAULTS3, ...options };
|
|
3260
3612
|
}
|
|
3261
|
-
|
|
3262
|
-
|
|
3263
|
-
|
|
3264
|
-
|
|
3265
|
-
|
|
3266
|
-
|
|
3613
|
+
async start() {
|
|
3614
|
+
this.stopEvents = this.host.rooms.onEvent((event) => this.heard(event));
|
|
3615
|
+
this.host.rooms.listRooms().then((rooms) => {
|
|
3616
|
+
for (const room of rooms) {
|
|
3617
|
+
if (room.status === "member" && !room.unlisted) {
|
|
3618
|
+
this.check(room.id);
|
|
3619
|
+
}
|
|
3620
|
+
}
|
|
3621
|
+
}).catch((error) => console.error("ta-rooms could not read the rooms for the doubles:", error));
|
|
3622
|
+
this.tasks.start().catch((error) => console.error("ta-rooms could not read the tasks of the doubles:", error));
|
|
3623
|
+
this.media.start();
|
|
3267
3624
|
}
|
|
3268
|
-
|
|
3269
|
-
|
|
3270
|
-
|
|
3271
|
-
|
|
3272
|
-
|
|
3273
|
-
|
|
3274
|
-
|
|
3625
|
+
dispose() {
|
|
3626
|
+
this.disposed = true;
|
|
3627
|
+
this.stopEvents?.();
|
|
3628
|
+
this.reach.dispose();
|
|
3629
|
+
this.tasks.dispose();
|
|
3630
|
+
this.media.dispose();
|
|
3631
|
+
this.user.wake();
|
|
3275
3632
|
}
|
|
3276
|
-
|
|
3277
|
-
|
|
3278
|
-
if (homeRelative(location) !== undefined) {
|
|
3279
|
-
return false;
|
|
3280
|
-
}
|
|
3281
|
-
return isRemote(location) || /^([\\/]|[A-Za-z]:)/.test(location);
|
|
3633
|
+
turnOf(sessionFile) {
|
|
3634
|
+
return sessionFile ? this.turns.get(sessionFile) : undefined;
|
|
3282
3635
|
}
|
|
3283
|
-
|
|
3284
|
-
|
|
3285
|
-
|
|
3286
|
-
const at = Math.max(trimmed.lastIndexOf("/"), trimmed.lastIndexOf("\\"));
|
|
3287
|
-
if (at < 0) {
|
|
3288
|
-
return;
|
|
3636
|
+
track(sessionFile, turn) {
|
|
3637
|
+
if (sessionFile) {
|
|
3638
|
+
this.turns.set(sessionFile, turn);
|
|
3289
3639
|
}
|
|
3290
|
-
const head = trimmed.slice(0, at);
|
|
3291
|
-
return head === "" || /^[A-Za-z]:$/.test(head) ? head + trimmed[at] : head;
|
|
3292
|
-
}
|
|
3293
|
-
function childPath(path2, name) {
|
|
3294
|
-
const separator = path2.includes("/") || !path2.includes("\\") ? "/" : "\\";
|
|
3295
|
-
return path2.endsWith(separator) ? path2 + name : path2 + separator + name;
|
|
3296
3640
|
}
|
|
3297
|
-
|
|
3298
|
-
const
|
|
3299
|
-
|
|
3641
|
+
count(room, now) {
|
|
3642
|
+
const answers = (this.answered.get(room) ?? []).filter((at) => now - at < 60 * 60 * 1000);
|
|
3643
|
+
this.answered.set(room, answers);
|
|
3644
|
+
return !DoubleUtil.limited(answers, now);
|
|
3300
3645
|
}
|
|
3301
|
-
|
|
3302
|
-
|
|
3303
|
-
var TaMessageContext;
|
|
3304
|
-
(function(TaMessageContext2) {
|
|
3305
|
-
const BLOCK = /^<context>\n?[\s\S]*?\n?<\/context>\n?/;
|
|
3306
|
-
function wrap(context, message) {
|
|
3307
|
-
return `<context>
|
|
3308
|
-
${context}
|
|
3309
|
-
</context>
|
|
3310
|
-
${message}`;
|
|
3646
|
+
counted(room, now) {
|
|
3647
|
+
this.answered.set(room, [...this.answered.get(room) ?? [], now]);
|
|
3311
3648
|
}
|
|
3312
|
-
|
|
3313
|
-
|
|
3314
|
-
|
|
3649
|
+
used(sessionFile, tool) {
|
|
3650
|
+
const turn = this.turnOf(sessionFile);
|
|
3651
|
+
if (turn?.active) {
|
|
3652
|
+
turn.tools.push(tool);
|
|
3653
|
+
}
|
|
3315
3654
|
}
|
|
3316
|
-
|
|
3317
|
-
|
|
3318
|
-
|
|
3319
|
-
const
|
|
3320
|
-
|
|
3655
|
+
async roomState(sessionFile, prefix) {
|
|
3656
|
+
const turn = this.activeTurn(sessionFile);
|
|
3657
|
+
const state = await this.host.rooms.getState(turn.room, prefix || undefined);
|
|
3658
|
+
const shown = Object.fromEntries(Object.entries(state).filter(([key]) => !key.startsWith("chat:") && !key.startsWith(ROOM_ANSWERING_KEY)).map(([key, value]) => [key, value.value]));
|
|
3659
|
+
const text = JSON.stringify(shown);
|
|
3660
|
+
return text.length > 16000 ? `${text.slice(0, 16000)}… (cut; read a narrower prefix)` : text;
|
|
3321
3661
|
}
|
|
3322
|
-
|
|
3323
|
-
|
|
3324
|
-
|
|
3325
|
-
(
|
|
3326
|
-
|
|
3327
|
-
|
|
3328
|
-
|
|
3329
|
-
return marked(message, line(from, detail));
|
|
3662
|
+
async setRoomState(sessionFile, key, value) {
|
|
3663
|
+
const turn = this.activeTurn(sessionFile);
|
|
3664
|
+
if (!key || key.startsWith("chat:")) {
|
|
3665
|
+
throw new Error("Say things in your reply: the keys of the chat are not for this tool.");
|
|
3666
|
+
}
|
|
3667
|
+
await this.host.rooms.setState(turn.room, key, value === undefined ? null : value);
|
|
3668
|
+
return `Set ${key}.`;
|
|
3330
3669
|
}
|
|
3331
|
-
|
|
3332
|
-
|
|
3333
|
-
|
|
3670
|
+
async request(request) {
|
|
3671
|
+
switch (request?.kind) {
|
|
3672
|
+
case "sendDraft":
|
|
3673
|
+
return this.reach.sendDraft(String(request.draft), typeof request.text === "string" ? request.text : undefined, true);
|
|
3674
|
+
case "dropDraft":
|
|
3675
|
+
return this.reach.sendDraft(String(request.draft), undefined, false);
|
|
3676
|
+
case "putPicture":
|
|
3677
|
+
case "picture":
|
|
3678
|
+
case "fileBegin":
|
|
3679
|
+
case "fileAppend":
|
|
3680
|
+
case "fileFinish":
|
|
3681
|
+
case "fileRead":
|
|
3682
|
+
return this.media.request(request);
|
|
3683
|
+
case "grant":
|
|
3684
|
+
return this.grants.grant(request.friend, request.name, request.capabilities, request.cwd, request.hours, request.room, request.title);
|
|
3685
|
+
case "answer":
|
|
3686
|
+
return this.grants.answer(request);
|
|
3687
|
+
case "revoke": {
|
|
3688
|
+
const grants = await this.get(DoubleKeys.GRANTS) ?? [];
|
|
3689
|
+
await this.set(DoubleKeys.GRANTS, grants.filter((grant) => grant.id !== request.grant));
|
|
3690
|
+
return;
|
|
3691
|
+
}
|
|
3692
|
+
case "revokeAll":
|
|
3693
|
+
return this.grants.revokeAll(String(request.friend));
|
|
3694
|
+
case "clearLog":
|
|
3695
|
+
await this.set(DoubleKeys.LOG, []);
|
|
3696
|
+
return;
|
|
3697
|
+
case "agents":
|
|
3698
|
+
return (await this.agents.teamAgents()).map(({ id, name, role, face }) => ({ id, name, role, ...typeof face === "number" ? { face } : {} }));
|
|
3699
|
+
case "invite":
|
|
3700
|
+
return this.agents.invite(String(request.room), String(request.agent));
|
|
3701
|
+
case "dismiss":
|
|
3702
|
+
return this.agents.dismiss(String(request.room), String(request.agent));
|
|
3703
|
+
case "stopTask":
|
|
3704
|
+
return request.handed ? this.tasks.cancel(String(request.task)) : this.tasks.stop(String(request.task));
|
|
3705
|
+
case "approveTask":
|
|
3706
|
+
return this.tasks.approve(String(request.task), !!request.accept);
|
|
3707
|
+
case "forgetTask":
|
|
3708
|
+
return this.tasks.forget(String(request.task), !!request.handed);
|
|
3709
|
+
default:
|
|
3710
|
+
throw new Error("An unknown request.");
|
|
3711
|
+
}
|
|
3334
3712
|
}
|
|
3335
|
-
|
|
3336
|
-
|
|
3337
|
-
|
|
3713
|
+
heard(event) {
|
|
3714
|
+
if (event.type === "message") {
|
|
3715
|
+
this.media.heard(event);
|
|
3716
|
+
return;
|
|
3717
|
+
}
|
|
3718
|
+
if (event.type === "request") {
|
|
3719
|
+
const { request } = event;
|
|
3720
|
+
this.host.rooms.listRooms().then((rooms) => {
|
|
3721
|
+
if (!rooms.find((room) => room.id === request.room)?.unlisted) {
|
|
3722
|
+
this.grants.tell(`join:${request.room}:${request.id}:${request.at}`, request.title, TaLanguageUtil.pick(language(), {
|
|
3723
|
+
"zh-CN": `${request.name}想加入群聊`,
|
|
3724
|
+
"zh-TW": `${request.name}想加入群聊`,
|
|
3725
|
+
"en-US": `${request.name} asks to join`,
|
|
3726
|
+
"ja-JP": `${request.name}はグループチャットへの参加をリクエストしています`
|
|
3727
|
+
}));
|
|
3728
|
+
}
|
|
3729
|
+
}).catch(() => {
|
|
3730
|
+
return;
|
|
3731
|
+
});
|
|
3732
|
+
return;
|
|
3733
|
+
}
|
|
3734
|
+
if (event.type === "chat") {
|
|
3735
|
+
const chat2 = this.chats.get(event.room);
|
|
3736
|
+
if (chat2) {
|
|
3737
|
+
chat2.then((found) => {
|
|
3738
|
+
found.hear(event);
|
|
3739
|
+
this.user.wake();
|
|
3740
|
+
this.check(event.room);
|
|
3741
|
+
this.tasks.sync(event.room);
|
|
3742
|
+
});
|
|
3743
|
+
} else {
|
|
3744
|
+
this.check(event.room);
|
|
3745
|
+
}
|
|
3746
|
+
return;
|
|
3747
|
+
}
|
|
3748
|
+
if (event.type !== "state") {
|
|
3749
|
+
return;
|
|
3750
|
+
}
|
|
3751
|
+
if (event.key.startsWith(ROOM_TASKS_KEY) || event.key.startsWith(ROOM_CANCEL_KEY)) {
|
|
3752
|
+
this.tasks.heard(event.room, event.key, event.value, event.by).catch((error) => console.error("ta-rooms: a task could not be followed:", error));
|
|
3753
|
+
return;
|
|
3754
|
+
}
|
|
3755
|
+
if (!(event.key.startsWith("chat:") || event.key.startsWith(ROOM_AGENTS_KEY))) {
|
|
3756
|
+
return;
|
|
3757
|
+
}
|
|
3758
|
+
const chat = this.chats.get(event.room);
|
|
3759
|
+
if (chat) {
|
|
3760
|
+
chat.then((found) => {
|
|
3761
|
+
found.heard(event.key, event);
|
|
3762
|
+
this.user.wake();
|
|
3763
|
+
this.check(event.room);
|
|
3764
|
+
this.tasks.sync(event.room);
|
|
3765
|
+
});
|
|
3766
|
+
} else {
|
|
3767
|
+
this.check(event.room);
|
|
3768
|
+
}
|
|
3338
3769
|
}
|
|
3339
|
-
|
|
3340
|
-
|
|
3341
|
-
|
|
3342
|
-
|
|
3343
|
-
|
|
3770
|
+
chatOf(room) {
|
|
3771
|
+
let chat = this.chats.get(room);
|
|
3772
|
+
if (!chat) {
|
|
3773
|
+
const made = new RelayRoomChat(this.host.rooms, room);
|
|
3774
|
+
chat = made.load().then(() => made);
|
|
3775
|
+
chat.catch(() => this.chats.delete(room));
|
|
3776
|
+
this.chats.set(room, chat);
|
|
3777
|
+
}
|
|
3778
|
+
return chat;
|
|
3344
3779
|
}
|
|
3345
|
-
|
|
3346
|
-
|
|
3347
|
-
|
|
3348
|
-
|
|
3780
|
+
check(room) {
|
|
3781
|
+
const before = this.queues.get(room) ?? Promise.resolve();
|
|
3782
|
+
const next = before.then(() => this.reach.look(room)).catch((error) => console.error("ta-rooms: the double could not answer:", error)).finally(() => {
|
|
3783
|
+
if (this.queues.get(room) === next) {
|
|
3784
|
+
this.queues.delete(room);
|
|
3785
|
+
}
|
|
3786
|
+
});
|
|
3787
|
+
this.queues.set(room, next);
|
|
3349
3788
|
}
|
|
3350
|
-
|
|
3351
|
-
|
|
3352
|
-
|
|
3353
|
-
|
|
3354
|
-
|
|
3355
|
-
|
|
3356
|
-
|
|
3357
|
-
|
|
3358
|
-
|
|
3359
|
-
|
|
3360
|
-
|
|
3361
|
-
|
|
3362
|
-
|
|
3789
|
+
async reply(room, self, chat, line, agent, call) {
|
|
3790
|
+
const member = room.members.find((each) => each.member === line.from);
|
|
3791
|
+
if (!member) {
|
|
3792
|
+
return;
|
|
3793
|
+
}
|
|
3794
|
+
const asker = { id: member.id, member: member.member, name: member.name, ...member.guest ? { guest: true } : {} };
|
|
3795
|
+
const own = !!agent && member.member === self.member;
|
|
3796
|
+
const now = this.options.now();
|
|
3797
|
+
const entry = {
|
|
3798
|
+
id: DoubleUtil.id("log"),
|
|
3799
|
+
at: now,
|
|
3800
|
+
room: room.id,
|
|
3801
|
+
title: room.title,
|
|
3802
|
+
friend: asker.id,
|
|
3803
|
+
name: asker.name,
|
|
3804
|
+
...asker.guest ? { guest: true } : {},
|
|
3805
|
+
asked: DoubleUtil.cut(line.text, 300),
|
|
3806
|
+
answer: "",
|
|
3807
|
+
tools: [],
|
|
3808
|
+
capabilities: [],
|
|
3809
|
+
outcome: "answered",
|
|
3810
|
+
...agent ? { agent: agent.name } : {},
|
|
3811
|
+
...own ? { own: true } : {}
|
|
3812
|
+
};
|
|
3813
|
+
if (!own && !this.count(room.id, now)) {
|
|
3814
|
+
await this.log({ ...entry, outcome: "limited" });
|
|
3815
|
+
return;
|
|
3816
|
+
}
|
|
3817
|
+
const grants = own || asker.guest ? [] : DoubleUtil.grantsOf(await this.get(DoubleKeys.GRANTS) ?? [], asker.id, now, room.id);
|
|
3818
|
+
const work = DoubleUtil.work(grants);
|
|
3819
|
+
const me = (await this.host.rooms.getIdentity()).name;
|
|
3820
|
+
const turn = {
|
|
3821
|
+
room: room.id,
|
|
3822
|
+
title: room.title,
|
|
3823
|
+
asker,
|
|
3824
|
+
capabilities: work?.capabilities ?? [],
|
|
3825
|
+
cwd: work?.cwd,
|
|
3826
|
+
active: true,
|
|
3827
|
+
tools: [],
|
|
3828
|
+
...agent ? { agent } : {},
|
|
3829
|
+
...own ? { trusted: true } : {}
|
|
3830
|
+
};
|
|
3831
|
+
try {
|
|
3832
|
+
const team = agent && (await this.agents.teamAgents()).find((each) => each.id === agent.id);
|
|
3833
|
+
if (agent && !team) {
|
|
3834
|
+
throw new Error(TaLanguageUtil.pick(language(), {
|
|
3835
|
+
"zh-CN": `${agent.name}已经被删除了。`,
|
|
3836
|
+
"zh-TW": `${agent.name}已經被刪除了。`,
|
|
3837
|
+
"en-US": `${agent.name} was deleted.`,
|
|
3838
|
+
"ja-JP": `${agent.name}は削除されました。`
|
|
3839
|
+
}));
|
|
3840
|
+
}
|
|
3841
|
+
const cwd = own ? team?.cwd || (await this.host.getServerInfo()).cwd : work?.cwd ?? await this.chatDirectory();
|
|
3842
|
+
turn.cwd = own ? cwd : turn.cwd;
|
|
3843
|
+
const key = DoubleUtil.sessionKey(room.id, work && { friend: asker.id, cwd: work.cwd }, agent?.id, own);
|
|
3844
|
+
const shared = team ? DoubleUtil.agentRole(me, room.title, team) : DoubleUtil.role(me, room.title);
|
|
3845
|
+
const role = own ? shared : TaConfineUtil.confine(shared);
|
|
3846
|
+
const name = this.sessionName(room, asker, work, team || undefined, own);
|
|
3847
|
+
const { sessionId, sessionFile } = await this.session(key, cwd, role, name, team ? team.model : undefined);
|
|
3848
|
+
this.track(sessionFile, turn);
|
|
3849
|
+
const before = await this.host.getState(sessionId);
|
|
3850
|
+
const nameOf = (each) => each.agent?.name ?? room.members.find((other) => other.member === each.from)?.name ?? each.name;
|
|
3851
|
+
const permits = [
|
|
3852
|
+
own ? DoubleUtil.ownPermits(me, cwd) : DoubleUtil.permits(me, asker.name, work),
|
|
3853
|
+
...call ? [DoubleUtil.callNote(me, asker.name, call.how, call.direct)] : []
|
|
3854
|
+
].join(`
|
|
3855
|
+
`);
|
|
3856
|
+
const message = DoubleUtil.prompt({ title: room.title, asker, line, lines: chat.lines, nameOf, permits });
|
|
3857
|
+
const images = line.images?.length ? await this.media.pictures.images(room.id, line) : [];
|
|
3858
|
+
await this.host.prompt(sessionId, {
|
|
3859
|
+
clientMessageId: `${room.id}:${line.id}:${agent?.id ?? "double"}`,
|
|
3860
|
+
message,
|
|
3861
|
+
...images.length ? { images } : {}
|
|
3862
|
+
});
|
|
3863
|
+
await this.settled(sessionId, before.messageCount);
|
|
3864
|
+
const text = (await this.host.getLastAssistantText(sessionId)).text?.trim();
|
|
3865
|
+
if (!text) {
|
|
3866
|
+
throw new Error("No answer.");
|
|
3867
|
+
}
|
|
3868
|
+
if (call?.how === "draft") {
|
|
3869
|
+
await this.reach.keepDraft(room, line, asker, text);
|
|
3870
|
+
} else {
|
|
3871
|
+
await chat.say(text, self, agent ? { agent: { id: agent.id, name: agent.name }, re: line.id } : { double: true, re: line.id });
|
|
3872
|
+
}
|
|
3873
|
+
if (!own) {
|
|
3874
|
+
this.counted(room.id, now);
|
|
3875
|
+
}
|
|
3876
|
+
await this.log({
|
|
3877
|
+
...entry,
|
|
3878
|
+
answer: DoubleUtil.cut(text, 300),
|
|
3879
|
+
tools: turn.tools,
|
|
3880
|
+
capabilities: turn.capabilities,
|
|
3881
|
+
...call?.how === "draft" ? { outcome: "drafted" } : {}
|
|
3882
|
+
});
|
|
3883
|
+
} catch (error) {
|
|
3884
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
3885
|
+
await this.log({ ...entry, outcome: "failed", error: reason, tools: turn.tools, capabilities: turn.capabilities });
|
|
3886
|
+
if (own && agent) {
|
|
3887
|
+
const said2 = TaLanguageUtil.pick(language(), {
|
|
3888
|
+
"zh-CN": `这次没能完成:${reason}`,
|
|
3889
|
+
"zh-TW": `這次沒能完成:${reason}`,
|
|
3890
|
+
"en-US": `I could not do it: ${reason}`,
|
|
3891
|
+
"ja-JP": `これを実行できませんでした:${reason}`
|
|
3892
|
+
});
|
|
3893
|
+
await chat.say(said2, self, { agent: { id: agent.id, name: agent.name }, re: line.id }).catch(() => {
|
|
3894
|
+
return;
|
|
3895
|
+
});
|
|
3896
|
+
}
|
|
3897
|
+
} finally {
|
|
3898
|
+
turn.active = false;
|
|
3899
|
+
}
|
|
3900
|
+
}
|
|
3901
|
+
async claim(roomId, member, re) {
|
|
3902
|
+
const identity = await this.host.rooms.getIdentity();
|
|
3903
|
+
if (!identity.shared) {
|
|
3904
|
+
return true;
|
|
3905
|
+
}
|
|
3906
|
+
if (this.host.presence().pages === 0 && this.options.claimDelayMs > 0) {
|
|
3907
|
+
await new Promise((resolve) => setTimeout(resolve, this.options.claimDelayMs));
|
|
3908
|
+
}
|
|
3909
|
+
const device = await this.device();
|
|
3910
|
+
const key = `${ROOM_ANSWERING_KEY}${member}`;
|
|
3911
|
+
for (let tries = 0;tries < 6; tries++) {
|
|
3912
|
+
const now = (await this.host.rooms.getState(roomId, key))[key];
|
|
3913
|
+
const taken = Array.isArray(now?.value) ? now.value : [];
|
|
3914
|
+
const found = taken.find((each) => each?.re === re);
|
|
3915
|
+
if (found) {
|
|
3916
|
+
return found.device === device;
|
|
3917
|
+
}
|
|
3918
|
+
const next = [...taken, { re, device, at: this.options.now() }].slice(-ANSWERING_KEPT);
|
|
3919
|
+
if ((await this.host.rooms.setState(roomId, key, next, now?.version ?? 0)).ok) {
|
|
3920
|
+
return true;
|
|
3921
|
+
}
|
|
3922
|
+
}
|
|
3923
|
+
return false;
|
|
3924
|
+
}
|
|
3925
|
+
async device() {
|
|
3926
|
+
const kept = await this.get(DoubleKeys.DEVICE);
|
|
3927
|
+
if (typeof kept === "string" && kept) {
|
|
3928
|
+
return kept;
|
|
3929
|
+
}
|
|
3930
|
+
const made = DoubleUtil.id("device");
|
|
3931
|
+
await this.set(DoubleKeys.DEVICE, made);
|
|
3932
|
+
return made;
|
|
3933
|
+
}
|
|
3934
|
+
sessionName(room, asker, work, agent, own) {
|
|
3935
|
+
const who = agent?.name ?? TaLanguageUtil.pick(language(), {
|
|
3936
|
+
"zh-CN": "分身",
|
|
3937
|
+
"zh-TW": "分身",
|
|
3938
|
+
"en-US": "Double",
|
|
3939
|
+
"ja-JP": "分身"
|
|
3940
|
+
});
|
|
3941
|
+
if (own || !work) {
|
|
3942
|
+
return `${who} · ${room.title}`;
|
|
3943
|
+
}
|
|
3944
|
+
return TaLanguageUtil.pick(language(), {
|
|
3945
|
+
"zh-CN": `${who}替${asker.name}做事 · ${room.title}`,
|
|
3946
|
+
"zh-TW": `${who}替${asker.name}做事 · ${room.title}`,
|
|
3947
|
+
"en-US": `${who} for ${asker.name} · ${room.title}`,
|
|
3948
|
+
"ja-JP": `${who}が${asker.name}のために · ${room.title}`
|
|
3949
|
+
});
|
|
3950
|
+
}
|
|
3951
|
+
async log(entry) {
|
|
3952
|
+
await this.set(DoubleKeys.LOG, DoubleUtil.log(await this.get(DoubleKeys.LOG) ?? [], entry));
|
|
3953
|
+
}
|
|
3954
|
+
async chatDirectory() {
|
|
3955
|
+
const directory = join3(this.host.agentDir, "ta-rooms", "double");
|
|
3956
|
+
await mkdir3(directory, { recursive: true });
|
|
3957
|
+
return directory;
|
|
3958
|
+
}
|
|
3959
|
+
session(key, cwd, role, name, model) {
|
|
3960
|
+
let opening = this.opening.get(key);
|
|
3961
|
+
if (!opening) {
|
|
3962
|
+
opening = this.openSession(key, cwd, role, name, model).finally(() => this.opening.delete(key));
|
|
3963
|
+
this.opening.set(key, opening);
|
|
3964
|
+
}
|
|
3965
|
+
return opening;
|
|
3966
|
+
}
|
|
3967
|
+
async openSession(key, cwd, role, name, model) {
|
|
3968
|
+
const sessions = await this.get(DoubleKeys.SESSIONS) ?? {};
|
|
3969
|
+
const file = sessions[key];
|
|
3970
|
+
const chosen = model ? { model } : {};
|
|
3971
|
+
if (file) {
|
|
3972
|
+
const open2 = (await this.host.listSessions()).find((session) => session.sessionFile === file);
|
|
3973
|
+
if (open2) {
|
|
3974
|
+
if (model && open2.model !== model) {
|
|
3975
|
+
const slash = model.indexOf("/");
|
|
3976
|
+
await this.host.setModel(open2.sessionId, model.slice(0, slash), model.slice(slash + 1));
|
|
3977
|
+
}
|
|
3978
|
+
return { sessionId: open2.sessionId, sessionFile: file };
|
|
3979
|
+
}
|
|
3980
|
+
try {
|
|
3981
|
+
const opened = await this.host.openSession({ cwd, sessionFile: file, appendSystemPrompt: role, group: NAME, ...chosen });
|
|
3982
|
+
return { sessionId: opened.sessionId, sessionFile: file };
|
|
3983
|
+
} catch {}
|
|
3984
|
+
}
|
|
3985
|
+
const { sessionId } = await this.host.openSession({ cwd, appendSystemPrompt: role, group: NAME, ...chosen });
|
|
3986
|
+
await this.host.setSessionName(sessionId, name);
|
|
3987
|
+
const { sessionFile } = await this.host.getState(sessionId);
|
|
3988
|
+
if (sessionFile) {
|
|
3989
|
+
await this.set(DoubleKeys.SESSIONS, { ...await this.get(DoubleKeys.SESSIONS) ?? {}, [key]: sessionFile });
|
|
3990
|
+
}
|
|
3991
|
+
return { sessionId, sessionFile };
|
|
3992
|
+
}
|
|
3993
|
+
async settled(sessionId, before, timeoutMs = this.options.timeoutMs) {
|
|
3994
|
+
const start = this.options.now();
|
|
3995
|
+
for (;; ) {
|
|
3996
|
+
const state = await this.host.getState(sessionId);
|
|
3997
|
+
if (!state.isStreaming && state.pendingMessageCount === 0 && state.messageCount > before) {
|
|
3998
|
+
return;
|
|
3999
|
+
}
|
|
4000
|
+
if (this.options.now() - start > timeoutMs) {
|
|
4001
|
+
await this.host.abort(sessionId).catch(() => {
|
|
4002
|
+
return;
|
|
4003
|
+
});
|
|
4004
|
+
throw new Error("No answer in time.");
|
|
4005
|
+
}
|
|
4006
|
+
await new Promise((resolve) => setTimeout(resolve, this.options.pollMs));
|
|
4007
|
+
}
|
|
4008
|
+
}
|
|
4009
|
+
activeTurn(sessionFile) {
|
|
4010
|
+
const turn = this.turnOf(sessionFile);
|
|
4011
|
+
if (!turn?.active) {
|
|
4012
|
+
throw new Error("Only while answering someone in a room.");
|
|
4013
|
+
}
|
|
4014
|
+
return turn;
|
|
4015
|
+
}
|
|
4016
|
+
async roomOf(ref) {
|
|
4017
|
+
const wanted = String(ref ?? "").trim();
|
|
4018
|
+
const rooms = (await this.host.rooms.listRooms()).filter((room2) => room2.status === "member" && !room2.unlisted);
|
|
4019
|
+
const room = rooms.find((each) => each.id === wanted) ?? rooms.find((each) => each.title.toLowerCase() === wanted.toLowerCase());
|
|
4020
|
+
if (!room) {
|
|
4021
|
+
throw new Error(`No room "${wanted}": ${rooms.map((each) => each.title).join(", ") || "the user is in none"}.`);
|
|
4022
|
+
}
|
|
4023
|
+
return room;
|
|
4024
|
+
}
|
|
4025
|
+
async get(key) {
|
|
4026
|
+
return await this.host.getExtensionData(NAME, key) ?? undefined;
|
|
4027
|
+
}
|
|
4028
|
+
set(key, value) {
|
|
4029
|
+
return this.host.setExtensionData(NAME, key, value);
|
|
4030
|
+
}
|
|
4031
|
+
}
|
|
4032
|
+
|
|
4033
|
+
// web-src/friend-notices.ts
|
|
4034
|
+
var NOTIFY2 = "@celljs/ta-notify";
|
|
4035
|
+
var NAME2 = "@celljs/ta-rooms";
|
|
4036
|
+
var VIEW = `${NAME2}/friends`;
|
|
4037
|
+
var ROOMS_VIEW2 = `${NAME2}/rooms`;
|
|
4038
|
+
|
|
4039
|
+
class FriendNotices {
|
|
4040
|
+
host;
|
|
4041
|
+
stop;
|
|
4042
|
+
told = new Set;
|
|
4043
|
+
joining = new Set;
|
|
4044
|
+
answered = new Set;
|
|
4045
|
+
constructor(host) {
|
|
4046
|
+
this.host = host;
|
|
4047
|
+
}
|
|
4048
|
+
start() {
|
|
4049
|
+
this.stop = this.host.rooms.onEvent((event) => {
|
|
4050
|
+
if (event.type === "friend") {
|
|
4051
|
+
this.tell(event.request);
|
|
4052
|
+
} else if (event.type === "rooms") {
|
|
4053
|
+
this.tellInvites();
|
|
4054
|
+
this.tellShares();
|
|
4055
|
+
}
|
|
4056
|
+
});
|
|
4057
|
+
this.tellInvites();
|
|
4058
|
+
this.tellShares();
|
|
4059
|
+
}
|
|
4060
|
+
dispose() {
|
|
4061
|
+
this.stop?.();
|
|
4062
|
+
}
|
|
4063
|
+
async tellInvites() {
|
|
4064
|
+
const invites = await Promise.resolve(this.host.rooms.listGroupInvites?.()).catch(() => {
|
|
4065
|
+
return;
|
|
4066
|
+
});
|
|
4067
|
+
for (const invite of invites ?? []) {
|
|
4068
|
+
if (invite.kind === DIRECT_KIND) {
|
|
4069
|
+
await this.joinDirect(invite.room, invite.by.id);
|
|
4070
|
+
continue;
|
|
4071
|
+
}
|
|
4072
|
+
const how = ReplyPolicyUtil.inviteOf(ReplyPolicyUtil.parse(await this.host.getExtensionData(NAME2, DoubleKeys.REPLY).catch(() => {
|
|
4073
|
+
return;
|
|
4074
|
+
})), invite.by.id);
|
|
4075
|
+
if (how !== "ask") {
|
|
4076
|
+
if (!this.answered.has(invite.room)) {
|
|
4077
|
+
this.answered.add(invite.room);
|
|
4078
|
+
await this.host.rooms.answerGroupInvite(invite.room, how === "join").catch(() => this.answered.delete(invite.room));
|
|
4079
|
+
}
|
|
4080
|
+
continue;
|
|
4081
|
+
}
|
|
4082
|
+
const key = `invite:${invite.room}:${invite.at}`;
|
|
4083
|
+
if (this.told.has(key)) {
|
|
4084
|
+
continue;
|
|
4085
|
+
}
|
|
4086
|
+
this.told.add(key);
|
|
4087
|
+
const lang = language();
|
|
4088
|
+
await this.host.callPackageHost(NOTIFY2, "notify", [
|
|
4089
|
+
{
|
|
4090
|
+
from: NAME2,
|
|
4091
|
+
id: `invite:${invite.room}`,
|
|
4092
|
+
level: "ask",
|
|
4093
|
+
title: invite.title,
|
|
4094
|
+
text: TaLanguageUtil.pick(lang, {
|
|
4095
|
+
"zh-CN": `${invite.by.name} 邀请你加入群聊`,
|
|
4096
|
+
"zh-TW": `${invite.by.name} 邀請你加入群聊`,
|
|
4097
|
+
"en-US": `${invite.by.name} invites you to a group chat`,
|
|
4098
|
+
"ja-JP": `${invite.by.name} からグループチャットへの招待`
|
|
4099
|
+
}),
|
|
4100
|
+
view: ROOMS_VIEW2
|
|
4101
|
+
}
|
|
4102
|
+
]).catch(() => {
|
|
4103
|
+
return;
|
|
4104
|
+
});
|
|
4105
|
+
}
|
|
4106
|
+
}
|
|
4107
|
+
async joinDirect(room, friend) {
|
|
4108
|
+
if (this.joining.has(room)) {
|
|
4109
|
+
return;
|
|
4110
|
+
}
|
|
4111
|
+
this.joining.add(room);
|
|
4112
|
+
try {
|
|
4113
|
+
await this.host.rooms.answerGroupInvite(room, true);
|
|
4114
|
+
const kept = DirectUtil.parse(await this.host.getExtensionData(NAME2, DIRECT_KEY));
|
|
4115
|
+
const current = kept[friend];
|
|
4116
|
+
const inside = !!current && (await this.host.rooms.listRooms()).some((each) => each.id === current && each.status === "member");
|
|
4117
|
+
if (!inside) {
|
|
4118
|
+
await this.host.setExtensionData(NAME2, DIRECT_KEY, { ...kept, [friend]: room });
|
|
4119
|
+
}
|
|
4120
|
+
} catch (error) {
|
|
4121
|
+
console.error("ta-rooms could not join a chat of two:", error);
|
|
4122
|
+
} finally {
|
|
4123
|
+
this.joining.delete(room);
|
|
4124
|
+
}
|
|
4125
|
+
}
|
|
4126
|
+
async tellShares() {
|
|
4127
|
+
const shares = await Promise.resolve(this.host.rooms.listFriendShares?.()).catch(() => {
|
|
4128
|
+
return;
|
|
4129
|
+
});
|
|
4130
|
+
for (const share of shares ?? []) {
|
|
4131
|
+
if (share.seen || this.told.has(`share:${share.id}`)) {
|
|
4132
|
+
continue;
|
|
4133
|
+
}
|
|
4134
|
+
this.told.add(`share:${share.id}`);
|
|
4135
|
+
await this.host.callPackageHost(NOTIFY2, "notify", [
|
|
4136
|
+
{
|
|
4137
|
+
from: NAME2,
|
|
4138
|
+
id: `share:${share.id}`,
|
|
4139
|
+
level: "ask",
|
|
4140
|
+
title: share.title || share.by.name,
|
|
4141
|
+
text: TaLanguageUtil.pick(language(), {
|
|
4142
|
+
"zh-CN": `${share.by.name} 发给你`,
|
|
4143
|
+
"zh-TW": `${share.by.name} 傳給你`,
|
|
4144
|
+
"en-US": `${share.by.name} sent you this`,
|
|
4145
|
+
"ja-JP": `${share.by.name} から届きました`
|
|
4146
|
+
}),
|
|
4147
|
+
view: ROOMS_VIEW2
|
|
4148
|
+
}
|
|
4149
|
+
]).catch(() => {
|
|
4150
|
+
return;
|
|
4151
|
+
});
|
|
4152
|
+
}
|
|
4153
|
+
}
|
|
4154
|
+
async tell(request) {
|
|
4155
|
+
const key = `${request.id}:${request.at}`;
|
|
4156
|
+
if (this.told.has(key)) {
|
|
4157
|
+
return;
|
|
4158
|
+
}
|
|
4159
|
+
this.told.add(key);
|
|
4160
|
+
const lang = language();
|
|
4161
|
+
const text = request.via === "room" && request.room ? TaLanguageUtil.pick(lang, {
|
|
4162
|
+
"zh-CN": `${request.name} 在「${request.room}」里想加你为好友`,
|
|
4163
|
+
"zh-TW": `${request.name} 在「${request.room}」裡想加你為好友`,
|
|
4164
|
+
"en-US": `${request.name} asks in “${request.room}” to be your friend`,
|
|
4165
|
+
"ja-JP": `${request.name} は「${request.room}」で友だちになるようにリクエストしています`
|
|
4166
|
+
}) : TaLanguageUtil.pick(lang, {
|
|
4167
|
+
"zh-CN": `${request.name} 想加你为好友`,
|
|
4168
|
+
"zh-TW": `${request.name} 想加你為好友`,
|
|
4169
|
+
"en-US": `${request.name} wants to be your friend`,
|
|
4170
|
+
"ja-JP": `${request.name} は友だちになるようにリクエストしています`
|
|
4171
|
+
});
|
|
4172
|
+
await this.host.callPackageHost(NOTIFY2, "notify", [
|
|
4173
|
+
{
|
|
4174
|
+
from: NAME2,
|
|
4175
|
+
id: `friend:${request.id}`,
|
|
4176
|
+
level: "ask",
|
|
4177
|
+
title: TaLanguageUtil.pick(lang, {
|
|
4178
|
+
"zh-CN": "新的好友",
|
|
4179
|
+
"zh-TW": "新的好友",
|
|
4180
|
+
"en-US": "New friend",
|
|
4181
|
+
"ja-JP": "新しい友だち"
|
|
4182
|
+
}),
|
|
4183
|
+
text,
|
|
4184
|
+
view: VIEW
|
|
4185
|
+
}
|
|
4186
|
+
]).catch(() => {
|
|
4187
|
+
return;
|
|
4188
|
+
});
|
|
4189
|
+
}
|
|
4190
|
+
}
|
|
4191
|
+
|
|
4192
|
+
// web-src/picture-host.ts
|
|
4193
|
+
import { readFile as readFile3 } from "node:fs/promises";
|
|
4194
|
+
import { resolve } from "node:path";
|
|
4195
|
+
|
|
4196
|
+
// ../ta-protocol/esm/common/access-protocol.js
|
|
4197
|
+
var TaAccessServer = Symbol("TaAccessServer");
|
|
4198
|
+
var TA_SIGN_IN_PATH = "/sign-in";
|
|
4199
|
+
var TA_DEVICE_SIGN_IN_PATH = `${TA_SIGN_IN_PATH}/device`;
|
|
4200
|
+
// ../ta-protocol/esm/common/account-protocol.js
|
|
4201
|
+
var TaAccountServer = Symbol("TaAccountServer");
|
|
4202
|
+
// ../ta-protocol/esm/common/delegate-protocol.js
|
|
4203
|
+
var TaDelegateUtil;
|
|
4204
|
+
(function(TaDelegateUtil2) {
|
|
4205
|
+
TaDelegateUtil2.LEVELS = ["less", "balanced", "more"];
|
|
4206
|
+
TaDelegateUtil2.DEFAULT = "balanced";
|
|
4207
|
+
TaDelegateUtil2.WORK_TOOLS = ["read", "bash", "powershell", "edit", "write", "grep", "find", "ls"];
|
|
4208
|
+
TaDelegateUtil2.CHANGE_TOOLS = ["edit", "write"];
|
|
4209
|
+
const LIMITS = {
|
|
4210
|
+
less: { noChanges: false },
|
|
4211
|
+
balanced: { noChanges: true, steps: 6 },
|
|
4212
|
+
more: { noChanges: true, steps: 2 }
|
|
4213
|
+
};
|
|
4214
|
+
function parse(value) {
|
|
4215
|
+
return TaDelegateUtil2.LEVELS.includes(value) ? value : TaDelegateUtil2.DEFAULT;
|
|
4216
|
+
}
|
|
4217
|
+
TaDelegateUtil2.parse = parse;
|
|
4218
|
+
function mark(level) {
|
|
4219
|
+
return `(delegate=${level})`;
|
|
4220
|
+
}
|
|
4221
|
+
TaDelegateUtil2.mark = mark;
|
|
4222
|
+
function levelOf(systemPrompt) {
|
|
4223
|
+
const role = TaHostUtil.role(systemPrompt);
|
|
4224
|
+
return TaDelegateUtil2.LEVELS.find((level) => role.includes(mark(level)));
|
|
4225
|
+
}
|
|
4226
|
+
TaDelegateUtil2.levelOf = levelOf;
|
|
4227
|
+
function limits(level) {
|
|
4228
|
+
return LIMITS[level];
|
|
4229
|
+
}
|
|
4230
|
+
TaDelegateUtil2.limits = limits;
|
|
4231
|
+
function refusal(level, tool, steps) {
|
|
4232
|
+
if (!TaDelegateUtil2.WORK_TOOLS.includes(tool)) {
|
|
4233
|
+
return;
|
|
4234
|
+
}
|
|
4235
|
+
const limit = LIMITS[level];
|
|
4236
|
+
if (limit.noChanges && TaDelegateUtil2.CHANGE_TOOLS.includes(tool)) {
|
|
4237
|
+
return "You do not change files yourself: hand this to a task with start_task, with instructions complete enough to do it. The user set how much you hand out in the settings of the double.";
|
|
4238
|
+
}
|
|
4239
|
+
if (limit.steps !== undefined && steps >= limit.steps) {
|
|
4240
|
+
return "You have looked enough for this: hand the work to a task with start_task now, with what you found in its instructions, and tell the user what you started.";
|
|
4241
|
+
}
|
|
4242
|
+
return;
|
|
4243
|
+
}
|
|
4244
|
+
TaDelegateUtil2.refusal = refusal;
|
|
4245
|
+
})(TaDelegateUtil || (TaDelegateUtil = {}));
|
|
4246
|
+
|
|
4247
|
+
// ../ta-protocol/esm/common/app-protocol.js
|
|
4248
|
+
var FONT_SIZES = ["small", "standard", "large", "xlarge"];
|
|
4249
|
+
var words = (en, zh, tw, ja) => ({ "en-US": en, "zh-CN": zh, "zh-TW": tw, "ja-JP": ja });
|
|
4250
|
+
var SYSTEM = words("as the system", "跟随系统", "跟隨系統", "システムに合わせる");
|
|
4251
|
+
var FONT_SHOWN = {
|
|
4252
|
+
small: words("small", "小", "小", "小"),
|
|
4253
|
+
standard: words("standard", "标准", "標準", "標準"),
|
|
4254
|
+
large: words("large", "大", "大", "大"),
|
|
4255
|
+
xlarge: words("extra large", "特大", "特大", "特大")
|
|
4256
|
+
};
|
|
4257
|
+
var TA_WEB_SETTINGS = [
|
|
4258
|
+
{
|
|
4259
|
+
key: "theme",
|
|
4260
|
+
type: "enum",
|
|
4261
|
+
values: ["system", "light", "dark"],
|
|
4262
|
+
default: "system",
|
|
4263
|
+
what: "Light or dark look; system follows the device.",
|
|
4264
|
+
title: words("Theme", "外观", "外觀", "外観"),
|
|
4265
|
+
shown: { system: SYSTEM, light: words("light", "浅色", "淺色", "ライト"), dark: words("dark", "深色", "深色", "ダーク") }
|
|
4266
|
+
},
|
|
4267
|
+
{
|
|
4268
|
+
key: "language",
|
|
4269
|
+
type: "enum",
|
|
4270
|
+
values: ["system", ...TaLanguageUtil.ALL],
|
|
4271
|
+
default: "system",
|
|
4272
|
+
what: "The language of Ta's pages and of what Ta says; system follows the device.",
|
|
4273
|
+
title: words("Language", "语言", "語言", "言語"),
|
|
4274
|
+
shown: {
|
|
4275
|
+
system: SYSTEM,
|
|
4276
|
+
...Object.fromEntries(TaLanguageUtil.ALL.map((language2) => [
|
|
4277
|
+
language2,
|
|
4278
|
+
words(TaLanguageUtil.NAMES[language2], TaLanguageUtil.NAMES[language2], TaLanguageUtil.NAMES[language2], TaLanguageUtil.NAMES[language2])
|
|
4279
|
+
]))
|
|
4280
|
+
}
|
|
4281
|
+
},
|
|
4282
|
+
{
|
|
4283
|
+
key: "fontSize",
|
|
4284
|
+
type: "enum",
|
|
4285
|
+
values: FONT_SIZES,
|
|
4286
|
+
default: "standard",
|
|
4287
|
+
what: "The size of the text of the whole page.",
|
|
4288
|
+
title: words("Text size", "字体大小", "字型大小", "文字サイズ"),
|
|
4289
|
+
shown: FONT_SHOWN
|
|
4290
|
+
},
|
|
4291
|
+
{
|
|
4292
|
+
key: "chatFontSize",
|
|
4293
|
+
type: "enum",
|
|
4294
|
+
values: FONT_SIZES,
|
|
4295
|
+
default: "standard",
|
|
4296
|
+
what: "The size of the text of conversations, on top of fontSize.",
|
|
4297
|
+
title: words("Text size of conversations", "对话字体大小", "對話字型大小", "会話の文字サイズ"),
|
|
4298
|
+
shown: FONT_SHOWN
|
|
4299
|
+
},
|
|
4300
|
+
{
|
|
4301
|
+
key: "speak",
|
|
4302
|
+
type: "boolean",
|
|
4303
|
+
default: false,
|
|
4304
|
+
what: "Read every reply aloud.",
|
|
4305
|
+
title: words("Read replies aloud", "朗读回复", "朗讀回覆", "返信を読み上げ")
|
|
4306
|
+
},
|
|
4307
|
+
{
|
|
4308
|
+
key: "speakTalk",
|
|
4309
|
+
type: "boolean",
|
|
4310
|
+
default: false,
|
|
4311
|
+
what: "Read the reply aloud when the user spoke to the double by voice.",
|
|
4312
|
+
title: words("Read aloud what was asked by voice", "语音提问时朗读回复", "語音提問時朗讀回覆", "音声で聞いたら読み上げ")
|
|
4313
|
+
},
|
|
4314
|
+
{
|
|
4315
|
+
key: "callCaptions",
|
|
4316
|
+
type: "boolean",
|
|
4317
|
+
default: false,
|
|
4318
|
+
what: "Show captions in a call: what the user says and what Ta reads.",
|
|
4319
|
+
title: words("Call captions", "通话字幕", "通話字幕", "通話の字幕")
|
|
4320
|
+
},
|
|
4321
|
+
{
|
|
4322
|
+
key: "voice",
|
|
4323
|
+
type: "string",
|
|
4324
|
+
default: "",
|
|
4325
|
+
what: "The voice replies are read with (a voiceURI of the device's voices); empty for the most natural one.",
|
|
4326
|
+
title: words("Voice", "朗读声音", "朗讀聲音", "読み上げの声")
|
|
4327
|
+
},
|
|
4328
|
+
{
|
|
4329
|
+
key: "confirmTasks",
|
|
4330
|
+
type: "boolean",
|
|
4331
|
+
default: false,
|
|
4332
|
+
what: "The double asks before it hands out work.",
|
|
4333
|
+
title: words("Ask before handing out work", "派任务前先问我", "派任務前先問我", "作業を割り当てる前に確認")
|
|
4334
|
+
},
|
|
4335
|
+
{
|
|
4336
|
+
key: "maxTasks",
|
|
4337
|
+
type: "number",
|
|
4338
|
+
values: [0, 2, 4, 8],
|
|
4339
|
+
default: 0,
|
|
4340
|
+
what: "How many of the double's tasks run at once; 0 for as many as it starts.",
|
|
4341
|
+
title: words("Tasks at once", "同时运行的任务", "同時執行的任務", "同時に実行するタスク")
|
|
4342
|
+
},
|
|
4343
|
+
{
|
|
4344
|
+
key: "delegate",
|
|
4345
|
+
type: "enum",
|
|
4346
|
+
values: TaDelegateUtil.LEVELS,
|
|
4347
|
+
default: TaDelegateUtil.DEFAULT,
|
|
4348
|
+
what: "How much of the work the double hands out to tasks rather than doing it itself.",
|
|
4349
|
+
title: words("Hand out work", "派活多少", "派活多少", "作業の割り当て"),
|
|
4350
|
+
shown: { less: words("less", "少", "少", "少なめ"), balanced: words("balanced", "适中", "適中", "ふつう"), more: words("more", "多", "多", "多め") }
|
|
4351
|
+
},
|
|
4352
|
+
{
|
|
4353
|
+
key: "assistantModel",
|
|
4354
|
+
type: "model",
|
|
4355
|
+
default: "",
|
|
4356
|
+
what: "The model of the double, <provider>/<model id>; empty for the default model.",
|
|
4357
|
+
title: words("Model of the double", "分身的模型", "分身的模型", "分身のモデル")
|
|
4358
|
+
},
|
|
4359
|
+
{
|
|
4360
|
+
key: "taskModel",
|
|
4361
|
+
type: "model",
|
|
4362
|
+
default: "",
|
|
4363
|
+
what: "The model of the tasks the double starts; empty for the default model.",
|
|
4364
|
+
title: words("Model of tasks", "任务的模型", "任務的模型", "タスクのモデル")
|
|
4365
|
+
},
|
|
4366
|
+
{ key: "reduceMotion", type: "boolean", default: false, what: "Fewer animations.", title: words("Reduce motion", "减少动画", "減少動畫", "動きを減らす") },
|
|
4367
|
+
{
|
|
4368
|
+
key: "diagramStyle",
|
|
4369
|
+
type: "enum",
|
|
4370
|
+
values: ["clean", "sketch", "colour", "motion"],
|
|
4371
|
+
default: "clean",
|
|
4372
|
+
what: "How diagrams in replies draw: in the colours of the page, as by hand, a colour to a node, or coming in with flowing lines.",
|
|
4373
|
+
title: words("Diagram style", "图表风格", "圖表風格", "図のスタイル"),
|
|
4374
|
+
shown: {
|
|
4375
|
+
clean: words("clean", "简洁", "簡潔", "シンプル"),
|
|
4376
|
+
sketch: words("sketch", "草图", "草圖", "スケッチ"),
|
|
4377
|
+
colour: words("colour", "彩色", "彩色", "カラー"),
|
|
4378
|
+
motion: words("motion", "动效", "動效", "モーション")
|
|
4379
|
+
}
|
|
4380
|
+
},
|
|
4381
|
+
{
|
|
4382
|
+
key: "keepAliveMinutes",
|
|
4383
|
+
type: "number",
|
|
4384
|
+
values: [1, 5, 15, 30],
|
|
4385
|
+
default: 5,
|
|
4386
|
+
what: "How long an extension started on open keeps running once left, in minutes.",
|
|
4387
|
+
title: words("Keep extensions running", "扩展离开后保留", "擴充功能離開後保留", "拡張機能を保持")
|
|
4388
|
+
},
|
|
4389
|
+
{
|
|
4390
|
+
key: "chatFoldDays",
|
|
4391
|
+
type: "number",
|
|
4392
|
+
values: [3, 7, 30, 0],
|
|
4393
|
+
default: 7,
|
|
4394
|
+
what: "Conversations not updated for these days fold at the end of the list; 0 for none.",
|
|
4395
|
+
title: words("Fold old conversations", "折叠旧对话", "摺疊舊對話", "古い会話を折りたたむ")
|
|
4396
|
+
},
|
|
4397
|
+
{
|
|
4398
|
+
key: "chatArchiveDays",
|
|
4399
|
+
type: "number",
|
|
4400
|
+
values: [30, 90, 365, 0],
|
|
4401
|
+
default: 30,
|
|
4402
|
+
what: "Conversations not updated for these days are archived (out of the list, found by search); 0 for none.",
|
|
4403
|
+
title: words("Archive old conversations", "归档旧对话", "歸檔舊對話", "古い会話をアーカイブ")
|
|
4404
|
+
},
|
|
4405
|
+
{
|
|
4406
|
+
key: "recentShots",
|
|
4407
|
+
type: "boolean",
|
|
4408
|
+
default: true,
|
|
4409
|
+
what: "The screenshot just taken is offered above the message box, once.",
|
|
4410
|
+
title: words("Offer recent screenshots", "提示刚截的图", "提示剛截的圖", "撮ったばかりのスクリーンショットを提案")
|
|
4411
|
+
}
|
|
4412
|
+
];
|
|
4413
|
+
var TaAppUtil;
|
|
4414
|
+
(function(TaAppUtil2) {
|
|
4415
|
+
function check(spec, value) {
|
|
4416
|
+
var _a, _b;
|
|
4417
|
+
switch (spec.type) {
|
|
4418
|
+
case "boolean":
|
|
4419
|
+
return typeof value === "boolean" ? undefined : "true or false";
|
|
4420
|
+
case "string":
|
|
4421
|
+
return typeof value === "string" ? undefined : "a string";
|
|
4422
|
+
case "model":
|
|
4423
|
+
return value === "" || typeof value === "string" && /^[^/\s]+\/\S+$/.test(value) ? undefined : "<provider>/<model id>, or empty";
|
|
4424
|
+
default:
|
|
4425
|
+
return ((_a = spec.values) === null || _a === undefined ? undefined : _a.includes(value)) ? undefined : `one of ${(_b = spec.values) === null || _b === undefined ? undefined : _b.map((one) => JSON.stringify(one)).join(", ")}`;
|
|
3363
4426
|
}
|
|
3364
|
-
const context = TaMessageContext.read(message);
|
|
3365
|
-
return TaMessageContext.wrap(context === undefined ? source : `${context}
|
|
3366
|
-
${source}`, TaMessageContext.strip(message));
|
|
3367
4427
|
}
|
|
3368
|
-
|
|
3369
|
-
|
|
3370
|
-
|
|
3371
|
-
|
|
3372
|
-
|
|
3373
|
-
|
|
3374
|
-
`);
|
|
3375
|
-
const kept = lines.filter((line) => !NOTE.test(line.trim()));
|
|
3376
|
-
if (kept.length === lines.length) {
|
|
3377
|
-
return text;
|
|
4428
|
+
TaAppUtil2.check = check;
|
|
4429
|
+
function shown(value, language2, spec) {
|
|
4430
|
+
var _a;
|
|
4431
|
+
const named = (_a = spec === null || spec === undefined ? undefined : spec.shown) === null || _a === undefined ? undefined : _a[String(value)];
|
|
4432
|
+
if (named) {
|
|
4433
|
+
return TaLanguageUtil.pick(language2, named);
|
|
3378
4434
|
}
|
|
3379
|
-
|
|
3380
|
-
|
|
4435
|
+
if (typeof value === "boolean") {
|
|
4436
|
+
return TaLanguageUtil.pick(language2, value ? words("on", "开", "開", "オン") : words("off", "关", "關", "オフ"));
|
|
4437
|
+
}
|
|
4438
|
+
if (value === "" || value === undefined || value === null) {
|
|
4439
|
+
return TaLanguageUtil.pick(language2, words("default", "默认", "預設", "デフォルト"));
|
|
4440
|
+
}
|
|
4441
|
+
return String(value);
|
|
3381
4442
|
}
|
|
3382
|
-
|
|
3383
|
-
})(
|
|
3384
|
-
|
|
3385
|
-
(
|
|
3386
|
-
|
|
3387
|
-
|
|
3388
|
-
|
|
3389
|
-
|
|
4443
|
+
TaAppUtil2.shown = shown;
|
|
4444
|
+
})(TaAppUtil || (TaAppUtil = {}));
|
|
4445
|
+
// ../ta-protocol/esm/common/auth-protocol.js
|
|
4446
|
+
var TaAuthServer = Symbol("TaAuthServer");
|
|
4447
|
+
// ../ta-protocol/esm/common/deployment-protocol.js
|
|
4448
|
+
var TA_WELL_KNOWN_PATH = "/.well-known/ta";
|
|
4449
|
+
|
|
4450
|
+
class TaDeploymentError extends Error {
|
|
4451
|
+
}
|
|
4452
|
+
var TaDeploymentUtil;
|
|
4453
|
+
(function(TaDeploymentUtil2) {
|
|
4454
|
+
TaDeploymentUtil2.DISTRIBUTION_FILE = "distribution.json";
|
|
4455
|
+
function url(value) {
|
|
4456
|
+
if (typeof value !== "string" || !value.trim()) {
|
|
4457
|
+
return;
|
|
4458
|
+
}
|
|
3390
4459
|
try {
|
|
3391
|
-
const
|
|
3392
|
-
|
|
3393
|
-
|
|
4460
|
+
const parsed = new URL(value.trim());
|
|
4461
|
+
if (!/^https?:$/.test(parsed.protocol) || parsed.search || parsed.hash || parsed.username || parsed.password) {
|
|
4462
|
+
return;
|
|
4463
|
+
}
|
|
4464
|
+
return `${parsed.origin}${parsed.pathname}`.replace(/\/+$/, "");
|
|
4465
|
+
} catch (_a) {
|
|
3394
4466
|
return;
|
|
3395
4467
|
}
|
|
3396
|
-
const size = (_c = (_b = (_a = png(bytes)) !== null && _a !== undefined ? _a : gif(bytes)) !== null && _b !== undefined ? _b : webp(bytes)) !== null && _c !== undefined ? _c : jpeg(bytes);
|
|
3397
|
-
return size && size.width > 0 && size.height > 0 ? size : undefined;
|
|
3398
|
-
}
|
|
3399
|
-
TaImageSize2.of = of;
|
|
3400
|
-
function u16be(bytes, at) {
|
|
3401
|
-
return bytes[at] << 8 | bytes[at + 1];
|
|
3402
|
-
}
|
|
3403
|
-
function u16le(bytes, at) {
|
|
3404
|
-
return bytes[at] | bytes[at + 1] << 8;
|
|
3405
|
-
}
|
|
3406
|
-
function u24le(bytes, at) {
|
|
3407
|
-
return bytes[at] | bytes[at + 1] << 8 | bytes[at + 2] << 16;
|
|
3408
|
-
}
|
|
3409
|
-
function u32be(bytes, at) {
|
|
3410
|
-
return (bytes[at] << 24 | bytes[at + 1] << 16 | bytes[at + 2] << 8 | bytes[at + 3]) >>> 0;
|
|
3411
|
-
}
|
|
3412
|
-
function text(bytes, at, length) {
|
|
3413
|
-
return String.fromCharCode(...bytes.subarray(at, at + length));
|
|
3414
4468
|
}
|
|
3415
|
-
|
|
3416
|
-
|
|
4469
|
+
TaDeploymentUtil2.url = url;
|
|
4470
|
+
function name(value) {
|
|
4471
|
+
return typeof value === "string" && value.trim() ? value.trim().slice(0, 80) : undefined;
|
|
3417
4472
|
}
|
|
3418
|
-
function
|
|
3419
|
-
|
|
4473
|
+
function describe(env, own, version) {
|
|
4474
|
+
var _a, _b;
|
|
4475
|
+
const gateway = (_a = url(own.gateway)) !== null && _a !== undefined ? _a : url(env.TA_GATEWAY_URL);
|
|
4476
|
+
const relay = (_b = url(own.relay)) !== null && _b !== undefined ? _b : url(env.TA_RELAY_URL);
|
|
4477
|
+
const called = name(env.TA_DEPLOYMENT_NAME);
|
|
4478
|
+
return { v: 1, ...called ? { name: called } : {}, ...gateway ? { gateway } : {}, ...relay ? { relay } : {}, ...version ? { version } : {} };
|
|
3420
4479
|
}
|
|
3421
|
-
|
|
3422
|
-
|
|
4480
|
+
TaDeploymentUtil2.describe = describe;
|
|
4481
|
+
TaDeploymentUtil2.HEADERS = {
|
|
4482
|
+
"content-type": "application/json",
|
|
4483
|
+
"access-control-allow-origin": "*",
|
|
4484
|
+
"cache-control": "no-store"
|
|
4485
|
+
};
|
|
4486
|
+
function parse(value) {
|
|
4487
|
+
const given = value;
|
|
4488
|
+
if (!given || typeof given !== "object" || given.v !== 1) {
|
|
3423
4489
|
return;
|
|
3424
4490
|
}
|
|
3425
|
-
|
|
3426
|
-
|
|
3427
|
-
|
|
3428
|
-
|
|
3429
|
-
|
|
3430
|
-
|
|
4491
|
+
const deployment = describe({ TA_DEPLOYMENT_NAME: given.name, TA_GATEWAY_URL: given.gateway, TA_RELAY_URL: given.relay }, {});
|
|
4492
|
+
const version = name(given.version);
|
|
4493
|
+
return version ? { ...deployment, version } : deployment;
|
|
4494
|
+
}
|
|
4495
|
+
TaDeploymentUtil2.parse = parse;
|
|
4496
|
+
async function discover(address, timeoutMs = 6000) {
|
|
4497
|
+
const base = url(address);
|
|
4498
|
+
if (!base) {
|
|
4499
|
+
throw new TaDeploymentError(`${address} is not an address of http or https.`);
|
|
4500
|
+
}
|
|
4501
|
+
const controller = new AbortController;
|
|
4502
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
4503
|
+
try {
|
|
4504
|
+
const response = await fetch(`${base}${TA_WELL_KNOWN_PATH}`, { signal: controller.signal, redirect: "follow" });
|
|
4505
|
+
if (!response.ok) {
|
|
4506
|
+
throw new TaDeploymentError(`${base} is not a deployment of Ta (${response.status}).`);
|
|
3431
4507
|
}
|
|
3432
|
-
|
|
3433
|
-
return { width: u24le(bytes, 24) + 1, height: u24le(bytes, 27) + 1 };
|
|
3434
|
-
default:
|
|
4508
|
+
const deployment = parse(await response.json().catch(() => {
|
|
3435
4509
|
return;
|
|
4510
|
+
}));
|
|
4511
|
+
if (!deployment || !deployment.gateway && !deployment.relay) {
|
|
4512
|
+
throw new TaDeploymentError(`${base} is not a deployment of Ta.`);
|
|
4513
|
+
}
|
|
4514
|
+
return deployment;
|
|
4515
|
+
} catch (error) {
|
|
4516
|
+
if ((error === null || error === undefined ? undefined : error.name) === "AbortError") {
|
|
4517
|
+
throw new TaDeploymentError(`${base} did not answer in time.`);
|
|
4518
|
+
}
|
|
4519
|
+
throw error;
|
|
4520
|
+
} finally {
|
|
4521
|
+
clearTimeout(timer);
|
|
3436
4522
|
}
|
|
3437
4523
|
}
|
|
3438
|
-
|
|
3439
|
-
|
|
3440
|
-
|
|
4524
|
+
TaDeploymentUtil2.discover = discover;
|
|
4525
|
+
function distribution(value) {
|
|
4526
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
4527
|
+
throw new TaDeploymentError(`${TaDeploymentUtil2.DISTRIBUTION_FILE} holds a JSON object.`);
|
|
3441
4528
|
}
|
|
3442
|
-
|
|
3443
|
-
|
|
3444
|
-
|
|
3445
|
-
|
|
4529
|
+
const given = value;
|
|
4530
|
+
const result = {};
|
|
4531
|
+
if (given.name !== undefined) {
|
|
4532
|
+
result.name = name(given.name);
|
|
4533
|
+
}
|
|
4534
|
+
for (const key of ["relay", "gateway"]) {
|
|
4535
|
+
if (given[key] !== undefined) {
|
|
4536
|
+
const address = url(given[key]);
|
|
4537
|
+
if (!address) {
|
|
4538
|
+
throw new TaDeploymentError(`"${key}" of ${TaDeploymentUtil2.DISTRIBUTION_FILE} is not an address of http or https.`);
|
|
4539
|
+
}
|
|
4540
|
+
result[key] = address;
|
|
3446
4541
|
}
|
|
3447
|
-
|
|
3448
|
-
|
|
3449
|
-
|
|
4542
|
+
}
|
|
4543
|
+
if (given.updates !== undefined) {
|
|
4544
|
+
const updates = given.updates;
|
|
4545
|
+
const base = url(updates === null || updates === undefined ? undefined : updates.base);
|
|
4546
|
+
if (!base && (updates === null || updates === undefined ? undefined : updates.base) !== undefined) {
|
|
4547
|
+
throw new TaDeploymentError(`"updates.base" of ${TaDeploymentUtil2.DISTRIBUTION_FILE} is not an address of http or https.`);
|
|
3450
4548
|
}
|
|
3451
|
-
|
|
4549
|
+
const key = typeof (updates === null || updates === undefined ? undefined : updates.key) === "string" && updates.key.includes("PUBLIC KEY") ? updates.key : undefined;
|
|
4550
|
+
if ((updates === null || updates === undefined ? undefined : updates.key) !== undefined && !key) {
|
|
4551
|
+
throw new TaDeploymentError(`"updates.key" of ${TaDeploymentUtil2.DISTRIBUTION_FILE} is a public key in PEM.`);
|
|
4552
|
+
}
|
|
4553
|
+
result.updates = { ...base ? { base } : {}, ...key ? { key } : {} };
|
|
3452
4554
|
}
|
|
3453
|
-
|
|
4555
|
+
if (given.reach !== undefined) {
|
|
4556
|
+
result.reach = reach(given.reach);
|
|
4557
|
+
}
|
|
4558
|
+
if (given.inside !== undefined) {
|
|
4559
|
+
const inside = url(given.inside);
|
|
4560
|
+
if (!inside) {
|
|
4561
|
+
throw new TaDeploymentError(`"inside" of ${TaDeploymentUtil2.DISTRIBUTION_FILE} is not an address of http or https.`);
|
|
4562
|
+
}
|
|
4563
|
+
result.inside = inside;
|
|
4564
|
+
}
|
|
4565
|
+
return result;
|
|
4566
|
+
}
|
|
4567
|
+
TaDeploymentUtil2.distribution = distribution;
|
|
4568
|
+
function reach(value) {
|
|
4569
|
+
const given = typeof value === "string" ? { "*": value } : value;
|
|
4570
|
+
const entries = given && typeof given === "object" && !Array.isArray(given) ? Object.entries(given) : [];
|
|
4571
|
+
const result = {};
|
|
4572
|
+
for (const [language2, text] of entries) {
|
|
4573
|
+
if (typeof text !== "string" || !text.trim()) {
|
|
4574
|
+
throw new TaDeploymentError(`"reach.${language2}" of ${TaDeploymentUtil2.DISTRIBUTION_FILE} is not a sentence.`);
|
|
4575
|
+
}
|
|
4576
|
+
result[language2] = text.trim().slice(0, 200);
|
|
4577
|
+
}
|
|
4578
|
+
if (Object.keys(result).length === 0) {
|
|
4579
|
+
throw new TaDeploymentError(`"reach" of ${TaDeploymentUtil2.DISTRIBUTION_FILE} is a sentence, or one by language.`);
|
|
4580
|
+
}
|
|
4581
|
+
return result;
|
|
4582
|
+
}
|
|
4583
|
+
function reachIn(reach2, language2) {
|
|
4584
|
+
var _a, _b, _c, _d, _e;
|
|
4585
|
+
if (!reach2) {
|
|
4586
|
+
return;
|
|
4587
|
+
}
|
|
4588
|
+
const base = language2.split("-")[0];
|
|
4589
|
+
const near = Object.keys(reach2).find((key) => key.split("-")[0] === base);
|
|
4590
|
+
return (_e = (_d = (_c = (_b = (_a = reach2[language2]) !== null && _a !== undefined ? _a : reach2[base]) !== null && _b !== undefined ? _b : near && reach2[near]) !== null && _c !== undefined ? _c : reach2["*"]) !== null && _d !== undefined ? _d : reach2.en) !== null && _e !== undefined ? _e : Object.values(reach2)[0];
|
|
4591
|
+
}
|
|
4592
|
+
TaDeploymentUtil2.reachIn = reachIn;
|
|
4593
|
+
function apply(distribution2, env) {
|
|
4594
|
+
const defaults = { TA_DEPLOYMENT_NAME: distribution2.name, TA_RELAY_URL: distribution2.relay, TA_GATEWAY_URL: distribution2.gateway };
|
|
4595
|
+
for (const [key, value] of Object.entries(defaults)) {
|
|
4596
|
+
if (value && !env[key]) {
|
|
4597
|
+
env[key] = value;
|
|
4598
|
+
}
|
|
4599
|
+
}
|
|
4600
|
+
}
|
|
4601
|
+
TaDeploymentUtil2.apply = apply;
|
|
4602
|
+
})(TaDeploymentUtil || (TaDeploymentUtil = {}));
|
|
4603
|
+
// ../ta-protocol/esm/common/error-protocol.js
|
|
4604
|
+
var TaErrorUtil;
|
|
4605
|
+
(function(TaErrorUtil2) {
|
|
4606
|
+
function code(error) {
|
|
4607
|
+
var _a;
|
|
4608
|
+
var _b;
|
|
4609
|
+
const failure = error;
|
|
4610
|
+
const code2 = (_b = failure === null || failure === undefined ? undefined : failure.code) !== null && _b !== undefined ? _b : (_a = failure === null || failure === undefined ? undefined : failure.data) === null || _a === undefined ? undefined : _a.code;
|
|
4611
|
+
return typeof code2 === "string" ? code2 : undefined;
|
|
4612
|
+
}
|
|
4613
|
+
TaErrorUtil2.code = code;
|
|
4614
|
+
})(TaErrorUtil || (TaErrorUtil = {}));
|
|
4615
|
+
// ../ta-protocol/esm/common/gateway-protocol.js
|
|
4616
|
+
var TaGatewayServer = Symbol("TaGatewayServer");
|
|
4617
|
+
// ../ta-protocol/esm/common/location.js
|
|
4618
|
+
var TaLocation;
|
|
4619
|
+
(function(TaLocation2) {
|
|
4620
|
+
TaLocation2.SSH = "ssh://";
|
|
4621
|
+
TaLocation2.CLOUD = "ta://";
|
|
4622
|
+
const CLOUD_NAME = /^[a-z0-9][a-z0-9-]{0,39}$/;
|
|
4623
|
+
const HOST = /^(?!-)[\w.@:%+[\]-]+$/;
|
|
4624
|
+
function isRemote(location) {
|
|
4625
|
+
return location.startsWith(TaLocation2.SSH) || location.startsWith(TaLocation2.CLOUD);
|
|
4626
|
+
}
|
|
4627
|
+
TaLocation2.isRemote = isRemote;
|
|
4628
|
+
function isCloud(host2) {
|
|
4629
|
+
return !!host2 && host2.startsWith(TaLocation2.CLOUD);
|
|
3454
4630
|
}
|
|
3455
|
-
|
|
3456
|
-
|
|
3457
|
-
(
|
|
3458
|
-
|
|
3459
|
-
|
|
3460
|
-
function
|
|
3461
|
-
|
|
4631
|
+
TaLocation2.isCloud = isCloud;
|
|
4632
|
+
function isCloudName(name) {
|
|
4633
|
+
return CLOUD_NAME.test(name);
|
|
4634
|
+
}
|
|
4635
|
+
TaLocation2.isCloudName = isCloudName;
|
|
4636
|
+
function cloud(name, path2 = "") {
|
|
4637
|
+
return `${TaLocation2.CLOUD}${name}${path2 ? normalize(path2) : ""}`;
|
|
4638
|
+
}
|
|
4639
|
+
TaLocation2.cloud = cloud;
|
|
4640
|
+
function cloudName(hostOrLocation) {
|
|
4641
|
+
if (!hostOrLocation.startsWith(TaLocation2.CLOUD)) {
|
|
3462
4642
|
return;
|
|
3463
4643
|
}
|
|
3464
|
-
|
|
3465
|
-
|
|
3466
|
-
|
|
3467
|
-
function strip(text) {
|
|
3468
|
-
return text.replace(BLOCK, "");
|
|
4644
|
+
const rest = hostOrLocation.slice(TaLocation2.CLOUD.length);
|
|
4645
|
+
const slash = rest.indexOf("/");
|
|
4646
|
+
return slash < 0 ? rest : rest.slice(0, slash);
|
|
3469
4647
|
}
|
|
3470
|
-
|
|
3471
|
-
|
|
3472
|
-
var TaSkillUse;
|
|
3473
|
-
(function(TaSkillUse2) {
|
|
3474
|
-
const BLOCK = /^<skill name="([^"]+)" location="([^"]+)">\n([\s\S]*?)\n<\/skill>(?:\n\n([\s\S]+))?$/;
|
|
3475
|
-
function parse(message) {
|
|
4648
|
+
TaLocation2.cloudName = cloudName;
|
|
4649
|
+
function parse(location) {
|
|
3476
4650
|
var _a;
|
|
3477
|
-
|
|
3478
|
-
if (!match) {
|
|
4651
|
+
if (!isRemote(location)) {
|
|
3479
4652
|
return;
|
|
3480
4653
|
}
|
|
3481
|
-
|
|
4654
|
+
if (location.startsWith(TaLocation2.CLOUD)) {
|
|
4655
|
+
const name = (_a = cloudName(location)) !== null && _a !== undefined ? _a : "";
|
|
4656
|
+
if (!isCloudName(name)) {
|
|
4657
|
+
throw new Error(`${JSON.stringify(location)} names no cloud workspace: write ta://<name>/<path>.`);
|
|
4658
|
+
}
|
|
4659
|
+
const path3 = location.slice(TaLocation2.CLOUD.length + name.length);
|
|
4660
|
+
return { host: `${TaLocation2.CLOUD}${name}`, path: path3 ? normalize(path3) : "" };
|
|
4661
|
+
}
|
|
4662
|
+
const rest = location.slice(TaLocation2.SSH.length);
|
|
4663
|
+
const slash = rest.indexOf("/");
|
|
4664
|
+
const host2 = slash < 0 ? rest : rest.slice(0, slash);
|
|
4665
|
+
const path2 = slash < 0 ? "" : normalize(rest.slice(slash));
|
|
4666
|
+
if (!isHost(host2)) {
|
|
4667
|
+
throw new Error(`${JSON.stringify(location)} names no host: write ssh://<host>/<path>, the host as ssh takes it.`);
|
|
4668
|
+
}
|
|
4669
|
+
return { host: host2, path: path2 };
|
|
3482
4670
|
}
|
|
3483
|
-
|
|
3484
|
-
function
|
|
3485
|
-
return
|
|
4671
|
+
TaLocation2.parse = parse;
|
|
4672
|
+
function isHost(host2) {
|
|
4673
|
+
return isCloud(host2) ? isCloudName(host2.slice(TaLocation2.CLOUD.length)) : HOST.test(host2);
|
|
3486
4674
|
}
|
|
3487
|
-
|
|
3488
|
-
|
|
3489
|
-
|
|
3490
|
-
(
|
|
3491
|
-
|
|
3492
|
-
|
|
3493
|
-
function write(note) {
|
|
3494
|
-
const attributes = [`id="${quote(note.id)}"`];
|
|
3495
|
-
if (note.was !== undefined) {
|
|
3496
|
-
attributes.push(`was="${quote(note.was)}"`);
|
|
4675
|
+
TaLocation2.isHost = isHost;
|
|
4676
|
+
const FLAGS = /^-[46AaCfGgKkMNnqsTtVvXxYy]+$/;
|
|
4677
|
+
function readHost(input) {
|
|
4678
|
+
const words2 = input.trim().split(/\s+/).filter(Boolean);
|
|
4679
|
+
if (words2[0] === "ssh") {
|
|
4680
|
+
words2.shift();
|
|
3497
4681
|
}
|
|
3498
|
-
|
|
3499
|
-
|
|
3500
|
-
|
|
4682
|
+
let destination;
|
|
4683
|
+
let port;
|
|
4684
|
+
let user;
|
|
4685
|
+
const options = [];
|
|
4686
|
+
for (let i = 0;i < words2.length; i++) {
|
|
4687
|
+
const word = words2[i];
|
|
4688
|
+
const glued = /^-([pl])(.+)$/.exec(word);
|
|
4689
|
+
if (word === "-p" || word === "-l" || glued) {
|
|
4690
|
+
const value = glued ? glued[2] : words2[++i];
|
|
4691
|
+
if (value === undefined) {
|
|
4692
|
+
return { error: "invalid" };
|
|
4693
|
+
}
|
|
4694
|
+
if ((glued ? glued[1] : word[1]) === "p") {
|
|
4695
|
+
port = value;
|
|
4696
|
+
} else {
|
|
4697
|
+
user = value;
|
|
4698
|
+
}
|
|
4699
|
+
} else if (word === "-o" || word.startsWith("-o")) {
|
|
4700
|
+
const value = word === "-o" ? words2[++i] : word.slice(2);
|
|
4701
|
+
const option = /^(port|user)\s*=?\s*(.+)$/i.exec(value !== null && value !== undefined ? value : "");
|
|
4702
|
+
if (!option) {
|
|
4703
|
+
options.push(`-o ${value !== null && value !== undefined ? value : ""}`.trim());
|
|
4704
|
+
} else if (option[1].toLowerCase() === "port") {
|
|
4705
|
+
port = option[2];
|
|
4706
|
+
} else {
|
|
4707
|
+
user = option[2];
|
|
4708
|
+
}
|
|
4709
|
+
} else if (FLAGS.test(word)) {} else if (word.startsWith("-")) {
|
|
4710
|
+
options.push(/^-[bBcDeEFiIJLmOQRSwW]$/.test(word) && words2[i + 1] !== undefined ? `${word} ${words2[++i]}` : word);
|
|
4711
|
+
} else if (destination === undefined) {
|
|
4712
|
+
destination = word;
|
|
4713
|
+
} else {
|
|
4714
|
+
return { error: "invalid" };
|
|
4715
|
+
}
|
|
3501
4716
|
}
|
|
3502
|
-
|
|
3503
|
-
|
|
3504
|
-
</task_${note.kind}>`;
|
|
3505
|
-
}
|
|
3506
|
-
TaTaskNote2.write = write;
|
|
3507
|
-
function parse(text) {
|
|
3508
|
-
const match = NOTE.exec(text.trim());
|
|
3509
|
-
if (!match) {
|
|
3510
|
-
return;
|
|
4717
|
+
if (options.length > 0) {
|
|
4718
|
+
return { error: "options", options };
|
|
3511
4719
|
}
|
|
3512
|
-
|
|
3513
|
-
|
|
3514
|
-
attributes[name] = unquote(value);
|
|
4720
|
+
if (!destination) {
|
|
4721
|
+
return { error: "empty" };
|
|
3515
4722
|
}
|
|
3516
|
-
|
|
3517
|
-
|
|
4723
|
+
let host2 = destination.replace(/^ssh:\/\//, "").replace(/\/.*$/, "");
|
|
4724
|
+
if (user && !host2.includes("@")) {
|
|
4725
|
+
host2 = `${user}@${host2}`;
|
|
3518
4726
|
}
|
|
3519
|
-
|
|
3520
|
-
|
|
4727
|
+
if (port) {
|
|
4728
|
+
if (!/^\d{1,5}$/.test(port) || /:\d+$/.test(host2)) {
|
|
4729
|
+
return { error: "invalid" };
|
|
4730
|
+
}
|
|
4731
|
+
host2 = port === "22" ? host2 : `${host2}:${port}`;
|
|
4732
|
+
}
|
|
4733
|
+
return isHost(host2) ? { host: host2 } : { error: "invalid" };
|
|
3521
4734
|
}
|
|
3522
|
-
|
|
3523
|
-
function
|
|
3524
|
-
return
|
|
4735
|
+
TaLocation2.readHost = readHost;
|
|
4736
|
+
function ssh(host2, path2 = "") {
|
|
4737
|
+
return isCloud(host2) ? `${host2}${path2 ? normalize(path2) : ""}` : `${TaLocation2.SSH}${host2}${path2 ? normalize(path2) : ""}`;
|
|
3525
4738
|
}
|
|
3526
|
-
|
|
3527
|
-
|
|
4739
|
+
TaLocation2.ssh = ssh;
|
|
4740
|
+
function hostLabel(host2) {
|
|
4741
|
+
if (isCloud(host2)) {
|
|
4742
|
+
return host2.slice(TaLocation2.CLOUD.length);
|
|
4743
|
+
}
|
|
4744
|
+
const name = host2.replace(/^[^@]*@/, "").replace(/^\[(.*)\](?::\d+)?$/, "$1").replace(/^([^:]*):\d+$/, "$1");
|
|
4745
|
+
return /^[\d.]+$/.test(name) || name.includes(":") ? name : name.split(".")[0] || name;
|
|
3528
4746
|
}
|
|
3529
|
-
|
|
3530
|
-
|
|
3531
|
-
|
|
3532
|
-
|
|
3533
|
-
const REF = /\$\{?TA_SECRET_([A-Z0-9][A-Z0-9_]*)\}?/g;
|
|
3534
|
-
const KINDS = [
|
|
3535
|
-
[/^PHONE(?:_\d+)?$/, "phone"],
|
|
3536
|
-
[/^EMAIL(?:_\d+)?$/, "email"],
|
|
3537
|
-
[/^ID_NUMBER(?:_\d+)?$/, "idNumber"],
|
|
3538
|
-
[/^BANK_CARD(?:_\d+)?$/, "bankCard"]
|
|
3539
|
-
];
|
|
3540
|
-
function has(text) {
|
|
3541
|
-
return text.includes("TA_SECRET_") && new RegExp(REF.source).test(text);
|
|
4747
|
+
TaLocation2.hostLabel = hostLabel;
|
|
4748
|
+
function host(location) {
|
|
4749
|
+
var _a;
|
|
4750
|
+
return isRemote(location) ? (_a = parse(location)) === null || _a === undefined ? undefined : _a.host : undefined;
|
|
3542
4751
|
}
|
|
3543
|
-
|
|
3544
|
-
function
|
|
4752
|
+
TaLocation2.host = host;
|
|
4753
|
+
function path(location) {
|
|
3545
4754
|
var _a;
|
|
3546
4755
|
var _b;
|
|
3547
|
-
return (_b = (_a =
|
|
4756
|
+
return (_b = (_a = parse(location)) === null || _a === undefined ? undefined : _a.path) !== null && _b !== undefined ? _b : location;
|
|
3548
4757
|
}
|
|
3549
|
-
|
|
3550
|
-
function
|
|
3551
|
-
|
|
3552
|
-
|
|
4758
|
+
TaLocation2.path = path;
|
|
4759
|
+
function parent(location) {
|
|
4760
|
+
const remote = parse(location);
|
|
4761
|
+
if (remote) {
|
|
4762
|
+
const above = remote.path ? parentPath(remote.path) : undefined;
|
|
4763
|
+
return above === undefined ? undefined : ssh(remote.host, above);
|
|
3553
4764
|
}
|
|
3554
|
-
|
|
3555
|
-
|
|
3556
|
-
|
|
3557
|
-
|
|
3558
|
-
|
|
3559
|
-
|
|
3560
|
-
|
|
3561
|
-
at = match.index + match[0].length;
|
|
4765
|
+
return parentPath(location);
|
|
4766
|
+
}
|
|
4767
|
+
TaLocation2.parent = parent;
|
|
4768
|
+
function child(location, name) {
|
|
4769
|
+
const remote = parse(location);
|
|
4770
|
+
if (remote) {
|
|
4771
|
+
return ssh(remote.host, childPath(remote.path || "/", name));
|
|
3562
4772
|
}
|
|
3563
|
-
|
|
3564
|
-
|
|
4773
|
+
return childPath(location, name);
|
|
4774
|
+
}
|
|
4775
|
+
TaLocation2.child = child;
|
|
4776
|
+
function basename(location) {
|
|
4777
|
+
const remote = parse(location);
|
|
4778
|
+
const path2 = (remote ? remote.path : location).replace(/[\\/]+$/, "");
|
|
4779
|
+
const name = path2.slice(Math.max(path2.lastIndexOf("/"), path2.lastIndexOf("\\")) + 1);
|
|
4780
|
+
return name || (remote === null || remote === undefined ? undefined : remote.host) || location;
|
|
4781
|
+
}
|
|
4782
|
+
TaLocation2.basename = basename;
|
|
4783
|
+
function homeRelative(location) {
|
|
4784
|
+
var _a;
|
|
4785
|
+
const remote = parse(location);
|
|
4786
|
+
const path2 = remote ? remote.path ? remote.path.slice(1) : "~" : location;
|
|
4787
|
+
const match = /^~(?:[\\/]+(.*))?$/.exec(path2);
|
|
4788
|
+
return match ? ((_a = match[1]) !== null && _a !== undefined ? _a : "").replace(/[\\/]+$/, "") : undefined;
|
|
4789
|
+
}
|
|
4790
|
+
TaLocation2.homeRelative = homeRelative;
|
|
4791
|
+
function isAbsolute2(location) {
|
|
4792
|
+
if (homeRelative(location) !== undefined) {
|
|
4793
|
+
return false;
|
|
3565
4794
|
}
|
|
3566
|
-
return
|
|
4795
|
+
return isRemote(location) || /^([\\/]|[A-Za-z]:)/.test(location);
|
|
3567
4796
|
}
|
|
3568
|
-
|
|
3569
|
-
function
|
|
3570
|
-
|
|
4797
|
+
TaLocation2.isAbsolute = isAbsolute2;
|
|
4798
|
+
function parentPath(path2) {
|
|
4799
|
+
const trimmed = path2.replace(/[\\/]+$/, "");
|
|
4800
|
+
const at = Math.max(trimmed.lastIndexOf("/"), trimmed.lastIndexOf("\\"));
|
|
4801
|
+
if (at < 0) {
|
|
4802
|
+
return;
|
|
4803
|
+
}
|
|
4804
|
+
const head = trimmed.slice(0, at);
|
|
4805
|
+
return head === "" || /^[A-Za-z]:$/.test(head) ? head + trimmed[at] : head;
|
|
3571
4806
|
}
|
|
3572
|
-
|
|
3573
|
-
|
|
4807
|
+
function childPath(path2, name) {
|
|
4808
|
+
const separator = path2.includes("/") || !path2.includes("\\") ? "/" : "\\";
|
|
4809
|
+
return path2.endsWith(separator) ? path2 + name : path2 + separator + name;
|
|
4810
|
+
}
|
|
4811
|
+
function normalize(path2) {
|
|
4812
|
+
const squeezed = `/${path2}`.replace(/\/+/g, "/");
|
|
4813
|
+
return squeezed.length > 1 ? squeezed.replace(/\/$/, "") : squeezed;
|
|
4814
|
+
}
|
|
4815
|
+
})(TaLocation || (TaLocation = {}));
|
|
3574
4816
|
// ../ta-protocol/esm/common/package-protocol.js
|
|
3575
4817
|
var TaPackageServer = Symbol("TaPackageServer");
|
|
3576
4818
|
var TaPackageTextUtil;
|
|
@@ -3613,14 +4855,14 @@ var TaPathMap;
|
|
|
3613
4855
|
return rest(path2, base) !== undefined;
|
|
3614
4856
|
}
|
|
3615
4857
|
TaPathMap2.under = under;
|
|
3616
|
-
function
|
|
4858
|
+
function join4(base, parts2) {
|
|
3617
4859
|
if (!parts2.length) {
|
|
3618
4860
|
return base;
|
|
3619
4861
|
}
|
|
3620
4862
|
const sep = isWindows(base) ? "\\" : "/";
|
|
3621
4863
|
return `${base.replace(/[\\/]+$/, "")}${sep}${parts2.join(sep)}`;
|
|
3622
4864
|
}
|
|
3623
|
-
TaPathMap2.join =
|
|
4865
|
+
TaPathMap2.join = join4;
|
|
3624
4866
|
function basename(path2) {
|
|
3625
4867
|
var _a;
|
|
3626
4868
|
return (_a = path2.split(/[\\/]+/).filter(Boolean).pop()) !== null && _a !== undefined ? _a : "";
|
|
@@ -3630,7 +4872,7 @@ var TaPathMap;
|
|
|
3630
4872
|
for (const [from, to] of deepest(pairs)) {
|
|
3631
4873
|
const found = rest(value, from);
|
|
3632
4874
|
if (found) {
|
|
3633
|
-
return
|
|
4875
|
+
return join4(to, found);
|
|
3634
4876
|
}
|
|
3635
4877
|
}
|
|
3636
4878
|
return value;
|
|
@@ -3648,7 +4890,7 @@ var TaPathMap;
|
|
|
3648
4890
|
const before = match[1];
|
|
3649
4891
|
const found = order.findIndex((_, i) => match[i + 2] !== undefined);
|
|
3650
4892
|
const tail = (_a = match[order.length + 2]) !== null && _a !== undefined ? _a : "";
|
|
3651
|
-
return `${before}${
|
|
4893
|
+
return `${before}${join4(order[found][1], tail.split(/[\\/]+/).filter(Boolean))}`;
|
|
3652
4894
|
});
|
|
3653
4895
|
}
|
|
3654
4896
|
TaPathMap2.text = text;
|
|
@@ -3734,6 +4976,7 @@ var TaRemoteServer = Symbol("TaRemoteServer");
|
|
|
3734
4976
|
// ../ta-protocol/esm/common/room-protocol.js
|
|
3735
4977
|
var TaRoomServer = Symbol("TaRoomServer");
|
|
3736
4978
|
var TA_PICTURE = { key: "picture:", maxChars: 16 * 1024 };
|
|
4979
|
+
var TA_CHAT_KEY = "chat:";
|
|
3737
4980
|
var TaRoomWatchUtil;
|
|
3738
4981
|
(function(TaRoomWatchUtil2) {
|
|
3739
4982
|
function of(messages, state) {
|
|
@@ -3761,6 +5004,9 @@ var TaRoomWatchUtil;
|
|
|
3761
5004
|
if (event.type === "message") {
|
|
3762
5005
|
return watch.messages;
|
|
3763
5006
|
}
|
|
5007
|
+
if (event.type === "chat") {
|
|
5008
|
+
return watch.messages || watch.state.some((prefix) => TA_CHAT_KEY.startsWith(prefix) || prefix.startsWith(TA_CHAT_KEY));
|
|
5009
|
+
}
|
|
3764
5010
|
if (event.type === "state") {
|
|
3765
5011
|
return watch.messages || watch.state.some((prefix) => event.key.startsWith(prefix));
|
|
3766
5012
|
}
|
|
@@ -3793,8 +5039,8 @@ var TaSaid;
|
|
|
3793
5039
|
}
|
|
3794
5040
|
TaSaid2.text = text;
|
|
3795
5041
|
function words2(role, text2, options = {}) {
|
|
3796
|
-
const
|
|
3797
|
-
return withoutFences(
|
|
5042
|
+
const said2 = role === "user" ? userWords(text2) : TaAgentSummary.strip(text2);
|
|
5043
|
+
return withoutFences(said2, (language2) => options.cards === false && CARD.test(language2), true).trim();
|
|
3798
5044
|
}
|
|
3799
5045
|
TaSaid2.words = words2;
|
|
3800
5046
|
function of(message, options) {
|
|
@@ -3917,14 +5163,14 @@ Switched to voice.`, said: "Switched to voice." },
|
|
|
3917
5163
|
Ready.`, said: "Ready." }
|
|
3918
5164
|
];
|
|
3919
5165
|
function userWords(text2) {
|
|
3920
|
-
const
|
|
3921
|
-
const skill = TaSkillUse.parse(
|
|
5166
|
+
const said2 = TaImageNote.strip(TaMessageContext.strip(text2));
|
|
5167
|
+
const skill = TaSkillUse.parse(said2);
|
|
3922
5168
|
if (skill) {
|
|
3923
5169
|
return TaSkillUse.command(skill);
|
|
3924
5170
|
}
|
|
3925
|
-
const note = TaTaskNote.parse(
|
|
5171
|
+
const note = TaTaskNote.parse(said2);
|
|
3926
5172
|
if (!note) {
|
|
3927
|
-
return
|
|
5173
|
+
return said2;
|
|
3928
5174
|
}
|
|
3929
5175
|
if (note.kind === "renamed") {
|
|
3930
5176
|
return "";
|
|
@@ -3937,9 +5183,9 @@ Ready.`, said: "Ready." }
|
|
|
3937
5183
|
let left = false;
|
|
3938
5184
|
for (const part of text2.split(`
|
|
3939
5185
|
`)) {
|
|
3940
|
-
const
|
|
3941
|
-
if (!fence &&
|
|
3942
|
-
fence = { mark:
|
|
5186
|
+
const open2 = FENCE.exec(part);
|
|
5187
|
+
if (!fence && open2) {
|
|
5188
|
+
fence = { mark: open2[1], dropped: drop(open2[2]) };
|
|
3943
5189
|
} else if (fence && closes(part, fence.mark)) {
|
|
3944
5190
|
const dropped = fence.dropped;
|
|
3945
5191
|
fence = undefined;
|
|
@@ -3964,9 +5210,9 @@ Ready.`, said: "Ready." }
|
|
|
3964
5210
|
let fence;
|
|
3965
5211
|
for (const part of text2.split(`
|
|
3966
5212
|
`)) {
|
|
3967
|
-
const
|
|
3968
|
-
if (!fence &&
|
|
3969
|
-
fence = { mark:
|
|
5213
|
+
const open2 = FENCE.exec(part);
|
|
5214
|
+
if (!fence && open2) {
|
|
5215
|
+
fence = { mark: open2[1], card: CARD.test(open2[2]) };
|
|
3970
5216
|
} else if (fence && closes(part, fence.mark)) {
|
|
3971
5217
|
fence = undefined;
|
|
3972
5218
|
} else if (fence && !fence.card) {
|
|
@@ -4239,7 +5485,7 @@ var PictureHost;
|
|
|
4239
5485
|
if (typeof file !== "string") {
|
|
4240
5486
|
throw new Error("Give the path of the picture: a PNG, JPEG, WebP or SVG file.");
|
|
4241
5487
|
}
|
|
4242
|
-
const bytes = new Uint8Array(await
|
|
5488
|
+
const bytes = new Uint8Array(await readFile3(resolve(file)));
|
|
4243
5489
|
const type = typeOf(bytes);
|
|
4244
5490
|
if (!type) {
|
|
4245
5491
|
throw new Error(`${file} is not a PNG, JPEG, WebP or SVG picture.`);
|