@archipelagolab/lobi 1.0.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 +164 -0
- package/ENDOFFILE +0 -0
- package/EOF +0 -0
- package/LICENSE +21 -0
- package/SPEC-SUPPORT.md +116 -0
- package/YAMLEND +0 -0
- package/api.ts +18 -0
- package/archipelagolab-lobi-1.0.0.tgz +0 -0
- package/auth-presence.ts +56 -0
- package/channel-plugin-api.ts +3 -0
- package/cli-metadata.ts +11 -0
- package/contract-api.ts +17 -0
- package/docs/CHECKLIST.md +83 -0
- package/docs/FORK_SDK_GUIDE.md +279 -0
- package/helper-api.ts +3 -0
- package/index.test.ts +61 -0
- package/index.ts +65 -0
- package/openclaw.plugin.json +23 -0
- package/package.json +52 -0
- package/plugin-entry.handlers.runtime.ts +1 -0
- package/runtime-api.ts +54 -0
- package/runtime-heavy-api.ts +1 -0
- package/scripts/migrate-to-lobi.sh +72 -0
- package/secret-contract-api.ts +5 -0
- package/setup-entry.ts +13 -0
- package/src/account-selection.test.ts +124 -0
- package/src/account-selection.ts +226 -0
- package/src/actions.account-propagation.test.ts +251 -0
- package/src/actions.test.ts +251 -0
- package/src/actions.ts +336 -0
- package/src/approval-auth.test.ts +23 -0
- package/src/approval-auth.ts +25 -0
- package/src/approval-handler.runtime.test.ts +46 -0
- package/src/approval-handler.runtime.ts +400 -0
- package/src/approval-ids.ts +6 -0
- package/src/approval-native.test.ts +329 -0
- package/src/approval-native.ts +336 -0
- package/src/approval-reactions.test.ts +107 -0
- package/src/approval-reactions.ts +158 -0
- package/src/auth-precedence.ts +61 -0
- package/src/channel-account-paths.ts +92 -0
- package/src/channel.account-paths.test.ts +102 -0
- package/src/channel.directory.test.ts +601 -0
- package/src/channel.resolve.test.ts +38 -0
- package/src/channel.runtime.ts +16 -0
- package/src/channel.setup.test.ts +269 -0
- package/src/channel.ts +570 -0
- package/src/cli-metadata.ts +19 -0
- package/src/cli.test.ts +1015 -0
- package/src/cli.ts +1198 -0
- package/src/config-adapter.ts +41 -0
- package/src/config-schema.test.ts +90 -0
- package/src/config-schema.ts +114 -0
- package/src/directory-live.test.ts +200 -0
- package/src/directory-live.ts +238 -0
- package/src/doctor-contract.ts +287 -0
- package/src/doctor.test.ts +440 -0
- package/src/doctor.ts +262 -0
- package/src/env-vars.ts +92 -0
- package/src/exec-approval-resolver.test.ts +68 -0
- package/src/exec-approval-resolver.ts +23 -0
- package/src/exec-approvals.test.ts +483 -0
- package/src/exec-approvals.ts +290 -0
- package/src/group-mentions.ts +41 -0
- package/src/legacy-crypto-inspector-availability.test.ts +81 -0
- package/src/legacy-crypto-inspector-availability.ts +60 -0
- package/src/legacy-crypto.test.ts +234 -0
- package/src/legacy-crypto.ts +549 -0
- package/src/legacy-state.test.ts +86 -0
- package/src/legacy-state.ts +156 -0
- package/src/matrix/account-config.ts +150 -0
- package/src/matrix/accounts.readiness.test.ts +27 -0
- package/src/matrix/accounts.test.ts +757 -0
- package/src/matrix/accounts.ts +194 -0
- package/src/matrix/actions/client.test.ts +215 -0
- package/src/matrix/actions/client.ts +31 -0
- package/src/matrix/actions/devices.test.ts +114 -0
- package/src/matrix/actions/devices.ts +34 -0
- package/src/matrix/actions/limits.test.ts +15 -0
- package/src/matrix/actions/limits.ts +6 -0
- package/src/matrix/actions/messages.test.ts +289 -0
- package/src/matrix/actions/messages.ts +123 -0
- package/src/matrix/actions/pins.test.ts +74 -0
- package/src/matrix/actions/pins.ts +64 -0
- package/src/matrix/actions/polls.test.ts +71 -0
- package/src/matrix/actions/polls.ts +109 -0
- package/src/matrix/actions/profile.test.ts +109 -0
- package/src/matrix/actions/profile.ts +37 -0
- package/src/matrix/actions/reactions.test.ts +135 -0
- package/src/matrix/actions/reactions.ts +59 -0
- package/src/matrix/actions/room.test.ts +79 -0
- package/src/matrix/actions/room.ts +71 -0
- package/src/matrix/actions/summary.test.ts +87 -0
- package/src/matrix/actions/summary.ts +88 -0
- package/src/matrix/actions/types.ts +82 -0
- package/src/matrix/actions/verification.test.ts +105 -0
- package/src/matrix/actions/verification.ts +237 -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 +115 -0
- package/src/matrix/client/config-runtime-api.ts +14 -0
- package/src/matrix/client/config-secret-input.runtime.ts +1 -0
- package/src/matrix/client/config.ts +982 -0
- package/src/matrix/client/create-client.test.ts +115 -0
- package/src/matrix/client/create-client.ts +101 -0
- package/src/matrix/client/env-auth.ts +6 -0
- package/src/matrix/client/file-sync-store.test.ts +265 -0
- package/src/matrix/client/file-sync-store.ts +289 -0
- package/src/matrix/client/logging.ts +123 -0
- package/src/matrix/client/migration-snapshot.runtime.ts +1 -0
- package/src/matrix/client/private-network-host.ts +56 -0
- package/src/matrix/client/runtime.ts +4 -0
- package/src/matrix/client/shared.test.ts +344 -0
- package/src/matrix/client/shared.ts +306 -0
- package/src/matrix/client/storage.test.ts +634 -0
- package/src/matrix/client/storage.ts +544 -0
- package/src/matrix/client/types.ts +50 -0
- package/src/matrix/client-bootstrap.test.ts +84 -0
- package/src/matrix/client-bootstrap.ts +164 -0
- package/src/matrix/client-resolver.test-helpers.ts +147 -0
- package/src/matrix/client.test.ts +1521 -0
- package/src/matrix/client.ts +23 -0
- package/src/matrix/config-paths.ts +31 -0
- package/src/matrix/config-update.test.ts +237 -0
- package/src/matrix/config-update.ts +291 -0
- package/src/matrix/credentials-read.ts +206 -0
- package/src/matrix/credentials-write.runtime.ts +26 -0
- package/src/matrix/credentials.test.ts +501 -0
- package/src/matrix/credentials.ts +95 -0
- package/src/matrix/deps.test.ts +74 -0
- package/src/matrix/deps.ts +225 -0
- package/src/matrix/device-health.test.ts +45 -0
- package/src/matrix/device-health.ts +31 -0
- package/src/matrix/direct-management.test.ts +350 -0
- package/src/matrix/direct-management.ts +347 -0
- package/src/matrix/direct-room.test.ts +61 -0
- package/src/matrix/direct-room.ts +128 -0
- package/src/matrix/draft-stream.test.ts +406 -0
- package/src/matrix/draft-stream.ts +216 -0
- package/src/matrix/encryption-guidance.ts +27 -0
- package/src/matrix/errors.ts +21 -0
- package/src/matrix/format.test.ts +340 -0
- package/src/matrix/format.ts +428 -0
- package/src/matrix/legacy-crypto-inspector.ts +95 -0
- package/src/matrix/media-errors.ts +20 -0
- package/src/matrix/media-text.ts +169 -0
- package/src/matrix/monitor/access-state.test.ts +45 -0
- package/src/matrix/monitor/access-state.ts +77 -0
- package/src/matrix/monitor/ack-config.test.ts +57 -0
- package/src/matrix/monitor/ack-config.ts +26 -0
- package/src/matrix/monitor/allowlist.test.ts +45 -0
- package/src/matrix/monitor/allowlist.ts +94 -0
- package/src/matrix/monitor/auto-join.test.ts +203 -0
- package/src/matrix/monitor/auto-join.ts +86 -0
- package/src/matrix/monitor/config.test.ts +197 -0
- package/src/matrix/monitor/config.ts +303 -0
- package/src/matrix/monitor/context-summary.ts +43 -0
- package/src/matrix/monitor/direct.test.ts +529 -0
- package/src/matrix/monitor/direct.ts +270 -0
- package/src/matrix/monitor/events.test.ts +1524 -0
- package/src/matrix/monitor/events.ts +213 -0
- package/src/matrix/monitor/handler.body-for-agent.test.ts +396 -0
- package/src/matrix/monitor/handler.group-history.test.ts +648 -0
- package/src/matrix/monitor/handler.media-failure.test.ts +267 -0
- package/src/matrix/monitor/handler.test-helpers.ts +308 -0
- package/src/matrix/monitor/handler.test.ts +2952 -0
- package/src/matrix/monitor/handler.thread-root-media.test.ts +82 -0
- package/src/matrix/monitor/handler.ts +1679 -0
- package/src/matrix/monitor/inbound-dedupe.test.ts +146 -0
- package/src/matrix/monitor/inbound-dedupe.ts +267 -0
- package/src/matrix/monitor/index.test.ts +920 -0
- package/src/matrix/monitor/index.ts +434 -0
- package/src/matrix/monitor/legacy-crypto-restore.test.ts +206 -0
- package/src/matrix/monitor/legacy-crypto-restore.ts +139 -0
- package/src/matrix/monitor/location.ts +100 -0
- package/src/matrix/monitor/media.test.ts +159 -0
- package/src/matrix/monitor/media.ts +119 -0
- package/src/matrix/monitor/mentions.test.ts +289 -0
- package/src/matrix/monitor/mentions.ts +177 -0
- package/src/matrix/monitor/reaction-events.test.ts +326 -0
- package/src/matrix/monitor/reaction-events.ts +187 -0
- package/src/matrix/monitor/recent-invite.test.ts +92 -0
- package/src/matrix/monitor/recent-invite.ts +30 -0
- package/src/matrix/monitor/replies.test.ts +265 -0
- package/src/matrix/monitor/replies.ts +136 -0
- package/src/matrix/monitor/reply-context.test.ts +276 -0
- package/src/matrix/monitor/reply-context.ts +92 -0
- package/src/matrix/monitor/room-history.test.ts +258 -0
- package/src/matrix/monitor/room-history.ts +301 -0
- package/src/matrix/monitor/room-info.test.ts +201 -0
- package/src/matrix/monitor/room-info.ts +126 -0
- package/src/matrix/monitor/rooms.test.ts +121 -0
- package/src/matrix/monitor/rooms.ts +52 -0
- package/src/matrix/monitor/route.test.ts +255 -0
- package/src/matrix/monitor/route.ts +178 -0
- package/src/matrix/monitor/runtime-api.ts +31 -0
- package/src/matrix/monitor/startup-verification.test.ts +294 -0
- package/src/matrix/monitor/startup-verification.ts +237 -0
- package/src/matrix/monitor/startup.test.ts +257 -0
- package/src/matrix/monitor/startup.ts +218 -0
- package/src/matrix/monitor/status.ts +111 -0
- package/src/matrix/monitor/sync-lifecycle.test.ts +224 -0
- package/src/matrix/monitor/sync-lifecycle.ts +91 -0
- package/src/matrix/monitor/task-runner.ts +38 -0
- package/src/matrix/monitor/thread-context.test.ts +149 -0
- package/src/matrix/monitor/thread-context.ts +108 -0
- package/src/matrix/monitor/threads.test.ts +68 -0
- package/src/matrix/monitor/threads.ts +85 -0
- package/src/matrix/monitor/types.ts +30 -0
- package/src/matrix/monitor/verification-events.ts +627 -0
- package/src/matrix/monitor/verification-utils.test.ts +47 -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.test.ts +205 -0
- package/src/matrix/poll-types.ts +433 -0
- package/src/matrix/probe.runtime.ts +4 -0
- package/src/matrix/probe.test.ts +154 -0
- package/src/matrix/probe.ts +96 -0
- package/src/matrix/profile.test.ts +154 -0
- package/src/matrix/profile.ts +184 -0
- package/src/matrix/reaction-common.test.ts +96 -0
- package/src/matrix/reaction-common.ts +147 -0
- package/src/matrix/sdk/crypto-bootstrap.test.ts +505 -0
- package/src/matrix/sdk/crypto-bootstrap.ts +341 -0
- package/src/matrix/sdk/crypto-facade.test.ts +197 -0
- package/src/matrix/sdk/crypto-facade.ts +207 -0
- package/src/matrix/sdk/crypto-node.runtime.test.ts +27 -0
- package/src/matrix/sdk/crypto-node.runtime.ts +9 -0
- package/src/matrix/sdk/crypto-runtime.ts +11 -0
- package/src/matrix/sdk/decrypt-bridge.ts +356 -0
- package/src/matrix/sdk/event-helpers.test.ts +60 -0
- package/src/matrix/sdk/event-helpers.ts +71 -0
- package/src/matrix/sdk/http-client.test.ts +134 -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.lock-order.test.ts +108 -0
- package/src/matrix/sdk/idb-persistence.test-helpers.ts +88 -0
- package/src/matrix/sdk/idb-persistence.test.ts +149 -0
- package/src/matrix/sdk/idb-persistence.ts +283 -0
- package/src/matrix/sdk/logger.test.ts +25 -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.test.ts +385 -0
- package/src/matrix/sdk/recovery-key-store.ts +430 -0
- package/src/matrix/sdk/transport.test.ts +161 -0
- package/src/matrix/sdk/transport.ts +344 -0
- package/src/matrix/sdk/types.ts +236 -0
- package/src/matrix/sdk/verification-manager.test.ts +509 -0
- package/src/matrix/sdk/verification-manager.ts +694 -0
- package/src/matrix/sdk/verification-status.ts +23 -0
- package/src/matrix/sdk.test.ts +2568 -0
- package/src/matrix/sdk.ts +1789 -0
- package/src/matrix/send/client.test.ts +174 -0
- package/src/matrix/send/client.ts +90 -0
- package/src/matrix/send/formatting.ts +189 -0
- package/src/matrix/send/media.ts +244 -0
- package/src/matrix/send/targets.test.ts +254 -0
- package/src/matrix/send/targets.ts +104 -0
- package/src/matrix/send/types.ts +134 -0
- package/src/matrix/send.test.ts +958 -0
- package/src/matrix/send.ts +609 -0
- package/src/matrix/session-store-metadata.ts +108 -0
- package/src/matrix/startup-abort.ts +44 -0
- package/src/matrix/sync-state.ts +27 -0
- package/src/matrix/target-ids.ts +102 -0
- package/src/matrix/thread-bindings-shared.ts +201 -0
- package/src/matrix/thread-bindings.test.ts +673 -0
- package/src/matrix/thread-bindings.ts +577 -0
- package/src/matrix-migration.runtime.ts +9 -0
- package/src/migration-config.test.ts +228 -0
- package/src/migration-config.ts +243 -0
- package/src/migration-snapshot-backup.ts +117 -0
- package/src/migration-snapshot.test.ts +184 -0
- package/src/migration-snapshot.ts +55 -0
- package/src/onboarding.resolve.test.ts +55 -0
- package/src/onboarding.test-harness.ts +158 -0
- package/src/onboarding.test.ts +665 -0
- package/src/onboarding.ts +773 -0
- package/src/outbound.test.ts +173 -0
- package/src/outbound.ts +78 -0
- package/src/plugin-entry.runtime.js +159 -0
- package/src/plugin-entry.runtime.test.ts +108 -0
- package/src/plugin-entry.runtime.ts +68 -0
- package/src/profile-update.ts +68 -0
- package/src/record-shared.ts +3 -0
- package/src/resolve-targets.test.ts +178 -0
- package/src/resolve-targets.ts +175 -0
- package/src/resolver.ts +21 -0
- package/src/runtime-api.ts +144 -0
- package/src/runtime.ts +7 -0
- package/src/secret-contract.ts +174 -0
- package/src/session-route.test.ts +315 -0
- package/src/session-route.ts +113 -0
- package/src/setup-bootstrap.ts +94 -0
- package/src/setup-config.ts +222 -0
- package/src/setup-contract.ts +89 -0
- package/src/setup-core.test.ts +326 -0
- package/src/setup-core.ts +50 -0
- package/src/setup-surface.ts +4 -0
- package/src/startup-maintenance.test.ts +227 -0
- package/src/startup-maintenance.ts +114 -0
- package/src/storage-paths.ts +92 -0
- package/src/test-helpers.ts +42 -0
- package/src/test-mocks.ts +55 -0
- package/src/test-runtime.ts +72 -0
- package/src/test-support/monitor-route-test-support.ts +8 -0
- package/src/tool-actions.runtime.ts +1 -0
- package/src/tool-actions.test.ts +422 -0
- package/src/tool-actions.ts +498 -0
- package/src/types.ts +230 -0
- package/test-api.ts +2 -0
- package/thread-bindings-runtime.ts +4 -0
- package/tsconfig.json +16 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { DEFAULT_ACCOUNT_ID } from "openclaw/plugin-sdk/account-id";
|
|
2
|
+
import {
|
|
3
|
+
adaptScopedAccountAccessor,
|
|
4
|
+
createScopedChannelConfigAdapter,
|
|
5
|
+
} from "openclaw/plugin-sdk/channel-config-helpers";
|
|
6
|
+
import {
|
|
7
|
+
listMatrixAccountIds,
|
|
8
|
+
resolveDefaultMatrixAccountId,
|
|
9
|
+
resolveMatrixAccount,
|
|
10
|
+
resolveMatrixAccountConfig,
|
|
11
|
+
type ResolvedMatrixAccount,
|
|
12
|
+
} from "./matrix/accounts.js";
|
|
13
|
+
import { normalizeMatrixAllowList } from "./matrix/monitor/allowlist.js";
|
|
14
|
+
|
|
15
|
+
export { DEFAULT_ACCOUNT_ID };
|
|
16
|
+
|
|
17
|
+
export const matrixConfigAdapter = createScopedChannelConfigAdapter<
|
|
18
|
+
ResolvedMatrixAccount,
|
|
19
|
+
ReturnType<typeof resolveMatrixAccountConfig>
|
|
20
|
+
>({
|
|
21
|
+
sectionKey: "matrix",
|
|
22
|
+
listAccountIds: listMatrixAccountIds,
|
|
23
|
+
resolveAccount: adaptScopedAccountAccessor(resolveMatrixAccount),
|
|
24
|
+
resolveAccessorAccount: ({ cfg, accountId }) => resolveMatrixAccountConfig({ cfg, accountId }),
|
|
25
|
+
defaultAccountId: resolveDefaultMatrixAccountId,
|
|
26
|
+
clearBaseFields: [
|
|
27
|
+
"name",
|
|
28
|
+
"homeserver",
|
|
29
|
+
"network",
|
|
30
|
+
"proxy",
|
|
31
|
+
"userId",
|
|
32
|
+
"accessToken",
|
|
33
|
+
"password",
|
|
34
|
+
"deviceId",
|
|
35
|
+
"deviceName",
|
|
36
|
+
"avatarUrl",
|
|
37
|
+
"initialSyncLimit",
|
|
38
|
+
],
|
|
39
|
+
resolveAllowFrom: (account) => account.dm?.allowFrom,
|
|
40
|
+
formatAllowFrom: (allowFrom) => normalizeMatrixAllowList(allowFrom),
|
|
41
|
+
});
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { MatrixConfigSchema } from "./config-schema.js";
|
|
3
|
+
|
|
4
|
+
describe("MatrixConfigSchema SecretInput", () => {
|
|
5
|
+
it("accepts SecretRef accessToken at top-level", () => {
|
|
6
|
+
const result = MatrixConfigSchema.safeParse({
|
|
7
|
+
homeserver: "https://matrix.example.org",
|
|
8
|
+
accessToken: { source: "env", provider: "default", id: "LOBI_ACCESS_TOKEN" },
|
|
9
|
+
});
|
|
10
|
+
expect(result.success).toBe(true);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it("accepts SecretRef password at top-level", () => {
|
|
14
|
+
const result = MatrixConfigSchema.safeParse({
|
|
15
|
+
homeserver: "https://matrix.example.org",
|
|
16
|
+
userId: "@bot:example.org",
|
|
17
|
+
password: { source: "env", provider: "default", id: "LOBI_PASSWORD" },
|
|
18
|
+
});
|
|
19
|
+
expect(result.success).toBe(true);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("accepts dm threadReplies overrides", () => {
|
|
23
|
+
const result = MatrixConfigSchema.safeParse({
|
|
24
|
+
homeserver: "https://matrix.example.org",
|
|
25
|
+
accessToken: "token",
|
|
26
|
+
dm: {
|
|
27
|
+
policy: "pairing",
|
|
28
|
+
threadReplies: "off",
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
expect(result.success).toBe(true);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("accepts dm sessionScope overrides", () => {
|
|
35
|
+
const result = MatrixConfigSchema.safeParse({
|
|
36
|
+
homeserver: "https://matrix.example.org",
|
|
37
|
+
accessToken: "token",
|
|
38
|
+
dm: {
|
|
39
|
+
policy: "pairing",
|
|
40
|
+
sessionScope: "per-room",
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
expect(result.success).toBe(true);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("accepts room-level account assignments", () => {
|
|
47
|
+
const result = MatrixConfigSchema.safeParse({
|
|
48
|
+
homeserver: "https://matrix.example.org",
|
|
49
|
+
accessToken: "token",
|
|
50
|
+
groups: {
|
|
51
|
+
"!room:example.org": {
|
|
52
|
+
enabled: true,
|
|
53
|
+
account: "axis",
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
expect(result.success).toBe(true);
|
|
58
|
+
if (!result.success) {
|
|
59
|
+
throw new Error("expected schema parse to succeed");
|
|
60
|
+
}
|
|
61
|
+
expect(result.data.groups?.["!room:example.org"]?.account).toBe("axis");
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("accepts legacy room-level account assignments", () => {
|
|
65
|
+
const result = MatrixConfigSchema.safeParse({
|
|
66
|
+
homeserver: "https://matrix.example.org",
|
|
67
|
+
accessToken: "token",
|
|
68
|
+
rooms: {
|
|
69
|
+
"!room:example.org": {
|
|
70
|
+
enabled: true,
|
|
71
|
+
account: "axis",
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
expect(result.success).toBe(true);
|
|
76
|
+
if (!result.success) {
|
|
77
|
+
throw new Error("expected schema parse to succeed");
|
|
78
|
+
}
|
|
79
|
+
expect(result.data.rooms?.["!room:example.org"]?.account).toBe("axis");
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("accepts quiet Matrix streaming mode", () => {
|
|
83
|
+
const result = MatrixConfigSchema.safeParse({
|
|
84
|
+
homeserver: "https://matrix.example.org",
|
|
85
|
+
accessToken: "token",
|
|
86
|
+
streaming: "quiet",
|
|
87
|
+
});
|
|
88
|
+
expect(result.success).toBe(true);
|
|
89
|
+
});
|
|
90
|
+
});
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AllowFromListSchema,
|
|
3
|
+
buildNestedDmConfigSchema,
|
|
4
|
+
ContextVisibilityModeSchema,
|
|
5
|
+
GroupPolicySchema,
|
|
6
|
+
MarkdownConfigSchema,
|
|
7
|
+
ToolPolicySchema,
|
|
8
|
+
} from "openclaw/plugin-sdk/channel-config-schema";
|
|
9
|
+
import { buildSecretInputSchema } from "openclaw/plugin-sdk/secret-input";
|
|
10
|
+
import { z } from "openclaw/plugin-sdk/zod";
|
|
11
|
+
|
|
12
|
+
const matrixActionSchema = z
|
|
13
|
+
.object({
|
|
14
|
+
reactions: z.boolean().optional(),
|
|
15
|
+
messages: z.boolean().optional(),
|
|
16
|
+
pins: z.boolean().optional(),
|
|
17
|
+
profile: z.boolean().optional(),
|
|
18
|
+
memberInfo: z.boolean().optional(),
|
|
19
|
+
channelInfo: z.boolean().optional(),
|
|
20
|
+
verification: z.boolean().optional(),
|
|
21
|
+
})
|
|
22
|
+
.optional();
|
|
23
|
+
|
|
24
|
+
const matrixThreadBindingsSchema = z
|
|
25
|
+
.object({
|
|
26
|
+
enabled: z.boolean().optional(),
|
|
27
|
+
idleHours: z.number().nonnegative().optional(),
|
|
28
|
+
maxAgeHours: z.number().nonnegative().optional(),
|
|
29
|
+
spawnSubagentSessions: z.boolean().optional(),
|
|
30
|
+
spawnAcpSessions: z.boolean().optional(),
|
|
31
|
+
})
|
|
32
|
+
.optional();
|
|
33
|
+
|
|
34
|
+
const matrixExecApprovalsSchema = z
|
|
35
|
+
.object({
|
|
36
|
+
enabled: z.boolean().optional(),
|
|
37
|
+
approvers: AllowFromListSchema,
|
|
38
|
+
agentFilter: z.array(z.string()).optional(),
|
|
39
|
+
sessionFilter: z.array(z.string()).optional(),
|
|
40
|
+
target: z.enum(["dm", "channel", "both"]).optional(),
|
|
41
|
+
})
|
|
42
|
+
.optional();
|
|
43
|
+
|
|
44
|
+
const matrixRoomSchema = z
|
|
45
|
+
.object({
|
|
46
|
+
account: z.string().optional(),
|
|
47
|
+
enabled: z.boolean().optional(),
|
|
48
|
+
requireMention: z.boolean().optional(),
|
|
49
|
+
allowBots: z.union([z.boolean(), z.literal("mentions")]).optional(),
|
|
50
|
+
tools: ToolPolicySchema,
|
|
51
|
+
autoReply: z.boolean().optional(),
|
|
52
|
+
users: AllowFromListSchema,
|
|
53
|
+
skills: z.array(z.string()).optional(),
|
|
54
|
+
systemPrompt: z.string().optional(),
|
|
55
|
+
})
|
|
56
|
+
.optional();
|
|
57
|
+
|
|
58
|
+
const matrixNetworkSchema = z
|
|
59
|
+
.object({
|
|
60
|
+
dangerouslyAllowPrivateNetwork: z.boolean().optional(),
|
|
61
|
+
})
|
|
62
|
+
.strict()
|
|
63
|
+
.optional();
|
|
64
|
+
|
|
65
|
+
export const MatrixConfigSchema = z.object({
|
|
66
|
+
name: z.string().optional(),
|
|
67
|
+
enabled: z.boolean().optional(),
|
|
68
|
+
defaultAccount: z.string().optional(),
|
|
69
|
+
accounts: z.record(z.string(), z.unknown()).optional(),
|
|
70
|
+
markdown: MarkdownConfigSchema,
|
|
71
|
+
homeserver: z.string().optional(),
|
|
72
|
+
network: matrixNetworkSchema,
|
|
73
|
+
proxy: z.string().optional(),
|
|
74
|
+
userId: z.string().optional(),
|
|
75
|
+
accessToken: buildSecretInputSchema().optional(),
|
|
76
|
+
password: buildSecretInputSchema().optional(),
|
|
77
|
+
deviceId: z.string().optional(),
|
|
78
|
+
deviceName: z.string().optional(),
|
|
79
|
+
avatarUrl: z.string().optional(),
|
|
80
|
+
initialSyncLimit: z.number().optional(),
|
|
81
|
+
encryption: z.boolean().optional(),
|
|
82
|
+
allowlistOnly: z.boolean().optional(),
|
|
83
|
+
allowBots: z.union([z.boolean(), z.literal("mentions")]).optional(),
|
|
84
|
+
groupPolicy: GroupPolicySchema.optional(),
|
|
85
|
+
contextVisibility: ContextVisibilityModeSchema.optional(),
|
|
86
|
+
blockStreaming: z.boolean().optional(),
|
|
87
|
+
streaming: z.union([z.enum(["partial", "quiet", "off"]), z.boolean()]).optional(),
|
|
88
|
+
replyToMode: z.enum(["off", "first", "all", "batched"]).optional(),
|
|
89
|
+
threadReplies: z.enum(["off", "inbound", "always"]).optional(),
|
|
90
|
+
textChunkLimit: z.number().optional(),
|
|
91
|
+
chunkMode: z.enum(["length", "newline"]).optional(),
|
|
92
|
+
responsePrefix: z.string().optional(),
|
|
93
|
+
ackReaction: z.string().optional(),
|
|
94
|
+
ackReactionScope: z
|
|
95
|
+
.enum(["group-mentions", "group-all", "direct", "all", "none", "off"])
|
|
96
|
+
.optional(),
|
|
97
|
+
reactionNotifications: z.enum(["off", "own"]).optional(),
|
|
98
|
+
threadBindings: matrixThreadBindingsSchema,
|
|
99
|
+
startupVerification: z.enum(["off", "if-unverified"]).optional(),
|
|
100
|
+
startupVerificationCooldownHours: z.number().optional(),
|
|
101
|
+
mediaMaxMb: z.number().optional(),
|
|
102
|
+
historyLimit: z.number().int().min(0).optional(),
|
|
103
|
+
autoJoin: z.enum(["always", "allowlist", "off"]).optional(),
|
|
104
|
+
autoJoinAllowlist: AllowFromListSchema,
|
|
105
|
+
groupAllowFrom: AllowFromListSchema,
|
|
106
|
+
dm: buildNestedDmConfigSchema({
|
|
107
|
+
sessionScope: z.enum(["per-user", "per-room"]).optional(),
|
|
108
|
+
threadReplies: z.enum(["off", "inbound", "always"]).optional(),
|
|
109
|
+
}),
|
|
110
|
+
execApprovals: matrixExecApprovalsSchema,
|
|
111
|
+
groups: z.object({}).catchall(matrixRoomSchema).optional(),
|
|
112
|
+
rooms: z.object({}).catchall(matrixRoomSchema).optional(),
|
|
113
|
+
actions: matrixActionSchema,
|
|
114
|
+
});
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
const { matrixAuthedHttpClientCtorMock, requestJsonMock } = vi.hoisted(() => ({
|
|
4
|
+
matrixAuthedHttpClientCtorMock: vi.fn(),
|
|
5
|
+
requestJsonMock: vi.fn(),
|
|
6
|
+
}));
|
|
7
|
+
|
|
8
|
+
vi.mock("./matrix/client.js", () => ({
|
|
9
|
+
resolveMatrixAuth: vi.fn(),
|
|
10
|
+
}));
|
|
11
|
+
|
|
12
|
+
vi.mock("./matrix/sdk/http-client.js", () => ({
|
|
13
|
+
MatrixAuthedHttpClient: class {
|
|
14
|
+
constructor(params: unknown) {
|
|
15
|
+
matrixAuthedHttpClientCtorMock(params);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
requestJson(params: unknown) {
|
|
19
|
+
return requestJsonMock(params);
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
}));
|
|
23
|
+
|
|
24
|
+
let listMatrixDirectoryGroupsLive: typeof import("./directory-live.js").listMatrixDirectoryGroupsLive;
|
|
25
|
+
let listMatrixDirectoryPeersLive: typeof import("./directory-live.js").listMatrixDirectoryPeersLive;
|
|
26
|
+
let resolveMatrixAuth: typeof import("./matrix/client.js").resolveMatrixAuth;
|
|
27
|
+
|
|
28
|
+
describe("matrix directory live", () => {
|
|
29
|
+
const cfg = { channels: { matrix: {} } };
|
|
30
|
+
|
|
31
|
+
beforeAll(async () => {
|
|
32
|
+
({ listMatrixDirectoryGroupsLive, listMatrixDirectoryPeersLive } =
|
|
33
|
+
await import("./directory-live.js"));
|
|
34
|
+
({ resolveMatrixAuth } = await import("./matrix/client.js"));
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
beforeEach(() => {
|
|
38
|
+
vi.mocked(resolveMatrixAuth).mockReset();
|
|
39
|
+
vi.mocked(resolveMatrixAuth).mockResolvedValue({
|
|
40
|
+
accountId: "assistant",
|
|
41
|
+
homeserver: "https://matrix.example.org",
|
|
42
|
+
userId: "@bot:example.org",
|
|
43
|
+
accessToken: "test-token",
|
|
44
|
+
});
|
|
45
|
+
matrixAuthedHttpClientCtorMock.mockReset();
|
|
46
|
+
requestJsonMock.mockReset();
|
|
47
|
+
requestJsonMock.mockResolvedValue({ results: [] });
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("passes accountId to peer directory auth resolution", async () => {
|
|
51
|
+
await listMatrixDirectoryPeersLive({
|
|
52
|
+
cfg,
|
|
53
|
+
accountId: "assistant",
|
|
54
|
+
query: "alice",
|
|
55
|
+
limit: 10,
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
expect(resolveMatrixAuth).toHaveBeenCalledWith({ cfg, accountId: "assistant" });
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("passes accountId to group directory auth resolution", async () => {
|
|
62
|
+
await listMatrixDirectoryGroupsLive({
|
|
63
|
+
cfg,
|
|
64
|
+
accountId: "assistant",
|
|
65
|
+
query: "channel:#room:example.org",
|
|
66
|
+
limit: 10,
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
expect(resolveMatrixAuth).toHaveBeenCalledWith({ cfg, accountId: "assistant" });
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("passes dispatcherPolicy through to the live directory client", async () => {
|
|
73
|
+
vi.mocked(resolveMatrixAuth).mockResolvedValue({
|
|
74
|
+
accountId: "assistant",
|
|
75
|
+
homeserver: "https://matrix.example.org",
|
|
76
|
+
userId: "@bot:example.org",
|
|
77
|
+
accessToken: "test-token",
|
|
78
|
+
dispatcherPolicy: {
|
|
79
|
+
mode: "explicit-proxy",
|
|
80
|
+
proxyUrl: "http://proxy.internal:8080",
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
await listMatrixDirectoryPeersLive({
|
|
85
|
+
cfg,
|
|
86
|
+
accountId: "assistant",
|
|
87
|
+
query: "alice",
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
expect(matrixAuthedHttpClientCtorMock).toHaveBeenCalledWith({
|
|
91
|
+
homeserver: "https://matrix.example.org",
|
|
92
|
+
accessToken: "test-token",
|
|
93
|
+
ssrfPolicy: undefined,
|
|
94
|
+
dispatcherPolicy: {
|
|
95
|
+
mode: "explicit-proxy",
|
|
96
|
+
proxyUrl: "http://proxy.internal:8080",
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("returns no peer results for empty query without resolving auth", async () => {
|
|
102
|
+
const result = await listMatrixDirectoryPeersLive({
|
|
103
|
+
cfg,
|
|
104
|
+
query: " ",
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
expect(result).toEqual([]);
|
|
108
|
+
expect(resolveMatrixAuth).not.toHaveBeenCalled();
|
|
109
|
+
expect(requestJsonMock).not.toHaveBeenCalled();
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("returns no group results for empty query without resolving auth", async () => {
|
|
113
|
+
const result = await listMatrixDirectoryGroupsLive({
|
|
114
|
+
cfg,
|
|
115
|
+
query: "",
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
expect(result).toEqual([]);
|
|
119
|
+
expect(resolveMatrixAuth).not.toHaveBeenCalled();
|
|
120
|
+
expect(requestJsonMock).not.toHaveBeenCalled();
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("preserves query casing when searching the Matrix user directory", async () => {
|
|
124
|
+
await listMatrixDirectoryPeersLive({
|
|
125
|
+
cfg,
|
|
126
|
+
query: "Alice",
|
|
127
|
+
limit: 3,
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
expect(requestJsonMock).toHaveBeenCalledWith(
|
|
131
|
+
expect.objectContaining({
|
|
132
|
+
method: "POST",
|
|
133
|
+
endpoint: "/_lobi/client/v3/user_directory/search",
|
|
134
|
+
timeoutMs: 10_000,
|
|
135
|
+
body: {
|
|
136
|
+
search_term: "Alice",
|
|
137
|
+
limit: 3,
|
|
138
|
+
},
|
|
139
|
+
}),
|
|
140
|
+
);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it("accepts prefixed fully qualified user ids without hitting Matrix", async () => {
|
|
144
|
+
const results = await listMatrixDirectoryPeersLive({
|
|
145
|
+
cfg,
|
|
146
|
+
query: "matrix:user:@Alice:Example.org",
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
expect(results).toEqual([
|
|
150
|
+
{
|
|
151
|
+
kind: "user",
|
|
152
|
+
id: "@Alice:Example.org",
|
|
153
|
+
},
|
|
154
|
+
]);
|
|
155
|
+
expect(requestJsonMock).not.toHaveBeenCalled();
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it("resolves prefixed room aliases through the hardened Matrix HTTP client", async () => {
|
|
159
|
+
requestJsonMock.mockResolvedValueOnce({
|
|
160
|
+
room_id: "!team:example.org",
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
const results = await listMatrixDirectoryGroupsLive({
|
|
164
|
+
cfg,
|
|
165
|
+
query: "channel:#Team:Example.org",
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
expect(results).toEqual([
|
|
169
|
+
{
|
|
170
|
+
kind: "group",
|
|
171
|
+
id: "!team:example.org",
|
|
172
|
+
name: "#Team:Example.org",
|
|
173
|
+
handle: "#Team:Example.org",
|
|
174
|
+
},
|
|
175
|
+
]);
|
|
176
|
+
expect(requestJsonMock).toHaveBeenCalledWith(
|
|
177
|
+
expect.objectContaining({
|
|
178
|
+
method: "GET",
|
|
179
|
+
endpoint: "/_lobi/client/v3/directory/room/%23Team%3AExample.org",
|
|
180
|
+
timeoutMs: 10_000,
|
|
181
|
+
}),
|
|
182
|
+
);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it("accepts prefixed room ids without additional Matrix lookups", async () => {
|
|
186
|
+
const results = await listMatrixDirectoryGroupsLive({
|
|
187
|
+
cfg,
|
|
188
|
+
query: "matrix:room:!team:example.org",
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
expect(results).toEqual([
|
|
192
|
+
{
|
|
193
|
+
kind: "group",
|
|
194
|
+
id: "!team:example.org",
|
|
195
|
+
name: "!team:example.org",
|
|
196
|
+
},
|
|
197
|
+
]);
|
|
198
|
+
expect(requestJsonMock).not.toHaveBeenCalled();
|
|
199
|
+
});
|
|
200
|
+
});
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import {
|
|
2
|
+
normalizeLowercaseStringOrEmpty,
|
|
3
|
+
normalizeOptionalString,
|
|
4
|
+
} from "openclaw/plugin-sdk/text-runtime";
|
|
5
|
+
import { resolveMatrixAuth } from "./matrix/client.js";
|
|
6
|
+
import { MatrixAuthedHttpClient } from "./matrix/sdk/http-client.js";
|
|
7
|
+
import { isMatrixQualifiedUserId, normalizeMatrixMessagingTarget } from "./matrix/target-ids.js";
|
|
8
|
+
import type { ChannelDirectoryEntry } from "./runtime-api.js";
|
|
9
|
+
|
|
10
|
+
type MatrixUserResult = {
|
|
11
|
+
user_id?: string;
|
|
12
|
+
display_name?: string;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
type MatrixUserDirectoryResponse = {
|
|
16
|
+
results?: MatrixUserResult[];
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
type MatrixJoinedRoomsResponse = {
|
|
20
|
+
joined_rooms?: string[];
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
type MatrixRoomNameState = {
|
|
24
|
+
name?: string;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
type MatrixAliasLookup = {
|
|
28
|
+
room_id?: string;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
type MatrixDirectoryLiveParams = {
|
|
32
|
+
cfg: unknown;
|
|
33
|
+
accountId?: string | null;
|
|
34
|
+
query?: string | null;
|
|
35
|
+
limit?: number | null;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
type MatrixResolvedAuth = Awaited<ReturnType<typeof resolveMatrixAuth>>;
|
|
39
|
+
|
|
40
|
+
const MATRIX_DIRECTORY_TIMEOUT_MS = 10_000;
|
|
41
|
+
|
|
42
|
+
function resolveMatrixDirectoryLimit(limit?: number | null): number {
|
|
43
|
+
return typeof limit === "number" && Number.isFinite(limit) && limit > 0
|
|
44
|
+
? Math.max(1, Math.floor(limit))
|
|
45
|
+
: 20;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function createMatrixDirectoryClient(auth: MatrixResolvedAuth): MatrixAuthedHttpClient {
|
|
49
|
+
return new MatrixAuthedHttpClient({
|
|
50
|
+
homeserver: auth.homeserver,
|
|
51
|
+
accessToken: auth.accessToken,
|
|
52
|
+
ssrfPolicy: auth.ssrfPolicy,
|
|
53
|
+
dispatcherPolicy: auth.dispatcherPolicy,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async function resolveMatrixDirectoryContext(params: MatrixDirectoryLiveParams): Promise<{
|
|
58
|
+
auth: MatrixResolvedAuth;
|
|
59
|
+
client: MatrixAuthedHttpClient;
|
|
60
|
+
query: string;
|
|
61
|
+
queryLower: string;
|
|
62
|
+
} | null> {
|
|
63
|
+
const query = normalizeOptionalString(params.query) ?? "";
|
|
64
|
+
if (!query) {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
const auth = await resolveMatrixAuth({ cfg: params.cfg as never, accountId: params.accountId });
|
|
68
|
+
return {
|
|
69
|
+
auth,
|
|
70
|
+
client: createMatrixDirectoryClient(auth),
|
|
71
|
+
query,
|
|
72
|
+
queryLower: normalizeLowercaseStringOrEmpty(query),
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function createGroupDirectoryEntry(params: {
|
|
77
|
+
id: string;
|
|
78
|
+
name: string;
|
|
79
|
+
handle?: string;
|
|
80
|
+
}): ChannelDirectoryEntry {
|
|
81
|
+
return {
|
|
82
|
+
kind: "group",
|
|
83
|
+
id: params.id,
|
|
84
|
+
name: params.name,
|
|
85
|
+
handle: params.handle,
|
|
86
|
+
} satisfies ChannelDirectoryEntry;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async function requestMatrixJson<T>(
|
|
90
|
+
client: MatrixAuthedHttpClient,
|
|
91
|
+
params: {
|
|
92
|
+
method: "GET" | "POST";
|
|
93
|
+
endpoint: string;
|
|
94
|
+
body?: unknown;
|
|
95
|
+
},
|
|
96
|
+
): Promise<T> {
|
|
97
|
+
return (await client.requestJson({
|
|
98
|
+
method: params.method,
|
|
99
|
+
endpoint: params.endpoint,
|
|
100
|
+
body: params.body,
|
|
101
|
+
timeoutMs: MATRIX_DIRECTORY_TIMEOUT_MS,
|
|
102
|
+
})) as T;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export async function listMatrixDirectoryPeersLive(
|
|
106
|
+
params: MatrixDirectoryLiveParams,
|
|
107
|
+
): Promise<ChannelDirectoryEntry[]> {
|
|
108
|
+
const query = normalizeOptionalString(params.query) ?? "";
|
|
109
|
+
if (!query) {
|
|
110
|
+
return [];
|
|
111
|
+
}
|
|
112
|
+
const directUserId = normalizeMatrixMessagingTarget(query);
|
|
113
|
+
if (directUserId && isMatrixQualifiedUserId(directUserId)) {
|
|
114
|
+
return [{ kind: "user", id: directUserId }];
|
|
115
|
+
}
|
|
116
|
+
const context = await resolveMatrixDirectoryContext({
|
|
117
|
+
...params,
|
|
118
|
+
query,
|
|
119
|
+
});
|
|
120
|
+
if (!context) {
|
|
121
|
+
return [];
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const res = await requestMatrixJson<MatrixUserDirectoryResponse>(context.client, {
|
|
125
|
+
method: "POST",
|
|
126
|
+
endpoint: "/_lobi/client/v3/user_directory/search",
|
|
127
|
+
body: {
|
|
128
|
+
search_term: context.query,
|
|
129
|
+
limit: resolveMatrixDirectoryLimit(params.limit),
|
|
130
|
+
},
|
|
131
|
+
});
|
|
132
|
+
const results = res.results ?? [];
|
|
133
|
+
return results
|
|
134
|
+
.map((entry) => {
|
|
135
|
+
const userId = normalizeOptionalString(entry.user_id);
|
|
136
|
+
if (!userId) {
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
const displayName = normalizeOptionalString(entry.display_name);
|
|
140
|
+
return {
|
|
141
|
+
kind: "user",
|
|
142
|
+
id: userId,
|
|
143
|
+
name: displayName,
|
|
144
|
+
handle: displayName ? `@${displayName}` : undefined,
|
|
145
|
+
raw: entry,
|
|
146
|
+
} satisfies ChannelDirectoryEntry;
|
|
147
|
+
})
|
|
148
|
+
.filter(Boolean) as ChannelDirectoryEntry[];
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
async function resolveMatrixRoomAlias(
|
|
152
|
+
client: MatrixAuthedHttpClient,
|
|
153
|
+
alias: string,
|
|
154
|
+
): Promise<string | null> {
|
|
155
|
+
try {
|
|
156
|
+
const res = await requestMatrixJson<MatrixAliasLookup>(client, {
|
|
157
|
+
method: "GET",
|
|
158
|
+
endpoint: `/_lobi/client/v3/directory/room/${encodeURIComponent(alias)}`,
|
|
159
|
+
});
|
|
160
|
+
return normalizeOptionalString(res.room_id) ?? null;
|
|
161
|
+
} catch {
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
async function fetchMatrixRoomName(
|
|
167
|
+
client: MatrixAuthedHttpClient,
|
|
168
|
+
roomId: string,
|
|
169
|
+
): Promise<string | null> {
|
|
170
|
+
try {
|
|
171
|
+
const res = await requestMatrixJson<MatrixRoomNameState>(client, {
|
|
172
|
+
method: "GET",
|
|
173
|
+
endpoint: `/_lobi/client/v3/rooms/${encodeURIComponent(roomId)}/state/m.room.name`,
|
|
174
|
+
});
|
|
175
|
+
return normalizeOptionalString(res.name) ?? null;
|
|
176
|
+
} catch {
|
|
177
|
+
return null;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export async function listMatrixDirectoryGroupsLive(
|
|
182
|
+
params: MatrixDirectoryLiveParams,
|
|
183
|
+
): Promise<ChannelDirectoryEntry[]> {
|
|
184
|
+
const query = normalizeOptionalString(params.query) ?? "";
|
|
185
|
+
if (!query) {
|
|
186
|
+
return [];
|
|
187
|
+
}
|
|
188
|
+
const directTarget = normalizeMatrixMessagingTarget(query);
|
|
189
|
+
|
|
190
|
+
if (directTarget?.startsWith("!")) {
|
|
191
|
+
return [createGroupDirectoryEntry({ id: directTarget, name: directTarget })];
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const context = await resolveMatrixDirectoryContext({
|
|
195
|
+
...params,
|
|
196
|
+
query,
|
|
197
|
+
});
|
|
198
|
+
if (!context) {
|
|
199
|
+
return [];
|
|
200
|
+
}
|
|
201
|
+
const { client, queryLower } = context;
|
|
202
|
+
const limit = resolveMatrixDirectoryLimit(params.limit);
|
|
203
|
+
|
|
204
|
+
if (directTarget?.startsWith("#")) {
|
|
205
|
+
const roomId = await resolveMatrixRoomAlias(client, directTarget);
|
|
206
|
+
if (!roomId) {
|
|
207
|
+
return [];
|
|
208
|
+
}
|
|
209
|
+
return [createGroupDirectoryEntry({ id: roomId, name: directTarget, handle: directTarget })];
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const joined = await requestMatrixJson<MatrixJoinedRoomsResponse>(client, {
|
|
213
|
+
method: "GET",
|
|
214
|
+
endpoint: "/_lobi/client/v3/joined_rooms",
|
|
215
|
+
});
|
|
216
|
+
const rooms = (joined.joined_rooms ?? [])
|
|
217
|
+
.map((roomId) => normalizeOptionalString(roomId))
|
|
218
|
+
.filter((roomId): roomId is string => Boolean(roomId));
|
|
219
|
+
const results: ChannelDirectoryEntry[] = [];
|
|
220
|
+
|
|
221
|
+
for (const roomId of rooms) {
|
|
222
|
+
const name = await fetchMatrixRoomName(client, roomId);
|
|
223
|
+
if (!name || !normalizeLowercaseStringOrEmpty(name).includes(queryLower)) {
|
|
224
|
+
continue;
|
|
225
|
+
}
|
|
226
|
+
results.push({
|
|
227
|
+
kind: "group",
|
|
228
|
+
id: roomId,
|
|
229
|
+
name,
|
|
230
|
+
handle: `#${name}`,
|
|
231
|
+
});
|
|
232
|
+
if (results.length >= limit) {
|
|
233
|
+
break;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
return results;
|
|
238
|
+
}
|