@@ -11,7 +11,7 @@ import { HumanMessage } from "@langchain/core/messages";
11
11
import { MemorySaver } from "@langchain/langgraph" ;
12
12
import { createReactAgent } from "@langchain/langgraph/prebuilt" ;
13
13
import { ChatOpenAI } from "@langchain/openai" ;
14
- import { StorageProvider } from "./types.js " ;
14
+ import type { StorageProvider } from "./types" ;
15
15
16
16
// Interface for parsed bet information
17
17
export interface ParsedBet {
@@ -22,16 +22,19 @@ export interface ParsedBet {
22
22
23
23
export async function getOrCreateWalletForUser (
24
24
userId : string ,
25
- storage : StorageProvider
25
+ storage : StorageProvider ,
26
26
) {
27
27
const walletDataStr = await storage . getUserWallet ( userId ) ;
28
28
29
29
let walletProvider ;
30
30
31
31
// Configure CDP Wallet Provider
32
32
const config = {
33
- apiKeyName : process . env . CDP_API_KEY_NAME ! ,
34
- apiKeyPrivateKey : process . env . CDP_API_KEY_PRIVATE_KEY ?. replace ( / \\ n / g, "\n" ) ,
33
+ apiKeyName : process . env . CDP_API_KEY_NAME as string ,
34
+ apiKeyPrivateKey : process . env . CDP_API_KEY_PRIVATE_KEY ?. replace (
35
+ / \\ n / g,
36
+ "\n" ,
37
+ ) ,
35
38
cdpWalletData : walletDataStr || undefined ,
36
39
networkId : process . env . NETWORK_ID ,
37
40
} ;
@@ -51,10 +54,13 @@ export async function getOrCreateWalletForUser(
51
54
return { walletProvider, config } ;
52
55
}
53
56
54
- export async function createGameWallet ( storage : StorageProvider ) {
57
+ export async function createGameWallet ( ) {
55
58
const config = {
56
- apiKeyName : process . env . CDP_API_KEY_NAME ! ,
57
- apiKeyPrivateKey : process . env . CDP_API_KEY_PRIVATE_KEY ?. replace ( / \\ n / g, "\n" ) ,
59
+ apiKeyName : process . env . CDP_API_KEY_NAME as string ,
60
+ apiKeyPrivateKey : process . env . CDP_API_KEY_PRIVATE_KEY ?. replace (
61
+ / \\ n / g,
62
+ "\n" ,
63
+ ) ,
58
64
networkId : process . env . NETWORK_ID || "base-mainnet" ,
59
65
} ;
60
66
@@ -68,29 +74,35 @@ export async function createGameWallet(storage: StorageProvider) {
68
74
} ;
69
75
}
70
76
71
- export async function initializeAgent ( userId : string , storage : StorageProvider ) {
77
+ export async function initializeAgent (
78
+ userId : string ,
79
+ storage : StorageProvider ,
80
+ ) {
72
81
try {
73
82
const llm = new ChatOpenAI ( {
74
83
modelName : "gpt-4o-mini" ,
75
84
} ) ;
76
85
77
- const { walletProvider, config : walletConfig } = await getOrCreateWalletForUser (
78
- userId ,
79
- storage
80
- ) ;
86
+ const { walletProvider } = await getOrCreateWalletForUser ( userId , storage ) ;
81
87
82
88
const agentkit = await AgentKit . from ( {
83
89
walletProvider,
84
90
actionProviders : [
85
91
walletActionProvider ( ) ,
86
92
erc20ActionProvider ( ) ,
87
93
cdpApiActionProvider ( {
88
- apiKeyName : process . env . CDP_API_KEY_NAME ! ,
89
- apiKeyPrivateKey : process . env . CDP_API_KEY_PRIVATE_KEY ?. replace ( / \\ n / g, "\n" ) ,
94
+ apiKeyName : process . env . CDP_API_KEY_NAME as string ,
95
+ apiKeyPrivateKey : process . env . CDP_API_KEY_PRIVATE_KEY ?. replace (
96
+ / \\ n / g,
97
+ "\n" ,
98
+ ) ,
90
99
} ) ,
91
100
cdpWalletActionProvider ( {
92
- apiKeyName : process . env . CDP_API_KEY_NAME ! ,
93
- apiKeyPrivateKey : process . env . CDP_API_KEY_PRIVATE_KEY ?. replace ( / \\ n / g, "\n" ) ,
101
+ apiKeyName : process . env . CDP_API_KEY_NAME as string ,
102
+ apiKeyPrivateKey : process . env . CDP_API_KEY_PRIVATE_KEY ?. replace (
103
+ / \\ n / g,
104
+ "\n" ,
105
+ ) ,
94
106
} ) ,
95
107
] ,
96
108
} ) ;
@@ -170,20 +182,20 @@ export async function initializeAgent(userId: string, storage: StorageProvider)
170
182
export async function processMessage (
171
183
agent : ReturnType < typeof createReactAgent > ,
172
184
config : { configurable : { thread_id : string } } ,
173
- message : string
185
+ message : string ,
174
186
) : Promise < string > {
175
187
try {
176
188
const stream = await agent . stream (
177
189
{ messages : [ new HumanMessage ( message ) ] } ,
178
- config
190
+ config ,
179
191
) ;
180
192
181
193
let response = "" ;
182
194
for await ( const chunk of stream ) {
183
195
if ( "agent" in chunk ) {
184
- response += chunk . agent . messages [ 0 ] . content + "\n" ;
196
+ response += ( chunk . agent . messages [ 0 ] . content as string ) + "\n" ;
185
197
} else if ( "tools" in chunk ) {
186
- response += chunk . tools . messages [ 0 ] . content + "\n" ;
198
+ response += ( chunk . tools . messages [ 0 ] . content as string ) + "\n" ;
187
199
}
188
200
}
189
201
@@ -204,22 +216,22 @@ export async function processMessage(
204
216
export async function parseNaturalLanguageBet (
205
217
agent : ReturnType < typeof createReactAgent > ,
206
218
config : { configurable : { thread_id : string } } ,
207
- prompt : string
219
+ prompt : string ,
208
220
) : Promise < ParsedBet > {
209
221
try {
210
222
// Default values in case parsing fails
211
223
const defaultResult : ParsedBet = {
212
224
topic : prompt ,
213
225
options : [ "yes" , "no" ] ,
214
- amount : "0.1"
226
+ amount : "0.1" ,
215
227
} ;
216
228
217
229
if ( ! prompt || prompt . length < 3 ) {
218
230
return defaultResult ;
219
231
}
220
232
221
233
console . log ( `🔄 Parsing natural language bet: "${ prompt } "` ) ;
222
-
234
+
223
235
// Format specific request for parsing
224
236
const parsingRequest = `
225
237
Parse this bet request into structured format: "${ prompt } "
@@ -231,27 +243,30 @@ export async function parseNaturalLanguageBet(
231
243
"amount": "bet amount"
232
244
}
233
245
` ;
234
-
246
+
235
247
// Process with the agent
236
248
const response = await processMessage ( agent , config , parsingRequest ) ;
237
-
249
+
238
250
// Try to extract JSON from the response
239
251
try {
240
252
// Find JSON in the response
241
253
const jsonMatch = response . match ( / \{ [ \s \S ] * \} / ) ;
242
254
if ( jsonMatch ) {
243
255
const parsedJson = JSON . parse ( jsonMatch [ 0 ] ) ;
244
-
256
+
245
257
// Validate and provide defaults if needed
246
258
const result : ParsedBet = {
247
259
topic : parsedJson . topic || prompt ,
248
- options : Array . isArray ( parsedJson . options ) && parsedJson . options . length >= 2
249
- ? [ parsedJson . options [ 0 ] , parsedJson . options [ 1 ] ]
250
- : [ "yes" , "no" ] ,
251
- amount : parsedJson . amount || "0.1"
260
+ options :
261
+ Array . isArray ( parsedJson . options ) && parsedJson . options . length >= 2
262
+ ? [ parsedJson . options [ 0 ] , parsedJson . options [ 1 ] ]
263
+ : [ "yes" , "no" ] ,
264
+ amount : parsedJson . amount || "0.1" ,
252
265
} ;
253
-
254
- console . log ( `✅ Parsed bet: "${ result . topic } " with options [${ result . options . join ( ', ' ) } ] for ${ result . amount } USDC` ) ;
266
+
267
+ console . log (
268
+ `✅ Parsed bet: "${ result . topic } " with options [${ result . options . join ( ", " ) } ] for ${ result . amount } USDC` ,
269
+ ) ;
255
270
return result ;
256
271
}
257
272
} catch ( error ) {
@@ -264,7 +279,7 @@ export async function parseNaturalLanguageBet(
264
279
return {
265
280
topic : prompt ,
266
281
options : [ "yes" , "no" ] ,
267
- amount : "0.1"
282
+ amount : "0.1" ,
268
283
} ;
269
284
}
270
- }
285
+ }
0 commit comments