Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: XMTP Client #2786

Merged
merged 7 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions agent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
"@elizaos/plugin-dcap": "workspace:*",
"@elizaos/plugin-form": "workspace:*",
"@elizaos/plugin-ankr": "workspace:*",
"@elizaos/client-xmtp": "workspace:*",
"readline": "1.3.0",
"ws": "8.18.0",
"yargs": "17.7.2"
Expand Down
7 changes: 6 additions & 1 deletion agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { MongoDBDatabaseAdapter } from "@elizaos/adapter-mongodb"
import { FarcasterClientInterface } from "@elizaos/client-farcaster"
import { OmniflixPlugin } from "@elizaos/plugin-omniflix"
import { JeeterClientInterface } from "@elizaos/client-simsai"

import { XmtpClientInterface } from "@elizaos/client-xmtp";
import { DirectClient } from "@elizaos/client-direct"
import { agentKitPlugin } from "@elizaos/plugin-agentkit"
import { gelatoPlugin } from "@elizaos/plugin-gelato";
Expand Down Expand Up @@ -627,6 +627,11 @@ export async function initializeClients(character: Character, runtime: IAgentRun
if (autoClient) clients.auto = autoClient
}

if (clientTypes.includes(Clients.XMTP)) {
const xmtpClient = await XmtpClientInterface.start(runtime);
if (xmtpClient) clients.xmtp = xmtpClient;
}

