@alexsun-top/opencode-chat-room 0.7.3 → 0.7.4
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 +15 -0
- package/README.md +11 -4
- package/README.zh-CN.md +11 -4
- package/cli.ts +26 -6
- package/dist/cli.js +7804 -0
- package/package.json +19 -6
- package/plugin.ts +7 -4
- package/server/admin-auth.ts +1 -1
- package/server/admin-routes.ts +2 -2
- package/server/admin-state.ts +2 -1
- package/server/assets/chat/admin.js +43 -3
- package/server/assets/chat/announcements.js +5 -3
- package/server/assets/chat/confirm-dialog.js +12 -2
- package/server/assets/chat/create-room-dialog.js +5 -3
- package/server/assets/chat/events.js +32 -7
- package/server/assets/chat/files.js +368 -0
- package/server/assets/chat/index.js +6 -0
- package/server/assets/chat/locales/de.js +17 -1
- package/server/assets/chat/locales/es.js +17 -1
- package/server/assets/chat/locales/fr.js +17 -1
- package/server/assets/chat/locales/it.js +17 -1
- package/server/assets/chat/locales/ja.js +17 -1
- package/server/assets/chat/locales/ko.js +17 -1
- package/server/assets/chat/message-polling.js +61 -6
- package/server/assets/chat/message-window.js +28 -0
- package/server/assets/chat/messages.js +29 -4
- package/server/assets/chat/palette.js +384 -0
- package/server/assets/chat/rooms.js +8 -3
- package/server/assets/chat/search.js +137 -0
- package/server/assets/chat/settings.js +11 -5
- package/server/assets/chat/state.js +34 -2
- package/server/assets/edit-room.css +77 -0
- package/server/assets/files.css +237 -0
- package/server/assets/palette.css +277 -0
- package/server/assets/responsive.css +3 -2
- package/server/assets/settings.css +6 -3
- package/server/chat-server.ts +46 -29
- package/server/chat.html +127 -31
- package/server/http-server.ts +182 -0
- package/server/join-requests.ts +4 -4
- package/server/manifest.webmanifest +1 -1
- package/server/member-routes.ts +3 -3
- package/server/message-recall-routes.ts +1 -1
- package/server/newrooms.ts +4 -4
- package/server/resource-routes.ts +19 -4
- package/server/room-join.ts +1 -1
- package/server/room-payload.ts +1 -1
- package/server/sw.js +3 -0
- package/server/wake.ts +1 -1
- package/src/README.md +4 -2
- package/src/config/file-config.ts +294 -0
- package/src/rooms/resource-read.ts +2 -1
- package/src/rooms/resource-upload.ts +6 -3
- package/src/rooms/resources.ts +7 -2
- package/src/runtime/watch.ts +13 -8
- package/src/state/receiver-state.ts +3 -5
- package/src/state/sqlite-state.ts +11 -7
- package/src/state/sqlite.ts +80 -0
- package/src/state/store.ts +1 -0
- package/src/tui/app.mjs +417 -0
- package/src/tui/config.mjs +194 -0
- package/src/tui/http.mjs +103 -0
- package/src/tui/main.mjs +176 -0
- package/src/tui/messages.mjs +67 -0
- package/src/tui/palette-app.mjs +265 -0
- package/src/tui/palette.mjs +101 -0
- package/src/tui/session.mjs +40 -0
- package/src/tui/ui-content.mjs +426 -0
- package/src/tui/ui-theme.mjs +60 -0
- package/src/tui/ui-view.mjs +452 -0
- package/src/tui/ui.mjs +262 -0
- package/src/tests/announcement-board.ts +0 -186
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
// Pure command palette logic: command lists, scoped client-side search,
|
|
2
|
+
// ranking and snippets. No I/O lives here so it is easy to test.
|
|
3
|
+
|
|
4
|
+
const SEARCH_LIMIT = 50;
|
|
5
|
+
|
|
6
|
+
export function filterCommands(commands, query) {
|
|
7
|
+
const needle = String(query ?? "").trim().toLowerCase();
|
|
8
|
+
if (!needle) return commands.slice();
|
|
9
|
+
return commands.filter((command) => command.label.toLowerCase().includes(needle));
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** Commands shown when the palette query is empty. */
|
|
13
|
+
export function buildCommands({ rooms = [], current = null, joined = false, help = false } = {}) {
|
|
14
|
+
const commands = [
|
|
15
|
+
{ id: "refresh", label: "Refresh rooms" },
|
|
16
|
+
{ id: "new-room", label: "New room..." },
|
|
17
|
+
];
|
|
18
|
+
for (const room of rooms) {
|
|
19
|
+
if (!room.joined) commands.push({ id: `join:${room.id}`, label: `Join #${room.name}`, roomId: room.id });
|
|
20
|
+
}
|
|
21
|
+
if (current && joined) commands.push({ id: "leave-room", label: `Leave #${current.name}` });
|
|
22
|
+
commands.push({ id: "toggle-help", label: help ? "Hide keyboard help" : "Show keyboard help" });
|
|
23
|
+
for (const room of rooms) {
|
|
24
|
+
if (room.joined) commands.push({ id: `switch:${room.id}`, label: `Switch to #${room.name}`, roomId: room.id });
|
|
25
|
+
}
|
|
26
|
+
commands.push({ id: "search-room", label: "Search this room" });
|
|
27
|
+
commands.push({ id: "search-all", label: "Search all joined rooms" });
|
|
28
|
+
return commands;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function nextScope(scope) {
|
|
32
|
+
return scope === "all" ? "room" : "all";
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Pick the message sources for a scope from the local cache. */
|
|
36
|
+
export function scopeSources({ rooms = [], current = null, scope = "room", cache = new Map() } = {}) {
|
|
37
|
+
const messagesOf = (id) => {
|
|
38
|
+
const value = cache instanceof Map ? cache.get(id) : cache?.[id];
|
|
39
|
+
return Array.isArray(value) ? value : [];
|
|
40
|
+
};
|
|
41
|
+
if (scope !== "all") {
|
|
42
|
+
return current ? [{ roomId: current.id, roomName: current.name, messages: messagesOf(current.id) }] : [];
|
|
43
|
+
}
|
|
44
|
+
return rooms
|
|
45
|
+
.filter((room) => room.joined)
|
|
46
|
+
.map((room) => ({ roomId: room.id, roomName: room.name, messages: messagesOf(room.id) }));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function stamp(message) {
|
|
50
|
+
const value = message?.createdAt;
|
|
51
|
+
if (typeof value === "number" && Number.isFinite(value)) return value;
|
|
52
|
+
const parsed = Date.parse(String(value ?? ""));
|
|
53
|
+
return Number.isFinite(parsed) ? parsed : 0;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function scoreOf(text, needle, index) {
|
|
57
|
+
let score = 0;
|
|
58
|
+
let from = 0;
|
|
59
|
+
let hits = 0;
|
|
60
|
+
while ((from = text.indexOf(needle, from)) !== -1) {
|
|
61
|
+
hits += 1;
|
|
62
|
+
from += needle.length;
|
|
63
|
+
}
|
|
64
|
+
score += hits * 10;
|
|
65
|
+
if (index === 0) score += 5;
|
|
66
|
+
else if (/[\s.,!?;:]/.test(text[index - 1])) score += 3;
|
|
67
|
+
return score;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function snippetOf(text, index, length, radius = 24) {
|
|
71
|
+
const plain = String(text).replace(/\s+/g, " ").trim();
|
|
72
|
+
const start = Math.max(0, index - radius);
|
|
73
|
+
const end = Math.min(plain.length, index + length + radius);
|
|
74
|
+
return `${start > 0 ? "…" : ""}${plain.slice(start, end)}${end < plain.length ? "…" : ""}`;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Search loaded messages. Recalled messages are never searchable. */
|
|
78
|
+
export function searchMessages(query, sources, { limit = SEARCH_LIMIT } = {}) {
|
|
79
|
+
const needle = String(query ?? "").trim().toLowerCase();
|
|
80
|
+
if (!needle) return [];
|
|
81
|
+
const results = [];
|
|
82
|
+
for (const source of sources) {
|
|
83
|
+
for (const message of source.messages ?? []) {
|
|
84
|
+
if (!message || message.recalledAt) continue;
|
|
85
|
+
const text = String(message.text ?? "").replace(/\s+/g, " ").trim();
|
|
86
|
+
if (!text || text === "[recalled]") continue;
|
|
87
|
+
const lower = text.toLowerCase();
|
|
88
|
+
const index = lower.indexOf(needle);
|
|
89
|
+
if (index < 0) continue;
|
|
90
|
+
results.push({
|
|
91
|
+
roomId: source.roomId,
|
|
92
|
+
roomName: source.roomName,
|
|
93
|
+
message,
|
|
94
|
+
snippet: snippetOf(text, index, needle.length),
|
|
95
|
+
score: scoreOf(lower, needle, index),
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
results.sort((a, b) => b.score - a.score || stamp(b.message) - stamp(a.message));
|
|
100
|
+
return results.slice(0, limit);
|
|
101
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// Stable per-install session identity for the TUI. The file is written
|
|
2
|
+
// atomically so a crash cannot leave a half-written session behind.
|
|
3
|
+
import { randomUUID } from "node:crypto";
|
|
4
|
+
import { mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
|
|
7
|
+
const DEFAULT_NAME = "chatroom-tui";
|
|
8
|
+
|
|
9
|
+
function readSession(file) {
|
|
10
|
+
try {
|
|
11
|
+
const data = JSON.parse(readFileSync(file, "utf8"));
|
|
12
|
+
if (!data || typeof data !== "object" || Array.isArray(data)) return null;
|
|
13
|
+
if (typeof data.sessionID !== "string" || !data.sessionID) return null;
|
|
14
|
+
return {
|
|
15
|
+
sessionID: data.sessionID,
|
|
16
|
+
name: typeof data.name === "string" && data.name ? data.name : undefined,
|
|
17
|
+
};
|
|
18
|
+
} catch {
|
|
19
|
+
return null; // A corrupt file starts a fresh session instead of crashing.
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function saveSession(file, session) {
|
|
24
|
+
mkdirSync(path.dirname(file), { recursive: true });
|
|
25
|
+
const temp = `${file}.${process.pid}.${randomUUID()}.tmp`;
|
|
26
|
+
const data = { sessionID: session.sessionID, name: session.name };
|
|
27
|
+
writeFileSync(temp, `${JSON.stringify(data)}\n`, { mode: 0o600 });
|
|
28
|
+
renameSync(temp, file);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function loadSession(file, fallbackName = DEFAULT_NAME) {
|
|
32
|
+
const stored = readSession(file);
|
|
33
|
+
const wanted = typeof fallbackName === "string" ? fallbackName.trim().slice(0, 64) : "";
|
|
34
|
+
const session = {
|
|
35
|
+
sessionID: stored?.sessionID ?? randomUUID(),
|
|
36
|
+
name: wanted || stored?.name || DEFAULT_NAME,
|
|
37
|
+
};
|
|
38
|
+
if (!stored || stored.name !== session.name) saveSession(file, session);
|
|
39
|
+
return session;
|
|
40
|
+
}
|
|
@@ -0,0 +1,426 @@
|
|
|
1
|
+
// State-to-renderable syncing for the OpenTUI view. These functions rebuild
|
|
2
|
+
// message and room rows only when their signature changes.
|
|
3
|
+
import {
|
|
4
|
+
BoxRenderable,
|
|
5
|
+
StyledText,
|
|
6
|
+
TextRenderable,
|
|
7
|
+
bold,
|
|
8
|
+
dim,
|
|
9
|
+
fg,
|
|
10
|
+
italic,
|
|
11
|
+
} from "@opentui/core";
|
|
12
|
+
import { formatMessage, messageStamp, sanitizeText } from "./messages.mjs";
|
|
13
|
+
import { connectionStyle } from "./ui-theme.mjs";
|
|
14
|
+
|
|
15
|
+
const clear = (container) => {
|
|
16
|
+
for (const child of container.getChildren()) container.remove(child.id);
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const plural = (count, word) => `${count} ${word}${count === 1 ? "" : "s"}`;
|
|
20
|
+
|
|
21
|
+
export function syncLayout(ui) {
|
|
22
|
+
const { renderer, theme } = ui;
|
|
23
|
+
const narrow = renderer.width < 70;
|
|
24
|
+
const showRooms = !narrow;
|
|
25
|
+
ui.roomsPane.visible = showRooms;
|
|
26
|
+
ui.roomsPane.width = showRooms ? Math.min(30, Math.max(22, Math.floor(renderer.width * 0.3))) : 0;
|
|
27
|
+
ui.roomsPane.borderColor = theme.border;
|
|
28
|
+
ui.headerBox.borderColor = theme.border;
|
|
29
|
+
ui.headerBox.paddingX = narrow ? 1 : 2;
|
|
30
|
+
ui.statusBox.paddingX = narrow ? 1 : 2;
|
|
31
|
+
ui.statusBox.borderColor = theme.border;
|
|
32
|
+
ui.chatPane.paddingX = narrow ? 1 : 2;
|
|
33
|
+
ui.composer.marginX = narrow ? 0 : 1;
|
|
34
|
+
ui.composer.height = renderer.height < 18 ? 4 : 5;
|
|
35
|
+
ui.textarea.height = ui.composer.height - 2;
|
|
36
|
+
ui.prompt.height = ui.textarea.height;
|
|
37
|
+
ui.statusHints.visible = !narrow;
|
|
38
|
+
ui.paletteBox.left = narrow ? 0 : 1;
|
|
39
|
+
ui.paletteBox.right = narrow ? 0 : 1;
|
|
40
|
+
ui.paletteBox.bottom = narrow ? 0 : 1;
|
|
41
|
+
ui.paletteBox.height = Math.min(16, Math.max(9, Math.floor(renderer.height / 2) + 1));
|
|
42
|
+
ui.helpBox.left = narrow ? 0 : 1;
|
|
43
|
+
ui.helpBox.right = narrow ? 0 : 1;
|
|
44
|
+
ui.helpBox.bottom = narrow ? 0 : 1;
|
|
45
|
+
ui.helpBox.height = Math.min(13, Math.max(8, renderer.height - 4));
|
|
46
|
+
ui.helpBody.gap = narrow ? 1 : 4;
|
|
47
|
+
ui.helpBody.paddingX = narrow ? 1 : 2;
|
|
48
|
+
ui.roomsTitle.fg = theme.muted;
|
|
49
|
+
ui.roomsCount.fg = theme.muted;
|
|
50
|
+
ui.chatMeta.fg = theme.muted;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function syncHeader(ui, state) {
|
|
54
|
+
const { theme } = ui;
|
|
55
|
+
const narrow = ui.renderer.width < 70;
|
|
56
|
+
const left = [bold(fg(theme.accent)("opencode-chat-room"))];
|
|
57
|
+
if (!narrow) {
|
|
58
|
+
left.push(fg(theme.muted)(" · "));
|
|
59
|
+
left.push(fg(theme.muted)(sanitizeText(state.host || "server")));
|
|
60
|
+
}
|
|
61
|
+
ui.headerLeft.content = new StyledText(left);
|
|
62
|
+
|
|
63
|
+
const status = connectionStyle(theme, state.connection);
|
|
64
|
+
const right = [];
|
|
65
|
+
right.push(fg(theme.text)(sanitizeText(state.participant || "?")), fg(theme.muted)(" "));
|
|
66
|
+
right.push(fg(status.color)(`${status.dot} ${status.label}`));
|
|
67
|
+
ui.headerRight.content = new StyledText(right);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function roomLabel(room, pendingID, theme, active) {
|
|
71
|
+
const pending = pendingID === room.id;
|
|
72
|
+
const marker = pending ? "?" : room.joined ? "●" : "○";
|
|
73
|
+
const markerColor = active ? theme.selectedFg : pending ? theme.warning : room.joined ? theme.success : theme.muted;
|
|
74
|
+
const nameColor = active ? theme.selectedFg : theme.text;
|
|
75
|
+
return new StyledText([
|
|
76
|
+
fg(markerColor)(`${marker} `),
|
|
77
|
+
fg(nameColor)(bold(sanitizeText(room.name))),
|
|
78
|
+
]);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function updateRoomStyles(ui, state) {
|
|
82
|
+
const selectedID = state.rooms[state.selected]?.id;
|
|
83
|
+
for (const [id, entry] of ui.roomRows) {
|
|
84
|
+
const room = state.rooms.find((item) => item.id === id);
|
|
85
|
+
if (!room) continue;
|
|
86
|
+
const active = id === selectedID;
|
|
87
|
+
const hovered = id === ui.hoverRoomId;
|
|
88
|
+
entry.row.backgroundColor = active ? ui.theme.selectedBg : hovered ? ui.theme.raised : "transparent";
|
|
89
|
+
entry.label.content = roomLabel(room, state.pending?.roomId, ui.theme, active);
|
|
90
|
+
entry.count.fg = active ? ui.theme.selectedFg : ui.theme.muted;
|
|
91
|
+
}
|
|
92
|
+
if (selectedID && selectedID !== ui.selectedRoomID) {
|
|
93
|
+
ui.selectedRoomID = selectedID;
|
|
94
|
+
ui.pendingRoomScroll = `room-${selectedID}`;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function syncRooms(ui, state) {
|
|
99
|
+
const pendingID = state.pending?.roomId;
|
|
100
|
+
const signature = `${ui.theme.mode}|${pendingID ?? ""}|${state.rooms
|
|
101
|
+
.map((room) => `${room.id}:${room.name}:${room.joined}:${room.participants.length}`)
|
|
102
|
+
.join("|")}`;
|
|
103
|
+
ui.roomsCount.content = state.rooms.length ? String(state.rooms.length) : "";
|
|
104
|
+
if (signature !== ui.roomSignature) {
|
|
105
|
+
ui.roomSignature = signature;
|
|
106
|
+
clear(ui.roomContent);
|
|
107
|
+
ui.roomRows.clear();
|
|
108
|
+
if (!state.rooms.length) {
|
|
109
|
+
ui.roomContent.add(new TextRenderable(ui.renderer, {
|
|
110
|
+
id: "room-empty",
|
|
111
|
+
height: 1,
|
|
112
|
+
wrapMode: "none",
|
|
113
|
+
fg: ui.theme.muted,
|
|
114
|
+
paddingX: 1,
|
|
115
|
+
content: "No rooms yet",
|
|
116
|
+
}));
|
|
117
|
+
}
|
|
118
|
+
for (const [index, room] of state.rooms.entries()) {
|
|
119
|
+
const row = new BoxRenderable(ui.renderer, {
|
|
120
|
+
id: `room-${room.id}`,
|
|
121
|
+
height: 1,
|
|
122
|
+
flexDirection: "row",
|
|
123
|
+
gap: 1,
|
|
124
|
+
paddingX: 1,
|
|
125
|
+
});
|
|
126
|
+
const label = new TextRenderable(ui.renderer, {
|
|
127
|
+
id: `room-label-${room.id}`,
|
|
128
|
+
flexGrow: 1,
|
|
129
|
+
height: 1,
|
|
130
|
+
wrapMode: "none",
|
|
131
|
+
truncate: true,
|
|
132
|
+
content: roomLabel(room, pendingID, ui.theme, false),
|
|
133
|
+
});
|
|
134
|
+
const count = new TextRenderable(ui.renderer, {
|
|
135
|
+
id: `room-count-${room.id}`,
|
|
136
|
+
flexShrink: 0,
|
|
137
|
+
height: 1,
|
|
138
|
+
wrapMode: "none",
|
|
139
|
+
fg: ui.theme.muted,
|
|
140
|
+
content: String(room.participants.length),
|
|
141
|
+
});
|
|
142
|
+
row.add(label);
|
|
143
|
+
row.add(count);
|
|
144
|
+
row.onMouseOver = () => {
|
|
145
|
+
ui.hoverRoomId = room.id;
|
|
146
|
+
updateRoomStyles(ui, state);
|
|
147
|
+
ui.renderer.requestRender();
|
|
148
|
+
};
|
|
149
|
+
row.onMouseOut = () => {
|
|
150
|
+
if (ui.hoverRoomId === room.id) ui.hoverRoomId = null;
|
|
151
|
+
updateRoomStyles(ui, state);
|
|
152
|
+
ui.renderer.requestRender();
|
|
153
|
+
};
|
|
154
|
+
row.onMouseUp = (event) => {
|
|
155
|
+
if (event.button !== 0) return;
|
|
156
|
+
ui.app.selectRoom(index);
|
|
157
|
+
void ui.app.openRoom(index);
|
|
158
|
+
};
|
|
159
|
+
ui.roomContent.add(row);
|
|
160
|
+
ui.roomRows.set(room.id, { row, label, count });
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
updateRoomStyles(ui, state);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function messageChunks(ui, message, info) {
|
|
167
|
+
const { theme } = ui;
|
|
168
|
+
const chunks = [];
|
|
169
|
+
const highlighted = message.id === ui.app.state.highlight;
|
|
170
|
+
if (highlighted) chunks.push(bold(fg(theme.accent)("▸ ")));
|
|
171
|
+
if (info.question) chunks.push(bold(fg(theme.warning)("? ")));
|
|
172
|
+
chunks.push(dim(`${info.time} `));
|
|
173
|
+
const own = message.senderId === ui.app.state.participant;
|
|
174
|
+
chunks.push(bold(fg(own ? theme.accent : theme.text)(info.sender)));
|
|
175
|
+
return chunks;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function messageBody(ui, info) {
|
|
179
|
+
if (info.recalled) return new StyledText([dim(italic(info.body))]);
|
|
180
|
+
return new StyledText([fg(ui.theme.text)(info.body)]);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export function syncMessages(ui, state) {
|
|
184
|
+
const { current, pending, joined, messages, highlight } = state;
|
|
185
|
+
const messageKey = messages
|
|
186
|
+
.map((message) => `${message.id}:${message.recalledAt ? 1 : 0}:${message.isQuestion ? 1 : 0}:${message.text?.length ?? 0}`)
|
|
187
|
+
.join("|");
|
|
188
|
+
const signature = `${ui.theme.mode}:${current?.id ?? ""}:${joined}:${pending?.roomId ?? ""}:${pending?.message ?? ""}:${highlight ?? ""}:${messageKey}`;
|
|
189
|
+
|
|
190
|
+
if (current) {
|
|
191
|
+
ui.chatTitle.content = new StyledText([bold(fg(ui.theme.accent)(`# ${sanitizeText(current.name)}`))]);
|
|
192
|
+
ui.chatMeta.content = plural(current.participants.length, "member");
|
|
193
|
+
ui.chatMeta.fg = ui.theme.muted;
|
|
194
|
+
} else {
|
|
195
|
+
ui.chatTitle.content = new StyledText([fg(ui.theme.muted)("# no room selected")]);
|
|
196
|
+
ui.chatMeta.content = "";
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
if (signature === ui.messageSignature) return;
|
|
200
|
+
const previousTop = ui.scrollBox.scrollTop;
|
|
201
|
+
const wasAtBottom = ui.scrollBox.scrollHeight - ui.scrollBox.scrollTop - ui.scrollBox.viewport.height < 2;
|
|
202
|
+
ui.messageSignature = signature;
|
|
203
|
+
|
|
204
|
+
clear(ui.messageContent);
|
|
205
|
+
ui.pendingScrollTo = null;
|
|
206
|
+
ui.pendingBottom = false;
|
|
207
|
+
|
|
208
|
+
if (pending && pending.roomId === current?.id) {
|
|
209
|
+
addNotice(ui, "pending", sanitizeText(pending.message), ui.theme.warning);
|
|
210
|
+
addNotice(ui, "pending-wait", "Waiting for approval...", ui.theme.muted);
|
|
211
|
+
} else if (!current) {
|
|
212
|
+
addNotice(ui, "empty", "Select a room and press Enter.", ui.theme.muted);
|
|
213
|
+
} else if (!joined) {
|
|
214
|
+
addNotice(ui, "empty", "Not joined · press Enter to join again.", ui.theme.muted);
|
|
215
|
+
} else if (!messages.length) {
|
|
216
|
+
addNotice(ui, "empty", "No messages yet. Say hello.", ui.theme.muted);
|
|
217
|
+
} else {
|
|
218
|
+
messages.forEach((message, index) => {
|
|
219
|
+
const info = formatMessage(message, current);
|
|
220
|
+
const previous = messages[index - 1];
|
|
221
|
+
const gap = previous
|
|
222
|
+
? previous.senderId !== message.senderId || messageStamp(message) - messageStamp(previous) > 300000
|
|
223
|
+
: false;
|
|
224
|
+
const highlighted = message.id === highlight;
|
|
225
|
+
const block = new BoxRenderable(ui.renderer, {
|
|
226
|
+
id: `message-${message.id}`,
|
|
227
|
+
width: "100%",
|
|
228
|
+
flexDirection: "column",
|
|
229
|
+
flexShrink: 0,
|
|
230
|
+
marginTop: index === 0 || gap ? 1 : 0,
|
|
231
|
+
});
|
|
232
|
+
block.add(new TextRenderable(ui.renderer, {
|
|
233
|
+
id: `message-head-${message.id}`,
|
|
234
|
+
width: "100%",
|
|
235
|
+
height: 1,
|
|
236
|
+
wrapMode: "none",
|
|
237
|
+
truncate: true,
|
|
238
|
+
content: new StyledText(messageChunks(ui, message, info)),
|
|
239
|
+
}));
|
|
240
|
+
block.add(new TextRenderable(ui.renderer, {
|
|
241
|
+
id: `message-body-${message.id}`,
|
|
242
|
+
width: "100%",
|
|
243
|
+
flexShrink: 0,
|
|
244
|
+
wrapMode: "word",
|
|
245
|
+
marginLeft: 2,
|
|
246
|
+
paddingRight: 1,
|
|
247
|
+
bg: highlighted ? ui.theme.raised : undefined,
|
|
248
|
+
content: messageBody(ui, info),
|
|
249
|
+
}));
|
|
250
|
+
ui.messageContent.add(block);
|
|
251
|
+
});
|
|
252
|
+
if (highlight) ui.pendingScrollTo = `message-${highlight}`;
|
|
253
|
+
else if (wasAtBottom) ui.pendingBottom = true;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
if (!ui.pendingScrollTo && !ui.pendingBottom && previousTop > 0) {
|
|
257
|
+
ui.scrollBox.scrollTop = previousTop;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function addNotice(ui, id, text, color) {
|
|
262
|
+
ui.messageContent.add(new TextRenderable(ui.renderer, {
|
|
263
|
+
id: `message-${id}`,
|
|
264
|
+
width: "100%",
|
|
265
|
+
flexShrink: 0,
|
|
266
|
+
wrapMode: "char",
|
|
267
|
+
content: new StyledText([fg(color)(sanitizeText(text))]),
|
|
268
|
+
}));
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
export function syncComposer(ui, state) {
|
|
272
|
+
const draft = String(state.draft ?? "");
|
|
273
|
+
ui.textarea.placeholderColor = ui.theme.faint;
|
|
274
|
+
ui.textarea.textColor = ui.theme.text;
|
|
275
|
+
ui.textarea.focusedTextColor = ui.theme.text;
|
|
276
|
+
ui.prompt.fg = ui.theme.accent;
|
|
277
|
+
if (ui.textarea.plainText === draft) return;
|
|
278
|
+
ui.syncingComposer = true;
|
|
279
|
+
try {
|
|
280
|
+
ui.textarea.setText(draft);
|
|
281
|
+
// setText can leave the caret at the start. A character typed while the
|
|
282
|
+
// room list was focused would then be followed by the rest before it.
|
|
283
|
+
ui.textarea.cursorOffset = draft.length;
|
|
284
|
+
} finally {
|
|
285
|
+
ui.syncingComposer = false;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
export function syncStatus(ui, state) {
|
|
290
|
+
const { theme } = ui;
|
|
291
|
+
const text = state.status || "Ready";
|
|
292
|
+
ui.statusLine.content = sanitizeText(text);
|
|
293
|
+
ui.statusLine.fg = state.statusKind === "error"
|
|
294
|
+
? theme.error
|
|
295
|
+
: state.statusKind === "success"
|
|
296
|
+
? theme.success
|
|
297
|
+
: theme.muted;
|
|
298
|
+
const status = connectionStyle(theme, state.connection);
|
|
299
|
+
ui.statusHints.content = ui.renderer.width < 70
|
|
300
|
+
? "? help"
|
|
301
|
+
: `${status.dot} ${status.label} · Tab focus · Alt+Space palette · ? help`;
|
|
302
|
+
ui.statusHints.fg = theme.muted;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
export function syncFocus(ui, state) {
|
|
306
|
+
const target = state.palette ? "palette" : state.help ? "help" : state.focus === "composer" ? "composer" : "rooms";
|
|
307
|
+
if (target !== ui.focusKey) {
|
|
308
|
+
if (ui.focusKey === "composer") ui.textarea.blur();
|
|
309
|
+
if (ui.focusKey === "palette") ui.paletteInput.blur();
|
|
310
|
+
if (target === "composer") ui.textarea.focus();
|
|
311
|
+
if (target === "palette") ui.paletteInput.focus();
|
|
312
|
+
ui.focusKey = target;
|
|
313
|
+
}
|
|
314
|
+
const accent = target === "composer";
|
|
315
|
+
ui.composer.borderColor = accent ? ui.theme.accent : ui.theme.border;
|
|
316
|
+
ui.prompt.fg = accent ? ui.theme.accent : ui.theme.muted;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
export function syncPalette(ui, state) {
|
|
320
|
+
const { theme, paletteBox } = ui;
|
|
321
|
+
const palette = state.palette;
|
|
322
|
+
paletteBox.visible = Boolean(palette);
|
|
323
|
+
if (!palette) return;
|
|
324
|
+
|
|
325
|
+
paletteBox.backgroundColor = theme.surface;
|
|
326
|
+
paletteBox.borderColor = theme.accent;
|
|
327
|
+
ui.paletteBar.backgroundColor = theme.raised;
|
|
328
|
+
ui.paletteTitle.fg = theme.text;
|
|
329
|
+
ui.paletteScope.fg = theme.muted;
|
|
330
|
+
ui.palettePrompt.fg = theme.accent;
|
|
331
|
+
ui.paletteInput.placeholderColor = theme.faint;
|
|
332
|
+
ui.paletteInput.textColor = theme.text;
|
|
333
|
+
ui.paletteInput.focusedTextColor = theme.text;
|
|
334
|
+
ui.paletteList.textColor = theme.text;
|
|
335
|
+
ui.paletteList.focusedTextColor = theme.text;
|
|
336
|
+
ui.paletteList.selectedBackgroundColor = theme.selectedBg;
|
|
337
|
+
ui.paletteList.selectedTextColor = theme.selectedFg;
|
|
338
|
+
|
|
339
|
+
const scope = palette.scope === "all" ? "all joined rooms" : "this room";
|
|
340
|
+
ui.paletteTitle.content = palette.mode === "prompt" ? "New room" : palette.mode === "search" ? "Search messages" : "Palette";
|
|
341
|
+
ui.paletteScope.content = palette.mode === "prompt" ? "create" : palette.mode === "search" ? scope : "commands";
|
|
342
|
+
ui.paletteInput.placeholder = palette.mode === "prompt"
|
|
343
|
+
? "Room name"
|
|
344
|
+
: palette.mode === "search"
|
|
345
|
+
? "Type to search messages"
|
|
346
|
+
: "Type to search or run a command";
|
|
347
|
+
|
|
348
|
+
const value = palette.mode === "prompt" ? palette.prompt?.value ?? "" : palette.query;
|
|
349
|
+
if (ui.paletteInput.value !== value) {
|
|
350
|
+
ui.syncingPalette = true;
|
|
351
|
+
try {
|
|
352
|
+
ui.paletteInput.value = value;
|
|
353
|
+
} finally {
|
|
354
|
+
ui.syncingPalette = false;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
let options;
|
|
359
|
+
if (palette.mode === "commands") {
|
|
360
|
+
options = ui.app.paletteCommands().map((command) => ({ name: command.label, description: "", value: command.id }));
|
|
361
|
+
} else if (palette.mode === "prompt") {
|
|
362
|
+
options = [{ name: "Enter creates the room", description: "", value: "" }];
|
|
363
|
+
} else if (!palette.results.length) {
|
|
364
|
+
options = [{ name: palette.searching ? "Searching..." : "No matches", description: "", value: "" }];
|
|
365
|
+
} else {
|
|
366
|
+
options = palette.results.map((result) => {
|
|
367
|
+
const info = formatMessage(result.message, null);
|
|
368
|
+
return {
|
|
369
|
+
name: `${sanitizeText(result.roomName)} ${info.time} ${info.sender} ${sanitizeText(result.snippet)}`,
|
|
370
|
+
description: "",
|
|
371
|
+
value: result.message.id,
|
|
372
|
+
};
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
ui.paletteList.options = options;
|
|
376
|
+
ui.paletteList.setSelectedIndex(Math.min(palette.selected, Math.max(0, options.length - 1)));
|
|
377
|
+
|
|
378
|
+
const footer = palette.error
|
|
379
|
+
|| (palette.mode === "prompt"
|
|
380
|
+
? "Enter create · Esc close"
|
|
381
|
+
: palette.mode === "search"
|
|
382
|
+
? `↑↓ select · Enter open · Tab: ${palette.scope === "all" ? "this room" : "all rooms"} · Esc close${palette.searching ? " · searching..." : ""}`
|
|
383
|
+
: "↑↓ select · Enter run · Esc close");
|
|
384
|
+
ui.paletteHint.content = ` ${sanitizeText(footer)}`;
|
|
385
|
+
ui.paletteHint.fg = palette.error ? theme.error : theme.muted;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
function shortcutChunks(title, rows, theme) {
|
|
389
|
+
const chunks = [bold(fg(theme.accent)(`${title}\n`))];
|
|
390
|
+
for (const [key, label] of rows) {
|
|
391
|
+
chunks.push(fg(theme.accent)(key.padEnd(12)));
|
|
392
|
+
chunks.push(fg(theme.text)(`${label}\n`));
|
|
393
|
+
}
|
|
394
|
+
return chunks;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
export function syncHelp(ui, state) {
|
|
398
|
+
const show = Boolean(state.help) && !state.palette;
|
|
399
|
+
ui.helpBox.visible = show;
|
|
400
|
+
if (!show) return;
|
|
401
|
+
ui.helpBox.borderColor = ui.theme.border;
|
|
402
|
+
ui.helpBox.backgroundColor = ui.theme.surface;
|
|
403
|
+
ui.helpBar.backgroundColor = ui.theme.raised;
|
|
404
|
+
ui.helpTitle.fg = ui.theme.text;
|
|
405
|
+
ui.helpClose.fg = ui.theme.muted;
|
|
406
|
+
ui.helpFooter.fg = ui.theme.muted;
|
|
407
|
+
const signature = ui.theme.mode;
|
|
408
|
+
if (signature === ui.helpSignature) return;
|
|
409
|
+
ui.helpSignature = signature;
|
|
410
|
+
ui.helpLeft.content = new StyledText(shortcutChunks("Navigation", [
|
|
411
|
+
["↑ / ↓", "select room"],
|
|
412
|
+
["Enter", "open / join"],
|
|
413
|
+
["Tab", "focus composer"],
|
|
414
|
+
["PgUp / PgDn", "scroll messages"],
|
|
415
|
+
["Alt+Home", "first message"],
|
|
416
|
+
["Alt+End", "last message"],
|
|
417
|
+
], ui.theme));
|
|
418
|
+
ui.helpRight.content = new StyledText(shortcutChunks("Composer", [
|
|
419
|
+
["Enter", "send message"],
|
|
420
|
+
["Ctrl+J", "insert newline"],
|
|
421
|
+
["Esc", "clear / blur"],
|
|
422
|
+
["Alt+Space", "palette / search"],
|
|
423
|
+
["Ctrl+R", "refresh rooms"],
|
|
424
|
+
["Ctrl+C", "quit"],
|
|
425
|
+
], ui.theme));
|
|
426
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// Semantic colors for the OpenTUI view. Dark and light variants follow the
|
|
2
|
+
// same roles OpenCode uses: background surface, text, muted metadata, border,
|
|
3
|
+
// selection, accent and feedback colors.
|
|
4
|
+
const PALETTES = {
|
|
5
|
+
dark: {
|
|
6
|
+
surface: "#111827",
|
|
7
|
+
raised: "#1f2937",
|
|
8
|
+
text: "#e5e7eb",
|
|
9
|
+
muted: "#9ca3af",
|
|
10
|
+
faint: "#6b7280",
|
|
11
|
+
border: "#374151",
|
|
12
|
+
accent: "#22d3ee",
|
|
13
|
+
selectedBg: "#164e63",
|
|
14
|
+
selectedFg: "#ecfeff",
|
|
15
|
+
error: "#f87171",
|
|
16
|
+
success: "#4ade80",
|
|
17
|
+
warning: "#fbbf24",
|
|
18
|
+
},
|
|
19
|
+
light: {
|
|
20
|
+
surface: "#f8fafc",
|
|
21
|
+
raised: "#e2e8f0",
|
|
22
|
+
text: "#111827",
|
|
23
|
+
muted: "#64748b",
|
|
24
|
+
faint: "#94a3b8",
|
|
25
|
+
border: "#cbd5e1",
|
|
26
|
+
accent: "#0e7490",
|
|
27
|
+
selectedBg: "#0e7490",
|
|
28
|
+
selectedFg: "#ecfeff",
|
|
29
|
+
error: "#b91c1c",
|
|
30
|
+
success: "#15803d",
|
|
31
|
+
warning: "#b45309",
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export function themeFor(mode) {
|
|
36
|
+
return { ...(mode === "light" ? PALETTES.light : PALETTES.dark) };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function createTheme(renderer) {
|
|
40
|
+
const theme = themeFor(renderer.themeMode);
|
|
41
|
+
return {
|
|
42
|
+
theme,
|
|
43
|
+
mode: renderer.themeMode === "light" ? "light" : "dark",
|
|
44
|
+
set(mode) {
|
|
45
|
+
if (mode !== "light" && mode !== "dark") return false;
|
|
46
|
+
Object.assign(theme, themeFor(mode));
|
|
47
|
+
this.mode = mode;
|
|
48
|
+
return true;
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function connectionStyle(theme, connection) {
|
|
54
|
+
switch (connection) {
|
|
55
|
+
case "online": return { label: "connected", dot: "●", color: theme.success };
|
|
56
|
+
case "reconnecting": return { label: "reconnecting", dot: "◌", color: theme.warning };
|
|
57
|
+
case "offline": return { label: "offline", dot: "●", color: theme.error };
|
|
58
|
+
default: return { label: "connecting", dot: "○", color: theme.muted };
|
|
59
|
+
}
|
|
60
|
+
}
|