@alexsun-top/opencode-chat-room 0.7.5 → 0.7.7
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 +27 -0
- package/README.md +5 -5
- package/README.zh-CN.md +5 -5
- package/cli.ts +20 -0
- package/dist/cli.js +753 -164
- package/package.json +2 -1
- package/plugin.ts +15 -3
- package/server/assets/base.css +0 -2
- package/server/assets/chat/doc-preview.js +122 -0
- package/server/assets/chat/editor.js +10 -7
- package/server/assets/chat/events.js +7 -1
- package/server/assets/chat/files.js +14 -6
- package/server/assets/chat/index.js +4 -0
- package/server/assets/chat/locales/de.js +21 -3
- package/server/assets/chat/locales/es.js +21 -3
- package/server/assets/chat/locales/fr.js +21 -3
- package/server/assets/chat/locales/it.js +21 -3
- package/server/assets/chat/locales/ja.js +21 -3
- package/server/assets/chat/locales/ko.js +21 -3
- package/server/assets/chat/markdown.js +39 -5
- package/server/assets/chat/messages.js +1 -0
- package/server/assets/chat/palette.js +18 -9
- package/server/assets/chat/questions.js +1 -0
- package/server/assets/chat/rooms.js +6 -1
- package/server/assets/chat/shortcuts.js +14 -3
- package/server/assets/chat/state.js +42 -6
- package/server/assets/chat/system-messages.js +9 -1
- package/server/assets/chat/task-form.js +197 -0
- package/server/assets/chat/task-row.js +169 -0
- package/server/assets/chat/tasks.js +494 -0
- package/server/assets/doc-preview.css +158 -0
- package/server/assets/edit-room.css +8 -0
- package/server/assets/responsive.css +26 -1
- package/server/assets/settings.css +9 -2
- package/server/assets/tasks.css +422 -0
- package/server/chat.html +129 -33
- package/server/member-routes.ts +3 -0
- package/server/sw.js +13 -2
- package/server/task-routes.ts +53 -0
- package/skills/chatroom/SKILL.md +3 -1
- package/src/README.md +3 -3
- package/src/agent/task-tool.ts +155 -0
- package/src/center/center-client.ts +44 -0
- package/src/rooms/tasks.ts +246 -0
- package/src/runtime/v2-plugin.ts +11 -2
- package/src/state/sqlite-state.ts +67 -0
- package/src/tui/app.mjs +21 -4
- package/src/tui/http.mjs +109 -10
- package/src/tui/info-app.mjs +435 -0
- package/src/tui/info-content.mjs +198 -0
- package/src/tui/join-app.mjs +71 -0
- package/src/tui/main.mjs +23 -4
- package/src/tui/markdown.mjs +293 -0
- package/src/tui/mentions.mjs +218 -0
- package/src/tui/message-app.mjs +178 -0
- package/src/tui/message-view.mjs +128 -0
- package/src/tui/palette-app.mjs +91 -8
- package/src/tui/palette.mjs +17 -2
- package/src/tui/reminder-app.mjs +189 -0
- package/src/tui/room-settings-app.mjs +249 -0
- package/src/tui/ui-content.mjs +144 -94
- package/src/tui/ui-mentions.mjs +44 -0
- package/src/tui/ui-overlay-view.mjs +255 -0
- package/src/tui/ui-suggest-view.mjs +63 -0
- package/src/tui/ui-suggest.mjs +197 -0
- package/src/tui/ui-theme.mjs +2 -1
- package/src/tui/ui-view.mjs +4 -190
- package/src/tui/ui.mjs +111 -1
|
@@ -0,0 +1,218 @@
|
|
|
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
|
+
const escapeRegExp = (value) => String(value).replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
4
|
+
|
|
5
|
+
export function mentionNames(participants = []) {
|
|
6
|
+
const names = [];
|
|
7
|
+
const seen = new Set(["all"]);
|
|
8
|
+
for (const participant of Array.isArray(participants) ? participants : []) {
|
|
9
|
+
const name = String(participant?.name ?? participant?.id ?? "").trim();
|
|
10
|
+
if (!name || seen.has(name.toLowerCase())) continue;
|
|
11
|
+
seen.add(name.toLowerCase());
|
|
12
|
+
names.push(name);
|
|
13
|
+
}
|
|
14
|
+
return names;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function mentionItems(participants, query = "") {
|
|
18
|
+
const needle = String(query ?? "").toLowerCase();
|
|
19
|
+
const items = mentionNames(participants).map((name) => ({
|
|
20
|
+
name,
|
|
21
|
+
insert: `@${name}`,
|
|
22
|
+
label: name,
|
|
23
|
+
}));
|
|
24
|
+
items.push({ name: "all", insert: "@all", label: "Everyone (@all)" });
|
|
25
|
+
return items.filter((item) => {
|
|
26
|
+
const haystack = `${item.label} ${item.name}`.toLowerCase();
|
|
27
|
+
return !needle || haystack.includes(needle);
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Locate the @token at the end of the draft, outside email-like tokens. */
|
|
32
|
+
export function mentionTrigger(text) {
|
|
33
|
+
const source = String(text ?? "");
|
|
34
|
+
const match = source.match(/(^|[^\p{L}\p{N}_@])@([^\n@]{0,64})$/u);
|
|
35
|
+
if (!match) return null;
|
|
36
|
+
const query = match[2];
|
|
37
|
+
return { start: source.length - query.length - 1, query, cursor: source.length };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function insertMention(draft, trigger, insert) {
|
|
41
|
+
const source = String(draft ?? "");
|
|
42
|
+
const at = trigger ?? mentionTrigger(source);
|
|
43
|
+
if (!at) return source;
|
|
44
|
+
const before = source.slice(0, at.start);
|
|
45
|
+
const after = source.slice(at.cursor ?? source.length);
|
|
46
|
+
return `${before}${insert} ${after}`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Find a closing backtick run of exactly `length`, never a longer run. */
|
|
50
|
+
function closingBackticks(source, from, end, length) {
|
|
51
|
+
let i = from;
|
|
52
|
+
while (i < end) {
|
|
53
|
+
if (source[i] !== "`") {
|
|
54
|
+
i += 1;
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
let run = 0;
|
|
58
|
+
while (i + run < end && source[i + run] === "`") run += 1;
|
|
59
|
+
if (run === length) return i;
|
|
60
|
+
i += run;
|
|
61
|
+
}
|
|
62
|
+
return -1;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Split Markdown prose from fenced/indented code and inline code spans. */
|
|
66
|
+
function codeSegments(source) {
|
|
67
|
+
const segments = [];
|
|
68
|
+
let proseStart = 0;
|
|
69
|
+
let lineStart = 0;
|
|
70
|
+
const push = (start, end, code) => {
|
|
71
|
+
if (end > start) segments.push({ text: source.slice(start, end), code, start, end });
|
|
72
|
+
};
|
|
73
|
+
while (lineStart < source.length) {
|
|
74
|
+
const lineEnd = source.indexOf("\n", lineStart);
|
|
75
|
+
const end = lineEnd < 0 ? source.length : lineEnd;
|
|
76
|
+
const line = source.slice(lineStart, end);
|
|
77
|
+
const opener = /^([ \t]*)(`{3,}|~{3,})/.exec(line);
|
|
78
|
+
if (opener) {
|
|
79
|
+
if (lineStart > proseStart) splitInlineCode(source, proseStart, lineStart, segments);
|
|
80
|
+
const fence = opener[2];
|
|
81
|
+
const indent = opener[1].length;
|
|
82
|
+
let scan = lineEnd < 0 ? source.length : lineEnd + 1;
|
|
83
|
+
let closed = source.length;
|
|
84
|
+
while (scan < source.length) {
|
|
85
|
+
const nextEnd = source.indexOf("\n", scan);
|
|
86
|
+
const stop = nextEnd < 0 ? source.length : nextEnd;
|
|
87
|
+
const close = /^([ \t]*)(`{3,}|~{3,})[ \t]*$/.exec(source.slice(scan, stop));
|
|
88
|
+
if (close && close[2][0] === fence[0] && close[2].length >= fence.length
|
|
89
|
+
&& close[1].length <= indent + 3) {
|
|
90
|
+
closed = nextEnd < 0 ? source.length : nextEnd + 1;
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
scan = nextEnd < 0 ? source.length : nextEnd + 1;
|
|
94
|
+
}
|
|
95
|
+
push(lineStart, closed, true);
|
|
96
|
+
proseStart = closed;
|
|
97
|
+
lineStart = closed;
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
// CommonMark indented code is also literal. Treat four spaces/tab as code
|
|
101
|
+
// rather than risking a mention rewrite inside it.
|
|
102
|
+
if (/^(?: {4,}|\t)/.test(line)) {
|
|
103
|
+
if (lineStart > proseStart) splitInlineCode(source, proseStart, lineStart, segments);
|
|
104
|
+
let scan = lineStart;
|
|
105
|
+
let closed = source.length;
|
|
106
|
+
while (scan < source.length) {
|
|
107
|
+
const nextEnd = source.indexOf("\n", scan);
|
|
108
|
+
const stop = nextEnd < 0 ? source.length : nextEnd;
|
|
109
|
+
const current = source.slice(scan, stop);
|
|
110
|
+
if (current.trim() && !/^(?: {4,}|\t)/.test(current)) {
|
|
111
|
+
closed = scan;
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
scan = nextEnd < 0 ? source.length : nextEnd + 1;
|
|
115
|
+
}
|
|
116
|
+
push(lineStart, closed, true);
|
|
117
|
+
proseStart = closed;
|
|
118
|
+
lineStart = closed;
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
lineStart = lineEnd < 0 ? source.length : lineEnd + 1;
|
|
122
|
+
}
|
|
123
|
+
splitInlineCode(source, proseStart, source.length, segments);
|
|
124
|
+
return segments;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function splitInlineCode(source, start, end, segments) {
|
|
128
|
+
let prose = start;
|
|
129
|
+
let i = start;
|
|
130
|
+
while (i < end) {
|
|
131
|
+
if (source[i] !== "`") {
|
|
132
|
+
i += 1;
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
let run = 1;
|
|
136
|
+
while (i + run < end && source[i + run] === "`") run += 1;
|
|
137
|
+
const close = closingBackticks(source, i + run, end, run);
|
|
138
|
+
if (close < 0) {
|
|
139
|
+
i += run;
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
if (i > prose) segments.push({ text: source.slice(prose, i), code: false, start: prose, end: i });
|
|
143
|
+
segments.push({ text: source.slice(i, close + run), code: true, start: i, end: close + run });
|
|
144
|
+
prose = close + run;
|
|
145
|
+
i = prose;
|
|
146
|
+
}
|
|
147
|
+
if (end > prose) segments.push({ text: source.slice(prose, end), code: false, start: prose, end });
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function convertPlain(segment, names) {
|
|
151
|
+
if (!segment) return segment;
|
|
152
|
+
const candidates = ["all", ...names].sort((a, b) => b.length - a.length);
|
|
153
|
+
const pattern = new RegExp(
|
|
154
|
+
`(?<![\\p{L}\\p{N}_.@])@(${candidates.map(escapeRegExp).join("|")})(?![\\p{L}\\p{N}_])`,
|
|
155
|
+
"gu",
|
|
156
|
+
);
|
|
157
|
+
return segment.replace(pattern, (_match, name) => `<at>@${name}</at>`);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function convertProse(source, names) {
|
|
161
|
+
const tag = /<at>[\s\S]*?<\/at>/giu;
|
|
162
|
+
let output = "";
|
|
163
|
+
let last = 0;
|
|
164
|
+
for (const match of source.matchAll(tag)) {
|
|
165
|
+
output += convertPlain(source.slice(last, match.index), names);
|
|
166
|
+
output += match[0];
|
|
167
|
+
last = match.index + match[0].length;
|
|
168
|
+
}
|
|
169
|
+
return output + convertPlain(source.slice(last), names);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** Convert known @names and @all to <at> tags, preserving code and existing tags. */
|
|
173
|
+
export function canonicalizeMentions(text, participants = []) {
|
|
174
|
+
const source = String(text ?? "");
|
|
175
|
+
const names = mentionNames(participants);
|
|
176
|
+
return codeSegments(source)
|
|
177
|
+
.map((segment) => (segment.code ? segment.text : convertProse(segment.text, names)))
|
|
178
|
+
.join("");
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** Character ranges for visible mention chips, excluding email-like tokens. */
|
|
182
|
+
export function mentionRanges(text, participants = []) {
|
|
183
|
+
const source = String(text ?? "");
|
|
184
|
+
const ranges = [];
|
|
185
|
+
const candidates = ["all", ...mentionNames(participants)].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
|
+
for (const segment of codeSegments(source)) {
|
|
191
|
+
if (segment.code) continue;
|
|
192
|
+
const tag = /<at>([\s\S]*?)<\/at>/giu;
|
|
193
|
+
let last = 0;
|
|
194
|
+
const plain = [];
|
|
195
|
+
for (const match of segment.text.matchAll(tag)) {
|
|
196
|
+
const inner = match[1];
|
|
197
|
+
const trimmed = inner.trim();
|
|
198
|
+
const at = trimmed.indexOf("@");
|
|
199
|
+
if (at >= 0) {
|
|
200
|
+
const innerStart = segment.start + match.index + 4 + (inner.length - inner.trimStart().length);
|
|
201
|
+
ranges.push({ start: innerStart + at, end: innerStart + trimmed.length });
|
|
202
|
+
}
|
|
203
|
+
plain.push([last, match.index]);
|
|
204
|
+
last = match.index + match[0].length;
|
|
205
|
+
}
|
|
206
|
+
plain.push([last, segment.text.length]);
|
|
207
|
+
for (const [start, end] of plain) {
|
|
208
|
+
const piece = segment.text.slice(start, end);
|
|
209
|
+
for (const match of piece.matchAll(pattern)) {
|
|
210
|
+
ranges.push({
|
|
211
|
+
start: segment.start + start + match.index,
|
|
212
|
+
end: segment.start + start + match.index + match[0].length,
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return ranges.sort((a, b) => a.start - b.start);
|
|
218
|
+
}
|
|
@@ -0,0 +1,178 @@
|
|
|
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
|
+
|
|
6
|
+
const errorText = (error) => (error instanceof Error ? error.message : String(error ?? "unknown error"));
|
|
7
|
+
|
|
8
|
+
export function attachMessageActions(Class) {
|
|
9
|
+
Object.assign(Class.prototype, {
|
|
10
|
+
messageActionPalette(title, label, action, data = {}) {
|
|
11
|
+
const palette = this.state.palette;
|
|
12
|
+
if (!palette || !this.state.current || !this.state.joined) {
|
|
13
|
+
this.state.palette = null;
|
|
14
|
+
this.setStatus("open a joined room first", "error");
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
palette.mode = "prompt";
|
|
18
|
+
palette.title = title;
|
|
19
|
+
palette.subtitle = `#${this.state.current.name}`;
|
|
20
|
+
palette.error = "";
|
|
21
|
+
palette.prompt = { label, value: "", action, data };
|
|
22
|
+
palette.hint = "Enter next · Esc cancel";
|
|
23
|
+
this.touch();
|
|
24
|
+
return palette;
|
|
25
|
+
},
|
|
26
|
+
|
|
27
|
+
startMessageRecall() {
|
|
28
|
+
return this.messageActionPalette("Recall message", "Message ID to recall", "message-recall-id");
|
|
29
|
+
},
|
|
30
|
+
|
|
31
|
+
startMessageReveal() {
|
|
32
|
+
return this.messageActionPalette("Reveal message", "Message ID to reveal", "message-reveal-id");
|
|
33
|
+
},
|
|
34
|
+
|
|
35
|
+
startMessageReply() {
|
|
36
|
+
return this.messageActionPalette("Reply", "Message ID to reply to", "message-reply-id");
|
|
37
|
+
},
|
|
38
|
+
|
|
39
|
+
startAskQuestion() {
|
|
40
|
+
return this.messageActionPalette("Ask question", "Question text", "message-ask");
|
|
41
|
+
},
|
|
42
|
+
|
|
43
|
+
async submitMessagePrompt(prompt, value) {
|
|
44
|
+
const palette = this.state.palette;
|
|
45
|
+
const room = this.state.current;
|
|
46
|
+
if (!palette || palette.mode !== "prompt") return;
|
|
47
|
+
if (!room) {
|
|
48
|
+
palette.error = "open a joined room first";
|
|
49
|
+
this.touch();
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
if (prompt.action === "message-ask") {
|
|
53
|
+
const question = canonicalizeMentions(String(value ?? "").trim(), room.participants);
|
|
54
|
+
if (!question) {
|
|
55
|
+
palette.error = "question text is required";
|
|
56
|
+
this.touch();
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
palette.searching = true;
|
|
60
|
+
palette.error = "";
|
|
61
|
+
this.touch();
|
|
62
|
+
try {
|
|
63
|
+
const result = await this.api.send(room.id, this.sessionID, question, { isQuestion: true });
|
|
64
|
+
if (Array.isArray(result?.messages)) mergeMessages(this.state.messages, result.messages);
|
|
65
|
+
else if (result?.id) mergeMessages(this.state.messages, [result]);
|
|
66
|
+
this.state.connection = "online";
|
|
67
|
+
this.closePalette();
|
|
68
|
+
this.setStatus("question sent", "success");
|
|
69
|
+
this.markRead();
|
|
70
|
+
} catch (error) {
|
|
71
|
+
if (this.state.palette === palette) {
|
|
72
|
+
palette.searching = false;
|
|
73
|
+
palette.error = errorText(error);
|
|
74
|
+
this.touch();
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
const id = String(value ?? "").trim();
|
|
80
|
+
if (!id) {
|
|
81
|
+
palette.error = "message ID is required";
|
|
82
|
+
this.touch();
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
if (prompt.action === "message-recall-id" || prompt.action === "message-reveal-id") {
|
|
86
|
+
const recall = prompt.action === "message-recall-id";
|
|
87
|
+
palette.mode = "confirm";
|
|
88
|
+
palette.previous = { mode: "prompt", hint: palette.hint };
|
|
89
|
+
palette.confirm = {
|
|
90
|
+
text: recall ? `Recall message "${id}"?` : `Reveal the original text of "${id}"?`,
|
|
91
|
+
action: recall ? "message-recall" : "message-reveal",
|
|
92
|
+
data: { id },
|
|
93
|
+
};
|
|
94
|
+
palette.hint = "Enter or y confirms · n or Esc cancels";
|
|
95
|
+
palette.error = "";
|
|
96
|
+
this.touch();
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
if (prompt.action === "message-reply-id") {
|
|
100
|
+
palette.prompt = {
|
|
101
|
+
label: `Reply to ${id}`,
|
|
102
|
+
value: "",
|
|
103
|
+
action: "message-reply-text",
|
|
104
|
+
data: { id },
|
|
105
|
+
};
|
|
106
|
+
palette.hint = "Enter send · Esc cancel";
|
|
107
|
+
palette.error = "";
|
|
108
|
+
this.touch();
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
if (prompt.action === "message-reply-text") {
|
|
112
|
+
const text = canonicalizeMentions(String(value ?? "").trim(), room.participants);
|
|
113
|
+
if (!text) {
|
|
114
|
+
palette.error = "reply text is required";
|
|
115
|
+
this.touch();
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
palette.searching = true;
|
|
119
|
+
palette.error = "";
|
|
120
|
+
this.touch();
|
|
121
|
+
try {
|
|
122
|
+
const result = await this.api.send(room.id, this.sessionID, text, { replyToMessageID: prompt.data.id });
|
|
123
|
+
if (!this.isCurrent(room.id)) return;
|
|
124
|
+
if (Array.isArray(result?.messages)) mergeMessages(this.state.messages, result.messages);
|
|
125
|
+
else if (result?.id) mergeMessages(this.state.messages, [result]);
|
|
126
|
+
this.state.connection = "online";
|
|
127
|
+
this.closePalette();
|
|
128
|
+
this.setStatus("reply sent", "success");
|
|
129
|
+
this.markRead();
|
|
130
|
+
} catch (error) {
|
|
131
|
+
if (this.state.palette === palette) {
|
|
132
|
+
palette.searching = false;
|
|
133
|
+
palette.error = errorText(error);
|
|
134
|
+
this.touch();
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
},
|
|
139
|
+
|
|
140
|
+
async runMessageConfirm(confirm) {
|
|
141
|
+
const palette = this.state.palette;
|
|
142
|
+
const room = this.state.current;
|
|
143
|
+
if (!palette || !room) return;
|
|
144
|
+
const id = confirm.data.id;
|
|
145
|
+
palette.searching = true;
|
|
146
|
+
palette.error = "";
|
|
147
|
+
this.touch();
|
|
148
|
+
try {
|
|
149
|
+
if (confirm.action === "message-recall") {
|
|
150
|
+
await this.api.recall(room.id, id, this.sessionID);
|
|
151
|
+
const target = this.state.messages.find((message) => message.id === id);
|
|
152
|
+
if (target) target.recalledAt = Date.now();
|
|
153
|
+
this.closePalette();
|
|
154
|
+
this.setStatus(`recalled ${id}`, "success");
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
const revealed = await this.api.reveal(room.id, id, this.sessionID);
|
|
158
|
+
palette.mode = "detail";
|
|
159
|
+
palette.title = "Revealed message";
|
|
160
|
+
palette.confirm = null;
|
|
161
|
+
palette.detail = {
|
|
162
|
+
title: "Revealed message",
|
|
163
|
+
markdown: String(revealed?.text ?? "(empty)"),
|
|
164
|
+
kind: "revealed-message",
|
|
165
|
+
};
|
|
166
|
+
palette.hint = "Esc close";
|
|
167
|
+
palette.searching = false;
|
|
168
|
+
this.touch();
|
|
169
|
+
} catch (error) {
|
|
170
|
+
if (this.state.palette === palette) {
|
|
171
|
+
palette.searching = false;
|
|
172
|
+
palette.error = errorText(error);
|
|
173
|
+
this.touch();
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
},
|
|
177
|
+
});
|
|
178
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
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 {
|
|
15
|
+
MENTION_SENTINEL,
|
|
16
|
+
createMarkdownSyntax,
|
|
17
|
+
createMentionRenderNode,
|
|
18
|
+
formatAttachments,
|
|
19
|
+
prepareMarkdown,
|
|
20
|
+
} from "./markdown.mjs";
|
|
21
|
+
|
|
22
|
+
let treeSitter;
|
|
23
|
+
const treeClient = () => (treeSitter ??= getTreeSitterClient());
|
|
24
|
+
|
|
25
|
+
export function hashText(text) {
|
|
26
|
+
let hash = 2166136261;
|
|
27
|
+
for (const char of String(text)) {
|
|
28
|
+
hash ^= char.codePointAt(0);
|
|
29
|
+
hash = Math.imul(hash, 16777619);
|
|
30
|
+
}
|
|
31
|
+
return (hash >>> 0).toString(36);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function markdownStyle(ui) {
|
|
35
|
+
let style = ui.syntaxStyles.get(ui.theme.mode);
|
|
36
|
+
if (!style) {
|
|
37
|
+
style = createMarkdownSyntax(ui.theme);
|
|
38
|
+
ui.syntaxStyles.set(ui.theme.mode, style);
|
|
39
|
+
}
|
|
40
|
+
return style;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function messageChunks(ui, message, info) {
|
|
44
|
+
const { theme } = ui;
|
|
45
|
+
const chunks = [];
|
|
46
|
+
const highlighted = message.id === ui.app.state.highlight;
|
|
47
|
+
if (highlighted) chunks.push(bold(fg(theme.accent)("▸ ")));
|
|
48
|
+
if (info.question) chunks.push(bold(fg(theme.warning)("? ")));
|
|
49
|
+
chunks.push(dim(`${info.time} `));
|
|
50
|
+
const own = message.senderId === ui.app.state.participant;
|
|
51
|
+
chunks.push(bold(fg(own ? theme.accent : theme.text)(info.sender)));
|
|
52
|
+
return chunks;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function messageBody(ui, message, info, highlighted) {
|
|
56
|
+
const body = new BoxRenderable(ui.renderer, {
|
|
57
|
+
id: `message-body-${message.id}`,
|
|
58
|
+
width: "100%",
|
|
59
|
+
flexDirection: "column",
|
|
60
|
+
flexShrink: 0,
|
|
61
|
+
paddingLeft: 2,
|
|
62
|
+
paddingRight: 1,
|
|
63
|
+
backgroundColor: highlighted ? ui.theme.raised : undefined,
|
|
64
|
+
});
|
|
65
|
+
if (info.recalled) {
|
|
66
|
+
body.add(new TextRenderable(ui.renderer, {
|
|
67
|
+
id: `message-recalled-${message.id}`,
|
|
68
|
+
width: "100%",
|
|
69
|
+
wrapMode: "word",
|
|
70
|
+
content: new StyledText([dim(italic("[recalled]"))]),
|
|
71
|
+
}));
|
|
72
|
+
return body;
|
|
73
|
+
}
|
|
74
|
+
const markdown = prepareMarkdown(formatAttachments(info.body));
|
|
75
|
+
body.add(new MarkdownRenderable(ui.renderer, {
|
|
76
|
+
id: `message-markdown-${message.id}`,
|
|
77
|
+
width: "100%",
|
|
78
|
+
content: markdown,
|
|
79
|
+
syntaxStyle: markdownStyle(ui),
|
|
80
|
+
treeSitterClient: treeClient(),
|
|
81
|
+
fg: ui.theme.text,
|
|
82
|
+
conceal: true,
|
|
83
|
+
concealCode: false,
|
|
84
|
+
streaming: false,
|
|
85
|
+
tableOptions: {
|
|
86
|
+
style: "columns",
|
|
87
|
+
widthMode: "full",
|
|
88
|
+
wrapMode: "word",
|
|
89
|
+
cellPaddingX: 1,
|
|
90
|
+
cellPaddingY: 0,
|
|
91
|
+
borderColor: ui.theme.border,
|
|
92
|
+
},
|
|
93
|
+
...(markdown.includes(MENTION_SENTINEL)
|
|
94
|
+
? { renderNode: createMentionRenderNode(ui.renderer, ui.theme, `message-${message.id}`) }
|
|
95
|
+
: {}),
|
|
96
|
+
}));
|
|
97
|
+
return body;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function buildMessageBlock(ui, message, info, highlighted, marginTop) {
|
|
101
|
+
const block = new BoxRenderable(ui.renderer, {
|
|
102
|
+
id: `message-${message.id}`,
|
|
103
|
+
width: "100%",
|
|
104
|
+
flexDirection: "column",
|
|
105
|
+
flexShrink: 0,
|
|
106
|
+
marginTop,
|
|
107
|
+
});
|
|
108
|
+
block.add(new TextRenderable(ui.renderer, {
|
|
109
|
+
id: `message-head-${message.id}`,
|
|
110
|
+
width: "100%",
|
|
111
|
+
height: 1,
|
|
112
|
+
wrapMode: "none",
|
|
113
|
+
truncate: true,
|
|
114
|
+
content: new StyledText(messageChunks(ui, message, info)),
|
|
115
|
+
}));
|
|
116
|
+
block.add(messageBody(ui, message, info, highlighted));
|
|
117
|
+
return block;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export function addNotice(ui, id, text, color) {
|
|
121
|
+
ui.messageContent.add(new TextRenderable(ui.renderer, {
|
|
122
|
+
id: `message-${id}`,
|
|
123
|
+
width: "100%",
|
|
124
|
+
flexShrink: 0,
|
|
125
|
+
wrapMode: "char",
|
|
126
|
+
content: new StyledText([fg(color)(text)]),
|
|
127
|
+
}));
|
|
128
|
+
}
|
package/src/tui/palette-app.mjs
CHANGED
|
@@ -1,24 +1,30 @@
|
|
|
1
1
|
// Command palette controller attached to ChatApp. Kept in its own file so the
|
|
2
2
|
// terminal core stays under the project line budget.
|
|
3
|
-
import { buildCommands, nextScope, scopeSources, searchMessages } from "./palette.mjs";
|
|
3
|
+
import { buildCommands, filterCommands, nextScope, scopeSources, searchMessages } from "./palette.mjs";
|
|
4
4
|
import { mergeMessages, oldestSeq } from "./messages.mjs";
|
|
5
5
|
|
|
6
6
|
const SEARCH_PAGES = 2;
|
|
7
7
|
|
|
8
8
|
export function attachPalette(Class) {
|
|
9
9
|
Object.assign(Class.prototype, {
|
|
10
|
-
openPalette() {
|
|
10
|
+
openPalette({ slash = false } = {}) {
|
|
11
11
|
this.historyPages.clear();
|
|
12
12
|
this.state.help = false;
|
|
13
13
|
this.state.palette = {
|
|
14
14
|
mode: "commands",
|
|
15
|
-
query: "",
|
|
15
|
+
query: slash ? "/" : "",
|
|
16
16
|
scope: "room",
|
|
17
17
|
selected: 0,
|
|
18
18
|
results: [],
|
|
19
19
|
prompt: null,
|
|
20
20
|
searching: false,
|
|
21
21
|
error: "",
|
|
22
|
+
commands: buildCommands({
|
|
23
|
+
rooms: this.state.rooms,
|
|
24
|
+
current: this.state.current,
|
|
25
|
+
joined: this.state.joined,
|
|
26
|
+
help: this.state.help,
|
|
27
|
+
}),
|
|
22
28
|
};
|
|
23
29
|
this.touch();
|
|
24
30
|
},
|
|
@@ -30,12 +36,15 @@ export function attachPalette(Class) {
|
|
|
30
36
|
},
|
|
31
37
|
|
|
32
38
|
paletteCommands() {
|
|
33
|
-
|
|
39
|
+
const palette = this.state.palette;
|
|
40
|
+
const query = palette?.mode === "commands" ? String(palette.query ?? "").replace(/^\s*\//, "") : "";
|
|
41
|
+
const commands = palette?.commands ?? buildCommands({
|
|
34
42
|
rooms: this.state.rooms,
|
|
35
43
|
current: this.state.current,
|
|
36
44
|
joined: this.state.joined,
|
|
37
45
|
help: this.state.help,
|
|
38
46
|
});
|
|
47
|
+
return filterCommands(commands, query);
|
|
39
48
|
},
|
|
40
49
|
|
|
41
50
|
paletteItems() {
|
|
@@ -43,6 +52,7 @@ export function attachPalette(Class) {
|
|
|
43
52
|
if (!palette) return [];
|
|
44
53
|
if (palette.mode === "search") return palette.results;
|
|
45
54
|
if (palette.mode === "commands") return this.paletteCommands();
|
|
55
|
+
if (palette.mode === "info") return palette.items ?? [];
|
|
46
56
|
return [];
|
|
47
57
|
},
|
|
48
58
|
|
|
@@ -114,9 +124,18 @@ export function attachPalette(Class) {
|
|
|
114
124
|
const palette = this.state.palette;
|
|
115
125
|
if (!palette) return;
|
|
116
126
|
if (palette.mode === "prompt") {
|
|
117
|
-
await this.
|
|
127
|
+
await this.submitPalettePrompt();
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
if (palette.mode === "info") {
|
|
131
|
+
await this.runInfoSelection();
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
if (palette.mode === "confirm") {
|
|
135
|
+
await this.runConfirm();
|
|
118
136
|
return;
|
|
119
137
|
}
|
|
138
|
+
if (palette.mode === "detail") return;
|
|
120
139
|
if (palette.mode === "commands") {
|
|
121
140
|
const command = this.paletteCommands()[palette.selected];
|
|
122
141
|
if (command) await this.runPaletteCommand(command);
|
|
@@ -126,6 +145,17 @@ export function attachPalette(Class) {
|
|
|
126
145
|
if (result) await this.activateResult(result);
|
|
127
146
|
},
|
|
128
147
|
|
|
148
|
+
async submitPalettePrompt() {
|
|
149
|
+
const palette = this.state.palette;
|
|
150
|
+
const value = palette?.prompt?.value ?? "";
|
|
151
|
+
if (!palette?.prompt) return;
|
|
152
|
+
if (palette.prompt.action && palette.prompt.action !== "new-room") {
|
|
153
|
+
await this.submitCommandPrompt(palette.prompt, value);
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
await this.createRoomFromPrompt();
|
|
157
|
+
},
|
|
158
|
+
|
|
129
159
|
async runPaletteCommand(command) {
|
|
130
160
|
const palette = this.state.palette;
|
|
131
161
|
switch (command.id) {
|
|
@@ -135,7 +165,7 @@ export function attachPalette(Class) {
|
|
|
135
165
|
return;
|
|
136
166
|
case "new-room":
|
|
137
167
|
palette.mode = "prompt";
|
|
138
|
-
palette.prompt = { label: "New room name", value: "" };
|
|
168
|
+
palette.prompt = { label: "New room name", value: "", action: "new-room", data: {} };
|
|
139
169
|
palette.selected = 0;
|
|
140
170
|
palette.error = "";
|
|
141
171
|
this.touch();
|
|
@@ -161,6 +191,50 @@ export function attachPalette(Class) {
|
|
|
161
191
|
this.touch();
|
|
162
192
|
void this.ensureSearchHistory();
|
|
163
193
|
return;
|
|
194
|
+
case "members":
|
|
195
|
+
await this.openMembers();
|
|
196
|
+
return;
|
|
197
|
+
case "join-requests":
|
|
198
|
+
await this.openJoinRequests();
|
|
199
|
+
return;
|
|
200
|
+
case "announcements":
|
|
201
|
+
await this.openAnnouncements();
|
|
202
|
+
return;
|
|
203
|
+
case "questions":
|
|
204
|
+
await this.openQuestions();
|
|
205
|
+
return;
|
|
206
|
+
case "files":
|
|
207
|
+
await this.openFiles();
|
|
208
|
+
return;
|
|
209
|
+
case "reminders":
|
|
210
|
+
await this.openReminders();
|
|
211
|
+
return;
|
|
212
|
+
case "settings":
|
|
213
|
+
this.openSettings();
|
|
214
|
+
return;
|
|
215
|
+
case "room-settings":
|
|
216
|
+
await this.openRoomSettings();
|
|
217
|
+
return;
|
|
218
|
+
case "delete-room":
|
|
219
|
+
await this.openRoomSettings();
|
|
220
|
+
this.askDeleteRoom();
|
|
221
|
+
return;
|
|
222
|
+
case "recall-message":
|
|
223
|
+
this.startMessageRecall();
|
|
224
|
+
return;
|
|
225
|
+
case "reveal-message":
|
|
226
|
+
this.startMessageReveal();
|
|
227
|
+
return;
|
|
228
|
+
case "reply-message":
|
|
229
|
+
this.startMessageReply();
|
|
230
|
+
return;
|
|
231
|
+
case "ask-question":
|
|
232
|
+
this.startAskQuestion();
|
|
233
|
+
return;
|
|
234
|
+
case "quit":
|
|
235
|
+
this.closePalette();
|
|
236
|
+
this.quit();
|
|
237
|
+
return;
|
|
164
238
|
default:
|
|
165
239
|
if (command.roomId) {
|
|
166
240
|
const index = this.state.rooms.findIndex((room) => room.id === command.roomId);
|
|
@@ -228,10 +302,10 @@ export function attachPalette(Class) {
|
|
|
228
302
|
|
|
229
303
|
setPaletteQuery(value) {
|
|
230
304
|
const palette = this.state.palette;
|
|
231
|
-
if (!palette || palette.mode
|
|
305
|
+
if (!palette || (palette.mode !== "commands" && palette.mode !== "search")) return;
|
|
232
306
|
const query = String(value ?? "");
|
|
233
307
|
palette.error = "";
|
|
234
|
-
if (!query) {
|
|
308
|
+
if (!query.trim()) {
|
|
235
309
|
palette.mode = "commands";
|
|
236
310
|
palette.query = "";
|
|
237
311
|
palette.results = [];
|
|
@@ -240,6 +314,15 @@ export function attachPalette(Class) {
|
|
|
240
314
|
this.touch();
|
|
241
315
|
return;
|
|
242
316
|
}
|
|
317
|
+
if (query.trimStart().startsWith("/")) {
|
|
318
|
+
palette.mode = "commands";
|
|
319
|
+
palette.query = query;
|
|
320
|
+
palette.results = [];
|
|
321
|
+
palette.searching = false;
|
|
322
|
+
palette.selected = 0;
|
|
323
|
+
this.touch();
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
243
326
|
palette.mode = "search";
|
|
244
327
|
palette.query = query;
|
|
245
328
|
palette.selected = 0;
|