@alexsun-top/opencode-chat-room 0.8.3 → 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 (156) hide show
  1. package/CHANGELOG.md +7 -1
  2. package/README.md +5 -5
  3. package/README.zh-CN.md +5 -5
  4. package/cli.ts +1 -1
  5. package/dist/cli.js +35 -16
  6. package/package.json +15 -9
  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-badge.js +1 -1
  15. package/server/assets/chat/admin.js +16 -10
  16. package/server/assets/chat/announcements.js +12 -2
  17. package/server/assets/chat/attachment-store.js +15 -6
  18. package/server/assets/chat/attachments.js +48 -33
  19. package/server/assets/chat/chat-background.js +8 -14
  20. package/server/assets/chat/clipboard.js +2 -1
  21. package/server/assets/chat/confirm-dialog.js +3 -3
  22. package/server/assets/chat/copy-action.js +1 -2
  23. package/server/assets/chat/diagram-viewer.js +7 -9
  24. package/server/assets/chat/doc-preview.js +19 -12
  25. package/server/assets/chat/editor.js +87 -61
  26. package/server/assets/chat/events.js +49 -34
  27. package/server/assets/chat/files.js +45 -13
  28. package/server/assets/chat/history-loader.js +5 -3
  29. package/server/assets/chat/image-zoom.js +21 -17
  30. package/server/assets/chat/install-prompt.js +10 -9
  31. package/server/assets/chat/integration-account-schema.js +5 -3
  32. package/server/assets/chat/integration-editor.js +2 -2
  33. package/server/assets/chat/integration-values.js +17 -11
  34. package/server/assets/chat/integrations.js +138 -81
  35. package/server/assets/chat/join-requests.js +20 -13
  36. package/server/assets/chat/markdown-footnotes.js +8 -8
  37. package/server/assets/chat/markdown.js +48 -38
  38. package/server/assets/chat/members.js +48 -25
  39. package/server/assets/chat/mentions.js +39 -12
  40. package/server/assets/chat/message-cache.js +2 -2
  41. package/server/assets/chat/message-polling.js +40 -21
  42. package/server/assets/chat/message-window.js +10 -6
  43. package/server/assets/chat/messages.js +70 -46
  44. package/server/assets/chat/office-preview.js +3 -3
  45. package/server/assets/chat/palette.js +45 -29
  46. package/server/assets/chat/panels.js +19 -1
  47. package/server/assets/chat/pwa.js +5 -4
  48. package/server/assets/chat/questions.js +25 -22
  49. package/server/assets/chat/recall.js +39 -21
  50. package/server/assets/chat/receipts.js +9 -7
  51. package/server/assets/chat/reminders.js +25 -8
  52. package/server/assets/chat/replies.js +34 -15
  53. package/server/assets/chat/resource-format.js +2 -2
  54. package/server/assets/chat/room-list.js +2 -2
  55. package/server/assets/chat/room-watcher.js +8 -4
  56. package/server/assets/chat/rooms.js +51 -40
  57. package/server/assets/chat/search.js +8 -6
  58. package/server/assets/chat/settings.js +16 -14
  59. package/server/assets/chat/shiki.js +1 -1
  60. package/server/assets/chat/shortcuts.js +3 -3
  61. package/server/assets/chat/side-pane.js +4 -3
  62. package/server/assets/chat/state.js +65 -37
  63. package/server/assets/chat/system-events.js +6 -5
  64. package/server/assets/chat/system-messages.js +15 -15
  65. package/server/assets/chat/task-form.js +5 -5
  66. package/server/assets/chat/task-row.js +1 -1
  67. package/server/assets/chat/tasks.js +41 -28
  68. package/server/assets/chat/window-controls.js +3 -2
  69. package/server/assets/conversation.css +14 -2
  70. package/server/assets/doc-preview.css +6 -0
  71. package/server/assets/office/THIRD-PARTY-NOTICES.txt +23 -1
  72. package/server/assets/office/licenses/docx-preview-HEADER.txt +6 -0
  73. package/server/assets/office/licenses/echarts-LICENSE-d3.txt +27 -0
  74. package/server/assets/office/licenses/echarts-NOTICE.txt +5 -0
  75. package/server/assets/office/licenses/immediate-LICENSE.txt +20 -0
  76. package/server/assets/office/licenses/lie-LICENSE.txt +7 -0
  77. package/server/assets/office/licenses/pako-LICENSE.txt +21 -0
  78. package/server/assets/office/licenses/pako-ZLIB.txt +18 -0
  79. package/server/assets/office/licenses/setimmediate-LICENSE.txt +20 -0
  80. package/server/chat.html +1 -0
  81. package/server/sw.js +3 -1
  82. package/skills/chatroom/SKILL.md +9 -0
  83. package/src/README.md +1 -1
  84. package/src/agent/agent-receipts.ts +12 -4
  85. package/src/center/center-client.ts +1 -1
  86. package/src/state/pg-sql.mjs +157 -142
  87. package/src/state/pg-sql.mts +175 -0
  88. package/src/state/pg-worker.mjs +107 -99
  89. package/src/state/pg-worker.mts +166 -0
  90. package/src/state/store.ts +21 -7
  91. package/src/tui/app-types.mjs +1 -0
  92. package/src/tui/app-types.mts +175 -0
  93. package/src/tui/app.mjs +426 -423
  94. package/src/tui/app.mts +500 -0
  95. package/src/tui/config.mjs +166 -158
  96. package/src/tui/config.mts +247 -0
  97. package/src/tui/http.mjs +239 -206
  98. package/src/tui/http.mts +412 -0
  99. package/src/tui/i18n.mjs +17 -20
  100. package/src/tui/i18n.mts +42 -0
  101. package/src/tui/info-app.mjs +431 -427
  102. package/src/tui/info-app.mts +500 -0
  103. package/src/tui/info-content.mjs +205 -199
  104. package/src/tui/info-content.mts +341 -0
  105. package/src/tui/join-app.mjs +75 -64
  106. package/src/tui/join-app.mts +99 -0
  107. package/src/tui/locales/en.mjs +302 -320
  108. package/src/tui/locales/en.mts +349 -0
  109. package/src/tui/locales/zh-CN.mjs +302 -320
  110. package/src/tui/locales/zh-CN.mts +349 -0
  111. package/src/tui/main.mjs +219 -213
  112. package/src/tui/main.mts +255 -0
  113. package/src/tui/markdown.mjs +128 -272
  114. package/src/tui/markdown.mts +152 -0
  115. package/src/tui/mentions.mjs +181 -184
  116. package/src/tui/mentions.mts +251 -0
  117. package/src/tui/message-app.mjs +181 -173
  118. package/src/tui/message-app.mts +213 -0
  119. package/src/tui/message-view.mjs +90 -111
  120. package/src/tui/message-view.mts +151 -0
  121. package/src/tui/messages.mjs +55 -49
  122. package/src/tui/messages.mts +106 -0
  123. package/src/tui/palette-app.mjs +364 -344
  124. package/src/tui/palette-app.mts +388 -0
  125. package/src/tui/palette.mjs +100 -98
  126. package/src/tui/palette.mts +176 -0
  127. package/src/tui/prefs.mjs +56 -63
  128. package/src/tui/prefs.mts +126 -0
  129. package/src/tui/reminder-app.mjs +191 -181
  130. package/src/tui/reminder-app.mts +228 -0
  131. package/src/tui/room-settings-app.mjs +257 -242
  132. package/src/tui/room-settings-app.mts +301 -0
  133. package/src/tui/session.mjs +30 -29
  134. package/src/tui/session.mts +51 -0
  135. package/src/tui/settings-app.mjs +151 -138
  136. package/src/tui/settings-app.mts +179 -0
  137. package/src/tui/ui-content.mjs +426 -421
  138. package/src/tui/ui-content.mts +499 -0
  139. package/src/tui/ui-help.mjs +58 -67
  140. package/src/tui/ui-help.mts +87 -0
  141. package/src/tui/ui-mentions.mjs +34 -36
  142. package/src/tui/ui-mentions.mts +60 -0
  143. package/src/tui/ui-overlay-view.mjs +259 -267
  144. package/src/tui/ui-overlay-view.mts +322 -0
  145. package/src/tui/ui-suggest-view.mjs +54 -57
  146. package/src/tui/ui-suggest-view.mts +78 -0
  147. package/src/tui/ui-suggest.mjs +164 -167
  148. package/src/tui/ui-suggest.mts +248 -0
  149. package/src/tui/ui-theme.mjs +52 -57
  150. package/src/tui/ui-theme.mts +98 -0
  151. package/src/tui/ui-view.mjs +267 -275
  152. package/src/tui/ui-view.mts +308 -0
  153. package/src/tui/ui.mjs +372 -359
  154. package/src/tui/ui.mts +440 -0
  155. package/src/tui/wire.mjs +215 -0
  156. package/src/tui/wire.mts +383 -0
