@alexsun-top/opencode-chat-room 0.8.3 → 0.8.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (156) hide show
  1. package/CHANGELOG.md +7 -1
  2. package/README.md +5 -5
  3. package/README.zh-CN.md +5 -5
  4. package/cli.ts +1 -1
  5. package/dist/cli.js +35 -16
  6. package/package.json +15 -9
  7. package/packages/chat-room-channeld/README.md +3 -2
  8. package/packages/chat-room-channeld/src/inbound.ts +3 -6
  9. package/packages/chat-room-channeld/src/pg/sql.mjs +157 -142
  10. package/packages/chat-room-channeld/src/pg/sql.mts +175 -0
  11. package/packages/chat-room-channeld/src/pg/worker.mjs +107 -99
  12. package/packages/chat-room-channeld/src/pg/worker.mts +166 -0
  13. package/plugin.ts +1 -1
  14. package/server/assets/chat/admin-badge.js +1 -1
  15. package/server/assets/chat/admin.js +16 -10
  16. package/server/assets/chat/announcements.js +12 -2
  17. package/server/assets/chat/attachment-store.js +15 -6
  18. package/server/assets/chat/attachments.js +48 -33
  19. package/server/assets/chat/chat-background.js +8 -14
  20. package/server/assets/chat/clipboard.js +2 -1
  21. package/server/assets/chat/confirm-dialog.js +3 -3
  22. package/server/assets/chat/copy-action.js +1 -2
  23. package/server/assets/chat/diagram-viewer.js +7 -9
  24. package/server/assets/chat/doc-preview.js +19 -12
  25. package/server/assets/chat/editor.js +87 -61
  26. package/server/assets/chat/events.js +49 -34
  27. package/server/assets/chat/files.js +45 -13
  28. package/server/assets/chat/history-loader.js +5 -3
  29. package/server/assets/chat/image-zoom.js +21 -17
  30. package/server/assets/chat/install-prompt.js +10 -9
  31. package/server/assets/chat/integration-account-schema.js +5 -3
  32. package/server/assets/chat/integration-editor.js +2 -2
  33. package/server/assets/chat/integration-values.js +17 -11
  34. package/server/assets/chat/integrations.js +138 -81
  35. package/server/assets/chat/join-requests.js +20 -13
  36. package/server/assets/chat/markdown-footnotes.js +8 -8
  37. package/server/assets/chat/markdown.js +48 -38
  38. package/server/assets/chat/members.js +48 -25
  39. package/server/assets/chat/mentions.js +39 -12
  40. package/server/assets/chat/message-cache.js +2 -2
  41. package/server/assets/chat/message-polling.js +40 -21
  42. package/server/assets/chat/message-window.js +10 -6
  43. package/server/assets/chat/messages.js +70 -46
  44. package/server/assets/chat/office-preview.js +3 -3
  45. package/server/assets/chat/palette.js +45 -29
  46. package/server/assets/chat/panels.js +19 -1
  47. package/server/assets/chat/pwa.js +5 -4
  48. package/server/assets/chat/questions.js +25 -22
  49. package/server/assets/chat/recall.js +39 -21
  50. package/server/assets/chat/receipts.js +9 -7
  51. package/server/assets/chat/reminders.js +25 -8
  52. package/server/assets/chat/replies.js +34 -15
  53. package/server/assets/chat/resource-format.js +2 -2
  54. package/server/assets/chat/room-list.js +2 -2
  55. package/server/assets/chat/room-watcher.js +8 -4
  56. package/server/assets/chat/rooms.js +51 -40
  57. package/server/assets/chat/search.js +8 -6
  58. package/server/assets/chat/settings.js +16 -14
  59. package/server/assets/chat/shiki.js +1 -1
  60. package/server/assets/chat/shortcuts.js +3 -3
  61. package/server/assets/chat/side-pane.js +4 -3
  62. package/server/assets/chat/state.js +65 -37
  63. package/server/assets/chat/system-events.js +6 -5
  64. package/server/assets/chat/system-messages.js +15 -15
  65. package/server/assets/chat/task-form.js +5 -5
  66. package/server/assets/chat/task-row.js +1 -1
  67. package/server/assets/chat/tasks.js +41 -28
  68. package/server/assets/chat/window-controls.js +3 -2
  69. package/server/assets/conversation.css +14 -2
  70. package/server/assets/doc-preview.css +6 -0
  71. package/server/assets/office/THIRD-PARTY-NOTICES.txt +23 -1
  72. package/server/assets/office/licenses/docx-preview-HEADER.txt +6 -0
  73. package/server/assets/office/licenses/echarts-LICENSE-d3.txt +27 -0
  74. package/server/assets/office/licenses/echarts-NOTICE.txt +5 -0
  75. package/server/assets/office/licenses/immediate-LICENSE.txt +20 -0
  76. package/server/assets/office/licenses/lie-LICENSE.txt +7 -0
  77. package/server/assets/office/licenses/pako-LICENSE.txt +21 -0
  78. package/server/assets/office/licenses/pako-ZLIB.txt +18 -0
  79. package/server/assets/office/licenses/setimmediate-LICENSE.txt +20 -0
  80. package/server/chat.html +1 -0
  81. package/server/sw.js +3 -1
  82. package/skills/chatroom/SKILL.md +9 -0
  83. package/src/README.md +1 -1
  84. package/src/agent/agent-receipts.ts +12 -4
  85. package/src/center/center-client.ts +1 -1
  86. package/src/state/pg-sql.mjs +157 -142
  87. package/src/state/pg-sql.mts +175 -0
  88. package/src/state/pg-worker.mjs +107 -99
  89. package/src/state/pg-worker.mts +166 -0
  90. package/src/state/store.ts +21 -7
  91. package/src/tui/app-types.mjs +1 -0
  92. package/src/tui/app-types.mts +175 -0
  93. package/src/tui/app.mjs +426 -423
  94. package/src/tui/app.mts +500 -0
  95. package/src/tui/config.mjs +166 -158
  96. package/src/tui/config.mts +247 -0
  97. package/src/tui/http.mjs +239 -206
  98. package/src/tui/http.mts +412 -0
  99. package/src/tui/i18n.mjs +17 -20
  100. package/src/tui/i18n.mts +42 -0
  101. package/src/tui/info-app.mjs +431 -427
  102. package/src/tui/info-app.mts +500 -0
  103. package/src/tui/info-content.mjs +205 -199
  104. package/src/tui/info-content.mts +341 -0
  105. package/src/tui/join-app.mjs +75 -64
  106. package/src/tui/join-app.mts +99 -0
  107. package/src/tui/locales/en.mjs +302 -320
  108. package/src/tui/locales/en.mts +349 -0
  109. package/src/tui/locales/zh-CN.mjs +302 -320
  110. package/src/tui/locales/zh-CN.mts +349 -0
  111. package/src/tui/main.mjs +219 -213
  112. package/src/tui/main.mts +255 -0
  113. package/src/tui/markdown.mjs +128 -272
  114. package/src/tui/markdown.mts +152 -0
  115. package/src/tui/mentions.mjs +181 -184
  116. package/src/tui/mentions.mts +251 -0
  117. package/src/tui/message-app.mjs +181 -173
  118. package/src/tui/message-app.mts +213 -0
  119. package/src/tui/message-view.mjs +90 -111
  120. package/src/tui/message-view.mts +151 -0
  121. package/src/tui/messages.mjs +55 -49
  122. package/src/tui/messages.mts +106 -0
  123. package/src/tui/palette-app.mjs +364 -344
  124. package/src/tui/palette-app.mts +388 -0
  125. package/src/tui/palette.mjs +100 -98
  126. package/src/tui/palette.mts +176 -0
  127. package/src/tui/prefs.mjs +56 -63
  128. package/src/tui/prefs.mts +126 -0
  129. package/src/tui/reminder-app.mjs +191 -181
  130. package/src/tui/reminder-app.mts +228 -0
  131. package/src/tui/room-settings-app.mjs +257 -242
  132. package/src/tui/room-settings-app.mts +301 -0
  133. package/src/tui/session.mjs +30 -29
  134. package/src/tui/session.mts +51 -0
  135. package/src/tui/settings-app.mjs +151 -138
  136. package/src/tui/settings-app.mts +179 -0
  137. package/src/tui/ui-content.mjs +426 -421
  138. package/src/tui/ui-content.mts +499 -0
  139. package/src/tui/ui-help.mjs +58 -67
  140. package/src/tui/ui-help.mts +87 -0
  141. package/src/tui/ui-mentions.mjs +34 -36
  142. package/src/tui/ui-mentions.mts +60 -0
  143. package/src/tui/ui-overlay-view.mjs +259 -267
  144. package/src/tui/ui-overlay-view.mts +322 -0
  145. package/src/tui/ui-suggest-view.mjs +54 -57
  146. package/src/tui/ui-suggest-view.mts +78 -0
  147. package/src/tui/ui-suggest.mjs +164 -167
  148. package/src/tui/ui-suggest.mts +248 -0
  149. package/src/tui/ui-theme.mjs +52 -57
  150. package/src/tui/ui-theme.mts +98 -0
  151. package/src/tui/ui-view.mjs +267 -275
  152. package/src/tui/ui-view.mts +308 -0
  153. package/src/tui/ui.mjs +372 -359
  154. package/src/tui/ui.mts +440 -0
  155. package/src/tui/wire.mjs +215 -0
  156. package/src/tui/wire.mts +383 -0
@@ -0,0 +1,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
+ }