-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhivemind.js
More file actions
346 lines (312 loc) · 11 KB
/
Copy pathhivemind.js
File metadata and controls
346 lines (312 loc) · 11 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
import fs from "fs";
import path from "path";
import crypto from "crypto";
import { fileURLToPath } from "url";
import { log } from "./logger.js";
import { config } from "./config.js";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const USER_CONFIG_PATH = path.join(__dirname, "user-config.json");
const CACHE_PATH = path.join(__dirname, "hivemind-cache.json");
const PACKAGE_JSON_PATH = path.join(__dirname, "package.json");
const HEARTBEAT_INTERVAL_MS = 15 * 60 * 1000;
let _heartbeatTimer = null;
function readJson(filePath, fallback) {
if (!fs.existsSync(filePath)) return fallback;
try {
return JSON.parse(fs.readFileSync(filePath, "utf8"));
} catch {
return fallback;
}
}
function writeJson(filePath, value) {
fs.writeFileSync(filePath, JSON.stringify(value, null, 2));
}
function sanitizeText(text, maxLen = 400) {
if (text == null) return null;
const cleaned = String(text)
.replace(/[\r\n\t]+/g, " ")
.replace(/\s+/g, " ")
.replace(/[<>`]/g, "")
.trim()
.slice(0, maxLen);
return cleaned || null;
}
function getVersion() {
try {
return JSON.parse(fs.readFileSync(PACKAGE_JSON_PATH, "utf8")).version || "1.0.0";
} catch {
return "1.0.0";
}
}
const AGENT_VERSION = getVersion();
function readUserConfig() {
return readJson(USER_CONFIG_PATH, {});
}
function writeUserConfig(nextConfig) {
writeJson(USER_CONFIG_PATH, nextConfig);
}
function readCache() {
return readJson(CACHE_PATH, {
sharedLessons: [],
presets: [],
pulledAt: null,
});
}
function writeCache(nextCache) {
writeJson(CACHE_PATH, nextCache);
}
function getBaseUrl() {
return sanitizeText(config.hiveMind?.url || "", 500) || "";
}
function getApiKey() {
return sanitizeText(config.hiveMind?.apiKey || "", 300) || "";
}
function getPullMode() {
const mode = sanitizeText(config.hiveMind?.pullMode || "auto", 20) || "auto";
return mode === "manual" ? "manual" : "auto";
}
export function getHiveMindPullMode() {
return getPullMode();
}
export function isHiveMindEnabled() {
return !!(getBaseUrl() && getApiKey());
}
export function ensureAgentId() {
const userConfig = readUserConfig();
if (userConfig.agentId) {
config.hiveMind.agentId = userConfig.agentId;
return userConfig.agentId;
}
const agentId = `agt_${crypto.randomBytes(12).toString("hex")}`;
userConfig.agentId = agentId;
writeUserConfig(userConfig);
config.hiveMind.agentId = agentId;
log("hivemind", `Generated agentId ${agentId}`);
return agentId;
}
function getAgentId() {
return config.hiveMind?.agentId || ensureAgentId();
}
function buildUrl(pathname, query = {}) {
const url = new URL(pathname, getBaseUrl());
for (const [key, value] of Object.entries(query)) {
if (value !== undefined && value !== null && value !== "") {
url.searchParams.set(key, String(value));
}
}
return url.toString();
}
async function requestJson(pathname, { method = "GET", body = null, query = {} } = {}) {
if (!isHiveMindEnabled()) return null;
const response = await fetch(buildUrl(pathname, query), {
method,
headers: {
accept: "application/json",
"x-api-key": getApiKey(),
...(body != null ? { "content-type": "application/json" } : {}),
},
body: body != null ? JSON.stringify(body) : undefined,
});
const payload = await response.json().catch(() => null);
if (!response.ok) {
throw new Error(payload?.error || `HiveMind ${response.status}`);
}
return payload;
}
function normalizeSharedLesson(lesson) {
const rule = sanitizeText(lesson?.rule, 400);
if (!rule) return null;
return {
id: lesson.id || lesson.lessonId || `shared_${Date.now()}`,
rule,
tags: Array.isArray(lesson.tags) ? lesson.tags.map((tag) => sanitizeText(tag, 48)).filter(Boolean) : [],
role: sanitizeText(lesson.role || "", 20) || null,
outcome: sanitizeText(lesson.outcome || "shared", 20) || "shared",
sourceType: sanitizeText(lesson.sourceType || lesson.source || "shared", 24) || "shared",
score: Number.isFinite(Number(lesson.score)) ? Number(lesson.score) : null,
created_at: lesson.created_at || lesson.createdAt || new Date().toISOString(),
};
}
export function getSharedLessonsForPrompt({ agentType = "GENERAL", maxLessons = 6 } = {}) {
const role = String(agentType || "GENERAL").toUpperCase();
const shared = (readCache().sharedLessons || [])
.map(normalizeSharedLesson)
.filter(Boolean)
.filter((lesson) => !lesson.role || lesson.role === role || role === "GENERAL")
.sort((left, right) => (Number(right.score) || 0) - (Number(left.score) || 0))
.slice(0, maxLessons);
if (!shared.length) return null;
return shared
.map((lesson) => `[HIVEMIND${lesson.score != null ? ` score=${lesson.score}` : ""}] ${lesson.rule}`)
.join("\n");
}
export async function registerHiveMindAgent({ reason = "heartbeat" } = {}) {
if (!isHiveMindEnabled()) return null;
try {
return await requestJson("/api/hivemind/agents/register", {
method: "POST",
body: {
agentId: getAgentId(),
version: AGENT_VERSION,
timestamp: new Date().toISOString(),
reason,
capabilities: {
telegram: !!process.env.TELEGRAM_BOT_TOKEN,
lpagent: !!process.env.LPAGENT_API_KEY,
dryRun: process.env.DRY_RUN === "true",
},
},
});
} catch (error) {
log("hivemind_warn", `Agent register failed: ${error.message}`);
return null;
}
}
export async function pullHiveMindLessons(limit = 12) {
if (!isHiveMindEnabled()) return null;
try {
const payload = await requestJson("/api/hivemind/lessons/pull", {
query: { agentId: getAgentId(), limit },
});
const cache = readCache();
cache.sharedLessons = Array.isArray(payload?.lessons)
? payload.lessons.map(normalizeSharedLesson).filter(Boolean)
: [];
cache.pulledAt = new Date().toISOString();
writeCache(cache);
return cache.sharedLessons;
} catch (error) {
log("hivemind_warn", `Lesson pull failed: ${error.message}`);
return null;
}
}
export async function pullHiveMindPresets() {
if (!isHiveMindEnabled()) return null;
try {
const payload = await requestJson("/api/hivemind/presets/pull", {
query: { agentId: getAgentId() },
});
const cache = readCache();
cache.presets = Array.isArray(payload?.presets) ? payload.presets : [];
cache.pulledAt = new Date().toISOString();
writeCache(cache);
return cache.presets;
} catch (error) {
log("hivemind_warn", `Preset pull failed: ${error.message}`);
return null;
}
}
export async function bootstrapHiveMind() {
if (!isHiveMindEnabled()) return null;
ensureAgentId();
const tasks = [registerHiveMindAgent({ reason: "startup" })];
if (getPullMode() === "auto") {
tasks.push(pullHiveMindLessons(), pullHiveMindPresets());
}
await Promise.allSettled(tasks);
return { enabled: true, agentId: getAgentId(), pullMode: getPullMode() };
}
export function startHiveMindBackgroundSync() {
if (!isHiveMindEnabled() || _heartbeatTimer) return null;
_heartbeatTimer = setInterval(() => {
const tasks = [registerHiveMindAgent({ reason: "heartbeat" })];
if (getPullMode() === "auto") {
tasks.push(pullHiveMindLessons(), pullHiveMindPresets());
}
Promise.allSettled(tasks).catch(() => null);
}, HEARTBEAT_INTERVAL_MS);
return _heartbeatTimer;
}
function buildLessonEvent(lesson) {
const rule = sanitizeText(lesson?.rule, 400);
if (!rule) return null;
const sourceType = sanitizeText(lesson.sourceType || inferLessonSourceType(lesson), 24) || "manual";
return {
eventId: `lesson:${getAgentId()}:${lesson.id || crypto.randomUUID()}`,
agentId: getAgentId(),
version: AGENT_VERSION,
timestamp: lesson.created_at || new Date().toISOString(),
lesson: {
id: lesson.id || null,
rule,
tags: Array.isArray(lesson.tags) ? lesson.tags.map((tag) => sanitizeText(tag, 48)).filter(Boolean) : [],
role: sanitizeText(lesson.role || "", 20) || null,
outcome: sanitizeText(lesson.outcome || "manual", 20) || "manual",
sourceType,
confidence: Number.isFinite(Number(lesson.confidence)) ? Number(lesson.confidence) : null,
pool: sanitizeText(lesson.pool || "", 64) || null,
pinned: !!lesson.pinned,
metrics: {
pnlPct: Number.isFinite(Number(lesson.pnl_pct)) ? Number(lesson.pnl_pct) : null,
feesUsd: Number.isFinite(Number(lesson.fees_earned_usd)) ? Number(lesson.fees_earned_usd) : null,
initialValueUsd: Number.isFinite(Number(lesson.initial_value_usd)) ? Number(lesson.initial_value_usd) : null,
rangeEfficiency: Number.isFinite(Number(lesson.range_efficiency)) ? Number(lesson.range_efficiency) : null,
closeReason: sanitizeText(lesson.close_reason || "", 160) || null,
},
},
};
}
function inferLessonSourceType(lesson) {
const tags = Array.isArray(lesson?.tags) ? lesson.tags.map((tag) => String(tag).toLowerCase()) : [];
const rule = String(lesson?.rule || "").toLowerCase();
if (tags.includes("self_tune") || tags.includes("config_change") || rule.startsWith("[self-tuned]")) {
return "config_change";
}
if (lesson?.outcome === "manual") {
return "manual";
}
return "performance";
}
export async function pushHiveLesson(lesson) {
if (!isHiveMindEnabled()) return null;
const body = buildLessonEvent(lesson);
if (!body) return null;
try {
return await requestJson("/api/hivemind/lessons/push", {
method: "POST",
body,
});
} catch (error) {
log("hivemind_warn", `Lesson push failed: ${error.message}`);
return null;
}
}
function shouldCountInAdjustedWinRate(closeReason) {
const text = String(closeReason || "").toLowerCase();
return !(
text.includes("out of range") ||
text.includes("pumped far above range") ||
text === "oor" ||
text.includes("oor")
);
}
export async function pushHivePerformanceEvent(perf) {
if (!isHiveMindEnabled()) return null;
try {
return await requestJson("/api/hivemind/performance/push", {
method: "POST",
body: {
eventId: sanitizeText(perf.eventId, 200) || `close:${getAgentId()}:${perf.position || perf.pool}:${perf.recorded_at || Date.now()}`,
agentId: getAgentId(),
version: AGENT_VERSION,
timestamp: perf.recorded_at || new Date().toISOString(),
event: {
pool: sanitizeText(perf.pool, 64) || null,
poolName: sanitizeText(perf.pool_name, 80) || null,
baseMint: sanitizeText(perf.base_mint, 64) || null,
strategy: sanitizeText(perf.strategy, 32) || null,
closeReason: sanitizeText(perf.close_reason, 200) || "unknown",
pnlUsd: Number(perf.pnl_usd || 0),
pnlPct: Number(perf.pnl_pct || 0),
feesUsd: Number(perf.fees_earned_usd || 0),
feesSol: Number(perf.fees_earned_sol || 0),
minutesHeld: Number(perf.minutes_held || 0),
countInAdjustedWinRate: shouldCountInAdjustedWinRate(perf.close_reason),
},
},
});
} catch (error) {
log("hivemind_warn", `Performance push failed: ${error.message}`);
return null;
}
}