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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (156) hide show
  1. package/CHANGELOG.md +7 -1
  2. package/README.md +5 -5
  3. package/README.zh-CN.md +5 -5
  4. package/cli.ts +1 -1
  5. package/dist/cli.js +35 -16
  6. package/package.json +15 -9
  7. package/packages/chat-room-channeld/README.md +3 -2
  8. package/packages/chat-room-channeld/src/inbound.ts +3 -6
  9. package/packages/chat-room-channeld/src/pg/sql.mjs +157 -142
  10. package/packages/chat-room-channeld/src/pg/sql.mts +175 -0
  11. package/packages/chat-room-channeld/src/pg/worker.mjs +107 -99
  12. package/packages/chat-room-channeld/src/pg/worker.mts +166 -0
  13. package/plugin.ts +1 -1
  14. package/server/assets/chat/admin-badge.js +1 -1
  15. package/server/assets/chat/admin.js +16 -10
  16. package/server/assets/chat/announcements.js +12 -2
  17. package/server/assets/chat/attachment-store.js +15 -6
  18. package/server/assets/chat/attachments.js +48 -33
  19. package/server/assets/chat/chat-background.js +8 -14
  20. package/server/assets/chat/clipboard.js +2 -1
  21. package/server/assets/chat/confirm-dialog.js +3 -3
  22. package/server/assets/chat/copy-action.js +1 -2
  23. package/server/assets/chat/diagram-viewer.js +7 -9
  24. package/server/assets/chat/doc-preview.js +19 -12
  25. package/server/assets/chat/editor.js +87 -61
  26. package/server/assets/chat/events.js +49 -34
  27. package/server/assets/chat/files.js +45 -13
  28. package/server/assets/chat/history-loader.js +5 -3
  29. package/server/assets/chat/image-zoom.js +21 -17
  30. package/server/assets/chat/install-prompt.js +10 -9
  31. package/server/assets/chat/integration-account-schema.js +5 -3
  32. package/server/assets/chat/integration-editor.js +2 -2
  33. package/server/assets/chat/integration-values.js +17 -11
  34. package/server/assets/chat/integrations.js +138 -81
  35. package/server/assets/chat/join-requests.js +20 -13
  36. package/server/assets/chat/markdown-footnotes.js +8 -8
  37. package/server/assets/chat/markdown.js +48 -38
  38. package/server/assets/chat/members.js +48 -25
  39. package/server/assets/chat/mentions.js +39 -12
  40. package/server/assets/chat/message-cache.js +2 -2
  41. package/server/assets/chat/message-polling.js +40 -21
  42. package/server/assets/chat/message-window.js +10 -6
  43. package/server/assets/chat/messages.js +70 -46
  44. package/server/assets/chat/office-preview.js +3 -3
  45. package/server/assets/chat/palette.js +45 -29
  46. package/server/assets/chat/panels.js +19 -1
  47. package/server/assets/chat/pwa.js +5 -4
  48. package/server/assets/chat/questions.js +25 -22
  49. package/server/assets/chat/recall.js +39 -21
  50. package/server/assets/chat/receipts.js +9 -7
  51. package/server/assets/chat/reminders.js +25 -8
  52. package/server/assets/chat/replies.js +34 -15
  53. package/server/assets/chat/resource-format.js +2 -2
  54. package/server/assets/chat/room-list.js +2 -2
  55. package/server/assets/chat/room-watcher.js +8 -4
  56. package/server/assets/chat/rooms.js +51 -40
  57. package/server/assets/chat/search.js +8 -6
  58. package/server/assets/chat/settings.js +16 -14
  59. package/server/assets/chat/shiki.js +1 -1
  60. package/server/assets/chat/shortcuts.js +3 -3
  61. package/server/assets/chat/side-pane.js +4 -3
  62. package/server/assets/chat/state.js +65 -37
  63. package/server/assets/chat/system-events.js +6 -5
  64. package/server/assets/chat/system-messages.js +15 -15
  65. package/server/assets/chat/task-form.js +5 -5
  66. package/server/assets/chat/task-row.js +1 -1
  67. package/server/assets/chat/tasks.js +41 -28
  68. package/server/assets/chat/window-controls.js +3 -2
  69. package/server/assets/conversation.css +14 -2
  70. package/server/assets/doc-preview.css +6 -0
  71. package/server/assets/office/THIRD-PARTY-NOTICES.txt +23 -1
  72. package/server/assets/office/licenses/docx-preview-HEADER.txt +6 -0
  73. package/server/assets/office/licenses/echarts-LICENSE-d3.txt +27 -0
  74. package/server/assets/office/licenses/echarts-NOTICE.txt +5 -0
  75. package/server/assets/office/licenses/immediate-LICENSE.txt +20 -0
  76. package/server/assets/office/licenses/lie-LICENSE.txt +7 -0
  77. package/server/assets/office/licenses/pako-LICENSE.txt +21 -0
  78. package/server/assets/office/licenses/pako-ZLIB.txt +18 -0
  79. package/server/assets/office/licenses/setimmediate-LICENSE.txt +20 -0
  80. package/server/chat.html +1 -0
  81. package/server/sw.js +3 -1
  82. package/skills/chatroom/SKILL.md +9 -0
  83. package/src/README.md +1 -1
  84. package/src/agent/agent-receipts.ts +12 -4
  85. package/src/center/center-client.ts +1 -1
  86. package/src/state/pg-sql.mjs +157 -142
  87. package/src/state/pg-sql.mts +175 -0
  88. package/src/state/pg-worker.mjs +107 -99
  89. package/src/state/pg-worker.mts +166 -0
  90. package/src/state/store.ts +21 -7
  91. package/src/tui/app-types.mjs +1 -0
  92. package/src/tui/app-types.mts +175 -0
  93. package/src/tui/app.mjs +426 -423
  94. package/src/tui/app.mts +500 -0
  95. package/src/tui/config.mjs +166 -158
  96. package/src/tui/config.mts +247 -0
  97. package/src/tui/http.mjs +239 -206
  98. package/src/tui/http.mts +412 -0
  99. package/src/tui/i18n.mjs +17 -20
  100. package/src/tui/i18n.mts +42 -0
  101. package/src/tui/info-app.mjs +431 -427
  102. package/src/tui/info-app.mts +500 -0
  103. package/src/tui/info-content.mjs +205 -199
  104. package/src/tui/info-content.mts +341 -0
  105. package/src/tui/join-app.mjs +75 -64
  106. package/src/tui/join-app.mts +99 -0
  107. package/src/tui/locales/en.mjs +302 -320
  108. package/src/tui/locales/en.mts +349 -0
  109. package/src/tui/locales/zh-CN.mjs +302 -320
  110. package/src/tui/locales/zh-CN.mts +349 -0
  111. package/src/tui/main.mjs +219 -213
  112. package/src/tui/main.mts +255 -0
  113. package/src/tui/markdown.mjs +128 -272
  114. package/src/tui/markdown.mts +152 -0
  115. package/src/tui/mentions.mjs +181 -184
  116. package/src/tui/mentions.mts +251 -0
  117. package/src/tui/message-app.mjs +181 -173
  118. package/src/tui/message-app.mts +213 -0
  119. package/src/tui/message-view.mjs +90 -111
  120. package/src/tui/message-view.mts +151 -0
  121. package/src/tui/messages.mjs +55 -49
  122. package/src/tui/messages.mts +106 -0
  123. package/src/tui/palette-app.mjs +364 -344
  124. package/src/tui/palette-app.mts +388 -0
  125. package/src/tui/palette.mjs +100 -98
  126. package/src/tui/palette.mts +176 -0
  127. package/src/tui/prefs.mjs +56 -63
  128. package/src/tui/prefs.mts +126 -0
  129. package/src/tui/reminder-app.mjs +191 -181
  130. package/src/tui/reminder-app.mts +228 -0
  131. package/src/tui/room-settings-app.mjs +257 -242
  132. package/src/tui/room-settings-app.mts +301 -0
  133. package/src/tui/session.mjs +30 -29
  134. package/src/tui/session.mts +51 -0
  135. package/src/tui/settings-app.mjs +151 -138
  136. package/src/tui/settings-app.mts +179 -0
  137. package/src/tui/ui-content.mjs +426 -421
  138. package/src/tui/ui-content.mts +499 -0
  139. package/src/tui/ui-help.mjs +58 -67
  140. package/src/tui/ui-help.mts +87 -0
  141. package/src/tui/ui-mentions.mjs +34 -36
  142. package/src/tui/ui-mentions.mts +60 -0
  143. package/src/tui/ui-overlay-view.mjs +259 -267
  144. package/src/tui/ui-overlay-view.mts +322 -0
  145. package/src/tui/ui-suggest-view.mjs +54 -57
  146. package/src/tui/ui-suggest-view.mts +78 -0
  147. package/src/tui/ui-suggest.mjs +164 -167
  148. package/src/tui/ui-suggest.mts +248 -0
  149. package/src/tui/ui-theme.mjs +52 -57
  150. package/src/tui/ui-theme.mts +98 -0
  151. package/src/tui/ui-view.mjs +267 -275
  152. package/src/tui/ui-view.mts +308 -0
  153. package/src/tui/ui.mjs +372 -359
  154. package/src/tui/ui.mts +440 -0
  155. package/src/tui/wire.mjs +215 -0
  156. package/src/tui/wire.mts +383 -0
