const __vite__mapDeps=(i,m=__vite__mapDeps,d=(m.f||(m.f=["./BlapV1mo.js","./bG-fsHMS.js","./DrKWWDpL.js","./DsQIgu-4.js","./DTkg6Mnx.js","./DYP3EJMY.js","./DeO5NWhN.js","./DgZn9uJn.js","./CbGRcr-D.js","./CudpqN6Z.js","./CX8DOZuw2.js","./DOqzs8kl2.js","./DIQmuJJZ.js","./D7w7HllH.js","./DB5ifLM-.js","./CxlOpib82.js","./Czb-RtcG.js","./C0nwgWmB.js","./D-eE9s2f.js","./CWxOfblT.js","./DlZwu-Y7.js","./DfVstSkc.js","./BSkUUZJr2.js","./kwfd020K.js","./BDDiPmhF.js","./BYJC6_MM.js","./BfWZkE2t2.js","./DmLgVuva2.js","./C0bVU7gr2.js","./Ouh6Dj2I2.js","./Dn7LRnjg2.js","./DsanjglN.js","./CvFbqtf9.js","./-TjUTcZv2.js"])))=>i.map(i=>d[i]);
import{s as e}from"./DrKWWDpL.js";import{n as t,t as n}from"./DsQIgu-4.js";import{t as r}from"./DYP3EJMY.js";import{t as i}from"./DndP_Gki2.js";import{k as a}from"./DRa3KiKE.js";import{t as o}from"./DqwoSFZY.js";var s=e(r(),1);t();var c={maxObservations:10,maxLearnings:8,maxPatterns:8,maxDecisions:5,includeHandoff:!0,includeAntiPatterns:!0},l=.5,u=90,d=.6;function f(e,t,...n){return e.prepare(t).all(...n)}async function p(e,t){let r={...c,...t},{getBrainDb:i,getBrainNativeDb:a}=await n(async()=>{let{getBrainDb:e,getBrainNativeDb:t}=await import(`./BlapV1mo.js`);return{getBrainDb:e,getBrainNativeDb:t}},__vite__mapDeps([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]),import.meta.url);await i(e);let o=a();if(!o)return S();let s=[`# CLEO Memory Bridge`,``,`> Auto-generated at ${new Date().toISOString().slice(0,19)}`,"> Do not edit manually. Regenerate with `cleo refresh-memory`.",``];if(r.includeHandoff){let t=await _(e);if(t){let e=t.handoff;s.push(`## Last Session`),s.push(``),s.push(`- **Session**: ${t.sessionId}`),e.lastTask&&s.push(`- **Last focused task**: ${e.lastTask}`),e.tasksCompleted.length>0&&s.push(`- **Completed**: ${e.tasksCompleted.join(`, `)}`),e.nextSuggested.length>0&&s.push(`- **Next suggested**: ${e.nextSuggested.join(`, `)}`),e.openBlockers.length>0&&s.push(`- **Open blockers**: ${e.openBlockers.join(`, `)}`),e.note&&s.push(`- **Note**: ${e.note}`),s.push(``)}}let l=v(o,r.maxDecisions);if(l.length>0){s.push(`## Recent Decisions`),s.push(``);for(let e of l){let t=(e.created_at??``).slice(0,10);s.push(`- [${e.id}] ${e.decision.slice(0,300)} (${t})`)}s.push(``)}let u=y(o,r.maxLearnings);if(u.length>0){s.push(`## Key Learnings`),s.push(``);for(let e of u)s.push(`- [${e.id}] ${e.insight.slice(0,400)} (confidence: ${e.confidence})`);s.push(``)}let d=b(o,`follow`,r.maxPatterns);if(d.length>0){s.push(`## Patterns to Follow`),s.push(``);for(let e of d)s.push(`- [${e.id}] ${e.pattern.slice(0,300)} (${e.type})`);s.push(``)}if(r.includeAntiPatterns){let e=b(o,`avoid`,r.maxPatterns);if(e.length>0){s.push(`## Anti-Patterns to Avoid`),s.push(``);for(let t of e)s.push(`- [${t.id}] AVOID: ${t.pattern.slice(0,300)}`);s.push(``)}}let f=x(o,r.maxObservations);if(f.length>0){s.push(`## Recent Observations`),s.push(``);for(let e of f){let t=(e.created_at??``).slice(0,10);s.push(`- [${e.id}] ${t}: ${e.title.slice(0,200)}`)}s.push(``)}return s.join(`
`)}async function m(e,t){let n=(0,s.join)(e,`.cleo`),r=(0,s.join)(n,`memory-bridge.md`);try{if(await o(e)!==`file`)return{path:r,written:!1};let i=await p(e,t);if((0,s.existsSync)(n)||(0,s.mkdirSync)(n,{recursive:!0}),(0,s.existsSync)(r)){let e=(0,s.readFileSync)(r,`utf-8`),t=e=>e.replace(/^> Auto-generated at .*/m,``);if(t(e)===t(i))return{path:r,written:!1}}return(0,s.writeFileSync)(r,i,`utf-8`),{path:r,written:!0}}catch(e){return a({code:`W_BRIDGE_WRITE_FAILED`,message:`Failed to write memory bridge`,severity:`warn`,context:{bridge:`memory`,path:r,error:e instanceof Error?e.message:String(e)}}),{path:r,written:!1}}}async function h(e,t,r){try{let{loadConfig:i}=await n(async()=>{let{loadConfig:e}=await import(`./DfVstSkc.js`);return{loadConfig:e}},__vite__mapDeps([21,22,2,23,12,13,14,5,15,24,25,10,3,11]),import.meta.url);(await i(e)).brain?.memoryBridge?.contextAware&&t?await g(e,t,r):await m(e)}catch(e){a({code:`W_BRIDGE_REFRESH_FAILED`,message:`Memory bridge refresh failed`,severity:`warn`,context:{bridge:`memory`,error:e instanceof Error?e.message:String(e)}})}}async function g(e,t,r){try{let{loadConfig:i}=await n(async()=>{let{loadConfig:e}=await import(`./DfVstSkc.js`);return{loadConfig:e}},__vite__mapDeps([21,22,2,23,12,13,14,5,15,24,25,10,3,11]),import.meta.url),a=(await i(e)).brain?.memoryBridge?.maxTokens??2e3;if(await o(e)===`cli`)return;let c=r?`${t} ${r}`:t,l=[];try{let{hybridSearch:t}=await n(async()=>{let{hybridSearch:e}=await import(`./BfWZkE2t2.js`);return{hybridSearch:e}},__vite__mapDeps([26,27,3,2,28,29,30,31,8,1,4,5,6,7,9,10,11,12,13,14,15,16,17,18,19,20,32,33]),import.meta.url),r=await t(c,e,{limit:10});r&&r.length>0&&(l=r.slice(0,5).map(e=>`- [${e.id}] ${e.title??e.text?.slice(0,120)??``}`))}catch{await m(e);return}let u=a*4,d=(0,s.join)(e,`.cleo`),f=(0,s.join)(d,`memory-bridge.md`),h=[`# CLEO Memory Bridge`,``,`> Auto-generated at ${new Date().toISOString().slice(0,19)} (context-aware: ${t})`,"> Do not edit manually. Regenerate with `cleo refresh-memory`.",``],g=l.length>0?[`## Relevant Context`,``,...l,``]:[],_=await p(e),v=[...h,...g].join(`
`),y=u-v.length,b=y>200?v+`
`+_.slice(0,y):v;(0,s.existsSync)(d)||(0,s.mkdirSync)(d,{recursive:!0}),(0,s.writeFileSync)(f,b,`utf-8`)}catch{try{await m(e)}catch{}}}async function _(e){try{return await i(e)}catch{return null}}function v(e,t){try{return f(e,`SELECT id, decision, created_at FROM brain_decisions ORDER BY created_at DESC LIMIT ?`,t)}catch{return[]}}function y(e,t){try{let n=f(e,`SELECT id, insight, confidence, created_at, updated_at FROM brain_learnings
         WHERE CAST(confidence AS REAL) >= 0.3
           AND insight NOT LIKE 'Completed:%'
         ORDER BY confidence DESC, created_at DESC
         LIMIT ?`,t*3),r=Date.now();return n.map(e=>{let t=e.updated_at||e.created_at,n=Math.max(0,(r-new Date(t).getTime())/864e5),i=(parseFloat(e.confidence)||0)*l**(n/u);return{...e,effectiveConfidence:i}}).filter(e=>e.effectiveConfidence>=d).sort((e,t)=>t.effectiveConfidence-e.effectiveConfidence).slice(0,t).map(({effectiveConfidence:e,...t})=>({...t,confidence:e.toFixed(2)}))}catch{return[]}}function b(e,t,n){try{return f(e,`SELECT id, pattern, type, impact, extracted_at FROM brain_patterns
         WHERE ${t===`follow`?`type IN ('success', 'workflow', 'optimization')`:`type IN ('failure', 'blocker')`}
           AND pattern NOT LIKE 'Recurring label%'
           AND pattern NOT LIKE 'Test pattern%'
           AND pattern NOT LIKE 'test'
           AND pattern NOT LIKE 'Audit probe:%'
           AND LENGTH(pattern) > 20
         ORDER BY extracted_at DESC
         LIMIT ?`,n)}catch{return[]}}function x(e,t){try{return f(e,`SELECT id, title, type, created_at FROM brain_observations
         WHERE type != 'change'
           AND title NOT LIKE 'File changed:%'
           AND title NOT LIKE 'Task start:%'
           AND title NOT LIKE 'Task complete:%'
           AND title NOT LIKE '[hook]%'
         ORDER BY created_at DESC LIMIT ?`,t)}catch{return[]}}function S(){return[`# CLEO Memory Bridge`,``,`> Auto-generated at ${new Date().toISOString().slice(0,19)}`,``,`No brain.db data available yet. Observations will appear here as you work.`,``].join(`
`)}export{h as refreshMemoryBridge};