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

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 +10 -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 +8 -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 +198 -161
  23. package/server/assets/chat/announcements.js +295 -208
  24. package/server/assets/chat/attachment-store.js +106 -75
  25. package/server/assets/chat/attachments.js +686 -441
  26. package/server/assets/chat/chat-background.js +185 -156
  27. package/server/assets/chat/clipboard.js +44 -25
  28. package/server/assets/chat/confirm-dialog.js +36 -33
  29. package/server/assets/chat/copy-action.js +40 -40
  30. package/server/assets/chat/create-room-dialog.js +50 -47
  31. package/server/assets/chat/diagram-viewer.js +293 -234
  32. package/server/assets/chat/doc-preview.js +507 -441
  33. package/server/assets/chat/editor.js +423 -335
  34. package/server/assets/chat/events.js +637 -510
  35. package/server/assets/chat/files.js +476 -430
  36. package/server/assets/chat/history-loader.js +79 -73
  37. package/server/assets/chat/icons.js +9 -11
  38. package/server/assets/chat/image-zoom.js +239 -176
  39. package/server/assets/chat/index.js +2 -3
  40. package/server/assets/chat/install-prompt.js +86 -66
  41. package/server/assets/chat/integration-account-schema.js +88 -86
  42. package/server/assets/chat/integration-editor.js +162 -153
  43. package/server/assets/chat/integration-values.js +109 -100
  44. package/server/assets/chat/integrations.js +689 -622
  45. package/server/assets/chat/join-requests.js +408 -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 -102
  54. package/server/assets/chat/markdown.js +547 -403
  55. package/server/assets/chat/members.js +236 -197
  56. package/server/assets/chat/mentions.js +157 -133
  57. package/server/assets/chat/message-cache.js +40 -33
  58. package/server/assets/chat/message-polling.js +280 -227
  59. package/server/assets/chat/message-window.js +74 -55
  60. package/server/assets/chat/messages.js +552 -480
  61. package/server/assets/chat/office-preview.js +46 -45
  62. package/server/assets/chat/palette.js +450 -417
  63. package/server/assets/chat/panels.js +94 -78
  64. package/server/assets/chat/pwa.js +144 -73
  65. package/server/assets/chat/questions.js +372 -302
  66. package/server/assets/chat/recall.js +320 -273
  67. package/server/assets/chat/receipts.js +149 -121
  68. package/server/assets/chat/reminders.js +331 -214
  69. package/server/assets/chat/replies.js +165 -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 +72 -50
  73. package/server/assets/chat/rooms.js +623 -500
  74. package/server/assets/chat/search.js +136 -114
  75. package/server/assets/chat/settings.js +223 -200
  76. package/server/assets/chat/shiki.js +61 -62
  77. package/server/assets/chat/shortcuts.js +212 -187
  78. package/server/assets/chat/side-pane.js +160 -150
  79. package/server/assets/chat/state.js +849 -735
  80. package/server/assets/chat/system-events.js +45 -43
  81. package/server/assets/chat/system-messages.js +57 -56
  82. package/server/assets/chat/task-form.js +178 -182
  83. package/server/assets/chat/task-row.js +158 -155
  84. package/server/assets/chat/tasks.js +653 -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 +2 -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
@@ -3,161 +3,170 @@
3
3
  const BUNDLE_URL = "/assets/monaco/integration-rules.js";
4
4
  const STYLES_URL = "/assets/monaco/integration-rules.css";
5
5
  let bundlePromise = null;
6
-
7
6
  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;
7
+ if (!bundlePromise) {
8
+ bundlePromise = import(BUNDLE_URL)
9
+ .then((module) => {
10
+ if (!document.querySelector("link[data-integration-rules-editor]")) {
11
+ const link = document.createElement("link");
12
+ link.rel = "stylesheet";
13
+ link.href = STYLES_URL;
14
+ link.dataset.integrationRulesEditor = "true";
15
+ document.head.append(link);
16
+ }
17
+ return module;
18
+ })
19
+ .catch((error) => {
20
+ // Keep this a warning: a blocked asset must not surface as an uncaught page error.
21
+ console.warn("Integration code editor unavailable; keeping the plain text editor.", error);
22
+ throw error;
23
+ });
24
+ }
25
+ return bundlePromise;
27
26
  }
28
-
29
27
  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";
