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

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 (176) hide show
  1. package/CHANGELOG.md +52 -0
  2. package/LICENSE +2 -2
  3. package/README.md +40 -8
  4. package/README.zh-CN.md +40 -8
  5. package/cli.ts +27 -1
  6. package/dist/cli.js +5406 -3088
  7. package/package.json +25 -5
  8. package/packages/chat-room-channeld/README.md +111 -33
  9. package/packages/chat-room-channeld/src/adapter.ts +179 -7
  10. package/packages/chat-room-channeld/src/config.ts +63 -1
  11. package/packages/chat-room-channeld/src/delivery.ts +6 -1
  12. package/packages/chat-room-channeld/src/host-helpers.ts +11 -0
  13. package/packages/chat-room-channeld/src/host.ts +142 -9
  14. package/packages/chat-room-channeld/src/inbound.ts +35 -0
  15. package/packages/chat-room-channeld/src/protocol.ts +52 -1
  16. package/packages/chat-room-channeld/src/rules.ts +7 -2
  17. package/packages/chat-room-channeld/src/state.ts +17 -0
  18. package/packages/chat-room-channeld/src/stats.ts +43 -1
  19. package/plugin.ts +1 -1
  20. package/server/admin-state.ts +13 -13
  21. package/server/app.ts +95 -0
  22. package/server/assets/NOTICE.txt +2 -1
  23. package/server/assets/base.css +21 -9
  24. package/server/assets/chat/admin.js +0 -2
  25. package/server/assets/chat/announcements.js +69 -8
  26. package/server/assets/chat/chat-background.js +177 -0
  27. package/server/assets/chat/confirm-dialog.js +0 -1
  28. package/server/assets/chat/create-room-dialog.js +0 -1
  29. package/server/assets/chat/diagram-viewer.js +7 -1
  30. package/server/assets/chat/doc-preview.js +251 -46
  31. package/server/assets/chat/events.js +16 -5
  32. package/server/assets/chat/files.js +53 -7
  33. package/server/assets/chat/image-zoom.js +8 -1
  34. package/server/assets/chat/index.js +6 -2
  35. package/server/assets/chat/integration-account-schema.js +99 -0
  36. package/server/assets/chat/integration-editor.js +163 -0
  37. package/server/assets/chat/integration-values.js +124 -0
  38. package/server/assets/chat/integrations.js +648 -0
  39. package/server/assets/chat/locales/de.js +40 -4
  40. package/server/assets/chat/locales/es.js +40 -4
  41. package/server/assets/chat/locales/fr.js +40 -4
  42. package/server/assets/chat/locales/it.js +40 -4
  43. package/server/assets/chat/locales/ja.js +40 -4
  44. package/server/assets/chat/locales/ko.js +40 -4
  45. package/server/assets/chat/markdown-footnotes.js +1 -1
  46. package/server/assets/chat/markdown.js +45 -8
  47. package/server/assets/chat/messages.js +6 -5
  48. package/server/assets/chat/office-preview.js +69 -0
  49. package/server/assets/chat/palette.js +6 -1
  50. package/server/assets/chat/panels.js +56 -0
  51. package/server/assets/chat/recall.js +58 -7
  52. package/server/assets/chat/receipts.js +17 -6
  53. package/server/assets/chat/reminders.js +68 -9
  54. package/server/assets/chat/replies.js +0 -6
  55. package/server/assets/chat/resource-format.js +11 -0
  56. package/server/assets/chat/rooms.js +1 -6
  57. package/server/assets/chat/settings.js +98 -4
  58. package/server/assets/chat/shortcuts.js +0 -3
  59. package/server/assets/chat/side-pane.js +34 -10
  60. package/server/assets/chat/state.js +125 -12
  61. package/server/assets/chat/system-events.js +58 -0
  62. package/server/assets/chat/system-messages.js +32 -14
  63. package/server/assets/chat/task-row.js +0 -6
  64. package/server/assets/chat/tasks.js +119 -37
  65. package/server/assets/chat/window-controls.js +37 -0
  66. package/server/assets/code-highlight.css +71 -32
  67. package/server/assets/conversation.css +174 -31
  68. package/server/assets/doc-preview.css +40 -3
  69. package/server/assets/files.css +8 -0
  70. package/server/assets/fluent-icons.svg +2 -0
  71. package/server/assets/monaco/LICENSE +21 -0
  72. package/server/assets/monaco/ThirdPartyNotices.txt +448 -0
  73. package/server/assets/monaco/codicon-jdswhhtn.ttf +0 -0
  74. package/server/assets/monaco/editor.worker.js +45 -0
  75. package/server/assets/monaco/integration-rules.css +1 -0
  76. package/server/assets/monaco/integration-rules.js +1605 -0
  77. package/server/assets/monaco/json.worker.js +77 -0
  78. package/server/assets/office/THIRD-PARTY-NOTICES.txt +11 -0
  79. package/server/assets/office/docx.bundle.js +33 -0
  80. package/server/assets/office/licenses/docx-preview-LICENSE.txt +201 -0
  81. package/server/assets/office/licenses/echarts-LICENSE.txt +222 -0
  82. package/server/assets/office/licenses/jszip-LICENSE.txt +651 -0
  83. package/server/assets/office/licenses/lodash-LICENSE.txt +47 -0
  84. package/server/assets/office/licenses/pptx-preview-LICENSE.txt +8 -0
  85. package/server/assets/office/licenses/tslib-LICENSE.txt +12 -0
  86. package/server/assets/office/licenses/uuid-LICENSE.txt +9 -0
  87. package/server/assets/office/licenses/xlsx-LICENSE.txt +201 -0
  88. package/server/assets/office/licenses/zrender-LICENSE.txt +29 -0
  89. package/server/assets/office/pptx.bundle.js +104 -0
  90. package/server/assets/office/xlsx.bundle.js +141 -0
  91. package/server/assets/recall.css +5 -4
  92. package/server/assets/responsive.css +80 -11
  93. package/server/assets/settings.css +185 -9
  94. package/server/assets/tasks.css +57 -7
  95. package/server/assets/window-controls.css +36 -0
  96. package/server/channel-attachments.ts +133 -0
  97. package/server/channel-integration.ts +572 -0
  98. package/server/channel-settings.ts +77 -0
  99. package/server/channels.ts +274 -21
  100. package/server/chat-server.ts +32 -580
  101. package/server/chat.html +234 -34
  102. package/server/http-server.ts +3 -182
  103. package/server/manifest.webmanifest +1 -0
  104. package/server/resource-routes.ts +16 -16
  105. package/server/routes/inbox.ts +100 -0
  106. package/server/routes/pages.ts +83 -0
  107. package/server/routes/questions.ts +39 -0
  108. package/server/routes/room-actions.ts +162 -0
  109. package/server/routes/rooms.ts +160 -0
  110. package/server/startup.ts +84 -0
  111. package/server/sw.js +5 -0
  112. package/server/task-routes.ts +1 -1
  113. package/src/center/center-client.ts +3 -11
  114. package/src/config/file-config.ts +7 -54
  115. package/src/platform/http-server.ts +182 -0
  116. package/src/protocol/addressing.ts +21 -0
  117. package/src/protocol/config.ts +58 -0
  118. package/src/protocol/room.ts +51 -0
  119. package/src/rooms/resources.ts +26 -16
  120. package/src/rooms/room-service.ts +2 -48
  121. package/src/rooms/storage.ts +329 -0
  122. package/src/rooms/tasks.ts +21 -5
  123. package/src/runtime/watch.ts +1 -1
  124. package/src/state/pg-database.ts +183 -0
  125. package/src/state/pg-migrate.ts +175 -0
  126. package/src/state/pg-sql.mjs +175 -0
  127. package/src/state/pg-worker.mjs +133 -0
  128. package/src/state/sqlite-announcements.ts +25 -0
  129. package/src/state/sqlite-database.ts +130 -0
  130. package/src/state/sqlite-legacy.ts +90 -0
  131. package/src/state/sqlite-members.ts +155 -0
  132. package/src/state/sqlite-messages.ts +288 -0
  133. package/src/state/sqlite-resources.ts +39 -0
  134. package/src/state/sqlite-rooms.ts +194 -0
  135. package/src/state/sqlite-schema.ts +80 -0
  136. package/src/state/sqlite-state-store.ts +34 -0
  137. package/src/state/sqlite-state.ts +15 -897
  138. package/src/state/sqlite-tasks.ts +65 -0
  139. package/src/state/sqlite-transaction.ts +13 -0
  140. package/src/state/sqlite.ts +21 -1
  141. package/src/state/store.ts +5 -1
  142. package/src/tui/app.mjs +32 -19
  143. package/src/tui/config.mjs +12 -4
  144. package/src/tui/i18n.mjs +38 -0
  145. package/src/tui/info-app.mjs +50 -51
  146. package/src/tui/info-content.mjs +79 -44
  147. package/src/tui/join-app.mjs +9 -9
  148. package/src/tui/locales/en.mjs +349 -0
  149. package/src/tui/locales/zh-CN.mjs +349 -0
  150. package/src/tui/main.mjs +85 -37
  151. package/src/tui/markdown.mjs +3 -2
  152. package/src/tui/mentions.mjs +3 -1
  153. package/src/tui/message-app.mjs +22 -21
  154. package/src/tui/message-view.mjs +2 -1
  155. package/src/tui/palette-app.mjs +9 -8
  156. package/src/tui/palette.mjs +30 -24
  157. package/src/tui/prefs.mjs +92 -0
  158. package/src/tui/reminder-app.mjs +18 -17
  159. package/src/tui/room-settings-app.mjs +30 -28
  160. package/src/tui/settings-app.mjs +153 -0
  161. package/src/tui/ui-content.mjs +65 -86
  162. package/src/tui/ui-help.mjs +73 -0
  163. package/src/tui/ui-overlay-view.mjs +23 -7
  164. package/src/tui/ui-suggest.mjs +6 -5
  165. package/src/tui/ui-theme.mjs +37 -33
  166. package/src/tui/ui-view.mjs +22 -7
  167. package/src/tui/ui.mjs +15 -3
  168. package/src/web/editor.worker.ts +1 -0
  169. package/src/web/integration-rules-editor.ts +573 -0
  170. package/src/web/integration-rules.ts +26 -0
  171. package/src/web/integration-schema.ts +229 -0
  172. package/src/web/json.worker.ts +1 -0
  173. package/src/web/office-viewer.ts +221 -0
  174. package/packages/chat-room-channeld/dist/feishu-adapter.js +0 -499
  175. package/packages/chat-room-channeld/src/feishu-adapter.ts +0 -473
  176. package/server/assets/chat/forward.js +0 -69
