@alexsun-top/opencode-chat-room 0.8.4 → 0.8.6
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 +17 -3
- package/README.md +5 -5
- package/README.zh-CN.md +5 -5
- package/cli.ts +20 -1
- package/dist/cli.js +3785 -1462
- package/package.json +19 -11
- package/packages/chat-room-channeld/README.md +16 -10
- package/packages/chat-room-channeld/src/delivery.ts +0 -4
- package/packages/chat-room-channeld/src/host.ts +48 -37
- package/packages/chat-room-channeld/src/inbound.ts +3 -6
- package/packages/chat-room-channeld/src/pg/database.ts +141 -49
- 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 +148 -99
- package/packages/chat-room-channeld/src/pg/worker.mts +201 -0
- package/packages/chat-room-channeld/src/state-rows.ts +132 -0
- package/packages/chat-room-channeld/src/state.ts +129 -157
- package/plugin.ts +21 -5
- package/server/app.ts +9 -0
- package/server/assets/base.css +22 -2
- package/server/assets/chat/admin.js +17 -1
- package/server/assets/chat/announcements.js +79 -34
- package/server/assets/chat/attachments.js +3 -3
- package/server/assets/chat/clipboard.js +1 -1
- package/server/assets/chat/confirm-dialog.js +3 -0
- package/server/assets/chat/copy-action.js +1 -1
- package/server/assets/chat/create-room-dialog.js +7 -0
- package/server/assets/chat/diagram-viewer.js +4 -8
- package/server/assets/chat/doc-preview.js +71 -31
- package/server/assets/chat/editor.js +49 -10
- package/server/assets/chat/events.js +13 -6
- package/server/assets/chat/files.js +33 -27
- package/server/assets/chat/history-loader.js +31 -15
- package/server/assets/chat/integration-editor.js +27 -1
- package/server/assets/chat/integrations.js +108 -16
- package/server/assets/chat/join-requests.js +40 -2
- package/server/assets/chat/locales/de.js +5 -3
- package/server/assets/chat/locales/es.js +5 -3
- package/server/assets/chat/locales/fr.js +5 -3
- package/server/assets/chat/locales/it.js +5 -3
- package/server/assets/chat/locales/ja.js +5 -3
- package/server/assets/chat/locales/ko.js +5 -3
- package/server/assets/chat/markdown-footnotes.js +27 -15
- package/server/assets/chat/markdown.js +6 -12
- package/server/assets/chat/members.js +34 -22
- package/server/assets/chat/mentions.js +8 -5
- package/server/assets/chat/message-cache.js +9 -3
- package/server/assets/chat/message-polling.js +10 -4
- package/server/assets/chat/message-window.js +58 -1
- package/server/assets/chat/messages.js +90 -37
- package/server/assets/chat/office-preview.js +3 -3
- package/server/assets/chat/operator.js +624 -0
- package/server/assets/chat/palette.js +7 -10
- package/server/assets/chat/panels.js +42 -0
- package/server/assets/chat/questions.js +75 -58
- package/server/assets/chat/recall.js +14 -5
- package/server/assets/chat/receipts.js +63 -7
- package/server/assets/chat/refresh-timing.js +6 -0
- package/server/assets/chat/reminders.js +47 -13
- package/server/assets/chat/replies.js +39 -25
- package/server/assets/chat/resource-format.js +6 -3
- package/server/assets/chat/room-list.js +202 -3
- package/server/assets/chat/room-watcher.js +3 -1
- package/server/assets/chat/rooms.js +29 -7
- package/server/assets/chat/settings.js +37 -6
- package/server/assets/chat/shortcuts.js +3 -1
- package/server/assets/chat/side-pane.js +1 -1
- package/server/assets/chat/state.js +51 -18
- package/server/assets/chat/system-events.js +3 -2
- package/server/assets/chat/system-messages.js +15 -15
- package/server/assets/chat/tasks.js +37 -14
- package/server/assets/chat/window-controls.js +25 -15
- package/server/assets/code-highlight.css +0 -13
- package/server/assets/conversation.css +45 -5
- package/server/assets/doc-preview.css +9 -0
- package/server/assets/files.css +4 -0
- package/server/assets/monaco/integration-rules.js +1 -1
- 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/assets/responsive.css +12 -2
- package/server/assets/settings.css +3 -1
- package/server/assets/tasks.css +6 -1
- package/server/assets/window-controls.css +3 -1
- package/server/channels.ts +3 -2
- package/server/chat.html +7 -2
- package/server/inbox.ts +3 -1
- package/server/join-requests.ts +8 -1
- package/server/member-routes.ts +16 -2
- package/server/newrooms.ts +2 -13
- package/server/operator-auth.ts +21 -0
- package/server/operator-routes.ts +150 -0
- package/server/operator-state.ts +64 -0
- package/server/operator.css +139 -0
- package/server/room-owner.ts +17 -0
- package/server/routes/inbox.ts +15 -5
- package/server/routes/pages.ts +12 -2
- package/server/routes/room-actions.ts +4 -3
- package/server/sw.js +3 -1
- package/server/system.html +109 -0
- package/server/wake.ts +1 -1
- package/skills/chatroom/SKILL.md +9 -0
- package/src/README.md +1 -1
- package/src/acp/client.ts +254 -0
- package/src/acp/protocol.ts +300 -0
- package/src/acp/watch.ts +498 -0
- package/src/agent/agent-receipts.ts +98 -8
- package/src/center/center-client.ts +6 -1
- package/src/center/center-inbox.ts +54 -7
- package/src/center/center-wake.ts +57 -3
- package/src/center/forward-client.ts +48 -0
- package/src/center/mentions.ts +16 -3
- package/src/center/notification-delay.ts +139 -0
- package/src/center/queue.ts +3 -0
- package/src/config/file-config.ts +25 -0
- package/src/mcp/server.ts +389 -0
- package/src/protocol/config.ts +7 -0
- package/src/runtime/plugin-notifications.ts +43 -9
- package/src/runtime/v2-plugin.ts +14 -3
- package/src/runtime/v2-receiver.ts +10 -2
- package/src/runtime/watch.ts +5 -1
- package/src/state/pg-database.ts +133 -49
- package/src/state/pg-sql.mjs +157 -142
- package/src/state/pg-sql.mts +175 -0
- package/src/state/pg-worker.mjs +148 -99
- package/src/state/pg-worker.mts +201 -0
- package/src/state/receiver-state.ts +47 -0
- package/src/state/sqlite-database.ts +6 -2
- package/src/state/sqlite-members.ts +48 -4
- package/src/state/sqlite-messages.ts +25 -12
- package/src/state/sqlite-schema.ts +4 -1
- package/src/state/sqlite-state.ts +1 -1
- package/src/state/store.ts +31 -11
- package/src/tui/app-types.mjs +1 -0
- package/src/tui/app-types.mts +175 -0
- package/src/tui/app.mjs +473 -423
- package/src/tui/app.mts +543 -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 +275 -242
- package/src/tui/room-settings-app.mts +318 -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/web/integration-rules-editor.ts +25 -2
package/src/tui/config.mjs
CHANGED
|
@@ -4,199 +4,207 @@ import { existsSync, readFileSync } from "node:fs";
|
|
|
4
4
|
import os from "node:os";
|
|
5
5
|
import path from "node:path";
|
|
6
6
|
import { parse as parseYaml } from "yaml";
|
|
7
|
-
|
|
8
7
|
export const FILE_NAMES = ["chatroom.json", "chatroom.jsonc", "chatroom.yaml", "chatroom.yml"];
|
|
9
8
|
export const DEFAULT_PARTICIPANT = "chatroom-tui";
|
|
10
9
|
export const DEFAULT_REQUEST_TIMEOUT_MS = 10000;
|
|
11
|
-
|
|
12
10
|
const URL_HINT = "set CHAT_ROOM_SERVER_URL or client.url in your chatroom config";
|
|
13
|
-
|
|
14
11
|
function asRecord(value, where) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
12
|
+
if (value === undefined)
|
|
13
|
+
return undefined;
|
|
14
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
15
|
+
throw new Error(`chatroom TUI: ${where} must be an object`);
|
|
16
|
+
}
|
|
17
|
+
return value;
|
|
20
18
|
}
|
|
21
|
-
|
|
22
19
|
function optionalString(value, where) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
20
|
+
if (value === undefined)
|
|
21
|
+
return undefined;
|
|
22
|
+
if (typeof value !== "string")
|
|
23
|
+
throw new Error(`chatroom TUI: ${where} must be a string`);
|
|
24
|
+
return value;
|
|
26
25
|
}
|
|
27
|
-
|
|
28
26
|
function optionalNumber(value, where) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
27
|
+
if (value === undefined)
|
|
28
|
+
return undefined;
|
|
29
|
+
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
30
|
+
throw new Error(`chatroom TUI: ${where} must be a finite number`);
|
|
31
|
+
}
|
|
32
|
+
return value;
|
|
34
33
|
}
|
|
35
|
-
|
|
36
34
|
function parseClient(value) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
35
|
+
const raw = asRecord(value, "client");
|
|
36
|
+
if (!raw)
|
|
37
|
+
return undefined;
|
|
38
|
+
const requestTimeoutMs = optionalNumber(raw.requestTimeoutMs, "client.requestTimeoutMs");
|
|
39
|
+
if (requestTimeoutMs !== undefined && requestTimeoutMs <= 0) {
|
|
40
|
+
throw new Error("chatroom TUI: client.requestTimeoutMs must be greater than zero");
|
|
41
|
+
}
|
|
42
|
+
return {
|
|
43
|
+
url: optionalString(raw.url, "client.url"),
|
|
44
|
+
token: optionalString(raw.token, "client.token"),
|
|
45
|
+
participant: optionalString(raw.participant, "client.participant"),
|
|
46
|
+
language: optionalString(raw.language, "client.language"),
|
|
47
|
+
theme: optionalString(raw.theme, "client.theme"),
|
|
48
|
+
requestTimeoutMs,
|
|
49
|
+
};
|
|
51
50
|
}
|
|
52
|
-
|
|
53
51
|
function parseConfig(value) {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
52
|
+
const raw = asRecord(value, "root");
|
|
53
|
+
if (!raw)
|
|
54
|
+
return {};
|
|
55
|
+
return {
|
|
56
|
+
url: optionalString(raw.url, "url"),
|
|
57
|
+
token: optionalString(raw.token, "token"),
|
|
58
|
+
language: optionalString(raw.language, "language"),
|
|
59
|
+
theme: optionalString(raw.theme, "theme"),
|
|
60
|
+
client: parseClient(raw.client),
|
|
61
|
+
};
|
|
63
62
|
}
|
|
64
|
-
|
|
65
63
|
/** Remove comments and trailing commas without touching quoted strings. */
|
|
66
64
|
function stripJsonc(source) {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
65
|
+
let out = "";
|
|
66
|
+
let quote = "";
|
|
67
|
+
let escaped = false;
|
|
68
|
+
let comma = -1;
|
|
69
|
+
for (let i = 0; i < source.length; i++) {
|
|
70
|
+
const char = source.charAt(i);
|
|
71
|
+
const next = source.charAt(i + 1);
|
|
72
|
+
if (quote) {
|
|
73
|
+
out += char;
|
|
74
|
+
if (escaped)
|
|
75
|
+
escaped = false;
|
|
76
|
+
else if (char === "\\")
|
|
77
|
+
escaped = true;
|
|
78
|
+
else if (char === quote)
|
|
79
|
+
quote = "";
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
if (char === '"') {
|
|
83
|
+
quote = char;
|
|
84
|
+
comma = -1;
|
|
85
|
+
out += char;
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
if (char === "/" && next === "/") {
|
|
89
|
+
while (i < source.length && source.charAt(i) !== "\n")
|
|
90
|
+
i++;
|
|
91
|
+
out += "\n";
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
if (char === "/" && next === "*") {
|
|
95
|
+
i += 2;
|
|
96
|
+
while (i < source.length && !(source.charAt(i) === "*" && source.charAt(i + 1) === "/"))
|
|
97
|
+
i++;
|
|
98
|
+
i++;
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
if (char === ",") {
|
|
102
|
+
comma = out.length;
|
|
103
|
+
out += char;
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
if (char === "}" || char === "]") {
|
|
107
|
+
if (comma >= 0)
|
|
108
|
+
out = out.slice(0, comma) + out.slice(comma + 1);
|
|
109
|
+
comma = -1;
|
|
110
|
+
out += char;
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
if (!/\s/.test(char))
|
|
114
|
+
comma = -1;
|
|
115
|
+
out += char;
|
|
102
116
|
}
|
|
103
|
-
|
|
104
|
-
if (comma >= 0) out = out.slice(0, comma) + out.slice(comma + 1);
|
|
105
|
-
comma = -1;
|
|
106
|
-
out += char;
|
|
107
|
-
continue;
|
|
108
|
-
}
|
|
109
|
-
if (!/\s/.test(char)) comma = -1;
|
|
110
|
-
out += char;
|
|
111
|
-
}
|
|
112
|
-
return out;
|
|
117
|
+
return out;
|
|
113
118
|
}
|
|
114
|
-
|
|
115
119
|
function parseFile(file) {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
if (extension === ".yaml" || extension === ".yml") return parseConfig(parseYaml(text));
|
|
125
|
-
if (extension === ".jsonc") return parseConfig(JSON.parse(stripJsonc(text)));
|
|
126
|
-
if (extension === ".json") return parseConfig(JSON.parse(text));
|
|
120
|
+
let text;
|
|
121
|
+
try {
|
|
122
|
+
text = readFileSync(file, "utf8");
|
|
123
|
+
}
|
|
124
|
+
catch {
|
|
125
|
+
throw new Error(`chatroom TUI: cannot read ${file}`);
|
|
126
|
+
}
|
|
127
|
+
const extension = path.extname(file).toLowerCase();
|
|
127
128
|
try {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
129
|
+
if (extension === ".yaml" || extension === ".yml")
|
|
130
|
+
return parseConfig(parseYaml(text));
|
|
131
|
+
if (extension === ".jsonc")
|
|
132
|
+
return parseConfig(JSON.parse(stripJsonc(text)));
|
|
133
|
+
if (extension === ".json")
|
|
134
|
+
return parseConfig(JSON.parse(text));
|
|
135
|
+
try {
|
|
136
|
+
return parseConfig(JSON.parse(text));
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
return parseConfig(parseYaml(text));
|
|
140
|
+
}
|
|
131
141
|
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
142
|
+
catch (error) {
|
|
143
|
+
if (error instanceof SyntaxError || (error instanceof Error && error.name === "YAMLParseError")) {
|
|
144
|
+
// Never include parser source text: a malformed file may contain a token.
|
|
145
|
+
throw new Error(`chatroom TUI: ${file} is not valid JSON/YAML`);
|
|
146
|
+
}
|
|
147
|
+
throw error;
|
|
136
148
|
}
|
|
137
|
-
throw error;
|
|
138
|
-
}
|
|
139
149
|
}
|
|
140
|
-
|
|
141
150
|
function expandHome(value, home) {
|
|
142
|
-
|
|
151
|
+
return value.startsWith("~/") ? path.join(home, value.slice(2)) : value;
|
|
143
152
|
}
|
|
144
|
-
|
|
145
153
|
/** Resolve the config path; an explicit CHAT_ROOM_CONFIG must exist. */
|
|
146
154
|
export function findTuiConfigFile(env = process.env, cwd = process.cwd()) {
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
const configHome = env.XDG_CONFIG_HOME || path.join(home, ".config");
|
|
155
|
-
for (const dir of [...new Set([cwd, path.join(configHome, "chatroom")])]) {
|
|
156
|
-
for (const name of FILE_NAMES) {
|
|
157
|
-
const file = path.join(dir, name);
|
|
158
|
-
if (existsSync(file)) return file;
|
|
155
|
+
const home = env.HOME || os.homedir();
|
|
156
|
+
const explicit = env.CHAT_ROOM_CONFIG?.trim();
|
|
157
|
+
if (explicit) {
|
|
158
|
+
const file = path.resolve(cwd, expandHome(explicit, home));
|
|
159
|
+
if (!existsSync(file))
|
|
160
|
+
throw new Error(`chatroom TUI: CHAT_ROOM_CONFIG file does not exist: ${file}`);
|
|
161
|
+
return file;
|
|
159
162
|
}
|
|
160
|
-
|
|
161
|
-
|
|
163
|
+
const configHome = env.XDG_CONFIG_HOME || path.join(home, ".config");
|
|
164
|
+
for (const dir of [...new Set([cwd, path.join(configHome, "chatroom")])]) {
|
|
165
|
+
for (const name of FILE_NAMES) {
|
|
166
|
+
const file = path.join(dir, name);
|
|
167
|
+
if (existsSync(file))
|
|
168
|
+
return file;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return null;
|
|
162
172
|
}
|
|
163
|
-
|
|
164
173
|
export function loadTuiConfig(env = process.env, cwd = process.cwd()) {
|
|
165
|
-
|
|
166
|
-
|
|
174
|
+
const file = findTuiConfigFile(env, cwd);
|
|
175
|
+
return file ? parseFile(file) : null;
|
|
167
176
|
}
|
|
168
|
-
|
|
169
177
|
function envValue(env, key) {
|
|
170
|
-
|
|
171
|
-
|
|
178
|
+
const value = env[key];
|
|
179
|
+
return value === undefined || value === "" ? undefined : value;
|
|
172
180
|
}
|
|
173
|
-
|
|
174
181
|
function stateFile(env, cwd, home) {
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
182
|
+
const explicit = envValue(env, "CHAT_ROOM_TUI_STATE");
|
|
183
|
+
if (explicit)
|
|
184
|
+
return path.resolve(cwd, expandHome(explicit, home));
|
|
185
|
+
const stateHome = env.XDG_STATE_HOME || path.join(home, ".local", "state");
|
|
186
|
+
return path.join(stateHome, "chatroom-tui", "session.json");
|
|
179
187
|
}
|
|
180
|
-
|
|
181
188
|
/** Resolve startup config. extra = { flags, prefs, project } keeps the paths
|
|
182
189
|
* used by main.mjs testable; callers without extra behave as before. */
|
|
183
190
|
export function resolveTuiConfig(env = process.env, cwd = process.cwd(), extra = {}) {
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
}
|
|
191
|
+
const file = extra.project === undefined ? loadTuiConfig(env, cwd) : extra.project;
|
|
192
|
+
const flags = extra.flags ?? {};
|
|
193
|
+
const prefs = extra.prefs ?? {};
|
|
194
|
+
const home = env.HOME || os.homedir();
|
|
195
|
+
const url = (flags.server ?? envValue(env, "CHAT_ROOM_SERVER_URL") ?? prefs.url ?? file?.client?.url ?? file?.url ?? "")
|
|
196
|
+
.trim().replace(/\/+$/, "");
|
|
197
|
+
if (!url)
|
|
198
|
+
throw new Error(`chatroom TUI: server URL is not set; ${URL_HINT}`);
|
|
199
|
+
if (!/^https?:\/\/[^/]+/i.test(url)) {
|
|
200
|
+
throw new Error(`chatroom TUI: server URL must be an http(s) URL; ${URL_HINT}`);
|
|
201
|
+
}
|
|
202
|
+
const name = flags.name ?? envValue(env, "CHAT_ROOM_TUI_NAME") ?? prefs.participant ?? file?.client?.participant ?? DEFAULT_PARTICIPANT;
|
|
203
|
+
return {
|
|
204
|
+
url,
|
|
205
|
+
token: envValue(env, "CHAT_ROOM_SERVER_TOKEN") ?? file?.client?.token ?? file?.token ?? "",
|
|
206
|
+
participant: name.trim().slice(0, 64) || DEFAULT_PARTICIPANT,
|
|
207
|
+
requestTimeoutMs: file?.client?.requestTimeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS,
|
|
208
|
+
stateFile: stateFile(env, cwd, home),
|
|
209
|
+
};
|
|
210
|
+
}
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
// Runtime-neutral chatroom config loader for the TUI: JSON, JSONC and YAML,
|
|
2
|
+
// with environment variables overriding file values.
|
|
3
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
4
|
+
import os from "node:os";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
import { parse as parseYaml } from "yaml";
|
|
7
|
+
import type { TuiPreferences } from "./prefs.mjs";
|
|
8
|
+
|
|
9
|
+
export type Env = Record<string, string | undefined>;
|
|
10
|
+
|
|
11
|
+
export interface ClientConfig {
|
|
12
|
+
url?: string;
|
|
13
|
+
token?: string;
|
|
14
|
+
participant?: string;
|
|
15
|
+
language?: string;
|
|
16
|
+
theme?: string;
|
|
17
|
+
requestTimeoutMs?: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface TuiProjectConfig {
|
|
21
|
+
url?: string;
|
|
22
|
+
token?: string;
|
|
23
|
+
language?: string;
|
|
24
|
+
theme?: string;
|
|
25
|
+
client?: ClientConfig;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface TuiConfigFlags {
|
|
29
|
+
server?: string;
|
|
30
|
+
name?: string;
|
|
31
|
+
language?: string;
|
|
32
|
+
theme?: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface TuiConfigExtra {
|
|
36
|
+
flags?: TuiConfigFlags;
|
|
37
|
+
prefs?: TuiPreferences;
|
|
38
|
+
project?: TuiProjectConfig | null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface ResolvedTuiConfig {
|
|
42
|
+
url: string;
|
|
43
|
+
token: string;
|
|
44
|
+
participant: string;
|
|
45
|
+
requestTimeoutMs: number;
|
|
46
|
+
stateFile: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export const FILE_NAMES: readonly string[] = ["chatroom.json", "chatroom.jsonc", "chatroom.yaml", "chatroom.yml"];
|
|
50
|
+
export const DEFAULT_PARTICIPANT = "chatroom-tui";
|
|
51
|
+
export const DEFAULT_REQUEST_TIMEOUT_MS = 10000;
|
|
52
|
+
|
|
53
|
+
const URL_HINT = "set CHAT_ROOM_SERVER_URL or client.url in your chatroom config";
|
|
54
|
+
|
|
55
|
+
function asRecord(value: unknown, where: string): Record<string, unknown> | undefined {
|
|
56
|
+
if (value === undefined) return undefined;
|
|
57
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
58
|
+
throw new Error(`chatroom TUI: ${where} must be an object`);
|
|
59
|
+
}
|
|
60
|
+
return value as Record<string, unknown>;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function optionalString(value: unknown, where: string): string | undefined {
|
|
64
|
+
if (value === undefined) return undefined;
|
|
65
|
+
if (typeof value !== "string") throw new Error(`chatroom TUI: ${where} must be a string`);
|
|
66
|
+
return value;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function optionalNumber(value: unknown, where: string): number | undefined {
|
|
70
|
+
if (value === undefined) return undefined;
|
|
71
|
+
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
72
|
+
throw new Error(`chatroom TUI: ${where} must be a finite number`);
|
|
73
|
+
}
|
|
74
|
+
return value;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function parseClient(value: unknown): ClientConfig | undefined {
|
|
78
|
+
const raw = asRecord(value, "client");
|
|
79
|
+
if (!raw) return undefined;
|
|
80
|
+
const requestTimeoutMs = optionalNumber(raw.requestTimeoutMs, "client.requestTimeoutMs");
|
|
81
|
+
if (requestTimeoutMs !== undefined && requestTimeoutMs <= 0) {
|
|
82
|
+
throw new Error("chatroom TUI: client.requestTimeoutMs must be greater than zero");
|
|
83
|
+
}
|
|
84
|
+
return {
|
|
85
|
+
url: optionalString(raw.url, "client.url"),
|
|
86
|
+
token: optionalString(raw.token, "client.token"),
|
|
87
|
+
participant: optionalString(raw.participant, "client.participant"),
|
|
88
|
+
language: optionalString(raw.language, "client.language"),
|
|
89
|
+
theme: optionalString(raw.theme, "client.theme"),
|
|
90
|
+
requestTimeoutMs,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function parseConfig(value: unknown): TuiProjectConfig {
|
|
95
|
+
const raw = asRecord(value, "root");
|
|
96
|
+
if (!raw) return {};
|
|
97
|
+
return {
|
|
98
|
+
url: optionalString(raw.url, "url"),
|
|
99
|
+
token: optionalString(raw.token, "token"),
|
|
100
|
+
language: optionalString(raw.language, "language"),
|
|
101
|
+
theme: optionalString(raw.theme, "theme"),
|
|
102
|
+
client: parseClient(raw.client),
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Remove comments and trailing commas without touching quoted strings. */
|
|
107
|
+
function stripJsonc(source: string): string {
|
|
108
|
+
let out = "";
|
|
109
|
+
let quote = "";
|
|
110
|
+
let escaped = false;
|
|
111
|
+
let comma = -1;
|
|
112
|
+
for (let i = 0; i < source.length; i++) {
|
|
113
|
+
const char = source.charAt(i);
|
|
114
|
+
const next = source.charAt(i + 1);
|
|
115
|
+
if (quote) {
|
|
116
|
+
out += char;
|
|
117
|
+
if (escaped) escaped = false;
|
|
118
|
+
else if (char === "\\") escaped = true;
|
|
119
|
+
else if (char === quote) quote = "";
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
if (char === '"') {
|
|
123
|
+
quote = char;
|
|
124
|
+
comma = -1;
|
|
125
|
+
out += char;
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
if (char === "/" && next === "/") {
|
|
129
|
+
while (i < source.length && source.charAt(i) !== "\n") i++;
|
|
130
|
+
out += "\n";
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
if (char === "/" && next === "*") {
|
|
134
|
+
i += 2;
|
|
135
|
+
while (i < source.length && !(source.charAt(i) === "*" && source.charAt(i + 1) === "/")) i++;
|
|
136
|
+
i++;
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
if (char === ",") {
|
|
140
|
+
comma = out.length;
|
|
141
|
+
out += char;
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
if (char === "}" || char === "]") {
|
|
145
|
+
if (comma >= 0) out = out.slice(0, comma) + out.slice(comma + 1);
|
|
146
|
+
comma = -1;
|
|
147
|
+
out += char;
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
if (!/\s/.test(char)) comma = -1;
|
|
151
|
+
out += char;
|
|
152
|
+
}
|
|
153
|
+
return out;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function parseFile(file: string): TuiProjectConfig {
|
|
157
|
+
let text: string;
|
|
158
|
+
try {
|
|
159
|
+
text = readFileSync(file, "utf8");
|
|
160
|
+
} catch {
|
|
161
|
+
throw new Error(`chatroom TUI: cannot read ${file}`);
|
|
162
|
+
}
|
|
163
|
+
const extension = path.extname(file).toLowerCase();
|
|
164
|
+
try {
|
|
165
|
+
if (extension === ".yaml" || extension === ".yml") return parseConfig(parseYaml(text));
|
|
166
|
+
if (extension === ".jsonc") return parseConfig(JSON.parse(stripJsonc(text)));
|
|
167
|
+
if (extension === ".json") return parseConfig(JSON.parse(text));
|
|
168
|
+
try {
|
|
169
|
+
return parseConfig(JSON.parse(text));
|
|
170
|
+
} catch {
|
|
171
|
+
return parseConfig(parseYaml(text));
|
|
172
|
+
}
|
|
173
|
+
} catch (error) {
|
|
174
|
+
if (error instanceof SyntaxError || (error instanceof Error && error.name === "YAMLParseError")) {
|
|
175
|
+
// Never include parser source text: a malformed file may contain a token.
|
|
176
|
+
throw new Error(`chatroom TUI: ${file} is not valid JSON/YAML`);
|
|
177
|
+
}
|
|
178
|
+
throw error;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function expandHome(value: string, home: string): string {
|
|
183
|
+
return value.startsWith("~/") ? path.join(home, value.slice(2)) : value;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** Resolve the config path; an explicit CHAT_ROOM_CONFIG must exist. */
|
|
187
|
+
export function findTuiConfigFile(env: Env = process.env, cwd = process.cwd()): string | null {
|
|
188
|
+
const home = env.HOME || os.homedir();
|
|
189
|
+
const explicit = env.CHAT_ROOM_CONFIG?.trim();
|
|
190
|
+
if (explicit) {
|
|
191
|
+
const file = path.resolve(cwd, expandHome(explicit, home));
|
|
192
|
+
if (!existsSync(file)) throw new Error(`chatroom TUI: CHAT_ROOM_CONFIG file does not exist: ${file}`);
|
|
193
|
+
return file;
|
|
194
|
+
}
|
|
195
|
+
const configHome = env.XDG_CONFIG_HOME || path.join(home, ".config");
|
|
196
|
+
for (const dir of [...new Set([cwd, path.join(configHome, "chatroom")])]) {
|
|
197
|
+
for (const name of FILE_NAMES) {
|
|
198
|
+
const file = path.join(dir, name);
|
|
199
|
+
if (existsSync(file)) return file;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return null;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export function loadTuiConfig(env: Env = process.env, cwd = process.cwd()): TuiProjectConfig | null {
|
|
206
|
+
const file = findTuiConfigFile(env, cwd);
|
|
207
|
+
return file ? parseFile(file) : null;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function envValue(env: Env, key: string): string | undefined {
|
|
211
|
+
const value = env[key];
|
|
212
|
+
return value === undefined || value === "" ? undefined : value;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function stateFile(env: Env, cwd: string, home: string): string {
|
|
216
|
+
const explicit = envValue(env, "CHAT_ROOM_TUI_STATE");
|
|
217
|
+
if (explicit) return path.resolve(cwd, expandHome(explicit, home));
|
|
218
|
+
const stateHome = env.XDG_STATE_HOME || path.join(home, ".local", "state");
|
|
219
|
+
return path.join(stateHome, "chatroom-tui", "session.json");
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** Resolve startup config. extra = { flags, prefs, project } keeps the paths
|
|
223
|
+
* used by main.mjs testable; callers without extra behave as before. */
|
|
224
|
+
export function resolveTuiConfig(
|
|
225
|
+
env: Env = process.env,
|
|
226
|
+
cwd = process.cwd(),
|
|
227
|
+
extra: TuiConfigExtra = {},
|
|
228
|
+
): ResolvedTuiConfig {
|
|
229
|
+
const file = extra.project === undefined ? loadTuiConfig(env, cwd) : extra.project;
|
|
230
|
+
const flags = extra.flags ?? {};
|
|
231
|
+
const prefs = extra.prefs ?? {};
|
|
232
|
+
const home = env.HOME || os.homedir();
|
|
233
|
+
const url = (flags.server ?? envValue(env, "CHAT_ROOM_SERVER_URL") ?? prefs.url ?? file?.client?.url ?? file?.url ?? "")
|
|
234
|
+
.trim().replace(/\/+$/, "");
|
|
235
|
+
if (!url) throw new Error(`chatroom TUI: server URL is not set; ${URL_HINT}`);
|
|
236
|
+
if (!/^https?:\/\/[^/]+/i.test(url)) {
|
|
237
|
+
throw new Error(`chatroom TUI: server URL must be an http(s) URL; ${URL_HINT}`);
|
|
238
|
+
}
|
|
239
|
+
const name = flags.name ?? envValue(env, "CHAT_ROOM_TUI_NAME") ?? prefs.participant ?? file?.client?.participant ?? DEFAULT_PARTICIPANT;
|
|
240
|
+
return {
|
|
241
|
+
url,
|
|
242
|
+
token: envValue(env, "CHAT_ROOM_SERVER_TOKEN") ?? file?.client?.token ?? file?.token ?? "",
|
|
243
|
+
participant: name.trim().slice(0, 64) || DEFAULT_PARTICIPANT,
|
|
244
|
+
requestTimeoutMs: file?.client?.requestTimeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS,
|
|
245
|
+
stateFile: stateFile(env, cwd, home),
|
|
246
|
+
};
|
|
247
|
+
}
|