@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
@@ -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
- if (value === undefined) return undefined;
16
- if (!value || typeof value !== "object" || Array.isArray(value)) {
17
- throw new Error(`chatroom TUI: ${where} must be an object`);
18
- }
19
- return value;
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
- if (value === undefined) return undefined;
24
- if (typeof value !== "string") throw new Error(`chatroom TUI: ${where} must be a string`);
25
- return value;
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
- if (value === undefined) return undefined;
30
- if (typeof value !== "number" || !Number.isFinite(value)) {
31
- throw new Error(`chatroom TUI: ${where} must be a finite number`);
32
- }
33
- return value;
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
- const raw = asRecord(value, "client");
38
- if (!raw) return undefined;
39
- const requestTimeoutMs = optionalNumber(raw.requestTimeoutMs, "client.requestTimeoutMs");
40
- if (requestTimeoutMs !== undefined && requestTimeoutMs <= 0) {
41
- throw new Error("chatroom TUI: client.requestTimeoutMs must be greater than zero");
42
- }
43
- return {
44
- url: optionalString(raw.url, "client.url"),
45
- token: optionalString(raw.token, "client.token"),
46
- participant: optionalString(raw.participant, "client.participant"),
47
- language: optionalString(raw.language, "client.language"),
48
- theme: optionalString(raw.theme, "client.theme"),
49
- requestTimeoutMs,
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
- const raw = asRecord(value, "root");
55
- if (!raw) return {};
56
- return {
57
- url: optionalString(raw.url, "url"),
58
- token: optionalString(raw.token, "token"),
59
- language: optionalString(raw.language, "language"),
60
- theme: optionalString(raw.theme, "theme"),
61
- client: parseClient(raw.client),
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
- let out = "";
68
- let quote = "";
69
- let escaped = false;
70
- let comma = -1;
71
- for (let i = 0; i < source.length; i++) {
72
- const char = source[i];
73
- const next = source[i + 1];
74
- if (quote) {
75
- out += char;
76
- if (escaped) escaped = false;
77
- else if (char === "\\") escaped = true;
78
- else if (char === quote) quote = "";
79
- continue;
80
- }
81
- if (char === '"') {
82
- quote = char;
83
- comma = -1;
84
- out += char;
85
- continue;
86
- }
87
- if (char === "/" && next === "/") {
88
- while (i < source.length && source[i] !== "\n") i++;
89
- out += "\n";
90
- continue;
91
- }
92
- if (char === "/" && next === "*") {
93
- i += 2;
94
- while (i < source.length && !(source[i] === "*" && source[i + 1] === "/")) i++;
95
- i++;
96
- continue;
97
- }
98
- if (char === ",") {
99
- comma = out.length;
100
- out += char;
101
- continue;
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
- if (char === "}" || char === "]") {
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
- let text;
117
- try {
118
- text = readFileSync(file, "utf8");
119
- } catch {
120
- throw new Error(`chatroom TUI: cannot read ${file}`);
121
- }
122
- const extension = path.extname(file).toLowerCase();
123
- try {
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
- return parseConfig(JSON.parse(text));
129
- } catch {
130
- return parseConfig(parseYaml(text));
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
- } catch (error) {
133
- if (error instanceof SyntaxError || error?.name === "YAMLParseError") {
134
- // Never include parser source text: a malformed file may contain a token.
135
- throw new Error(`chatroom TUI: ${file} is not valid JSON/YAML`);
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
- return value.startsWith("~/") ? path.join(home, value.slice(2)) : value;
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
- const home = env.HOME || os.homedir();
148
- const explicit = env.CHAT_ROOM_CONFIG?.trim();
149
- if (explicit) {
150
- const file = path.resolve(cwd, expandHome(explicit, home));
151
- if (!existsSync(file)) throw new Error(`chatroom TUI: CHAT_ROOM_CONFIG file does not exist: ${file}`);
152
- return file;
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
- return null;
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
- const file = findTuiConfigFile(env, cwd);
166
- return file ? parseFile(file) : null;
174
+ const file = findTuiConfigFile(env, cwd);
175
+ return file ? parseFile(file) : null;
167
176
  }
168
-
169
177
  function envValue(env, key) {
170
- const value = env[key];
171
- return value === undefined || value === "" ? undefined : value;
178
+ const value = env[key];
179
+ return value === undefined || value === "" ? undefined : value;
172
180
  }
173
-
174
181
  function stateFile(env, cwd, home) {
175
- const explicit = envValue(env, "CHAT_ROOM_TUI_STATE");
176
- if (explicit) return path.resolve(cwd, expandHome(explicit, home));
177
- const stateHome = env.XDG_STATE_HOME || path.join(home, ".local", "state");
178
- return path.join(stateHome, "chatroom-tui", "session.json");
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
- const file = extra.project === undefined ? loadTuiConfig(env, cwd) : extra.project;
185
- const flags = extra.flags ?? {};
186
- const prefs = extra.prefs ?? {};
187
- const home = env.HOME || os.homedir();
188
- const url = (flags.server ?? envValue(env, "CHAT_ROOM_SERVER_URL") ?? prefs.url ?? file?.client?.url ?? file?.url ?? "")
189
- .trim().replace(/\/+$/, "");
190
- if (!url) throw new Error(`chatroom TUI: server URL is not set; ${URL_HINT}`);
191
- if (!/^https?:\/\/[^/]+/i.test(url)) {
192
- throw new Error(`chatroom TUI: server URL must be an http(s) URL; ${URL_HINT}`);
193
- }
194
- const name = flags.name ?? envValue(env, "CHAT_ROOM_TUI_NAME") ?? prefs.participant ?? file?.client?.participant ?? DEFAULT_PARTICIPANT;
195
- return {
196
- url,
197
- token: envValue(env, "CHAT_ROOM_SERVER_TOKEN") ?? file?.client?.token ?? file?.token ?? "",
198
- participant: name.trim().slice(0, 64) || DEFAULT_PARTICIPANT,
199
- requestTimeoutMs: file?.client?.requestTimeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS,
200
- stateFile: stateFile(env, cwd, home),
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
+ }