@kodelyth/matrix 2026.5.42 → 2026.6.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 (205) hide show
  1. package/klaw.plugin.json +891 -3
  2. package/package.json +18 -6
  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,120 +0,0 @@
1
- import type { ChannelAccountSnapshot } from "klaw/plugin-sdk/channel-contract";
2
- import {
3
- createConnectedChannelStatusPatch,
4
- createTransportActivityStatusPatch,
5
- } from "klaw/plugin-sdk/gateway-runtime";
6
- import { formatMatrixErrorMessage } from "../errors.js";
7
- import {
8
- isMatrixDisconnectedSyncState,
9
- isMatrixReadySyncState,
10
- type MatrixSyncState,
11
- } from "../sync-state.js";
12
-
13
- type MatrixMonitorStatusSink = (patch: ChannelAccountSnapshot) => void;
14
-
15
- function cloneLastDisconnect(
16
- value: ChannelAccountSnapshot["lastDisconnect"],
17
- ): ChannelAccountSnapshot["lastDisconnect"] {
18
- if (!value || typeof value === "string") {
19
- return value ?? null;
20
- }
21
- return { ...value };
22
- }
23
-
24
- function formatSyncError(error: unknown): string | null {
25
- if (!error) {
26
- return null;
27
- }
28
- if (error instanceof Error) {
29
- return error.message || error.name || "unknown";
30
- }
31
- return formatMatrixErrorMessage(error);
32
- }
33
-
34
- export type MatrixMonitorStatusController = ReturnType<typeof createMatrixMonitorStatusController>;
35
-
36
- export function createMatrixMonitorStatusController(params: {
37
- accountId: string;
38
- baseUrl?: string;
39
- statusSink?: MatrixMonitorStatusSink;
40
- }) {
41
- const status: ChannelAccountSnapshot = {
42
- accountId: params.accountId,
43
- ...(params.baseUrl ? { baseUrl: params.baseUrl } : {}),
44
- connected: false,
45
- lastConnectedAt: null,
46
- lastDisconnect: null,
47
- lastError: null,
48
- healthState: "starting",
49
- };
50
-
51
- const emit = () => {
52
- params.statusSink?.({
53
- ...status,
54
- lastDisconnect: cloneLastDisconnect(status.lastDisconnect),
55
- });
56
- };
57
-
58
- const noteConnected = (at = Date.now(), options?: { transportActivity?: boolean }) => {
59
- if (status.connected === true) {
60
- status.lastEventAt = at;
61
- } else {
62
- Object.assign(status, createConnectedChannelStatusPatch(at));
63
- }
64
- if (options?.transportActivity) {
65
- Object.assign(status, createTransportActivityStatusPatch(at));
66
- }
67
- status.lastError = null;
68
- status.lastDisconnect = null;
69
- status.healthState = "healthy";
70
- emit();
71
- };
72
-
73
- const noteDisconnected = (params: { state: MatrixSyncState; at?: number; error?: unknown }) => {
74
- const at = params.at ?? Date.now();
75
- const error = formatSyncError(params.error);
76
- status.connected = false;
77
- status.lastEventAt = at;
78
- status.lastDisconnect = {
79
- at,
80
- ...(error ? { error } : {}),
81
- };
82
- status.lastError = error;
83
- status.healthState = params.state.toLowerCase();
84
- emit();
85
- };
86
-
87
- emit();
88
-
89
- return {
90
- noteSyncState(state: MatrixSyncState, error?: unknown, at = Date.now()) {
91
- if (isMatrixReadySyncState(state)) {
92
- // matrix-js-sdk emits SYNCING after each successful /sync response.
93
- // PREPARED can be cache-backed and CATCHUP is a lifecycle bridge, so
94
- // neither should refresh transport liveness.
95
- noteConnected(at, { transportActivity: state === "SYNCING" });
96
- return;
97
- }
98
- if (isMatrixDisconnectedSyncState(state)) {
99
- noteDisconnected({ state, at, error });
100
- return;
101
- }
102
- // Unknown future SDK states inherit the current connectivity bit until the
103
- // SDK classifies them as ready or disconnected. Avoid guessing here.
104
- status.lastEventAt = at;
105
- status.healthState = state.toLowerCase();
106
- emit();
107
- },
108
- noteUnexpectedError(error: unknown, at = Date.now()) {
109
- noteDisconnected({ state: "ERROR", at, error });
110
- },
111
- markStopped(at = Date.now()) {
112
- status.connected = false;
113
- status.lastEventAt = at;
114
- if (status.healthState !== "error") {
115
- status.healthState = "stopped";
116
- }
117
- emit();
118
- },
119
- };
120
- }
@@ -1,91 +0,0 @@
1
- import type { MatrixClient } from "../sdk.js";
2
- import { isMatrixTerminalSyncState, type MatrixSyncState } from "../sync-state.js";
3
- import type { MatrixMonitorStatusController } from "./status.js";
4
-
5
- function formatSyncLifecycleError(state: MatrixSyncState, error?: unknown): Error {
6
- if (error instanceof Error) {
7
- return error;
8
- }
9
- const message = typeof error === "string" && error.trim() ? error.trim() : undefined;
10
- if (state === "STOPPED") {
11
- return new Error(message ?? "Matrix sync stopped unexpectedly");
12
- }
13
- if (state === "ERROR") {
14
- return new Error(message ?? "Matrix sync entered ERROR unexpectedly");
15
- }
16
- return new Error(message ?? `Matrix sync entered ${state} unexpectedly`);
17
- }
18
-
19
- export function createMatrixMonitorSyncLifecycle(params: {
20
- client: MatrixClient;
21
- statusController: MatrixMonitorStatusController;
22
- isStopping?: () => boolean;
23
- }) {
24
- let fatalError: Error | null = null;
25
- let resolveFatalWait: (() => void) | null = null;
26
- let rejectFatalWait: ((error: Error) => void) | null = null;
27
-
28
- const settleFatal = (error: Error) => {
29
- if (fatalError) {
30
- return;
31
- }
32
- fatalError = error;
33
- rejectFatalWait?.(error);
34
- resolveFatalWait = null;
35
- rejectFatalWait = null;
36
- };
37
-
38
- const onSyncState = (state: MatrixSyncState, _prevState: string | null, error?: unknown) => {
39
- if (isMatrixTerminalSyncState(state) && !params.isStopping?.()) {
40
- const fatalError = formatSyncLifecycleError(state, error);
41
- params.statusController.noteUnexpectedError(fatalError);
42
- settleFatal(fatalError);
43
- return;
44
- }
45
- // Fatal sync failures are sticky for telemetry; later SDK state churn during
46
- // cleanup or reconnect should not overwrite the first recorded error.
47
- if (fatalError) {
48
- return;
49
- }
50
- // Operator-initiated shutdown can still emit transient sync states before
51
- // the final STOPPED. Ignore that churn so intentional stops do not look
52
- // like runtime failures.
53
- if (params.isStopping?.() && !isMatrixTerminalSyncState(state)) {
54
- return;
55
- }
56
- params.statusController.noteSyncState(state, error);
57
- };
58
-
59
- const onUnexpectedError = (error: Error) => {
60
- if (params.isStopping?.()) {
61
- return;
62
- }
63
- params.statusController.noteUnexpectedError(error);
64
- settleFatal(error);
65
- };
66
-
67
- params.client.on("sync.state", onSyncState);
68
- params.client.on("sync.unexpected_error", onUnexpectedError);
69
-
70
- return {
71
- async waitForFatalStop(): Promise<void> {
72
- if (fatalError) {
73
- throw fatalError;
74
- }
75
- if (resolveFatalWait || rejectFatalWait) {
76
- throw new Error("Matrix fatal-stop wait already in progress");
77
- }
78
- await new Promise<void>((resolve, reject) => {
79
- resolveFatalWait = resolve;
80
- rejectFatalWait = (error) => reject(error);
81
- });
82
- },
83
- dispose() {
84
- resolveFatalWait?.();
85
- resolveFatalWait = null;
86
- rejectFatalWait = null;
87
- params.client.off("sync.state", onSyncState);
88
- params.client.off("sync.unexpected_error", onUnexpectedError);
89
- },
90
- };
91
- }
@@ -1,38 +0,0 @@
1
- import type { RuntimeLogger } from "../../runtime-api.js";
2
-
3
- export function createMatrixMonitorTaskRunner(params: {
4
- logger: RuntimeLogger;
5
- logVerboseMessage: (message: string) => void;
6
- }) {
7
- const inFlight = new Set<Promise<void>>();
8
-
9
- const runDetachedTask = (label: string, task: () => Promise<void>): Promise<void> => {
10
- let trackedTask!: Promise<void>;
11
- trackedTask = Promise.resolve()
12
- .then(task)
13
- .catch((error) => {
14
- const message = String(error);
15
- params.logVerboseMessage(`matrix: ${label} failed (${message})`);
16
- params.logger.warn("matrix background task failed", {
17
- task: label,
18
- error: message,
19
- });
20
- })
21
- .finally(() => {
22
- inFlight.delete(trackedTask);
23
- });
24
- inFlight.add(trackedTask);
25
- return trackedTask;
26
- };
27
-
28
- const waitForIdle = async (): Promise<void> => {
29
- while (inFlight.size > 0) {
30
- await Promise.allSettled(Array.from(inFlight));
31
- }
32
- };
33
-
34
- return {
35
- runDetachedTask,
36
- waitForIdle,
37
- };
38
- }
@@ -1,21 +0,0 @@
1
- import type { MatrixRawEvent } from "./types.js";
2
-
3
- export function createPollStartEvent(eventId: string): MatrixRawEvent {
4
- return {
5
- event_id: eventId,
6
- sender: "@alice:example.org",
7
- type: "m.poll.start",
8
- origin_server_ts: Date.now(),
9
- content: {
10
- "m.poll.start": {
11
- question: { "m.text": "Lunch?" },
12
- kind: "m.poll.disclosed",
13
- max_selections: 1,
14
- answers: [
15
- { id: "a1", "m.text": "Pizza" },
16
- { id: "a2", "m.text": "Sushi" },
17
- ],
18
- },
19
- },
20
- };
21
- }
@@ -1,108 +0,0 @@
1
- import type { MatrixClient } from "../sdk.js";
2
- import { summarizeMatrixMessageContextEvent, trimMatrixMaybeString } from "./context-summary.js";
3
- import type { MatrixRawEvent } from "./types.js";
4
-
5
- const MAX_TRACKED_THREAD_STARTERS = 256;
6
- const MAX_THREAD_STARTER_BODY_LENGTH = 500;
7
-
8
- type MatrixThreadContext = {
9
- threadStarterBody?: string;
10
- senderId?: string;
11
- senderLabel?: string;
12
- summary?: string;
13
- };
14
-
15
- function truncateThreadStarterBody(value: string): string {
16
- if (value.length <= MAX_THREAD_STARTER_BODY_LENGTH) {
17
- return value;
18
- }
19
- return `${value.slice(0, MAX_THREAD_STARTER_BODY_LENGTH - 3)}...`;
20
- }
21
-
22
- export function summarizeMatrixThreadStarterEvent(event: MatrixRawEvent): string | undefined {
23
- const body = summarizeMatrixMessageContextEvent(event);
24
- if (body) {
25
- return truncateThreadStarterBody(body);
26
- }
27
- const content = event.content as { msgtype?: unknown };
28
- const msgtype = trimMatrixMaybeString(content.msgtype);
29
- if (msgtype) {
30
- return `Matrix ${msgtype} message`;
31
- }
32
- const eventType = trimMatrixMaybeString(event.type);
33
- return eventType ? `Matrix ${eventType} event` : undefined;
34
- }
35
-
36
- function formatMatrixThreadStarterBody(params: {
37
- threadRootId: string;
38
- senderName?: string;
39
- senderId?: string;
40
- summary?: string;
41
- }): string {
42
- const senderLabel = params.senderName ?? params.senderId ?? "unknown sender";
43
- const lines = [`Matrix thread root ${params.threadRootId} from ${senderLabel}:`];
44
- if (params.summary) {
45
- lines.push(params.summary);
46
- }
47
- return lines.join("\n");
48
- }
49
-
50
- export function createMatrixThreadContextResolver(params: {
51
- client: MatrixClient;
52
- getMemberDisplayName: (roomId: string, userId: string) => Promise<string>;
53
- logVerboseMessage: (message: string) => void;
54
- }) {
55
- const cache = new Map<string, MatrixThreadContext>();
56
-
57
- const remember = (key: string, value: MatrixThreadContext): MatrixThreadContext => {
58
- cache.set(key, value);
59
- if (cache.size > MAX_TRACKED_THREAD_STARTERS) {
60
- const oldest = cache.keys().next().value;
61
- if (typeof oldest === "string") {
62
- cache.delete(oldest);
63
- }
64
- }
65
- return value;
66
- };
67
-
68
- return async (input: { roomId: string; threadRootId: string }): Promise<MatrixThreadContext> => {
69
- const cacheKey = `${input.roomId}:${input.threadRootId}`;
70
- const cached = cache.get(cacheKey);
71
- if (cached) {
72
- return cached;
73
- }
74
-
75
- const rootEvent = await params.client
76
- .getEvent(input.roomId, input.threadRootId)
77
- .catch((err) => {
78
- params.logVerboseMessage(
79
- `matrix: failed resolving thread root room=${input.roomId} id=${input.threadRootId}: ${String(err)}`,
80
- );
81
- return null;
82
- });
83
- if (!rootEvent) {
84
- return {
85
- threadStarterBody: `Matrix thread root ${input.threadRootId}`,
86
- };
87
- }
88
-
89
- const rawEvent = rootEvent as MatrixRawEvent;
90
- const senderId = trimMatrixMaybeString(rawEvent.sender);
91
- const senderName =
92
- senderId &&
93
- (await params.getMemberDisplayName(input.roomId, senderId).catch(() => undefined));
94
- const senderLabel = senderName ?? senderId;
95
- const summary = summarizeMatrixThreadStarterEvent(rawEvent);
96
- return remember(cacheKey, {
97
- threadStarterBody: formatMatrixThreadStarterBody({
98
- threadRootId: input.threadRootId,
99
- senderId,
100
- senderName,
101
- summary,
102
- }),
103
- senderId,
104
- senderLabel,
105
- summary,
106
- });
107
- };
108
- }
@@ -1,85 +0,0 @@
1
- import { resolveThreadSessionKeys } from "klaw/plugin-sdk/routing";
2
- import type { MatrixRawEvent, RoomMessageEventContent } from "./types.js";
3
- import { RelationType } from "./types.js";
4
-
5
- type MatrixThreadReplies = "off" | "inbound" | "always";
6
-
7
- type MatrixThreadRouting = {
8
- threadId?: string;
9
- };
10
-
11
- export function resolveMatrixThreadSessionKeys(params: {
12
- baseSessionKey: string;
13
- threadId?: string | null;
14
- parentSessionKey?: string;
15
- useSuffix?: boolean;
16
- }): { sessionKey: string; parentSessionKey?: string } {
17
- return resolveThreadSessionKeys({
18
- ...params,
19
- // Matrix event IDs are opaque and case-sensitive; keep the exact thread root.
20
- normalizeThreadId: (threadId) => threadId,
21
- });
22
- }
23
-
24
- function resolveMatrixRelatedReplyToEventId(relates: unknown): string | undefined {
25
- if (!relates || typeof relates !== "object") {
26
- return undefined;
27
- }
28
- if (
29
- "m.in_reply_to" in relates &&
30
- typeof relates["m.in_reply_to"] === "object" &&
31
- relates["m.in_reply_to"] &&
32
- "event_id" in relates["m.in_reply_to"] &&
33
- typeof relates["m.in_reply_to"].event_id === "string"
34
- ) {
35
- return relates["m.in_reply_to"].event_id;
36
- }
37
- return undefined;
38
- }
39
-
40
- export function resolveMatrixThreadRouting(params: {
41
- isDirectMessage: boolean;
42
- threadReplies: MatrixThreadReplies;
43
- dmThreadReplies?: MatrixThreadReplies;
44
- messageId: string;
45
- threadRootId?: string;
46
- }): MatrixThreadRouting {
47
- const effectiveThreadReplies =
48
- params.isDirectMessage && params.dmThreadReplies !== undefined
49
- ? params.dmThreadReplies
50
- : params.threadReplies;
51
- const messageId = params.messageId.trim();
52
- const threadRootId = params.threadRootId?.trim();
53
- const inboundThreadId = threadRootId && threadRootId !== messageId ? threadRootId : undefined;
54
- const threadId =
55
- effectiveThreadReplies === "off"
56
- ? undefined
57
- : effectiveThreadReplies === "inbound"
58
- ? inboundThreadId
59
- : (inboundThreadId ?? (messageId || undefined));
60
-
61
- return {
62
- threadId,
63
- };
64
- }
65
-
66
- export function resolveMatrixThreadRootId(params: {
67
- event: MatrixRawEvent;
68
- content: RoomMessageEventContent;
69
- }): string | undefined {
70
- const relates = params.content["m.relates_to"];
71
- if (!relates || typeof relates !== "object") {
72
- return undefined;
73
- }
74
- if ("rel_type" in relates && relates.rel_type === RelationType.Thread) {
75
- if ("event_id" in relates && typeof relates.event_id === "string") {
76
- return relates.event_id;
77
- }
78
- return resolveMatrixRelatedReplyToEventId(relates);
79
- }
80
- return undefined;
81
- }
82
-
83
- export function resolveMatrixReplyToEventId(content: RoomMessageEventContent): string | undefined {
84
- return resolveMatrixRelatedReplyToEventId(content["m.relates_to"]);
85
- }
@@ -1,30 +0,0 @@
1
- import { MATRIX_REACTION_EVENT_TYPE } from "../reaction-common.js";
2
- import type { EncryptedFile, MessageEventContent } from "../sdk.js";
3
- export type { MatrixRawEvent } from "../sdk.js";
4
-
5
- export const EventType = {
6
- RoomMessage: "m.room.message",
7
- RoomMessageEncrypted: "m.room.encrypted",
8
- RoomMember: "m.room.member",
9
- Location: "m.location",
10
- Reaction: MATRIX_REACTION_EVENT_TYPE,
11
- } as const;
12
-
13
- export const RelationType = {
14
- Replace: "m.replace",
15
- Thread: "m.thread",
16
- } as const;
17
-
18
- export type RoomMessageEventContent = MessageEventContent & {
19
- url?: string;
20
- file?: EncryptedFile;
21
- info?: {
22
- mimetype?: string;
23
- size?: number;
24
- };
25
- "m.relates_to"?: {
26
- rel_type?: string;
27
- event_id?: string;
28
- "m.in_reply_to"?: { event_id?: string };
29
- };
30
- };