@@ -1,15 +1,10 @@
1
1
  import { parentPort } from "node:worker_threads";
2
2
  import pg from "pg";
3
3
  import { hasSql, translate } from "./pg-sql.mjs";
4
-
5
- // One pg.Client per worker: transactions and their advisory locks belong to a
6
- // single session, so a pool would be incorrect here.
7
- //
8
- // The worker deliberately starts with no workerData. Some Bun versions fail to
9
- // load `pg` in a worker that received any workerData at startup. The main
10
- // thread sends one init message over the default worker channel instead; that
11
- // message also transfers the request MessagePort and the ready SharedArrayBuffer.
12
-
4
+ const ERROR_FIELDS = [
5
+ "name", "code", "detail", "hint", "severity",
6
+ "position", "constraint", "table", "column", "dataType",
7
+ ];
13
8
  let client;
14
9
  let port;
15
10
  let readyFlag;
@@ -18,116 +13,129 @@ let connected = false;
18
13
  let draining = false;
19
14
  let closing = false;
20
15
  const queue = [];
21
-
22
16
  // SQLite returns all integers as JS numbers; int8 is a string in node-postgres
23
17
  // by default and COUNT(*) / BIGINT columns must stay numbers.
24
18
  pg.types.setTypeParser(20, (value) => Number(value));
