@alexsun-top/opencode-chat-room 0.7.3 → 0.7.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 (89) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/README.md +12 -5
  3. package/README.zh-CN.md +12 -5
  4. package/cli.ts +26 -6
  5. package/dist/cli.js +7804 -0
  6. package/package.json +19 -6
  7. package/plugin.ts +7 -4
  8. package/server/admin-auth.ts +1 -1
  9. package/server/admin-routes.ts +2 -2
  10. package/server/admin-state.ts +2 -1
  11. package/server/assets/FLUENT-ICONS-LICENSE +25 -0
  12. package/server/assets/NOTICE.txt +2 -1
  13. package/server/assets/attachments.css +1 -1
  14. package/server/assets/base.css +29 -7
  15. package/server/assets/chat/admin.js +43 -3
  16. package/server/assets/chat/announcements.js +5 -3
  17. package/server/assets/chat/confirm-dialog.js +12 -2
  18. package/server/assets/chat/create-room-dialog.js +5 -3
  19. package/server/assets/chat/events.js +32 -7
  20. package/server/assets/chat/files.js +369 -0
  21. package/server/assets/chat/history-loader.js +36 -26
  22. package/server/assets/chat/icons.js +15 -0
  23. package/server/assets/chat/index.js +7 -1
  24. package/server/assets/chat/locales/de.js +19 -2
  25. package/server/assets/chat/locales/es.js +19 -2
  26. package/server/assets/chat/locales/fr.js +19 -2
  27. package/server/assets/chat/locales/it.js +19 -2
  28. package/server/assets/chat/locales/ja.js +19 -2
  29. package/server/assets/chat/locales/ko.js +19 -2
  30. package/server/assets/chat/markdown.js +5 -33
  31. package/server/assets/chat/message-polling.js +61 -6
  32. package/server/assets/chat/message-window.js +28 -0
  33. package/server/assets/chat/messages.js +32 -12
  34. package/server/assets/chat/palette.js +396 -0
  35. package/server/assets/chat/pwa.js +67 -9
  36. package/server/assets/chat/recall.js +3 -2
  37. package/server/assets/chat/replies.js +2 -1
  38. package/server/assets/chat/rooms.js +20 -7
  39. package/server/assets/chat/search.js +137 -0
  40. package/server/assets/chat/settings.js +11 -5
  41. package/server/assets/chat/shortcuts.js +1 -1
  42. package/server/assets/chat/state.js +38 -4
  43. package/server/assets/code-highlight.css +0 -5
  44. package/server/assets/conversation.css +5 -4
  45. package/server/assets/edit-room.css +77 -0
  46. package/server/assets/files.css +222 -0
  47. package/server/assets/fluent-icons.svg +42 -0
  48. package/server/assets/icons.css +9 -0
  49. package/server/assets/palette.css +263 -0
  50. package/server/assets/recall.css +2 -2
  51. package/server/assets/responsive.css +6 -5
  52. package/server/assets/room-create.css +1 -1
  53. package/server/assets/settings.css +10 -7
  54. package/server/chat-server.ts +46 -29
  55. package/server/chat.html +175 -61
  56. package/server/http-server.ts +182 -0
  57. package/server/join-requests.ts +4 -4
  58. package/server/manifest.webmanifest +1 -1
  59. package/server/member-routes.ts +3 -3
  60. package/server/message-recall-routes.ts +1 -1
  61. package/server/newrooms.ts +4 -4
  62. package/server/resource-routes.ts +19 -4
  63. package/server/room-join.ts +1 -1
  64. package/server/room-payload.ts +1 -1
  65. package/server/sw.js +12 -1
  66. package/server/wake.ts +1 -1
  67. package/src/README.md +4 -2
  68. package/src/config/file-config.ts +294 -0
  69. package/src/rooms/resource-read.ts +2 -1
  70. package/src/rooms/resource-upload.ts +6 -3
  71. package/src/rooms/resources.ts +7 -2
  72. package/src/runtime/watch.ts +13 -8
  73. package/src/state/receiver-state.ts +3 -5
  74. package/src/state/sqlite-state.ts +11 -7
  75. package/src/state/sqlite.ts +80 -0
  76. package/src/state/store.ts +1 -0
  77. package/src/tui/app.mjs +417 -0
  78. package/src/tui/config.mjs +194 -0
  79. package/src/tui/http.mjs +103 -0
  80. package/src/tui/main.mjs +176 -0
  81. package/src/tui/messages.mjs +67 -0
  82. package/src/tui/palette-app.mjs +265 -0
  83. package/src/tui/palette.mjs +101 -0
  84. package/src/tui/session.mjs +40 -0
  85. package/src/tui/ui-content.mjs +426 -0
  86. package/src/tui/ui-theme.mjs +60 -0
  87. package/src/tui/ui-view.mjs +452 -0
  88. package/src/tui/ui.mjs +262 -0
  89. package/src/tests/announcement-board.ts +0 -186
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@alexsun-top/opencode-chat-room",
3
- "version": "0.7.3",
4
- "description": "A chat-room plugin and central server for OpenCode.",
3
+ "version": "0.7.5",
4
+ "description": "A chat-room server, web client, terminal client, and OpenCode/Codex/dsh plugins.",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/agentsyaml/opencode-chat-room.git"
@@ -16,16 +16,20 @@
16
16
  "./chat-server": "./server/chat-server.ts"
