@openclaw/matrix 2026.3.13 → 2026.5.9-beta.1

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 (206) hide show
  1. package/dist/account-config-D2W-V1eQ.js +96 -0
  2. package/dist/account-selection-BWwIruri.js +158 -0
  3. package/dist/accounts-Bm90Rzvp.js +130 -0
  4. package/dist/active-client-uhlxdhEy.js +20 -0
  5. package/dist/allowlist-sTzpCn5d.js +68 -0
  6. package/dist/api.js +12 -0
  7. package/dist/approval-handler.runtime-DWTQfd4m.js +370 -0
  8. package/dist/approval-ids-DoC2z7tR.js +7 -0
  9. package/dist/approval-reaction-auth-DbcA1gGd.js +27 -0
  10. package/dist/approval-reactions-o2_tuH8D.js +162 -0
  11. package/dist/async-lock-uQfhfQIY.js +19 -0
  12. package/dist/auth-presence.js +26 -0
  13. package/dist/backup-health-Cabu_WQC.js +60 -0
  14. package/dist/channel-DJNir3Rb.js +1116 -0
  15. package/dist/channel-plugin-api.js +2 -0
  16. package/dist/channel.runtime-BQu0hTih.js +246 -0
  17. package/dist/cli-BmfTmg7x.js +1340 -0
  18. package/dist/cli-metadata-B-PCEzrA.js +22 -0
  19. package/dist/cli-metadata.js +2 -0
  20. package/dist/client-DkcXnm0X.js +25 -0
  21. package/dist/client-_hckQNGW.js +31 -0
  22. package/dist/client-bootstrap-Rb8oHvhH.js +114 -0
  23. package/dist/config--5-S2Akv.js +452 -0
  24. package/dist/config-paths-nsVaysCu.js +19 -0
  25. package/dist/config-schema-nPLpEgHl.js +200 -0
  26. package/dist/config-secret-input.runtime-DiKFehsE.js +2 -0
  27. package/dist/config-update-wZX-HLMn.js +143 -0
  28. package/dist/contract-api.js +9 -0
  29. package/dist/create-client-DCnqDaqd.js +64 -0
  30. package/dist/credentials-DV6fWXhC.js +56 -0
  31. package/dist/credentials-read-cmHgousK.js +112 -0
  32. package/dist/credentials-write.runtime-zniTq-Gr.js +17 -0
  33. package/dist/crypto-node.runtime-pihzdpY7.js +12 -0
  34. package/dist/crypto-runtime-ZI0zAtn3.js +1214 -0
  35. package/dist/deps-C6WqKY7m.js +235 -0
  36. package/dist/device-health-UVYpbA_W.js +16 -0
  37. package/dist/direct-management-DMMMgtTB.js +249 -0
  38. package/dist/direct-room-XkutHjES.js +76 -0
  39. package/dist/directory-live-DmOtMhyr.js +150 -0
  40. package/dist/doctor-C4__7c-U.js +153 -0
  41. package/dist/doctor-contract-D4-64QuJ.js +246 -0
  42. package/dist/doctor-contract-api.js +2 -0
  43. package/dist/draft-stream-BE2QevQQ.js +144 -0
  44. package/dist/encryption-guidance-BPi3A_m3.js +15 -0
  45. package/dist/env-auth-BJqGI8M6.js +63 -0
  46. package/dist/env-vars-C7uQCTKn.js +63 -0
  47. package/dist/errors-CTcpEDq-.js +17 -0
  48. package/dist/exec-approval-resolver-Bza9Dhlm.js +15 -0
  49. package/dist/exec-approvals-Crnh543m.js +196 -0
  50. package/dist/helper-api.js +4 -0
  51. package/dist/http-client-C7AeVJay.js +319 -0
  52. package/dist/index.js +46 -0
  53. package/dist/legacy-crypto-inspector-poDWldgy.js +41 -0
  54. package/dist/legacy-crypto-restore-Biw-w2ng.js +85 -0
  55. package/dist/logger-CnZRVrux.js +78 -0
  56. package/dist/logging-DZHSPP5N.js +99 -0
  57. package/dist/matrix-migration.runtime-WY6ffcrf.js +525 -0
  58. package/dist/media-text-DU6nWZuj.js +146 -0
  59. package/dist/messages-BpihMh82.js +140 -0
  60. package/dist/migration-snapshot-backup-DaCHTp8C.js +69 -0
  61. package/dist/migration-snapshot.runtime-CKHE3xF9.js +2 -0
  62. package/dist/monitor-C_81r_Ck.js +4125 -0
  63. package/dist/plugin-entry.handlers.runtime.js +51 -0
  64. package/dist/probe.runtime-BvAzYAIe.js +3 -0
  65. package/dist/profile-BlHu0wDX.js +111 -0
  66. package/dist/profile-update-DjeBNgIV.js +69 -0
  67. package/dist/reaction-common-ejrL19w-.js +71 -0
  68. package/dist/reaction-events-CiARZfjk.js +121 -0
  69. package/dist/record-shared-CHWJCTWf.js +2 -0
  70. package/dist/recovery-key-store-BTJ6jz5v.js +294 -0
  71. package/dist/resolve-targets-YtJnw1Tb.js +140 -0
  72. package/dist/resolver.runtime-D9piiGEl.js +5 -0
  73. package/dist/rolldown-runtime-DUslC3ob.js +14 -0
  74. package/dist/route-D6rg-iXN.js +161 -0
  75. package/dist/runtime-C6X4h_SJ.js +6 -0
  76. package/dist/runtime-Dog86njy.js +8 -0
  77. package/dist/runtime-api-BXWBFIqm.js +25 -0
  78. package/dist/runtime-api.js +25 -0
  79. package/dist/runtime-heavy-api.js +3 -0
  80. package/dist/runtime-setter-api.js +2 -0
  81. package/dist/sdk-B2vZA27-.js +1416 -0
  82. package/dist/secret-contract-DcrJWCQI.js +120 -0
  83. package/dist/secret-contract-api.js +2 -0
  84. package/dist/send-Bo0DU1ca.js +1200 -0
  85. package/dist/session-store-metadata-DI5SCofx.js +77 -0
  86. package/dist/setup-bootstrap-ImenBsMt.js +62 -0
  87. package/dist/setup-core-CfZy05oW.js +116 -0
  88. package/dist/setup-dm-policy-2-r1FrQh.js +194 -0
  89. package/dist/setup-entry.js +19 -0
  90. package/dist/setup-plugin-api.js +44 -0
  91. package/dist/setup-surface-CqT_o61M.js +540 -0
  92. package/dist/shared-CpMoYKm1.js +195 -0
  93. package/dist/startup-abort-56edvmbM.js +32 -0
  94. package/dist/startup-verification-Demyp0bP.js +132 -0
  95. package/dist/storage-paths-BJLdnCjV.js +52 -0
  96. package/dist/storage-tC3ujLiW.js +281 -0
  97. package/dist/subagent-hooks-DQbyqq9V.js +149 -0
  98. package/dist/subagent-hooks-api.js +23 -0
  99. package/dist/sync-state-C_beeevA.js +12 -0
  100. package/dist/target-ids-80nQ2gql.js +77 -0
  101. package/dist/test-api.js +4 -0
  102. package/dist/thread-binding-api-Cq_E-E1K.js +17 -0
  103. package/dist/thread-binding-api.js +2 -0
  104. package/dist/thread-bindings-B9mesxXk.js +352 -0
  105. package/dist/thread-bindings-runtime.js +2 -0
  106. package/dist/thread-bindings-shared-DK-d-oYX.js +97 -0
  107. package/dist/timeout-abort-signal-CtaIaP1v.js +2 -0
  108. package/dist/tool-actions.runtime-BIH49vRr.js +532 -0
  109. package/dist/url-validation-DiK9j7jz.js +36 -0
  110. package/dist/verification-CZ2rDeHL.js +345 -0
  111. package/openclaw.plugin.json +788 -1
  112. package/package.json +82 -16
  113. package/CHANGELOG.md +0 -104
  114. package/index.ts +0 -22
  115. package/src/actions.ts +0 -195
  116. package/src/channel.directory.test.ts +0 -135
  117. package/src/channel.ts +0 -461
  118. package/src/config-schema.test.ts +0 -26
  119. package/src/config-schema.ts +0 -62
  120. package/src/directory-live.test.ts +0 -85
  121. package/src/directory-live.ts +0 -209
  122. package/src/group-mentions.ts +0 -52
  123. package/src/matrix/accounts.test.ts +0 -131
  124. package/src/matrix/accounts.ts +0 -114
  125. package/src/matrix/actions/client.ts +0 -47
  126. package/src/matrix/actions/limits.test.ts +0 -15
  127. package/src/matrix/actions/limits.ts +0 -6
  128. package/src/matrix/actions/messages.ts +0 -126
  129. package/src/matrix/actions/pins.test.ts +0 -74
  130. package/src/matrix/actions/pins.ts +0 -84
  131. package/src/matrix/actions/reactions.test.ts +0 -109
  132. package/src/matrix/actions/reactions.ts +0 -102
  133. package/src/matrix/actions/room.ts +0 -85
  134. package/src/matrix/actions/summary.ts +0 -75
  135. package/src/matrix/actions/types.ts +0 -85
  136. package/src/matrix/actions.ts +0 -15
  137. package/src/matrix/active-client.ts +0 -32
  138. package/src/matrix/client/config.ts +0 -245
  139. package/src/matrix/client/create-client.ts +0 -125
  140. package/src/matrix/client/logging.ts +0 -46
  141. package/src/matrix/client/runtime.ts +0 -4
  142. package/src/matrix/client/shared.test.ts +0 -85
  143. package/src/matrix/client/shared.ts +0 -210
  144. package/src/matrix/client/startup.test.ts +0 -49
  145. package/src/matrix/client/startup.ts +0 -29
  146. package/src/matrix/client/storage.ts +0 -131
  147. package/src/matrix/client/types.ts +0 -34
  148. package/src/matrix/client-bootstrap.ts +0 -47
  149. package/src/matrix/client.test.ts +0 -56
  150. package/src/matrix/client.ts +0 -14
  151. package/src/matrix/credentials.ts +0 -125
  152. package/src/matrix/deps.test.ts +0 -74
  153. package/src/matrix/deps.ts +0 -126
  154. package/src/matrix/format.test.ts +0 -33
  155. package/src/matrix/format.ts +0 -22
  156. package/src/matrix/index.ts +0 -11
  157. package/src/matrix/monitor/access-policy.ts +0 -126
  158. package/src/matrix/monitor/allowlist.test.ts +0 -45
  159. package/src/matrix/monitor/allowlist.ts +0 -94
  160. package/src/matrix/monitor/auto-join.ts +0 -72
  161. package/src/matrix/monitor/direct.test.ts +0 -396
  162. package/src/matrix/monitor/direct.ts +0 -152
  163. package/src/matrix/monitor/events.test.ts +0 -186
  164. package/src/matrix/monitor/events.ts +0 -168
  165. package/src/matrix/monitor/handler.body-for-agent.test.ts +0 -196
  166. package/src/matrix/monitor/handler.ts +0 -768
  167. package/src/matrix/monitor/inbound-body.test.ts +0 -73
  168. package/src/matrix/monitor/inbound-body.ts +0 -28
  169. package/src/matrix/monitor/index.test.ts +0 -18
  170. package/src/matrix/monitor/index.ts +0 -414
  171. package/src/matrix/monitor/location.ts +0 -100
  172. package/src/matrix/monitor/media.test.ts +0 -86
  173. package/src/matrix/monitor/media.ts +0 -118
  174. package/src/matrix/monitor/mentions.test.ts +0 -154
  175. package/src/matrix/monitor/mentions.ts +0 -62
  176. package/src/matrix/monitor/replies.test.ts +0 -184
  177. package/src/matrix/monitor/replies.ts +0 -124
  178. package/src/matrix/monitor/room-info.ts +0 -55
  179. package/src/matrix/monitor/rooms.test.ts +0 -124
  180. package/src/matrix/monitor/rooms.ts +0 -47
  181. package/src/matrix/monitor/threads.ts +0 -68
  182. package/src/matrix/monitor/types.ts +0 -39
  183. package/src/matrix/poll-types.test.ts +0 -21
  184. package/src/matrix/poll-types.ts +0 -167
  185. package/src/matrix/probe.ts +0 -69
  186. package/src/matrix/sdk-runtime.ts +0 -18
  187. package/src/matrix/send/client.ts +0 -99
  188. package/src/matrix/send/formatting.ts +0 -93
  189. package/src/matrix/send/media.ts +0 -230
  190. package/src/matrix/send/targets.test.ts +0 -98
  191. package/src/matrix/send/targets.ts +0 -150
  192. package/src/matrix/send/types.ts +0 -110
  193. package/src/matrix/send-queue.test.ts +0 -145
  194. package/src/matrix/send-queue.ts +0 -28
  195. package/src/matrix/send.test.ts +0 -319
  196. package/src/matrix/send.ts +0 -267
  197. package/src/onboarding.ts +0 -462
  198. package/src/outbound.test.ts +0 -159
  199. package/src/outbound.ts +0 -58
  200. package/src/resolve-targets.test.ts +0 -68
  201. package/src/resolve-targets.ts +0 -125
  202. package/src/runtime.ts +0 -6
  203. package/src/secret-input.ts +0 -13
  204. package/src/test-mocks.ts +0 -53
  205. package/src/tool-actions.ts +0 -164
  206. package/src/types.ts +0 -118
