@@ -2,43 +2,60 @@ import { Client, type XmtpEnv } from "@xmtp/node-sdk";
2
2
import OpenAI from "openai" ;
3
3
import { createSigner , getEncryptionKeyFromHex } from "@/helpers" ;
4
4
5
+ /* Get the wallet key associated to the public key of
6
+ * the agent and the encryption key for the local db
7
+ * that stores your agent's messages */
5
8
const { WALLET_KEY , ENCRYPTION_KEY , OPENAI_API_KEY } = process . env ;
6
9
10
+ /* Check if the environment variables are set */
7
11
if ( ! WALLET_KEY ) {
8
12
throw new Error ( "WALLET_KEY must be set" ) ;
9
13
}
10
14
15
+ /* Check if the encryption key is set */
11
16
if ( ! ENCRYPTION_KEY ) {
12
17
throw new Error ( "ENCRYPTION_KEY must be set" ) ;
13
18
}
14
19
20
+ /* Check if the OpenAI API key is set */
15
21
if ( ! OPENAI_API_KEY ) {
16
22
throw new Error ( "OPENAI_API_KEY must be set" ) ;
17
23
}
18
24
25
+ /* Create the signer using viem and parse the encryption key for the local db */
19
26
const signer = createSigner ( WALLET_KEY ) ;
20
27
const encryptionKey = getEncryptionKeyFromHex ( ENCRYPTION_KEY ) ;
28
+
29
+ /* Initialize the OpenAI client */
21
30
const openai = new OpenAI ( { apiKey : OPENAI_API_KEY } ) ;
22
31
32
+ /* Set the environment to dev or production */
23
33
const env : XmtpEnv = "dev" ;
24
34
35
+ /**
36
+ * Main function to run the agent
37
+ */
25
38
async function main ( ) {
26
39
console . log ( `Creating client on the '${ env } ' network...` ) ;
40
+ /* Initialize the xmtp client */
27
41
const client = await Client . create ( signer , encryptionKey , {
28
42
env,
29
43
} ) ;
30
44
31
45
console . log ( "Syncing conversations..." ) ;
46
+ /* Sync the conversations from the network to update the local db */
32
47
await client . conversations . sync ( ) ;
33
48
34
49
console . log (
35
50
`Agent initialized on ${ client . accountAddress } \nSend a message on http://xmtp.chat/dm/${ client . accountAddress } ` ,
36
51
) ;
37
52
38
53
console . log ( "Waiting for messages..." ) ;
54
+ /* Stream all messages from the network */
39
55
const stream = client . conversations . streamAllMessages ( ) ;
40
56
41
57
for await ( const message of await stream ) {
58
+ /* Ignore messages from the same agent or non-text messages */
42
59
if (
43
60
message ?. senderInboxId . toLowerCase ( ) === client . inboxId . toLowerCase ( ) ||
44
61
message ?. contentType ?. typeId !== "text"
@@ -50,25 +67,31 @@ async function main() {
50
67
`Received message: ${ message . content as string } by ${ message . senderInboxId } ` ,
51
68
) ;
52
69
70
+ /* Get the conversation from the local db */
53
71
const conversation = client . conversations . getConversationById (
54
72
message . conversationId ,
55
73
) ;
56
74
75
+ /* If the conversation is not found, skip the message */
57
76
if ( ! conversation ) {
58
77
console . log ( "Unable to find conversation, skipping" ) ;
59
78
continue ;
60
79
}
61
80
62
81
try {
82
+ /* Get the AI response */
63
83
const completion = await openai . chat . completions . create ( {
64
84
messages : [ { role : "user" , content : message . content as string } ] ,
65
85
model : "gpt-3.5-turbo" ,
66
86
} ) ;
67
87
88
+ /* Get the AI response */
68
89
const response =
69
90
completion . choices [ 0 ] ?. message ?. content ||
70
91
"I'm not sure how to respond to that." ;
92
+
71
93
console . log ( `Sending AI response: ${ response } ` ) ;
94
+ /* Send the AI response to the conversation */
72
95
await conversation . send ( response ) ;
73
96
} catch ( error ) {
74
97
console . error ( "Error getting AI response:" , error ) ;
0 commit comments