25
19
  pg.types.setTypeParser(1700, (value) => Number(value));
26
-
27
20
  function normalize(params) {
28
- return params.map((value) =>
29
- value === undefined ? null : typeof value === "boolean" ? (value ? 1 : 0) : value);
21
+ return params.map((value) => value === undefined ? null : typeof value === "boolean" ? (value ? 1 : 0) : value);
30
22
  }
31
-
32
23
  function serialize(error) {
33
- return {
34
- name: error?.name,
35
- message: error?.message ?? String(error),
36
- code: error?.code,
37
- detail: error?.detail,
38
- hint: error?.hint,
39
- severity: error?.severity,
40
- position: error?.position,
41
- constraint: error?.constraint,
42
- table: error?.table,
43
- column: error?.column,
44
- dataType: error?.dataType,
45
- };
24
+ const source = (typeof error === "object" && error !== null ? error : {});
25
+ const result = {
26
+ message: typeof source.message === "string" ? source.message : String(error),
27
+ };
28
+ for (const key of ERROR_FIELDS) {
29
+ const value = source[key];
30
+ if (typeof value === "string" || typeof value === "number")
31
+ result[key] = value;
32
+ }
33
+ return result;
46
34
  }
47
-
48
35
  function signalReady() {
49
- Atomics.store(readyFlag, 0, 1);
50
- Atomics.notify(readyFlag, 0);
36
+ if (!readyFlag)
37
+ return;
38
+ Atomics.store(readyFlag, 0, 1);
39
+ Atomics.notify(readyFlag, 0);
51
40
  }
52
-
53
41
  function reply(request, payload) {
54
- port.postMessage({ id: request.id, ...payload });
55
- const flag = new Int32Array(request.signal);
56
- Atomics.store(flag, 0, 1);
57
- Atomics.notify(flag, 0);
42
+ port?.postMessage({ id: request.id, ...payload });
43
+ const flag = new Int32Array(request.signal);
44
+ Atomics.store(flag, 0, 1);
45
+ Atomics.notify(flag, 0);
58
46
  }
