@alexsun-top/opencode-chat-room 0.8.2 → 0.8.4

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 (92) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +18 -62
  3. package/README.zh-CN.md +25 -69
  4. package/dist/cli.js +466 -188
  5. package/package.json +11 -5
  6. package/packages/chat-room-channeld/README.md +25 -3
  7. package/packages/chat-room-channeld/dist/mock-adapter.js +5 -5
  8. package/packages/chat-room-channeld/src/adapter.ts +5 -0
  9. package/packages/chat-room-channeld/src/cli.ts +72 -5
  10. package/packages/chat-room-channeld/src/host.ts +17 -2
  11. package/packages/chat-room-channeld/src/pg/database.ts +187 -0
  12. package/packages/chat-room-channeld/src/pg/migrate.ts +150 -0
  13. package/packages/chat-room-channeld/src/pg/schema.ts +60 -0
  14. package/packages/chat-room-channeld/src/pg/sql.mjs +175 -0
  15. package/packages/chat-room-channeld/src/pg/sqlite.ts +100 -0
  16. package/packages/chat-room-channeld/src/pg/worker.mjs +133 -0
  17. package/packages/chat-room-channeld/src/protocol.ts +1 -0
  18. package/packages/chat-room-channeld/src/state.ts +73 -97
  19. package/packages/chat-room-channeld/src/stats.ts +140 -109
  20. package/server/assets/base.css +3 -1
  21. package/server/assets/chat/admin-badge.js +32 -30
  22. package/server/assets/chat/admin.js +201 -161
  23. package/server/assets/chat/announcements.js +302 -208
  24. package/server/assets/chat/attachment-store.js +116 -76
  25. package/server/assets/chat/attachments.js +701 -441
  26. package/server/assets/chat/chat-background.js +179 -156
  27. package/server/assets/chat/clipboard.js +45 -25
  28. package/server/assets/chat/confirm-dialog.js +38 -35
  29. package/server/assets/chat/copy-action.js +40 -41
  30. package/server/assets/chat/create-room-dialog.js +50 -47
  31. package/server/assets/chat/diagram-viewer.js +293 -236
  32. package/server/assets/chat/doc-preview.js +516 -443
  33. package/server/assets/chat/editor.js +446 -336
  34. package/server/assets/chat/events.js +650 -510
  35. package/server/assets/chat/files.js +505 -431
  36. package/server/assets/chat/history-loader.js +81 -73
  37. package/server/assets/chat/icons.js +9 -11
  38. package/server/assets/chat/image-zoom.js +246 -179
  39. package/server/assets/chat/index.js +2 -3
  40. package/server/assets/chat/install-prompt.js +90 -69
  41. package/server/assets/chat/integration-account-schema.js +91 -87
  42. package/server/assets/chat/integration-editor.js +162 -155
  43. package/server/assets/chat/integration-values.js +116 -101
  44. package/server/assets/chat/integrations.js +750 -626
  45. package/server/assets/chat/join-requests.js +415 -358
  46. package/server/assets/chat/locales/de.js +193 -194
  47. package/server/assets/chat/locales/es.js +193 -194
  48. package/server/assets/chat/locales/fr.js +193 -194
  49. package/server/assets/chat/locales/index.js +1 -2
  50. package/server/assets/chat/locales/it.js +193 -194
  51. package/server/assets/chat/locales/ja.js +193 -193
  52. package/server/assets/chat/locales/ko.js +193 -193
  53. package/server/assets/chat/markdown-footnotes.js +134 -103
  54. package/server/assets/chat/markdown.js +557 -404
  55. package/server/assets/chat/members.js +259 -197
  56. package/server/assets/chat/mentions.js +181 -133
  57. package/server/assets/chat/message-cache.js +40 -33
  58. package/server/assets/chat/message-polling.js +299 -227
  59. package/server/assets/chat/message-window.js +78 -55
  60. package/server/assets/chat/messages.js +562 -480
  61. package/server/assets/chat/office-preview.js +46 -45
  62. package/server/assets/chat/palette.js +464 -417
  63. package/server/assets/chat/panels.js +94 -79
  64. package/server/assets/chat/pwa.js +147 -75
  65. package/server/assets/chat/questions.js +374 -302
  66. package/server/assets/chat/recall.js +338 -273
  67. package/server/assets/chat/receipts.js +151 -121
  68. package/server/assets/chat/reminders.js +345 -214
  69. package/server/assets/chat/replies.js +181 -130
  70. package/server/assets/chat/resource-format.js +59 -62
  71. package/server/assets/chat/room-list.js +58 -55
  72. package/server/assets/chat/room-watcher.js +76 -50
  73. package/server/assets/chat/rooms.js +631 -500
  74. package/server/assets/chat/search.js +138 -114
  75. package/server/assets/chat/settings.js +225 -201
  76. package/server/assets/chat/shiki.js +62 -63
  77. package/server/assets/chat/shortcuts.js +212 -189
  78. package/server/assets/chat/side-pane.js +161 -150
  79. package/server/assets/chat/state.js +860 -737
  80. package/server/assets/chat/system-events.js +46 -44
  81. package/server/assets/chat/system-messages.js +57 -56
  82. package/server/assets/chat/task-form.js +179 -183
  83. package/server/assets/chat/task-row.js +158 -155
  84. package/server/assets/chat/tasks.js +665 -587
  85. package/server/assets/chat/viewport.js +32 -29
  86. package/server/assets/chat/window-controls.js +32 -32
  87. package/server/assets/conversation.css +10 -4
  88. package/server/chat-server.ts +9 -1
  89. package/server/chat.html +3 -1
  90. package/server/routes/pages.ts +7 -5
  91. package/server/sw.js +346 -268
  92. package/src/web/integration-rules-editor.ts +2 -0