17
17
  },
18
18
  "bin": {
19
- "opencode-chat-room": "cli.ts"
19
+ "opencode-chat-room": "dist/cli.js"
20
20
  },
21
21
  "files": [
22
22
  "cli.ts",
23
+ "dist",
23
24
  "plugin.ts",
24
25
  "index.ts",
25
26
  "src",
26
- "!src/**/*.test.ts",
27
+ "!src/tests",
28
+ "!src/**/*.test.*",
29
+ "!src/tui/test",
27
30
  "skills/chatroom/SKILL.md",
28
31
  "server/chat-server.ts",
32
+ "server/http-server.ts",
29
33
  "server/json-body.ts",
30
34
  "server/inbox.ts",
31
35
  "server/history-route.ts",
@@ -57,10 +61,17 @@
57
61
  "registry": "https://registry.npmjs.org/"
58
62
  },
59
63
  "scripts": {
60
- "server": "bun run server/chat-server.ts",
61
- "test": "for file in src/tests/*.test.ts server/*.test.ts; do bun test \"$file\" || exit; done",
64
+ "build:cli": "bun build cli.ts --target=node --packages external --outfile dist/cli.js >&2",
65
+ "prepack": "bun run build:cli",
66
+ "server": "node cli.ts",
67
+ "server:bun": "bun cli.ts",
68
+ "server:deno": "deno run --allow-env --allow-read --allow-write --allow-net cli.ts",
69
+ "test": "for file in src/tests/*.test.ts server/*.test.ts; do bun test \"$file\" || exit; done && bun test src/tui/test",
62
70
  "typecheck": "tsc --noEmit"
63
71
  },
72
+ "engines": {
73
+ "node": ">=24.2.0"
74
+ },
64
75
  "devDependencies": {
65
76
  "@opencode/plugin": "2.0.29",
66
77
  "@types/bun": "latest",
@@ -72,7 +83,9 @@
72
83
  "dependencies": {
73
84
  "@opencode-ai/plugin": "^1.18.29",
74
85
  "@opencode/client": "2.0.32",
86
+ "@opentui/core": "0.2.16",
75
87
  "croner": "11.0.0",
88
+ "yaml": "^2.9.0",
76
89
  "zod": "4.1.8"
77
90
  }
78
91
  }
package/plugin.ts CHANGED
@@ -34,12 +34,15 @@ import { uploadAgentResource } from "./src/rooms/resource-upload.ts";
34
34
  import { readAgentResource, resourceReadFailure } from "./src/rooms/resource-read.ts";
35
35
  import { adminTool, adminToolDescription, adminToolSchema } from "./src/agent/admin-client.ts";
36
36
  import { runReminderTool } from "./src/center/reminder-client.ts";
37
+ import { loadRoomConfig, resolveClientSettings } from "./src/config/file-config.ts";
37
38
  // ponytail: CHAT_ROOM_SERVER_URL switches to central mode (all state on a
38
- // central server); unset = standalone local-file mode
39
- const centerUrl = process.env.CHAT_ROOM_SERVER_URL ?? "";
40
- const centerToken = process.env.CHAT_ROOM_SERVER_TOKEN ?? "";
39
+ // central server); unset = standalone local-file mode. A chatroom.json(c)/yaml
40
+ // file is the shared fallback; environment variables still override it.
41
+ const connection = resolveClientSettings(loadRoomConfig());
42
+ const centerUrl = connection.url;
43
+ const centerToken = connection.token;
41
44
  // A configured OpenCode serve URL is required to wake exited sessions.
