@celljs/ta-relay 4.21.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.
Files changed (88) hide show
  1. package/esm/common/index.d.ts +3 -0
  2. package/esm/common/index.js +3 -0
  3. package/esm/common/relay-blobs.d.ts +36 -2
  4. package/esm/common/relay-blobs.js +33 -5
  5. package/esm/common/relay-chat-store.d.ts +59 -0
  6. package/esm/common/relay-chat-store.js +40 -0
  7. package/esm/common/relay-hub-charter.d.ts +7 -1
  8. package/esm/common/relay-hub-charter.js +6 -0
  9. package/esm/common/relay-hub-log.d.ts +65 -0
  10. package/esm/common/relay-hub-log.js +147 -0
  11. package/esm/common/relay-hub.d.ts +3 -0
  12. package/esm/common/relay-hub.js +18 -0
  13. package/esm/common/relay-hubs-admins.d.ts +1 -0
  14. package/esm/common/relay-hubs-admins.js +10 -6
  15. package/esm/common/relay-hubs-chats.d.ts +66 -0
  16. package/esm/common/relay-hubs-chats.js +342 -0
  17. package/esm/common/relay-hubs-connection.d.ts +3 -1
  18. package/esm/common/relay-hubs-connection.js +7 -0
  19. package/esm/common/relay-hubs-protocol.d.ts +14 -0
  20. package/esm/common/relay-hubs.d.ts +5 -2
  21. package/esm/common/relay-hubs.js +31 -13
  22. package/esm/common/relay-info.d.ts +23 -1
  23. package/esm/common/relay-info.js +25 -2
  24. package/esm/common/relay-protocol.d.ts +60 -1
  25. package/esm/common/relay-protocol.js +15 -0
  26. package/esm/common/relay-room-app.d.ts +106 -0
  27. package/esm/common/relay-room-app.js +171 -0
  28. package/esm/common/relay-room-chat.d.ts +126 -4
  29. package/esm/common/relay-room-chat.js +230 -6
  30. package/esm/guest/guest-i18n.d.ts +5 -0
  31. package/esm/guest/guest-i18n.js +20 -0
  32. package/esm/guest/guest-style.d.ts +1 -1
  33. package/esm/guest/guest-style.js +11 -0
  34. package/esm/guest/guest.js +84 -11
  35. package/esm/node/index.d.ts +1 -0
  36. package/esm/node/index.js +1 -0
  37. package/esm/node/relay-chat-files.d.ts +46 -0
  38. package/esm/node/relay-chat-files.js +199 -0
  39. package/esm/node/relay-hub-files.d.ts +11 -1
  40. package/esm/node/relay-hub-files.js +8 -1
  41. package/esm/node/relay-server.d.ts +7 -1
  42. package/esm/node/relay-server.js +23 -13
  43. package/lib/common/index.d.ts +3 -0
  44. package/lib/common/index.js +3 -0
  45. package/lib/common/relay-blobs.d.ts +36 -2
  46. package/lib/common/relay-blobs.js +34 -6
  47. package/lib/common/relay-chat-store.d.ts +60 -0
  48. package/lib/common/relay-chat-store.js +44 -0
  49. package/lib/common/relay-hub-charter.d.ts +7 -1
  50. package/lib/common/relay-hub-charter.js +7 -1
  51. package/lib/common/relay-hub-log.d.ts +66 -0
  52. package/lib/common/relay-hub-log.js +151 -0
  53. package/lib/common/relay-hub.d.ts +3 -0
  54. package/lib/common/relay-hub.js +18 -0
  55. package/lib/common/relay-hubs-admins.d.ts +1 -0
  56. package/lib/common/relay-hubs-admins.js +9 -5
  57. package/lib/common/relay-hubs-chats.d.ts +67 -0
  58. package/lib/common/relay-hubs-chats.js +346 -0
  59. package/lib/common/relay-hubs-connection.d.ts +3 -1
  60. package/lib/common/relay-hubs-connection.js +7 -0
  61. package/lib/common/relay-hubs-protocol.d.ts +14 -0
  62. package/lib/common/relay-hubs.d.ts +5 -2
  63. package/lib/common/relay-hubs.js +30 -12
  64. package/lib/common/relay-info.d.ts +23 -1
  65. package/lib/common/relay-info.js +26 -3
  66. package/lib/common/relay-protocol.d.ts +60 -1
  67. package/lib/common/relay-protocol.js +16 -1
  68. package/lib/common/relay-room-app.d.ts +107 -0
  69. package/lib/common/relay-room-app.js +176 -0
  70. package/lib/common/relay-room-chat.d.ts +126 -4
  71. package/lib/common/relay-room-chat.js +231 -7
  72. package/lib/guest/guest-i18n.d.ts +5 -0
  73. package/lib/guest/guest-i18n.js +20 -0
  74. package/lib/guest/guest-style.d.ts +1 -1
  75. package/lib/guest/guest-style.js +11 -0
  76. package/lib/guest/guest.js +83 -10
  77. package/lib/node/index.d.ts +1 -0
  78. package/lib/node/index.js +1 -0
  79. package/lib/node/relay-chat-files.d.ts +47 -0
  80. package/lib/node/relay-chat-files.js +203 -0
  81. package/lib/node/relay-hub-files.d.ts +11 -1
  82. package/lib/node/relay-hub-files.js +8 -1
  83. package/lib/node/relay-server.d.ts +7 -1
  84. package/lib/node/relay-server.js +21 -11
  85. package/package.json +3 -3
  86. package/viewer/join.html +17 -6
  87. package/viewer/page.html +10 -10
  88. package/viewer/room.html +26 -15