@@ -1,163 +1,170 @@
1
- // Lazy Monaco wrapper for one integration rules section. The textarea stays in the
2
- // page as the no-JS and failed-download fallback, so this module never imports Monaco.
3
1
  const BUNDLE_URL = "/assets/monaco/integration-rules.js";
4
2
  const STYLES_URL = "/assets/monaco/integration-rules.css";
5
3
  let bundlePromise = null;
6
-
7
4
  function loadBundle() {
8
- if (!bundlePromise) {
9
- bundlePromise = import(BUNDLE_URL)
10
- .then((module) => {
11
- if (!document.querySelector("link[data-integration-rules-editor]")) {
12
- const link = document.createElement("link");
13
- link.rel = "stylesheet";
14
- link.href = STYLES_URL;
15
- link.dataset.integrationRulesEditor = "true";
16
- document.head.append(link);
17
- }
18
- return module;
19
- })
20
- .catch((error) => {
21
- // Keep this a warning: a blocked asset must not surface as an uncaught page error.
22
- console.warn("Integration code editor unavailable; keeping the plain text editor.", error);
23
- throw error;
24
- });
25
- }
26
- return bundlePromise;
5
+ if (!bundlePromise) {
6
+ bundlePromise = import(BUNDLE_URL)
7
+ .then((module) => {
8
+ if (!document.querySelector("link[data-integration-rules-editor]")) {
9
+ const link = document.createElement("link");
10
+ link.rel = "stylesheet";
11
+ link.href = STYLES_URL;
12
+ link.dataset.integrationRulesEditor = "true";
13
+ document.head.append(link);
14
+ }
15
+ return module;
16
+ })
17
+ .catch((error) => {
18
+ // Keep this a warning: a blocked asset must not surface as an uncaught page error.
19
+ console.warn("Integration code editor unavailable; keeping the plain text editor.", error);
20
+ throw error;
21
+ });
22
+ }
23
+ return bundlePromise;
27
24
  }
