@cachly-dev/mcp-server 0.10.201 → 0.10.203

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/src/index.js CHANGED
@@ -1,69 +1,69 @@
1
1
  #!/usr/bin/env node
2
- import{setzeClientKennung as Bt}from"./herkunft.js";import{jwtExpiryMs as jt,checkJwt as Ht,handleApiError as Ot,diagnoseAuth as _e,planAuthHeal as Pe,readClientCredentialsFromEnv as Pt,buildClientCredentialsBody as Mt,clientCredentialsTokenUrl as Dt}from"./auth.js";import{cachlyUrl as N}from"./cachly-url.js";import{nutzungsSchluessel as Jt}from"./werkzeug-nutzung.js";import{istLieferung as Yt,lieferSchluessel as Ut,tokenDerAntwort as Wt,hatTreffer as Ft,trefferSchluessel as Me}from"./geliefert.js";import{notify as De}from"./notifier.js";import{fileURLToPath as Je}from"node:url";import{createRequire as zt}from"node:module";import{execFile as $e}from"node:child_process";const Kt=process.argv[1]!=null&&(process.argv[1]===Je(import.meta.url)||process.argv[1].endsWith("/dist/index.js")||process.argv[1].endsWith("/src/index.ts"));import{Server as Ye}from"@modelcontextprotocol/sdk/server/index.js";import{StdioServerTransport as qt}from"@modelcontextprotocol/sdk/server/stdio.js";import{StreamableHTTPServerTransport as Gt}from"@modelcontextprotocol/sdk/server/streamableHttp.js";import{CallToolRequestSchema as Ue,ErrorCode as ne,ListPromptsRequestSchema as We,ListResourceTemplatesRequestSchema as Fe,ListResourcesRequestSchema as ze,ListToolsRequestSchema as Ke,McpError as ee}from"@modelcontextprotocol/sdk/types.js";import{Redis as qe}from"ioredis";let x=process.env.CACHLY_API_URL??"https://api.cachly.dev",v=it()??"";ae(v);const Ts=process.env.CACHLY_EMBED_MODEL??"",D=(()=>{try{const e=zt(import.meta.url);for(const t of["../../package.json","../package.json"])try{const s=e(t);if(s.name==="@cachly-dev/mcp-server"&&s.version)return s.version}catch{}}catch{}return"0.10.100"})(),Vt=Number(process.env.CACHLY_PROVISION_TIMEOUT_MS??9e4);let H=Re(process.env.CACHLY_BRAIN_INSTANCE_ID),V=0,ce=null,Ge=!1;async function Zt(){let e=[];try{G.getClientCapabilities()?.roots&&(e=(await G.listRoots(void 0,{timeout:3e3})).roots)}catch{}return Cn({wurzeln:e,env:process.env,cwd:process.cwd(),dateiPfad:Je})}function se(){return Ge?dt(process.env):!1}function Ce(e,t,s){!e||!se()||wn({instanzId:e,vomHook:s,verbindung:P,ausGit:o=>Ie("brain_from_git",o,P,O),projektOrdner:t?async()=>t:Zt,einbetten:o=>Qe(o,{geduld:"lang"}),hatEinbettung:uo,einbettenZweit:o=>Qe(o,{geduld:"lang",modell:po}),melde:o=>A("brain_from_git",{...v?{api_key:v}:{},instance_id:e,total:o,auto:"startwissen"}),startvorrat:o=>Le("brain_seed_starter",o,P,O),meldeStartvorrat:o=>A("brain_seed_starter",{...v?{api_key:v}:{},instance_id:e,auto:!0},{seeded_count:o,auto:!0})})}function Ae(){return se()&&An()}const Qt=7e3,Xt=12e3,eo=18e4;let he=null;function Se(e){return he||(he=to(e).finally(()=>{he=null})),he}async function to(e){try{if(!v){const t=Date.now()+e;for(;!Ee()&&fn({})&&Date.now()<t;)await new Promise(n=>setTimeout(n,pn));const s=Ee();if(!s)return!1;v=s,ae(s);const o=tn();o&&!H&&(H=o,V=0),await le(s),o&&await de(o),A("instant_trial_started",{tool:"sessionstart-hook",...o?{instance_id:o}:{}})}if(se()&&lt()){const t=mn();t&&(Ne(t.instanzId,se(),Ae()),Ce(t.instanzId,t.projekt||void 0,t.vomHook))}}catch{}return!!v}function oo(){if(!dt(process.env))return;const e=Date.now()+eo,t=setInterval(()=>{if(Date.now()>e){clearInterval(t);return}v&&!lt()||Se(0)},500);t.unref(),Se(0)}async function no(){if(!v)return"";try{const e=await fetch(`${x}/api/v1/instances/auto`,{method:"POST",headers:{Authorization:`Bearer ${v}`,"Content-Type":"application/json"},signal:AbortSignal.timeout(12e3)});if(e.ok){const t=await e.json(),s=t.instance?.id??t.instance_id;if(s)return s}else fetch(`${x}/api/v1/telemetry/mcp`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${v}`},body:JSON.stringify({event:"auto_provision_failed",status:e.status,version:D}),signal:AbortSignal.timeout(3e3)}).catch(()=>{})}catch{fetch(`${x}/api/v1/telemetry/mcp`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({event:"auto_provision_failed",status:0,reason:"network_error",version:D}),signal:AbortSignal.timeout(3e3)}).catch(()=>{})}return""}async function Ve(){if(H)return H;if(!v)return"";if(ce)return ce;const e=Date.now();if(V>0&&e-V<3e4)return"";V=e,ce=(async()=>{try{const t=await fetch(`${x}/api/v1/instances`,{headers:{Authorization:`Bearer ${v}`,Accept:"application/json"},signal:AbortSignal.timeout(5e3)});if(t.ok){const o=(await t.json())?.data??[],n=o.find(i=>i.status==="running")??o.find(i=>i.status==="provisioning");if(n)return H=n.id,V=0,H;const c=await no();if(c)return H=c,V=0,Ne(c,se(),Ae()),de(c),Ce(c),H}}catch{}return""})();try{return await ce}finally{ce=null}}let ke=0;const so=6e4;let re="";async function le(e){try{const{writeFile:t,mkdir:s,readFile:o}=await import("node:fs/promises"),{existsSync:n}=await import("node:fs"),{resolve:c,dirname:i}=await import("node:path"),a=process.env.HOME??process.env.USERPROFILE??process.env.APPDATA??"";if(!a)return;const u=c(a,".claude","mcp.json");let g={};if(n(u))try{g=JSON.parse(await o(u,"utf-8"))}catch{}g.mcpServers??={};const l=g.mcpServers.cachly;if(l)l.env??={},l.env.CACHLY_JWT=e;else{const y={};y.CACHLY_JWT=e,g.mcpServers.cachly={command:"npx",args:["-y","@cachly-dev/mcp-server@latest"],env:y}}await s(i(u),{recursive:!0}),await t(u,JSON.stringify(g,null,2),"utf-8");const m=[c(a,".cursor","mcp.json"),c(a,".codeium","windsurf","mcp_config.json")];await Promise.allSettled(m.map(async y=>{if(n(y))try{const w=await o(y,"utf-8"),b=JSON.parse(w),p=b?.mcpServers?.cachly;p?.env&&(p.env.CACHLY_JWT=e,await t(y,JSON.stringify(b,null,2),"utf-8"))}catch{}}))}catch{}}async function de(e){if(e){at(e,{apiKey:v});try{const{writeFile:t,readFile:s}=await import("node:fs/promises"),{existsSync:o}=await import("node:fs"),{resolve:n}=await import("node:path"),c=process.env.HOME??process.env.USERPROFILE??process.env.APPDATA??"";if(!c)return;const i=[n(c,".claude","mcp.json"),n(c,".cursor","mcp.json"),n(c,".codeium","windsurf","mcp_config.json")];await Promise.allSettled(i.map(async a=>{if(o(a))try{const u=JSON.parse(await s(a,"utf-8")),g=u?.mcpServers?.cachly;g?.env&&(g.env.CACHLY_BRAIN_INSTANCE_ID=e,await t(a,JSON.stringify(u,null,2),"utf-8"))}catch{}}))}catch{}}}async function ro(){try{const e=await fetch(`${x}/api/v1/api-keys`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${v}`},body:JSON.stringify({name:"cachly-mcp-selfheal",scope:"read_write"}),signal:AbortSignal.timeout(8e3)});if(!e.ok)return!1;const t=await e.json();return t.key?(v=t.key,ae(t.key),le(t.key),A("auth_self_healed",{reason:"near_expiry_refresh"}),!0):!1}catch{return!1}}async function Te(){const e=_e(v),t=Pe(e);if(t==="none")return re="",!0;if(t==="refresh"){const s=Date.now();return s-ke>=so&&(ke=s,await ro())?(re="",!0):(re=e.message,e.usable)}return re=e.message,!1}function ie(e,t){const s=(i,a)=>{try{t?.(i,a)}catch{}},o=i=>{i?s(!1,i.code??i.message):s(!0)},n=Po(e);if(!n){s(!1,"keine-http-adresse");return}const c=Mo(n);try{process.platform==="win32"?$e("cmd",["/c","start","",c],{windowsHide:!0},o):process.platform==="darwin"?$e("open",[n],o):$e("xdg-open",[n],o)}catch(i){s(!1,i instanceof Error?i.message:"unknown")}}let q=null;async function io(){try{const e=await fetch(`${x}/auth/device`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({}),signal:AbortSignal.timeout(8e3)});if(e.ok){const t=await e.json(),s=t.verification_uri||N("/device","ambient-signin"),o=`${s}${s.includes("?")?"&":"?"}code=${encodeURIComponent(t.user_code)}`;return{deviceCode:t.device_code,userCode:t.user_code,verifyUrl:o,pollInterval:(t.interval??5)*1e3,deadline:Date.now()+600*1e3,polling:!1}}}catch{}try{const t=await fetch("https://auth.cachly.dev/realms/cachly/protocol/openid-connect/auth/device",{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:"client_id=cachly-cli&scope=openid",signal:AbortSignal.timeout(8e3)});if(!t.ok)return null;const s=await t.json();return{deviceCode:s.device_code,userCode:s.user_code,verifyUrl:s.verification_uri_complete,pollInterval:(s.interval??5)*1e3,deadline:Date.now()+600*1e3,polling:!1}}catch{return null}}async function ao(e){if(Date.now()>e.deadline)return"expired";const t=async()=>{const o=await fetch(`${x}/auth/device/token`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({device_code:e.deviceCode}),signal:AbortSignal.timeout(8e3)});return o.ok?o.json():null},s=async()=>(await fetch("https://auth.cachly.dev/realms/cachly/protocol/openid-connect/token",{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:`client_id=cachly-cli&grant_type=urn:ietf:params:oauth:grant-type:device_code&device_code=${e.deviceCode}`,signal:AbortSignal.timeout(8e3)})).json();try{let o=await t().catch(()=>null);if(o||(o=await s().catch(()=>null)),!o)return"pending";if(o.access_token){let n=o.access_token;try{const c=await fetch(`${x}/api/v1/api-keys`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${n}`},body:JSON.stringify({name:"cachly-mcp-smithery",scope:"read_write"}),signal:AbortSignal.timeout(8e3)});if(c.ok){const i=await c.json();i.key&&(n=i.key)}}catch{}return v=n,ae(n),q=null,A("device_flow_completed"),le(n),Z(n),V=0,await Ve(),H&&de(H),"done"}return o.error==="slow_down"&&(e.pollInterval=Math.min(e.pollInterval+2e3,15e3)),"pending"}catch{return"pending"}}async function Ze(e){if(e.polling)return"pending";e.polling=!0;try{return await ao(e)}finally{e.polling=!1}}function co(e,t=Ze,s=n=>q===n,o=lo){const n=async()=>{if(!s(e))return;let c="pending";try{c=await t(e)}catch{}if(c!=="done"){if(c==="expired"){s(e)&&(q=null);return}s(e)&&o(n,e.pollInterval)}};o(n,e.pollInterval)}function lo(e,t){const s=setTimeout(e,t);typeof s.unref=="function"&&s.unref()}import{setEmbedJwt as ae,computeEmbedding as Qe,hasEmbedProvider as uo,EMBED_PROVIDER as Xe}from"./embeddings.js";import{ZWEIT_MODELL as po}from"./rangfolge-stellschrauben.js";import{tokenize as mo,splitMultiQuery as go,levenshtein as fo,recencyBoost as ho,extractTimestamp as yo,STOPWORDS as bo,katakanaToRomaji as vo,arabicLightStem as wo,expandCrossLingual as xo,CROSS_LINGUAL_MAP as _o,ZERO_RESULTS_LOG as $o,zeroResultsTotal as Co}from"./search.js";const te=new Map;function Ao(e){return e<=1?200:null}async function P(e){if(!e)throw new ee(ne.InvalidRequest,`\u23F3 Your Brain instance is still being set up.
2
+ import{setzeClientKennung as Nt}from"./herkunft.js";import{jwtExpiryMs as Ht,checkJwt as Mt,handleApiError as Dt,diagnoseAuth as Se,planAuthHeal as Ue,readClientCredentialsFromEnv as Ut,buildClientCredentialsBody as Jt,clientCredentialsTokenUrl as zt}from"./auth.js";import{cachlyUrl as j}from"./cachly-url.js";import{nutzungsSchluessel as Ft}from"./werkzeug-nutzung.js";import{istLieferung as Wt,lieferSchluessel as Yt,tokenDerAntwort as Kt,hatTreffer as qt,trefferSchluessel as Je}from"./geliefert.js";import{notify as ze}from"./notifier.js";import{fileURLToPath as Fe}from"node:url";import{createRequire as Gt}from"node:module";import{execFile as Ae}from"node:child_process";const Vt=process.argv[1]!=null&&(process.argv[1]===Fe(import.meta.url)||process.argv[1].endsWith("/dist/index.js")||process.argv[1].endsWith("/src/index.ts"));import{Server as We}from"@modelcontextprotocol/sdk/server/index.js";import{StdioServerTransport as Zt}from"@modelcontextprotocol/sdk/server/stdio.js";import{StreamableHTTPServerTransport as Qt}from"@modelcontextprotocol/sdk/server/streamableHttp.js";import{CallToolRequestSchema as Ye,ErrorCode as ie,ListPromptsRequestSchema as Ke,ListResourceTemplatesRequestSchema as qe,ListResourcesRequestSchema as Ge,ListToolsRequestSchema as Ve,McpError as te}from"@modelcontextprotocol/sdk/types.js";import{Redis as Ze}from"ioredis";let x=process.env.CACHLY_API_URL??"https://api.cachly.dev",v=Le()??"";le(v);const js=process.env.CACHLY_EMBED_MODEL??"",D=(()=>{try{const e=Gt(import.meta.url);for(const t of["../../package.json","../package.json"])try{const s=e(t);if(s.name==="@cachly-dev/mcp-server"&&s.version)return s.version}catch{}}catch{}return"0.10.100"})(),Xt=Number(process.env.CACHLY_PROVISION_TIMEOUT_MS??9e4);let P=xe(process.env.CACHLY_BRAIN_INSTANCE_ID);function oe(){return v||Le()||""}function F(){return xe(process.env.CACHLY_BRAIN_INSTANCE_ID)||ut()||""}let Z=0,de=null,Qe=!1;async function eo(){let e=[];try{V.getClientCapabilities()?.roots&&(e=(await V.listRoots(void 0,{timeout:3e3})).roots)}catch{}return En({wurzeln:e,env:process.env,cwd:process.cwd(),dateiPfad:Fe})}function re(){return Qe?gt(process.env):!1}function Ce(e,t,s){!e||!re()||Sn({instanzId:e,vomHook:s,verbindung:H,ausGit:n=>je("brain_from_git",n,H,N),projektOrdner:t?async()=>t:eo,einbetten:n=>tt(n,{geduld:"lang"}),hatEinbettung:go,einbettenZweit:n=>tt(n,{geduld:"lang",modell:fo}),melde:n=>S("brain_from_git",{...v?{api_key:v}:{},instance_id:e,total:n,auto:"startwissen"}),startvorrat:n=>Ne("brain_seed_starter",n,H,N),meldeStartvorrat:n=>S("brain_seed_starter",{...v?{api_key:v}:{},instance_id:e,auto:!0},{seeded_count:n,auto:!0})})}function Te(){return re()&&In()}const to=7e3,oo=12e3,no=18e4;let ve=null;function Ee(e){return ve||(ve=so(e).finally(()=>{ve=null})),ve}async function so(e){try{if(!v){const t=Date.now()+e;for(;!Oe()&&wn({})&&Date.now()<t;)await new Promise(o=>setTimeout(o,yn));const s=Oe();if(!s)return!1;v=s,le(s);const n=ut();n&&!P&&(P=n,Z=0),await ue(s),n&&await pe(n),S("instant_trial_started",{tool:"sessionstart-hook",...n?{instance_id:n}:{}})}if(re()&&mt()){const t=bn();t&&(Pe(t.instanzId,re(),Te()),Ce(t.instanzId,t.projekt||void 0,t.vomHook))}}catch{}return!!v}function io(){if(!gt(process.env))return;const e=Date.now()+no,t=setInterval(()=>{if(Date.now()>e){clearInterval(t);return}v&&!mt()||Ee(0)},500);t.unref(),Ee(0)}async function ro(){if(!v)return"";try{const e=await fetch(`${x}/api/v1/instances/auto`,{method:"POST",headers:{Authorization:`Bearer ${v}`,"Content-Type":"application/json"},signal:AbortSignal.timeout(12e3)});if(e.ok){const t=await e.json(),s=t.instance?.id??t.instance_id;if(s)return s}else fetch(`${x}/api/v1/telemetry/mcp`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${v}`},body:JSON.stringify({event:"auto_provision_failed",status:e.status,version:D}),signal:AbortSignal.timeout(3e3)}).catch(()=>{})}catch{fetch(`${x}/api/v1/telemetry/mcp`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({event:"auto_provision_failed",status:0,reason:"network_error",version:D}),signal:AbortSignal.timeout(3e3)}).catch(()=>{})}return""}async function Xe(){if(P)return P;if(!v)return"";if(de)return de;const e=Date.now();if(Z>0&&e-Z<3e4)return"";Z=e,de=(async()=>{try{const t=await fetch(`${x}/api/v1/instances`,{headers:{Authorization:`Bearer ${v}`,Accept:"application/json"},signal:AbortSignal.timeout(5e3)});if(t.ok){const n=(await t.json())?.data??[],o=n.find(r=>r.status==="running")??n.find(r=>r.status==="provisioning");if(o)return P=o.id,Z=0,P;const c=await ro();if(c)return P=c,Z=0,Pe(c,re(),Te()),pe(c),Ce(c),P}}catch{}return""})();try{return await de}finally{de=null}}let Ie=0;const ao=6e4;let ae="";async function ue(e){try{const{writeFile:t,mkdir:s,readFile:n}=await import("node:fs/promises"),{existsSync:o}=await import("node:fs"),{resolve:c,dirname:r}=await import("node:path"),a=process.env.HOME??process.env.USERPROFILE??process.env.APPDATA??"";if(!a)return;const u=c(a,".claude","mcp.json");let g={};if(o(u))try{g=JSON.parse(await n(u,"utf-8"))}catch{}g.mcpServers??={};const l=g.mcpServers.cachly;if(l)l.env??={},l.env.CACHLY_JWT=e;else{const h={};h.CACHLY_JWT=e,g.mcpServers.cachly={command:"npx",args:["-y","@cachly-dev/mcp-server@latest"],env:h}}await s(r(u),{recursive:!0}),await t(u,JSON.stringify(g,null,2),"utf-8");const m=[c(a,".cursor","mcp.json"),c(a,".codeium","windsurf","mcp_config.json")];await Promise.allSettled(m.map(async h=>{if(o(h))try{const w=await n(h,"utf-8"),b=JSON.parse(w),p=b?.mcpServers?.cachly;p?.env&&(p.env.CACHLY_JWT=e,await t(h,JSON.stringify(b,null,2),"utf-8"))}catch{}}))}catch{}}async function pe(e){if(e){dt(e,{apiKey:v});try{const{writeFile:t,readFile:s}=await import("node:fs/promises"),{existsSync:n}=await import("node:fs"),{resolve:o}=await import("node:path"),c=process.env.HOME??process.env.USERPROFILE??process.env.APPDATA??"";if(!c)return;const r=[o(c,".claude","mcp.json"),o(c,".cursor","mcp.json"),o(c,".codeium","windsurf","mcp_config.json")];await Promise.allSettled(r.map(async a=>{if(n(a))try{const u=JSON.parse(await s(a,"utf-8")),g=u?.mcpServers?.cachly;g?.env&&(g.env.CACHLY_BRAIN_INSTANCE_ID=e,await t(a,JSON.stringify(u,null,2),"utf-8"))}catch{}}))}catch{}}}async function co(){try{const e=await fetch(`${x}/api/v1/api-keys`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${v}`},body:JSON.stringify({name:"cachly-mcp-selfheal",scope:"read_write"}),signal:AbortSignal.timeout(8e3)});if(!e.ok)return!1;const t=await e.json();return t.key?(v=t.key,le(t.key),ue(t.key),S("auth_self_healed",{reason:"near_expiry_refresh"}),!0):!1}catch{return!1}}async function Re(){const e=Se(v),t=Ue(e);if(t==="none")return ae="",!0;if(t==="refresh"){const s=Date.now();return s-Ie>=ao&&(Ie=s,await co())?(ae="",!0):(ae=e.message,e.usable)}return ae=e.message,!1}function ce(e,t){const s=(r,a)=>{try{t?.(r,a)}catch{}},n=r=>{r?s(!1,r.code??r.message):s(!0)},o=Fo(e);if(!o){s(!1,"keine-http-adresse");return}const c=Wo(o);try{process.platform==="win32"?Ae("cmd",["/c","start","",c],{windowsHide:!0},n):process.platform==="darwin"?Ae("open",[o],n):Ae("xdg-open",[o],n)}catch(r){s(!1,r instanceof Error?r.message:"unknown")}}let G=null;async function lo(){try{const e=await fetch(`${x}/auth/device`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({}),signal:AbortSignal.timeout(8e3)});if(e.ok){const t=await e.json(),s=t.verification_uri||j("/device","ambient-signin"),n=`${s}${s.includes("?")?"&":"?"}code=${encodeURIComponent(t.user_code)}`;return{deviceCode:t.device_code,userCode:t.user_code,verifyUrl:n,pollInterval:(t.interval??5)*1e3,deadline:Date.now()+600*1e3,polling:!1}}}catch{}try{const t=await fetch("https://auth.cachly.dev/realms/cachly/protocol/openid-connect/auth/device",{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:"client_id=cachly-cli&scope=openid",signal:AbortSignal.timeout(8e3)});if(!t.ok)return null;const s=await t.json();return{deviceCode:s.device_code,userCode:s.user_code,verifyUrl:s.verification_uri_complete,pollInterval:(s.interval??5)*1e3,deadline:Date.now()+600*1e3,polling:!1}}catch{return null}}async function uo(e){if(Date.now()>e.deadline)return"expired";const t=async()=>{const n=await fetch(`${x}/auth/device/token`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({device_code:e.deviceCode}),signal:AbortSignal.timeout(8e3)});return n.ok?n.json():null},s=async()=>(await fetch("https://auth.cachly.dev/realms/cachly/protocol/openid-connect/token",{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:`client_id=cachly-cli&grant_type=urn:ietf:params:oauth:grant-type:device_code&device_code=${e.deviceCode}`,signal:AbortSignal.timeout(8e3)})).json();try{let n=await t().catch(()=>null);if(n||(n=await s().catch(()=>null)),!n)return"pending";if(n.access_token){let o=n.access_token;try{const c=await fetch(`${x}/api/v1/api-keys`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${o}`},body:JSON.stringify({name:"cachly-mcp-smithery",scope:"read_write"}),signal:AbortSignal.timeout(8e3)});if(c.ok){const r=await c.json();r.key&&(o=r.key)}}catch{}return v=o,le(o),G=null,S("device_flow_completed"),ue(o),Q(o),Z=0,await Xe(),P&&pe(P),"done"}return n.error==="slow_down"&&(e.pollInterval=Math.min(e.pollInterval+2e3,15e3)),"pending"}catch{return"pending"}}async function et(e){if(e.polling)return"pending";e.polling=!0;try{return await uo(e)}finally{e.polling=!1}}function po(e,t=et,s=o=>G===o,n=mo){const o=async()=>{if(!s(e))return;let c="pending";try{c=await t(e)}catch{}if(c!=="done"){if(c==="expired"){s(e)&&(G=null);return}s(e)&&n(o,e.pollInterval)}};n(o,e.pollInterval)}function mo(e,t){const s=setTimeout(e,t);typeof s.unref=="function"&&s.unref()}import{setEmbedJwt as le,computeEmbedding as tt,hasEmbedProvider as go,EMBED_PROVIDER as ot}from"./embeddings.js";import{ZWEIT_MODELL as fo}from"./rangfolge-stellschrauben.js";import{tokenize as ho,splitMultiQuery as yo,levenshtein as bo,recencyBoost as vo,extractTimestamp as wo,STOPWORDS as xo,katakanaToRomaji as _o,arabicLightStem as $o,expandCrossLingual as ko,CROSS_LINGUAL_MAP as So,ZERO_RESULTS_LOG as Ao,zeroResultsTotal as Co}from"./search.js";const ne=new Map;function To(e){return e<=1?200:null}async function H(e){if(!e)throw new te(ie.InvalidRequest,`\u23F3 Your Brain instance is still being set up.
3
3
 
4
4
  This usually resolves in a few seconds \u2014 just call the tool again and it will connect automatically.
5
5
 
6
6
  If it keeps happening:
7
7
  \u2022 Run \`list_instances\` to check your instances, or
8
8
  \u2022 Set CACHLY_BRAIN_INSTANCE_ID in your MCP config, or
9
- \u2022 Reach us at support@cachly.dev.`);const t=te.get(e);return t||bn(So,e,()=>ko(e))}const So=new Map;async function ko(e){W("verbindung: Instanz abfragen");let t=await O(`/api/v1/instances/${e}`);if(W(`verbindung: Instanz ${t.status}`),t=await yn(t,()=>O(`/api/v1/instances/${e}`),{fristMs:Vt,melde:i=>W(`verbindung: Instanz ${i.status}`)}),t.status!=="running"){fetch(`${x}/api/v1/telemetry/mcp`,{method:"POST",headers:{"Content-Type":"application/json",...v?{Authorization:`Bearer ${v}`}:{}},body:JSON.stringify({event:"instance_not_reachable",instance_id:e,status:t.status,version:D}),signal:AbortSignal.timeout(3e3)}).catch(()=>{});let i;throw t.status==="provisioning"?i=`\u23F3 Brain instance "${t.name}" is still starting up.
9
+ \u2022 Reach us at support@cachly.dev.`);const t=ne.get(e);return t||$n(Eo,e,()=>Io(e))}const Eo=new Map;async function Io(e){W("verbindung: Instanz abfragen");let t=await N(`/api/v1/instances/${e}`);if(W(`verbindung: Instanz ${t.status}`),t=await _n(t,()=>N(`/api/v1/instances/${e}`),{fristMs:Xt,melde:r=>W(`verbindung: Instanz ${r.status}`)}),t.status!=="running"){fetch(`${x}/api/v1/telemetry/mcp`,{method:"POST",headers:{"Content-Type":"application/json",...v?{Authorization:`Bearer ${v}`}:{}},body:JSON.stringify({event:"instance_not_reachable",instance_id:e,status:t.status,version:D}),signal:AbortSignal.timeout(3e3)}).catch(()=>{});let r;throw t.status==="provisioning"?r=`\u23F3 Brain instance "${t.name}" is still starting up.
10
10
 
11
- First-time provisioning typically takes 1\u20133 minutes. Please retry in a moment \u2014 the instance will be ready soon.`:t.status==="failed"?i=`\u274C Brain instance "${t.name}" failed to start.
11
+ First-time provisioning typically takes 1\u20133 minutes. Please retry in a moment \u2014 the instance will be ready soon.`:t.status==="failed"?r=`\u274C Brain instance "${t.name}" failed to start.
12
12
 
13
- Our system will retry automatically. Check status at: ${N("/instances","instance-error")}
14
- If this persists, contact support@cachly.dev.`:t.status==="suspended"?i=`\u23F8 Brain instance "${t.name}" is suspended (billing issue).
13
+ Our system will retry automatically. Check status at: ${j("/instances","instance-error")}
14
+ If this persists, contact support@cachly.dev.`:t.status==="suspended"?r=`\u23F8 Brain instance "${t.name}" is suspended (billing issue).
15
15
 
16
- Update your payment method at: ${N("/billing","upgrade")}`:t.status==="pending_payment"?i=`\u{1F4B3} Brain instance "${t.name}" is waiting for payment.
16
+ Update your payment method at: ${j("/billing","upgrade")}`:t.status==="pending_payment"?r=`\u{1F4B3} Brain instance "${t.name}" is waiting for payment.
17
17
 
18
- Complete your checkout at: ${N("/instances","upgrade")}`:i=`Brain instance "${t.name}" is not reachable (status: ${t.status}).
18
+ Complete your checkout at: ${j("/instances","upgrade")}`:r=`Brain instance "${t.name}" is not reachable (status: ${t.status}).
19
19
 
20
- \u2022 View your instance at: ${N("/instances","instance-error")}
21
- \u2022 Run \`get_api_status\` for a full diagnostic.`,new ee(ne.InvalidRequest,i)}if(!t.host||!t.port)throw new ee(ne.InternalError,`Instance "${t.name}" is running but has no host/port \u2014 contact support@cachly.dev.`);let s,o=t.tls_enabled!==!1;try{const i=await O(`/api/v1/instances/${e}/connection`);s=i.password??void 0,o=i.tls_enabled!==!1,W("verbindung: Zugangsdaten da")}catch(i){const a=i instanceof Error?i.message:String(i);if(a.includes("401")||a.includes("403")||a.includes("Unauthorized"))throw new ee(ne.InvalidRequest,"API key rejected \u2014 run `cachly setup` to refresh your credentials.")}const n=new qe({host:t.host,port:t.port,password:s||void 0,commandTimeout:8e3,...o?{tls:{}}:{},lazyConnect:!0,enableReadyCheck:!0,connectTimeout:8e3,retryStrategy:Ao,maxRetriesPerRequest:1,keepAlive:15e3}),c=()=>{te.delete(e),n.disconnect()};n.on("error",c),n.on("end",c);try{await n.connect()}catch(i){n.disconnect();const a=i instanceof Error?i.message:String(i);throw fetch(`${x}/api/v1/telemetry/mcp`,{method:"POST",headers:{"Content-Type":"application/json",...v?{Authorization:`Bearer ${v}`}:{}},body:JSON.stringify({event:"redis_connect_failed",instance_id:e,host:t.host,port:t.port,error:a,version:D}),signal:AbortSignal.timeout(3e3)}).catch(()=>{}),new ee(ne.InternalError,`Could not reach Brain instance "${t.name}" (${t.host}:${t.port}).
20
+ \u2022 View your instance at: ${j("/instances","instance-error")}
21
+ \u2022 Run \`get_api_status\` for a full diagnostic.`,new te(ie.InvalidRequest,r)}if(!t.host||!t.port)throw new te(ie.InternalError,`Instance "${t.name}" is running but has no host/port \u2014 contact support@cachly.dev.`);let s,n=t.tls_enabled!==!1;try{const r=await N(`/api/v1/instances/${e}/connection`);s=r.password??void 0,n=r.tls_enabled!==!1,W("verbindung: Zugangsdaten da")}catch(r){const a=r instanceof Error?r.message:String(r);if(a.includes("401")||a.includes("403")||a.includes("Unauthorized"))throw new te(ie.InvalidRequest,"API key rejected \u2014 run `cachly setup` to refresh your credentials.")}const o=new Ze({host:t.host,port:t.port,password:s||void 0,commandTimeout:8e3,...n?{tls:{}}:{},lazyConnect:!0,enableReadyCheck:!0,connectTimeout:8e3,retryStrategy:To,maxRetriesPerRequest:1,keepAlive:15e3}),c=()=>{ne.delete(e),o.disconnect()};o.on("error",c),o.on("end",c);try{await o.connect()}catch(r){o.disconnect();const a=r instanceof Error?r.message:String(r);throw fetch(`${x}/api/v1/telemetry/mcp`,{method:"POST",headers:{"Content-Type":"application/json",...v?{Authorization:`Bearer ${v}`}:{}},body:JSON.stringify({event:"redis_connect_failed",instance_id:e,host:t.host,port:t.port,error:a,version:D}),signal:AbortSignal.timeout(3e3)}).catch(()=>{}),new te(ie.InternalError,`Could not reach Brain instance "${t.name}" (${t.host}:${t.port}).
22
22
 
23
23
  \u2022 Check your network connection
24
24
  \u2022 Run \`get_api_status\` to verify instance status
25
- \u2022 Technical detail: ${a}`)}return te.set(e,n),W("verbindung: Valkey verbunden"),n}async function O(e,t={},s=!1){s||Pe(_e(v))!=="none"&&await Te(),Ht(v);const o=Number(process.env.CACHLY_API_TIMEOUT_MS??15e3),n=await fetch(`${x}${e}`,{...t,signal:AbortSignal.timeout(o),headers:{"Content-Type":"application/json",Authorization:`Bearer ${v}`,...t.headers}});if(!n.ok){if(n.status===401&&!s&&(ke=0,await Te()))return O(e,t,!0);const i=(await n.json().catch(()=>({error:n.statusText}))).error??n.statusText;Ot(n.status,i)}if(n.status!==204)return n.json()}import{handleBrainTool as To}from"./handlers/brain.js";import{handleContextTool as Io}from"./handlers/context.js";import{handleInstanceTool as Eo}from"./handlers/instances.js";import{handleCacheTool as Ro}from"./handlers/cache.js";import{handleTeamTool as No}from"./handlers/team.js";import{handleRoadmapTool as Lo}from"./handlers/roadmap.js";import{handleAdvancedTool as Bo}from"./handlers/advanced.js";import{handleSyndicateTool as jo}from"./handlers/syndicate.js";import{handleFedbrainTool as Ie,_lastBrainFromGitCounts as ye}from"./handlers/fedbrain.js";import{extractFirstRecallProof as Ho,renderFirstRecallProof as Oo}from"./first-recall-proof.js";import{installClsPostCommitHook as et,CLS_HOOK_VERSION as tt}from"./cls-hook.js";import{gitLesen as ot}from"./git-ohne-shell.js";import{browserAdresse as Po,fuerCmd as Mo}from"./browser-adresse.js";import{installAmbientHooks as nt,installCursorHooks as Do,installCopilotHooks as Jo,installCodexHooks as Yo,installGeminiHooks as Uo,AMBIENT_HOOK_VERSION as st,COPILOT_HOOKS_DATEI as Wo,CODEX_HOOKS_DATEI as Fo,GEMINI_SETTINGS_DATEI as zo}from"./ambient-hooks.js";import{hookHauptlauf as Ko,auffrischenHauptlauf as qo,startwissenImportHauptlauf as Go}from"./ambient-hook.js";import{STARTWISSEN_IMPORT as Vo}from"./startwissen-hook.js";import{rahmeAntwort as Zo}from"./antwort-rahmen.js";import{appendLedgerEntry as Qo,readLedger as rt,defaultLedgerPath as Xo,reportLedgerEntry as en}from"./ambient-ledger.js";import{resolveApiKey as it,saveApiKey as Z,saveInstanceId as at,readSavedApiKey as Ee,readInstanceId as tn}from"./credentials.js";import{KEYCLOAK_BASIS as ct,istTestSchluessel as on,waehleTestSchluessel as nn,starteKeycloakAnmeldung as sn,warteAufKeycloakAnmeldung as rn,uebernimm as an,brainImKonto as cn,erfolgsText as ln,fehlerText as dn,ersetzeSchluessel as un}from"./uebernahme.js";import{WARTE_TAKT_MS as pn,auftragLiegt as lt,nimmAuftrag as mn,sichereKonto as gn,sperreLiegt as fn,startwissenEingeschaltet as dt}from"./konto-anlegen.js";import{sichereZugang as hn,echterWert as Re}from"./zugang.js";import{zeitpunkt as W}from"./zeitmessung.js";import{warteAufLaufendeInstanz as yn,gemeinsamerAufbau as bn}from"./instanz-warten.js";import{merkeNeuesBrain as Ne,startetLautLetztemBlick as vn}from"./leeres-brain.js";import{starteStartwissen as wn,startwissenGestartet as xn,beanspruchStartwissen as _n,holeStartwissenHinweis as $n,waehleProjektOrdner as Cn,startvorratErlaubt as An}from"./startwissen.js";import{eigeneFassung as Sn,pruefeNeueFassung as kn,holeNeueFassungHinweis as Tn}from"./neue-fassung.js";import{beiStdinEnde as In}from"./stdin-ende.js";import{merkeWerkzeugAufruf as En,starteBrandWachhund as Rn}from"./brand-wachhund.js";import{sichtbareWerkzeuge as Nn,VERTEILER as ut}from"./werkzeug-auswahl.js";import{buildLessonsMarkdown as Ln,buildLessonsJsonl as Bn,vonRohLektion as jn}from"./brain-export.js";import{detectEditor as Hn}from"./editor.js";import{milestoneSent as pt,markMilestoneSent as mt}from"./funnel-milestones.js";import{kostprobeUebrig as On,kostprobeVerbrauchen as Pn,kostprobeHinweis as Mn,schrankeNachKostproben as Dn}from"./kostprobe.js";import{netBalance as Jn}from"./ambient-recall.js";import{handleShareTool as Le}from"./handlers/share.js";import{handleVizTool as Yn}from"./handlers/viz.js";import{commitArt as Un,istRueckbau as Wn,zaehleLektionen as Fn}from"./commit-art.js";import{TOOLS as ue}from"./tools.js";let gt=!1,be=!1;function ft(){return Hn()}function zn(e){if(!e)return null;const t=e.match(/\*\*💡\s+([^*]+)\*\*/),s=e.match(/^>\s+(.+)$/m);if(!t||!s)return null;const o=t[1].trim(),n=s[1].trim();return!o||!n?null:{topic:o,what:n}}function Kn(e){try{const t=e.split(".");if(t.length!==3)return;const s=JSON.parse(Buffer.from(t[1],"base64url").toString("utf-8"));if(!s.sub)return;let o=2166136261;for(let n=0;n<s.sub.length;n++)o^=s.sub.charCodeAt(n),o=o*16777619>>>0;return o.toString(16).padStart(8,"0")+s.sub.length.toString(16).padStart(4,"0")}catch{return}}function A(e,t,s){if(process.env.CACHLY_NO_TELEMETRY==="1")return;const o=v?Kn(v):void 0;fetch(`${x}/api/v1/telemetry/mcp`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({event:e,version:D,editor:ft(),...v?{jwt:v}:{},...o?{user_fingerprint:o}:{},...s?{metrics:s}:{},...t}),signal:AbortSignal.timeout(3e3)}).catch(()=>{})}function qn(e,t){return en({apiUrl:x,jwt:v,instanceId:e},t)}async function Gn(e){if(!gt&&process.env.CACHLY_NO_TELEMETRY!=="1"){gt=!0;try{await fetch(`${x}/api/v1/telemetry/mcp`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({event:"first_call_no_jwt",version:D,editor:ft(),tool:e}),signal:AbortSignal.timeout(3e3)})}catch{}}}const Vn=new Set(["causal_trace","brain_predict","brain_plan","brain_predict_failures","team_learn","team_confirm","team_recall","team_synthesize","team_crystallize","global_learn","global_recall"]),Zn={causal_trace:"Causal root-cause tracing \u2014 follow the failure chain to the real cause, not just the symptom.",brain_predict:"Predictive risk \u2014 your Brain warns you what will break *before* you run it.",brain_plan:"Generative planning \u2014 turn a goal into an ordered, risk-aware plan from your own history.",brain_predict_failures:"Failure prediction \u2014 catch CI/build breakage before it happens.",team_learn:"Team Brain \u2014 one shared memory across your whole team.",team_confirm:"Team Brain \u2014 peer + senior review that raises lesson trust.",team_recall:"Team Brain \u2014 recall everything your teammates have already solved.",team_synthesize:"Team Brain \u2014 synthesize collective knowledge across authors.",team_crystallize:"Team Brain \u2014 compress your team's wisdom into a shareable crystal.",global_learn:"Team Brain \u2014 contribute to your organization's shared memory.",global_recall:"Team Brain \u2014 recall across your entire organization."},ht=N("/billing","upgrade"),yt=new Map;async function Qn(e){const t=yt.get(e);if(t&&t.expiresAt>Date.now())return t.info;try{const o=((await O(`/api/v1/instances/${e}`))?.tier??"free").toLowerCase(),n={tier:o,isFree:o==="free"||o===""};return yt.set(e,{info:n,expiresAt:Date.now()+6e4}),n}catch{return{tier:"unknown",isFree:!1}}}async function Xn(e,t){if(!Vn.has(e)||!t)return null;const{isFree:s}=await Qn(t);if(!s)return null;const o=Zn[e]??"A Premium intelligence feature.";if(On(e)){const n=Pn(e);return{kostprobe:Mn(e,n,ht)}}return{sperre:Dn(e,o,ht)}}async function es(e,t){try{await(await P(e)).hincrby(Jt(e),t,1)}catch{}}async function ts(e,t,s){if(Yt(t))try{const o=await P(e),n=Ft(s);await o.incrby(Ut(e),n?Wt(s):0),await o.hincrby(Me(e),"abrufe",1),n&&await o.hincrby(Me(e),"treffer",1)}catch{}}async function os(e){const t=await io();return t?(q=t,A("device_flow_started",{tool:e}),ie(t.verifyUrl,(s,o)=>{if(s){A("device_browser_opened",{stelle:"tool"});return}A("device_browser_failed",{reason:o??"unknown",stelle:"tool"}),process.stderr.write(`
26
- cachly: could not open a browser (${o??"unknown"}).
25
+ \u2022 Technical detail: ${a}`)}return ne.set(e,o),W("verbindung: Valkey verbunden"),o}async function N(e,t={},s=!1){s||Ue(Se(v))!=="none"&&await Re(),Mt(v);const n=Number(process.env.CACHLY_API_TIMEOUT_MS??15e3),o=await fetch(`${x}${e}`,{...t,signal:AbortSignal.timeout(n),headers:{"Content-Type":"application/json",Authorization:`Bearer ${v}`,...t.headers}});if(!o.ok){if(o.status===401&&!s&&(Ie=0,await Re()))return N(e,t,!0);const r=(await o.json().catch(()=>({error:o.statusText}))).error??o.statusText;Dt(o.status,r)}if(o.status!==204)return o.json()}import{handleBrainTool as Ro}from"./handlers/brain.js";import{handleContextTool as jo}from"./handlers/context.js";import{handleInstanceTool as Bo}from"./handlers/instances.js";import{handleCacheTool as Lo}from"./handlers/cache.js";import{handleTeamTool as Oo}from"./handlers/team.js";import{handleRoadmapTool as Po}from"./handlers/roadmap.js";import{handleAdvancedTool as No}from"./handlers/advanced.js";import{handleSyndicateTool as Ho}from"./handlers/syndicate.js";import{handleFedbrainTool as je,_lastBrainFromGitCounts as we}from"./handlers/fedbrain.js";import{extractFirstRecallProof as Mo,renderFirstRecallProof as Do}from"./first-recall-proof.js";import{installClsPostCommitHook as nt,CLS_HOOK_VERSION as st}from"./cls-hook.js";import{CLAUDE_MD_MARKER_START as Be,CLAUDE_MD_MARKER_END as it,EDITOR_FILES as me,ANWEISUNGS_DATEIEN as Uo,blockMuster as Jo}from"./projektdateien.js";import{uninstallBefehl as zo}from"./uninstall.js";import{gitLesen as rt}from"./git-ohne-shell.js";import{browserAdresse as Fo,fuerCmd as Wo}from"./browser-adresse.js";import{installAmbientHooks as at,installCursorHooks as Yo,installCopilotHooks as Ko,installCodexHooks as qo,installGeminiHooks as Go,AMBIENT_HOOK_VERSION as ct,COPILOT_HOOKS_DATEI as Vo,CODEX_HOOKS_DATEI as Zo,GEMINI_SETTINGS_DATEI as Qo}from"./ambient-hooks.js";import{hookHauptlauf as Xo,auffrischenHauptlauf as en,startwissenImportHauptlauf as tn}from"./ambient-hook.js";import{STARTWISSEN_IMPORT as on}from"./startwissen-hook.js";import{rahmeAntwort as nn}from"./antwort-rahmen.js";import{appendLedgerEntry as sn,readLedger as lt,defaultLedgerPath as rn,reportLedgerEntry as an}from"./ambient-ledger.js";import{resolveApiKey as Le,saveApiKey as Q,saveInstanceId as dt,readSavedApiKey as Oe,readInstanceId as ut}from"./credentials.js";import{KEYCLOAK_BASIS as pt,istTestSchluessel as cn,waehleTestSchluessel as ln,starteKeycloakAnmeldung as dn,warteAufKeycloakAnmeldung as un,uebernimm as pn,brainImKonto as mn,erfolgsText as gn,fehlerText as fn,ersetzeSchluessel as hn}from"./uebernahme.js";import{WARTE_TAKT_MS as yn,auftragLiegt as mt,nimmAuftrag as bn,sichereKonto as vn,sperreLiegt as wn,startwissenEingeschaltet as gt}from"./konto-anlegen.js";import{sichereZugang as xn,echterWert as xe}from"./zugang.js";import{zeitpunkt as W}from"./zeitmessung.js";import{warteAufLaufendeInstanz as _n,gemeinsamerAufbau as $n}from"./instanz-warten.js";import{merkeNeuesBrain as Pe,startetLautLetztemBlick as kn}from"./leeres-brain.js";import{starteStartwissen as Sn,startwissenGestartet as An,beanspruchStartwissen as Cn,holeStartwissenHinweis as Tn,waehleProjektOrdner as En,startvorratErlaubt as In}from"./startwissen.js";import{eigeneFassung as Rn,pruefeNeueFassung as jn,holeNeueFassungHinweis as Bn}from"./neue-fassung.js";import{beiStdinEnde as Ln}from"./stdin-ende.js";import{merkeWerkzeugAufruf as On,starteBrandWachhund as Pn}from"./brand-wachhund.js";import{sichtbareWerkzeuge as Nn,VERTEILER as ft}from"./werkzeug-auswahl.js";import{buildLessonsMarkdown as Hn,buildLessonsJsonl as Mn,vonRohLektion as Dn}from"./brain-export.js";import{detectEditor as Un}from"./editor.js";import{milestoneSent as ht,markMilestoneSent as yt}from"./funnel-milestones.js";import{kostprobeUebrig as Jn,kostprobeVerbrauchen as zn,kostprobeHinweis as Fn,schrankeNachKostproben as Wn}from"./kostprobe.js";import{netBalance as Yn}from"./ambient-recall.js";import{handleShareTool as Ne}from"./handlers/share.js";import{handleVizTool as Kn}from"./handlers/viz.js";import{commitArt as qn,istRueckbau as Gn,zaehleLektionen as Vn}from"./commit-art.js";import{TOOLS as ge}from"./tools.js";let bt=!1,_e=!1;function vt(){return Un()}function Zn(e){if(!e)return null;const t=e.match(/\*\*💡\s+([^*]+)\*\*/),s=e.match(/^>\s+(.+)$/m);if(!t||!s)return null;const n=t[1].trim(),o=s[1].trim();return!n||!o?null:{topic:n,what:o}}function Qn(e){try{const t=e.split(".");if(t.length!==3)return;const s=JSON.parse(Buffer.from(t[1],"base64url").toString("utf-8"));if(!s.sub)return;let n=2166136261;for(let o=0;o<s.sub.length;o++)n^=s.sub.charCodeAt(o),n=n*16777619>>>0;return n.toString(16).padStart(8,"0")+s.sub.length.toString(16).padStart(4,"0")}catch{return}}function S(e,t,s){if(process.env.CACHLY_NO_TELEMETRY==="1")return;const n=v?Qn(v):void 0;fetch(`${x}/api/v1/telemetry/mcp`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({event:e,version:D,editor:vt(),...v?{jwt:v}:{},...n?{user_fingerprint:n}:{},...s?{metrics:s}:{},...t}),signal:AbortSignal.timeout(3e3)}).catch(()=>{})}function Xn(e,t){return an({apiUrl:x,jwt:v,instanceId:e},t)}async function es(e){if(!bt&&process.env.CACHLY_NO_TELEMETRY!=="1"){bt=!0;try{await fetch(`${x}/api/v1/telemetry/mcp`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({event:"first_call_no_jwt",version:D,editor:vt(),tool:e}),signal:AbortSignal.timeout(3e3)})}catch{}}}const ts=new Set(["causal_trace","brain_predict","brain_plan","brain_predict_failures","team_learn","team_confirm","team_recall","team_synthesize","team_crystallize","global_learn","global_recall"]),os={causal_trace:"Causal root-cause tracing \u2014 follow the failure chain to the real cause, not just the symptom.",brain_predict:"Predictive risk \u2014 your Brain warns you what will break *before* you run it.",brain_plan:"Generative planning \u2014 turn a goal into an ordered, risk-aware plan from your own history.",brain_predict_failures:"Failure prediction \u2014 catch CI/build breakage before it happens.",team_learn:"Team Brain \u2014 one shared memory across your whole team.",team_confirm:"Team Brain \u2014 peer + senior review that raises lesson trust.",team_recall:"Team Brain \u2014 recall everything your teammates have already solved.",team_synthesize:"Team Brain \u2014 synthesize collective knowledge across authors.",team_crystallize:"Team Brain \u2014 compress your team's wisdom into a shareable crystal.",global_learn:"Team Brain \u2014 contribute to your organization's shared memory.",global_recall:"Team Brain \u2014 recall across your entire organization."},wt=j("/billing","upgrade"),xt=new Map;async function ns(e){const t=xt.get(e);if(t&&t.expiresAt>Date.now())return t.info;try{const n=((await N(`/api/v1/instances/${e}`))?.tier??"free").toLowerCase(),o={tier:n,isFree:n==="free"||n===""};return xt.set(e,{info:o,expiresAt:Date.now()+6e4}),o}catch{return{tier:"unknown",isFree:!1}}}async function ss(e,t){if(!ts.has(e)||!t)return null;const{isFree:s}=await ns(t);if(!s)return null;const n=os[e]??"A Premium intelligence feature.";if(Jn(e)){const o=zn(e);return{kostprobe:Fn(e,o,wt)}}return{sperre:Wn(e,n,wt)}}async function is(e,t){try{await(await H(e)).hincrby(Ft(e),t,1)}catch{}}async function rs(e,t,s){if(Wt(t))try{const n=await H(e),o=qt(s);await n.incrby(Yt(e),o?Kt(s):0),await n.hincrby(Je(e),"abrufe",1),o&&await n.hincrby(Je(e),"treffer",1)}catch{}}async function as(e){const t=await lo();return t?(G=t,S("device_flow_started",{tool:e}),ce(t.verifyUrl,(s,n)=>{if(s){S("device_browser_opened",{stelle:"tool"});return}S("device_browser_failed",{reason:n??"unknown",stelle:"tool"}),process.stderr.write(`
26
+ cachly: could not open a browser (${n??"unknown"}).
27
27
  cachly: open this yourself to finish signing in:
28
28
  cachly: ${t.verifyUrl}
29
29
  cachly: code ${t.userCode}
30
30
 
31
- `)}),co(t),["\u{1F9E0} **cachly AI Brain \u2014 the user must sign in once.**","","**Show the following link and code to the user verbatim \u2014 they cannot","continue without them.**","",`\u{1F449} **${t.verifyUrl}**`,"",`Code: **${t.userCode}**`,"","A browser may have opened automatically with the code pre-filled. If it","did not, the link above is the whole step.","","Once signed in, the Brain activates on its own \u2014 no further action, no","restart. The next request arrives brain-powered.","",`\u2728 ${ue.length} MCP tools \xB7 GDPR \xB7 EU servers \xB7 free tier, no credit card`].join(`
32
- `)):["\u{1F9E0} **cachly AI Brain \u2014 Setup required**","","Run the setup wizard once in your terminal:"," ```"," npx @cachly-dev/mcp-server@latest autopilot"," ```","",`Or get your API key at: ${N("/setup-ai","ambient-signin")}`,"","\u2728 Free tier includes: 1 Brain instance, persistent memory, semantic search."].join(`
33
- `)}async function ns(e){if(!e)return!0;if(xn(e))return!1;try{return await _n(await P(e),"sitzungsstart")}catch{return!0}}async function U(e,t){if(!v&&await Se(Qt))return U(e,t);if(!v){if(Gn(e),q){const r=await Ze(q);return r==="done"?U(e,t):r==="expired"?(A("device_flow_failed",{reason:"timeout"}),q=null,"\u231B **Authentication timed out.** Please call any tool again to restart the sign-in flow."):["\u23F3 **Signing you in\u2026**","",`Finish in your browser: **${q.verifyUrl}**`,`Code: **${q.userCode}**`,"","This completes automatically once you sign in \u2014 no need to come back here."].join(`
34
- `)}return hn({schluessel:v,konfigurierteInstanz:H,aufrufInstanz:t.instance_id,werkzeugName:e,holeSofortTest:async()=>{W("sofort-test: Anfang");const r=await gn({apiUrl:x,version:D,fristMs:Xt}),d=r.art==="angelegt"?r.test:r.art==="vorhanden"&&r.instanzId?{apiKey:r.apiKey,instanzId:r.instanzId}:null;return W(`sofort-test: ${d?"Brain angelegt":"kein Test"}`),d&&Ne(d.instanzId,se(),Ae()),d},ablagen:{setzeSchluessel:r=>{v=r},setEmbedJwt:ae,saveApiKey:r=>Z(r),persistApiKeyToConfig:le,merkeInstanz:r=>{H=r,V=0},persistInstanceIdToConfig:de},meldeEreignis:A,werkzeug:()=>U(e,t),anmelden:()=>os(e),neuesBrain:Ce})}if(!t.instance_id){const r=await Ve();r&&(t={...t,instance_id:r})}const s=await Xn(e,t.instance_id);let o="";if(s!==null){if("sperre"in s)return A("premium_gate_hit",{tool:e,instance_id:t.instance_id??""}),s.sperre;o=s.kostprobe,A("premium_taste_used",{tool:e,instance_id:t.instance_id??""})}typeof t.instance_id=="string"&&t.instance_id&&es(t.instance_id,e),W(`werkzeug ${e}: Zugang und Tarif geklaert`);const n=await To(e,t,P,O);if(W(`werkzeug ${e}: Brain-Werkzeug fertig`),n!==null){typeof t.instance_id=="string"&&t.instance_id&&ts(t.instance_id,e,String(n??"")),!be&&v&&(be=!0,pt("first_call_success")||(mt("first_call_success"),A("first_call_success",{tool:e,instance_id:t.instance_id??H??""})));const r=t.instance_id??H??"",d=v?{api_key:v,instance_id:r}:{instance_id:r};if(e==="recall_best_solution")A("recall_best_solution",d);else if(e==="learn_from_attempts")A("learn_from_attempts",d);else if(e==="session_start"){A("session_start",d,{born_at:void 0,recalls_total:void 0,starter_seeded:!1});const h=typeof n=="object"&&n!==null&&"content"in n?JSON.stringify(n):String(n??""),T=h.includes("Welcome! Your AI Brain is live.");if(!vn(r)){if(!T&&h.length>100)A("recall_best_solution",d);else if(T&&t.workspace_path&&await ns(r))try{const I=await Ie("brain_from_git",{instance_id:r,repo_path:t.workspace_path,limit:50},P,O);if(I!==null){A("brain_from_git",{...d,...ye??{}}),A("recall_best_solution",d);let k="";if((ye?.total??0)===0)try{const j=await Le("brain_seed_starter",{instance_id:r},P,O);j&&(A("brain_seed_starter",{...d,auto:!0},{seeded_count:16,auto:!0}),k=`
31
+ `)}),po(t),["\u{1F9E0} **cachly AI Brain \u2014 the user must sign in once.**","","**Show the following link and code to the user verbatim \u2014 they cannot","continue without them.**","",`\u{1F449} **${t.verifyUrl}**`,"",`Code: **${t.userCode}**`,"","A browser may have opened automatically with the code pre-filled. If it","did not, the link above is the whole step.","","Once signed in, the Brain activates on its own \u2014 no further action, no","restart. The next request arrives brain-powered.","",`\u2728 ${ge.length} MCP tools \xB7 GDPR \xB7 EU servers \xB7 free tier, no credit card`].join(`
32
+ `)):["\u{1F9E0} **cachly AI Brain \u2014 Setup required**","","Run the setup wizard once in your terminal:"," ```"," npx @cachly-dev/mcp-server@latest autopilot"," ```","",`Or get your API key at: ${j("/setup-ai","ambient-signin")}`,"","\u2728 Free tier includes: 1 Brain instance, persistent memory, semantic search."].join(`
33
+ `)}async function cs(e){if(!e)return!0;if(An(e))return!1;try{return await Cn(await H(e),"sitzungsstart")}catch{return!0}}async function z(e,t){if(!v&&await Ee(to))return z(e,t);if(!v){if(es(e),G){const i=await et(G);return i==="done"?z(e,t):i==="expired"?(S("device_flow_failed",{reason:"timeout"}),G=null,"\u231B **Authentication timed out.** Please call any tool again to restart the sign-in flow."):["\u23F3 **Signing you in\u2026**","",`Finish in your browser: **${G.verifyUrl}**`,`Code: **${G.userCode}**`,"","This completes automatically once you sign in \u2014 no need to come back here."].join(`
34
+ `)}return xn({schluessel:v,konfigurierteInstanz:P,aufrufInstanz:t.instance_id,werkzeugName:e,holeSofortTest:async()=>{W("sofort-test: Anfang");const i=await vn({apiUrl:x,version:D,fristMs:oo}),d=i.art==="angelegt"?i.test:i.art==="vorhanden"&&i.instanzId?{apiKey:i.apiKey,instanzId:i.instanzId}:null;return W(`sofort-test: ${d?"Brain angelegt":"kein Test"}`),d&&Pe(d.instanzId,re(),Te()),d},ablagen:{setzeSchluessel:i=>{v=i},setEmbedJwt:le,saveApiKey:i=>Q(i),persistApiKeyToConfig:ue,merkeInstanz:i=>{P=i,Z=0},persistInstanceIdToConfig:pe},meldeEreignis:S,werkzeug:()=>z(e,t),anmelden:()=>as(e),neuesBrain:Ce})}if(!t.instance_id){const i=await Xe();i&&(t={...t,instance_id:i})}const s=await ss(e,t.instance_id);let n="";if(s!==null){if("sperre"in s)return S("premium_gate_hit",{tool:e,instance_id:t.instance_id??""}),s.sperre;n=s.kostprobe,S("premium_taste_used",{tool:e,instance_id:t.instance_id??""})}typeof t.instance_id=="string"&&t.instance_id&&is(t.instance_id,e),W(`werkzeug ${e}: Zugang und Tarif geklaert`);const o=await Ro(e,t,H,N);if(W(`werkzeug ${e}: Brain-Werkzeug fertig`),o!==null){typeof t.instance_id=="string"&&t.instance_id&&rs(t.instance_id,e,String(o??"")),!_e&&v&&(_e=!0,ht("first_call_success")||(yt("first_call_success"),S("first_call_success",{tool:e,instance_id:t.instance_id??P??""})));const i=t.instance_id??P??"",d=v?{api_key:v,instance_id:i}:{instance_id:i};if(e==="recall_best_solution")S("recall_best_solution",d);else if(e==="learn_from_attempts")S("learn_from_attempts",d);else if(e==="session_start"){S("session_start",d,{born_at:void 0,recalls_total:void 0,starter_seeded:!1});const y=typeof o=="object"&&o!==null&&"content"in o?JSON.stringify(o):String(o??""),T=y.includes("Welcome! Your AI Brain is live.");if(!kn(i)){if(!T&&y.length>100)S("recall_best_solution",d);else if(T&&t.workspace_path&&await cs(i))try{const E=await je("brain_from_git",{instance_id:i,repo_path:t.workspace_path,limit:50},H,N);if(E!==null){S("brain_from_git",{...d,...we??{}}),S("recall_best_solution",d);let C="";if((we?.total??0)===0)try{const O=await Ne("brain_seed_starter",{instance_id:i},H,N);O&&(S("brain_seed_starter",{...d,auto:!0},{seeded_count:16,auto:!0}),C=`
35
35
  ---
36
- `+String(j))}catch{}const E=String(I),$=ye?.total??0,C=$>0?`
37
- \u2705 **Brain bootstrapped from git \u2014 ${$} lesson${$===1?"":"s"} loaded.** Your AI already knows your history.`:"";if(E||k||C)return String(n)+C+`
36
+ `+String(O))}catch{}const I=String(E),$=we?.total??0,k=$>0?`
37
+ \u2705 **Brain bootstrapped from git \u2014 ${$} lesson${$===1?"":"s"} loaded.** Your AI already knows your history.`:"";if(I||C||k)return String(o)+k+`
38
38
  ---
39
- `+E+k}}catch(I){return A("brain_from_git_failed",{...d,reason:I instanceof Error?I.message.slice(0,120):"unknown"}),String(n)+`
39
+ `+I+C}}catch(E){return S("brain_from_git_failed",{...d,reason:E instanceof Error?E.message.slice(0,120):"unknown"}),String(o)+`
40
40
 
41
41
  > \u26A0\uFE0F **Auto-bootstrap from git history failed** \u2014 your Brain is live but starts empty.
42
- > Run \`brain_from_git(instance_id="`+r+'", repo_path="'+String(t.workspace_path)+'")` manually to seed it.\n> Or try `brain_seed_starter` to load 16 universal engineering lessons instantly.'}}}else if(e==="session_end")A("session_end",d);else if(e==="smart_recall"){const h=typeof n=="object"&&n!==null&&"content"in n?JSON.stringify(n):String(n??""),T=h.length>50&&!h.includes("No lessons found")&&!h.includes("no lessons")&&!h.includes("No matches found");if(A("smart_recall",d,{hit:T,topic:String(t.query??"").slice(0,80)}),T&&A("recall_best_solution",d),re&&typeof n=="string")return`> \u26A0\uFE0F **Brain auth degraded** \u2014 ${re.split(`
42
+ > Run \`brain_from_git(instance_id="`+i+'", repo_path="'+String(t.workspace_path)+'")` manually to seed it.\n> Or try `brain_seed_starter` to load 16 universal engineering lessons instantly.'}}}else if(e==="session_end")S("session_end",d);else if(e==="smart_recall"){const y=typeof o=="object"&&o!==null&&"content"in o?JSON.stringify(o):String(o??""),T=y.length>50&&!y.includes("No lessons found")&&!y.includes("no lessons")&&!y.includes("No matches found");if(S("smart_recall",d,{hit:T,topic:String(t.query??"").slice(0,80)}),T&&S("recall_best_solution",d),ae&&typeof o=="string")return`> \u26A0\uFE0F **Brain auth degraded** \u2014 ${ae.split(`
43
43
  `)[0]}
44
44
 
45
- ${n}`}return n}const c=await Io(e,t,P,O);if(c!==null)return!be&&v&&(be=!0,pt("first_call_success")||(mt("first_call_success"),A("first_call_success",{tool:e,instance_id:t.instance_id??H??""}))),c;const i=await Eo(e,t,P,O);if(i!==null)return i;const a=await Ro(e,t,P,O);if(a!==null)return a;const u=r=>o?r+o:r,g=await No(e,t,P,O);if(g!==null)return u(g);const l=await Lo(e,t,P,O);if(l!==null)return l;const m=await Bo(e,t,P,O);if(m!==null)return u(m);const y=await jo(e,t,P,O);if(y!==null){if(e==="brain_predict"){const r=t.instance_id??H??"";A("brain_predict",v?{api_key:v,instance_id:r}:{instance_id:r})}return u(y)}const w=await Le(e,t,P,O);if(w!==null)return w;const b=await Yn(e,t,P,O);if(b!==null)return b;const p=await Ie(e,t,P,O);if(p!==null){const r=t.instance_id??H??"",d=v?{api_key:v,instance_id:r}:{instance_id:r};if(e==="brain_from_git"){const h=ye;A("brain_from_git",{...d,...h??{}},h?{fixes:h.fixes,features:h.features,refactors:h.refactors,total:h.total}:void 0)}else e==="brain_predict_failures"&&A("brain_predict_failures",d);return p}switch(e){case"get_api_status":{let r="unknown";try{const I=await fetch(`${x}/health`,{signal:AbortSignal.timeout(8e3)});if(I.ok){const S=await I.json();r=`${S.status??"ok"} (db: ${S.db??"?"})`}else r=`HTTP ${I.status}`}catch(I){r=`unreachable: ${I.message}`}if(!v)return["\u{1F4E1} **cachly API Status**","",` \u{1F310} API: ${x}`,` \u{1F493} Health: ${r}`," \u{1F511} Auth: \u274C CACHLY_JWT not set","",`\u{1F4A1} Get your API token at ${N("/instances","api-status")} \u2192 Settings \u2192 API Token`].join(`
46
- `);let d="\u274C invalid JWT format";try{const I=v.split(".");if(I.length===3){const S=JSON.parse(Buffer.from(I[1],"base64url").toString("utf-8")),k=S.sub??"(unknown)",E=S.iss??"(unknown)",$=E.includes("keycloak")?"Keycloak":"OIDC",C=S.exp?new Date(S.exp*1e3):null,j=C?C<new Date:!1;d=["\u2705 JWT decoded",` Sub (user ID): ${k}`,` Provider: ${$}`,` Issuer: ${E}`,` Expires: ${C?C.toISOString():"never"} ${j?"\u26A0\uFE0F EXPIRED \u2013 get a new token!":"\u2705"}`].join(`
47
- `)}}catch{d="\u274C JWT decode failed \u2013 check CACHLY_JWT format"}let h="";try{const S=(await O("/api/v1/instances")).data??[];if(S.length===0)h=`
45
+ ${o}`}return o}const c=await jo(e,t,H,N);if(c!==null)return!_e&&v&&(_e=!0,ht("first_call_success")||(yt("first_call_success"),S("first_call_success",{tool:e,instance_id:t.instance_id??P??""}))),c;const r=await Bo(e,t,H,N);if(r!==null)return r;const a=await Lo(e,t,H,N);if(a!==null)return a;const u=i=>n?i+n:i,g=await Oo(e,t,H,N);if(g!==null)return u(g);const l=await Po(e,t,H,N);if(l!==null)return l;const m=await No(e,t,H,N);if(m!==null)return u(m);const h=await Ho(e,t,H,N);if(h!==null){if(e==="brain_predict"){const i=t.instance_id??P??"";S("brain_predict",v?{api_key:v,instance_id:i}:{instance_id:i})}return u(h)}const w=await Ne(e,t,H,N);if(w!==null)return w;const b=await Kn(e,t,H,N);if(b!==null)return b;const p=await je(e,t,H,N);if(p!==null){const i=t.instance_id??P??"",d=v?{api_key:v,instance_id:i}:{instance_id:i};if(e==="brain_from_git"){const y=we;S("brain_from_git",{...d,...y??{}},y?{fixes:y.fixes,features:y.features,refactors:y.refactors,total:y.total}:void 0)}else e==="brain_predict_failures"&&S("brain_predict_failures",d);return p}switch(e){case"get_api_status":{let i="unknown";try{const E=await fetch(`${x}/health`,{signal:AbortSignal.timeout(8e3)});if(E.ok){const A=await E.json();i=`${A.status??"ok"} (db: ${A.db??"?"})`}else i=`HTTP ${E.status}`}catch(E){i=`unreachable: ${E.message}`}if(!v)return["\u{1F4E1} **cachly API Status**","",` \u{1F310} API: ${x}`,` \u{1F493} Health: ${i}`," \u{1F511} Auth: \u274C CACHLY_JWT not set","",`\u{1F4A1} Get your API token at ${j("/instances","api-status")} \u2192 Settings \u2192 API Token`].join(`
46
+ `);let d="\u274C invalid JWT format";try{const E=v.split(".");if(E.length===3){const A=JSON.parse(Buffer.from(E[1],"base64url").toString("utf-8")),C=A.sub??"(unknown)",I=A.iss??"(unknown)",$=I.includes("keycloak")?"Keycloak":"OIDC",k=A.exp?new Date(A.exp*1e3):null,O=k?k<new Date:!1;d=["\u2705 JWT decoded",` Sub (user ID): ${C}`,` Provider: ${$}`,` Issuer: ${I}`,` Expires: ${k?k.toISOString():"never"} ${O?"\u26A0\uFE0F EXPIRED \u2013 get a new token!":"\u2705"}`].join(`
47
+ `)}}catch{d="\u274C JWT decode failed \u2013 check CACHLY_JWT format"}let y="";try{const A=(await N("/api/v1/instances")).data??[];if(A.length===0)y=`
48
48
 
49
- \u{1F9E0} **Brain Instances:** none \u2014 create one at ${N("/instances","api-status")}`;else{const k=[`
49
+ \u{1F9E0} **Brain Instances:** none \u2014 create one at ${j("/instances","api-status")}`;else{const C=[`
50
50
 
51
- \u{1F9E0} **Brain Instances:**`];for(const E of S){const $=E.status==="running"?"\u{1F7E2}":E.status==="provisioning"?"\u{1F7E1}":"\u{1F534}",C=E.id===H?" \u2190 active":"";if(k.push(` ${$} **${E.name}** (\`${E.id}\`) \xB7 ${E.status}${C}`),E.status==="running"&&E.host){const j=te.get(E.id);if(j)try{await j.ping(),k.push(" \u{1F493} Redis: connected")}catch{k.push(" \u26A0\uFE0F Redis: pooled but ping failed \u2014 will reconnect on next use")}else k.push(" \u{1F4A4} Redis: not connected yet (connects on first tool use)")}else E.status==="provisioning"?k.push(" \u23F3 Still provisioning \u2014 check back in ~30 s"):E.status!=="running"&&k.push(` \u274C Not reachable (status: ${E.status})`)}h=k.join(`
52
- `)}}catch(I){h=`
51
+ \u{1F9E0} **Brain Instances:**`];for(const I of A){const $=I.status==="running"?"\u{1F7E2}":I.status==="provisioning"?"\u{1F7E1}":"\u{1F534}",k=I.id===P?" \u2190 active":"";if(C.push(` ${$} **${I.name}** (\`${I.id}\`) \xB7 ${I.status}${k}`),I.status==="running"&&I.host){const O=ne.get(I.id);if(O)try{await O.ping(),C.push(" \u{1F493} Redis: connected")}catch{C.push(" \u26A0\uFE0F Redis: pooled but ping failed \u2014 will reconnect on next use")}else C.push(" \u{1F4A4} Redis: not connected yet (connects on first tool use)")}else I.status==="provisioning"?C.push(" \u23F3 Still provisioning \u2014 check back in ~30 s"):I.status!=="running"&&C.push(` \u274C Not reachable (status: ${I.status})`)}y=C.join(`
52
+ `)}}catch(E){y=`
53
53
 
54
- \u{1F9E0} **Brain Instances:** could not fetch \u2014 ${I.message}`}await Te();const T=_e(v),B=T.state==="long_lived"?"\u{1F6E1}\uFE0F Self-healing: \u2705 long-lived API key (won't expire silently)":T.state==="healthy"?`\u{1F6E1}\uFE0F Self-healing: \u2705 token valid${T.expiresInMs?` (${Math.round(T.expiresInMs/36e5)}h left)`:""}`:T.state==="near_expiry"?"\u{1F6E1}\uFE0F Self-healing: \u267B\uFE0F token near expiry \u2014 auto-refresh attempted":`\u{1F6E1}\uFE0F Self-healing: \u26A0\uFE0F ${T.message.split(`
55
- `)[0]}`;return["\u{1F4E1} **cachly API Status**","",` \u{1F310} API: ${x}`,` \u{1F493} Health: ${r}`,` ${B}`,"","\u{1F511} **Auth:**",d,h].join(`
56
- `)}default:throw new bt(`Unknown tool: ${e}`)}}class bt extends Error{}const vt=`This project has a Cachly AI Brain \u2014 persistent, cross-session memory of past fixes, decisions, and gotchas for THIS codebase. Use it proactively; it makes you faster and stops you repeating known mistakes.
54
+ \u{1F9E0} **Brain Instances:** could not fetch \u2014 ${E.message}`}await Re();const T=Se(v),L=T.state==="long_lived"?"\u{1F6E1}\uFE0F Self-healing: \u2705 long-lived API key (won't expire silently)":T.state==="healthy"?`\u{1F6E1}\uFE0F Self-healing: \u2705 token valid${T.expiresInMs?` (${Math.round(T.expiresInMs/36e5)}h left)`:""}`:T.state==="near_expiry"?"\u{1F6E1}\uFE0F Self-healing: \u267B\uFE0F token near expiry \u2014 auto-refresh attempted":`\u{1F6E1}\uFE0F Self-healing: \u26A0\uFE0F ${T.message.split(`
55
+ `)[0]}`;return["\u{1F4E1} **cachly API Status**","",` \u{1F310} API: ${x}`,` \u{1F493} Health: ${i}`,` ${L}`,"","\u{1F511} **Auth:**",d,y].join(`
56
+ `)}default:throw new _t(`Unknown tool: ${e}`)}}class _t extends Error{}const $t=`This project has a Cachly AI Brain \u2014 persistent, cross-session memory of past fixes, decisions, and gotchas for THIS codebase. Use it proactively; it makes you faster and stops you repeating known mistakes.
57
57
 
58
58
  - BEFORE a non-trivial task: call smart_recall with a short description of what you're about to do. If it returns lessons, apply them before researching from scratch.
59
59
  - WHEN debugging an error: call causal_trace with the exact error first \u2014 a known fix may already exist.
60
60
  - AFTER a fix, deploy, or discovery: call learn_from_attempts so the knowledge persists.
61
61
  - BEFORE a risky op (deploy, migration, dependency bump): call brain_predict.
62
62
 
63
- The Brain instance id comes from the CACHLY_BRAIN_INSTANCE_ID environment variable.`,wt={tools:{},resources:{},prompts:{}},xt=async()=>{const{katalog:e,unbekannteKernnamen:t}=Nn(ue);return t.length>0&&console.error(`cachly: ${t.length} Kernwerkzeug(e) gibt es gar nicht: ${t.join(", ")}`),{tools:e}};async function _t(){return{resources:[]}}async function $t(){return{resourceTemplates:[]}}async function Ct(){return{prompts:[]}}function ss(e){e.setRequestHandler(Ke,xt),e.setRequestHandler(Ue,kt),e.setRequestHandler(ze,_t),e.setRequestHandler(Fe,$t),e.setRequestHandler(We,Ct)}function rs(){const e=new Ye({name:"cachly-mcp",version:D},{capabilities:wt,instructions:vt});return ss(e),At(e),e}function At(e){const t=e.oninitialized;e.oninitialized=()=>{try{const s=e.getClientVersion();Bt(s?.name,s?.version)}catch{}t?.()}}const G=new Ye({name:"cachly-mcp",version:D},{capabilities:wt,instructions:vt});At(G),G.setRequestHandler(Ke,xt),G.setRequestHandler(ze,_t),G.setRequestHandler(Fe,$t),G.setRequestHandler(We,Ct);let Be=null,pe=!1,Q=null,St=0;async function is(e){if(!pe){if(Q){await Q;return}Q=(async()=>{pe=!0,Be=e;try{await U("session_start",{instance_id:e,focus:"auto (MCP session)",workspace_path:process.cwd()})}catch{}finally{Q=null}})(),await Q,process.env.CACHLY_AUTO_INDEX!=="false"&&(async()=>{const t=await P(e),s=await t.get(`cachly:index:last_indexed:${e}`),o=1440*60*1e3;(!s||Date.now()-parseInt(s,10)>o)&&(await t.set(`cachly:index:last_indexed:${e}`,String(Date.now()),"EX",9e4),await U("index_project",{instance_id:e,dir:process.cwd(),max_files:150,ttl:86400*7,namespace:"cachly:sem:code",hintergrund:!0}))})().catch(()=>{})}}async function je(){if(!(!pe||!Be)){pe=!1;try{await U("session_end",{instance_id:Be,summary:`Auto-ended after ${St} tool call(s). Session was started automatically.`,files_changed:[],lessons_learned:0})}catch{}}}const kt=async e=>{let{name:t,arguments:s}=e.params;if(t===ut){const n=s??{},c=typeof n.tool=="string"?n.tool:"";if(!c)return{content:[{type:"text",text:'cachly_tool braucht das Feld "tool" \u2014 den Namen des Werkzeugs.'}],isError:!0};const i=ue.find(a=>a.name===c);if(!i)return{content:[{type:"text",text:`Kein Werkzeug namens "${c}". Die Liste steht in der Beschreibung von ${ut}.`}],isError:!0};if(n.describe===!0)return{content:[{type:"text",text:JSON.stringify({name:i.name,description:i.description,inputSchema:i.inputSchema},null,2)}]};t=c,s=n.arguments??{}}const o=new Set(["session_start","session_start_summary","session_end","auto_learn_session"]);if(!o.has(t)&&!pe&&!Q){const n=Re(s?.instance_id)||H||Re(process.env.CACHLY_BRAIN_INSTANCE_ID);n&&await is(n).catch(()=>{})}else Q&&await Q.catch(()=>{});o.has(t)||St++;try{En(),W(`werkzeug ${t}: Anfang`);const n=await U(t,s??{});return W(`werkzeug ${t}: Antwort fertig`),{content:[{type:"text",text:Zo(t,n)+$n()+Tn(t)}]}}catch(n){if(n instanceof bt)return{content:[{type:"text",text:n.message}],isError:!0};if(n instanceof ee)throw n;const c=n.message??String(n);throw De("cachly","tool_error",{tool:t,error:c}).catch(()=>{}),new ee(ne.InternalError,c)}};G.setRequestHandler(Ue,kt),process.on("SIGTERM",async()=>{await je().catch(()=>{});for(const[,e]of te)await e.quit().catch(()=>{});process.exit(0)}),process.on("SIGINT",async()=>{await je().catch(()=>{});for(const[,e]of te)await e.quit().catch(()=>{});process.exit(0)}),process.on("unhandledRejection",e=>{const t=e instanceof Error?e.message:String(e);process.stderr.write(`[cachly-mcp] unhandledRejection: ${t}
63
+ The Brain instance id comes from the CACHLY_BRAIN_INSTANCE_ID environment variable.`,kt={tools:{},resources:{},prompts:{}},St=async()=>{const{katalog:e,unbekannteKernnamen:t}=Nn(ge);return t.length>0&&console.error(`cachly: ${t.length} Kernwerkzeug(e) gibt es gar nicht: ${t.join(", ")}`),{tools:e}};async function At(){return{resources:[]}}async function Ct(){return{resourceTemplates:[]}}async function Tt(){return{prompts:[]}}function ls(e){e.setRequestHandler(Ve,St),e.setRequestHandler(Ye,Rt),e.setRequestHandler(Ge,At),e.setRequestHandler(qe,Ct),e.setRequestHandler(Ke,Tt)}function ds(){const e=new We({name:"cachly-mcp",version:D},{capabilities:kt,instructions:$t});return ls(e),Et(e),e}function Et(e){const t=e.oninitialized;e.oninitialized=()=>{try{const s=e.getClientVersion();Nt(s?.name,s?.version)}catch{}t?.()}}const V=new We({name:"cachly-mcp",version:D},{capabilities:kt,instructions:$t});Et(V),V.setRequestHandler(Ve,St),V.setRequestHandler(Ge,At),V.setRequestHandler(qe,Ct),V.setRequestHandler(Ke,Tt);let He=null,fe=!1,X=null,It=0;async function us(e){if(!fe){if(X){await X;return}X=(async()=>{fe=!0,He=e;try{await z("session_start",{instance_id:e,focus:"auto (MCP session)",workspace_path:process.cwd()})}catch{}finally{X=null}})(),await X,process.env.CACHLY_AUTO_INDEX!=="false"&&(async()=>{const t=await H(e),s=await t.get(`cachly:index:last_indexed:${e}`),n=1440*60*1e3;(!s||Date.now()-parseInt(s,10)>n)&&(await t.set(`cachly:index:last_indexed:${e}`,String(Date.now()),"EX",9e4),await z("index_project",{instance_id:e,dir:process.cwd(),max_files:150,ttl:86400*7,namespace:"cachly:sem:code",hintergrund:!0}))})().catch(()=>{})}}async function Me(){if(!(!fe||!He)){fe=!1;try{await z("session_end",{instance_id:He,summary:`Auto-ended after ${It} tool call(s). Session was started automatically.`,files_changed:[],lessons_learned:0})}catch{}}}const Rt=async e=>{let{name:t,arguments:s}=e.params;if(t===ft){const o=s??{},c=typeof o.tool=="string"?o.tool:"";if(!c)return{content:[{type:"text",text:'cachly_tool braucht das Feld "tool" \u2014 den Namen des Werkzeugs.'}],isError:!0};const r=ge.find(a=>a.name===c);if(!r)return{content:[{type:"text",text:`Kein Werkzeug namens "${c}". Die Liste steht in der Beschreibung von ${ft}.`}],isError:!0};if(o.describe===!0)return{content:[{type:"text",text:JSON.stringify({name:r.name,description:r.description,inputSchema:r.inputSchema},null,2)}]};t=c,s=o.arguments??{}}const n=new Set(["session_start","session_start_summary","session_end","auto_learn_session"]);if(!n.has(t)&&!fe&&!X){const o=xe(s?.instance_id)||P||xe(process.env.CACHLY_BRAIN_INSTANCE_ID);o&&await us(o).catch(()=>{})}else X&&await X.catch(()=>{});n.has(t)||It++;try{On(),W(`werkzeug ${t}: Anfang`);const o=await z(t,s??{});return W(`werkzeug ${t}: Antwort fertig`),{content:[{type:"text",text:nn(t,o)+Tn()+Bn(t)}]}}catch(o){if(o instanceof _t)return{content:[{type:"text",text:o.message}],isError:!0};if(o instanceof te)throw o;const c=o.message??String(o);throw ze("cachly","tool_error",{tool:t,error:c}).catch(()=>{}),new te(ie.InternalError,c)}};V.setRequestHandler(Ye,Rt),process.on("SIGTERM",async()=>{await Me().catch(()=>{});for(const[,e]of ne)await e.quit().catch(()=>{});process.exit(0)}),process.on("SIGINT",async()=>{await Me().catch(()=>{});for(const[,e]of ne)await e.quit().catch(()=>{});process.exit(0)}),process.on("unhandledRejection",e=>{const t=e instanceof Error?e.message:String(e);process.stderr.write(`[cachly-mcp] unhandledRejection: ${t}
64
64
  `)}),process.on("uncaughtException",e=>{process.stderr.write(`[cachly-mcp] uncaughtException: ${e?.stack??String(e)}
65
- `)}),process.stdout.on("error",()=>{}),process.stdin.on("error",()=>{});const ve={claude:".mcp.json",cursor:".cursor/mcp.json",windsurf:".windsurf/mcp.json",copilot:".vscode/mcp.json",continue:".continue/config.json",cline:".vscode/mcp.json",zed:".zed/settings.json"};function as(e,t){const s=Object.entries(t).map(([o,n])=>`${o}=${n}`);return e==="codex"?{befehl:`codex mcp add cachly ${s.map(o=>`--env ${o}`).join(" ")} -- npx -y @cachly-dev/mcp-server@latest`,wo:"~/.codex/config.toml"}:e==="gemini"?{befehl:`gemini mcp add -s project ${s.map(o=>`-e ${o}`).join(" ")} cachly npx -- -y @cachly-dev/mcp-server@latest`,wo:".gemini/settings.json"}:null}const He="<!-- cachly-brain-start -->",Tt="<!-- cachly-brain-end -->",It="https://api.cachly.dev";function oe(e,t,s={}){const o={CACHLY_BRAIN_INSTANCE_ID:t};return s.includeApiKey&&(o.CACHLY_JWT=e),x&&x!==It&&(o.CACHLY_API_URL=x),o}function cs(e,t,s){return s==="continue"?JSON.stringify({experimental:{modelContextProtocolServers:[{transport:{type:"stdio",command:"npx",args:["-y","@cachly-dev/mcp-server@latest"]},env:oe(e,t)}]}},null,2):s==="zed"?JSON.stringify({context_servers:{cachly:{command:{path:"npx",args:["-y","@cachly-dev/mcp-server@latest"],env:oe(e,t)},settings:{}}}},null,2):Et(s)?JSON.stringify({servers:{cachly:{type:"stdio",command:"npx",args:["-y","@cachly-dev/mcp-server@latest"],env:oe(e,t)}}},null,2):JSON.stringify({mcpServers:{cachly:{command:"npx",args:["-y","@cachly-dev/mcp-server@latest"],env:oe(e,t)}}},null,2)}function Et(e){return ve[e]===".vscode/mcp.json"}function me(e,t){on(e)||Z(e,t)}async function Rt(e,t,s,o,n,c={}){const i={command:"npx",args:["-y","@cachly-dev/mcp-server@latest"],env:oe(t,s)};if(!n.existsSync(e))return cs(t,s,o);let a={};try{a=JSON.parse(await n.readFile(e,"utf-8"))}catch{try{const{rename:m}=await import("node:fs/promises");await m(e,e+".bak"),process.stderr.write(`\u26A0\uFE0F ${e} was not valid JSON \u2014 backed up to ${e}.bak
66
- `)}catch{}}if(o==="continue"){const m=a.experimental??{},y=m.modelContextProtocolServers??[];let w;for(const p of y){const r=p.env??{};if(r.CACHLY_JWT){w=r.CACHLY_JWT;break}}w&&me(w,c);const b=y.filter(p=>{const r=p.env??{};return!r.CACHLY_JWT&&!r.CACHLY_BRAIN_INSTANCE_ID});return b.push({transport:{type:"stdio",command:"npx",args:["-y","@cachly-dev/mcp-server@latest"]},env:i.env}),a.experimental={...m,modelContextProtocolServers:b},JSON.stringify(a,null,2)}if(o==="zed"){const m=a.context_servers??{},w=m.cachly?.command?.env?.CACHLY_JWT;return w&&me(w,c),m.cachly={command:{path:"npx",args:["-y","@cachly-dev/mcp-server@latest"],env:i.env},settings:{}},a.context_servers=m,JSON.stringify(a,null,2)}if(Et(o)){const m=a.mcpServers??void 0,w=m?.cachly?.env?.CACHLY_JWT;w&&me(w,c),m&&"cachly"in m&&(delete m.cachly,Object.keys(m).length===0&&delete a.mcpServers);const b=a.servers??{},p=b.cachly,r=p?.env?.CACHLY_JWT;return r&&me(r,c),b.cachly=p?.envFile?{type:"stdio",...i,envFile:p.envFile}:{type:"stdio",...i},a.servers=b,JSON.stringify(a,null,2)}const u=a.mcpServers??{},l=u.cachly?.env?.CACHLY_JWT;return l&&me(l,c),u.cachly=i,a.mcpServers=u,JSON.stringify(a,null,2)}function Nt(e){return`${He}
65
+ `)}),process.stdout.on("error",()=>{}),process.stdin.on("error",()=>{});function ps(e,t){const s=Object.entries(t).map(([n,o])=>`${n}=${o}`);return e==="codex"?{befehl:`codex mcp add cachly ${s.map(n=>`--env ${n}`).join(" ")} -- npx -y @cachly-dev/mcp-server@latest`,wo:"~/.codex/config.toml"}:e==="gemini"?{befehl:`gemini mcp add -s project ${s.map(n=>`-e ${n}`).join(" ")} cachly npx -- -y @cachly-dev/mcp-server@latest`,wo:".gemini/settings.json"}:null}const jt="https://api.cachly.dev";function se(e,t,s={}){const n={CACHLY_BRAIN_INSTANCE_ID:t};return s.includeApiKey&&(n.CACHLY_JWT=e),x&&x!==jt&&(n.CACHLY_API_URL=x),n}function ms(e,t,s){return s==="continue"?JSON.stringify({experimental:{modelContextProtocolServers:[{transport:{type:"stdio",command:"npx",args:["-y","@cachly-dev/mcp-server@latest"]},env:se(e,t)}]}},null,2):s==="zed"?JSON.stringify({context_servers:{cachly:{command:{path:"npx",args:["-y","@cachly-dev/mcp-server@latest"],env:se(e,t)},settings:{}}}},null,2):Bt(s)?JSON.stringify({servers:{cachly:{type:"stdio",command:"npx",args:["-y","@cachly-dev/mcp-server@latest"],env:se(e,t)}}},null,2):JSON.stringify({mcpServers:{cachly:{command:"npx",args:["-y","@cachly-dev/mcp-server@latest"],env:se(e,t)}}},null,2)}function Bt(e){return me[e]===".vscode/mcp.json"}function he(e,t){cn(e)||Q(e,t)}async function Lt(e,t,s,n,o,c={}){const r={command:"npx",args:["-y","@cachly-dev/mcp-server@latest"],env:se(t,s)};if(!o.existsSync(e))return ms(t,s,n);let a={};try{a=JSON.parse(await o.readFile(e,"utf-8"))}catch{try{const{rename:m}=await import("node:fs/promises");await m(e,e+".bak"),process.stderr.write(`\u26A0\uFE0F ${e} was not valid JSON \u2014 backed up to ${e}.bak
66
+ `)}catch{}}if(n==="continue"){const m=a.experimental??{},h=m.modelContextProtocolServers??[];let w;for(const p of h){const i=p.env??{};if(i.CACHLY_JWT){w=i.CACHLY_JWT;break}}w&&he(w,c);const b=h.filter(p=>{const i=p.env??{};return!i.CACHLY_JWT&&!i.CACHLY_BRAIN_INSTANCE_ID});return b.push({transport:{type:"stdio",command:"npx",args:["-y","@cachly-dev/mcp-server@latest"]},env:r.env}),a.experimental={...m,modelContextProtocolServers:b},JSON.stringify(a,null,2)}if(n==="zed"){const m=a.context_servers??{},w=m.cachly?.command?.env?.CACHLY_JWT;return w&&he(w,c),m.cachly={command:{path:"npx",args:["-y","@cachly-dev/mcp-server@latest"],env:r.env},settings:{}},a.context_servers=m,JSON.stringify(a,null,2)}if(Bt(n)){const m=a.mcpServers??void 0,w=m?.cachly?.env?.CACHLY_JWT;w&&he(w,c),m&&"cachly"in m&&(delete m.cachly,Object.keys(m).length===0&&delete a.mcpServers);const b=a.servers??{},p=b.cachly,i=p?.env?.CACHLY_JWT;return i&&he(i,c),b.cachly=p?.envFile?{type:"stdio",...r,envFile:p.envFile}:{type:"stdio",...r},a.servers=b,JSON.stringify(a,null,2)}const u=a.mcpServers??{},l=u.cachly?.env?.CACHLY_JWT;return l&&he(l,c),u.cachly=r,a.mcpServers=u,JSON.stringify(a,null,2)}function Ot(e){return`${Be}
67
67
  ## Cachly AI Brain \u2014 Always Active
68
68
 
69
69
  The Brain MCP server runs automatically. Instance: \`${e}\`
@@ -118,66 +118,61 @@ Report predicted failure risks to the user before proceeding.
118
118
 
119
119
  ### Half-finished code rule:
120
120
  Never commit code that does not compile. Run \`tsc --noEmit\` / \`go build ./...\` before every commit.
121
- ${Tt}`}const ls=`---
122
- description: Cachly AI Brain \u2014 persistent memory protocol (recall before tasks, learn after fixes)
123
- alwaysApply: true
124
- ---
125
-
126
- `;async function Lt(e,t){const{writeFile:s,appendFile:o,readFile:n,mkdir:c}=await import("node:fs/promises"),{existsSync:i}=await import("node:fs"),{resolve:a,dirname:u}=await import("node:path"),g=[{path:a(e,"CLAUDE.md")},{path:a(e,"AGENTS.md")},{path:a(e,".github","copilot-instructions.md")},{path:a(e,".cursor","rules","cachly.mdc"),prefix:ls},{path:a(e,".windsurfrules")},{path:a(e,".clinerules")},...i(a(e,".idea"))?[{path:a(e,".aiassistant","rules","cachly.md")}]:[]],l=`
127
- `+Nt(t)+`
128
- `,m={};for(const{path:y,prefix:w}of g){if(await c(u(y),{recursive:!0}),i(y)){const b=await n(y,"utf-8");if(b.includes(He)){const p=b.replace(new RegExp(`${He}[\\s\\S]*?${Tt}`),Nt(t));await s(y,p,"utf-8"),m[y]="updated";continue}await o(y,l,"utf-8"),m[y]="appended";continue}await s(y,(w??"")+l.trimStart(),"utf-8"),m[y]="written"}return m}if(process.argv[2]==="digest"){const e=process.env.CACHLY_JWT??"",t=process.env.CACHLY_BRAIN_INSTANCE_ID??"";(!e||!t)&&(console.log(`
129
- \u26A0\uFE0F CACHLY_JWT and CACHLY_BRAIN_INSTANCE_ID must be set.`),console.log(` Run: npx @cachly-dev/mcp-server@latest autopilot
121
+ ${it}`}async function Pt(e,t){const{writeFile:s,appendFile:n,readFile:o,mkdir:c}=await import("node:fs/promises"),{existsSync:r}=await import("node:fs"),{resolve:a,dirname:u}=await import("node:path"),g=Uo.filter(h=>!h.nurMitIdea||r(a(e,".idea"))).map(h=>({path:a(e,...h.pfad),prefix:h.prefix})),l=`
122
+ `+Ot(t)+`
123
+ `,m={};for(const{path:h,prefix:w}of g){if(await c(u(h),{recursive:!0}),r(h)){const b=await o(h,"utf-8");if(b.includes(Be)){const p=b.replace(Jo(),Ot(t));await s(h,p,"utf-8"),m[h]="updated";continue}await n(h,l,"utf-8"),m[h]="appended";continue}await s(h,(w??"")+l.trimStart(),"utf-8"),m[h]="written"}return m}if(process.argv[2]==="digest"){const e=oe(),t=F();(!e||!t)&&(console.log(`
124
+ \u26A0\uFE0F No cachly key or Brain instance found (environment or ~/.cachly/credentials.json).`),console.log(` Run: npx @cachly-dev/mcp-server@latest autopilot
130
125
  `),process.exit(1)),process.stdout.write(`
131
126
  \u{1F9E0} Fetching your weekly Brain digest...
132
127
 
133
- `);try{const s=await fetch(`${x}/api/v1/instances/${t}/memory`,{headers:{Authorization:`Bearer ${e}`},signal:AbortSignal.timeout(8e3)});if(!s.ok)throw new Error(`stats HTTP ${s.status}`);const o=await s.json(),n=o.lesson_count??0,c=o.total_recall_count??0,i=Math.round(o.iq_boost_pct??0),a=(o.top_lessons??[]).slice(0,5),u=o.team_authors??[],g=c*1200,l=(g*3e-6).toFixed(2),m=n<10?"Junior Dev \u{1F527}":n<30?"Mid Dev \u26A1":n<60?"Senior Dev \u{1F9E0}":n<100?"Staff Eng \u{1F680}":"Principal Eng \u{1F3C6}",y=new Date,w=new Date(y);w.setDate(y.getDate()-7);const b=h=>h.toLocaleDateString("en-US",{month:"short",day:"numeric"});if(console.log("\u250C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510"),console.log(`\u2502 \x1B[1m\u{1F9E0} Brain Weekly Digest\x1B[0m \x1B[90m${b(w)} \u2013 ${b(y)}\x1B[0m \u2502`),console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524"),console.log(`\u2502 Total lessons : \x1B[33m${String(n).padEnd(8)}\x1B[0m Brain level : \x1B[32m${m.padEnd(18)}\x1B[0m\u2502`),console.log(`\u2502 Total recalls : \x1B[36m${String(c).padEnd(8)}\x1B[0m Quality score: \x1B[32m${String(i).padEnd(3)}%\x1B[0m \u2502`),console.log(`\u2502 Tokens saved : \x1B[32m${(Math.round(g/1e3)+"K").padEnd(8)}\x1B[0m Cost saved : \x1B[32m$${l.padEnd(20)}\x1B[0m\u2502`),u.length>0&&(console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524"),console.log(`\u2502 \x1B[36m\u{1F465} Team contributors:\x1B[0m ${u.slice(0,5).join(", ").slice(0,42).padEnd(42)} \u2502`)),a.length>0){console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524"),console.log("\u2502 \x1B[33m\u{1F525} Most recalled lessons this week:\x1B[0m \u2502");for(const h of a){const T=`${h.topic}: ${h.what_worked}`.slice(0,57);console.log(`\u2502 \x1B[90m\u2022 ${T.padEnd(58)}\x1B[0m\u2502`)}}console.log("\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518"),console.log("");const p=a[0],r=[`\u{1F9E0} My AI Brain weekly digest (${b(w)} \u2013 ${b(y)}):`,"",` \u{1F4DA} ${n} lessons learned`,` \u{1F501} ${c} recalls \xB7 ~${Math.round(g/1e3)}K tokens saved`,` \u{1F3AF} Brain Level: ${m}`,p?` \u{1F525} Top lesson: "${p.topic}: ${p.what_worked.slice(0,60)}${p.what_worked.length>60?"\u2026":""}"`:"","","Built with @cachly_dev \u2014 AI that actually remembers \u{1F680}","cachly.dev"].filter(Boolean).join(`
134
- `),d="https://twitter.com/intent/tweet?text="+encodeURIComponent(r);console.log("\x1B[1m\u{1F4E3} Share your digest:\x1B[0m"),console.log(""),console.log("\x1B[90m"+"\u2500".repeat(63)+"\x1B[0m");for(const h of r.split(`
135
- `))console.log(` ${h}`);console.log("\x1B[90m"+"\u2500".repeat(63)+"\x1B[0m"),console.log(""),console.log(" \x1B[36m\u{1F426} Tweet this:\x1B[0m"),console.log(` \x1B[4m${d.slice(0,90)}...\x1B[0m`),console.log(""),console.log(" \x1B[2m\u{1F4A1} Invite your team: npx @cachly-dev/mcp-server@latest invite\x1B[0m"),console.log(" \x1B[2m Cron: 0 9 * * 1 npx @cachly-dev/mcp-server@latest digest\x1B[0m"),console.log("")}catch(s){console.log(`
128
+ `);try{const s=await fetch(`${x}/api/v1/instances/${t}/memory`,{headers:{Authorization:`Bearer ${e}`},signal:AbortSignal.timeout(8e3)});if(!s.ok)throw new Error(`stats HTTP ${s.status}`);const n=await s.json(),o=n.lesson_count??0,c=n.total_recall_count??0,r=Math.round(n.iq_boost_pct??0),a=(n.top_lessons??[]).slice(0,5),u=n.team_authors??[],g=c*1200,l=(g*3e-6).toFixed(2),m=o<10?"Junior Dev \u{1F527}":o<30?"Mid Dev \u26A1":o<60?"Senior Dev \u{1F9E0}":o<100?"Staff Eng \u{1F680}":"Principal Eng \u{1F3C6}",h=new Date,w=new Date(h);w.setDate(h.getDate()-7);const b=y=>y.toLocaleDateString("en-US",{month:"short",day:"numeric"});if(console.log("\u250C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510"),console.log(`\u2502 \x1B[1m\u{1F9E0} Brain Weekly Digest\x1B[0m \x1B[90m${b(w)} \u2013 ${b(h)}\x1B[0m \u2502`),console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524"),console.log(`\u2502 Total lessons : \x1B[33m${String(o).padEnd(8)}\x1B[0m Brain level : \x1B[32m${m.padEnd(18)}\x1B[0m\u2502`),console.log(`\u2502 Total recalls : \x1B[36m${String(c).padEnd(8)}\x1B[0m Quality score: \x1B[32m${String(r).padEnd(3)}%\x1B[0m \u2502`),console.log(`\u2502 Tokens saved : \x1B[32m${(Math.round(g/1e3)+"K").padEnd(8)}\x1B[0m Cost saved : \x1B[32m$${l.padEnd(20)}\x1B[0m\u2502`),u.length>0&&(console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524"),console.log(`\u2502 \x1B[36m\u{1F465} Team contributors:\x1B[0m ${u.slice(0,5).join(", ").slice(0,42).padEnd(42)} \u2502`)),a.length>0){console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524"),console.log("\u2502 \x1B[33m\u{1F525} Most recalled lessons this week:\x1B[0m \u2502");for(const y of a){const T=`${y.topic}: ${y.what_worked}`.slice(0,57);console.log(`\u2502 \x1B[90m\u2022 ${T.padEnd(58)}\x1B[0m\u2502`)}}console.log("\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518"),console.log("");const p=a[0],i=[`\u{1F9E0} My AI Brain weekly digest (${b(w)} \u2013 ${b(h)}):`,"",` \u{1F4DA} ${o} lessons learned`,` \u{1F501} ${c} recalls \xB7 ~${Math.round(g/1e3)}K tokens saved`,` \u{1F3AF} Brain Level: ${m}`,p?` \u{1F525} Top lesson: "${p.topic}: ${p.what_worked.slice(0,60)}${p.what_worked.length>60?"\u2026":""}"`:"","","Built with @cachly_dev \u2014 AI that actually remembers \u{1F680}","cachly.dev"].filter(Boolean).join(`
129
+ `),d="https://twitter.com/intent/tweet?text="+encodeURIComponent(i);console.log("\x1B[1m\u{1F4E3} Share your digest:\x1B[0m"),console.log(""),console.log("\x1B[90m"+"\u2500".repeat(63)+"\x1B[0m");for(const y of i.split(`
130
+ `))console.log(` ${y}`);console.log("\x1B[90m"+"\u2500".repeat(63)+"\x1B[0m"),console.log(""),console.log(" \x1B[36m\u{1F426} Tweet this:\x1B[0m"),console.log(` \x1B[4m${d.slice(0,90)}...\x1B[0m`),console.log(""),console.log(" \x1B[2m\u{1F4A1} Invite your team: npx @cachly-dev/mcp-server@latest invite\x1B[0m"),console.log(" \x1B[2m Cron: 0 9 * * 1 npx @cachly-dev/mcp-server@latest digest\x1B[0m"),console.log("")}catch(s){console.log(`
136
131
  \u274C Could not fetch digest: ${s.message}
137
- `),process.exit(1)}process.exit(0)}if(process.argv[2]==="export"){const{writeFile:e,mkdir:t}=await import("node:fs/promises"),{resolve:s}=await import("node:path"),o=process.argv.slice(3),n=g=>{const l=o.indexOf(`--${g}`);return l!==-1?o[l+1]:void 0},c=n("format")??"both";c!=="md"&&c!=="jsonl"&&c!=="both"&&(console.log(`
132
+ `),process.exit(1)}process.exit(0)}if(process.argv[2]==="export"){const{writeFile:e,mkdir:t}=await import("node:fs/promises"),{resolve:s}=await import("node:path"),n=process.argv.slice(3),o=g=>{const l=n.indexOf(`--${g}`);return l!==-1?n[l+1]:void 0},c=o("format")??"both";c!=="md"&&c!=="jsonl"&&c!=="both"&&(console.log(`
138
133
  \u274C Unknown --format "${c}" \u2014 use md, jsonl, or both.
139
- `),process.exit(1));const i=s(n("out")??"."),a=v||it()||"",u=process.env.CACHLY_BRAIN_INSTANCE_ID??"";(!a||!u)&&(console.log(`
140
- \u26A0\uFE0F CACHLY_JWT and CACHLY_BRAIN_INSTANCE_ID must be set.`),console.log(` Run: npx @cachly-dev/mcp-server@latest autopilot
134
+ `),process.exit(1));const r=s(o("out")??"."),a=v||Le()||"",u=F();(!a||!u)&&(console.log(`
135
+ \u26A0\uFE0F No cachly key or Brain instance found (environment or ~/.cachly/credentials.json).`),console.log(` Run: npx @cachly-dev/mcp-server@latest autopilot
141
136
  `),process.exit(1)),process.stdout.write(`
142
137
  \u{1F4E4} Exporting your Brain...
143
138
 
144
- `);try{let g=[],l="";const m=await fetch(`${x}/api/v1/instances/${u}/export`,{headers:{Authorization:`Bearer ${a}`},signal:AbortSignal.timeout(12e4)});if(m.ok){const w=await m.json();g=(w.lessons??[]).map(jn).filter(b=>b!==null),w.complete===!1&&(l=w.note??"the server reported an incomplete read")}else if(m.status===404){const w=await fetch(`${x}/api/v1/instances/${u}/memory`,{headers:{Authorization:`Bearer ${a}`},signal:AbortSignal.timeout(8e3)});if(!w.ok)throw new Error(`memory HTTP ${w.status}`);g=((await w.json()).top_lessons??[]).map(p=>({topic:p.topic,outcome:p.outcome,severity:p.severity,tags:p.tags,whatWorked:p.what_worked,ts:p.ts,author:p.author,recallCount:p.recall_count})),l="this server has no /export endpoint yet, so this is the DASHBOARD SUMMARY: at most 50 lessons, each shortened to 120 characters. Update the server for a full export."}else throw new Error(`export HTTP ${m.status}`);g.length===0&&(console.log(` No lessons in this Brain yet \u2014 nothing to export.
145
- `),process.exit(0)),await t(i,{recursive:!0});const y=[];if(c==="md"||c==="both"){const w=s(i,"lessons.md");await e(w,Ln(g),"utf-8"),y.push(`${g.length} lesson${g.length===1?"":"s"} \u2192 ${w}`)}if(c==="jsonl"||c==="both"){const w=s(i,"lessons.jsonl");await e(w,Bn(g),"utf-8"),y.push(`${g.length} lesson${g.length===1?"":"s"} \u2192 ${w}`)}l?(console.log("\u26A0\uFE0F Export INCOMPLETE \u2014 this is not your whole Brain:"),console.log(` ${l}`),console.log("")):console.log("\u2705 Export complete:");for(const w of y)console.log(` ${w}`);console.log("")}catch(g){console.log(`
139
+ `);try{let g=[],l="";const m=await fetch(`${x}/api/v1/instances/${u}/export`,{headers:{Authorization:`Bearer ${a}`},signal:AbortSignal.timeout(12e4)});if(m.ok){const w=await m.json();g=(w.lessons??[]).map(Dn).filter(b=>b!==null),w.complete===!1&&(l=w.note??"the server reported an incomplete read")}else if(m.status===404){const w=await fetch(`${x}/api/v1/instances/${u}/memory`,{headers:{Authorization:`Bearer ${a}`},signal:AbortSignal.timeout(8e3)});if(!w.ok)throw new Error(`memory HTTP ${w.status}`);g=((await w.json()).top_lessons??[]).map(p=>({topic:p.topic,outcome:p.outcome,severity:p.severity,tags:p.tags,whatWorked:p.what_worked,ts:p.ts,author:p.author,recallCount:p.recall_count})),l="this server has no /export endpoint yet, so this is the DASHBOARD SUMMARY: at most 50 lessons, each shortened to 120 characters. Update the server for a full export."}else throw new Error(`export HTTP ${m.status}`);g.length===0&&(console.log(` No lessons in this Brain yet \u2014 nothing to export.
140
+ `),process.exit(0)),await t(r,{recursive:!0});const h=[];if(c==="md"||c==="both"){const w=s(r,"lessons.md");await e(w,Hn(g),"utf-8"),h.push(`${g.length} lesson${g.length===1?"":"s"} \u2192 ${w}`)}if(c==="jsonl"||c==="both"){const w=s(r,"lessons.jsonl");await e(w,Mn(g),"utf-8"),h.push(`${g.length} lesson${g.length===1?"":"s"} \u2192 ${w}`)}l?(console.log("\u26A0\uFE0F Export INCOMPLETE \u2014 this is not your whole Brain:"),console.log(` ${l}`),console.log("")):console.log("\u2705 Export complete:");for(const w of h)console.log(` ${w}`);console.log("")}catch(g){console.log(`
146
141
  \u274C Could not export your Brain: ${g.message}
147
- `),process.exit(1)}process.exit(0)}if(process.argv[2]==="invite"){const e=process.env.CACHLY_JWT??"";e||(console.log(`
148
- \u26A0\uFE0F CACHLY_JWT must be set.`),console.log(` Run: npx @cachly-dev/mcp-server@latest autopilot
149
- `),process.exit(1));try{const t=await fetch(`${x}/api/v1/referral/me`,{headers:{Authorization:`Bearer ${e}`},signal:AbortSignal.timeout(8e3)});if(!t.ok)throw new Error(`HTTP ${t.status}`);const s=await t.json(),o=s.url??N(`/r/${s.code}`,"invite"),n=s.referral_count??0,c=`Hey, I've been using cachly to give my AI persistent memory across sessions \u2014 no more re-explaining my stack every morning.
142
+ `),process.exit(1)}process.exit(0)}if(process.argv[2]==="invite"){const e=oe();e||(console.log(`
143
+ \u26A0\uFE0F No cachly key found (environment or ~/.cachly/credentials.json).`),console.log(` Run: npx @cachly-dev/mcp-server@latest autopilot
144
+ `),process.exit(1));try{const t=await fetch(`${x}/api/v1/referral/me`,{headers:{Authorization:`Bearer ${e}`},signal:AbortSignal.timeout(8e3)});if(!t.ok)throw new Error(`HTTP ${t.status}`);const s=await t.json(),n=s.url??j(`/r/${s.code}`,"invite"),o=s.referral_count??0,c=`Hey, I've been using cachly to give my AI persistent memory across sessions \u2014 no more re-explaining my stack every morning.
150
145
 
151
- You get a free Brain: ${o}`,i=encodeURIComponent(`I gave my AI persistent memory. It remembers fixes, patterns, and context across every session \u2014 no more re-explaining.
146
+ You get a free Brain: ${n}`,r=encodeURIComponent(`I gave my AI persistent memory. It remembers fixes, patterns, and context across every session \u2014 no more re-explaining.
152
147
 
153
- Try it free: ${o}
148
+ Try it free: ${n}
154
149
 
155
- @cachlydev`);console.log(""),console.log("\u250C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510"),console.log("\u2502 \u{1F9E0} Your cachly Brain invite link \u2502"),console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524"),console.log(`\u2502 ${o.padEnd(61)}\u2502`),console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524"),n>0&&(console.log(`\u2502 \x1B[32m\u2713 ${n} developer${n===1?"":"s"} joined via your link so far\x1B[0m`.padEnd(72)+"\u2502"),console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524")),console.log("\u2502 \x1B[33mSlack / DM message:\x1B[0m \u2502"),console.log("\u2502 \u2502");for(const a of c.split(`
156
- `))console.log(`\u2502 \x1B[90m${a.slice(0,58).padEnd(58)}\x1B[0m \u2502`);console.log("\u2502 \u2502"),console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524"),console.log(`\u2502 \x1B[36m\u{1D54F} Tweet:\x1B[0m https://twitter.com/intent/tweet?text=${i.slice(0,10)}... \u2502`),console.log("\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518"),console.log(""),console.log("\x1B[2m Each developer who signs up via your link earns you 1 month Pro.\x1B[0m"),console.log("")}catch(t){console.log(`
150
+ @cachlydev`);console.log(""),console.log("\u250C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510"),console.log("\u2502 \u{1F9E0} Your cachly Brain invite link \u2502"),console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524"),console.log(`\u2502 ${n.padEnd(61)}\u2502`),console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524"),o>0&&(console.log(`\u2502 \x1B[32m\u2713 ${o} developer${o===1?"":"s"} joined via your link so far\x1B[0m`.padEnd(72)+"\u2502"),console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524")),console.log("\u2502 \x1B[33mSlack / DM message:\x1B[0m \u2502"),console.log("\u2502 \u2502");for(const a of c.split(`
151
+ `))console.log(`\u2502 \x1B[90m${a.slice(0,58).padEnd(58)}\x1B[0m \u2502`);console.log("\u2502 \u2502"),console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524"),console.log(`\u2502 \x1B[36m\u{1D54F} Tweet:\x1B[0m https://twitter.com/intent/tweet?text=${r.slice(0,10)}... \u2502`),console.log("\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518"),console.log(""),console.log("\x1B[2m Each developer who signs up via your link earns you 1 month Pro.\x1B[0m"),console.log("")}catch(t){console.log(`
157
152
  \u274C Could not fetch invite link: ${t.message}
158
- `),process.exit(1)}process.exit(0)}if(process.argv[2]==="join"){const{writeFile:e,readFile:t,mkdir:s}=await import("node:fs/promises"),{existsSync:o}=await import("node:fs"),{resolve:n,dirname:c}=await import("node:path"),{homedir:i}=await import("node:os"),{createInterface:a}=await import("node:readline"),u=process.argv[3]??"";u||(console.log(`
153
+ `),process.exit(1)}process.exit(0)}if(process.argv[2]==="join"){const{writeFile:e,readFile:t,mkdir:s}=await import("node:fs/promises"),{existsSync:n}=await import("node:fs"),{resolve:o,dirname:c}=await import("node:path"),{homedir:r}=await import("node:os"),{createInterface:a}=await import("node:readline"),u=process.argv[3]??"";u||(console.log(`
159
154
  \u274C Usage: npx @cachly-dev/mcp-server@latest join <token>
160
155
  `),console.log(` Get a token from a teammate: npx @cachly-dev/mcp-server@latest invite
161
156
  `),process.exit(1)),console.log(`
162
- \u23F3 Looking up invite...`);let g;try{const S=await fetch(`${x}/api/invite/${u}`,{signal:AbortSignal.timeout(8e3)});S.status===404&&(console.log(`
157
+ \u23F3 Looking up invite...`);let g;try{const A=await fetch(`${x}/api/invite/${u}`,{signal:AbortSignal.timeout(8e3)});A.status===404&&(console.log(`
163
158
  \u274C Invite not found \u2014 the link may be invalid.
164
- `),process.exit(1)),S.status===410&&(console.log(`
159
+ `),process.exit(1)),A.status===410&&(console.log(`
165
160
  \u274C This invite link has expired (7-day limit).
166
- `),process.exit(1)),S.ok||(console.log(`
167
- \u274C Could not fetch invite: HTTP ${S.status}
168
- `),process.exit(1)),g=await S.json()}catch(S){console.log(`
169
- \u274C Network error: ${S.message}
170
- `),process.exit(1)}const{instance_id:l,instance_name:m,tier:y,label:w}=g,b=new Date(g.expires_at).toLocaleDateString();console.log(""),console.log(" \x1B[35m\u2554\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2557\x1B[0m"),console.log(" \x1B[35m\u2551\x1B[0m \x1B[1m\u{1F9E0} Brain Invite\x1B[0m \x1B[35m\u2551\x1B[0m"),console.log(" \x1B[35m\u255A\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u255D\x1B[0m"),console.log(""),console.log(` Brain : \x1B[1m${m}\x1B[0m`),console.log(` Tier : ${y}`),w&&console.log(` Note : ${w}`),console.log(` Expires : ${b}`),console.log("");const p=a({input:process.stdin,output:process.stdout}),r=await new Promise(S=>{p.question(" Join this Brain? [Y/n] ",k=>{p.close(),S(k.trim().toLowerCase()||"y")})});r!=="y"&&r!=="yes"&&(console.log(`
161
+ `),process.exit(1)),A.ok||(console.log(`
162
+ \u274C Could not fetch invite: HTTP ${A.status}
163
+ `),process.exit(1)),g=await A.json()}catch(A){console.log(`
164
+ \u274C Network error: ${A.message}
165
+ `),process.exit(1)}const{instance_id:l,instance_name:m,tier:h,label:w}=g,b=new Date(g.expires_at).toLocaleDateString();console.log(""),console.log(" \x1B[35m\u2554\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2557\x1B[0m"),console.log(" \x1B[35m\u2551\x1B[0m \x1B[1m\u{1F9E0} Brain Invite\x1B[0m \x1B[35m\u2551\x1B[0m"),console.log(" \x1B[35m\u255A\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u255D\x1B[0m"),console.log(""),console.log(` Brain : \x1B[1m${m}\x1B[0m`),console.log(` Tier : ${h}`),w&&console.log(` Note : ${w}`),console.log(` Expires : ${b}`),console.log("");const p=a({input:process.stdin,output:process.stdout}),i=await new Promise(A=>{p.question(" Join this Brain? [Y/n] ",C=>{p.close(),A(C.trim().toLowerCase()||"y")})});i!=="y"&&i!=="yes"&&(console.log(`
171
166
  Cancelled.
172
167
  `),process.exit(0));let d=v;if(!d){console.log(`
173
168
  No API key found \u2014 starting sign-in (10 seconds)...
174
- `);try{const S=await fetch(`${x}/api/v1/auth/device/code`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({client_id:"cachly-mcp-cli"}),signal:AbortSignal.timeout(1e4)});if(S.ok){const k=await S.json();console.log(` \u{1F449} Open: \x1B[36m${k.verification_uri}\x1B[0m`),console.log(` Code: \x1B[1m${k.user_code}\x1B[0m
175
- `);const E=(k.interval??5)*1e3;for(let $=0;$<24;$++){await new Promise(C=>setTimeout(C,E));try{const C=await fetch(`${x}/api/v1/auth/device/token`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({device_code:k.device_code,client_id:"cachly-mcp-cli"}),signal:AbortSignal.timeout(8e3)});if(C.ok){const j=await C.json();if(j.access_token){d=j.access_token;break}}}catch{}}d||(console.log(`
169
+ `);try{const A=await fetch(`${x}/api/v1/auth/device/code`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({client_id:"cachly-mcp-cli"}),signal:AbortSignal.timeout(1e4)});if(A.ok){const C=await A.json();console.log(` \u{1F449} Open: \x1B[36m${C.verification_uri}\x1B[0m`),console.log(` Code: \x1B[1m${C.user_code}\x1B[0m
170
+ `);const I=(C.interval??5)*1e3;for(let $=0;$<24;$++){await new Promise(k=>setTimeout(k,I));try{const k=await fetch(`${x}/api/v1/auth/device/token`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({device_code:C.device_code,client_id:"cachly-mcp-cli"}),signal:AbortSignal.timeout(8e3)});if(k.ok){const O=await k.json();if(O.access_token){d=O.access_token;break}}}catch{}}d||(console.log(`
176
171
  \u274C Sign-in timed out. Run the command again.
177
172
  `),process.exit(1)),console.log(` \u2705 Signed in!
178
- `)}}catch(S){console.log(`
179
- \u26A0\uFE0F Could not start sign-in: ${S.message}`),console.log(` Set CACHLY_JWT manually from ${N("/setup-ai","join")}
180
- `),process.exit(1)}}d&&d!==v&&Z(d);const h=process.cwd(),T=i(),B=[{label:".mcp.json (Claude Code project)",path:n(h,".mcp.json")},{label:".cursor/mcp.json",path:n(h,".cursor","mcp.json")},{label:".windsurf/mcp.json",path:n(h,".windsurf","mcp.json")},{label:".vscode/mcp.json (Copilot/Cline)",path:n(h,".vscode","mcp.json")},{label:"~/.claude/mcp.json (global)",path:n(T,".claude","mcp.json"),global:!0}];let I=0;for(const{label:S,path:k,global:E}of B){const $=c(k);if(!(!E&&!o($)&&!o(k)))try{await s($,{recursive:!0});let C={};if(o(k))try{C=JSON.parse(await t(k,"utf-8"))}catch{try{await(await import("node:fs/promises")).rename(k,k+".bak")}catch{}}const j=C.mcpServers??{},F=j.cachly??{},z=F.env??{};z.CACHLY_BRAIN_INSTANCE_ID=l,d&&E&&!z.CACHLY_JWT&&(z.CACHLY_JWT=d),x!==It&&(z.CACHLY_API_URL=x),j.cachly={...F,command:"npx",args:["-y","@cachly-dev/mcp-server@latest"],env:z},C.mcpServers=j,await e(k,JSON.stringify(C,null,2),"utf-8"),console.log(` \u2705 ${S}`),I++}catch(C){C.code!=="ENOENT"&&console.log(` \u26A0\uFE0F ${S}: ${C.message}`)}}console.log(""),I===0?console.log(" \u26A0\uFE0F No editor configs found \u2014 run `cachly setup` first.\n"):(console.log(` \u{1F9E0} You're now sharing Brain: \x1B[1m${m}\x1B[0m`),console.log(` Restart your editor \u2014 your AI arrives pre-briefed from the team Brain.
173
+ `)}}catch(A){console.log(`
174
+ \u26A0\uFE0F Could not start sign-in: ${A.message}`),console.log(` Set CACHLY_JWT manually from ${j("/setup-ai","join")}
175
+ `),process.exit(1)}}d&&d!==v&&Q(d);const y=process.cwd(),T=r(),L=[{label:".mcp.json (Claude Code project)",path:o(y,".mcp.json")},{label:".cursor/mcp.json",path:o(y,".cursor","mcp.json")},{label:".windsurf/mcp.json",path:o(y,".windsurf","mcp.json")},{label:".vscode/mcp.json (Copilot/Cline)",path:o(y,".vscode","mcp.json")},{label:"~/.claude/mcp.json (global)",path:o(T,".claude","mcp.json"),global:!0}];let E=0;for(const{label:A,path:C,global:I}of L){const $=c(C);if(!(!I&&!n($)&&!n(C)))try{await s($,{recursive:!0});let k={};if(n(C))try{k=JSON.parse(await t(C,"utf-8"))}catch{try{await(await import("node:fs/promises")).rename(C,C+".bak")}catch{}}const O=k.mcpServers??{},Y=O.cachly??{},K=Y.env??{};K.CACHLY_BRAIN_INSTANCE_ID=l,d&&I&&!K.CACHLY_JWT&&(K.CACHLY_JWT=d),x!==jt&&(K.CACHLY_API_URL=x),O.cachly={...Y,command:"npx",args:["-y","@cachly-dev/mcp-server@latest"],env:K},k.mcpServers=O,await e(C,JSON.stringify(k,null,2),"utf-8"),console.log(` \u2705 ${A}`),E++}catch(k){k.code!=="ENOENT"&&console.log(` \u26A0\uFE0F ${A}: ${k.message}`)}}console.log(""),E===0?console.log(" \u26A0\uFE0F No editor configs found \u2014 run `cachly setup` first.\n"):(console.log(` \u{1F9E0} You're now sharing Brain: \x1B[1m${m}\x1B[0m`),console.log(` Restart your editor \u2014 your AI arrives pre-briefed from the team Brain.
181
176
  `),console.log(" \u{1F4DB} Add the badge to your README:"),console.log(` npx @cachly-dev/mcp-server@latest badge
182
177
  `)),process.exit(0)}if(process.argv[2]==="upgrade"){process.stdout.write(`
183
178
  \u23F3 Checking for updates...
@@ -185,178 +180,178 @@ Try it free: ${o}
185
180
  \u2705 Already on the latest version: \x1B[32mv${D}\x1B[0m
186
181
  `):(console.log(""),console.log("\x1B[35m \u2554\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2557\x1B[0m"),console.log("\x1B[35m \u2551\x1B[0m \x1B[1m\u{1F9E0} cachly update available\x1B[0m \x1B[35m\u2551\x1B[0m"),console.log("\x1B[35m \u255A\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u255D\x1B[0m"),console.log(""),console.log(` Current : \x1B[31mv${D}\x1B[0m`),console.log(` Latest : \x1B[32mv${s}\x1B[0m`),console.log(""),console.log(" Update your editor configs to pick up the new version:"),console.log(""),console.log(" \x1B[32m npx @cachly-dev/mcp-server@latest autopilot\x1B[0m"),console.log(""),console.log(" \x1B[2m(npx always fetches the latest when @latest is specified)\x1B[0m"),console.log(""))}catch(e){console.log(`
187
182
  \u274C Could not check for updates: ${e.message}
188
- `),process.exit(1)}process.exit(0)}if(process.argv[2]==="bench"){const{fileURLToPath:e}=await import("node:url"),{dirname:t,resolve:s}=await import("node:path"),{loadExternalCorpus:o,runExternalBenchmark:n}=await import("./bench/external-corpus.js"),c=process.argv.includes("--json"),i=process.argv.slice(3).find(g=>!g.startsWith("--")),a=t(e(import.meta.url)),u=i?s(process.cwd(),i):s(a,"bench","external","sample-corpus.json");try{const g=await o(u),l=await n(g);c&&(console.log(JSON.stringify({corpus:g.name,lessons:l.corpusSize,queries:l.queryCount,cachly_p1:Math.round(l.cachly.precisionAt1*1e3)/10,flatfile_p1:Math.round(l.flatfile.precisionAt1*1e3)/10,cachly_mrr:Math.round(l.cachly.mrr*1e3)/10,flatfile_mrr:Math.round(l.flatfile.mrr*1e3)/10,p1_lift_vs_flatfile_pct:Math.round((l.cachly.precisionAt1-l.flatfile.precisionAt1)*1e3)/10,mrr_lift_vs_flatfile_pct:Math.round((l.cachly.mrr-l.flatfile.mrr)*1e3)/10},null,2)),process.exit(0));const m=((l.cachly.precisionAt1-l.flatfile.precisionAt1)*100).toFixed(1),y=((l.cachly.mrr-l.flatfile.mrr)*100).toFixed(1),w=((l.cachly.precisionAt1-l.baseline.precisionAt1)*100).toFixed(1),b=l.cachly.precisionAt1>=l.flatfile.precisionAt1?"\x1B[32m+":"\x1B[31m",p=l.cachly.mrr>=l.flatfile.mrr?"\x1B[32m+":"\x1B[31m";console.log(""),console.log("\x1B[35m \u2554\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2557\x1B[0m"),console.log("\x1B[35m \u2551\x1B[0m \x1B[1m\u{1F9E0} cachly recall quality benchmark\x1B[0m \x1B[35m\u2551\x1B[0m"),console.log("\x1B[35m \u255A\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u255D\x1B[0m"),console.log(""),console.log(` Corpus : ${g.name??u}`),console.log(` Dataset: ${l.corpusSize} lessons \xB7 ${l.queryCount} queries \xB7 10 engineering domains`),console.log(" (k8s, DB, auth, CI, frontend, API, payments, observability, Node.js, infra)"),console.log(""),console.log(" \x1B[1mPrecision@1\x1B[0m (finds the right lesson at rank #1):"),console.log(` flat-file memory : \x1B[33m${(l.flatfile.precisionAt1*100).toFixed(1)}%\x1B[0m (naive keyword overlap \u2014 no quality signal)`),console.log(` raw BM25 : \x1B[33m${(l.baseline.precisionAt1*100).toFixed(1)}%\x1B[0m (keyword ranking)`),console.log(` cachly : \x1B[1m\x1B[32m${(l.cachly.precisionAt1*100).toFixed(1)}%\x1B[0m (BM25 + quality reranking)`),console.log(""),console.log(" \x1B[1mMean Reciprocal Rank\x1B[0m (average rank of the right answer):"),console.log(` flat-file memory : \x1B[33m${(l.flatfile.mrr*100).toFixed(1)}%\x1B[0m`),console.log(` raw BM25 : \x1B[33m${(l.baseline.mrr*100).toFixed(1)}%\x1B[0m`),console.log(` cachly : \x1B[1m\x1B[32m${(l.cachly.mrr*100).toFixed(1)}%\x1B[0m`),console.log(""),console.log(" \x1B[1mLift vs flat-file memory\x1B[0m (what you get by switching from plain memory files):"),console.log(` Precision@1 : ${b}${m}%\x1B[0m`),console.log(` MRR : ${p}${y}%\x1B[0m`),console.log(` vs BM25 : \x1B[32m+${w}%\x1B[0m Precision@1 (quality reranking on top of keyword search)`),console.log(""),console.log(" \x1B[2mFlat-file = lexical overlap over raw text files (how Anthropic/Claude Memory works).\x1B[0m"),console.log(" \x1B[2mAdversarial distractors included \u2014 failure lessons that share the exact query vocabulary.\x1B[0m"),console.log(" \x1B[2mcachly ignores them because they have low confidence / failed outcome.\x1B[0m"),console.log(""),console.log(" Share your results:"),console.log(` \x1B[36m"My Brain: Precision@1 ${(l.cachly.precisionAt1*100).toFixed(1)}% (${b}${m}% vs flat-file memory, ${l.queryCount} queries)"\x1B[0m`),console.log(""),console.log(" Run on a custom corpus:"),console.log(" \x1B[2m npx @cachly-dev/mcp-server@latest bench my-incidents.json\x1B[0m"),console.log(" \x1B[2m npx @cachly-dev/mcp-server@latest bench --json # machine-readable\x1B[0m"),console.log(""),console.log(" \x1B[1m\u2192 Give your AI this recall quality on your own work (free, 1\u20135 min):\x1B[0m"),console.log(" \x1B[32m$ npx @cachly-dev/mcp-server@latest autopilot\x1B[0m"),console.log(" \x1B[90m Signs in, configures every editor, bootstraps from your git history.\x1B[0m"),console.log(` \x1B[90m Or create your Brain at: \x1B[36m${N("/setup-ai","bench")}\x1B[0m`),console.log("")}catch(g){console.error(`
183
+ `),process.exit(1)}process.exit(0)}if(process.argv[2]==="bench"){const{fileURLToPath:e}=await import("node:url"),{dirname:t,resolve:s}=await import("node:path"),{loadExternalCorpus:n,runExternalBenchmark:o}=await import("./bench/external-corpus.js"),c=process.argv.includes("--json"),r=process.argv.slice(3).find(g=>!g.startsWith("--")),a=t(e(import.meta.url)),u=r?s(process.cwd(),r):s(a,"bench","external","sample-corpus.json");try{const g=await n(u),l=await o(g);c&&(console.log(JSON.stringify({corpus:g.name,lessons:l.corpusSize,queries:l.queryCount,cachly_p1:Math.round(l.cachly.precisionAt1*1e3)/10,flatfile_p1:Math.round(l.flatfile.precisionAt1*1e3)/10,cachly_mrr:Math.round(l.cachly.mrr*1e3)/10,flatfile_mrr:Math.round(l.flatfile.mrr*1e3)/10,p1_lift_vs_flatfile_pct:Math.round((l.cachly.precisionAt1-l.flatfile.precisionAt1)*1e3)/10,mrr_lift_vs_flatfile_pct:Math.round((l.cachly.mrr-l.flatfile.mrr)*1e3)/10},null,2)),process.exit(0));const m=((l.cachly.precisionAt1-l.flatfile.precisionAt1)*100).toFixed(1),h=((l.cachly.mrr-l.flatfile.mrr)*100).toFixed(1),w=((l.cachly.precisionAt1-l.baseline.precisionAt1)*100).toFixed(1),b=l.cachly.precisionAt1>=l.flatfile.precisionAt1?"\x1B[32m+":"\x1B[31m",p=l.cachly.mrr>=l.flatfile.mrr?"\x1B[32m+":"\x1B[31m";console.log(""),console.log("\x1B[35m \u2554\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2557\x1B[0m"),console.log("\x1B[35m \u2551\x1B[0m \x1B[1m\u{1F9E0} cachly recall quality benchmark\x1B[0m \x1B[35m\u2551\x1B[0m"),console.log("\x1B[35m \u255A\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u255D\x1B[0m"),console.log(""),console.log(` Corpus : ${g.name??u}`),console.log(` Dataset: ${l.corpusSize} lessons \xB7 ${l.queryCount} queries \xB7 10 engineering domains`),console.log(" (k8s, DB, auth, CI, frontend, API, payments, observability, Node.js, infra)"),console.log(""),console.log(" \x1B[1mPrecision@1\x1B[0m (finds the right lesson at rank #1):"),console.log(` flat-file memory : \x1B[33m${(l.flatfile.precisionAt1*100).toFixed(1)}%\x1B[0m (naive keyword overlap \u2014 no quality signal)`),console.log(` raw BM25 : \x1B[33m${(l.baseline.precisionAt1*100).toFixed(1)}%\x1B[0m (keyword ranking)`),console.log(` cachly : \x1B[1m\x1B[32m${(l.cachly.precisionAt1*100).toFixed(1)}%\x1B[0m (BM25 + quality reranking)`),console.log(""),console.log(" \x1B[1mMean Reciprocal Rank\x1B[0m (average rank of the right answer):"),console.log(` flat-file memory : \x1B[33m${(l.flatfile.mrr*100).toFixed(1)}%\x1B[0m`),console.log(` raw BM25 : \x1B[33m${(l.baseline.mrr*100).toFixed(1)}%\x1B[0m`),console.log(` cachly : \x1B[1m\x1B[32m${(l.cachly.mrr*100).toFixed(1)}%\x1B[0m`),console.log(""),console.log(" \x1B[1mLift vs flat-file memory\x1B[0m (what you get by switching from plain memory files):"),console.log(` Precision@1 : ${b}${m}%\x1B[0m`),console.log(` MRR : ${p}${h}%\x1B[0m`),console.log(` vs BM25 : \x1B[32m+${w}%\x1B[0m Precision@1 (quality reranking on top of keyword search)`),console.log(""),console.log(" \x1B[2mFlat-file = lexical overlap over raw text files (how Anthropic/Claude Memory works).\x1B[0m"),console.log(" \x1B[2mAdversarial distractors included \u2014 failure lessons that share the exact query vocabulary.\x1B[0m"),console.log(" \x1B[2mcachly ignores them because they have low confidence / failed outcome.\x1B[0m"),console.log(""),console.log(" Share your results:"),console.log(` \x1B[36m"My Brain: Precision@1 ${(l.cachly.precisionAt1*100).toFixed(1)}% (${b}${m}% vs flat-file memory, ${l.queryCount} queries)"\x1B[0m`),console.log(""),console.log(" Run on a custom corpus:"),console.log(" \x1B[2m npx @cachly-dev/mcp-server@latest bench my-incidents.json\x1B[0m"),console.log(" \x1B[2m npx @cachly-dev/mcp-server@latest bench --json # machine-readable\x1B[0m"),console.log(""),console.log(" \x1B[1m\u2192 Give your AI this recall quality on your own work (free, 1\u20135 min):\x1B[0m"),console.log(" \x1B[32m$ npx @cachly-dev/mcp-server@latest autopilot\x1B[0m"),console.log(" \x1B[90m Signs in, configures every editor, bootstraps from your git history.\x1B[0m"),console.log(` \x1B[90m Or create your Brain at: \x1B[36m${j("/setup-ai","bench")}\x1B[0m`),console.log("")}catch(g){console.error(`
189
184
  \u274C bench failed: ${g.message}
190
- `),process.exit(1)}process.exit(0)}if(process.argv[2]==="demo"){const{existsSync:e,readFileSync:t}=await import("node:fs"),{resolve:s,basename:o}=await import("node:path"),n=process.cwd();console.log(`
185
+ `),process.exit(1)}process.exit(0)}if(process.argv[2]==="demo"){const{existsSync:e,readFileSync:t}=await import("node:fs"),{resolve:s,basename:n}=await import("node:path"),o=process.cwd();console.log(`
191
186
  \x1B[35m\u{1F9E0} cachly Brain \u2014 Live Demo\x1B[0m`),console.log(`\x1B[90mNo account needed. Reading your git history...\x1B[0m
192
- `),e(s(n,".git"))||(console.log("\u26A0\uFE0F No git repository found here."),console.log(` Run this command inside a project directory with git history.
187
+ `),e(s(o,".git"))||(console.log("\u26A0\uFE0F No git repository found here."),console.log(` Run this command inside a project directory with git history.
193
188
  `),console.log(` Example: cd ~/my-project && npx @cachly-dev/mcp-server@latest demo
194
- `),process.exit(0));let c="";try{c=ot(n,["config","user.name"]).trim().split(" ")[0]??""}catch{}let i=o(n);try{const $=JSON.parse(t(s(n,"package.json"),"utf-8"));$.name&&(i=$.name.replace(/^@[^/]+\//,""))}catch{}let a="";try{a=ot(n,["log","HEAD","--pretty=format:%H|||%s|||%ad|||%an","--date=short","--no-merges","-n","200"],{timeoutMs:3e4})}catch{console.log(`\u26A0\uFE0F Could not read git log. Make sure you have commits.
189
+ `),process.exit(0));let c="";try{c=rt(o,["config","user.name"]).trim().split(" ")[0]??""}catch{}let r=n(o);try{const $=JSON.parse(t(s(o,"package.json"),"utf-8"));$.name&&(r=$.name.replace(/^@[^/]+\//,""))}catch{}let a="";try{a=rt(o,["log","HEAD","--pretty=format:%H|||%s|||%ad|||%an","--date=short","--no-merges","-n","200"],{timeoutMs:3e4})}catch{console.log(`\u26A0\uFE0F Could not read git log. Make sure you have commits.
195
190
  `),process.exit(0)}const u=a.trim().split(`
196
- `).filter(Boolean).map($=>{const[C,j,F,z]=$.split("|||");return{sha:(C??"").trim().slice(0,8),subject:(j??"").trim(),date:(F??"").trim(),author:(z??"").trim()}});u.length===0&&(console.log(`\u26A0\uFE0F No commits found.
197
- `),process.exit(0));const g=new Map,l=[],m=[],y=[],w=new Set;let b=0;for(const $ of u){const C=Un($.subject);g.set(C,(g.get(C)??0)+1),Wn($.subject)&&b++,C==="fix"&&l.length<5&&l.push($.subject.slice(0,72)),C==="feat"&&m.length<5&&m.push($.subject.slice(0,72)),C==="security"&&y.length<3&&y.push($.subject.slice(0,72)),$.author&&w.add($.author)}const p=Fn(u.map($=>$.subject)),r=`${u[u.length-1]?.date??"?"} \u2192 ${u[0]?.date??"?"}`,d=p>=501?"Oracle":p>=201?"Architect":p>=51?"Expert":p>=11?"Explorer":"Apprentice",T=`${c?`Hey ${c} \u2014 `:""}here's what your AI would know about ${i}`;process.stdout.write("\x1B[2K\r"),console.log("\u250C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510"),console.log(`\u2502 \x1B[1m\u{1F9E0} ${T.slice(0,58).padEnd(58)}\x1B[0m\u2502`),console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524"),console.log(`\u2502 Commits analysed : \x1B[33m${String(u.length).padEnd(6)}\x1B[0m Date range: \x1B[90m${r.slice(0,23).padEnd(23)}\x1B[0m \u2502`),console.log(`\u2502 Lessons extracted: \x1B[32m${String(p).padEnd(6)}\x1B[0m Contributors: \x1B[36m${String(w.size).padEnd(20)}\x1B[0m\u2502`),console.log(`\u2502 Brain Level : \x1B[35m${d.padEnd(10)}\x1B[0m Reverts: \x1B[33m${String(b).padEnd(21)}\x1B[0m\u2502`),console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524");const B=[...g.entries()].filter(([$])=>$!=="chore").sort(($,C)=>C[1]-$[1]),I=($,C)=>"\u2588".repeat(Math.round($/C*20)).padEnd(20),S=Math.max(...B.map(([,$])=>$),1);console.log("\u2502 \x1B[90mCategory breakdown:\x1B[0m \u2502");for(const[$,C]of B){const j={fix:"\u{1F527}",feat:"\u2728",perf:"\u26A1",security:"\u{1F512}",deploy:"\u{1F680}",refactor:"\u{1F504}"}[$]??"\u2022";console.log(`\u2502 ${j} \x1B[36m${$.padEnd(10)}\x1B[0m \x1B[35m${I(C,S)}\x1B[0m \x1B[33m${String(C).padStart(3)}\x1B[0m \u2502`)}if(y.length>0){console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524"),console.log("\u2502 \x1B[31m\u{1F512} Security fixes your AI would know:\x1B[0m \u2502");for(const $ of y)console.log(`\u2502 \x1B[90m\u2022 ${$.slice(0,58).padEnd(58)}\x1B[0m \u2502`)}if(l.length>0){console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524"),console.log("\u2502 \x1B[33m\u{1F527} Bug fixes your AI would never repeat:\x1B[0m \u2502");for(const $ of l.slice(0,4))console.log(`\u2502 \x1B[90m\u2022 ${$.slice(0,58).padEnd(58)}\x1B[0m \u2502`)}if(m.length>0){console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524"),console.log("\u2502 \x1B[32m\u2728 Features & decisions it would recall:\x1B[0m \u2502");for(const $ of m.slice(0,3))console.log(`\u2502 \x1B[90m\u2022 ${$.slice(0,58).padEnd(58)}\x1B[0m \u2502`)}console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524"),console.log("\u2502 \x1B[32m\u{1F4A1} With cachly, your AI arrives pre-briefed every session.\x1B[0m \u2502"),console.log("\u2502 \x1B[32m No more re-explaining. No more repeated mistakes.\x1B[0m \u2502"),console.log("\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518"),console.log("");const k=new URLSearchParams({repo:i,commits:String(u.length),lessons:String(p),level:d,authors:String(w.size),reverts:String(b)}),E=N(`/preview?${k.toString()}`,"demo");console.log(" \x1B[1mMake this permanent (free, 1\u20135 minutes):\x1B[0m"),console.log(" \x1B[32m$ npx @cachly-dev/mcp-server@latest autopilot\x1B[0m"),console.log(""),console.log(` \x1B[90m\u{1F517} Shareable preview:\x1B[0m \x1B[36m${E}\x1B[0m`),console.log(""),console.log(" Works with: Claude Code \xB7 Cursor \xB7 Windsurf \xB7 Copilot \xB7 Cline \xB7 Zed"),console.log(" GDPR \xB7 German servers \xB7 free tier, no credit card"),console.log(""),process.exit(0)}if(process.argv[2]==="share"){const e=process.env.CACHLY_JWT??"",t=process.env.CACHLY_BRAIN_INSTANCE_ID??"";(!e||!t)&&(console.log(`
198
- \u26A0\uFE0F CACHLY_JWT and CACHLY_BRAIN_INSTANCE_ID must be set.`),console.log(` Run: npx @cachly-dev/mcp-server@latest autopilot (takes 1\u20135 minutes)
191
+ `).filter(Boolean).map($=>{const[k,O,Y,K]=$.split("|||");return{sha:(k??"").trim().slice(0,8),subject:(O??"").trim(),date:(Y??"").trim(),author:(K??"").trim()}});u.length===0&&(console.log(`\u26A0\uFE0F No commits found.
192
+ `),process.exit(0));const g=new Map,l=[],m=[],h=[],w=new Set;let b=0;for(const $ of u){const k=qn($.subject);g.set(k,(g.get(k)??0)+1),Gn($.subject)&&b++,k==="fix"&&l.length<5&&l.push($.subject.slice(0,72)),k==="feat"&&m.length<5&&m.push($.subject.slice(0,72)),k==="security"&&h.length<3&&h.push($.subject.slice(0,72)),$.author&&w.add($.author)}const p=Vn(u.map($=>$.subject)),i=`${u[u.length-1]?.date??"?"} \u2192 ${u[0]?.date??"?"}`,d=p>=501?"Oracle":p>=201?"Architect":p>=51?"Expert":p>=11?"Explorer":"Apprentice",T=`${c?`Hey ${c} \u2014 `:""}here's what your AI would know about ${r}`;process.stdout.write("\x1B[2K\r"),console.log("\u250C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510"),console.log(`\u2502 \x1B[1m\u{1F9E0} ${T.slice(0,58).padEnd(58)}\x1B[0m\u2502`),console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524"),console.log(`\u2502 Commits analysed : \x1B[33m${String(u.length).padEnd(6)}\x1B[0m Date range: \x1B[90m${i.slice(0,23).padEnd(23)}\x1B[0m \u2502`),console.log(`\u2502 Lessons extracted: \x1B[32m${String(p).padEnd(6)}\x1B[0m Contributors: \x1B[36m${String(w.size).padEnd(20)}\x1B[0m\u2502`),console.log(`\u2502 Brain Level : \x1B[35m${d.padEnd(10)}\x1B[0m Reverts: \x1B[33m${String(b).padEnd(21)}\x1B[0m\u2502`),console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524");const L=[...g.entries()].filter(([$])=>$!=="chore").sort(($,k)=>k[1]-$[1]),E=($,k)=>"\u2588".repeat(Math.round($/k*20)).padEnd(20),A=Math.max(...L.map(([,$])=>$),1);console.log("\u2502 \x1B[90mCategory breakdown:\x1B[0m \u2502");for(const[$,k]of L){const O={fix:"\u{1F527}",feat:"\u2728",perf:"\u26A1",security:"\u{1F512}",deploy:"\u{1F680}",refactor:"\u{1F504}"}[$]??"\u2022";console.log(`\u2502 ${O} \x1B[36m${$.padEnd(10)}\x1B[0m \x1B[35m${E(k,A)}\x1B[0m \x1B[33m${String(k).padStart(3)}\x1B[0m \u2502`)}if(h.length>0){console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524"),console.log("\u2502 \x1B[31m\u{1F512} Security fixes your AI would know:\x1B[0m \u2502");for(const $ of h)console.log(`\u2502 \x1B[90m\u2022 ${$.slice(0,58).padEnd(58)}\x1B[0m \u2502`)}if(l.length>0){console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524"),console.log("\u2502 \x1B[33m\u{1F527} Bug fixes your AI would never repeat:\x1B[0m \u2502");for(const $ of l.slice(0,4))console.log(`\u2502 \x1B[90m\u2022 ${$.slice(0,58).padEnd(58)}\x1B[0m \u2502`)}if(m.length>0){console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524"),console.log("\u2502 \x1B[32m\u2728 Features & decisions it would recall:\x1B[0m \u2502");for(const $ of m.slice(0,3))console.log(`\u2502 \x1B[90m\u2022 ${$.slice(0,58).padEnd(58)}\x1B[0m \u2502`)}console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524"),console.log("\u2502 \x1B[32m\u{1F4A1} With cachly, your AI arrives pre-briefed every session.\x1B[0m \u2502"),console.log("\u2502 \x1B[32m No more re-explaining. No more repeated mistakes.\x1B[0m \u2502"),console.log("\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518"),console.log("");const C=new URLSearchParams({repo:r,commits:String(u.length),lessons:String(p),level:d,authors:String(w.size),reverts:String(b)}),I=j(`/preview?${C.toString()}`,"demo");console.log(" \x1B[1mMake this permanent (free, 1\u20135 minutes):\x1B[0m"),console.log(" \x1B[32m$ npx @cachly-dev/mcp-server@latest autopilot\x1B[0m"),console.log(""),console.log(` \x1B[90m\u{1F517} Shareable preview:\x1B[0m \x1B[36m${I}\x1B[0m`),console.log(""),console.log(" Works with: Claude Code \xB7 Cursor \xB7 Windsurf \xB7 Copilot \xB7 Cline \xB7 Zed"),console.log(" GDPR \xB7 German servers \xB7 free tier, no credit card"),console.log(""),process.exit(0)}if(process.argv[2]==="share"){const e=oe(),t=F();(!e||!t)&&(console.log(`
193
+ \u26A0\uFE0F No cachly key or Brain instance found (environment or ~/.cachly/credentials.json).`),console.log(` Run: npx @cachly-dev/mcp-server@latest autopilot (takes 1\u20135 minutes)
199
194
  `),process.exit(1)),process.stdout.write(`
200
195
  \u{1F9E0} Fetching your Brain stats...
201
- `);try{const s=await fetch(`${x}/api/v1/instances/${t}/memory`,{headers:{Authorization:`Bearer ${e}`},signal:AbortSignal.timeout(8e3)});if(!s.ok)throw new Error(`HTTP ${s.status}`);const o=await s.json(),n=o.lesson_count??0,c=o.total_recall_count??0,i=Math.round(o.iq_boost_pct??0),a=c*1200,u=(a*3e-6).toFixed(2),g=n===0?"Intern \u{1F331}":n<10?"Junior Dev \u{1F527}":n<30?"Mid Dev \u26A1":n<60?"Senior Dev \u{1F9E0}":n<100?"Staff Eng \u{1F680}":"Principal Eng \u{1F3C6}";console.log(""),console.log("\u250C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510"),console.log("\u2502 \u{1F9E0} My AI Brain \xB7 powered by \x1B[35mcachly.dev\x1B[0m \u2502"),console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524"),console.log(`\u2502 Lessons stored : \x1B[33m${String(n).padEnd(8)}\x1B[0m Brain level: \x1B[32m${g.padEnd(18)}\x1B[0m\u2502`),console.log(`\u2502 Total recalls : \x1B[36m${String(c).padEnd(8)}\x1B[0m Quality score: \x1B[32m${String(i).padEnd(3)}%\x1B[0m \u2502`),console.log(`\u2502 Tokens saved : \x1B[32m${((a/1e3).toFixed(0)+"K").padEnd(8)}\x1B[0m Cost saved: \x1B[32m$${u.padEnd(20)}\x1B[0m\u2502`),console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524"),console.log("\u2502 Works with: Claude Code \xB7 Cursor \xB7 Windsurf \xB7 Copilot \u2502"),console.log("\u2502 Free \xB7 GDPR \xB7 German servers \u2502"),console.log("\u2502 \x1B[35mnpx @cachly-dev/mcp-server@latest autopilot\x1B[0m \u2502"),console.log("\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518"),console.log("");const l=N("/","share"),m=`\u{1F9E0} My AI coding assistant now has persistent memory.
196
+ `);try{const s=await fetch(`${x}/api/v1/instances/${t}/memory`,{headers:{Authorization:`Bearer ${e}`},signal:AbortSignal.timeout(8e3)});if(!s.ok)throw new Error(`HTTP ${s.status}`);const n=await s.json(),o=n.lesson_count??0,c=n.total_recall_count??0,r=Math.round(n.iq_boost_pct??0),a=c*1200,u=(a*3e-6).toFixed(2),g=o===0?"Intern \u{1F331}":o<10?"Junior Dev \u{1F527}":o<30?"Mid Dev \u26A1":o<60?"Senior Dev \u{1F9E0}":o<100?"Staff Eng \u{1F680}":"Principal Eng \u{1F3C6}";console.log(""),console.log("\u250C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510"),console.log("\u2502 \u{1F9E0} My AI Brain \xB7 powered by \x1B[35mcachly.dev\x1B[0m \u2502"),console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524"),console.log(`\u2502 Lessons stored : \x1B[33m${String(o).padEnd(8)}\x1B[0m Brain level: \x1B[32m${g.padEnd(18)}\x1B[0m\u2502`),console.log(`\u2502 Total recalls : \x1B[36m${String(c).padEnd(8)}\x1B[0m Quality score: \x1B[32m${String(r).padEnd(3)}%\x1B[0m \u2502`),console.log(`\u2502 Tokens saved : \x1B[32m${((a/1e3).toFixed(0)+"K").padEnd(8)}\x1B[0m Cost saved: \x1B[32m$${u.padEnd(20)}\x1B[0m\u2502`),console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524"),console.log("\u2502 Works with: Claude Code \xB7 Cursor \xB7 Windsurf \xB7 Copilot \u2502"),console.log("\u2502 Free \xB7 GDPR \xB7 German servers \u2502"),console.log("\u2502 \x1B[35mnpx @cachly-dev/mcp-server@latest autopilot\x1B[0m \u2502"),console.log("\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518"),console.log("");const l=j("/","share"),m=`\u{1F9E0} My AI coding assistant now has persistent memory.
202
197
 
203
- ${n} lessons stored \xB7 ${c} recalls \xB7 ${g}
198
+ ${o} lessons stored \xB7 ${c} recalls \xB7 ${g}
204
199
 
205
200
  Fix a bug once \u2192 AI remembers forever. Across Claude Code, Cursor, Windsurf, Copilot.
206
201
 
207
202
  ${l}
208
203
 
209
204
  #AIMemory #ClaudeCode #Cursor #DeveloperTools`;console.log(" \x1B[1m\u{1F4CB} Share on Twitter/X (copy this):\x1B[0m"),console.log(" \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"),console.log(m.split(`
210
- `).map(y=>` ${y}`).join(`
205
+ `).map(h=>` ${h}`).join(`
211
206
  `)),console.log(" \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"),console.log(""),console.log(" \x1B[2mPro tip: add a screenshot of your Brain panel for 3x more engagement\x1B[0m"),console.log(" \x1B[2mAlso add a live badge to your README:\x1B[0m"),console.log(" \x1B[90m npx @cachly-dev/mcp-server@latest badge\x1B[0m"),console.log("")}catch(s){console.log(`
212
207
  \u274C Could not fetch stats: ${s.message}
213
- `),process.exit(1)}process.exit(0)}if(process.argv[2]==="publish"){const e=process.env.CACHLY_JWT??"",t=process.env.CACHLY_BRAIN_INSTANCE_ID??"";(!e||!t)&&(console.log(`
214
- \u26A0\uFE0F CACHLY_JWT and CACHLY_BRAIN_INSTANCE_ID must be set.`),console.log(` Run: npx @cachly-dev/mcp-server@latest autopilot
215
- `),process.exit(1));const s=process.argv.indexOf("--title"),o=s!==-1?process.argv[s+1]??"My Brain Snapshot":"My Brain Snapshot",n=process.argv.includes("--public");process.stdout.write(`
208
+ `),process.exit(1)}process.exit(0)}if(process.argv[2]==="publish"){const e=oe(),t=F();(!e||!t)&&(console.log(`
209
+ \u26A0\uFE0F No cachly key or Brain instance found (environment or ~/.cachly/credentials.json).`),console.log(` Run: npx @cachly-dev/mcp-server@latest autopilot
210
+ `),process.exit(1));const s=process.argv.indexOf("--title"),n=s!==-1?process.argv[s+1]??"My Brain Snapshot":"My Brain Snapshot",o=process.argv.includes("--public");process.stdout.write(`
216
211
  \u{1F9E0} Publishing Brain snapshot...
217
- `);try{const c=await fetch(`${x}/api/v1/instances/${t}/memory`,{headers:{Authorization:`Bearer ${e}`},signal:AbortSignal.timeout(8e3)});let i=0,a=0;if(c.ok){const y=await c.json();i=y.lesson_count??0,a=y.total_recall_count??0}const u=await fetch(`${x}/api/v1/brains/share`,{method:"POST",headers:{Authorization:`Bearer ${e}`,"Content-Type":"application/json"},body:JSON.stringify({title:o,visibility:n?"public":"unlisted",instance_id:t}),signal:AbortSignal.timeout(15e3)});if(!u.ok)throw new Error(`HTTP ${u.status}`);const l=(await u.json()).share_id??"unavailable",m=N(`/brain/share/${l}`,"publish");console.log(""),console.log("\u250C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510"),console.log("\u2502 \u{1F4E4} Brain Published \xB7 powered by \x1B[35mcachly.dev\x1B[0m \u2502"),console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524"),console.log(`\u2502 Title : \x1B[33m${o.slice(0,44).padEnd(44)}\x1B[0m\u2502`),console.log(`\u2502 Lessons : \x1B[32m${String(i).padEnd(10)}\x1B[0m Recalls: \x1B[36m${String(a).padEnd(26)}\x1B[0m\u2502`),console.log(`\u2502 Visibility: \x1B[${n?"32":"33"}m${(n?"public (discoverable)":"unlisted (link only)").padEnd(44)}\x1B[0m\u2502`),console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524"),console.log(`\u2502 \x1B[1mShare URL:\x1B[0m \x1B[36m${m.padEnd(51)}\x1B[0m\u2502`),console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524"),console.log("\u2502 \x1B[2mAnyone can import with:\x1B[0m \u2502"),console.log(`\u2502 \x1B[90m brain_import(instance_id="...", share_id="${l.slice(0,17)}")\x1B[0m\u2502`),console.log("\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518"),console.log(""),console.log(" \x1B[1m\u{1F4CB} Share this with your team:\x1B[0m"),console.log(` ${m}`),console.log(""),console.log(` \x1B[2mTo list all your shares:\x1B[0m brain_share_list(instance_id="${t.slice(0,24)}...")`),console.log(` \x1B[2mTo revoke this share:\x1B[0m brain_unshare(share_id="${l.slice(0,24)}...")`),console.log("")}catch(c){console.log(`
212
+ `);try{const c=await fetch(`${x}/api/v1/instances/${t}/memory`,{headers:{Authorization:`Bearer ${e}`},signal:AbortSignal.timeout(8e3)});let r=0,a=0;if(c.ok){const h=await c.json();r=h.lesson_count??0,a=h.total_recall_count??0}const u=await fetch(`${x}/api/v1/brains/share`,{method:"POST",headers:{Authorization:`Bearer ${e}`,"Content-Type":"application/json"},body:JSON.stringify({title:n,visibility:o?"public":"unlisted",instance_id:t}),signal:AbortSignal.timeout(15e3)});if(!u.ok)throw new Error(`HTTP ${u.status}`);const l=(await u.json()).share_id??"unavailable",m=j(`/brain/share/${l}`,"publish");console.log(""),console.log("\u250C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510"),console.log("\u2502 \u{1F4E4} Brain Published \xB7 powered by \x1B[35mcachly.dev\x1B[0m \u2502"),console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524"),console.log(`\u2502 Title : \x1B[33m${n.slice(0,44).padEnd(44)}\x1B[0m\u2502`),console.log(`\u2502 Lessons : \x1B[32m${String(r).padEnd(10)}\x1B[0m Recalls: \x1B[36m${String(a).padEnd(26)}\x1B[0m\u2502`),console.log(`\u2502 Visibility: \x1B[${o?"32":"33"}m${(o?"public (discoverable)":"unlisted (link only)").padEnd(44)}\x1B[0m\u2502`),console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524"),console.log(`\u2502 \x1B[1mShare URL:\x1B[0m \x1B[36m${m.padEnd(51)}\x1B[0m\u2502`),console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524"),console.log("\u2502 \x1B[2mAnyone can import with:\x1B[0m \u2502"),console.log(`\u2502 \x1B[90m brain_import(instance_id="...", share_id="${l.slice(0,17)}")\x1B[0m\u2502`),console.log("\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518"),console.log(""),console.log(" \x1B[1m\u{1F4CB} Share this with your team:\x1B[0m"),console.log(` ${m}`),console.log(""),console.log(` \x1B[2mTo list all your shares:\x1B[0m brain_share_list(instance_id="${t.slice(0,24)}...")`),console.log(` \x1B[2mTo revoke this share:\x1B[0m brain_unshare(share_id="${l.slice(0,24)}...")`),console.log("")}catch(c){console.log(`
218
213
  \u274C Could not publish Brain: ${c.message}
219
- `),process.exit(1)}process.exit(0)}if(process.argv[2]==="tool-specs"||process.argv[2]==="openapi"){const{renderToolSpecs:e}=await import("./toolspecs.js");let t="openapi";if(process.argv[2]!=="openapi"){const o=process.argv.find(n=>n.startsWith("--format="));if(o)t=o.slice(9);else{const n=process.argv.indexOf("--format");n!==-1&&(t=process.argv[n+1]??"openapi")}}const s=["openapi","openai","anthropic","langchain"];s.includes(t)||(process.stderr.write(`
214
+ `),process.exit(1)}process.exit(0)}if(process.argv[2]==="tool-specs"||process.argv[2]==="openapi"){const{renderToolSpecs:e}=await import("./toolspecs.js");let t="openapi";if(process.argv[2]!=="openapi"){const n=process.argv.find(o=>o.startsWith("--format="));if(n)t=n.slice(9);else{const o=process.argv.indexOf("--format");o!==-1&&(t=process.argv[o+1]??"openapi")}}const s=["openapi","openai","anthropic","langchain"];s.includes(t)||(process.stderr.write(`
220
215
  \u274C Unknown format "${t}". Use one of: ${s.join(", ")}
221
216
 
222
- `),process.exit(1)),process.stdout.write(e(ue,t,D)+`
223
- `,()=>process.exit(0))}if(process.argv[2]==="badge"){const e=process.env.CACHLY_BRAIN_INSTANCE_ID??"";e||(console.log(`
224
- \u26A0\uFE0F CACHLY_BRAIN_INSTANCE_ID must be set.`),console.log(` Run: npx @cachly-dev/mcp-server@latest autopilot (takes 1\u20135 minutes)
225
- `),process.exit(1));const t=`${x}/api/v1/badge/${e}`,s=N("/","badge"),o=`[![cachly Brain](${t})](${s})`,n=`<a href="${s}"><img src="${t}" alt="cachly Brain" /></a>`;console.log(""),console.log("\x1B[35m \u2554\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2557\x1B[0m"),console.log("\x1B[35m \u2551\x1B[0m \x1B[1m\u{1F9E0} cachly Brain Badge\x1B[0m \x1B[35m\u2551\x1B[0m"),console.log("\x1B[35m \u255A\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u255D\x1B[0m"),console.log(""),console.log(" Paste into your README.md:"),console.log(""),console.log(` \x1B[32m${o}\x1B[0m`),console.log(""),console.log(" \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"),console.log(" Or paste into HTML:"),console.log(""),console.log(` \x1B[36m${n}\x1B[0m`),console.log(""),console.log(" \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"),console.log(" Badge URL (live, updates every hour):"),console.log(` \x1B[90m${t}\x1B[0m`),console.log(""),console.log(" \x1B[2mThe badge shows your current lesson count \u2014 no auth required,"),console.log(" safe to embed in public repos. Updates hourly.\x1B[0m"),console.log(""),process.exit(0)}!process.argv[2]&&process.stdout.isTTY&&(console.log(""),console.log("\x1B[35m \u2554\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2557\x1B[0m"),console.log("\x1B[35m \u2551\x1B[0m \x1B[1m\u{1F9E0} cachly \u2014 Persistent AI Memory for Developers\x1B[0m \x1B[35m\u2551\x1B[0m"),console.log("\x1B[35m \u255A\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u255D\x1B[0m"),console.log(""),console.log(" Stop re-teaching your AI every morning."),console.log(" cachly gives it a permanent brain \u2014 pre-briefed every session."),console.log(""),console.log(" \x1B[1mCommands:\x1B[0m"),console.log(" \x1B[1m\x1B[32m npx @cachly-dev/mcp-server@latest autopilot\x1B[0m \u2190 \x1B[1mOne command, fully ready\x1B[0m"),console.log(" \x1B[32m npx @cachly-dev/mcp-server@latest demo\x1B[0m \u2190 Try it first (no account)"),console.log(" \x1B[36m npx @cachly-dev/mcp-server@latest setup\x1B[0m \u2190 Interactive setup (pick editors)"),console.log(" \x1B[36m npx @cachly-dev/mcp-server@latest claim\x1B[0m \u2190 Keep a test Brain in your own account"),console.log(" \x1B[36m npx @cachly-dev/mcp-server@latest health\x1B[0m \u2190 Check everything works"),console.log(" \x1B[36m npx @cachly-dev/mcp-server@latest doctor\x1B[0m \u2190 Diagnose a broken or missing install"),console.log(" \x1B[36m npx @cachly-dev/mcp-server@latest digest\x1B[0m \u2190 Weekly Brain summary"),console.log(" \x1B[36m npx @cachly-dev/mcp-server@latest share\x1B[0m \u2190 Share your Brain stats"),console.log(" \x1B[36m npx @cachly-dev/mcp-server@latest badge\x1B[0m \u2190 README badge for your Brain"),console.log(" \x1B[36m npx @cachly-dev/mcp-server@latest invite\x1B[0m \u2190 Invite a teammate"),console.log(" \x1B[36m npx @cachly-dev/mcp-server@latest join <token>\x1B[0m \u2190 Accept a Brain invite"),console.log(" \x1B[36m npx @cachly-dev/mcp-server@latest bench\x1B[0m \u2190 Recall quality vs flat-file memory"),console.log(" \x1B[36m npx @cachly-dev/mcp-server@latest upgrade\x1B[0m \u2190 Check for updates"),console.log(""),console.log(" \x1B[90mWorks with: Claude Code \xB7 Cursor \xB7 Windsurf \xB7 GitHub Copilot \xB7 Cline \xB7 Zed\x1B[0m"),console.log(` \x1B[90mGDPR \xB7 German servers \xB7 ${ue.length} MCP tools\x1B[0m`),console.log(""),process.exit(0));const ds={cursor:{datei:".cursor/hooks.json",name:"Cursor",wie:"session briefing, per-prompt recall after the first tool call"},copilot:{datei:Wo,name:"VS Code / Copilot",wie:"session briefing, per-prompt recall"},codex:{datei:Fo,name:"Codex",wie:"session briefing, per-prompt recall; trust it once in /hooks"},gemini:{datei:zo,name:"Gemini CLI",wie:"session briefing, per-prompt recall"}};function us(e,t,s){if(!t)return;const{datei:o,name:n,wie:c}=ds[e];if(t.hooks==="skipped"){console.log(`\u26A0\uFE0F ${s}${o} ${t.grund??"is not valid JSON"} \u2014 left untouched, ${n} hooks not wired`);return}const i=t.hooks==="unchanged"?"\u2713 ":"\u2705 ",a=t.hooks==="written"?"Written":t.hooks==="merged"?"Merged":"Already wired";console.log(`${i}${s}${a}: ${o} (Ambient Recall for ${n} \u2014 ${c})`)}async function ge(e,t,s,o){const n={cursor:Do,copilot:Jo,codex:Yo,gemini:Uo}[e];us(e,await n(t,s).catch(()=>null),o)}if(process.argv[2]==="init"){const e=Date.now(),{writeFile:t,mkdir:s,readFile:o}=await import("node:fs/promises"),{resolve:n,dirname:c}=await import("node:path"),i=process.argv.slice(3),a=r=>{const d=i.indexOf(`--${r}`);return d!==-1?i[d+1]:void 0};let u=a("instance-id")??process.env.CACHLY_BRAIN_INSTANCE_ID,g=a("api-key")??process.env.CACHLY_JWT;const l=(a("editor")??"claude").toLowerCase(),m=n(a("project-dir")??"."),y=a("api-url");if(y&&(x=y.replace(/\/+$/,"")),!u||!g)try{const{existsSync:r}=await import("node:fs"),d=process.env.HOME??process.env.USERPROFILE??"",h=d?n(d,".claude","mcp.json"):"";if(h&&r(h)){const B=JSON.parse(await o(h,"utf-8")).mcpServers?.cachly?.env??{};g??=B.CACHLY_JWT,u??=B.CACHLY_BRAIN_INSTANCE_ID,g&&u&&console.log("\u2713 Reusing credentials from ~/.claude/mcp.json (saved by a prior setup)")}}catch{}(!u||!g)&&(console.error(`
217
+ `),process.exit(1)),process.stdout.write(e(ge,t,D)+`
218
+ `,()=>process.exit(0))}if(process.argv[2]==="badge"){const e=F();e||(console.log(`
219
+ \u26A0\uFE0F No Brain instance found (environment or ~/.cachly/credentials.json).`),console.log(` Run: npx @cachly-dev/mcp-server@latest autopilot (takes 1\u20135 minutes)
220
+ `),process.exit(1));const t=`${x}/api/v1/badge/${e}`,s=j("/","badge"),n=`[![cachly Brain](${t})](${s})`,o=`<a href="${s}"><img src="${t}" alt="cachly Brain" /></a>`;console.log(""),console.log("\x1B[35m \u2554\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2557\x1B[0m"),console.log("\x1B[35m \u2551\x1B[0m \x1B[1m\u{1F9E0} cachly Brain Badge\x1B[0m \x1B[35m\u2551\x1B[0m"),console.log("\x1B[35m \u255A\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u255D\x1B[0m"),console.log(""),console.log(" Paste into your README.md:"),console.log(""),console.log(` \x1B[32m${n}\x1B[0m`),console.log(""),console.log(" \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"),console.log(" Or paste into HTML:"),console.log(""),console.log(` \x1B[36m${o}\x1B[0m`),console.log(""),console.log(" \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"),console.log(" Badge URL (live, updates every hour):"),console.log(` \x1B[90m${t}\x1B[0m`),console.log(""),console.log(" \x1B[2mThe badge shows your current lesson count \u2014 no auth required,"),console.log(" safe to embed in public repos. Updates hourly.\x1B[0m"),console.log(""),process.exit(0)}if(!process.argv[2]&&process.stdout.isTTY){console.log(""),console.log("\x1B[35m \u2554\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2557\x1B[0m"),console.log("\x1B[35m \u2551\x1B[0m \x1B[1m\u{1F9E0} cachly \u2014 Persistent AI Memory for Developers\x1B[0m \x1B[35m\u2551\x1B[0m"),console.log("\x1B[35m \u255A\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u255D\x1B[0m"),console.log(""),console.log(" Stop re-teaching your AI every morning."),console.log(" cachly gives it a permanent brain \u2014 pre-briefed every session."),console.log("");const e=(t,s)=>console.log(` \x1B[36m ${t.padEnd(22)}\x1B[0m ${s}`);console.log(" \x1B[1mStart:\x1B[0m \x1B[1m\x1B[32mnpx @cachly-dev/mcp-server@latest autopilot\x1B[0m \u2190 \x1B[1mOne command, fully ready\x1B[0m"),console.log(" Every command below runs as: npx @cachly-dev/mcp-server@latest <command>"),console.log(""),console.log(" \x1B[1mEvery day:\x1B[0m"),e("status","Lessons, recalls and team at a glance"),e("ambient-stats","Tokens injected vs saved \u2014 the honest net, even when negative"),e("doctor","Diagnose a broken or missing install"),e("health","Check key, API, editor configs and git hook"),e("digest","What your Brain learned this week"),console.log(" \x1B[1mSet up:\x1B[0m"),e("demo","Try it first (no account)"),e("setup","Interactive setup (pick editors)"),e("init","Write the config for one editor (flags)"),e("claim","Keep a test Brain in your own account"),e("upgrade","Check for updates"),e("uninstall","Take cachly out of this project (--dry-run first)"),console.log(" \x1B[1mYour data:\x1B[0m"),e("export","Write your lessons to disk (md / jsonl)"),e("index [dir]","Index a project into the Brain (CI)"),e("learn-git [repo]","Learn lessons from recent commits (CI)"),console.log(" \x1B[1mTeam & sharing:\x1B[0m"),e("invite","Invite a teammate"),e("join <token>","Accept a Brain invite"),e("share","Share your Brain stats"),e("publish","Publish an importable Brain snapshot"),e("badge","README badge for your Brain"),console.log(" \x1B[1mMore:\x1B[0m"),e("bench","Recall quality vs flat-file memory"),e("tool-specs | openapi","Tool definitions for other agent frameworks"),console.log(""),console.log(" \x1B[90mWorks with: Claude Code \xB7 Cursor \xB7 Windsurf \xB7 GitHub Copilot \xB7 Cline \xB7 Zed\x1B[0m"),console.log(` \x1B[90mGDPR \xB7 German servers \xB7 ${ge.length} MCP tools\x1B[0m`),console.log(""),process.exit(0)}const gs={cursor:{datei:".cursor/hooks.json",name:"Cursor",wie:"session briefing, per-prompt recall after the first tool call"},copilot:{datei:Vo,name:"VS Code / Copilot",wie:"session briefing, per-prompt recall"},codex:{datei:Zo,name:"Codex",wie:"session briefing, per-prompt recall; trust it once in /hooks"},gemini:{datei:Qo,name:"Gemini CLI",wie:"session briefing, per-prompt recall"}};function fs(e,t,s){if(!t)return;const{datei:n,name:o,wie:c}=gs[e];if(t.hooks==="skipped"){console.log(`\u26A0\uFE0F ${s}${n} ${t.grund??"is not valid JSON"} \u2014 left untouched, ${o} hooks not wired`);return}const r=t.hooks==="unchanged"?"\u2713 ":"\u2705 ",a=t.hooks==="written"?"Written":t.hooks==="merged"?"Merged":"Already wired";console.log(`${r}${s}${a}: ${n} (Ambient Recall for ${o} \u2014 ${c})`)}async function ye(e,t,s,n){const o={cursor:Yo,copilot:Ko,codex:qo,gemini:Go}[e];fs(e,await o(t,s).catch(()=>null),n)}if(process.argv[2]==="init"){const e=Date.now(),{writeFile:t,mkdir:s,readFile:n}=await import("node:fs/promises"),{resolve:o,dirname:c}=await import("node:path"),r=process.argv.slice(3),a=i=>{const d=r.indexOf(`--${i}`);return d!==-1?r[d+1]:void 0};let u=a("instance-id")??process.env.CACHLY_BRAIN_INSTANCE_ID,g=a("api-key")??process.env.CACHLY_JWT;const l=(a("editor")??"claude").toLowerCase(),m=o(a("project-dir")??"."),h=a("api-url");if(h&&(x=h.replace(/\/+$/,"")),!u||!g)try{const{existsSync:i}=await import("node:fs"),d=process.env.HOME??process.env.USERPROFILE??"",y=d?o(d,".claude","mcp.json"):"";if(y&&i(y)){const L=JSON.parse(await n(y,"utf-8")).mcpServers?.cachly?.env??{};g??=L.CACHLY_JWT,u??=L.CACHLY_BRAIN_INSTANCE_ID,g&&u&&console.log("\u2713 Reusing credentials from ~/.claude/mcp.json (saved by a prior setup)")}}catch{}(!u||!g)&&(console.error(`
226
221
  Usage: npx @cachly-dev/mcp-server@latest init --instance-id <uuid> --api-key <cky_live_...> [--editor claude|cursor|windsurf|copilot|continue|codex|gemini] [--project-dir /path] [--api-url https://your-self-hosted-backend]
227
222
  `),console.error(`First time? Run the zero-config wizard (signs you in, auto-provisions): npx @cachly-dev/mcp-server@latest autopilot
228
- `),console.error(`Get your credentials from: ${N("/setup-ai","init")}
229
- `),process.exit(1));const w=as(l,oe(g,u));if(w)Z(g),console.log(`
230
- \u{1F449} Register the MCP server once (writes ${w.wo}):`),console.log(` ${w.befehl}`);else{const r=ve[l]??".mcp.json",d=n(m,r);await s(c(d),{recursive:!0});const{existsSync:h}=await import("node:fs"),T=h(d),B=T?await o(d,"utf-8").catch(()=>""):"";Z(g);const I=await Rt(d,g,u,l,{readFile:o,existsSync:h});I!==B?(await t(d,I,"utf-8"),console.log(`
231
- \u2705 ${T?"Updated":"Written"}: ${r}`)):console.log(`
232
- \u2713 Already configured: ${r} (no change)`)}const b=await Lt(m,u);for(const[r,d]of Object.entries(b)){const h=d==="updated"?"\u2705 Updated":d==="appended"?"\u2705 Appended to":"\u2705 Written",T=r.split(/[\\/]/).pop()??r;console.log(`${h}: ${T}`)}try{const r=await et(m,u);r==="written"?console.log("\u2705 Written: .git/hooks/post-commit (CLS hook)"):r==="upgraded"?console.log(`\u2705 Upgraded: .git/hooks/post-commit (CLS hook \u2192 ${tt})`):r==="appended"?console.log("\u2705 Appended: .git/hooks/post-commit (CLS hook)"):r==="unchanged"&&console.log("\u2713 CLS hook already current in .git/hooks/post-commit")}catch{}if(l==="claude")try{const r=await nt(m,u),d=r.scripts==="written"?"\u2705 Written":r.scripts==="upgraded"?`\u2705 Upgraded (\u2192 ${st})`:"\u2713 Unchanged";console.log(`${d}: .claude/hooks/ (Ambient Recall \u2014 session briefing, per-prompt recall, file briefing, auto-learn)`),r.settings==="written"?console.log("\u2705 Written: .claude/settings.json (Ambient Recall hooks wired)"):r.settings==="merged"?console.log("\u2705 Merged: .claude/settings.json (Ambient Recall hooks wired)"):console.log("\u2713 Ambient Recall hooks already wired in .claude/settings.json")}catch{}(l==="cursor"||l==="copilot"||l==="codex"||l==="gemini")&&await ge(l,m,u,"");const p=((Date.now()-e)/1e3).toFixed(1);console.log(`
223
+ `),console.error(`Get your credentials from: ${j("/setup-ai","init")}
224
+ `),process.exit(1));const w=ps(l,se(g,u));if(w)Q(g),console.log(`
225
+ \u{1F449} Register the MCP server once (writes ${w.wo}):`),console.log(` ${w.befehl}`);else{const i=me[l]??".mcp.json",d=o(m,i);await s(c(d),{recursive:!0});const{existsSync:y}=await import("node:fs"),T=y(d),L=T?await n(d,"utf-8").catch(()=>""):"";Q(g);const E=await Lt(d,g,u,l,{readFile:n,existsSync:y});E!==L?(await t(d,E,"utf-8"),console.log(`
226
+ \u2705 ${T?"Updated":"Written"}: ${i}`)):console.log(`
227
+ \u2713 Already configured: ${i} (no change)`)}const b=await Pt(m,u);for(const[i,d]of Object.entries(b)){const y=d==="updated"?"\u2705 Updated":d==="appended"?"\u2705 Appended to":"\u2705 Written",T=i.split(/[\\/]/).pop()??i;console.log(`${y}: ${T}`)}try{const i=await nt(m,u);i==="written"?console.log("\u2705 Written: .git/hooks/post-commit (CLS hook)"):i==="upgraded"?console.log(`\u2705 Upgraded: .git/hooks/post-commit (CLS hook \u2192 ${st})`):i==="appended"?console.log("\u2705 Appended: .git/hooks/post-commit (CLS hook)"):i==="unchanged"&&console.log("\u2713 CLS hook already current in .git/hooks/post-commit")}catch{}if(l==="claude")try{const i=await at(m,u),d=i.scripts==="written"?"\u2705 Written":i.scripts==="upgraded"?`\u2705 Upgraded (\u2192 ${ct})`:"\u2713 Unchanged";console.log(`${d}: .claude/hooks/ (Ambient Recall \u2014 session briefing, per-prompt recall, file briefing, auto-learn)`),i.settings==="written"?console.log("\u2705 Written: .claude/settings.json (Ambient Recall hooks wired)"):i.settings==="merged"?console.log("\u2705 Merged: .claude/settings.json (Ambient Recall hooks wired)"):console.log("\u2713 Ambient Recall hooks already wired in .claude/settings.json")}catch{}(l==="cursor"||l==="copilot"||l==="codex"||l==="gemini")&&await ye(l,m,u,"");const p=((Date.now()-e)/1e3).toFixed(1);console.log(`
233
228
  \u{1F9E0} Cachly AI Brain configured for ${l==="claude"?"Claude Code":l}! (${p}s)`),console.log(w?` Run the \`mcp add\` line above, then restart ${l} \u2014 the \`cachly\` MCP tools will appear.`:" Restart your editor \u2014 the `cachly` MCP tools will appear."),console.log(" Re-run `init` anytime \u2014 it's idempotent (only writes what changed)."),console.log(`
234
229
  \u{1F4DB} Add a live badge to your README:`),console.log(` npx @cachly-dev/mcp-server@latest badge
235
- `),process.exit(0)}if(process.argv[2]==="status"){const e=process.env.CACHLY_JWT??"",t=process.env.CACHLY_BRAIN_INSTANCE_ID??"";(!e||!t)&&(console.log(`
236
- \u26A0\uFE0F CACHLY_JWT and CACHLY_BRAIN_INSTANCE_ID must be set.`),console.log(` Run: npx @cachly-dev/mcp-server@latest autopilot
237
- `),process.exit(1));try{const[s,o]=await Promise.all([fetch(`${x}/api/v1/instances/${t}/memory`,{headers:{Authorization:`Bearer ${e}`},signal:AbortSignal.timeout(8e3)}),fetch(`${x}/api/v1/referral/me`,{headers:{Authorization:`Bearer ${e}`},signal:AbortSignal.timeout(8e3)})]);if(!s.ok)throw new Error(`stats HTTP ${s.status}`);const n=await s.json(),c=o.ok?await o.json():null,i=n.lesson_count??0,a=n.total_recall_count??0,u=Math.round(n.iq_boost_pct??0),g=n.team_authors??[],l=c?.referral_count??0,m=c?.url??"",y=i===0?"Intern \u{1F331}":i<10?"Junior Dev \u{1F527}":i<30?"Mid Dev \u26A1":i<60?"Senior Dev \u{1F9E0}":i<100?"Staff Eng \u{1F680}":"Principal Eng \u{1F3C6}",w=i>0?"\x1B[32m\u25CF\x1B[0m":"\x1B[33m\u25CF\x1B[0m";if(console.log(""),console.log("\u250C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510"),console.log(`\u2502 ${w} \x1B[1mBrain status\x1B[0m \xB7 instance ${t.slice(0,8)}... \u2502`),console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524"),console.log(`\u2502 Lessons : \x1B[33m${String(i).padEnd(8)}\x1B[0m Level : \x1B[32m${y.padEnd(18)}\x1B[0m\u2502`),console.log(`\u2502 Recalls : \x1B[36m${String(a).padEnd(8)}\x1B[0m Quality score: \x1B[32m${String(u).padEnd(3)}%\x1B[0m \u2502`),g.length>0){console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524");const b=g.slice(0,4).join(", ");console.log(`\u2502 \x1B[36m\u{1F465} Team Brain\x1B[0m \xB7 ${String(g.length).padEnd(2)} developer${g.length===1?" ":"s"} sharing lessons${" ".repeat(Math.max(0,19-b.length))} \u2502`),console.log(`\u2502 ${b.slice(0,56).padEnd(56)} \u2502`)}if(m){console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524");const b=`${m.slice(0,44)}${m.length>44?"\u2026":""}`;console.log(`\u2502 \x1B[35m\u{1F517} Invite link\x1B[0m \xB7 ${l>0?`\x1B[32m${l} joined\x1B[0m`:"share to grow your team"}${" ".repeat(Math.max(0,35-String(l).length))} \u2502`),console.log(`\u2502 \x1B[2m${b.padEnd(56)}\x1B[0m \u2502`)}console.log("\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518"),console.log(""),i===0&&(console.log(" \x1B[33m\u{1F4A1} No lessons yet.\x1B[0m Run a session with cachly connected to start learning."),console.log(""))}catch(s){console.log(`
230
+ `),process.exit(0)}if(process.argv[2]==="status"){const e=oe(),t=F();(!e||!t)&&(console.log(`
231
+ \u26A0\uFE0F No cachly key or Brain instance found (environment or ~/.cachly/credentials.json).`),console.log(` Run: npx @cachly-dev/mcp-server@latest autopilot
232
+ `),process.exit(1));try{const[s,n]=await Promise.all([fetch(`${x}/api/v1/instances/${t}/memory`,{headers:{Authorization:`Bearer ${e}`},signal:AbortSignal.timeout(8e3)}),fetch(`${x}/api/v1/referral/me`,{headers:{Authorization:`Bearer ${e}`},signal:AbortSignal.timeout(8e3)})]);if(!s.ok)throw new Error(`stats HTTP ${s.status}`);const o=await s.json(),c=n.ok?await n.json():null,r=o.lesson_count??0,a=o.total_recall_count??0,u=Math.round(o.iq_boost_pct??0),g=o.team_authors??[],l=c?.referral_count??0,m=c?.url??"",h=r===0?"Intern \u{1F331}":r<10?"Junior Dev \u{1F527}":r<30?"Mid Dev \u26A1":r<60?"Senior Dev \u{1F9E0}":r<100?"Staff Eng \u{1F680}":"Principal Eng \u{1F3C6}",w=r>0?"\x1B[32m\u25CF\x1B[0m":"\x1B[33m\u25CF\x1B[0m";if(console.log(""),console.log("\u250C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510"),console.log(`\u2502 ${w} \x1B[1mBrain status\x1B[0m \xB7 instance ${t.slice(0,8)}... \u2502`),console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524"),console.log(`\u2502 Lessons : \x1B[33m${String(r).padEnd(8)}\x1B[0m Level : \x1B[32m${h.padEnd(18)}\x1B[0m\u2502`),console.log(`\u2502 Recalls : \x1B[36m${String(a).padEnd(8)}\x1B[0m Quality score: \x1B[32m${String(u).padEnd(3)}%\x1B[0m \u2502`),g.length>0){console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524");const b=g.slice(0,4).join(", ");console.log(`\u2502 \x1B[36m\u{1F465} Team Brain\x1B[0m \xB7 ${String(g.length).padEnd(2)} developer${g.length===1?" ":"s"} sharing lessons${" ".repeat(Math.max(0,19-b.length))} \u2502`),console.log(`\u2502 ${b.slice(0,56).padEnd(56)} \u2502`)}if(m){console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524");const b=`${m.slice(0,44)}${m.length>44?"\u2026":""}`;console.log(`\u2502 \x1B[35m\u{1F517} Invite link\x1B[0m \xB7 ${l>0?`\x1B[32m${l} joined\x1B[0m`:"share to grow your team"}${" ".repeat(Math.max(0,35-String(l).length))} \u2502`),console.log(`\u2502 \x1B[2m${b.padEnd(56)}\x1B[0m \u2502`)}console.log("\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518"),console.log(""),r===0&&(console.log(" \x1B[33m\u{1F4A1} No lessons yet.\x1B[0m Run a session with cachly connected to start learning."),console.log(""))}catch(s){console.log(`
238
233
  \u274C Could not fetch status: ${s.message}
239
- `),process.exit(1)}process.exit(0)}if(process.argv[2]==="health"){const{existsSync:e}=await import("node:fs"),{readFile:t}=await import("node:fs/promises"),{resolve:s}=await import("node:path"),o=process.cwd();let n=0,c=0;const i=p=>{console.log(` \u2705 ${p}`),n++},a=p=>{console.log(` \u26A0\uFE0F ${p}`)},u=p=>{console.log(` \u274C ${p}`),c++};console.log(`
234
+ `),process.exit(1)}process.exit(0)}if(process.argv[2]==="health"){const{existsSync:e}=await import("node:fs"),{readFile:t}=await import("node:fs/promises"),{resolve:s}=await import("node:path"),n=process.cwd();let o=0,c=0;const r=p=>{console.log(` \u2705 ${p}`),o++},a=p=>{console.log(` \u26A0\uFE0F ${p}`)},u=p=>{console.log(` \u274C ${p}`),c++};console.log(`
240
235
  \u{1F9E0} cachly health check
241
- `),console.log("\u{1F511} Auth token");const g=process.env.CACHLY_JWT??"";if(!g)u("CACHLY_JWT not set \u2014 run: npx @cachly-dev/mcp-server@latest autopilot");else if(g.startsWith("cky_"))i("Long-lived API key set (cky_\u2026) \u2014 no client-side expiry");else try{const p=g.split(".");if(p.length!==3)throw new Error("not a JWT");const r=JSON.parse(Buffer.from(p[1],"base64url").toString("utf-8")),d=r.exp?r.exp*1e3:null,h=d?Math.floor((d-Date.now())/6e4):null;d&&d<Date.now()?u(`JWT expired ${Math.abs(h)} minute(s) ago \u2014 get a new one: ${N("/setup-ai","health")}`):h!==null&&h<30?a(`JWT expires in ${h} minute(s) \u2014 refresh soon at ${N("/setup-ai","health")}`):i(`JWT valid${h!==null?` (expires in ${Math.floor(h/60)}h ${h%60}m)`:""}`)}catch{u("CACHLY_JWT format invalid (expected a cky_ API key or a 3-part JWT)")}console.log(`
242
- \u{1F310} Brain API`);const l=process.env.CACHLY_API_URL??"https://api.cachly.dev";try{const p=await fetch(`${l}/health`,{signal:AbortSignal.timeout(5e3)});if(p.ok){const r=await p.json();i(`${l} reachable (status: ${r.status??"ok"})`)}else p.status===401||p.status===403?i(`${l} reachable (auth required \u2014 expected)`):u(`${l}/health returned HTTP ${p.status}`)}catch(p){u(`${l} unreachable \u2014 ${p.message}`)}console.log(`
243
- \u{1F9E0} Brain instance`);const m=process.env.CACHLY_BRAIN_INSTANCE_ID??"";if(!m)a("CACHLY_BRAIN_INSTANCE_ID not set \u2014 will auto-resolve on first tool call");else if(g)try{const p=await fetch(`${l}/api/v1/instances/${m}`,{headers:{Authorization:`Bearer ${g}`,Accept:"application/json"},signal:AbortSignal.timeout(5e3)});if(p.ok){const r=await p.json();if(r.status==="running")if(i(`Instance "${r.name}" (${m.slice(0,8)}\u2026) is running`),console.log(`
244
- \u{1F50C} Redis connectivity`),r.host&&r.port)try{let d,h=r.tls_enabled!==!1;try{const I=await fetch(`${l}/api/v1/instances/${m}/connection`,{headers:{Authorization:`Bearer ${g}`,Accept:"application/json"},signal:AbortSignal.timeout(5e3)});if(I.ok){const S=await I.json();d=S.password,h=S.tls_enabled!==!1}}catch{}const T=new qe({host:r.host,port:r.port,password:d,...h?{tls:{}}:{},lazyConnect:!0,connectTimeout:5e3,retryStrategy:()=>null});await T.connect();const B=await T.ping();T.disconnect(),B==="PONG"?i(`Redis PING \u2192 PONG (${r.host}:${r.port})`):a(`Redis responded but not PONG: ${B}`)}catch(d){u(`Redis unreachable \u2014 ${d.message}`)}else a("Instance has no host/port yet");else a(`Instance "${r.name}" status: ${r.status}`)}else u(`Instance ${m.slice(0,8)}\u2026 not found (HTTP ${p.status})`)}catch(p){u(`Could not check instance \u2014 ${p.message}`)}console.log(`
245
- \u{1F6E0} Editor configs (project dir)`);const y=[{editor:"Claude Code",path:".mcp.json"},{editor:"Cursor",path:".cursor/mcp.json"},{editor:"Windsurf",path:".windsurf/mcp.json"},{editor:"Copilot / Cline",path:".vscode/mcp.json"},{editor:"Zed",path:".zed/settings.json"},{editor:"Continue.dev",path:".continue/config.json"}];let w=0;for(const{editor:p,path:r}of y){const d=s(o,r);if(e(d))try{const h=await t(d,"utf-8");h.includes("@cachly-dev/mcp-server")||h.includes("cachly")?(i(`${p}: ${r}`),w++):a(`${p}: ${r} exists but cachly not found inside`)}catch{a(`${p}: ${r} exists but could not read`)}}w===0&&u("No editor MCP configs found \u2014 run: npx @cachly-dev/mcp-server@latest autopilot"),console.log(`
246
- \u{1FA9D} Git hook (ambient learning)`);const b=s(o,".git","hooks","post-commit");if(!e(s(o,".git")))a("Not in a git repository \u2014 skip");else if(e(b))try{(await t(b,"utf-8")).includes("cachly")?i(".git/hooks/post-commit (cachly CLS hook present)"):a(".git/hooks/post-commit exists but cachly hook not found \u2014 run setup to add it")}catch{a(".git/hooks/post-commit exists but could not read")}else a(".git/hooks/post-commit not found \u2014 run: npx @cachly-dev/mcp-server@latest autopilot");console.log(`
247
- \u{1F9EC} Embedding provider (semantic search)`);{const p=[["OPENAI_API_KEY","openai"],["GEMINI_API_KEY","gemini"],["MISTRAL_API_KEY","mistral"],["COHERE_API_KEY","cohere"],["OLLAMA_BASE_URL","ollama"]],r=(process.env.CACHLY_EMBED_PROVIDER??"").toLowerCase();if(r&&r!=="cachly"){const d=p.find(([,h])=>h===r)?.[0];d&&process.env[d]?i(`BYOK: ${r} (key present in ${d})`):a(`CACHLY_EMBED_PROVIDER=${r} but its key env var is not set \u2014 semantic search will fail`)}else{Xe==="ollama"?i("Local Ollama (OLLAMA_BASE_URL) \u2014 text stays on your machine"):Xe==="cachly"?i("Server-side embeddings (cachly, EU) \u2014 no key needed, uses your JWT"):a("No embedding provider active \u2014 sign in (CACHLY_JWT) for EU-hosted embeddings; search runs on words until then");for(const[d,h]of p)h!=="ollama"&&process.env[d]&&i(`${d} is set but not used \u2014 ${h} runs only if you name it with CACHLY_EMBED_PROVIDER=${h}`)}}console.log(`
248
- ${"\u2500".repeat(40)}`),c===0?console.log(`\u2705 All checks passed (${n} ok)
249
- `):(console.log(`\u274C ${c} check(s) failed, ${n} passed
236
+ `),console.log("\u{1F511} Auth token");const g=oe();if(!g)u("No cachly key found (environment or ~/.cachly/credentials.json) \u2014 run: npx @cachly-dev/mcp-server@latest autopilot");else if(g.startsWith("cky_"))r("Long-lived API key set (cky_\u2026) \u2014 no client-side expiry");else try{const p=g.split(".");if(p.length!==3)throw new Error("not a JWT");const i=JSON.parse(Buffer.from(p[1],"base64url").toString("utf-8")),d=i.exp?i.exp*1e3:null,y=d?Math.floor((d-Date.now())/6e4):null;d&&d<Date.now()?u(`JWT expired ${Math.abs(y)} minute(s) ago \u2014 get a new one: ${j("/setup-ai","health")}`):y!==null&&y<30?a(`JWT expires in ${y} minute(s) \u2014 refresh soon at ${j("/setup-ai","health")}`):r(`JWT valid${y!==null?` (expires in ${Math.floor(y/60)}h ${y%60}m)`:""}`)}catch{u("CACHLY_JWT format invalid (expected a cky_ API key or a 3-part JWT)")}console.log(`
237
+ \u{1F310} Brain API`);const l=process.env.CACHLY_API_URL??"https://api.cachly.dev";try{const p=await fetch(`${l}/health`,{signal:AbortSignal.timeout(5e3)});if(p.ok){const i=await p.json();r(`${l} reachable (status: ${i.status??"ok"})`)}else p.status===401||p.status===403?r(`${l} reachable (auth required \u2014 expected)`):u(`${l}/health returned HTTP ${p.status}`)}catch(p){u(`${l} unreachable \u2014 ${p.message}`)}console.log(`
238
+ \u{1F9E0} Brain instance`);const m=F();if(!m)a("No Brain instance found (environment or ~/.cachly/credentials.json) \u2014 will auto-resolve on first tool call");else if(g)try{const p=await fetch(`${l}/api/v1/instances/${m}`,{headers:{Authorization:`Bearer ${g}`,Accept:"application/json"},signal:AbortSignal.timeout(5e3)});if(p.ok){const i=await p.json();if(i.status==="running")if(r(`Instance "${i.name}" (${m.slice(0,8)}\u2026) is running`),console.log(`
239
+ \u{1F50C} Redis connectivity`),i.host&&i.port)try{let d,y=i.tls_enabled!==!1;try{const E=await fetch(`${l}/api/v1/instances/${m}/connection`,{headers:{Authorization:`Bearer ${g}`,Accept:"application/json"},signal:AbortSignal.timeout(5e3)});if(E.ok){const A=await E.json();d=A.password,y=A.tls_enabled!==!1}}catch{}const T=new Ze({host:i.host,port:i.port,password:d,...y?{tls:{}}:{},lazyConnect:!0,connectTimeout:5e3,retryStrategy:()=>null});await T.connect();const L=await T.ping();T.disconnect(),L==="PONG"?r(`Redis PING \u2192 PONG (${i.host}:${i.port})`):a(`Redis responded but not PONG: ${L}`)}catch(d){u(`Redis unreachable \u2014 ${d.message}`)}else a("Instance has no host/port yet");else a(`Instance "${i.name}" status: ${i.status}`)}else u(`Instance ${m.slice(0,8)}\u2026 not found (HTTP ${p.status})`)}catch(p){u(`Could not check instance \u2014 ${p.message}`)}console.log(`
240
+ \u{1F6E0} Editor configs (project dir)`);const h=[{editor:"Claude Code",path:".mcp.json"},{editor:"Cursor",path:".cursor/mcp.json"},{editor:"Windsurf",path:".windsurf/mcp.json"},{editor:"Copilot / Cline",path:".vscode/mcp.json"},{editor:"Zed",path:".zed/settings.json"},{editor:"Continue.dev",path:".continue/config.json"}];let w=0;for(const{editor:p,path:i}of h){const d=s(n,i);if(e(d))try{const y=await t(d,"utf-8");y.includes("@cachly-dev/mcp-server")||y.includes("cachly")?(r(`${p}: ${i}`),w++):a(`${p}: ${i} exists but cachly not found inside`)}catch{a(`${p}: ${i} exists but could not read`)}}w===0&&u("No editor MCP configs found \u2014 run: npx @cachly-dev/mcp-server@latest autopilot"),console.log(`
241
+ \u{1FA9D} Git hook (ambient learning)`);const b=s(n,".git","hooks","post-commit");if(!e(s(n,".git")))a("Not in a git repository \u2014 skip");else if(e(b))try{(await t(b,"utf-8")).includes("cachly")?r(".git/hooks/post-commit (cachly CLS hook present)"):a(".git/hooks/post-commit exists but cachly hook not found \u2014 run setup to add it")}catch{a(".git/hooks/post-commit exists but could not read")}else a(".git/hooks/post-commit not found \u2014 run: npx @cachly-dev/mcp-server@latest autopilot");console.log(`
242
+ \u{1F9EC} Embedding provider (semantic search)`);{const p=[["OPENAI_API_KEY","openai"],["GEMINI_API_KEY","gemini"],["MISTRAL_API_KEY","mistral"],["COHERE_API_KEY","cohere"],["OLLAMA_BASE_URL","ollama"]],i=(process.env.CACHLY_EMBED_PROVIDER??"").toLowerCase();if(i&&i!=="cachly"){const d=p.find(([,y])=>y===i)?.[0];d&&process.env[d]?r(`BYOK: ${i} (key present in ${d})`):a(`CACHLY_EMBED_PROVIDER=${i} but its key env var is not set \u2014 semantic search will fail`)}else{ot==="ollama"?r("Local Ollama (OLLAMA_BASE_URL) \u2014 text stays on your machine"):ot==="cachly"?r("Server-side embeddings (cachly, EU) \u2014 no key needed, uses your JWT"):a("No embedding provider active \u2014 sign in (CACHLY_JWT) for EU-hosted embeddings; search runs on words until then");for(const[d,y]of p)y!=="ollama"&&process.env[d]&&r(`${d} is set but not used \u2014 ${y} runs only if you name it with CACHLY_EMBED_PROVIDER=${y}`)}}console.log(`
243
+ ${"\u2500".repeat(40)}`),c===0?console.log(`\u2705 All checks passed (${o} ok)
244
+ `):(console.log(`\u274C ${c} check(s) failed, ${o} passed
250
245
  `),console.log(`\u{1F4A1} Fix issues with: npx @cachly-dev/mcp-server@latest autopilot
251
- `),process.exit(1)),process.exit(0)}if(process.argv[2]==="claim"){const{readFile:e,writeFile:t}=await import("node:fs/promises"),{existsSync:s}=await import("node:fs"),{resolve:o}=await import("node:path"),{homedir:n}=await import("node:os"),c=r=>{const d=process.argv.indexOf(`--${r}`);return d!==-1?process.argv[d+1]:void 0};x=(c("api-url")??"https://api.cachly.dev").replace(/\/+$/,"");const i=(c("auth-url")??ct).replace(/\/+$/,"");console.log(`
246
+ `),process.exit(1)),process.exit(0)}if(process.argv[2]==="claim"){const{readFile:e,writeFile:t}=await import("node:fs/promises"),{existsSync:s}=await import("node:fs"),{resolve:n}=await import("node:path"),{homedir:o}=await import("node:os"),c=i=>{const d=process.argv.indexOf(`--${i}`);return d!==-1?process.argv[d+1]:void 0};x=(c("api-url")??"https://api.cachly.dev").replace(/\/+$/,"");const r=(c("auth-url")??pt).replace(/\/+$/,"");console.log(`
252
247
  \u{1F9E0} cachly \u2014 keep your test Brain in your own account
253
- `),(x!=="https://api.cachly.dev"||i!==ct)&&console.log(`\u26A0\uFE0F Not the cachly servers: API ${x}, sign-in ${i}
254
- `);const a=nn({argument:c("key"),credentials:Ee()});a||(console.log("No test key (cky_trial_\u2026) was found in ~/.cachly/credentials.json."),console.log("If you already use your own cachly account, there is nothing to claim."),console.log(`Otherwise pass the key: npx @cachly-dev/mcp-server@latest claim --key cky_trial_\u2026
248
+ `),(x!=="https://api.cachly.dev"||r!==pt)&&console.log(`\u26A0\uFE0F Not the cachly servers: API ${x}, sign-in ${r}
249
+ `);const a=ln({argument:c("key"),credentials:Oe()});a||(console.log("No test key (cky_trial_\u2026) was found in ~/.cachly/credentials.json."),console.log("If you already use your own cachly account, there is nothing to claim."),console.log(`Otherwise pass the key: npx @cachly-dev/mcp-server@latest claim --key cky_trial_\u2026
255
250
  `),process.exit(1));const u=a.schluessel,g={argument:"--key",credentials:"~/.cachly/credentials.json"}[a.quelle];console.log(`Test key: ${u.slice(0,14)}\u2026 (from ${g})
256
251
  `);let l="";try{console.log(`Step 1: Sign in to your cachly account
257
- `);const r=await sn(fetch,i);console.log(` Code: \x1B[1;33m${r.userCode}\x1B[0m`),console.log(" Open this URL and confirm the code:"),console.log(`${r.adresse}
258
- `),ie(r.adresse,(d,h)=>{d||console.log(`
259
- \u2139\uFE0F No browser could be opened here (${h??"unknown"}) \u2014 please open the URL yourself.`)}),process.stdout.write(" Waiting for sign-in"),l=await rn(fetch,r,{basis:i,puls:()=>process.stdout.write(".")}),console.log(` \x1B[32m\u2713\x1B[0m
260
- `)}catch(r){console.error(`
252
+ `);const i=await dn(fetch,r);console.log(` Code: \x1B[1;33m${i.userCode}\x1B[0m`),console.log(" Open this URL and confirm the code:"),console.log(`${i.adresse}
253
+ `),ce(i.adresse,(d,y)=>{d||console.log(`
254
+ \u2139\uFE0F No browser could be opened here (${y??"unknown"}) \u2014 please open the URL yourself.`)}),process.stdout.write(" Waiting for sign-in"),l=await un(fetch,i,{basis:r,puls:()=>process.stdout.write(".")}),console.log(` \x1B[32m\u2713\x1B[0m
255
+ `)}catch(i){console.error(`
261
256
 
262
- ${r.message}. Nothing was changed.
263
- `),process.exit(1)}process.stdout.write("Step 2: Moving the test Brain into your account (it restarts once, up to a minute)\u2026");let m;try{m=await an(fetch,x,l,u,"mcp",`cachly-mcp/${D}`),console.log(` \x1B[32m\u2713\x1B[0m
264
- `)}catch(r){console.error(`
257
+ ${i.message}. Nothing was changed.
258
+ `),process.exit(1)}process.stdout.write("Step 2: Moving the test Brain into your account (it restarts once, up to a minute)\u2026");let m;try{m=await pn(fetch,x,l,u,"mcp",`cachly-mcp/${D}`),console.log(` \x1B[32m\u2713\x1B[0m
259
+ `)}catch(i){console.error(`
265
260
 
266
- ${dn(r)}
267
- `),process.exit(1)}v=m.apiKey,Z(m.apiKey),at(m.instanceId,{apiKey:m.apiKey}),await le(m.apiKey),await de(m.instanceId);const y=n(),w=new Set([...Object.values(ve).map(r=>o(process.cwd(),r)),o(y,".claude.json"),o(y,".claude","mcp.json"),o(y,".cursor","mcp.json"),o(y,".codeium","windsurf","mcp_config.json")]);let b=0;for(const r of w)if(s(r))try{const d=un(await e(r,"utf-8"),u,m.apiKey);d.ersetzt&&(await t(r,d.text,"utf-8"),b++)}catch{}const p=await cn(fetch,x,m.apiKey,m.instanceId);console.log(ln(m,p)),console.log(`
261
+ ${fn(i)}
262
+ `),process.exit(1)}v=m.apiKey,Q(m.apiKey),dt(m.instanceId,{apiKey:m.apiKey}),await ue(m.apiKey),await pe(m.instanceId);const h=o(),w=new Set([...Object.values(me).map(i=>n(process.cwd(),i)),n(h,".claude.json"),n(h,".claude","mcp.json"),n(h,".cursor","mcp.json"),n(h,".codeium","windsurf","mcp_config.json")]);let b=0;for(const i of w)if(s(i))try{const d=hn(await e(i,"utf-8"),u,m.apiKey);d.ersetzt&&(await t(i,d.text,"utf-8"),b++)}catch{}const p=await mn(fetch,x,m.apiKey,m.instanceId);console.log(gn(m,p)),console.log(`
268
263
  New key stored in ~/.cachly/credentials.json${b?` and in ${b} editor config file(s) that held the test key`:""}.`),console.log(`Restart your editor (or reload its MCP servers) so it uses the new key.
269
- `),process.exit(0)}const we=process.argv[2]==="autopilot";if(process.argv[2]==="autosetup"||process.argv[2]==="setup"||we){const{writeFile:e,mkdir:t,readFile:s}=await import("node:fs/promises"),{existsSync:o}=await import("node:fs"),{resolve:n,dirname:c}=await import("node:path"),{createInterface:i}=await import("node:readline"),a=Date.now(),u=we?"autopilot":"setup",g=process.argv.indexOf("--api-url");g!==-1&&process.argv[g+1]&&(x=process.argv[g+1].replace(/\/+$/,""),console.log(`\u2139\uFE0F Using self-hosted backend: ${x}
270
- `)),A("setup_started");const l=process.stdin.isTTY===!0,m=we||process.argv.includes("--yes")||process.argv.includes("-y")||!l;we?console.log(`\u{1F916} Autopilot \u2014 one-command setup (auth \u2192 instance \u2192 configs \u2192 Brain), zero prompts.
264
+ `),process.exit(0)}const $e=process.argv[2]==="autopilot";if(process.argv[2]==="autosetup"||process.argv[2]==="setup"||$e){const{writeFile:e,mkdir:t,readFile:s}=await import("node:fs/promises"),{existsSync:n}=await import("node:fs"),{resolve:o,dirname:c}=await import("node:path"),{createInterface:r}=await import("node:readline"),a=Date.now(),u=$e?"autopilot":"setup",g=process.argv.indexOf("--api-url");g!==-1&&process.argv[g+1]&&(x=process.argv[g+1].replace(/\/+$/,""),console.log(`\u2139\uFE0F Using self-hosted backend: ${x}
265
+ `)),S("setup_started");const l=process.stdin.isTTY===!0,m=$e||process.argv.includes("--yes")||process.argv.includes("-y")||!l;$e?console.log(`\u{1F916} Autopilot \u2014 one-command setup (auth \u2192 instance \u2192 configs \u2192 Brain), zero prompts.
271
266
  `):!l&&!process.argv.includes("--yes")&&!process.argv.includes("-y")&&console.log(`\u2139\uFE0F Non-interactive terminal detected \u2014 running in automatic mode (no prompts).
272
- `);const y=i({input:process.stdin,output:process.stdout}),w=(f,_="")=>m?(console.log(`${f}${_}`),Promise.resolve(_)):new Promise(R=>y.question(f,J=>R(J.trim()||_)));console.log(`
267
+ `);const h=r({input:process.stdin,output:process.stdout}),w=(f,_="")=>m?(console.log(`${f}${_}`),Promise.resolve(_)):new Promise(R=>h.question(f,U=>R(U.trim()||_)));console.log(`
273
268
  \u{1F9E0} cachly AI Brain \u2014 Setup`),console.log(`\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
274
269
  `);let b=process.env.CACHLY_JWT??"";if(b)console.log(`\u2713 Using token from CACHLY_JWT env var
275
270
  `);else{console.log(`Step 1: Sign in to cachly (free, no credit card)
276
- `),A("setup_auth_started");let f="",_="",R="",J=5e3,Y=!1,X="cachly";try{const K=await fetch(`${x}/auth/device`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({}),signal:AbortSignal.timeout(8e3)});if(K.ok){const L=await K.json();f=L.device_code,_=L.user_code;const M=L.verification_uri||N("/device",u);R=`${M}${M.includes("?")?"&":"?"}code=${encodeURIComponent(_)}`,J=(L.interval??5)*1e3,Y=!!f}}catch{}if(!Y)try{const L=await fetch("https://auth.cachly.dev/realms/cachly/protocol/openid-connect/auth/device",{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:"client_id=cachly-cli&scope=openid",signal:AbortSignal.timeout(8e3)});if(L.ok){const M=await L.json();f=M.device_code,_=M.user_code,R=M.verification_uri_complete,J=(M.interval??5)*1e3,Y=!0,X="keycloak"}}catch{}if(Y&&f){console.log(` Code: \x1B[1;33m${_}\x1B[0m`),console.log(" Open this URL and confirm the code:"),console.log(`${R}
277
- `),ie(R,(L,M)=>{if(L){A("device_browser_opened",{stelle:"setup"});return}A("device_browser_failed",{reason:M??"unknown",stelle:"setup"}),console.log(` \u2139\uFE0F No browser could be opened here (${M??"unknown"}) \u2014 please open the URL yourself.
278
- `)}),process.stdout.write(" Waiting for authorization");const K=Date.now()+600*1e3;for(;Date.now()<K;){await new Promise(L=>setTimeout(L,J)),process.stdout.write(".");try{let L=null;if(X==="cachly")try{const M=await fetch(`${x}/auth/device/token`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({device_code:f}),signal:AbortSignal.timeout(8e3)});M.status<500&&(L=await M.json())}catch{}else{const M="https://auth.cachly.dev/realms/cachly/protocol/openid-connect";try{L=await(await fetch(`${M}/token`,{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:`client_id=cachly-cli&grant_type=urn:ietf:params:oauth:grant-type:device_code&device_code=${f}`,signal:AbortSignal.timeout(8e3)})).json()}catch{}}if(L?.access_token){b=L.access_token,console.log(` \x1B[32m\u2713 Authorized!\x1B[0m
279
- `),A("setup_auth_completed"),A("device_flow_completed");break}L?.error==="slow_down"?J=Math.min(J+2e3,15e3):L?.error&&L.error!=="authorization_pending"&&(A("device_flow_failed",{reason:L.error}),console.error(`
280
- Auth error: ${L.error}. Aborting.
281
- `),y.close(),process.exit(1))}catch{}}b||(A("device_flow_failed",{reason:"timeout"}),console.error(`
271
+ `),S("setup_auth_started");let f="",_="",R="",U=5e3,J=!1,ee="cachly";try{const q=await fetch(`${x}/auth/device`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({}),signal:AbortSignal.timeout(8e3)});if(q.ok){const B=await q.json();f=B.device_code,_=B.user_code;const M=B.verification_uri||j("/device",u);R=`${M}${M.includes("?")?"&":"?"}code=${encodeURIComponent(_)}`,U=(B.interval??5)*1e3,J=!!f}}catch{}if(!J)try{const B=await fetch("https://auth.cachly.dev/realms/cachly/protocol/openid-connect/auth/device",{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:"client_id=cachly-cli&scope=openid",signal:AbortSignal.timeout(8e3)});if(B.ok){const M=await B.json();f=M.device_code,_=M.user_code,R=M.verification_uri_complete,U=(M.interval??5)*1e3,J=!0,ee="keycloak"}}catch{}if(J&&f){console.log(` Code: \x1B[1;33m${_}\x1B[0m`),console.log(" Open this URL and confirm the code:"),console.log(`${R}
272
+ `),ce(R,(B,M)=>{if(B){S("device_browser_opened",{stelle:"setup"});return}S("device_browser_failed",{reason:M??"unknown",stelle:"setup"}),console.log(` \u2139\uFE0F No browser could be opened here (${M??"unknown"}) \u2014 please open the URL yourself.
273
+ `)}),process.stdout.write(" Waiting for authorization");const q=Date.now()+600*1e3;for(;Date.now()<q;){await new Promise(B=>setTimeout(B,U)),process.stdout.write(".");try{let B=null;if(ee==="cachly")try{const M=await fetch(`${x}/auth/device/token`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({device_code:f}),signal:AbortSignal.timeout(8e3)});M.status<500&&(B=await M.json())}catch{}else{const M="https://auth.cachly.dev/realms/cachly/protocol/openid-connect";try{B=await(await fetch(`${M}/token`,{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:`client_id=cachly-cli&grant_type=urn:ietf:params:oauth:grant-type:device_code&device_code=${f}`,signal:AbortSignal.timeout(8e3)})).json()}catch{}}if(B?.access_token){b=B.access_token,console.log(` \x1B[32m\u2713 Authorized!\x1B[0m
274
+ `),S("setup_auth_completed"),S("device_flow_completed");break}B?.error==="slow_down"?U=Math.min(U+2e3,15e3):B?.error&&B.error!=="authorization_pending"&&(S("device_flow_failed",{reason:B.error}),console.error(`
275
+ Auth error: ${B.error}. Aborting.
276
+ `),h.close(),process.exit(1))}catch{}}b||(S("device_flow_failed",{reason:"timeout"}),console.error(`
282
277
  Timed out waiting for authorization. Aborting.
283
- `),y.close(),process.exit(1)),console.log("")}else{A("device_flow_failed",{reason:"device_flow_unavailable"});const K=N("/setup-ai",u);console.log(` \u26A0\uFE0F Could not start automatic sign-in. Opening browser for web sign-in...
284
- `),console.log(` URL: \x1B[36m${K}\x1B[0m`),console.log(" 1. Sign up / log in at the URL above"),console.log(" 2. Go to Settings \u2192 API Keys \u2192 Create new key"),console.log(` 3. Copy the key (starts with cky_live_...)
285
- `),ie(K),m?(A("device_flow_failed",{reason:"non_interactive_no_key"}),console.error(`
278
+ `),h.close(),process.exit(1)),console.log("")}else{S("device_flow_failed",{reason:"device_flow_unavailable"});const q=j("/setup-ai",u);console.log(` \u26A0\uFE0F Could not start automatic sign-in. Opening browser for web sign-in...
279
+ `),console.log(` URL: \x1B[36m${q}\x1B[0m`),console.log(" 1. Sign up / log in at the URL above"),console.log(" 2. Go to Settings \u2192 API Keys \u2192 Create new key"),console.log(` 3. Copy the key (starts with cky_live_...)
280
+ `),ce(q),m?(S("device_flow_failed",{reason:"non_interactive_no_key"}),console.error(`
286
281
  \u26A0\uFE0F This terminal is non-interactive, so the key cannot be pasted here.`),console.error(` Get your key at the URL above, then set it and re-run setup:
287
282
  `),console.error(` \x1B[1mCACHLY_JWT=cky_live_xxx npx @cachly-dev/mcp-server@latest autopilot\x1B[0m
288
283
  `),console.error(` Or add it to your editor's MCP config under env.CACHLY_JWT.
289
- `),y.close(),process.exit(1)):(b=await w(" Paste API key (cky_live_...): "),b||(console.error(`
284
+ `),h.close(),process.exit(1)):(b=await w(" Paste API key (cky_live_...): "),b||(console.error(`
290
285
  API key is required. Aborting.
291
- `),y.close(),process.exit(1)),A("setup_auth_completed"),console.log(""))}}if(b.startsWith("eyJ")){process.stdout.write("\u23F3 Generating your API key...");try{const f=await fetch(`${x}/api/v1/api-keys`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${b}`},body:JSON.stringify({name:"cachly-mcp-setup",scope:"read_write"})});if(!f.ok)throw new Error(`HTTP ${f.status}`);const _=await f.json();if(!_.key)throw new Error("no key in response");b=_.key,console.log(` \u2713
286
+ `),h.close(),process.exit(1)),S("setup_auth_completed"),console.log(""))}}if(b.startsWith("eyJ")){process.stdout.write("\u23F3 Generating your API key...");try{const f=await fetch(`${x}/api/v1/api-keys`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${b}`},body:JSON.stringify({name:"cachly-mcp-setup",scope:"read_write"})});if(!f.ok)throw new Error(`HTTP ${f.status}`);const _=await f.json();if(!_.key)throw new Error("no key in response");b=_.key,console.log(` \u2713
292
287
  `)}catch{console.log(` (skipped)
293
288
  `)}}process.stdout.write("\u23F3 Fetching your instances...");let p=[];try{const f=await fetch(`${x}/api/v1/instances`,{headers:{Authorization:`Bearer ${b}`}});if(!f.ok)throw new Error(`HTTP ${f.status}`);p=((await f.json()).data??[]).filter(R=>R.status==="running")}catch(f){const _=f.message;console.error(`
294
289
 
295
- Failed to fetch instances: ${_}`),_.includes("401")&&(console.error(`Token rejected. Get a valid token at ${N("/setup-ai",u)}
296
- `),ie(N("/setup-ai",u))),y.close(),process.exit(1)}if(console.log(` found ${p.length}
290
+ Failed to fetch instances: ${_}`),_.includes("401")&&(console.error(`Token rejected. Get a valid token at ${j("/setup-ai",u)}
291
+ `),ce(j("/setup-ai",u))),h.close(),process.exit(1)}if(console.log(` found ${p.length}
297
292
  `),p.length===0){process.stdout.write("\u23F3 Creating your free Brain instance...");try{const f=await fetch(`${x}/api/v1/instances/auto`,{method:"POST",headers:{Authorization:`Bearer ${b}`,"Content-Type":"application/json"}});if(!f.ok)throw new Error(`HTTP ${f.status}`);const _=await f.json();if(_.instance)p=[_.instance];else if(_.instance_id){const R=_.instance_id;console.log(` \u2713 created (${R.slice(0,8)}\u2026)
298
- `),process.stdout.write("\u23F3 Waiting for instance to start");for(let J=0;J<45;J++){await new Promise(Y=>setTimeout(Y,3e3)),process.stdout.write(".");try{const Y=await fetch(`${x}/api/v1/instances/${R}`,{headers:{Authorization:`Bearer ${b}`}});if(Y.ok){const X=await Y.json();if(X.status==="running"){p=[X];break}}}catch{}}console.log("")}}catch(f){console.log(` failed: ${f.message}
293
+ `),process.stdout.write("\u23F3 Waiting for instance to start");for(let U=0;U<45;U++){await new Promise(J=>setTimeout(J,3e3)),process.stdout.write(".");try{const J=await fetch(`${x}/api/v1/instances/${R}`,{headers:{Authorization:`Bearer ${b}`}});if(J.ok){const ee=await J.json();if(ee.status==="running"){p=[ee];break}}}catch{}}console.log("")}}catch(f){console.log(` failed: ${f.message}
299
294
  `)}p.length===0&&(console.error(`
300
- Could not create an instance automatically. Opening ${N("/instances",u)} \u2026
301
- `),ie(N("/instances",u)),y.close(),process.exit(1))}const r=p[0];p.length>1&&(console.log(`\u2139\uFE0F Multiple instances found \u2014 using most recent: ${r.name}`),console.log(` (Run with --instance-id <id> to use a different one)
302
- `)),console.log(`\u2713 Instance: ${r.name} (${r.id.slice(0,8)}\u2026)
303
- `),A("setup_instance_ready",{instance_id:r.id});const d=process.cwd(),h=[],{homedir:T}=await import("node:os"),B=T();h.push("claude"),o(n(d,".cursor"))&&h.push("cursor"),o(n(d,".windsurf"))&&h.push("windsurf"),o(n(d,".vscode"))&&h.push("copilot"),o(n(d,".continue"))&&h.push("continue");const I=process.platform==="darwin"?n(B,"Library/Application Support/Code/User/globalStorage/saoudrizwan.claude-dev"):n(B,".vscode/extensions");(o(I)&&!h.includes("copilot")||o(I))&&h.push("cline");const S=process.platform==="darwin"?n(B,"Library/Application Support/Zed"):n(B,".config/zed");o(S)&&h.push("zed");const k=f=>({claude:"Claude Code",cursor:"Cursor",windsurf:"Windsurf",copilot:"GitHub Copilot",continue:"Continue.dev",cline:"Cline (VSCode)",zed:"Zed"})[f]??f,E=h;console.log(`Step 3: Configuring for: ${E.map(k).join(", ")}
304
- `);const{existsSync:$}=await import("node:fs"),C=[];let j=0;Z(b);for(const f of E){const _=ve[f]??".mcp.json",R=n(d,_);try{await t(c(R),{recursive:!0});const J=$(R),Y=await Rt(R,b,r.id,f,{readFile:s,existsSync:$});await e(R,Y,"utf-8"),console.log(`\u2705 ${J?"Updated":"Written"}: ${_}`),j++}catch(J){const Y=J.message;console.log(`\u26A0\uFE0F Could not write ${_}: ${Y}`),console.log(" Fix permissions or run with sudo, then re-run setup."),C.push({file:_,reason:Y})}}const F=n(B,".claude","mcp.json");try{await t(c(F),{recursive:!0});let f={};if(o(F))try{f=JSON.parse(await s(F,"utf-8"))}catch{}f.mcpServers??={},f.mcpServers.cachly={command:"npx",args:["-y","@cachly-dev/mcp-server@latest"],env:oe(b,r.id,{includeApiKey:!0})},await e(F,JSON.stringify(f,null,2),"utf-8"),console.log("\u2705 Written: ~/.claude/mcp.json (global \u2014 works in every project)"),j++}catch(f){const _=f.message;console.log(`\u26A0\uFE0F Could not write ~/.claude/mcp.json: ${_}`),C.push({file:"~/.claude/mcp.json",reason:_})}const z=await Lt(d,r.id);for(const[f,_]of Object.entries(z))console.log(`${_==="updated"?"\u2705 Updated":_==="appended"?"\u2705 Appended to":"\u2705 Written"}: ${f}`);v=b;let fe=0;if(o(n(d,".git"))){process.stdout.write("\u23F3 Bootstrapping Brain from git history (~10s)...");try{const f=await U("brain_from_git",{instance_id:r.id,repo_path:d,limit:100}),_=f.match(/Ingested: \*\*(\d+)\*\*/);fe=_?parseInt(_[1],10):0,console.log(` \u2713 ${fe} lesson${fe===1?"":"s"} extracted from git history`);const R=Ho(f);R&&(console.log(""),console.log(R))}catch(f){console.log(` (skipped: ${f.message.slice(0,80)})`)}}if(fe===0){process.stdout.write("\u{1F331} Seeding 16 universal engineering lessons (first recall works instantly)...");try{await U("brain_seed_starter",{instance_id:r.id}),console.log(" \u2713");try{const f=await U("smart_recall",{instance_id:r.id,query:"docker build slow"}),_=zn(f);_&&(console.log(""),console.log(Oo(_.topic,_.what)))}catch{}}catch(f){console.log(` (skipped: ${f.message.slice(0,60)})`)}}process.stdout.write(`
305
- \u23F3 Fetching your Brain health preview...`);try{const f=await fetch(`${x}/api/v1/instances/${r.id}/memory`,{headers:{Authorization:`Bearer ${b}`},signal:AbortSignal.timeout(6e3)});if(f.ok){const _=await f.json(),R=_.lesson_count??0,J=_.context_count??0,Y=Math.round(_.iq_boost_pct??0),X=R===0?"Intern \u{1F331}":R<10?"Junior Dev \u{1F527}":R<30?"Mid Dev \u26A1":R<60?"Senior Dev \u{1F9E0}":R<100?"Staff Eng \u{1F680}":"Principal Eng \u{1F3C6}";if(console.log(` \u2713
306
- `),console.log("\u250C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510"),console.log("\u2502 \u{1F9E0} Brain Health Report \u2502"),console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524"),console.log(`\u2502 Lessons stored : ${String(R).padEnd(6)} \u2502 Level: ${X.padEnd(20)}\u2502`),console.log(`\u2502 Context entries : ${String(J).padEnd(6)} \u2502 Quality score: ${String(Y).padEnd(3)}% \u2502`),R===0&&(console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524"),console.log("\u2502 Your Brain is empty and ready to learn. \u2502"),console.log("\u2502 After your first coding session it will contain: \u2502"),console.log("\u2502 \u2022 Lessons from every bug you fixed \u2502"),console.log("\u2502 \u2022 Your project's indexed files \u2502"),console.log("\u2502 \u2022 A session summary for next time \u2502")),console.log("\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518"),R>=5){process.stdout.write(`
307
- \u23F3 Running predictive risk scan...`);try{const L=(await U("brain_predict",{instance_id:r.id,context:"starting a new development session"})).split(`
308
- `).filter(M=>M.trim()).slice(0,6);if(L.length>0){console.log(` \u2713
309
- `),console.log("\u250C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510"),console.log("\u2502 \u{1F52E} Brain Risk Preview (brain_predict) \u2502"),console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524");for(const M of L)console.log(`\u2502 ${M.slice(0,51).padEnd(51)} \u2502`);console.log("\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518")}else console.log(" (no risks found \u2014 clean slate!)")}catch{console.log(" (skipped)")}}}else console.log(" (skipped)")}catch{console.log(" (skipped)")}if(console.log(`
295
+ Could not create an instance automatically. Opening ${j("/instances",u)} \u2026
296
+ `),ce(j("/instances",u)),h.close(),process.exit(1))}const i=p[0];p.length>1&&(console.log(`\u2139\uFE0F Multiple instances found \u2014 using most recent: ${i.name}`),console.log(` (Run with --instance-id <id> to use a different one)
297
+ `)),console.log(`\u2713 Instance: ${i.name} (${i.id.slice(0,8)}\u2026)
298
+ `),S("setup_instance_ready",{instance_id:i.id});const d=process.cwd(),y=[],{homedir:T}=await import("node:os"),L=T();y.push("claude"),n(o(d,".cursor"))&&y.push("cursor"),n(o(d,".windsurf"))&&y.push("windsurf"),n(o(d,".vscode"))&&y.push("copilot"),n(o(d,".continue"))&&y.push("continue");const E=process.platform==="darwin"?o(L,"Library/Application Support/Code/User/globalStorage/saoudrizwan.claude-dev"):o(L,".vscode/extensions");(n(E)&&!y.includes("copilot")||n(E))&&y.push("cline");const A=process.platform==="darwin"?o(L,"Library/Application Support/Zed"):o(L,".config/zed");n(A)&&y.push("zed");const C=f=>({claude:"Claude Code",cursor:"Cursor",windsurf:"Windsurf",copilot:"GitHub Copilot",continue:"Continue.dev",cline:"Cline (VSCode)",zed:"Zed"})[f]??f,I=y;console.log(`Step 3: Configuring for: ${I.map(C).join(", ")}
299
+ `);const{existsSync:$}=await import("node:fs"),k=[];let O=0;Q(b);for(const f of I){const _=me[f]??".mcp.json",R=o(d,_);try{await t(c(R),{recursive:!0});const U=$(R),J=await Lt(R,b,i.id,f,{readFile:s,existsSync:$});await e(R,J,"utf-8"),console.log(`\u2705 ${U?"Updated":"Written"}: ${_}`),O++}catch(U){const J=U.message;console.log(`\u26A0\uFE0F Could not write ${_}: ${J}`),console.log(" Fix permissions or run with sudo, then re-run setup."),k.push({file:_,reason:J})}}const Y=o(L,".claude","mcp.json");try{await t(c(Y),{recursive:!0});let f={};if(n(Y))try{f=JSON.parse(await s(Y,"utf-8"))}catch{}f.mcpServers??={},f.mcpServers.cachly={command:"npx",args:["-y","@cachly-dev/mcp-server@latest"],env:se(b,i.id,{includeApiKey:!0})},await e(Y,JSON.stringify(f,null,2),"utf-8"),console.log("\u2705 Written: ~/.claude/mcp.json (global \u2014 works in every project)"),O++}catch(f){const _=f.message;console.log(`\u26A0\uFE0F Could not write ~/.claude/mcp.json: ${_}`),k.push({file:"~/.claude/mcp.json",reason:_})}const K=await Pt(d,i.id);for(const[f,_]of Object.entries(K))console.log(`${_==="updated"?"\u2705 Updated":_==="appended"?"\u2705 Appended to":"\u2705 Written"}: ${f}`);v=b;let be=0;if(n(o(d,".git"))){process.stdout.write("\u23F3 Bootstrapping Brain from git history (~10s)...");try{const f=await z("brain_from_git",{instance_id:i.id,repo_path:d,limit:100}),_=f.match(/Ingested: \*\*(\d+)\*\*/);be=_?parseInt(_[1],10):0,console.log(` \u2713 ${be} lesson${be===1?"":"s"} extracted from git history`);const R=Mo(f);R&&(console.log(""),console.log(R))}catch(f){console.log(` (skipped: ${f.message.slice(0,80)})`)}}if(be===0){process.stdout.write("\u{1F331} Seeding 16 universal engineering lessons (first recall works instantly)...");try{await z("brain_seed_starter",{instance_id:i.id}),console.log(" \u2713");try{const f=await z("smart_recall",{instance_id:i.id,query:"docker build slow"}),_=Zn(f);_&&(console.log(""),console.log(Do(_.topic,_.what)))}catch{}}catch(f){console.log(` (skipped: ${f.message.slice(0,60)})`)}}process.stdout.write(`
300
+ \u23F3 Fetching your Brain health preview...`);try{const f=await fetch(`${x}/api/v1/instances/${i.id}/memory`,{headers:{Authorization:`Bearer ${b}`},signal:AbortSignal.timeout(6e3)});if(f.ok){const _=await f.json(),R=_.lesson_count??0,U=_.context_count??0,J=Math.round(_.iq_boost_pct??0),ee=R===0?"Intern \u{1F331}":R<10?"Junior Dev \u{1F527}":R<30?"Mid Dev \u26A1":R<60?"Senior Dev \u{1F9E0}":R<100?"Staff Eng \u{1F680}":"Principal Eng \u{1F3C6}";if(console.log(` \u2713
301
+ `),console.log("\u250C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510"),console.log("\u2502 \u{1F9E0} Brain Health Report \u2502"),console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524"),console.log(`\u2502 Lessons stored : ${String(R).padEnd(6)} \u2502 Level: ${ee.padEnd(20)}\u2502`),console.log(`\u2502 Context entries : ${String(U).padEnd(6)} \u2502 Quality score: ${String(J).padEnd(3)}% \u2502`),R===0&&(console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524"),console.log("\u2502 Your Brain is empty and ready to learn. \u2502"),console.log("\u2502 After your first coding session it will contain: \u2502"),console.log("\u2502 \u2022 Lessons from every bug you fixed \u2502"),console.log("\u2502 \u2022 Your project's indexed files \u2502"),console.log("\u2502 \u2022 A session summary for next time \u2502")),console.log("\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518"),R>=5){process.stdout.write(`
302
+ \u23F3 Running predictive risk scan...`);try{const B=(await z("brain_predict",{instance_id:i.id,context:"starting a new development session"})).split(`
303
+ `).filter(M=>M.trim()).slice(0,6);if(B.length>0){console.log(` \u2713
304
+ `),console.log("\u250C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510"),console.log("\u2502 \u{1F52E} Brain Risk Preview (brain_predict) \u2502"),console.log("\u251C\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2524");for(const M of B)console.log(`\u2502 ${M.slice(0,51).padEnd(51)} \u2502`);console.log("\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518")}else console.log(" (no risks found \u2014 clean slate!)")}catch{console.log(" (skipped)")}}}else console.log(" (skipped)")}catch{console.log(" (skipped)")}if(console.log(`
310
305
  \u{1F389} Your AI just got a permanent brain.`),console.log(` Restart your editor \u2014 from this session on, it arrives pre-briefed.
311
306
  `),console.log(` No more re-explaining your stack. No repeated mistakes.
312
- `),console.log(` Dashboard: ${N(`/instances/${r.id}`,u)}`),console.log(`
307
+ `),console.log(` Dashboard: ${j(`/instances/${i.id}`,u)}`),console.log(`
313
308
  \u{1F4DB} Add a live badge to your README:`),console.log(` npx @cachly-dev/mcp-server@latest badge
314
309
  `),!m)try{if(console.log(`
315
- \u{1F451} Governance (optional \u2014 role model: admin \xB7 reviewer \xB7 contributor \xB7 viewer)`),console.log("\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"),(await w(" Set yourself as the first admin? (y/N): ","N")).toLowerCase()==="y"){const _=await w(' Your handle (e.g. "alice"): ');_.trim()&&await U("team_assign_role",{instance_id:r.id,handle:_.trim(),role:"admin"}).catch(()=>"")&&(console.log(` \u2705 ${_.trim()} is now admin. Invite teammates:
316
- `),console.log(` team_assign_role(instance_id="${r.id}", handle="<teammate>", role="contributor", assigned_by="${_.trim()}")
310
+ \u{1F451} Governance (optional \u2014 role model: admin \xB7 reviewer \xB7 contributor \xB7 viewer)`),console.log("\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"),(await w(" Set yourself as the first admin? (y/N): ","N")).toLowerCase()==="y"){const _=await w(' Your handle (e.g. "alice"): ');_.trim()&&await z("team_assign_role",{instance_id:i.id,handle:_.trim(),role:"admin"}).catch(()=>"")&&(console.log(` \u2705 ${_.trim()} is now admin. Invite teammates:
311
+ `),console.log(` team_assign_role(instance_id="${i.id}", handle="<teammate>", role="contributor", assigned_by="${_.trim()}")
317
312
  `))}else console.log(` Skipped \u2014 run team_assign_role at any time.
318
313
  `)}catch{}if(!m){const f=await w(" \u{1F4EC} Stay in the loop? Email for release notes [Enter to skip]: ");if(f&&f.includes("@"))try{await fetch(`${x}/api/newsletter/subscribe`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({email:f.trim().toLowerCase(),source:"mcp-setup"}),signal:AbortSignal.timeout(5e3)}),console.log(` \u2705 Subscribed \u2014 you'll only hear from us when it matters.
319
- `)}catch{}}try{const f=await et(process.cwd(),r.id);f==="written"?console.log(`
314
+ `)}catch{}}try{const f=await nt(process.cwd(),i.id);f==="written"?console.log(`
320
315
  \u2705 CLS git hook installed \u2014 your brain will learn from every commit.`):f==="upgraded"?console.log(`
321
- \u2705 CLS git hook upgraded to ${tt}.`):f==="appended"&&console.log(`
322
- \u2705 CLS git hook appended to existing post-commit.`)}catch{}if(E.includes("claude"))try{const f=await nt(process.cwd(),r.id),_=f.scripts==="written"?"\u2705 Written":f.scripts==="upgraded"?`\u2705 Upgraded (\u2192 ${st})`:"\u2713 Unchanged";console.log(`${_}: .claude/hooks/ (Ambient Recall \u2014 session briefing, per-prompt recall, file briefing, auto-learn)`),f.settings==="written"?console.log("\u2705 Written: .claude/settings.json (Ambient Recall hooks wired)"):f.settings==="merged"?console.log("\u2705 Merged: .claude/settings.json (Ambient Recall hooks wired)"):console.log("\u2713 Ambient Recall hooks already wired in .claude/settings.json")}catch{}E.includes("cursor")&&await ge("cursor",process.cwd(),r.id," "),E.includes("copilot")&&await ge("copilot",process.cwd(),r.id," "),(o(n(B,".codex"))||o(n(d,".codex")))&&await ge("codex",process.cwd(),r.id," "),(o(n(B,".gemini"))||o(n(d,".gemini")))&&await ge("gemini",process.cwd(),r.id," "),v=b;const Oe=Math.round((Date.now()-a)/1e3);if(C.length>0&&A("setup_config_write_failed",{instance_id:r.id,failed:C.length,succeeded:j}),A("setup_completed",{instance_id:r.id,elapsed_s:Oe,config_failures:C.length}),C.length>0&&j===0){console.log(`
316
+ \u2705 CLS git hook upgraded to ${st}.`):f==="appended"&&console.log(`
317
+ \u2705 CLS git hook appended to existing post-commit.`)}catch{}if(I.includes("claude"))try{const f=await at(process.cwd(),i.id),_=f.scripts==="written"?"\u2705 Written":f.scripts==="upgraded"?`\u2705 Upgraded (\u2192 ${ct})`:"\u2713 Unchanged";console.log(`${_}: .claude/hooks/ (Ambient Recall \u2014 session briefing, per-prompt recall, file briefing, auto-learn)`),f.settings==="written"?console.log("\u2705 Written: .claude/settings.json (Ambient Recall hooks wired)"):f.settings==="merged"?console.log("\u2705 Merged: .claude/settings.json (Ambient Recall hooks wired)"):console.log("\u2713 Ambient Recall hooks already wired in .claude/settings.json")}catch{}I.includes("cursor")&&await ye("cursor",process.cwd(),i.id," "),I.includes("copilot")&&await ye("copilot",process.cwd(),i.id," "),(n(o(L,".codex"))||n(o(d,".codex")))&&await ye("codex",process.cwd(),i.id," "),(n(o(L,".gemini"))||n(o(d,".gemini")))&&await ye("gemini",process.cwd(),i.id," "),v=b;const De=Math.round((Date.now()-a)/1e3);if(k.length>0&&S("setup_config_write_failed",{instance_id:i.id,failed:k.length,succeeded:O}),S("setup_completed",{instance_id:i.id,elapsed_s:De,config_failures:k.length}),k.length>0&&O===0){console.log(`
323
318
  \u2554\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2557`),console.log("\u2551 \u26A0\uFE0F Setup incomplete \u2014 no editor config was written \u2551"),console.log("\u255A\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u255D"),console.log(`
324
- Your Brain instance exists, but no editor knows about it yet.`),console.log(" Failed writes:");for(const f of C)console.log(` \u2022 ${f.file} \u2014 ${f.reason}`);console.log(`
325
- Fix the permissions above and re-run, or configure manually:`),console.log(` CACHLY_JWT=${b.slice(0,12)}\u2026 CACHLY_BRAIN_INSTANCE_ID=${r.id}`),console.log(` Docs: ${N("/docs/mcp",u)}
326
- `),y.close(),await new Promise(f=>setTimeout(f,300)),process.exit(1)}if(console.log(`
327
- \u2554\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2557`),console.log("\u2551 \u{1F680} Brain is ready. \u2551"),console.log(`\u2551 \u23F1\uFE0F Setup completed in ${(Oe+"s").padEnd(5)} \u2014 restart your editor \u2551`),console.log("\u2551 and your AI arrives pre-briefed every session. \u2551"),console.log("\u255A\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u255D"),C.length>0){console.log(`
328
- \u26A0\uFE0F Some editor configs could not be written (others succeeded):`);for(const f of C)console.log(` \u2022 ${f.file} \u2014 ${f.reason}`);console.log(" Fix the permissions above and re-run setup to configure them too.")}console.log(""),y.close(),await new Promise(f=>setTimeout(f,300)),process.exit(0)}if(process.argv[2]==="index"){const{resolve:e}=await import("node:path"),t=process.argv.slice(3),s=a=>{const u=t.indexOf(`--${a}`);return u!==-1?t[u+1]:void 0},o=e(s("dir")??t.find(a=>!a.startsWith("--"))??"."),n=s("instance-id")??process.env.CACHLY_BRAIN_INSTANCE_ID,c=parseInt(s("max-files")??"500",10),i=s("namespace")??"cachly:sem:code";(!n||!v)&&(console.error(`
329
- \u274C CACHLY_BRAIN_INSTANCE_ID and CACHLY_JWT must be set
319
+ Your Brain instance exists, but no editor knows about it yet.`),console.log(" Failed writes:");for(const f of k)console.log(` \u2022 ${f.file} \u2014 ${f.reason}`);console.log(`
320
+ Fix the permissions above and re-run, or configure manually:`),console.log(` CACHLY_JWT=${b.slice(0,12)}\u2026 CACHLY_BRAIN_INSTANCE_ID=${i.id}`),console.log(` Docs: ${j("/docs/mcp",u)}
321
+ `),h.close(),await new Promise(f=>setTimeout(f,300)),process.exit(1)}if(console.log(`
322
+ \u2554\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2557`),console.log("\u2551 \u{1F680} Brain is ready. \u2551"),console.log(`\u2551 \u23F1\uFE0F Setup completed in ${(De+"s").padEnd(5)} \u2014 restart your editor \u2551`),console.log("\u2551 and your AI arrives pre-briefed every session. \u2551"),console.log("\u255A\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u255D"),k.length>0){console.log(`
323
+ \u26A0\uFE0F Some editor configs could not be written (others succeeded):`);for(const f of k)console.log(` \u2022 ${f.file} \u2014 ${f.reason}`);console.log(" Fix the permissions above and re-run setup to configure them too.")}console.log(""),h.close(),await new Promise(f=>setTimeout(f,300)),process.exit(0)}if(process.argv[2]==="index"){const{resolve:e}=await import("node:path"),t=process.argv.slice(3),s=a=>{const u=t.indexOf(`--${a}`);return u!==-1?t[u+1]:void 0},n=e(s("dir")??t.find(a=>!a.startsWith("--"))??"."),o=s("instance-id")||F(),c=parseInt(s("max-files")??"500",10),r=s("namespace")??"cachly:sem:code";(!o||!v)&&(console.error(`
324
+ \u274C No Brain instance or key found (flag, environment or ~/.cachly/credentials.json)
330
325
  `),console.error(" export CACHLY_BRAIN_INSTANCE_ID=<uuid>"),console.error(" export CACHLY_JWT=<cky_live_...>"),console.error(` npx @cachly-dev/mcp-server@latest index ./my-project
331
326
  `),process.exit(1)),console.log(`
332
- \u{1F4C2} Indexing: ${o}`),console.log(` Instance: ${n.slice(0,8)}\u2026 Max files: ${c}
333
- `);try{const a=await U("index_project",{instance_id:n,dir:o,max_files:c,ttl:604800,namespace:i});console.log(a),console.log(`
327
+ \u{1F4C2} Indexing: ${n}`),console.log(` Instance: ${o.slice(0,8)}\u2026 Max files: ${c}
328
+ `);try{const a=await z("index_project",{instance_id:o,dir:n,max_files:c,ttl:604800,namespace:r});console.log(a),console.log(`
334
329
  \u2705 Indexing complete.
335
330
  `)}catch(a){console.error(`
336
331
  \u274C Indexing failed: ${a.message}
337
- `),process.exit(1)}process.exit(0)}if(process.argv[2]==="learn-git"){const{resolve:e}=await import("node:path"),t=process.argv.slice(3),s=i=>{const a=t.indexOf(`--${i}`);return a!==-1?t[a+1]:void 0},o=e(s("dir")??t.find(i=>!i.startsWith("--"))??"."),n=s("instance-id")??process.env.CACHLY_BRAIN_INSTANCE_ID,c=parseInt(s("max-commits")??"50",10);(!n||!v)&&(console.error(`
338
- \u274C CACHLY_BRAIN_INSTANCE_ID and CACHLY_JWT must be set
332
+ `),process.exit(1)}process.exit(0)}if(process.argv[2]==="learn-git"){const{resolve:e}=await import("node:path"),t=process.argv.slice(3),s=r=>{const a=t.indexOf(`--${r}`);return a!==-1?t[a+1]:void 0},n=e(s("dir")??t.find(r=>!r.startsWith("--"))??"."),o=s("instance-id")||F(),c=parseInt(s("max-commits")??"50",10);(!o||!v)&&(console.error(`
333
+ \u274C No Brain instance or key found (flag, environment or ~/.cachly/credentials.json)
339
334
  `),console.error(" export CACHLY_BRAIN_INSTANCE_ID=<uuid>"),console.error(" export CACHLY_JWT=<cky_live_...>"),console.error(` npx @cachly-dev/mcp-server@latest learn-git .
340
335
  `),process.exit(1)),console.log(`
341
- \u{1F9E0} Learning from git history: ${o}`),console.log(` Instance: ${n.slice(0,8)}\u2026 Max commits: ${c}
342
- `);try{const i=await U("brain_from_git",{instance_id:n,repo_path:o,limit:c});console.log(i),A("brain_from_git",{api_key:v,instance_id:n}),console.log(`
336
+ \u{1F9E0} Learning from git history: ${n}`),console.log(` Instance: ${o.slice(0,8)}\u2026 Max commits: ${c}
337
+ `);try{const r=await z("brain_from_git",{instance_id:o,repo_path:n,limit:c});console.log(r),S("brain_from_git",{api_key:v,instance_id:o}),console.log(`
343
338
  \u2705 Brain learned from your commits.
344
- `),await new Promise(a=>setTimeout(a,300))}catch(i){console.error(`
345
- \u274C learn-git failed: ${i.message}
346
- `),process.exit(1)}process.exit(0)}if(process.argv[2]==="ambient-recall"&&await Ko(process.argv.slice(3),process.argv[1]),process.argv[2]==="ambient-auffrischen"&&await qo(),process.argv[2]===Vo&&await Go(process.argv[3]??""),process.argv[2]==="ambient-credit"){try{const e=Math.round(Number(process.argv[3]));if(Number.isFinite(e)&&e>0){const t=Math.min(e,2e4),s=process.argv.slice(4).join(" ").slice(0,200)||void 0,o={ts:new Date().toISOString(),event:"credit",injected:0,prevented:t,note:s};await Qo(o),await qn(process.env.CACHLY_BRAIN_INSTANCE_ID??H,o),console.log(`\u2705 ambient credit recorded: ${t} tokens${s?` (${s})`:""}`)}else console.error("Usage: npx @cachly-dev/mcp-server@latest ambient-credit <tokens-saved> [note]")}catch{}process.exit(0)}if(process.argv[2]==="ambient-stats"){const e=await rt(),t=Jn(e),s=e.slice(-5);if(console.log(`
339
+ `),await new Promise(a=>setTimeout(a,300))}catch(r){console.error(`
340
+ \u274C learn-git failed: ${r.message}
341
+ `),process.exit(1)}process.exit(0)}if(process.argv[2]==="ambient-recall"&&await Xo(process.argv.slice(3),process.argv[1]),process.argv[2]==="ambient-auffrischen"&&await en(),process.argv[2]===on&&await tn(process.argv[3]??""),process.argv[2]==="ambient-credit"){try{const e=Math.round(Number(process.argv[3]));if(Number.isFinite(e)&&e>0){const t=Math.min(e,2e4),s=process.argv.slice(4).join(" ").slice(0,200)||void 0,n={ts:new Date().toISOString(),event:"credit",injected:0,prevented:t,note:s};await sn(n),await Xn(process.env.CACHLY_BRAIN_INSTANCE_ID??P,n),console.log(`\u2705 ambient credit recorded: ${t} tokens${s?` (${s})`:""}`)}else console.error("Usage: npx @cachly-dev/mcp-server@latest ambient-credit <tokens-saved> [note]")}catch{}process.exit(0)}if(process.argv[2]==="ambient-stats"){const e=await lt(),t=Yn(e),s=e.slice(-5);if(console.log(`
347
342
  \u{1F9E0} Ambient Recall \u2014 net-token ledger
348
343
  `),console.log(` Turns recorded: ${e.length}`),console.log(` Injected tokens: ${t.injected}`),console.log(` Prevented tokens: ${t.prevented} (agent-reported via ambient-credit)`),console.log(` NET: ${t.net>=0?"+":""}${t.net} tokens`),s.length>0){console.log(`
349
- Last entries:`);for(const o of s){const n=o.prevented>0?`+${o.prevented} prevented`:`-${o.injected} injected`;console.log(` \u2022 ${o.ts.slice(0,19)} ${o.event}: ${n}${o.note?` (${o.note})`:""}`)}}console.log(""),process.exit(0)}if(process.argv[2]==="doctor"){const{checkNodeVersion:e,checkCredential:t,checkApiReachable:s,checkAuthAccepted:o,checkInstance:n,checkVectorCoverage:c,inspectAmbientHooks:i,checkHooks:a,checkLedger:u,renderDoctorReport:g,doctorExitCode:l}=await import("./doctor.js"),m=[e(),t(v),await s(x),await o(x,v),n(process.env.CACHLY_BRAIN_INSTANCE_ID??H),await c(x,v,process.env.CACHLY_BRAIN_INSTANCE_ID??H),a(i(process.cwd())),u(await rt(),Xo())];console.log(g(m)),process.exit(l(m))}if(process.argv[2]==="cls-ingest"){try{const e=process.argv[3];(!e||!v)&&process.exit(0);let t;try{t=JSON.parse(e)}catch{process.exit(0)}const s=t.instance_id??H;(!s||!t.source)&&process.exit(0),await U("cls_ingest",{instance_id:s,source:t.source,payload:t.payload??{}})}catch{}process.exit(0)}if(!v){const e=Pt();if(e)try{const t=await fetch(Dt(e),{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:Mt(e),signal:AbortSignal.timeout(8e3)});if(t.ok){const s=await t.json();s.access_token&&(v=s.access_token,ae(s.access_token),A("m2m_auth_completed",{grant:"client_credentials",client_id:e.clientId}),process.stderr.write(`\u{1F916} cachly: authenticated via client_credentials (M2M, headless).
344
+ Last entries:`);for(const n of s){const o=n.prevented>0?`+${n.prevented} prevented`:`-${n.injected} injected`;console.log(` \u2022 ${n.ts.slice(0,19)} ${n.event}: ${o}${n.note?` (${n.note})`:""}`)}}console.log(""),process.exit(0)}if(process.argv[2]==="uninstall"){const{homedir:e,tmpdir:t}=await import("node:os"),{resolve:s}=await import("node:path"),{createInterface:n}=await import("node:readline/promises"),o=process.argv.slice(3),r={projekt:s((a=>{const u=o.indexOf(`--${a}`);return u!==-1?o[u+1]:void 0})("project-dir")??"."),heim:process.env.HOME??process.env.USERPROFILE??process.env.APPDATA??e(),tmp:t(),schluessel:oe(),instanz:F(),apiUrl:x,istTty:process.stdin.isTTY===!0&&process.stdout.isTTY===!0,ausgabe:a=>console.log(a),frage:async a=>{const u=n({input:process.stdin,output:process.stdout});try{return/^(y|yes|j|ja)$/i.test((await u.question(a)).trim())}finally{u.close()}}};process.exit(await zo(o,r))}if(process.argv[2]==="doctor"){const{checkNodeVersion:e,checkCredential:t,checkApiReachable:s,checkAuthAccepted:n,checkInstance:o,checkVectorCoverage:c,inspectAmbientHooks:r,checkHooks:a,checkLedger:u,renderDoctorReport:g,doctorExitCode:l}=await import("./doctor.js"),m=[e(),t(v),await s(x),await n(x,v),o(F()||P),await c(x,v,F()||P),a(r(process.cwd())),u(await lt(),rn())];console.log(g(m)),process.exit(l(m))}if(process.argv[2]==="cls-ingest"){try{const e=process.argv[3];(!e||!v)&&process.exit(0);let t;try{t=JSON.parse(e)}catch{process.exit(0)}const s=t.instance_id??P;(!s||!t.source)&&process.exit(0),await z("cls_ingest",{instance_id:s,source:t.source,payload:t.payload??{}})}catch{}process.exit(0)}if(!v){const e=Ut();if(e)try{const t=await fetch(zt(e),{method:"POST",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:Jt(e),signal:AbortSignal.timeout(8e3)});if(t.ok){const s=await t.json();s.access_token&&(v=s.access_token,le(s.access_token),S("m2m_auth_completed",{grant:"client_credentials",client_id:e.clientId}),process.stderr.write(`\u{1F916} cachly: authenticated via client_credentials (M2M, headless).
350
345
  `))}else process.stderr.write(`\u26A0\uFE0F cachly: client_credentials auth failed (${t.status}). Check CACHLY_CLIENT_ID / CACHLY_CLIENT_SECRET.
351
- `),A("m2m_auth_failed",{status:t.status})}catch(t){process.stderr.write(`\u26A0\uFE0F cachly: client_credentials auth error: ${t.message}
352
- `),A("m2m_auth_failed",{reason:"network"})}}const ps=["demo","share","publish","health","autosetup","setup","autopilot","init","digest","invite","badge","join","upgrade","bench","tool-specs","openapi","ambient-credit","ambient-stats","doctor"];if(!v&&!ps.includes(process.argv[2]??""))if(!process.argv[2]&&process.stdout.isTTY===!0&&Kt){const o=`
346
+ `),S("m2m_auth_failed",{status:t.status})}catch(t){process.stderr.write(`\u26A0\uFE0F cachly: client_credentials auth error: ${t.message}
347
+ `),S("m2m_auth_failed",{reason:"network"})}}const hs=["demo","share","publish","health","autosetup","setup","autopilot","init","digest","invite","badge","join","upgrade","bench","tool-specs","openapi","ambient-credit","ambient-stats","doctor","uninstall"];if(!v&&!hs.includes(process.argv[2]??""))if(!process.argv[2]&&process.stdout.isTTY===!0&&Vt){const n=`
353
348
  \u2554\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2557
354
349
  \u2551 \u{1F9E0} cachly AI Brain \u2014 Setup required \u2551
355
350
  \u2560\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2563
356
351
  \u2551 \u2551
357
352
  \u2551 CACHLY_JWT is not set. Get your free credentials at: \u2551
358
353
  \u2551 \u2551
359
- `+(`\u2551 \u{1F449} ${N("/setup-ai","first-run")}`.padEnd(66)+`\u2551
354
+ `+(`\u2551 \u{1F449} ${j("/setup-ai","first-run")}`.padEnd(66)+`\u2551
360
355
  `)+`\u2551 \u2551
361
356
  \u2551 Then run the one-command setup wizard: \u2551
362
357
  \u2551 \u2551
@@ -365,19 +360,19 @@ Could not create an instance automatically. Opening ${N("/instances",u)} \u2026
365
360
  \u2551 Free tier \u2014 no credit card required. \u2551
366
361
  \u255A\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u255D
367
362
 
368
- `;process.stdout.write(o),process.exit(0)}else process.stderr.write(`
363
+ `;process.stdout.write(n),process.exit(0)}else process.stderr.write(`
369
364
  \u{1F9E0} cachly: no API key yet \u2014 the first cachly tool call creates a test Brain on EU servers, no sign-up.
370
365
  (Own account: npx @cachly-dev/mcp-server@latest autopilot)
371
366
 
372
- `);else{const e=jt(v);if(e!==null){const t=Math.floor((e-Date.now())/6e4);t<=0?process.stderr.write(`
367
+ `);else{const e=Ht(v);if(e!==null){const t=Math.floor((e-Date.now())/6e4);t<=0?process.stderr.write(`
373
368
  \u26A0\uFE0F cachly: CACHLY_JWT expired ${Math.abs(t)} minute(s) ago.
374
- All tool calls will fail. Get a fresh token at ${N("/setup-ai","jwt-expiry")}
369
+ All tool calls will fail. Get a fresh token at ${j("/setup-ai","jwt-expiry")}
375
370
 
376
371
  `):t<60&&process.stderr.write(`
377
372
  \u26A0\uFE0F cachly: CACHLY_JWT expires in ${t} minute(s).
378
- Refresh it soon at ${N("/setup-ai","jwt-expiry")} to avoid interruptions.
373
+ Refresh it soon at ${j("/setup-ai","jwt-expiry")} to avoid interruptions.
379
374
 
380
- `)}}process.env.PORT||kn({aktuelle:Sn(D)});const xe=process.env.PORT?parseInt(process.env.PORT,10):void 0;if(xe){const{createServer:e}=await import("node:http");e((s,o)=>{const n=new URL(s.url??"/",`http://localhost:${xe}`);if(n.pathname==="/health"){o.writeHead(200,{"Content-Type":"application/json"}),o.end(JSON.stringify({status:"ok",version:D,zeroResults:{total:Co,last10:$o.slice(-10).map(c=>({query:c.query,ts:new Date(c.ts).toISOString()}))}}));return}if(n.pathname==="/mcp"||n.pathname==="/"){(async()=>{const c=rs(),i=new Gt({sessionIdGenerator:void 0});o.on("close",()=>{i.close().catch(()=>{}),c.close().catch(()=>{})});try{await c.connect(i),await i.handleRequest(s,o)}catch(a){process.stderr.write(`cachly-mcp HTTP: ${a.message}
381
- `),o.headersSent||(o.writeHead(500,{"Content-Type":"application/json"}),o.end(JSON.stringify({error:a.message})))}})();return}o.writeHead(404),o.end("not found")}).listen(xe,()=>{process.stderr.write(`cachly-mcp HTTP server listening on :${xe}
382
- `)})}else{const e=new qt;W("start: Module geladen, stdio verbinden"),await G.connect(e),typeof process.argv[2]=="string"&&!process.argv[2].startsWith("-")||(Ge=!0,oo(),Rn(),In(process.stdin,s=>{process.stderr.write(`[cachly-mcp] ${s} \u2014 Session zu Ende, Server geht mit
383
- `),(async()=>{await je().catch(()=>{});for(const[,o]of te)await o.quit().catch(()=>{});process.exit(0)})()}))}De("cachly","startup",{version:D,mode:process.env.MCP_HTTP_PORT?"http":"stdio"}).catch(()=>{});export{Tt as CLAUDE_MD_MARKER_END,He as CLAUDE_MD_MARKER_START,_o as CROSS_LINGUAL_MAP,ve as EDITOR_FILES,bo as STOPWORDS,wo as arabicLightStem,Nt as buildClaudeMdBlock,cs as buildMcpConfig,oe as buildServerEnv,xo as expandCrossLingual,yo as extractTimestamp,vo as katakanaToRomaji,fo as levenshtein,as as mcpAddBefehl,Rt as mergeMcpConfig,ho as recencyBoost,Ao as reconnectOnce,go as splitMultiQuery,co as startBackgroundDevicePoll,mo as tokenize,Lt as writeInstructions};
375
+ `)}}process.env.PORT||jn({aktuelle:Rn(D)});const ke=process.env.PORT?parseInt(process.env.PORT,10):void 0;if(ke){const{createServer:e}=await import("node:http");e((s,n)=>{const o=new URL(s.url??"/",`http://localhost:${ke}`);if(o.pathname==="/health"){n.writeHead(200,{"Content-Type":"application/json"}),n.end(JSON.stringify({status:"ok",version:D,zeroResults:{total:Co,last10:Ao.slice(-10).map(c=>({query:c.query,ts:new Date(c.ts).toISOString()}))}}));return}if(o.pathname==="/mcp"||o.pathname==="/"){(async()=>{const c=ds(),r=new Qt({sessionIdGenerator:void 0});n.on("close",()=>{r.close().catch(()=>{}),c.close().catch(()=>{})});try{await c.connect(r),await r.handleRequest(s,n)}catch(a){process.stderr.write(`cachly-mcp HTTP: ${a.message}
376
+ `),n.headersSent||(n.writeHead(500,{"Content-Type":"application/json"}),n.end(JSON.stringify({error:a.message})))}})();return}n.writeHead(404),n.end("not found")}).listen(ke,()=>{process.stderr.write(`cachly-mcp HTTP server listening on :${ke}
377
+ `)})}else{const e=new Zt;W("start: Module geladen, stdio verbinden"),await V.connect(e),typeof process.argv[2]=="string"&&!process.argv[2].startsWith("-")||(Qe=!0,io(),Pn(),Ln(process.stdin,s=>{process.stderr.write(`[cachly-mcp] ${s} \u2014 Session zu Ende, Server geht mit
378
+ `),(async()=>{await Me().catch(()=>{});for(const[,n]of ne)await n.quit().catch(()=>{});process.exit(0)})()}))}ze("cachly","startup",{version:D,mode:process.env.MCP_HTTP_PORT?"http":"stdio"}).catch(()=>{});export{it as CLAUDE_MD_MARKER_END,Be as CLAUDE_MD_MARKER_START,So as CROSS_LINGUAL_MAP,me as EDITOR_FILES,xo as STOPWORDS,$o as arabicLightStem,Ot as buildClaudeMdBlock,ms as buildMcpConfig,se as buildServerEnv,ko as expandCrossLingual,wo as extractTimestamp,_o as katakanaToRomaji,bo as levenshtein,ps as mcpAddBefehl,Lt as mergeMcpConfig,vo as recencyBoost,To as reconnectOnce,yo as splitMultiQuery,po as startBackgroundDevicePoll,ho as tokenize,Pt as writeInstructions};