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