@kodelyth/matrix 2026.5.42 → 2026.6.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 (205) hide show
  1. package/klaw.plugin.json +891 -3
  2. package/package.json +16 -4
  3. package/CHANGELOG.md +0 -321
  4. package/SPEC-SUPPORT.md +0 -116
  5. package/api.ts +0 -38
  6. package/auth-presence.ts +0 -56
  7. package/channel-plugin-api.ts +0 -3
  8. package/cli-metadata.ts +0 -11
  9. package/contract-api.ts +0 -17
  10. package/doctor-contract-api.ts +0 -1
  11. package/helper-api.ts +0 -3
  12. package/index.ts +0 -55
  13. package/plugin-entry.handlers.runtime.ts +0 -1
  14. package/runtime-api.ts +0 -72
  15. package/runtime-heavy-api.ts +0 -1
  16. package/runtime-setter-api.ts +0 -3
  17. package/secret-contract-api.ts +0 -5
  18. package/setup-entry.ts +0 -17
  19. package/setup-plugin-api.ts +0 -3
  20. package/src/account-selection.ts +0 -223
  21. package/src/actions.ts +0 -346
  22. package/src/approval-auth.ts +0 -25
  23. package/src/approval-handler.runtime.ts +0 -592
  24. package/src/approval-ids.ts +0 -6
  25. package/src/approval-native.ts +0 -345
  26. package/src/approval-reaction-auth.ts +0 -45
  27. package/src/approval-reactions.ts +0 -313
  28. package/src/auth-precedence.ts +0 -61
  29. package/src/channel-account-paths.ts +0 -97
  30. package/src/channel.runtime.ts +0 -17
  31. package/src/channel.setup.ts +0 -48
  32. package/src/channel.ts +0 -667
  33. package/src/cli-metadata.ts +0 -19
  34. package/src/cli.ts +0 -2298
  35. package/src/config-adapter.ts +0 -41
  36. package/src/config-schema.ts +0 -159
  37. package/src/config-ui-hints.ts +0 -56
  38. package/src/directory-live.ts +0 -238
  39. package/src/doctor-contract.ts +0 -287
  40. package/src/doctor.ts +0 -262
  41. package/src/env-vars.ts +0 -92
  42. package/src/exec-approval-resolver.ts +0 -23
  43. package/src/exec-approvals.ts +0 -287
  44. package/src/group-mentions.ts +0 -41
  45. package/src/legacy-crypto-inspector-availability.ts +0 -60
  46. package/src/legacy-crypto.ts +0 -531
  47. package/src/legacy-state.ts +0 -156
  48. package/src/matrix/account-config.ts +0 -175
  49. package/src/matrix/accounts.ts +0 -194
  50. package/src/matrix/actions/client.ts +0 -31
  51. package/src/matrix/actions/devices.ts +0 -34
  52. package/src/matrix/actions/limits.ts +0 -6
  53. package/src/matrix/actions/messages.ts +0 -129
  54. package/src/matrix/actions/pins.ts +0 -63
  55. package/src/matrix/actions/polls.ts +0 -109
  56. package/src/matrix/actions/profile.ts +0 -37
  57. package/src/matrix/actions/reactions.ts +0 -59
  58. package/src/matrix/actions/room.ts +0 -71
  59. package/src/matrix/actions/summary.ts +0 -88
  60. package/src/matrix/actions/types.ts +0 -63
  61. package/src/matrix/actions/verification.ts +0 -589
  62. package/src/matrix/actions.ts +0 -37
  63. package/src/matrix/active-client.ts +0 -26
  64. package/src/matrix/async-lock.ts +0 -18
  65. package/src/matrix/backup-health.ts +0 -124
  66. package/src/matrix/client/config-runtime-api.ts +0 -9
  67. package/src/matrix/client/config-secret-input.runtime.ts +0 -1
  68. package/src/matrix/client/config.ts +0 -853
  69. package/src/matrix/client/create-client.ts +0 -105
  70. package/src/matrix/client/env-auth.ts +0 -95
  71. package/src/matrix/client/file-sync-store.ts +0 -289
  72. package/src/matrix/client/logging.ts +0 -140
  73. package/src/matrix/client/migration-snapshot.runtime.ts +0 -1
  74. package/src/matrix/client/private-network-host.ts +0 -1
  75. package/src/matrix/client/runtime.ts +0 -4
  76. package/src/matrix/client/shared.ts +0 -316
  77. package/src/matrix/client/storage.ts +0 -543
  78. package/src/matrix/client/types.ts +0 -50
  79. package/src/matrix/client/url-validation.ts +0 -73
  80. package/src/matrix/client-bootstrap.ts +0 -173
  81. package/src/matrix/client.ts +0 -23
  82. package/src/matrix/config-paths.ts +0 -31
  83. package/src/matrix/config-update.ts +0 -292
  84. package/src/matrix/credentials-read.ts +0 -208
  85. package/src/matrix/credentials-write.runtime.ts +0 -35
  86. package/src/matrix/credentials.ts +0 -95
  87. package/src/matrix/deps.ts +0 -309
  88. package/src/matrix/device-health.ts +0 -29
  89. package/src/matrix/direct-management.ts +0 -349
  90. package/src/matrix/direct-room.ts +0 -128
  91. package/src/matrix/draft-stream.ts +0 -225
  92. package/src/matrix/encryption-guidance.ts +0 -24
  93. package/src/matrix/errors.ts +0 -21
  94. package/src/matrix/format.ts +0 -426
  95. package/src/matrix/legacy-crypto-inspector.ts +0 -95
  96. package/src/matrix/media-errors.ts +0 -20
  97. package/src/matrix/media-text.ts +0 -162
  98. package/src/matrix/monitor/access-state.ts +0 -145
  99. package/src/matrix/monitor/ack-config.ts +0 -27
  100. package/src/matrix/monitor/allowlist.ts +0 -89
  101. package/src/matrix/monitor/auto-join.ts +0 -86
  102. package/src/matrix/monitor/config.ts +0 -569
  103. package/src/matrix/monitor/context-summary.ts +0 -43
  104. package/src/matrix/monitor/direct.ts +0 -296
  105. package/src/matrix/monitor/events.ts +0 -397
  106. package/src/matrix/monitor/handler.ts +0 -2266
  107. package/src/matrix/monitor/inbound-dedupe.ts +0 -267
  108. package/src/matrix/monitor/index.ts +0 -540
  109. package/src/matrix/monitor/legacy-crypto-restore.ts +0 -139
  110. package/src/matrix/monitor/location.ts +0 -108
  111. package/src/matrix/monitor/media.ts +0 -119
  112. package/src/matrix/monitor/mentions.ts +0 -256
  113. package/src/matrix/monitor/reaction-events.ts +0 -197
  114. package/src/matrix/monitor/recent-invite.ts +0 -30
  115. package/src/matrix/monitor/replies.ts +0 -136
  116. package/src/matrix/monitor/reply-context.ts +0 -92
  117. package/src/matrix/monitor/room-history.ts +0 -301
  118. package/src/matrix/monitor/room-info.ts +0 -126
  119. package/src/matrix/monitor/rooms.ts +0 -52
  120. package/src/matrix/monitor/route.ts +0 -179
  121. package/src/matrix/monitor/runtime-api.ts +0 -28
  122. package/src/matrix/monitor/startup-verification.ts +0 -237
  123. package/src/matrix/monitor/startup.ts +0 -218
  124. package/src/matrix/monitor/status.ts +0 -120
  125. package/src/matrix/monitor/sync-lifecycle.ts +0 -91
  126. package/src/matrix/monitor/task-runner.ts +0 -38
  127. package/src/matrix/monitor/test-events.ts +0 -21
  128. package/src/matrix/monitor/thread-context.ts +0 -108
  129. package/src/matrix/monitor/threads.ts +0 -85
  130. package/src/matrix/monitor/types.ts +0 -30
  131. package/src/matrix/monitor/verification-events.ts +0 -643
  132. package/src/matrix/monitor/verification-utils.ts +0 -46
  133. package/src/matrix/outbound-media-runtime.ts +0 -1
  134. package/src/matrix/poll-summary.ts +0 -110
  135. package/src/matrix/poll-types.ts +0 -429
  136. package/src/matrix/probe.runtime.ts +0 -4
  137. package/src/matrix/probe.ts +0 -97
  138. package/src/matrix/profile.ts +0 -184
  139. package/src/matrix/reaction-common.ts +0 -147
  140. package/src/matrix/sdk/crypto-bootstrap.ts +0 -438
  141. package/src/matrix/sdk/crypto-facade.ts +0 -242
  142. package/src/matrix/sdk/crypto-node.runtime.ts +0 -17
  143. package/src/matrix/sdk/crypto-runtime.ts +0 -14
  144. package/src/matrix/sdk/decrypt-bridge.ts +0 -410
  145. package/src/matrix/sdk/event-helpers.ts +0 -83
  146. package/src/matrix/sdk/http-client.ts +0 -87
  147. package/src/matrix/sdk/idb-persistence-lock.ts +0 -51
  148. package/src/matrix/sdk/idb-persistence.ts +0 -288
  149. package/src/matrix/sdk/logger.ts +0 -108
  150. package/src/matrix/sdk/read-response-with-limit.ts +0 -19
  151. package/src/matrix/sdk/recovery-key-store.ts +0 -453
  152. package/src/matrix/sdk/timeout-abort-signal.ts +0 -1
  153. package/src/matrix/sdk/transport-runtime-api.ts +0 -18
  154. package/src/matrix/sdk/transport.ts +0 -352
  155. package/src/matrix/sdk/types.ts +0 -245
  156. package/src/matrix/sdk/verification-manager.ts +0 -795
  157. package/src/matrix/sdk/verification-status.ts +0 -23
  158. package/src/matrix/sdk.ts +0 -2152
  159. package/src/matrix/send/client.ts +0 -93
  160. package/src/matrix/send/formatting.ts +0 -189
  161. package/src/matrix/send/media.ts +0 -244
  162. package/src/matrix/send/targets.ts +0 -104
  163. package/src/matrix/send/types.ts +0 -131
  164. package/src/matrix/send.ts +0 -660
  165. package/src/matrix/session-store-metadata.ts +0 -108
  166. package/src/matrix/startup-abort.ts +0 -44
  167. package/src/matrix/subagent-hooks.ts +0 -308
  168. package/src/matrix/sync-state.ts +0 -27
  169. package/src/matrix/target-ids.ts +0 -79
  170. package/src/matrix/thread-bindings-shared.ts +0 -206
  171. package/src/matrix/thread-bindings.ts +0 -580
  172. package/src/matrix-migration.runtime.ts +0 -9
  173. package/src/migration-config.ts +0 -243
  174. package/src/migration-snapshot-backup.ts +0 -116
  175. package/src/migration-snapshot.ts +0 -53
  176. package/src/onboarding.ts +0 -775
  177. package/src/outbound.ts +0 -248
  178. package/src/plugin-entry.runtime.js +0 -115
  179. package/src/plugin-entry.runtime.ts +0 -70
  180. package/src/profile-update.ts +0 -71
  181. package/src/record-shared.ts +0 -3
  182. package/src/resolve-targets.ts +0 -175
  183. package/src/resolver.runtime.ts +0 -5
  184. package/src/resolver.ts +0 -21
  185. package/src/runtime-api.ts +0 -106
  186. package/src/runtime.ts +0 -13
  187. package/src/secret-contract.ts +0 -174
  188. package/src/session-route.ts +0 -126
  189. package/src/setup-bootstrap.ts +0 -102
  190. package/src/setup-config.ts +0 -222
  191. package/src/setup-contract.ts +0 -90
  192. package/src/setup-core.ts +0 -146
  193. package/src/setup-dm-policy.ts +0 -15
  194. package/src/setup-surface.ts +0 -4
  195. package/src/startup-maintenance.ts +0 -114
  196. package/src/storage-paths.ts +0 -92
  197. package/src/thread-binding-api.ts +0 -23
  198. package/src/tool-actions.runtime.ts +0 -1
  199. package/src/tool-actions.ts +0 -498
  200. package/src/types.ts +0 -257
  201. package/subagent-hooks-api.ts +0 -31
  202. package/test-api.ts +0 -21
  203. package/thread-binding-api.ts +0 -4
  204. package/thread-bindings-runtime.ts +0 -4
  205. package/tsconfig.json +0 -16
