Skip to content

Persistent Context Across Sessions for Every Agent – Captures everything your agent does during sessions, compresses it with AI, and injects relevant context back into future sessions. Works with Claude Code, OpenClaw, Codex, Gemini, Hermes, Copilot, OpenCode + More

9.4 KB
Raw Blame
var __CM_FILENAME__ = typeof __filename !== "undefined" ? __filename : require("node:path").resolve(process.argv[1] || "");
var __CM_DIRNAME__ = typeof __dirname !== "undefined" ? __dirname : require("node:path").dirname(__CM_FILENAME__);
var __IMPORT_META_URL__ = require("node:url").pathToFileURL(__CM_FILENAME__).href;
"use strict";var b=Object.defineProperty;var Y=Object.getOwnPropertyDescriptor;var z=Object.getOwnPropertyNames;var K=Object.prototype.hasOwnProperty;var q=(n,t)=>{for(var r in t)b(n,r,{get:t[r],enumerable:!0})},X=(n,t,r,e)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of z(t))!K.call(n,o)&&o!==r&&b(n,o,{get:()=>t[o],enumerable:!(e=Y(t,o))||e.enumerable});return n};var Q=n=>X(b({},"__esModule",{value:!0}),n);var ft={};q(ft,{parseFileList:()=>ut,parseIndexedFilePaths:()=>$,replaceObservationFileIndex:()=>at});module.exports=Q(ft);var a=require("fs"),R=require("path");var s=require("path"),W=require("os");var B=require("url");var U=require("fs"),J=require("os"),A=require("path");var f=require("fs");var Z=null;function V(n){return(Z??process.stderr.write.bind(process.stderr))(n)}function x(n){V(n)}var dt=Promise.resolve();var Et=process.platform==="win32";function tt(n){return n.replace(/^\uFEFF/,"")}function et(n){return JSON.parse(tt(n))}function y(n){return et((0,f.readFileSync)(n,"utf-8"))}var N=require("os"),M=require("path");function h(n,t=process.platform,r=(0,N.homedir)()){return typeof n!="string"||n.length===0?n:n==="~"?r:n.startsWith("~/")||t==="win32"&&n.startsWith("~\\")?(0,M.join)(r,n.slice(2)):n}var P=n=>n!==null&&typeof n=="object"&&!Array.isArray(n);function j(n){let t=n.env;return P(t)&&Object.keys(t).some(r=>r.startsWith("CLAUDE_MEM_"))?"nested":"flat"}function D(n){return j(n)==="nested"?n.env:n}function k(){if(process.env.CLAUDE_MEM_DATA_DIR)return h(process.env.CLAUDE_MEM_DATA_DIR);let n=(0,A.join)((0,J.homedir)(),".claude-mem"),t=(0,A.join)(n,"settings.json");try{if((0,U.existsSync)(t)){let r=y(t);if(r===null||typeof r!="object"||Array.isArray(r))return n;let e=D(r);if(typeof e.CLAUDE_MEM_DATA_DIR=="string"&&e.CLAUDE_MEM_DATA_DIR)return h(e.CLAUDE_MEM_DATA_DIR)}}catch{}return n}function rt(){return typeof __CM_DIRNAME__<"u"?__CM_DIRNAME__:(0,s.dirname)((0,B.fileURLToPath)(__IMPORT_META_URL__))}var $t=rt(),c=k(),nt=(0,s.join)((0,W.homedir)(),".claude"),ot=process.env.CLAUDE_CONFIG_DIR||nt,Lt=(0,s.join)(ot,"plugins","marketplaces","thedotmack"),it=(0,s.join)(c,"logs"),It=(0,s.join)(c,"settings.json"),G="claude-mem.db";var Ft=(0,s.join)(c,G),st=(0,s.join)(c,"observer-sessions"),Nt=(0,s.basename)(st);var w={dataDir:()=>c,workerPid:()=>(0,s.join)(c,"worker.pid"),workerHeartbeat:()=>(0,s.join)(c,"worker-heartbeat.json"),serverPid:()=>(0,s.join)(c,".server-beta.pid"),serverPort:()=>(0,s.join)(c,".server-beta.port"),serverRuntime:()=>(0,s.join)(c,".server-beta.runtime.json"),settings:()=>(0,s.join)(c,"settings.json"),database:()=>(0,s.join)(c,G),backups:()=>(0,s.join)(c,"backups","auto"),pendingRestore:()=>(0,s.join)(c,"backups","restore-pending.json"),lastRestore:()=>(0,s.join)(c,"backups","last-restore.json"),chroma:()=>(0,s.join)(c,"chroma"),combinedCerts:()=>(0,s.join)(c,"combined_certs.pem"),transcriptsConfig:()=>(0,s.join)(c,"transcript-watch.json"),transcriptsState:()=>(0,s.join)(c,"transcript-watch-state.json"),corpora:()=>(0,s.join)(c,"corpora"),supervisorRegistry:()=>(0,s.join)(c,"supervisor.json"),envFile:()=>(0,s.join)(c,".env"),logsDir:()=>it};var C=(i=>(i[i.DEBUG=0]="DEBUG",i[i.INFO=1]="INFO",i[i.WARN=2]="WARN",i[i.ERROR=3]="ERROR",i[i.SILENT=4]="SILENT",i))(C||{}),_=null;function p(n){return n.replace(/\r/g,"\\r").replace(/\n/g,"\\n")}function ct(n){return"  "+n.replace(/\r/g,"\\r").replace(/\n(?![ \t]+at )/g,"\\n")}var T=class{level=null;useColor;logFilePath=null;logFileInitialized=!1;logFileDate=null;logFd=null;constructor(){this.useColor=process.stdout.isTTY??!1}ensureLogFileInitialized(){let t=new Date().toISOString().split("T")[0];if(this.logFileInitialized&&this.logFileDate===t)return;let r=this.logFileDate!==t;this.closeLogFd(),this.logFileInitialized=!1,this.logFileDate=t;try{let e=w.logsDir();(0,a.existsSync)(e)||(0,a.mkdirSync)(e,{recursive:!0}),this.logFilePath=(0,R.join)(e,`claude-mem-${t}.log`),this.logFileInitialized=!0}catch(e){r&&console.error("[LOGGER] Failed to initialize log file:",e instanceof Error?e.message:String(e)),this.logFilePath=null}}closeLogFd(){if(this.logFd!==null){try{(0,a.closeSync)(this.logFd)}catch{}this.logFd=null}}appendLine(t,r){if(this.logFd!==null){let e=!1;try{let o=(0,a.fstatSync)(this.logFd),i=(0,a.statSync)(t);e=o.ino===i.ino&&o.dev===i.dev}catch{}e||this.closeLogFd()}this.logFd===null&&((0,a.mkdirSync)((0,R.dirname)(t),{recursive:!0}),this.logFd=(0,a.openSync)(t,"a"));try{let e=Buffer.from(r,"utf8");for(;e.length>0;){let o=(0,a.writeSync)(this.logFd,e);if(o<=0)throw new Error("log write accepted 0 bytes");e=e.subarray(o)}}catch(e){throw this.closeLogFd(),e}}getLevel(){if(this.level===null)try{let t=w.settings();if((0,a.existsSync)(t)){let e=(D(y(t)).CLAUDE_MEM_LOG_LEVEL||"INFO").toString().toUpperCase();this.level=C[e]??1}else this.level=1}catch(t){console.error("[LOGGER] Failed to load log level from settings:",t instanceof Error?t.message:String(t)),this.level=1}return this.level}safeStringify(t,r,e=6){let o=new WeakSet,i=(l,S)=>{if(typeof l=="bigint")return`${l}n`;if(l===null||typeof l!="object")return l;let E=l.toJSON,u=typeof E=="function"?E.call(l):l;if(typeof u=="bigint")return`${u}n`;if(u===null||typeof u!="object")return u;if(o.has(u))return"[Circular]";if(S>=e)return Array.isArray(u)?"[Array]":"[Object]";o.add(u);try{if(Array.isArray(u))return u.map(d=>i(d,S+1));let g={};for(let d of Object.keys(u))try{g[d]=i(u[d],S+1)}catch{g[d]="[unreadable]"}return g}finally{o.delete(u)}};try{return JSON.stringify(i(t,0),null,r)??String(t)}catch{return Array.isArray(t)?`[${t.length} items]`:"[unserializable]"}}formatData(t){if(t==null)return"";if(typeof t=="string")return t;if(typeof t=="number"||typeof t=="boolean")return t.toString();if(typeof t=="object"){if(t instanceof Error)return this.getLevel()===0?`${t.message}
${t.stack}`:t.message;if(Array.isArray(t))return`[${t.length} items]`;let r=Object.keys(t);return r.length===0?"{}":r.length<=3?this.safeStringify(t):`{${r.length} keys: ${r.slice(0,3).join(", ")}...}`}return String(t)}formatTool(t,r){if(!r)return t;let e=r;if(typeof r=="string")try{e=JSON.parse(r)}catch{e=r}if(e===null)return t;if(t==="Bash"&&e.command)return`${t}(${e.command})`;if(e.file_path)return`${t}(${e.file_path})`;if(e.notebook_path)return`${t}(${e.notebook_path})`;if(t==="Glob"&&e.pattern)return`${t}(${e.pattern})`;if(t==="Grep"&&e.pattern)return`${t}(${e.pattern})`;if(e.url)return`${t}(${e.url})`;if(e.query)return`${t}(${e.query})`;if(t==="Task"){if(e.subagent_type)return`${t}(${e.subagent_type})`;if(e.description)return`${t}(${e.description})`}return t==="Skill"&&e.skill?`${t}(${e.skill})`:t==="LSP"&&e.operation?`${t}(${e.operation})`:t}formatTimestamp(t){let r=t.getFullYear(),e=String(t.getMonth()+1).padStart(2,"0"),o=String(t.getDate()).padStart(2,"0"),i=String(t.getHours()).padStart(2,"0"),l=String(t.getMinutes()).padStart(2,"0"),S=String(t.getSeconds()).padStart(2,"0"),E=String(t.getMilliseconds()).padStart(3,"0");return`${r}-${e}-${o} ${i}:${l}:${S}.${E}`}log(t,r,e,o,i){if(t<this.getLevel())return;this.ensureLogFileInitialized();let l=this.formatTimestamp(new Date),S=C[t].padEnd(5),E=r.padEnd(6),u="";o?.correlationId?u=`[${p(String(o.correlationId))}] `:o?.sessionId&&(u=`[session-${p(String(o.sessionId))}] `);let g="";i!=null&&(i instanceof Error?(g=` ${p(i.message)}`,this.getLevel()===0&&i.stack&&(g+=`
${ct(i.stack)}`)):this.getLevel()===0&&typeof i=="object"?g=`
`+this.safeStringify(i,2):g=" "+p(this.formatData(i)));let d="";if(o){let{sessionId:O,memorySessionId:v,correlationId:lt,...I}=o;Object.keys(I).length>0&&(d=` {${Object.entries(I).map(([F,m])=>typeof m!="object"||m===null||m instanceof Error||m instanceof Date?`${p(F)}=${p(String(m))}`:`${p(F)}=${p(Array.isArray(m)?this.safeStringify(m):this.formatData(m))}`).join(", ")}}`)}let L=`[${l}] [${S}] [${E}] ${u}${p(e)}${d}${g}`;if(this.logFilePath)try{this.appendLine(this.logFilePath,L+`
`)}catch(O){let v=O instanceof Error?O:new Error(String(O));x(`[LOGGER] Failed to write to log file: ${v.message}
${v.stack??""}
`)}else x(L+`
`)}debug(t,r,e,o){this.log(0,t,r,e,o)}info(t,r,e,o){this.log(1,t,r,e,o)}warn(t,r,e,o){this.log(2,t,r,e,o)}setErrorSink(t){_=t}error(t,r,e,o){this.log(3,t,r,e,o),this.routeErrorToSink(r,e,o)}routeErrorToSink(t,r,e){try{if(!_||!(e instanceof Error))return;_(e)}catch{}}dataIn(t,r,e,o){this.info(t,`\u2192 ${r}`,e,o)}dataOut(t,r,e,o){this.info(t,`\u2190 ${r}`,e,o)}success(t,r,e,o){this.info(t,`\u2713 ${r}`,e,o)}failure(t,r,e,o){this.error(t,`\u2717 ${r}`,e,o)}},H=new T;function ut(n){if(!n)return[];try{let t=JSON.parse(n);return Array.isArray(t)?t:[String(t)]}catch(t){return H.debug("DB","File list is not JSON; treating value as a single path",{value:n},t instanceof Error?t:new Error(String(t))),[n]}}function $(n){if(!n)return[];try{let t=JSON.parse(n);return Array.isArray(t)?t.filter(r=>typeof r=="string"&&r.length>0):[]}catch{return[]}}function at(n,t,r,e){n.prepare("DELETE FROM observation_files WHERE observation_id = ?").run(t);let o=new Set([...$(r),...$(e)]);if(o.size===0)return;let i=n.prepare("INSERT OR IGNORE INTO observation_files(path, observation_id) VALUES (?, ?)");for(let l of o)i.run(l,t)}0&&(module.exports={parseFileList,parseIndexedFilePaths,replaceObservationFileIndex});
//# sourceMappingURL=files.js.map