42
- const serveWakeUrl = process.env.CHAT_ROOM_WAKE_SERVE_URL ?? "";
45
+ const serveWakeUrl = connection.wakeServeUrl;
43
46
  type PluginRuntime = V2Runtime;
44
47
 
45
48
  const runtimeContext = new AsyncLocalStorage<PluginRuntime>();
@@ -1,4 +1,4 @@
1
- import { matchesHash, type Binding } from "./admin-state";
1
+ import { matchesHash, type Binding } from "./admin-state.ts";
2
2
 
3
3
  export function agentHeadersMatch(request: Request, binding: Binding): boolean {
4
4
  return request.headers.get("x-chat-agent-session") === binding.session_id &&
@@ -1,7 +1,7 @@
1
1
  import { databasePath } from "../src/state/store.ts";
2
2
  import { readHistoryPage } from "../src/state/sqlite-state.ts";
3
- import { hasAdminCapability } from "./admin-auth";
4
- import { binding, digest, ensureAdminSchema, exec, isAgentMember, many, matchesHash, newSecret, publicRoom, tx, validCurrent } from "./admin-state";
3
+ import { hasAdminCapability } from "./admin-auth.ts";
4
+ import { binding, digest, ensureAdminSchema, exec, isAgentMember, many, matchesHash, newSecret, publicRoom, tx, validCurrent } from "./admin-state.ts";
5
5
 
6
6
  const json = (body: unknown, status = 200) => Response.json(body, { status, headers: { "Cache-Control": "no-store" } });
7
7
  const object = (value: unknown): value is Record<string, unknown> => !!value && typeof value === "object" && !Array.isArray(value);
@@ -1,5 +1,6 @@
1
+ import { Buffer } from "node:buffer";
1
2
  import { createHash, randomBytes, randomUUID, timingSafeEqual } from "node:crypto";
2
- import type { Database, SQLQueryBindings } from "bun:sqlite";
3
+ import type { Database, SQLQueryBindings } from "../src/state/sqlite.ts";
3
4
  import { databasePath } from "../src/state/store.ts";
4
5
  import { initializeAdminSchema, withStateTransaction } from "../src/state/sqlite-state.ts";
5
6
 
@@ -0,0 +1,25 @@
1
+ Fluent System Icons
2
+ @fluentui/svg-icons 1.1.347, 24px regular variants
3
+ https://github.com/microsoft/fluentui-system-icons
4
+
5
+ MIT License
6
+
7
+ Copyright (c) 2020 Microsoft Corporation
8
+
9
+ Permission is hereby granted, free of charge, to any person obtaining a copy
10
+ of this software and associated documentation files (the "Software"), to deal
11
+ in the Software without restriction, including without limitation the rights
12
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
+ copies of the Software, and to permit persons to whom the Software is
14
+ furnished to do so, subject to the following conditions:
15
+
16
+ The above copyright notice and this permission notice shall be included in all
17
+ copies or substantial portions of the Software.
18
+
19
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
+ SOFTWARE.
@@ -4,5 +4,6 @@ marked 18.0.4 — MIT License, see MARKED-LICENSE.
4
4
  DOMPurify 3.4.7 — MPL 2.0 or Apache License 2.0, see DOMPURIFY-LICENSE.
5
5
  Mermaid 11.12.2 — MIT License, see MERMAID-LICENSE.
6
6
  highlight.js 10.7.3 — BSD 3-Clause License, see HIGHLIGHT-LICENSE.
7
+ Fluent System Icons (@fluentui/svg-icons 1.1.347) — MIT License, see FLUENT-ICONS-LICENSE.
7
8
 
8
- Sources: https://www.npmjs.com/package/marked, https://www.npmjs.com/package/dompurify, https://www.npmjs.com/package/mermaid and https://www.npmjs.com/package/highlight.js
9
+ Sources: https://www.npmjs.com/package/marked, https://www.npmjs.com/package/dompurify, https://www.npmjs.com/package/mermaid, https://www.npmjs.com/package/highlight.js and https://github.com/microsoft/fluentui-system-icons
@@ -1,6 +1,6 @@
1
1
  /* Composer attachment tray: kept apart from the conversation layout. */
2
2
  .attachment-button { flex: none; display: grid; place-items: center; width: 44px; height: 44px; padding: 0; border: 1px solid var(--line); border-radius: 12px; color: var(--ink-2); background: var(--surface); font: inherit; font-size: 24px; cursor: pointer; }
3
- .attachment-button svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-linecap: round; stroke-linejoin: round; stroke-width: 1.8; }
3
+ .attachment-button svg { width: 20px; height: 20px; }
4
4
  .attachment-button:hover { color: var(--accent); border-color: var(--accent); }
