@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,213 @@
1
+ // Message actions on top of the palette prompts: recall and reveal by id with
2
+ // confirmation, and a prompt-based reply flow that uses api.send's replyTo.
3
+ import { mergeMessages } from "./messages.mjs";
4
+ import { canonicalizeMentions } from "./mentions.mjs";
5
+ import { t } from "./i18n.mjs";
6
+ import type { ChatApp } from "./app.mjs";
7
+ import type { PaletteConfirm, PaletteData, PalettePrompt, PaletteState } from "./app-types.mjs";
8
+
9
+ const errorText = (error: unknown): string => (error instanceof Error ? error.message : String(error ?? "unknown error"));
10
+
11
+ // Palette methods owned by the still-JS palette attach module.
12
+ interface MessageApp extends ChatApp {
13
+ closePalette(): void;
14
+ }
15
+
16
+ // The methods this module attaches become visible on ChatApp for app.mts and
17
+ // the later attach migrations.
18
+ declare module "./app.mjs" {
19
+ interface ChatApp {
20
+ messageActionPalette(title: string, label: string, action: string, data?: PaletteData): PaletteState | null;
21
+ startMessageRecall(): PaletteState | null;
22
+ startMessageReveal(): PaletteState | null;
23
+ startMessageReply(): PaletteState | null;
24
+ startAskQuestion(): PaletteState | null;
25
+ submitMessagePrompt(prompt: PalettePrompt, value: string): Promise<void>;
26
+ runMessageConfirm(confirm: PaletteConfirm): Promise<void>;
27
+ }
28
+ }
29
+
30
+ export function attachMessageActions(Class: { prototype: ChatApp }) {
31
+ Object.assign(Class.prototype, {
32
+ messageActionPalette(
33
+ this: MessageApp,
34
+ title: string,
35
+ label: string,
36
+ action: string,
37
+ data: PaletteData = {},
38
+ ): PaletteState | null {
39
+ const palette = this.state.palette;
40
+ const current = this.state.current;
41
+ if (!palette || !current || !this.state.joined) {
42
+ this.state.palette = null;
43
+ this.setStatus(t("info.joinFirst"), "error");
44
+ return null;
45
+ }
46
+ palette.mode = "prompt";
47
+ palette.roomId = current.id;
48
+ palette.roomParticipants = [...(current.participants || [])];
49
+ palette.title = title;
50
+ palette.subtitle = `#${current.name}`;
51
+ palette.error = "";
52
+ palette.prompt = { label, value: "", action, data };
53
+ palette.hint = t("palette.enterNextHint");
54
+ this.touch();
55
+ return palette;
56
+ },
57
+
58
+ startMessageRecall(this: MessageApp): PaletteState | null {
59
+ return this.messageActionPalette(t("message.recallTitle"), t("message.recallPrompt"), "message-recall-id");
60
+ },
61
+
62
+ startMessageReveal(this: MessageApp): PaletteState | null {
63
+ return this.messageActionPalette(t("message.revealTitle"), t("message.revealPrompt"), "message-reveal-id");
64
+ },
65
+
66
+ startMessageReply(this: MessageApp): PaletteState | null {
67
+ return this.messageActionPalette(t("message.replyTitle"), t("message.replyPrompt"), "message-reply-id");
68
+ },
69
+
70
+ startAskQuestion(this: MessageApp): PaletteState | null {
71
+ return this.messageActionPalette(t("message.askTitle"), t("message.askPrompt"), "message-ask");
72
+ },
73
+
74
+ async submitMessagePrompt(this: MessageApp, prompt: PalettePrompt, value: string): Promise<void> {
75
+ const palette = this.state.palette;
76
+ const roomId = palette?.roomId || this.state.current?.id;
77
+ const participants = palette?.roomParticipants || this.state.current?.participants;
78
+ if (!palette || palette.mode !== "prompt") return;
79
+ if (!roomId) {
80
+ palette.error = t("info.joinFirst");
81
+ this.touch();
82
+ return;
83
+ }
84
+ if (prompt.action === "message-ask") {
85
+ const question = canonicalizeMentions(String(value ?? "").trim(), participants);
86
+ if (!question) {
87
+ palette.error = t("message.questionRequired");
88
+ this.touch();
89
+ return;
90
+ }
91
+ palette.searching = true;
92
+ palette.error = "";
93
+ this.touch();
94
+ try {
95
+ const result = await this.api.send(roomId, this.sessionID, question, { isQuestion: true });
96
+ const messages = result?.messages;
97
+ if (Array.isArray(messages)) mergeMessages(this.state.messages, messages);
98
+ else if (result?.id) mergeMessages(this.state.messages, [result]);
99
+ this.state.connection = "online";
100
+ this.closePalette();
101
+ this.setStatus(t("message.questionSent"), "success");
102
+ this.markRead();
103
+ } catch (error) {
104
+ if (this.state.palette === palette) {
105
+ palette.searching = false;
106
+ palette.error = errorText(error);
107
+ this.touch();
108
+ }
109
+ }
110
+ return;
111
+ }
112
+ const id = String(value ?? "").trim();
113
+ if (!id) {
114
+ palette.error = t("message.idRequired");
115
+ this.touch();
116
+ return;
117
+ }
118
+ if (prompt.action === "message-recall-id" || prompt.action === "message-reveal-id") {
119
+ const recall = prompt.action === "message-recall-id";
120
+ palette.mode = "confirm";
121
+ palette.previous = { mode: "prompt", hint: palette.hint };
122
+ palette.confirm = {
123
+ text: recall ? t("message.recallConfirm", { id }) : t("message.revealConfirm", { id }),
124
+ action: recall ? "message-recall" : "message-reveal",
125
+ data: { id },
126
+ };
127
+ palette.hint = t("palette.confirmHint");
128
+ palette.error = "";
129
+ this.touch();
130
+ return;
131
+ }
132
+ if (prompt.action === "message-reply-id") {
133
+ palette.prompt = {
134
+ label: t("message.replyLabel", { id }),
135
+ value: "",
136
+ action: "message-reply-text",
137
+ data: { id },
138
+ };
139
+ palette.hint = t("message.replyHint");
140
+ palette.error = "";
141
+ this.touch();
142
+ return;
143
+ }
144
+ if (prompt.action === "message-reply-text") {
145
+ const text = canonicalizeMentions(String(value ?? "").trim(), participants);
146
+ if (!text) {
147
+ palette.error = t("message.replyRequired");
148
+ this.touch();
149
+ return;
150
+ }
151
+ palette.searching = true;
152
+ palette.error = "";
153
+ this.touch();
154
+ try {
155
+ const result = await this.api.send(roomId, this.sessionID, text, { replyToMessageID: prompt.data.id });
156
+ if (!this.isCurrent(roomId)) return;
157
+ const messages = result?.messages;
158
+ if (Array.isArray(messages)) mergeMessages(this.state.messages, messages);
159
+ else if (result?.id) mergeMessages(this.state.messages, [result]);
160
+ this.state.connection = "online";
161
+ this.closePalette();
162
+ this.setStatus(t("message.replySent"), "success");
163
+ this.markRead();
164
+ } catch (error) {
165
+ if (this.state.palette === palette) {
166
+ palette.searching = false;
167
+ palette.error = errorText(error);
168
+ this.touch();
169
+ }
170
+ }
171
+ }
172
+ },
173
+
174
+ async runMessageConfirm(this: MessageApp, confirm: PaletteConfirm): Promise<void> {
175
+ const palette = this.state.palette;
176
+ const roomId = palette?.roomId || this.state.current?.id;
177
+ if (!palette || !roomId) return;
178
+ const id = confirm.data.id;
179
+ if (!id) return;
180
+ palette.searching = true;
181
+ palette.error = "";
182
+ this.touch();
183
+ try {
184
+ if (confirm.action === "message-recall") {
185
+ await this.api.recall(roomId, id, this.sessionID);
186
+ const target = this.state.messages.find((message) => message.id === id);
187
+ if (target) target.recalledAt = Date.now();
188
+ this.closePalette();
189
+ this.setStatus(t("message.recalledStatus", { id }), "success");
190
+ return;
191
+ }
192
+ const revealed = await this.api.reveal(roomId, id, this.sessionID);
193
+ palette.mode = "detail";
194
+ palette.title = t("message.revealed");
195
+ palette.confirm = null;
196
+ palette.detail = {
197
+ title: t("message.revealed"),
198
+ markdown: String(revealed?.text ?? t("message.empty")),
199
+ kind: "revealed-message",
200
+ };
201
+ palette.hint = t("palette.closeHint");
202
+ palette.searching = false;
203
+ this.touch();
204
+ } catch (error) {
205
+ if (this.state.palette === palette) {
206
+ palette.searching = false;
207
+ palette.error = errorText(error);
208
+ this.touch();
209
+ }
210
+ }
211
+ },
212
+ });
213
+ }
@@ -1,129 +1,108 @@
1
1
  // Message row renderables: grouped metadata plus a Markdown body with mention