28
-
29
25
  export function initIntegrationEditor(app, options) {
30
- const host = app.$(options.editor);
31
- const textarea = app.$(options.textarea);
32
- const fallback = app.$(options.fallback);
33
- const error = app.$(options.error);
34
- const errorList = app.$(options.errors);
35
- const formatGroup = app.$(options.formats);
36
- let controller = null, loading = null, failed = false, editable = true;
37
-
38
- if (formatGroup) formatGroup.hidden = true;
39
-
40
- const applyEditable = () => {
41
- host?.setAttribute("data-read-only", String(!editable));
42
- if (textarea) textarea.readOnly = !editable;
43
- for (const button of formatGroup?.querySelectorAll("[data-format]") || []) button.disabled = !editable;
44
- controller?.setReadOnly(!editable);
45
- };
46
-
47
- const updateFormatButtons = () => {
48
- const active = controller?.getFormat() || "yaml";
26
+ const host = app.$(options.editor);
27
+ const textarea = app.$(options.textarea);
28
+ const fallback = app.$(options.fallback);
29
+ const error = app.$(options.error);
30
+ const errorList = app.$(options.errors);
31
+ const formatGroup = app.$(options.formats);
32
+ let controller = null, loading = null, failed = false, editable = true;
33
+ if (formatGroup)
34
+ formatGroup.hidden = true;
35
+ const applyEditable = () => {
36
+ host?.setAttribute("data-read-only", String(!editable));
37
+ if (textarea)
38
+ textarea.readOnly = !editable;
39
+ for (const button of formatGroup?.querySelectorAll("[data-format]") || [])
40
+ button.disabled = !editable;
41
+ controller?.setReadOnly(!editable);
42
+ };
43
+ const updateFormatButtons = () => {
44
+ const active = controller?.getFormat() || "yaml";
45
+ for (const button of formatGroup?.querySelectorAll("[data-format]") || []) {
46
+ button.setAttribute("aria-pressed", String(button.dataset.format === active));
47
+ }
48
+ };
49
+ const render = (result) => {
50
+ if (textarea)
51
+ textarea.value = result.text;
52
+ host?.setAttribute("data-error-count", String(result.errors.length));
53
+ host?.setAttribute("aria-invalid", String(result.errors.length > 0));
54
+ updateFormatButtons();
55
+ if (!error || !errorList)
56
+ return;
57
+ errorList.textContent = "";
58
+ if (!result.errors.length) {
59
+ error.hidden = true;
60
+ errorList.hidden = true;
61
+ return;
62
+ }
63
+ const first = result.errors[0];
64
+ error.textContent = app.t("integrationsRulesErrors", {
65
+ count: result.errors.length,
66
+ first: `Line ${first.line}: ${first.message}`,
67
+ });
68
+ error.hidden = false;
69
+ errorList.hidden = false;
70
+ for (const issue of result.errors) {
71
+ const item = document.createElement("li");
72
+ const button = document.createElement("button");
73
+ button.type = "button";
74
+ button.className = "integration-rule-error";
75
+ button.textContent = `Line ${issue.line}${issue.path ? ` · ${issue.path}` : ""}: ${issue.message}`;
76
+ button.addEventListener("click", () => controller?.reveal(issue.line, issue.column));
77
+ item.append(button);
78
+ errorList.append(item);
79
+ }
80
+ };
81
+ const clearErrors = () => {
82
+ if (error)
83
+ error.hidden = true;
84
+ if (errorList) {
85
+ errorList.hidden = true;
86
+ errorList.textContent = "";
87
+ }
88
+ host?.setAttribute("data-error-count", "0");
89
+ host?.removeAttribute("aria-invalid");
90
+ };
91
+ const ensure = () => {
92
+ if (controller || failed || !host)
93
+ return loading;
94
+ if (loading)
95
+ return loading;
96
+ loading = loadBundle().then((module) => {
97
+ controller = module.createIntegrationRulesEditor({
98
+ kind: options.kind,
99
+ container: host,
100
+ value: options.getValue(),
101
+ accounts: options.getAccounts(),
102
+ onChange: render,
103
+ });
104
+ host.hidden = false;
105
+ if (textarea)
106
+ textarea.hidden = true;
107
+ if (fallback)
108
+ fallback.hidden = true;
109
+ if (formatGroup)
110
+ formatGroup.hidden = false;
111
+ applyEditable();
112
+ render(controller.validate());
113
+ }).catch(() => {
114
+ failed = true;
115
+ if (fallback) {
116
+ fallback.textContent = app.t("integrationsEditorUnavailable");
117
+ fallback.hidden = false;
118
+ }
119
+ }).finally(() => { loading = null; });
120
+ return loading;
121
+ };
122
+ const refresh = () => {
123
+ const value = options.getValue() ?? [];
124
+ clearErrors();
125
+ if (controller)
126
+ controller.setValue(value);
127
+ else if (textarea)
128
+ textarea.value = JSON.stringify(value, null, 2);
129
+ };
130
+ const current = () => {
131
+ if (controller) {
132
+ const result = controller.validate();
133
+ render(result);
134
+ return { editor: true, ...result };
135
+ }
136
+ return { editor: false, text: textarea?.value || "", errors: [] };
137
+ };
49
138
  for (const button of formatGroup?.querySelectorAll("[data-format]") || []) {
50
- button.setAttribute("aria-pressed", String(button.dataset.format === active));
51
- }
52
- };
53
-
54
- const render = (result) => {
55
- if (textarea) textarea.value = result.text;
56
- host?.setAttribute("data-error-count", String(result.errors.length));
57
- host?.setAttribute("aria-invalid", String(result.errors.length > 0));
58
- updateFormatButtons();
59
- if (!error || !errorList) return;
60
- errorList.textContent = "";
61
- if (!result.errors.length) {
62
- error.hidden = true;
63
- errorList.hidden = true;
64
- return;
65
- }
66
- const first = result.errors[0];
67
- error.textContent = app.t("integrationsRulesErrors", {
68
- count: result.errors.length,
69
- first: `Line ${first.line}: ${first.message}`,
70
- });
71
- error.hidden = false;
72
- errorList.hidden = false;
73
- for (const issue of result.errors) {
74
- const item = document.createElement("li");
75
- const button = document.createElement("button");
76
- button.type = "button";
77
- button.className = "integration-rule-error";
78
- button.textContent = `Line ${issue.line}${issue.path ? ` · ${issue.path}` : ""}: ${issue.message}`;
79
- button.addEventListener("click", () => controller?.reveal(issue.line, issue.column));
80
- item.append(button);
81
- errorList.append(item);
139
+ button.addEventListener("click", () => {
140
+ if (!controller || !editable)
141
+ return;
142
+ controller.setFormat(button.dataset.format === "yaml" ? "yaml" : "json");
143
+ updateFormatButtons();
144
+ });
82
145
  }
83
- };
84
-
85
- const clearErrors = () => {
86
- if (error) error.hidden = true;
87
- if (errorList) { errorList.hidden = true; errorList.textContent = ""; }
88
- host?.setAttribute("data-error-count", "0");
89
- host?.removeAttribute("aria-invalid");
90
- };
91
-
92
- const ensure = () => {
93
- if (controller || failed || !host) return loading;
94
- if (loading) return loading;
95
- loading = loadBundle().then((module) => {
96
- controller = module.createIntegrationRulesEditor({
97
- kind: options.kind,
98
- container: host,
99
- value: options.getValue(),
100
- accounts: options.getAccounts(),
101
- onChange: render,
102
- });
103
- host.hidden = false;
104
- if (textarea) textarea.hidden = true;
105
- if (fallback) fallback.hidden = true;
106
- if (formatGroup) formatGroup.hidden = false;
107
- applyEditable();
108
- render(controller.validate());
109
- }).catch(() => {
110
- failed = true;
111
- if (fallback) {
112
- fallback.textContent = app.t("integrationsEditorUnavailable");
113
- fallback.hidden = false;
114
- }
115
- }).finally(() => { loading = null; });
116
- return loading;
117
- };
118
-
119
- const refresh = () => {
120
- const value = options.getValue() ?? [];
121
- clearErrors();
122
- if (controller) controller.setValue(value);
123
- else if (textarea) textarea.value = JSON.stringify(value, null, 2);
124
- };
125
-
126
- const current = () => {
127
- if (controller) {
128
- const result = controller.validate();
129
- render(result);
130
- return { editor: true, ...result };
131
- }
132
- return { editor: false, text: textarea?.value || "", errors: [] };
133
- };
134
-
135
- for (const button of formatGroup?.querySelectorAll("[data-format]") || []) {
136
- button.addEventListener("click", () => {
137
- if (!controller || !editable) return;
138
- controller.setFormat(button.dataset.format === "yaml" ? "yaml" : "json");
139
- updateFormatButtons();
140
- });
141
- }
142
- textarea?.addEventListener("input", clearErrors);
143
-
144
- return {
145
- ensure,
146
- refresh,
147
- current,
148
- showMessage: (message) => {
149
- if (!error) return;
150
- error.textContent = message;
151
- error.hidden = false;
152
- if (errorList) { errorList.hidden = true; errorList.textContent = ""; }
153
- },
154
- setAccounts: (accounts) => controller?.setAccounts(accounts),
155
- setEditable: (next) => {
156
- editable = next !== false;
157
- applyEditable();
158
- },
159
- getText: () => textarea?.value || "",
160
- hasEditor: () => Boolean(controller),
161
- dispose: () => controller?.dispose(),
162
- };
163
- }
146
+ textarea?.addEventListener("input", clearErrors);
147
+ return {
148
+ ensure,
149
+ refresh,
150
+ current,
151
+ showMessage: (message) => {
152
+ if (!error)
153
+ return;
154
+ error.textContent = message;
155
+ error.hidden = false;
156
+ if (errorList) {
157
+ errorList.hidden = true;
158
+ errorList.textContent = "";
159
+ }
160
+ },
161
+ setAccounts: (accounts) => controller?.setAccounts(accounts),
162
+ setEditable: (next) => {
163
+ editable = next !== false;
164
+ applyEditable();
165
+ },
166
+ getText: () => textarea?.value || "",
167
+ hasEditor: () => Boolean(controller),
168
+ dispose: () => controller?.dispose(),
169
+ };
170
+ }
@@ -1,124 +1,139 @@
1
- // Parsing and account-reference checks for integration rules and inbound routes.
2
1
  const isRecord = (value) => Boolean(value) && typeof value === "object" && !Array.isArray(value);