5
5
  .attachment-button:disabled { opacity: .5; cursor: default; }
6
6
  .attachment-tray { width: min(940px, 100%); margin: 0 auto; }
@@ -90,6 +90,27 @@
90
90
  .install-prompt { grid-template-columns: minmax(0, 1fr); }
91
91
  .install-prompt-actions { justify-content: flex-start; }
92
92
  }
93
+ .pwa-update {
94
+ position: fixed;
95
+ z-index: 60;
96
+ left: 18px;
97
+ bottom: max(18px, env(safe-area-inset-bottom));
98
+ display: grid;
99
+ gap: 10px;
100
+ width: min(392px, calc(100vw - 36px));
101
+ padding: 14px 16px;
102
+ border: 1px solid color-mix(in srgb, var(--accent) 28%, var(--line));
103
+ border-radius: 16px;
104
+ background: var(--surface);
105
+ box-shadow: 0 8px 30px rgb(34 53 47 / .16);
106
+ }
107
+ .pwa-update-copy strong { display: block; font-size: 13.5px; font-weight: 650; }
108
+ .pwa-update-copy p { margin: 3px 0 0; font-size: 12.5px; line-height: 1.5; color: var(--ink-2); }
109
+ .pwa-update-actions { display: flex; flex-wrap: wrap; gap: 8px; }
110
+ .pwa-update-actions .btn { min-height: 36px; padding: 7px 12px; font-size: 12.5px; }
111
+ @media (max-width: 560px) {
112
+ .pwa-update { right: 10px; bottom: max(10px, env(safe-area-inset-bottom)); left: 10px; width: auto; }
113
+ }
93
114
  .entry-card {
94
115
  display: grid;
95
116
  gap: 10px;
@@ -112,10 +133,10 @@
112
133
  appearance: none;
113
134
  padding-left: var(--select-pad);
114
135
  padding-right: calc(var(--select-pad) + 22px);
115
- background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1.2 1.6 6 6.4l4.8-4.8' fill='none' stroke='%2363716b' stroke-width='1.7' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
136
+ background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24'%3E%3Cpath fill='%2363716b' d='M4.22 8.47c.3-.3.77-.3 1.06 0L12 15.19l6.72-6.72a.75.75 0 1 1 1.06 1.06l-7.25 7.25c-.3.3-.77.3-1.06 0L4.22 9.53a.75.75 0 0 1 0-1.06'/%3E%3C/svg%3E");
116
137
  background-position: right var(--select-pad) center;
117
138
  background-repeat: no-repeat;
118
- background-size: 12px 8px;
139
+ background-size: 16px 16px;
119
140
  }
120
141
  @media (forced-colors: active) {
121
142
  select { appearance: auto; padding-right: var(--select-pad); background-image: none; }
@@ -146,7 +167,7 @@
146
167
  color: var(--ink-2);
147
168
  }
148
169
  .locale-label { display: flex; align-items: center; gap: 6px; }
149
- .locale-icon { width: 16px; height: 16px; flex: none; fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; }
170
+ .locale-icon { width: 16px; height: 16px; flex: none; }
150
171
 
151
172
  .input {
152
173
  width: 100%;
@@ -326,8 +347,8 @@
326
347
  transition: background .12s, border-color .12s;
327
348
  }
328
349
  .room-row:hover { background: var(--surface-2); border-color: var(--line); }
329
- .room-row.room-switch-enter { animation: room-switch-row .2s ease-out; }
330
- @keyframes room-switch-row { from { transform: translateY(var(--room-switch-shift, 6px)); } }
350
+ .room-row.room-switch-enter { animation: room-switch-row .15s cubic-bezier(.1, .9, .2, 1); }
351
+ @keyframes room-switch-row { from { opacity: .55; transform: translateY(var(--room-switch-shift, 3px)); } }
331
352
  .r-name { font-weight: 600; font-size: 14px; }
332
353
  .r-meta { font-size: 12px; color: var(--ink-3); font-variant-numeric: tabular-nums; }