@@ -0,0 +1,195 @@
1
+ import { t as __exportAll } from "./rolldown-runtime-DUslC3ob.js";
2
+ import { n as LogService } from "./logger-CnZRVrux.js";
3
+ import { t as awaitMatrixStartupWithAbort } from "./startup-abort-56edvmbM.js";
4
+ import { n as resolveMatrixAuth, r as resolveMatrixAuthContext } from "./config--5-S2Akv.js";
5
+ import { normalizeOptionalAccountId } from "openclaw/plugin-sdk/account-id";
6
+ //#region extensions/matrix/src/matrix/client/shared.ts
7
+ var shared_exports = /* @__PURE__ */ __exportAll({
8
+ acquireSharedMatrixClient: () => acquireSharedMatrixClient,
9
+ releaseSharedClientInstance: () => releaseSharedClientInstance,
10
+ removeSharedClientInstance: () => removeSharedClientInstance,
11
+ resolveSharedMatrixClient: () => resolveSharedMatrixClient,
12
+ stopSharedClient: () => stopSharedClient,
13
+ stopSharedClientForAccount: () => stopSharedClientForAccount,
14
+ stopSharedClientInstance: () => stopSharedClientInstance
15
+ });
16
+ let matrixCreateClientDepsPromise;
17
+ async function loadMatrixCreateClientDeps() {
18
+ matrixCreateClientDepsPromise ??= import("./create-client-DCnqDaqd.js").then((n) => n.n).then((runtime) => ({ createMatrixClient: runtime.createMatrixClient }));
19
+ return await matrixCreateClientDepsPromise;
20
+ }
21
+ const sharedClientStates = /* @__PURE__ */ new Map();
22
+ const sharedClientPromises = /* @__PURE__ */ new Map();
23
+ function serializeDispatcherPolicyKey(auth) {
24
+ return JSON.stringify(auth.dispatcherPolicy ?? null);
25
+ }
26
+ function buildSharedClientKey(auth) {
27
+ return [
28
+ auth.homeserver,
29
+ auth.userId,
30
+ auth.accessToken,
31
+ auth.encryption ? "e2ee" : "plain",
32
+ auth.allowPrivateNetwork ? "private-net" : "strict-net",
33
+ serializeDispatcherPolicyKey(auth),
34
+ auth.accountId
35
+ ].join("|");
36
+ }
37
+ async function createSharedMatrixClient(params) {
38
+ const { createMatrixClient } = await loadMatrixCreateClientDeps();
39
+ return {
40
+ client: await createMatrixClient({
41
+ homeserver: params.auth.homeserver,
42
+ userId: params.auth.userId,
43
+ accessToken: params.auth.accessToken,
44
+ password: params.auth.password,
45
+ deviceId: params.auth.deviceId,
46
+ encryption: params.auth.encryption,
47
+ localTimeoutMs: params.timeoutMs,
48
+ initialSyncLimit: params.auth.initialSyncLimit,
49
+ accountId: params.auth.accountId,
50
+ allowPrivateNetwork: params.auth.allowPrivateNetwork,
51
+ ssrfPolicy: params.auth.ssrfPolicy,
52
+ dispatcherPolicy: params.auth.dispatcherPolicy
53
+ }),
54
+ key: buildSharedClientKey(params.auth),
55
+ started: false,
56
+ cryptoReady: false,
57
+ startPromise: null,
58
+ leases: 0
59
+ };
60
+ }
61
+ function findSharedClientStateByInstance(client) {
62
+ for (const state of sharedClientStates.values()) if (state.client === client) return state;
63
+ return null;
64
+ }
65
+ function deleteSharedClientState(state) {
66
+ sharedClientStates.delete(state.key);
67
+ sharedClientPromises.delete(state.key);
68
+ }
69
+ async function ensureSharedClientStarted(params) {
70
+ const waitForStart = async (startPromise) => {
71
+ await awaitMatrixStartupWithAbort(startPromise, params.abortSignal);
72
+ };
73
+ if (params.state.started) return;
74
+ if (params.state.startPromise) {
75
+ await waitForStart(params.state.startPromise);
76
+ return;
77
+ }
78
+ const guardedStart = (async () => {
79
+ const client = params.state.client;
80
+ if (params.encryption && !params.state.cryptoReady) try {
81
+ const joinedRooms = await client.getJoinedRooms();
82
+ if (client.crypto) {
83
+ await client.crypto.prepare(joinedRooms);
84
+ params.state.cryptoReady = true;
85
+ }
86
+ } catch (err) {
87
+ LogService.warn("MatrixClientLite", "Failed to prepare crypto:", err);
88
+ }
89
+ await client.start({ abortSignal: params.abortSignal });
90
+ params.state.started = true;
91
+ })().finally(() => {
92
+ if (params.state.startPromise === guardedStart) params.state.startPromise = null;
93
+ });
94
+ params.state.startPromise = guardedStart;
95
+ await waitForStart(guardedStart);
96
+ }
97
+ async function resolveSharedMatrixClientState(params = {}) {
98
+ const requestedAccountId = normalizeOptionalAccountId(params.accountId);
99
+ if (params.auth && requestedAccountId && requestedAccountId !== params.auth.accountId) throw new Error(`Matrix shared client account mismatch: requested ${requestedAccountId}, auth resolved ${params.auth.accountId}`);
100
+ const authContext = (() => {
101
+ if (params.auth) return null;
102
+ if (!params.cfg) throw new Error("Matrix shared client requires a resolved runtime config. Load and resolve config at the command or gateway boundary, then pass cfg through the runtime path.");
103
+ return resolveMatrixAuthContext({
104
+ cfg: params.cfg,
105
+ env: params.env,
106
+ accountId: params.accountId
107
+ });
108
+ })();
109
+ const auth = params.auth ?? await resolveMatrixAuth({
110
+ cfg: authContext?.cfg ?? params.cfg,
111
+ env: authContext?.env ?? params.env,
112
+ accountId: authContext?.accountId
113
+ });
114
+ const key = buildSharedClientKey(auth);
115
+ const shouldStart = params.startClient !== false;
116
+ const existingState = sharedClientStates.get(key);
117
+ if (existingState) {
118
+ if (shouldStart) await ensureSharedClientStarted({
119
+ state: existingState,
120
+ encryption: auth.encryption,
121
+ abortSignal: params.abortSignal
122
+ });
123
+ return existingState;
124
+ }
125
+ const existingPromise = sharedClientPromises.get(key);
126
+ if (existingPromise) {
127
+ const pending = await existingPromise;
128
+ if (shouldStart) await ensureSharedClientStarted({
129
+ state: pending,
130
+ encryption: auth.encryption,
131
+ abortSignal: params.abortSignal
132
+ });
133
+ return pending;
134
+ }
135
+ const creationPromise = createSharedMatrixClient({
136
+ auth,
137
+ timeoutMs: params.timeoutMs
138
+ });
139
+ sharedClientPromises.set(key, creationPromise);
140
+ try {
141
+ const created = await creationPromise;
142
+ sharedClientStates.set(key, created);
143
+ if (shouldStart) await ensureSharedClientStarted({
144
+ state: created,
145
+ encryption: auth.encryption,
146
+ abortSignal: params.abortSignal
147
+ });
148
+ return created;
149
+ } finally {
150
+ sharedClientPromises.delete(key);
151
+ }
152
+ }
153
+ async function resolveSharedMatrixClient(params = {}) {
154
+ return (await resolveSharedMatrixClientState(params)).client;
155
+ }
156
+ async function acquireSharedMatrixClient(params = {}) {
157
+ const state = await resolveSharedMatrixClientState(params);
158
+ state.leases += 1;
159
+ return state.client;
160
+ }
161
+ function stopSharedClient() {
162
+ for (const state of sharedClientStates.values()) state.client.stop();
163
+ sharedClientStates.clear();
164
+ sharedClientPromises.clear();
165
+ }
166
+ function stopSharedClientForAccount(auth) {
167
+ const key = buildSharedClientKey(auth);
168
+ const state = sharedClientStates.get(key);
169
+ if (!state) return;
170
+ state.client.stop();
171
+ deleteSharedClientState(state);
172
+ }
173
+ function removeSharedClientInstance(client) {
174
+ const state = findSharedClientStateByInstance(client);
175
+ if (!state) return false;
176
+ deleteSharedClientState(state);
177
+ return true;
178
+ }
179
+ function stopSharedClientInstance(client) {
180
+ if (!removeSharedClientInstance(client)) return;
181
+ client.stop();
182
+ }
183
+ async function releaseSharedClientInstance(client, mode = "stop") {
184
+ const state = findSharedClientStateByInstance(client);
185
+ if (!state) return false;
186
+ state.leases = Math.max(0, state.leases - 1);
187
+ if (state.leases > 0) return false;
188
+ deleteSharedClientState(state);
189
+ if (mode === "persist") await client.stopAndPersist();
190
+ else if (mode === "discard") client.stopWithoutPersist();
191
+ else client.stop();
192
+ return true;
193
+ }
194
+ //#endregion
195
+ export { shared_exports as a, resolveSharedMatrixClient as i, releaseSharedClientInstance as n, stopSharedClientForAccount as o, removeSharedClientInstance as r, stopSharedClientInstance as s, acquireSharedMatrixClient as t };
@@ -0,0 +1,32 @@
1
+ //#region extensions/matrix/src/matrix/startup-abort.ts
2
+ function createMatrixStartupAbortError() {
3
+ const error = /* @__PURE__ */ new Error("Matrix startup aborted");
4
+ error.name = "AbortError";
5
+ return error;
6
+ }
7
+ function throwIfMatrixStartupAborted(abortSignal) {
8
+ if (abortSignal?.aborted === true) throw createMatrixStartupAbortError();
9
+ }
10
+ function isMatrixStartupAbortError(error) {
11
+ return error instanceof Error && error.name === "AbortError";
12
+ }
13
+ async function awaitMatrixStartupWithAbort(promise, abortSignal) {
14
+ if (!abortSignal) return await promise;
15
+ if (abortSignal.aborted) throw createMatrixStartupAbortError();
16
+ return await new Promise((resolve, reject) => {
17
+ const onAbort = () => {
18
+ abortSignal.removeEventListener("abort", onAbort);
19
+ reject(createMatrixStartupAbortError());
20
+ };
21
+ abortSignal.addEventListener("abort", onAbort, { once: true });
22
+ promise.then((value) => {
23
+ abortSignal.removeEventListener("abort", onAbort);
24
+ resolve(value);
25
+ }, (error) => {
26
+ abortSignal.removeEventListener("abort", onAbort);
27
+ reject(error);
28
+ });
29
+ });
30
+ }
31
+ //#endregion
32
+ export { throwIfMatrixStartupAborted as i, createMatrixStartupAbortError as n, isMatrixStartupAbortError as r, awaitMatrixStartupWithAbort as t };
@@ -0,0 +1,132 @@
1
+ import { t as formatMatrixErrorMessage } from "./errors-CTcpEDq-.js";
2
+ import { a as resolveMatrixStoragePaths } from "./storage-tC3ujLiW.js";
3
+ import path from "node:path";
4
+ import { readJsonFileWithFallback, writeJsonFileAtomically } from "openclaw/plugin-sdk/json-store";
5
+ import fs from "node:fs/promises";
6
+ //#region extensions/matrix/src/matrix/monitor/startup-verification.ts
7
+ const STARTUP_VERIFICATION_STATE_FILENAME = "startup-verification.json";
8
+ const DEFAULT_STARTUP_VERIFICATION_MODE = "if-unverified";
9
+ const DEFAULT_STARTUP_VERIFICATION_COOLDOWN_HOURS = 24;
10
+ const DEFAULT_STARTUP_VERIFICATION_FAILURE_COOLDOWN_MS = 3600 * 1e3;
11
+ function normalizeCooldownHours(value) {
12
+ if (typeof value !== "number" || !Number.isFinite(value)) return DEFAULT_STARTUP_VERIFICATION_COOLDOWN_HOURS;
13
+ return Math.max(0, value);
14
+ }
15
+ function resolveStartupVerificationStatePath(params) {
16
+ const storagePaths = resolveMatrixStoragePaths({
17
+ homeserver: params.auth.homeserver,
18
+ userId: params.auth.userId,
19
+ accessToken: params.auth.accessToken,
20
+ accountId: params.auth.accountId,
21
+ deviceId: params.auth.deviceId,
22
+ env: params.env
23
+ });
24
+ return path.join(storagePaths.rootDir, STARTUP_VERIFICATION_STATE_FILENAME);
25
+ }
26
+ async function readStartupVerificationState(filePath) {
27
+ const { value } = await readJsonFileWithFallback(filePath, null);
28
+ return value && typeof value === "object" ? value : null;
29
+ }
30
+ async function clearStartupVerificationState(filePath) {
31
+ await fs.rm(filePath, { force: true }).catch(() => {});
32
+ }
33
+ function resolveStateCooldownMs(state, cooldownMs) {
34
+ if (state?.outcome === "failed") return Math.min(cooldownMs, DEFAULT_STARTUP_VERIFICATION_FAILURE_COOLDOWN_MS);
35
+ return cooldownMs;
36
+ }
37
+ function resolveRetryAfterMs(params) {
38
+ const attemptedAtMs = Date.parse(params.attemptedAt ?? "");
39
+ if (!Number.isFinite(attemptedAtMs)) return;
40
+ const remaining = attemptedAtMs + params.cooldownMs - params.nowMs;
41
+ return remaining > 0 ? remaining : void 0;
42
+ }
43
+ function shouldHonorCooldown(params) {
44
+ if (!params.state || params.stateCooldownMs <= 0) return false;
45
+ if (params.state.userId && params.verification.userId && params.state.userId !== params.verification.userId) return false;
46
+ if (params.state.deviceId && params.verification.deviceId && params.state.deviceId !== params.verification.deviceId) return false;
47
+ return resolveRetryAfterMs({
48
+ attemptedAt: params.state.attemptedAt,
49
+ cooldownMs: params.stateCooldownMs,
50
+ nowMs: params.nowMs
51
+ }) !== void 0;
52
+ }
53
+ function hasPendingSelfVerification(verifications) {
54
+ return verifications.some((entry) => entry.isSelfVerification && !entry.completed && entry.pending);
55
+ }
56
+ async function ensureMatrixStartupVerification(params) {
57
+ if (params.auth.encryption !== true || !params.client.crypto) return { kind: "unsupported" };
58
+ const verification = await params.client.getOwnDeviceVerificationStatus();
59
+ const statePath = params.stateFilePath ?? resolveStartupVerificationStatePath({
60
+ auth: params.auth,
61
+ env: params.env
62
+ });
63
+ if (verification.verified) {
64
+ await clearStartupVerificationState(statePath);
65
+ return {
66
+ kind: "verified",
67
+ verification
68
+ };
69
+ }
70
+ if ((params.accountConfig.startupVerification ?? DEFAULT_STARTUP_VERIFICATION_MODE) === "off") {
71
+ await clearStartupVerificationState(statePath);
72
+ return {
73
+ kind: "disabled",
74
+ verification
75
+ };
76
+ }
77
+ if (hasPendingSelfVerification(await params.client.crypto.listVerifications().catch(() => []))) return {
78
+ kind: "pending",
79
+ verification
80
+ };
81
+ const cooldownMs = normalizeCooldownHours(params.accountConfig.startupVerificationCooldownHours) * 60 * 60 * 1e3;
82
+ const nowMs = params.nowMs ?? Date.now();
83
+ const state = await readStartupVerificationState(statePath);
84
+ const stateCooldownMs = resolveStateCooldownMs(state, cooldownMs);
85
+ if (shouldHonorCooldown({
86
+ state,
87
+ verification,
88
+ stateCooldownMs,
89
+ nowMs
90
+ })) return {
91
+ kind: "cooldown",
92
+ verification,
93
+ retryAfterMs: resolveRetryAfterMs({
94
+ attemptedAt: state?.attemptedAt,
95
+ cooldownMs: stateCooldownMs,
96
+ nowMs
97
+ })
98
+ };
99
+ try {
100
+ const request = await params.client.crypto.requestVerification({ ownUser: true });
101
+ await writeJsonFileAtomically(statePath, {
102
+ userId: verification.userId,
103
+ deviceId: verification.deviceId,
104
+ attemptedAt: new Date(nowMs).toISOString(),
105
+ outcome: "requested",
106
+ requestId: request.id,
107
+ transactionId: request.transactionId
108
+ });
109
+ return {
110
+ kind: "requested",
111
+ verification,
112
+ requestId: request.id,
113
+ transactionId: request.transactionId ?? void 0
114
+ };
115
+ } catch (err) {
116
+ const error = formatMatrixErrorMessage(err);
117
+ await writeJsonFileAtomically(statePath, {
118
+ userId: verification.userId,
119
+ deviceId: verification.deviceId,
120
+ attemptedAt: new Date(nowMs).toISOString(),
121
+ outcome: "failed",
122
+ error
123
+ }).catch(() => {});
124
+ return {
125
+ kind: "request-failed",
126
+ verification,
127
+ error
128
+ };
129
+ }
130
+ }
131
+ //#endregion
132
+ export { ensureMatrixStartupVerification };
@@ -0,0 +1,52 @@
1
+ import { DEFAULT_ACCOUNT_ID, normalizeAccountId } from "openclaw/plugin-sdk/account-id";
2
+ import { normalizeLowercaseStringOrEmpty } from "openclaw/plugin-sdk/string-coerce-runtime";
3
+ import path from "node:path";
4
+ import crypto from "node:crypto";
5
+ //#region extensions/matrix/src/storage-paths.ts
6
+ function sanitizeMatrixPathSegment(value) {
7
+ return normalizeLowercaseStringOrEmpty(value).replace(/[^a-z0-9._-]+/g, "_").replace(/^_+|_+$/g, "") || "unknown";
8
+ }
9
+ function resolveMatrixHomeserverKey(homeserver) {
10
+ try {
11
+ const url = new URL(homeserver);
12
+ if (url.host) return sanitizeMatrixPathSegment(url.host);
13
+ } catch {}
14
+ return sanitizeMatrixPathSegment(homeserver);
15
+ }
16
+ function hashMatrixAccessToken(accessToken) {
17
+ return crypto.createHash("sha256").update(accessToken).digest("hex").slice(0, 16);
18
+ }
19
+ function resolveMatrixCredentialsFilename(accountId) {
20
+ const normalized = normalizeAccountId(accountId);
21
+ return normalized === DEFAULT_ACCOUNT_ID ? "credentials.json" : `credentials-${normalized}.json`;
22
+ }
23
+ function resolveMatrixCredentialsDir(stateDir) {
24
+ return path.join(stateDir, "credentials", "matrix");
25
+ }
26
+ function resolveMatrixCredentialsPath(params) {
27
+ return path.join(resolveMatrixCredentialsDir(params.stateDir), resolveMatrixCredentialsFilename(params.accountId));
28
+ }
29
+ function resolveMatrixLegacyFlatStoreRoot(stateDir) {
30
+ return path.join(stateDir, "matrix");
31
+ }
32
+ function resolveMatrixLegacyFlatStoragePaths(stateDir) {
33
+ const rootDir = resolveMatrixLegacyFlatStoreRoot(stateDir);
34
+ return {
35
+ rootDir,
36
+ storagePath: path.join(rootDir, "bot-storage.json"),
37
+ cryptoPath: path.join(rootDir, "crypto")
38
+ };
39
+ }
40
+ function resolveMatrixAccountStorageRoot(params) {
41
+ const accountKey = sanitizeMatrixPathSegment(params.accountId ?? DEFAULT_ACCOUNT_ID);
42
+ const userKey = sanitizeMatrixPathSegment(params.userId);
43
+ const serverKey = resolveMatrixHomeserverKey(params.homeserver);
44
+ const tokenHash = hashMatrixAccessToken(params.accessToken);
45
+ return {
46
+ rootDir: path.join(params.stateDir, "matrix", "accounts", accountKey, `${serverKey}__${userKey}`, tokenHash),
47
+ accountKey,
48
+ tokenHash
49
+ };
50
+ }
51
+ //#endregion
52
+ export { resolveMatrixCredentialsPath as a, resolveMatrixLegacyFlatStoreRoot as c, resolveMatrixCredentialsFilename as i, sanitizeMatrixPathSegment as l, resolveMatrixAccountStorageRoot as n, resolveMatrixHomeserverKey as o, resolveMatrixCredentialsDir as r, resolveMatrixLegacyFlatStoragePaths as s, hashMatrixAccessToken as t };