@@ -0,0 +1,388 @@
1
+ // Command palette controller attached to ChatApp. Kept in its own file so the
2
+ // terminal core stays under the project line budget.
3
+ import { buildCommands, filterCommands, nextScope, scopeSources, searchMessages } from "./palette.mjs";
4
+ import type { Command, SearchResult } from "./palette.mjs";
5
+ import { mergeMessages, oldestSeq } from "./messages.mjs";
6
+ import { t } from "./i18n.mjs";
7
+ import type { ChatApp } from "./app.mjs";
8
+ import type { CurrentRoom, PaletteItem, PaletteScope, PaletteState } from "./app-types.mjs";
9
+
10
+ const SEARCH_PAGES = 2;
11
+ const errorText = (error: unknown): string => (error instanceof Error ? error.message : String(error ?? "unknown error"));
12
+
13
+ // The methods this module attaches become visible on ChatApp for app.mts and
14
+ // the later attach migrations.
15
+ declare module "./app.mjs" {
16
+ interface ChatApp {
17
+ openPalette(options?: { slash?: boolean }): void;
18
+ closePalette(): void;
19
+ paletteCommands(): Command[];
20
+ paletteItems(): PaletteItem[] | Command[] | SearchResult[];
21
+ paletteScopeRooms(scope: PaletteScope): CurrentRoom[];
22
+ refreshPaletteResults(): void;
23
+ ensureSearchHistory(): Promise<void>;
24
+ fetchHistoryPage(roomID: string): Promise<void>;
25
+ movePalette(delta: number): void;
26
+ runPaletteSelection(): Promise<void>;
27
+ submitPalettePrompt(): Promise<void>;
28
+ runPaletteCommand(command: Command): Promise<void>;
29
+ activateResult(result: SearchResult): Promise<void>;
30
+ createRoomFromPrompt(): Promise<void>;
31
+ leaveCurrent(): Promise<void>;
32
+ setPaletteQuery(value: string): void;
33
+ setPalettePrompt(value: string): void;
34
+ togglePaletteScope(): void;
35
+ }
36
+ }
37
+
38
+ export function attachPalette(Class: { prototype: ChatApp }) {
39
+ Object.assign(Class.prototype, {
40
+ openPalette(this: ChatApp, { slash = false }: { slash?: boolean } = {}): void {
41
+ this.historyPages.clear();
42
+ this.state.help = false;
43
+ // Same rule as the web palette: only "/" means commands; empty input
44
+ // waits for a search query instead of showing a command wall.
45
+ this.state.palette = {
46
+ mode: slash ? "commands" : "search",
47
+ query: slash ? "/" : "",
48
+ scope: "room",
49
+ selected: 0,
50
+ results: [],
51
+ items: [],
52
+ confirm: null,
53
+ detail: null,
54
+ searching: false,
55
+ error: "",
56
+ commands: buildCommands({
57
+ rooms: this.state.rooms,
58
+ current: this.state.current,
59
+ joined: this.state.joined,
60
+ help: this.state.help,
61
+ }),
62
+ };
63
+ this.touch();
64
+ },
65
+
66
+ closePalette(this: ChatApp): void {
67
+ this.state.palette = null;
68
+ this.state.focus = "composer";
69
+ this.touch();
70
+ },
71
+
72
+ paletteCommands(this: ChatApp): Command[] {
73
+ const palette = this.state.palette;
74
+ const query = palette?.mode === "commands" ? String(palette.query ?? "").replace(/^\s*\//, "") : "";
75
+ const commands = palette?.commands ?? buildCommands({
76
+ rooms: this.state.rooms,
77
+ current: this.state.current,
78
+ joined: this.state.joined,
79
+ help: this.state.help,
80
+ });
81
+ return filterCommands(commands, query);
82
+ },
83
+
84
+ paletteItems(this: ChatApp): PaletteItem[] | Command[] | SearchResult[] {
85
+ const palette = this.state.palette;
86
+ if (!palette) return [];
87
+ if (palette.mode === "search") return palette.results;
88
+ if (palette.mode === "commands") return this.paletteCommands();
89
+ if (palette.mode === "info") return palette.items ?? [];
90
+ return [];
91
+ },
92
+
93
+ paletteScopeRooms(this: ChatApp, scope: PaletteScope): CurrentRoom[] {
94
+ if (scope === "all") return this.state.rooms.filter((room) => room.joined);
95
+ return this.state.current && this.state.joined ? [this.state.current] : [];
96
+ },
97
+
98
+ refreshPaletteResults(this: ChatApp): void {
99
+ const palette = this.state.palette;
100
+ if (!palette || palette.mode !== "search") return;
101
+ palette.results = searchMessages(String(palette.query ?? ""), scopeSources({
102
+ rooms: this.state.rooms,
103
+ current: this.state.current,
104
+ scope: palette.scope,
105
+ cache: this.cache,
106
+ }));
107
+ palette.selected = Math.min(palette.selected, Math.max(0, palette.results.length - 1));
108
+ this.touch();
109
+ },
110
+
111
+ async ensureSearchHistory(this: ChatApp): Promise<void> {
112
+ const palette = this.state.palette;
113
+ if (!palette || palette.mode !== "search" || typeof this.api.history !== "function") return;
114
+ const jobs = this.paletteScopeRooms(palette.scope ?? "room").filter((room) =>
115
+ (this.historyPages.get(room.id) ?? 0) < SEARCH_PAGES
116
+ && this.historyMore.get(room.id) !== false
117
+ && !this.historyInflight.has(room.id));
118
+ if (!jobs.length) return;
119
+ palette.searching = true;
120
+ this.touch();
121
+ for (const room of jobs) {
122
+ this.historyInflight.add(room.id);
123
+ try {
124
+ await this.fetchHistoryPage(room.id);
125
+ // A failed page must not consume the room's search budget.
126
+ this.historyPages.set(room.id, (this.historyPages.get(room.id) ?? 0) + 1);
127
+ } catch (error) {
128
+ if (this.state.palette === palette) palette.error = t("palette.historyError", { error: errorText(error) });
129
+ } finally {
130
+ this.historyInflight.delete(room.id);
131
+ }
132
+ if (this.state.palette === palette) this.refreshPaletteResults();
133
+ }
134
+ if (this.state.palette === palette) {
135
+ palette.searching = this.historyInflight.size > 0;
136
+ this.refreshPaletteResults();
137
+ }
138
+ },
139
+
140
+ async fetchHistoryPage(this: ChatApp, roomID: string): Promise<void> {
141
+ const beforeSeq = this.historyCursor.get(roomID);
142
+ const result = await this.api.history(roomID, this.sessionID, beforeSeq);
143
+ const messages = result?.messages ?? [];
144
+ mergeMessages(this.historyList(roomID), messages);
145
+ this.historyMore.set(roomID, result?.hasMore === true);
146
+ const next = result?.nextBeforeSeq ?? oldestSeq(this.historyList(roomID));
147
+ if (typeof next === "number" && Number.isFinite(next)) this.historyCursor.set(roomID, next);
148
+ },
149
+
150
+ movePalette(this: ChatApp, delta: number): void {
151
+ const items = this.paletteItems();
152
+ if (!items.length) return;
153
+ const palette = this.state.palette;
154
+ if (!palette) return;
155
+ palette.selected = Math.min(items.length - 1, Math.max(0, palette.selected + delta));
156
+ this.touch();
157
+ },
158
+
159
+ async runPaletteSelection(this: ChatApp): Promise<void> {
160
+ const palette = this.state.palette;
161
+ if (!palette) return;
162
+ if (palette.mode === "prompt") {
163
+ await this.submitPalettePrompt();
164
+ return;
165
+ }
166
+ if (palette.mode === "info") {
167
+ await this.runInfoSelection();
168
+ return;
169
+ }
170
+ if (palette.mode === "confirm") {
171
+ await this.runConfirm();
172
+ return;
173
+ }
174
+ if (palette.mode === "detail") return;
175
+ if (palette.mode === "commands") {
176
+ const command = this.paletteCommands()[palette.selected];
177
+ if (command) await this.runPaletteCommand(command);
178
+ return;
179
+ }
180
+ const result = palette.results[palette.selected];
181
+ if (result) await this.activateResult(result);
182
+ },
183
+
184
+ async submitPalettePrompt(this: ChatApp): Promise<void> {
185
+ const palette = this.state.palette;
186
+ const prompt = palette?.prompt;
187
+ if (!palette || !prompt) return;
188
+ const value = prompt.value ?? "";
189
+ if (prompt.action && prompt.action !== "new-room") {
190
+ await this.submitCommandPrompt(prompt, value);
191
+ return;
192
+ }
193
+ await this.createRoomFromPrompt();
194
+ },
195
+
196
+ async runPaletteCommand(this: ChatApp, command: Command): Promise<void> {
197
+ const palette = this.state.palette;
198
+ if (!palette) return;
199
+ switch (command.id) {
200
+ case "refresh":
201
+ this.closePalette();
202
+ await this.refreshRooms();
203
+ return;
204
+ case "new-room":
205
+ palette.mode = "prompt";
206
+ palette.prompt = { label: t("palette.newRoomPrompt"), value: "", action: "new-room", data: {} };
207
+ palette.selected = 0;
208
+ palette.error = "";
209
+ this.touch();
210
+ return;
211
+ case "toggle-help":
212
+ this.state.help = !this.state.help;
213
+ this.state.palette = null;
214
+ this.state.focus = "rooms";
215
+ this.touch();
216
+ return;
217
+ case "leave-room":
218
+ this.closePalette();
219
+ await this.leaveCurrent();
220
+ return;
221
+ case "search-room":
222
+ case "search-all":
223
+ palette.mode = "search";
224
+ palette.scope = command.id === "search-all" ? "all" : "room";
225
+ palette.query = "";
226
+ palette.results = [];
227
+ palette.selected = 0;
228
+ palette.error = "";
229
+ this.touch();
230
+ void this.ensureSearchHistory();
231
+ return;
232
+ case "members":
233
+ await this.openMembers();
234
+ return;
235
+ case "join-requests":
236
+ await this.openJoinRequests();
237
+ return;
238
+ case "announcements":
239
+ await this.openAnnouncements();
240
+ return;
241
+ case "questions":
242
+ await this.openQuestions();
243
+ return;
244
+ case "files":
245
+ await this.openFiles();
246
+ return;
247
+ case "reminders":
248
+ await this.openReminders();
249
+ return;
250
+ case "settings":
251
+ this.openSettingsEditor();
252
+ return;
253
+ case "room-settings":
254
+ await this.openRoomSettings();
255
+ return;
256
+ case "delete-room":
257
+ await this.openRoomSettings();
258
+ this.askDeleteRoom();
259
+ return;
260
+ case "recall-message":
261
+ this.startMessageRecall();
262
+ return;
263
+ case "reveal-message":
264
+ this.startMessageReveal();
265
+ return;
266
+ case "reply-message":
267
+ this.startMessageReply();
268
+ return;
269
+ case "ask-question":
270
+ this.startAskQuestion();
271
+ return;
272
+ case "quit":
273
+ this.closePalette();
274
+ this.quit();
275
+ return;
276
+ default:
277
+ if (command.roomId) {
278
+ const index = this.state.rooms.findIndex((room) => room.id === command.roomId);
279
+ this.closePalette();
280
+ if (index >= 0) await this.openRoom(index);
281
+ }
282
+ }
283
+ },
284
+
285
+ async activateResult(this: ChatApp, result: SearchResult): Promise<void> {
286
+ const index = this.state.rooms.findIndex((room) => room.id === result.roomId);
287
+ this.closePalette();
288
+ if (index >= 0 && this.state.current?.id !== result.roomId) await this.openRoom(index);
289
+ this.state.highlight = result.message.id;
290
+ this.state.focus = "composer";
291
+ this.touch();
292
+ },
293
+
294
+ async createRoomFromPrompt(this: ChatApp): Promise<void> {
295
+ const palette = this.state.palette;
296
+ if (!palette) return;
297
+ const name = String(palette.prompt?.value ?? "").trim();
298
+ if (!name) {
299
+ palette.error = t("palette.enterRoomName");
300
+ this.touch();
301
+ return;
302
+ }
303
+ palette.searching = true;
304
+ palette.error = "";
305
+ this.touch();
306
+ try {
307
+ const created = await this.api.createRoom(name, this.sessionID, this.participant);
308
+ const roomID = created?.id;
309
+ await this.refreshRooms(true);
310
+ const index = this.state.rooms.findIndex((room) => room.id === roomID);
311
+ this.closePalette();
312
+ if (index >= 0) await this.openRoom(index);
313
+ else this.setStatus(t("palette.created", { name }), "success");
314
+ } catch (error) {
315
+ const current = this.state.palette;
316
+ if (current) {
317
+ current.searching = false;
318
+ current.error = errorText(error);
319
+ this.touch();
320
+ }
321
+ }
322
+ },
323
+
324
+ async leaveCurrent(this: ChatApp): Promise<void> {
325
+ const room = this.state.current;
326
+ if (!room || !this.state.joined) {
327
+ this.setStatus(t("palette.notJoined"), "error");
328
+ return;
329
+ }
330
+ try {
331
+ await this.api.leave(room.id, this.sessionID);
332
+ this.stopPolling();
333
+ this.state.joined = false;
334
+ const summary = this.state.rooms.find((item) => item.id === room.id);
335
+ if (summary) summary.joined = false;
336
+ await this.refreshRooms(true);
337
+ this.setStatus(t("palette.left", { name: room.name }), "success");
338
+ } catch (error) {
339
+ this.setStatus(t("palette.leaveFailed", { error: errorText(error) }), "error");
340
+ }
341
+ },
342
+
343
+ setPaletteQuery(this: ChatApp, value: string): void {
344
+ const palette = this.state.palette;
345
+ if (!palette || (palette.mode !== "commands" && palette.mode !== "search")) return;
346
+ const query = String(value ?? "");
347
+ palette.error = "";
348
+ if (!query.trim()) {
349
+ palette.mode = "search";
350
+ palette.query = "";
351
+ palette.results = [];
352
+ palette.selected = 0;
353
+ palette.searching = false;
354
+ this.touch();
355
+ return;
356
+ }
357
+ if (query.trimStart().startsWith("/")) {
358
+ palette.mode = "commands";
359
+ palette.query = query;
360
+ palette.results = [];
361
+ palette.searching = false;
362
+ palette.selected = 0;
363
+ this.touch();
364
+ return;
365
+ }
366
+ palette.mode = "search";
367
+ palette.query = query;
368
+ palette.selected = 0;
369
+ this.refreshPaletteResults();
370
+ void this.ensureSearchHistory();
371
+ },
372
+
373
+ setPalettePrompt(this: ChatApp, value: string): void {
374
+ const palette = this.state.palette;
375
+ if (palette?.prompt) palette.prompt.value = String(value ?? "");
376
+ },
377
+
378
+ togglePaletteScope(this: ChatApp): void {
379
+ const palette = this.state.palette;
380
+ if (!palette || palette.mode !== "search") return;
381
+ palette.scope = nextScope(palette.scope ?? "room");
382
+ palette.selected = 0;
383
+ palette.error = "";
384
+ this.refreshPaletteResults();
385
+ void this.ensureSearchHistory();
386
+ },
387
+ });
388
+ }
@@ -1,122 +1,124 @@
1
1
  // Pure command palette logic: command lists, scoped client-side search,
2
2
  // ranking and snippets. No I/O lives here so it is easy to test.
3
3
  import { t } from "./i18n.mjs";
4
-
5
4
  const SEARCH_LIMIT = 50;
6
-
7
5
  /** Match the visible label plus optional search aliases such as exit/quit. */
8
6
  export function filterCommands(commands, query) {
9
- const normalize = (value) => String(value ?? "").trim().toLowerCase()
10
- .replace(/^[/\\]+/, "").replace(/[-_]+/g, " ");
11
- const needle = normalize(query);
12
- if (!needle) return commands.slice();
13
- return commands.filter((command) => {
14
- const haystack = normalize([command.label, ...(command.keywords ?? [])].join(" "));
15
- return haystack.includes(needle);
16
- });
7
+ const normalize = (value) => String(value ?? "").trim().toLowerCase()
8
+ .replace(/^[/\\]+/, "").replace(/[-_]+/g, " ");
9
+ const needle = normalize(query);
10
+ if (!needle)
11
+ return commands.slice();
12
+ return commands.filter((command) => {
13
+ const haystack = normalize([command.label, ...(command.keywords ?? [])].join(" "));
14
+ return haystack.includes(needle);
15
+ });
17
16
  }
18
-
19
17
  /** Commands shown when the palette query is empty. */
20
18
  export function buildCommands({ rooms = [], current = null, joined = false, help = false } = {}) {
21
- const commands = [
22
- { id: "refresh", label: t("command.refresh") },
23
- { id: "new-room", label: t("command.newRoom") },
24
- ];
25
- for (const room of rooms) {
26
- if (!room.joined) commands.push({ id: `join:${room.id}`, label: t("command.join", { name: room.name }), roomId: room.id });
27
- }
28
- if (current && joined) commands.push({ id: "leave-room", label: t("command.leave", { name: current.name }) });
29
- commands.push({ id: "toggle-help", label: help ? t("command.hideHelp") : t("command.showHelp") });
30
- for (const room of rooms) {
31
- if (room.joined) commands.push({ id: `switch:${room.id}`, label: t("command.switch", { name: room.name }), roomId: room.id });
32
- }
33
- commands.push({ id: "search-room", label: t("command.searchRoom") });
34
- commands.push({ id: "search-all", label: t("command.searchAll") });
35
- commands.push({ id: "members", label: t("command.members") });
36
- commands.push({ id: "join-requests", label: t("command.joinRequests") });
37
- commands.push({ id: "announcements", label: t("command.announcements") });
38
- commands.push({ id: "questions", label: t("command.questions") });
39
- commands.push({ id: "files", label: t("command.files") });
40
- commands.push({ id: "reminders", label: t("command.reminders") });
41
- commands.push({ id: "settings", label: t("command.settings") });
42
- commands.push({ id: "room-settings", label: t("command.roomSettings") });
43
- commands.push({ id: "delete-room", label: t("command.deleteRoom") });
44
- commands.push({ id: "recall-message", label: t("command.recall") });
45
- commands.push({ id: "reveal-message", label: t("command.reveal") });
46
- commands.push({ id: "reply-message", label: t("command.reply") });
47
- commands.push({ id: "ask-question", label: t("command.ask") });
48
- commands.push({ id: "quit", label: t("command.quit"), keywords: ["exit", "quit", "q"] });
49
- return commands;
19
+ const commands = [
20
+ { id: "refresh", label: t("command.refresh") },
21
+ { id: "new-room", label: t("command.newRoom") },
22
+ ];
23
+ for (const room of rooms) {
24
+ if (!room.joined)
25
+ commands.push({ id: `join:${room.id}`, label: t("command.join", { name: room.name }), roomId: room.id });
26
+ }
27
+ if (current && joined)
28
+ commands.push({ id: "leave-room", label: t("command.leave", { name: current.name }) });
29
+ commands.push({ id: "toggle-help", label: help ? t("command.hideHelp") : t("command.showHelp") });
30
+ for (const room of rooms) {
31
+ if (room.joined)
32
+ commands.push({ id: `switch:${room.id}`, label: t("command.switch", { name: room.name }), roomId: room.id });
33
+ }
34
+ commands.push({ id: "search-room", label: t("command.searchRoom") });
35
+ commands.push({ id: "search-all", label: t("command.searchAll") });
36
+ commands.push({ id: "members", label: t("command.members") });
37
+ commands.push({ id: "join-requests", label: t("command.joinRequests") });
38
+ commands.push({ id: "announcements", label: t("command.announcements") });
39
+ commands.push({ id: "questions", label: t("command.questions") });
40
+ commands.push({ id: "files", label: t("command.files") });
41
+ commands.push({ id: "reminders", label: t("command.reminders") });
42
+ commands.push({ id: "settings", label: t("command.settings") });
43
+ commands.push({ id: "room-settings", label: t("command.roomSettings") });
44
+ commands.push({ id: "delete-room", label: t("command.deleteRoom") });
45
+ commands.push({ id: "recall-message", label: t("command.recall") });
46
+ commands.push({ id: "reveal-message", label: t("command.reveal") });
47
+ commands.push({ id: "reply-message", label: t("command.reply") });
48
+ commands.push({ id: "ask-question", label: t("command.ask") });
49
+ commands.push({ id: "quit", label: t("command.quit"), keywords: ["exit", "quit", "q"] });
50
+ return commands;
50
51
  }
51
-
52
52
  export function nextScope(scope) {
53
- return scope === "all" ? "room" : "all";
53
+ return scope === "all" ? "room" : "all";
54
54
  }
55
-
56
55
  /** Pick the message sources for a scope from the local cache. */
57
56
  export function scopeSources({ rooms = [], current = null, scope = "room", cache = new Map() } = {}) {
58
- const messagesOf = (id) => {
59
- const value = cache instanceof Map ? cache.get(id) : cache?.[id];
60
- return Array.isArray(value) ? value : [];
61
- };
62
- if (scope !== "all") {
63
- return current ? [{ roomId: current.id, roomName: current.name, messages: messagesOf(current.id) }] : [];
64
- }
65
- return rooms
66
- .filter((room) => room.joined)
67
- .map((room) => ({ roomId: room.id, roomName: room.name, messages: messagesOf(room.id) }));
57
+ const messagesOf = (id) => {
58
+ const value = cache instanceof Map ? cache.get(id) : cache[id];
59
+ return Array.isArray(value) ? value : [];
60
+ };
61
+ if (scope !== "all") {
62
+ return current ? [{ roomId: current.id, roomName: current.name, messages: messagesOf(current.id) }] : [];
63
+ }
64
+ return rooms
65
+ .filter((room) => room.joined)
66
+ .map((room) => ({ roomId: room.id, roomName: room.name, messages: messagesOf(room.id) }));
68
67
  }
69
-
70
68
  function stamp(message) {
71
- const value = message?.createdAt;
72
- if (typeof value === "number" && Number.isFinite(value)) return value;
73
- const parsed = Date.parse(String(value ?? ""));
74
- return Number.isFinite(parsed) ? parsed : 0;
69
+ const value = message?.createdAt;
70
+ if (typeof value === "number" && Number.isFinite(value))
71
+ return value;
72
+ const parsed = Date.parse(String(value ?? ""));
73
+ return Number.isFinite(parsed) ? parsed : 0;
75
74
  }
76
-
77
75
  function scoreOf(text, needle, index) {
78
- let score = 0;
79
- let from = 0;
80
- let hits = 0;
81
- while ((from = text.indexOf(needle, from)) !== -1) {
82
- hits += 1;
83
- from += needle.length;
84
- }
85
- score += hits * 10;
86
- if (index === 0) score += 5;
87
- else if (/[\s.,!?;:]/.test(text[index - 1])) score += 3;
88
- return score;
76
+ let score = 0;
77
+ let from = 0;
78
+ let hits = 0;
79
+ while ((from = text.indexOf(needle, from)) !== -1) {
80
+ hits += 1;
81
+ from += needle.length;
82
+ }
83
+ score += hits * 10;
84
+ if (index === 0)
85
+ score += 5;
86
+ else if (/[\s.,!?;:]/.test(text[index - 1] ?? ""))
87
+ score += 3;
88
+ return score;
89
89
  }
90
-
91
90
  export function snippetOf(text, index, length, radius = 24) {
92
- const plain = String(text).replace(/\s+/g, " ").trim();
93
- const start = Math.max(0, index - radius);
94
- const end = Math.min(plain.length, index + length + radius);
95
- return `${start > 0 ? "…" : ""}${plain.slice(start, end)}${end < plain.length ? "…" : ""}`;
91
+ const plain = String(text).replace(/\s+/g, " ").trim();
92
+ const start = Math.max(0, index - radius);
93
+ const end = Math.min(plain.length, index + length + radius);
94
+ return `${start > 0 ? "…" : ""}${plain.slice(start, end)}${end < plain.length ? "…" : ""}`;
96
95
  }
97
-
98
96
  /** Search loaded messages. Recalled messages are never searchable. */
99
97
  export function searchMessages(query, sources, { limit = SEARCH_LIMIT } = {}) {
100
- const needle = String(query ?? "").trim().toLowerCase();
101
- if (!needle) return [];
102
- const results = [];
103
- for (const source of sources) {
104
- for (const message of source.messages ?? []) {
105
- if (!message || message.recalledAt) continue;
106
- const text = String(message.text ?? "").replace(/\s+/g, " ").trim();
107
- if (!text || text === "[recalled]") continue;
108
- const lower = text.toLowerCase();
109
- const index = lower.indexOf(needle);
110
- if (index < 0) continue;
111
- results.push({
112
- roomId: source.roomId,
113
- roomName: source.roomName,
114
- message,
115
- snippet: snippetOf(text, index, needle.length),
116
- score: scoreOf(lower, needle, index),
117
- });
98
+ const needle = String(query ?? "").trim().toLowerCase();
99
+ if (!needle)
100
+ return [];
101
+ const results = [];
102
+ for (const source of sources) {
103
+ for (const message of source.messages ?? []) {
104
+ if (!message || message.recalledAt)
105
+ continue;
106
+ const text = String(message.text ?? "").replace(/\s+/g, " ").trim();
107
+ if (!text || text === "[recalled]")
108
+ continue;
109
+ const lower = text.toLowerCase();
110
+ const index = lower.indexOf(needle);
111
+ if (index < 0)
112
+ continue;
113
+ results.push({
114
+ roomId: source.roomId,
115
+ roomName: source.roomName,
116
+ message,
117
+ snippet: snippetOf(text, index, needle.length),
118
+ score: scoreOf(lower, needle, index),
119
+ });
120
+ }
118
121
  }
119
- }
120
- results.sort((a, b) => b.score - a.score || stamp(b.message) - stamp(a.message));
121
- return results.slice(0, limit);
122
- }
122
+ results.sort((a, b) => b.score - a.score || stamp(b.message) - stamp(a.message));
123
+ return results.slice(0, limit);
124
+ }