@alexsun-top/opencode-chat-room 0.6.1 → 0.6.3

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 CHANGED
@@ -1,5 +1,16 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.6.3 — 2026-11-15
4
+
5
+ - Keep a room pinned to the live bottom while images, diagrams and attachments finish loading. Late rows used to push the thread down by their full height, so a room opened at the bottom could end up above the newest message; a growth-triggered scroll also counted as the reader scrolling away, which stopped the pin for good. Only real user scrolling ends the pin now.
6
+
7
+ ## 0.6.2 — 2026-11-15
8
+
9
+ - Opening a room always lands on the live bottom, including when the previous visit ended mid-history. The saved-position and first-unread landing are gone; they reopened rooms above messages the reader had already seen.
10
+ - Treat a thread within 80px of the bottom as the bottom when saving the position, following new messages, and restoring layout after an image expands, matching the read and jump-to-latest thresholds.
11
+ - On phones, put the room name and the action buttons on separate header rows so a long title is no longer squeezed to a few characters.
12
+ - Give the Reminders button its bell icon on phones instead of an empty circle, and open every header panel below the header with 12px side margins so no panel runs off screen.
13
+
3
14
  ## 0.6.1 — 2026-11-14
4
15
 
5
16
  - Keep the thread pinned to the bottom when images finish loading after a room opens, without yanking a reader who scrolled up into history.