3
-
4
2
  export function cleanError(error, fallback) {
5
- const value = String(error?.message || error || "").trim()
6
- .replace(/channeld/gi, "integrations")
7
- .replace(/\bBearer\s+\S+/gi, "Bearer [redacted]")
8
- .replace(/\b(secret|token|password|authorization|api[_-]?key)\b\s*[:=]\s*\S+/gi, "$1=[redacted]");
9
- return value || fallback;
3
+ const detail = error instanceof Error ? error.message || error : error;
4
+ const value = String(detail || "").trim()
5
+ .replace(/channeld/gi, "integrations")
6
+ .replace(/\bBearer\s+\S+/gi, "Bearer [redacted]")
7
+ .replace(/\b(secret|token|password|authorization|api[_-]?key)\b\s*[:=]\s*\S+/gi, "$1=[redacted]");
8
+ return value || fallback;
10
9
  }
11
-
12
10
  export function stateLabel(state, text) {
13
- return text({
14
- started: "integrationsStateStarted", starting: "integrationsStateStarting", stopped: "integrationsStateStopped",
15
- error: "integrationsStateError", failed: "integrationsStateError",
16
- }[state] || "integrationsStateUnknown");
11
+ return text({
12
+ started: "integrationsStateStarted", starting: "integrationsStateStarting", stopped: "integrationsStateStopped",
13
+ error: "integrationsStateError", failed: "integrationsStateError",
14
+ }[state] || "integrationsStateUnknown");
17
15
  }