28
+ const host = app.$(options.editor);
29
+ const textarea = app.$(options.textarea);
30
+ const fallback = app.$(options.fallback);
31
+ const error = app.$(options.error);
32
+ const errorList = app.$(options.errors);
33
+ const formatGroup = app.$(options.formats);
34
+ let controller = null, loading = null, failed = false, editable = true;
35
+ if (formatGroup)
36
+ formatGroup.hidden = true;
37
+ const applyEditable = () => {
38
+ host?.setAttribute("data-read-only", String(!editable));
39
+ if (textarea)
40
+ textarea.readOnly = !editable;
41
+ for (const button of formatGroup?.querySelectorAll("[data-format]") || [])
42
+ button.disabled = !editable;
43
+ controller?.setReadOnly(!editable);
44
+ };
45
+ const updateFormatButtons = () => {
46
+ const active = controller?.getFormat() || "yaml";
47
+ for (const button of formatGroup?.querySelectorAll("[data-format]") || []) {
48
+ button.setAttribute("aria-pressed", String(button.dataset.format === active));
49
+ }
50
+ };
51
+ const render = (result) => {
52
+ if (textarea)
53
+ textarea.value = result.text;
54
+ host?.setAttribute("data-error-count", String(result.errors.length));
55
+ host?.setAttribute("aria-invalid", String(result.errors.length > 0));
56
+ updateFormatButtons();
57
+ if (!error || !errorList)
58
+ return;
59
+ errorList.textContent = "";
60
+ if (!result.errors.length) {
61
+ error.hidden = true;
62
+ errorList.hidden = true;
63
+ return;
64
+ }
65
+ const first = result.errors[0];
66
+ error.textContent = app.t("integrationsRulesErrors", {
67
+ count: result.errors.length,
68
+ first: `Line ${first.line}: ${first.message}`,
69
+ });
70
+ error.hidden = false;
71
+ errorList.hidden = false;
72
+ for (const issue of result.errors) {
73
+ const item = document.createElement("li");
74
+ const button = document.createElement("button");
75
+ button.type = "button";
76
+ button.className = "integration-rule-error";
77
+ button.textContent = `Line ${issue.line}${issue.path ? ` · ${issue.path}` : ""}: ${issue.message}`;
78
+ button.addEventListener("click", () => controller?.reveal(issue.line, issue.column));
79
+ item.append(button);
80
+ errorList.append(item);
81
+ }
82
+ };
83
+ const clearErrors = () => {
84
+ if (error)
85
+ error.hidden = true;
86
+ if (errorList) {
87
+ errorList.hidden = true;
88
+ errorList.textContent = "";
89
+ }
90
+ host?.setAttribute("data-error-count", "0");
91
+ host?.removeAttribute("aria-invalid");
92
+ };
93
+ const ensure = () => {
94
+ if (controller || failed || !host)
95
+ return loading;
96
+ if (loading)
97
+ return loading;
98
+ loading = loadBundle().then((module) => {
99
+ controller = module.createIntegrationRulesEditor({
100
+ kind: options.kind,
101
+ container: host,
102
+ value: options.getValue(),
103
+ accounts: options.getAccounts(),
104
+ onChange: render,
105
+ });
106
+ host.hidden = false;
107
+ if (textarea)
108
+ textarea.hidden = true;
109
+ if (fallback)
110
+ fallback.hidden = true;
111
+ if (formatGroup)
112
+ formatGroup.hidden = false;
113
+ applyEditable();
114
+ render(controller.validate());
115
+ }).catch(() => {
116
+ failed = true;
117
+ if (fallback) {
118
+ fallback.textContent = app.t("integrationsEditorUnavailable");
119
+ fallback.hidden = false;
120
+ }
121
+ }).finally(() => { loading = null; });
122
+ return loading;
123
+ };
124
+ const refresh = () => {
125
+ const value = options.getValue() ?? [];
126
+ clearErrors();
127
+ if (controller)
128
+ controller.setValue(value);
129
+ else if (textarea)
130
+ textarea.value = JSON.stringify(value, null, 2);
131
+ };
132
+ const current = () => {
133
+ if (controller) {
134
+ const result = controller.validate();
135
+ render(result);
136
+ return { editor: true, ...result };
137
+ }
138
+ return { editor: false, text: textarea?.value || "", errors: [] };
139
+ };
49
140
  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);