@@ -0,0 +1,99 @@
1
+ // Account form generated from the JSON Schema a plugin declares in initialize.
2
+ const CONTROL = { string: "text", secret: "password", number: "number" };
3
+
4
+ function textFor(field, fallback) {
5
+ return typeof field.label === "string" && field.label.trim() ? field.label : fallback;
6
+ }
7
+
8
+ function addField(container, field, existing, stored, text) {
9
+ const label = document.createElement("label");
10
+ label.className = "field integration-schema-field";
11
+ const caption = document.createElement("span");
12
+ caption.className = "field-label";
13
+ caption.textContent = textFor(field, field.key);
14
+ if (field.required) {
15
+ const required = document.createElement("span");
16
+ required.className = "integration-required";
17
+ required.textContent = " *";
18
+ caption.append(required);
19
+ }
20
+ let control;
21
+ if (field.type === "boolean" || field.type === "select") {
22
+ control = document.createElement("select");
23
+ control.className = "input";
24
+ const blank = document.createElement("option");
25
+ blank.value = "";
26
+ blank.textContent = existing
27
+ ? (stored ? text("integrationsKeepStored") : text("integrationsNotSet"))
28
+ : "";
29
+ control.append(blank);
30
+ const options = field.type === "boolean"
31
+ ? [{ value: "1", label: "True" }, { value: "0", label: "False" }]
32
+ : Array.isArray(field.options) ? field.options : [];
33
+ for (const option of options) {
34
+ const node = document.createElement("option");
35
+ node.value = String(option.value);
36
+ node.textContent = typeof option.label === "string" && option.label ? option.label : String(option.value);
37
+ control.append(node);
38
+ }
39
+ if (!existing && typeof field.default === "string" && field.default !== "") {
40
+ control.value = field.type === "boolean"
41
+ ? (["1", "true", "on", "yes"].includes(field.default.toLowerCase()) ? "1" : "0")
42
+ : field.default;
43
+ }
44
+ } else {
45
+ control = document.createElement("input");
46
+ control.className = "input";
47
+ control.type = CONTROL[field.type] || "text";
48
+ control.autocomplete = field.type === "secret" ? "new-password" : "off";
49
+ if (existing) control.placeholder = stored ? text("integrationsLeaveBlank") : text("integrationsNotSet");
50
+ else if (typeof field.default === "string") control.value = field.default;
51
+ }
52
+ control.dataset.accountKey = field.key;
53
+ control.dataset.accountType = field.type || "string";
54
+ control.dataset.accountRequired = field.required ? "1" : "0";
55
+ control.dataset.accountStored = stored ? "1" : "0";
56
+ label.append(caption, control);
57
+ if (typeof field.description === "string" && field.description.trim()) {
58
+ const hint = document.createElement("span");
59
+ hint.className = "hint";
60
+ hint.textContent = field.description;
61
+ label.append(hint);
62
+ }
63
+ container.append(label);
64
+ }
65
+
66
+ export function renderAccountSchema(container, plugin, account, text) {
67
+ container.textContent = "";
68
+ const fields = Array.isArray(plugin?.account?.schema) ? plugin.account.schema : [];
69
+ container.hidden = fields.length === 0;
70
+ if (!fields.length) return;
71
+ const existing = Boolean(account);
72
+ const stored = new Set(account?.envKeys || []);
73
+ for (const field of fields) {
74
+ if (!field || typeof field.key !== "string") continue;
75
+ addField(container, field, existing, stored.has(field.key), text);
76
+ }
77
+ }
78
+
79
+ /** Values still land in the account env map; blank means "keep stored". */
80
+ export function readAccountSchema(container, text) {
81
+ const values = {};
82
+ const keys = new Set();
83
+ const missing = [];
84
+ for (const control of container.querySelectorAll("[data-account-key]")) {
85
+ const key = control.dataset.accountKey;
86
+ keys.add(key);
87
+ const value = String(control.value ?? "").trim();
88
+ if (value !== "") {
89
+ values[key] = value;
90
+ continue;
91
+ }
92
+ if (control.dataset.accountRequired === "1" && control.dataset.accountStored !== "1") {
93
+ const label = control.closest(".integration-schema-field")?.querySelector(".field-label")?.textContent || key;
94
+ missing.push(label.replace(/\s*\*$/, ""));
95
+ }
96
+ }
97
+ if (missing.length) throw new Error(text("integrationsAccountRequired", { fields: missing.join(", ") }));
98
+ return { values, keys };
99
+ }
@@ -0,0 +1,163 @@
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
+ const BUNDLE_URL = "/assets/monaco/integration-rules.js";
4
+ const STYLES_URL = "/assets/monaco/integration-rules.css";
5
+ let bundlePromise = null;
6
+
7
+ 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;
27
+ }
28
+
29
+ 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";
49
+ 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);
82
+ }
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
+ }
@@ -0,0 +1,124 @@
1
+ // Parsing and account-reference checks for integration rules and inbound routes.
2
+ const isRecord = (value) => Boolean(value) && typeof value === "object" && !Array.isArray(value);
3
+
4
+ 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;
10
+ }
11
+
12
+ export function stateLabel(state, text) {
13
+ return text({
14
+ started: "integrationsStateStarted", starting: "integrationsStateStarting", stopped: "integrationsStateStopped",
15
+ error: "integrationsStateError", failed: "integrationsStateError",
16
+ }[state] || "integrationsStateUnknown");
17
+ }
18
+
19
+ 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;
28
+ }
29
+
30
+ 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
+ };
44
+ }
45
+
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 });
51
+ }
52
+ }
53
+ return plugins;
54
+ }
55
+
56
+ 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;
59
+ }
60
+
61
+ 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 }));
79
+ }
80
+ if (!Array.isArray(rule.to) || !rule.to.length) {
81
+ throw new Error(text("integrationsTargetRequired", { index: ruleNumber, target: 1, field: "to" }));
82
+ }
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 }));
88
+ }
89
+ }
90
+ if (!known.has(target.account)) {
91
+ throw new Error(text("integrationsTargetMissing", { index: ruleNumber, target: targetIndex + 1, account: target.account }));
92
+ }
93
+ });
94
+ });
95
+ return rules;
96
+ }
97
+
98
+ 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);
105
+ }
106
+
107
+ 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
+ }