package/README.md CHANGED
@@ -17,6 +17,8 @@ Install [Bun](https://bun.sh), then add the plugin to `~/.config/opencode/openco
17
17
 
18
18
  Restart OpenCode. Ask an agent to create a room named `dev`, or enter `/room create dev`.
19
19
 
20
+ When upgrading an existing install, quit OpenCode first or use `opencode plugin add @alexsun-top/opencode-chat-room@<version>`. Editing the version in `opencode.json` while the host is running can leave an empty package directory under `~/.cache/opencode/npm/`, which makes the plugin fail to load. Delete that directory and run `opencode plugin add` again to recover.
21
+
20
22
  ## 2. Connect multiple devices
21
23
 
22
24
  On a machine reachable by the other devices, start the server. Replace `your-password` with a shared password:
package/README.zh-CN.md CHANGED
@@ -17,6 +17,8 @@
17
17
 
18
18
  重启 OpenCode。现在你可以让 Agent「创建一个叫 dev 的聊天室」,或输入 `/room create dev`。
19
19
 
20
+ 升级已有安装时,先退出 OpenCode,或使用 `opencode plugin add @alexsun-top/opencode-chat-room@<版本>`。在宿主运行中直接改 `opencode.json` 里的版本号,可能在 `~/.cache/opencode/npm/` 下留下空目录,导致插件加载失败。遇到时删掉该空目录,再执行一次 `opencode plugin add`。
21
+
20
22
  ## 2. 多台设备一起使用
21
23
 
22
24
  在一台可被其他设备访问的机器上启动服务器,把 `换成自己的密码` 换成共享密码:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alexsun-top/opencode-chat-room",
3
- "version": "0.6.1",
3
+ "version": "0.6.3",
4
4
  "description": "A chat-room plugin and central server for OpenCode.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -1,6 +1,5 @@
1
1
  import { LS } from "./state.js";
2
2
  import { copyTextToClipboard } from "./markdown.js";
3
- import { isUnreadMessage } from "./messages.js";
4
3
 
5
4
  export function initEvents(app) {
6
5
  const $ = app.$;
@@ -13,6 +12,29 @@ export function initEvents(app) {
13
12
  $("#msgInput").focus({ preventScroll: true });
14
13
  };
15
14
  document.addEventListener("click", () => requestAnimationFrame(() => app.focusComposer?.()));
15
+ // Mobile panels sit below the header, whose height depends on the title and locale.
16
+ const chatHead = document.querySelector("#chatView > .chat-head");
17
+ if (chatHead) {
18
+ const syncHeadBottom = () => document.documentElement.style.setProperty("--chat-head-bottom", `${Math.round(chatHead.getBoundingClientRect().bottom + 8)}px`);
19
+ new ResizeObserver(syncHeadBottom).observe(chatHead);
20
+ syncHeadBottom();
21
+ }
22
+ // Images, diagrams and attachments grow the list after the first paint and the browser
23
+ // answers with a scroll that looks like the reader moving. Subtract the growth: only a
24
+ // reader who was already at the bottom gets pinned back.
25
+ const messageList = $("#msgList");
26
+ const rememberListMetrics = () => {
27
+ const thread = $("#thread");
28
+ app.state.listMetrics = { height: messageList.offsetHeight, top: thread.scrollTop, client: thread.clientHeight };
29
+ };
30
+ new ResizeObserver(() => {
31
+ const thread = $("#thread"), previous = app.state.listMetrics, growth = messageList.offsetHeight - previous.height;
32
+ // Judge the position before this growth: a reader who was at the bottom stays there,
33
+ // a reader in history is left alone, and a viewport resize keeps following the anchor.
34
+ const wasAtBottom = previous.height - previous.top - previous.client <= 80;
35
+ if (growth > 0 && wasAtBottom) thread.scrollTop = thread.scrollHeight;
36
+ rememberListMetrics();
37
+ }).observe(messageList);
16
38
  app.showRestoreFailure = (message) => {
17
39
  document.documentElement.classList.add("session-restore");
18
40
  $("#joinView").hidden = $("#chatView").hidden = true;
@@ -28,7 +50,7 @@ export function initEvents(app) {
28
50
  const thread = $("#thread"), edge = thread.getBoundingClientRect(), top = edge.top + thread.clientTop;
29
51
  const anchor = [...$("#msgList").querySelectorAll("[data-msg-id]")].find((row) => row.getBoundingClientRect().bottom > top);
30
52
  readingPosition = { roomId: app.state.room?.id, messageId: anchor?.dataset.msgId, offset: anchor ? anchor.getBoundingClientRect().top - top : 0,
31
- atBottom: thread.scrollHeight - thread.scrollTop - thread.clientHeight <= 4 };
53
+ atBottom: thread.scrollHeight - thread.scrollTop - thread.clientHeight <= 80 };
32
54
  const guard = app.state.readAnchorGuard;
33
55
  let missing = guard?.roomId === readingPosition.roomId && guard.missing;
34
56
  // A failed restore keeps its anchor while the landing stays put; scrolling to a
@@ -65,29 +87,13 @@ export function initEvents(app) {
65
87
  layoutQueue = task.catch(() => {});
66
88
  return task;
67
89
  };
68
- app.restoreReadingPosition = (roomId) => {
69
- const thread = $("#thread"), saved = app.state.positions.get(roomId), entry = app.state.rooms.get(roomId);
70
- const atBottom = () => thread.scrollHeight - thread.scrollTop - thread.clientHeight <= 4;
71
- const align = (messageId, offset) => {
72
- const row = [...$("#msgList").querySelectorAll("[data-msg-id]")].find((item) => item.dataset.msgId === messageId);
73
- if (!row) return false;
74
- thread.scrollTop += row.getBoundingClientRect().top - (thread.getBoundingClientRect().top + thread.clientTop + offset);
75
- captureReadingPosition(); return true;
76
- };
77
- if (typeof saved === "number") { thread.scrollTop = saved; captureReadingPosition(); return { messageId: readingPosition?.messageId, atBottom: atBottom() }; }
78
- const readTs = entry?.readTs || app.state.lastReadTs.get(roomId) || 0;
79
- // Own messages never form an unread boundary; landing on them hides the newest activity.
80
- const boundary = readTs && entry?.room.messages.find((message) => message.createdAt > readTs && isUnreadMessage(message, app, entry.room, entry.name));
81
- // Leaving at the bottom and returning to new messages lands on the first unread;
82
- // a deliberate scroll position is restored exactly.
83
- if ((entry?.unreadIds.size || entry?.unreadOverflow) && saved?.atBottom !== false && boundary && align(boundary.id, 0)) return { messageId: boundary.id, atBottom: atBottom() };
84
- // Left at the bottom with nothing unread: land on the live bottom, not the
85
- // old anchor — late-loading cards can grow content below the saved row.
86
- if (saved?.atBottom === true && !entry?.unreadIds.size && !entry?.unreadOverflow) { thread.scrollTop = thread.scrollHeight; captureReadingPosition(); return { messageId: readingPosition?.messageId, atBottom: atBottom() }; }
87
- if (saved?.messageId && align(saved.messageId, Number(saved.offset) || 0)) return { messageId: saved.messageId, atBottom: atBottom() };
88
- if (boundary && align(boundary.id, 0)) return { messageId: boundary.id, atBottom: atBottom() };
89
- if (!saved && !boundary) return false;
90
- return saved?.messageId || readTs ? { messageId: saved?.messageId || null, missing: true } : false;
90
+ app.restoreReadingPosition = () => {
91
+ const thread = $("#thread");
92
+ // Entering a room always shows the newest messages. Restoring an old anchor
93
+ // leaves the reader above messages that already arrived or grew afterwards.
94
+ thread.scrollTop = thread.scrollHeight;
95
+ captureReadingPosition();
96
+ return { messageId: readingPosition?.messageId, atBottom: true };
91
97
  };
92
98
  function closeRoomDrawer(restore = true) {
93
99
  roomRail.classList.remove("drawer-open"); roomRail.inert = true; roomRail.setAttribute("aria-hidden", "true");
@@ -136,38 +142,10 @@ export function initEvents(app) {
136
142
  backdrop.addEventListener("click", () => closeRoomDrawer());
137
143
  $("#roomListChat").addEventListener("click", (event) => { if (roomRail.classList.contains("drawer-open") && event.target.closest("[data-room-id]")) closeRoomDrawer(); });
138
144
  const activateRoom = app.activateRoom;
139
- const pendingAnchorLoads = new Map();
140
- function cancelAnchorLoads() {
141
- for (const { controller } of pendingAnchorLoads.values()) controller.abort();
142
- pendingAnchorLoads.clear();
143
- }
144
- const showJoin = app.showJoin;
145
- app.showJoin = (...args) => { cancelAnchorLoads(); return showJoin(...args); };
146
- const removeJoinedRoom = app.removeJoinedRoom;
147
- app.removeJoinedRoom = (roomId, options) => {
148
- if (app.state.room?.id === roomId) cancelAnchorLoads();
149
- return removeJoinedRoom(roomId, options);
150
- };
151
- function activateNow(id, preserveRoomListGeneration = false) {
152
- activateRoom(id, preserveRoomListGeneration); captureReadingPosition();
153
- if (roomRail.classList.contains("drawer-open")) closeRoomDrawer();
154
- }
155
145
  app.activateRoom = (id, preserveRoomListGeneration = false) => {
156
- cancelAnchorLoads();
157
- const entry = app.state.rooms.get(id), saved = app.state.positions.get(id);
158
- const intent = ++app.state.navigationGeneration;
159
- if (!entry) return activateNow(id, preserveRoomListGeneration);
160
- const readTs = entry.readTs || app.state.lastReadTs.get(id) || 0;
161
- const needAnchor = saved?.messageId && !entry.room.messages.some((message) => message.id === saved.messageId);
162
- const latest = entry.room.messages.at(-1);
163
- const needBoundary = !saved?.messageId && readTs && (!latest || latest.createdAt > readTs && !entry.room.messages.some((message) => message.createdAt <= readTs));
164
- if (!needAnchor && !needBoundary) return activateNow(id, preserveRoomListGeneration);
165
- const controller = new AbortController();
166
- const task = loadAnchorHistory(id, entry, saved?.messageId, app, needBoundary ? readTs : 0, controller.signal).catch(() => {}).finally(() => {
167
- if (pendingAnchorLoads.get(id)?.controller === controller) pendingAnchorLoads.delete(id);
168
- if (intent === app.state.navigationGeneration) activateNow(id, preserveRoomListGeneration);
169
- });
170
- pendingAnchorLoads.set(id, { controller, task });
146
+ activateRoom(id, preserveRoomListGeneration);
147
+ captureReadingPosition();
148
+ if (roomRail.classList.contains("drawer-open")) closeRoomDrawer();
171
149
  };
172
150
  document.querySelectorAll(".locale-select").forEach((select) => {
173
151
  select.addEventListener("change", () => {
@@ -270,6 +248,7 @@ export function initEvents(app) {
270
248
  thread.addEventListener("scroll", () => {
271
249
  const adjusting = (app.state.resizeScrollAdjusting || app.state.layoutScrollAdjusting)
272
250
  && performance.now() > app.state.userScrollIntentUntil;
251
+ if (!adjusting) rememberListMetrics();
273
252
  if (!adjusting || performance.now() <= app.state.userScrollIntentUntil) captureReadingPosition();
274
253
  if (!adjusting && thread.scrollHeight - thread.scrollTop - thread.clientHeight < 80) void app.markActiveRoomRead(performance.now() <= app.state.userScrollIntentUntil);
275
254
  app.queueReceiptRefresh();
@@ -337,28 +316,6 @@ export function initEvents(app) {
337
316
  if (!$("#joinView").hidden) $("#inRoomId").focus();
338
317
  }
339
318
 
340
- async function loadAnchorHistory(roomId, entry, messageId, app, readTs = 0, signal) {
341
- let beforeSeq, reachedStart = false;
342
- const messages = [];
343
- while (!signal?.aborted) {
344
- const query = new URLSearchParams({ limit: "100" });
345
- if (beforeSeq) query.set("beforeSeq", String(beforeSeq));
346
- const result = await app.api(`/rooms/${encodeURIComponent(roomId)}/history?${query}`, { signal });
347
- if (signal?.aborted) return;
348
- messages.push(...(result.messages || []));
349
- const found = messageId ? messages.some((message) => message.id === messageId)
350
- : messages.some((message) => message.createdAt > readTs) && messages.some((message) => message.createdAt <= readTs);
351
- if (found || !result.hasMore || !result.nextBeforeSeq || result.nextBeforeSeq === beforeSeq) { reachedStart = !result.hasMore; break; }
352
- beforeSeq = result.nextBeforeSeq;
353
- }
354
- if (messageId ? !messages.some((message) => message.id === messageId) : !messages.some((message) => message.createdAt > readTs)) return;
355
- const merged = new Map([...entry.room.messages, ...messages].map((message) => [message.id, { ...message, roomId }]));
356
- entry.room.messages = [...merged.values()].sort((left, right) => left.createdAt - right.createdAt);
357
- // Keep the upward-pagination cursor aligned with what the anchor load fetched.
358
- if (typeof entry.room.messages[0]?.seq === "number") entry.historyBeforeSeq = entry.room.messages[0].seq;
359
- if (reachedStart) entry.historyComplete = true;
360
- }
361
-
362
319
  function saveAccessToken(button, app) {
363
320
  const input = document.getElementById(button.dataset.tokenSave);
364
321
  input.value = app.setToken(input.value);
@@ -244,7 +244,7 @@ export function initMessages(app) {
244
244
 
245
245
  app.renderMessages = (live = false) => {
246
246
  const thread = $("#thread"), list = $("#msgList"), messages = app.state.room.messages;
247
- const nearBottom = thread.scrollHeight - thread.scrollTop - thread.clientHeight <= 2;
247
+ const nearBottom = thread.scrollHeight - thread.scrollTop - thread.clientHeight <= 80;
248
248
  let skipped = 0;
249
249
  let anchorId = null, anchorOffset = 0, oldTop = thread.scrollTop;
250
250
  const viewport = thread.getBoundingClientRect();
@@ -307,18 +307,8 @@ export function initMessages(app) {
307
307
  if (anchor) thread.scrollTop += anchor.getBoundingClientRect().top - thread.getBoundingClientRect().top - anchorOffset;
308
308
  else thread.scrollTop = oldTop;
309
309
  }
310
- // A late-loading image below the viewport grows the thread after the scroll
311
- // settled. Re-pin only when the growth is exactly the gap the reader had, so
312
- // someone reading history is never yanked to the bottom.
313
- for (const img of list.querySelectorAll(".msg-text img:not([data-load-guard])")) {
314
- if (img.complete && img.naturalWidth) continue;
315
- img.dataset.loadGuard = "1";
316
- img.addEventListener("load", () => {
317
- if (!img.isConnected) return;
318
- const thread = $("#thread");
319
- if (thread.scrollHeight - thread.scrollTop - thread.clientHeight - img.getBoundingClientRect().height <= 80) thread.scrollTop = thread.scrollHeight;
320
- }, { once: true });
321
- }
310
+ // Late growth (images, diagrams, attachments) is handled by the list ResizeObserver:
311
+ // it re-pins the thread while the reader is following the newest message.
322
312
  app.queueReceiptRefresh?.();
323
313
  app.queueReceiptObservation?.();
324
314
  return { skipped };
@@ -115,7 +115,6 @@ export function initRooms(app) {
115
115
  app.stopPolling();
116
116
  app.state.room = entry.room;
117
117
  app.state.myName = entry.name;
118
- const savedPosition = app.state.positions.get(roomId);
119
118
  app.state.unread = entry.unreadIds.size + (entry.unreadOverflow ? 100 : 0);
120
119
  app.rendered = 0;
121
120
  app.firstMessageId = null;
@@ -124,13 +123,12 @@ export function initRooms(app) {
124
123
  app.showChat();
125
124
  app.renderHead();
126
125
  $("#msgList").textContent = "";
126
+ app.state.listMetrics = { height: 0, top: 0, client: 0 };
127
127
  app.renderMessages(false);
128
- const restoredPosition = app.restoreReadingPosition?.(roomId);
129
- // A restore above the bottom needs a scroll before it counts as read.
130
- app.state.readAnchorGuard = restoredPosition && !restoredPosition.atBottom ? { roomId, ...restoredPosition } : null;
128
+ app.restoreReadingPosition?.(roomId);
129
+ app.state.readAnchorGuard = null;
131
130
  app.startPolling();
132
131
  app.renderMembers();
133
- if (!restoredPosition) $("#thread").scrollTop = typeof savedPosition === "number" ? savedPosition : entry.position || $("#thread").scrollHeight;
134
132
  $("#msgInput").contentEditable = "true";
135
133
  $("#msgInput").setAttribute("aria-disabled", "false"); $("#msgInput").tabIndex = 0;
136
134
  $("#msgInput").draftValue = app.state.drafts.get(roomId) || ""; app.renderReplyPreview?.();
@@ -141,7 +139,7 @@ export function initRooms(app) {
141
139
  app.hideSendError();
142
140
  app.updateBadge();
143
141
  app.updateJump();
144
- if (!restoredPosition || restoredPosition.atBottom) app.markActiveRoomRead();
142
+ app.markActiveRoomRead();
145
143
  app.renderRoomOptions($("#roomList"), app.state.availableRooms || []);
146
144
  app.renderRoomOptions($("#roomListChat"), app.state.availableRooms || []);
147
145
  void app.refreshRoomList();
@@ -11,7 +11,7 @@ export const app = {
11
11
  memberApiUnavailable: new Set(), receiptApiUnavailable: new Set(),
12
12
  receipts: new Map(), acknowledgedMessages: new Map(), pendingAcknowledgements: new Map(),
13
13
  pendingAction: null, watcherTimer: 0, restored: false, resizeScrollAdjusting: false, layoutScrollAdjusting: false,
14
- readAnchorGuard: null, userScrollIntentUntil: 0,
14
+ readAnchorGuard: null, userScrollIntentUntil: 0, listMetrics: { height: 0, top: 0, client: 0 },
15
15
  },
16
16
  rendered: 0,
17
17
  firstMessageId: null,
@@ -145,20 +145,20 @@
145
145
  #roomListChat .room-row { height: auto; padding: 10px 12px; }
146
146
  #roomListChat .r-meta { font-size: 12px; }
147
147
  #roomListChat .r-preview { display: block; }
148
- #chatView > .chat-head { grid-column: 1; grid-row: 1; flex-wrap: nowrap; align-items: center; padding: 9px 12px; gap: 8px; }
148
+ #chatView > .chat-head { grid-column: 1; grid-row: 1; flex-wrap: wrap; align-items: center; padding: 9px 12px; gap: 6px; }
149
149
  #chatView > .conn-bar { grid-column: 1; grid-row: 2; }
150
150
  #chatView > .msg-scroll { grid-column: 1; grid-row: 3; }
151
151
  #chatView > .composer { grid-column: 1; grid-row: 4; }
152
- .chat-head-actions { flex: none; flex-wrap: nowrap; gap: 4px; }
152
+ .chat-head-actions { flex: 1 1 100%; flex-wrap: nowrap; justify-content: flex-end; gap: 6px; }
153
153
  .chat-head-actions > .locale-control { display: none; }
154
- .chat-head-main { flex: 1; min-width: 0; }
154
+ .chat-head-main { flex: 1 1 100%; min-width: 0; }
155
155
  .room-title-row { gap: 6px; }
156
156
  .chat-head-main h1 { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 18px; }
157
157
  .chat-head .chip { display: grid; place-items: center; width: 44px; height: 44px; padding: 0; font-size: 0; }
158
158
  #chatView > .chat-head .action-icon { display: block; }
159
159
  .rooms-drawer-trigger { display: grid; flex: none; }
160
160
  #btnQuestions .question-badge { position: absolute; top: -5px; right: -5px; margin: 0; font-size: 10px; }
161
- #questionPanel, #announcementPanel { position: fixed; top: 70px; left: 12px; right: 12px; width: auto; max-height: calc(100dvh - 92px); }
161
+ #questionPanel, #announcementPanel, #reminderPanel, #memberPanel, #roomOptions { position: fixed; top: var(--chat-head-bottom, 116px); left: 12px; right: 12px; width: auto; max-height: calc(100dvh - 132px); overflow: auto; }
162
162
  .room-options-panel { right: 0; left: auto; }
163
163
  .thread { padding: 14px 12px 6px; }
164
164
  .msg-text { max-width: 92%; font-size: 15px; }
package/server/chat.html CHANGED
@@ -139,7 +139,7 @@
139
139
  </section>
140
140
  </div>
141
141
  <div class="anchor">
142
- <button class="chip" type="button" id="btnReminders" aria-haspopup="true" aria-expanded="false" aria-controls="reminderPanel"><span class="action-label" data-i18n="reminders">Reminders</span></button>
142
+ <button class="chip" type="button" id="btnReminders" aria-haspopup="true" aria-expanded="false" aria-controls="reminderPanel"><svg class="action-icon" aria-hidden="true" viewBox="0 0 24 24"><path d="M18 8a6 6 0 1 0-12 0c0 7-3 7-3 9h18c0-2-3-2-3-9"/><path d="M10 21a2 2 0 0 0 4 0"/></svg><span class="action-label" data-i18n="reminders">Reminders</span></button>
143
143
  <section class="panel reminder-panel" id="reminderPanel" aria-labelledby="reminderHeading" hidden>
144
144
  <div class="announcement-head"><h3 id="reminderHeading" tabindex="-1" data-i18n="reminders">Reminders</h3><button type="button" id="btnRefreshReminders" class="question-refresh" data-i18n="refreshQuestions">Refresh</button></div>
145
145
  <p id="reminderStatus" class="hint" role="status" aria-live="polite"></p><ul id="reminderList" class="reminder-list"></ul>