-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathinvestmentBot.ts
367 lines (316 loc) · 11.6 KB
/
investmentBot.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import { readFileSync, writeFileSync, existsSync, mkdirSync } from "fs";
import path from "path";
import { ChatOpenAI } from "@langchain/openai";
import { HumanMessage, SystemMessage } from "@langchain/core/messages";
import dotenv from "dotenv";
import { agent } from "./bot";
dotenv.config();
export interface UserPreferences {
riskTolerance: "low" | "medium" | "high";
preferredAssets: string[];
preferredChains: string[];
investmentHorizon: "short" | "medium" | "long";
minTVL?: number;
targetAPY?: number;
timestamp?: string;
}
interface Pool {
name: string;
apy: number;
tvl: number;
chain: string;
assets: string[];
riskLevel: "low" | "medium" | "high";
impermanentLoss: number;
protocol: string;
}
export class InvestmentAdvisor {
private static instance: InvestmentAdvisor;
private readonly chatModel: ChatOpenAI;
private pools: Pool[] = [];
private readonly userPreferencesDir: string;
private readonly chatHistoryDir: string;
constructor() {
this.chatModel = agent;
this.userPreferencesDir = path.join(__dirname, "data", "user_preferences");
this.chatHistoryDir = path.join(__dirname, "data", "chat_history");
this.loadPoolData();
this.ensureUserPreferencesDirExists();
this.ensureChatHistoryDirExists();
}
private ensureUserPreferencesDirExists(): void {
if (!existsSync(this.userPreferencesDir)) {
mkdirSync(this.userPreferencesDir, { recursive: true });
}
}
private ensureChatHistoryDirExists(): void {
if (!existsSync(this.chatHistoryDir)) {
mkdirSync(this.chatHistoryDir, { recursive: true });
}
}
public static getInstance(): InvestmentAdvisor {
if (!InvestmentAdvisor.instance) {
InvestmentAdvisor.instance = new InvestmentAdvisor();
}
return InvestmentAdvisor.instance;
}
private async detectIntent(message: string): Promise<{
intent: "set_preferences" | "get_recommendations" | "other";
confidence: number;
}> {
const response = await this.chatModel.invoke([
new SystemMessage(`You are a DeFi investment assistant. Analyze the message and determine if the user is:
1. Setting investment preferences mentioning risk levels, assets, chains, or investment criteria
2. Requesting recommendations asking about available pools or opportunities
3. Something else
Return JSON with format:
{
"intent": "set_preferences|get_recommendations|other",
"confidence": number between 0 and 1
}
Example messages that indicate setting preferences:
- "I am looking for low-risk investments in ETH and USDC"
- "I want to invest in Ethereum with minimal risk"
- "Show me safe options for BTC pools"
`),
new HumanMessage(message),
]);
const cleanJson = response.content
.toString()
.replace(/```json\s*|\s*```/g, "");
return JSON.parse(cleanJson);
}
private async extractPreferences(message: string): Promise<UserPreferences> {
const response = await this.chatModel.invoke([
new SystemMessage(`Extract investment preferences from the message. If a preference isn't specified, make a reasonable assumption based on the user's risk tolerance. Return JSON with format:
{
"riskTolerance": "low|medium|high",
"preferredAssets": ["asset1", "asset2"],
"preferredChains": ["chain1", "chain2"],
"investmentHorizon": "short|medium|long",
"minTVL": number,
"targetAPY": number
}
Rules:
- If user mentions "safe" or "low-risk", set riskTolerance to "low"
- If TVL isn't specified, set minTVL to 1000000 for low risk
- If horizon isn't specified, assume "long" for low risk
- Set reasonable targetAPY based on risk tolerance
`),
new HumanMessage(message),
]);
const cleanJson = response.content
.toString()
.replace(/```json\s*|\s*```/g, "");
return JSON.parse(cleanJson);
}
private getUserPreferencesWithHistory(userId: string): UserPreferences {
const explicitPreferences = this.getUserPreferences(userId);
const chatHistory = this.getChatHistory(userId);
const sortedHistory = chatHistory
.filter((entry) => entry.preferences)
.sort((a, b) => {
const dateA = new Date(a.timestamp);
const dateB = new Date(b.timestamp);
return dateB.getTime() - dateA.getTime();
});
const mostRecentPreferences = sortedHistory[0]?.preferences;
const defaultPreferences: UserPreferences = {
riskTolerance: "medium",
preferredAssets: [],
preferredChains: [],
investmentHorizon: "medium",
timestamp: new Date().toISOString(),
};
return {
...defaultPreferences,
...explicitPreferences,
...mostRecentPreferences,
};
}
async processMessage(userId: string, message: string): Promise<string> {
try {
const { intent } = await this.detectIntent(message);
if (
intent === "set_preferences" ||
message.toLowerCase().includes("looking for")
) {
const preferences = await this.extractPreferences(message);
preferences.timestamp = new Date().toISOString();
const userPreferencesPath = path.join(
this.userPreferencesDir,
`${userId}.json`
);
writeFileSync(
userPreferencesPath,
JSON.stringify(preferences, null, 2)
);
this.storeChatHistory(userId, message, intent, preferences);
const response = await this.getRecommendations(userId);
this.storeChatHistory(userId, response);
return response;
}
if (intent === "get_recommendations") {
const preferences = this.getUserPreferences(userId);
if (!preferences) {
const response =
"Could you tell me what kind of investments you're looking for? For example, what assets and risk level you prefer?";
this.storeChatHistory(userId, response);
return response;
}
this.storeChatHistory(userId, message, intent);
const response = await this.getRecommendations(userId);
this.storeChatHistory(userId, response);
return response;
}
const response =
"agent_control";
this.storeChatHistory(userId, response);
return response;
} catch (error) {
console.error("Error processing message:", error);
const errorResponse =
"I encountered an error while processing your request. Please try again.";
this.storeChatHistory(userId, errorResponse);
return errorResponse;
}
}
private loadPoolData(): void {
try {
const filePath = path.join(__dirname, "data", "tokens.json");
console.log("Loading pool data from:", filePath);
const rawData = readFileSync(filePath, "utf-8");
const parsedData = JSON.parse(rawData);
if (!Array.isArray(parsedData)) {
console.error("Loaded pool data is not an array");
this.pools = [];
return;
}
this.pools = parsedData.filter((pool): pool is Pool => {
return (
typeof pool === "object" &&
pool !== null &&
typeof pool.name === "string" &&
typeof pool.apy === "number" &&
typeof pool.tvl === "number" &&
typeof pool.chain === "string" &&
Array.isArray(pool.assets) &&
typeof pool.riskLevel === "string" &&
typeof pool.impermanentLoss === "number" &&
typeof pool.protocol === "string"
);
});
console.log(`Successfully loaded ${this.pools.length} pools`);
} catch (error) {
console.error("Error loading pool data:", error);
this.pools = [];
}
}
private calculateRiskScore(pool: Pool, preferences: UserPreferences): number {
let score = 0;
if (pool.apy > 50) score += 3;
else if (pool.apy > 20) score += 2;
else score += 1;
if (pool.tvl > 1000000) score -= 1;
if (pool.tvl > 10000000) score -= 1;
if (
preferences.preferredAssets.some((asset) => pool.assets.includes(asset))
) {
score -= 1;
}
score += Math.floor(pool.impermanentLoss / 10);
return score;
}
private filterPoolsByPreferences(preferences: UserPreferences): Pool[] {
return this.pools.filter((pool) => {
const chainMatch = preferences.preferredChains.includes(pool.chain);
const tvlMatch = !preferences.minTVL || pool.tvl >= preferences.minTVL;
const apyMatch =
!preferences.targetAPY || pool.apy >= preferences.targetAPY;
const riskScore = this.calculateRiskScore(pool, preferences);
const riskMatch =
(preferences.riskTolerance === "low" && riskScore <= 2) ||
(preferences.riskTolerance === "medium" && riskScore <= 4) ||
preferences.riskTolerance === "high";
return chainMatch && tvlMatch && apyMatch && riskMatch;
});
}
public getUserPreferences(userId: string): UserPreferences | undefined {
const userPreferencesPath = path.join(
this.userPreferencesDir,
`${userId}.json`
);
if (existsSync(userPreferencesPath)) {
const rawData = readFileSync(userPreferencesPath, "utf-8");
return JSON.parse(rawData);
}
return undefined;
}
private getChatHistory(userId: string): {
message: string;
intent?: string;
preferences?: UserPreferences;
timestamp: string;
}[] {
const chatHistoryPath = path.join(this.chatHistoryDir, `${userId}.json`);
if (existsSync(chatHistoryPath)) {
const rawData = readFileSync(chatHistoryPath, "utf-8");
return JSON.parse(rawData);
}
return [];
}
private storeChatHistory(
userId: string,
message: string,
intent?: string,
preferences?: UserPreferences
): void {
const chatHistoryPath = path.join(this.chatHistoryDir, `${userId}.json`);
const chatHistory = this.getChatHistory(userId);
chatHistory.push({
message,
intent,
preferences,
timestamp: new Date().toISOString(),
});
writeFileSync(chatHistoryPath, JSON.stringify(chatHistory, null, 2));
}
async getRecommendations(userId: string): Promise<string> {
const preferences = this.getUserPreferencesWithHistory(userId);
if (!preferences) {
return "Please tell me what kind of investments you're looking for first.";
}
try {
if (this.pools.length === 0) {
return "No pool data available. Please try again later.";
}
const filteredPools = this.filterPoolsByPreferences(preferences);
if (filteredPools.length === 0) {
return "I couldn't find any pools matching your criteria. Would you like to see options with slightly different parameters?";
}
const chatHistory = this.getChatHistory(userId);
const response = await this.chatModel.invoke([
new SystemMessage(`Create a concise summary of the best investment opportunities based on the user's preferences and chat history. Format:
🎯 Top Recommendations:
1. [Pool Name] ([Protocol])
• APY: X%
• Risk Level: [Low/Medium/High]
• Assets: [Assets]
• TVL: $[Amount]
[Add 2-3 more recommendations]
💡 These options match your preferences for [summarize preferences]`),
new HumanMessage(
JSON.stringify({
preferences,
chatHistory,
recommendations: filteredPools.slice(0, 3),
})
),
]);
return response.content as string;
} catch (error) {
console.error("Error generating recommendations:", error);
return "Sorry, I had trouble generating recommendations. Please try again.";
}
}
}