@elizaos/plugin-matrix-typescript 2.0.0-alpha.1

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/src/index.ts ADDED
@@ -0,0 +1,105 @@
1
+ /**
2
+ * Matrix messaging integration plugin for ElizaOS.
3
+ *
4
+ * This plugin provides Matrix protocol integration using matrix-js-sdk.
5
+ */
6
+
7
+ import type { Plugin, IAgentRuntime } from "@elizaos/core";
8
+ import { logger } from "@elizaos/core";
9
+
10
+ // Service
11
+ export { MatrixService } from "./service.js";
12
+
13
+ // Types
14
+ export * from "./types.js";
15
+
16
+ // Actions
17
+ import { sendMessage } from "./actions/sendMessage.js";
18
+ import { sendReaction } from "./actions/sendReaction.js";
19
+ import { listRooms } from "./actions/listRooms.js";
20
+ import { joinRoom } from "./actions/joinRoom.js";
21
+
22
+ export { sendMessage, sendReaction, listRooms, joinRoom };
23
+
24
+ // Providers
25
+ import { roomStateProvider } from "./providers/roomState.js";
26
+ import { userContextProvider } from "./providers/userContext.js";
27
+
28
+ export { roomStateProvider, userContextProvider };
29
+
30
+ // Import service for plugin
31
+ import { MatrixService } from "./service.js";
32
+
33
+ /**
34
+ * Matrix plugin definition.
35
+ */
36
+ const matrixPlugin: Plugin = {
37
+ name: "matrix",
38
+ description: "Matrix messaging integration plugin for ElizaOS with E2EE support",
39
+
40
+ services: [MatrixService],
41
+
42
+ actions: [sendMessage, sendReaction, listRooms, joinRoom],
43
+
44
+ providers: [roomStateProvider, userContextProvider],
45
+
46
+ tests: [],
47
+
48
+ init: async (
49
+ _config: Record<string, string>,
50
+ runtime: IAgentRuntime
51
+ ): Promise<void> => {
52
+ const homeserver = runtime.getSetting("MATRIX_HOMESERVER");
53
+ const userId = runtime.getSetting("MATRIX_USER_ID");
54
+ const accessToken = runtime.getSetting("MATRIX_ACCESS_TOKEN");
55
+
56
+ logger.info("=".repeat(60));
57
+ logger.info("Matrix Plugin Configuration");
58
+ logger.info("=".repeat(60));
59
+ logger.info(` Homeserver: ${homeserver ? `✓ ${homeserver}` : "✗ Missing (required)"}`);
60
+ logger.info(` User ID: ${userId ? `✓ ${userId}` : "✗ Missing (required)"}`);
61
+ logger.info(` Access Token: ${accessToken ? "✓ Set" : "✗ Missing (required)"}`);
62
+ logger.info("=".repeat(60));
63
+
64
+ // Validate required settings
65
+ const missing: string[] = [];
66
+ if (!homeserver) missing.push("MATRIX_HOMESERVER");
67
+ if (!userId) missing.push("MATRIX_USER_ID");
68
+ if (!accessToken) missing.push("MATRIX_ACCESS_TOKEN");
69
+
70
+ if (missing.length > 0) {
71
+ logger.warn(
72
+ `Matrix plugin: Missing required configuration: ${missing.join(", ")}`
73
+ );
74
+ }
75
+
76
+ // Additional optional settings
77
+ const deviceId = runtime.getSetting("MATRIX_DEVICE_ID");
78
+ const rooms = runtime.getSetting("MATRIX_ROOMS");
79
+ const autoJoin = runtime.getSetting("MATRIX_AUTO_JOIN");
80
+ const encryption = runtime.getSetting("MATRIX_ENCRYPTION");
81
+ const requireMention = runtime.getSetting("MATRIX_REQUIRE_MENTION");
82
+
83
+ if (deviceId) {
84
+ logger.info(` Device ID: ${deviceId}`);
85
+ }
86
+
87
+ if (rooms) {
88
+ logger.info(` Auto-join Rooms: ${rooms}`);
89
+ }
90
+
91
+ if (autoJoin === "true") {
92
+ logger.info(" Auto-join Invites: ✓ Enabled");
93
+ }
94
+
95
+ if (encryption === "true") {
96
+ logger.info(" End-to-End Encryption: ✓ Enabled");
97
+ }
98
+
99
+ if (requireMention === "true") {
100
+ logger.info(" Require Mention: ✓ Enabled (will only respond to mentions in rooms)");
101
+ }
102
+ },
103
+ };
104
+
105
+ export default matrixPlugin;
@@ -0,0 +1,95 @@
1
+ /**
2
+ * Room state provider for Matrix plugin.
3
+ */
4
+
5
+ import type { Provider, ProviderResult, IAgentRuntime, Memory, State } from "@elizaos/core";
6
+ import { MatrixService } from "../service.js";
7
+ import { MATRIX_SERVICE_NAME, getMatrixLocalpart } from "../types.js";
8
+
9
+ /**
10
+ * Provider that gives the agent information about the current Matrix room context.
11
+ */
12
+ export const roomStateProvider: Provider = {
13
+ name: "matrixRoomState",
14
+ description: "Provides information about the current Matrix room context",
15
+
16
+ get: async (
17
+ runtime: IAgentRuntime,
18
+ message: Memory,
19
+ state: State
20
+ ): Promise<ProviderResult> => {
21
+ // Only provide context for Matrix messages
22
+ if (message.content.source !== "matrix") {
23
+ return {
24
+ data: {},
25
+ values: {},
26
+ text: "",
27
+ };
28
+ }
29
+
30
+ const matrixService = runtime.getService(MATRIX_SERVICE_NAME) as MatrixService | undefined;
31
+
32
+ if (!matrixService || !matrixService.isConnected()) {
33
+ return {
34
+ data: { connected: false },
35
+ values: { connected: false },
36
+ text: "",
37
+ };
38
+ }
39
+
40
+ const agentName = state?.agentName || "The agent";
41
+
42
+ // Get room from state if available
43
+ const room = state?.data?.room as Record<string, unknown> | undefined;
44
+ const roomId = room?.roomId as string | undefined;
45
+ const roomName = room?.name as string | undefined;
46
+ const isEncrypted = room?.isEncrypted as boolean | undefined;
47
+ const isDirect = room?.isDirect as boolean | undefined;
48
+ const memberCount = room?.memberCount as number | undefined;
49
+
50
+ const userId = matrixService.getUserId();
51
+ const displayName = getMatrixLocalpart(userId);
52
+
53
+ let responseText = "";
54
+
55
+ if (isDirect) {
56
+ responseText = `${agentName} is in a direct message conversation on Matrix.`;
57
+ } else {
58
+ const roomLabel = roomName || roomId || "a Matrix room";
59
+ responseText = `${agentName} is currently in Matrix room "${roomLabel}".`;
60
+
61
+ if (memberCount) {
62
+ responseText += ` The room has ${memberCount} members.`;
63
+ }
64
+ }
65
+
66
+ if (isEncrypted) {
67
+ responseText += " This room has end-to-end encryption enabled.";
68
+ }
69
+
70
+ responseText += `\n\nMatrix is a decentralized communication protocol. ${agentName} is logged in as ${userId}.`;
71
+
72
+ return {
73
+ data: {
74
+ roomId,
75
+ roomName,
76
+ isEncrypted: isEncrypted || false,
77
+ isDirect: isDirect || false,
78
+ memberCount: memberCount || 0,
79
+ userId,
80
+ displayName,
81
+ homeserver: matrixService.getHomeserver(),
82
+ connected: true,
83
+ },
84
+ values: {
85
+ roomId,
86
+ roomName,
87
+ isEncrypted: isEncrypted || false,
88
+ isDirect: isDirect || false,
89
+ memberCount: memberCount || 0,
90
+ userId,
91
+ },
92
+ text: responseText,
93
+ };
94
+ },
95
+ };
@@ -0,0 +1,79 @@
1
+ /**
2
+ * User context provider for Matrix plugin.
3
+ */
4
+
5
+ import type { Provider, ProviderResult, IAgentRuntime, Memory, State } from "@elizaos/core";
6
+ import { MatrixService } from "../service.js";
7
+ import {
8
+ MATRIX_SERVICE_NAME,
9
+ getMatrixLocalpart,
10
+ getMatrixUserDisplayName,
11
+ type MatrixUserInfo,
12
+ } from "../types.js";
13
+
14
+ /**
15
+ * Provider that gives the agent information about the Matrix user context.
16
+ */
17
+ export const userContextProvider: Provider = {
18
+ name: "matrixUserContext",
19
+ description: "Provides information about the Matrix user in the current conversation",
20
+
21
+ get: async (
22
+ runtime: IAgentRuntime,
23
+ message: Memory,
24
+ state: State
25
+ ): Promise<ProviderResult> => {
26
+ // Only provide context for Matrix messages
27
+ if (message.content.source !== "matrix") {
28
+ return {
29
+ data: {},
30
+ values: {},
31
+ text: "",
32
+ };
33
+ }
34
+
35
+ const matrixService = runtime.getService(MATRIX_SERVICE_NAME) as MatrixService | undefined;
36
+
37
+ if (!matrixService || !matrixService.isConnected()) {
38
+ return {
39
+ data: {},
40
+ values: {},
41
+ text: "",
42
+ };
43
+ }
44
+
45
+ const agentName = state?.agentName || "The agent";
46
+
47
+ // Try to get sender info from message metadata
48
+ const metadata = message.content.metadata as Record<string, unknown> | undefined;
49
+ const senderInfo = metadata?.senderInfo as MatrixUserInfo | undefined;
50
+
51
+ if (!senderInfo) {
52
+ return {
53
+ data: {},
54
+ values: {},
55
+ text: "",
56
+ };
57
+ }
58
+
59
+ const displayName = getMatrixUserDisplayName(senderInfo);
60
+ const localpart = getMatrixLocalpart(senderInfo.userId);
61
+
62
+ const responseText = `${agentName} is talking to ${displayName} (${senderInfo.userId}) on Matrix.`;
63
+
64
+ return {
65
+ data: {
66
+ userId: senderInfo.userId,
67
+ displayName,
68
+ localpart,
69
+ avatarUrl: senderInfo.avatarUrl,
70
+ },
71
+ values: {
72
+ userId: senderInfo.userId,
73
+ displayName,
74
+ localpart,
75
+ },
76
+ text: responseText,
77
+ };
78
+ },
79
+ };