@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
@@ -1,443 +1,447 @@
1
1
  // CLI parity commands: read-only member/question/file/reminder views plus
2
2
  // announcement and question actions. Reuses the palette overlay as a scrollable
3
3
  // info/list/detail surface instead of introducing another panel.
4
- import {
5
- announcementRows,
6
- fileRows,
7
- memberRows,
8
- questionRows,
9
- } from "./info-content.mjs";
4
+ import { announcementRows, fileRows, memberRows, questionRows, } from "./info-content.mjs";
10
5
  import { t } from "./i18n.mjs";
11
6
  import { sanitizeText } from "./messages.mjs";
12
-
13
7
  const errorText = (error) => (error instanceof Error ? error.message : String(error ?? "unknown error"));
14
8
  export const MAX_UPLOAD_BYTES = 100 * 1024 * 1024;
15
9
  const announcementHint = () => t("info.announcementHint");
16
-
17
10
  export function createInfoPalette(kind, title, subtitle) {
18
- return {
19
- mode: "info",
20
- kind,
21
- title,
22
- subtitle,
23
- roomId: "",
24
- items: [],
25
- selected: 0,
26
- error: "",
27
- loading: false,
28
- hint: "",
29
- query: "",
30
- scope: "room",
31
- results: [],
32
- searching: false,
33
- prompt: null,
34
- confirm: null,
35
- detail: null,
36
- };
11
+ return {
12
+ mode: "info",
13
+ kind,
14
+ title,
15
+ subtitle,
16
+ roomId: "",
17
+ items: [],
18
+ selected: 0,
19
+ error: "",
20
+ loading: false,
21
+ hint: "",
22
+ query: "",
23
+ scope: "room",
24
+ results: [],
25
+ searching: false,
26
+ confirm: null,
27
+ detail: null,
28
+ };
37
29
  }