2
2
  // highlighting. Split from ui-content.mjs to keep the state sync readable.
3
- import {
4
- BoxRenderable,
5
- MarkdownRenderable,
6
- StyledText,
7
- TextRenderable,
8
- bold,
9
- dim,
10
- fg,
11
- getTreeSitterClient,
12
- italic,
13
- } from "@opentui/core";
14
- import {
15
- MENTION_SENTINEL,
16
- createMarkdownSyntax,
17
- createMentionRenderNode,
18
- formatAttachments,
19
- prepareMarkdown,
20
- } from "./markdown.mjs";
3
+ import { BoxRenderable, MarkdownRenderable, StyledText, TextRenderable, bold, dim, fg, getTreeSitterClient, italic, } from "@opentui/core";
4
+ import { MENTION_SENTINEL, createMarkdownSyntax, createMentionInlineHook, formatAttachments, prepareMarkdown, } from "./markdown.mjs";
21
5
  import { t } from "./i18n.mjs";
22
-
23
6
  let treeSitter;
24
7
  const treeClient = () => (treeSitter ??= getTreeSitterClient());
25
-
26
8
  export function hashText(text) {
27
- let hash = 2166136261;
28
- for (const char of String(text)) {
29
- hash ^= char.codePointAt(0);
30
- hash = Math.imul(hash, 16777619);
31
- }
32
- return (hash >>> 0).toString(36);
9
+ let hash = 2166136261;
10
+ for (const char of String(text)) {
11
+ hash ^= char.codePointAt(0) ?? 0;
12
+ hash = Math.imul(hash, 16777619);
13
+ }
14
+ return (hash >>> 0).toString(36);
33
15
  }
