|
| 1 | +import { Client, type XmtpEnv } from "@xmtp/node-sdk"; |
| 2 | +import { createSigner, getEncryptionKeyFromHex } from "@/helpers"; |
| 3 | + |
| 4 | +/* Get the wallet key associated to the public key of |
| 5 | + * the agent and the encryption key for the local db |
| 6 | + * that stores your agent's messages */ |
| 7 | +const { WALLET_KEY, ENCRYPTION_KEY, GROK_API_KEY } = process.env; |
| 8 | + |
| 9 | +/* Check if the environment variables are set */ |
| 10 | +if (!WALLET_KEY) { |
| 11 | + throw new Error("WALLET_KEY must be set"); |
| 12 | +} |
| 13 | + |
| 14 | +/* Check if the encryption key is set */ |
| 15 | +if (!ENCRYPTION_KEY) { |
| 16 | + throw new Error("ENCRYPTION_KEY must be set"); |
| 17 | +} |
| 18 | + |
| 19 | +/* Check if the Grok API key is set */ |
| 20 | +if (!GROK_API_KEY) { |
| 21 | + throw new Error("GROK_API_KEY must be set"); |
| 22 | +} |
| 23 | + |
| 24 | +/* Create the signer using viem and parse the encryption key for the local db */ |
| 25 | +const signer = createSigner(WALLET_KEY); |
| 26 | +const encryptionKey = getEncryptionKeyFromHex(ENCRYPTION_KEY); |
| 27 | + |
| 28 | +/* Set the environment to dev or production */ |
| 29 | +const env: XmtpEnv = "dev"; |
| 30 | + |
| 31 | +/** |
| 32 | + * Main function to run the agent |
| 33 | + */ |
| 34 | +async function main() { |
| 35 | + console.log(`Creating client on the '${env}' network...`); |
| 36 | + /* Initialize the xmtp client */ |
| 37 | + const client = await Client.create(signer, encryptionKey, { |
| 38 | + env, |
| 39 | + }); |
| 40 | + |
| 41 | + console.log("Syncing conversations..."); |
| 42 | + /* Sync the conversations from the network to update the local db */ |
| 43 | + await client.conversations.sync(); |
| 44 | + |
| 45 | + console.log( |
| 46 | + `Agent initialized on ${client.accountAddress}\nSend a message on http://xmtp.chat/dm/${client.accountAddress}?env=${env}`, |
| 47 | + ); |
| 48 | + console.log("Waiting for messages..."); |
| 49 | + /* Stream all messages from the network */ |
| 50 | + const stream = client.conversations.streamAllMessages(); |
| 51 | + |
| 52 | + for await (const message of await stream) { |
| 53 | + /* Ignore messages from the same agent or non-text messages */ |
| 54 | + if ( |
| 55 | + message?.senderInboxId.toLowerCase() === client.inboxId.toLowerCase() || |
| 56 | + message?.contentType?.typeId !== "text" |
| 57 | + ) { |
| 58 | + continue; |
| 59 | + } |
| 60 | + |
| 61 | + console.log( |
| 62 | + `Received message: ${message.content as string} by ${message.senderInboxId}`, |
| 63 | + ); |
| 64 | + |
| 65 | + /* Get the conversation from the local db */ |
| 66 | + const conversation = client.conversations.getConversationById( |
| 67 | + message.conversationId, |
| 68 | + ); |
| 69 | + |
| 70 | + /* If the conversation is not found, skip the message */ |
| 71 | + if (!conversation) { |
| 72 | + console.log("Unable to find conversation, skipping"); |
| 73 | + continue; |
| 74 | + } |
| 75 | + |
| 76 | + try { |
| 77 | + /* Get the AI response from Grok */ |
| 78 | + const response = await fetch("https://api.x.ai/v1/chat/completions", { |
| 79 | + method: "POST", |
| 80 | + headers: { |
| 81 | + "Content-Type": "application/json", |
| 82 | + Authorization: `Bearer ${GROK_API_KEY}`, // Use the same API key variable |
| 83 | + }, |
| 84 | + body: JSON.stringify({ |
| 85 | + messages: [ |
| 86 | + { role: "system", content: "You are a test assistant." }, |
| 87 | + { role: "user", content: message.content as string }, |
| 88 | + ], |
| 89 | + model: "grok-2-latest", |
| 90 | + stream: false, |
| 91 | + temperature: 0, |
| 92 | + }), |
| 93 | + }).then( |
| 94 | + (res) => |
| 95 | + res.json() as Promise<{ |
| 96 | + choices: { message: { content: string } }[]; |
| 97 | + }>, |
| 98 | + ); |
| 99 | + const aiResponse = response.choices[0]?.message?.content || ""; |
| 100 | + console.log(`Sending AI response: ${aiResponse}`); |
| 101 | + /* Send the AI response to the conversation */ |
| 102 | + await conversation.send(aiResponse); |
| 103 | + } catch (error) { |
| 104 | + console.error("Error getting AI response:", error); |
| 105 | + await conversation.send( |
| 106 | + "Sorry, I encountered an error processing your message.", |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + console.log("Waiting for messages..."); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +main().catch(console.error); |
0 commit comments