18
-
19
16
  export function accountBody(account) {
20
- const body = { id: account.id, platform: account.platform, pluginId: account.pluginId };
21
- if (account.envClear) { body.env = {}; return body; }
22
- const setKeys = Object.keys(account.envSet || {});
23
- if (setKeys.length) body.env = account.envSet;
24
- const deleted = (account.envDelete || []).filter((key) => !setKeys.includes(key));
25
- // Delete-only edits must omit env; {} means clear every stored key.
26
- if (deleted.length) body.envDelete = deleted;
27
- return body;
17
+ const body = { id: account.id, platform: account.platform, pluginId: account.pluginId };
18
+ if (account.envClear) {
19
+ body.env = {};
20
+ return body;
21
+ }
22
+ const setKeys = Object.keys(account.envSet || {});
23
+ if (setKeys.length)
24
+ body.env = account.envSet;
25
+ const deleted = (account.envDelete || []).filter((key) => !setKeys.includes(key));
26
+ // Delete-only edits must omit env; {} means clear every stored key.
27
+ if (deleted.length)
28
+ body.envDelete = deleted;
29
+ return body;
28
30
  }
29
-
30
31
  export function normalizeAccount(account) {
31
- const envKeys = Array.isArray(account.envKeys) ? account.envKeys.filter((key) => typeof key === "string") : [];
32
- return {
33
- id: String(account.id || ""),
34
- platform: String(account.platform || ""),
35
- pluginId: String(account.pluginId || ""),
36
- envKeys,
37
- storedKeys: [...envKeys],
38
- envSet: {},
39
- envDelete: [],
40
- envClear: false,
41
- state: typeof account.state === "string" ? account.state : "unknown",
42
- lastError: typeof account.lastError === "string" ? account.lastError : "",
43
- };
32
+ const envKeys = Array.isArray(account.envKeys) ? account.envKeys.filter((key) => typeof key === "string") : [];
33
+ return {
34
+ id: String(account.id || ""),
35
+ platform: String(account.platform || ""),
36
+ pluginId: String(account.pluginId || ""),
37
+ envKeys,
38
+ storedKeys: [...envKeys],
39
+ envSet: {},
40
+ envDelete: [],
41
+ envClear: false,
42
+ state: typeof account.state === "string" ? account.state : "unknown",
43
+ lastError: typeof account.lastError === "string" ? account.lastError : "",
44
+ };
44
45
  }
