handler.ts 20 KB

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