38
-
39
30
  export function attachInfo(Class) {
40
- Object.assign(Class.prototype, {
41
- currentInfoRoom() {
42
- if (this.state.current && this.state.joined) return this.state.current;
43
- this.state.palette = null;
44
- this.setStatus(t("info.joinFirst"), "error");
45
- return null;
46
- },
47
-
48
- // A palette acts on the room it was opened for, even if the rail has since
49
- // switched rooms; otherwise an announcement prompt could overwrite the new
50
- // room's same-named announcement.
51
- paletteRoomId(palette) {
52
- const roomId = palette.roomId || this.state.current?.id;
53
- if (!roomId) {
54
- palette.error = t("info.joinFirst");
55
- this.touch();
56
- }
57
- return roomId;
58
- },
59
-
60
- async openInfo(kind, title, hint, load) {
61
- const room = this.currentInfoRoom();
62
- if (!room) return;
63
- const palette = createInfoPalette(kind, title, `#${room.name}`);
64
- palette.roomId = room.id;
65
- palette.hint = hint;
66
- palette.loading = true;
67
- this.state.palette = palette;
68
- this.touch();
69
- try {
70
- const items = await load(room);
71
- if (this.state.palette !== palette) return;
72
- palette.items = Array.isArray(items) ? items : [];
73
- palette.loading = false;
74
- palette.selected = Math.min(palette.selected, Math.max(0, palette.items.length - 1));
75
- this.touch();
76
- } catch (error) {
77
- if (this.state.palette !== palette) return;
78
- palette.loading = false;
79
- palette.error = errorText(error);
80
- this.touch();
81
- }
82
- },
83
-
84
- openMembers() {
85
- return this.openInfo("members", t("info.members"), t("info.readOnlyHint"), async (room) =>
86
- memberRows(await this.api.members(room.id), this.sessionID));
87
- },
88
-
89
- openAnnouncements() {
90
- return this.openInfo("announcements", t("info.announcements"), announcementHint(), async (room) =>
91
- announcementRows(await this.api.announcements(room.id)));
92
- },
93
-
94
- openQuestions() {
95
- return this.openInfo("questions", t("info.questions"), t("info.questionHint"), async (room) =>
96
- questionRows(await this.api.questions(room.id), this.state.messages));
97
- },
98
-
99
- openFiles() {
100
- return this.openInfo("files", t("info.files"), t("info.fileHint"), async (room) => {
101
- const result = await this.api.resources(room.id, this.sessionID);
102
- return fileRows(Array.isArray(result) ? result : result?.resources);
103
- });
104
- },
105
-
106
- async runInfoSelection() {
107
- const palette = this.state.palette;
108
- if (!palette || palette.mode !== "info" || palette.loading) return;
109
- const item = this.paletteItems()[palette.selected];
110
- if (!item) return;
111
- if (palette.kind === "settings") {
112
- this.editSetting(item);
113
- return;
114
- }
115
- if (palette.kind === "announcements") {
116
- palette.mode = "detail";
117
- palette.detail = {
118
- title: String(item.value),
119
- markdown: String(item.content ?? "").trim() || t("common.emptyAnnouncement"),
120
- kind: "announcements",
121
- };
122
- palette.hint = t("info.announcementBackHint");
123
- this.touch();
124
- return;
125
- }
126
- if (palette.kind === "questions" && item.open) {
127
- palette.mode = "prompt";
128
- palette.prompt = {
129
- label: t("info.answerLabel", { text: sanitizeText(item.text).slice(0, 40) }),
130
- value: "",
131
- action: "question-answer",
132
- data: { id: String(item.value) },
133
- };
134
- palette.hint = t("palette.enterPostHint");
135
- this.touch();
136
- return;
137
- }
138
- if (palette.kind === "join-requests") {
139
- this.askJoinDecision("approve");
140
- return;
141
- }
142
- if (palette.kind === "files") await this.downloadFile(item);
143
- },
144
-
145
- startAnnouncementCreate() {
146
- const palette = this.state.palette;
147
- if (!palette || palette.mode !== "info" || palette.kind !== "announcements") return;
148
- palette.mode = "prompt";
149
- palette.prompt = { label: t("info.announcementName"), value: "", action: "announcement-name", data: {} };
150
- palette.error = "";
151
- palette.hint = t("palette.enterNextHint");
152
- this.touch();
153
- },
154
-
155
- askDeleteFile() {
156
- const palette = this.state.palette;
157
- if (!palette || palette.mode !== "info" || palette.kind !== "files") return;
158
- const item = this.paletteItems()[palette.selected];
159
- if (!item?.resource) return;
160
- palette.mode = "confirm";
161
- palette.previous = { mode: "info", hint: palette.hint };
162
- palette.confirm = {
163
- text: t("info.deleteFile", { name: sanitizeText(item.resource.name) }),
164
- action: "file-delete",
165
- data: { id: item.resource.id, name: item.resource.name },
166
- };
167
- palette.error = "";
168
- palette.hint = t("palette.deleteHint");
169
- this.touch();
170
- },
171
-
172
- async runFileDeleteConfirm(confirm) {
173
- const palette = this.state.palette;
174
- if (!palette) return;
175
- const roomId = this.paletteRoomId(palette);
176
- if (!roomId) return;
177
- palette.searching = true;
178
- palette.error = "";
179
- this.touch();
180
- try {
181
- await this.api.deleteResource(roomId, confirm.data.id, this.sessionID);
182
- this.setStatus(t("info.fileDeleted", { name: confirm.data.name }), "success");
183
- await this.openFiles();
184
- } catch (error) {
185
- if (this.state.palette === palette) {
186
- palette.mode = "info";
187
- palette.confirm = null;
188
- palette.previous = null;
189
- palette.searching = false;
190
- palette.hint = t("info.fileHint");
191
- palette.error = errorText(error);
192
- this.touch();
193
- }
194
- }
195
- },
196
-
197
- startFileUpload() {
198
- const palette = this.state.palette;
199
- if (!palette || palette.mode !== "info" || palette.kind !== "files") return;
200
- palette.mode = "prompt";
201
- palette.prompt = { label: t("info.filePath"), value: "", action: "file-upload", data: {} };
202
- palette.error = "";
203
- palette.hint = t("palette.enterUploadHint");
204
- this.touch();
205
- },
206
-
207
- askDeleteAnnouncement(name) {
208
- const palette = this.state.palette;
209
- const selected = name ?? palette?.items?.[palette.selected]?.value;
210
- if (!palette || !selected) return;
211
- palette.mode = "confirm";
212
- palette.error = "";
213
- palette.previous = { mode: "info", hint: palette.hint };
214
- palette.confirm = {
215
- text: t("info.deleteAnnouncement", { name: sanitizeText(selected) }),
216
- action: "announcement-delete",
217
- data: { name: String(selected) },
218
- };
219
- palette.hint = t("palette.deleteHint");
220
- this.touch();
221
- },
222
-
223
- async runConfirm() {
224
- const palette = this.state.palette;
225
- const confirm = palette?.confirm;
226
- if (!palette || !confirm) return;
227
- if (typeof confirm.action === "string" && confirm.action.startsWith("message-")) {
228
- await this.runMessageConfirm(confirm);
229
- return;
230
- }
231
- if (typeof confirm.action === "string" && confirm.action.startsWith("reminder-")) {
232
- await this.runReminderConfirm(confirm);
233
- return;
234
- }
235
- if (typeof confirm.action === "string" && confirm.action.startsWith("room-")) {
236
- await this.runRoomSettingsConfirm(confirm);
237
- return;
238
- }
239
- if (typeof confirm.action === "string" && confirm.action.startsWith("join-")) {
240
- await this.runJoinConfirm(confirm);
241
- return;
242
- }
243
- if (typeof confirm.action === "string" && confirm.action.startsWith("file-")) {
244
- await this.runFileDeleteConfirm(confirm);
245
- return;
246
- }
247
- if (confirm.action === "announcement-delete") {
248
- palette.searching = true;
249
- this.touch();
250
- try {
251
- await this.api.deleteAnnouncement(palette.roomId, confirm.data.name, this.sessionID);
252
- this.setStatus(t("info.announcementDeleted", { name: confirm.data.name }), "success");
253
- await this.openAnnouncements();
254
- } catch (error) {
255
- if (this.state.palette === palette) {
256
- palette.searching = false;
257
- palette.error = errorText(error);
31
+ Object.assign(Class.prototype, {
32
+ currentInfoRoom() {
33
+ if (this.state.current && this.state.joined)
34
+ return this.state.current;
35
+ this.state.palette = null;
36
+ this.setStatus(t("info.joinFirst"), "error");
37
+ return null;
38
+ },
39
+ // A palette acts on the room it was opened for, even if the rail has since
40
+ // switched rooms; otherwise an announcement prompt could overwrite the new
41
+ // room's same-named announcement.
42
+ paletteRoomId(palette) {
43
+ const roomId = palette.roomId || this.state.current?.id;
44
+ if (!roomId) {
45
+ palette.error = t("info.joinFirst");
46
+ this.touch();
47
+ }
48
+ return roomId;
49
+ },
50
+ async openInfo(kind, title, hint, load) {
51
+ const room = this.currentInfoRoom();
52
+ if (!room)
53
+ return;
54
+ const palette = createInfoPalette(kind, title, `#${room.name}`);
55
+ palette.roomId = room.id;
56
+ palette.hint = hint;
57
+ palette.loading = true;
58
+ this.state.palette = palette;
258
59
  this.touch();
259
- }
260
- }
261
- }
262
- },
263
-
264
- cancelConfirm() {
265
- const palette = this.state.palette;
266
- if (!palette) return;
267
- const previous = palette.previous;
268
- palette.previous = null;
269
- palette.confirm = null;
270
- palette.error = "";
271
- if (previous?.mode) {
272
- palette.mode = previous.mode;
273
- palette.hint = previous.hint ?? "";
274
- } else {
275
- palette.mode = "info";
276
- palette.hint = announcementHint();
277
- }
278
- this.touch();
279
- },
280
-
281
- backFromDetail() {
282
- const palette = this.state.palette;
283
- if (!palette) return;
284
- if (palette.previous?.mode === "info") {
285
- palette.mode = "info";
286
- palette.detail = null;
287
- palette.hint = palette.previous.hint ?? t("palette.listHint");
288
- palette.previous = null;
289
- this.touch();
290
- return;
291
- }
292
- if (palette.kind !== "announcements") {
293
- this.closePalette();
294
- return;
295
- }
296
- palette.mode = "info";
297
- palette.detail = null;
298
- palette.hint = announcementHint();
299
- this.touch();
300
- },
301
-
302
- async downloadFile(item) {
303
- const palette = this.state.palette;
304
- const roomId = palette ? this.paletteRoomId(palette) : this.state.current?.id;
305
- if (!roomId || !item?.resource) return;
306
- if (palette) palette.searching = true;
307
- this.touch();
308
- try {
309
- const payload = await this.api.resourceContent(roomId, item.resource.id);
310
- const [{ mkdir, writeFile, access }, path] = await Promise.all([
311
- import("node:fs/promises"),
312
- import("node:path"),
313
- ]);
314
- await mkdir(this.downloadDir, { recursive: true });
315
- const base = path.basename(String(item.resource.name || "download")).replace(/[\0\r\n]/g, "_");
316
- const filename = !base || base === "." || base === ".." ? "download" : base;
317
- let target = path.join(this.downloadDir, filename);
318
- try {
319
- await access(target);
320
- const extension = path.extname(filename);
321
- target = path.join(this.downloadDir, `${path.basename(filename, extension)}-${Date.now()}${extension}`);
322
- } catch {
323
- // Target does not exist yet.
324
- }
325
- await writeFile(target, payload.bytes ?? payload);
326
- this.closePalette();
327
- this.setStatus(t("info.fileSaved", { target }), "success");
328
- } catch (error) {
329
- if (this.state.palette === palette) {
330
- palette.searching = false;
331
- palette.error = errorText(error);
332
- this.touch();
333
- }
334
- }
335
- },
336
-
337
- async submitCommandPrompt(prompt, value) {
338
- const palette = this.state.palette;
339
- if (!palette || palette.mode !== "prompt") return;
340
- if (typeof prompt.action === "string" && prompt.action.startsWith("settings-")) {
341
- await this.submitPreferencePrompt(prompt, value);
342
- return;
343
- }
344
- if (typeof prompt.action === "string" && prompt.action.startsWith("reminder-")) {
345
- await this.submitReminderPrompt(prompt, value);
346
- return;
347
- }
348
- if (typeof prompt.action === "string" && prompt.action.startsWith("room-")) {
349
- await this.submitRoomSettingsPrompt(prompt, value);
350
- return;
351
- }
352
- if (typeof prompt.action === "string" && prompt.action.startsWith("message-")) {
353
- await this.submitMessagePrompt(prompt, value);
354
- return;
355
- }
356
- const roomId = this.paletteRoomId(palette);
357
- if (!roomId) return;
358
- if (prompt.action === "announcement-name") {
359
- const name = String(value ?? "").trim();
360
- if (!name) {
361
- palette.error = t("info.announcementNameRequired");
362
- this.touch();
363
- return;
364
- }
365
- palette.prompt = { label: t("info.announcementContent", { name: sanitizeText(name) }), value: "", action: "announcement-content", data: { name } };
366
- palette.error = "";
367
- this.touch();
368
- return;
369
- }
370
- if (prompt.action === "announcement-content") {
371
- const name = prompt.data?.name;
372
- if (!name) return;
373
- palette.searching = true;
374
- this.touch();
375
- try {
376
- await this.api.saveAnnouncement(roomId, name, String(value ?? ""), this.sessionID);
377
- this.setStatus(t("info.announcementSaved", { name }), "success");
378
- await this.openAnnouncements();
379
- } catch (error) {
380
- if (this.state.palette === palette) {
381
- palette.searching = false;
382
- palette.error = errorText(error);
60
+ try {
61
+ const items = await load(room);
62
+ if (this.state.palette !== palette)
63
+ return;
64
+ palette.items = Array.isArray(items) ? items : [];
65
+ palette.loading = false;
66
+ palette.selected = Math.min(palette.selected, Math.max(0, palette.items.length - 1));
67
+ this.touch();
68
+ }
69
+ catch (error) {
70
+ if (this.state.palette !== palette)
71
+ return;
72
+ palette.loading = false;
73
+ palette.error = errorText(error);
74
+ this.touch();
75
+ }
76
+ },
77
+ openMembers() {
78
+ return this.openInfo("members", t("info.members"), t("info.readOnlyHint"), async (room) => memberRows(await this.api.members(room.id), this.sessionID));
79
+ },
80
+ openAnnouncements() {
81
+ return this.openInfo("announcements", t("info.announcements"), announcementHint(), async (room) => announcementRows(await this.api.announcements(room.id)));
82
+ },
83
+ openQuestions() {
84
+ return this.openInfo("questions", t("info.questions"), t("info.questionHint"), async (room) => questionRows(await this.api.questions(room.id), this.state.messages));
85
+ },
86
+ openFiles() {
87
+ return this.openInfo("files", t("info.files"), t("info.fileHint"), async (room) => {
88
+ const result = await this.api.resources(room.id, this.sessionID);
89
+ const list = Array.isArray(result) ? result : result?.resources;
90
+ return fileRows(list ?? []);
91
+ });
92
+ },
93
+ async runInfoSelection() {
94
+ const palette = this.state.palette;
95
+ if (!palette || palette.mode !== "info" || palette.loading)
96
+ return;
97
+ const item = this.paletteItems()[palette.selected];
98
+ if (!item)
99
+ return;
100
+ if (palette.kind === "settings") {
101
+ this.editSetting(item);
102
+ return;
103
+ }
104
+ if (palette.kind === "announcements") {
105
+ palette.mode = "detail";
106
+ palette.detail = {
107
+ title: String(item.value),
108
+ markdown: String(item.content ?? "").trim() || t("common.emptyAnnouncement"),
109
+ kind: "announcements",
110
+ };
111
+ palette.hint = t("info.announcementBackHint");
112
+ this.touch();
113
+ return;
114
+ }
115
+ if (palette.kind === "questions" && item.open) {
116
+ palette.mode = "prompt";
117
+ palette.prompt = {
118
+ label: t("info.answerLabel", { text: sanitizeText(item.text).slice(0, 40) }),
119
+ value: "",
120
+ action: "question-answer",
121
+ data: { id: String(item.value) },
122
+ };
123
+ palette.hint = t("palette.enterPostHint");
124
+ this.touch();
125
+ return;
126
+ }
127
+ if (palette.kind === "join-requests") {
128
+ this.askJoinDecision("approve");
129
+ return;
130
+ }
131
+ if (palette.kind === "files")
132
+ await this.downloadFile(item);
133
+ },
134
+ startAnnouncementCreate() {
135
+ const palette = this.state.palette;
136
+ if (!palette || palette.mode !== "info" || palette.kind !== "announcements")
137
+ return;
138
+ palette.mode = "prompt";
139
+ palette.prompt = { label: t("info.announcementName"), value: "", action: "announcement-name", data: {} };
140
+ palette.error = "";
141
+ palette.hint = t("palette.enterNextHint");
383
142
  this.touch();
384
- }
385
- }
386
- return;
387
- }
388
- if (prompt.action === "question-answer") {
389
- const answer = String(value ?? "").trim();
390
- if (!answer) {
391
- palette.error = t("info.answerRequired");
392
- this.touch();
393
- return;
394
- }
395
- palette.searching = true;
396
- this.touch();
397
- try {
398
- await this.api.answerQuestion(roomId, prompt.data?.id, this.sessionID, answer);
399
- this.setStatus(t("info.answerPosted"), "success");
400
- await this.openQuestions();
401
- } catch (error) {
402
- if (this.state.palette === palette) {
403
- palette.searching = false;
404
- palette.error = errorText(error);
143
+ },
144
+ askDeleteFile() {
145
+ const palette = this.state.palette;
146
+ if (!palette || palette.mode !== "info" || palette.kind !== "files")
147
+ return;
148
+ const item = this.paletteItems()[palette.selected];
149
+ if (!item?.resource)
150
+ return;
151
+ palette.mode = "confirm";
152
+ palette.previous = { mode: "info", hint: palette.hint };
153
+ palette.confirm = {
154
+ text: t("info.deleteFile", { name: sanitizeText(item.resource.name) }),
155
+ action: "file-delete",
156
+ data: { id: String(item.resource.id), name: item.resource.name },
157
+ };
158
+ palette.error = "";
159
+ palette.hint = t("palette.deleteHint");
405
160
  this.touch();
406
- }
407
- }
408
- return;
409
- }
410
- if (prompt.action === "file-upload") {
411
- const file = String(value ?? "").trim();
412
- const path = await import("node:path");
413
- if (!file || !path.isAbsolute(file)) {
414
- palette.error = t("info.filePathRequired");
415
- this.touch();
416
- return;
417
- }
418
- palette.searching = true;
419
- this.touch();
420
- try {
421
- const fs = await import("node:fs/promises");
422
- const info = await fs.stat(file);
423
- if (info.size > MAX_UPLOAD_BYTES) {
424
- palette.searching = false;
425
- palette.error = t("info.fileTooLarge");
161
+ },
162
+ async runFileDeleteConfirm(confirm) {
163
+ const palette = this.state.palette;
164
+ if (!palette)
165
+ return;
166
+ const roomId = this.paletteRoomId(palette);
167
+ if (!roomId)
168
+ return;
169
+ palette.searching = true;
170
+ palette.error = "";
426
171
  this.touch();
427
- return;
428
- }
429
- const bytes = new Uint8Array(await fs.readFile(file));
430
- await this.api.uploadResource(roomId, this.sessionID, path.basename(file), bytes);
431
- this.setStatus(t("info.fileUploaded", { name: path.basename(file) }), "success");
432
- await this.openFiles();
433
- } catch (error) {
434
- if (this.state.palette === palette) {
435
- palette.searching = false;
436
- palette.error = errorText(error);
172
+ try {
173
+ await this.api.deleteResource(roomId, String(confirm.data.id), this.sessionID);
174
+ this.setStatus(t("info.fileDeleted", { name: confirm.data.name }), "success");
175
+ await this.openFiles();
176
+ }
177
+ catch (error) {
178
+ if (this.state.palette === palette) {
179
+ palette.mode = "info";
180
+ palette.confirm = null;
181
+ palette.previous = null;
182
+ palette.searching = false;
183
+ palette.hint = t("info.fileHint");
184
+ palette.error = errorText(error);
185
+ this.touch();
186
+ }
187
+ }
188
+ },
189
+ startFileUpload() {
190
+ const palette = this.state.palette;
191
+ if (!palette || palette.mode !== "info" || palette.kind !== "files")
192
+ return;
193
+ palette.mode = "prompt";
194
+ palette.prompt = { label: t("info.filePath"), value: "", action: "file-upload", data: {} };
195
+ palette.error = "";
196
+ palette.hint = t("palette.enterUploadHint");
437
197
  this.touch();
438
- }
439
- }
440
- }
441
- },
442
- });
443
- }
198
+ },
199
+ askDeleteAnnouncement(name) {
200
+ const palette = this.state.palette;
201
+ const selected = name ?? palette?.items?.[palette.selected]?.value;
202
+ if (!palette || !selected)
203
+ return;
204
+ palette.mode = "confirm";
205
+ palette.error = "";
206
+ palette.previous = { mode: "info", hint: palette.hint };
207
+ palette.confirm = {
208
+ text: t("info.deleteAnnouncement", { name: sanitizeText(selected) }),
209
+ action: "announcement-delete",
210
+ data: { name: String(selected) },
211
+ };
212
+ palette.hint = t("palette.deleteHint");
213
+ this.touch();
214
+ },
215
+ async runConfirm() {
216
+ const palette = this.state.palette;
217
+ const confirm = palette?.confirm;
218
+ if (!palette || !confirm)
219
+ return;
220
+ if (typeof confirm.action === "string" && confirm.action.startsWith("message-")) {
221
+ await this.runMessageConfirm(confirm);
222
+ return;
223
+ }
224
+ if (typeof confirm.action === "string" && confirm.action.startsWith("reminder-")) {
225
+ await this.runReminderConfirm(confirm);
226
+ return;
227
+ }
228
+ if (typeof confirm.action === "string" && confirm.action.startsWith("room-")) {
229
+ await this.runRoomSettingsConfirm(confirm);
230
+ return;
231
+ }
232
+ if (typeof confirm.action === "string" && confirm.action.startsWith("join-")) {
233
+ await this.runJoinConfirm(confirm);
234
+ return;
235
+ }
236
+ if (typeof confirm.action === "string" && confirm.action.startsWith("file-")) {
237
+ await this.runFileDeleteConfirm(confirm);
238
+ return;
239
+ }
240
+ if (confirm.action === "announcement-delete") {
241
+ palette.searching = true;
242
+ this.touch();
243
+ try {
244
+ await this.api.deleteAnnouncement(String(palette.roomId), String(confirm.data.name), this.sessionID);
245
+ this.setStatus(t("info.announcementDeleted", { name: confirm.data.name }), "success");
246
+ await this.openAnnouncements();
247
+ }
248
+ catch (error) {
249
+ if (this.state.palette === palette) {
250
+ palette.searching = false;
251
+ palette.error = errorText(error);
252
+ this.touch();
253
+ }
254
+ }
255
+ }
256
+ },
257
+ cancelConfirm() {
258
+ const palette = this.state.palette;
259
+ if (!palette)
260
+ return;
261
+ const previous = palette.previous;
262
+ palette.previous = null;
263
+ palette.confirm = null;
264
+ palette.error = "";
265
+ if (previous?.mode) {
266
+ palette.mode = previous.mode;
267
+ palette.hint = previous.hint ?? "";
268
+ }
269
+ else {
270
+ palette.mode = "info";
271
+ palette.hint = announcementHint();
272
+ }
273
+ this.touch();
274
+ },
275
+ backFromDetail() {
276
+ const palette = this.state.palette;
277
+ if (!palette)
278
+ return;
279
+ if (palette.previous?.mode === "info") {
280
+ palette.mode = "info";
281
+ palette.detail = null;
282
+ palette.hint = palette.previous.hint ?? t("palette.listHint");
283
+ palette.previous = null;
284
+ this.touch();
285
+ return;
286
+ }
287
+ if (palette.kind !== "announcements") {
288
+ this.closePalette();
289
+ return;
290
+ }
291
+ palette.mode = "info";
292
+ palette.detail = null;
293
+ palette.hint = announcementHint();
294
+ this.touch();
295
+ },
296
+ async downloadFile(item) {
297
+ const palette = this.state.palette;
298
+ const roomId = palette ? this.paletteRoomId(palette) : this.state.current?.id;
299
+ if (!roomId || !item.resource)
300
+ return;
301
+ if (palette)
302
+ palette.searching = true;
303
+ this.touch();
304
+ try {
305
+ const payload = await this.api.resourceContent(roomId, String(item.resource.id));
306
+ const [{ mkdir, writeFile, access }, path] = await Promise.all([
307
+ import("node:fs/promises"),
308
+ import("node:path"),
309
+ ]);
310
+ await mkdir(this.downloadDir, { recursive: true });
311
+ const base = path.basename(String(item.resource.name || "download")).replace(/[\0\r\n]/g, "_");
312
+ const filename = !base || base === "." || base === ".." ? "download" : base;
313
+ let target = path.join(this.downloadDir, filename);
314
+ try {
315
+ await access(target);
316
+ const extension = path.extname(filename);
317
+ target = path.join(this.downloadDir, `${path.basename(filename, extension)}-${Date.now()}${extension}`);
318
+ }
319
+ catch {
320
+ // Target does not exist yet.
321
+ }
322
+ const content = payload;
323
+ await writeFile(target, content instanceof Uint8Array ? content : payload.bytes);
324
+ this.closePalette();
325
+ this.setStatus(t("info.fileSaved", { target }), "success");
326
+ }
327
+ catch (error) {
328
+ if (palette && this.state.palette === palette) {
329
+ palette.searching = false;
330
+ palette.error = errorText(error);
331
+ this.touch();
332
+ }
333
+ }
334
+ },
335
+ async submitCommandPrompt(prompt, value) {
336
+ const palette = this.state.palette;
337
+ if (!palette || palette.mode !== "prompt")
338
+ return;
339
+ if (typeof prompt.action === "string" && prompt.action.startsWith("settings-")) {
340
+ await this.submitPreferencePrompt(prompt, value);
341
+ return;
342
+ }
343
+ if (typeof prompt.action === "string" && prompt.action.startsWith("reminder-")) {
344
+ await this.submitReminderPrompt(prompt, value);
345
+ return;
346
+ }
347
+ if (typeof prompt.action === "string" && prompt.action.startsWith("room-")) {
348
+ await this.submitRoomSettingsPrompt(prompt, value);
349
+ return;
350
+ }
351
+ if (typeof prompt.action === "string" && prompt.action.startsWith("message-")) {
352
+ await this.submitMessagePrompt(prompt, value);
353
+ return;
354
+ }
355
+ const roomId = this.paletteRoomId(palette);
356
+ if (!roomId)
357
+ return;
358
+ if (prompt.action === "announcement-name") {
359
+ const name = String(value ?? "").trim();
360
+ if (!name) {
361
+ palette.error = t("info.announcementNameRequired");
362
+ this.touch();
363
+ return;
364
+ }
365
+ palette.prompt = { label: t("info.announcementContent", { name: sanitizeText(name) }), value: "", action: "announcement-content", data: { name } };
366
+ palette.error = "";
367
+ this.touch();
368
+ return;
369
+ }
370
+ if (prompt.action === "announcement-content") {
371
+ const name = prompt.data?.name;
372
+ if (!name)
373
+ return;
374
+ palette.searching = true;
375
+ this.touch();
376
+ try {
377
+ await this.api.saveAnnouncement(roomId, name, String(value ?? ""), this.sessionID);
378
+ this.setStatus(t("info.announcementSaved", { name }), "success");
379
+ await this.openAnnouncements();
380
+ }
381
+ catch (error) {
382
+ if (this.state.palette === palette) {
383
+ palette.searching = false;
384
+ palette.error = errorText(error);
385
+ this.touch();
386
+ }
387
+ }
388
+ return;
389
+ }
390
+ if (prompt.action === "question-answer") {
391
+ const answer = String(value ?? "").trim();
392
+ if (!answer) {
393
+ palette.error = t("info.answerRequired");
394
+ this.touch();
395
+ return;
396
+ }
397
+ palette.searching = true;
398
+ this.touch();
399
+ try {
400
+ await this.api.answerQuestion(roomId, String(prompt.data?.id), this.sessionID, answer);
401
+ this.setStatus(t("info.answerPosted"), "success");
402
+ await this.openQuestions();
403
+ }
404
+ catch (error) {
405
+ if (this.state.palette === palette) {
406
+ palette.searching = false;
407
+ palette.error = errorText(error);
408
+ this.touch();
409
+ }
410
+ }
411
+ return;
412
+ }
413
+ if (prompt.action === "file-upload") {
414
+ const file = String(value ?? "").trim();
415
+ const path = await import("node:path");
416
+ if (!file || !path.isAbsolute(file)) {
417
+ palette.error = t("info.filePathRequired");
418
+ this.touch();
419
+ return;
420
+ }
421
+ palette.searching = true;
422
+ this.touch();
423
+ try {
424
+ const fs = await import("node:fs/promises");
425
+ const info = await fs.stat(file);
426
+ if (info.size > MAX_UPLOAD_BYTES) {
427
+ palette.searching = false;
428
+ palette.error = t("info.fileTooLarge");
429
+ this.touch();
430
+ return;
431
+ }
432
+ const bytes = new Uint8Array(await fs.readFile(file));
433
+ await this.api.uploadResource(roomId, this.sessionID, path.basename(file), bytes);
434
+ this.setStatus(t("info.fileUploaded", { name: path.basename(file) }), "success");
435
+ await this.openFiles();
436
+ }
437
+ catch (error) {
438
+ if (this.state.palette === palette) {
439
+ palette.searching = false;
440
+ palette.error = errorText(error);
441
+ this.touch();
442
+ }
443
+ }
444
+ }
445
+ },
446
+ });
447
+ }