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

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 (206) hide show
  1. package/CHANGELOG.md +17 -3
  2. package/README.md +5 -5
  3. package/README.zh-CN.md +5 -5
  4. package/cli.ts +20 -1
  5. package/dist/cli.js +3785 -1462
  6. package/package.json +19 -11
  7. package/packages/chat-room-channeld/README.md +16 -10
  8. package/packages/chat-room-channeld/src/delivery.ts +0 -4
  9. package/packages/chat-room-channeld/src/host.ts +48 -37
  10. package/packages/chat-room-channeld/src/inbound.ts +3 -6
  11. package/packages/chat-room-channeld/src/pg/database.ts +141 -49
  12. package/packages/chat-room-channeld/src/pg/sql.mjs +157 -142
  13. package/packages/chat-room-channeld/src/pg/sql.mts +175 -0
  14. package/packages/chat-room-channeld/src/pg/worker.mjs +148 -99
  15. package/packages/chat-room-channeld/src/pg/worker.mts +201 -0
  16. package/packages/chat-room-channeld/src/state-rows.ts +132 -0
  17. package/packages/chat-room-channeld/src/state.ts +129 -157
  18. package/plugin.ts +21 -5
  19. package/server/app.ts +9 -0
  20. package/server/assets/base.css +22 -2
  21. package/server/assets/chat/admin.js +17 -1
  22. package/server/assets/chat/announcements.js +79 -34
  23. package/server/assets/chat/attachments.js +3 -3
  24. package/server/assets/chat/clipboard.js +1 -1
  25. package/server/assets/chat/confirm-dialog.js +3 -0
  26. package/server/assets/chat/copy-action.js +1 -1
  27. package/server/assets/chat/create-room-dialog.js +7 -0
  28. package/server/assets/chat/diagram-viewer.js +4 -8
  29. package/server/assets/chat/doc-preview.js +71 -31
  30. package/server/assets/chat/editor.js +49 -10
  31. package/server/assets/chat/events.js +13 -6
  32. package/server/assets/chat/files.js +33 -27
  33. package/server/assets/chat/history-loader.js +31 -15
  34. package/server/assets/chat/integration-editor.js +27 -1
  35. package/server/assets/chat/integrations.js +108 -16
  36. package/server/assets/chat/join-requests.js +40 -2
  37. package/server/assets/chat/locales/de.js +5 -3
  38. package/server/assets/chat/locales/es.js +5 -3
  39. package/server/assets/chat/locales/fr.js +5 -3
  40. package/server/assets/chat/locales/it.js +5 -3
  41. package/server/assets/chat/locales/ja.js +5 -3
  42. package/server/assets/chat/locales/ko.js +5 -3
  43. package/server/assets/chat/markdown-footnotes.js +27 -15
  44. package/server/assets/chat/markdown.js +6 -12
  45. package/server/assets/chat/members.js +34 -22
  46. package/server/assets/chat/mentions.js +8 -5
  47. package/server/assets/chat/message-cache.js +9 -3
  48. package/server/assets/chat/message-polling.js +10 -4
  49. package/server/assets/chat/message-window.js +58 -1
  50. package/server/assets/chat/messages.js +90 -37
  51. package/server/assets/chat/office-preview.js +3 -3
  52. package/server/assets/chat/operator.js +624 -0
  53. package/server/assets/chat/palette.js +7 -10
  54. package/server/assets/chat/panels.js +42 -0
  55. package/server/assets/chat/questions.js +75 -58
  56. package/server/assets/chat/recall.js +14 -5
  57. package/server/assets/chat/receipts.js +63 -7
  58. package/server/assets/chat/refresh-timing.js +6 -0
  59. package/server/assets/chat/reminders.js +47 -13
  60. package/server/assets/chat/replies.js +39 -25
  61. package/server/assets/chat/resource-format.js +6 -3
  62. package/server/assets/chat/room-list.js +202 -3
  63. package/server/assets/chat/room-watcher.js +3 -1
  64. package/server/assets/chat/rooms.js +29 -7
  65. package/server/assets/chat/settings.js +37 -6
  66. package/server/assets/chat/shortcuts.js +3 -1
  67. package/server/assets/chat/side-pane.js +1 -1
  68. package/server/assets/chat/state.js +51 -18
  69. package/server/assets/chat/system-events.js +3 -2
  70. package/server/assets/chat/system-messages.js +15 -15
  71. package/server/assets/chat/tasks.js +37 -14
  72. package/server/assets/chat/window-controls.js +25 -15
  73. package/server/assets/code-highlight.css +0 -13
  74. package/server/assets/conversation.css +45 -5
  75. package/server/assets/doc-preview.css +9 -0
  76. package/server/assets/files.css +4 -0
  77. package/server/assets/monaco/integration-rules.js +1 -1
  78. package/server/assets/office/THIRD-PARTY-NOTICES.txt +23 -1
  79. package/server/assets/office/licenses/docx-preview-HEADER.txt +6 -0
  80. package/server/assets/office/licenses/echarts-LICENSE-d3.txt +27 -0
  81. package/server/assets/office/licenses/echarts-NOTICE.txt +5 -0
  82. package/server/assets/office/licenses/immediate-LICENSE.txt +20 -0
  83. package/server/assets/office/licenses/lie-LICENSE.txt +7 -0
  84. package/server/assets/office/licenses/pako-LICENSE.txt +21 -0
  85. package/server/assets/office/licenses/pako-ZLIB.txt +18 -0
  86. package/server/assets/office/licenses/setimmediate-LICENSE.txt +20 -0
  87. package/server/assets/responsive.css +12 -2
  88. package/server/assets/settings.css +3 -1
  89. package/server/assets/tasks.css +6 -1
  90. package/server/assets/window-controls.css +3 -1
  91. package/server/channels.ts +3 -2
  92. package/server/chat.html +7 -2
  93. package/server/inbox.ts +3 -1
  94. package/server/join-requests.ts +8 -1
  95. package/server/member-routes.ts +16 -2
  96. package/server/newrooms.ts +2 -13
  97. package/server/operator-auth.ts +21 -0
  98. package/server/operator-routes.ts +150 -0
  99. package/server/operator-state.ts +64 -0
  100. package/server/operator.css +139 -0
  101. package/server/room-owner.ts +17 -0
  102. package/server/routes/inbox.ts +15 -5
  103. package/server/routes/pages.ts +12 -2
  104. package/server/routes/room-actions.ts +4 -3
  105. package/server/sw.js +3 -1
  106. package/server/system.html +109 -0
  107. package/server/wake.ts +1 -1
  108. package/skills/chatroom/SKILL.md +9 -0
  109. package/src/README.md +1 -1
  110. package/src/acp/client.ts +254 -0
  111. package/src/acp/protocol.ts +300 -0
  112. package/src/acp/watch.ts +498 -0
  113. package/src/agent/agent-receipts.ts +98 -8
  114. package/src/center/center-client.ts +6 -1
  115. package/src/center/center-inbox.ts +54 -7
  116. package/src/center/center-wake.ts +57 -3
  117. package/src/center/forward-client.ts +48 -0
  118. package/src/center/mentions.ts +16 -3
  119. package/src/center/notification-delay.ts +139 -0
  120. package/src/center/queue.ts +3 -0
  121. package/src/config/file-config.ts +25 -0
  122. package/src/mcp/server.ts +389 -0
  123. package/src/protocol/config.ts +7 -0
  124. package/src/runtime/plugin-notifications.ts +43 -9
  125. package/src/runtime/v2-plugin.ts +14 -3
  126. package/src/runtime/v2-receiver.ts +10 -2
  127. package/src/runtime/watch.ts +5 -1
  128. package/src/state/pg-database.ts +133 -49
  129. package/src/state/pg-sql.mjs +157 -142
  130. package/src/state/pg-sql.mts +175 -0
  131. package/src/state/pg-worker.mjs +148 -99
  132. package/src/state/pg-worker.mts +201 -0
  133. package/src/state/receiver-state.ts +47 -0
  134. package/src/state/sqlite-database.ts +6 -2
  135. package/src/state/sqlite-members.ts +48 -4
  136. package/src/state/sqlite-messages.ts +25 -12
  137. package/src/state/sqlite-schema.ts +4 -1
  138. package/src/state/sqlite-state.ts +1 -1
  139. package/src/state/store.ts +31 -11
  140. package/src/tui/app-types.mjs +1 -0
  141. package/src/tui/app-types.mts +175 -0
  142. package/src/tui/app.mjs +473 -423
  143. package/src/tui/app.mts +543 -0
  144. package/src/tui/config.mjs +166 -158
  145. package/src/tui/config.mts +247 -0
  146. package/src/tui/http.mjs +239 -206
  147. package/src/tui/http.mts +412 -0
  148. package/src/tui/i18n.mjs +17 -20
  149. package/src/tui/i18n.mts +42 -0
  150. package/src/tui/info-app.mjs +431 -427
  151. package/src/tui/info-app.mts +500 -0
  152. package/src/tui/info-content.mjs +205 -199
  153. package/src/tui/info-content.mts +341 -0
  154. package/src/tui/join-app.mjs +75 -64
  155. package/src/tui/join-app.mts +99 -0
  156. package/src/tui/locales/en.mjs +302 -320
  157. package/src/tui/locales/en.mts +349 -0
  158. package/src/tui/locales/zh-CN.mjs +302 -320
  159. package/src/tui/locales/zh-CN.mts +349 -0
  160. package/src/tui/main.mjs +219 -213
  161. package/src/tui/main.mts +255 -0
  162. package/src/tui/markdown.mjs +128 -272
  163. package/src/tui/markdown.mts +152 -0
  164. package/src/tui/mentions.mjs +181 -184
  165. package/src/tui/mentions.mts +251 -0
  166. package/src/tui/message-app.mjs +181 -173
  167. package/src/tui/message-app.mts +213 -0
  168. package/src/tui/message-view.mjs +90 -111
  169. package/src/tui/message-view.mts +151 -0
  170. package/src/tui/messages.mjs +55 -49
  171. package/src/tui/messages.mts +106 -0
  172. package/src/tui/palette-app.mjs +364 -344
  173. package/src/tui/palette-app.mts +388 -0
  174. package/src/tui/palette.mjs +100 -98
  175. package/src/tui/palette.mts +176 -0
  176. package/src/tui/prefs.mjs +56 -63
  177. package/src/tui/prefs.mts +126 -0
  178. package/src/tui/reminder-app.mjs +191 -181
  179. package/src/tui/reminder-app.mts +228 -0
  180. package/src/tui/room-settings-app.mjs +275 -242
  181. package/src/tui/room-settings-app.mts +318 -0
  182. package/src/tui/session.mjs +30 -29
  183. package/src/tui/session.mts +51 -0
  184. package/src/tui/settings-app.mjs +151 -138
  185. package/src/tui/settings-app.mts +179 -0
  186. package/src/tui/ui-content.mjs +426 -421
  187. package/src/tui/ui-content.mts +499 -0
  188. package/src/tui/ui-help.mjs +58 -67
  189. package/src/tui/ui-help.mts +87 -0
  190. package/src/tui/ui-mentions.mjs +34 -36
  191. package/src/tui/ui-mentions.mts +60 -0
  192. package/src/tui/ui-overlay-view.mjs +259 -267
  193. package/src/tui/ui-overlay-view.mts +322 -0
  194. package/src/tui/ui-suggest-view.mjs +54 -57
  195. package/src/tui/ui-suggest-view.mts +78 -0
  196. package/src/tui/ui-suggest.mjs +164 -167
  197. package/src/tui/ui-suggest.mts +248 -0
  198. package/src/tui/ui-theme.mjs +52 -57
  199. package/src/tui/ui-theme.mts +98 -0
  200. package/src/tui/ui-view.mjs +267 -275
  201. package/src/tui/ui-view.mts +308 -0
  202. package/src/tui/ui.mjs +372 -359
  203. package/src/tui/ui.mts +440 -0
  204. package/src/tui/wire.mjs +215 -0
  205. package/src/tui/wire.mts +383 -0
  206. package/src/web/integration-rules-editor.ts +25 -2
@@ -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
+ }