@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,251 @@
1
+ // Mention helpers shared by the composer suggestions and outgoing messages.
2
+ // Display names stay visible while typing; the wire format is applied on send.
3
+ import { t } from "./i18n.mjs";
4
+
5
+ export interface Participant {
6
+ id?: string;
7
+ name?: string;
8
+ }
9
+
10
+ export interface MentionItem {
11
+ name: string;
12
+ insert: string;
13
+ label: string;
14
+ }
15
+
16
+ export interface MentionTrigger {
17
+ start: number;
18
+ query: string;
19
+ cursor: number;
20
+ }
21
+
22
+ export interface MentionRange {
23
+ start: number;
24
+ end: number;
25
+ }
26
+
27
+ interface Segment {
28
+ text: string;
29
+ code: boolean;
30
+ start: number;
31
+ end: number;
32
+ }
33
+
34
+ const escapeRegExp = (value: string) => String(value).replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
35
+
36
+ export function mentionNames(participants: readonly Participant[] = []): string[] {
37
+ const names: string[] = [];
38
+ const seen = new Set(["all"]);
39
+ const list = Array.isArray(participants) ? participants : [];
40
+ for (const participant of list) {
41
+ const name = String(participant?.name ?? participant?.id ?? "").trim();
42
+ if (!name || seen.has(name.toLowerCase())) continue;
43
+ seen.add(name.toLowerCase());
44
+ names.push(name);
45
+ }
46
+ return names;
47
+ }
48
+
49
+ export function mentionItems(participants: readonly Participant[], query = ""): MentionItem[] {
50
+ const needle = String(query ?? "").toLowerCase();
51
+ const items: MentionItem[] = mentionNames(participants).map((name) => ({
52
+ name,
53
+ insert: `@${name}`,
54
+ label: name,
55
+ }));
56
+ items.push({ name: "all", insert: "@all", label: t("mentions.everyone") });
57
+ return items.filter((item) => {
58
+ const haystack = `${item.label} ${item.name}`.toLowerCase();
59
+ return !needle || haystack.includes(needle);
60
+ });
61
+ }
62
+
63
+ /** Locate the @token at the end of the draft, outside email-like tokens. */
64
+ export function mentionTrigger(text: string): MentionTrigger | null {
65
+ const source = String(text ?? "");
66
+ const match = source.match(/(^|[^\p{L}\p{N}_@])@([^\n@]{0,64})$/u);
67
+ if (!match) return null;
68
+ const query = match[2] ?? "";
69
+ return { start: source.length - query.length - 1, query, cursor: source.length };
70
+ }
71
+
72
+ export function insertMention(draft: string, trigger: MentionTrigger | null, insert: string): string {
73
+ const source = String(draft ?? "");
74
+ const at = trigger ?? mentionTrigger(source);
75
+ if (!at) return source;
76
+ const before = source.slice(0, at.start);
77
+ const after = source.slice(at.cursor ?? source.length);
78
+ return `${before}${insert} ${after}`;
79
+ }
80
+
81
+ /** Find a closing backtick run of exactly `length`, never a longer run. */
82
+ function closingBackticks(source: string, from: number, end: number, length: number): number {
83
+ let i = from;
84
+ while (i < end) {
85
+ if (source.charAt(i) !== "`") {
86
+ i += 1;
87
+ continue;
88
+ }
89
+ let run = 0;
90
+ while (i + run < end && source.charAt(i + run) === "`") run += 1;
91
+ if (run === length) return i;
92
+ i += run;
93
+ }
94
+ return -1;
95
+ }
96
+
97
+ /** Split Markdown prose from fenced/indented code and inline code spans. */
98
+ function codeSegments(source: string): Segment[] {
99
+ const segments: Segment[] = [];
100
+ let proseStart = 0;
101
+ let lineStart = 0;
102
+ const push = (start: number, end: number, code: boolean) => {
103
+ if (end > start) segments.push({ text: source.slice(start, end), code, start, end });
104
+ };
105
+ while (lineStart < source.length) {
106
+ const lineEnd = source.indexOf("\n", lineStart);
107
+ const end = lineEnd < 0 ? source.length : lineEnd;
108
+ const line = source.slice(lineStart, end).replace(/\r$/, "");
109
+ const opener = /^([ \t]*)(`{3,}|~{3,})/.exec(line);
110
+ if (opener) {
111
+ if (lineStart > proseStart) splitInlineCode(source, proseStart, lineStart, segments);
112
+ const fence = opener[2] ?? "";
113
+ const indent = (opener[1] ?? "").length;
114
+ let scan = lineEnd < 0 ? source.length : lineEnd + 1;
115
+ let closed = source.length;
116
+ while (scan < source.length) {
117
+ const nextEnd = source.indexOf("\n", scan);
118
+ const stop = nextEnd < 0 ? source.length : nextEnd;
119
+ const close = /^([ \t]*)(`{3,}|~{3,})[ \t]*$/.exec(source.slice(scan, stop).replace(/\r$/, ""));
120
+ const closeFence = close?.[2] ?? "";
121
+ if (close && closeFence.charAt(0) === fence.charAt(0) && closeFence.length >= fence.length
122
+ && (close[1] ?? "").length <= indent + 3) {
123
+ closed = nextEnd < 0 ? source.length : nextEnd + 1;
124
+ break;
125
+ }
126
+ scan = nextEnd < 0 ? source.length : nextEnd + 1;
127
+ }
128
+ push(lineStart, closed, true);
129
+ proseStart = closed;
130
+ lineStart = closed;
131
+ continue;
132
+ }
133
+ // CommonMark indented code is also literal. Treat four spaces/tab as code
134
+ // rather than risking a mention rewrite inside it.
135
+ if (/^(?: {4,}|\t)/.test(line)) {
136
+ if (lineStart > proseStart) splitInlineCode(source, proseStart, lineStart, segments);
137
+ let scan = lineStart;
138
+ let closed = source.length;
139
+ while (scan < source.length) {
140
+ const nextEnd = source.indexOf("\n", scan);
141
+ const stop = nextEnd < 0 ? source.length : nextEnd;
142
+ const current = source.slice(scan, stop).replace(/\r$/, "");
143
+ if (current.trim() && !/^(?: {4,}|\t)/.test(current)) {
144
+ closed = scan;
145
+ break;
146
+ }
147
+ scan = nextEnd < 0 ? source.length : nextEnd + 1;
148
+ }
149
+ push(lineStart, closed, true);
150
+ proseStart = closed;
151
+ lineStart = closed;
152
+ continue;
153
+ }
154
+ lineStart = lineEnd < 0 ? source.length : lineEnd + 1;
155
+ }
156
+ splitInlineCode(source, proseStart, source.length, segments);
157
+ return segments;
158
+ }
159
+
160
+ function splitInlineCode(source: string, start: number, end: number, segments: Segment[]): void {
161
+ let prose = start;
162
+ let i = start;
163
+ while (i < end) {
164
+ if (source.charAt(i) !== "`") {
165
+ i += 1;
166
+ continue;
167
+ }
168
+ let run = 1;
169
+ while (i + run < end && source.charAt(i + run) === "`") run += 1;
170
+ const close = closingBackticks(source, i + run, end, run);
171
+ if (close < 0) {
172
+ i += run;
173
+ continue;
174
+ }
175
+ if (i > prose) segments.push({ text: source.slice(prose, i), code: false, start: prose, end: i });
176
+ segments.push({ text: source.slice(i, close + run), code: true, start: i, end: close + run });
177
+ prose = close + run;
178
+ i = prose;
179
+ }
180
+ if (end > prose) segments.push({ text: source.slice(prose, end), code: false, start: prose, end });
181
+ }
182
+
183
+ function convertPlain(segment: string, names: readonly string[]): string {
184
+ if (!segment) return segment;
185
+ const candidates = ["all", ...names].sort((a, b) => b.length - a.length);
186
+ const pattern = new RegExp(
187
+ `(?<![\\p{L}\\p{N}_.@])@(${candidates.map(escapeRegExp).join("|")})(?![\\p{L}\\p{N}_])`,
188
+ "gu",
189
+ );
190
+ return segment.replace(pattern, (_match: string, name: string) => `<at>@${name}</at>`);
191
+ }
192
+
193
+ function convertProse(source: string, names: readonly string[]): string {
194
+ const tag = /<at>[\s\S]*?<\/at>/giu;
195
+ let output = "";
196
+ let last = 0;
197
+ for (const match of source.matchAll(tag)) {
198
+ output += convertPlain(source.slice(last, match.index), names);
199
+ output += match[0];
200
+ last = match.index + match[0].length;
201
+ }
202
+ return output + convertPlain(source.slice(last), names);
203
+ }
204
+
205
+ /** Convert known @names and @all to <at> tags, preserving code and existing tags. */
206
+ export function canonicalizeMentions(text: string, participants: readonly Participant[] = []): string {
207
+ const source = String(text ?? "");
208
+ const names = mentionNames(participants);
209
+ return codeSegments(source)
210
+ .map((segment) => (segment.code ? segment.text : convertProse(segment.text, names)))
211
+ .join("");
212
+ }
213
+
214
+ /** Character ranges for visible mention chips, excluding email-like tokens. */
215
+ export function mentionRanges(text: string, participants: readonly Participant[] = []): MentionRange[] {
216
+ const source = String(text ?? "");
217
+ const ranges: MentionRange[] = [];
218
+ const candidates = ["all", ...mentionNames(participants)].sort((a, b) => b.length - a.length);
219
+ const pattern = new RegExp(
220
+ `(?<![\\p{L}\\p{N}_.@])@(${candidates.map(escapeRegExp).join("|")})(?![\\p{L}\\p{N}_])`,
221
+ "gu",
222
+ );
223
+ for (const segment of codeSegments(source)) {
224
+ if (segment.code) continue;
225
+ const tag = /<at>([\s\S]*?)<\/at>/giu;
226
+ let last = 0;
227
+ const plain: [number, number][] = [];
228
+ for (const match of segment.text.matchAll(tag)) {
229
+ const inner = match[1] ?? "";
230
+ const trimmed = inner.trim();
231
+ const at = trimmed.indexOf("@");
232
+ if (at >= 0) {
233
+ const innerStart = segment.start + match.index + 4 + (inner.length - inner.trimStart().length);
234
+ ranges.push({ start: innerStart + at, end: innerStart + trimmed.length });
235
+ }
236
+ plain.push([last, match.index]);
237
+ last = match.index + match[0].length;
238
+ }
239
+ plain.push([last, segment.text.length]);
240
+ for (const [start, end] of plain) {
241
+ const piece = segment.text.slice(start, end);
242
+ for (const match of piece.matchAll(pattern)) {
243
+ ranges.push({
244
+ start: segment.start + start + match.index,
245
+ end: segment.start + start + match.index + match[0].length,
246
+ });
247
+ }
248
+ }
249
+ }
250
+ return ranges.sort((a, b) => a.start - b.start);
251
+ }
@@ -3,180 +3,188 @@
3
3
  import { mergeMessages } from "./messages.mjs";
