@alexsun-top/opencode-chat-room 0.8.4 → 0.8.5

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 (136) hide show
  1. package/CHANGELOG.md +5 -1
  2. package/README.md +4 -4
  3. package/README.zh-CN.md +4 -4
  4. package/cli.ts +1 -1
  5. package/dist/cli.js +35 -16
  6. package/package.json +14 -11
  7. package/packages/chat-room-channeld/README.md +3 -2
  8. package/packages/chat-room-channeld/src/inbound.ts +3 -6
  9. package/packages/chat-room-channeld/src/pg/sql.mjs +157 -142
  10. package/packages/chat-room-channeld/src/pg/sql.mts +175 -0
  11. package/packages/chat-room-channeld/src/pg/worker.mjs +107 -99
  12. package/packages/chat-room-channeld/src/pg/worker.mts +166 -0
  13. package/plugin.ts +1 -1
  14. package/server/assets/chat/admin.js +4 -1
  15. package/server/assets/chat/announcements.js +3 -0
  16. package/server/assets/chat/attachments.js +3 -3
  17. package/server/assets/chat/clipboard.js +1 -1
  18. package/server/assets/chat/copy-action.js +1 -1
  19. package/server/assets/chat/doc-preview.js +1 -1
  20. package/server/assets/chat/editor.js +11 -7
  21. package/server/assets/chat/events.js +7 -5
  22. package/server/assets/chat/files.js +5 -1
  23. package/server/assets/chat/integration-editor.js +2 -0
  24. package/server/assets/chat/integrations.js +1 -1
  25. package/server/assets/chat/join-requests.js +1 -1
  26. package/server/assets/chat/markdown-footnotes.js +8 -7
  27. package/server/assets/chat/markdown.js +6 -5
  28. package/server/assets/chat/members.js +3 -3
  29. package/server/assets/chat/mentions.js +8 -5
  30. package/server/assets/chat/message-cache.js +1 -1
  31. package/server/assets/chat/message-polling.js +2 -2
  32. package/server/assets/chat/message-window.js +1 -1
  33. package/server/assets/chat/messages.js +17 -3
  34. package/server/assets/chat/office-preview.js +3 -3
  35. package/server/assets/chat/palette.js +5 -3
  36. package/server/assets/chat/panels.js +19 -0
  37. package/server/assets/chat/questions.js +3 -2
  38. package/server/assets/chat/reminders.js +3 -0
  39. package/server/assets/chat/replies.js +6 -3
  40. package/server/assets/chat/resource-format.js +2 -2
  41. package/server/assets/chat/rooms.js +7 -4
  42. package/server/assets/chat/settings.js +4 -3
  43. package/server/assets/chat/shortcuts.js +3 -1
  44. package/server/assets/chat/side-pane.js +1 -1
  45. package/server/assets/chat/state.js +30 -11
  46. package/server/assets/chat/system-events.js +3 -2
  47. package/server/assets/chat/system-messages.js +15 -15
  48. package/server/assets/chat/tasks.js +4 -3
  49. package/server/assets/chat/window-controls.js +3 -2
  50. package/server/assets/conversation.css +14 -2
  51. package/server/assets/doc-preview.css +6 -0
  52. package/server/assets/office/THIRD-PARTY-NOTICES.txt +23 -1
  53. package/server/assets/office/licenses/docx-preview-HEADER.txt +6 -0
  54. package/server/assets/office/licenses/echarts-LICENSE-d3.txt +27 -0
  55. package/server/assets/office/licenses/echarts-NOTICE.txt +5 -0
  56. package/server/assets/office/licenses/immediate-LICENSE.txt +20 -0
  57. package/server/assets/office/licenses/lie-LICENSE.txt +7 -0
  58. package/server/assets/office/licenses/pako-LICENSE.txt +21 -0
  59. package/server/assets/office/licenses/pako-ZLIB.txt +18 -0
  60. package/server/assets/office/licenses/setimmediate-LICENSE.txt +20 -0
  61. package/server/sw.js +3 -1
  62. package/skills/chatroom/SKILL.md +9 -0
  63. package/src/README.md +1 -1
  64. package/src/agent/agent-receipts.ts +12 -4
  65. package/src/center/center-client.ts +1 -1
  66. package/src/state/pg-sql.mjs +157 -142
  67. package/src/state/pg-sql.mts +175 -0
  68. package/src/state/pg-worker.mjs +107 -99
  69. package/src/state/pg-worker.mts +166 -0
  70. package/src/state/store.ts +21 -7
  71. package/src/tui/app-types.mjs +1 -0
  72. package/src/tui/app-types.mts +175 -0
  73. package/src/tui/app.mjs +426 -423
  74. package/src/tui/app.mts +500 -0
  75. package/src/tui/config.mjs +166 -158
  76. package/src/tui/config.mts +247 -0
  77. package/src/tui/http.mjs +239 -206
  78. package/src/tui/http.mts +412 -0
  79. package/src/tui/i18n.mjs +17 -20
  80. package/src/tui/i18n.mts +42 -0
  81. package/src/tui/info-app.mjs +431 -427
  82. package/src/tui/info-app.mts +500 -0
  83. package/src/tui/info-content.mjs +205 -199
  84. package/src/tui/info-content.mts +341 -0
  85. package/src/tui/join-app.mjs +75 -64
  86. package/src/tui/join-app.mts +99 -0
  87. package/src/tui/locales/en.mjs +302 -320
  88. package/src/tui/locales/en.mts +349 -0
  89. package/src/tui/locales/zh-CN.mjs +302 -320
  90. package/src/tui/locales/zh-CN.mts +349 -0
  91. package/src/tui/main.mjs +219 -213
  92. package/src/tui/main.mts +255 -0
  93. package/src/tui/markdown.mjs +128 -272
  94. package/src/tui/markdown.mts +152 -0
  95. package/src/tui/mentions.mjs +181 -184
  96. package/src/tui/mentions.mts +251 -0
  97. package/src/tui/message-app.mjs +181 -173
  98. package/src/tui/message-app.mts +213 -0
  99. package/src/tui/message-view.mjs +90 -111
  100. package/src/tui/message-view.mts +151 -0
  101. package/src/tui/messages.mjs +55 -49
  102. package/src/tui/messages.mts +106 -0
  103. package/src/tui/palette-app.mjs +364 -344
  104. package/src/tui/palette-app.mts +388 -0
  105. package/src/tui/palette.mjs +100 -98
  106. package/src/tui/palette.mts +176 -0
  107. package/src/tui/prefs.mjs +56 -63
  108. package/src/tui/prefs.mts +126 -0
  109. package/src/tui/reminder-app.mjs +191 -181
  110. package/src/tui/reminder-app.mts +228 -0
  111. package/src/tui/room-settings-app.mjs +257 -242
  112. package/src/tui/room-settings-app.mts +301 -0
  113. package/src/tui/session.mjs +30 -29
  114. package/src/tui/session.mts +51 -0
  115. package/src/tui/settings-app.mjs +151 -138
  116. package/src/tui/settings-app.mts +179 -0
  117. package/src/tui/ui-content.mjs +426 -421
  118. package/src/tui/ui-content.mts +499 -0
  119. package/src/tui/ui-help.mjs +58 -67
  120. package/src/tui/ui-help.mts +87 -0
  121. package/src/tui/ui-mentions.mjs +34 -36
  122. package/src/tui/ui-mentions.mts +60 -0
  123. package/src/tui/ui-overlay-view.mjs +259 -267
  124. package/src/tui/ui-overlay-view.mts +322 -0
  125. package/src/tui/ui-suggest-view.mjs +54 -57
  126. package/src/tui/ui-suggest-view.mts +78 -0
  127. package/src/tui/ui-suggest.mjs +164 -167
  128. package/src/tui/ui-suggest.mts +248 -0
  129. package/src/tui/ui-theme.mjs +52 -57
  130. package/src/tui/ui-theme.mts +98 -0
  131. package/src/tui/ui-view.mjs +267 -275
  132. package/src/tui/ui-view.mts +308 -0
  133. package/src/tui/ui.mjs +372 -359
  134. package/src/tui/ui.mts +440 -0
  135. package/src/tui/wire.mjs +215 -0
  136. package/src/tui/wire.mts +383 -0