@@ -1,9 +1,9 @@
1
1
  import { RelayFriendSends } from './relay-friend-sends.js';
2
2
  import { RELAY_FRIENDS_KIND, RelayFriends, RelayFriendsUtil } from './relay-friends.js';
3
3
  import { RelayGroupInvites } from './relay-group-invites.js';
4
- import { RelayHubCharterUtil } from './relay-hub-charter.js';
4
+ import { RELAY_GUESTS_PER_HOUR, RELAY_GUESTS_RULE, RelayHubCharterUtil } from './relay-hub-charter.js';
5
5
  import { RelayHubUtil } from './relay-hub-util.js';
6
- import { RelayHubsAdmins } from './relay-hubs-admins.js';
6
+ import { RelayHubsChats } from './relay-hubs-chats.js';
7
7
  import { Live, OPEN, READY_MS } from './relay-hubs-live.js';
8
8
  import { RelayIdentityUtil, RelayKeys } from './relay-identity.js';
9
9
  import { RELAY_INVITE_MAX_DAYS, RELAY_ROOM_MAX_MEMBERS } from './relay-protocol.js';
@@ -14,7 +14,7 @@ const DAY_MS = 24 * 60 * 60 * 1000;
14
14
  * the others; every message signed by its member and sealed with the key of the room. The friends are a room of
15
15
  * their own (RelayFriends).
16
16
  */
