@alexsun-top/opencode-chat-room 0.8.4 → 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.
- package/CHANGELOG.md +5 -1
- package/README.md +4 -4
- package/README.zh-CN.md +4 -4
- package/cli.ts +1 -1
- package/dist/cli.js +35 -16
- package/package.json +14 -11
- package/packages/chat-room-channeld/README.md +3 -2
- package/packages/chat-room-channeld/src/inbound.ts +3 -6
- package/packages/chat-room-channeld/src/pg/sql.mjs +157 -142
- package/packages/chat-room-channeld/src/pg/sql.mts +175 -0
- package/packages/chat-room-channeld/src/pg/worker.mjs +107 -99
- package/packages/chat-room-channeld/src/pg/worker.mts +166 -0
- package/plugin.ts +1 -1
- package/server/assets/chat/admin.js +4 -1
- package/server/assets/chat/announcements.js +3 -0
- package/server/assets/chat/attachments.js +3 -3
- package/server/assets/chat/clipboard.js +1 -1
- package/server/assets/chat/copy-action.js +1 -1
- package/server/assets/chat/doc-preview.js +1 -1
- package/server/assets/chat/editor.js +11 -7
- package/server/assets/chat/events.js +7 -5
- package/server/assets/chat/files.js +5 -1
- package/server/assets/chat/integration-editor.js +2 -0
- package/server/assets/chat/integrations.js +1 -1
- package/server/assets/chat/join-requests.js +1 -1
- package/server/assets/chat/markdown-footnotes.js +8 -7
- package/server/assets/chat/markdown.js +6 -5
- package/server/assets/chat/members.js +3 -3
- package/server/assets/chat/mentions.js +8 -5
- package/server/assets/chat/message-cache.js +1 -1
- package/server/assets/chat/message-polling.js +2 -2
- package/server/assets/chat/message-window.js +1 -1
- package/server/assets/chat/messages.js +17 -3
- package/server/assets/chat/office-preview.js +3 -3
- package/server/assets/chat/palette.js +5 -3
- package/server/assets/chat/panels.js +19 -0
- package/server/assets/chat/questions.js +3 -2
- package/server/assets/chat/reminders.js +3 -0
- package/server/assets/chat/replies.js +6 -3
- package/server/assets/chat/resource-format.js +2 -2
- package/server/assets/chat/rooms.js +7 -4
- package/server/assets/chat/settings.js +4 -3
- package/server/assets/chat/shortcuts.js +3 -1
- package/server/assets/chat/side-pane.js +1 -1
- package/server/assets/chat/state.js +30 -11
- package/server/assets/chat/system-events.js +3 -2
- package/server/assets/chat/system-messages.js +15 -15
- package/server/assets/chat/tasks.js +4 -3
- package/server/assets/chat/window-controls.js +3 -2
- package/server/assets/conversation.css +14 -2
- package/server/assets/doc-preview.css +6 -0
- package/server/assets/office/THIRD-PARTY-NOTICES.txt +23 -1
- package/server/assets/office/licenses/docx-preview-HEADER.txt +6 -0
- package/server/assets/office/licenses/echarts-LICENSE-d3.txt +27 -0
- package/server/assets/office/licenses/echarts-NOTICE.txt +5 -0
- package/server/assets/office/licenses/immediate-LICENSE.txt +20 -0
- package/server/assets/office/licenses/lie-LICENSE.txt +7 -0
- package/server/assets/office/licenses/pako-LICENSE.txt +21 -0
- package/server/assets/office/licenses/pako-ZLIB.txt +18 -0
- package/server/assets/office/licenses/setimmediate-LICENSE.txt +20 -0
- package/server/sw.js +3 -1
- package/skills/chatroom/SKILL.md +9 -0
- package/src/README.md +1 -1
- package/src/agent/agent-receipts.ts +12 -4
- package/src/center/center-client.ts +1 -1
- package/src/state/pg-sql.mjs +157 -142
- package/src/state/pg-sql.mts +175 -0
- package/src/state/pg-worker.mjs +107 -99
- package/src/state/pg-worker.mts +166 -0
- package/src/state/store.ts +21 -7
- package/src/tui/app-types.mjs +1 -0
- package/src/tui/app-types.mts +175 -0
- package/src/tui/app.mjs +426 -423
- package/src/tui/app.mts +500 -0
- package/src/tui/config.mjs +166 -158
- package/src/tui/config.mts +247 -0
- package/src/tui/http.mjs +239 -206
- package/src/tui/http.mts +412 -0
- package/src/tui/i18n.mjs +17 -20
- package/src/tui/i18n.mts +42 -0
- package/src/tui/info-app.mjs +431 -427
- package/src/tui/info-app.mts +500 -0
- package/src/tui/info-content.mjs +205 -199
- package/src/tui/info-content.mts +341 -0
- package/src/tui/join-app.mjs +75 -64
- package/src/tui/join-app.mts +99 -0
- package/src/tui/locales/en.mjs +302 -320
- package/src/tui/locales/en.mts +349 -0
- package/src/tui/locales/zh-CN.mjs +302 -320
- package/src/tui/locales/zh-CN.mts +349 -0
- package/src/tui/main.mjs +219 -213
- package/src/tui/main.mts +255 -0
- package/src/tui/markdown.mjs +128 -272
- package/src/tui/markdown.mts +152 -0
- package/src/tui/mentions.mjs +181 -184
- package/src/tui/mentions.mts +251 -0
- package/src/tui/message-app.mjs +181 -173
- package/src/tui/message-app.mts +213 -0
- package/src/tui/message-view.mjs +90 -111
- package/src/tui/message-view.mts +151 -0
- package/src/tui/messages.mjs +55 -49
- package/src/tui/messages.mts +106 -0
- package/src/tui/palette-app.mjs +364 -344
- package/src/tui/palette-app.mts +388 -0
- package/src/tui/palette.mjs +100 -98
- package/src/tui/palette.mts +176 -0
- package/src/tui/prefs.mjs +56 -63
- package/src/tui/prefs.mts +126 -0
- package/src/tui/reminder-app.mjs +191 -181
- package/src/tui/reminder-app.mts +228 -0
- package/src/tui/room-settings-app.mjs +257 -242
- package/src/tui/room-settings-app.mts +301 -0
- package/src/tui/session.mjs +30 -29
- package/src/tui/session.mts +51 -0
- package/src/tui/settings-app.mjs +151 -138
- package/src/tui/settings-app.mts +179 -0
- package/src/tui/ui-content.mjs +426 -421
- package/src/tui/ui-content.mts +499 -0
- package/src/tui/ui-help.mjs +58 -67
- package/src/tui/ui-help.mts +87 -0
- package/src/tui/ui-mentions.mjs +34 -36
- package/src/tui/ui-mentions.mts +60 -0
- package/src/tui/ui-overlay-view.mjs +259 -267
- package/src/tui/ui-overlay-view.mts +322 -0
- package/src/tui/ui-suggest-view.mjs +54 -57
- package/src/tui/ui-suggest-view.mts +78 -0
- package/src/tui/ui-suggest.mjs +164 -167
- package/src/tui/ui-suggest.mts +248 -0
- package/src/tui/ui-theme.mjs +52 -57
- package/src/tui/ui-theme.mts +98 -0
- package/src/tui/ui-view.mjs +267 -275
- package/src/tui/ui-view.mts +308 -0
- package/src/tui/ui.mjs +372 -359
- package/src/tui/ui.mts +440 -0
- package/src/tui/wire.mjs +215 -0
- package/src/tui/wire.mts +383 -0
package/src/state/pg-worker.mjs
CHANGED
|
@@ -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
|
-
|
|
6
|
-
|
|
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
|
-
|
|
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
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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
|
-
|
|
50
|
-
|
|
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
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
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
|
-
|
|
83
|
-
|
|
84
|
-
|
|
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
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
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
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
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
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
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
|
+
}
|
package/src/state/store.ts
CHANGED
|
@@ -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
|
-
|
|
148
|
-
|
|
149
|
-
|
|
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) =>
|
|
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
|
-
|
|
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
|
+
}
|