45
-
46
46
  export function knownPlugins(data) {
47
- const plugins = [...data.plugins];
48
- for (const account of data.accounts) {
49
- if (account.pluginId && !plugins.some((plugin) => plugin.pluginId === account.pluginId)) {
50
- plugins.push({ pluginId: account.pluginId, name: account.pluginId, platform: account.platform });
47
+ const plugins = [...data.plugins];
48
+ for (const account of data.accounts) {
49
+ if (account.pluginId && !plugins.some((plugin) => plugin.pluginId === account.pluginId)) {
50
+ plugins.push({ pluginId: account.pluginId, name: account.pluginId, platform: account.platform });
51
+ }
51
52
  }
52
- }
53
- return plugins;
53
+ return plugins;
54
54
  }
55
-
56
55
  function jsonLine(value, message) {
57
- const position = /position (\d+)/.exec(message), row = /line (\d+)/i.exec(message);
58
- return position ? value.slice(0, Number(position[1])).split("\n").length : row ? Number(row[1]) : 1;
56
+ const position = /position (\d+)/.exec(message), row = /line (\d+)/i.exec(message);
57
+ return position ? value.slice(0, Number(position[1])).split("\n").length : row ? Number(row[1]) : 1;
59
58
  }
60
-
61
59
  export function parseRules(value, accounts, text) {
62
- let rules;
63
- try { rules = JSON.parse(value || "[]"); }
64
- catch (error) {
65
- throw new Error(text("integrationsInvalidJson", { line: jsonLine(value, String(error.message)) }));
66
- }
67
- return checkRules(rules, accounts, text);
68
- }
69
-
70
- export function checkRules(rules, accounts, text) {
71
- if (!Array.isArray(rules)) throw new Error(text("integrationsInvalidRules"));
72
- const known = new Set(accounts.map((account) => account.id));
73
- rules.forEach((rule, index) => {
74
- const ruleNumber = index + 1;
75
- if (!isRecord(rule)) throw new Error(text("integrationsInvalidRules"));
76
- const from = rule.from;
77
- if (!isRecord(from) || from.platform !== "chatroom" || from.account !== "center") {
78
- throw new Error(text("integrationsFromRequired", { index: ruleNumber }));
60
+ let rules;
61
+ try {
62
+ rules = JSON.parse(value || "[]");
79
63
  }
80
- if (!Array.isArray(rule.to) || !rule.to.length) {
81
- throw new Error(text("integrationsTargetRequired", { index: ruleNumber, target: 1, field: "to" }));
64
+ catch (error) {
65
+ const message = error instanceof Error ? error.message : String(error);
66
+ throw new Error(text("integrationsInvalidJson", { line: jsonLine(value, message) }));
82
67
  }
83
- rule.to.forEach((target, targetIndex) => {
84
- if (!isRecord(target)) throw new Error(text("integrationsInvalidRules"));
85
- for (const field of ["platform", "account", "room"]) {
86
- if (typeof target[field] !== "string" || !target[field].trim()) {
87
- throw new Error(text("integrationsTargetRequired", { index: ruleNumber, target: targetIndex + 1, field }));
68
+ return checkRules(rules, accounts, text);
69
+ }
70
+ export function checkRules(rules, accounts, text) {
71
+ if (!Array.isArray(rules))
72
+ throw new Error(text("integrationsInvalidRules"));
73
+ const known = new Set(accounts.map((account) => account.id));
74
+ rules.forEach((rule, index) => {
75
+ const ruleNumber = index + 1;
76
+ if (!isRecord(rule))
77
+ throw new Error(text("integrationsInvalidRules"));
78
+ const from = rule.from;
79
+ if (!isRecord(from) || from.platform !== "chatroom" || from.account !== "center") {
80
+ throw new Error(text("integrationsFromRequired", { index: ruleNumber }));
81
+ }
82
+ if (!Array.isArray(rule.to) || !rule.to.length) {
83
+ throw new Error(text("integrationsTargetRequired", { index: ruleNumber, target: 1, field: "to" }));
88
84
  }
89
- }
90
- if (!known.has(target.account)) {
91
- throw new Error(text("integrationsTargetMissing", { index: ruleNumber, target: targetIndex + 1, account: target.account }));
92
- }
85
+ rule.to.forEach((target, targetIndex) => {
86
+ if (!isRecord(target))
87
+ throw new Error(text("integrationsInvalidRules"));
88
+ for (const field of ["platform", "account", "room"]) {
89
+ const value = target[field];
90
+ if (typeof value !== "string" || !value.trim()) {
91
+ throw new Error(text("integrationsTargetRequired", { index: ruleNumber, target: targetIndex + 1, field }));
92
+ }
93
+ }
94
+ const targetAccount = typeof target.account === "string" ? target.account : "";
95
+ if (!known.has(targetAccount)) {
96
+ throw new Error(text("integrationsTargetMissing", { index: ruleNumber, target: targetIndex + 1, account: targetAccount }));
97
+ }
98
+ });
93
99
  });
94
- });
95
- return rules;
100
+ return rules;
96
101
  }
97
-
98
102
  export function parseInbound(value, accounts, text) {
99
- let inbound;
100
- try { inbound = JSON.parse(value || "[]"); }
101
- catch (error) {
102
- throw new Error(text("integrationsInvalidJson", { line: jsonLine(value, String(error.message)) }));
103
- }
104
- return checkInbound(inbound, accounts);
103
+ let inbound;
104
+ try {
105
+ inbound = JSON.parse(value || "[]");
106
+ }
107
+ catch (error) {
108
+ const message = error instanceof Error ? error.message : String(error);
109
+ throw new Error(text("integrationsInvalidJson", { line: jsonLine(value, message) }));
110
+ }
111
+ return checkInbound(inbound, accounts);
105
112
  }
106
-
107
113
  export function checkInbound(inbound, accounts) {
108
- if (!Array.isArray(inbound)) throw new Error("Inbound routes must be a JSON array.");
109
- const known = new Map(accounts.map((account) => [account.id, account]));
110
- inbound.forEach((route, index) => {
111
- const label = `Inbound route ${index + 1}`;
112
- if (!isRecord(route) || !isRecord(route.from)) throw new Error(`${label}: from is required.`);
113
- const from = route.from;
114
- for (const field of ["platform", "account", "room"]) {
115
- if (typeof from[field] !== "string" || !from[field].trim()) throw new Error(`${label}: from.${field} is required.`);
116
- }
117
- if (from.event !== undefined && typeof from.event !== "string") throw new Error(`${label}: from.event must be a string.`);
118
- const account = known.get(from.account);
119
- if (!account) throw new Error(`${label}: account “${from.account}” does not exist.`);
120
- if (account.platform !== from.platform) throw new Error(`${label}: account “${from.account}” belongs to “${account.platform}”.`);
121
- if (!isRecord(route.to) || typeof route.to.room !== "string" || !route.to.room.trim()) throw new Error(`${label}: to.room is required.`);
122
- });
123
- return inbound;
124
- }
114
+ if (!Array.isArray(inbound))
115
+ throw new Error("Inbound routes must be a JSON array.");
116
+ const known = new Map(accounts.map((account) => [account.id, account]));
117
+ inbound.forEach((route, index) => {
118
+ const label = `Inbound route ${index + 1}`;
119
+ if (!isRecord(route) || !isRecord(route.from))
120
+ throw new Error(`${label}: from is required.`);
121
+ const from = route.from;
122
+ for (const field of ["platform", "account", "room"]) {
123
+ const value = from[field];
124
+ if (typeof value !== "string" || !value.trim())
125
+ throw new Error(`${label}: from.${field} is required.`);
126
+ }
127
+ if (from.event !== undefined && typeof from.event !== "string")
128
+ throw new Error(`${label}: from.event must be a string.`);
129
+ const fromAccount = typeof from.account === "string" ? from.account : "";
130
+ const account = known.get(fromAccount);
131
+ if (!account)
132
+ throw new Error(`${label}: account “${fromAccount}” does not exist.`);
133
+ if (account.platform !== from.platform)
134
+ throw new Error(`${label}: account “${fromAccount}” belongs to “${account.platform}”.`);
135
+ if (!isRecord(route.to) || typeof route.to.room !== "string" || !route.to.room.trim())
136
+ throw new Error(`${label}: to.room is required.`);
137
+ });
138
+ return inbound;
139
+ }