const __vite__mapDeps=(i,m=__vite__mapDeps,d=(m.f||(m.f=["./vDOh_fWF.js","./DFB29SWc.js","./DrKWWDpL.js","./DsQIgu-4.js","./Ouh6Dj2I2.js","./C0bVU7gr2.js","./Dn7LRnjg2.js","./DsanjglN.js","./CbGRcr-D.js","./bG-fsHMS.js","./DTkg6Mnx.js","./DYP3EJMY.js","./DeO5NWhN.js","./DgZn9uJn.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","./CvFbqtf9.js","./Dg_1c8FK2.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{n as i}from"./Dn7LRnjg2.js";import{r as a}from"./Dg_1c8FK2.js";import{a as o}from"./-TjUTcZv2.js";import{n as s,t as c}from"./2yUiEINB.js";var l=e(r(),1);t();function u(){return`P-${(0,l.randomBytes)(4).toString(`hex`)}`}async function d(e,t){if(!t.pattern?.trim())throw Error(`Pattern description is required`);if(!t.context?.trim())throw Error(`Pattern context is required`);if(!t._skipGate){let{verifyCandidate:r}=await n(async()=>{let{verifyCandidate:e}=await import(`./vDOh_fWF.js`);return{verifyCandidate:e}},__vite__mapDeps([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27]),import.meta.url),a=t.source?.startsWith(`auto`)??!1?`speculative`:`agent`,o=await r(e,{text:t.pattern.trim(),title:t.context.trim().slice(0,120),memoryType:`procedural`,tier:`medium`,confidence:.6,source:`transcript`,sourceConfidence:a});if(o.action!==`stored`){let n=o.id?await(await i(e)).getPattern(o.id).catch(()=>null):null;if(n)return{...n,examples:JSON.parse(n.examplesJson||`[]`)};let r=new Date().toISOString().replace(`T`,` `).slice(0,19);return{id:o.id??``,type:t.type,pattern:t.pattern.trim(),context:t.context.trim(),frequency:1,successRate:t.successRate??null,impact:t.impact??null,antiPattern:t.antiPattern??null,mitigation:t.mitigation??null,examplesJson:`[]`,examples:[],extractedAt:r,qualityScore:0,memoryTier:`medium`,memoryType:`procedural`,sourceConfidence:a,verified:!1,contentHash:``,tierPromotedAt:null,tierPromotionReason:null,invalidAt:null,pruneCandidateAt:null,citationCount:0,pruneCandidate:!1}}}let r=await i(e),d=await r.findPatterns({type:t.type}),f=t.pattern.trim().toLowerCase(),p=d.find(e=>e.pattern.trim().toLowerCase()===f),m=new Date().toISOString().replace(`T`,` `).slice(0,19);if(p){let n=JSON.parse(p.examplesJson||`[]`),i=t.examples??[],o=Array.from(new Set([...n,...i]));await r.updatePattern(p.id,{frequency:p.frequency+1,extractedAt:m,examplesJson:JSON.stringify(o)});let s=await r.getPattern(p.id);return a(e,`pattern:${p.id}`,`pattern`,p.pattern.substring(0,200),p.qualityScore??.5,p.pattern+p.context,{type:p.type,impact:p.impact??void 0,frequency:p.frequency+1}).catch(()=>{}),{...s,examples:o}}let h=`medium`,g=t.source?.startsWith(`auto`)?`speculative`:`agent`,_=t.examples?JSON.stringify(t.examples):`[]`,v=o({type:t.type,pattern:t.pattern.trim(),context:t.context.trim(),examples_json:_,sourceConfidence:g,memoryTier:h}),y=(0,l.createHash)(`sha256`).update(t.pattern.trim().toLowerCase()).digest(`hex`).slice(0,16),b={id:u(),type:t.type,pattern:t.pattern.trim(),context:t.context.trim(),frequency:1,successRate:t.successRate??null,impact:t.impact??null,antiPattern:t.antiPattern??null,mitigation:t.mitigation??null,examplesJson:_,extractedAt:m,qualityScore:v,memoryTier:h,memoryType:`procedural`,sourceConfidence:g,verified:!1,contentHash:y},x=await r.addPattern(b);return a(e,`pattern:${x.id}`,`pattern`,x.pattern.substring(0,200),v,x.pattern+x.context,{type:x.type,impact:x.impact??void 0}).catch(()=>{}),(g===`owner`||g===`task-outcome`)&&c(e,{id:x.id,text:x.pattern+` `+x.context,createdAt:x.extractedAt??new Date().toISOString().replace(`T`,` `).slice(0,19)}).then(t=>{for(let n of t)s(e,n.existingId,x.id,`auto:pattern-supersedes — high overlap detected at store time`).catch(()=>{})}).catch(()=>{}),{...x,examples:JSON.parse(x.examplesJson||`[]`)}}async function f(e,t={}){let n=await(await i(e)).findPatterns({type:t.type,impact:t.impact,minFrequency:t.minFrequency,limit:t.limit});if(t.query){let e=t.query.toLowerCase();n=n.filter(t=>t.pattern.toLowerCase().includes(e)||t.context.toLowerCase().includes(e)||t.antiPattern?.toLowerCase().includes(e)||t.mitigation?.toLowerCase().includes(e))}return n.map(e=>({...e,examples:JSON.parse(e.examplesJson||`[]`)}))}export{d as n,f as t};