handler.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. import fs from "fs";
  2. import os from "os";
  3. import path from "path";
  4. const CHAT_LOG_FILE = "/tmp/openclaw-chat.log";
  5. // ---------------------------------------------------------------------------
  6. // Config — openclaw does NOT inject cfg into hook events.
  7. // Read from env vars; fall back to ~/.openclaw/mem0.json for convenience.
  8. // ---------------------------------------------------------------------------
  9. function loadPluginCfg() {
  10. const cfgPath = path.join(os.homedir(), ".openclaw", "mem0.json");
  11. let fileCfg: Record<string, unknown> = {};
  12. try {
  13. if (fs.existsSync(cfgPath)) {
  14. fileCfg = JSON.parse(fs.readFileSync(cfgPath, "utf8"));
  15. }
  16. } catch {
  17. // ignore malformed file
  18. }
  19. return {
  20. baseUrl:
  21. process.env.MEM0_BASE_URL ||
  22. (fileCfg.baseUrl as string) ||
  23. "http://192.168.0.200:8420",
  24. userId:
  25. process.env.MEM0_USER_ID || (fileCfg.userId as string) || undefined,
  26. recallLimit: Number(
  27. process.env.MEM0_RECALL_LIMIT || fileCfg.recallLimit || 5
  28. ),
  29. captureTrigger: (
  30. process.env.MEM0_CAPTURE_TRIGGER ||
  31. (fileCfg.captureTrigger as string) ||
  32. "always"
  33. ) as "always" | "phrase" | "explicit",
  34. triggerPhrase:
  35. process.env.MEM0_TRIGGER_PHRASE ||
  36. (fileCfg.triggerPhrase as string) ||
  37. "please remember",
  38. autoCapture:
  39. (process.env.MEM0_AUTO_CAPTURE ||
  40. String(fileCfg.autoCapture ?? "true")) !== "false",
  41. autoRecall:
  42. (process.env.MEM0_AUTO_RECALL ||
  43. String(fileCfg.autoRecall ?? "true")) !== "false",
  44. recentKeep: Number(
  45. process.env.MEM0_RECENT_KEEP || fileCfg.recentKeep || 5
  46. ),
  47. // Knowledge-base settings (tool-only; hook no longer injects knowledge)
  48. knowledgeUserId:
  49. process.env.MEM0_KNOWLEDGE_USER_ID ||
  50. (fileCfg.knowledgeUserId as string) ||
  51. "knowledge_base",
  52. rerankThreshold: Number(
  53. process.env.MEM0_RERANK_THRESHOLD || fileCfg.rerankThreshold || 0.002
  54. ),
  55. knowledgeLimit: Number(
  56. process.env.MEM0_KNOWLEDGE_LIMIT || fileCfg.knowledgeLimit || 5
  57. ),
  58. debugCapture:
  59. (process.env.MEM0_DEBUG_CAPTURE ||
  60. String(fileCfg.debugCapture ?? "false")) === "true",
  61. };
  62. }
  63. // ---------------------------------------------------------------------------
  64. // Types
  65. // ---------------------------------------------------------------------------
  66. type HookEvent = {
  67. type: string;
  68. action: string;
  69. sessionKey: string;
  70. timestamp: Date;
  71. messages: string[] | any[];
  72. context: any;
  73. };
  74. interface MemoryResult {
  75. id: string;
  76. memory: string;
  77. score: number;
  78. rerank_score: number;
  79. metadata?: Record<string, any>;
  80. }
  81. interface SearchResponse {
  82. results: MemoryResult[];
  83. }
  84. // ---------------------------------------------------------------------------
  85. // In-memory state — capped to avoid unbounded growth
  86. // ---------------------------------------------------------------------------
  87. const MAX_SESSIONS = 500;
  88. const MAX_TRANSCRIPTS = 1000;
  89. class LRUMap<K, V> extends Map<K, V> {
  90. private readonly maxSize: number;
  91. constructor(maxSize: number) {
  92. super();
  93. this.maxSize = maxSize;
  94. }
  95. set(key: K, value: V): this {
  96. if (this.size >= this.maxSize && !this.has(key)) {
  97. this.delete(this.keys().next().value!);
  98. }
  99. return super.set(key, value);
  100. }
  101. }
  102. const recentBySession = new LRUMap<string, string[]>(MAX_SESSIONS);
  103. const transcriptByMessageId = new LRUMap<string, string>(MAX_TRANSCRIPTS);
  104. // ---------------------------------------------------------------------------
  105. // Helpers
  106. // ---------------------------------------------------------------------------
  107. const LOCAL_STT_URL = "http://192.168.0.200:5005/transcribe";
  108. function getAgentIdFromSessionKey(sessionKey?: string): string | undefined {
  109. if (!sessionKey) return undefined;
  110. const parts = sessionKey.split(":");
  111. if (parts.length >= 2 && parts[0] === "agent") return parts[1];
  112. console.warn("[mem0] unexpected sessionKey format:", sessionKey);
  113. return undefined;
  114. }
  115. function pushRecent(
  116. sessionKey: string,
  117. text: string,
  118. keep: number
  119. ): string[] {
  120. const list = recentBySession.get(sessionKey) || [];
  121. list.push(text);
  122. while (list.length > keep) list.shift();
  123. recentBySession.set(sessionKey, list);
  124. return list;
  125. }
  126. function isMediaPlaceholder(text: string): boolean {
  127. return /<media:[^>]+>/i.test(text);
  128. }
  129. function extractMessageText(context: any): string {
  130. const transcript =
  131. typeof context?.transcript === "string" ? context.transcript.trim() : "";
  132. if (transcript) return transcript;
  133. const candidates = [context?.bodyForAgent, context?.content, context?.body];
  134. for (const candidate of candidates) {
  135. if (typeof candidate === "string") {
  136. const trimmed = candidate.trim();
  137. if (trimmed && !isMediaPlaceholder(trimmed)) return trimmed;
  138. }
  139. }
  140. return "";
  141. }
  142. function getAudioPath(context: any): string | undefined {
  143. if (typeof context?.mediaPath === "string") return context.mediaPath;
  144. if (
  145. Array.isArray(context?.mediaPaths) &&
  146. typeof context.mediaPaths[0] === "string"
  147. ) {
  148. return context.mediaPaths[0];
  149. }
  150. if (typeof context?.media?.path === "string") return context.media.path;
  151. return undefined;
  152. }
  153. function readLastAssistantMessage(sessionKey?: string): string | undefined {
  154. try {
  155. if (!fs.existsSync(CHAT_LOG_FILE)) return undefined;
  156. const payload = fs.readFileSync(CHAT_LOG_FILE, "utf8");
  157. const lines = payload.split(/\r?\n/).filter(Boolean);
  158. // First pass: strict session match.
  159. for (let i = lines.length - 1; i >= 0; i--) {
  160. try {
  161. const entry = JSON.parse(lines[i]);
  162. if (
  163. sessionKey &&
  164. entry?.sessionKey &&
  165. entry.sessionKey !== sessionKey
  166. ) {
  167. continue;
  168. }
  169. const candidate = entry?.messages?.[1];
  170. if (candidate?.role && candidate.role !== "assistant") {
  171. continue;
  172. }
  173. const assistantText = candidate?.content;
  174. if (typeof assistantText === "string" && assistantText.trim()) {
  175. return assistantText.trim();
  176. }
  177. } catch {
  178. // skip malformed lines
  179. }
  180. }
  181. // Second pass fallback: latest assistant message from any session.
  182. if (sessionKey) {
  183. for (let i = lines.length - 1; i >= 0; i--) {
  184. try {
  185. const entry = JSON.parse(lines[i]);
  186. const candidate = entry?.messages?.[1];
  187. if (candidate?.role && candidate.role !== "assistant") {
  188. continue;
  189. }
  190. const assistantText = candidate?.content;
  191. if (typeof assistantText === "string" && assistantText.trim()) {
  192. console.warn(
  193. "[mem0-auto-capture] assistant lookup fallback to global latest",
  194. { requestedSessionKey: sessionKey, logSessionKey: entry?.sessionKey }
  195. );
  196. return assistantText.trim();
  197. }
  198. } catch {
  199. // skip malformed lines
  200. }
  201. }
  202. }
  203. } catch {
  204. // ignore missing or unreadable log
  205. }
  206. return undefined;
  207. }
  208. async function transcribeAudio(localPath: string): Promise<string> {
  209. const buffer = fs.readFileSync(localPath);
  210. const blob = new Blob([buffer]);
  211. const form = new FormData();
  212. form.append("file", blob, "audio.ogg");
  213. const res = await fetch(LOCAL_STT_URL, { method: "POST", body: form });
  214. if (!res.ok) throw new Error(`STT failed: ${res.status}`);
  215. const data = await res.json();
  216. const text =
  217. typeof data?.text === "string" ? data.text.trim() : "";
  218. if (!text) throw new Error("STT returned empty transcript");
  219. return text;
  220. }
  221. // ---------------------------------------------------------------------------
  222. // mem0 search helpers — each returns null on failure so callers can degrade
  223. // gracefully when one endpoint is down.
  224. // ---------------------------------------------------------------------------
  225. async function mem0SearchMemories(
  226. baseUrl: string,
  227. userId: string,
  228. query: string,
  229. limit: number
  230. ): Promise<MemoryResult[] | null> {
  231. try {
  232. const res = await fetch(`${baseUrl}/memories/search`, {
  233. method: "POST",
  234. headers: { "Content-Type": "application/json" },
  235. body: JSON.stringify({ query, userId }),
  236. });
  237. if (!res.ok) {
  238. console.error(`[mem0-recall] /memories/search returned ${res.status}`);
  239. return null;
  240. }
  241. const data: SearchResponse = await res.json();
  242. return Array.isArray(data?.results) ? data.results.slice(0, limit) : [];
  243. } catch (err) {
  244. console.error("[mem0-recall] /memories/search failed:", err);
  245. return null;
  246. }
  247. }
  248. async function mem0SearchKnowledge(
  249. baseUrl: string,
  250. knowledgeUserId: string,
  251. query: string,
  252. limit: number
  253. ): Promise<MemoryResult[] | null> {
  254. try {
  255. const res = await fetch(`${baseUrl}/knowledge/search`, {
  256. method: "POST",
  257. headers: { "Content-Type": "application/json" },
  258. body: JSON.stringify({ query, userId: knowledgeUserId }),
  259. });
  260. if (!res.ok) {
  261. console.error(`[mem0-recall] /knowledge/search returned ${res.status}`);
  262. return null;
  263. }
  264. const data: SearchResponse = await res.json();
  265. return Array.isArray(data?.results) ? data.results.slice(0, limit) : [];
  266. } catch (err) {
  267. console.error("[mem0-recall] /knowledge/search failed:", err);
  268. return null;
  269. }
  270. }
  271. // ---------------------------------------------------------------------------
  272. // Result filtering & formatting
  273. // ---------------------------------------------------------------------------
  274. function filterByRerank(
  275. results: MemoryResult[],
  276. threshold: number
  277. ): MemoryResult[] {
  278. return results.filter(
  279. (r) => typeof r.rerank_score === "number" && r.rerank_score >= threshold
  280. );
  281. }
  282. function formatKnowledgeCitation(meta: Record<string, any> = {}): string {
  283. const parts: string[] = [];
  284. if (meta.source_file) parts.push(meta.source_file);
  285. if (meta.chapter != null) parts.push(`ch.${meta.chapter}`);
  286. if (meta.page_start != null && meta.page_end != null) {
  287. parts.push(`pp.${meta.page_start}-${meta.page_end}`);
  288. } else if (meta.page_start != null) {
  289. parts.push(`p.${meta.page_start}`);
  290. }
  291. return parts.length > 0 ? `(from: ${parts.join(", ")})` : "";
  292. }
  293. function formatAge(createdAt?: string): string {
  294. if (!createdAt) return "";
  295. const created = new Date(createdAt).getTime();
  296. if (!Number.isFinite(created)) return "";
  297. const deltaMs = Date.now() - created;
  298. if (deltaMs < 0) return "just now";
  299. const minute = 60_000;
  300. const hour = 60 * minute;
  301. const day = 24 * hour;
  302. if (deltaMs < minute) return "just now";
  303. if (deltaMs < hour) return `${Math.floor(deltaMs / minute)}m ago`;
  304. if (deltaMs < day) return `${Math.floor(deltaMs / hour)}h ago`;
  305. return `${Math.floor(deltaMs / day)}d ago`;
  306. }
  307. function buildInjectionBlock(
  308. personalResults: MemoryResult[] | null,
  309. knowledgeResults: MemoryResult[] | null,
  310. threshold: number
  311. ): string {
  312. const sections: string[] = [];
  313. // Personal memories
  314. const personal = personalResults
  315. ? filterByRerank(personalResults, threshold)
  316. : null;
  317. if (personal === null) {
  318. // endpoint was down — omit the section entirely, already logged
  319. } else if (personal.length > 0) {
  320. const lines = personal
  321. .map((r) => {
  322. const createdAt =
  323. (r as any)?.created_at ||
  324. (r.metadata && (r.metadata as any).created_at);
  325. const age = formatAge(createdAt);
  326. return age ? `- [${age}] ${r.memory}` : `- ${r.memory}`;
  327. })
  328. .join("\n");
  329. sections.push(`[MEMORY - Personal]\n${lines}`);
  330. }
  331. // Knowledge base — sort by rerank_score descending
  332. const knowledge = knowledgeResults
  333. ? filterByRerank(knowledgeResults, threshold).sort(
  334. (a, b) => b.rerank_score - a.rerank_score
  335. )
  336. : null;
  337. if (knowledge === null) {
  338. // endpoint was down — omit the section entirely, already logged
  339. } else if (knowledge.length > 0) {
  340. const lines = knowledge
  341. .map((r) => {
  342. const citation = formatKnowledgeCitation(r.metadata || {});
  343. return citation ? `- ${citation} ${r.memory}` : `- ${r.memory}`;
  344. })
  345. .join("\n");
  346. sections.push(`[MEMORY - Knowledge Base]\n${lines}`);
  347. }
  348. return sections.join("\n\n");
  349. }
  350. // Build a quick hash to deduplicate captures
  351. function simpleHash(text: string): string {
  352. let h = 0;
  353. for (let i = 0; i < text.length; i++) {
  354. h = (Math.imul(31, h) + text.charCodeAt(i)) | 0;
  355. }
  356. return h.toString(36);
  357. }
  358. const recentlyCaptured = new LRUMap<string, number>(200);
  359. const CAPTURE_DEDUP_MS = 60_000;
  360. // ---------------------------------------------------------------------------
  361. // Main handler
  362. // ---------------------------------------------------------------------------
  363. export default async function handler(event: HookEvent) {
  364. console.log(
  365. "[mem0-FIRE]",
  366. JSON.stringify(
  367. {
  368. type: event.type,
  369. action: event.action,
  370. sessionKey: event.sessionKey,
  371. contextKeys: Object.keys(event.context || {}),
  372. content: event.context?.content?.slice(0, 80),
  373. messagesLen: event.messages?.length,
  374. },
  375. null,
  376. 2
  377. )
  378. );
  379. if (event.type !== "message") {
  380. console.log("[mem0-FIRE] bailed: type is not message, got:", event.type);
  381. return;
  382. }
  383. const pluginCfg = loadPluginCfg();
  384. const userId =
  385. getAgentIdFromSessionKey(event.sessionKey) ||
  386. pluginCfg.userId ||
  387. "default";
  388. // ── Audio transcription (runs once per messageId) ────────────────────────
  389. const messageId =
  390. event.context?.messageId || event.context?.metadata?.messageId;
  391. const audioPath = getAudioPath(event.context);
  392. const hasTranscript =
  393. typeof event.context?.transcript === "string" &&
  394. event.context.transcript.trim().length > 0;
  395. if (!hasTranscript && audioPath && fs.existsSync(audioPath)) {
  396. if (messageId && transcriptByMessageId.has(messageId)) {
  397. event.context.transcript = transcriptByMessageId.get(messageId);
  398. } else {
  399. try {
  400. const text = await transcribeAudio(audioPath);
  401. event.context.transcript = text;
  402. if (messageId) transcriptByMessageId.set(messageId, text);
  403. } catch (err) {
  404. console.error("[mem0-stt] failed:", err);
  405. if (
  406. isMediaPlaceholder(
  407. event.context?.bodyForAgent ||
  408. event.context?.content ||
  409. ""
  410. )
  411. ) {
  412. return;
  413. }
  414. }
  415. }
  416. }
  417. // Patch bodyForAgent with transcript for audio messages
  418. const transcriptNow =
  419. typeof event.context?.transcript === "string"
  420. ? event.context.transcript.trim()
  421. : "";
  422. if (transcriptNow && isMediaPlaceholder(event.context?.bodyForAgent || "")) {
  423. event.context.bodyForAgent = transcriptNow;
  424. }
  425. // ── Extract text (shared by both branches) ───────────────────────────────
  426. function resolveText(): string {
  427. let text = extractMessageText(event.context);
  428. if (!text && Array.isArray(event.messages) && event.messages.length > 0) {
  429. const m = event.messages[0];
  430. if (typeof m === "string") text = m.trim();
  431. else if (typeof m?.text === "string") text = m.text.trim();
  432. else if (typeof m?.body === "string") text = m.body.trim();
  433. }
  434. return text;
  435. }
  436. // ── Auto-recall: queries both endpoints in parallel ───────────────────────
  437. const runAutoRecall = async (text: string) => {
  438. if (!pluginCfg.autoRecall) return;
  439. const { baseUrl, recallLimit, rerankThreshold } = pluginCfg;
  440. console.log("[mem0-auto-recall] query:", text.slice(0, 120));
  441. const personalResults = await mem0SearchMemories(
  442. baseUrl,
  443. userId,
  444. text,
  445. recallLimit
  446. );
  447. console.log("[mem0-auto-recall]", {
  448. userId,
  449. personalCount: personalResults?.length ?? "error",
  450. threshold: rerankThreshold,
  451. });
  452. const injectionBlock = buildInjectionBlock(
  453. personalResults,
  454. null,
  455. rerankThreshold
  456. );
  457. if (!injectionBlock) return; // nothing passed the threshold from either endpoint
  458. event.context.bodyForAgent = `${text}\n\n${injectionBlock}`;
  459. console.log("[mem0-injected-prompt]\n" + event.context.bodyForAgent);
  460. };
  461. // ── received: capture ────────────────────────────────────────────────────
  462. if (event.action === "received") {
  463. if (!pluginCfg.autoCapture) return;
  464. const text = resolveText();
  465. if (!text) return;
  466. if (isMediaPlaceholder(text) && !transcriptNow) return;
  467. const recent = pushRecent(
  468. event.sessionKey || "global",
  469. text,
  470. pluginCfg.recentKeep
  471. );
  472. const { captureTrigger, triggerPhrase } = pluginCfg;
  473. let shouldCapture = false;
  474. if (captureTrigger === "always") {
  475. shouldCapture = true;
  476. } else if (captureTrigger === "phrase") {
  477. shouldCapture = new RegExp(triggerPhrase, "i").test(text);
  478. } else {
  479. shouldCapture = /please\s+remember/i.test(text);
  480. }
  481. if (!shouldCapture) return;
  482. const assistantMessage = readLastAssistantMessage(event.sessionKey);
  483. const userCaptureText = recent.join("\n");
  484. const captureMessages: Array<{ role: "user" | "assistant"; content: string }> = [
  485. { role: "user", content: userCaptureText },
  486. ];
  487. if (assistantMessage) {
  488. captureMessages.push({ role: "assistant", content: assistantMessage });
  489. }
  490. const hash = simpleHash(JSON.stringify(captureMessages));
  491. const lastCapture = recentlyCaptured.get(hash);
  492. if (lastCapture && Date.now() - lastCapture < CAPTURE_DEDUP_MS) {
  493. console.log("[mem0-auto-capture] skipped duplicate");
  494. return;
  495. }
  496. recentlyCaptured.set(hash, Date.now());
  497. try {
  498. if (pluginCfg.debugCapture) {
  499. console.log("[mem0-auto-capture]", {
  500. sessionKey: event.sessionKey,
  501. userId,
  502. captureTrigger,
  503. hasAssistantMessage: !!assistantMessage,
  504. messages: captureMessages,
  505. });
  506. } else {
  507. console.log("[mem0-auto-capture]", {
  508. sessionKey: event.sessionKey,
  509. userId,
  510. captureTrigger,
  511. hasAssistantMessage: !!assistantMessage,
  512. messageCount: captureMessages.length,
  513. });
  514. }
  515. await fetch(`${pluginCfg.baseUrl}/memories`, {
  516. method: "POST",
  517. headers: { "Content-Type": "application/json" },
  518. body: JSON.stringify({ userId, messages: captureMessages }),
  519. });
  520. } catch (err) {
  521. console.error("[mem0-auto-capture] write failed:", err);
  522. }
  523. return;
  524. }
  525. // ── preprocessed: recall ─────────────────────────────────────────────────
  526. if (event.action === "preprocessed") {
  527. const text = resolveText();
  528. if (!text) return;
  529. if (!transcriptNow && isMediaPlaceholder(text)) return;
  530. await runAutoRecall(text);
  531. return;
  532. }
  533. // ── transcribed: recall ──────────────────────────────────────────────────
  534. if (event.action === "transcribed") {
  535. const text = extractMessageText(event.context);
  536. if (!text) return;
  537. await runAutoRecall(text);
  538. }
  539. }