17
- export class RelayHubs extends RelayHubsAdmins {
17
+ export class RelayHubs extends RelayHubsChats {
18
18
  constructor() {
19
19
  super(...arguments);
20
20
  this.book = new RelayFriends({
@@ -62,6 +62,7 @@ export class RelayHubs extends RelayHubsAdmins {
62
62
  getState: (room, prefix) => this.getState(room, prefix),
63
63
  setState: (room, key, value) => this.setState(room, key, value),
64
64
  }, this.options.store);
65
+ this.guestsIn = new Map();
65
66
  }
66
67
  /** Connects to the rooms of rooms.json, and keeps connected. */
67
68
  start() {
@@ -229,8 +230,8 @@ export class RelayHubs extends RelayHubsAdmins {
229
230
  }
230
231
  /** The user's answer to one asking to join: admitted, it gets the key of the room and is in the roster. */
231
232
  async answer(roomId, askId, accept) {
232
- var _a, _b;
233
- var _c;
233
+ var _a, _b, _c;
234
+ var _d, _e;
234
235
  const live = this.managed(roomId);
235
236
  const asked = live.asks.get(askId);
236
237
  if (!asked) {
@@ -251,7 +252,7 @@ export class RelayHubs extends RelayHubsAdmins {
251
252
  ...(live.record.unlisted ? { unlisted: true } : {}),
252
253
  };
253
254
  const ask = asked.body;
254
- if (((_c = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.members.length) !== null && _c !== void 0 ? _c : 0) >= this.capacityOf(live)) {
255
+ if (((_d = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.members.length) !== null && _d !== void 0 ? _d : 0) >= this.capacityOf(live)) {
255
256
  throw new Error('The room is full.');
256
257
  }
257
258
  const head = live.record.owner;
@@ -261,7 +262,7 @@ export class RelayHubs extends RelayHubsAdmins {
261
262
  const answer = await RelayHubUtil.sealAnswer(identity, live.record.id, ask, body, asked.invite, proof);
262
263
  const member = { ...RelayIdentityUtil.publicOf(ask), member: ask.member, ...(ask.guest ? { guest: true } : {}), at: Date.now() };
263
264
  const roster = await this.nextRoster(live, members => [...members.filter(other => other.member !== ask.member), member]);
264
- this.request(live, { t: 'admit', ask: askId, answer, roster });
265
+ this.request(live, { t: 'admit', ask: askId, answer, roster, history: (_e = (_c = live.charter) === null || _c === void 0 ? void 0 : _c.history) !== null && _e !== void 0 ? _e : 0 });
265
266
  }
266
267
  live.asks.delete(askId);
267
268
  this.emit({ type: 'rooms' });
@@ -298,6 +299,7 @@ export class RelayHubs extends RelayHubsAdmins {
298
299
  this.disconnect(live);
299
300
  this.live.delete(roomId);
300
301
  this.save();
302
+ await this.forgetChat(roomId);
301
303
  this.emit({ type: 'rooms' });
302
304
  }
303
305
  /** To every other member, or to one (`to`, its member id). */
@@ -443,7 +445,7 @@ export class RelayHubs extends RelayHubsAdmins {
443
445
  return this.book.ids();
444
446
  }
445
447
  heardAsk(live, ask) {
446
- var _a;
448
+ var _a, _b;
447
449
  if (this.isFriends(live)) {
448
450
  this.book.asked(ask);
449
451
  }
@@ -451,10 +453,25 @@ export class RelayHubs extends RelayHubsAdmins {
451
453
  // A friend the user invited: in at once, as the user chose it; asked as any other should that fail.
452
454
  void this.answer(live.record.id, ask.member, true).then(() => this.groups.admitted(live.record, ask.id), () => super.heardAsk(live, ask));
453
455
  }
456
+ else if (ask.guest && ((_b = live.charter) === null || _b === void 0 ? void 0 : _b.rules[RELAY_GUESTS_RULE]) && this.manages(live) && this.guestsLetIn(live.record.id)) {
457
+ // The owner lets those of a browser in without asking (they only chat), so many an hour; asked past it, or should that fail.
458
+ void this.answer(live.record.id, ask.member, true).catch(() => super.heardAsk(live, ask));
459
+ }
454
460
  else {
455
461
  super.heardAsk(live, ask);
456
462
  }
457
463
  }
464
+ /** One more guest let in without asking fits in the hour of a room (RELAY_GUESTS_PER_HOUR): counted. */
465
+ guestsLetIn(room) {
466
+ var _a;
467
+ const now = Date.now();
468
+ const times = ((_a = this.guestsIn.get(room)) !== null && _a !== void 0 ? _a : []).filter(at => now - at < 60 * 60 * 1000);
469
+ if (times.length >= RELAY_GUESTS_PER_HOUR) {
470
+ return false;
471
+ }
472
+ this.guestsIn.set(room, [...times, now]);
473
+ return true;
474
+ }
458
475
  foreignSet(live, key) {
459
476
  // Not awaited: the owner clears what one set under another's name, as soon as it is in the room.
460
477
  void this.setState(live.record.id, key, null).catch(() => undefined);
@@ -482,10 +499,10 @@ export class RelayHubs extends RelayHubsAdmins {
482
499
  }
483
500
  }
484
501
  info(live) {
485
- var _a, _b;
486
- var _c, _d;
502
+ var _a, _b, _c;
503
+ var _d, _e;
487
504
  const { record } = live;
488
- const members = ((_c = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.members) !== null && _c !== void 0 ? _c : []).map(member => { var _a; return ({
505
+ const members = ((_d = (_a = live.roster) === null || _a === void 0 ? void 0 : _a.members) !== null && _d !== void 0 ? _d : []).map(member => { var _a; return ({
489
506
  member: member.member,
490
507
  id: member.id,
491
508
  name: member.name,
@@ -505,12 +522,13 @@ export class RelayHubs extends RelayHubsAdmins {
505
522
  relay: record.relay,
506
523
  owner: record.own,
507
524
  ...(!record.own && this.isAdmin(live, record.member) ? { admin: true } : {}),
508
- ...(record.own ? { capacity: (_d = record.capacity) !== null && _d !== void 0 ? _d : RELAY_ROOM_MAX_MEMBERS } : {}),
525
+ ...(record.own ? { capacity: (_e = record.capacity) !== null && _e !== void 0 ? _e : RELAY_ROOM_MAX_MEMBERS } : {}),
509
526
  ...(live.charter && Object.keys(live.charter.rules).length ? { rules: { ...live.charter.rules } } : {}),
527
+ ...(((_b = live.charter) === null || _b === void 0 ? void 0 : _b.history) ? { history: live.charter.history } : {}),
510
528
  ownerName: record.owner.name,
511
529
  status: record.status,
512
530
  ...(record.reason ? { reason: record.reason } : {}),
513
- connected: ((_b = live.socket) === null || _b === void 0 ? void 0 : _b.readyState) === OPEN && (live.synced || record.status === 'waiting'),
531
+ connected: ((_c = live.socket) === null || _c === void 0 ? void 0 : _c.readyState) === OPEN && (live.synced || record.status === 'waiting'),
514
532
  members,
515
533
  createdAt: record.createdAt,
516
534
  ...(record.joinedAt ? { joinedAt: record.joinedAt } : {}),
@@ -19,7 +19,24 @@ export interface RelayInfo {
19
19
  limits: RelayLimits;
20
20
  /** Whether rooms and copies stay when the relay restarts. */
21
21
  persistent: boolean;
22
+ /**
23
+ * Sealed copies of the pictures of chats its operator lets it keep (RELAY_MEDIA_PATH), for whoever comes while no
24
+ * member who has one is online: so many days, so many bytes each. None (the default): it keeps none.
25
+ */
26
+ media?: RelayMedia;
22
27
  }
28
+ /** What a relay keeps of the pictures of chats (RelayInfo.media), as its operator set it. */
29
+ export interface RelayMedia {
30
+ days: number;
31
+ bytes: number;
32
+ }
33
+ /** The most an operator may let a relay keep of a picture: as long and as large as a copy of content. */
34
+ export declare const RELAY_MEDIA_MAX: RelayMedia;
35
+ /**
36
+ * Days a relay keeps a picture of a chat unless its operator says otherwise: short, so that what it keeps stays small
37
+ * however many use it, long enough for the others to open a chat the sender left while away.
38
+ */
39
+ export declare const RELAY_MEDIA_DEFAULT_DAYS = 1;
23
40
  /** A relay that answers no RELAY_INFO_PATH, made before it: all it can do, with the limits of then. */
24
41
  export declare const RELAY_LEGACY_INFO: RelayInfo;
25
42
  /** What a probe of a relay found. */
@@ -33,7 +50,12 @@ export interface RelayProbe {
33
50
  error?: string;
34
51
  }
35
52
  export declare namespace RelayInfoUtil {
36
- function of(persistent: boolean): RelayInfo;
53
+ function of(persistent: boolean, media?: RelayMedia): RelayInfo;
54
+ /**
55
+ * What the operator of a relay lets it keep of the pictures of chats, from its settings (`TA_MEDIA_DAYS`,
56
+ * `TA_MEDIA_MAX_MB`): RELAY_MEDIA_DEFAULT_DAYS unless set, none with 0, within RELAY_MEDIA_MAX.
57
+ */
58
+ function media(days: unknown, megabytes?: unknown): RelayMedia | undefined;
37
59
  /** The answer of RELAY_INFO_PATH: JSON, readable by any page (it says nothing secret). */
38
60
  function response(info: RelayInfo): Response;
39
61
  /** The address of a relay as it is kept: http(s), its origin and path, no trailing slash. Throws on what is not one. */
@@ -1,6 +1,13 @@
1
1
  import { RELAY_BLOB_MAX_DAYS, RELAY_CONTENT_MAX_BYTES, RELAY_HUB_IDLE_DAYS, RELAY_HUB_MAX_KEYS, RELAY_HUB_MAX_MEMBERS, RELAY_HUB_MAX_STATE, RELAY_HUB_MAX_VALUE, } from './relay-protocol.js';
2
2
  /** What a relay says of itself (docs/design/ta-relays.md 3.1): GET RELAY_INFO_PATH. */
3
3
  export const RELAY_INFO_PATH = '/v1/info';
4
+ /** The most an operator may let a relay keep of a picture: as long and as large as a copy of content. */
5
+ export const RELAY_MEDIA_MAX = { days: 30, bytes: 2 * 1024 * 1024 };
6
+ /**
7
+ * Days a relay keeps a picture of a chat unless its operator says otherwise: short, so that what it keeps stays small
8
+ * however many use it, long enough for the others to open a chat the sender left while away.
9
+ */
10
+ export const RELAY_MEDIA_DEFAULT_DAYS = 1;
4
11
  /** A relay that answers no RELAY_INFO_PATH, made before it: all it can do, with the limits of then. */
5
12
  export const RELAY_LEGACY_INFO = {
6
13
  v: 1,
@@ -18,10 +25,24 @@ export const RELAY_LEGACY_INFO = {
18
25
  };
19
26
  export var RelayInfoUtil;
20
27
  (function (RelayInfoUtil) {
21
- function of(persistent) {
22
- return { ...RELAY_LEGACY_INFO, persistent };
28
+ function of(persistent, media) {
29
+ return { ...RELAY_LEGACY_INFO, persistent, ...(media ? { media } : {}) };
23
30
  }
24
31
  RelayInfoUtil.of = of;
32
+ /**
33
+ * What the operator of a relay lets it keep of the pictures of chats, from its settings (`TA_MEDIA_DAYS`,
34
+ * `TA_MEDIA_MAX_MB`): RELAY_MEDIA_DEFAULT_DAYS unless set, none with 0, within RELAY_MEDIA_MAX.
35
+ */
36
+ function media(days, megabytes) {
37
+ const set = days !== undefined && days !== null && String(days).trim() !== '';
38
+ const kept = Math.min(Math.floor(set ? Number(days) || 0 : RELAY_MEDIA_DEFAULT_DAYS), RELAY_MEDIA_MAX.days);
39
+ if (!(kept > 0)) {
40
+ return undefined;
41
+ }
42
+ const bytes = Number(megabytes) > 0 ? Math.floor(Number(megabytes) * 1024 * 1024) : RELAY_MEDIA_MAX.bytes;
43
+ return { days: kept, bytes: Math.min(bytes, RELAY_MEDIA_MAX.bytes) };
44
+ }
45
+ RelayInfoUtil.media = media;
25
46
  /** The answer of RELAY_INFO_PATH: JSON, readable by any page (it says nothing secret). */
26
47
  function response(info) {
27
48
  return new Response(JSON.stringify(info), {
@@ -82,11 +103,13 @@ export var RelayInfoUtil;
82
103
  limits[key] = given;
83
104
  }
84
105
  }
106
+ const kept = info.media && media(info.media.days, (Number(info.media.bytes) || 0) / (1024 * 1024));
85
107
  return {
86
108
  v: 1,
87
109
  capabilities: info.capabilities.filter((capability) => known.includes(capability)),
88
110
  limits,
89
111
  persistent: info.persistent !== false,
112
+ ...(kept ? { media: kept } : {}),
90
113
  };
91
114
  }
92
115
  RelayInfoUtil.parse = parse;
@@ -39,6 +39,11 @@ export declare const RELAY_VIEWER_HEADERS: {
39
39
  };
40
40
  /** The copies of content kept on the relay, sealed, for when the computer that shared it is off (RelayBlobs). */
41
41
  export declare const RELAY_BLOB_PATH = "/v1/blob/";
42
+ /**
43
+ * Sealed copies of the pictures of chats (docs/design/ta-chat-history.md 7), only on a relay whose operator lets it
44
+ * (RelayInfo.media): none by default, what it keeps costing more the more people use it.
45
+ */
46
+ export declare const RELAY_MEDIA_PATH = "/v1/media/";
42
47
  /** The page a pairing link opens in a browser: the link is for the app, and the key in it stays in the fragment. */
43
48
  export declare const RELAY_PAIR_PAGE = "<!doctype html><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width\"><title>Ta</title>\n<body style=\"font:16px system-ui;max-width:32em;margin:3em auto;padding:0 1em\">\n<h1>Ta</h1><p>Open this link in the Ta app, or paste it there, to reach your Ta from this device.</p>\n<p>\u6253\u5F00 Ta \u5E94\u7528\uFF0C\u7C98\u8D34\u8FD9\u4E2A\u94FE\u63A5\uFF0C\u5C31\u80FD\u4ECE\u8FD9\u53F0\u8BBE\u5907\u8FDE\u5230\u4F60\u7684 Ta\u3002</p><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 \u00B7 \u5728 Ta \u4E2D\u6253\u5F00</a></p>\n<script>document.getElementById('open').href='ta://open?link='+encodeURIComponent(location.href)</script></body>";
44
49
  /** The page a link of an extension opens in a browser. */
@@ -200,6 +205,26 @@ export declare const RELAY_HUB_ASK_DAYS = 7;
200
205
  export declare const RELAY_HUB_IDLE_DAYS = 90;
201
206
  /** Messages of a connection in a second. */
202
207
  export declare const RELAY_HUB_RATE = 40;
208
+ /**
209
+ * One message of the log of a room (docs/design/ta-chat-history.md 5): its number in the room, the member who posted it,
210
+ * sealed and signed as a message is, and when the relay took it. What is said in a room goes here, not in the state.
211
+ */
212
+ export interface RelayHubLogItem {
213
+ s: number;
214
+ m: string;
215
+ d: string;
216
+ at: number;
217
+ }
218
+ /** The log of a room keeps so many messages, so many characters at most: past them the oldest go, read or not. */
219
+ export declare const RELAY_HUB_LOG_MAX = 5000;
220
+ export declare const RELAY_HUB_LOG_CHARS: number;
221
+ /** Days a message is kept once every member has it, and at most. */
222
+ export declare const RELAY_HUB_LOG_KEEP_DAYS = 7;
223
+ export declare const RELAY_HUB_LOG_MAX_DAYS = 30;
224
+ /** Messages of the log in one answer to `sync`. */
225
+ export declare const RELAY_HUB_SYNC_PAGE = 200;
226
+ /** Messages from before they joined a newcomer may read at most, as the charter of the room says (history, 0 by default). */
227
+ export declare const RELAY_HUB_HISTORY_MAX = 500;
203
228
  /**
204
229
  * The sealed list of the members of a room, signed by its owner (or an admin); `v` grows with each change. After a
205
230
  * member was taken out or left, `k` holds the keys of the room's epochs sealed for each member left
@@ -259,6 +284,24 @@ export type RelayHubRequest = {
259
284
  d: string;
260
285
  v?: number;
261
286
  own?: true;
287
+ }
288
+ /** Adds to the log (`mid` the id of the line: the same again is the same message), answered by `done` with its number in `v`. */
289
+ | {
290
+ t: 'post';
291
+ id: number;
292
+ mid: string;
293
+ d: string;
294
+ }
295
+ /** The log after the number `after`, a page of it (RELAY_HUB_SYNC_PAGE at most), answered by `log`. */
296
+ | {
297
+ t: 'sync';
298
+ after: number;
299
+ limit?: number;
300
+ }
301
+ /** This member has the log up to `seq`: the relay may let go of what all have, once old enough. */
302
+ | {
303
+ t: 'ack';
304
+ seq: number;
262
305
  } | {
263
306
  t: 'invite';
264
307
  hash: string;
@@ -266,11 +309,14 @@ export type RelayHubRequest = {
266
309
  } | {
267
310
  t: 'admins';
268
311
  admins: string[];
269
- } | {
312
+ }
313
+ /** `history`: messages from before they joined the newcomer may read (RELAY_HUB_HISTORY_MAX at most), 0 by default. */
314
+ | {
270
315
  t: 'admit';
271
316
  ask: string;
272
317
  answer: string;
273
318
  roster: RelayHubRoster;
319
+ history?: number;
274
320
  } | {
275
321
  t: 'refuse';
276
322
  ask: string;
@@ -307,6 +353,19 @@ export type RelayHubEvent = {
307
353
  t: 'msg';
308
354
  from: string;
309
355
  d: string;
356
+ }
357
+ /** A page of the log: `first` and `last` the numbers it holds now, `more` when there is more after the page. */
358
+ | {
359
+ t: 'log';
360
+ items: RelayHubLogItem[];
361
+ first: number;
362
+ last: number;
363
+ more: boolean;
364
+ }
365
+ /** Another member posted. */
366
+ | {
367
+ t: 'post';
368
+ item: RelayHubLogItem;
310
369
  } | {
311
370
  t: 'state';
312
371
  entry: RelayHubEntry;
@@ -44,6 +44,11 @@ export const RELAY_VIEWER_HEADERS = {
44
44
  };
45
45
  /** The copies of content kept on the relay, sealed, for when the computer that shared it is off (RelayBlobs). */
46
46
  export const RELAY_BLOB_PATH = '/v1/blob/';
47
+ /**
48
+ * Sealed copies of the pictures of chats (docs/design/ta-chat-history.md 7), only on a relay whose operator lets it
49
+ * (RelayInfo.media): none by default, what it keeps costing more the more people use it.
50
+ */
51
+ export const RELAY_MEDIA_PATH = '/v1/media/';
47
52
  /** Open in Ta on the plain pages of the relay: the link of the page, its fragment too, to the Ta app (RelayOpenInTa). */
48
53
  const 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>
49
54
  <script>document.getElementById('open').href='ta://open?link='+encodeURIComponent(location.href)</script>`;
@@ -133,4 +138,14 @@ export const RELAY_HUB_ASK_DAYS = 7;
133
138
  export const RELAY_HUB_IDLE_DAYS = 90;
134
139
  /** Messages of a connection in a second. */
135
140
  export const RELAY_HUB_RATE = 40;
141
+ /** The log of a room keeps so many messages, so many characters at most: past them the oldest go, read or not. */
142
+ export const RELAY_HUB_LOG_MAX = 5000;
143
+ export const RELAY_HUB_LOG_CHARS = 4 * 1024 * 1024;
144
+ /** Days a message is kept once every member has it, and at most. */
145
+ export const RELAY_HUB_LOG_KEEP_DAYS = 7;
146
+ export const RELAY_HUB_LOG_MAX_DAYS = 30;
147
+ /** Messages of the log in one answer to `sync`. */
148
+ export const RELAY_HUB_SYNC_PAGE = 200;
149
+ /** Messages from before they joined a newcomer may read at most, as the charter of the room says (history, 0 by default). */
150
+ export const RELAY_HUB_HISTORY_MAX = 500;
136
151
  //# sourceMappingURL=relay-protocol.js.map
@@ -0,0 +1,106 @@
1
+ import type { RelayChatLine, RelayChatLineExtra } from './relay-room-chat.js';
2
+ /**
3
+ * What every app of a group chat that listens and speaks there needs (docs/design/ta-feedback.md 1): what others said
4
+ * since it last read, paced; who speaks for the user (their double or an agent of theirs in the room); what others wrote
5
+ * kept as theirs when a model reads it; and a board of how things go that every member, and those in a browser, see.
6
+ * Feedback is one such app; a vote, a rota or a help desk would be others.
7
+ */
8
+ /** Under `agents:<member>` of the state of a room, each member lists the agents of theirs they invited to it. */
9
+ export declare const ROOM_AGENTS_KEY = "agents:";
10
+ /** A line older than this when an app reads it is left: Ta was off for days. */
11
+ export declare const ROOM_APP_STALE_MS: number;
12
+ export declare namespace RoomAppUtil {
13
+ /** What others wrote, which a model reads inside tags: no tags of its own. */
14
+ function quote(text: string): string;
15
+ /**
16
+ * What others said to the room since `since`: not by the user, not too long ago, not to someone in particular (@ a
17
+ * person, a double or an agent: theirs to answer), not said by a double or an agent.
18
+ */
19
+ function fresh(lines: readonly RelayChatLine[], self: string, since: number, now: number, staleMs?: number): RelayChatLine[];
20
+ /** Whether one more fits in the hour before `now`. */
21
+ function within(times: readonly number[], now: number, most: number): boolean;
22
+ /** People named with @, each once. */
23
+ function mentions(names: readonly string[]): string;
24
+ }
25
+ export interface RoomSpeaker {
26
+ name: string;
27
+ /** What the line it says carries (RelayRoomChat.say): the agent, or that it is the double. */
28
+ extra: RelayChatLineExtra;
29
+ }
30
+ /**
31
+ * Who speaks for the user in a room: the agent of theirs they chose, while it is in the room (as `agents:<member>` of
32
+ * its state lists it, written by them), else their double, named `doubleName`.
33
+ */
34
+ export declare function roomSpeaker(rooms: {
35
+ getState(room: string, prefix?: string): Promise<Record<string, {
36
+ value: unknown;
37
+ by: string;
38
+ }>>;
39
+ }, room: string, self: string, agent: string | undefined, doubleName: string): Promise<RoomSpeaker>;
40
+ /**
41
+ * Paces the work of an app on what is said in rooms: once the room is quiet for `settleMs` (a few lines read as one), one
42
+ * run at a time per room, and no more runs that did something than `hourly` in an hour (past it, again after `laterMs`).
43
+ * `run` says whether it did something (a turn of a model, a line said).
44
+ */
45
+ export declare class RoomPacer {
46
+ protected readonly run: (room: string) => Promise<boolean>;
47
+ protected readonly options: {
48
+ settleMs: number;
49
+ hourly: number;
50
+ laterMs: number;
51
+ now(): number;
52
+ failed(error: unknown): void;
53
+ };
54
+ protected readonly timers: Map<string, NodeJS.Timeout>;
55
+ protected readonly queues: Map<string, Promise<void>>;
56
+ protected readonly runs: Map<string, number[]>;
57
+ protected disposed: boolean;
58
+ constructor(run: (room: string) => Promise<boolean>, options: {
59
+ settleMs: number;
60
+ hourly: number;
61
+ laterMs: number;
62
+ now(): number;
63
+ failed(error: unknown): void;
64
+ });
65
+ /** Something was said in a room: it is read once it is quiet (`delay`, settleMs by default). */
66
+ heard(room: string, delay?: number): void;
67
+ /** What runs in a room now, done. */
68
+ idle(room: string): Promise<void>;
69
+ dispose(): void;
70
+ protected turn(room: string): Promise<void>;
71
+ }
72
+ /** Under `board:<app>:<member>` of the state of a room, its owner says how the things of an app go, for all in it. */
73
+ export declare const RELAY_BOARD_KEY = "board:";
74
+ /** Items of a board at most, the latest. */
75
+ export declare const RELAY_BOARD_MAX = 30;
76
+ /**
77
+ * How an item goes, for anyone to draw without knowing the app: being done, waiting (for the owner, or for more from
78
+ * those who said it), done, or closed without it.
79
+ */
80
+ export type RelayBoardState = 'doing' | 'waiting' | 'done' | 'closed';
81
+ export interface RelayBoardItem {
82
+ /** Its number in the room, as people name it (#n). */
83
+ n: number;
84
+ title: string;
85
+ state: RelayBoardState;
86
+ /** How it goes in words of the owner's language: `Being fixed`, `Waiting for the owner`. */
87
+ label: string;
88
+ /** Who it came from, by name. */
89
+ people: string[];
90
+ at: number;
91
+ }
92
+ /** A board of an app in a room: its name (`Feedback`), and its items, the latest last. */
93
+ export interface RelayRoomBoard {
94
+ title: string;
95
+ items: RelayBoardItem[];
96
+ }
97
+ export declare namespace RelayBoardUtil {
98
+ function key(app: string, member: string): string;
99
+ /** The board under a key of the state, if the owner of the room wrote it under their own name and it is well formed. */
100
+ function read(key: string, value: unknown, by: string, owner: string | undefined): RelayRoomBoard | undefined;
101
+ /** How many are being done or wait, and how many are done: what a line over the chat says. */
102
+ function counts(board: RelayRoomBoard): {
103
+ open: number;
104
+ done: number;
105
+ };
106
+ }
@@ -0,0 +1,171 @@
1
+ /**
2
+ * What every app of a group chat that listens and speaks there needs (docs/design/ta-feedback.md 1): what others said
3
+ * since it last read, paced; who speaks for the user (their double or an agent of theirs in the room); what others wrote
4
+ * kept as theirs when a model reads it; and a board of how things go that every member, and those in a browser, see.
5
+ * Feedback is one such app; a vote, a rota or a help desk would be others.
6
+ */
7
+ /** Under `agents:<member>` of the state of a room, each member lists the agents of theirs they invited to it. */
8
+ export const ROOM_AGENTS_KEY = 'agents:';
9
+ /** A line older than this when an app reads it is left: Ta was off for days. */
10
+ export const ROOM_APP_STALE_MS = 3 * 24 * 60 * 60 * 1000;
11
+ const HOUR_MS = 60 * 60 * 1000;
12
+ export var RoomAppUtil;
13
+ (function (RoomAppUtil) {
14
+ /** What others wrote, which a model reads inside tags: no tags of its own. */
15
+ function quote(text) {
16
+ return text.replace(/</g, '&lt;').replace(/>/g, '&gt;');
17
+ }
18
+ RoomAppUtil.quote = quote;
19
+ /**
20
+ * What others said to the room since `since`: not by the user, not too long ago, not to someone in particular (@ a
21
+ * person, a double or an agent: theirs to answer), not said by a double or an agent.
22
+ */
23
+ function fresh(lines, self, since, now, staleMs = ROOM_APP_STALE_MS) {
24
+ return lines.filter(line => { var _a; return line.from !== self && line.at > since && now - line.at < staleMs && !((_a = line.to) === null || _a === void 0 ? void 0 : _a.length) && !line.double && !line.agent; });
25
+ }
26
+ RoomAppUtil.fresh = fresh;
27
+ /** Whether one more fits in the hour before `now`. */
28
+ function within(times, now, most) {
29
+ return times.filter(at => now - at < HOUR_MS).length < most;
30
+ }
31
+ RoomAppUtil.within = within;
32
+ /** People named with @, each once. */
33
+ function mentions(names) {
34
+ return [...new Set(names)].map(name => `@${name}`).join(' ');
35
+ }
36
+ RoomAppUtil.mentions = mentions;
37
+ })(RoomAppUtil || (RoomAppUtil = {}));
38
+ /**
39
+ * Who speaks for the user in a room: the agent of theirs they chose, while it is in the room (as `agents:<member>` of
40
+ * its state lists it, written by them), else their double, named `doubleName`.
41
+ */
42
+ export async function roomSpeaker(rooms, room, self, agent, doubleName) {
43
+ if (agent) {
44
+ const key = `${ROOM_AGENTS_KEY}${self}`;
45
+ const listed = (await rooms.getState(room, key).catch(() => ({})))[key];
46
+ const found = (listed === null || listed === void 0 ? void 0 : listed.by) === self && Array.isArray(listed.value) ? listed.value.find(each => (each === null || each === void 0 ? void 0 : each.id) === agent) : undefined;
47
+ if (found && typeof found.name === 'string') {
48
+ return { name: found.name, extra: { agent: { id: found.id, name: found.name } } };
49
+ }
50
+ }
51
+ return { name: doubleName, extra: { double: true } };
52
+ }
53
+ /**
54
+ * Paces the work of an app on what is said in rooms: once the room is quiet for `settleMs` (a few lines read as one), one
55
+ * run at a time per room, and no more runs that did something than `hourly` in an hour (past it, again after `laterMs`).
56
+ * `run` says whether it did something (a turn of a model, a line said).
57
+ */
58
+ export class RoomPacer {
59
+ constructor(run, options) {
60
+ this.run = run;
61
+ this.options = options;
62
+ this.timers = new Map();
63
+ this.queues = new Map();
64
+ this.runs = new Map();
65
+ this.disposed = false;
66
+ }
67
+ /** Something was said in a room: it is read once it is quiet (`delay`, settleMs by default). */
68
+ heard(room, delay = this.options.settleMs) {
69
+ var _a, _b;
70
+ if (this.disposed) {
71
+ return;
72
+ }
73
+ clearTimeout(this.timers.get(room));
74
+ const timer = setTimeout(() => {
75
+ var _a;
76
+ this.timers.delete(room);
77
+ const next = ((_a = this.queues.get(room)) !== null && _a !== void 0 ? _a : Promise.resolve())
78
+ .then(() => this.turn(room))
79
+ .catch(error => this.options.failed(error))
80
+ .finally(() => {
81
+ if (this.queues.get(room) === next) {
82
+ this.queues.delete(room);
83
+ }
84
+ });
85
+ this.queues.set(room, next);
86
+ }, delay);
87
+ (_b = (_a = timer).unref) === null || _b === void 0 ? void 0 : _b.call(_a);
88
+ this.timers.set(room, timer);
89
+ }
90
+ /** What runs in a room now, done. */
91
+ idle(room) {
92
+ var _a;
93
+ return (_a = this.queues.get(room)) !== null && _a !== void 0 ? _a : Promise.resolve();
94
+ }
95
+ dispose() {
96
+ this.disposed = true;
97
+ for (const timer of this.timers.values()) {
98
+ clearTimeout(timer);
99
+ }
100
+ }
101
+ async turn(room) {
102
+ var _a;
103
+ if (this.disposed) {
104
+ return;
105
+ }
106
+ const now = this.options.now();
107
+ const times = ((_a = this.runs.get(room)) !== null && _a !== void 0 ? _a : []).filter(at => now - at < HOUR_MS);
108
+ if (!RoomAppUtil.within(times, now, this.options.hourly)) {
109
+ this.heard(room, this.options.laterMs);
110
+ return;
111
+ }
112
+ if (await this.run(room)) {
113
+ this.runs.set(room, [...times, now]);
114
+ }
115
+ }
116
+ }
117
+ /** Under `board:<app>:<member>` of the state of a room, its owner says how the things of an app go, for all in it. */
118
+ export const RELAY_BOARD_KEY = 'board:';
119
+ /** Items of a board at most, the latest. */
120
+ export const RELAY_BOARD_MAX = 30;
121
+ const STATES = ['doing', 'waiting', 'done', 'closed'];
122
+ export var RelayBoardUtil;
123
+ (function (RelayBoardUtil) {
124
+ function key(app, member) {
125
+ return `${RELAY_BOARD_KEY}${app}:${member}`;
126
+ }
127
+ RelayBoardUtil.key = key;
128
+ /** The board under a key of the state, if the owner of the room wrote it under their own name and it is well formed. */
129
+ function read(key, value, by, owner) {
130
+ if (!owner || by !== owner || !key.startsWith(RELAY_BOARD_KEY) || !key.endsWith(`:${owner}`) || !value || typeof value !== 'object') {
131
+ return undefined;
132
+ }
133
+ const { title, items } = value;
134
+ if (typeof title !== 'string' || !Array.isArray(items)) {
135
+ return undefined;
136
+ }
137
+ return {
138
+ title: cut(title, 40),
139
+ items: items
140
+ .filter(item => item &&
141
+ Number.isInteger(item.n) &&
142
+ typeof item.title === 'string' &&
143
+ STATES.includes(item.state) &&
144
+ typeof item.label === 'string' &&
145
+ typeof item.at === 'number')
146
+ .slice(-RELAY_BOARD_MAX)
147
+ .map(item => ({
148
+ n: item.n,
149
+ title: cut(item.title, 80),
150
+ state: item.state,
151
+ label: cut(item.label, 30),
152
+ people: Array.isArray(item.people) ? item.people.filter((name) => typeof name === 'string').slice(0, 5) : [],
153
+ at: item.at,
154
+ })),
155
+ };
156
+ }
157
+ RelayBoardUtil.read = read;
158
+ /** How many are being done or wait, and how many are done: what a line over the chat says. */
159
+ function counts(board) {
160
+ return {
161
+ open: board.items.filter(item => item.state === 'doing' || item.state === 'waiting').length,
162
+ done: board.items.filter(item => item.state === 'done').length,
163
+ };
164
+ }
165
+ RelayBoardUtil.counts = counts;
166
+ function cut(text, max) {
167
+ const one = text.replace(/\s+/g, ' ').trim();
168
+ return one.length > max ? `${one.slice(0, max - 1)}…` : one;
169
+ }
170
+ })(RelayBoardUtil || (RelayBoardUtil = {}));
171
+ //# sourceMappingURL=relay-room-app.js.map