4
4
  import { canonicalizeMentions } from "./mentions.mjs";
5
5
  import { t } from "./i18n.mjs";
6
-
7
6
  const errorText = (error) => (error instanceof Error ? error.message : String(error ?? "unknown error"));
8
-
9
7
  export function attachMessageActions(Class) {
10
- Object.assign(Class.prototype, {
11
- messageActionPalette(title, label, action, data = {}) {
12
- const palette = this.state.palette;
13
- if (!palette || !this.state.current || !this.state.joined) {
14
- this.state.palette = null;
15
- this.setStatus(t("info.joinFirst"), "error");
16
- return null;
17
- }
18
- palette.mode = "prompt";
19
- palette.roomId = this.state.current.id;
20
- palette.roomParticipants = [...(this.state.current.participants || [])];
21
- palette.title = title;
22
- palette.subtitle = `#${this.state.current.name}`;
23
- palette.error = "";
24
- palette.prompt = { label, value: "", action, data };
25
- palette.hint = t("palette.enterNextHint");
26
- this.touch();
27
- return palette;
28
- },
29
-
30
- startMessageRecall() {
31
- return this.messageActionPalette(t("message.recallTitle"), t("message.recallPrompt"), "message-recall-id");
32
- },
33
-
34
- startMessageReveal() {
35
- return this.messageActionPalette(t("message.revealTitle"), t("message.revealPrompt"), "message-reveal-id");
36
- },
37
-
38
- startMessageReply() {
39
- return this.messageActionPalette(t("message.replyTitle"), t("message.replyPrompt"), "message-reply-id");
40
- },
41
-
42
- startAskQuestion() {
43
- return this.messageActionPalette(t("message.askTitle"), t("message.askPrompt"), "message-ask");
44
- },
45
-
46
- async submitMessagePrompt(prompt, value) {
47
- const palette = this.state.palette;
48
- const roomId = palette?.roomId || this.state.current?.id;
49
- const participants = palette?.roomParticipants || this.state.current?.participants;
50
- if (!palette || palette.mode !== "prompt") return;
51
- if (!roomId) {
52
- palette.error = t("info.joinFirst");
53
- this.touch();
54
- return;
55
- }
56
- if (prompt.action === "message-ask") {
57
- const question = canonicalizeMentions(String(value ?? "").trim(), participants);
58
- if (!question) {
59
- palette.error = t("message.questionRequired");
60
- this.touch();
61
- return;
62
- }
63
- palette.searching = true;
64
- palette.error = "";
65
- this.touch();
66
- try {
67
- const result = await this.api.send(roomId, this.sessionID, question, { isQuestion: true });
68
- if (Array.isArray(result?.messages)) mergeMessages(this.state.messages, result.messages);
69
- else if (result?.id) mergeMessages(this.state.messages, [result]);
70
- this.state.connection = "online";
71
- this.closePalette();
72
- this.setStatus(t("message.questionSent"), "success");
73
- this.markRead();
74
- } catch (error) {
75
- if (this.state.palette === palette) {
76
- palette.searching = false;
77
- palette.error = errorText(error);
8
+ Object.assign(Class.prototype, {
9
+ messageActionPalette(title, label, action, data = {}) {
10
+ const palette = this.state.palette;
11
+ const current = this.state.current;
12
+ if (!palette || !current || !this.state.joined) {
13
+ this.state.palette = null;
14
+ this.setStatus(t("info.joinFirst"), "error");
15
+ return null;
16
+ }
17
+ palette.mode = "prompt";
18
+ palette.roomId = current.id;
19
+ palette.roomParticipants = [...(current.participants || [])];
20
+ palette.title = title;
21
+ palette.subtitle = `#${current.name}`;
22
+ palette.error = "";
23
+ palette.prompt = { label, value: "", action, data };
24
+ palette.hint = t("palette.enterNextHint");
78
25
  this.touch();
79
- }
80
- }
81
- return;
82
- }
83
- const id = String(value ?? "").trim();
84
- if (!id) {
85
- palette.error = t("message.idRequired");
86
- this.touch();
87
- return;
88
- }
89
- if (prompt.action === "message-recall-id" || prompt.action === "message-reveal-id") {
90
- const recall = prompt.action === "message-recall-id";
91
- palette.mode = "confirm";
92
- palette.previous = { mode: "prompt", hint: palette.hint };
93
- palette.confirm = {
94
- text: recall ? t("message.recallConfirm", { id }) : t("message.revealConfirm", { id }),
95
- action: recall ? "message-recall" : "message-reveal",
96
- data: { id },
97
- };
98
- palette.hint = t("palette.confirmHint");
99
- palette.error = "";
100
- this.touch();
101
- return;
102
- }
103
- if (prompt.action === "message-reply-id") {
104
- palette.prompt = {
105
- label: t("message.replyLabel", { id }),
106
- value: "",
107
- action: "message-reply-text",
108
- data: { id },
109
- };
110
- palette.hint = t("message.replyHint");
111
- palette.error = "";
112
- this.touch();
113
- return;
114
- }
115
- if (prompt.action === "message-reply-text") {
116
- const text = canonicalizeMentions(String(value ?? "").trim(), participants);
117
- if (!text) {
118
- palette.error = t("message.replyRequired");
119
- this.touch();
120
- return;
121
- }
122
- palette.searching = true;
123
- palette.error = "";
124
- this.touch();
125
- try {
126
- const result = await this.api.send(roomId, this.sessionID, text, { replyToMessageID: prompt.data.id });
127
- if (!this.isCurrent(roomId)) return;
128
- if (Array.isArray(result?.messages)) mergeMessages(this.state.messages, result.messages);
129
- else if (result?.id) mergeMessages(this.state.messages, [result]);
130
- this.state.connection = "online";
131
- this.closePalette();
132
- this.setStatus(t("message.replySent"), "success");
133
- this.markRead();
134
- } catch (error) {
135
- if (this.state.palette === palette) {
136
- palette.searching = false;
137
- palette.error = errorText(error);
26
+ return palette;
27
+ },
28
+ startMessageRecall() {
29
+ return this.messageActionPalette(t("message.recallTitle"), t("message.recallPrompt"), "message-recall-id");
30
+ },
31
+ startMessageReveal() {
32
+ return this.messageActionPalette(t("message.revealTitle"), t("message.revealPrompt"), "message-reveal-id");
33
+ },
34
+ startMessageReply() {
35
+ return this.messageActionPalette(t("message.replyTitle"), t("message.replyPrompt"), "message-reply-id");
36
+ },
37
+ startAskQuestion() {
38
+ return this.messageActionPalette(t("message.askTitle"), t("message.askPrompt"), "message-ask");
39
+ },
40
+ async submitMessagePrompt(prompt, value) {
41
+ const palette = this.state.palette;
42
+ const roomId = palette?.roomId || this.state.current?.id;
43
+ const participants = palette?.roomParticipants || this.state.current?.participants;
44
+ if (!palette || palette.mode !== "prompt")
45
+ return;
46
+ if (!roomId) {
47
+ palette.error = t("info.joinFirst");
48
+ this.touch();
49
+ return;
50
+ }
51
+ if (prompt.action === "message-ask") {
52
+ const question = canonicalizeMentions(String(value ?? "").trim(), participants);
53
+ if (!question) {
54
+ palette.error = t("message.questionRequired");
55
+ this.touch();
56
+ return;
57
+ }
58
+ palette.searching = true;
59
+ palette.error = "";
60
+ this.touch();
61
+ try {
62
+ const result = await this.api.send(roomId, this.sessionID, question, { isQuestion: true });
63
+ const messages = result?.messages;
64
+ if (Array.isArray(messages))
65
+ mergeMessages(this.state.messages, messages);
66
+ else if (result?.id)
67
+ mergeMessages(this.state.messages, [result]);
68
+ this.state.connection = "online";
69
+ this.closePalette();
70
+ this.setStatus(t("message.questionSent"), "success");
71
+ this.markRead();
72
+ }
73
+ catch (error) {
74
+ if (this.state.palette === palette) {
75
+ palette.searching = false;
76
+ palette.error = errorText(error);
77
+ this.touch();
78
+ }
79
+ }
80
+ return;
81
+ }
82
+ const id = String(value ?? "").trim();
83
+ if (!id) {
84
+ palette.error = t("message.idRequired");
85
+ this.touch();
86
+ return;
87
+ }
88
+ if (prompt.action === "message-recall-id" || prompt.action === "message-reveal-id") {
89
+ const recall = prompt.action === "message-recall-id";
90
+ palette.mode = "confirm";
91
+ palette.previous = { mode: "prompt", hint: palette.hint };
92
+ palette.confirm = {
93
+ text: recall ? t("message.recallConfirm", { id }) : t("message.revealConfirm", { id }),
94
+ action: recall ? "message-recall" : "message-reveal",
95
+ data: { id },
96
+ };
97
+ palette.hint = t("palette.confirmHint");
98
+ palette.error = "";
99
+ this.touch();
100
+ return;
101
+ }
102
+ if (prompt.action === "message-reply-id") {
103
+ palette.prompt = {
104
+ label: t("message.replyLabel", { id }),
105
+ value: "",
106
+ action: "message-reply-text",
107
+ data: { id },
108
+ };
109
+ palette.hint = t("message.replyHint");
110
+ palette.error = "";
111
+ this.touch();
112
+ return;
113
+ }
114
+ if (prompt.action === "message-reply-text") {
115
+ const text = canonicalizeMentions(String(value ?? "").trim(), participants);
116
+ if (!text) {
117
+ palette.error = t("message.replyRequired");
118
+ this.touch();
119
+ return;
120
+ }
121
+ palette.searching = true;
122
+ palette.error = "";
123
+ this.touch();
124
+ try {
125
+ const result = await this.api.send(roomId, this.sessionID, text, { replyToMessageID: prompt.data.id });
126
+ if (!this.isCurrent(roomId))
127
+ return;
128
+ const messages = result?.messages;
129
+ if (Array.isArray(messages))
130
+ mergeMessages(this.state.messages, messages);
131
+ else if (result?.id)
132
+ mergeMessages(this.state.messages, [result]);
133
+ this.state.connection = "online";
134
+ this.closePalette();
135
+ this.setStatus(t("message.replySent"), "success");
136
+ this.markRead();
137
+ }
138
+ catch (error) {
139
+ if (this.state.palette === palette) {
140
+ palette.searching = false;
141
+ palette.error = errorText(error);
142
+ this.touch();
143
+ }
144
+ }
145
+ }
146
+ },
147
+ async runMessageConfirm(confirm) {
148
+ const palette = this.state.palette;
149
+ const roomId = palette?.roomId || this.state.current?.id;
150
+ if (!palette || !roomId)
151
+ return;
152
+ const id = confirm.data.id;
153
+ if (!id)
154
+ return;
155
+ palette.searching = true;
156
+ palette.error = "";
138
157
  this.touch();
139
- }
140
- }
141
- }
142
- },
143
-
144
- async runMessageConfirm(confirm) {
145
- const palette = this.state.palette;
146
- const roomId = palette?.roomId || this.state.current?.id;
147
- if (!palette || !roomId) return;
148
- const id = confirm.data.id;
149
- palette.searching = true;
150
- palette.error = "";
151
- this.touch();
152
- try {
153
- if (confirm.action === "message-recall") {
154
- await this.api.recall(roomId, id, this.sessionID);
155
- const target = this.state.messages.find((message) => message.id === id);
156
- if (target) target.recalledAt = Date.now();
157
- this.closePalette();
158
- this.setStatus(t("message.recalledStatus", { id }), "success");
159
- return;
160
- }
161
- const revealed = await this.api.reveal(roomId, id, this.sessionID);
162
- palette.mode = "detail";
163
- palette.title = t("message.revealed");
164
- palette.confirm = null;
165
- palette.detail = {
166
- title: t("message.revealed"),
167
- markdown: String(revealed?.text ?? t("message.empty")),
168
- kind: "revealed-message",
169
- };
170
- palette.hint = t("palette.closeHint");
171
- palette.searching = false;
172
- this.touch();
173
- } catch (error) {
174
- if (this.state.palette === palette) {
175
- palette.searching = false;
176
- palette.error = errorText(error);
177
- this.touch();
178
- }
179
- }
180
- },
181
- });
182
- }
158
+ try {
159
+ if (confirm.action === "message-recall") {
160
+ await this.api.recall(roomId, id, this.sessionID);
161
+ const target = this.state.messages.find((message) => message.id === id);
162
+ if (target)
163
+ target.recalledAt = Date.now();
164
+ this.closePalette();
165
+ this.setStatus(t("message.recalledStatus", { id }), "success");
166
+ return;
167
+ }
168
+ const revealed = await this.api.reveal(roomId, id, this.sessionID);
169
+ palette.mode = "detail";
170
+ palette.title = t("message.revealed");
171
+ palette.confirm = null;
172
+ palette.detail = {
173
+ title: t("message.revealed"),
174
+ markdown: String(revealed?.text ?? t("message.empty")),
175
+ kind: "revealed-message",
176
+ };
177
+ palette.hint = t("palette.closeHint");
178
+ palette.searching = false;
179
+ this.touch();
180
+ }
181
+ catch (error) {
182
+ if (this.state.palette === palette) {
183
+ palette.searching = false;
184
+ palette.error = errorText(error);
185
+ this.touch();
186
+ }
187
+ }
188
+ },
189
+ });
190
+ }