59
-
60
47
  async function handle(request) {
61
- try {
62
- const text = translate(request.sql);
63
- if (!hasSql(text)) return reply(request, { ok: true, rows: [], row: null, changes: 0 });
64
- const params = normalize(request.params ?? []);
65
- // exec without params uses the simple-query protocol so multi-statement
66
- // schema scripts (and BEGIN/COMMIT) run in one round trip.
67
- const result = request.mode === "exec" && params.length === 0
68
- ? await client.query(text)
69
- : await client.query({ text, values: params });
70
- if (request.mode === "get") return reply(request, { ok: true, row: result.rows?.[0] ?? null });
71
- if (request.mode === "all") return reply(request, { ok: true, rows: result.rows ?? [] });
72
- const last = Array.isArray(result) ? result[result.length - 1] : result;
73
- // Identity columns are never read through Database.run in the state layer;
74
- // keep the API shape without pretending to know an insert id.
75
- return reply(request, { ok: true, changes: last?.rowCount ?? 0, lastInsertRowid: 0 });
76
- } catch (error) {
77
- return reply(request, { ok: false, error: serialize(error) });
78
- }
48
+ const db = client;
49
+ if (!db) {
50
+ return reply(request, { ok: false, error: { message: "PostgreSQL worker is not connected" } });
51
+ }
52
+ try {
53
+ const text = translate(request.sql);
54
+ if (!hasSql(text))
55
+ return reply(request, { ok: true, rows: [], row: null, changes: 0 });
56
+ const params = normalize(request.params ?? []);
57
+ // exec without params uses the simple-query protocol so multi-statement
58
+ // schema scripts (and BEGIN/COMMIT) run in one round trip.
59
+ const result = request.mode === "exec" && params.length === 0
60
+ ? await db.query(text)
61
+ : await db.query({ text, values: params });
62
+ if (request.mode === "get")
63
+ return reply(request, { ok: true, row: result.rows?.[0] ?? null });
64
+ if (request.mode === "all")
65
+ return reply(request, { ok: true, rows: result.rows ?? [] });
66
+ const last = Array.isArray(result) ? result.at(-1) : result;
67
+ // Identity columns are never read through Database.run in the state layer;
68
+ // keep the API shape without pretending to know an insert id.
69
+ return reply(request, { ok: true, changes: last?.rowCount ?? 0, lastInsertRowid: 0 });
70
+ }
71
+ catch (error) {
72
+ return reply(request, { ok: false, error: serialize(error) });
73
+ }
79
74
  }
80
-
81
75
  async function drain() {
82
- draining = true;
83
- while (queue.length && !closing) await handle(queue.shift());
84
- draining = false;
76
+ draining = true;
77
+ while (queue.length && !closing) {
78
+ const request = queue.shift();
79
+ if (request)
80
+ await handle(request);
81
+ }
82
+ draining = false;
85
83
  }
86
-
87
84
  async function shutdown(signal) {
88
- closing = true;
89
- try { await client.end(); } catch { /* connection already gone */ }
90
- try { port.close(); } catch { /* already closed */ }
91
- if (signal) {
92
- const flag = new Int32Array(signal);
93
- Atomics.store(flag, 0, 1);
94
- Atomics.notify(flag, 0);
95
- }
85
+ closing = true;
86
+ try {
87
+ await client?.end();
88
+ }
89
+ catch { /* connection already gone */ }
90
+ try {
91
+ port?.close();
92
+ }
93
+ catch { /* already closed */ }
94
+ if (signal) {
95
+ const flag = new Int32Array(signal);
96
+ Atomics.store(flag, 0, 1);
97
+ Atomics.notify(flag, 0);
98
+ }
96
99
  }
97
-
98
100
  function handlePortMessage(message) {
99
- if (message?.type === "close") {
100
- void shutdown(message.signal);
101
- return;
102
- }
103
- queue.push(message);
104
- if (connected && !draining) void drain();
101
+ if (!("id" in message)) {
102
+ void shutdown(message.signal);
103
+ return;
104
+ }
105
+ queue.push(message);
106
+ if (connected && !draining)
107
+ void drain();
105
108
  }
