const __vite__mapDeps=(i,m=__vite__mapDeps,d=(m.f||(m.f=["./DfVstSkc.js","./BSkUUZJr2.js","./DrKWWDpL.js","./kwfd020K.js","./DIQmuJJZ.js","./D7w7HllH.js","./DB5ifLM-.js","./DYP3EJMY.js","./CxlOpib82.js","./BDDiPmhF.js","./BYJC6_MM.js","./CX8DOZuw2.js","./DsQIgu-4.js","./DOqzs8kl2.js","./Dm1HkpJd2.js","./Ouh6Dj2I2.js","./DE1jbOEi.js","./DVQI6VOy.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"./DTkg6Mnx.js";import{_ as a,d as o,g as s,h as c,i as l,m as u,n as d,u as f}from"./DeO5NWhN.js";import{M as p}from"./D-eE9s2f.js";var m=e(r(),1);t();var h=(0,m.createRequire)(import.meta.url),g=null;function _(){return g===null&&(g=h(`drizzle-orm/node-sqlite`).drizzle),g}var v=`1.0.0`,y=null,b=null,x=null,S=null,C=!1;function w(e){return l(`project`,e)}function T(){return f(`drizzle-brain`)}var E=[`brain_attention`,`brain_backfill_runs`,`brain_consolidation_events`,`brain_decisions`,`brain_deriver_queue`,`brain_embeddings`,`brain_learnings`,`brain_memory_links`,`brain_memory_trees`,`brain_modulators`,`brain_observations`,`brain_observations_staging`,`brain_page_edges`,`brain_page_nodes`,`brain_patterns`,`brain_plasticity_events`,`brain_promotion_log`,`brain_retrieval_log`,`brain_schema_meta`,`brain_session_narrative`,`brain_sticky_notes`,`brain_sticky_tags`,`brain_transcript_events`,`brain_usage_log`,`brain_weight_history`];function D(e){if(!a(e,`brain_attention`))return!1;let t=e.prepare(`PRAGMA table_info(brain_attention)`).all().find(e=>e.name===`created_at`);return t!==void 0&&t.type.toUpperCase()!==`INTEGER`}function O(e,t){let n=i(`brain-schema`);if(D(e)){let t=e.prepare(`PRAGMA foreign_keys`).get()?.foreign_keys===1;e.exec(`PRAGMA foreign_keys=OFF`);try{for(let t of E)try{e.exec(`DROP TABLE IF EXISTS \`${t}\``)}catch(e){n.warn({table:t,err:e},`Failed to drop consolidated brain table during legacy rebuild.`)}}finally{e.exec(`PRAGMA foreign_keys=${t?`ON`:`OFF`}`)}n.debug({count:E.length},`Dropped consolidated (exodus-target) brain tables — rebuilding in legacy runtime shape.`)}let r=T();if(a(e,`brain_decisions`)&&!D(e)){let{marked:t,applied:i}=c(e,r);n.debug({marked:t,applied:i},`brain consolidated cleo.db reconcile (T11647) — marked already-present migrations applied + executed the missing unprefixed-table migrations directly.`);return}a(e,`brain_decisions`)&&x&&o(x),s(e,r,`brain_decisions`,`brain`),u(t,r,e,`brain_decisions`,`brain`)}function k(e){try{return h(`sqlite-vec`).load(e),!0}catch{return!1}}function A(e){e.prepare(`CREATE VIRTUAL TABLE IF NOT EXISTS brain_embeddings USING vec0(id TEXT PRIMARY KEY, embedding FLOAT[384])`).run()}function j(){return C}async function M(e){try{let{loadConfig:t}=await n(async()=>{let{loadConfig:e}=await import(`./DfVstSkc.js`);return{loadConfig:e}},__vite__mapDeps([0,1,2,3,4,5,6,7,8,9,10,11,12,13]),import.meta.url);if((await t(e)).brain?.embedding?.enabled){let{initDefaultProvider:e}=await n(async()=>{let{initDefaultProvider:e}=await import(`./Dm1HkpJd2.js`);return{initDefaultProvider:e}},__vite__mapDeps([14,15,12,2]),import.meta.url);await e()}}catch{}}async function N(e){let t=w(e),{assertTestEnv:r}=await n(async()=>{let{assertTestEnv:e}=await import(`./DE1jbOEi.js`);return{assertTestEnv:e}},__vite__mapDeps([16,17,12,2]),import.meta.url);if(r(t),y&&x!==t&&F(),y&&(b===null||!b.isOpen)&&F(),y)return y;if(S)return S;S=(async()=>{let n=(await d(`project`,e)).db.$client??null;if(!n)throw Error(`E6-L2: openDualScopeDb returned a handle without $client — cannot extract DatabaseSync for legacy brain-schema wrapping.`);b=n,x=t,C=k(n);let r=_()({client:n,schema:p});return O(n,r),C&&A(n),n.prepare(`INSERT OR IGNORE INTO brain_schema_meta (key, value) VALUES ('schemaVersion', '${v}')`).run(),y=r,C&&setImmediate(()=>{M(e).catch(()=>{})}),r})();try{return await S}finally{S=null}}function P(){b=null,y=null,x=null,S=null,C=!1}function F(){b=null,y=null,x=null,S=null,C=!1}function I(){return b}export{I as a,T as c,w as i,P as n,j as o,N as r,F as s,v as t};