@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,175 @@
|
|
|
1
|
+
import { normalizeAccountId } from "autobot/plugin-sdk/account-id";
|
|
2
|
+
import { DEFAULT_ACCOUNT_ID } from "autobot/plugin-sdk/account-id";
|
|
3
|
+
import {
|
|
4
|
+
listConfiguredAccountIds,
|
|
5
|
+
resolveMergedAccountConfig,
|
|
6
|
+
resolveNormalizedAccountEntry,
|
|
7
|
+
} from "autobot/plugin-sdk/account-resolution-runtime";
|
|
8
|
+
import { hasConfiguredSecretInput } from "autobot/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
|
+
}
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { DEFAULT_ACCOUNT_ID, normalizeAccountId } from "autobot/plugin-sdk/account-id";
|
|
2
|
+
import { hasConfiguredSecretInput } from "autobot/plugin-sdk/secret-input-runtime";
|
|
3
|
+
import { normalizeOptionalString } from "autobot/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";
|
|
@@ -0,0 +1,31 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
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.staleAutoBotDevices.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
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { normalizeOptionalString } from "autobot/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
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
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
|
+
}
|