package/src/tui/app.mjs CHANGED
@@ -12,436 +12,440 @@ import { canonicalizeMentions } from "./mentions.mjs";
12
12
  import { mergeMessages, messageStamp, oldestSeq } from "./messages.mjs";
13
13
  import { normalizeLanguage, setLanguage, t } from "./i18n.mjs";
14
14
  import { normalizeTheme } from "./prefs.mjs";
15
-
16
15
  const BACKOFF_MS = [1000, 2000, 4000, 8000, 15000];
17
-
16
+ const FIRST_BACKOFF = BACKOFF_MS[0] ?? 1000;
17
+ const MAX_BACKOFF = BACKOFF_MS.at(-1) ?? 15000;
18
+ const joinStatusOf = (result) => typeof result === "string" ? result
19
+ : result !== null && typeof result === "object" && "status" in result ? result.status : undefined;
18
20
  const errorText = (error) => (error instanceof Error ? error.message : String(error ?? "unknown error"));
19
-
20
21
  const normalizeRoom = (room) => ({
21
- id: String(room.id ?? ""),
22
- name: String(room.name ?? room.id ?? "room"),
23
- participants: Array.isArray(room.participants) ? room.participants : [],
24
- joined: room.joined === true,
22
+ id: String(room.id ?? ""),
23
+ name: String(room.name ?? room.id ?? "room"),
24
+ participants: Array.isArray(room.participants) ? room.participants : [],
25
+ joined: room.joined === true,
25
26
  });