106
-
107
109
  async function startup(connectionString, ready, timeout) {
108
- readyFlag = new Int32Array(ready);
109
- statementTimeout = timeout;
110
- try {
111
- client = new pg.Client({
112
- connectionString,
113
- application_name: "opencode-chat-room",
114
- statement_timeout: statementTimeout,
110
+ readyFlag = new Int32Array(ready);
111
+ statementTimeout = timeout;
112
+ try {
113
+ const db = new pg.Client({
114
+ connectionString,
115
+ application_name: "opencode-chat-room",
116
+ statement_timeout: statementTimeout,
117
+ });
118
+ client = db;
119
+ await db.connect();
120
+ connected = true;
121
+ port?.postMessage({ type: "ready" });
122
+ signalReady();
123
+ if (queue.length && !draining)
124
+ void drain();
125
+ }
126
+ catch (error) {
127
+ port?.postMessage({ type: "fatal", error: serialize(error) });
128
+ signalReady();
129
+ }
130
+ }
131
+ const parent = parentPort;
132
+ if (parent) {
133
+ parent.on("message", (message) => {
134
+ if (message?.type !== "init" || client)
135
+ return;
136
+ port = message.port;
137
+ port.on("message", handlePortMessage);
138
+ parent.close();
139
+ void startup(message.connectionString, message.ready, message.statementTimeout ?? 4_500);
115
140
  });
116
- await client.connect();
117
- connected = true;
118
- port.postMessage({ type: "ready" });
119
- signalReady();
120
- if (queue.length && !draining) void drain();
121
- } catch (error) {
122
- port.postMessage({ type: "fatal", error: serialize(error) });
123
- signalReady();
124
- }
125
141
  }
126
-
127
- parentPort.on("message", (message) => {
128
- if (message?.type !== "init" || client) return;
129
- port = message.port;
130
- port.on("message", handlePortMessage);
131
- parentPort.close();
132
- void startup(message.connectionString, message.ready, message.statementTimeout ?? 4_500);
133
- });
@@ -0,0 +1,166 @@
1
+ import { parentPort, type MessagePort } from "node:worker_threads";
2
+ import pg from "pg";
3
+ import { hasSql, translate } from "./pg-sql.mjs";
4
+
5
+ // One pg.Client per worker: transactions and their advisory locks belong to a
6
+ // single session, so a pool would be incorrect here.
7
+ //
8
+ // The worker deliberately starts with no workerData. Some Bun versions fail to
9
+ // load `pg` in a worker that received any workerData at startup. The main
10
+ // thread sends one init message over the default worker channel instead; that
11
+ // message also transfers the request MessagePort and the ready SharedArrayBuffer.
12
+
13
+ type Request = {
14
+ id: number;
15
+ mode: "all" | "get" | "run" | "exec";
16
+ sql: string;
17
+ params?: unknown[];
18
+ signal: SharedArrayBuffer;
19
+ };
20
+
21
+ type PortMessage = Request | { type: "close"; signal?: SharedArrayBuffer };
22
+
23
+ type InitMessage = {
24
+ type: "init";
25
+ connectionString: string;
26
+ ready: SharedArrayBuffer;
27
+ statementTimeout?: number;
28
+ port: MessagePort;
29
+ };
30
+
31
+ type SerializedError = { message: string; [field: string]: string | number | undefined };
32
+
33
+ const ERROR_FIELDS = [
34
+ "name", "code", "detail", "hint", "severity",
35
+ "position", "constraint", "table", "column", "dataType",
36
+ ] as const;
37
+
38
+ let client: pg.Client | undefined;
39
+ let port: MessagePort | undefined;
40
+ let readyFlag: Int32Array | undefined;
41
+ let statementTimeout = 4_500;
42
+ let connected = false;
43
+ let draining = false;
44
+ let closing = false;
45
+ const queue: Request[] = [];
46
+
47
+ // SQLite returns all integers as JS numbers; int8 is a string in node-postgres
48
+ // by default and COUNT(*) / BIGINT columns must stay numbers.
49
+ pg.types.setTypeParser(20, (value) => Number(value));
50
+ pg.types.setTypeParser(1700, (value) => Number(value));
51
+
52
+ function normalize(params: unknown[]): unknown[] {
53
+ return params.map((value) =>
54
+ value === undefined ? null : typeof value === "boolean" ? (value ? 1 : 0) : value);
55
+ }
56
+
57
+ function serialize(error: unknown): SerializedError {
58
+ const source = (typeof error === "object" && error !== null ? error : {}) as Record<string, unknown>;
59
+ const result: SerializedError = {
60
+ message: typeof source.message === "string" ? source.message : String(error),
61
+ };
62
+ for (const key of ERROR_FIELDS) {
63
+ const value = source[key];
64
+ if (typeof value === "string" || typeof value === "number") result[key] = value;
65
+ }
66
+ return result;
67
+ }
68
+
69
+ function signalReady(): void {
70
+ if (!readyFlag) return;
71
+ Atomics.store(readyFlag, 0, 1);
72
+ Atomics.notify(readyFlag, 0);
73
+ }
74
+
75
+ function reply(request: Request, payload: Record<string, unknown>): void {
76
+ port?.postMessage({ id: request.id, ...payload });
77
+ const flag = new Int32Array(request.signal);
78
+ Atomics.store(flag, 0, 1);
79
+ Atomics.notify(flag, 0);
80
+ }
81
+
82
+ async function handle(request: Request): Promise<void> {
83
+ const db = client;
84
+ if (!db) {
85
+ return reply(request, { ok: false, error: { message: "PostgreSQL worker is not connected" } });
86
+ }
87
+ try {
88
+ const text = translate(request.sql);
89
+ if (!hasSql(text)) return reply(request, { ok: true, rows: [], row: null, changes: 0 });
90
+ const params = normalize(request.params ?? []);
91
+ // exec without params uses the simple-query protocol so multi-statement
92
+ // schema scripts (and BEGIN/COMMIT) run in one round trip.
93
+ const result = request.mode === "exec" && params.length === 0
94
+ ? await db.query(text)
95
+ : await db.query({ text, values: params });
96
+ if (request.mode === "get") return reply(request, { ok: true, row: result.rows?.[0] ?? null });
97
+ if (request.mode === "all") return reply(request, { ok: true, rows: result.rows ?? [] });
98
+ const last = Array.isArray(result) ? result.at(-1) : result;
99
+ // Identity columns are never read through Database.run in the state layer;
100
+ // keep the API shape without pretending to know an insert id.
101
+ return reply(request, { ok: true, changes: last?.rowCount ?? 0, lastInsertRowid: 0 });
102
+ } catch (error) {
103
+ return reply(request, { ok: false, error: serialize(error) });
104
+ }
105
+ }
106
+
107
+ async function drain(): Promise<void> {
108
+ draining = true;
109
+ while (queue.length && !closing) {
110
+ const request = queue.shift();
111
+ if (request) await handle(request);
112
+ }
113
+ draining = false;
114
+ }
115
+
116
+ async function shutdown(signal?: SharedArrayBuffer): Promise<void> {
117
+ closing = true;
118
+ try { await client?.end(); } catch { /* connection already gone */ }
119
+ try { port?.close(); } catch { /* already closed */ }
120
+ if (signal) {
121
+ const flag = new Int32Array(signal);
122
+ Atomics.store(flag, 0, 1);
123
+ Atomics.notify(flag, 0);
124
+ }
125
+ }
126
+
127
+ function handlePortMessage(message: PortMessage): void {
128
+ if (!("id" in message)) {
129
+ void shutdown(message.signal);
130
+ return;
131
+ }
132
+ queue.push(message);
133
+ if (connected && !draining) void drain();
134
+ }
135
+
136
+ async function startup(connectionString: string, ready: SharedArrayBuffer, timeout: number): Promise<void> {
137
+ readyFlag = new Int32Array(ready);
138
+ statementTimeout = timeout;
139
+ try {
140
+ const db = new pg.Client({
141
+ connectionString,
142
+ application_name: "opencode-chat-room",
143
+ statement_timeout: statementTimeout,
144
+ });
145
+ client = db;
146
+ await db.connect();
147
+ connected = true;
148
+ port?.postMessage({ type: "ready" });
149
+ signalReady();
150
+ if (queue.length && !draining) void drain();
151
+ } catch (error) {
152
+ port?.postMessage({ type: "fatal", error: serialize(error) });
153
+ signalReady();
154
+ }
155
+ }
156
+
157
+ const parent = parentPort;
158
+ if (parent) {
159
+ parent.on("message", (message: InitMessage) => {
160
+ if (message?.type !== "init" || client) return;
161
+ port = message.port;
162
+ port.on("message", handlePortMessage);
163
+ parent.close();
164
+ void startup(message.connectionString, message.ready, message.statementTimeout ?? 4_500);
165
+ });
166
+ }
@@ -144,9 +144,25 @@ const MAX_LINES = 20;
144
144
  // Display lines for a push. `capped` means the notification is lossy because
145
145
  // lines were folded or message text was truncated; callers must not advance
146
146
  // the watermark until a full poll confirms it.
147
- const MENTION_MARKER = /<at>([\s\S]*?)<\/at>/g;
148
- // `<at>@name</at>` is an editor token; notifications and receipts show the plain mention.
149
- export const visibleText = (text: string) => text.replace(MENTION_MARKER, "$1");
147
+ function notificationPreview(text: string): string {
148
+ if (text.length <= MAX_LINE) return text;
149
+ let end = MAX_LINE;
150
+ for (const mention of text.matchAll(/<at>@[^<\r\n]+<\/at>/g)) {
151
+ if (mention.index >= end) break;
152
+ if (mention.index + mention[0].length > end) {
153
+ end = mention.index;
154
+ break;
155
+ }
156
+ }
157
+ return `${text.slice(0, end)}…`;
158
+ }
159
+
160
+ export function notificationMessageLine(
161
+ item: { createdAt: number; senderId: string; text: string },
162
+ text = item.text,
163
+ ): string {
164
+ return `- [${timeOf(item.createdAt)}] ${item.senderId}: ${text}`;
165
+ }
150
166
 
151
167
  export function formatNotificationLines(
152
168
  items: ReadonlyArray<{ createdAt: number; senderId: string; text: string; answerQuestionExcerpt?: string }>,
@@ -161,14 +177,12 @@ export function formatNotificationLines(
161
177
  lineCount += required;
162
178
  }
163
179
  const countCapped = visible.length < items.length;
164
- const textCapped = items.some((it) => visibleText(it.text).length > MAX_LINE);
180
+ const textCapped = items.some((it) => it.text.length > MAX_LINE);
165
181
  const capped = countCapped || textCapped;
166
182
  const lines = visible.flatMap(({ item: it }) => {
167
- const plain = visibleText(it.text);
168
- const text = plain.length > MAX_LINE ? `${plain.slice(0, MAX_LINE)}\u2026` : plain;
169
183
  return [
170
184
  ...(it.answerQuestionExcerpt ? [` ↳ Answer to: “${it.answerQuestionExcerpt.replace(/[\r\n]+/g, " ")}”`] : []),
171
- `- [${timeOf(it.createdAt)}] ${it.senderId}: ${text}`,
185
+ notificationMessageLine(it, notificationPreview(it.text)),
172
186
  ];
173
187
  });
174
188
  if (countCapped) {
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,175 @@
1
+ // Shared ChatApp state and option shapes. Attach modules extend ChatApp by
2
+ // declaration merging; they must not redeclare the fields listed here.
3
+ import type { ChatApi } from "./http.mjs";
4
+ import type { Language } from "./i18n.mjs";
5
+ import type { ChatMessage, Participant } from "./messages.mjs";
6
+ import type { Command, SearchResult } from "./palette.mjs";
7
+ import type { Theme, TuiPreferences } from "./prefs.mjs";
8
+ import type { JoinRequest, Resource, RoomWire } from "./wire.mjs";
9
+
10
+ export type ConnectionState = "connecting" | "online" | "offline" | "reconnecting";
11
+ export type StatusKind = "info" | "error" | "success";
12
+ export type FocusTarget = "rooms" | "composer";
13
+ export type TimerHandle = ReturnType<typeof setTimeout>;
14
+
15
+ export interface CurrentRoom {
16
+ id: string;
17
+ name: string;
18
+ participants: Participant[];
19
+ }
20
+
21
+ export interface RoomSummary extends CurrentRoom {
22
+ joined: boolean;
23
+ }
24
+
25
+ export interface PendingJoin {
26
+ roomId: string;
27
+ message: string;
28
+ }
29
+
30
+ export interface PendingSend {
31
+ text: string;
32
+ clientMessageID: string;
33
+ }
34
+
35
+ export type AppPrefs = Required<TuiPreferences>;
36
+
37
+ export interface AppSettings {
38
+ server?: string;
39
+ participant?: string;
40
+ language?: string;
41
+ theme?: string;
42
+ token?: string;
43
+ sessionFile?: string;
44
+ configFile?: string;
45
+ prefsFile?: string;
46
+ requestTimeoutMs?: number;
47
+ pollTimeoutMs?: number;
48
+ }
49
+
50
+ // The room payload as the settings editor reads it: nullable wire fields are
51
+ // folded to undefined so the markdown formatter gets plain optionals.
52
+ export interface SettingsRoom {
53
+ id: string;
54
+ name?: string;
55
+ description?: string;
56
+ ownerId?: string;
57
+ adminIds?: string[];
58
+ adminSessionID?: string;
59
+ successorId?: string;
60
+ joinPolicy?: string;
61
+ kind?: string;
62
+ }
63
+
64
+ export interface RoomSettingsState {
65
+ room: SettingsRoom;
66
+ canEdit: boolean;
67
+ canDelete: boolean;
68
+ }
69
+
70
+ // The palette is one mutable object whose staged fields drive the overlay:
71
+ // commands/search, then an info list, then prompt/confirm/detail overlays.
72
+ export type PaletteMode = "commands" | "search" | "info" | "prompt" | "detail" | "confirm";
73
+ export type PaletteScope = "room" | "all";
74
+
75
+ export type PaletteData = Record<string, string | undefined>;
76
+
77
+ export interface PalettePrompt {
78
+ label: string;
79
+ value: string;
80
+ action: string;
81
+ data: PaletteData;
82
+ }
83
+
84
+ export interface PaletteConfirm {
85
+ text: string;
86
+ action: string;
87
+ data: PaletteData;
88
+ }
89
+
90
+ export interface PalettePrevious {
91
+ mode: PaletteMode;
92
+ hint?: string;
93
+ }
94
+
95
+ export interface PaletteDetail {
96
+ title: string;
97
+ markdown: string;
98
+ kind?: string;
99
+ }
100
+
101
+ // One row shape covers every info list; each kind fills only its own fields.
102
+ export interface PaletteItem {
103
+ name: string;
104
+ value: string;
105
+ content?: string;
106
+ text?: string;
107
+ open?: boolean;
108
+ paused?: boolean;
109
+ resource?: Resource;
110
+ request?: JoinRequest;
111
+ }
112
+
113
+ export interface PaletteState {
114
+ mode: PaletteMode;
115
+ kind?: string;
116
+ title?: string;
117
+ subtitle?: string;
118
+ roomId?: string;
119
+ roomParticipants?: Participant[];
120
+ roomMeta?: RoomWire;
121
+ items: PaletteItem[];
122
+ selected: number;
123
+ error: string;
124
+ loading?: boolean;
125
+ hint?: string;
126
+ query?: string;
127
+ scope?: PaletteScope;
128
+ results: SearchResult[];
129
+ commands?: Command[];
130
+ searching: boolean;
131
+ // ui-content models a missing prompt as an absent optional; runtime writers
132
+ // may also leave null, which every reader treats the same.
133
+ prompt?: PalettePrompt;
134
+ confirm: PaletteConfirm | null;
135
+ detail: PaletteDetail | null;
136
+ previous?: PalettePrevious | null;
137
+ }
138
+
139
+ export interface ChatState {
140
+ host: string;
141
+ participant: string;
142
+ theme: Theme;
143
+ language: Language;
144
+ connection: ConnectionState;
145
+ rooms: RoomSummary[];
146
+ selected: number;
147
+ current: CurrentRoom | null;
148
+ joined: boolean;
149
+ messages: ChatMessage[];
150
+ pending: PendingJoin | null;
151
+ focus: FocusTarget;
152
+ draft: string;
153
+ highlight: string | null;
154
+ help: boolean;
155
+ status: string;
156
+ statusKind: StatusKind;
157
+ palette: PaletteState | null;
158
+ quit: boolean;
159
+ }
160
+
161
+ export type AppListener = (state: ChatState) => void;
162
+ export type Schedule = (fn: () => void, ms: number) => TimerHandle | undefined;
163
+ export type SavePrefs = (prefs: TuiPreferences) => unknown;
164
+
165
+ export interface ChatAppOptions {
166
+ api: ChatApi;
167
+ sessionID: string;
168
+ participant: string;
169
+ host?: string;
170
+ autoPoll?: boolean;
171
+ settings?: AppSettings;
172
+ savePrefs?: SavePrefs | null;
173
+ downloadDir?: string;
174
+ schedule?: Schedule;
175
+ }