@@ -137,120 +137,6 @@ async function main() {
137
137
}
138
138
}
139
139
140
- main ().catch (console .error );
141
- import { Client , type XmtpEnv } from " @xmtp/node-sdk" ;
142
- import { createSigner , getEncryptionKeyFromHex } from " @/helpers" ;
143
-
144
- /* Get the wallet key associated to the public key of
145
- * the agent and the encryption key for the local db
146
- * that stores your agent's messages */
147
- const { WALLET_KEY, ENCRYPTION_KEY, GROK_API_KEY } = process .env ;
148
-
149
- /* Check if the environment variables are set */
150
- if (! WALLET_KEY ) {
151
- throw new Error (" WALLET_KEY must be set" );
152
- }
153
-
154
- /* Check if the encryption key is set */
155
- if (! ENCRYPTION_KEY ) {
156
- throw new Error (" ENCRYPTION_KEY must be set" );
157
- }
158
-
159
- /* Check if the Grok API key is set */
160
- if (! GROK_API_KEY ) {
161
- throw new Error (" GROK_API_KEY must be set" );
162
- }
163
-
164
- /* Create the signer using viem and parse the encryption key for the local db */
165
- const signer = createSigner (WALLET_KEY );
166
- const encryptionKey = getEncryptionKeyFromHex (ENCRYPTION_KEY );
167
-
168
- /* Set the environment to dev or production */
169
- const env: XmtpEnv = " dev" ;
170
-
171
- /**
172
- * Main function to run the agent
173
- */
174
- async function main() {
175
- console .log (` Creating client on the '${env }' network... ` );
176
- /* Initialize the xmtp client */
177
- const client = await Client .create (signer , encryptionKey , {
178
- env ,
179
- });
180
-
181
- console .log (" Syncing conversations..." );
182
- /* Sync the conversations from the network to update the local db */
183
- await client .conversations .sync ();
184
-
185
- console .log (
186
- ` Agent initialized on ${client .accountAddress }\n Send a message on http://xmtp.chat/dm/${client .accountAddress }?env=${env } ` ,
187
- );
188
- console .log (" Waiting for messages..." );
189
- /* Stream all messages from the network */
190
- const stream = client .conversations .streamAllMessages ();
191
-
192
- for await (const message of await stream ) {
193
- /* Ignore messages from the same agent or non-text messages */
194
- if (
195
- message ?.senderInboxId .toLowerCase () === client .inboxId .toLowerCase () ||
196
- message ?.contentType ?.typeId !== " text"
197
- ) {
198
- continue ;
199
- }
200
-
201
- console .log (
202
- ` Received message: ${message .content as string } by ${message .senderInboxId } ` ,
203
- );
204
-
205
- /* Get the conversation from the local db */
206
- const conversation = client .conversations .getConversationById (
207
- message .conversationId ,
208
- );
209
-
210
- /* If the conversation is not found, skip the message */
211
- if (! conversation ) {
212
- console .log (" Unable to find conversation, skipping" );
213
- continue ;
214
- }
215
-
216
- try {
217
- /* Get the AI response from Grok */
218
- const response = await fetch (" https://api.x.ai/v1/chat/completions" , {
219
- method: " POST" ,
220
- headers: {
221
- " Content-Type" : " application/json" ,
222
- Authorization: ` Bearer ${GROK_API_KEY } ` , // Use the same API key variable
223
- },
224
- body: JSON .stringify ({
225
- messages: [
226
- { role: " system" , content: " You are a test assistant." },
227
- { role: " user" , content: message .content as string },
228
- ],
229
- model: " grok-2-latest" ,
230
- stream: false ,
231
- temperature: 0 ,
232
- }),
233
- }).then (
234
- (res ) =>
235
- res .json () as Promise <{
236
- choices: { message: { content: string } }[];
237
- }>,
238
- );
239
- const aiResponse = response .choices [0 ]?.message ?.content || " " ;
240
- console .log (` Sending AI response: ${aiResponse } ` );
241
- /* Send the AI response to the conversation */
242
- await conversation .send (aiResponse );
243
- } catch (error ) {
244
- console .error (" Error getting AI response:" , error );
245
- await conversation .send (
246
- " Sorry, I encountered an error processing your message." ,
247
- );
248
- }
249
-
250
- console .log (" Waiting for messages..." );
251
- }
252
- }
253
-
254
140
main ().catch (console .error );
255
141
```
256
142
0 commit comments