@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
@@ -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
+ }