34
-
35
16
  export function markdownStyle(ui) {
36
- let style = ui.syntaxStyles.get(ui.theme.mode);
37
- if (!style) {
38
- style = createMarkdownSyntax(ui.theme);
39
- ui.syntaxStyles.set(ui.theme.mode, style);
40
- }
41
- return style;
17
+ let style = ui.syntaxStyles.get(ui.theme.mode);
18
+ if (!style) {
19
+ style = createMarkdownSyntax(ui.renderer, ui.theme);
20
+ ui.syntaxStyles.set(ui.theme.mode, style);
21
+ }
22
+ return style;
42
23
  }
43
-
44
24
  function messageChunks(ui, message, info) {
45
- const { theme } = ui;
46
- const chunks = [];
47
- const highlighted = message.id === ui.app.state.highlight;
48
- if (highlighted) chunks.push(bold(fg(theme.accent)("▸ ")));
49
- if (info.question) chunks.push(bold(fg(theme.warning)("? ")));
50
- chunks.push(dim(`${info.time} `));
51
- const own = message.senderId === ui.app.state.participant;
52
- chunks.push(bold(fg(own ? theme.accent : theme.text)(info.sender)));
53
- return chunks;
25
+ const { theme } = ui;
26
+ const chunks = [];
27
+ const highlighted = message.id === ui.app.state.highlight;
28
+ if (highlighted)
29
+ chunks.push(bold(fg(theme.accent)("▸ ")));
30
+ if (info.question)
31
+ chunks.push(bold(fg(theme.warning)("? ")));
32
+ chunks.push(dim(`${info.time} `));
33
+ const own = message.senderId === ui.app.state.participant;
34
+ chunks.push(bold(fg(own ? theme.accent : theme.text)(info.sender)));
35
+ return chunks;
54
36
  }