if (clientTypes.includes(Clients.DISCORD)) {
const discordClient = await DiscordClientInterface.start(runtime)
if (discordClient) clients.discord = discordClient
Expand Down
4 changes: 4 additions & 0 deletions docs/docs/packages/agent.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ export async function initializeClients(
if (clientTypes.includes(Clients.DIRECT)) {
clients.push(await AutoClientInterface.start(runtime));
}
if (clientTypes.includes(Clients.XMTP)) {
const xmtpClient = await XmtpClientInterface.start(runtime);
if (xmtpClient) clients.xmtp = xmtpClient;
}

return clients;
}
Expand Down
6 changes: 6 additions & 0 deletions packages/client-xmtp/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*

!dist/**
!package.json
!readme.md
!tsup.config.ts
3 changes: 3 additions & 0 deletions packages/client-xmtp/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import eslintGlobalConfig from "../../eslint.config.mjs";

export default [...eslintGlobalConfig];
19 changes: 19 additions & 0 deletions packages/client-xmtp/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "@elizaos/client-xmtp",
"version": "0.1.8+build.1",
"main": "dist/index.js",
"type": "module",
"types": "dist/index.d.ts",
"dependencies": {
"@elizaos/core": "workspace:*",
"@xmtp/agent-starter": "^0.0.6"
},
"devDependencies": {
"tsup": "8.3.5"
},
"scripts": {
"build": "tsup --format esm --dts",
"dev": "tsup --format esm --dts --watch",
"lint": "eslint --fix --cache ."
}
}
188 changes: 188 additions & 0 deletions packages/client-xmtp/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
import { Message, XMTP, xmtpClient } from "@xmtp/agent-starter";
import {
composeContext,
Content,
elizaLogger,
Memory,
ModelClass,
stringToUuid,
messageCompletionFooter,
generateMessageResponse,
Client,
IAgentRuntime,
} from "@elizaos/core";

let xmtp: XMTP = null;
let elizaRuntime: IAgentRuntime = null;

export const messageHandlerTemplate =
// {{goals}}
`# Action Examples
{{actionExamples}}
(Action examples are for reference only. Do not use the information from them in your response.)

# Knowledge
{{knowledge}}

# Task: Generate dialog and actions for the character {{agentName}}.
About {{agentName}}:
{{bio}}
{{lore}}

{{providers}}

{{attachments}}

# Capabilities
Note that {{agentName}} is capable of reading/seeing/hearing various forms of media, including images, videos, audio, plaintext and PDFs. Recent attachments have been included above under the "Attachments" section.

{{messageDirections}}

{{recentMessages}}

{{actions}}

# Instructions: Write the next message for {{agentName}}.
` + messageCompletionFooter;

export const XmtpClientInterface: Client = {
start: async (runtime: IAgentRuntime) => {
if (!xmtp) {
elizaRuntime = runtime;

xmtp = await xmtpClient({
walletKey: process.env.EVM_PRIVATE_KEY as string,
onMessage,
});

elizaLogger.success("✅ XMTP client started");
elizaLogger.info(`XMTP address: ${xmtp.address}`);
elizaLogger.info(`Talk to me on:`);
elizaLogger.log(
`Converse: https://converse.xyz/dm/${xmtp.address}`
);
elizaLogger.log(
`Coinbase Wallet: https://go.cb-w.com/messaging?address=${xmtp.address}`
);
elizaLogger.log(
`Web or Farcaster Frame: https://client.message-kit.org/?address=${xmtp.address}`
);

return xmtp;
}
return xmtp;
},
stop: async (_runtime: IAgentRuntime) => {
elizaLogger.warn("XMTP client does not support stopping yet");
},
};

const onMessage = async (message: Message) => {
elizaLogger.info(
`Decoded message: ${message.content?.text ?? "no text"} by ${
message.sender.address
}`
);

try {
const text = message?.content?.text ?? "";
const messageId = stringToUuid(message.id as string);
const userId = stringToUuid(message.sender.address as string);
const roomId = stringToUuid(message.group.id as string);
await elizaRuntime.ensureConnection(
userId,
roomId,
message.sender.address,
message.sender.address,
"xmtp"
);

const content: Content = {
text,
source: "xmtp",
inReplyTo: undefined,
};

const userMessage = {
content,
userId,
roomId,
agentId: elizaRuntime.agentId,
};

const memory: Memory = {
id: messageId,
agentId: elizaRuntime.agentId,
userId,
roomId,
content,
createdAt: Date.now(),
};

await elizaRuntime.messageManager.createMemory(memory);

const state = await elizaRuntime.composeState(userMessage, {
agentName: elizaRuntime.character.name,
});

const context = composeContext({
state,
template: messageHandlerTemplate,
});

const response = await generateMessageResponse({
runtime: elizaRuntime,
context,
modelClass: ModelClass.LARGE,
});
const _newMessage = [
{
text: response?.text,
source: "xmtp",
inReplyTo: messageId,
},
];
// save response to memory
const responseMessage = {
...userMessage,
userId: elizaRuntime.agentId,
content: response,
};

await elizaRuntime.messageManager.createMemory(responseMessage);

if (!response) {
elizaLogger.error("No response from generateMessageResponse");
return;
}

await elizaRuntime.evaluate(memory, state);

const _result = await elizaRuntime.processActions(
memory,
[responseMessage],
state,
async (newMessages) => {
if (newMessages.text) {
_newMessage.push({
text: newMessages.text,
source: "xmtp",
inReplyTo: undefined,
});
}
return [memory];
}
);
for (const newMsg of _newMessage) {
await xmtp.send({
message: newMsg.text,
originalMessage: message,
metadata: {},
});
}
} catch (error) {
elizaLogger.error("Error in onMessage", error);
}
};

export default XmtpClientInterface;
10 changes: 10 additions & 0 deletions packages/client-xmtp/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../core/tsconfig.json",
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
},
"include": [
"src/**/*.ts"
]
}
10 changes: 10 additions & 0 deletions packages/client-xmtp/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineConfig } from "tsup";

export default defineConfig({
entry: ["src/index.ts"],
outDir: "dist",
sourcemap: true,
clean: true,
format: ["esm"],
external: ["dotenv", "fs", "path"],
});
3 changes: 2 additions & 1 deletion packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,8 @@ export enum Clients {
SLACK = "slack",
GITHUB = "github",
INSTAGRAM = "instagram",
SIMSAI = "simsai"
SIMSAI = "simsai",
XMTP = "xmtp",
}

export interface IAgentConfig {
Expand Down
Loading