@@ -1,175 +0,0 @@
1
- import { normalizeAccountId } from "klaw/plugin-sdk/account-id";
2
- import { DEFAULT_ACCOUNT_ID } from "klaw/plugin-sdk/account-id";
3
- import {
4
- listConfiguredAccountIds,
5
- resolveMergedAccountConfig,
6
- resolveNormalizedAccountEntry,
7
- } from "klaw/plugin-sdk/account-resolution-runtime";
8
- import { hasConfiguredSecretInput } from "klaw/plugin-sdk/secret-input-runtime";
9
- import type { CoreConfig, MatrixAccountConfig, MatrixConfig } from "../types.js";
10
-
11
- type MatrixRoomEntries = Record<string, NonNullable<MatrixConfig["groups"]>[string]>;
12
-
13
- export function resolveMatrixBaseConfig(cfg: CoreConfig): MatrixConfig {
14
- return cfg.channels?.matrix ?? {};
15
- }
16
-
17
- function resolveMatrixAccountsMap(cfg: CoreConfig): Readonly<Record<string, MatrixAccountConfig>> {
18
- const accounts = resolveMatrixBaseConfig(cfg).accounts;
19
- if (!accounts || typeof accounts !== "object") {
20
- return {};
21
- }
22
- return accounts;
23
- }
24
-
25
- function selectInheritedMatrixRoomEntries(params: {
26
- entries: MatrixRoomEntries | undefined;
27
- accountId: string;
28
- }): MatrixRoomEntries | undefined {
29
- const entries = params.entries;
30
- if (!entries) {
31
- return undefined;
32
- }
33
- const selected = Object.fromEntries(
34
- Object.entries(entries).filter(([, value]) => {
35
- const scopedAccount =
36
- typeof value?.account === "string" ? normalizeAccountId(value.account) : undefined;
37
- return scopedAccount === undefined || scopedAccount === params.accountId;
38
- }),
39
- ) as MatrixRoomEntries;
40
- return Object.keys(selected).length > 0 ? selected : undefined;
41
- }
42
-
43
- function mergeMatrixRoomEntries(
44
- inherited: MatrixRoomEntries | undefined,
45
- accountEntries: MatrixRoomEntries | undefined,
46
- hasAccountOverride: boolean,
47
- ): MatrixRoomEntries | undefined {
48
- if (!inherited && !accountEntries) {
49
- return undefined;
50
- }
51
- if (hasAccountOverride && Object.keys(accountEntries ?? {}).length === 0) {
52
- return undefined;
53
- }
54
- const merged: MatrixRoomEntries = {
55
- ...inherited,
56
- };
57
- for (const [key, value] of Object.entries(accountEntries ?? {})) {
58
- const inheritedValue = merged[key];
59
- merged[key] =
60
- inheritedValue && value
61
- ? {
62
- ...inheritedValue,
63
- ...value,
64
- }
65
- : (value ?? inheritedValue);
66
- }
67
- return Object.keys(merged).length > 0 ? merged : undefined;
68
- }
69
-
70
- export function listNormalizedMatrixAccountIds(cfg: CoreConfig): string[] {
71
- return listConfiguredAccountIds({
72
- accounts: resolveMatrixAccountsMap(cfg),
73
- normalizeAccountId,
74
- });
75
- }
76
-
77
- export function findMatrixAccountConfig(
78
- cfg: CoreConfig,
79
- accountId: string,
80
- ): MatrixAccountConfig | undefined {
81
- return resolveNormalizedAccountEntry(
82
- resolveMatrixAccountsMap(cfg),
83
- accountId,
84
- normalizeAccountId,
85
- );
86
- }
87
-
88
- export function hasExplicitMatrixAccountConfig(cfg: CoreConfig, accountId: string): boolean {
89
- const normalized = normalizeAccountId(accountId);
90
- if (findMatrixAccountConfig(cfg, normalized)) {
91
- return true;
92
- }
93
- if (normalized !== DEFAULT_ACCOUNT_ID) {
94
- return false;
95
- }
96
- const matrix = resolveMatrixBaseConfig(cfg);
97
- return (
98
- typeof matrix.enabled === "boolean" ||
99
- typeof matrix.name === "string" ||
100
- typeof matrix.homeserver === "string" ||
101
- typeof matrix.userId === "string" ||
102
- hasConfiguredSecretInput(matrix.accessToken) ||
103
- hasConfiguredSecretInput(matrix.password) ||
104
- typeof matrix.deviceId === "string" ||
105
- typeof matrix.deviceName === "string" ||
106
- typeof matrix.avatarUrl === "string"
107
- );
108
- }
109
-
110
- export function resolveMatrixAccountConfig(params: {
111
- cfg: CoreConfig;
112
- accountId?: string | null;
113
- env?: NodeJS.ProcessEnv;
114
- }): MatrixConfig {
115
- const accountId = normalizeAccountId(params.accountId);
116
- const base = resolveMatrixBaseConfig(params.cfg);
117
- const merged = resolveMergedAccountConfig<MatrixConfig>({
118
- channelConfig: base,
119
- accounts: params.cfg.channels?.matrix?.accounts as
120
- | Record<string, Partial<MatrixConfig>>
121
- | undefined,
122
- accountId,
123
- normalizeAccountId,
124
- nestedObjectKeys: ["dm", "actions", "execApprovals", "botLoopProtection"],
125
- });
126
- const accountConfig = findMatrixAccountConfig(params.cfg, accountId);
127
- const groups = mergeMatrixRoomEntries(
128
- selectInheritedMatrixRoomEntries({
129
- entries: base.groups,
130
- accountId,
131
- }),
132
- accountConfig?.groups,
133
- Boolean(accountConfig && Object.hasOwn(accountConfig, "groups")),
134
- );
135
- const rooms = mergeMatrixRoomEntries(
136
- selectInheritedMatrixRoomEntries({
137
- entries: base.rooms,
138
- accountId,
139
- }),
140
- accountConfig?.rooms,
141
- Boolean(accountConfig && Object.hasOwn(accountConfig, "rooms")),
142
- );
143
- // Room maps need custom scoping, so keep the generic merge for all other fields.
144
- const { groups: _ignoredGroups, rooms: _ignoredRooms, ...rest } = merged;
145
- return {
146
- ...rest,
147
- ...(groups ? { groups } : {}),
148
- ...(rooms ? { rooms } : {}),
149
- };
150
- }
151
-
152
- export function resolveMatrixAccountAllowlistConfig(params: {
153
- cfg: CoreConfig;
154
- accountId?: string | null;
155
- }): {
156
- dmAllowFrom?: NonNullable<MatrixConfig["dm"]>["allowFrom"];
157
- groupAllowFrom?: MatrixConfig["groupAllowFrom"];
158
- } {
159
- const accountId = normalizeAccountId(params.accountId);
160
- const base = resolveMatrixBaseConfig(params.cfg);
161
- const accountConfig = findMatrixAccountConfig(params.cfg, accountId);
162
- const accountDm = accountConfig?.dm;
163
-
164
- let dmAllowFrom = base.dm?.allowFrom;
165
- if (accountDm && Object.hasOwn(accountDm, "allowFrom")) {
166
- dmAllowFrom = accountDm.allowFrom;
167
- }
168
-
169
- let groupAllowFrom = base.groupAllowFrom;
170
- if (accountConfig && Object.hasOwn(accountConfig, "groupAllowFrom")) {
171
- groupAllowFrom = accountConfig.groupAllowFrom;
172
- }
173
-
174
- return { dmAllowFrom, groupAllowFrom };
175
- }
@@ -1,194 +0,0 @@
1
- import { DEFAULT_ACCOUNT_ID, normalizeAccountId } from "klaw/plugin-sdk/account-id";
2
- import { hasConfiguredSecretInput } from "klaw/plugin-sdk/secret-input-runtime";
3
- import { normalizeOptionalString } from "klaw/plugin-sdk/string-coerce-runtime";
4
- import {
5
- resolveConfiguredMatrixAccountIds,
6
- resolveMatrixDefaultOrOnlyAccountId,
7
- } from "../account-selection.js";
8
- import { resolveMatrixAccountStringValues } from "../auth-precedence.js";
9
- import type { CoreConfig, MatrixConfig } from "../types.js";
10
- import {
11
- findMatrixAccountConfig,
12
- resolveMatrixAccountConfig,
13
- resolveMatrixBaseConfig,
14
- } from "./account-config.js";
15
- import { resolveGlobalMatrixEnvConfig, resolveScopedMatrixEnvConfig } from "./client/env-auth.js";
16
- import { credentialsMatchConfig, loadMatrixCredentials } from "./credentials-read.js";
17
-
18
- export type ResolvedMatrixAccount = {
19
- accountId: string;
20
- enabled: boolean;
21
- name?: string;
22
- configured: boolean;
23
- homeserver?: string;
24
- userId?: string;
25
- config: MatrixConfig;
26
- };
27
-
28
- function clean(value: unknown): string {
29
- return normalizeOptionalString(value) ?? "";
30
- }
31
-
32
- function resolveMatrixAccountAuthView(params: {
33
- cfg: CoreConfig;
34
- accountId: string;
35
- env: NodeJS.ProcessEnv;
36
- }): {
37
- homeserver: string;
38
- userId: string;
39
- accessToken?: string;
40
- password?: string;
41
- } {
42
- const normalizedAccountId = normalizeAccountId(params.accountId);
43
- const matrix = resolveMatrixBaseConfig(params.cfg);
44
- const account = findMatrixAccountConfig(params.cfg, normalizedAccountId) ?? {};
45
- const resolvedStrings = resolveMatrixAccountStringValues({
46
- accountId: normalizedAccountId,
47
- account: {
48
- homeserver: clean(account.homeserver),
49
- userId: clean(account.userId),
50
- accessToken: typeof account.accessToken === "string" ? clean(account.accessToken) : "",
51
- password: typeof account.password === "string" ? clean(account.password) : "",
52
- deviceId: clean(account.deviceId),
53
- deviceName: clean(account.deviceName),
54
- },
55
- scopedEnv: resolveScopedMatrixEnvConfig(normalizedAccountId, params.env),
56
- channel: {
57
- homeserver: clean(matrix.homeserver),
58
- userId: clean(matrix.userId),
59
- accessToken: typeof matrix.accessToken === "string" ? clean(matrix.accessToken) : "",
60
- password: typeof matrix.password === "string" ? clean(matrix.password) : "",
61
- deviceId: clean(matrix.deviceId),
62
- deviceName: clean(matrix.deviceName),
63
- },
64
- globalEnv: resolveGlobalMatrixEnvConfig(params.env),
65
- });
66
- return {
67
- homeserver: resolvedStrings.homeserver,
68
- userId: resolvedStrings.userId,
69
- accessToken: resolvedStrings.accessToken || undefined,
70
- password: resolvedStrings.password || undefined,
71
- };
72
- }
73
-
74
- function resolveMatrixAccountUserId(params: {
75
- cfg: CoreConfig;
76
- accountId: string;
77
- env?: NodeJS.ProcessEnv;
78
- }): string | null {
79
- const env = params.env ?? process.env;
80
- const authView = resolveMatrixAccountAuthView({
81
- cfg: params.cfg,
82
- accountId: params.accountId,
83
- env,
84
- });
85
- const configuredUserId = authView.userId.trim();
86
- if (configuredUserId) {
87
- return configuredUserId;
88
- }
89
-
90
- const stored = loadMatrixCredentials(env, params.accountId);
91
- if (!stored) {
92
- return null;
93
- }
94
- if (authView.homeserver && stored.homeserver !== authView.homeserver) {
95
- return null;
96
- }
97
- if (authView.accessToken && stored.accessToken !== authView.accessToken) {
98
- return null;
99
- }
100
- return stored.userId.trim() || null;
101
- }
102
-
103
- export function listMatrixAccountIds(cfg: CoreConfig): string[] {
104
- const ids = resolveConfiguredMatrixAccountIds(cfg, process.env);
105
- return ids.length > 0 ? ids : [DEFAULT_ACCOUNT_ID];
106
- }
107
-
108
- export function resolveDefaultMatrixAccountId(cfg: CoreConfig): string {
109
- return normalizeAccountId(resolveMatrixDefaultOrOnlyAccountId(cfg));
110
- }
111
-
112
- export function resolveConfiguredMatrixBotUserIds(params: {
113
- cfg: CoreConfig;
114
- accountId?: string | null;
115
- env?: NodeJS.ProcessEnv;
116
- }): Set<string> {
117
- const env = params.env ?? process.env;
118
- const currentAccountId = normalizeAccountId(params.accountId);
119
- const accountIds = new Set(resolveConfiguredMatrixAccountIds(params.cfg, env));
120
- if (resolveMatrixAccount({ cfg: params.cfg, accountId: DEFAULT_ACCOUNT_ID, env }).configured) {
121
- accountIds.add(DEFAULT_ACCOUNT_ID);
122
- }
123
- const ids = new Set<string>();
124
-
125
- for (const accountId of accountIds) {
126
- if (normalizeAccountId(accountId) === currentAccountId) {
127
- continue;
128
- }
129
- if (!resolveMatrixAccount({ cfg: params.cfg, accountId, env }).configured) {
130
- continue;
131
- }
132
- const userId = resolveMatrixAccountUserId({
133
- cfg: params.cfg,
134
- accountId,
135
- env,
136
- });
137
- if (userId) {
138
- ids.add(userId);
139
- }
140
- }
141
-
142
- return ids;
143
- }
144
-
145
- export function resolveMatrixAccount(params: {
146
- cfg: CoreConfig;
147
- accountId?: string | null;
148
- env?: NodeJS.ProcessEnv;
149
- }): ResolvedMatrixAccount {
150
- const env = params.env ?? process.env;
151
- const accountId = normalizeAccountId(
152
- params.accountId ?? resolveDefaultMatrixAccountId(params.cfg),
153
- );
154
- const matrixBase = resolveMatrixBaseConfig(params.cfg);
155
- const base = resolveMatrixAccountConfig({ cfg: params.cfg, accountId, env });
156
- const explicitAuthConfig =
157
- accountId === DEFAULT_ACCOUNT_ID
158
- ? base
159
- : (findMatrixAccountConfig(params.cfg, accountId) ?? {});
160
- const enabled = base.enabled !== false && matrixBase.enabled !== false;
161
-
162
- const authView = resolveMatrixAccountAuthView({
163
- cfg: params.cfg,
164
- accountId,
165
- env,
166
- });
167
- const hasHomeserver = Boolean(authView.homeserver);
168
- const hasUserId = Boolean(authView.userId);
169
- const hasAccessToken =
170
- Boolean(authView.accessToken) || hasConfiguredSecretInput(explicitAuthConfig.accessToken);
171
- const hasPassword = Boolean(authView.password);
172
- const hasPasswordAuth =
173
- hasUserId && (hasPassword || hasConfiguredSecretInput(explicitAuthConfig.password));
174
- const stored = loadMatrixCredentials(env, accountId);
175
- const hasStored =
176
- stored && authView.homeserver
177
- ? credentialsMatchConfig(stored, {
178
- homeserver: authView.homeserver,
179
- userId: authView.userId || "",
180
- })
181
- : false;
182
- const configured = hasHomeserver && (hasAccessToken || hasPasswordAuth || hasStored);
183
- return {
184
- accountId,
185
- enabled,
186
- name: normalizeOptionalString(base.name),
187
- configured,
188
- homeserver: authView.homeserver || undefined,
189
- userId: authView.userId || undefined,
190
- config: base,
191
- };
192
- }
193
-
194
- export { resolveMatrixAccountConfig } from "./account-config.js";
@@ -1,31 +0,0 @@
1
- import { withResolvedRuntimeMatrixClient } from "../client-bootstrap.js";
2
- import { resolveMatrixRoomId } from "../send.js";
3
- import type { MatrixActionClient, MatrixActionClientOpts } from "./types.js";
4
-
5
- type MatrixActionClientStopMode = "stop" | "persist" | "discard";
6
-
7
- export async function withResolvedActionClient<T>(
8
- opts: MatrixActionClientOpts,
9
- run: (client: MatrixActionClient["client"]) => Promise<T>,
10
- mode: MatrixActionClientStopMode = "stop",
11
- ): Promise<T> {
12
- return await withResolvedRuntimeMatrixClient(opts, run, mode);
13
- }
14
-
15
- export async function withStartedActionClient<T>(
16
- opts: MatrixActionClientOpts,
17
- run: (client: MatrixActionClient["client"]) => Promise<T>,
18
- ): Promise<T> {
19
- return await withResolvedActionClient({ ...opts, readiness: "started" }, run, "persist");
20
- }
21
-
22
- export async function withResolvedRoomAction<T>(
23
- roomId: string,
24
- opts: MatrixActionClientOpts,
25
- run: (client: MatrixActionClient["client"], resolvedRoom: string) => Promise<T>,
26
- ): Promise<T> {
27
- return await withResolvedActionClient(opts, async (client) => {
28
- const resolvedRoom = await resolveMatrixRoomId(client, roomId);
29
- return await run(client, resolvedRoom);
30
- });
31
- }
@@ -1,34 +0,0 @@
1
- import { summarizeMatrixDeviceHealth } from "../device-health.js";
2
- import { withResolvedActionClient } from "./client.js";
3
- import type { MatrixActionClientOpts } from "./types.js";
4
-
5
- export async function listMatrixOwnDevices(opts: MatrixActionClientOpts = {}) {
6
- return await withResolvedActionClient(opts, async (client) => await client.listOwnDevices());
7
- }
8
-
9
- export async function pruneMatrixStaleGatewayDevices(opts: MatrixActionClientOpts = {}) {
10
- return await withResolvedActionClient(opts, async (client) => {
11
- const devices = await client.listOwnDevices();
12
- const health = summarizeMatrixDeviceHealth(devices);
13
- const staleGatewayDeviceIds = health.staleKlawDevices.map((device) => device.deviceId);
14
- const deleted =
15
- staleGatewayDeviceIds.length > 0
16
- ? await client.deleteOwnDevices(staleGatewayDeviceIds)
17
- : {
18
- currentDeviceId: devices.find((device) => device.current)?.deviceId ?? null,
19
- deletedDeviceIds: [] as string[],
20
- remainingDevices: devices,
21
- };
22
- return {
23
- before: devices,
24
- staleGatewayDeviceIds,
25
- ...deleted,
26
- };
27
- });
28
- }
29
-
30
- export async function getMatrixDeviceHealth(opts: MatrixActionClientOpts = {}) {
31
- return await withResolvedActionClient(opts, async (client) =>
32
- summarizeMatrixDeviceHealth(await client.listOwnDevices()),
33
- );
34
- }
@@ -1,6 +0,0 @@
1
- export function resolveMatrixActionLimit(raw: unknown, fallback: number): number {
2
- if (typeof raw !== "number" || !Number.isFinite(raw)) {
3
- return fallback;
4
- }
5
- return Math.max(1, Math.floor(raw));
6
- }
@@ -1,129 +0,0 @@
1
- import { normalizeOptionalString } from "klaw/plugin-sdk/string-coerce-runtime";
2
- import { fetchMatrixPollMessageSummary, resolveMatrixPollRootEventId } from "../poll-summary.js";
3
- import { isPollEventType } from "../poll-types.js";
4
- import { editMessageMatrix, sendMessageMatrix } from "../send.js";
5
- import { withResolvedRoomAction } from "./client.js";
6
- import { resolveMatrixActionLimit } from "./limits.js";
7
- import { summarizeMatrixRawEvent } from "./summary.js";
8
- import {
9
- EventType,
10
- type MatrixActionClientOpts,
11
- type MatrixMessageSummary,
12
- type MatrixRawEvent,
13
- } from "./types.js";
14
-
15
- export async function sendMatrixMessage(
16
- to: string,
17
- content: string | undefined,
18
- opts: MatrixActionClientOpts & {
19
- mediaUrl?: string;
20
- replyToId?: string;
21
- threadId?: string;
22
- audioAsVoice?: boolean;
23
- } = {},
24
- ) {
25
- if (!opts.cfg) {
26
- throw new Error("Matrix message actions require a resolved runtime config.");
27
- }
28
- return await sendMessageMatrix(to, content, {
29
- cfg: opts.cfg,
30
- mediaUrl: opts.mediaUrl,
31
- mediaLocalRoots: opts.mediaLocalRoots,
32
- replyToId: opts.replyToId,
33
- threadId: opts.threadId,
34
- audioAsVoice: opts.audioAsVoice,
35
- accountId: opts.accountId ?? undefined,
36
- client: opts.client,
37
- timeoutMs: opts.timeoutMs,
38
- });
39
- }
40
-
41
- export async function editMatrixMessage(
42
- roomId: string,
43
- messageId: string,
44
- content: string,
45
- opts: MatrixActionClientOpts = {},
46
- ) {
47
- if (!opts.cfg) {
48
- throw new Error("Matrix message actions require a resolved runtime config.");
49
- }
50
- const trimmed = content.trim();
51
- if (!trimmed) {
52
- throw new Error("Matrix edit requires content");
53
- }
54
- const eventId = await editMessageMatrix(roomId, messageId, trimmed, {
55
- cfg: opts.cfg,
56
- accountId: opts.accountId ?? undefined,
57
- client: opts.client,
58
- timeoutMs: opts.timeoutMs,
59
- });
60
- return { eventId: eventId || null };
61
- }
62
-
63
- export async function deleteMatrixMessage(
64
- roomId: string,
65
- messageId: string,
66
- opts: MatrixActionClientOpts & { reason?: string } = {},
67
- ) {
68
- await withResolvedRoomAction(roomId, opts, async (client, resolvedRoom) => {
69
- await client.redactEvent(resolvedRoom, messageId, opts.reason);
70
- });
71
- }
72
-
73
- export async function readMatrixMessages(
74
- roomId: string,
75
- opts: MatrixActionClientOpts & {
76
- limit?: number;
77
- before?: string;
78
- after?: string;
79
- } = {},
80
- ): Promise<{
81
- messages: MatrixMessageSummary[];
82
- nextBatch?: string | null;
83
- prevBatch?: string | null;
84
- }> {
85
- return await withResolvedRoomAction(roomId, opts, async (client, resolvedRoom) => {
86
- const limit = resolveMatrixActionLimit(opts.limit, 20);
87
- const token = normalizeOptionalString(opts.before) ?? normalizeOptionalString(opts.after);
88
- const dir = opts.after ? "f" : "b";
89
- // Room history is queried via the low-level endpoint for compatibility.
90
- const res = (await client.doRequest(
91
- "GET",
92
- `/_matrix/client/v3/rooms/${encodeURIComponent(resolvedRoom)}/messages`,
93
- {
94
- dir,
95
- limit,
96
- from: token,
97
- },
98
- )) as { chunk: MatrixRawEvent[]; start?: string; end?: string };
99
- const hydratedChunk = await client.hydrateEvents(resolvedRoom, res.chunk);
100
- const seenPollRoots = new Set<string>();
101
- const messages: MatrixMessageSummary[] = [];
102
- for (const event of hydratedChunk) {
103
- if (event.unsigned?.redacted_because) {
104
- continue;
105
- }
106
- if (event.type === EventType.RoomMessage) {
107
- messages.push(summarizeMatrixRawEvent(event));
108
- continue;
109
- }
110
- if (!isPollEventType(event.type)) {
111
- continue;
112
- }
113
- const pollRootId = resolveMatrixPollRootEventId(event);
114
- if (!pollRootId || seenPollRoots.has(pollRootId)) {
115
- continue;
116
- }
117
- seenPollRoots.add(pollRootId);
118
- const pollSummary = await fetchMatrixPollMessageSummary(client, resolvedRoom, event);
119
- if (pollSummary) {
120
- messages.push(pollSummary);
121
- }
122
- }
123
- return {
124
- messages,
125
- nextBatch: res.end ?? null,
126
- prevBatch: res.start ?? null,
127
- };
128
- });
129
- }
@@ -1,63 +0,0 @@
1
- import { withResolvedRoomAction } from "./client.js";
2
- import { fetchEventSummary, readPinnedEvents } from "./summary.js";
3
- import {
4
- EventType,
5
- type MatrixActionClientOpts,
6
- type MatrixMessageSummary,
7
- type RoomPinnedEventsEventContent,
8
- } from "./types.js";
9
-
10
- async function updateMatrixPins(
11
- roomId: string,
12
- opts: MatrixActionClientOpts,
13
- update: (current: string[]) => string[],
14
- ): Promise<{ pinned: string[] }> {
15
- return await withResolvedRoomAction(roomId, opts, async (client, resolvedRoom) => {
16
- const current = await readPinnedEvents(client, resolvedRoom);
17
- const next = update(current);
18
- const payload: RoomPinnedEventsEventContent = { pinned: next };
19
- await client.sendStateEvent(resolvedRoom, EventType.RoomPinnedEvents, "", payload);
20
- return { pinned: next };
21
- });
22
- }
23
-
24
- export async function pinMatrixMessage(
25
- roomId: string,
26
- messageId: string,
27
- opts: MatrixActionClientOpts = {},
28
- ): Promise<{ pinned: string[] }> {
29
- return await updateMatrixPins(roomId, opts, (current) =>
30
- current.includes(messageId) ? current : [...current, messageId],
31
- );
32
- }
33
-
34
- export async function unpinMatrixMessage(
35
- roomId: string,
36
- messageId: string,
37
- opts: MatrixActionClientOpts = {},
38
- ): Promise<{ pinned: string[] }> {
39
- return await updateMatrixPins(roomId, opts, (current) =>
40
- current.filter((id) => id !== messageId),
41
- );
42
- }
43
-
44
- export async function listMatrixPins(
45
- roomId: string,
46
- opts: MatrixActionClientOpts = {},
47
- ): Promise<{ pinned: string[]; events: MatrixMessageSummary[] }> {
48
- return await withResolvedRoomAction(roomId, opts, async (client, resolvedRoom) => {
49
- const pinned = await readPinnedEvents(client, resolvedRoom);
50
- const events = (
51
- await Promise.all(
52
- pinned.map(async (eventId) => {
53
- try {
54
- return await fetchEventSummary(client, resolvedRoom, eventId);
55
- } catch {
56
- return null;
57
- }
58
- }),
59
- )
60
- ).filter((event): event is MatrixMessageSummary => Boolean(event));
61
- return { pinned, events };
62
- });
63
- }