333
354
  .r-go {
@@ -346,6 +367,7 @@
346
367
  /* Full-screen image viewer zoom controls and split file actions. */
347
368
  .image-dialog-tools { position: fixed; z-index: 2; top: 14px; left: 14px; display: flex; align-items: center; gap: 2px; padding: 3px; border: 1px solid rgb(255 255 255 / .18); border-radius: 999px; background: rgb(9 16 22 / .58); backdrop-filter: blur(6px); }
348
369
  .image-dialog-tool { display: grid; place-items: center; min-width: 40px; height: 40px; padding: 0 8px; border: 1px solid transparent; border-radius: 999px; color: #fff; background: transparent; font: inherit; font-size: 18px; font-weight: 600; line-height: 1; cursor: pointer; }
370
+ .image-dialog-tool .fluent-icon { width: 16px; height: 16px; }
349
371
  .image-dialog-tool:hover { background: rgb(255 255 255 / .14); }
350
372
  .image-dialog-tool:focus-visible { outline: 2px solid #fff; outline-offset: 2px; }
351
373
  .image-dialog-tool:disabled { opacity: .38; cursor: default; }
@@ -355,7 +377,7 @@
355
377
  .resource-split { position: relative; display: inline-flex; align-items: stretch; }
356
378
  .resource-split .resource-download { border-right: 0; border-radius: 10px 0 0 10px; }
357
379
  .resource-split .resource-split-toggle { display: grid; place-items: center; width: 34px; min-height: 34px; padding: 0; border-radius: 0 10px 10px 0; color: var(--ink-2); }
358
- .resource-split .resource-split-toggle svg { width: 15px; height: 15px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
380
+ .resource-split .resource-split-toggle svg { width: 15px; height: 15px; }
359
381
  .resource-split .resource-split-toggle[aria-expanded="true"] { color: var(--accent); background: var(--accent-soft); }
360
382
  .resource-split-menu { position: absolute; z-index: 30; top: calc(100% + 5px); right: 0; min-width: 160px; padding: 4px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); box-shadow: var(--shadow); }
361
383
 
@@ -377,7 +399,7 @@
377
399
  .admin-join-policy + .join-policy-hint { margin-bottom: 12px; }
378
400
  .join-pending { display: grid; grid-template-columns: auto minmax(0, 1fr) auto; align-items: start; gap: 12px 14px; padding: 18px 20px; border: 1px solid color-mix(in srgb, var(--accent) 25%, var(--line)); border-radius: 18px; background: linear-gradient(135deg, color-mix(in srgb, var(--accent-soft) 65%, var(--surface)), var(--surface)); box-shadow: 0 2px 5px rgb(34 53 47 / .04), 0 14px 34px rgb(34 53 47 / .07); }
379
401
  .join-pending-mark { display: grid; place-items: center; width: 38px; height: 38px; border-radius: 12px; color: var(--accent-ink); background: var(--accent); }
380
- .join-pending-mark svg { width: 19px; height: 19px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
402
+ .join-pending-mark svg { width: 19px; height: 19px; }
381
403
  .join-pending h2 { margin: 0; font-family: var(--font-display); font-size: 21px; font-weight: 600; letter-spacing: -.02em; }
382
404
  .join-pending p { margin: 5px 0 0; font-size: 13px; line-height: 1.55; color: var(--ink-2); }
383
405
  .join-pending .hint { color: var(--ink-3); font-size: 12px; }
@@ -5,7 +5,33 @@ import { isRoomAdmin } from "./admin-badge.js";
5
5
  // the room payload and room owners manage it here instead of an operator token.
6
6
  export function initAdmin(app) {
7
7
  const $ = app.$, dialog = $("#editRoomDialog");
8
- let agents = [], people = [], generation = 0, agentsReady = false, rosterReady = false;
8
+ let agents = [], people = [], generation = 0, agentsReady = false, rosterReady = false, opener = null;
9
+ const tabs = [...dialog.querySelectorAll('[role="tab"]')];
10
+
11
+ // One tab stop for the visible tabs; arrow keys move selection like a native tab widget.
12
+ const activateTab = (name, { focus = false } = {}) => {
13
+ for (const tab of tabs) {
14
+ const selected = tab.dataset.editRoomTab === name;
15
+ tab.setAttribute("aria-selected", String(selected));
16
+ tab.tabIndex = selected ? 0 : -1;
17
+ const panel = document.getElementById(tab.getAttribute("aria-controls"));
18
+ if (panel) panel.hidden = !selected;
19
+ if (selected && focus) tab.focus();
20
+ }
21
+ };
22
+ const onTabKey = (event) => {
23
+ const items = tabs.filter((tab) => !tab.hidden), index = items.indexOf(event.target);
24
+ if (index < 0) return;
25
+ const step = event.key === "ArrowUp" || event.key === "ArrowLeft" ? -1 : event.key === "ArrowDown" || event.key === "ArrowRight" ? 1 : 0;
26
+ const target = event.key === "Home" ? 0 : event.key === "End" ? items.length - 1 : step ? (index + step + items.length) % items.length : -1;
27
+ if (target < 0) return;
28
+ event.preventDefault();
29
+ activateTab(items[target].dataset.editRoomTab, { focus: true });
30
+ };
31
+ for (const tab of tabs) {
32
+ tab.addEventListener("click", () => activateTab(tab.dataset.editRoomTab));
33
+ tab.addEventListener("keydown", onTabKey);
34
+ }
9
35
 
10
36
  // Only people can own a room: agents carry a protocol, a host, or a receiver binding.
11
37
  const isPerson = (entry) => Boolean(entry?.name) && !entry.protocol && !entry.host && !entry.receiverOwnerID;
@@ -67,9 +93,17 @@ export function initAdmin(app) {
67
93
  renderAdminIds(room);
68
94
  $("#editRoomAdminIdsEmpty").hidden = true;
69
95
  renderSuccessorOptions(room);
70
- $("#editRoomOwnerOnly").hidden = !isOwner();
71
- $("#editRoomOwnerOnlyNote").hidden = isOwner();
96
+ const owner = isOwner();
97
+ // The Ownership tab only exists for the owner; its fields stay in the form for everyone.
98
+ dialog.dataset.owner = owner ? "true" : "false";
99
+ $("#editRoomTabOwnership").hidden = !owner;
100
+ $("#editRoomOwnerOnly").hidden = !owner;
101
+ $("#editRoomOwnerOnlyNote").hidden = owner;
102
+ activateTab("details");
103
+ const active = document.activeElement;
104
+ opener = active instanceof HTMLElement && active.getClientRects().length ? active : $("#btnRoomOptions");
72
105
  dialog.showModal();
106
+ $("#editRoomName").focus();
73
107
  void loadAgents(++generation);
74
108
  }
75
109
 
@@ -90,7 +124,13 @@ export function initAdmin(app) {
90
124
  $("#btnOwnerOnlyOk").addEventListener("click", () => ownerDialog.close());
91
125
  ownerDialog.addEventListener("click", (event) => { if (event.target === ownerDialog) ownerDialog.close(); });
92
126
  $("#btnCloseEditRoom").addEventListener("click", () => dialog.close());
127
+ $("#btnCancelEditRoom").addEventListener("click", () => dialog.close());
93
128
  dialog.addEventListener("click", (event) => { if (event.target === dialog) dialog.close(); });
129
+ dialog.addEventListener("close", () => {
130
+ const target = opener;
131
+ opener = null;
132
+ if (target?.isConnected && target.getClientRects().length) target.focus({ preventScroll: true });
133
+ });
94
134
  $("#editRoomForm").addEventListener("submit", async (event) => {
95
135
  event.preventDefault();
96
136
  const room = app.state.room; if (!room) return;
@@ -135,12 +135,14 @@ export function initAnnouncements(app) {
135
135
  $("#btnCancelAnnouncement").addEventListener("click", () => dialog.close());
136
136
  dialog.addEventListener("click", (event) => { if (event.target === dialog) dialog.close(); });
137
137
  trigger.addEventListener("click", () => {
138
+ // The entry lives in the Options menu, so choosing it closes that menu first.
138
139
  const open = panel.hidden; app.closePanels(); panel.hidden = !open;
139
- trigger.setAttribute("aria-expanded", String(open)); if (open) { void load(); $("#announcementHeading").focus(); }
140
+ if (open) { void load(); $("#announcementHeading").focus(); }
141
+ else $("#btnRoomOptions").focus();
140
142
  });
141
- document.addEventListener("keydown", (event) => { if (event.key === "Escape" && !dialog.open && !panel.hidden) { app.closePanels(); trigger.focus(); } });
143
+ document.addEventListener("keydown", (event) => { if (event.key === "Escape" && !dialog.open && !panel.hidden) { app.closePanels(); $("#btnRoomOptions").focus(); } });
142
144
  const close = app.closePanels;
143
- app.closePanels = () => { close(); panel.hidden = true; trigger.setAttribute("aria-expanded", "false"); if (dialog.open) dialog.close(); };
145
+ app.closePanels = () => { close(); panel.hidden = true; if (dialog.open) dialog.close(); };
144
146
  document.addEventListener("chat-room-activated", (event) => {
145
147
  const id = event.detail;
146
148
  if (dialog.open) dialog.close();
@@ -3,13 +3,14 @@
3
3
  export function initConfirmDialog(app) {
4
4
  const dialog = app.$("#confirmDialog"), message = app.$("#confirmMessage");
5
5
  const accept = app.$("#btnAcceptConfirm"), cancel = app.$("#btnCancelConfirm");
6
- let pending = null;
6
+ let pending = null, opener = null;
7
7
  const settle = (value) => { const resolve = pending; pending = null; resolve?.(value); };
8
8
  function close(value) { settle(value); dialog.close(); }
9
9
 
10
10
  app.confirmAction = (text) => {
11
11
  // One dialog at a time: a newer request dismisses the open one as cancelled.
12
12
  if (dialog.open) close(false);
13
+ opener = document.activeElement instanceof HTMLElement ? document.activeElement : null;
13
14
  message.textContent = text;
14
15
  dialog.showModal();
15
16
  cancel.focus();
@@ -23,6 +24,15 @@ export function initConfirmDialog(app) {
23
24
  dialog.addEventListener("close", () => {
24
25
  // A close from outside the buttons still settles the promise; a re-open in the
25
26
  // same task leaves the dialog open, so the stale event must not clear it.
26
- if (!dialog.open) { settle(false); message.textContent = ""; }
27
+ if (dialog.open) return;
28
+ settle(false);
29
+ message.textContent = "";
30
+ const target = opener;
31
+ opener = null;
32
+ requestAnimationFrame(() => {
33
+ if (dialog.open) return;
34
+ if (app.focusChatComposer?.({ force: true })) return;
35
+ if (target?.isConnected && target.getClientRects().length) target.focus({ preventScroll: true });
36
+ });
27
37
  });
28
38
  }
@@ -36,12 +36,14 @@ export function initCreateRoomDialog(app) {
36
36
  dialog.addEventListener("click", (event) => { if (event.target === dialog) close(); });
37
37
  dialog.addEventListener("cancel", () => trigger.setAttribute("aria-expanded", "false"));
38
38
  dialog.addEventListener("close", () => {
39
- const target = opener;
39
+ const target = opener, restore = restoreFocus;
40
40
  opener = null;
41
+ restoreFocus = true;
41
42
  trigger.setAttribute("aria-expanded", "false");
42
43
  app.hideCreateRoomError();
43
- if (restoreFocus && target?.isConnected) target.focus({ preventScroll: true });
44
- restoreFocus = true;
44
+ if (!restore) return;
45
+ if (app.focusChatComposer?.({ force: true })) return;
46
+ if (target?.isConnected) target.focus({ preventScroll: true });
45
47
  });
46
48
  app.$("#btnCloseCreateRoom")?.addEventListener("click", () => close());
47
49
  app.$("#btnCancelCreateRoom")?.addEventListener("click", () => close());
@@ -6,11 +6,17 @@ export function initEvents(app) {
6
6
  const $ = app.$;
7
7
  // Thread clicks land on non-focusable nodes, so the browser drops focus to BODY
8
8
  // and the next keystroke is lost. Returning it to the composer matches chat UX.
9
+ app.focusChatComposer = ({ force = false } = {}) => {
10
+ const editor = $("#msgInput");
11
+ if (matchMedia("(max-width: 760px)").matches || document.querySelector("dialog[open]")) return false;
12
+ if ($("#chatView").hidden || editor.contentEditable !== "true" || !editor.getClientRects().length) return false;
13
+ if (roomRail.classList.contains("drawer-open")) return false;
14
+ if (!force && (document.activeElement !== document.body || window.getSelection()?.toString())) return false;
15
+ editor.focus({ preventScroll: true });
16
+ return true;
17
+ };
9
18
  app.focusComposer = () => {
10
- if (matchMedia("(max-width: 760px)").matches || document.querySelector("dialog[open]")) return;
11
- if ($("#chatView").hidden || $("#msgInput").contentEditable !== "true") return;
12
- if (document.activeElement !== document.body || window.getSelection()?.toString()) return;
13
- $("#msgInput").focus({ preventScroll: true });
19
+ app.focusChatComposer();
14
20
  };
15
21
  document.addEventListener("click", () => requestAnimationFrame(() => app.focusComposer?.()));
16
22
  // Mobile panels sit below the header, whose height depends on the title and locale.
@@ -211,7 +217,7 @@ export function initEvents(app) {
211
217
  $("#btnSend").addEventListener("click", () => void app.send());
212
218
  $("#btnJump").addEventListener("click", app.jumpToBottom);
213
219
  $("#btnCopyId").addEventListener("click", () => void app.copyRoomId());
214
- $("#btnLeave").addEventListener("click", () => app.leaveRoom(false));
220
+ $("#btnLeave").addEventListener("click", () => { app.closePanels(); void app.leaveRoom(false); });
215
221
  $("#btnDeleteRoom").addEventListener("click", () => void app.deleteRoom());
216
222
  $("#btnMembers").addEventListener("click", () => app.togglePanel("#memberPanel"));
217
223
  $("#btnRoomOptions").addEventListener("click", () => {
@@ -324,8 +330,13 @@ export function initEvents(app) {
324
330
  // baseline here would make that growth look like no growth at all.
325
331
  // Only a scroll the reader started ends following; layout shifts do not.
326
332
  const gap = thread.scrollHeight - thread.scrollTop - thread.clientHeight;
327
- if (gap <= 80) app.state.followBottom = true;
328
- else if (performance.now() <= app.state.userScrollIntentUntil) app.state.followBottom = false;
333
+ if (gap <= 80) {
334
+ app.state.followBottom = true;
335
+ if (app.state.focusMessageId && performance.now() > (app.state.focusHoldUntil || 0)) {
336
+ app.state.focusMessageId = null;
337
+ app.rerenderMessages?.();
338
+ }
339
+ } else if (performance.now() <= app.state.userScrollIntentUntil) app.state.followBottom = false;
329
340
  }
330
341
  if (!adjusting || performance.now() <= app.state.userScrollIntentUntil) captureReadingPosition();
331
342
  if (!adjusting && thread.scrollHeight - thread.scrollTop - thread.clientHeight < 80) void app.markActiveRoomRead(performance.now() <= app.state.userScrollIntentUntil);
@@ -398,7 +409,17 @@ export function initEvents(app) {
398
409
  if (app.mentionActive && !event.target.closest("#mentionPicker")) app.closeMention();
399
410
  if (!event.target.closest(".anchor, dialog")) app.closePanels();
400
411
  });
412
+ let lastEscapeAt = -Infinity;
401
413
  document.addEventListener("keydown", (event) => {
414
+ if (event.key === "Escape" && !event.repeat && !event.defaultPrevented
415
+ && !event.altKey && !event.ctrlKey && !event.metaKey && !event.shiftKey) {
416
+ const overlay = Boolean(document.querySelector("dialog[open], .panel:not([hidden]), #mentionPicker:not([hidden])"))
417
+ || roomRail.classList.contains("drawer-open") || !$("#readerPanel").hidden;
418
+ const now = performance.now();
419
+ if (overlay) lastEscapeAt = -Infinity;
420
+ else if (now - lastEscapeAt <= 500) { lastEscapeAt = -Infinity; app.focusChatComposer({ force: true }); }
421
+ else lastEscapeAt = now;
422
+ }
402
423
  if (event.key === "Tab" && roomRail.classList.contains("drawer-open") && !document.querySelector("dialog[open]")) {
403
424
  const stops = [...roomRail.querySelectorAll("button:not(:disabled), input:not(:disabled)")].filter((node) => node.getClientRects().length);
404
425
  const first = stops[0], last = stops.at(-1);
@@ -412,6 +433,10 @@ export function initEvents(app) {
412
433
  app.closePanels();
413
434
  app.lastFocus?.focus();
414
435
  }
436
+ if (event.key === "Escape" && !$("#roomOptions").hidden) {
437
+ app.closePanels();
438
+ $("#btnRoomOptions").focus();
439
+ }
415
440
  if (event.altKey && !event.ctrlKey && !event.metaKey && !event.shiftKey && (event.key === "ArrowUp" || event.key === "ArrowDown")) {
416
441
  // Same switch as a rail row click on a joined room, in the order the rail renders.
417
442
  const joined = (app.state.availableRooms || []).filter((room) => app.state.rooms.has(room.id));