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","./bXAQrK7j.js"])))=>i.map(i=>d[i]);
import{n as e,t}from"./DsQIgu-4.js";import{n}from"./Dn7LRnjg2.js";import{t as r}from"./CvFbqtf9.js";import{t as i}from"./-TjUTcZv2.js";import{t as a}from"./C0bVU7gr2.js";e();var o=null,s=!1;function c(e){if(o!==null)return o;try{e.prepare(`CREATE VIRTUAL TABLE IF NOT EXISTS _fts5_check USING fts5(test)`).run(),e.prepare(`DROP TABLE IF EXISTS _fts5_check`).run(),o=!0}catch{o=!1}return o}function l(e,t){e.prepare(t).run()}function u(e){return c(e)?(l(e,`
    CREATE VIRTUAL TABLE IF NOT EXISTS brain_decisions_fts
    USING fts5(id, decision, rationale, content=brain_decisions, content_rowid=rowid)
  `),l(e,`
    CREATE VIRTUAL TABLE IF NOT EXISTS brain_patterns_fts
    USING fts5(id, pattern, context, content=brain_patterns, content_rowid=rowid)
  `),l(e,`
    CREATE VIRTUAL TABLE IF NOT EXISTS brain_learnings_fts
    USING fts5(id, insight, source, content=brain_learnings, content_rowid=rowid)
  `),l(e,`
    CREATE TRIGGER IF NOT EXISTS brain_decisions_ai AFTER INSERT ON brain_decisions BEGIN
      INSERT INTO brain_decisions_fts(rowid, id, decision, rationale)
      VALUES (new.rowid, new.id, new.decision, new.rationale);
    END
  `),l(e,`
    CREATE TRIGGER IF NOT EXISTS brain_decisions_ad AFTER DELETE ON brain_decisions BEGIN
      INSERT INTO brain_decisions_fts(brain_decisions_fts, rowid, id, decision, rationale)
      VALUES('delete', old.rowid, old.id, old.decision, old.rationale);
    END
  `),l(e,`
    CREATE TRIGGER IF NOT EXISTS brain_decisions_au AFTER UPDATE ON brain_decisions BEGIN
      INSERT INTO brain_decisions_fts(brain_decisions_fts, rowid, id, decision, rationale)
      VALUES('delete', old.rowid, old.id, old.decision, old.rationale);
      INSERT INTO brain_decisions_fts(rowid, id, decision, rationale)
      VALUES (new.rowid, new.id, new.decision, new.rationale);
    END
  `),l(e,`
    CREATE TRIGGER IF NOT EXISTS brain_patterns_ai AFTER INSERT ON brain_patterns BEGIN
      INSERT INTO brain_patterns_fts(rowid, id, pattern, context)
      VALUES (new.rowid, new.id, new.pattern, new.context);
    END
  `),l(e,`
    CREATE TRIGGER IF NOT EXISTS brain_patterns_ad AFTER DELETE ON brain_patterns BEGIN
      INSERT INTO brain_patterns_fts(brain_patterns_fts, rowid, id, pattern, context)
      VALUES('delete', old.rowid, old.id, old.pattern, old.context);
    END
  `),l(e,`
    CREATE TRIGGER IF NOT EXISTS brain_patterns_au AFTER UPDATE ON brain_patterns BEGIN
      INSERT INTO brain_patterns_fts(brain_patterns_fts, rowid, id, pattern, context)
      VALUES('delete', old.rowid, old.id, old.pattern, old.context);
      INSERT INTO brain_patterns_fts(rowid, id, pattern, context)
      VALUES (new.rowid, new.id, new.pattern, new.context);
    END
  `),l(e,`
    CREATE TRIGGER IF NOT EXISTS brain_learnings_ai AFTER INSERT ON brain_learnings BEGIN
      INSERT INTO brain_learnings_fts(rowid, id, insight, source)
      VALUES (new.rowid, new.id, new.insight, new.source);
    END
  `),l(e,`
    CREATE TRIGGER IF NOT EXISTS brain_learnings_ad AFTER DELETE ON brain_learnings BEGIN
      INSERT INTO brain_learnings_fts(brain_learnings_fts, rowid, id, insight, source)
      VALUES('delete', old.rowid, old.id, old.insight, old.source);
    END
  `),l(e,`
    CREATE TRIGGER IF NOT EXISTS brain_learnings_au AFTER UPDATE ON brain_learnings BEGIN
      INSERT INTO brain_learnings_fts(brain_learnings_fts, rowid, id, insight, source)
      VALUES('delete', old.rowid, old.id, old.insight, old.source);
      INSERT INTO brain_learnings_fts(rowid, id, insight, source)
      VALUES (new.rowid, new.id, new.insight, new.source);
    END
  `),l(e,`
    CREATE VIRTUAL TABLE IF NOT EXISTS brain_observations_fts
    USING fts5(id, title, narrative, content=brain_observations, content_rowid=rowid)
  `),l(e,`
    CREATE TRIGGER IF NOT EXISTS brain_observations_ai AFTER INSERT ON brain_observations BEGIN
      INSERT INTO brain_observations_fts(rowid, id, title, narrative)
      VALUES (new.rowid, new.id, new.title, new.narrative);
    END
  `),l(e,`
    CREATE TRIGGER IF NOT EXISTS brain_observations_ad AFTER DELETE ON brain_observations BEGIN
      INSERT INTO brain_observations_fts(brain_observations_fts, rowid, id, title, narrative)
      VALUES('delete', old.rowid, old.id, old.title, old.narrative);
    END
  `),l(e,`
    CREATE TRIGGER IF NOT EXISTS brain_observations_au AFTER UPDATE ON brain_observations BEGIN
      INSERT INTO brain_observations_fts(brain_observations_fts, rowid, id, title, narrative)
      VALUES('delete', old.rowid, old.id, old.title, old.narrative);
      INSERT INTO brain_observations_fts(rowid, id, title, narrative)
      VALUES (new.rowid, new.id, new.title, new.narrative);
    END
  `),!0):!1}function d(e){if(c(e)){e.prepare(`INSERT INTO brain_decisions_fts(brain_decisions_fts) VALUES('rebuild')`).run(),e.prepare(`INSERT INTO brain_patterns_fts(brain_patterns_fts) VALUES('rebuild')`).run(),e.prepare(`INSERT INTO brain_learnings_fts(brain_learnings_fts) VALUES('rebuild')`).run();try{e.prepare(`INSERT INTO brain_observations_fts(brain_observations_fts) VALUES('rebuild')`).run()}catch{}}}async function f(e,n,r){if(!n?.trim())return{decisions:[],patterns:[],learnings:[],observations:[]};let{getBrainDb:i,getBrainNativeDb:a}=await t(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{decisions:[],patterns:[],learnings:[],observations:[]};let c=r?.limit??10,l=r?.tables??[`decisions`,`patterns`,`learnings`,`observations`],f=r?.peerId,p=r?.includeGlobal??!0;return u(o)?(s||(s=!0,d(o)),m(o,n,l,c,f,p)):h(o,n,l,c,f,p)}function p(e,t,n){return t?n?{clause:` AND (${e}.peer_id = ? OR ${e}.peer_id = 'global')`,params:[t]}:{clause:` AND ${e}.peer_id = ?`,params:[t]}:{clause:``,params:[]}}function m(e,t,n,a,o,s=!0){let c={decisions:[],patterns:[],learnings:[],observations:[]},l=b(t);if(n.includes(`decisions`)){let{clause:n,params:u}=p(`d`,o,s);try{c.decisions=r(e.prepare(`
        SELECT d.*
        FROM brain_decisions_fts fts
        JOIN brain_decisions d ON d.rowid = fts.rowid
        WHERE brain_decisions_fts MATCH ?
          AND (d.quality_score IS NULL OR d.quality_score >= ?)
          ${n}
        ORDER BY bm25(brain_decisions_fts)
        LIMIT ?
      `),l,i,...u,a)}catch{c.decisions=g(e,t,a,o,s)}}if(n.includes(`patterns`)){let{clause:n,params:u}=p(`p`,o,s);try{c.patterns=r(e.prepare(`
        SELECT p.*
        FROM brain_patterns_fts fts
        JOIN brain_patterns p ON p.rowid = fts.rowid
        WHERE brain_patterns_fts MATCH ?
          AND (p.quality_score IS NULL OR p.quality_score >= ?)
          ${n}
        ORDER BY bm25(brain_patterns_fts)
        LIMIT ?
      `),l,i,...u,a)}catch{c.patterns=_(e,t,a,o,s)}}if(n.includes(`learnings`)){let{clause:n,params:u}=p(`l`,o,s);try{c.learnings=r(e.prepare(`
        SELECT l.*
        FROM brain_learnings_fts fts
        JOIN brain_learnings l ON l.rowid = fts.rowid
        WHERE brain_learnings_fts MATCH ?
          AND (l.quality_score IS NULL OR l.quality_score >= ?)
          ${n}
        ORDER BY bm25(brain_learnings_fts)
        LIMIT ?
      `),l,i,...u,a)}catch{c.learnings=v(e,t,a,o,s)}}if(n.includes(`observations`)){let{clause:n,params:u}=p(`o`,o,s);try{c.observations=r(e.prepare(`
        SELECT o.*
        FROM brain_observations_fts fts
        JOIN brain_observations o ON o.rowid = fts.rowid
        WHERE brain_observations_fts MATCH ?
          AND (o.quality_score IS NULL OR o.quality_score >= ?)
          ${n}
        ORDER BY bm25(brain_observations_fts)
        LIMIT ?
      `),l,i,...u,a)}catch{c.observations=y(e,t,a,o,s)}}return c}function h(e,t,n,r,i,a=!0){let o={decisions:[],patterns:[],learnings:[],observations:[]};return n.includes(`decisions`)&&(o.decisions=g(e,t,r,i,a)),n.includes(`patterns`)&&(o.patterns=_(e,t,r,i,a)),n.includes(`learnings`)&&(o.learnings=v(e,t,r,i,a)),n.includes(`observations`)&&(o.observations=y(e,t,r,i,a)),o}function g(e,t,n,a,o=!0){let s=`%${t}%`,{clause:c,params:l}=p(`brain_decisions`,a,o);return r(e.prepare(`
    SELECT * FROM brain_decisions
    WHERE (decision LIKE ? OR rationale LIKE ?)
      AND (quality_score IS NULL OR quality_score >= ?)
      ${c}
    ORDER BY created_at DESC
    LIMIT ?
  `),s,s,i,...l,n)}function _(e,t,n,a,o=!0){let s=`%${t}%`,{clause:c,params:l}=p(`brain_patterns`,a,o);return r(e.prepare(`
    SELECT * FROM brain_patterns
    WHERE (pattern LIKE ? OR context LIKE ?)
      AND (quality_score IS NULL OR quality_score >= ?)
      ${c}
    ORDER BY frequency DESC
    LIMIT ?
  `),s,s,i,...l,n)}function v(e,t,n,a,o=!0){let s=`%${t}%`,{clause:c,params:l}=p(`brain_learnings`,a,o);return r(e.prepare(`
    SELECT * FROM brain_learnings
    WHERE (insight LIKE ? OR source LIKE ?)
      AND (quality_score IS NULL OR quality_score >= ?)
      ${c}
    ORDER BY confidence DESC
    LIMIT ?
  `),s,s,i,...l,n)}function y(e,t,n,a,o=!0){let s=`%${t}%`,{clause:c,params:l}=p(`brain_observations`,a,o);return r(e.prepare(`
    SELECT * FROM brain_observations
    WHERE (title LIKE ? OR narrative LIKE ?)
      AND (quality_score IS NULL OR quality_score >= ?)
      ${c}
    ORDER BY created_at DESC
    LIMIT ?
  `),s,s,i,...l,n)}function b(e){let t=e.trim().split(/\s+/).filter(Boolean);if(t.length===0)return`""`;let n=new Set,r=[];for(let e of t){let t=e.replace(/^\W+|\W+$/g,``);if(t.length<2||!/\w/.test(t))continue;let i=t.toLowerCase();n.has(i)||(n.add(i),r.push(`"${t.replace(/"/g,`""`)}"`))}return r.length===0?`""`:r.join(` OR `)}function x(e,t=60){let n=new Map;for(let{source:r,hits:i}of e)for(let e=0;e<i.length;e++){let a=i[e],o=1/(t+e),s=n.get(a.id);s?(s.rrfScore+=o,s.sources.add(r),r===`fts`&&(s.ftsRank=e),r===`vec`&&(s.vecRank=e)):n.set(a.id,{rrfScore:o,type:a.type,title:a.title,text:a.text,sources:new Set([r]),ftsRank:r===`fts`?e:void 0,vecRank:r===`vec`?e:void 0})}return[...n.entries()].map(([e,t])=>({id:e,rrfScore:t.rrfScore,type:t.type,title:t.title,text:t.text,sources:[...t.sources],ftsRank:t.ftsRank,vecRank:t.vecRank})).sort((e,t)=>t.rrfScore-e.rrfScore)}async function S(e,r,i){if(!e?.trim())return[];let o=i?.limit??10,s=i?.rrfK??60,c=i?.includeCode??!1,l=[f(r,e,{limit:o*3}).catch(()=>({decisions:[],patterns:[],learnings:[],observations:[]})),a(e,r,o*3).catch(()=>[])],u=null;c&&(u=(async()=>{try{let{smartSearch:n}=await t(async()=>{let{smartSearch:e}=await import(`./bXAQrK7j.js`);return{smartSearch:e}},__vite__mapDeps([21,2,12,13,14,5]),import.meta.url);return n(e,{maxResults:o*2,rootDir:r}).map(e=>({id:`code:${e.symbol.filePath}:${e.symbol.name}:${e.symbol.startLine}`,title:e.symbol.name,kind:e.symbol.kind,score:e.score}))}catch{return[]}})(),l.push(u));let d=await Promise.all(l),p=d[0],m=d[1],h=c?d[2]??[]:[],g=[];for(let e of p.decisions)g.push({id:e.id,type:`decision`,title:e.decision,text:`${e.decision} — ${e.rationale}`});for(let e of p.patterns)g.push({id:e.id,type:`pattern`,title:e.pattern,text:`${e.pattern} — ${e.context}`});for(let e of p.learnings)g.push({id:e.id,type:`learning`,title:e.insight,text:`${e.insight} (source: ${e.source})`});for(let e of p.observations)g.push({id:e.id,type:`observation`,title:e.title,text:e.narrative??e.title});let _=m.map(e=>({id:e.id,type:e.type,title:e.title,text:e.text})),v=h.map(e=>({id:e.id,type:`code-symbol`,title:e.title,text:`${e.title} (${e.kind})`})),y=[];g.length>0&&y.push({source:`fts`,hits:g}),_.length>0&&y.push({source:`vec`,hits:_}),v.length>0&&y.push({source:`code`,hits:v});try{let t=await n(r),i=[`concept:${e.toLowerCase().replace(/\s+/g,`-`)}`,`task:${e}`,`doc:${e}`],a=[];for(let e of i){if(!await t.getPageNode(e))continue;let n=await t.getNeighbors(e);for(let e of n)a.push({id:e.id,type:e.nodeType,title:e.label,text:e.label})}a.length>0&&y.push({source:`graph`,hits:a})}catch{}return x(y,s).slice(0,o).map(e=>({id:e.id,score:e.rrfScore,type:e.type,title:e.title,text:e.text,sources:e.sources,ftsRank:e.ftsRank,vecRank:e.vecRank}))}export{f as a,x as i,S as n,d as r,u as t};