26
-
27
27
  export class ChatApp {
28
- constructor({ api, sessionID, participant, host = "", autoPoll = true, settings = {}, savePrefs = null, downloadDir = process.cwd(), schedule = (fn, ms) => setTimeout(fn, ms) }) {
29
- this.api = api;
30
- this.sessionID = sessionID;
31
- this.participant = participant;
32
- this.autoPoll = autoPoll;
33
- this.schedule = schedule;
34
- this.savePrefs = typeof savePrefs === "function" ? savePrefs : null;
35
- this.settings = { ...settings };
36
- this.token = this.settings.token ?? "";
37
- delete this.settings.token;
38
- this.prefs = {
39
- language: normalizeLanguage(this.settings.language),
40
- theme: normalizeTheme(this.settings.theme),
41
- participant: this.settings.participant ?? participant,
42
- url: this.settings.server ?? "",
43
- };
44
- setLanguage(this.prefs.language);
45
- this.downloadDir = downloadDir;
46
- this.closed = false;
47
- this.pollToken = 0;
48
- this.timers = new Set();
49
- this.pollAbort = null;
50
- this.cache = new Map();
51
- this.historyCursor = new Map();
52
- this.historyMore = new Map();
53
- this.historyPages = new Map();
54
- this.historyInflight = new Set();
55
- this.listeners = new Set();
56
- this.state = {
57
- host,
58
- participant,
59
- theme: this.prefs.theme,
60
- language: this.prefs.language,
61
- connection: "connecting",
62
- rooms: [],
63
- selected: 0,
64
- current: null,
65
- joined: false,
66
- messages: [],
67
- pending: null,
68
- focus: "rooms",
69
- draft: "",
70
- highlight: null,
71
- help: false,
72
- status: "",
73
- statusKind: "info",
74
- palette: null,
75
- quit: false,
76
- };
77
- }
78
-
79
- subscribe(listener) {
80
- this.listeners.add(listener);
81
- return () => this.listeners.delete(listener);
82
- }
83
-
84
- touch() {
85
- for (const listener of this.listeners) listener(this.state);
86
- }
87
-
88
- later(fn, ms) {
89
- if (this.closed) return undefined;
90
- let timer;
91
- const run = () => {
92
- if (timer !== undefined) this.timers.delete(timer);
93
- return fn();
94
- };
95
- timer = this.schedule(run, ms);
96
- if (timer !== undefined) this.timers.add(timer);
97
- return timer;
98
- }
99
-
100
- setStatus(text, kind = "info") {
101
- this.state.status = text;
102
- this.state.statusKind = kind;
103
- this.touch();
104
- }
105
-
106
- async init() {
107
- await this.refreshRooms(true);
108
- }
109
-
110
- async refreshRooms(quiet = false) {
111
- try {
112
- const list = await this.api.listRooms(this.sessionID);
113
- const rooms = (Array.isArray(list) ? list : []).map(normalizeRoom);
114
- const selectedID = this.state.rooms[this.state.selected]?.id;
115
- this.state.rooms = rooms;
116
- const found = selectedID ? rooms.findIndex((room) => room.id === selectedID) : -1;
117
- this.state.selected = found >= 0 ? found : Math.min(this.state.selected, Math.max(0, rooms.length - 1));
118
- this.state.connection = "online";
119
- if (!quiet) {
120
- this.state.status = t("status.refreshed");
121
- this.state.statusKind = "info";
122
- }
123
- this.touch();
124
- return true;
125
- } catch (error) {
126
- this.state.connection = "offline";
127
- this.state.status = t("status.refreshFailed", { error: errorText(error) });
128
- this.state.statusKind = "error";
129
- this.touch();
130
- return false;
131
- }
132
- }
133
-
134
- isCurrent(roomID) {
135
- return this.state.current?.id === roomID;
136
- }
137
-
138
- async openSelected() {
139
- return this.openRoom(this.state.selected);
140
- }
141
-
142
- async openRoom(indexOrRoom) {
143
- const index = typeof indexOrRoom === "number" ? indexOrRoom : this.state.rooms.indexOf(indexOrRoom);
144
- const summary = this.state.rooms[index];
145
- if (!summary) return;
146
- this.state.selected = index;
147
- this.stopPolling();
148
- this.state.current = { id: summary.id, name: summary.name, participants: summary.participants };
149
- this.state.joined = summary.joined;
150
- this.state.messages = [];
151
- this.state.pending = null;
152
- this.state.highlight = null;
153
- this.state.status = t("status.opening", { name: summary.name });
154
- this.state.statusKind = "info";
155
- this.touch();
156
- if (summary.joined) await this.loadRoom(summary.id);
157
- else await this.joinRoom(summary.id);
158
- }
159
-
160
- async joinRoom(roomID) {
161
- try {
162
- const result = await this.api.join(roomID, this.sessionID, this.participant);
163
- if (!this.isCurrent(roomID)) return;
164
- if (result?.pending) {
165
- const message = String(result.message ?? t("status.joinPending"));
166
- this.state.pending = { roomId: roomID, message };
167
- this.state.joined = false;
168
- this.state.status = message;
28
+ constructor({ api, sessionID, participant, host = "", autoPoll = true, settings = {}, savePrefs = null, downloadDir = process.cwd(), schedule = (fn, ms) => setTimeout(fn, ms) }) {
29
+ this.api = api;
30
+ this.sessionID = sessionID;
31
+ this.participant = participant;
32
+ this.autoPoll = autoPoll;
33
+ this.schedule = schedule;
34
+ this.savePrefs = typeof savePrefs === "function" ? savePrefs : null;
35
+ this.settings = { ...settings };
36
+ this.token = this.settings.token ?? "";
37
+ delete this.settings.token;
38
+ this.prefs = {
39
+ language: normalizeLanguage(this.settings.language),
40
+ theme: normalizeTheme(this.settings.theme),
41
+ participant: this.settings.participant ?? participant,
42
+ url: this.settings.server ?? "",
43
+ };
44
+ setLanguage(this.prefs.language);
45
+ this.downloadDir = downloadDir;
46
+ this.closed = false;
47
+ this.pollToken = 0;
48
+ this.timers = new Set();
49
+ this.pollAbort = null;
50
+ this.cache = new Map();
51
+ this.historyCursor = new Map();
52
+ this.historyMore = new Map();
53
+ this.historyPages = new Map();
54
+ this.historyInflight = new Set();
55
+ this.listeners = new Set();
56
+ this.state = {
57
+ host,
58
+ participant,
59
+ theme: this.prefs.theme,
60
+ language: this.prefs.language,
61
+ connection: "connecting",
62
+ rooms: [],
63
+ selected: 0,
64
+ current: null,
65
+ joined: false,
66
+ messages: [],
67
+ pending: null,
68
+ focus: "rooms",
69
+ draft: "",
70
+ highlight: null,
71
+ help: false,
72
+ status: "",
73
+ statusKind: "info",
74
+ palette: null,
75
+ quit: false,
76
+ };
77
+ }
78
+ subscribe(listener) {
79
+ this.listeners.add(listener);
80
+ return () => this.listeners.delete(listener);
81
+ }
82
+ touch() {
83
+ for (const listener of this.listeners)
84
+ listener(this.state);
85
+ }
86
+ later(fn, ms) {
87
+ if (this.closed)
88
+ return undefined;
89
+ let timer;
90
+ const run = () => {
91
+ if (timer !== undefined)
92
+ this.timers.delete(timer);
93
+ return fn();
94
+ };
95
+ timer = this.schedule(run, ms);
96
+ if (timer !== undefined)
97
+ this.timers.add(timer);
98
+ return timer;
99
+ }
100
+ setStatus(text, kind = "info") {
101
+ this.state.status = text;
102
+ this.state.statusKind = kind;
103
+ this.touch();
104
+ }
105
+ async init() {
106
+ await this.refreshRooms(true);
107
+ }
108
+ async refreshRooms(quiet = false) {
109
+ try {
110
+ const list = await this.api.listRooms(this.sessionID);
111
+ const rooms = (Array.isArray(list) ? list : []).map(normalizeRoom);
112
+ const selectedID = this.state.rooms[this.state.selected]?.id;
113
+ this.state.rooms = rooms;
114
+ const found = selectedID ? rooms.findIndex((room) => room.id === selectedID) : -1;
115
+ this.state.selected = found >= 0 ? found : Math.min(this.state.selected, Math.max(0, rooms.length - 1));
116
+ this.state.connection = "online";
117
+ if (!quiet) {
118
+ this.state.status = t("status.refreshed");
119
+ this.state.statusKind = "info";
120
+ }
121
+ this.touch();
122
+ return true;
123
+ }
124
+ catch (error) {
125
+ this.state.connection = "offline";
126
+ this.state.status = t("status.refreshFailed", { error: errorText(error) });
127
+ this.state.statusKind = "error";
128
+ this.touch();
129
+ return false;
130
+ }
131
+ }
132
+ isCurrent(roomID) {
133
+ return this.state.current?.id === roomID;
134
+ }
135
+ async openSelected() {
136
+ return this.openRoom(this.state.selected);
137
+ }
138
+ async openRoom(indexOrRoom) {
139
+ const index = typeof indexOrRoom === "number" ? indexOrRoom : this.state.rooms.indexOf(indexOrRoom);
140
+ const summary = this.state.rooms[index];
141
+ if (!summary)
142
+ return;
143
+ this.state.selected = index;
144
+ this.stopPolling();
145
+ this.state.current = { id: summary.id, name: summary.name, participants: summary.participants };
146
+ this.state.joined = summary.joined;
147
+ this.state.messages = [];
148
+ this.state.pending = null;
149
+ this.state.highlight = null;
150
+ this.state.status = t("status.opening", { name: summary.name });
169
151
  this.state.statusKind = "info";
170
152
  this.touch();
171
- this.later(() => this.checkJoin(roomID), 1500);
172
- return;
173
- }
174
- this.enterRoom(result?.room ?? result, roomID, true);
175
- } catch (error) {
176
- if (this.isCurrent(roomID)) this.setStatus(t("status.joinFailed", { error: errorText(error) }), "error");
177
- }
178
- }
179
-
180
- async checkJoin(roomID) {
181
- if (this.closed || !this.isCurrent(roomID) || this.state.pending?.roomId !== roomID) return;
182
- try {
183
- const result = await this.api.joinStatus(roomID, this.sessionID);
184
- if (!this.isCurrent(roomID) || this.state.pending?.roomId !== roomID) return;
185
- const status = typeof result === "string" ? result : result?.status;
186
- if (status === "joined") {
153
+ if (summary.joined)
154
+ await this.loadRoom(summary.id);
155
+ else
156
+ await this.joinRoom(summary.id);
157
+ }
158
+ async joinRoom(roomID) {
159
+ try {
160
+ const result = await this.api.join(roomID, this.sessionID, this.participant);
161
+ if (!this.isCurrent(roomID))
162
+ return;
163
+ if (result.pending) {
164
+ const message = String(result.message ?? t("status.joinPending"));
165
+ this.state.pending = { roomId: roomID, message };
166
+ this.state.joined = false;
167
+ this.state.status = message;
168
+ this.state.statusKind = "info";
169
+ this.touch();
170
+ this.later(() => this.checkJoin(roomID), 1500);
171
+ return;
172
+ }
173
+ this.enterRoom(result.room, roomID, true);
174
+ }
175
+ catch (error) {
176
+ if (this.isCurrent(roomID))
177
+ this.setStatus(t("status.joinFailed", { error: errorText(error) }), "error");
178
+ }
179
+ }
180
+ async checkJoin(roomID) {
181
+ if (this.closed || !this.isCurrent(roomID) || this.state.pending?.roomId !== roomID)
182
+ return;
183
+ try {
184
+ const status = joinStatusOf(await this.api.joinStatus(roomID, this.sessionID));
185
+ if (!this.isCurrent(roomID) || this.state.pending?.roomId !== roomID)
186
+ return;
187
+ if (status === "joined") {
188
+ this.state.pending = null;
189
+ this.setStatus(t("status.joinApproved"), "success");
190
+ await this.loadRoom(roomID);
191
+ return;
192
+ }
193
+ if (status === "pending") {
194
+ this.later(() => this.checkJoin(roomID), 1500);
195
+ return;
196
+ }
197
+ this.state.pending = null;
198
+ this.setStatus(t("status.joinStale"), "error");
199
+ }
200
+ catch {
201
+ this.later(() => this.checkJoin(roomID), 1500);
202
+ }
203
+ }
204
+ async loadRoom(roomID) {
205
+ try {
206
+ const room = await this.api.getRoom(roomID);
207
+ if (!this.isCurrent(roomID) || !room)
208
+ return;
209
+ if (room.joined === false) {
210
+ this.notJoined(roomID, t("status.notMember"));
211
+ return;
212
+ }
213
+ this.enterRoom(room, roomID, false);
214
+ }
215
+ catch (error) {
216
+ if (this.isCurrent(roomID))
217
+ this.setStatus(t("status.openFailed", { error: errorText(error) }), "error");
218
+ }
219
+ }
220
+ enterRoom(room, roomID, joinedNow) {
221
+ if (!room || typeof room !== "object") {
222
+ this.setStatus(t("status.roomMissing"), "error");
223
+ return;
224
+ }
225
+ const summary = this.state.rooms.find((item) => item.id === roomID);
226
+ if (summary) {
227
+ summary.joined = true;
228
+ summary.name = String(room.name ?? summary.name);
229
+ if (Array.isArray(room.participants))
230
+ summary.participants = room.participants;
231
+ }
232
+ const cache = this.historyList(roomID);
233
+ mergeMessages(cache, room.messages);
234
+ this.state.current = {
235
+ id: roomID,
236
+ name: String(room.name ?? summary?.name ?? roomID),
237
+ participants: Array.isArray(room.participants) ? room.participants : summary?.participants ?? [],
238
+ };
239
+ this.state.joined = true;
187
240
  this.state.pending = null;
188
- this.setStatus(t("status.joinApproved"), "success");
189
- await this.loadRoom(roomID);
190
- return;
191
- }
192
- if (status === "pending") {
193
- this.later(() => this.checkJoin(roomID), 1500);
194
- return;
195
- }
196
- this.state.pending = null;
197
- this.setStatus(t("status.joinStale"), "error");
198
- } catch {
199
- this.later(() => this.checkJoin(roomID), 1500);
200
- }
201
- }
202
-
203
- async loadRoom(roomID) {
204
- try {
205
- const room = await this.api.getRoom(roomID);
206
- if (!this.isCurrent(roomID) || !room) return;
207
- if (room.joined === false) {
208
- this.notJoined(roomID, t("status.notMember"));
209
- return;
210
- }
211
- this.enterRoom(room, roomID, false);
212
- } catch (error) {
213
- if (this.isCurrent(roomID)) this.setStatus(t("status.openFailed", { error: errorText(error) }), "error");
214
- }
215
- }
216
-
217
- enterRoom(room, roomID, joinedNow) {
218
- if (!room || typeof room !== "object") {
219
- this.setStatus(t("status.roomMissing"), "error");
220
- return;
221
- }
222
- const summary = this.state.rooms.find((item) => item.id === roomID);
223
- if (summary) {
224
- summary.joined = true;
225
- summary.name = String(room.name ?? summary.name);
226
- if (Array.isArray(room.participants)) summary.participants = room.participants;
227
- }
228
- const cache = this.historyList(roomID);
229
- mergeMessages(cache, room.messages);
230
- this.state.current = {
231
- id: roomID,
232
- name: String(room.name ?? summary?.name ?? roomID),
233
- participants: Array.isArray(room.participants) ? room.participants : summary?.participants ?? [],
234
- };
235
- this.state.joined = true;
236
- this.state.pending = null;
237
- this.state.messages = cache;
238
- this.state.connection = "online";
239
- this.state.status = joinedNow ? t("status.joined", { name: this.state.current.name }) : "";
240
- this.state.statusKind = "success";
241
- if (typeof room.hasMore === "boolean") this.historyMore.set(roomID, room.hasMore);
242
- const oldest = oldestSeq(cache);
243
- if (oldest !== undefined) this.historyCursor.set(roomID, oldest);
244
- this.touch();
245
- this.markRead();
246
- if (this.autoPoll) this.startPolling();
247
- }
248
-
249
- historyList(roomID) {
250
- let list = this.cache.get(roomID);
251
- if (!list) {
252
- list = [];
253
- this.cache.set(roomID, list);
254
- }
255
- return list;
256
- }
257
-
258
- merge(items) {
259
- mergeMessages(this.state.messages, items);
260
- }
261
-
262
- lastTs() {
263
- return this.state.messages.reduce((max, message) => Math.max(max, messageStamp(message)), 0);
264
- }
265
-
266
- async pollOnce() {
267
- const room = this.state.current;
268
- if (!room || !this.state.joined) return false;
269
- const roomID = room.id;
270
- const controller = typeof AbortController === "function" ? new AbortController() : null;
271
- this.pollAbort = controller;
272
- try {
273
- const result = await this.api.poll(
274
- roomID,
275
- this.sessionID,
276
- this.lastTs(),
277
- controller ? { signal: controller.signal } : undefined,
278
- );
279
- if (!this.isCurrent(roomID)) return false;
280
- if (!result || result.member === false) {
281
- this.notJoined(roomID, t("status.memberLost"));
282
- return false;
283
- }
284
- const wasOffline = this.state.connection !== "online";
285
- this.merge(result.items);
286
- this.state.connection = "online";
287
- if (wasOffline) {
288
- this.state.status = t("status.reconnected");
241
+ this.state.messages = cache;
242
+ this.state.connection = "online";
243
+ this.state.status = joinedNow ? t("status.joined", { name: this.state.current.name }) : "";
289
244
  this.state.statusKind = "success";
290
- }
291
- this.touch();
292
- this.markRead();
293
- return true;
294
- } catch (error) {
295
- if (controller?.signal.aborted) return false;
296
- this.state.connection = "reconnecting";
297
- this.state.status = t("status.connectionLost", { error: errorText(error) });
298
- this.state.statusKind = "error";
299
- this.touch();
300
- return false;
301
- } finally {
302
- if (this.pollAbort === controller) this.pollAbort = null;
303
- }
304
- }
305
-
306
- startPolling() {
307
- const token = ++this.pollToken;
308
- let delay = 0;
309
- const step = async () => {
310
- if (this.closed || token !== this.pollToken || !this.state.joined) return;
311
- const ok = await this.pollOnce();
312
- if (this.closed || token !== this.pollToken || !this.state.joined) return;
313
- delay = ok ? 0 : Math.min(delay ? delay * 2 : BACKOFF_MS[0], BACKOFF_MS.at(-1));
314
- this.later(step, delay);
315
- };
316
- step();
317
- }
318
-
319
- stopPolling() {
320
- this.pollToken += 1;
321
- this.pollAbort?.abort();
322
- }
323
-
324
- notJoined(roomID, message) {
325
- if (!this.isCurrent(roomID)) return;
326
- this.state.joined = false;
327
- const summary = this.state.rooms.find((room) => room.id === roomID);
328
- if (summary) summary.joined = false;
329
- this.setStatus(message, "error");
330
- }
331
-
332
- markRead() {
333
- const room = this.state.current;
334
- const newest = this.lastTs();
335
- if (!room || !newest) return;
336
- try {
337
- const result = this.api.markRead?.(room.id, this.sessionID, newest);
338
- if (result && typeof result.catch === "function") result.catch(() => {});
339
- } catch {
340
- // A read receipt failure must not interrupt the UI.
341
- }
342
- }
343
-
344
- async send() {
345
- const raw = String(this.state.draft ?? "").trim();
346
- if (!raw) return;
347
- const room = this.state.current;
348
- if (!room || !this.state.joined) {
349
- this.setStatus(t("status.joinBeforeSend"), "error");
350
- return;
351
- }
352
- const text = canonicalizeMentions(raw, room.participants);
353
- // Reuse the attempt's id when the same failed text is sent again, so a lost
354
- // response cannot produce two messages.
355
- const attempt = this.pendingSend && this.pendingSend.text === raw
356
- ? this.pendingSend
357
- : { text: raw, clientMessageID: globalThis.crypto.randomUUID() };
358
- this.pendingSend = attempt;
359
- this.state.draft = "";
360
- this.touch();
361
- try {
362
- const result = await this.api.send(room.id, this.sessionID, text, { clientMessageID: attempt.clientMessageID });
363
- if (!this.isCurrent(room.id)) return;
364
- this.pendingSend = null;
365
- if (Array.isArray(result?.messages)) this.merge(result.messages);
366
- else if (result?.id) this.merge([result]);
367
- this.state.highlight = null;
368
- this.state.connection = "online";
369
- this.setStatus(t("status.sent"), "success");
370
- this.markRead();
371
- } catch (error) {
372
- // Keep text typed while the request was in flight; dropping it loses work.
373
- this.state.draft = this.state.draft ? `${raw}\n${this.state.draft}` : raw;
374
- this.setStatus(t("status.sendFailed", { error: errorText(error) }), "error");
375
- }
376
- }
377
-
378
- setDraft(text) {
379
- this.state.draft = String(text ?? "");
380
- }
381
-
382
- appendDraft(text) {
383
- this.state.draft += String(text ?? "");
384
- this.state.focus = "composer";
385
- this.state.help = false;
386
- this.touch();
387
- }
388
-
389
- moveSelection(delta) {
390
- if (!this.state.rooms.length) return;
391
- this.state.selected = Math.min(
392
- this.state.rooms.length - 1,
393
- Math.max(0, this.state.selected + delta),
394
- );
395
- this.state.highlight = null;
396
- this.touch();
397
- }
398
-
399
- selectRoom(index) {
400
- if (!this.state.rooms[index]) return;
401
- this.state.selected = index;
402
- this.state.highlight = null;
403
- this.touch();
404
- }
405
-
406
- toggleFocus() {
407
- this.state.focus = this.state.focus === "rooms" ? "composer" : "rooms";
408
- if (this.state.focus === "composer") this.state.help = false;
409
- this.touch();
410
- }
411
-
412
- toggleHelp() {
413
- this.state.help = !this.state.help;
414
- this.touch();
415
- }
416
-
417
- clearOrBlur() {
418
- this.state.draft = "";
419
- this.state.focus = "rooms";
420
- this.state.highlight = null;
421
- this.touch();
422
- }
423
-
424
- clearHighlight() {
425
- if (!this.state.highlight) return;
426
- this.state.highlight = null;
427
- this.touch();
428
- }
429
-
430
- quit() {
431
- this.state.quit = true;
432
- this.close();
433
- this.touch();
434
- }
435
-
436
- close() {
437
- this.closed = true;
438
- this.pollToken += 1;
439
- this.pollAbort?.abort();
440
- for (const timer of this.timers) clearTimeout(timer);
441
- this.timers.clear();
442
- }
245
+ if (typeof room.hasMore === "boolean")
246
+ this.historyMore.set(roomID, room.hasMore);
247
+ const oldest = oldestSeq(cache);
248
+ if (oldest !== undefined)
249
+ this.historyCursor.set(roomID, oldest);
250
+ this.touch();
251
+ this.markRead();
252
+ if (this.autoPoll)
253
+ this.startPolling();
254
+ }
255
+ historyList(roomID) {
256
+ let list = this.cache.get(roomID);
257
+ if (!list) {
258
+ list = [];
259
+ this.cache.set(roomID, list);
260
+ }
261
+ return list;
262
+ }
263
+ merge(items) {
264
+ mergeMessages(this.state.messages, items);
265
+ }
266
+ lastTs() {
267
+ return this.state.messages.reduce((max, message) => Math.max(max, messageStamp(message)), 0);
268
+ }
269
+ async pollOnce() {
270
+ const room = this.state.current;
271
+ if (!room || !this.state.joined)
272
+ return false;
273
+ const roomID = room.id;
274
+ const controller = typeof AbortController === "function" ? new AbortController() : null;
275
+ this.pollAbort = controller;
276
+ try {
277
+ const result = await this.api.poll(roomID, this.sessionID, this.lastTs(), controller ? { signal: controller.signal } : undefined);
278
+ if (!this.isCurrent(roomID))
279
+ return false;
280
+ if (result === null)
281
+ throw new Error("Invalid inbox response");
282
+ if (result.member === false) {
283
+ this.notJoined(roomID, t("status.memberLost"));
284
+ return false;
285
+ }
286
+ const wasOffline = this.state.connection !== "online";
287
+ this.merge(result.items);
288
+ this.state.connection = "online";
289
+ if (wasOffline) {
290
+ this.state.status = t("status.reconnected");
291
+ this.state.statusKind = "success";
292
+ }
293
+ this.touch();
294
+ this.markRead();
295
+ return true;
296
+ }
297
+ catch (error) {
298
+ if (controller?.signal.aborted)
299
+ return false;
300
+ this.state.connection = "reconnecting";
301
+ this.state.status = t("status.connectionLost", { error: errorText(error) });
302
+ this.state.statusKind = "error";
303
+ this.touch();
304
+ return false;
305
+ }
306
+ finally {
307
+ if (this.pollAbort === controller)
308
+ this.pollAbort = null;
309
+ }
310
+ }
311
+ startPolling() {
312
+ const token = ++this.pollToken;
313
+ let delay = 0;
314
+ const step = async () => {
315
+ if (this.closed || token !== this.pollToken || !this.state.joined)
316
+ return;
317
+ const ok = await this.pollOnce();
318
+ if (this.closed || token !== this.pollToken || !this.state.joined)
319
+ return;
320
+ delay = ok ? 0 : Math.min(delay ? delay * 2 : FIRST_BACKOFF, MAX_BACKOFF);
321
+ this.later(step, delay);
322
+ };
323
+ void step();
324
+ }
325
+ stopPolling() {
326
+ this.pollToken += 1;
327
+ this.pollAbort?.abort();
328
+ }
329
+ notJoined(roomID, message) {
330
+ if (!this.isCurrent(roomID))
331
+ return;
332
+ this.state.joined = false;
333
+ const summary = this.state.rooms.find((room) => room.id === roomID);
334
+ if (summary)
335
+ summary.joined = false;
336
+ this.setStatus(message, "error");
337
+ }
338
+ markRead() {
339
+ const room = this.state.current;
340
+ const newest = this.lastTs();
341
+ if (!room || !newest)
342
+ return;
343
+ try {
344
+ const result = this.api.markRead?.(room.id, this.sessionID, newest);
345
+ if (result && typeof result.catch === "function")
346
+ result.catch(() => { });
347
+ }
348
+ catch {
349
+ // A read receipt failure must not interrupt the UI.
350
+ }
351
+ }
352
+ async send() {
353
+ const raw = String(this.state.draft ?? "").trim();
354
+ if (!raw)
355
+ return;
356
+ const room = this.state.current;
357
+ if (!room || !this.state.joined) {
358
+ this.setStatus(t("status.joinBeforeSend"), "error");
359
+ return;
360
+ }
361
+ const text = canonicalizeMentions(raw, room.participants);
362
+ // Reuse the attempt's id when the same failed text is sent again, so a lost
363
+ // response cannot produce two messages.
364
+ const attempt = this.pendingSend && this.pendingSend.text === raw
365
+ ? this.pendingSend
366
+ : { text: raw, clientMessageID: globalThis.crypto.randomUUID() };
367
+ this.pendingSend = attempt;
368
+ this.state.draft = "";
369
+ this.touch();
370
+ try {
371
+ const result = await this.api.send(room.id, this.sessionID, text, { clientMessageID: attempt.clientMessageID });
372
+ if (!this.isCurrent(room.id))
373
+ return;
374
+ this.pendingSend = null;
375
+ if (Array.isArray(result?.messages))
376
+ this.merge(result.messages);
377
+ else if (result?.id)
378
+ this.merge([result]);
379
+ this.state.highlight = null;
380
+ this.state.connection = "online";
381
+ this.setStatus(t("status.sent"), "success");
382
+ this.markRead();
383
+ }
384
+ catch (error) {
385
+ // Keep text typed while the request was in flight; dropping it loses work.
386
+ this.state.draft = this.state.draft ? `${raw}\n${this.state.draft}` : raw;
387
+ this.setStatus(t("status.sendFailed", { error: errorText(error) }), "error");
388
+ }
389
+ }
390
+ setDraft(text) {
391
+ this.state.draft = String(text ?? "");
392
+ }
393
+ appendDraft(text) {
394
+ this.state.draft += String(text ?? "");
395
+ this.state.focus = "composer";
396
+ this.state.help = false;
397
+ this.touch();
398
+ }
399
+ moveSelection(delta) {
400
+ if (!this.state.rooms.length)
401
+ return;
402
+ this.state.selected = Math.min(this.state.rooms.length - 1, Math.max(0, this.state.selected + delta));
403
+ this.state.highlight = null;
404
+ this.touch();
405
+ }
406
+ selectRoom(index) {
407
+ if (!this.state.rooms[index])
408
+ return;
409
+ this.state.selected = index;
410
+ this.state.highlight = null;
411
+ this.touch();
412
+ }
413
+ toggleFocus() {
414
+ this.state.focus = this.state.focus === "rooms" ? "composer" : "rooms";
415
+ if (this.state.focus === "composer")
416
+ this.state.help = false;
417
+ this.touch();
418
+ }
419
+ toggleHelp() {
420
+ this.state.help = !this.state.help;
421
+ this.touch();
422
+ }
423
+ clearOrBlur() {
424
+ this.state.draft = "";
425
+ this.state.focus = "rooms";
426
+ this.state.highlight = null;
427
+ this.touch();
428
+ }
429
+ clearHighlight() {
430
+ if (!this.state.highlight)
431
+ return;
432
+ this.state.highlight = null;
433
+ this.touch();
434
+ }
435
+ quit() {
436
+ this.state.quit = true;
437
+ this.close();
438
+ this.touch();
439
+ }
440
+ close() {
441
+ this.closed = true;
442
+ this.pollToken += 1;
443
+ this.pollAbort?.abort();
444
+ for (const timer of this.timers)
445
+ clearTimeout(timer);
446
+ this.timers.clear();
447
+ }
443
448
  }
444
-
445
449
  attachPalette(ChatApp);
446
450
  attachInfo(ChatApp);
447
451
  attachSettings(ChatApp);
@@ -449,7 +453,6 @@ attachReminders(ChatApp);
449
453
  attachRoomSettings(ChatApp);
450
454
  attachMessageActions(ChatApp);
451
455
  attachJoinRequests(ChatApp);
452
-
453
456
  export function createApp(options) {
454
- return new ChatApp(options);
455
- }
457
+ return new ChatApp(options);
458
+ }