diff --git a/extensions/matrix/index.test.ts b/extensions/matrix/index.test.ts index ecdd6619595..5cc8cd5a8c2 100644 --- a/extensions/matrix/index.test.ts +++ b/extensions/matrix/index.test.ts @@ -1,6 +1,10 @@ import path from "node:path"; import { createJiti } from "jiti"; import { beforeEach, describe, expect, it, vi } from "vitest"; +import { + buildPluginLoaderJitiOptions, + resolvePluginSdkScopedAliasMap, +} from "../../src/plugins/sdk-alias.ts"; const setMatrixRuntimeMock = vi.hoisted(() => vi.fn()); const registerChannelMock = vi.hoisted(() => vi.fn()); @@ -17,12 +21,13 @@ describe("matrix plugin registration", () => { }); it("loads the matrix runtime api through Jiti", () => { - const jiti = createJiti(import.meta.url, { - interopDefault: true, - tryNative: false, - extensions: [".ts", ".tsx", ".mts", ".cts", ".js", ".mjs", ".cjs", ".json"], - }); const runtimeApiPath = path.join(process.cwd(), "extensions", "matrix", "runtime-api.ts"); + const jiti = createJiti(import.meta.url, { + ...buildPluginLoaderJitiOptions( + resolvePluginSdkScopedAliasMap({ modulePath: runtimeApiPath }), + ), + tryNative: false, + }); expect(jiti(runtimeApiPath)).toMatchObject({ requiresExplicitMatrixDefaultAccount: expect.any(Function), diff --git a/extensions/matrix/src/matrix/sdk/crypto-facade.ts b/extensions/matrix/src/matrix/sdk/crypto-facade.ts index f5e85cca26c..5d85539b0a3 100644 --- a/extensions/matrix/src/matrix/sdk/crypto-facade.ts +++ b/extensions/matrix/src/matrix/sdk/crypto-facade.ts @@ -1,4 +1,3 @@ -import { Attachment, EncryptedAttachment } from "@matrix-org/matrix-sdk-crypto-nodejs"; import type { MatrixRecoveryKeyStore } from "./recovery-key-store.js"; import type { EncryptedFile } from "./types.js"; import type { @@ -64,6 +63,15 @@ export type MatrixCryptoFacade = { ) => Promise<{ decimal?: [number, number, number]; emoji?: Array<[string, string]> }>; }; +type MatrixCryptoNodeRuntime = typeof import("./crypto-node.runtime.js"); +let matrixCryptoNodeRuntimePromise: Promise | null = null; + +async function loadMatrixCryptoNodeRuntime(): Promise { + // Keep the native crypto package out of the main CLI startup graph. + matrixCryptoNodeRuntimePromise ??= import("./crypto-node.runtime.js"); + return await matrixCryptoNodeRuntimePromise; +} + export function createMatrixCryptoFacade(deps: { client: MatrixCryptoFacadeClient; verificationManager: MatrixVerificationManager; @@ -110,6 +118,7 @@ export function createMatrixCryptoFacade(deps: { encryptMedia: async ( buffer: Buffer, ): Promise<{ buffer: Buffer; file: Omit }> => { + const { Attachment } = await loadMatrixCryptoNodeRuntime(); const encrypted = Attachment.encrypt(new Uint8Array(buffer)); const mediaInfoJson = encrypted.mediaEncryptionInfo; if (!mediaInfoJson) { @@ -130,6 +139,7 @@ export function createMatrixCryptoFacade(deps: { file: EncryptedFile, opts?: { maxBytes?: number; readIdleTimeoutMs?: number }, ): Promise => { + const { Attachment, EncryptedAttachment } = await loadMatrixCryptoNodeRuntime(); const encrypted = await deps.downloadContent(file.url, opts); const metadata: EncryptedFile = { url: file.url, diff --git a/extensions/matrix/src/matrix/sdk/crypto-node.runtime.ts b/extensions/matrix/src/matrix/sdk/crypto-node.runtime.ts new file mode 100644 index 00000000000..8b3485cc7d0 --- /dev/null +++ b/extensions/matrix/src/matrix/sdk/crypto-node.runtime.ts @@ -0,0 +1,3 @@ +import { Attachment, EncryptedAttachment } from "@matrix-org/matrix-sdk-crypto-nodejs"; + +export { Attachment, EncryptedAttachment }; diff --git a/extensions/matrix/src/matrix/sdk/logger.ts b/extensions/matrix/src/matrix/sdk/logger.ts index 61c8c1fcfdb..758b0c1e85e 100644 --- a/extensions/matrix/src/matrix/sdk/logger.ts +++ b/extensions/matrix/src/matrix/sdk/logger.ts @@ -1,5 +1,6 @@ import { format } from "node:util"; -import { redactSensitiveText, type RuntimeLogger } from "../../runtime-api.js"; +import { redactSensitiveText } from "openclaw/plugin-sdk/diagnostics-otel"; +import type { RuntimeLogger } from "openclaw/plugin-sdk/plugin-runtime"; import { getMatrixRuntime } from "../../runtime.js"; export type Logger = { diff --git a/src/plugin-sdk/plugin-runtime.ts b/src/plugin-sdk/plugin-runtime.ts index 7286beae159..8066d30212b 100644 --- a/src/plugin-sdk/plugin-runtime.ts +++ b/src/plugin-sdk/plugin-runtime.ts @@ -6,3 +6,4 @@ export * from "../plugins/http-path.js"; export * from "../plugins/http-registry.js"; export * from "../plugins/interactive.js"; export * from "../plugins/types.js"; +export type { RuntimeLogger } from "../plugins/runtime/types.js";