141
+ button.addEventListener("click", () => {
142
+ if (!controller || !editable)
143
+ return;
144
+ controller.setFormat(button.dataset.format === "yaml" ? "yaml" : "json");
145
+ updateFormatButtons();
146
+ });
82
147
  }
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
- }
148
+ textarea?.addEventListener("input", clearErrors);
149
+ return {
150
+ ensure,
151
+ refresh,
152
+ current,
153
+ showMessage: (message) => {
154
+ if (!error)
155
+ return;
156
+ error.textContent = message;
157
+ error.hidden = false;
158
+ if (errorList) {
159
+ errorList.hidden = true;
160
+ errorList.textContent = "";
161
+ }
162
+ },
163
+ setAccounts: (accounts) => controller?.setAccounts(accounts),
164
+ setEditable: (next) => {
165
+ editable = next !== false;
166
+ applyEditable();
167
+ },
168
+ getText: () => textarea?.value || "",
169
+ hasEditor: () => Boolean(controller),
170
+ dispose: () => controller?.dispose(),
171
+ };
172
+ }
@@ -1,124 +1,133 @@
1
1
  // Parsing and account-reference checks for integration rules and inbound routes.
2
2
  const isRecord = (value) => Boolean(value) && typeof value === "object" && !Array.isArray(value);
3
-
4
3
  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;
4
+ const value = String(error?.message || error || "").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
+ throw new Error(text("integrationsInvalidJson", { line: jsonLine(value, String(error.message)) }));
82
66
  }
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 }));
67
+ return checkRules(rules, accounts, text);
68
+ }
69
+ export function checkRules(rules, accounts, text) {
70
+ if (!Array.isArray(rules))
71
+ 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))
76
+ throw new Error(text("integrationsInvalidRules"));
77
+ const from = rule.from;
78
+ if (!isRecord(from) || from.platform !== "chatroom" || from.account !== "center") {
79
+ throw new Error(text("integrationsFromRequired", { index: ruleNumber }));
80
+ }
81
+ if (!Array.isArray(rule.to) || !rule.to.length) {
82
+ throw new Error(text("integrationsTargetRequired", { index: ruleNumber, target: 1, field: "to" }));
88
83
  }
89
- }
90
- if (!known.has(target.account)) {
91
- throw new Error(text("integrationsTargetMissing", { index: ruleNumber, target: targetIndex + 1, account: target.account }));
92
- }
84
+ rule.to.forEach((target, targetIndex) => {
85
+ if (!isRecord(target))
86
+ throw new Error(text("integrationsInvalidRules"));
87
+ for (const field of ["platform", "account", "room"]) {
88
+ if (typeof target[field] !== "string" || !target[field].trim()) {
89
+ throw new Error(text("integrationsTargetRequired", { index: ruleNumber, target: targetIndex + 1, field }));
90
+ }
91
+ }
92
+ if (!known.has(target.account)) {
93
+ throw new Error(text("integrationsTargetMissing", { index: ruleNumber, target: targetIndex + 1, account: target.account }));
94
+ }
95
+ });
93
96
  });
94
- });
95
- return rules;
97
+ return rules;
96
98
  }
97
-
98
99
  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);
100
+ let inbound;
101
+ try {
102
+ inbound = JSON.parse(value || "[]");
103
+ }
104
+ catch (error) {
105
+ throw new Error(text("integrationsInvalidJson", { line: jsonLine(value, String(error.message)) }));
106
+ }
107
+ return checkInbound(inbound, accounts);
105
108
  }
106
-
107
109
  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
- }
110
+ if (!Array.isArray(inbound))
111
+ throw new Error("Inbound routes must be a JSON array.");
112
+ const known = new Map(accounts.map((account) => [account.id, account]));
113
+ inbound.forEach((route, index) => {
114
+ const label = `Inbound route ${index + 1}`;
115
+ if (!isRecord(route) || !isRecord(route.from))
116
+ throw new Error(`${label}: from is required.`);
117
+ const from = route.from;
118
+ for (const field of ["platform", "account", "room"]) {
119
+ if (typeof from[field] !== "string" || !from[field].trim())
120
+ throw new Error(`${label}: from.${field} is required.`);
121
+ }
122
+ if (from.event !== undefined && typeof from.event !== "string")
123
+ throw new Error(`${label}: from.event must be a string.`);
124
+ const account = known.get(from.account);
125
+ if (!account)
126
+ throw new Error(`${label}: account “${from.account}” does not exist.`);
127
+ if (account.platform !== from.platform)
128
+ throw new Error(`${label}: account “${from.account}” belongs to “${account.platform}”.`);
129
+ if (!isRecord(route.to) || typeof route.to.room !== "string" || !route.to.room.trim())
130
+ throw new Error(`${label}: to.room is required.`);
131
+ });
132
+ return inbound;
133
+ }