-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.js
More file actions
402 lines (367 loc) · 19.4 KB
/
Copy pathagent.js
File metadata and controls
402 lines (367 loc) · 19.4 KB
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
import OpenAI from "openai";
import { jsonrepair } from "jsonrepair";
import { buildSystemPrompt } from "./prompt.js";
import { executeTool } from "./tools/executor.js";
import { tools } from "./tools/definitions.js";
const MANAGER_TOOLS = new Set(["close_position", "claim_fees", "swap_token", "get_position_pnl", "get_my_positions", "get_wallet_balance"]);
const SCREENER_TOOLS = new Set(["deploy_position", "get_active_bin", "get_top_candidates", "check_smart_wallets_on_pool", "get_token_holders", "get_token_narrative", "get_token_info", "search_pools", "get_pool_memory", "get_wallet_balance", "get_my_positions"]);
const GENERAL_INTENT_ONLY_TOOLS = new Set([
"self_update",
"update_config",
"add_to_blacklist",
"remove_from_blacklist",
"block_deployer",
"unblock_deployer",
"add_pool_note",
"set_position_note",
"add_smart_wallet",
"remove_smart_wallet",
"add_lesson",
"pin_lesson",
"unpin_lesson",
"clear_lessons",
"add_strategy",
"remove_strategy",
"set_active_strategy",
]);
// Intent → tool subsets for GENERAL role
const INTENT_TOOLS = {
decisions: new Set(["get_recent_decisions"]),
deploy: new Set(["deploy_position", "get_top_candidates", "get_active_bin", "get_pool_memory", "check_smart_wallets_on_pool", "get_token_holders", "get_token_narrative", "get_token_info", "search_pools", "get_wallet_balance", "get_my_positions", "add_pool_note"]),
close: new Set(["close_position", "get_my_positions", "get_position_pnl", "get_wallet_balance", "swap_token"]),
claim: new Set(["claim_fees", "get_my_positions", "get_position_pnl", "get_wallet_balance"]),
swap: new Set(["swap_token", "get_wallet_balance"]),
config: new Set(["update_config"]),
blocklist: new Set(["add_to_blacklist", "remove_from_blacklist", "list_blacklist", "block_deployer", "unblock_deployer", "list_blocked_deployers"]),
selfupdate: new Set(["self_update"]),
balance: new Set(["get_wallet_balance", "get_my_positions", "get_wallet_positions"]),
positions: new Set(["get_my_positions", "get_position_pnl", "get_wallet_balance", "set_position_note", "get_wallet_positions"]),
strategy: new Set(["list_strategies", "get_strategy", "add_strategy", "update_strategy", "delete_strategy", "remove_strategy", "set_active_strategy"]),
screen: new Set(["get_top_candidates", "get_token_holders", "get_token_narrative", "get_token_info", "search_pools", "check_smart_wallets_on_pool", "get_pool_detail", "get_my_positions", "discover_pools"]),
memory: new Set(["get_pool_memory", "add_pool_note", "list_blacklist", "add_to_blacklist", "remove_from_blacklist"]),
smartwallet: new Set(["add_smart_wallet", "remove_smart_wallet", "list_smart_wallets", "check_smart_wallets_on_pool"]),
study: new Set(["study_top_lpers", "get_top_lpers", "get_pool_detail", "search_pools", "get_token_info", "discover_pools", "add_smart_wallet", "list_smart_wallets"]),
performance: new Set(["get_performance_history", "get_my_positions", "get_position_pnl"]),
lessons: new Set(["add_lesson", "pin_lesson", "unpin_lesson", "list_lessons", "clear_lessons"]),
};
const INTENT_PATTERNS = [
{ intent: "decisions", re: /\b(why did you|why'd you|why was (?:this|that|it)|what made you|what was the reason|why no deploy|why didn't you deploy|why did you close|why did you deploy|why did you skip)\b/i },
{ intent: "deploy", re: /\b(deploy|open|add liquidity|lp into|invest in)\b/i },
{ intent: "close", re: /\b(close|exit|withdraw|remove liquidity|shut down)\b/i },
{ intent: "claim", re: /\b(claim|harvest|collect)\b.*\bfee/i },
{ intent: "swap", re: /\b(swap|convert|sell|exchange)\b/i },
{ intent: "selfupdate", re: /\b(self.?update|git pull|pull latest|update (the )?bot|update (the )?agent|update yourself)\b/i },
{ intent: "blocklist", re: /\b(blacklist|block|unblock|blocklist|blocked deployer|rugger|block dev|block deployer)\b/i },
{ intent: "config", re: /\b(config|setting|threshold|update|set |change)\b/i },
{ intent: "balance", re: /\b(balance|wallet|sol|how much)\b/i },
{ intent: "positions", re: /\b(position|portfolio|open|pnl|yield|range)\b/i },
{ intent: "strategy", re: /\b(strategy|strategies)\b/i },
{ intent: "screen", re: /\b(screen|candidate|find pool|search|research|token)\b/i },
{ intent: "memory", re: /\b(memory|pool history|note|remember)\b/i },
{ intent: "smartwallet", re: /\b(smart wallet|kol|whale|watch.?list|add wallet|remove wallet|list wallet|tracked wallet|check pool|who.?s in|wallets in|add to (smart|watch|kol))\b/i },
{ intent: "study", re: /\b(study top|top lpers?|best lpers?|who.?s lping|lp behavior|lpers?)\b/i },
{ intent: "performance", re: /\b(performance|history|how.?s the bot|how.?s it doing|stats|report)\b/i },
{ intent: "lessons", re: /\b(lesson|learned|teach|pin|unpin|clear lesson|what did you learn)\b/i },
];
function getToolsForRole(agentType, goal = "") {
if (agentType === "MANAGER") return tools.filter(t => MANAGER_TOOLS.has(t.function.name));
if (agentType === "SCREENER") return tools.filter(t => SCREENER_TOOLS.has(t.function.name));
// GENERAL: match intent from goal, combine matched tool sets
const matched = new Set();
for (const { intent, re } of INTENT_PATTERNS) {
if (re.test(goal)) {
for (const t of INTENT_TOOLS[intent]) matched.add(t);
}
}
// Fall back to all tools if no intent matched
if (matched.size === 0) return tools.filter(t => !GENERAL_INTENT_ONLY_TOOLS.has(t.function.name));
return tools.filter(t => matched.has(t.function.name));
}
import { getWalletBalances } from "./tools/wallet.js";
import { getMyPositions } from "./tools/dlmm.js";
import { log } from "./logger.js";
import { config } from "./config.js";
import { getStateSummary } from "./state.js";
import { getLessonsForPrompt, getPerformanceSummary } from "./lessons.js";
import { getDecisionSummary } from "./decision-log.js";
// Supports OpenRouter (default) or any OpenAI-compatible local server (e.g. LM Studio)
// To use LM Studio: set LLM_BASE_URL=http://localhost:1234/v1 and LLM_API_KEY=lm-studio in .env
const client = new OpenAI({
baseURL: process.env.LLM_BASE_URL || "https://openrouter.ai/api/v1",
apiKey: process.env.LLM_API_KEY || process.env.OPENROUTER_API_KEY,
timeout: 5 * 60 * 1000,
});
const DEFAULT_MODEL = process.env.LLM_MODEL || "openrouter/healer-alpha";
const MUTATING_TOOL_INTENTS = /\b(deploy|open position|add liquidity|lp into|invest in|close|exit|withdraw|remove liquidity|claim|harvest|collect|swap|convert|sell|exchange|block|unblock|blacklist|add smart wallet|remove smart wallet|add wallet|remove wallet|pin|unpin|clear lesson|add lesson|set active strategy|remove strategy|add strategy|set |change |update |self.?update|pull latest|git pull|update yourself)\b/i;
const LIVE_DATA_TOOL_INTENTS = /\b(balance|wallet|position|portfolio|pnl|yield|range|show positions|open positions|screen|candidate|find pool|search|research|analyze|check pool|token holders|narrative|study top|top lpers?|lp behavior|who.?s lping|performance|history|stats|report|list smart wallets|list blacklist|list blocked deployers|list lessons)\b/i;
const CONFIG_READ_ONLY_INTENTS = /\b(check|show|what(?:'s| is)?|review|inspect|see)\b.*\b(config|settings?|thresholds?)\b/i;
const DECISION_EXPLANATION_INTENTS = /\b(why did you|why'd you|why was (?:this|that|it)|what made you|what was the reason|why no deploy|why didn't you deploy|why did you close|why did you deploy|why did you skip)\b/i;
function shouldRequireRealToolUse(goal, agentType, interactive = false) {
if (agentType === "MANAGER") return false;
if (DECISION_EXPLANATION_INTENTS.test(goal)) return false;
if (CONFIG_READ_ONLY_INTENTS.test(goal)) return false;
if (MUTATING_TOOL_INTENTS.test(goal)) return true;
return interactive && LIVE_DATA_TOOL_INTENTS.test(goal);
}
function buildMessages(systemPrompt, sessionHistory, goal, providerMode = "system") {
if (providerMode === "user_embedded") {
return [
...sessionHistory,
{
role: "user",
content: `[SYSTEM INSTRUCTIONS]\n${systemPrompt}\n\n[USER REQUEST]\n${goal}`,
},
];
}
return [
{ role: "system", content: systemPrompt },
...sessionHistory,
{ role: "user", content: goal },
];
}
function isSystemRoleError(error) {
const message = String(error?.message || error?.error?.message || error || "");
return /invalid message role:\s*system/i.test(message);
}
function isToolChoiceRequiredError(error) {
const message = String(error?.message || error?.error?.message || error || "");
return /tool_choice/i.test(message) && /required/i.test(message);
}
/**
* Core ReAct agent loop.
*
* @param {string} goal - The task description for the agent
* @param {number} maxSteps - Safety limit on iterations (default 20)
* @returns {string} - The agent's final text response
*/
export async function agentLoop(goal, maxSteps = config.llm.maxSteps, sessionHistory = [], agentType = "GENERAL", model = null, maxOutputTokens = null, options = {}) {
const { interactive = false, onToolStart = null, onToolFinish = null } = options;
// Build dynamic system prompt with current portfolio state
const [portfolio, positions] = await Promise.all([getWalletBalances(), getMyPositions()]);
const stateSummary = getStateSummary();
const lessons = getLessonsForPrompt({ agentType });
const perfSummary = getPerformanceSummary();
const decisionSummary = getDecisionSummary();
let weightsSummary = null;
if (agentType === "SCREENER") {
try {
const { getWeightsSummary } = await import("./signal-weights.js");
const { config } = await import("./config.js");
if (config.darwin?.enabled) weightsSummary = getWeightsSummary();
} catch { /* signal-weights not critical */ }
}
const systemPrompt = buildSystemPrompt(agentType, portfolio, positions, stateSummary, lessons, perfSummary, weightsSummary, decisionSummary);
let providerMode = "system";
let messages = buildMessages(systemPrompt, sessionHistory, goal, providerMode);
// Track write tools fired this session — prevent the model from calling the same
// destructive tool twice (e.g. deploy twice, swap twice after auto-swap)
const ONCE_PER_SESSION = new Set(["deploy_position", "swap_token", "close_position"]);
// These lock after first attempt regardless of success — retrying them is always wrong
const NO_RETRY_TOOLS = new Set(["deploy_position"]);
const firedOnce = new Set();
const mustUseRealTool = shouldRequireRealToolUse(goal, agentType, interactive);
let sawToolCall = false;
let noToolRetryCount = 0;
let emptyStreak = 0;
for (let step = 0; step < maxSteps; step++) {
log("agent", `Step ${step + 1}/${maxSteps}`);
try {
const activeModel = model || DEFAULT_MODEL;
// Retry up to 3 times on transient provider errors (502, 503, 529)
const FALLBACK_MODEL = "stepfun/step-3.5-flash:free";
let response;
let usedModel = activeModel;
// Force a tool call on step 0 for action intents — prevents the model from inventing deploy/close outcomes
const ACTION_INTENTS = /\b(deploy|open|add liquidity|close|exit|withdraw|claim|swap|block|unblock)\b/i;
let toolChoice = (step === 0 && (ACTION_INTENTS.test(goal) || mustUseRealTool)) ? "required" : "auto";
for (let attempt = 0; attempt < 3; attempt++) {
try {
response = await client.chat.completions.create({
model: usedModel,
messages,
tools: getToolsForRole(agentType, goal),
tool_choice: toolChoice,
temperature: config.llm.temperature,
max_tokens: maxOutputTokens ?? config.llm.maxTokens,
});
} catch (error) {
if (providerMode === "system" && isSystemRoleError(error)) {
providerMode = "user_embedded";
messages = buildMessages(systemPrompt, sessionHistory, goal, providerMode);
log("agent", "Provider rejected system role — retrying with embedded system instructions");
attempt -= 1;
continue;
}
if (toolChoice === "required" && isToolChoiceRequiredError(error)) {
toolChoice = "auto";
log("agent", "Provider rejected tool_choice=required — retrying with tool_choice=auto");
attempt -= 1;
continue;
}
throw error;
}
if (response.choices?.length) break;
const errCode = response.error?.code;
if (errCode === 502 || errCode === 503 || errCode === 529) {
const wait = (attempt + 1) * 5000;
if (attempt === 1 && usedModel !== FALLBACK_MODEL) {
usedModel = FALLBACK_MODEL;
log("agent", `Switching to fallback model ${FALLBACK_MODEL}`);
} else {
log("agent", `Provider error ${errCode}, retrying in ${wait / 1000}s (attempt ${attempt + 1}/3)`);
await new Promise((r) => setTimeout(r, wait));
}
} else {
break;
}
}
if (!response.choices?.length) {
log("error", `Bad API response: ${JSON.stringify(response).slice(0, 200)}`);
throw new Error(`API returned no choices: ${response.error?.message || JSON.stringify(response)}`);
}
const msg = response.choices[0].message;
const invalidToolArgErrors = new Map();
// Keep tool-call history API-valid, but never execute unrecoverable args.
if (msg.tool_calls) {
for (const tc of msg.tool_calls) {
if (tc.function?.arguments) {
try {
JSON.parse(tc.function.arguments);
} catch {
try {
tc.function.arguments = JSON.stringify(JSON.parse(jsonrepair(tc.function.arguments)));
log("warn", `Repaired malformed JSON args for ${tc.function.name}`);
} catch {
tc.function.arguments = "{}";
const error = `Invalid tool arguments for ${tc.function.name}`;
invalidToolArgErrors.set(tc.id, error);
log("error", `${error}: could not repair JSON`);
}
}
}
}
}
messages.push(msg);
// If the model didn't call any tools, it's done
if (!msg.tool_calls || msg.tool_calls.length === 0) {
// Hermes sometimes returns null content — pop the empty message and retry once
if (!msg.content) {
messages.pop(); // remove the empty assistant message
log("agent", "Empty response, retrying...");
continue;
}
if (mustUseRealTool && !sawToolCall) {
noToolRetryCount += 1;
messages.pop();
log("agent", `Rejected no-tool final answer (${noToolRetryCount}/2) for tool-required request`);
if (noToolRetryCount >= 2) {
return {
content: "I couldn't complete that reliably because no tool call was made. Please retry after checking the logs.",
userMessage: goal,
};
}
messages.push({
role: providerMode === "system" ? "system" : "user",
content: providerMode === "system"
? "You have not used any tool yet. This request requires real tool execution or live tool-backed data. Do not answer from memory or inference. Call the appropriate tool first, then report only the real result."
: "[SYSTEM REMINDER]\nYou have not used any tool yet. This request requires real tool execution or live tool-backed data. Do not answer from memory or inference. Call the appropriate tool first, then report only the real result.",
});
continue;
}
log("agent", "Final answer reached");
log("agent", msg.content);
return { content: msg.content, userMessage: goal };
}
sawToolCall = true;
// Execute each tool call in parallel
const toolResults = await Promise.all(msg.tool_calls.map(async (toolCall) => {
const functionName = toolCall.function.name.replace(/<.*$/, "").trim();
let functionArgs;
if (invalidToolArgErrors.has(toolCall.id)) {
const result = {
success: false,
error: invalidToolArgErrors.get(toolCall.id),
blocked: true,
};
await onToolFinish?.({ name: functionName, args: {}, result, success: false, step });
return {
role: "tool",
tool_call_id: toolCall.id,
content: JSON.stringify(result),
};
}
try {
functionArgs = JSON.parse(toolCall.function.arguments);
} catch {
try {
functionArgs = JSON.parse(jsonrepair(toolCall.function.arguments));
log("warn", `Repaired malformed JSON args for ${functionName}`);
} catch (parseError) {
log("error", `Failed to parse args for ${functionName}: ${parseError.message}`);
const result = {
success: false,
error: `Invalid tool arguments for ${functionName}`,
blocked: true,
};
await onToolFinish?.({ name: functionName, args: {}, result, success: false, step });
return {
role: "tool",
tool_call_id: toolCall.id,
content: JSON.stringify(result),
};
}
}
// Block once-per-session tools from firing a second time
if (ONCE_PER_SESSION.has(functionName) && firedOnce.has(functionName)) {
log("agent", `Blocked duplicate ${functionName} call — already executed this session`);
await onToolFinish?.({
name: functionName,
args: functionArgs,
result: { blocked: true, reason: `${functionName} already attempted this session — do not retry. If it failed, report the error and stop.` },
success: false,
step,
});
return {
role: "tool",
tool_call_id: toolCall.id,
content: JSON.stringify({ blocked: true, reason: `${functionName} already attempted this session — do not retry. If it failed, report the error and stop.` }),
};
}
await onToolStart?.({ name: functionName, args: functionArgs, step });
const result = await executeTool(functionName, functionArgs);
await onToolFinish?.({
name: functionName,
args: functionArgs,
result,
success: result?.success !== false && !result?.error && !result?.blocked,
step,
});
// Lock deploy_position after first attempt regardless of outcome — retrying is never right
// For close/swap: only lock on success so genuine failures can be retried
if (NO_RETRY_TOOLS.has(functionName)) firedOnce.add(functionName);
else if (ONCE_PER_SESSION.has(functionName) && result.success === true) firedOnce.add(functionName);
return {
role: "tool",
tool_call_id: toolCall.id,
content: JSON.stringify(result),
};
}));
messages.push(...toolResults);
} catch (error) {
log("error", `Agent loop error at step ${step}: ${error.message}`);
// If it's a rate limit, wait and retry
if (error.status === 429) {
log("agent", "Rate limited, waiting 30s...");
await sleep(30000);
continue;
}
// For other errors, break the loop
throw error;
}
}
log("agent", "Max steps reached without final answer");
return { content: "Max steps reached. Review logs for partial progress.", userMessage: goal };
}
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}