55
-
56
37
  function messageBody(ui, message, info, highlighted) {
57
- const body = new BoxRenderable(ui.renderer, {
58
- id: `message-body-${message.id}`,
59
- width: "100%",
60
- flexDirection: "column",
61
- flexShrink: 0,
62
- paddingLeft: 2,
63
- paddingRight: 1,
64
- backgroundColor: highlighted ? ui.theme.raised : undefined,
65
- });
66
- if (info.recalled) {
67
- body.add(new TextRenderable(ui.renderer, {
68
- id: `message-recalled-${message.id}`,
69
- width: "100%",
70
- wrapMode: "word",
71
- content: new StyledText([dim(italic(t("chat.recalled")))]),
38
+ const body = new BoxRenderable(ui.renderer, {
39
+ id: `message-body-${message.id}`,
40
+ width: "100%",
41
+ flexDirection: "column",
42
+ flexShrink: 0,
43
+ paddingLeft: 2,
44
+ paddingRight: 1,
45
+ backgroundColor: highlighted ? ui.theme.raised : undefined,
46
+ });
47
+ if (info.recalled) {
48
+ body.add(new TextRenderable(ui.renderer, {
49
+ id: `message-recalled-${message.id}`,
50
+ width: "100%",
51
+ wrapMode: "word",
52
+ content: new StyledText([dim(italic(t("chat.recalled")))]),
53
+ }));
54
+ return body;
55
+ }
56
+ const markdown = prepareMarkdown(formatAttachments(info.body));
57
+ body.add(new MarkdownRenderable(ui.renderer, {
58
+ id: `message-markdown-${message.id}`,
59
+ width: "100%",
60
+ content: markdown,
61
+ syntaxStyle: markdownStyle(ui),
62
+ treeSitterClient: treeClient(),
63
+ fg: ui.theme.text,
64
+ conceal: true,
65
+ concealCode: false,
66
+ streaming: false,
67
+ tableOptions: {
68
+ style: "columns",
69
+ widthMode: "full",
70
+ wrapMode: "word",
71
+ cellPaddingX: 1,
72
+ cellPaddingY: 0,
73
+ borderColor: ui.theme.border,
74
+ },
75
+ ...(markdown.includes(MENTION_SENTINEL)
76
+ ? { inlineHook: createMentionInlineHook(ui.theme), inlineHookFilter: [MENTION_SENTINEL] }
77
+ : {}),
72
78
  }));
73
79
  return body;
74
- }
75
- const markdown = prepareMarkdown(formatAttachments(info.body));
76
- body.add(new MarkdownRenderable(ui.renderer, {
77
- id: `message-markdown-${message.id}`,
78
- width: "100%",
79
- content: markdown,
80
- syntaxStyle: markdownStyle(ui),
81
- treeSitterClient: treeClient(),
82
- fg: ui.theme.text,
83
- conceal: true,
84
- concealCode: false,
85
- streaming: false,
86
- tableOptions: {
87
- style: "columns",
88
- widthMode: "full",
89
- wrapMode: "word",
90
- cellPaddingX: 1,
91
- cellPaddingY: 0,
92
- borderColor: ui.theme.border,
93
- },
94
- ...(markdown.includes(MENTION_SENTINEL)
95
- ? { renderNode: createMentionRenderNode(ui.renderer, ui.theme, `message-${message.id}`) }
96
- : {}),
97
- }));
98
- return body;
99
80
  }
100
-
101
81
  export function buildMessageBlock(ui, message, info, highlighted, marginTop) {
102
- const block = new BoxRenderable(ui.renderer, {
103
- id: `message-${message.id}`,
104
- width: "100%",
105
- flexDirection: "column",
106
- flexShrink: 0,
107
- marginTop,
108
- });
109
- block.add(new TextRenderable(ui.renderer, {
110
- id: `message-head-${message.id}`,
111
- width: "100%",
112
- height: 1,
113
- wrapMode: "none",
114
- truncate: true,
115
- content: new StyledText(messageChunks(ui, message, info)),
116
- }));
117
- block.add(messageBody(ui, message, info, highlighted));
118
- return block;
82
+ const block = new BoxRenderable(ui.renderer, {
83
+ id: `message-${message.id}`,
84
+ width: "100%",
85
+ flexDirection: "column",
86
+ flexShrink: 0,
87
+ marginTop,
88
+ });
89
+ block.add(new TextRenderable(ui.renderer, {
90
+ id: `message-head-${message.id}`,
91
+ width: "100%",
92
+ height: 1,
93
+ wrapMode: "none",
94
+ truncate: true,
95
+ content: new StyledText(messageChunks(ui, message, info)),
96
+ }));
97
+ block.add(messageBody(ui, message, info, highlighted));
98
+ return block;
119
99
  }
120
-
121
100
  export function addNotice(ui, id, text, color) {
122
- ui.messageContent.add(new TextRenderable(ui.renderer, {
123
- id: `message-${id}`,
124
- width: "100%",
125
- flexShrink: 0,
126
- wrapMode: "char",
127
- content: new StyledText([fg(color)(text)]),
128
- }));
129
- }
101
+ ui.messageContent.add(new TextRenderable(ui.renderer, {
102
+ id: `message-${id}`,
103
+ width: "100%",
104
+ flexShrink: 0,
105
+ wrapMode: "char",
106
+ content: new StyledText([fg(color)(text)]),
107
+ }));
108
+ }
@@ -0,0 +1,151 @@
1
+ // Message row renderables: grouped metadata plus a Markdown body with mention
2
+ // highlighting. Split from ui-content.mjs to keep the state sync readable.
3
+ import {
4
+ BoxRenderable,
5
+ MarkdownRenderable,
6
+ StyledText,
7
+ TextRenderable,
8
+ bold,
9
+ dim,
10
+ fg,
11
+ getTreeSitterClient,
12
+ italic,
13
+ } from "@opentui/core";
14
+ import type { CliRenderer, SyntaxStyle, TextChunk, ThemeMode } from "@opentui/core";
15
+ import {
16
+ MENTION_SENTINEL,
17
+ createMarkdownSyntax,
18
+ createMentionInlineHook,
19
+ formatAttachments,
20
+ prepareMarkdown,
21
+ } from "./markdown.mjs";
22
+ import { t } from "./i18n.mjs";
23
+ import type { ChatMessage, FormattedMessage } from "./messages.mjs";
24
+ import type { TuiTheme } from "./ui-theme.mjs";
25
+
26
+ export interface MessageAppState {
27
+ highlight?: string | null;
28
+ participant?: string;
29
+ }
30
+
31
+ export interface MessageUi {
32
+ renderer: CliRenderer;
33
+ theme: TuiTheme;
34
+ syntaxStyles: Map<ThemeMode, SyntaxStyle>;
35
+ app: { state: MessageAppState };
36
+ messageContent: BoxRenderable;
37
+ }
38
+
39
+ let treeSitter: ReturnType<typeof getTreeSitterClient> | undefined;
40
+ const treeClient = () => (treeSitter ??= getTreeSitterClient());
41
+
42
+ export function hashText(text: unknown): string {
43
+ let hash = 2166136261;
44
+ for (const char of String(text)) {
45
+ hash ^= char.codePointAt(0) ?? 0;
46
+ hash = Math.imul(hash, 16777619);
47
+ }
48
+ return (hash >>> 0).toString(36);
49
+ }
50
+
51
+ export function markdownStyle(ui: MessageUi): SyntaxStyle {
52
+ let style = ui.syntaxStyles.get(ui.theme.mode);
53
+ if (!style) {
54
+ style = createMarkdownSyntax(ui.renderer, ui.theme);
55
+ ui.syntaxStyles.set(ui.theme.mode, style);
56
+ }
57
+ return style;
58
+ }
59
+
60
+ function messageChunks(ui: MessageUi, message: ChatMessage, info: FormattedMessage): TextChunk[] {
61
+ const { theme } = ui;
62
+ const chunks: TextChunk[] = [];
63
+ const highlighted = message.id === ui.app.state.highlight;
64
+ if (highlighted) chunks.push(bold(fg(theme.accent)("▸ ")));
65
+ if (info.question) chunks.push(bold(fg(theme.warning)("? ")));
66
+ chunks.push(dim(`${info.time} `));
67
+ const own = message.senderId === ui.app.state.participant;
68
+ chunks.push(bold(fg(own ? theme.accent : theme.text)(info.sender)));
69
+ return chunks;
70
+ }
71
+
72
+ function messageBody(ui: MessageUi, message: ChatMessage, info: FormattedMessage, highlighted: boolean): BoxRenderable {
73
+ const body = new BoxRenderable(ui.renderer, {
74
+ id: `message-body-${message.id}`,
75
+ width: "100%",
76
+ flexDirection: "column",
77
+ flexShrink: 0,
78
+ paddingLeft: 2,
79
+ paddingRight: 1,
80
+ backgroundColor: highlighted ? ui.theme.raised : undefined,
81
+ });
82
+ if (info.recalled) {
83
+ body.add(new TextRenderable(ui.renderer, {
84
+ id: `message-recalled-${message.id}`,
85
+ width: "100%",
86
+ wrapMode: "word",
87
+ content: new StyledText([dim(italic(t("chat.recalled")))]),
88
+ }));
89
+ return body;
90
+ }
91
+ const markdown = prepareMarkdown(formatAttachments(info.body));
92
+ body.add(new MarkdownRenderable(ui.renderer, {
93
+ id: `message-markdown-${message.id}`,
94
+ width: "100%",
95
+ content: markdown,
96
+ syntaxStyle: markdownStyle(ui),
97
+ treeSitterClient: treeClient(),
98
+ fg: ui.theme.text,
99
+ conceal: true,
100
+ concealCode: false,
101
+ streaming: false,
102
+ tableOptions: {
103
+ style: "columns",
104
+ widthMode: "full",
105
+ wrapMode: "word",
106
+ cellPaddingX: 1,
107
+ cellPaddingY: 0,
108
+ borderColor: ui.theme.border,
109
+ },
110
+ ...(markdown.includes(MENTION_SENTINEL)
111
+ ? { inlineHook: createMentionInlineHook(ui.theme), inlineHookFilter: [MENTION_SENTINEL] }
112
+ : {}),
113
+ }));
114
+ return body;
115
+ }
116
+
117
+ export function buildMessageBlock(
118
+ ui: MessageUi,
119
+ message: ChatMessage,
120
+ info: FormattedMessage,
121
+ highlighted: boolean,
122
+ marginTop: number,
123
+ ): BoxRenderable {
124
+ const block = new BoxRenderable(ui.renderer, {
125
+ id: `message-${message.id}`,
126
+ width: "100%",
127
+ flexDirection: "column",
128
+ flexShrink: 0,
129
+ marginTop,
130
+ });
131
+ block.add(new TextRenderable(ui.renderer, {
132
+ id: `message-head-${message.id}`,
133
+ width: "100%",
134
+ height: 1,
135
+ wrapMode: "none",
136
+ truncate: true,
137
+ content: new StyledText(messageChunks(ui, message, info)),
138
+ }));
139
+ block.add(messageBody(ui, message, info, highlighted));
140
+ return block;
141
+ }
142
+
143
+ export function addNotice(ui: MessageUi, id: string, text: string, color: string): void {
144
+ ui.messageContent.add(new TextRenderable(ui.renderer, {
145
+ id: `message-${id}`,
146
+ width: "100%",
147
+ flexShrink: 0,
148
+ wrapMode: "char",
149
+ content: new StyledText([fg(color)(text)]),
150
+ }));
151
+ }