@gakr-gakr/matrix 0.1.0
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.
- package/CHANGELOG.md +285 -0
- package/SPEC-SUPPORT.md +116 -0
- package/api.ts +38 -0
- package/auth-presence.ts +56 -0
- package/autobot.plugin.json +28 -0
- package/channel-plugin-api.ts +3 -0
- package/cli-metadata.ts +11 -0
- package/contract-api.ts +17 -0
- package/doctor-contract-api.ts +1 -0
- package/helper-api.ts +3 -0
- package/index.ts +55 -0
- package/package.json +101 -0
- package/plugin-entry.handlers.runtime.ts +1 -0
- package/runtime-api.ts +72 -0
- package/runtime-heavy-api.ts +1 -0
- package/runtime-setter-api.ts +3 -0
- package/secret-contract-api.ts +5 -0
- package/setup-entry.ts +17 -0
- package/setup-plugin-api.ts +3 -0
- package/src/account-selection.ts +223 -0
- package/src/actions.ts +346 -0
- package/src/approval-auth.ts +25 -0
- package/src/approval-handler.runtime.ts +595 -0
- package/src/approval-ids.ts +6 -0
- package/src/approval-native.ts +348 -0
- package/src/approval-reaction-auth.ts +45 -0
- package/src/approval-reactions.ts +313 -0
- package/src/auth-precedence.ts +61 -0
- package/src/channel-account-paths.ts +97 -0
- package/src/channel.runtime.ts +17 -0
- package/src/channel.setup.ts +48 -0
- package/src/channel.ts +667 -0
- package/src/cli-metadata.ts +19 -0
- package/src/cli.ts +2298 -0
- package/src/config-adapter.ts +41 -0
- package/src/config-schema.ts +159 -0
- package/src/config-ui-hints.ts +56 -0
- package/src/directory-live.ts +238 -0
- package/src/doctor-contract.ts +287 -0
- package/src/doctor.ts +262 -0
- package/src/env-vars.ts +92 -0
- package/src/exec-approval-resolver.ts +23 -0
- package/src/exec-approvals.ts +293 -0
- package/src/group-mentions.ts +41 -0
- package/src/legacy-crypto-inspector-availability.ts +60 -0
- package/src/legacy-crypto.ts +531 -0
- package/src/legacy-state.ts +156 -0
- package/src/matrix/account-config.ts +175 -0
- package/src/matrix/accounts.ts +194 -0
- package/src/matrix/actions/client.ts +31 -0
- package/src/matrix/actions/devices.ts +34 -0
- package/src/matrix/actions/limits.ts +6 -0
- package/src/matrix/actions/messages.ts +129 -0
- package/src/matrix/actions/pins.ts +63 -0
- package/src/matrix/actions/polls.ts +109 -0
- package/src/matrix/actions/profile.ts +37 -0
- package/src/matrix/actions/reactions.ts +59 -0
- package/src/matrix/actions/room.ts +71 -0
- package/src/matrix/actions/summary.ts +88 -0
- package/src/matrix/actions/types.ts +63 -0
- package/src/matrix/actions/verification.ts +589 -0
- package/src/matrix/actions.ts +37 -0
- package/src/matrix/active-client.ts +26 -0
- package/src/matrix/async-lock.ts +18 -0
- package/src/matrix/backup-health.ts +124 -0
- package/src/matrix/client/config-runtime-api.ts +9 -0
- package/src/matrix/client/config-secret-input.runtime.ts +1 -0
- package/src/matrix/client/config.ts +853 -0
- package/src/matrix/client/create-client.ts +105 -0
- package/src/matrix/client/env-auth.ts +95 -0
- package/src/matrix/client/file-sync-store.ts +289 -0
- package/src/matrix/client/logging.ts +140 -0
- package/src/matrix/client/migration-snapshot.runtime.ts +1 -0
- package/src/matrix/client/private-network-host.ts +1 -0
- package/src/matrix/client/runtime.ts +4 -0
- package/src/matrix/client/shared.ts +316 -0
- package/src/matrix/client/storage.ts +543 -0
- package/src/matrix/client/types.ts +50 -0
- package/src/matrix/client/url-validation.ts +76 -0
- package/src/matrix/client-bootstrap.ts +173 -0
- package/src/matrix/client.ts +23 -0
- package/src/matrix/config-paths.ts +31 -0
- package/src/matrix/config-update.ts +292 -0
- package/src/matrix/credentials-read.ts +207 -0
- package/src/matrix/credentials-write.runtime.ts +35 -0
- package/src/matrix/credentials.ts +95 -0
- package/src/matrix/deps.ts +309 -0
- package/src/matrix/device-health.ts +31 -0
- package/src/matrix/direct-management.ts +349 -0
- package/src/matrix/direct-room.ts +128 -0
- package/src/matrix/draft-stream.ts +225 -0
- package/src/matrix/encryption-guidance.ts +24 -0
- package/src/matrix/errors.ts +21 -0
- package/src/matrix/format.ts +426 -0
- package/src/matrix/legacy-crypto-inspector.ts +95 -0
- package/src/matrix/media-errors.ts +20 -0
- package/src/matrix/media-text.ts +162 -0
- package/src/matrix/monitor/access-state.ts +145 -0
- package/src/matrix/monitor/ack-config.ts +27 -0
- package/src/matrix/monitor/allowlist.ts +92 -0
- package/src/matrix/monitor/auto-join.ts +86 -0
- package/src/matrix/monitor/config.ts +569 -0
- package/src/matrix/monitor/context-summary.ts +43 -0
- package/src/matrix/monitor/direct.ts +296 -0
- package/src/matrix/monitor/events.ts +397 -0
- package/src/matrix/monitor/handler.ts +2271 -0
- package/src/matrix/monitor/inbound-dedupe.ts +267 -0
- package/src/matrix/monitor/index.ts +540 -0
- package/src/matrix/monitor/legacy-crypto-restore.ts +139 -0
- package/src/matrix/monitor/location.ts +108 -0
- package/src/matrix/monitor/media.ts +119 -0
- package/src/matrix/monitor/mentions.ts +256 -0
- package/src/matrix/monitor/reaction-events.ts +197 -0
- package/src/matrix/monitor/recent-invite.ts +30 -0
- package/src/matrix/monitor/replies.ts +136 -0
- package/src/matrix/monitor/reply-context.ts +92 -0
- package/src/matrix/monitor/room-history.ts +301 -0
- package/src/matrix/monitor/room-info.ts +126 -0
- package/src/matrix/monitor/rooms.ts +52 -0
- package/src/matrix/monitor/route.ts +179 -0
- package/src/matrix/monitor/runtime-api.ts +28 -0
- package/src/matrix/monitor/startup-verification.ts +237 -0
- package/src/matrix/monitor/startup.ts +218 -0
- package/src/matrix/monitor/status.ts +120 -0
- package/src/matrix/monitor/sync-lifecycle.ts +91 -0
- package/src/matrix/monitor/task-runner.ts +38 -0
- package/src/matrix/monitor/test-events.ts +21 -0
- package/src/matrix/monitor/thread-context.ts +108 -0
- package/src/matrix/monitor/threads.ts +85 -0
- package/src/matrix/monitor/types.ts +30 -0
- package/src/matrix/monitor/verification-events.ts +643 -0
- package/src/matrix/monitor/verification-utils.ts +46 -0
- package/src/matrix/outbound-media-runtime.ts +1 -0
- package/src/matrix/poll-summary.ts +110 -0
- package/src/matrix/poll-types.ts +429 -0
- package/src/matrix/probe.runtime.ts +4 -0
- package/src/matrix/probe.ts +97 -0
- package/src/matrix/profile.ts +184 -0
- package/src/matrix/reaction-common.ts +147 -0
- package/src/matrix/sdk/crypto-bootstrap.ts +438 -0
- package/src/matrix/sdk/crypto-facade.ts +242 -0
- package/src/matrix/sdk/crypto-node.runtime.ts +17 -0
- package/src/matrix/sdk/crypto-runtime.ts +14 -0
- package/src/matrix/sdk/decrypt-bridge.ts +410 -0
- package/src/matrix/sdk/event-helpers.ts +83 -0
- package/src/matrix/sdk/http-client.ts +87 -0
- package/src/matrix/sdk/idb-persistence-lock.ts +51 -0
- package/src/matrix/sdk/idb-persistence.ts +286 -0
- package/src/matrix/sdk/logger.ts +108 -0
- package/src/matrix/sdk/read-response-with-limit.ts +19 -0
- package/src/matrix/sdk/recovery-key-store.ts +453 -0
- package/src/matrix/sdk/timeout-abort-signal.ts +1 -0
- package/src/matrix/sdk/transport-runtime-api.ts +18 -0
- package/src/matrix/sdk/transport.ts +352 -0
- package/src/matrix/sdk/types.ts +245 -0
- package/src/matrix/sdk/verification-manager.ts +795 -0
- package/src/matrix/sdk/verification-status.ts +23 -0
- package/src/matrix/sdk.ts +2152 -0
- package/src/matrix/send/client.ts +93 -0
- package/src/matrix/send/formatting.ts +189 -0
- package/src/matrix/send/media.ts +244 -0
- package/src/matrix/send/targets.ts +104 -0
- package/src/matrix/send/types.ts +131 -0
- package/src/matrix/send.ts +660 -0
- package/src/matrix/session-store-metadata.ts +108 -0
- package/src/matrix/startup-abort.ts +44 -0
- package/src/matrix/subagent-hooks.ts +308 -0
- package/src/matrix/sync-state.ts +27 -0
- package/src/matrix/target-ids.ts +79 -0
- package/src/matrix/thread-bindings-shared.ts +206 -0
- package/src/matrix/thread-bindings.ts +580 -0
- package/src/matrix-migration.runtime.ts +9 -0
- package/src/migration-config.ts +243 -0
- package/src/migration-snapshot-backup.ts +116 -0
- package/src/migration-snapshot.ts +53 -0
- package/src/onboarding.ts +775 -0
- package/src/outbound.ts +248 -0
- package/src/plugin-entry.runtime.js +115 -0
- package/src/plugin-entry.runtime.ts +70 -0
- package/src/profile-update.ts +71 -0
- package/src/record-shared.ts +3 -0
- package/src/resolve-targets.ts +175 -0
- package/src/resolver.runtime.ts +5 -0
- package/src/resolver.ts +21 -0
- package/src/runtime-api.ts +106 -0
- package/src/runtime.ts +13 -0
- package/src/secret-contract.ts +174 -0
- package/src/session-route.ts +126 -0
- package/src/setup-bootstrap.ts +102 -0
- package/src/setup-config.ts +222 -0
- package/src/setup-contract.ts +90 -0
- package/src/setup-core.ts +146 -0
- package/src/setup-dm-policy.ts +15 -0
- package/src/setup-surface.ts +4 -0
- package/src/startup-maintenance.ts +114 -0
- package/src/storage-paths.ts +92 -0
- package/src/thread-binding-api.ts +23 -0
- package/src/tool-actions.runtime.ts +1 -0
- package/src/tool-actions.ts +498 -0
- package/src/types.ts +257 -0
- package/subagent-hooks-api.ts +31 -0
- package/test-api.ts +21 -0
- package/thread-binding-api.ts +4 -0
- package/thread-bindings-runtime.ts +4 -0
- package/tsconfig.json +16 -0
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import type { ChannelAccountSnapshot } from "autobot/plugin-sdk/channel-contract";
|
|
2
|
+
import {
|
|
3
|
+
createConnectedChannelStatusPatch,
|
|
4
|
+
createTransportActivityStatusPatch,
|
|
5
|
+
} from "autobot/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
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { resolveThreadSessionKeys } from "autobot/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
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
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
|
+
};
|