@vibecontrols/agent 2026.824.1 → 2026.1124.1
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/agent-config-v9d7gcz0.js +2 -0
- package/dist/agent-ready-tracker-mewxehg9.js +2 -0
- package/dist/app-k7z7tjem.js +2 -0
- package/dist/autostart.cmd-ahet7b1t.js +2 -0
- package/dist/bootstrap-workspace-3pb8qjsh.js +2 -0
- package/dist/bootstrap.service-9m7m1ksa.js +2 -0
- package/dist/bridge-client-xvqy3n15.js +2 -0
- package/dist/cli.js +2 -2
- package/dist/daemon-profile-sg8fp50z.js +2 -0
- package/dist/finalize-retry-handle-registry-z9mbbjmj.js +2 -0
- package/dist/finalize-retry-worker-az74f2qv.js +2 -0
- package/dist/gateway-client-ermpz52n.js +2 -0
- package/dist/getMachineId-bsd-4w1wmcjv.js +2 -0
- package/dist/getMachineId-darwin-kejwz8w5.js +3 -0
- package/dist/getMachineId-linux-mpm53wat.js +2 -0
- package/dist/getMachineId-unsupported-54seyw5v.js +2 -0
- package/dist/getMachineId-win-cva7ghz9.js +2 -0
- package/dist/highlights-3nz7pbkh.js +2 -0
- package/dist/highlights-6dgztt5d.js +2 -0
- package/dist/highlights-jq0g1547.js +2 -0
- package/dist/highlights-yc7cwzcr.js +2 -0
- package/dist/highlights-z2sw1kpq.js +2 -0
- package/dist/index-01fp85ft.js +171 -0
- package/dist/index-06bmw4xf.js +83 -0
- package/dist/index-0gmhd3hs.js +4 -0
- package/dist/index-0qjbnyg4.js +21 -0
- package/dist/index-2r6cmxxz.js +9 -0
- package/dist/index-2vcxt38h.js +3 -0
- package/dist/index-3b5xejjs.js +5 -0
- package/dist/index-3cbesatx.js +178 -0
- package/dist/index-4q8y6k1e.js +26 -0
- package/dist/index-4z1y39jt.js +3 -0
- package/dist/index-5021rk8n.js +3 -0
- package/dist/index-652f9scs.js +4 -0
- package/dist/index-76m37yd8.js +22 -0
- package/dist/index-7f2k4g6q.js +8 -0
- package/dist/index-7gk1x74m.js +15 -0
- package/dist/index-7wben1jx.js +3 -0
- package/dist/index-8rbhm4g8.js +7 -0
- package/dist/index-9bj65n3s.js +13 -0
- package/dist/index-9xrhb68w.js +14 -0
- package/dist/index-a0vx2zyx.js +3 -0
- package/dist/index-aav1drxa.js +158 -0
- package/dist/index-akk3rxnn.js +4 -0
- package/dist/index-b3v08awy.js +3 -0
- package/dist/index-byerrz46.js +3 -0
- package/dist/index-c4whjk5r.js +3 -0
- package/dist/index-cy97asyh.js +133 -0
- package/dist/index-d59kp6zr.js +3 -0
- package/dist/index-haww9jpq.js +27 -0
- package/dist/index-hsrt2v87.js +2 -0
- package/dist/index-jmx5awkj.js +3 -0
- package/dist/index-jnxjjx2y.js +3 -0
- package/dist/index-kwhx023z.js +11 -0
- package/dist/index-m942hydk.js +116 -0
- package/dist/index-mmx2kp76.js +16 -0
- package/dist/index-p0bbp16g.js +11 -0
- package/dist/index-qe3xpt41.js +3 -0
- package/dist/index-qfxwk38p.js +82 -0
- package/dist/index-ss5gez2p.js +2 -0
- package/dist/index-t5cfkz1e.js +4 -0
- package/dist/index-t6p4szqx.js +5 -0
- package/dist/index-tahaxp6n.js +3 -0
- package/dist/index-v16qz1fa.js +3 -0
- package/dist/index-vtsd74g0.js +9 -0
- package/dist/index-w0m3jm0s.js +408 -0
- package/dist/index-w4jqdhc2.js +5 -0
- package/dist/index-wr888ydc.js +17 -0
- package/dist/index-xtd0j8ve.js +3 -0
- package/dist/index-ywnfz6nm.js +3 -0
- package/dist/index.js +3 -3
- package/dist/injections-h25c1fmk.js +2 -0
- package/dist/interactive-vbz68zh5.js +2 -0
- package/dist/key.cmd-f6rjvjy8.js +2 -0
- package/dist/log-shipper-0erbwn0q.js +2 -0
- package/dist/path-utils-r4df8d3w.js +2 -0
- package/dist/plugin-system-kvg955t3.js +2 -0
- package/dist/prereqs-runner-gca0bytz.js +2 -0
- package/dist/profile-mount-s4mc1x18.js +2 -0
- package/dist/prune-stale-shims-4nqtv0xt.js +2 -0
- package/dist/register-core-31wm1d8m.js +2 -0
- package/dist/secondary-profile-attach-95kmnrhf.js +2 -0
- package/dist/subprocess-4gg77skg.js +2 -0
- package/dist/telemetry-nmtywcba.js +2 -0
- package/dist/tree-sitter-javascript-rfadfcwb.js +2 -0
- package/dist/tree-sitter-markdown-gkyb2qez.js +2 -0
- package/dist/tree-sitter-markdown_inline-xzttqkvb.js +2 -0
- package/dist/tree-sitter-typescript-4z897028.js +2 -0
- package/dist/tree-sitter-zig-y6n7djp6.js +2 -0
- package/dist/tunnel-bootstrap-rym0b0x5.js +2 -0
- package/package.json +10 -9
- package/dist/agent-config-5b0avmy6.js +0 -2
- package/dist/agent-ready-tracker-ez6r9djg.js +0 -2
- package/dist/app-hkt10ta3.js +0 -2
- package/dist/autostart.cmd-fdcdqrr2.js +0 -2
- package/dist/bootstrap-workspace-t5fyq07m.js +0 -2
- package/dist/bootstrap.service-nxhkrsf1.js +0 -2
- package/dist/bridge-client-bgkr7bcn.js +0 -2
- package/dist/daemon-profile-j2wt35x8.js +0 -2
- package/dist/esm-n9r8p5n2.js +0 -2
- package/dist/finalize-retry-handle-registry-fvm4gm8s.js +0 -2
- package/dist/finalize-retry-worker-wb8wj1yw.js +0 -2
- package/dist/gateway-client-6dqagbg4.js +0 -2
- package/dist/getMachineId-bsd-fgatzdd7.js +0 -2
- package/dist/getMachineId-darwin-gt8j2jw2.js +0 -3
- package/dist/getMachineId-linux-bcz4rr1x.js +0 -2
- package/dist/getMachineId-unsupported-079qwwb4.js +0 -2
- package/dist/getMachineId-win-2hk4e8bg.js +0 -2
- package/dist/highlights-0gfz787m.js +0 -2
- package/dist/highlights-86sbx76f.js +0 -2
- package/dist/highlights-n55f5jnk.js +0 -2
- package/dist/highlights-tbmszdr7.js +0 -2
- package/dist/highlights-wm2cs773.js +0 -2
- package/dist/index-06es0fmw.js +0 -10
- package/dist/index-0d4wfjbt.js +0 -171
- package/dist/index-29fmptfn.js +0 -13
- package/dist/index-2q2jadag.js +0 -11
- package/dist/index-2sn36kaj.js +0 -7
- package/dist/index-3p4n7nwg.js +0 -83
- package/dist/index-3tf12pw3.js +0 -3
- package/dist/index-4gsn3d9r.js +0 -26
- package/dist/index-54mmh1eb.js +0 -4
- package/dist/index-6jzsthh9.js +0 -3
- package/dist/index-7wejfjj9.js +0 -5
- package/dist/index-8q19ryt2.js +0 -28
- package/dist/index-8xh8m2z7.js +0 -8
- package/dist/index-a8n6s2ae.js +0 -2
- package/dist/index-b16y8wyz.js +0 -15
- package/dist/index-bv1z3fxw.js +0 -3
- package/dist/index-cs78wq6y.js +0 -3
- package/dist/index-df19rrbe.js +0 -5
- package/dist/index-dx9hjdp4.js +0 -178
- package/dist/index-ea42kfvk.js +0 -5
- package/dist/index-ekr40gvh.js +0 -3
- package/dist/index-evnbrmdf.js +0 -4
- package/dist/index-ggxs7nwz.js +0 -3
- package/dist/index-gz5t0vvc.js +0 -4
- package/dist/index-hey6w5m0.js +0 -3
- package/dist/index-hp8jrj6j.js +0 -3
- package/dist/index-hrfkzecw.js +0 -3
- package/dist/index-hwpmdq5z.js +0 -4
- package/dist/index-hz5cy6vz.js +0 -11
- package/dist/index-jbxh0ffn.js +0 -407
- package/dist/index-js2r0men.js +0 -9
- package/dist/index-k3jkzj3k.js +0 -9
- package/dist/index-kteawn2b.js +0 -3
- package/dist/index-mbhq916m.js +0 -3
- package/dist/index-mxc61yr1.js +0 -3
- package/dist/index-pnk33bjw.js +0 -116
- package/dist/index-qdbxvcwr.js +0 -17
- package/dist/index-qqq9qyy6.js +0 -21
- package/dist/index-rmh8pff5.js +0 -3
- package/dist/index-sspydg85.js +0 -15
- package/dist/index-tk81f249.js +0 -3
- package/dist/index-vjdg5qb2.js +0 -2
- package/dist/index-vkrf8kzp.js +0 -158
- package/dist/index-vnxqna70.js +0 -16
- package/dist/index-w2j4ttsx.js +0 -130
- package/dist/index-w7gctzhm.js +0 -83
- package/dist/index-wj5hx7gq.js +0 -4
- package/dist/index-x4eaagvr.js +0 -3
- package/dist/index-xjr60th0.js +0 -3
- package/dist/index-xtbppqnv.js +0 -3
- package/dist/index-zr9rmjyp.js +0 -3
- package/dist/injections-2g0zh0bq.js +0 -2
- package/dist/interactive-08vyaapn.js +0 -2
- package/dist/key.cmd-642xcxb4.js +0 -2
- package/dist/log-shipper-9xfnjwr6.js +0 -2
- package/dist/path-utils-g449hqvm.js +0 -2
- package/dist/plugin-system-gpzcch00.js +0 -2
- package/dist/prereqs-runner-re9f51fp.js +0 -2
- package/dist/profile-mount-txvgkaz8.js +0 -2
- package/dist/prune-stale-shims-vs6a07tb.js +0 -2
- package/dist/register-core-e0ha52yw.js +0 -2
- package/dist/secondary-profile-attach-m9x9e7py.js +0 -2
- package/dist/subprocess-zcgpccp1.js +0 -2
- package/dist/telemetry-k6em3vy2.js +0 -2
- package/dist/tree-sitter-javascript-qh90w7td.js +0 -2
- package/dist/tree-sitter-markdown-favzteb6.js +0 -2
- package/dist/tree-sitter-markdown_inline-x2726t9j.js +0 -2
- package/dist/tree-sitter-typescript-m7ep8z1h.js +0 -2
- package/dist/tree-sitter-zig-3vsxg5wh.js +0 -2
- package/dist/tunnel-bootstrap-y8dczezc.js +0 -2
package/dist/index-dx9hjdp4.js
DELETED
|
@@ -1,178 +0,0 @@
|
|
|
1
|
-
// @bun
|
|
2
|
-
import{Aa as Fa,Da as za}from"./index-w7gctzhm.js";import{Ea as Ua}from"./index-hey6w5m0.js";import{Ha as Pa,_a as Ba}from"./index-7wejfjj9.js";import{$a as ja}from"./index-ea42kfvk.js";import{rb as Ka}from"./index-3p4n7nwg.js";import{Nb as qa}from"./index-vnxqna70.js";import{vd as ba,xd as wt}from"./index-2q2jadag.js";import{Nd as Zt,Pd as Ra,Qd as ka,Sd as La,Wd as x,_d as Ta,ae as g,be as Oa}from"./index-qqq9qyy6.js";import{le as Vt,qe as Ia}from"./index-gz5t0vvc.js";import{we as Ae}from"./index-xjr60th0.js";var Gt=[0,5000,15000,30000,60000],Xa=2000,Ha=5000,Va=120000,cs=10,Ja=1048576;class ei extends Error{resetAt;resource;constructor(e,t,i){super(e);this.name="RateLimitError",this.resource=t,this.resetAt=i}}function ti(e,t){if(!e?.length)return null;let i=e.find((r)=>r.extensions?.code==="RATE_LIMITED");if(!i)return null;let s=i.extensions?.resetAt,n=null;if(s){let r=new Date(s);if(!Number.isNaN(r.getTime()))n=r}return new ei(`${t} rate limited${n?` until ${n.toISOString()}`:""}`,t,n)}function Ya(e){if(!e.resetAt)return 60000;let t=e.resetAt.getTime()-Date.now()+Xa;return Math.min(Math.max(t,Ha),Va)}async function Wa(e,t){let i=e.headers.get("content-length");if(i){let o=Number.parseInt(i,10);if(Number.isInteger(o)&&o>t)throw Error(`Gateway response is too large (${o} bytes)`)}if(!e.body)return"";let s=e.body.getReader(),n=[],r=0;try{while(!0){let{done:o,value:c}=await s.read();if(o)break;if(!c)continue;if(r+=c.byteLength,r>t)throw await s.cancel().catch(()=>{return}),Error(`Gateway response exceeds ${t} bytes`);n.push(c)}}finally{s.releaseLock()}let a=new Uint8Array(r),l=0;for(let o of n)a.set(o,l),l+=o.byteLength;return new TextDecoder().decode(a)}async function ii(e,t){let i=await Wa(e,Ja),s;try{s=i?JSON.parse(i):{}}catch(n){let r=i.slice(0,240).replace(/\s+/g," ");throw Error(`${t} returned non-JSON response (${e.status} ${e.statusText})${r?`: ${r}`:""}`,{cause:n})}if(!e.ok){let n=i.slice(0,240).replace(/\s+/g," ");throw Error(`${t} failed (${e.status} ${e.statusText})${n?`: ${n}`:""}`)}return s}function si(e){return e.endsWith("/graphql")?e:`${e}/graphql`}function Qa(e){let t;try{t=new URL(e).hostname.toLowerCase().replace(/^\[|\]$/g,"")}catch{return!1}if(t==="localhost"||t==="127.0.0.1"||t==="::1"||t==="0.0.0.0"||t==="host.docker.internal")return!0;return t.endsWith(".localhost")||t.endsWith(".local")||t.endsWith(".local.burdenoff.com")}function ni(e){if(!(process.env.VIBE_DEV_INSECURE_TLS==="1"||process.env.NODE_TLS_REJECT_UNAUTHORIZED==="0")&&!Qa(e))return{};return{tls:{rejectUnauthorized:!1}}}async function Ga(e,t,i){let s=await fetch(si(e),{method:"POST",...ni(e),headers:{"Content-Type":"application/json"},body:JSON.stringify({query:`mutation AuthenticateApp($input: AuthenticateAppInput!) {
|
|
3
|
-
authenticateApp(input: $input) { accessToken expiresIn }
|
|
4
|
-
}`,variables:{input:{clientId:t,clientSecret:i,scopes:[]}}}),signal:AbortSignal.timeout(15000)}),n=await ii(s,"authenticateApp"),r=ti(n.errors,"authenticateApp");if(r)throw r;if(n.errors?.length){let l=n.errors.map((o)=>o.extensions?.code??"UNKNOWN").join(",");throw Error(`authenticateApp errors [${l}]: ${n.errors.map((o)=>o.message).join("; ")}`)}let a=n.data?.authenticateApp?.accessToken;if(!a)throw Error("authenticateApp returned no token");return a}async function Za(e,t,i,s){let n=await fetch(si(e),{method:"POST",...ni(e),headers:{"Content-Type":"application/json",Authorization:`Bearer ${t}`},body:JSON.stringify({query:`mutation IssueAgentWorkspaceToken($input: IssueWorkspaceTokenInput!) {
|
|
5
|
-
issueWorkspaceToken(input: $input) {
|
|
6
|
-
success
|
|
7
|
-
token
|
|
8
|
-
expiresAt
|
|
9
|
-
error { code message }
|
|
10
|
-
}
|
|
11
|
-
}`,variables:{input:{organizationId:s||i,workspaceId:i}}}),signal:AbortSignal.timeout(15000)}),r=await ii(n,"issueWorkspaceToken"),a=ti(r.errors,"issueWorkspaceToken");if(a)throw a;if(r.errors?.length)throw Error(`issueWorkspaceToken errors: ${r.errors.map((o)=>o.message).join("; ")}`);let l=r.data?.issueWorkspaceToken;if(!l?.success||!l.token){let o=l?.error?.code??"UNKNOWN",c=l?.error?.message??"no workspace token returned";throw Error(`issueWorkspaceToken failed [${o}]: ${c}`)}return l.token}async function eo(e){let t=await Ga(e.globalGatewayUrl,e.clientId,e.clientSecret),i=await Za(e.workspaceGatewayUrl,t,e.workspaceId,e.organizationId),s=await fetch(si(e.workspaceGatewayUrl),{method:"POST",...ni(e.workspaceGatewayUrl),headers:{"Content-Type":"application/json",Authorization:`Bearer ${t}`,"X-Workspace-Authorization":`Bearer ${i}`},body:JSON.stringify({query:`query FetchAgentEncryptionKey($workspaceId: ID!) {
|
|
12
|
-
vibecontrolsAgentEncryptionKey(workspaceId: $workspaceId)
|
|
13
|
-
}`,variables:{workspaceId:e.workspaceId}}),signal:AbortSignal.timeout(15000)}),n=await ii(s,"vibecontrolsAgentEncryptionKey"),r=ti(n.errors,"vibecontrolsAgentEncryptionKey");if(r)throw r;if(n.errors?.length)throw Error(`vibecontrolsAgentEncryptionKey errors: ${n.errors.map((l)=>l.message).join("; ")}`);let a=n.data?.vibecontrolsAgentEncryptionKey;if(!a)throw Error("vibecontrolsAgentEncryptionKey returned empty");return a}async function hs(e,t=Gt.length,i={}){let s={...e,globalGatewayUrl:Zt(e.globalGatewayUrl,"globalGatewayUrl"),workspaceGatewayUrl:Zt(e.workspaceGatewayUrl,"workspaceGatewayUrl")},n=i.signal,r=()=>{if(n?.aborted)throw n.reason instanceof Error?n.reason:Error("encryption key fetch aborted")},a=(h)=>new Promise((u,d)=>{let _=setTimeout(()=>{if(n)n.removeEventListener("abort",f);u()},h),f=()=>{clearTimeout(_),d(n?.reason instanceof Error?n.reason:Error("encryption key fetch aborted"))};n?.addEventListener("abort",f,{once:!0})}),l,o=0,c=0;while(o<t){r();try{let h=await eo(s);return g().logger.info("finalizer","Storage encryption key fetched from backend",{attempt:o+1,rateLimitRetries:c}),h}catch(h){if(l=h,r(),h instanceof ei){if(c>=cs){g().logger.error("finalizer","Rate-limit retries exhausted; giving up",{resource:h.resource,resetAt:h.resetAt?.toISOString()??null,rateLimitRetries:c});break}let _=Ya(h);c+=1,g().logger.warn("finalizer",`Rate limited by ${h.resource}; waiting ${Math.round(_/1000)}s for window to clear (retry ${c}/${cs})`,{resource:h.resource,resetAt:h.resetAt?.toISOString()??null,waitMs:_}),await a(_);continue}if(o+=1,o>=t){g().logger.error("finalizer","Failed to fetch storage encryption key from backend (final attempt)",{error:String(h),attempts:t});break}let d=Gt[o]??Gt.at(-1)??0;if(g().logger.warn("finalizer",`Storage key fetch attempt ${o}/${t} failed; retrying in ${Math.round(d/1000)}s`,{error:String(h)}),d>0)await a(d)}}throw l instanceof Error?l:Error(String(l??"encryption key fetch failed"))}import{existsSync as to,readFileSync as io}from"fs";import{dirname as ri,join as so}from"path";import{fileURLToPath as no}from"url";import{closeSync as ro,existsSync as ao,mkdirSync as oo,openSync as lo,readFileSync as co,unlinkSync as ds,writeFileSync as ho}from"fs";import{join as uo}from"path";import _o from"crypto";import ze from"path";import j from"fs";import ai from"zlib";import fo from"http";import{closeSync as po,existsSync as mo,mkdirSync as go,openSync as yo,readFileSync as wo,unlinkSync as us,writeFileSync as Ao}from"fs";import{join as xo}from"path";import vo from"crypto";import Be from"path";import q from"fs";import oi from"zlib";import Eo from"http";import{closeSync as So,existsSync as Io,mkdirSync as bo,openSync as To,readFileSync as Oo,unlinkSync as _s,writeFileSync as No}from"fs";import{join as $o}from"path";import Do from"crypto";import Xe from"path";import K from"fs";import li from"zlib";import Co from"http";import{closeSync as Ro,existsSync as ko,mkdirSync as Lo,openSync as Po,readFileSync as Mo,unlinkSync as fs,writeFileSync as jo}from"fs";import{join as qo}from"path";import Ko from"crypto";import He from"path";import F from"fs";import ci from"zlib";import Fo from"http";function Uo(){let e=Date.now().toString(16),t;try{t=_o.randomBytes(8).toString("hex")}catch{let i=new Uint8Array(8);crypto.getRandomValues(i),t=Array.from(i).map((s)=>s.toString(16).padStart(2,"0")).join("")}return`${e}${t}`}function ps(e,t){let i=e instanceof Error?e.message:e;if(t==="error")console.error(i);else console.log(i)}var ms=new Set(["__proto__","constructor","prototype"]);function jt(e,t){if(!t.includes(".")){if(ms.has(t))return;return e[t]}let i=e;for(let s of t.split(".")){if(ms.has(s))return;if(i==null)return;i=i[s]}return i}class Me extends Error{constructor(e,t,i={}){super(t);this.name=this.constructor.name,this.code=e,this.details=i}}class N extends Me{}class xe extends Me{}class me extends Me{}class Re extends Me{}class ue extends Me{}class Qe extends Me{}function yi(e,t){if(e===t)return!0;if(e==null||t==null)return!1;if(typeof e!==typeof t)return!1;if(e instanceof Date&&t instanceof Date)return e.getTime()===t.getTime();if(e instanceof RegExp&&t instanceof RegExp)return e.source===t.source&&e.flags===t.flags;if(Array.isArray(e)){if(!Array.isArray(t)||e.length!==t.length)return!1;for(let i=0;i<e.length;i++)if(!yi(e[i],t[i]))return!1;return!0}if(typeof e==="object"){if(Array.isArray(t))return!1;let i=Object.keys(e);if(i.length!==Object.keys(t).length)return!1;for(let s of i){if(!Object.prototype.hasOwnProperty.call(t,s))return!1;if(!yi(e[s],t[s]))return!1}return!0}return!1}function H(e,t){if(typeof t==="function")return t(e);if(t==null)return!0;if(typeof t==="object"&&Object.keys(t).length===0)return!0;if("$or"in t){let i=t.$or;if(!Array.isArray(i))throw new Qe("ERR_SKALEX_QUERY_INVALID_OPERATOR","$or must be an array of filters",{operator:"$or"});if(!i.some((s)=>H(e,s)))return!1}if("$and"in t){let i=t.$and;if(!Array.isArray(i))throw new Qe("ERR_SKALEX_QUERY_INVALID_OPERATOR","$and must be an array of filters",{operator:"$and"});if(!i.every((s)=>H(e,s)))return!1}if("$not"in t){if(H(e,t.$not))return!1}for(let i in t){if(i==="$or"||i==="$and"||i==="$not")continue;let s=t[i],n;try{n=jt(e,i)}catch{return!1}if(s instanceof RegExp){if(!s.test(String(n)))return!1}else if(typeof s==="object"&&s!==null){if(Object.keys(s).some((r)=>r.startsWith("$"))){if("$eq"in s&&n!==s.$eq)return!1;if("$ne"in s&&n===s.$ne)return!1;if("$gt"in s&&!(n>s.$gt))return!1;if("$lt"in s&&!(n<s.$lt))return!1;if("$gte"in s&&!(n>=s.$gte))return!1;if("$lte"in s&&!(n<=s.$lte))return!1;if("$in"in s&&!s.$in.includes(n))return!1;if("$nin"in s&&s.$nin.includes(n))return!1;if("$regex"in s){if(!(s.$regex instanceof RegExp?s.$regex:new RegExp(s.$regex)).test(String(n)))return!1}if("$fn"in s&&!s.$fn(n))return!1}else if(!yi(n,s))return!1}else if(n!==s)return!1}return!0}function zo(e,t=new Set){if(typeof e!=="object"||e===null||typeof e==="function")return e;let i=[],s=[],n=[],r=[],a=[];for(let o in e){if(o==="$or"||o==="$and"||o==="$not"){a.push(o);continue}let c=e[o];if(t.has(o))i.push(o);else if(c instanceof RegExp||typeof c==="object"&&c!==null&&(("$regex"in c)||("$fn"in c))||typeof c==="function")r.push(o);else if(typeof c==="object"&&c!==null&&(("$gt"in c)||("$lt"in c)||("$gte"in c)||("$lte"in c)||("$ne"in c)||("$in"in c)||("$nin"in c)))n.push(o);else s.push(o)}let l={};for(let o of[...i,...s,...n,...r,...a])l[o]=e[o];return l}var wi=new Set(["string","number","boolean","object","array","date","any"]);function bi(e){if(Array.isArray(e))return"array";if(e instanceof Date)return"date";return typeof e}function pn(e){let t=new Map,i=[];for(let[s,n]of Object.entries(e)){let r;if(typeof n==="string"){if(!wi.has(n))throw new N("ERR_SKALEX_VALIDATION_UNKNOWN_TYPE",`Unknown schema type "${n}" for field "${s}"`,{field:s,type:n});r={type:n,required:!1,unique:!1}}else if(typeof n==="object"&&n!==null){let{type:a="any",required:l=!1,unique:o=!1,enum:c}=n;if(!wi.has(a))throw new N("ERR_SKALEX_VALIDATION_UNKNOWN_TYPE",`Unknown schema type "${a}" for field "${s}"`,{field:s,type:a});if(r={type:a,required:l,unique:o,enum:c},o)i.push(s)}else throw new N("ERR_SKALEX_VALIDATION_INVALID_SCHEMA",`Invalid schema definition for field "${s}"`,{field:s});t.set(s,r)}return{fields:t,uniqueFields:i}}function gs(e,t,i=!1){let s=[];for(let[n,r]of t){let a=e[n],l=a===void 0||a===null;if(r.required&&l){s.push(`Field "${n}" is required`);continue}if(l)continue;if(r.type!=="any"){let o=bi(a);if(o!==r.type)s.push(`Field "${n}" must be of type "${r.type}", got "${o}"`)}if(r.enum&&!r.enum.includes(a))s.push(`Field "${n}" must be one of [${r.enum.map((o)=>JSON.stringify(o)).join(", ")}], got ${JSON.stringify(a)}`)}if(i){for(let n of Object.keys(e))if(!n.startsWith("_")&&!t.has(n))s.push(`Unknown field "${n}" (strict mode)`)}return s}function ys(e,t){let i={};for(let[s,n]of Object.entries(e)){if(s.startsWith("_")){i[s]=n;continue}if(!t.has(s))continue;let r=t.get(s);if(r.type!=="any"){if(bi(n)!==r.type)continue}if(r.enum&&!r.enum.includes(n))continue;i[s]=n}return i}function Bo(e){let t={};for(let[i,s]of Object.entries(e)){if(i.startsWith("_"))continue;let n=bi(s);t[i]=wi.has(n)?n:"any"}return t}function Xo(e){if(typeof e==="number"){if(e<=0)throw new N("ERR_SKALEX_VALIDATION_TTL",`TTL must be positive, got ${e}`,{ttl:e});return e*1000}if(typeof e!=="string")throw new N("ERR_SKALEX_VALIDATION_TTL",`Invalid TTL value: ${e}`,{ttl:e});let t=e.match(/^(\d+(?:\.\d+)?)(ms|s|m|h|d)$/);if(!t)throw new N("ERR_SKALEX_VALIDATION_TTL_FORMAT",`Invalid TTL format: "${e}". Use e.g. 300 (seconds), "30m", "24h", "7d"`,{ttl:e});let i=parseFloat(t[1]);if(i<=0)throw new N("ERR_SKALEX_VALIDATION_TTL",`TTL must be positive, got "${e}"`,{ttl:e});let s=t[2],n=i*{ms:1,s:1000,m:60000,h:3600000,d:86400000}[s];if(!isFinite(n))throw new N("ERR_SKALEX_VALIDATION_TTL",`TTL value "${e}" is too large`,{ttl:e});return n}function Ho(e){return new Date(Date.now()+Xo(e))}function Vo(e,t,i=null){let s=Date.now(),n=0,r=e.length;while(r--){let a=e[r];if(a._expiresAt&&new Date(a._expiresAt).getTime()<=s){if(e.splice(r,1),t.delete(a._id),i)i(a);n++}}return n}function ws(e,t){if(e.length!==t.length)throw new Qe("ERR_SKALEX_QUERY_VECTOR_MISMATCH",`Vector dimension mismatch: ${e.length} vs ${t.length}`,{expected:e.length,got:t.length});let i=0,s=0,n=0;for(let a=0;a<e.length;a++)i+=e[a]*t[a],s+=e[a]*e[a],n+=t[a]*t[a];let r=Math.sqrt(s*n);return r===0?0:i/r}function U(e){if(!("_vector"in e))return{...e};let{_vector:t,...i}=e;return i}function Jo(e){return e.length}function Yo(e,t){let i=0;for(let s of e){let n=jt(s,t);if(typeof n==="number"&&!isNaN(n))i+=n}return i}function Wo(e,t){let i=0,s=0;for(let n of e){let r=jt(n,t);if(typeof r==="number"&&!isNaN(r))i+=r,s++}return s===0?null:i/s}function Qo(e,t){let i=Object.create(null);for(let s of e){let n=String(jt(s,t)??"__null__");if(!i[n])i[n]=[];i[n].push(s)}return i}class mn{constructor(e){this._col=e}get _ctx(){return this._col._ctx}async execute({op:e,beforeHook:t,afterHook:i,hookPayload:s,mutate:n,afterHookPayload:r,save:a,session:l}){let o=this._ctx;await o.ensureConnected(),this._col._txSnapshotIfNeeded();let c=o.txManager,h=c.active&&this._col._activeTxId===c.context?.id,u=h?c.context.id:null,d=this._col._createdInTxId,_=()=>{if(u!==null&&c._abortedIds.has(u))throw new me("ERR_SKALEX_TX_ABORTED",`Transaction ${u} was aborted. No further mutations allowed.`);if(d!==null&&c._abortedIds.has(d))throw new me("ERR_SKALEX_TX_ABORTED",`Transaction ${d} was aborted. Collection obtained inside that transaction cannot be used for further mutations.`)};if(h||d!==null)_();if(t)await o.plugins.run(t,s);let{docs:f,prevDocs:p=[]}=await n(_);if(o.persistence.markDirty(o.collections,this._col.name),await this._col._saveIfNeeded(a),this._col._changelogEnabled)for(let m=0;m<f.length;m++)await o.logChange(e,this._col.name,f[m],p[m]??null,l||null);if(!h||!c.defer(()=>o.sessionStats.recordWrite(l)))o.sessionStats.recordWrite(l);for(let m of f)o.emitEvent(this._col.name,{op:e,collection:this._col.name,doc:U(m)});if(i)if(r)await o.runAfterHook(i,r(f));else for(let m of f)await o.runAfterHook(i,{collection:this._col.name,doc:U(m)});return{docs:f,prevDocs:p}}}function Go(e){let t={};for(let i in e){if(i.startsWith("$"))continue;let s=e[i];if(s&&typeof s==="object"&&!Array.isArray(s)){let n=Object.keys(s);if(n.length===1&&n[0]==="$eq")t[i]=s.$eq}else t[i]=s}return t}var gn=new Set(["__proto__","constructor","prototype"]);function yn(e){if(typeof e!=="object"||e===null||Array.isArray(e))return e;let t={};for(let i of Object.keys(e)){if(gn.has(i))continue;t[i]=yn(e[i])}return t}class wn{constructor(e,t){this.name=e.collectionName,this.database=t,this._ctx=t._buildCollectionContext(),this._store=e,this._pipeline=new mn(this),this._createdInTxId=t._txManager.context?.id??null,this._activeTxId=null}get _data(){return this._store.data}set _data(e){this._store.data=e}get _index(){return this._store.index}get _fieldIndex(){return this._store.fieldIndex||null}get _schema(){return this._store.schema?this._store.schema.fields:null}get _changelogEnabled(){return this._store.changelog===!0}get _softDelete(){return this._store.softDelete===!0}get _versioning(){return this._store.versioning===!0}get _strict(){return this._store.strict===!0}get _onSchemaError(){return this._store.onSchemaError??"throw"}get _defaultTtl(){return this._store.defaultTtl||null}get _defaultEmbed(){return this._store.defaultEmbed||null}get _maxDocs(){return this._store.maxDocs||null}async insertOne(e,t={}){let{save:i,ifNotExists:s,ttl:n,embed:r,session:a}=t;if(s){await this._ctx.ensureConnected();let o=this._findRaw(e);if(o)return U({...o})}let{docs:l}=await this._insertCore([e],{ttl:n,embed:r,session:a,save:i});return U(l[0])}async insertMany(e,t={}){let{save:i,ttl:s,embed:n,session:r}=t,{docs:a}=await this._insertCore(e,{ttl:s,embed:n,session:r,save:i});return a.map(U)}async _insertCore(e,{ttl:t,embed:i,session:s,save:n}){return this._pipeline.execute({op:"insert",beforeHook:null,afterHook:"afterInsert",hookPayload:null,save:n,session:s,mutate:async(r)=>{let a=[],l=new Set;for(let o of e){let c=this._applyValidation(o);await this._ctx.plugins.run("beforeInsert",{collection:this.name,doc:c});let h=await this._buildDoc(c,{ttl:t,embed:i});if(this._index.has(h._id)||l.has(h._id))throw new xe("ERR_SKALEX_UNIQUE_DUPLICATE_ID",`Duplicate _id "${h._id}" in collection "${this.name}"`,{id:h._id,collection:this.name});l.add(h._id),a.push(h)}if(r(),this._fieldIndex)this._fieldIndex.assertUniqueBatch(a);for(let o of a)this._addToIndex(o);this._data.push(...a);for(let o of a)this._index.set(o._id,o);return this._enforceCapAfterInsert(),{docs:a}}})}async updateOne(e,t,i={}){await this._ctx.ensureConnected();let s=this._findRaw(e);if(!s)return null;let{save:n,session:r}=i,{docs:a}=await this._updateCore([s],e,t,{save:n,session:r});return U(a[0])}async updateMany(e,t,i={}){await this._ctx.ensureConnected();let s=this._findAllRaw(e);if(s.length===0)return[];let{save:n,session:r}=i,{docs:a}=await this._updateCore(s,e,t,{save:n,session:r});return a.map(U)}async _updateCore(e,t,i,{save:s,session:n}){return this._pipeline.execute({op:"update",beforeHook:"beforeUpdate",afterHook:"afterUpdate",hookPayload:{collection:this.name,filter:t,update:i},save:s,session:n,afterHookPayload:(r)=>({collection:this.name,filter:t,update:i,result:r.length===1?r[0]:r}),mutate:async(r)=>{let a=this._changelogEnabled?e.map((h)=>structuredClone(h)):e.map(()=>null),l=e.map((h)=>this._prepareUpdatedDoc(h,i));this._assertUniqueCandidates(e,l),r();let o=new Set(e),c=new Map;for(let h=0;h<this._data.length;h++)if(o.has(this._data[h]))c.set(this._data[h],h);for(let h=0;h<e.length;h++)this._commitUpdatedDoc(e[h],l[h],c);return{docs:l,prevDocs:a}}})}applyUpdate(e,t){for(let i in t){if(gn.has(i))continue;if(i==="_id"||i==="createdAt")continue;let s=t[i];if(Array.isArray(s))e[i]=s;else if(typeof s==="object"&&s!==null){let n=Object.keys(s);if(n.some((r)=>r.startsWith("$"))){for(let r of n)if(r==="$inc"&&typeof e[i]==="number")e[i]+=s[r];else if(r==="$push"){if(!Array.isArray(e[i]))e[i]=[];e[i].push(s[r])}}else e[i]=yn(s)}else e[i]=s}if(e.updatedAt=new Date,this._versioning)e._version=(e._version??0)+1;return e}_prepareUpdatedDoc(e,t){let i=structuredClone(e),s=structuredClone(e);if(this.applyUpdate(s,t),!this._schema)return s;let n=gs(s,this._schema,this._strict);if(!n.length)return s;switch(this._onSchemaError){case"throw":throw new N("ERR_SKALEX_VALIDATION_UPDATE",`Update validation failed on doc "${e._id}": ${n.join("; ")}`,{id:e._id,errors:n});case"strip":return this._stripCandidateToValid(s,i);default:return this._ctx.logger(`[${this.name}] Update validation warning: ${n.join("; ")}`,"warn"),s}}_stripCandidateToValid(e,t){let i=ys(e,this._schema),s=structuredClone(t);for(let[n,r]of Object.entries(i)){if(n.startsWith("_"))continue;s[n]=r}if(s.updatedAt=e.updatedAt,this._versioning)s._version=e._version;return s}_commitUpdatedDoc(e,t,i){let s=i?i.get(e):this._data.indexOf(e);if(s===void 0||s===-1)throw new Re("ERR_SKALEX_PERSISTENCE_DOC_MISSING",`Document "${e._id}" no longer exists in collection "${this.name}"`,{id:e._id,collection:this.name});this._data[s]=t,this._index.set(t._id,t),this._updateInIndex(e,t)}_assertUniqueCandidates(e,t){this._fieldIndex?.assertUniqueCandidates(e,t)}async upsert(e,t,i={}){if(await this._ctx.ensureConnected(),this._findRaw(e))return this.updateOne(e,t,i);return this.insertOne({...Go(e),...t},i)}async upsertMany(e,t,i={}){await this._ctx.ensureConnected();let{save:s,...n}=i,r=[];for(let a of e)r.push(await this.upsert({[t]:a[t]},a,{...n,save:!1}));return await this._saveIfNeeded(s),r}async restore(e,t={}){if(!this._softDelete)throw new Qe("ERR_SKALEX_QUERY_SOFT_DELETE_REQUIRED",`restore() requires softDelete on "${this.name}"`,{collection:this.name});await this._ctx.ensureConnected();let{save:i,session:s}=t,n=this._findRaw(e,{includeDeleted:!0});if(!n||!n._deletedAt)return null;let{docs:r}=await this._pipeline.execute({op:"restore",beforeHook:null,afterHook:null,hookPayload:null,save:i,session:s,mutate:async(a)=>{return a(),delete n._deletedAt,n.updatedAt=new Date,this._index.set(n._id,n),{docs:[n]}}});return U(r[0])}watch(e,t){if(typeof e==="function")t=e,e=null;if(t)return this._ctx.eventBus.on(this.name,(i)=>{if(!e||H(i.doc,e))t(i)});return this._watchIterator(e)}_watchIterator(e){let t=[],i=null,s=!1,n=this._ctx.eventBus.on(this.name,(r)=>{if(e&&!H(r.doc,e))return;if(i){let a=i;i=null,a({value:r,done:!1})}else t.push(r)});return{[Symbol.asyncIterator](){return this},next(){if(t.length>0)return Promise.resolve({value:t.shift(),done:!1});if(s)return Promise.resolve({value:void 0,done:!0});return new Promise((r)=>{i=r})},return(){if(s=!0,n(),i){let r=i;i=null,r({value:void 0,done:!0})}return Promise.resolve({value:void 0,done:!0})}}}async count(e={}){return await this._ctx.ensureConnected(),Jo(this._findAllRaw(e))}async sum(e,t={}){return await this._ctx.ensureConnected(),Yo(this._findAllRaw(t),e)}async avg(e,t={}){return await this._ctx.ensureConnected(),Wo(this._findAllRaw(t),e)}async groupBy(e,t={}){return await this._ctx.ensureConnected(),Qo(this._findAllRaw(t),e)}async findOne(e,t={}){await this._ctx.ensureConnected();let{populate:i,select:s,includeDeleted:n=!1}=t,r=this._findRaw(e,{includeDeleted:n});if(!r)return null;let a=this._projectDoc(r,s);if(i)await this._populateDoc(a,r,i);return a}async find(e,t={}){await this._ctx.ensureConnected();let i=Date.now(),{populate:s,select:n,sort:r,page:a=1,limit:l,session:o,includeDeleted:c=!1}=t;await this._ctx.plugins.run("beforeFind",{collection:this.name,filter:e,options:t});let h=this._getCandidates(e),u=zo(e,this._fieldIndex?this._fieldIndex.indexedFields:new Set),d=[];for(let f of h){if(this._softDelete&&f._deletedAt&&!c)continue;if(!H(f,u))continue;let p=this._projectDoc(f,n);if(s)await this._populateDoc(p,f,s);d.push(p)}if(r){let f=Object.keys(r);d.sort((p,m)=>{for(let w of f){let v=r[w];if(p[w]<m[w])return-v;if(p[w]>m[w])return v}return 0})}let _;if(l){let f=d.length,p=Math.ceil(f/l),m=(a-1)*l;d=d.slice(m,m+l),_={page:a,totalDocs:f,totalPages:p}}return this._ctx.queryLog?.record({collection:this.name,op:"find",filter:e,duration:Date.now()-i,resultCount:d.length}),this._ctx.sessionStats.recordRead(o),await this._ctx.plugins.run("afterFind",{collection:this.name,filter:e,options:t,docs:d}),_?{docs:d,..._}:{docs:d}}async search(e,{filter:t,limit:i=10,minScore:s=0,session:n}={}){await this._ctx.ensureConnected();let r=Date.now();await this._ctx.plugins.run("beforeSearch",{collection:this.name,query:e,options:{filter:t,limit:i,minScore:s}});let a=await this._ctx.embed(e),l=t?this._findAllRaw(t):this._data,o=[];for(let d of l){if(!d._vector)continue;let _=ws(a,d._vector);if(_>=s)o.push({doc:d,score:_})}o.sort((d,_)=>_.score-d.score);let c=o.slice(0,i);this._ctx.queryLog?.record({collection:this.name,op:"search",query:e,duration:Date.now()-r,resultCount:c.length}),this._ctx.sessionStats.recordRead(n);let h=c.map((d)=>U(d.doc)),u=c.map((d)=>d.score);return await this._ctx.plugins.run("afterSearch",{collection:this.name,query:e,options:{filter:t,limit:i,minScore:s},docs:h,scores:u}),{docs:h,scores:u}}async similar(e,{limit:t=10,minScore:i=0}={}){await this._ctx.ensureConnected();let s=this._index.get(e);if(!s||!s._vector)return{docs:[],scores:[]};let n=[];for(let a of this._data){if(a._id===e||!a._vector)continue;let l=ws(s._vector,a._vector);if(l>=i)n.push({doc:a,score:l})}n.sort((a,l)=>l.score-a.score);let r=n.slice(0,t);return{docs:r.map((a)=>U(a.doc)),scores:r.map((a)=>a.score)}}async deleteOne(e,t={}){await this._ctx.ensureConnected();let{save:i,session:s}=t;if(this._softDelete){let r=this._findRaw(e);if(!r)return null;let{docs:a}=await this._deleteCore("soft",[r],e,{save:i,session:s});return U(a[0])}let{docs:n}=await this._deleteCore("hard",null,e,{save:i,session:s});if(n.length===0)return null;return U(n[0])}async deleteMany(e,t={}){await this._ctx.ensureConnected();let{save:i,session:s}=t;if(this._softDelete){let r=this._findAllRaw(e);if(r.length===0)return[];let{docs:a}=await this._deleteCore("soft",r,e,{save:i,session:s});return a.map(U)}let{docs:n}=await this._deleteCore("hardMany",null,e,{save:i,session:s});return n.map(U)}async _deleteCore(e,t,i,{save:s,session:n}){return this._pipeline.execute({op:"delete",beforeHook:"beforeDelete",afterHook:"afterDelete",hookPayload:{collection:this.name,filter:i},save:s,session:n,afterHookPayload:(r)=>({collection:this.name,filter:i,result:r.length===1?r[0]:r}),mutate:async(r)=>{if(r(),e==="soft"){let o=new Date;for(let c of t)c._deletedAt=o,c.updatedAt=o,this._index.set(c._id,c);return{docs:t}}if(e==="hard"){let o=this._findIndex(i);if(o===-1)return{docs:[]};let c=this._data.splice(o,1)[0];return this._index.delete(c._id),this._removeFromIndex(c),{docs:[c]}}let a=[],l=[];for(let o of this._data)if(H(o,i))a.push(o),this._index.delete(o._id),this._removeFromIndex(o);else l.push(o);return this._data=l,{docs:a}}})}async export(e={},t={}){let{dir:i,name:s,format:n="json"}=t;try{let r=this._data.filter((h)=>H(h,e));if(r.length===0)throw new Qe("ERR_SKALEX_QUERY_EXPORT_EMPTY",`export(): no documents matched the filter in "${this.name}"`,{collection:this.name});let a;if(n==="json")a=JSON.stringify(r,null,2);else{let h=(_)=>{if(_==null)return"";let f=typeof _==="object"?JSON.stringify(_):String(_);return f.includes(",")||f.includes('"')||f.includes(`
|
|
14
|
-
`)||f.includes("\r")?`"${f.replace(/"/g,'""')}"`:f},u=Object.keys(r[0]).map(h).join(","),d=r.map((_)=>Object.values(_).map(h).join(","));a=[u,...d].join(`
|
|
15
|
-
`)}if(typeof this._ctx.fs.writeRaw!=="function")throw new ue("ERR_SKALEX_ADAPTER_NO_RAW_WRITE","export() requires a file-system adapter (FsAdapter). The current adapter does not support raw file writes.");let l=i||`${this._ctx.dataDirectory}/exports`,o=`${s||this.name}.${n}`,c=this._ctx.fs.join(l,o);this._ctx.fs.ensureDir(l),await this._ctx.fs.writeRaw(c,a)}catch(r){throw this._ctx.logger(`Error exporting "${this.name}": ${r.message}`,"error"),r}}_txSnapshotIfNeeded(){let e=this._ctx.txManager;if(!e.active)return;if(this._activeTxId!==e.context?.id)return;e.snapshotIfNeeded(this.name,this._store,(t)=>this._ctx.snapshotCollection(t))}async _saveIfNeeded(e){let t=this._ctx.txManager;if(t.active&&this._activeTxId===t.context?.id)return;if(e??this._ctx.autoSave)await this._ctx.saveCollection(this.name)}_applyValidation(e){if(!this._schema)return e;let t=gs(e,this._schema,this._strict);if(!t.length)return e;switch(this._onSchemaError){case"warn":return this._ctx.logger(`[${this.name}] Validation warning: ${t.join("; ")}`,"warn"),e;case"strip":return ys(e,this._schema);default:throw new N("ERR_SKALEX_VALIDATION_FAILED",`Validation failed: ${t.join("; ")}`,{errors:t})}}_enforceCapAfterInsert(){let e=this._maxDocs;if(!e||this._data.length<=e)return;let t=this._data.splice(0,this._data.length-e);for(let i of t)this._index.delete(i._id),this._removeFromIndex(i)}_findRaw(e,{includeDeleted:t=!1}={}){if(typeof e==="function"){for(let i of this._data){if(this._softDelete&&i._deletedAt&&!t)continue;if(e(i))return i}return null}if(e._id){let i=this._index.get(e._id)||null;if(!i)return null;if(this._softDelete&&i._deletedAt&&!t)return null;if(Object.keys(e).length>1)return H(i,e)?i:null;return i}if(this._fieldIndex)for(let i in e){if(i==="$or"||i==="$and"||i==="$not")continue;let s=e[i];if(typeof s!=="object"||s===null){let n=this._fieldIndex._lookupIterable(i,s);if(n!==null){for(let r of n){if(this._softDelete&&r._deletedAt&&!t)continue;if(H(r,e))return r}return null}}}for(let i of this._data){if(this._softDelete&&i._deletedAt&&!t)continue;if(H(i,e))return i}return null}_findAllRaw(e,{includeDeleted:t=!1}={}){if(e&&typeof e!=="function"&&e._id){let s=this._index.get(e._id);if(!s)return[];if(this._softDelete&&s._deletedAt&&!t)return[];return H(s,e)?[s]:[]}let i=[];for(let s of this._getCandidates(e)){if(this._softDelete&&s._deletedAt&&!t)continue;if(H(s,e))i.push(s)}return i}_getCandidates(e){if(!this._fieldIndex)return this._data;if(this._fieldIndex._compoundIndexes.size>0){let t={};for(let i in e){if(i==="$or"||i==="$and"||i==="$not")continue;let s=e[i];if(typeof s!=="object"||s===null)t[i]=s}if(Object.keys(t).length>=2){let i=this._fieldIndex.lookupCompound(t);if(i!==null)return i}}for(let t in e){if(t==="$or"||t==="$and"||t==="$not")continue;let i=e[t];if(typeof i!=="object"||i===null){let s=this._fieldIndex._lookupIterable(t,i);if(s!==null)return s}}return this._data}_findIndex(e){for(let t=0;t<this._data.length;t++)if(H(this._data[t],e))return t;return-1}_addToIndex(e){if(this._fieldIndex)this._fieldIndex.add(e)}_removeFromIndex(e){if(this._fieldIndex)this._fieldIndex.remove(e)}_updateInIndex(e,t){if(this._fieldIndex)this._fieldIndex.update(e,t)}async _buildDoc(e,{ttl:t,embed:i}={}){let s={...e,_id:e._id??(this._ctx.idGenerator??Uo)(),createdAt:new Date,updatedAt:new Date},n=t??this._defaultTtl;if(n)s._expiresAt=Ho(n);let r=i??this._defaultEmbed;if(r){let a=typeof r==="function"?r(s):s[r];s._vector=await this._ctx.embed(String(a))}if(this._versioning)s._version=1;return s}_projectDoc(e,t){if(t){let s={};for(let n of t){if(n==="_vector")continue;s[n]=e[n]}return s}let i={...e};return delete i._vector,i}async _populateDoc(e,t,i){for(let s of i){let n=await this._ctx.getCollection(s).findOne({_id:t[s]});if(n)e[s]=n}}}class Ti{async read(e){throw Error("StorageAdapter.read() not implemented")}async write(e,t){throw Error("StorageAdapter.write() not implemented")}async delete(e){throw Error("StorageAdapter.delete() not implemented")}async list(){throw Error("StorageAdapter.list() not implemented")}async writeAll(e){for(let{name:t,data:i}of e)await this.write(t,i)}}class An extends Ti{constructor({dir:e,format:t="gz"}){super();this.dir=ze.resolve(e),this.format=t,this._ensureDir(this.dir)}_ensureDir(e){if(!j.existsSync(e))j.mkdirSync(e,{recursive:!0})}_filePath(e){return ze.join(this.dir,`${e}.${this.format}`)}async read(e){let t=this._filePath(e);try{let i=await j.promises.readFile(t);if(this.format==="gz")i=ai.inflateSync(i);return i.toString("utf8")}catch(i){if(i.code==="ENOENT")return null;throw i}}async write(e,t){let i=this._filePath(e),s=ze.join(this.dir,`${e}_${globalThis.crypto.randomUUID()}.tmp.${this.format}`);if(this.format==="gz"){let n=ai.deflateSync(t);await j.promises.writeFile(s,n)}else await j.promises.writeFile(s,t,"utf8");await j.promises.rename(s,i)}async writeAll(e){let t=[];try{for(let{name:i,data:s}of e){let n=this._filePath(i),r=ze.join(this.dir,`${i}_${globalThis.crypto.randomUUID()}.tmp.${this.format}`);if(this.format==="gz"){let a=ai.deflateSync(s);await j.promises.writeFile(r,a)}else await j.promises.writeFile(r,s,"utf8");t.push({tmp:r,fp:n})}for(let{tmp:i,fp:s}of t)await j.promises.rename(i,s)}catch(i){for(let{tmp:s}of t)try{await j.promises.unlink(s)}catch{}throw i}}async delete(e){let t=this._filePath(e);try{await j.promises.unlink(t)}catch(i){if(i.code!=="ENOENT")throw i}}async list(){try{let e=await j.promises.readdir(this.dir),t=`.${this.format}`;return e.filter((i)=>i.endsWith(t)&&!i.includes(".tmp.")).map((i)=>i.slice(0,-t.length))}catch(e){if(e.code==="ENOENT")return[];throw e}}join(...e){return ze.join(...e)}ensureDir(e){this._ensureDir(e)}async writeRaw(e,t){this._ensureDir(ze.dirname(e)),await j.promises.writeFile(e,t,"utf8")}async readRaw(e){return j.promises.readFile(ze.resolve(e),"utf8")}}class xn{constructor(){this._migrations=[]}add(e){let{version:t,up:i}=e;if(typeof t!=="number"||t<1)throw new N("ERR_SKALEX_VALIDATION_MIGRATION",`Migration version must be a positive integer, got ${t}`,{version:t});if(typeof i!=="function")throw new N("ERR_SKALEX_VALIDATION_MIGRATION",`Migration version ${t} must have an "up" function`,{version:t});if(this._migrations.some((s)=>s.version===t))throw new N("ERR_SKALEX_VALIDATION_MIGRATION_DUPLICATE",`Migration version ${t} is already registered`,{version:t});this._migrations.push({...e}),this._migrations.sort((s,n)=>s.version-n.version)}async run(e,t=[]){let i=new Set(t),s=this._migrations.filter((n)=>!i.has(n.version));for(let n of s){let r=e(n.version);await n.up(r),i.add(n.version)}return[...i].sort((n,r)=>n-r)}status(e=[]){let t=new Set(e),i=this._migrations.map((s)=>s.version).filter((s)=>!t.has(s));return{current:e.length?Math.max(...e):0,applied:[...t].sort((s,n)=>s-n),pending:i}}}var As={[Symbol.iterator](){return{next(){return{done:!0}}}},size:0};function xs(e){return{[Symbol.iterator](){return e[Symbol.iterator]()},get size(){return e.size}}}function hi(e){return e.map((t)=>{if(t===null||t===void 0)return"\x00";if(typeof t==="boolean")return t?"\x01T":"\x01F";if(typeof t==="number")return`\x02${t}`;return`\x03${String(t)}`}).join("\x1F")}class kt{constructor(e=[],t=[]){this._fields=new Set,this._compoundFields=[];for(let i of e)if(Array.isArray(i)){for(let s of i)this._validateFieldName(s);this._compoundFields.push(i)}else this._validateFieldName(i),this._fields.add(i);for(let i of t)this._validateFieldName(i);this._uniqueFields=new Set(t),this._indexedFields=new Set([...this._fields,...this._uniqueFields]),this._fieldIndexes=new Map,this._uniqueIndexes=new Map;for(let i of this._fields)this._fieldIndexes.set(i,new Map);for(let i of this._uniqueFields)if(this._uniqueIndexes.set(i,new Map),!this._fieldIndexes.has(i))this._fieldIndexes.set(i,new Map);this._compoundIndexes=new Map;for(let i of this._compoundFields)this._compoundIndexes.set(i.join("\x00"),{fields:i,map:new Map})}get indexedFields(){return this._indexedFields}buildFromData(e){for(let[,t]of this._fieldIndexes)t.clear();for(let[,t]of this._uniqueIndexes)t.clear();for(let[,t]of this._compoundIndexes)t.map.clear();for(let t of e)this._indexDoc(t)}add(e){this._checkUnique(e,null),this._indexDoc(e)}remove(e){for(let[t,i]of this._fieldIndexes){let s=e[t];if(s!==void 0){let n=i.get(s);if(n){if(n.delete(e),n.size===0)i.delete(s)}}}for(let[t,i]of this._uniqueIndexes){let s=e[t];if(s!==void 0)i.delete(s)}for(let[,t]of this._compoundIndexes){let i=hi(t.fields.map((n)=>e[n])),s=t.map.get(i);if(s){if(s.delete(e),s.size===0)t.map.delete(i)}}}update(e,t){this._checkUnique(t,e),this.remove(e);try{this._indexDoc(t)}catch(i){try{this._indexDoc(e)}catch{}throw i}}lookup(e,t){let i=this._fieldIndexes.get(e);if(!i)return null;let s=i.get(t);return s?[...s]:[]}_lookupIterable(e,t){let i=this._fieldIndexes.get(e);if(!i)return null;let s=i.get(t);return s?xs(s):As}lookupCompound(e){let t=Object.keys(e).sort();for(let[,i]of this._compoundIndexes){let s=[...i.fields].sort();if(s.length!==t.length)continue;if(s.every((n,r)=>n===t[r])){let n=hi(i.fields.map((a)=>e[a])),r=i.map.get(n);return r?xs(r):As}}return null}isUniqueTaken(e,t){let i=this._uniqueIndexes.get(e);if(!i)return!1;return i.has(t)}assertUniqueCandidates(e,t){if(this._uniqueFields.size===0)return;let i=new Set(e.map((s)=>s._id));for(let s of this._uniqueFields){let n=new Set,r=this._uniqueIndexes.get(s);if(r){for(let[l,o]of r.entries())if(!i.has(o._id))n.add(l)}let a=new Map;for(let l=0;l<t.length;l++){let o=t[l][s];if(o===void 0)continue;if(n.has(o))throw new xe("ERR_SKALEX_UNIQUE_VIOLATION",`Unique constraint violation: field "${s}" value "${o}" already exists`,{field:s,value:o});let c=a.get(o);if(c&&c!==e[l]._id)throw new xe("ERR_SKALEX_UNIQUE_VIOLATION",`Unique constraint violation: field "${s}" value "${o}" already exists`,{field:s,value:o});a.set(o,e[l]._id)}}}assertUniqueBatch(e){if(this._uniqueFields.size===0)return;for(let t of this._uniqueFields){let i=this._uniqueIndexes.get(t),s=new Set;for(let n of e){let r=n[t];if(r===void 0)continue;if(i&&i.has(r))throw new xe("ERR_SKALEX_UNIQUE_VIOLATION",`Unique constraint violation: field "${t}" value "${r}" already exists`,{field:t,value:r});if(s.has(r))throw new xe("ERR_SKALEX_UNIQUE_VIOLATION",`Unique constraint violation: duplicate "${t}" value "${r}" within batch`,{field:t,value:r});s.add(r)}}}_validateFieldName(e){if(e.includes("."))throw new N("ERR_SKALEX_VALIDATION_INDEX_DOT_PATH",`Index fields cannot use dot-notation: "${e}". Use a flat field name.`,{field:e})}_indexDoc(e){for(let[t,i]of this._fieldIndexes){let s=e[t];if(s!==void 0){if(!i.has(s))i.set(s,new Set);i.get(s).add(e)}}for(let[t,i]of this._uniqueIndexes){let s=e[t];if(s!==void 0)i.set(s,e)}for(let[,t]of this._compoundIndexes){for(let s of t.fields){let n=e[s];if(n!==void 0&&n!==null&&typeof n==="object")throw new N("ERR_SKALEX_VALIDATION_COMPOUND_INDEX",`Compound index field "${s}" must be a scalar value (string, number, or boolean), got ${Array.isArray(n)?"array":typeof n}`,{field:s})}let i=hi(t.fields.map((s)=>e[s]));if(!t.map.has(i))t.map.set(i,new Set);t.map.get(i).add(e)}}_checkUnique(e,t){for(let i of this._uniqueFields){let s=e[i];if(s===void 0)continue;let n=this._uniqueIndexes.get(i);if(!n)continue;let r=n.get(s);if(r&&r._id!==t?._id)throw new xe("ERR_SKALEX_UNIQUE_VIOLATION",`Unique constraint violation: field "${i}" value "${s}" already exists`,{field:i,value:s})}}}var vt="_flush",Et="migrations";class vn{constructor({adapter:e,serializer:t,deserializer:i,logger:s,debug:n=!1,lenientLoad:r=!1}){this._adapter=e,this._serializer=t,this._deserializer=i,this._logger=s,this._debug=n,this._lenientLoad=r,this._saveLock=Promise.resolve()}_withSaveLock(e){let t=this._saveLock.then(e);return this._saveLock=t.catch(()=>{}),t}async loadAll(e,{parseSchema:t,buildIndex:i,IndexEngine:s}){try{let n=await this._adapter.list();await Promise.all(n.map(async(r)=>{try{let a=await this._adapter.read(r);if(!a)return;let l=this._deserializer(a),{collectionName:o,data:c}=l;if(!o)return;let h=e[o],u=h?.rawSchema??l.rawSchema??null,d=h?.schema??(u?t(u):null),_=h?.changelog??l.changelog??!1,f=h?.softDelete??l.softDelete??!1,p=h?.versioning??l.versioning??!1,m=h?.strict??l.strict??!1,w=h?.onSchemaError??l.onSchemaError??"throw",v=h?.defaultTtl??l.defaultTtl??null,R=h?.defaultEmbed??l.defaultEmbed??null,E=h?.maxDocs??l.maxDocs??null,A=h?h.fieldIndex:null;if(!A&&d?.uniqueFields?.length)A=new s([],d.uniqueFields);let Ue=i(c,"_id");if(A)A.buildFromData(c);e[o]={collectionName:o,data:c,index:Ue,isSaving:!1,_pendingSave:!1,_dirty:!1,schema:d,rawSchema:u,fieldIndex:A,changelog:_,softDelete:f,versioning:p,strict:m,onSchemaError:w,defaultTtl:v,defaultEmbed:R,maxDocs:E}}catch(a){if(a.code==="ENOENT")return;if(this._lenientLoad){this._logger(`WARNING: Could not load collection "${r}": ${a.message}. Collection will be empty.`,"error");return}throw new Re("ERR_SKALEX_PERSISTENCE_CORRUPT",`Failed to load collection "${r}": ${a.message}`,{collection:r})}})),this._detectIncompleteFlush(e),await this._cleanOrphanTempFiles()}catch(n){if(n.code!=="ENOENT")throw this._logger(`Error loading data: ${n}`,"error"),n}}async save(e,t){if(t)await this._saveOne(e,t);else await Promise.all(Object.keys(e).map((i)=>this._saveOne(e,i)))}async saveDirty(e){let t=Object.keys(e).filter((i)=>e[i]._dirty);if(t.length===0)return;await Promise.all(t.map((i)=>this._saveOne(e,i)))}saveAtomic(e,t){return this._withSaveLock(async()=>{if(t.length===0)return;this._writeFlushSentinel(e,t);let i=new Set(t);i.add("_meta");let s=[...i].filter((n)=>e[n]).map((n)=>({name:n,data:this._serializeCollection(e[n])}));try{await this._adapter.writeAll(s)}catch(n){throw new Re("ERR_SKALEX_PERSISTENCE_FLUSH_FAILED",`Batch save failed during writeAll: ${n.message}`,{collections:t})}for(let n of t)if(e[n])e[n]._dirty=!1;try{this._clearFlushSentinel(e),await this._adapter.write("_meta",this._serializeCollection(e._meta))}catch(n){this._logger(`WARNING: Batch data committed but sentinel clear failed: ${n.message}. Next load will report an incomplete flush (false positive).`,"error")}})}markDirty(e,t){let i=e[t];if(i)i._dirty=!0}_serializeCollection(e){let t={collectionName:e.collectionName,data:e.data};if(e.rawSchema)t.rawSchema=e.rawSchema;if(e.changelog)t.changelog=e.changelog;if(e.softDelete)t.softDelete=e.softDelete;if(e.versioning)t.versioning=e.versioning;if(e.strict)t.strict=e.strict;if(e.onSchemaError!=="throw")t.onSchemaError=e.onSchemaError;if(e.defaultTtl)t.defaultTtl=e.defaultTtl;if(e.defaultEmbed)t.defaultEmbed=e.defaultEmbed;if(e.maxDocs)t.maxDocs=e.maxDocs;return this._serializer(t)}async _saveOne(e,t){let i=e[t];if(!i)return;if(i.isSaving)return i._pendingSave=!0,new Promise((s,n)=>{(i._saveWaiters??=[]).push({resolve:s,reject:n})});i.isSaving=!0,i._pendingSave=!1;try{await this._writeCollection(t,i);while(i._pendingSave)i._pendingSave=!1,await this._writeCollection(t,i);this._resolveSaveWaiters(i)}catch(s){throw this._rejectSaveWaiters(i,s),s}finally{i.isSaving=!1}}async _writeCollection(e,t){try{await this._adapter.write(e,this._serializeCollection(t)),t._dirty=!1}catch(i){throw this._logger(`Error saving "${e}": ${i.message}`,"error"),i}}_resolveSaveWaiters(e){let t=e._saveWaiters?.splice(0)??[];for(let i of t)i.resolve()}_rejectSaveWaiters(e,t){let i=e._saveWaiters?.splice(0)??[];for(let s of i)s.reject(t)}_writeFlushSentinel(e,t){let i=this._getOrCreateMeta(e);i[vt]={startedAt:new Date().toISOString(),collections:t,completedAt:null}}_clearFlushSentinel(e){let t=this._getOrCreateMeta(e);if(t[vt])t[vt].completedAt=new Date().toISOString()}_detectIncompleteFlush(e){let t=e._meta;if(!t)return;let i=t.index.get(Et);if(!i)return;let s=i[vt];if(!s)return;if(s.startedAt&&!s.completedAt)this._logger(`WARNING: Incomplete flush detected (started ${s.startedAt}, collections: ${s.collections?.join(", ")}). Data may be inconsistent.`,"error")}async _cleanOrphanTempFiles(){if(typeof this._adapter.join!=="function")return;if(typeof this._adapter.list!=="function")return;try{let e=await import("fs"),t=await import("path"),i=this._adapter.dir;if(!i)return;let s=(await e.default.promises.readdir(i)).filter((n)=>n.includes(".tmp."));for(let n of s){let r=t.default.join(i,n);try{await e.default.promises.unlink(r),this._log(`Cleaned orphan temp file: ${n}`)}catch{}}}catch{}}_getOrCreateMeta(e){if(!e._meta)e._meta={collectionName:"_meta",data:[],index:new Map,isSaving:!1,_pendingSave:!1,_dirty:!1,schema:null,rawSchema:null,fieldIndex:null,changelog:!1,softDelete:!1,versioning:!1,strict:!1,onSchemaError:"throw",defaultTtl:null,defaultEmbed:null,maxDocs:null};let t=e._meta,i=t.index.get(Et);if(!i)i={_id:Et},t.data.push(i),t.index.set(Et,i);return i}_log(e){if(this._debug)this._logger(e,"info")}}var Zo=0;class En{constructor(){this._txLock=Promise.resolve(),this._ctx=null,this._abortedIds=new Set}get active(){return this._ctx!==null&&!this._ctx.aborted}get context(){return this._ctx}async run(e,t,{timeout:i=0}={}){let s=async()=>{await t._ensureConnected();let r=new Set(Object.keys(t.collections)),a={id:++Zo,startedAt:Date.now(),aborted:!1,preExisting:r,touchedCollections:new Set,snapshots:new Map,deferredEffects:[],timeout:i};this._ctx=a;let l=null,o=i>0?new Promise((u,d)=>{l=setTimeout(()=>{a.aborted=!0,d(new me("ERR_SKALEX_TX_TIMEOUT",`Transaction ${a.id} timed out after ${i}ms`))},i)}):null,c=this,h=new Proxy(t,{get(u,d){if(d==="_txId")return a.id;if(a!==c._ctx)throw new me("ERR_SKALEX_TX_STALE_PROXY",`Transaction ${a.id} has ended. This proxy is no longer usable.`);if(d==="collections")throw new me("ERR_SKALEX_TX_DIRECT_ACCESS","Direct access to db.collections inside transaction() is not covered by rollback. Use the collection API (db.useCollection) instead.");if(d==="useCollection")return(f)=>{let p=u.useCollection(f);return p._activeTxId=a.id,p};let _=Reflect.get(u,d);return typeof _==="function"?_.bind(u):_}});try{let u=e(h),d=o?await Promise.race([u,o]):await u;if(a.aborted)throw new me("ERR_SKALEX_TX_ABORTED",`Transaction ${a.id} was aborted`);let _=[...a.touchedCollections];if(_.length>0)await t._persistence.saveAtomic(t.collections,_);for(let f of a.deferredEffects)await f();return d}catch(u){for(let[d,_]of a.snapshots)if(a.preExisting.has(d)){if(t._applySnapshot(d,_),t.collections[d])t.collections[d]._dirty=_._dirty}for(let d in t.collections)if(!a.preExisting.has(d))delete t.collections[d],delete t._collectionInstances[d];throw u}finally{if(l)clearTimeout(l);if(a.aborted)this._abortedIds.add(a.id);this._ctx=null;for(let u in t._collectionInstances){let d=t._collectionInstances[u];if(d._createdInTxId===a.id)d._createdInTxId=null;if(d._activeTxId===a.id)d._activeTxId=null}}},n=this._txLock.then(s);return this._txLock=n.catch(()=>{}),n}snapshotIfNeeded(e,t,i){let s=this._ctx;if(!s)return;if(this._assertCtxNotAborted(s),!s.snapshots.has(e)){let n=i(t);n._dirty=t._dirty??!1,s.snapshots.set(e,n)}s.touchedCollections.add(e)}assertNotAborted(){let e=this._ctx;if(e)this._assertCtxNotAborted(e)}_assertCtxNotAborted(e){if(e.aborted)throw new me("ERR_SKALEX_TX_ABORTED",`Transaction ${e.id} was aborted. No further mutations allowed.`)}defer(e){if(this._ctx)return this._ctx.deferredEffects.push(e),!0;return!1}}class Sn{constructor(e){this._CollectionClass=e,this.stores={},this._instances={}}get(e,t){if(this._instances[e])return this._instances[e];if(!this.stores[e])this.createStore(e);let i=new this._CollectionClass(this.stores[e],t);return this._instances[e]=i,i}create(e,t={},i){this.createStore(e,t);let s=new this._CollectionClass(this.stores[e],i);return this._instances[e]=s,s}createStore(e,{schema:t,indexes:i=[],changelog:s=!1,softDelete:n=!1,versioning:r=!1,strict:a=!1,onSchemaError:l="throw",defaultTtl:o=null,defaultEmbed:c=null,maxDocs:h=null}={}){let u=null,d=null;if(t){u=pn(t);let _=u.uniqueFields;if(i.length||_.length)d=new kt(i,_)}else if(i.length)d=new kt(i,[]);this.stores[e]={collectionName:e,data:[],index:new Map,isSaving:!1,_pendingSave:!1,_dirty:!1,schema:u,rawSchema:t||null,fieldIndex:d,changelog:s,softDelete:n,versioning:r,strict:a,onSchemaError:l,defaultTtl:o,defaultEmbed:c,maxDocs:h}}rename(e,t){if(!this.stores[e])throw new Re("ERR_SKALEX_PERSISTENCE_COLLECTION_NOT_FOUND",`Collection "${e}" not found`,{collection:e});if(this.stores[t])throw new Re("ERR_SKALEX_PERSISTENCE_COLLECTION_EXISTS",`Collection "${t}" already exists`,{collection:t});let i=this.stores[e];if(i.collectionName=t,this.stores[t]=i,delete this.stores[e],this._instances[e]){let s=this._instances[e];s.name=t,this._instances[t]=s,delete this._instances[e]}}buildIndex(e,t){let i=new Map;for(let s of e)i.set(s[t],s);return i}inspect(e){if(e){let i=this.stores[e];if(!i)return null;return{name:e,count:i.data.length,schema:i.schema?Object.fromEntries(i.schema.fields):null,indexes:i.fieldIndex?[...i.fieldIndex.indexedFields]:[],softDelete:i.softDelete??!1,versioning:i.versioning??!1,strict:i.strict??!1,onSchemaError:i.onSchemaError??"throw",maxDocs:i.maxDocs??null}}let t={};for(let i in this.stores)t[i]=this.inspect(i);return t}dump(){let e={};for(let t in this.stores)if(!t.startsWith("_"))e[t]=structuredClone(this.stores[t].data);return e}schema(e){let t=this.stores[e];if(!t)return null;if(t.schema)return Object.fromEntries([...t.schema.fields.entries()].map(([i,s])=>[i,s.type]));if(t.data.length>0)return Bo(t.data[0]);return null}stats(e){let t=(i)=>{let s=this.stores[i];if(!s)return null;let n=s.data.length,r=0;for(let a of s.data)try{r+=JSON.stringify(a).length}catch(l){}return{collection:i,count:n,estimatedSize:r,avgDocSize:n>0?Math.round(r/n):0}};if(e)return t(e);return Object.keys(this.stores).map(t)}clear(){this.stores={},this._instances={}}}class Oi{async embed(e){throw Error("EmbeddingAdapter.embed() not implemented")}}var le=(e)=>globalThis.process?.env?.[e]??globalThis.Deno?.env?.get(e);class In extends Oi{constructor({apiKey:e=le("OPENAI_API_KEY"),model:t=le("OPENAI_EMBED_MODEL")??"text-embedding-3-small",baseUrl:i=le("OPENAI_EMBED_BASE_URL")??"https://api.openai.com/v1/embeddings",dimensions:s=le("OPENAI_EMBED_DIMENSIONS")!=null?Number(le("OPENAI_EMBED_DIMENSIONS")):void 0,organization:n=le("OPENAI_ORGANIZATION")??void 0,timeout:r=le("OPENAI_EMBED_TIMEOUT")!=null?Number(le("OPENAI_EMBED_TIMEOUT")):void 0,retries:a=Number(le("OPENAI_EMBED_RETRIES")??0),retryDelay:l=Number(le("OPENAI_EMBED_RETRY_DELAY")??1000),headers:o={},fetch:c=globalThis.fetch}={}){super();if(!e)throw Error("OpenAIEmbeddingAdapter requires an apiKey");this.apiKey=e,this.model=t,this.baseUrl=i,this.dimensions=s,this.organization=n,this.timeout=r,this.retries=a,this.retryDelay=l,this.headers=o,this._fetch=c}async embed(e){let t;for(let i=0;i<=this.retries;i++){let s=this.timeout!=null?new AbortController:null,n=s?setTimeout(()=>s.abort(),this.timeout):null;try{let r=await this._fetch(this.baseUrl,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`,...this.organization&&{"OpenAI-Organization":this.organization},...this.headers},body:JSON.stringify({input:e,model:this.model,...this.dimensions!==void 0&&{dimensions:this.dimensions}}),...s&&{signal:s.signal}});if(!r.ok){let a=(await r.text()).slice(0,200);throw Error(`OpenAI embedding API error ${r.status}: ${a}`)}return(await r.json()).data[0].embedding}catch(r){if(t=r,i<this.retries)await new Promise((a)=>setTimeout(a,this.retryDelay*2**i))}finally{if(n!==null)clearTimeout(n)}}throw t}}var it=(e)=>globalThis.process?.env?.[e]??globalThis.Deno?.env?.get(e);class bn extends Oi{constructor({model:e=it("OLLAMA_EMBED_MODEL")??"nomic-embed-text",host:t=it("OLLAMA_HOST")??"http://localhost:11434",timeout:i=it("OLLAMA_EMBED_TIMEOUT")!=null?Number(it("OLLAMA_EMBED_TIMEOUT")):void 0,retries:s=Number(it("OLLAMA_EMBED_RETRIES")??0),retryDelay:n=Number(it("OLLAMA_EMBED_RETRY_DELAY")??1000),headers:r={},fetch:a=globalThis.fetch}={}){super();this.model=e,this.host=t,this.timeout=i,this.retries=s,this.retryDelay=n,this.headers=r,this._fetch=a}async embed(e){let t;for(let i=0;i<=this.retries;i++){let s=this.timeout!=null?new AbortController:null,n=s?setTimeout(()=>s.abort(),this.timeout):null;try{let r=await this._fetch(`${this.host}/api/embeddings`,{method:"POST",headers:{"Content-Type":"application/json",...this.headers},body:JSON.stringify({model:this.model,prompt:e}),...s&&{signal:s.signal}});if(!r.ok){let a=(await r.text()).slice(0,200);throw Error(`Ollama embedding API error ${r.status}: ${a}`)}return(await r.json()).embedding}catch(r){if(t=r,i<this.retries)await new Promise((a)=>setTimeout(a,this.retryDelay*2**i))}finally{if(n!==null)clearTimeout(n)}}throw t}}class qt{async generate(e,t){throw Error("LLMAdapter.generate() not implemented")}async summarize(e){throw Error("LLMAdapter.summarize() not implemented")}}var Ni=`You are a database query assistant. Convert the user's natural language request into a JSON filter object for a document database.
|
|
16
|
-
|
|
17
|
-
Rules:
|
|
18
|
-
- Return ONLY a valid JSON object, nothing else
|
|
19
|
-
- Use standard query operators: $eq, $ne, $gt, $gte, $lt, $lte, $in, $nin, $regex
|
|
20
|
-
- For simple equality, use plain key-value pairs: {"field": "value"}
|
|
21
|
-
- For numeric comparisons use operators: {"age": {"$gt": 18}}
|
|
22
|
-
- For text search use $regex: {"name": {"$regex": "pattern"}}
|
|
23
|
-
- Return {} to match all documents
|
|
24
|
-
- No explanations, no markdown, no code fences`,$i="Summarise the following memory entries into one concise paragraph. Preserve all important facts.",k=(e)=>globalThis.process?.env?.[e]??globalThis.Deno?.env?.get(e);class Tn extends qt{constructor({apiKey:e=k("OPENAI_API_KEY"),model:t=k("OPENAI_MODEL")??"gpt-4o-mini",baseUrl:i=k("OPENAI_BASE_URL")??"https://api.openai.com/v1/chat/completions",maxTokens:s=k("OPENAI_MAX_TOKENS")!=null?Number(k("OPENAI_MAX_TOKENS")):void 0,temperature:n=Number(k("OPENAI_TEMPERATURE")??0.3),topP:r=k("OPENAI_TOP_P")!=null?Number(k("OPENAI_TOP_P")):void 0,organization:a=k("OPENAI_ORGANIZATION")??void 0,timeout:l=k("OPENAI_TIMEOUT")!=null?Number(k("OPENAI_TIMEOUT")):void 0,retries:o=Number(k("OPENAI_RETRIES")??0),retryDelay:c=Number(k("OPENAI_RETRY_DELAY")??1000),headers:h={},fetch:u=globalThis.fetch,seed:d=k("OPENAI_SEED")!=null?Number(k("OPENAI_SEED")):void 0,generatePrompt:_=Ni,summarizePrompt:f=$i}={}){super();if(!e)throw Error("OpenAILLMAdapter requires an apiKey");this.apiKey=e,this.model=t,this.baseUrl=i,this.maxTokens=s,this.temperature=n,this.topP=r,this.organization=a,this.timeout=l,this.retries=o,this.retryDelay=c,this.headers=h,this._fetch=u,this.seed=d,this.generatePrompt=_,this.summarizePrompt=f}async generate(e,t){let i=await this._post({model:this.model,messages:[{role:"system",content:`${this.generatePrompt}
|
|
25
|
-
Schema: ${JSON.stringify(e)}`},{role:"user",content:t}],response_format:{type:"json_object"},temperature:0,...this.maxTokens!==void 0&&{max_tokens:this.maxTokens},...this.seed!==void 0&&{seed:this.seed}});return JSON.parse(i.choices[0].message.content)}async summarize(e){return(await this._post({model:this.model,messages:[{role:"system",content:this.summarizePrompt},{role:"user",content:e}],temperature:this.temperature,...this.maxTokens!==void 0&&{max_tokens:this.maxTokens},...this.topP!==void 0&&{top_p:this.topP},...this.seed!==void 0&&{seed:this.seed}})).choices[0].message.content.trim()}async _post(e){let t;for(let i=0;i<=this.retries;i++){let s=this.timeout!=null?new AbortController:null,n=s?setTimeout(()=>s.abort(),this.timeout):null;try{let r=await this._fetch(this.baseUrl,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`,...this.organization&&{"OpenAI-Organization":this.organization},...this.headers},body:JSON.stringify(e),...s&&{signal:s.signal}});if(!r.ok){let a=(await r.text()).slice(0,200);throw Error(`OpenAI API error ${r.status}: ${a}`)}return r.json()}catch(r){if(t=r,i<this.retries)await new Promise((a)=>setTimeout(a,this.retryDelay*2**i))}finally{if(n!==null)clearTimeout(n)}}throw t}}var W=(e)=>globalThis.process?.env?.[e]??globalThis.Deno?.env?.get(e);class On extends qt{constructor({apiKey:e=W("ANTHROPIC_API_KEY"),model:t=W("ANTHROPIC_MODEL")??"claude-haiku-4-5",baseUrl:i=W("ANTHROPIC_BASE_URL")??"https://api.anthropic.com/v1/messages",apiVersion:s="2023-06-01",maxTokens:n=Number(W("ANTHROPIC_MAX_TOKENS")??1024),temperature:r=Number(W("ANTHROPIC_TEMPERATURE")??0.3),topP:a=W("ANTHROPIC_TOP_P")!=null?Number(W("ANTHROPIC_TOP_P")):void 0,topK:l=W("ANTHROPIC_TOP_K")!=null?Number(W("ANTHROPIC_TOP_K")):void 0,timeout:o=W("ANTHROPIC_TIMEOUT")!=null?Number(W("ANTHROPIC_TIMEOUT")):void 0,retries:c=Number(W("ANTHROPIC_RETRIES")??0),retryDelay:h=Number(W("ANTHROPIC_RETRY_DELAY")??1000),headers:u={},fetch:d=globalThis.fetch,generatePrompt:_=Ni,summarizePrompt:f=$i}={}){super();if(!e)throw Error("AnthropicLLMAdapter requires an apiKey");this.apiKey=e,this.model=t,this.baseUrl=i,this.apiVersion=s,this.maxTokens=n,this.temperature=r,this.topP=a,this.topK=l,this.timeout=o,this.retries=c,this.retryDelay=h,this.headers=u,this._fetch=d,this.generatePrompt=_,this.summarizePrompt=f}async generate(e,t){let i=(await this._post({model:this.model,max_tokens:this.maxTokens,temperature:0,system:`${this.generatePrompt}
|
|
26
|
-
Schema: ${JSON.stringify(e)}`,messages:[{role:"user",content:t}]})).content[0].text.trim().replace(/^```(?:json)?\n?/,"").replace(/\n?```$/,"");return JSON.parse(i)}async summarize(e){return(await this._post({model:this.model,max_tokens:this.maxTokens,temperature:this.temperature,...this.topP!==void 0&&{top_p:this.topP},...this.topK!==void 0&&{top_k:this.topK},system:this.summarizePrompt,messages:[{role:"user",content:e}]})).content[0].text.trim()}async _post(e){let t;for(let i=0;i<=this.retries;i++){let s=this.timeout!=null?new AbortController:null,n=s?setTimeout(()=>s.abort(),this.timeout):null;try{let r=await this._fetch(this.baseUrl,{method:"POST",headers:{"Content-Type":"application/json","x-api-key":this.apiKey,"anthropic-version":this.apiVersion,...this.headers},body:JSON.stringify(e),...s&&{signal:s.signal}});if(!r.ok){let a=(await r.text()).slice(0,200);throw Error(`Anthropic API error ${r.status}: ${a}`)}return r.json()}catch(r){if(t=r,i<this.retries)await new Promise((a)=>setTimeout(a,this.retryDelay*2**i))}finally{if(n!==null)clearTimeout(n)}}throw t}}var ne=(e)=>globalThis.process?.env?.[e]??globalThis.Deno?.env?.get(e);class Nn extends qt{constructor({model:e=ne("OLLAMA_MODEL")??"llama3.2",host:t=ne("OLLAMA_HOST")??"http://localhost:11434",temperature:i=Number(ne("OLLAMA_TEMPERATURE")??0.3),topP:s=ne("OLLAMA_TOP_P")!=null?Number(ne("OLLAMA_TOP_P")):void 0,topK:n=ne("OLLAMA_TOP_K")!=null?Number(ne("OLLAMA_TOP_K")):void 0,timeout:r=ne("OLLAMA_TIMEOUT")!=null?Number(ne("OLLAMA_TIMEOUT")):void 0,retries:a=Number(ne("OLLAMA_RETRIES")??0),retryDelay:l=Number(ne("OLLAMA_RETRY_DELAY")??1000),headers:o={},fetch:c=globalThis.fetch,generatePrompt:h=Ni,summarizePrompt:u=$i}={}){super();this.model=e,this.host=t,this.temperature=i,this.topP=s,this.topK=n,this.timeout=r,this.retries=a,this.retryDelay=l,this.headers=o,this._fetch=c,this.generatePrompt=h,this.summarizePrompt=u}async generate(e,t){let i=`${this.generatePrompt}
|
|
27
|
-
Schema: ${JSON.stringify(e)}
|
|
28
|
-
Query: ${t}`,s=await this._post({model:this.model,prompt:i,format:"json",options:{temperature:0},stream:!1});return JSON.parse(s.response)}async summarize(e){return(await this._post({model:this.model,prompt:`${this.summarizePrompt}
|
|
29
|
-
|
|
30
|
-
${e}`,options:{temperature:this.temperature,...this.topP!==void 0&&{top_p:this.topP},...this.topK!==void 0&&{top_k:this.topK}},stream:!1})).response.trim()}async _post(e){let t;for(let i=0;i<=this.retries;i++){let s=this.timeout!=null?new AbortController:null,n=s?setTimeout(()=>s.abort(),this.timeout):null;try{let r=await this._fetch(`${this.host}/api/generate`,{method:"POST",headers:{"Content-Type":"application/json",...this.headers},body:JSON.stringify(e),...s&&{signal:s.signal}});if(!r.ok){let a=(await r.text()).slice(0,200);throw Error(`Ollama API error ${r.status}: ${a}`)}return r.json()}catch(r){if(t=r,i<this.retries)await new Promise((a)=>setTimeout(a,this.retryDelay*2**i))}finally{if(n!==null)clearTimeout(n)}}throw t}}function el({provider:e,apiKey:t,embedModel:i,model:s,host:n,embedBaseUrl:r,dimensions:a,organization:l,embedTimeout:o,embedRetries:c,embedRetryDelay:h}){let u=i||s;switch(e){case"openai":return new In({apiKey:t,model:u,baseUrl:r,...a!==void 0&&{dimensions:a},...l!==void 0&&{organization:l},...o!==void 0&&{timeout:o},...c!==void 0&&{retries:c},...h!==void 0&&{retryDelay:h}});case"ollama":return new bn({model:u,host:n,...o!==void 0&&{timeout:o},...c!==void 0&&{retries:c},...h!==void 0&&{retryDelay:h}});default:throw new ue("ERR_SKALEX_ADAPTER_UNKNOWN_PROVIDER",`Unknown AI provider: "${e}". Supported: "openai", "ollama".`,{provider:e})}}function tl({provider:e,apiKey:t,model:i,host:s,baseUrl:n,apiVersion:r,temperature:a,maxTokens:l,topP:o,topK:c,organization:h,timeout:u,retries:d,retryDelay:_,seed:f,generatePrompt:p,summarizePrompt:m}){if(!i)return null;switch(e){case"openai":return new Tn({apiKey:t,model:i,baseUrl:n,...l!==void 0&&{maxTokens:l},...a!==void 0&&{temperature:a},...o!==void 0&&{topP:o},...h!==void 0&&{organization:h},...u!==void 0&&{timeout:u},...d!==void 0&&{retries:d},..._!==void 0&&{retryDelay:_},...f!==void 0&&{seed:f},...p!==void 0&&{generatePrompt:p},...m!==void 0&&{summarizePrompt:m}});case"anthropic":return new On({apiKey:t,model:i,baseUrl:n,apiVersion:r,...l!==void 0&&{maxTokens:l},...a!==void 0&&{temperature:a},...o!==void 0&&{topP:o},...c!==void 0&&{topK:c},...u!==void 0&&{timeout:u},...d!==void 0&&{retries:d},..._!==void 0&&{retryDelay:_},...p!==void 0&&{generatePrompt:p},...m!==void 0&&{summarizePrompt:m}});case"ollama":return new Nn({model:i,host:s,...a!==void 0&&{temperature:a},...o!==void 0&&{topP:o},...c!==void 0&&{topK:c},...u!==void 0&&{timeout:u},...d!==void 0&&{retries:d},..._!==void 0&&{retryDelay:_},...p!==void 0&&{generatePrompt:p},...m!==void 0&&{summarizePrompt:m}});default:return null}}var di="AES-GCM",pt=12,ve=32,il=new TextEncoder,sl=new TextDecoder;class $n extends Ti{constructor(e,t){super();if(this._adapter=e,this._rawKey=typeof t==="string"?nl(t):Uint8Array.from(t),this._rawKey.length!==ve)throw new ue("ERR_SKALEX_ADAPTER_INVALID_KEY",`EncryptedAdapter: key must be ${ve} bytes (${ve*2} hex chars), got ${this._rawKey.length}`);this._cryptoKey=null}async read(e){let t=await this._adapter.read(e);if(!t)return null;return this._decrypt(t)}async write(e,t){return this._adapter.write(e,await this._encrypt(t))}async delete(e){return this._adapter.delete(e)}async list(){return this._adapter.list()}async writeAll(e){let t=[];for(let{name:i,data:s}of e)t.push({name:i,data:await this._encrypt(s)});return this._adapter.writeAll(t)}join(...e){return this._adapter.join?.(...e)}ensureDir(e){return this._adapter.ensureDir?.(e)}async writeRaw(e,t){return this._adapter.writeRaw?.(e,await this._encrypt(t))}async readRaw(e){let t=await this._adapter.readRaw?.(e);if(!t)return null;return this._decrypt(t)}async _getKey(){if(!this._cryptoKey)this._cryptoKey=await globalThis.crypto.subtle.importKey("raw",this._rawKey,{name:di},!1,["encrypt","decrypt"]);return this._cryptoKey}async _encrypt(e){let t=await this._getKey(),i=globalThis.crypto.getRandomValues(new Uint8Array(pt)),s=il.encode(e),n=await globalThis.crypto.subtle.encrypt({name:di,iv:i,tagLength:128},t,s),r=new Uint8Array(pt+n.byteLength);return r.set(i,0),r.set(new Uint8Array(n),pt),rl(r)}async _decrypt(e){let t=await this._getKey(),i=al(e),s=i.slice(0,pt),n=i.slice(pt),r=await globalThis.crypto.subtle.decrypt({name:di,iv:s,tagLength:128},t,n);return sl.decode(r)}}function nl(e){if(e.length!==ve*2)throw new ue("ERR_SKALEX_ADAPTER_INVALID_KEY",`EncryptedAdapter: hex key must be ${ve*2} characters (${ve} bytes)`);if(!/^[0-9a-fA-F]+$/.test(e))throw new ue("ERR_SKALEX_ADAPTER_INVALID_KEY","EncryptedAdapter: hex key contains invalid characters");let t=new Uint8Array(ve);for(let i=0;i<ve;i++)t[i]=parseInt(e.slice(i*2,i*2+2),16);return t}function rl(e){let t="";for(let i=0;i<e.length;i++)t+=String.fromCharCode(e[i]);return globalThis.btoa(t)}function al(e){let t=globalThis.atob(e),i=new Uint8Array(t.length);for(let s=0;s<t.length;s++)i[s]=t.charCodeAt(s);return i}var ol=4;class Dn{constructor(e,t){this.sessionId=e,this._db=t,this._col=t.useCollection(`_memory_${e}`)}async remember(e){let t=await this._col.insertOne({text:e,sessionId:this.sessionId},{embed:"text"}),i=this._db._memoryConfig?.maxEntries;if(i&&this._col._data.length>i)await this.compress({threshold:0});return t}async recall(e,{limit:t=10,minScore:i=0}={}){return this._col.search(e,{limit:t,minScore:i})}async history({since:e,limit:t}={}){let i=e?{createdAt:{$gte:new Date(e)}}:{},s={sort:{createdAt:1}};if(t)s.limit=t;let{docs:n}=await this._col.find(i,s);return n}async forget(e){return this._col.deleteOne({_id:e})}tokenCount(){let e=this._col._data;return{tokens:e.reduce((t,i)=>t+this._docTokens(i),0),count:e.length}}context({tokens:e=this._db._memoryConfig?.contextTokens??4000}={}){let t=this._sortedData("desc"),i=[],s=0;for(let n of t){let r=this._docTokens(n);if(s+r>e)break;i.push(n.text),s+=r}return i.reverse().join(`
|
|
31
|
-
`)}async compress({threshold:e=this._db._memoryConfig?.compressionThreshold??8000,keepRecent:t=this._db._memoryConfig?.keepRecent??10}={}){let{tokens:i}=this.tokenCount();if(i<=e)return;if(!this._db._aiAdapter)throw Error('memory.compress() requires a language model adapter. Configure { ai: { model: "..." } }.');let s=this._sortedData("asc"),n=Math.max(0,s.length-t),r=s.slice(0,n);if(r.length===0)return;let a=r.map((o)=>o.text).join(`
|
|
32
|
-
`),l=await this._db._aiAdapter.summarize(a);await this._col.deleteMany({_id:{$in:r.map((o)=>o._id)}}),await this._col.insertOne({text:l,sessionId:this.sessionId,compressed:!0})}_docTokens(e){return Math.ceil((e.text||"").length/ol)}_sortedData(e){return[...this._col._data].sort(e==="asc"?(t,i)=>new Date(t.createdAt)-new Date(i.createdAt):(t,i)=>new Date(i.createdAt)-new Date(t.createdAt))}}class Cn{constructor(e){this._db=e,this._restoring=!1}get _col(){return this._db.useCollection("_changelog")}async log(e,t,i,s=null,n=null){if(this._restoring)return;let r={op:e,collection:t,docId:i._id,doc:{...i},timestamp:new Date};if(s)r.prev={...s};if(n)r.session=n;await this._col.insertOne(r)}async query(e,{since:t,limit:i,session:s}={}){let n={collection:e};if(t)n.timestamp={$gte:new Date(t)};if(s)n.session=s;let r={sort:{timestamp:1}};if(i)r.limit=i;let{docs:a}=await this._col.find(n,r);return a}async restore(e,t,{_id:i}={}){let s=new Date(t),n=this._db.useCollection(e),r=(await this.query(e,{})).filter((l)=>new Date(l.timestamp)<=s);if(i){let l=r.filter((c)=>c.docId===i);if(l.length===0)return;let o=l[l.length-1];this._restoring=!0;try{if(o.op==="delete"){if(await n.findOne({_id:i}))await n.deleteOne({_id:i});return}if(await n.findOne({_id:i})){let{_id:c,createdAt:h,updatedAt:u,...d}=o.doc;await n.updateOne({_id:i},d)}else await n.insertOne({...o.doc})}finally{this._restoring=!1}await this._db.saveData(e);return}let a=new Map;for(let l of r)if(l.op==="insert"||l.op==="update")a.set(l.docId,{doc:l.doc,deleted:!1});else if(l.op==="delete")a.set(l.docId,{doc:null,deleted:!0});this._restoring=!0;try{await n.deleteMany({});for(let[,{doc:l,deleted:o}]of a)if(!o&&l)await n.insertOne({...l})}finally{this._restoring=!1}await this._db.saveData(e)}}function ll(e){let t=5381;for(let i=0;i<e.length;i++)t=(t<<5)+t+e.charCodeAt(i)|0;return(t>>>0).toString(16).padStart(8,"0")}class Rn{constructor({maxSize:e=500,ttl:t=0}={}){this._cache=new Map,this._maxSize=e,this._ttl=t}_key(e,t,i){return ll(JSON.stringify({collectionName:e,schema:t,query:i}))}get(e,t,i){let s=this._key(e,t,i),n=this._cache.get(s);if(!n)return;if(this._ttl>0&&Date.now()-n.ts>this._ttl){this._cache.delete(s);return}return n.filter}set(e,t,i,s){let n=this._key(e,t,i);if(this._cache.size>=this._maxSize&&!this._cache.has(n))this._cache.delete(this._cache.keys().next().value);this._cache.set(n,{filter:s,ts:Date.now()})}toJSON(){return Object.fromEntries([...this._cache.entries()].map(([e,t])=>[e,t]))}fromJSON(e){if(!e||typeof e!=="object")return;for(let[t,i]of Object.entries(e))this._cache.set(t,i)}get size(){return this._cache.size}}function cl(e,{regexMaxLength:t=500}={}){if(typeof e!=="object"||e===null)return e;let i={};for(let s of Object.keys(e)){let n=e[s];if(n&&typeof n==="object"&&!Array.isArray(n)){let r={};for(let a of Object.keys(n))if(a==="$regex"&&typeof n.$regex==="string"){if(n.$regex.length>t)throw Error(`$regex pattern too long (max ${t} characters)`);if(/\([^)]*[+*][^)]*\)[+*{]/.test(n.$regex))throw Error("$regex pattern rejected: nested quantifiers risk ReDoS");try{r.$regex=new RegExp(n.$regex)}catch{throw Error(`invalid $regex pattern: "${n.$regex}"`)}}else if(["$gt","$gte","$lt","$lte"].includes(a)&&hl(n[a]))r[a]=new Date(n[a]);else r[a]=n[a];i[s]=r}else i[s]=n}return i}function hl(e){if(typeof e!=="string")return!1;return/\d{4}-\d{2}-\d{2}/.test(e)&&!isNaN(Date.parse(e))}function dl(e,t){let i=[];if(!t||typeof e!=="object"||e===null)return i;for(let s of Object.keys(e)){if(s.startsWith("$"))continue;if(!(s.split(".")[0]in t))i.push(`Unknown field referenced in generated filter: "${s}"`)}return i}class kn{constructor(){this._listeners=new Map}on(e,t){if(!this._listeners.has(e))this._listeners.set(e,new Set);return this._listeners.get(e).add(t),()=>this._listeners.get(e)?.delete(t)}off(e,t){this._listeners.get(e)?.delete(t)}emit(e,t){for(let i of[e,"*"]){let s=this._listeners.get(i);if(!s)continue;for(let n of s)try{n(t)}catch(r){}}}removeAll(e){if(e)this._listeners.delete(e);else this._listeners.clear()}listenerCount(e){return this._listeners.get(e)?.size??0}}class Ln{constructor({threshold:e=100,maxEntries:t=500}={}){this._threshold=e,this._maxEntries=t,this._entries=[]}record({collection:e,op:t,filter:i,query:s,duration:n,resultCount:r}){if(n<this._threshold)return;let a={collection:e,op:t,duration:n,resultCount:r,timestamp:new Date};if(i!==void 0)a.filter=i;if(s!==void 0)a.query=s;if(this._entries.push(a),this._entries.length>this._maxEntries)this._entries.shift()}entries({limit:e,minDuration:t,collection:i}={}){let s=this._entries;if(i)s=s.filter((n)=>n.collection===i);if(t)s=s.filter((n)=>n.duration>=t);if(e)s=s.slice(-e);return s}get size(){return this._entries.length}clear(){this._entries=[]}}class Pn{constructor(){this._sessions=new Map}_ensure(e){if(!this._sessions.has(e))this._sessions.set(e,{reads:0,writes:0,lastActive:null});return this._sessions.get(e)}recordRead(e){if(!e)return;let t=this._ensure(e);t.reads++,t.lastActive=new Date}recordWrite(e){if(!e)return;let t=this._ensure(e);t.writes++,t.lastActive=new Date}get(e){let t=this._sessions.get(e);if(!t)return null;return{sessionId:e,...t}}all(){return[...this._sessions.entries()].map(([e,t])=>({sessionId:e,...t}))}clear(e){if(e)this._sessions.delete(e);else this._sessions.clear()}}class Mn{constructor(){this._plugins=[]}register(e){if(typeof e!=="object"||e===null)throw TypeError("Plugin must be a non-null object.");this._plugins.push(e)}async run(e,t){for(let i of this._plugins)if(typeof i[e]==="function")await i[e](t)}get size(){return this._plugins.length}}function Ve(e){if(typeof e!=="string"||!e.trim())throw Error("collection name must be a non-empty string");if(/[/\\]/.test(e)||e.includes("..")||e.includes("\x00"))throw Error(`invalid collection name: "${e}"`);if(e.trim().startsWith("_"))throw Error(`access to system collection "${e}" is not permitted`);return e.trim()}var vs=[{name:"skalex_collections",description:"List all collection names in the database.",inputSchema:{type:"object",properties:{}},scope:"read"},{name:"skalex_schema",description:"Return the schema for a collection as a { field: type } map. Returns null if the collection is empty.",inputSchema:{type:"object",properties:{collection:{type:"string",description:"Collection name."}},required:["collection"]},scope:"read"},{name:"skalex_find",description:"Find documents in a collection that match a filter.",inputSchema:{type:"object",properties:{collection:{type:"string",description:"Collection name."},filter:{type:"object",description:"Query filter (MongoDB-style operators supported)."},limit:{type:"number",description:"Maximum number of results. Default: 20."},sort:{type:"object",description:"Sort descriptor: { field: 1 } for ascending, { field: -1 } for descending."}},required:["collection"]},scope:"read"},{name:"skalex_insert",description:"Insert a single document into a collection.",inputSchema:{type:"object",properties:{collection:{type:"string",description:"Collection name."},doc:{type:"object",description:"Document to insert."}},required:["collection","doc"]},scope:"write"},{name:"skalex_update",description:"Update the first document matching a filter.",inputSchema:{type:"object",properties:{collection:{type:"string",description:"Collection name."},filter:{type:"object",description:"Query filter to identify the document."},update:{type:"object",description:"Fields to update (direct assignment, $inc, $push supported)."},many:{type:"boolean",description:"If true, update all matching documents. Default: false."}},required:["collection","filter","update"]},scope:"write"},{name:"skalex_delete",description:"Delete the first document matching a filter.",inputSchema:{type:"object",properties:{collection:{type:"string",description:"Collection name."},filter:{type:"object",description:"Query filter to identify the document."},many:{type:"boolean",description:"If true, delete all matching documents. Default: false."}},required:["collection","filter"]},scope:"write"},{name:"skalex_search",description:"Semantic similarity search. Requires an embedding adapter to be configured.",inputSchema:{type:"object",properties:{collection:{type:"string",description:"Collection name."},query:{type:"string",description:"Natural-language query string to embed and compare."},limit:{type:"number",description:"Maximum number of results. Default: 10."},minScore:{type:"number",description:"Minimum cosine similarity score [0, 1]. Default: 0."},filter:{type:"object",description:"Optional structured pre-filter (hybrid search)."}},required:["collection","query"]},scope:"read"},{name:"skalex_ask",description:"Translate a natural-language question into a filter and query a collection. Requires a language model adapter.",inputSchema:{type:"object",properties:{collection:{type:"string",description:"Collection name."},question:{type:"string",description:"Natural-language question about the data."},limit:{type:"number",description:"Maximum number of results. Default: 20."}},required:["collection","question"]},scope:"read"}];async function ul(e,t,i){switch(e){case"skalex_collections":return Object.keys(i.collections).filter((s)=>!s.startsWith("_"));case"skalex_schema":return i.schema(Ve(t.collection))??null;case"skalex_find":{let s=i.useCollection(Ve(t.collection)),n={};if(t.limit)n.limit=t.limit;if(t.sort)n.sort=t.sort;return s.find(t.filter||{},n)}case"skalex_insert":return i.useCollection(Ve(t.collection)).insertOne(t.doc||{});case"skalex_update":{let s=i.useCollection(Ve(t.collection));if(t.many)return s.updateMany(t.filter||{},t.update||{});return s.updateOne(t.filter||{},t.update||{})}case"skalex_delete":{let s=i.useCollection(Ve(t.collection));if(t.many)return s.deleteMany(t.filter||{});return s.deleteOne(t.filter||{})}case"skalex_search":return i.useCollection(Ve(t.collection)).search(t.query,{limit:t.limit??10,minScore:t.minScore??0,filter:t.filter});case"skalex_ask":return i.ask(Ve(t.collection),t.question,{limit:t.limit??20});default:throw Object.assign(Error(`Unknown tool: ${e}`),{code:"NOT_FOUND"})}}var Di="2.0",Ci=-32700,_l=-32600,Es=-32601,fl=-32602,pl=-32603;function st(e,t){return{jsonrpc:Di,id:e,result:t}}function Ne(e,t,i,s){return{jsonrpc:Di,id:e,error:{code:t,message:i}}}function ml(e){try{let t=JSON.parse(e);if(typeof t!=="object"||t===null||t.jsonrpc!==Di)return{parseError:Ne(null,_l,"Invalid JSON-RPC request")};return{msg:t}}catch(t){return{parseError:Ne(null,Ci,"Parse error")}}}function gl(e){return{content:[{type:"text",text:e}]}}function Ss(e){return{content:[{type:"text",text:e}],isError:!0}}class jn{constructor({port:e=3000,host:t="127.0.0.1",allowedOrigin:i=null,maxBodySize:s=1048576}={}){this._port=e,this._host=t,this._allowedOrigin=i,this._maxBodySize=s,this._clients=new Set,this._onMessage=null,this._server=null}onMessage(e){this._onMessage=e}send(e){let t=`data: ${JSON.stringify(e)}
|
|
33
|
-
|
|
34
|
-
`;for(let i of this._clients)try{i.write(t)}catch(s){this._clients.delete(i)}}start(){return new Promise((e,t)=>{this._server=fo.createServer((i,s)=>{if(this._allowedOrigin)s.setHeader("Access-Control-Allow-Origin",this._allowedOrigin),s.setHeader("Access-Control-Allow-Methods","GET, POST, OPTIONS"),s.setHeader("Access-Control-Allow-Headers","Content-Type");if(i.method==="OPTIONS"){s.writeHead(204),s.end();return}if(i.method==="GET"&&i.url==="/sse")this._handleSSE(i,s);else if(i.method==="POST"&&i.url==="/message")this._handleMessage(i,s);else s.writeHead(404,{"Content-Type":"text/plain"}),s.end("Not Found")}),this._server.on("error",t),this._server.listen(this._port,this._host,()=>e())})}stop(){return new Promise((e)=>{if(!this._server){e();return}for(let t of this._clients)try{t.end()}catch(i){}this._clients.clear(),this._server.close(()=>e()),this._server=null})}_handleSSE(e,t){t.writeHead(200,{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}),t.write(`event: endpoint
|
|
35
|
-
data: /message
|
|
36
|
-
|
|
37
|
-
`),this._clients.add(t),e.on("close",()=>{this._clients.delete(t)})}async _handleMessage(e,t){let i="";e.setEncoding("utf8"),e.on("data",(s)=>{if(i+=s,i.length>this._maxBodySize)e.destroy(),t.writeHead(413,{"Content-Type":"application/json"}),t.end(JSON.stringify({error:"Request body too large"}))}),e.on("end",async()=>{t.writeHead(202,{"Content-Type":"application/json"}),t.end("{}");let s;try{s=JSON.parse(i)}catch(n){this.send(Ne(null,Ci,"Parse error"));return}if(this._onMessage)await this._onMessage(s).catch(()=>{})})}get port(){return this._port}get host(){return this._host}get url(){return`http://${this._host}:${this._port}`}get sseUrl(){return`${this.url}/sse`}}class qn{constructor(){this._onMessage=null,this._buffer="",this._started=!1}onMessage(e){this._onMessage=e}send(e){process.stdout.write(JSON.stringify(e)+`
|
|
38
|
-
`)}start(){if(this._started)return;this._started=!0,process.stdin.setEncoding("utf8"),process.stdin.on("data",(e)=>{this._buffer+=e;let t;while((t=this._buffer.indexOf(`
|
|
39
|
-
`))!==-1){let i=this._buffer.slice(0,t).trim();if(this._buffer=this._buffer.slice(t+1),i&&this._onMessage){let s;try{s=JSON.parse(i)}catch(n){this.send(Ne(null,Ci,"Parse error"));continue}this._onMessage(s).catch(()=>{})}}}),process.stdin.on("end",()=>{process.exit(0)})}stop(){process.stdin.removeAllListeners("data"),process.stdin.removeAllListeners("end"),this._started=!1}}var yl={name:"skalex",version:"4.0.0-alpha"},wl="2024-11-05";class Kn{constructor(e,t={}){this._db=e,this._transport=t.transport||"stdio",this._port=t.port||3000,this._host=t.host||"127.0.0.1",this._allowedOrigin=t.allowedOrigin??null,this._maxBodySize=t.maxBodySize??1048576,this._scopes=t.scopes||{"*":["read"]},this._t=null}async listen(){if(this._transport==="http")this._t=new jn({port:this._port,host:this._host,allowedOrigin:this._allowedOrigin,maxBodySize:this._maxBodySize});else this._t=new qn;this._t.onMessage((e)=>this._handleMessage(e)),await this._t.start()}async connect(e){if(this._t=e,this._t.onMessage((t)=>this._handleMessage(t)),typeof this._t.start==="function")await this._t.start()}async close(){if(this._t&&typeof this._t.stop==="function")await this._t.stop();this._t=null}get transport(){return this._transport}get url(){return this._t?.url}async _handleMessage(e){let{msg:t,parseError:i}=typeof e==="string"?ml(e):{msg:e};if(i){this._send(i);return}let{id:s,method:n,params:r}=t;if(s===void 0){if(n==="notifications/initialized")return;return}try{switch(n){case"initialize":this._send(st(s,{protocolVersion:wl,capabilities:{tools:{}},serverInfo:yl}));break;case"tools/list":this._send(st(s,{tools:this._visibleTools()}));break;case"tools/call":await this._handleToolCall(s,r);break;case"ping":this._send(st(s,{}));break;default:this._send(Ne(s,Es,`Method not found: ${n}`))}}catch(a){this._send(Ne(s,pl,a.message||"Internal error"))}}async _handleToolCall(e,t){let i=t?.name,s=t?.arguments??{};if(!i){this._send(Ne(e,fl,"tools/call requires params.name"));return}let n=vs.find((a)=>a.name===i);if(!n){this._send(Ne(e,Es,`Unknown tool: ${i}`));return}let r=s.collection||s.collection_name||null;if(!this._hasScope(r,n.scope)){this._send(st(e,Ss(`Access denied: "${i}" requires "${n.scope}" scope on collection "${r}".`)));return}try{let a=await ul(i,s,this._db);this._send(st(e,gl(JSON.stringify(a,null,2))))}catch(a){this._send(st(e,Ss(a.message||String(a))))}}_visibleTools(){return vs.filter((e)=>{let t=this._scopes["*"];if(t&&(t.includes(e.scope)||t.includes("admin")))return!0;for(let[,i]of Object.entries(this._scopes))if(i.includes(e.scope)||i.includes("admin"))return!0;return!1})}_hasScope(e,t){let i=(s)=>s.includes("admin")||s.includes(t);if(e&&this._scopes[e])return i(this._scopes[e]);if(this._scopes["*"])return i(this._scopes["*"]);return!1}_send(e){this._t?.send(e)}}var St="migrations",Is=(e)=>JSON.stringify(e,function(t,i){let s=this[t];if(s instanceof Date)return{__skalex_date__:s.toISOString()};if(typeof i==="bigint")return{__skalex_bigint__:i.toString()};return i}),bs=(e)=>JSON.parse(e,(t,i)=>{if(i&&typeof i==="object"){if("__skalex_bigint__"in i)return BigInt(i.__skalex_bigint__);if("__skalex_date__"in i)return new Date(i.__skalex_date__)}return i});class ee{constructor({path:e="./.db",format:t="gz",debug:i=!1,adapter:s,ai:n,encrypt:r,slowQueryLog:a,queryCache:l,memory:o,logger:c,plugins:h,llmAdapter:u,embeddingAdapter:d,regexMaxLength:_,idGenerator:f,serializer:p,deserializer:m,autoSave:w,ttlSweepInterval:v,lenientLoad:R}={}){if(this.dataDirectory=e,this.dataFormat=t,this.debug=i,!ee._errorsAttached)ee.SkalexError=Me,ee.ValidationError=N,ee.UniqueConstraintError=xe,ee.TransactionError=me,ee.PersistenceError=Re,ee.AdapterError=ue,ee.QueryError=Qe,ee._errorsAttached=!0;this._adapterConfig=s??null;let E=s||new An({dir:e,format:t});if(r)E=new $n(E,r.key);if(this.fs=E,this._registry=new Sn(wn),this.collections=this._registry.stores,this._collectionInstances=this._registry._instances,this._migrations=new xn,this._connectPromise=null,this.isConnected=!1,this._aiConfig=n||null,this._encryptConfig=r||null,this._pluginsConfig=Array.isArray(h)?h:null,this._memoryConfig=o||null,this._regexMaxLength=_??500,this._idGenerator=f??null,this._serializer=p??Is,this._deserializer=m??bs,this._autoSave=w??!1,this._txManager=new En,this._ttlSweepInterval=v??0,this._ttlTimer=null,this._logger=c??ps,this._embeddingAdapter=d??(n?el(n):null),this._aiAdapter=u??(n?tl(n):null),this._persistence=new vn({adapter:this.fs,serializer:this._serializer,deserializer:this._deserializer,logger:this._logger,debug:this.debug,lenientLoad:R??!1}),this._changeLog=new Cn(this),this._queryCache=new Rn(l||{}),this._eventBus=new kn,this._queryLog=a?new Ln(a):null,this._sessionStats=new Pn,this._plugins=new Mn,Array.isArray(h))for(let A of h)this._plugins.register(A)}async connect(){if(this._connectPromise)return this._connectPromise;return this._connectPromise=this._doConnect(),this._connectPromise}async _doConnect(){try{await this.loadData();let e=this._getMeta();if(e.queryCache)this._queryCache.fromJSON(e.queryCache);if(this._migrations._migrations.length>0){let t=e.appliedVersions||[],i=await this._migrations.run((s)=>this.useCollection(`_migration_${s}`),t);this._saveMeta({appliedVersions:i})}if(this._sweepTtl(),this._ttlSweepInterval>0){if(this._ttlTimer=setInterval(()=>this._sweepTtl(),this._ttlSweepInterval),this._ttlTimer?.unref)this._ttlTimer.unref()}this.isConnected=!0,this._log("> - Connected to the database (\u221A)")}catch(e){throw this._logger(`Error connecting to the database: ${e}`,"error"),e}}async disconnect(){try{if(this._ttlTimer)clearInterval(this._ttlTimer),this._ttlTimer=null;await this.saveData(),this._registry.clear(),this.collections=this._registry.stores,this._collectionInstances=this._registry._instances,this._connectPromise=null,this.isConnected=!1,this._log("> - Disconnected from the database (\u221A)")}catch(e){throw this._logger(`Error disconnecting from the database: ${e}`,"error"),e}}async _ensureConnected(){if(this.isConnected)return;return this.connect()}useCollection(e){return this._registry.get(e,this)}createCollection(e,t={}){return this._registry.create(e,t,this)}_createCollectionStore(e,t={}){this._registry.createStore(e,t)}async renameCollection(e,t){this._registry.rename(e,t),await this.saveData(t),await this.fs.delete(e)}async loadData(){await this._persistence.loadAll(this.collections,{parseSchema:pn,buildIndex:this._registry.buildIndex,IndexEngine:kt});for(let e in this._collectionInstances){let t=this.collections[e];if(t&&this._collectionInstances[e]._store!==t)this._collectionInstances[e]._store=t}}async saveData(e){await this._persistence.save(this.collections,e)}buildIndex(e,t){return this._registry.buildIndex(e,t)}addMigration(e){this._migrations.add(e)}migrationStatus(){let e=this._getMeta();return this._migrations.status(e.appliedVersions||[])}namespace(e){if(this._adapterConfig)throw new ue("ERR_SKALEX_ADAPTER_NAMESPACE_REQUIRES_FS","namespace() requires the default FsAdapter. When a custom storage adapter is configured, create a separate Skalex instance with your adapter instead.");let t=String(e).replace(/[^a-zA-Z0-9_-]/g,"_");if(!t)throw new N("ERR_SKALEX_VALIDATION_NAMESPACE_ID","namespace: id must contain at least one alphanumeric character",{id:e});return new ee({path:`${this.dataDirectory}/${t}`,format:this.dataFormat,debug:this.debug,ai:this._aiConfig||void 0,encrypt:this._encryptConfig||void 0,slowQueryLog:this._queryLog?{threshold:this._queryLog._threshold,maxEntries:this._queryLog._maxEntries}:void 0,queryCache:this._queryCache?{maxSize:this._queryCache._maxSize,ttl:this._queryCache._ttl}:void 0,plugins:this._pluginsConfig||void 0,memory:this._memoryConfig||void 0,logger:this._logger!==ps?this._logger:void 0,llmAdapter:this._aiAdapter&&!this._aiConfig?this._aiAdapter:void 0,embeddingAdapter:this._embeddingAdapter&&!this._aiConfig?this._embeddingAdapter:void 0,regexMaxLength:this._regexMaxLength!==500?this._regexMaxLength:void 0,idGenerator:this._idGenerator||void 0,serializer:this._serializer!==Is?this._serializer:void 0,deserializer:this._deserializer!==bs?this._deserializer:void 0,autoSave:this._autoSave||void 0,ttlSweepInterval:this._ttlSweepInterval||void 0})}use(e){this._plugins.register(e)}watch(e){return this._eventBus.on("*",e)}sessionStats(e){if(e!==void 0)return this._sessionStats.get(e);return this._sessionStats.all()}get _inTransaction(){return this._txManager.active}_emitEvent(e,t){if(!this._txManager.defer(()=>this._eventBus.emit(e,t)))this._eventBus.emit(e,t)}async _runAfterHook(e,t){if(!this._txManager.defer(()=>this._plugins.run(e,t)))await this._plugins.run(e,t)}async _logChange(e,t,i,s,n){if(!this._txManager.defer(()=>this._changeLog.log(e,t,i,s,n)))await this._changeLog.log(e,t,i,s,n)}async transaction(e,t={}){return this._txManager.run(e,this,t)}async seed(e,{reset:t=!1}={}){for(let[i,s]of Object.entries(e)){if(t&&this.collections[i])this._applySnapshot(i,{data:[],index:new Map}),delete this._collectionInstances[i];await this.useCollection(i).insertMany(s)}await this.saveData()}dump(){return this._registry.dump()}inspect(e){return this._registry.inspect(e)}async import(e){let t=await this.fs.readRaw(e),i;try{i=JSON.parse(t)}catch{throw new Re("ERR_SKALEX_PERSISTENCE_INVALID_JSON",`import: invalid JSON in file "${e}"`,{filePath:e})}let s=e.split("/").pop().replace(/\.[^.]+$/,"");return this.useCollection(s).insertMany(Array.isArray(i)?i:[i],{save:!0})}async embed(e){if(!this._embeddingAdapter)throw new ue("ERR_SKALEX_ADAPTER_EMBEDDING_REQUIRED","db.embed() requires an AI adapter. Pass { ai: { provider, apiKey } } to the Skalex constructor.");return this._embeddingAdapter.embed(e)}async ask(e,t,{limit:i=20}={}){if(!this._aiAdapter)throw new ue("ERR_SKALEX_ADAPTER_LLM_REQUIRED",'db.ask() requires a language model adapter. Configure { ai: { provider, model: "..." } }.');let s=this.useCollection(e),n=this.schema(e),r=this._queryCache.get(e,n,t);if(!r){r=await this._aiAdapter.generate(n,t);let a=dl(r,n);if(a.length)a.forEach((l)=>this._log(`[ask] ${l}`));this._queryCache.set(e,n,t,r),this._saveMeta({queryCache:this._queryCache.toJSON()})}return s.find(cl(r,{regexMaxLength:this._regexMaxLength}),{limit:i})}schema(e){return this._registry.schema(e)}useMemory(e){return new Dn(e,this)}changelog(){return this._changeLog}async restore(e,t,i={}){return this._changeLog.restore(e,t,i)}stats(e){return this._registry.stats(e)}slowQueries(e={}){if(!this._queryLog)return[];return this._queryLog.entries(e)}slowQueryCount(){return this._queryLog?this._queryLog.size:0}clearSlowQueries(){this._queryLog?.clear()}mcp(e={}){return new Kn(this,e)}_getMeta(){let e=this.collections._meta;if(!e)return{};return e.index.get(St)||{}}_saveMeta(e){if(!this.collections._meta)this._createCollectionStore("_meta");let t=this.collections._meta,i=t.index.get(St);if(i)Object.assign(i,e);else{let s={_id:St,...e};t.data.push(s),t.index.set(St,s)}this._persistence.markDirty(this.collections,"_meta")}_sweepTtl(){for(let e in this.collections){let t=this.collections[e],i=Vo(t.data,t.index,t.fieldIndex?(s)=>t.fieldIndex.remove(s):null);if(i>0)this._persistence.markDirty(this.collections,e),this._log(`TTL sweep: removed ${i} expired docs from "${e}"`)}}_buildCollectionContext(){let e=this;return{ensureConnected:()=>e._ensureConnected(),get txManager(){return e._txManager},get plugins(){return e._plugins},get eventBus(){return e._eventBus},get sessionStats(){return e._sessionStats},get queryLog(){return e._queryLog},get logger(){return e._logger},get persistence(){return e._persistence},get collections(){return e.collections},embed:(t)=>e.embed(t),get idGenerator(){return e._idGenerator},get autoSave(){return e._autoSave},saveCollection:(t)=>e.saveData(t),snapshotCollection:(t)=>e._snapshotCollection(t),getCollection:(t)=>e.useCollection(t),emitEvent:(t,i)=>e._emitEvent(t,i),runAfterHook:(t,i)=>e._runAfterHook(t,i),logChange:(t,i,s,n,r)=>e._logChange(t,i,s,n,r),get fs(){return e.fs},get dataDirectory(){return e.dataDirectory}}}_log(e){if(this.debug)this._logger(e,"info")}_snapshotCollection(e){return{data:structuredClone(e.data)}}_applySnapshot(e,t){let i=this.collections[e];if(!i)return;if(i.data=t.data,i.index=this.buildIndex(t.data,"_id"),i.fieldIndex)i.fieldIndex.buildFromData(t.data)}}function Al(){let e=Date.now().toString(16),t;try{t=vo.randomBytes(8).toString("hex")}catch{let i=new Uint8Array(8);crypto.getRandomValues(i),t=Array.from(i).map((s)=>s.toString(16).padStart(2,"0")).join("")}return`${e}${t}`}function Ts(e,t){let i=e instanceof Error?e.message:e;if(t==="error")console.error(i);else console.log(i)}var Os=new Set(["__proto__","constructor","prototype"]);function Kt(e,t){if(!t.includes(".")){if(Os.has(t))return;return e[t]}let i=e;for(let s of t.split(".")){if(Os.has(s))return;if(i==null)return;i=i[s]}return i}class je extends Error{constructor(e,t,i={}){super(t);this.name=this.constructor.name,this.code=e,this.details=i}}class $ extends je{}class Ee extends je{}class ge extends je{}class ke extends je{}class _e extends je{}class Ge extends je{}function Ai(e,t){if(e===t)return!0;if(e==null||t==null)return!1;if(typeof e!==typeof t)return!1;if(e instanceof Date&&t instanceof Date)return e.getTime()===t.getTime();if(e instanceof RegExp&&t instanceof RegExp)return e.source===t.source&&e.flags===t.flags;if(Array.isArray(e)){if(!Array.isArray(t)||e.length!==t.length)return!1;for(let i=0;i<e.length;i++)if(!Ai(e[i],t[i]))return!1;return!0}if(typeof e==="object"){if(Array.isArray(t))return!1;let i=Object.keys(e);if(i.length!==Object.keys(t).length)return!1;for(let s of i){if(!Object.prototype.hasOwnProperty.call(t,s))return!1;if(!Ai(e[s],t[s]))return!1}return!0}return!1}function V(e,t){if(typeof t==="function")return t(e);if(t==null)return!0;if(typeof t==="object"&&Object.keys(t).length===0)return!0;if("$or"in t){let i=t.$or;if(!Array.isArray(i))throw new Ge("ERR_SKALEX_QUERY_INVALID_OPERATOR","$or must be an array of filters",{operator:"$or"});if(!i.some((s)=>V(e,s)))return!1}if("$and"in t){let i=t.$and;if(!Array.isArray(i))throw new Ge("ERR_SKALEX_QUERY_INVALID_OPERATOR","$and must be an array of filters",{operator:"$and"});if(!i.every((s)=>V(e,s)))return!1}if("$not"in t){if(V(e,t.$not))return!1}for(let i in t){if(i==="$or"||i==="$and"||i==="$not")continue;let s=t[i],n;try{n=Kt(e,i)}catch{return!1}if(s instanceof RegExp){if(!s.test(String(n)))return!1}else if(typeof s==="object"&&s!==null){if(Object.keys(s).some((r)=>r.startsWith("$"))){if("$eq"in s&&n!==s.$eq)return!1;if("$ne"in s&&n===s.$ne)return!1;if("$gt"in s&&!(n>s.$gt))return!1;if("$lt"in s&&!(n<s.$lt))return!1;if("$gte"in s&&!(n>=s.$gte))return!1;if("$lte"in s&&!(n<=s.$lte))return!1;if("$in"in s&&!s.$in.includes(n))return!1;if("$nin"in s&&s.$nin.includes(n))return!1;if("$regex"in s){if(!(s.$regex instanceof RegExp?s.$regex:new RegExp(s.$regex)).test(String(n)))return!1}if("$fn"in s&&!s.$fn(n))return!1}else if(!Ai(n,s))return!1}else if(n!==s)return!1}return!0}function xl(e,t=new Set){if(typeof e!=="object"||e===null||typeof e==="function")return e;let i=[],s=[],n=[],r=[],a=[];for(let o in e){if(o==="$or"||o==="$and"||o==="$not"){a.push(o);continue}let c=e[o];if(t.has(o))i.push(o);else if(c instanceof RegExp||typeof c==="object"&&c!==null&&(("$regex"in c)||("$fn"in c))||typeof c==="function")r.push(o);else if(typeof c==="object"&&c!==null&&(("$gt"in c)||("$lt"in c)||("$gte"in c)||("$lte"in c)||("$ne"in c)||("$in"in c)||("$nin"in c)))n.push(o);else s.push(o)}let l={};for(let o of[...i,...s,...n,...r,...a])l[o]=e[o];return l}var xi=new Set(["string","number","boolean","object","array","date","any"]);function Ri(e){if(Array.isArray(e))return"array";if(e instanceof Date)return"date";return typeof e}function Fn(e){let t=new Map,i=[];for(let[s,n]of Object.entries(e)){let r;if(typeof n==="string"){if(!xi.has(n))throw new $("ERR_SKALEX_VALIDATION_UNKNOWN_TYPE",`Unknown schema type "${n}" for field "${s}"`,{field:s,type:n});r={type:n,required:!1,unique:!1}}else if(typeof n==="object"&&n!==null){let{type:a="any",required:l=!1,unique:o=!1,enum:c}=n;if(!xi.has(a))throw new $("ERR_SKALEX_VALIDATION_UNKNOWN_TYPE",`Unknown schema type "${a}" for field "${s}"`,{field:s,type:a});if(r={type:a,required:l,unique:o,enum:c},o)i.push(s)}else throw new $("ERR_SKALEX_VALIDATION_INVALID_SCHEMA",`Invalid schema definition for field "${s}"`,{field:s});t.set(s,r)}return{fields:t,uniqueFields:i}}function Ns(e,t,i=!1){let s=[];for(let[n,r]of t){let a=e[n],l=a===void 0||a===null;if(r.required&&l){s.push(`Field "${n}" is required`);continue}if(l)continue;if(r.type!=="any"){let o=Ri(a);if(o!==r.type)s.push(`Field "${n}" must be of type "${r.type}", got "${o}"`)}if(r.enum&&!r.enum.includes(a))s.push(`Field "${n}" must be one of [${r.enum.map((o)=>JSON.stringify(o)).join(", ")}], got ${JSON.stringify(a)}`)}if(i){for(let n of Object.keys(e))if(!n.startsWith("_")&&!t.has(n))s.push(`Unknown field "${n}" (strict mode)`)}return s}function $s(e,t){let i={};for(let[s,n]of Object.entries(e)){if(s.startsWith("_")){i[s]=n;continue}if(!t.has(s))continue;let r=t.get(s);if(r.type!=="any"){if(Ri(n)!==r.type)continue}if(r.enum&&!r.enum.includes(n))continue;i[s]=n}return i}function vl(e){let t={};for(let[i,s]of Object.entries(e)){if(i.startsWith("_"))continue;let n=Ri(s);t[i]=xi.has(n)?n:"any"}return t}function El(e){if(typeof e==="number"){if(e<=0)throw new $("ERR_SKALEX_VALIDATION_TTL",`TTL must be positive, got ${e}`,{ttl:e});return e*1000}if(typeof e!=="string")throw new $("ERR_SKALEX_VALIDATION_TTL",`Invalid TTL value: ${e}`,{ttl:e});let t=e.match(/^(\d+(?:\.\d+)?)(ms|s|m|h|d)$/);if(!t)throw new $("ERR_SKALEX_VALIDATION_TTL_FORMAT",`Invalid TTL format: "${e}". Use e.g. 300 (seconds), "30m", "24h", "7d"`,{ttl:e});let i=parseFloat(t[1]);if(i<=0)throw new $("ERR_SKALEX_VALIDATION_TTL",`TTL must be positive, got "${e}"`,{ttl:e});let s=t[2],n=i*{ms:1,s:1000,m:60000,h:3600000,d:86400000}[s];if(!isFinite(n))throw new $("ERR_SKALEX_VALIDATION_TTL",`TTL value "${e}" is too large`,{ttl:e});return n}function Sl(e){return new Date(Date.now()+El(e))}function Il(e,t,i=null){let s=Date.now(),n=0,r=e.length;while(r--){let a=e[r];if(a._expiresAt&&new Date(a._expiresAt).getTime()<=s){if(e.splice(r,1),t.delete(a._id),i)i(a);n++}}return n}function Ds(e,t){if(e.length!==t.length)throw new Ge("ERR_SKALEX_QUERY_VECTOR_MISMATCH",`Vector dimension mismatch: ${e.length} vs ${t.length}`,{expected:e.length,got:t.length});let i=0,s=0,n=0;for(let a=0;a<e.length;a++)i+=e[a]*t[a],s+=e[a]*e[a],n+=t[a]*t[a];let r=Math.sqrt(s*n);return r===0?0:i/r}function z(e){if(!("_vector"in e))return{...e};let{_vector:t,...i}=e;return i}function bl(e){return e.length}function Tl(e,t){let i=0;for(let s of e){let n=Kt(s,t);if(typeof n==="number"&&!isNaN(n))i+=n}return i}function Ol(e,t){let i=0,s=0;for(let n of e){let r=Kt(n,t);if(typeof r==="number"&&!isNaN(r))i+=r,s++}return s===0?null:i/s}function Nl(e,t){let i=Object.create(null);for(let s of e){let n=String(Kt(s,t)??"__null__");if(!i[n])i[n]=[];i[n].push(s)}return i}class Un{constructor(e){this._col=e}get _ctx(){return this._col._ctx}async execute({op:e,beforeHook:t,afterHook:i,hookPayload:s,mutate:n,afterHookPayload:r,save:a,session:l}){let o=this._ctx;await o.ensureConnected(),this._col._txSnapshotIfNeeded();let c=o.txManager,h=c.active&&this._col._activeTxId===c.context?.id,u=h?c.context.id:null,d=this._col._createdInTxId,_=()=>{if(u!==null&&c._abortedIds.has(u))throw new ge("ERR_SKALEX_TX_ABORTED",`Transaction ${u} was aborted. No further mutations allowed.`);if(d!==null&&c._abortedIds.has(d))throw new ge("ERR_SKALEX_TX_ABORTED",`Transaction ${d} was aborted. Collection obtained inside that transaction cannot be used for further mutations.`)};if(h||d!==null)_();if(t)await o.plugins.run(t,s);let{docs:f,prevDocs:p=[]}=await n(_);if(o.persistence.markDirty(o.collections,this._col.name),await this._col._saveIfNeeded(a),this._col._changelogEnabled)for(let m=0;m<f.length;m++)await o.logChange(e,this._col.name,f[m],p[m]??null,l||null);if(!h||!c.defer(()=>o.sessionStats.recordWrite(l)))o.sessionStats.recordWrite(l);for(let m of f)o.emitEvent(this._col.name,{op:e,collection:this._col.name,doc:z(m)});if(i)if(r)await o.runAfterHook(i,r(f));else for(let m of f)await o.runAfterHook(i,{collection:this._col.name,doc:z(m)});return{docs:f,prevDocs:p}}}function $l(e){let t={};for(let i in e){if(i.startsWith("$"))continue;let s=e[i];if(s&&typeof s==="object"&&!Array.isArray(s)){let n=Object.keys(s);if(n.length===1&&n[0]==="$eq")t[i]=s.$eq}else t[i]=s}return t}var zn=new Set(["__proto__","constructor","prototype"]);function Bn(e){if(typeof e!=="object"||e===null||Array.isArray(e))return e;let t={};for(let i of Object.keys(e)){if(zn.has(i))continue;t[i]=Bn(e[i])}return t}class Xn{constructor(e,t){this.name=e.collectionName,this.database=t,this._ctx=t._buildCollectionContext(),this._store=e,this._pipeline=new Un(this),this._createdInTxId=t._txManager.context?.id??null,this._activeTxId=null}get _data(){return this._store.data}set _data(e){this._store.data=e}get _index(){return this._store.index}get _fieldIndex(){return this._store.fieldIndex||null}get _schema(){return this._store.schema?this._store.schema.fields:null}get _changelogEnabled(){return this._store.changelog===!0}get _softDelete(){return this._store.softDelete===!0}get _versioning(){return this._store.versioning===!0}get _strict(){return this._store.strict===!0}get _onSchemaError(){return this._store.onSchemaError??"throw"}get _defaultTtl(){return this._store.defaultTtl||null}get _defaultEmbed(){return this._store.defaultEmbed||null}get _maxDocs(){return this._store.maxDocs||null}async insertOne(e,t={}){let{save:i,ifNotExists:s,ttl:n,embed:r,session:a}=t;if(s){await this._ctx.ensureConnected();let o=this._findRaw(e);if(o)return z({...o})}let{docs:l}=await this._insertCore([e],{ttl:n,embed:r,session:a,save:i});return z(l[0])}async insertMany(e,t={}){let{save:i,ttl:s,embed:n,session:r}=t,{docs:a}=await this._insertCore(e,{ttl:s,embed:n,session:r,save:i});return a.map(z)}async _insertCore(e,{ttl:t,embed:i,session:s,save:n}){return this._pipeline.execute({op:"insert",beforeHook:null,afterHook:"afterInsert",hookPayload:null,save:n,session:s,mutate:async(r)=>{let a=[],l=new Set;for(let o of e){let c=this._applyValidation(o);await this._ctx.plugins.run("beforeInsert",{collection:this.name,doc:c});let h=await this._buildDoc(c,{ttl:t,embed:i});if(this._index.has(h._id)||l.has(h._id))throw new Ee("ERR_SKALEX_UNIQUE_DUPLICATE_ID",`Duplicate _id "${h._id}" in collection "${this.name}"`,{id:h._id,collection:this.name});l.add(h._id),a.push(h)}if(r(),this._fieldIndex)this._fieldIndex.assertUniqueBatch(a);for(let o of a)this._addToIndex(o);this._data.push(...a);for(let o of a)this._index.set(o._id,o);return this._enforceCapAfterInsert(),{docs:a}}})}async updateOne(e,t,i={}){await this._ctx.ensureConnected();let s=this._findRaw(e);if(!s)return null;let{save:n,session:r}=i,{docs:a}=await this._updateCore([s],e,t,{save:n,session:r});return z(a[0])}async updateMany(e,t,i={}){await this._ctx.ensureConnected();let s=this._findAllRaw(e);if(s.length===0)return[];let{save:n,session:r}=i,{docs:a}=await this._updateCore(s,e,t,{save:n,session:r});return a.map(z)}async _updateCore(e,t,i,{save:s,session:n}){return this._pipeline.execute({op:"update",beforeHook:"beforeUpdate",afterHook:"afterUpdate",hookPayload:{collection:this.name,filter:t,update:i},save:s,session:n,afterHookPayload:(r)=>({collection:this.name,filter:t,update:i,result:r.length===1?r[0]:r}),mutate:async(r)=>{let a=this._changelogEnabled?e.map((h)=>structuredClone(h)):e.map(()=>null),l=e.map((h)=>this._prepareUpdatedDoc(h,i));this._assertUniqueCandidates(e,l),r();let o=new Set(e),c=new Map;for(let h=0;h<this._data.length;h++)if(o.has(this._data[h]))c.set(this._data[h],h);for(let h=0;h<e.length;h++)this._commitUpdatedDoc(e[h],l[h],c);return{docs:l,prevDocs:a}}})}applyUpdate(e,t){for(let i in t){if(zn.has(i))continue;if(i==="_id"||i==="createdAt")continue;let s=t[i];if(Array.isArray(s))e[i]=s;else if(typeof s==="object"&&s!==null){let n=Object.keys(s);if(n.some((r)=>r.startsWith("$"))){for(let r of n)if(r==="$inc"&&typeof e[i]==="number")e[i]+=s[r];else if(r==="$push"){if(!Array.isArray(e[i]))e[i]=[];e[i].push(s[r])}}else e[i]=Bn(s)}else e[i]=s}if(e.updatedAt=new Date,this._versioning)e._version=(e._version??0)+1;return e}_prepareUpdatedDoc(e,t){let i=structuredClone(e),s=structuredClone(e);if(this.applyUpdate(s,t),!this._schema)return s;let n=Ns(s,this._schema,this._strict);if(!n.length)return s;switch(this._onSchemaError){case"throw":throw new $("ERR_SKALEX_VALIDATION_UPDATE",`Update validation failed on doc "${e._id}": ${n.join("; ")}`,{id:e._id,errors:n});case"strip":return this._stripCandidateToValid(s,i);default:return this._ctx.logger(`[${this.name}] Update validation warning: ${n.join("; ")}`,"warn"),s}}_stripCandidateToValid(e,t){let i=$s(e,this._schema),s=structuredClone(t);for(let[n,r]of Object.entries(i)){if(n.startsWith("_"))continue;s[n]=r}if(s.updatedAt=e.updatedAt,this._versioning)s._version=e._version;return s}_commitUpdatedDoc(e,t,i){let s=i?i.get(e):this._data.indexOf(e);if(s===void 0||s===-1)throw new ke("ERR_SKALEX_PERSISTENCE_DOC_MISSING",`Document "${e._id}" no longer exists in collection "${this.name}"`,{id:e._id,collection:this.name});this._data[s]=t,this._index.set(t._id,t),this._updateInIndex(e,t)}_assertUniqueCandidates(e,t){this._fieldIndex?.assertUniqueCandidates(e,t)}async upsert(e,t,i={}){if(await this._ctx.ensureConnected(),this._findRaw(e))return this.updateOne(e,t,i);return this.insertOne({...$l(e),...t},i)}async upsertMany(e,t,i={}){await this._ctx.ensureConnected();let{save:s,...n}=i,r=[];for(let a of e)r.push(await this.upsert({[t]:a[t]},a,{...n,save:!1}));return await this._saveIfNeeded(s),r}async restore(e,t={}){if(!this._softDelete)throw new Ge("ERR_SKALEX_QUERY_SOFT_DELETE_REQUIRED",`restore() requires softDelete on "${this.name}"`,{collection:this.name});await this._ctx.ensureConnected();let{save:i,session:s}=t,n=this._findRaw(e,{includeDeleted:!0});if(!n||!n._deletedAt)return null;let{docs:r}=await this._pipeline.execute({op:"restore",beforeHook:null,afterHook:null,hookPayload:null,save:i,session:s,mutate:async(a)=>{return a(),delete n._deletedAt,n.updatedAt=new Date,this._index.set(n._id,n),{docs:[n]}}});return z(r[0])}watch(e,t){if(typeof e==="function")t=e,e=null;if(t)return this._ctx.eventBus.on(this.name,(i)=>{if(!e||V(i.doc,e))t(i)});return this._watchIterator(e)}_watchIterator(e){let t=[],i=null,s=!1,n=this._ctx.eventBus.on(this.name,(r)=>{if(e&&!V(r.doc,e))return;if(i){let a=i;i=null,a({value:r,done:!1})}else t.push(r)});return{[Symbol.asyncIterator](){return this},next(){if(t.length>0)return Promise.resolve({value:t.shift(),done:!1});if(s)return Promise.resolve({value:void 0,done:!0});return new Promise((r)=>{i=r})},return(){if(s=!0,n(),i){let r=i;i=null,r({value:void 0,done:!0})}return Promise.resolve({value:void 0,done:!0})}}}async count(e={}){return await this._ctx.ensureConnected(),bl(this._findAllRaw(e))}async sum(e,t={}){return await this._ctx.ensureConnected(),Tl(this._findAllRaw(t),e)}async avg(e,t={}){return await this._ctx.ensureConnected(),Ol(this._findAllRaw(t),e)}async groupBy(e,t={}){return await this._ctx.ensureConnected(),Nl(this._findAllRaw(t),e)}async findOne(e,t={}){await this._ctx.ensureConnected();let{populate:i,select:s,includeDeleted:n=!1}=t,r=this._findRaw(e,{includeDeleted:n});if(!r)return null;let a=this._projectDoc(r,s);if(i)await this._populateDoc(a,r,i);return a}async find(e,t={}){await this._ctx.ensureConnected();let i=Date.now(),{populate:s,select:n,sort:r,page:a=1,limit:l,session:o,includeDeleted:c=!1}=t;await this._ctx.plugins.run("beforeFind",{collection:this.name,filter:e,options:t});let h=this._getCandidates(e),u=xl(e,this._fieldIndex?this._fieldIndex.indexedFields:new Set),d=[];for(let f of h){if(this._softDelete&&f._deletedAt&&!c)continue;if(!V(f,u))continue;let p=this._projectDoc(f,n);if(s)await this._populateDoc(p,f,s);d.push(p)}if(r){let f=Object.keys(r);d.sort((p,m)=>{for(let w of f){let v=r[w];if(p[w]<m[w])return-v;if(p[w]>m[w])return v}return 0})}let _;if(l){let f=d.length,p=Math.ceil(f/l),m=(a-1)*l;d=d.slice(m,m+l),_={page:a,totalDocs:f,totalPages:p}}return this._ctx.queryLog?.record({collection:this.name,op:"find",filter:e,duration:Date.now()-i,resultCount:d.length}),this._ctx.sessionStats.recordRead(o),await this._ctx.plugins.run("afterFind",{collection:this.name,filter:e,options:t,docs:d}),_?{docs:d,..._}:{docs:d}}async search(e,{filter:t,limit:i=10,minScore:s=0,session:n}={}){await this._ctx.ensureConnected();let r=Date.now();await this._ctx.plugins.run("beforeSearch",{collection:this.name,query:e,options:{filter:t,limit:i,minScore:s}});let a=await this._ctx.embed(e),l=t?this._findAllRaw(t):this._data,o=[];for(let d of l){if(!d._vector)continue;let _=Ds(a,d._vector);if(_>=s)o.push({doc:d,score:_})}o.sort((d,_)=>_.score-d.score);let c=o.slice(0,i);this._ctx.queryLog?.record({collection:this.name,op:"search",query:e,duration:Date.now()-r,resultCount:c.length}),this._ctx.sessionStats.recordRead(n);let h=c.map((d)=>z(d.doc)),u=c.map((d)=>d.score);return await this._ctx.plugins.run("afterSearch",{collection:this.name,query:e,options:{filter:t,limit:i,minScore:s},docs:h,scores:u}),{docs:h,scores:u}}async similar(e,{limit:t=10,minScore:i=0}={}){await this._ctx.ensureConnected();let s=this._index.get(e);if(!s||!s._vector)return{docs:[],scores:[]};let n=[];for(let a of this._data){if(a._id===e||!a._vector)continue;let l=Ds(s._vector,a._vector);if(l>=i)n.push({doc:a,score:l})}n.sort((a,l)=>l.score-a.score);let r=n.slice(0,t);return{docs:r.map((a)=>z(a.doc)),scores:r.map((a)=>a.score)}}async deleteOne(e,t={}){await this._ctx.ensureConnected();let{save:i,session:s}=t;if(this._softDelete){let r=this._findRaw(e);if(!r)return null;let{docs:a}=await this._deleteCore("soft",[r],e,{save:i,session:s});return z(a[0])}let{docs:n}=await this._deleteCore("hard",null,e,{save:i,session:s});if(n.length===0)return null;return z(n[0])}async deleteMany(e,t={}){await this._ctx.ensureConnected();let{save:i,session:s}=t;if(this._softDelete){let r=this._findAllRaw(e);if(r.length===0)return[];let{docs:a}=await this._deleteCore("soft",r,e,{save:i,session:s});return a.map(z)}let{docs:n}=await this._deleteCore("hardMany",null,e,{save:i,session:s});return n.map(z)}async _deleteCore(e,t,i,{save:s,session:n}){return this._pipeline.execute({op:"delete",beforeHook:"beforeDelete",afterHook:"afterDelete",hookPayload:{collection:this.name,filter:i},save:s,session:n,afterHookPayload:(r)=>({collection:this.name,filter:i,result:r.length===1?r[0]:r}),mutate:async(r)=>{if(r(),e==="soft"){let o=new Date;for(let c of t)c._deletedAt=o,c.updatedAt=o,this._index.set(c._id,c);return{docs:t}}if(e==="hard"){let o=this._findIndex(i);if(o===-1)return{docs:[]};let c=this._data.splice(o,1)[0];return this._index.delete(c._id),this._removeFromIndex(c),{docs:[c]}}let a=[],l=[];for(let o of this._data)if(V(o,i))a.push(o),this._index.delete(o._id),this._removeFromIndex(o);else l.push(o);return this._data=l,{docs:a}}})}async export(e={},t={}){let{dir:i,name:s,format:n="json"}=t;try{let r=this._data.filter((h)=>V(h,e));if(r.length===0)throw new Ge("ERR_SKALEX_QUERY_EXPORT_EMPTY",`export(): no documents matched the filter in "${this.name}"`,{collection:this.name});let a;if(n==="json")a=JSON.stringify(r,null,2);else{let h=(_)=>{if(_==null)return"";let f=typeof _==="object"?JSON.stringify(_):String(_);return f.includes(",")||f.includes('"')||f.includes(`
|
|
40
|
-
`)||f.includes("\r")?`"${f.replace(/"/g,'""')}"`:f},u=Object.keys(r[0]).map(h).join(","),d=r.map((_)=>Object.values(_).map(h).join(","));a=[u,...d].join(`
|
|
41
|
-
`)}if(typeof this._ctx.fs.writeRaw!=="function")throw new _e("ERR_SKALEX_ADAPTER_NO_RAW_WRITE","export() requires a file-system adapter (FsAdapter). The current adapter does not support raw file writes.");let l=i||`${this._ctx.dataDirectory}/exports`,o=`${s||this.name}.${n}`,c=this._ctx.fs.join(l,o);this._ctx.fs.ensureDir(l),await this._ctx.fs.writeRaw(c,a)}catch(r){throw this._ctx.logger(`Error exporting "${this.name}": ${r.message}`,"error"),r}}_txSnapshotIfNeeded(){let e=this._ctx.txManager;if(!e.active)return;if(this._activeTxId!==e.context?.id)return;e.snapshotIfNeeded(this.name,this._store,(t)=>this._ctx.snapshotCollection(t))}async _saveIfNeeded(e){let t=this._ctx.txManager;if(t.active&&this._activeTxId===t.context?.id)return;if(e??this._ctx.autoSave)await this._ctx.saveCollection(this.name)}_applyValidation(e){if(!this._schema)return e;let t=Ns(e,this._schema,this._strict);if(!t.length)return e;switch(this._onSchemaError){case"warn":return this._ctx.logger(`[${this.name}] Validation warning: ${t.join("; ")}`,"warn"),e;case"strip":return $s(e,this._schema);default:throw new $("ERR_SKALEX_VALIDATION_FAILED",`Validation failed: ${t.join("; ")}`,{errors:t})}}_enforceCapAfterInsert(){let e=this._maxDocs;if(!e||this._data.length<=e)return;let t=this._data.splice(0,this._data.length-e);for(let i of t)this._index.delete(i._id),this._removeFromIndex(i)}_findRaw(e,{includeDeleted:t=!1}={}){if(typeof e==="function"){for(let i of this._data){if(this._softDelete&&i._deletedAt&&!t)continue;if(e(i))return i}return null}if(e._id){let i=this._index.get(e._id)||null;if(!i)return null;if(this._softDelete&&i._deletedAt&&!t)return null;if(Object.keys(e).length>1)return V(i,e)?i:null;return i}if(this._fieldIndex)for(let i in e){if(i==="$or"||i==="$and"||i==="$not")continue;let s=e[i];if(typeof s!=="object"||s===null){let n=this._fieldIndex._lookupIterable(i,s);if(n!==null){for(let r of n){if(this._softDelete&&r._deletedAt&&!t)continue;if(V(r,e))return r}return null}}}for(let i of this._data){if(this._softDelete&&i._deletedAt&&!t)continue;if(V(i,e))return i}return null}_findAllRaw(e,{includeDeleted:t=!1}={}){if(e&&typeof e!=="function"&&e._id){let s=this._index.get(e._id);if(!s)return[];if(this._softDelete&&s._deletedAt&&!t)return[];return V(s,e)?[s]:[]}let i=[];for(let s of this._getCandidates(e)){if(this._softDelete&&s._deletedAt&&!t)continue;if(V(s,e))i.push(s)}return i}_getCandidates(e){if(!this._fieldIndex)return this._data;if(this._fieldIndex._compoundIndexes.size>0){let t={};for(let i in e){if(i==="$or"||i==="$and"||i==="$not")continue;let s=e[i];if(typeof s!=="object"||s===null)t[i]=s}if(Object.keys(t).length>=2){let i=this._fieldIndex.lookupCompound(t);if(i!==null)return i}}for(let t in e){if(t==="$or"||t==="$and"||t==="$not")continue;let i=e[t];if(typeof i!=="object"||i===null){let s=this._fieldIndex._lookupIterable(t,i);if(s!==null)return s}}return this._data}_findIndex(e){for(let t=0;t<this._data.length;t++)if(V(this._data[t],e))return t;return-1}_addToIndex(e){if(this._fieldIndex)this._fieldIndex.add(e)}_removeFromIndex(e){if(this._fieldIndex)this._fieldIndex.remove(e)}_updateInIndex(e,t){if(this._fieldIndex)this._fieldIndex.update(e,t)}async _buildDoc(e,{ttl:t,embed:i}={}){let s={...e,_id:e._id??(this._ctx.idGenerator??Al)(),createdAt:new Date,updatedAt:new Date},n=t??this._defaultTtl;if(n)s._expiresAt=Sl(n);let r=i??this._defaultEmbed;if(r){let a=typeof r==="function"?r(s):s[r];s._vector=await this._ctx.embed(String(a))}if(this._versioning)s._version=1;return s}_projectDoc(e,t){if(t){let s={};for(let n of t){if(n==="_vector")continue;s[n]=e[n]}return s}let i={...e};return delete i._vector,i}async _populateDoc(e,t,i){for(let s of i){let n=await this._ctx.getCollection(s).findOne({_id:t[s]});if(n)e[s]=n}}}class ki{async read(e){throw Error("StorageAdapter.read() not implemented")}async write(e,t){throw Error("StorageAdapter.write() not implemented")}async delete(e){throw Error("StorageAdapter.delete() not implemented")}async list(){throw Error("StorageAdapter.list() not implemented")}async writeAll(e){for(let{name:t,data:i}of e)await this.write(t,i)}}class Hn extends ki{constructor({dir:e,format:t="gz"}){super();this.dir=Be.resolve(e),this.format=t,this._ensureDir(this.dir)}_ensureDir(e){if(!q.existsSync(e))q.mkdirSync(e,{recursive:!0})}_filePath(e){return Be.join(this.dir,`${e}.${this.format}`)}async read(e){let t=this._filePath(e);try{let i=await q.promises.readFile(t);if(this.format==="gz")i=oi.inflateSync(i);return i.toString("utf8")}catch(i){if(i.code==="ENOENT")return null;throw i}}async write(e,t){let i=this._filePath(e),s=Be.join(this.dir,`${e}_${globalThis.crypto.randomUUID()}.tmp.${this.format}`);if(this.format==="gz"){let n=oi.deflateSync(t);await q.promises.writeFile(s,n)}else await q.promises.writeFile(s,t,"utf8");await q.promises.rename(s,i)}async writeAll(e){let t=[];try{for(let{name:i,data:s}of e){let n=this._filePath(i),r=Be.join(this.dir,`${i}_${globalThis.crypto.randomUUID()}.tmp.${this.format}`);if(this.format==="gz"){let a=oi.deflateSync(s);await q.promises.writeFile(r,a)}else await q.promises.writeFile(r,s,"utf8");t.push({tmp:r,fp:n})}for(let{tmp:i,fp:s}of t)await q.promises.rename(i,s)}catch(i){for(let{tmp:s}of t)try{await q.promises.unlink(s)}catch{}throw i}}async delete(e){let t=this._filePath(e);try{await q.promises.unlink(t)}catch(i){if(i.code!=="ENOENT")throw i}}async list(){try{let e=await q.promises.readdir(this.dir),t=`.${this.format}`;return e.filter((i)=>i.endsWith(t)&&!i.includes(".tmp.")).map((i)=>i.slice(0,-t.length))}catch(e){if(e.code==="ENOENT")return[];throw e}}join(...e){return Be.join(...e)}ensureDir(e){this._ensureDir(e)}async writeRaw(e,t){this._ensureDir(Be.dirname(e)),await q.promises.writeFile(e,t,"utf8")}async readRaw(e){return q.promises.readFile(Be.resolve(e),"utf8")}}class Vn{constructor(){this._migrations=[]}add(e){let{version:t,up:i}=e;if(typeof t!=="number"||t<1)throw new $("ERR_SKALEX_VALIDATION_MIGRATION",`Migration version must be a positive integer, got ${t}`,{version:t});if(typeof i!=="function")throw new $("ERR_SKALEX_VALIDATION_MIGRATION",`Migration version ${t} must have an "up" function`,{version:t});if(this._migrations.some((s)=>s.version===t))throw new $("ERR_SKALEX_VALIDATION_MIGRATION_DUPLICATE",`Migration version ${t} is already registered`,{version:t});this._migrations.push({...e}),this._migrations.sort((s,n)=>s.version-n.version)}async run(e,t=[]){let i=new Set(t),s=this._migrations.filter((n)=>!i.has(n.version));for(let n of s){let r=e(n.version);await n.up(r),i.add(n.version)}return[...i].sort((n,r)=>n-r)}status(e=[]){let t=new Set(e),i=this._migrations.map((s)=>s.version).filter((s)=>!t.has(s));return{current:e.length?Math.max(...e):0,applied:[...t].sort((s,n)=>s-n),pending:i}}}var Cs={[Symbol.iterator](){return{next(){return{done:!0}}}},size:0};function Rs(e){return{[Symbol.iterator](){return e[Symbol.iterator]()},get size(){return e.size}}}function ui(e){return e.map((t)=>{if(t===null||t===void 0)return"\x00";if(typeof t==="boolean")return t?"\x01T":"\x01F";if(typeof t==="number")return`\x02${t}`;return`\x03${String(t)}`}).join("\x1F")}class Lt{constructor(e=[],t=[]){this._fields=new Set,this._compoundFields=[];for(let i of e)if(Array.isArray(i)){for(let s of i)this._validateFieldName(s);this._compoundFields.push(i)}else this._validateFieldName(i),this._fields.add(i);for(let i of t)this._validateFieldName(i);this._uniqueFields=new Set(t),this._indexedFields=new Set([...this._fields,...this._uniqueFields]),this._fieldIndexes=new Map,this._uniqueIndexes=new Map;for(let i of this._fields)this._fieldIndexes.set(i,new Map);for(let i of this._uniqueFields)if(this._uniqueIndexes.set(i,new Map),!this._fieldIndexes.has(i))this._fieldIndexes.set(i,new Map);this._compoundIndexes=new Map;for(let i of this._compoundFields)this._compoundIndexes.set(i.join("\x00"),{fields:i,map:new Map})}get indexedFields(){return this._indexedFields}buildFromData(e){for(let[,t]of this._fieldIndexes)t.clear();for(let[,t]of this._uniqueIndexes)t.clear();for(let[,t]of this._compoundIndexes)t.map.clear();for(let t of e)this._indexDoc(t)}add(e){this._checkUnique(e,null),this._indexDoc(e)}remove(e){for(let[t,i]of this._fieldIndexes){let s=e[t];if(s!==void 0){let n=i.get(s);if(n){if(n.delete(e),n.size===0)i.delete(s)}}}for(let[t,i]of this._uniqueIndexes){let s=e[t];if(s!==void 0)i.delete(s)}for(let[,t]of this._compoundIndexes){let i=ui(t.fields.map((n)=>e[n])),s=t.map.get(i);if(s){if(s.delete(e),s.size===0)t.map.delete(i)}}}update(e,t){this._checkUnique(t,e),this.remove(e);try{this._indexDoc(t)}catch(i){try{this._indexDoc(e)}catch{}throw i}}lookup(e,t){let i=this._fieldIndexes.get(e);if(!i)return null;let s=i.get(t);return s?[...s]:[]}_lookupIterable(e,t){let i=this._fieldIndexes.get(e);if(!i)return null;let s=i.get(t);return s?Rs(s):Cs}lookupCompound(e){let t=Object.keys(e).sort();for(let[,i]of this._compoundIndexes){let s=[...i.fields].sort();if(s.length!==t.length)continue;if(s.every((n,r)=>n===t[r])){let n=ui(i.fields.map((a)=>e[a])),r=i.map.get(n);return r?Rs(r):Cs}}return null}isUniqueTaken(e,t){let i=this._uniqueIndexes.get(e);if(!i)return!1;return i.has(t)}assertUniqueCandidates(e,t){if(this._uniqueFields.size===0)return;let i=new Set(e.map((s)=>s._id));for(let s of this._uniqueFields){let n=new Set,r=this._uniqueIndexes.get(s);if(r){for(let[l,o]of r.entries())if(!i.has(o._id))n.add(l)}let a=new Map;for(let l=0;l<t.length;l++){let o=t[l][s];if(o===void 0)continue;if(n.has(o))throw new Ee("ERR_SKALEX_UNIQUE_VIOLATION",`Unique constraint violation: field "${s}" value "${o}" already exists`,{field:s,value:o});let c=a.get(o);if(c&&c!==e[l]._id)throw new Ee("ERR_SKALEX_UNIQUE_VIOLATION",`Unique constraint violation: field "${s}" value "${o}" already exists`,{field:s,value:o});a.set(o,e[l]._id)}}}assertUniqueBatch(e){if(this._uniqueFields.size===0)return;for(let t of this._uniqueFields){let i=this._uniqueIndexes.get(t),s=new Set;for(let n of e){let r=n[t];if(r===void 0)continue;if(i&&i.has(r))throw new Ee("ERR_SKALEX_UNIQUE_VIOLATION",`Unique constraint violation: field "${t}" value "${r}" already exists`,{field:t,value:r});if(s.has(r))throw new Ee("ERR_SKALEX_UNIQUE_VIOLATION",`Unique constraint violation: duplicate "${t}" value "${r}" within batch`,{field:t,value:r});s.add(r)}}}_validateFieldName(e){if(e.includes("."))throw new $("ERR_SKALEX_VALIDATION_INDEX_DOT_PATH",`Index fields cannot use dot-notation: "${e}". Use a flat field name.`,{field:e})}_indexDoc(e){for(let[t,i]of this._fieldIndexes){let s=e[t];if(s!==void 0){if(!i.has(s))i.set(s,new Set);i.get(s).add(e)}}for(let[t,i]of this._uniqueIndexes){let s=e[t];if(s!==void 0)i.set(s,e)}for(let[,t]of this._compoundIndexes){for(let s of t.fields){let n=e[s];if(n!==void 0&&n!==null&&typeof n==="object")throw new $("ERR_SKALEX_VALIDATION_COMPOUND_INDEX",`Compound index field "${s}" must be a scalar value (string, number, or boolean), got ${Array.isArray(n)?"array":typeof n}`,{field:s})}let i=ui(t.fields.map((s)=>e[s]));if(!t.map.has(i))t.map.set(i,new Set);t.map.get(i).add(e)}}_checkUnique(e,t){for(let i of this._uniqueFields){let s=e[i];if(s===void 0)continue;let n=this._uniqueIndexes.get(i);if(!n)continue;let r=n.get(s);if(r&&r._id!==t?._id)throw new Ee("ERR_SKALEX_UNIQUE_VIOLATION",`Unique constraint violation: field "${i}" value "${s}" already exists`,{field:i,value:s})}}}var It="_flush",bt="migrations";class Jn{constructor({adapter:e,serializer:t,deserializer:i,logger:s,debug:n=!1,lenientLoad:r=!1}){this._adapter=e,this._serializer=t,this._deserializer=i,this._logger=s,this._debug=n,this._lenientLoad=r,this._saveLock=Promise.resolve()}_withSaveLock(e){let t=this._saveLock.then(e);return this._saveLock=t.catch(()=>{}),t}async loadAll(e,{parseSchema:t,buildIndex:i,IndexEngine:s}){try{let n=await this._adapter.list();await Promise.all(n.map(async(r)=>{try{let a=await this._adapter.read(r);if(!a)return;let l=this._deserializer(a),{collectionName:o,data:c}=l;if(!o)return;let h=e[o],u=h?.rawSchema??l.rawSchema??null,d=h?.schema??(u?t(u):null),_=h?.changelog??l.changelog??!1,f=h?.softDelete??l.softDelete??!1,p=h?.versioning??l.versioning??!1,m=h?.strict??l.strict??!1,w=h?.onSchemaError??l.onSchemaError??"throw",v=h?.defaultTtl??l.defaultTtl??null,R=h?.defaultEmbed??l.defaultEmbed??null,E=h?.maxDocs??l.maxDocs??null,A=h?h.fieldIndex:null;if(!A&&d?.uniqueFields?.length)A=new s([],d.uniqueFields);let Ue=i(c,"_id");if(A)A.buildFromData(c);e[o]={collectionName:o,data:c,index:Ue,isSaving:!1,_pendingSave:!1,_dirty:!1,schema:d,rawSchema:u,fieldIndex:A,changelog:_,softDelete:f,versioning:p,strict:m,onSchemaError:w,defaultTtl:v,defaultEmbed:R,maxDocs:E}}catch(a){if(a.code==="ENOENT")return;if(this._lenientLoad){this._logger(`WARNING: Could not load collection "${r}": ${a.message}. Collection will be empty.`,"error");return}throw new ke("ERR_SKALEX_PERSISTENCE_CORRUPT",`Failed to load collection "${r}": ${a.message}`,{collection:r})}})),this._detectIncompleteFlush(e),await this._cleanOrphanTempFiles()}catch(n){if(n.code!=="ENOENT")throw this._logger(`Error loading data: ${n}`,"error"),n}}async save(e,t){if(t)await this._saveOne(e,t);else await Promise.all(Object.keys(e).map((i)=>this._saveOne(e,i)))}async saveDirty(e){let t=Object.keys(e).filter((i)=>e[i]._dirty);if(t.length===0)return;await Promise.all(t.map((i)=>this._saveOne(e,i)))}saveAtomic(e,t){return this._withSaveLock(async()=>{if(t.length===0)return;this._writeFlushSentinel(e,t);let i=new Set(t);i.add("_meta");let s=[...i].filter((n)=>e[n]).map((n)=>({name:n,data:this._serializeCollection(e[n])}));try{await this._adapter.writeAll(s)}catch(n){throw new ke("ERR_SKALEX_PERSISTENCE_FLUSH_FAILED",`Batch save failed during writeAll: ${n.message}`,{collections:t})}for(let n of t)if(e[n])e[n]._dirty=!1;try{this._clearFlushSentinel(e),await this._adapter.write("_meta",this._serializeCollection(e._meta))}catch(n){this._logger(`WARNING: Batch data committed but sentinel clear failed: ${n.message}. Next load will report an incomplete flush (false positive).`,"error")}})}markDirty(e,t){let i=e[t];if(i)i._dirty=!0}_serializeCollection(e){let t={collectionName:e.collectionName,data:e.data};if(e.rawSchema)t.rawSchema=e.rawSchema;if(e.changelog)t.changelog=e.changelog;if(e.softDelete)t.softDelete=e.softDelete;if(e.versioning)t.versioning=e.versioning;if(e.strict)t.strict=e.strict;if(e.onSchemaError!=="throw")t.onSchemaError=e.onSchemaError;if(e.defaultTtl)t.defaultTtl=e.defaultTtl;if(e.defaultEmbed)t.defaultEmbed=e.defaultEmbed;if(e.maxDocs)t.maxDocs=e.maxDocs;return this._serializer(t)}async _saveOne(e,t){let i=e[t];if(!i)return;if(i.isSaving)return i._pendingSave=!0,new Promise((s,n)=>{(i._saveWaiters??=[]).push({resolve:s,reject:n})});i.isSaving=!0,i._pendingSave=!1;try{await this._writeCollection(t,i);while(i._pendingSave)i._pendingSave=!1,await this._writeCollection(t,i);this._resolveSaveWaiters(i)}catch(s){throw this._rejectSaveWaiters(i,s),s}finally{i.isSaving=!1}}async _writeCollection(e,t){try{await this._adapter.write(e,this._serializeCollection(t)),t._dirty=!1}catch(i){throw this._logger(`Error saving "${e}": ${i.message}`,"error"),i}}_resolveSaveWaiters(e){let t=e._saveWaiters?.splice(0)??[];for(let i of t)i.resolve()}_rejectSaveWaiters(e,t){let i=e._saveWaiters?.splice(0)??[];for(let s of i)s.reject(t)}_writeFlushSentinel(e,t){let i=this._getOrCreateMeta(e);i[It]={startedAt:new Date().toISOString(),collections:t,completedAt:null}}_clearFlushSentinel(e){let t=this._getOrCreateMeta(e);if(t[It])t[It].completedAt=new Date().toISOString()}_detectIncompleteFlush(e){let t=e._meta;if(!t)return;let i=t.index.get(bt);if(!i)return;let s=i[It];if(!s)return;if(s.startedAt&&!s.completedAt)this._logger(`WARNING: Incomplete flush detected (started ${s.startedAt}, collections: ${s.collections?.join(", ")}). Data may be inconsistent.`,"error")}async _cleanOrphanTempFiles(){if(typeof this._adapter.join!=="function")return;if(typeof this._adapter.list!=="function")return;try{let e=await import("fs"),t=await import("path"),i=this._adapter.dir;if(!i)return;let s=(await e.default.promises.readdir(i)).filter((n)=>n.includes(".tmp."));for(let n of s){let r=t.default.join(i,n);try{await e.default.promises.unlink(r),this._log(`Cleaned orphan temp file: ${n}`)}catch{}}}catch{}}_getOrCreateMeta(e){if(!e._meta)e._meta={collectionName:"_meta",data:[],index:new Map,isSaving:!1,_pendingSave:!1,_dirty:!1,schema:null,rawSchema:null,fieldIndex:null,changelog:!1,softDelete:!1,versioning:!1,strict:!1,onSchemaError:"throw",defaultTtl:null,defaultEmbed:null,maxDocs:null};let t=e._meta,i=t.index.get(bt);if(!i)i={_id:bt},t.data.push(i),t.index.set(bt,i);return i}_log(e){if(this._debug)this._logger(e,"info")}}var Dl=0;class Yn{constructor(){this._txLock=Promise.resolve(),this._ctx=null,this._abortedIds=new Set}get active(){return this._ctx!==null&&!this._ctx.aborted}get context(){return this._ctx}async run(e,t,{timeout:i=0}={}){let s=async()=>{await t._ensureConnected();let r=new Set(Object.keys(t.collections)),a={id:++Dl,startedAt:Date.now(),aborted:!1,preExisting:r,touchedCollections:new Set,snapshots:new Map,deferredEffects:[],timeout:i};this._ctx=a;let l=null,o=i>0?new Promise((u,d)=>{l=setTimeout(()=>{a.aborted=!0,d(new ge("ERR_SKALEX_TX_TIMEOUT",`Transaction ${a.id} timed out after ${i}ms`))},i)}):null,c=this,h=new Proxy(t,{get(u,d){if(d==="_txId")return a.id;if(a!==c._ctx)throw new ge("ERR_SKALEX_TX_STALE_PROXY",`Transaction ${a.id} has ended. This proxy is no longer usable.`);if(d==="collections")throw new ge("ERR_SKALEX_TX_DIRECT_ACCESS","Direct access to db.collections inside transaction() is not covered by rollback. Use the collection API (db.useCollection) instead.");if(d==="useCollection")return(f)=>{let p=u.useCollection(f);return p._activeTxId=a.id,p};let _=Reflect.get(u,d);return typeof _==="function"?_.bind(u):_}});try{let u=e(h),d=o?await Promise.race([u,o]):await u;if(a.aborted)throw new ge("ERR_SKALEX_TX_ABORTED",`Transaction ${a.id} was aborted`);let _=[...a.touchedCollections];if(_.length>0)await t._persistence.saveAtomic(t.collections,_);for(let f of a.deferredEffects)await f();return d}catch(u){for(let[d,_]of a.snapshots)if(a.preExisting.has(d)){if(t._applySnapshot(d,_),t.collections[d])t.collections[d]._dirty=_._dirty}for(let d in t.collections)if(!a.preExisting.has(d))delete t.collections[d],delete t._collectionInstances[d];throw u}finally{if(l)clearTimeout(l);if(a.aborted)this._abortedIds.add(a.id);this._ctx=null;for(let u in t._collectionInstances){let d=t._collectionInstances[u];if(d._createdInTxId===a.id)d._createdInTxId=null;if(d._activeTxId===a.id)d._activeTxId=null}}},n=this._txLock.then(s);return this._txLock=n.catch(()=>{}),n}snapshotIfNeeded(e,t,i){let s=this._ctx;if(!s)return;if(this._assertCtxNotAborted(s),!s.snapshots.has(e)){let n=i(t);n._dirty=t._dirty??!1,s.snapshots.set(e,n)}s.touchedCollections.add(e)}assertNotAborted(){let e=this._ctx;if(e)this._assertCtxNotAborted(e)}_assertCtxNotAborted(e){if(e.aborted)throw new ge("ERR_SKALEX_TX_ABORTED",`Transaction ${e.id} was aborted. No further mutations allowed.`)}defer(e){if(this._ctx)return this._ctx.deferredEffects.push(e),!0;return!1}}class Wn{constructor(e){this._CollectionClass=e,this.stores={},this._instances={}}get(e,t){if(this._instances[e])return this._instances[e];if(!this.stores[e])this.createStore(e);let i=new this._CollectionClass(this.stores[e],t);return this._instances[e]=i,i}create(e,t={},i){this.createStore(e,t);let s=new this._CollectionClass(this.stores[e],i);return this._instances[e]=s,s}createStore(e,{schema:t,indexes:i=[],changelog:s=!1,softDelete:n=!1,versioning:r=!1,strict:a=!1,onSchemaError:l="throw",defaultTtl:o=null,defaultEmbed:c=null,maxDocs:h=null}={}){let u=null,d=null;if(t){u=Fn(t);let _=u.uniqueFields;if(i.length||_.length)d=new Lt(i,_)}else if(i.length)d=new Lt(i,[]);this.stores[e]={collectionName:e,data:[],index:new Map,isSaving:!1,_pendingSave:!1,_dirty:!1,schema:u,rawSchema:t||null,fieldIndex:d,changelog:s,softDelete:n,versioning:r,strict:a,onSchemaError:l,defaultTtl:o,defaultEmbed:c,maxDocs:h}}rename(e,t){if(!this.stores[e])throw new ke("ERR_SKALEX_PERSISTENCE_COLLECTION_NOT_FOUND",`Collection "${e}" not found`,{collection:e});if(this.stores[t])throw new ke("ERR_SKALEX_PERSISTENCE_COLLECTION_EXISTS",`Collection "${t}" already exists`,{collection:t});let i=this.stores[e];if(i.collectionName=t,this.stores[t]=i,delete this.stores[e],this._instances[e]){let s=this._instances[e];s.name=t,this._instances[t]=s,delete this._instances[e]}}buildIndex(e,t){let i=new Map;for(let s of e)i.set(s[t],s);return i}inspect(e){if(e){let i=this.stores[e];if(!i)return null;return{name:e,count:i.data.length,schema:i.schema?Object.fromEntries(i.schema.fields):null,indexes:i.fieldIndex?[...i.fieldIndex.indexedFields]:[],softDelete:i.softDelete??!1,versioning:i.versioning??!1,strict:i.strict??!1,onSchemaError:i.onSchemaError??"throw",maxDocs:i.maxDocs??null}}let t={};for(let i in this.stores)t[i]=this.inspect(i);return t}dump(){let e={};for(let t in this.stores)if(!t.startsWith("_"))e[t]=structuredClone(this.stores[t].data);return e}schema(e){let t=this.stores[e];if(!t)return null;if(t.schema)return Object.fromEntries([...t.schema.fields.entries()].map(([i,s])=>[i,s.type]));if(t.data.length>0)return vl(t.data[0]);return null}stats(e){let t=(i)=>{let s=this.stores[i];if(!s)return null;let n=s.data.length,r=0;for(let a of s.data)try{r+=JSON.stringify(a).length}catch(l){}return{collection:i,count:n,estimatedSize:r,avgDocSize:n>0?Math.round(r/n):0}};if(e)return t(e);return Object.keys(this.stores).map(t)}clear(){this.stores={},this._instances={}}}class Li{async embed(e){throw Error("EmbeddingAdapter.embed() not implemented")}}var ce=(e)=>globalThis.process?.env?.[e]??globalThis.Deno?.env?.get(e);class Qn extends Li{constructor({apiKey:e=ce("OPENAI_API_KEY"),model:t=ce("OPENAI_EMBED_MODEL")??"text-embedding-3-small",baseUrl:i=ce("OPENAI_EMBED_BASE_URL")??"https://api.openai.com/v1/embeddings",dimensions:s=ce("OPENAI_EMBED_DIMENSIONS")!=null?Number(ce("OPENAI_EMBED_DIMENSIONS")):void 0,organization:n=ce("OPENAI_ORGANIZATION")??void 0,timeout:r=ce("OPENAI_EMBED_TIMEOUT")!=null?Number(ce("OPENAI_EMBED_TIMEOUT")):void 0,retries:a=Number(ce("OPENAI_EMBED_RETRIES")??0),retryDelay:l=Number(ce("OPENAI_EMBED_RETRY_DELAY")??1000),headers:o={},fetch:c=globalThis.fetch}={}){super();if(!e)throw Error("OpenAIEmbeddingAdapter requires an apiKey");this.apiKey=e,this.model=t,this.baseUrl=i,this.dimensions=s,this.organization=n,this.timeout=r,this.retries=a,this.retryDelay=l,this.headers=o,this._fetch=c}async embed(e){let t;for(let i=0;i<=this.retries;i++){let s=this.timeout!=null?new AbortController:null,n=s?setTimeout(()=>s.abort(),this.timeout):null;try{let r=await this._fetch(this.baseUrl,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`,...this.organization&&{"OpenAI-Organization":this.organization},...this.headers},body:JSON.stringify({input:e,model:this.model,...this.dimensions!==void 0&&{dimensions:this.dimensions}}),...s&&{signal:s.signal}});if(!r.ok){let a=(await r.text()).slice(0,200);throw Error(`OpenAI embedding API error ${r.status}: ${a}`)}return(await r.json()).data[0].embedding}catch(r){if(t=r,i<this.retries)await new Promise((a)=>setTimeout(a,this.retryDelay*2**i))}finally{if(n!==null)clearTimeout(n)}}throw t}}var nt=(e)=>globalThis.process?.env?.[e]??globalThis.Deno?.env?.get(e);class Gn extends Li{constructor({model:e=nt("OLLAMA_EMBED_MODEL")??"nomic-embed-text",host:t=nt("OLLAMA_HOST")??"http://localhost:11434",timeout:i=nt("OLLAMA_EMBED_TIMEOUT")!=null?Number(nt("OLLAMA_EMBED_TIMEOUT")):void 0,retries:s=Number(nt("OLLAMA_EMBED_RETRIES")??0),retryDelay:n=Number(nt("OLLAMA_EMBED_RETRY_DELAY")??1000),headers:r={},fetch:a=globalThis.fetch}={}){super();this.model=e,this.host=t,this.timeout=i,this.retries=s,this.retryDelay=n,this.headers=r,this._fetch=a}async embed(e){let t;for(let i=0;i<=this.retries;i++){let s=this.timeout!=null?new AbortController:null,n=s?setTimeout(()=>s.abort(),this.timeout):null;try{let r=await this._fetch(`${this.host}/api/embeddings`,{method:"POST",headers:{"Content-Type":"application/json",...this.headers},body:JSON.stringify({model:this.model,prompt:e}),...s&&{signal:s.signal}});if(!r.ok){let a=(await r.text()).slice(0,200);throw Error(`Ollama embedding API error ${r.status}: ${a}`)}return(await r.json()).embedding}catch(r){if(t=r,i<this.retries)await new Promise((a)=>setTimeout(a,this.retryDelay*2**i))}finally{if(n!==null)clearTimeout(n)}}throw t}}class Ft{async generate(e,t){throw Error("LLMAdapter.generate() not implemented")}async summarize(e){throw Error("LLMAdapter.summarize() not implemented")}}var Pi=`You are a database query assistant. Convert the user's natural language request into a JSON filter object for a document database.
|
|
42
|
-
|
|
43
|
-
Rules:
|
|
44
|
-
- Return ONLY a valid JSON object, nothing else
|
|
45
|
-
- Use standard query operators: $eq, $ne, $gt, $gte, $lt, $lte, $in, $nin, $regex
|
|
46
|
-
- For simple equality, use plain key-value pairs: {"field": "value"}
|
|
47
|
-
- For numeric comparisons use operators: {"age": {"$gt": 18}}
|
|
48
|
-
- For text search use $regex: {"name": {"$regex": "pattern"}}
|
|
49
|
-
- Return {} to match all documents
|
|
50
|
-
- No explanations, no markdown, no code fences`,Mi="Summarise the following memory entries into one concise paragraph. Preserve all important facts.",L=(e)=>globalThis.process?.env?.[e]??globalThis.Deno?.env?.get(e);class Zn extends Ft{constructor({apiKey:e=L("OPENAI_API_KEY"),model:t=L("OPENAI_MODEL")??"gpt-4o-mini",baseUrl:i=L("OPENAI_BASE_URL")??"https://api.openai.com/v1/chat/completions",maxTokens:s=L("OPENAI_MAX_TOKENS")!=null?Number(L("OPENAI_MAX_TOKENS")):void 0,temperature:n=Number(L("OPENAI_TEMPERATURE")??0.3),topP:r=L("OPENAI_TOP_P")!=null?Number(L("OPENAI_TOP_P")):void 0,organization:a=L("OPENAI_ORGANIZATION")??void 0,timeout:l=L("OPENAI_TIMEOUT")!=null?Number(L("OPENAI_TIMEOUT")):void 0,retries:o=Number(L("OPENAI_RETRIES")??0),retryDelay:c=Number(L("OPENAI_RETRY_DELAY")??1000),headers:h={},fetch:u=globalThis.fetch,seed:d=L("OPENAI_SEED")!=null?Number(L("OPENAI_SEED")):void 0,generatePrompt:_=Pi,summarizePrompt:f=Mi}={}){super();if(!e)throw Error("OpenAILLMAdapter requires an apiKey");this.apiKey=e,this.model=t,this.baseUrl=i,this.maxTokens=s,this.temperature=n,this.topP=r,this.organization=a,this.timeout=l,this.retries=o,this.retryDelay=c,this.headers=h,this._fetch=u,this.seed=d,this.generatePrompt=_,this.summarizePrompt=f}async generate(e,t){let i=await this._post({model:this.model,messages:[{role:"system",content:`${this.generatePrompt}
|
|
51
|
-
Schema: ${JSON.stringify(e)}`},{role:"user",content:t}],response_format:{type:"json_object"},temperature:0,...this.maxTokens!==void 0&&{max_tokens:this.maxTokens},...this.seed!==void 0&&{seed:this.seed}});return JSON.parse(i.choices[0].message.content)}async summarize(e){return(await this._post({model:this.model,messages:[{role:"system",content:this.summarizePrompt},{role:"user",content:e}],temperature:this.temperature,...this.maxTokens!==void 0&&{max_tokens:this.maxTokens},...this.topP!==void 0&&{top_p:this.topP},...this.seed!==void 0&&{seed:this.seed}})).choices[0].message.content.trim()}async _post(e){let t;for(let i=0;i<=this.retries;i++){let s=this.timeout!=null?new AbortController:null,n=s?setTimeout(()=>s.abort(),this.timeout):null;try{let r=await this._fetch(this.baseUrl,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`,...this.organization&&{"OpenAI-Organization":this.organization},...this.headers},body:JSON.stringify(e),...s&&{signal:s.signal}});if(!r.ok){let a=(await r.text()).slice(0,200);throw Error(`OpenAI API error ${r.status}: ${a}`)}return r.json()}catch(r){if(t=r,i<this.retries)await new Promise((a)=>setTimeout(a,this.retryDelay*2**i))}finally{if(n!==null)clearTimeout(n)}}throw t}}var Q=(e)=>globalThis.process?.env?.[e]??globalThis.Deno?.env?.get(e);class er extends Ft{constructor({apiKey:e=Q("ANTHROPIC_API_KEY"),model:t=Q("ANTHROPIC_MODEL")??"claude-haiku-4-5",baseUrl:i=Q("ANTHROPIC_BASE_URL")??"https://api.anthropic.com/v1/messages",apiVersion:s="2023-06-01",maxTokens:n=Number(Q("ANTHROPIC_MAX_TOKENS")??1024),temperature:r=Number(Q("ANTHROPIC_TEMPERATURE")??0.3),topP:a=Q("ANTHROPIC_TOP_P")!=null?Number(Q("ANTHROPIC_TOP_P")):void 0,topK:l=Q("ANTHROPIC_TOP_K")!=null?Number(Q("ANTHROPIC_TOP_K")):void 0,timeout:o=Q("ANTHROPIC_TIMEOUT")!=null?Number(Q("ANTHROPIC_TIMEOUT")):void 0,retries:c=Number(Q("ANTHROPIC_RETRIES")??0),retryDelay:h=Number(Q("ANTHROPIC_RETRY_DELAY")??1000),headers:u={},fetch:d=globalThis.fetch,generatePrompt:_=Pi,summarizePrompt:f=Mi}={}){super();if(!e)throw Error("AnthropicLLMAdapter requires an apiKey");this.apiKey=e,this.model=t,this.baseUrl=i,this.apiVersion=s,this.maxTokens=n,this.temperature=r,this.topP=a,this.topK=l,this.timeout=o,this.retries=c,this.retryDelay=h,this.headers=u,this._fetch=d,this.generatePrompt=_,this.summarizePrompt=f}async generate(e,t){let i=(await this._post({model:this.model,max_tokens:this.maxTokens,temperature:0,system:`${this.generatePrompt}
|
|
52
|
-
Schema: ${JSON.stringify(e)}`,messages:[{role:"user",content:t}]})).content[0].text.trim().replace(/^```(?:json)?\n?/,"").replace(/\n?```$/,"");return JSON.parse(i)}async summarize(e){return(await this._post({model:this.model,max_tokens:this.maxTokens,temperature:this.temperature,...this.topP!==void 0&&{top_p:this.topP},...this.topK!==void 0&&{top_k:this.topK},system:this.summarizePrompt,messages:[{role:"user",content:e}]})).content[0].text.trim()}async _post(e){let t;for(let i=0;i<=this.retries;i++){let s=this.timeout!=null?new AbortController:null,n=s?setTimeout(()=>s.abort(),this.timeout):null;try{let r=await this._fetch(this.baseUrl,{method:"POST",headers:{"Content-Type":"application/json","x-api-key":this.apiKey,"anthropic-version":this.apiVersion,...this.headers},body:JSON.stringify(e),...s&&{signal:s.signal}});if(!r.ok){let a=(await r.text()).slice(0,200);throw Error(`Anthropic API error ${r.status}: ${a}`)}return r.json()}catch(r){if(t=r,i<this.retries)await new Promise((a)=>setTimeout(a,this.retryDelay*2**i))}finally{if(n!==null)clearTimeout(n)}}throw t}}var re=(e)=>globalThis.process?.env?.[e]??globalThis.Deno?.env?.get(e);class tr extends Ft{constructor({model:e=re("OLLAMA_MODEL")??"llama3.2",host:t=re("OLLAMA_HOST")??"http://localhost:11434",temperature:i=Number(re("OLLAMA_TEMPERATURE")??0.3),topP:s=re("OLLAMA_TOP_P")!=null?Number(re("OLLAMA_TOP_P")):void 0,topK:n=re("OLLAMA_TOP_K")!=null?Number(re("OLLAMA_TOP_K")):void 0,timeout:r=re("OLLAMA_TIMEOUT")!=null?Number(re("OLLAMA_TIMEOUT")):void 0,retries:a=Number(re("OLLAMA_RETRIES")??0),retryDelay:l=Number(re("OLLAMA_RETRY_DELAY")??1000),headers:o={},fetch:c=globalThis.fetch,generatePrompt:h=Pi,summarizePrompt:u=Mi}={}){super();this.model=e,this.host=t,this.temperature=i,this.topP=s,this.topK=n,this.timeout=r,this.retries=a,this.retryDelay=l,this.headers=o,this._fetch=c,this.generatePrompt=h,this.summarizePrompt=u}async generate(e,t){let i=`${this.generatePrompt}
|
|
53
|
-
Schema: ${JSON.stringify(e)}
|
|
54
|
-
Query: ${t}`,s=await this._post({model:this.model,prompt:i,format:"json",options:{temperature:0},stream:!1});return JSON.parse(s.response)}async summarize(e){return(await this._post({model:this.model,prompt:`${this.summarizePrompt}
|
|
55
|
-
|
|
56
|
-
${e}`,options:{temperature:this.temperature,...this.topP!==void 0&&{top_p:this.topP},...this.topK!==void 0&&{top_k:this.topK}},stream:!1})).response.trim()}async _post(e){let t;for(let i=0;i<=this.retries;i++){let s=this.timeout!=null?new AbortController:null,n=s?setTimeout(()=>s.abort(),this.timeout):null;try{let r=await this._fetch(`${this.host}/api/generate`,{method:"POST",headers:{"Content-Type":"application/json",...this.headers},body:JSON.stringify(e),...s&&{signal:s.signal}});if(!r.ok){let a=(await r.text()).slice(0,200);throw Error(`Ollama API error ${r.status}: ${a}`)}return r.json()}catch(r){if(t=r,i<this.retries)await new Promise((a)=>setTimeout(a,this.retryDelay*2**i))}finally{if(n!==null)clearTimeout(n)}}throw t}}function Cl({provider:e,apiKey:t,embedModel:i,model:s,host:n,embedBaseUrl:r,dimensions:a,organization:l,embedTimeout:o,embedRetries:c,embedRetryDelay:h}){let u=i||s;switch(e){case"openai":return new Qn({apiKey:t,model:u,baseUrl:r,...a!==void 0&&{dimensions:a},...l!==void 0&&{organization:l},...o!==void 0&&{timeout:o},...c!==void 0&&{retries:c},...h!==void 0&&{retryDelay:h}});case"ollama":return new Gn({model:u,host:n,...o!==void 0&&{timeout:o},...c!==void 0&&{retries:c},...h!==void 0&&{retryDelay:h}});default:throw new _e("ERR_SKALEX_ADAPTER_UNKNOWN_PROVIDER",`Unknown AI provider: "${e}". Supported: "openai", "ollama".`,{provider:e})}}function Rl({provider:e,apiKey:t,model:i,host:s,baseUrl:n,apiVersion:r,temperature:a,maxTokens:l,topP:o,topK:c,organization:h,timeout:u,retries:d,retryDelay:_,seed:f,generatePrompt:p,summarizePrompt:m}){if(!i)return null;switch(e){case"openai":return new Zn({apiKey:t,model:i,baseUrl:n,...l!==void 0&&{maxTokens:l},...a!==void 0&&{temperature:a},...o!==void 0&&{topP:o},...h!==void 0&&{organization:h},...u!==void 0&&{timeout:u},...d!==void 0&&{retries:d},..._!==void 0&&{retryDelay:_},...f!==void 0&&{seed:f},...p!==void 0&&{generatePrompt:p},...m!==void 0&&{summarizePrompt:m}});case"anthropic":return new er({apiKey:t,model:i,baseUrl:n,apiVersion:r,...l!==void 0&&{maxTokens:l},...a!==void 0&&{temperature:a},...o!==void 0&&{topP:o},...c!==void 0&&{topK:c},...u!==void 0&&{timeout:u},...d!==void 0&&{retries:d},..._!==void 0&&{retryDelay:_},...p!==void 0&&{generatePrompt:p},...m!==void 0&&{summarizePrompt:m}});case"ollama":return new tr({model:i,host:s,...a!==void 0&&{temperature:a},...o!==void 0&&{topP:o},...c!==void 0&&{topK:c},...u!==void 0&&{timeout:u},...d!==void 0&&{retries:d},..._!==void 0&&{retryDelay:_},...p!==void 0&&{generatePrompt:p},...m!==void 0&&{summarizePrompt:m}});default:return null}}var _i="AES-GCM",mt=12,Se=32,kl=new TextEncoder,Ll=new TextDecoder;class ir extends ki{constructor(e,t){super();if(this._adapter=e,this._rawKey=typeof t==="string"?Pl(t):Uint8Array.from(t),this._rawKey.length!==Se)throw new _e("ERR_SKALEX_ADAPTER_INVALID_KEY",`EncryptedAdapter: key must be ${Se} bytes (${Se*2} hex chars), got ${this._rawKey.length}`);this._cryptoKey=null}async read(e){let t=await this._adapter.read(e);if(!t)return null;return this._decrypt(t)}async write(e,t){return this._adapter.write(e,await this._encrypt(t))}async delete(e){return this._adapter.delete(e)}async list(){return this._adapter.list()}async writeAll(e){let t=[];for(let{name:i,data:s}of e)t.push({name:i,data:await this._encrypt(s)});return this._adapter.writeAll(t)}join(...e){return this._adapter.join?.(...e)}ensureDir(e){return this._adapter.ensureDir?.(e)}async writeRaw(e,t){return this._adapter.writeRaw?.(e,await this._encrypt(t))}async readRaw(e){let t=await this._adapter.readRaw?.(e);if(!t)return null;return this._decrypt(t)}async _getKey(){if(!this._cryptoKey)this._cryptoKey=await globalThis.crypto.subtle.importKey("raw",this._rawKey,{name:_i},!1,["encrypt","decrypt"]);return this._cryptoKey}async _encrypt(e){let t=await this._getKey(),i=globalThis.crypto.getRandomValues(new Uint8Array(mt)),s=kl.encode(e),n=await globalThis.crypto.subtle.encrypt({name:_i,iv:i,tagLength:128},t,s),r=new Uint8Array(mt+n.byteLength);return r.set(i,0),r.set(new Uint8Array(n),mt),Ml(r)}async _decrypt(e){let t=await this._getKey(),i=jl(e),s=i.slice(0,mt),n=i.slice(mt),r=await globalThis.crypto.subtle.decrypt({name:_i,iv:s,tagLength:128},t,n);return Ll.decode(r)}}function Pl(e){if(e.length!==Se*2)throw new _e("ERR_SKALEX_ADAPTER_INVALID_KEY",`EncryptedAdapter: hex key must be ${Se*2} characters (${Se} bytes)`);if(!/^[0-9a-fA-F]+$/.test(e))throw new _e("ERR_SKALEX_ADAPTER_INVALID_KEY","EncryptedAdapter: hex key contains invalid characters");let t=new Uint8Array(Se);for(let i=0;i<Se;i++)t[i]=parseInt(e.slice(i*2,i*2+2),16);return t}function Ml(e){let t="";for(let i=0;i<e.length;i++)t+=String.fromCharCode(e[i]);return globalThis.btoa(t)}function jl(e){let t=globalThis.atob(e),i=new Uint8Array(t.length);for(let s=0;s<t.length;s++)i[s]=t.charCodeAt(s);return i}var ql=4;class sr{constructor(e,t){this.sessionId=e,this._db=t,this._col=t.useCollection(`_memory_${e}`)}async remember(e){let t=await this._col.insertOne({text:e,sessionId:this.sessionId},{embed:"text"}),i=this._db._memoryConfig?.maxEntries;if(i&&this._col._data.length>i)await this.compress({threshold:0});return t}async recall(e,{limit:t=10,minScore:i=0}={}){return this._col.search(e,{limit:t,minScore:i})}async history({since:e,limit:t}={}){let i=e?{createdAt:{$gte:new Date(e)}}:{},s={sort:{createdAt:1}};if(t)s.limit=t;let{docs:n}=await this._col.find(i,s);return n}async forget(e){return this._col.deleteOne({_id:e})}tokenCount(){let e=this._col._data;return{tokens:e.reduce((t,i)=>t+this._docTokens(i),0),count:e.length}}context({tokens:e=this._db._memoryConfig?.contextTokens??4000}={}){let t=this._sortedData("desc"),i=[],s=0;for(let n of t){let r=this._docTokens(n);if(s+r>e)break;i.push(n.text),s+=r}return i.reverse().join(`
|
|
57
|
-
`)}async compress({threshold:e=this._db._memoryConfig?.compressionThreshold??8000,keepRecent:t=this._db._memoryConfig?.keepRecent??10}={}){let{tokens:i}=this.tokenCount();if(i<=e)return;if(!this._db._aiAdapter)throw Error('memory.compress() requires a language model adapter. Configure { ai: { model: "..." } }.');let s=this._sortedData("asc"),n=Math.max(0,s.length-t),r=s.slice(0,n);if(r.length===0)return;let a=r.map((o)=>o.text).join(`
|
|
58
|
-
`),l=await this._db._aiAdapter.summarize(a);await this._col.deleteMany({_id:{$in:r.map((o)=>o._id)}}),await this._col.insertOne({text:l,sessionId:this.sessionId,compressed:!0})}_docTokens(e){return Math.ceil((e.text||"").length/ql)}_sortedData(e){return[...this._col._data].sort(e==="asc"?(t,i)=>new Date(t.createdAt)-new Date(i.createdAt):(t,i)=>new Date(i.createdAt)-new Date(t.createdAt))}}class nr{constructor(e){this._db=e,this._restoring=!1}get _col(){return this._db.useCollection("_changelog")}async log(e,t,i,s=null,n=null){if(this._restoring)return;let r={op:e,collection:t,docId:i._id,doc:{...i},timestamp:new Date};if(s)r.prev={...s};if(n)r.session=n;await this._col.insertOne(r)}async query(e,{since:t,limit:i,session:s}={}){let n={collection:e};if(t)n.timestamp={$gte:new Date(t)};if(s)n.session=s;let r={sort:{timestamp:1}};if(i)r.limit=i;let{docs:a}=await this._col.find(n,r);return a}async restore(e,t,{_id:i}={}){let s=new Date(t),n=this._db.useCollection(e),r=(await this.query(e,{})).filter((l)=>new Date(l.timestamp)<=s);if(i){let l=r.filter((c)=>c.docId===i);if(l.length===0)return;let o=l[l.length-1];this._restoring=!0;try{if(o.op==="delete"){if(await n.findOne({_id:i}))await n.deleteOne({_id:i});return}if(await n.findOne({_id:i})){let{_id:c,createdAt:h,updatedAt:u,...d}=o.doc;await n.updateOne({_id:i},d)}else await n.insertOne({...o.doc})}finally{this._restoring=!1}await this._db.saveData(e);return}let a=new Map;for(let l of r)if(l.op==="insert"||l.op==="update")a.set(l.docId,{doc:l.doc,deleted:!1});else if(l.op==="delete")a.set(l.docId,{doc:null,deleted:!0});this._restoring=!0;try{await n.deleteMany({});for(let[,{doc:l,deleted:o}]of a)if(!o&&l)await n.insertOne({...l})}finally{this._restoring=!1}await this._db.saveData(e)}}function Kl(e){let t=5381;for(let i=0;i<e.length;i++)t=(t<<5)+t+e.charCodeAt(i)|0;return(t>>>0).toString(16).padStart(8,"0")}class rr{constructor({maxSize:e=500,ttl:t=0}={}){this._cache=new Map,this._maxSize=e,this._ttl=t}_key(e,t,i){return Kl(JSON.stringify({collectionName:e,schema:t,query:i}))}get(e,t,i){let s=this._key(e,t,i),n=this._cache.get(s);if(!n)return;if(this._ttl>0&&Date.now()-n.ts>this._ttl){this._cache.delete(s);return}return n.filter}set(e,t,i,s){let n=this._key(e,t,i);if(this._cache.size>=this._maxSize&&!this._cache.has(n))this._cache.delete(this._cache.keys().next().value);this._cache.set(n,{filter:s,ts:Date.now()})}toJSON(){return Object.fromEntries([...this._cache.entries()].map(([e,t])=>[e,t]))}fromJSON(e){if(!e||typeof e!=="object")return;for(let[t,i]of Object.entries(e))this._cache.set(t,i)}get size(){return this._cache.size}}function Fl(e,{regexMaxLength:t=500}={}){if(typeof e!=="object"||e===null)return e;let i={};for(let s of Object.keys(e)){let n=e[s];if(n&&typeof n==="object"&&!Array.isArray(n)){let r={};for(let a of Object.keys(n))if(a==="$regex"&&typeof n.$regex==="string"){if(n.$regex.length>t)throw Error(`$regex pattern too long (max ${t} characters)`);if(/\([^)]*[+*][^)]*\)[+*{]/.test(n.$regex))throw Error("$regex pattern rejected: nested quantifiers risk ReDoS");try{r.$regex=new RegExp(n.$regex)}catch{throw Error(`invalid $regex pattern: "${n.$regex}"`)}}else if(["$gt","$gte","$lt","$lte"].includes(a)&&Ul(n[a]))r[a]=new Date(n[a]);else r[a]=n[a];i[s]=r}else i[s]=n}return i}function Ul(e){if(typeof e!=="string")return!1;return/\d{4}-\d{2}-\d{2}/.test(e)&&!isNaN(Date.parse(e))}function zl(e,t){let i=[];if(!t||typeof e!=="object"||e===null)return i;for(let s of Object.keys(e)){if(s.startsWith("$"))continue;if(!(s.split(".")[0]in t))i.push(`Unknown field referenced in generated filter: "${s}"`)}return i}class ar{constructor(){this._listeners=new Map}on(e,t){if(!this._listeners.has(e))this._listeners.set(e,new Set);return this._listeners.get(e).add(t),()=>this._listeners.get(e)?.delete(t)}off(e,t){this._listeners.get(e)?.delete(t)}emit(e,t){for(let i of[e,"*"]){let s=this._listeners.get(i);if(!s)continue;for(let n of s)try{n(t)}catch(r){}}}removeAll(e){if(e)this._listeners.delete(e);else this._listeners.clear()}listenerCount(e){return this._listeners.get(e)?.size??0}}class or{constructor({threshold:e=100,maxEntries:t=500}={}){this._threshold=e,this._maxEntries=t,this._entries=[]}record({collection:e,op:t,filter:i,query:s,duration:n,resultCount:r}){if(n<this._threshold)return;let a={collection:e,op:t,duration:n,resultCount:r,timestamp:new Date};if(i!==void 0)a.filter=i;if(s!==void 0)a.query=s;if(this._entries.push(a),this._entries.length>this._maxEntries)this._entries.shift()}entries({limit:e,minDuration:t,collection:i}={}){let s=this._entries;if(i)s=s.filter((n)=>n.collection===i);if(t)s=s.filter((n)=>n.duration>=t);if(e)s=s.slice(-e);return s}get size(){return this._entries.length}clear(){this._entries=[]}}class lr{constructor(){this._sessions=new Map}_ensure(e){if(!this._sessions.has(e))this._sessions.set(e,{reads:0,writes:0,lastActive:null});return this._sessions.get(e)}recordRead(e){if(!e)return;let t=this._ensure(e);t.reads++,t.lastActive=new Date}recordWrite(e){if(!e)return;let t=this._ensure(e);t.writes++,t.lastActive=new Date}get(e){let t=this._sessions.get(e);if(!t)return null;return{sessionId:e,...t}}all(){return[...this._sessions.entries()].map(([e,t])=>({sessionId:e,...t}))}clear(e){if(e)this._sessions.delete(e);else this._sessions.clear()}}class cr{constructor(){this._plugins=[]}register(e){if(typeof e!=="object"||e===null)throw TypeError("Plugin must be a non-null object.");this._plugins.push(e)}async run(e,t){for(let i of this._plugins)if(typeof i[e]==="function")await i[e](t)}get size(){return this._plugins.length}}function Je(e){if(typeof e!=="string"||!e.trim())throw Error("collection name must be a non-empty string");if(/[/\\]/.test(e)||e.includes("..")||e.includes("\x00"))throw Error(`invalid collection name: "${e}"`);if(e.trim().startsWith("_"))throw Error(`access to system collection "${e}" is not permitted`);return e.trim()}var ks=[{name:"skalex_collections",description:"List all collection names in the database.",inputSchema:{type:"object",properties:{}},scope:"read"},{name:"skalex_schema",description:"Return the schema for a collection as a { field: type } map. Returns null if the collection is empty.",inputSchema:{type:"object",properties:{collection:{type:"string",description:"Collection name."}},required:["collection"]},scope:"read"},{name:"skalex_find",description:"Find documents in a collection that match a filter.",inputSchema:{type:"object",properties:{collection:{type:"string",description:"Collection name."},filter:{type:"object",description:"Query filter (MongoDB-style operators supported)."},limit:{type:"number",description:"Maximum number of results. Default: 20."},sort:{type:"object",description:"Sort descriptor: { field: 1 } for ascending, { field: -1 } for descending."}},required:["collection"]},scope:"read"},{name:"skalex_insert",description:"Insert a single document into a collection.",inputSchema:{type:"object",properties:{collection:{type:"string",description:"Collection name."},doc:{type:"object",description:"Document to insert."}},required:["collection","doc"]},scope:"write"},{name:"skalex_update",description:"Update the first document matching a filter.",inputSchema:{type:"object",properties:{collection:{type:"string",description:"Collection name."},filter:{type:"object",description:"Query filter to identify the document."},update:{type:"object",description:"Fields to update (direct assignment, $inc, $push supported)."},many:{type:"boolean",description:"If true, update all matching documents. Default: false."}},required:["collection","filter","update"]},scope:"write"},{name:"skalex_delete",description:"Delete the first document matching a filter.",inputSchema:{type:"object",properties:{collection:{type:"string",description:"Collection name."},filter:{type:"object",description:"Query filter to identify the document."},many:{type:"boolean",description:"If true, delete all matching documents. Default: false."}},required:["collection","filter"]},scope:"write"},{name:"skalex_search",description:"Semantic similarity search. Requires an embedding adapter to be configured.",inputSchema:{type:"object",properties:{collection:{type:"string",description:"Collection name."},query:{type:"string",description:"Natural-language query string to embed and compare."},limit:{type:"number",description:"Maximum number of results. Default: 10."},minScore:{type:"number",description:"Minimum cosine similarity score [0, 1]. Default: 0."},filter:{type:"object",description:"Optional structured pre-filter (hybrid search)."}},required:["collection","query"]},scope:"read"},{name:"skalex_ask",description:"Translate a natural-language question into a filter and query a collection. Requires a language model adapter.",inputSchema:{type:"object",properties:{collection:{type:"string",description:"Collection name."},question:{type:"string",description:"Natural-language question about the data."},limit:{type:"number",description:"Maximum number of results. Default: 20."}},required:["collection","question"]},scope:"read"}];async function Bl(e,t,i){switch(e){case"skalex_collections":return Object.keys(i.collections).filter((s)=>!s.startsWith("_"));case"skalex_schema":return i.schema(Je(t.collection))??null;case"skalex_find":{let s=i.useCollection(Je(t.collection)),n={};if(t.limit)n.limit=t.limit;if(t.sort)n.sort=t.sort;return s.find(t.filter||{},n)}case"skalex_insert":return i.useCollection(Je(t.collection)).insertOne(t.doc||{});case"skalex_update":{let s=i.useCollection(Je(t.collection));if(t.many)return s.updateMany(t.filter||{},t.update||{});return s.updateOne(t.filter||{},t.update||{})}case"skalex_delete":{let s=i.useCollection(Je(t.collection));if(t.many)return s.deleteMany(t.filter||{});return s.deleteOne(t.filter||{})}case"skalex_search":return i.useCollection(Je(t.collection)).search(t.query,{limit:t.limit??10,minScore:t.minScore??0,filter:t.filter});case"skalex_ask":return i.ask(Je(t.collection),t.question,{limit:t.limit??20});default:throw Object.assign(Error(`Unknown tool: ${e}`),{code:"NOT_FOUND"})}}var ji="2.0",qi=-32700,Xl=-32600,Ls=-32601,Hl=-32602,Vl=-32603;function rt(e,t){return{jsonrpc:ji,id:e,result:t}}function $e(e,t,i,s){return{jsonrpc:ji,id:e,error:{code:t,message:i}}}function Jl(e){try{let t=JSON.parse(e);if(typeof t!=="object"||t===null||t.jsonrpc!==ji)return{parseError:$e(null,Xl,"Invalid JSON-RPC request")};return{msg:t}}catch(t){return{parseError:$e(null,qi,"Parse error")}}}function Yl(e){return{content:[{type:"text",text:e}]}}function Ps(e){return{content:[{type:"text",text:e}],isError:!0}}class hr{constructor({port:e=3000,host:t="127.0.0.1",allowedOrigin:i=null,maxBodySize:s=1048576}={}){this._port=e,this._host=t,this._allowedOrigin=i,this._maxBodySize=s,this._clients=new Set,this._onMessage=null,this._server=null}onMessage(e){this._onMessage=e}send(e){let t=`data: ${JSON.stringify(e)}
|
|
59
|
-
|
|
60
|
-
`;for(let i of this._clients)try{i.write(t)}catch(s){this._clients.delete(i)}}start(){return new Promise((e,t)=>{this._server=Eo.createServer((i,s)=>{if(this._allowedOrigin)s.setHeader("Access-Control-Allow-Origin",this._allowedOrigin),s.setHeader("Access-Control-Allow-Methods","GET, POST, OPTIONS"),s.setHeader("Access-Control-Allow-Headers","Content-Type");if(i.method==="OPTIONS"){s.writeHead(204),s.end();return}if(i.method==="GET"&&i.url==="/sse")this._handleSSE(i,s);else if(i.method==="POST"&&i.url==="/message")this._handleMessage(i,s);else s.writeHead(404,{"Content-Type":"text/plain"}),s.end("Not Found")}),this._server.on("error",t),this._server.listen(this._port,this._host,()=>e())})}stop(){return new Promise((e)=>{if(!this._server){e();return}for(let t of this._clients)try{t.end()}catch(i){}this._clients.clear(),this._server.close(()=>e()),this._server=null})}_handleSSE(e,t){t.writeHead(200,{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}),t.write(`event: endpoint
|
|
61
|
-
data: /message
|
|
62
|
-
|
|
63
|
-
`),this._clients.add(t),e.on("close",()=>{this._clients.delete(t)})}async _handleMessage(e,t){let i="";e.setEncoding("utf8"),e.on("data",(s)=>{if(i+=s,i.length>this._maxBodySize)e.destroy(),t.writeHead(413,{"Content-Type":"application/json"}),t.end(JSON.stringify({error:"Request body too large"}))}),e.on("end",async()=>{t.writeHead(202,{"Content-Type":"application/json"}),t.end("{}");let s;try{s=JSON.parse(i)}catch(n){this.send($e(null,qi,"Parse error"));return}if(this._onMessage)await this._onMessage(s).catch(()=>{})})}get port(){return this._port}get host(){return this._host}get url(){return`http://${this._host}:${this._port}`}get sseUrl(){return`${this.url}/sse`}}class dr{constructor(){this._onMessage=null,this._buffer="",this._started=!1}onMessage(e){this._onMessage=e}send(e){process.stdout.write(JSON.stringify(e)+`
|
|
64
|
-
`)}start(){if(this._started)return;this._started=!0,process.stdin.setEncoding("utf8"),process.stdin.on("data",(e)=>{this._buffer+=e;let t;while((t=this._buffer.indexOf(`
|
|
65
|
-
`))!==-1){let i=this._buffer.slice(0,t).trim();if(this._buffer=this._buffer.slice(t+1),i&&this._onMessage){let s;try{s=JSON.parse(i)}catch(n){this.send($e(null,qi,"Parse error"));continue}this._onMessage(s).catch(()=>{})}}}),process.stdin.on("end",()=>{process.exit(0)})}stop(){process.stdin.removeAllListeners("data"),process.stdin.removeAllListeners("end"),this._started=!1}}var Wl={name:"skalex",version:"4.0.0-alpha"},Ql="2024-11-05";class ur{constructor(e,t={}){this._db=e,this._transport=t.transport||"stdio",this._port=t.port||3000,this._host=t.host||"127.0.0.1",this._allowedOrigin=t.allowedOrigin??null,this._maxBodySize=t.maxBodySize??1048576,this._scopes=t.scopes||{"*":["read"]},this._t=null}async listen(){if(this._transport==="http")this._t=new hr({port:this._port,host:this._host,allowedOrigin:this._allowedOrigin,maxBodySize:this._maxBodySize});else this._t=new dr;this._t.onMessage((e)=>this._handleMessage(e)),await this._t.start()}async connect(e){if(this._t=e,this._t.onMessage((t)=>this._handleMessage(t)),typeof this._t.start==="function")await this._t.start()}async close(){if(this._t&&typeof this._t.stop==="function")await this._t.stop();this._t=null}get transport(){return this._transport}get url(){return this._t?.url}async _handleMessage(e){let{msg:t,parseError:i}=typeof e==="string"?Jl(e):{msg:e};if(i){this._send(i);return}let{id:s,method:n,params:r}=t;if(s===void 0){if(n==="notifications/initialized")return;return}try{switch(n){case"initialize":this._send(rt(s,{protocolVersion:Ql,capabilities:{tools:{}},serverInfo:Wl}));break;case"tools/list":this._send(rt(s,{tools:this._visibleTools()}));break;case"tools/call":await this._handleToolCall(s,r);break;case"ping":this._send(rt(s,{}));break;default:this._send($e(s,Ls,`Method not found: ${n}`))}}catch(a){this._send($e(s,Vl,a.message||"Internal error"))}}async _handleToolCall(e,t){let i=t?.name,s=t?.arguments??{};if(!i){this._send($e(e,Hl,"tools/call requires params.name"));return}let n=ks.find((a)=>a.name===i);if(!n){this._send($e(e,Ls,`Unknown tool: ${i}`));return}let r=s.collection||s.collection_name||null;if(!this._hasScope(r,n.scope)){this._send(rt(e,Ps(`Access denied: "${i}" requires "${n.scope}" scope on collection "${r}".`)));return}try{let a=await Bl(i,s,this._db);this._send(rt(e,Yl(JSON.stringify(a,null,2))))}catch(a){this._send(rt(e,Ps(a.message||String(a))))}}_visibleTools(){return ks.filter((e)=>{let t=this._scopes["*"];if(t&&(t.includes(e.scope)||t.includes("admin")))return!0;for(let[,i]of Object.entries(this._scopes))if(i.includes(e.scope)||i.includes("admin"))return!0;return!1})}_hasScope(e,t){let i=(s)=>s.includes("admin")||s.includes(t);if(e&&this._scopes[e])return i(this._scopes[e]);if(this._scopes["*"])return i(this._scopes["*"]);return!1}_send(e){this._t?.send(e)}}var Tt="migrations",Ms=(e)=>JSON.stringify(e,function(t,i){let s=this[t];if(s instanceof Date)return{__skalex_date__:s.toISOString()};if(typeof i==="bigint")return{__skalex_bigint__:i.toString()};return i}),js=(e)=>JSON.parse(e,(t,i)=>{if(i&&typeof i==="object"){if("__skalex_bigint__"in i)return BigInt(i.__skalex_bigint__);if("__skalex_date__"in i)return new Date(i.__skalex_date__)}return i});class te{constructor({path:e="./.db",format:t="gz",debug:i=!1,adapter:s,ai:n,encrypt:r,slowQueryLog:a,queryCache:l,memory:o,logger:c,plugins:h,llmAdapter:u,embeddingAdapter:d,regexMaxLength:_,idGenerator:f,serializer:p,deserializer:m,autoSave:w,ttlSweepInterval:v,lenientLoad:R}={}){if(this.dataDirectory=e,this.dataFormat=t,this.debug=i,!te._errorsAttached)te.SkalexError=je,te.ValidationError=$,te.UniqueConstraintError=Ee,te.TransactionError=ge,te.PersistenceError=ke,te.AdapterError=_e,te.QueryError=Ge,te._errorsAttached=!0;this._adapterConfig=s??null;let E=s||new Hn({dir:e,format:t});if(r)E=new ir(E,r.key);if(this.fs=E,this._registry=new Wn(Xn),this.collections=this._registry.stores,this._collectionInstances=this._registry._instances,this._migrations=new Vn,this._connectPromise=null,this.isConnected=!1,this._aiConfig=n||null,this._encryptConfig=r||null,this._pluginsConfig=Array.isArray(h)?h:null,this._memoryConfig=o||null,this._regexMaxLength=_??500,this._idGenerator=f??null,this._serializer=p??Ms,this._deserializer=m??js,this._autoSave=w??!1,this._txManager=new Yn,this._ttlSweepInterval=v??0,this._ttlTimer=null,this._logger=c??Ts,this._embeddingAdapter=d??(n?Cl(n):null),this._aiAdapter=u??(n?Rl(n):null),this._persistence=new Jn({adapter:this.fs,serializer:this._serializer,deserializer:this._deserializer,logger:this._logger,debug:this.debug,lenientLoad:R??!1}),this._changeLog=new nr(this),this._queryCache=new rr(l||{}),this._eventBus=new ar,this._queryLog=a?new or(a):null,this._sessionStats=new lr,this._plugins=new cr,Array.isArray(h))for(let A of h)this._plugins.register(A)}async connect(){if(this._connectPromise)return this._connectPromise;return this._connectPromise=this._doConnect(),this._connectPromise}async _doConnect(){try{await this.loadData();let e=this._getMeta();if(e.queryCache)this._queryCache.fromJSON(e.queryCache);if(this._migrations._migrations.length>0){let t=e.appliedVersions||[],i=await this._migrations.run((s)=>this.useCollection(`_migration_${s}`),t);this._saveMeta({appliedVersions:i})}if(this._sweepTtl(),this._ttlSweepInterval>0){if(this._ttlTimer=setInterval(()=>this._sweepTtl(),this._ttlSweepInterval),this._ttlTimer?.unref)this._ttlTimer.unref()}this.isConnected=!0,this._log("> - Connected to the database (\u221A)")}catch(e){throw this._logger(`Error connecting to the database: ${e}`,"error"),e}}async disconnect(){try{if(this._ttlTimer)clearInterval(this._ttlTimer),this._ttlTimer=null;await this.saveData(),this._registry.clear(),this.collections=this._registry.stores,this._collectionInstances=this._registry._instances,this._connectPromise=null,this.isConnected=!1,this._log("> - Disconnected from the database (\u221A)")}catch(e){throw this._logger(`Error disconnecting from the database: ${e}`,"error"),e}}async _ensureConnected(){if(this.isConnected)return;return this.connect()}useCollection(e){return this._registry.get(e,this)}createCollection(e,t={}){return this._registry.create(e,t,this)}_createCollectionStore(e,t={}){this._registry.createStore(e,t)}async renameCollection(e,t){this._registry.rename(e,t),await this.saveData(t),await this.fs.delete(e)}async loadData(){await this._persistence.loadAll(this.collections,{parseSchema:Fn,buildIndex:this._registry.buildIndex,IndexEngine:Lt});for(let e in this._collectionInstances){let t=this.collections[e];if(t&&this._collectionInstances[e]._store!==t)this._collectionInstances[e]._store=t}}async saveData(e){await this._persistence.save(this.collections,e)}buildIndex(e,t){return this._registry.buildIndex(e,t)}addMigration(e){this._migrations.add(e)}migrationStatus(){let e=this._getMeta();return this._migrations.status(e.appliedVersions||[])}namespace(e){if(this._adapterConfig)throw new _e("ERR_SKALEX_ADAPTER_NAMESPACE_REQUIRES_FS","namespace() requires the default FsAdapter. When a custom storage adapter is configured, create a separate Skalex instance with your adapter instead.");let t=String(e).replace(/[^a-zA-Z0-9_-]/g,"_");if(!t)throw new $("ERR_SKALEX_VALIDATION_NAMESPACE_ID","namespace: id must contain at least one alphanumeric character",{id:e});return new te({path:`${this.dataDirectory}/${t}`,format:this.dataFormat,debug:this.debug,ai:this._aiConfig||void 0,encrypt:this._encryptConfig||void 0,slowQueryLog:this._queryLog?{threshold:this._queryLog._threshold,maxEntries:this._queryLog._maxEntries}:void 0,queryCache:this._queryCache?{maxSize:this._queryCache._maxSize,ttl:this._queryCache._ttl}:void 0,plugins:this._pluginsConfig||void 0,memory:this._memoryConfig||void 0,logger:this._logger!==Ts?this._logger:void 0,llmAdapter:this._aiAdapter&&!this._aiConfig?this._aiAdapter:void 0,embeddingAdapter:this._embeddingAdapter&&!this._aiConfig?this._embeddingAdapter:void 0,regexMaxLength:this._regexMaxLength!==500?this._regexMaxLength:void 0,idGenerator:this._idGenerator||void 0,serializer:this._serializer!==Ms?this._serializer:void 0,deserializer:this._deserializer!==js?this._deserializer:void 0,autoSave:this._autoSave||void 0,ttlSweepInterval:this._ttlSweepInterval||void 0})}use(e){this._plugins.register(e)}watch(e){return this._eventBus.on("*",e)}sessionStats(e){if(e!==void 0)return this._sessionStats.get(e);return this._sessionStats.all()}get _inTransaction(){return this._txManager.active}_emitEvent(e,t){if(!this._txManager.defer(()=>this._eventBus.emit(e,t)))this._eventBus.emit(e,t)}async _runAfterHook(e,t){if(!this._txManager.defer(()=>this._plugins.run(e,t)))await this._plugins.run(e,t)}async _logChange(e,t,i,s,n){if(!this._txManager.defer(()=>this._changeLog.log(e,t,i,s,n)))await this._changeLog.log(e,t,i,s,n)}async transaction(e,t={}){return this._txManager.run(e,this,t)}async seed(e,{reset:t=!1}={}){for(let[i,s]of Object.entries(e)){if(t&&this.collections[i])this._applySnapshot(i,{data:[],index:new Map}),delete this._collectionInstances[i];await this.useCollection(i).insertMany(s)}await this.saveData()}dump(){return this._registry.dump()}inspect(e){return this._registry.inspect(e)}async import(e){let t=await this.fs.readRaw(e),i;try{i=JSON.parse(t)}catch{throw new ke("ERR_SKALEX_PERSISTENCE_INVALID_JSON",`import: invalid JSON in file "${e}"`,{filePath:e})}let s=e.split("/").pop().replace(/\.[^.]+$/,"");return this.useCollection(s).insertMany(Array.isArray(i)?i:[i],{save:!0})}async embed(e){if(!this._embeddingAdapter)throw new _e("ERR_SKALEX_ADAPTER_EMBEDDING_REQUIRED","db.embed() requires an AI adapter. Pass { ai: { provider, apiKey } } to the Skalex constructor.");return this._embeddingAdapter.embed(e)}async ask(e,t,{limit:i=20}={}){if(!this._aiAdapter)throw new _e("ERR_SKALEX_ADAPTER_LLM_REQUIRED",'db.ask() requires a language model adapter. Configure { ai: { provider, model: "..." } }.');let s=this.useCollection(e),n=this.schema(e),r=this._queryCache.get(e,n,t);if(!r){r=await this._aiAdapter.generate(n,t);let a=zl(r,n);if(a.length)a.forEach((l)=>this._log(`[ask] ${l}`));this._queryCache.set(e,n,t,r),this._saveMeta({queryCache:this._queryCache.toJSON()})}return s.find(Fl(r,{regexMaxLength:this._regexMaxLength}),{limit:i})}schema(e){return this._registry.schema(e)}useMemory(e){return new sr(e,this)}changelog(){return this._changeLog}async restore(e,t,i={}){return this._changeLog.restore(e,t,i)}stats(e){return this._registry.stats(e)}slowQueries(e={}){if(!this._queryLog)return[];return this._queryLog.entries(e)}slowQueryCount(){return this._queryLog?this._queryLog.size:0}clearSlowQueries(){this._queryLog?.clear()}mcp(e={}){return new ur(this,e)}_getMeta(){let e=this.collections._meta;if(!e)return{};return e.index.get(Tt)||{}}_saveMeta(e){if(!this.collections._meta)this._createCollectionStore("_meta");let t=this.collections._meta,i=t.index.get(Tt);if(i)Object.assign(i,e);else{let s={_id:Tt,...e};t.data.push(s),t.index.set(Tt,s)}this._persistence.markDirty(this.collections,"_meta")}_sweepTtl(){for(let e in this.collections){let t=this.collections[e],i=Il(t.data,t.index,t.fieldIndex?(s)=>t.fieldIndex.remove(s):null);if(i>0)this._persistence.markDirty(this.collections,e),this._log(`TTL sweep: removed ${i} expired docs from "${e}"`)}}_buildCollectionContext(){let e=this;return{ensureConnected:()=>e._ensureConnected(),get txManager(){return e._txManager},get plugins(){return e._plugins},get eventBus(){return e._eventBus},get sessionStats(){return e._sessionStats},get queryLog(){return e._queryLog},get logger(){return e._logger},get persistence(){return e._persistence},get collections(){return e.collections},embed:(t)=>e.embed(t),get idGenerator(){return e._idGenerator},get autoSave(){return e._autoSave},saveCollection:(t)=>e.saveData(t),snapshotCollection:(t)=>e._snapshotCollection(t),getCollection:(t)=>e.useCollection(t),emitEvent:(t,i)=>e._emitEvent(t,i),runAfterHook:(t,i)=>e._runAfterHook(t,i),logChange:(t,i,s,n,r)=>e._logChange(t,i,s,n,r),get fs(){return e.fs},get dataDirectory(){return e.dataDirectory}}}_log(e){if(this.debug)this._logger(e,"info")}_snapshotCollection(e){return{data:structuredClone(e.data)}}_applySnapshot(e,t){let i=this.collections[e];if(!i)return;if(i.data=t.data,i.index=this.buildIndex(t.data,"_id"),i.fieldIndex)i.fieldIndex.buildFromData(t.data)}}function Gl(){let e=Date.now().toString(16),t;try{t=Do.randomBytes(8).toString("hex")}catch{let i=new Uint8Array(8);crypto.getRandomValues(i),t=Array.from(i).map((s)=>s.toString(16).padStart(2,"0")).join("")}return`${e}${t}`}function qs(e,t){let i=e instanceof Error?e.message:e;if(t==="error")console.error(i);else console.log(i)}var Ks=new Set(["__proto__","constructor","prototype"]);function Ut(e,t){if(!t.includes(".")){if(Ks.has(t))return;return e[t]}let i=e;for(let s of t.split(".")){if(Ks.has(s))return;if(i==null)return;i=i[s]}return i}class qe extends Error{constructor(e,t,i={}){super(t);this.name=this.constructor.name,this.code=e,this.details=i}}class D extends qe{}class Ie extends qe{}class ye extends qe{}class Le extends qe{}class fe extends qe{}class Ze extends qe{}function vi(e,t){if(e===t)return!0;if(e==null||t==null)return!1;if(typeof e!==typeof t)return!1;if(e instanceof Date&&t instanceof Date)return e.getTime()===t.getTime();if(e instanceof RegExp&&t instanceof RegExp)return e.source===t.source&&e.flags===t.flags;if(Array.isArray(e)){if(!Array.isArray(t)||e.length!==t.length)return!1;for(let i=0;i<e.length;i++)if(!vi(e[i],t[i]))return!1;return!0}if(typeof e==="object"){if(Array.isArray(t))return!1;let i=Object.keys(e);if(i.length!==Object.keys(t).length)return!1;for(let s of i){if(!Object.prototype.hasOwnProperty.call(t,s))return!1;if(!vi(e[s],t[s]))return!1}return!0}return!1}function J(e,t){if(typeof t==="function")return t(e);if(t==null)return!0;if(typeof t==="object"&&Object.keys(t).length===0)return!0;if("$or"in t){let i=t.$or;if(!Array.isArray(i))throw new Ze("ERR_SKALEX_QUERY_INVALID_OPERATOR","$or must be an array of filters",{operator:"$or"});if(!i.some((s)=>J(e,s)))return!1}if("$and"in t){let i=t.$and;if(!Array.isArray(i))throw new Ze("ERR_SKALEX_QUERY_INVALID_OPERATOR","$and must be an array of filters",{operator:"$and"});if(!i.every((s)=>J(e,s)))return!1}if("$not"in t){if(J(e,t.$not))return!1}for(let i in t){if(i==="$or"||i==="$and"||i==="$not")continue;let s=t[i],n;try{n=Ut(e,i)}catch{return!1}if(s instanceof RegExp){if(!s.test(String(n)))return!1}else if(typeof s==="object"&&s!==null){if(Object.keys(s).some((r)=>r.startsWith("$"))){if("$eq"in s&&n!==s.$eq)return!1;if("$ne"in s&&n===s.$ne)return!1;if("$gt"in s&&!(n>s.$gt))return!1;if("$lt"in s&&!(n<s.$lt))return!1;if("$gte"in s&&!(n>=s.$gte))return!1;if("$lte"in s&&!(n<=s.$lte))return!1;if("$in"in s&&!s.$in.includes(n))return!1;if("$nin"in s&&s.$nin.includes(n))return!1;if("$regex"in s){if(!(s.$regex instanceof RegExp?s.$regex:new RegExp(s.$regex)).test(String(n)))return!1}if("$fn"in s&&!s.$fn(n))return!1}else if(!vi(n,s))return!1}else if(n!==s)return!1}return!0}function Zl(e,t=new Set){if(typeof e!=="object"||e===null||typeof e==="function")return e;let i=[],s=[],n=[],r=[],a=[];for(let o in e){if(o==="$or"||o==="$and"||o==="$not"){a.push(o);continue}let c=e[o];if(t.has(o))i.push(o);else if(c instanceof RegExp||typeof c==="object"&&c!==null&&(("$regex"in c)||("$fn"in c))||typeof c==="function")r.push(o);else if(typeof c==="object"&&c!==null&&(("$gt"in c)||("$lt"in c)||("$gte"in c)||("$lte"in c)||("$ne"in c)||("$in"in c)||("$nin"in c)))n.push(o);else s.push(o)}let l={};for(let o of[...i,...s,...n,...r,...a])l[o]=e[o];return l}var Ei=new Set(["string","number","boolean","object","array","date","any"]);function Ki(e){if(Array.isArray(e))return"array";if(e instanceof Date)return"date";return typeof e}function _r(e){let t=new Map,i=[];for(let[s,n]of Object.entries(e)){let r;if(typeof n==="string"){if(!Ei.has(n))throw new D("ERR_SKALEX_VALIDATION_UNKNOWN_TYPE",`Unknown schema type "${n}" for field "${s}"`,{field:s,type:n});r={type:n,required:!1,unique:!1}}else if(typeof n==="object"&&n!==null){let{type:a="any",required:l=!1,unique:o=!1,enum:c}=n;if(!Ei.has(a))throw new D("ERR_SKALEX_VALIDATION_UNKNOWN_TYPE",`Unknown schema type "${a}" for field "${s}"`,{field:s,type:a});if(r={type:a,required:l,unique:o,enum:c},o)i.push(s)}else throw new D("ERR_SKALEX_VALIDATION_INVALID_SCHEMA",`Invalid schema definition for field "${s}"`,{field:s});t.set(s,r)}return{fields:t,uniqueFields:i}}function Fs(e,t,i=!1){let s=[];for(let[n,r]of t){let a=e[n],l=a===void 0||a===null;if(r.required&&l){s.push(`Field "${n}" is required`);continue}if(l)continue;if(r.type!=="any"){let o=Ki(a);if(o!==r.type)s.push(`Field "${n}" must be of type "${r.type}", got "${o}"`)}if(r.enum&&!r.enum.includes(a))s.push(`Field "${n}" must be one of [${r.enum.map((o)=>JSON.stringify(o)).join(", ")}], got ${JSON.stringify(a)}`)}if(i){for(let n of Object.keys(e))if(!n.startsWith("_")&&!t.has(n))s.push(`Unknown field "${n}" (strict mode)`)}return s}function Us(e,t){let i={};for(let[s,n]of Object.entries(e)){if(s.startsWith("_")){i[s]=n;continue}if(!t.has(s))continue;let r=t.get(s);if(r.type!=="any"){if(Ki(n)!==r.type)continue}if(r.enum&&!r.enum.includes(n))continue;i[s]=n}return i}function ec(e){let t={};for(let[i,s]of Object.entries(e)){if(i.startsWith("_"))continue;let n=Ki(s);t[i]=Ei.has(n)?n:"any"}return t}function tc(e){if(typeof e==="number"){if(e<=0)throw new D("ERR_SKALEX_VALIDATION_TTL",`TTL must be positive, got ${e}`,{ttl:e});return e*1000}if(typeof e!=="string")throw new D("ERR_SKALEX_VALIDATION_TTL",`Invalid TTL value: ${e}`,{ttl:e});let t=e.match(/^(\d+(?:\.\d+)?)(ms|s|m|h|d)$/);if(!t)throw new D("ERR_SKALEX_VALIDATION_TTL_FORMAT",`Invalid TTL format: "${e}". Use e.g. 300 (seconds), "30m", "24h", "7d"`,{ttl:e});let i=parseFloat(t[1]);if(i<=0)throw new D("ERR_SKALEX_VALIDATION_TTL",`TTL must be positive, got "${e}"`,{ttl:e});let s=t[2],n=i*{ms:1,s:1000,m:60000,h:3600000,d:86400000}[s];if(!isFinite(n))throw new D("ERR_SKALEX_VALIDATION_TTL",`TTL value "${e}" is too large`,{ttl:e});return n}function ic(e){return new Date(Date.now()+tc(e))}function sc(e,t,i=null){let s=Date.now(),n=0,r=e.length;while(r--){let a=e[r];if(a._expiresAt&&new Date(a._expiresAt).getTime()<=s){if(e.splice(r,1),t.delete(a._id),i)i(a);n++}}return n}function zs(e,t){if(e.length!==t.length)throw new Ze("ERR_SKALEX_QUERY_VECTOR_MISMATCH",`Vector dimension mismatch: ${e.length} vs ${t.length}`,{expected:e.length,got:t.length});let i=0,s=0,n=0;for(let a=0;a<e.length;a++)i+=e[a]*t[a],s+=e[a]*e[a],n+=t[a]*t[a];let r=Math.sqrt(s*n);return r===0?0:i/r}function B(e){if(!("_vector"in e))return{...e};let{_vector:t,...i}=e;return i}function nc(e){return e.length}function rc(e,t){let i=0;for(let s of e){let n=Ut(s,t);if(typeof n==="number"&&!isNaN(n))i+=n}return i}function ac(e,t){let i=0,s=0;for(let n of e){let r=Ut(n,t);if(typeof r==="number"&&!isNaN(r))i+=r,s++}return s===0?null:i/s}function oc(e,t){let i=Object.create(null);for(let s of e){let n=String(Ut(s,t)??"__null__");if(!i[n])i[n]=[];i[n].push(s)}return i}class fr{constructor(e){this._col=e}get _ctx(){return this._col._ctx}async execute({op:e,beforeHook:t,afterHook:i,hookPayload:s,mutate:n,afterHookPayload:r,save:a,session:l}){let o=this._ctx;await o.ensureConnected(),this._col._txSnapshotIfNeeded();let c=o.txManager,h=c.active&&this._col._activeTxId===c.context?.id,u=h?c.context.id:null,d=this._col._createdInTxId,_=()=>{if(u!==null&&c._abortedIds.has(u))throw new ye("ERR_SKALEX_TX_ABORTED",`Transaction ${u} was aborted. No further mutations allowed.`);if(d!==null&&c._abortedIds.has(d))throw new ye("ERR_SKALEX_TX_ABORTED",`Transaction ${d} was aborted. Collection obtained inside that transaction cannot be used for further mutations.`)};if(h||d!==null)_();if(t)await o.plugins.run(t,s);let{docs:f,prevDocs:p=[]}=await n(_);if(o.persistence.markDirty(o.collections,this._col.name),await this._col._saveIfNeeded(a),this._col._changelogEnabled)for(let m=0;m<f.length;m++)await o.logChange(e,this._col.name,f[m],p[m]??null,l||null);if(!h||!c.defer(()=>o.sessionStats.recordWrite(l)))o.sessionStats.recordWrite(l);for(let m of f)o.emitEvent(this._col.name,{op:e,collection:this._col.name,doc:B(m)});if(i)if(r)await o.runAfterHook(i,r(f));else for(let m of f)await o.runAfterHook(i,{collection:this._col.name,doc:B(m)});return{docs:f,prevDocs:p}}}function lc(e){let t={};for(let i in e){if(i.startsWith("$"))continue;let s=e[i];if(s&&typeof s==="object"&&!Array.isArray(s)){let n=Object.keys(s);if(n.length===1&&n[0]==="$eq")t[i]=s.$eq}else t[i]=s}return t}var pr=new Set(["__proto__","constructor","prototype"]);function mr(e){if(typeof e!=="object"||e===null||Array.isArray(e))return e;let t={};for(let i of Object.keys(e)){if(pr.has(i))continue;t[i]=mr(e[i])}return t}class gr{constructor(e,t){this.name=e.collectionName,this.database=t,this._ctx=t._buildCollectionContext(),this._store=e,this._pipeline=new fr(this),this._createdInTxId=t._txManager.context?.id??null,this._activeTxId=null}get _data(){return this._store.data}set _data(e){this._store.data=e}get _index(){return this._store.index}get _fieldIndex(){return this._store.fieldIndex||null}get _schema(){return this._store.schema?this._store.schema.fields:null}get _changelogEnabled(){return this._store.changelog===!0}get _softDelete(){return this._store.softDelete===!0}get _versioning(){return this._store.versioning===!0}get _strict(){return this._store.strict===!0}get _onSchemaError(){return this._store.onSchemaError??"throw"}get _defaultTtl(){return this._store.defaultTtl||null}get _defaultEmbed(){return this._store.defaultEmbed||null}get _maxDocs(){return this._store.maxDocs||null}async insertOne(e,t={}){let{save:i,ifNotExists:s,ttl:n,embed:r,session:a}=t;if(s){await this._ctx.ensureConnected();let o=this._findRaw(e);if(o)return B({...o})}let{docs:l}=await this._insertCore([e],{ttl:n,embed:r,session:a,save:i});return B(l[0])}async insertMany(e,t={}){let{save:i,ttl:s,embed:n,session:r}=t,{docs:a}=await this._insertCore(e,{ttl:s,embed:n,session:r,save:i});return a.map(B)}async _insertCore(e,{ttl:t,embed:i,session:s,save:n}){return this._pipeline.execute({op:"insert",beforeHook:null,afterHook:"afterInsert",hookPayload:null,save:n,session:s,mutate:async(r)=>{let a=[],l=new Set;for(let o of e){let c=this._applyValidation(o);await this._ctx.plugins.run("beforeInsert",{collection:this.name,doc:c});let h=await this._buildDoc(c,{ttl:t,embed:i});if(this._index.has(h._id)||l.has(h._id))throw new Ie("ERR_SKALEX_UNIQUE_DUPLICATE_ID",`Duplicate _id "${h._id}" in collection "${this.name}"`,{id:h._id,collection:this.name});l.add(h._id),a.push(h)}if(r(),this._fieldIndex)this._fieldIndex.assertUniqueBatch(a);for(let o of a)this._addToIndex(o);this._data.push(...a);for(let o of a)this._index.set(o._id,o);return this._enforceCapAfterInsert(),{docs:a}}})}async updateOne(e,t,i={}){await this._ctx.ensureConnected();let s=this._findRaw(e);if(!s)return null;let{save:n,session:r}=i,{docs:a}=await this._updateCore([s],e,t,{save:n,session:r});return B(a[0])}async updateMany(e,t,i={}){await this._ctx.ensureConnected();let s=this._findAllRaw(e);if(s.length===0)return[];let{save:n,session:r}=i,{docs:a}=await this._updateCore(s,e,t,{save:n,session:r});return a.map(B)}async _updateCore(e,t,i,{save:s,session:n}){return this._pipeline.execute({op:"update",beforeHook:"beforeUpdate",afterHook:"afterUpdate",hookPayload:{collection:this.name,filter:t,update:i},save:s,session:n,afterHookPayload:(r)=>({collection:this.name,filter:t,update:i,result:r.length===1?r[0]:r}),mutate:async(r)=>{let a=this._changelogEnabled?e.map((h)=>structuredClone(h)):e.map(()=>null),l=e.map((h)=>this._prepareUpdatedDoc(h,i));this._assertUniqueCandidates(e,l),r();let o=new Set(e),c=new Map;for(let h=0;h<this._data.length;h++)if(o.has(this._data[h]))c.set(this._data[h],h);for(let h=0;h<e.length;h++)this._commitUpdatedDoc(e[h],l[h],c);return{docs:l,prevDocs:a}}})}applyUpdate(e,t){for(let i in t){if(pr.has(i))continue;if(i==="_id"||i==="createdAt")continue;let s=t[i];if(Array.isArray(s))e[i]=s;else if(typeof s==="object"&&s!==null){let n=Object.keys(s);if(n.some((r)=>r.startsWith("$"))){for(let r of n)if(r==="$inc"&&typeof e[i]==="number")e[i]+=s[r];else if(r==="$push"){if(!Array.isArray(e[i]))e[i]=[];e[i].push(s[r])}}else e[i]=mr(s)}else e[i]=s}if(e.updatedAt=new Date,this._versioning)e._version=(e._version??0)+1;return e}_prepareUpdatedDoc(e,t){let i=structuredClone(e),s=structuredClone(e);if(this.applyUpdate(s,t),!this._schema)return s;let n=Fs(s,this._schema,this._strict);if(!n.length)return s;switch(this._onSchemaError){case"throw":throw new D("ERR_SKALEX_VALIDATION_UPDATE",`Update validation failed on doc "${e._id}": ${n.join("; ")}`,{id:e._id,errors:n});case"strip":return this._stripCandidateToValid(s,i);default:return this._ctx.logger(`[${this.name}] Update validation warning: ${n.join("; ")}`,"warn"),s}}_stripCandidateToValid(e,t){let i=Us(e,this._schema),s=structuredClone(t);for(let[n,r]of Object.entries(i)){if(n.startsWith("_"))continue;s[n]=r}if(s.updatedAt=e.updatedAt,this._versioning)s._version=e._version;return s}_commitUpdatedDoc(e,t,i){let s=i?i.get(e):this._data.indexOf(e);if(s===void 0||s===-1)throw new Le("ERR_SKALEX_PERSISTENCE_DOC_MISSING",`Document "${e._id}" no longer exists in collection "${this.name}"`,{id:e._id,collection:this.name});this._data[s]=t,this._index.set(t._id,t),this._updateInIndex(e,t)}_assertUniqueCandidates(e,t){this._fieldIndex?.assertUniqueCandidates(e,t)}async upsert(e,t,i={}){if(await this._ctx.ensureConnected(),this._findRaw(e))return this.updateOne(e,t,i);return this.insertOne({...lc(e),...t},i)}async upsertMany(e,t,i={}){await this._ctx.ensureConnected();let{save:s,...n}=i,r=[];for(let a of e)r.push(await this.upsert({[t]:a[t]},a,{...n,save:!1}));return await this._saveIfNeeded(s),r}async restore(e,t={}){if(!this._softDelete)throw new Ze("ERR_SKALEX_QUERY_SOFT_DELETE_REQUIRED",`restore() requires softDelete on "${this.name}"`,{collection:this.name});await this._ctx.ensureConnected();let{save:i,session:s}=t,n=this._findRaw(e,{includeDeleted:!0});if(!n||!n._deletedAt)return null;let{docs:r}=await this._pipeline.execute({op:"restore",beforeHook:null,afterHook:null,hookPayload:null,save:i,session:s,mutate:async(a)=>{return a(),delete n._deletedAt,n.updatedAt=new Date,this._index.set(n._id,n),{docs:[n]}}});return B(r[0])}watch(e,t){if(typeof e==="function")t=e,e=null;if(t)return this._ctx.eventBus.on(this.name,(i)=>{if(!e||J(i.doc,e))t(i)});return this._watchIterator(e)}_watchIterator(e){let t=[],i=null,s=!1,n=this._ctx.eventBus.on(this.name,(r)=>{if(e&&!J(r.doc,e))return;if(i){let a=i;i=null,a({value:r,done:!1})}else t.push(r)});return{[Symbol.asyncIterator](){return this},next(){if(t.length>0)return Promise.resolve({value:t.shift(),done:!1});if(s)return Promise.resolve({value:void 0,done:!0});return new Promise((r)=>{i=r})},return(){if(s=!0,n(),i){let r=i;i=null,r({value:void 0,done:!0})}return Promise.resolve({value:void 0,done:!0})}}}async count(e={}){return await this._ctx.ensureConnected(),nc(this._findAllRaw(e))}async sum(e,t={}){return await this._ctx.ensureConnected(),rc(this._findAllRaw(t),e)}async avg(e,t={}){return await this._ctx.ensureConnected(),ac(this._findAllRaw(t),e)}async groupBy(e,t={}){return await this._ctx.ensureConnected(),oc(this._findAllRaw(t),e)}async findOne(e,t={}){await this._ctx.ensureConnected();let{populate:i,select:s,includeDeleted:n=!1}=t,r=this._findRaw(e,{includeDeleted:n});if(!r)return null;let a=this._projectDoc(r,s);if(i)await this._populateDoc(a,r,i);return a}async find(e,t={}){await this._ctx.ensureConnected();let i=Date.now(),{populate:s,select:n,sort:r,page:a=1,limit:l,session:o,includeDeleted:c=!1}=t;await this._ctx.plugins.run("beforeFind",{collection:this.name,filter:e,options:t});let h=this._getCandidates(e),u=Zl(e,this._fieldIndex?this._fieldIndex.indexedFields:new Set),d=[];for(let f of h){if(this._softDelete&&f._deletedAt&&!c)continue;if(!J(f,u))continue;let p=this._projectDoc(f,n);if(s)await this._populateDoc(p,f,s);d.push(p)}if(r){let f=Object.keys(r);d.sort((p,m)=>{for(let w of f){let v=r[w];if(p[w]<m[w])return-v;if(p[w]>m[w])return v}return 0})}let _;if(l){let f=d.length,p=Math.ceil(f/l),m=(a-1)*l;d=d.slice(m,m+l),_={page:a,totalDocs:f,totalPages:p}}return this._ctx.queryLog?.record({collection:this.name,op:"find",filter:e,duration:Date.now()-i,resultCount:d.length}),this._ctx.sessionStats.recordRead(o),await this._ctx.plugins.run("afterFind",{collection:this.name,filter:e,options:t,docs:d}),_?{docs:d,..._}:{docs:d}}async search(e,{filter:t,limit:i=10,minScore:s=0,session:n}={}){await this._ctx.ensureConnected();let r=Date.now();await this._ctx.plugins.run("beforeSearch",{collection:this.name,query:e,options:{filter:t,limit:i,minScore:s}});let a=await this._ctx.embed(e),l=t?this._findAllRaw(t):this._data,o=[];for(let d of l){if(!d._vector)continue;let _=zs(a,d._vector);if(_>=s)o.push({doc:d,score:_})}o.sort((d,_)=>_.score-d.score);let c=o.slice(0,i);this._ctx.queryLog?.record({collection:this.name,op:"search",query:e,duration:Date.now()-r,resultCount:c.length}),this._ctx.sessionStats.recordRead(n);let h=c.map((d)=>B(d.doc)),u=c.map((d)=>d.score);return await this._ctx.plugins.run("afterSearch",{collection:this.name,query:e,options:{filter:t,limit:i,minScore:s},docs:h,scores:u}),{docs:h,scores:u}}async similar(e,{limit:t=10,minScore:i=0}={}){await this._ctx.ensureConnected();let s=this._index.get(e);if(!s||!s._vector)return{docs:[],scores:[]};let n=[];for(let a of this._data){if(a._id===e||!a._vector)continue;let l=zs(s._vector,a._vector);if(l>=i)n.push({doc:a,score:l})}n.sort((a,l)=>l.score-a.score);let r=n.slice(0,t);return{docs:r.map((a)=>B(a.doc)),scores:r.map((a)=>a.score)}}async deleteOne(e,t={}){await this._ctx.ensureConnected();let{save:i,session:s}=t;if(this._softDelete){let r=this._findRaw(e);if(!r)return null;let{docs:a}=await this._deleteCore("soft",[r],e,{save:i,session:s});return B(a[0])}let{docs:n}=await this._deleteCore("hard",null,e,{save:i,session:s});if(n.length===0)return null;return B(n[0])}async deleteMany(e,t={}){await this._ctx.ensureConnected();let{save:i,session:s}=t;if(this._softDelete){let r=this._findAllRaw(e);if(r.length===0)return[];let{docs:a}=await this._deleteCore("soft",r,e,{save:i,session:s});return a.map(B)}let{docs:n}=await this._deleteCore("hardMany",null,e,{save:i,session:s});return n.map(B)}async _deleteCore(e,t,i,{save:s,session:n}){return this._pipeline.execute({op:"delete",beforeHook:"beforeDelete",afterHook:"afterDelete",hookPayload:{collection:this.name,filter:i},save:s,session:n,afterHookPayload:(r)=>({collection:this.name,filter:i,result:r.length===1?r[0]:r}),mutate:async(r)=>{if(r(),e==="soft"){let o=new Date;for(let c of t)c._deletedAt=o,c.updatedAt=o,this._index.set(c._id,c);return{docs:t}}if(e==="hard"){let o=this._findIndex(i);if(o===-1)return{docs:[]};let c=this._data.splice(o,1)[0];return this._index.delete(c._id),this._removeFromIndex(c),{docs:[c]}}let a=[],l=[];for(let o of this._data)if(J(o,i))a.push(o),this._index.delete(o._id),this._removeFromIndex(o);else l.push(o);return this._data=l,{docs:a}}})}async export(e={},t={}){let{dir:i,name:s,format:n="json"}=t;try{let r=this._data.filter((h)=>J(h,e));if(r.length===0)throw new Ze("ERR_SKALEX_QUERY_EXPORT_EMPTY",`export(): no documents matched the filter in "${this.name}"`,{collection:this.name});let a;if(n==="json")a=JSON.stringify(r,null,2);else{let h=(_)=>{if(_==null)return"";let f=typeof _==="object"?JSON.stringify(_):String(_);return f.includes(",")||f.includes('"')||f.includes(`
|
|
66
|
-
`)||f.includes("\r")?`"${f.replace(/"/g,'""')}"`:f},u=Object.keys(r[0]).map(h).join(","),d=r.map((_)=>Object.values(_).map(h).join(","));a=[u,...d].join(`
|
|
67
|
-
`)}if(typeof this._ctx.fs.writeRaw!=="function")throw new fe("ERR_SKALEX_ADAPTER_NO_RAW_WRITE","export() requires a file-system adapter (FsAdapter). The current adapter does not support raw file writes.");let l=i||`${this._ctx.dataDirectory}/exports`,o=`${s||this.name}.${n}`,c=this._ctx.fs.join(l,o);this._ctx.fs.ensureDir(l),await this._ctx.fs.writeRaw(c,a)}catch(r){throw this._ctx.logger(`Error exporting "${this.name}": ${r.message}`,"error"),r}}_txSnapshotIfNeeded(){let e=this._ctx.txManager;if(!e.active)return;if(this._activeTxId!==e.context?.id)return;e.snapshotIfNeeded(this.name,this._store,(t)=>this._ctx.snapshotCollection(t))}async _saveIfNeeded(e){let t=this._ctx.txManager;if(t.active&&this._activeTxId===t.context?.id)return;if(e??this._ctx.autoSave)await this._ctx.saveCollection(this.name)}_applyValidation(e){if(!this._schema)return e;let t=Fs(e,this._schema,this._strict);if(!t.length)return e;switch(this._onSchemaError){case"warn":return this._ctx.logger(`[${this.name}] Validation warning: ${t.join("; ")}`,"warn"),e;case"strip":return Us(e,this._schema);default:throw new D("ERR_SKALEX_VALIDATION_FAILED",`Validation failed: ${t.join("; ")}`,{errors:t})}}_enforceCapAfterInsert(){let e=this._maxDocs;if(!e||this._data.length<=e)return;let t=this._data.splice(0,this._data.length-e);for(let i of t)this._index.delete(i._id),this._removeFromIndex(i)}_findRaw(e,{includeDeleted:t=!1}={}){if(typeof e==="function"){for(let i of this._data){if(this._softDelete&&i._deletedAt&&!t)continue;if(e(i))return i}return null}if(e._id){let i=this._index.get(e._id)||null;if(!i)return null;if(this._softDelete&&i._deletedAt&&!t)return null;if(Object.keys(e).length>1)return J(i,e)?i:null;return i}if(this._fieldIndex)for(let i in e){if(i==="$or"||i==="$and"||i==="$not")continue;let s=e[i];if(typeof s!=="object"||s===null){let n=this._fieldIndex._lookupIterable(i,s);if(n!==null){for(let r of n){if(this._softDelete&&r._deletedAt&&!t)continue;if(J(r,e))return r}return null}}}for(let i of this._data){if(this._softDelete&&i._deletedAt&&!t)continue;if(J(i,e))return i}return null}_findAllRaw(e,{includeDeleted:t=!1}={}){if(e&&typeof e!=="function"&&e._id){let s=this._index.get(e._id);if(!s)return[];if(this._softDelete&&s._deletedAt&&!t)return[];return J(s,e)?[s]:[]}let i=[];for(let s of this._getCandidates(e)){if(this._softDelete&&s._deletedAt&&!t)continue;if(J(s,e))i.push(s)}return i}_getCandidates(e){if(!this._fieldIndex)return this._data;if(this._fieldIndex._compoundIndexes.size>0){let t={};for(let i in e){if(i==="$or"||i==="$and"||i==="$not")continue;let s=e[i];if(typeof s!=="object"||s===null)t[i]=s}if(Object.keys(t).length>=2){let i=this._fieldIndex.lookupCompound(t);if(i!==null)return i}}for(let t in e){if(t==="$or"||t==="$and"||t==="$not")continue;let i=e[t];if(typeof i!=="object"||i===null){let s=this._fieldIndex._lookupIterable(t,i);if(s!==null)return s}}return this._data}_findIndex(e){for(let t=0;t<this._data.length;t++)if(J(this._data[t],e))return t;return-1}_addToIndex(e){if(this._fieldIndex)this._fieldIndex.add(e)}_removeFromIndex(e){if(this._fieldIndex)this._fieldIndex.remove(e)}_updateInIndex(e,t){if(this._fieldIndex)this._fieldIndex.update(e,t)}async _buildDoc(e,{ttl:t,embed:i}={}){let s={...e,_id:e._id??(this._ctx.idGenerator??Gl)(),createdAt:new Date,updatedAt:new Date},n=t??this._defaultTtl;if(n)s._expiresAt=ic(n);let r=i??this._defaultEmbed;if(r){let a=typeof r==="function"?r(s):s[r];s._vector=await this._ctx.embed(String(a))}if(this._versioning)s._version=1;return s}_projectDoc(e,t){if(t){let s={};for(let n of t){if(n==="_vector")continue;s[n]=e[n]}return s}let i={...e};return delete i._vector,i}async _populateDoc(e,t,i){for(let s of i){let n=await this._ctx.getCollection(s).findOne({_id:t[s]});if(n)e[s]=n}}}class Fi{async read(e){throw Error("StorageAdapter.read() not implemented")}async write(e,t){throw Error("StorageAdapter.write() not implemented")}async delete(e){throw Error("StorageAdapter.delete() not implemented")}async list(){throw Error("StorageAdapter.list() not implemented")}async writeAll(e){for(let{name:t,data:i}of e)await this.write(t,i)}}class yr extends Fi{constructor({dir:e,format:t="gz"}){super();this.dir=Xe.resolve(e),this.format=t,this._ensureDir(this.dir)}_ensureDir(e){if(!K.existsSync(e))K.mkdirSync(e,{recursive:!0})}_filePath(e){return Xe.join(this.dir,`${e}.${this.format}`)}async read(e){let t=this._filePath(e);try{let i=await K.promises.readFile(t);if(this.format==="gz")i=li.inflateSync(i);return i.toString("utf8")}catch(i){if(i.code==="ENOENT")return null;throw i}}async write(e,t){let i=this._filePath(e),s=Xe.join(this.dir,`${e}_${globalThis.crypto.randomUUID()}.tmp.${this.format}`);if(this.format==="gz"){let n=li.deflateSync(t);await K.promises.writeFile(s,n)}else await K.promises.writeFile(s,t,"utf8");await K.promises.rename(s,i)}async writeAll(e){let t=[];try{for(let{name:i,data:s}of e){let n=this._filePath(i),r=Xe.join(this.dir,`${i}_${globalThis.crypto.randomUUID()}.tmp.${this.format}`);if(this.format==="gz"){let a=li.deflateSync(s);await K.promises.writeFile(r,a)}else await K.promises.writeFile(r,s,"utf8");t.push({tmp:r,fp:n})}for(let{tmp:i,fp:s}of t)await K.promises.rename(i,s)}catch(i){for(let{tmp:s}of t)try{await K.promises.unlink(s)}catch{}throw i}}async delete(e){let t=this._filePath(e);try{await K.promises.unlink(t)}catch(i){if(i.code!=="ENOENT")throw i}}async list(){try{let e=await K.promises.readdir(this.dir),t=`.${this.format}`;return e.filter((i)=>i.endsWith(t)&&!i.includes(".tmp.")).map((i)=>i.slice(0,-t.length))}catch(e){if(e.code==="ENOENT")return[];throw e}}join(...e){return Xe.join(...e)}ensureDir(e){this._ensureDir(e)}async writeRaw(e,t){this._ensureDir(Xe.dirname(e)),await K.promises.writeFile(e,t,"utf8")}async readRaw(e){return K.promises.readFile(Xe.resolve(e),"utf8")}}class wr{constructor(){this._migrations=[]}add(e){let{version:t,up:i}=e;if(typeof t!=="number"||t<1)throw new D("ERR_SKALEX_VALIDATION_MIGRATION",`Migration version must be a positive integer, got ${t}`,{version:t});if(typeof i!=="function")throw new D("ERR_SKALEX_VALIDATION_MIGRATION",`Migration version ${t} must have an "up" function`,{version:t});if(this._migrations.some((s)=>s.version===t))throw new D("ERR_SKALEX_VALIDATION_MIGRATION_DUPLICATE",`Migration version ${t} is already registered`,{version:t});this._migrations.push({...e}),this._migrations.sort((s,n)=>s.version-n.version)}async run(e,t=[]){let i=new Set(t),s=this._migrations.filter((n)=>!i.has(n.version));for(let n of s){let r=e(n.version);await n.up(r),i.add(n.version)}return[...i].sort((n,r)=>n-r)}status(e=[]){let t=new Set(e),i=this._migrations.map((s)=>s.version).filter((s)=>!t.has(s));return{current:e.length?Math.max(...e):0,applied:[...t].sort((s,n)=>s-n),pending:i}}}var Bs={[Symbol.iterator](){return{next(){return{done:!0}}}},size:0};function Xs(e){return{[Symbol.iterator](){return e[Symbol.iterator]()},get size(){return e.size}}}function fi(e){return e.map((t)=>{if(t===null||t===void 0)return"\x00";if(typeof t==="boolean")return t?"\x01T":"\x01F";if(typeof t==="number")return`\x02${t}`;return`\x03${String(t)}`}).join("\x1F")}class Pt{constructor(e=[],t=[]){this._fields=new Set,this._compoundFields=[];for(let i of e)if(Array.isArray(i)){for(let s of i)this._validateFieldName(s);this._compoundFields.push(i)}else this._validateFieldName(i),this._fields.add(i);for(let i of t)this._validateFieldName(i);this._uniqueFields=new Set(t),this._indexedFields=new Set([...this._fields,...this._uniqueFields]),this._fieldIndexes=new Map,this._uniqueIndexes=new Map;for(let i of this._fields)this._fieldIndexes.set(i,new Map);for(let i of this._uniqueFields)if(this._uniqueIndexes.set(i,new Map),!this._fieldIndexes.has(i))this._fieldIndexes.set(i,new Map);this._compoundIndexes=new Map;for(let i of this._compoundFields)this._compoundIndexes.set(i.join("\x00"),{fields:i,map:new Map})}get indexedFields(){return this._indexedFields}buildFromData(e){for(let[,t]of this._fieldIndexes)t.clear();for(let[,t]of this._uniqueIndexes)t.clear();for(let[,t]of this._compoundIndexes)t.map.clear();for(let t of e)this._indexDoc(t)}add(e){this._checkUnique(e,null),this._indexDoc(e)}remove(e){for(let[t,i]of this._fieldIndexes){let s=e[t];if(s!==void 0){let n=i.get(s);if(n){if(n.delete(e),n.size===0)i.delete(s)}}}for(let[t,i]of this._uniqueIndexes){let s=e[t];if(s!==void 0)i.delete(s)}for(let[,t]of this._compoundIndexes){let i=fi(t.fields.map((n)=>e[n])),s=t.map.get(i);if(s){if(s.delete(e),s.size===0)t.map.delete(i)}}}update(e,t){this._checkUnique(t,e),this.remove(e);try{this._indexDoc(t)}catch(i){try{this._indexDoc(e)}catch{}throw i}}lookup(e,t){let i=this._fieldIndexes.get(e);if(!i)return null;let s=i.get(t);return s?[...s]:[]}_lookupIterable(e,t){let i=this._fieldIndexes.get(e);if(!i)return null;let s=i.get(t);return s?Xs(s):Bs}lookupCompound(e){let t=Object.keys(e).sort();for(let[,i]of this._compoundIndexes){let s=[...i.fields].sort();if(s.length!==t.length)continue;if(s.every((n,r)=>n===t[r])){let n=fi(i.fields.map((a)=>e[a])),r=i.map.get(n);return r?Xs(r):Bs}}return null}isUniqueTaken(e,t){let i=this._uniqueIndexes.get(e);if(!i)return!1;return i.has(t)}assertUniqueCandidates(e,t){if(this._uniqueFields.size===0)return;let i=new Set(e.map((s)=>s._id));for(let s of this._uniqueFields){let n=new Set,r=this._uniqueIndexes.get(s);if(r){for(let[l,o]of r.entries())if(!i.has(o._id))n.add(l)}let a=new Map;for(let l=0;l<t.length;l++){let o=t[l][s];if(o===void 0)continue;if(n.has(o))throw new Ie("ERR_SKALEX_UNIQUE_VIOLATION",`Unique constraint violation: field "${s}" value "${o}" already exists`,{field:s,value:o});let c=a.get(o);if(c&&c!==e[l]._id)throw new Ie("ERR_SKALEX_UNIQUE_VIOLATION",`Unique constraint violation: field "${s}" value "${o}" already exists`,{field:s,value:o});a.set(o,e[l]._id)}}}assertUniqueBatch(e){if(this._uniqueFields.size===0)return;for(let t of this._uniqueFields){let i=this._uniqueIndexes.get(t),s=new Set;for(let n of e){let r=n[t];if(r===void 0)continue;if(i&&i.has(r))throw new Ie("ERR_SKALEX_UNIQUE_VIOLATION",`Unique constraint violation: field "${t}" value "${r}" already exists`,{field:t,value:r});if(s.has(r))throw new Ie("ERR_SKALEX_UNIQUE_VIOLATION",`Unique constraint violation: duplicate "${t}" value "${r}" within batch`,{field:t,value:r});s.add(r)}}}_validateFieldName(e){if(e.includes("."))throw new D("ERR_SKALEX_VALIDATION_INDEX_DOT_PATH",`Index fields cannot use dot-notation: "${e}". Use a flat field name.`,{field:e})}_indexDoc(e){for(let[t,i]of this._fieldIndexes){let s=e[t];if(s!==void 0){if(!i.has(s))i.set(s,new Set);i.get(s).add(e)}}for(let[t,i]of this._uniqueIndexes){let s=e[t];if(s!==void 0)i.set(s,e)}for(let[,t]of this._compoundIndexes){for(let s of t.fields){let n=e[s];if(n!==void 0&&n!==null&&typeof n==="object")throw new D("ERR_SKALEX_VALIDATION_COMPOUND_INDEX",`Compound index field "${s}" must be a scalar value (string, number, or boolean), got ${Array.isArray(n)?"array":typeof n}`,{field:s})}let i=fi(t.fields.map((s)=>e[s]));if(!t.map.has(i))t.map.set(i,new Set);t.map.get(i).add(e)}}_checkUnique(e,t){for(let i of this._uniqueFields){let s=e[i];if(s===void 0)continue;let n=this._uniqueIndexes.get(i);if(!n)continue;let r=n.get(s);if(r&&r._id!==t?._id)throw new Ie("ERR_SKALEX_UNIQUE_VIOLATION",`Unique constraint violation: field "${i}" value "${s}" already exists`,{field:i,value:s})}}}var Ot="_flush",Nt="migrations";class Ar{constructor({adapter:e,serializer:t,deserializer:i,logger:s,debug:n=!1,lenientLoad:r=!1}){this._adapter=e,this._serializer=t,this._deserializer=i,this._logger=s,this._debug=n,this._lenientLoad=r,this._saveLock=Promise.resolve()}_withSaveLock(e){let t=this._saveLock.then(e);return this._saveLock=t.catch(()=>{}),t}async loadAll(e,{parseSchema:t,buildIndex:i,IndexEngine:s}){try{let n=await this._adapter.list();await Promise.all(n.map(async(r)=>{try{let a=await this._adapter.read(r);if(!a)return;let l=this._deserializer(a),{collectionName:o,data:c}=l;if(!o)return;let h=e[o],u=h?.rawSchema??l.rawSchema??null,d=h?.schema??(u?t(u):null),_=h?.changelog??l.changelog??!1,f=h?.softDelete??l.softDelete??!1,p=h?.versioning??l.versioning??!1,m=h?.strict??l.strict??!1,w=h?.onSchemaError??l.onSchemaError??"throw",v=h?.defaultTtl??l.defaultTtl??null,R=h?.defaultEmbed??l.defaultEmbed??null,E=h?.maxDocs??l.maxDocs??null,A=h?h.fieldIndex:null;if(!A&&d?.uniqueFields?.length)A=new s([],d.uniqueFields);let Ue=i(c,"_id");if(A)A.buildFromData(c);e[o]={collectionName:o,data:c,index:Ue,isSaving:!1,_pendingSave:!1,_dirty:!1,schema:d,rawSchema:u,fieldIndex:A,changelog:_,softDelete:f,versioning:p,strict:m,onSchemaError:w,defaultTtl:v,defaultEmbed:R,maxDocs:E}}catch(a){if(a.code==="ENOENT")return;if(this._lenientLoad){this._logger(`WARNING: Could not load collection "${r}": ${a.message}. Collection will be empty.`,"error");return}throw new Le("ERR_SKALEX_PERSISTENCE_CORRUPT",`Failed to load collection "${r}": ${a.message}`,{collection:r})}})),this._detectIncompleteFlush(e),await this._cleanOrphanTempFiles()}catch(n){if(n.code!=="ENOENT")throw this._logger(`Error loading data: ${n}`,"error"),n}}async save(e,t){if(t)await this._saveOne(e,t);else await Promise.all(Object.keys(e).map((i)=>this._saveOne(e,i)))}async saveDirty(e){let t=Object.keys(e).filter((i)=>e[i]._dirty);if(t.length===0)return;await Promise.all(t.map((i)=>this._saveOne(e,i)))}saveAtomic(e,t){return this._withSaveLock(async()=>{if(t.length===0)return;this._writeFlushSentinel(e,t);let i=new Set(t);i.add("_meta");let s=[...i].filter((n)=>e[n]).map((n)=>({name:n,data:this._serializeCollection(e[n])}));try{await this._adapter.writeAll(s)}catch(n){throw new Le("ERR_SKALEX_PERSISTENCE_FLUSH_FAILED",`Batch save failed during writeAll: ${n.message}`,{collections:t})}for(let n of t)if(e[n])e[n]._dirty=!1;try{this._clearFlushSentinel(e),await this._adapter.write("_meta",this._serializeCollection(e._meta))}catch(n){this._logger(`WARNING: Batch data committed but sentinel clear failed: ${n.message}. Next load will report an incomplete flush (false positive).`,"error")}})}markDirty(e,t){let i=e[t];if(i)i._dirty=!0}_serializeCollection(e){let t={collectionName:e.collectionName,data:e.data};if(e.rawSchema)t.rawSchema=e.rawSchema;if(e.changelog)t.changelog=e.changelog;if(e.softDelete)t.softDelete=e.softDelete;if(e.versioning)t.versioning=e.versioning;if(e.strict)t.strict=e.strict;if(e.onSchemaError!=="throw")t.onSchemaError=e.onSchemaError;if(e.defaultTtl)t.defaultTtl=e.defaultTtl;if(e.defaultEmbed)t.defaultEmbed=e.defaultEmbed;if(e.maxDocs)t.maxDocs=e.maxDocs;return this._serializer(t)}async _saveOne(e,t){let i=e[t];if(!i)return;if(i.isSaving)return i._pendingSave=!0,new Promise((s,n)=>{(i._saveWaiters??=[]).push({resolve:s,reject:n})});i.isSaving=!0,i._pendingSave=!1;try{await this._writeCollection(t,i);while(i._pendingSave)i._pendingSave=!1,await this._writeCollection(t,i);this._resolveSaveWaiters(i)}catch(s){throw this._rejectSaveWaiters(i,s),s}finally{i.isSaving=!1}}async _writeCollection(e,t){try{await this._adapter.write(e,this._serializeCollection(t)),t._dirty=!1}catch(i){throw this._logger(`Error saving "${e}": ${i.message}`,"error"),i}}_resolveSaveWaiters(e){let t=e._saveWaiters?.splice(0)??[];for(let i of t)i.resolve()}_rejectSaveWaiters(e,t){let i=e._saveWaiters?.splice(0)??[];for(let s of i)s.reject(t)}_writeFlushSentinel(e,t){let i=this._getOrCreateMeta(e);i[Ot]={startedAt:new Date().toISOString(),collections:t,completedAt:null}}_clearFlushSentinel(e){let t=this._getOrCreateMeta(e);if(t[Ot])t[Ot].completedAt=new Date().toISOString()}_detectIncompleteFlush(e){let t=e._meta;if(!t)return;let i=t.index.get(Nt);if(!i)return;let s=i[Ot];if(!s)return;if(s.startedAt&&!s.completedAt)this._logger(`WARNING: Incomplete flush detected (started ${s.startedAt}, collections: ${s.collections?.join(", ")}). Data may be inconsistent.`,"error")}async _cleanOrphanTempFiles(){if(typeof this._adapter.join!=="function")return;if(typeof this._adapter.list!=="function")return;try{let e=await import("fs"),t=await import("path"),i=this._adapter.dir;if(!i)return;let s=(await e.default.promises.readdir(i)).filter((n)=>n.includes(".tmp."));for(let n of s){let r=t.default.join(i,n);try{await e.default.promises.unlink(r),this._log(`Cleaned orphan temp file: ${n}`)}catch{}}}catch{}}_getOrCreateMeta(e){if(!e._meta)e._meta={collectionName:"_meta",data:[],index:new Map,isSaving:!1,_pendingSave:!1,_dirty:!1,schema:null,rawSchema:null,fieldIndex:null,changelog:!1,softDelete:!1,versioning:!1,strict:!1,onSchemaError:"throw",defaultTtl:null,defaultEmbed:null,maxDocs:null};let t=e._meta,i=t.index.get(Nt);if(!i)i={_id:Nt},t.data.push(i),t.index.set(Nt,i);return i}_log(e){if(this._debug)this._logger(e,"info")}}var cc=0;class xr{constructor(){this._txLock=Promise.resolve(),this._ctx=null,this._abortedIds=new Set}get active(){return this._ctx!==null&&!this._ctx.aborted}get context(){return this._ctx}async run(e,t,{timeout:i=0}={}){let s=async()=>{await t._ensureConnected();let r=new Set(Object.keys(t.collections)),a={id:++cc,startedAt:Date.now(),aborted:!1,preExisting:r,touchedCollections:new Set,snapshots:new Map,deferredEffects:[],timeout:i};this._ctx=a;let l=null,o=i>0?new Promise((u,d)=>{l=setTimeout(()=>{a.aborted=!0,d(new ye("ERR_SKALEX_TX_TIMEOUT",`Transaction ${a.id} timed out after ${i}ms`))},i)}):null,c=this,h=new Proxy(t,{get(u,d){if(d==="_txId")return a.id;if(a!==c._ctx)throw new ye("ERR_SKALEX_TX_STALE_PROXY",`Transaction ${a.id} has ended. This proxy is no longer usable.`);if(d==="collections")throw new ye("ERR_SKALEX_TX_DIRECT_ACCESS","Direct access to db.collections inside transaction() is not covered by rollback. Use the collection API (db.useCollection) instead.");if(d==="useCollection")return(f)=>{let p=u.useCollection(f);return p._activeTxId=a.id,p};let _=Reflect.get(u,d);return typeof _==="function"?_.bind(u):_}});try{let u=e(h),d=o?await Promise.race([u,o]):await u;if(a.aborted)throw new ye("ERR_SKALEX_TX_ABORTED",`Transaction ${a.id} was aborted`);let _=[...a.touchedCollections];if(_.length>0)await t._persistence.saveAtomic(t.collections,_);for(let f of a.deferredEffects)await f();return d}catch(u){for(let[d,_]of a.snapshots)if(a.preExisting.has(d)){if(t._applySnapshot(d,_),t.collections[d])t.collections[d]._dirty=_._dirty}for(let d in t.collections)if(!a.preExisting.has(d))delete t.collections[d],delete t._collectionInstances[d];throw u}finally{if(l)clearTimeout(l);if(a.aborted)this._abortedIds.add(a.id);this._ctx=null;for(let u in t._collectionInstances){let d=t._collectionInstances[u];if(d._createdInTxId===a.id)d._createdInTxId=null;if(d._activeTxId===a.id)d._activeTxId=null}}},n=this._txLock.then(s);return this._txLock=n.catch(()=>{}),n}snapshotIfNeeded(e,t,i){let s=this._ctx;if(!s)return;if(this._assertCtxNotAborted(s),!s.snapshots.has(e)){let n=i(t);n._dirty=t._dirty??!1,s.snapshots.set(e,n)}s.touchedCollections.add(e)}assertNotAborted(){let e=this._ctx;if(e)this._assertCtxNotAborted(e)}_assertCtxNotAborted(e){if(e.aborted)throw new ye("ERR_SKALEX_TX_ABORTED",`Transaction ${e.id} was aborted. No further mutations allowed.`)}defer(e){if(this._ctx)return this._ctx.deferredEffects.push(e),!0;return!1}}class vr{constructor(e){this._CollectionClass=e,this.stores={},this._instances={}}get(e,t){if(this._instances[e])return this._instances[e];if(!this.stores[e])this.createStore(e);let i=new this._CollectionClass(this.stores[e],t);return this._instances[e]=i,i}create(e,t={},i){this.createStore(e,t);let s=new this._CollectionClass(this.stores[e],i);return this._instances[e]=s,s}createStore(e,{schema:t,indexes:i=[],changelog:s=!1,softDelete:n=!1,versioning:r=!1,strict:a=!1,onSchemaError:l="throw",defaultTtl:o=null,defaultEmbed:c=null,maxDocs:h=null}={}){let u=null,d=null;if(t){u=_r(t);let _=u.uniqueFields;if(i.length||_.length)d=new Pt(i,_)}else if(i.length)d=new Pt(i,[]);this.stores[e]={collectionName:e,data:[],index:new Map,isSaving:!1,_pendingSave:!1,_dirty:!1,schema:u,rawSchema:t||null,fieldIndex:d,changelog:s,softDelete:n,versioning:r,strict:a,onSchemaError:l,defaultTtl:o,defaultEmbed:c,maxDocs:h}}rename(e,t){if(!this.stores[e])throw new Le("ERR_SKALEX_PERSISTENCE_COLLECTION_NOT_FOUND",`Collection "${e}" not found`,{collection:e});if(this.stores[t])throw new Le("ERR_SKALEX_PERSISTENCE_COLLECTION_EXISTS",`Collection "${t}" already exists`,{collection:t});let i=this.stores[e];if(i.collectionName=t,this.stores[t]=i,delete this.stores[e],this._instances[e]){let s=this._instances[e];s.name=t,this._instances[t]=s,delete this._instances[e]}}buildIndex(e,t){let i=new Map;for(let s of e)i.set(s[t],s);return i}inspect(e){if(e){let i=this.stores[e];if(!i)return null;return{name:e,count:i.data.length,schema:i.schema?Object.fromEntries(i.schema.fields):null,indexes:i.fieldIndex?[...i.fieldIndex.indexedFields]:[],softDelete:i.softDelete??!1,versioning:i.versioning??!1,strict:i.strict??!1,onSchemaError:i.onSchemaError??"throw",maxDocs:i.maxDocs??null}}let t={};for(let i in this.stores)t[i]=this.inspect(i);return t}dump(){let e={};for(let t in this.stores)if(!t.startsWith("_"))e[t]=structuredClone(this.stores[t].data);return e}schema(e){let t=this.stores[e];if(!t)return null;if(t.schema)return Object.fromEntries([...t.schema.fields.entries()].map(([i,s])=>[i,s.type]));if(t.data.length>0)return ec(t.data[0]);return null}stats(e){let t=(i)=>{let s=this.stores[i];if(!s)return null;let n=s.data.length,r=0;for(let a of s.data)try{r+=JSON.stringify(a).length}catch(l){}return{collection:i,count:n,estimatedSize:r,avgDocSize:n>0?Math.round(r/n):0}};if(e)return t(e);return Object.keys(this.stores).map(t)}clear(){this.stores={},this._instances={}}}class Ui{async embed(e){throw Error("EmbeddingAdapter.embed() not implemented")}}var he=(e)=>globalThis.process?.env?.[e]??globalThis.Deno?.env?.get(e);class Er extends Ui{constructor({apiKey:e=he("OPENAI_API_KEY"),model:t=he("OPENAI_EMBED_MODEL")??"text-embedding-3-small",baseUrl:i=he("OPENAI_EMBED_BASE_URL")??"https://api.openai.com/v1/embeddings",dimensions:s=he("OPENAI_EMBED_DIMENSIONS")!=null?Number(he("OPENAI_EMBED_DIMENSIONS")):void 0,organization:n=he("OPENAI_ORGANIZATION")??void 0,timeout:r=he("OPENAI_EMBED_TIMEOUT")!=null?Number(he("OPENAI_EMBED_TIMEOUT")):void 0,retries:a=Number(he("OPENAI_EMBED_RETRIES")??0),retryDelay:l=Number(he("OPENAI_EMBED_RETRY_DELAY")??1000),headers:o={},fetch:c=globalThis.fetch}={}){super();if(!e)throw Error("OpenAIEmbeddingAdapter requires an apiKey");this.apiKey=e,this.model=t,this.baseUrl=i,this.dimensions=s,this.organization=n,this.timeout=r,this.retries=a,this.retryDelay=l,this.headers=o,this._fetch=c}async embed(e){let t;for(let i=0;i<=this.retries;i++){let s=this.timeout!=null?new AbortController:null,n=s?setTimeout(()=>s.abort(),this.timeout):null;try{let r=await this._fetch(this.baseUrl,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`,...this.organization&&{"OpenAI-Organization":this.organization},...this.headers},body:JSON.stringify({input:e,model:this.model,...this.dimensions!==void 0&&{dimensions:this.dimensions}}),...s&&{signal:s.signal}});if(!r.ok){let a=(await r.text()).slice(0,200);throw Error(`OpenAI embedding API error ${r.status}: ${a}`)}return(await r.json()).data[0].embedding}catch(r){if(t=r,i<this.retries)await new Promise((a)=>setTimeout(a,this.retryDelay*2**i))}finally{if(n!==null)clearTimeout(n)}}throw t}}var at=(e)=>globalThis.process?.env?.[e]??globalThis.Deno?.env?.get(e);class Sr extends Ui{constructor({model:e=at("OLLAMA_EMBED_MODEL")??"nomic-embed-text",host:t=at("OLLAMA_HOST")??"http://localhost:11434",timeout:i=at("OLLAMA_EMBED_TIMEOUT")!=null?Number(at("OLLAMA_EMBED_TIMEOUT")):void 0,retries:s=Number(at("OLLAMA_EMBED_RETRIES")??0),retryDelay:n=Number(at("OLLAMA_EMBED_RETRY_DELAY")??1000),headers:r={},fetch:a=globalThis.fetch}={}){super();this.model=e,this.host=t,this.timeout=i,this.retries=s,this.retryDelay=n,this.headers=r,this._fetch=a}async embed(e){let t;for(let i=0;i<=this.retries;i++){let s=this.timeout!=null?new AbortController:null,n=s?setTimeout(()=>s.abort(),this.timeout):null;try{let r=await this._fetch(`${this.host}/api/embeddings`,{method:"POST",headers:{"Content-Type":"application/json",...this.headers},body:JSON.stringify({model:this.model,prompt:e}),...s&&{signal:s.signal}});if(!r.ok){let a=(await r.text()).slice(0,200);throw Error(`Ollama embedding API error ${r.status}: ${a}`)}return(await r.json()).embedding}catch(r){if(t=r,i<this.retries)await new Promise((a)=>setTimeout(a,this.retryDelay*2**i))}finally{if(n!==null)clearTimeout(n)}}throw t}}class zt{async generate(e,t){throw Error("LLMAdapter.generate() not implemented")}async summarize(e){throw Error("LLMAdapter.summarize() not implemented")}}var zi=`You are a database query assistant. Convert the user's natural language request into a JSON filter object for a document database.
|
|
68
|
-
|
|
69
|
-
Rules:
|
|
70
|
-
- Return ONLY a valid JSON object, nothing else
|
|
71
|
-
- Use standard query operators: $eq, $ne, $gt, $gte, $lt, $lte, $in, $nin, $regex
|
|
72
|
-
- For simple equality, use plain key-value pairs: {"field": "value"}
|
|
73
|
-
- For numeric comparisons use operators: {"age": {"$gt": 18}}
|
|
74
|
-
- For text search use $regex: {"name": {"$regex": "pattern"}}
|
|
75
|
-
- Return {} to match all documents
|
|
76
|
-
- No explanations, no markdown, no code fences`,Bi="Summarise the following memory entries into one concise paragraph. Preserve all important facts.",P=(e)=>globalThis.process?.env?.[e]??globalThis.Deno?.env?.get(e);class Ir extends zt{constructor({apiKey:e=P("OPENAI_API_KEY"),model:t=P("OPENAI_MODEL")??"gpt-4o-mini",baseUrl:i=P("OPENAI_BASE_URL")??"https://api.openai.com/v1/chat/completions",maxTokens:s=P("OPENAI_MAX_TOKENS")!=null?Number(P("OPENAI_MAX_TOKENS")):void 0,temperature:n=Number(P("OPENAI_TEMPERATURE")??0.3),topP:r=P("OPENAI_TOP_P")!=null?Number(P("OPENAI_TOP_P")):void 0,organization:a=P("OPENAI_ORGANIZATION")??void 0,timeout:l=P("OPENAI_TIMEOUT")!=null?Number(P("OPENAI_TIMEOUT")):void 0,retries:o=Number(P("OPENAI_RETRIES")??0),retryDelay:c=Number(P("OPENAI_RETRY_DELAY")??1000),headers:h={},fetch:u=globalThis.fetch,seed:d=P("OPENAI_SEED")!=null?Number(P("OPENAI_SEED")):void 0,generatePrompt:_=zi,summarizePrompt:f=Bi}={}){super();if(!e)throw Error("OpenAILLMAdapter requires an apiKey");this.apiKey=e,this.model=t,this.baseUrl=i,this.maxTokens=s,this.temperature=n,this.topP=r,this.organization=a,this.timeout=l,this.retries=o,this.retryDelay=c,this.headers=h,this._fetch=u,this.seed=d,this.generatePrompt=_,this.summarizePrompt=f}async generate(e,t){let i=await this._post({model:this.model,messages:[{role:"system",content:`${this.generatePrompt}
|
|
77
|
-
Schema: ${JSON.stringify(e)}`},{role:"user",content:t}],response_format:{type:"json_object"},temperature:0,...this.maxTokens!==void 0&&{max_tokens:this.maxTokens},...this.seed!==void 0&&{seed:this.seed}});return JSON.parse(i.choices[0].message.content)}async summarize(e){return(await this._post({model:this.model,messages:[{role:"system",content:this.summarizePrompt},{role:"user",content:e}],temperature:this.temperature,...this.maxTokens!==void 0&&{max_tokens:this.maxTokens},...this.topP!==void 0&&{top_p:this.topP},...this.seed!==void 0&&{seed:this.seed}})).choices[0].message.content.trim()}async _post(e){let t;for(let i=0;i<=this.retries;i++){let s=this.timeout!=null?new AbortController:null,n=s?setTimeout(()=>s.abort(),this.timeout):null;try{let r=await this._fetch(this.baseUrl,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`,...this.organization&&{"OpenAI-Organization":this.organization},...this.headers},body:JSON.stringify(e),...s&&{signal:s.signal}});if(!r.ok){let a=(await r.text()).slice(0,200);throw Error(`OpenAI API error ${r.status}: ${a}`)}return r.json()}catch(r){if(t=r,i<this.retries)await new Promise((a)=>setTimeout(a,this.retryDelay*2**i))}finally{if(n!==null)clearTimeout(n)}}throw t}}var G=(e)=>globalThis.process?.env?.[e]??globalThis.Deno?.env?.get(e);class br extends zt{constructor({apiKey:e=G("ANTHROPIC_API_KEY"),model:t=G("ANTHROPIC_MODEL")??"claude-haiku-4-5",baseUrl:i=G("ANTHROPIC_BASE_URL")??"https://api.anthropic.com/v1/messages",apiVersion:s="2023-06-01",maxTokens:n=Number(G("ANTHROPIC_MAX_TOKENS")??1024),temperature:r=Number(G("ANTHROPIC_TEMPERATURE")??0.3),topP:a=G("ANTHROPIC_TOP_P")!=null?Number(G("ANTHROPIC_TOP_P")):void 0,topK:l=G("ANTHROPIC_TOP_K")!=null?Number(G("ANTHROPIC_TOP_K")):void 0,timeout:o=G("ANTHROPIC_TIMEOUT")!=null?Number(G("ANTHROPIC_TIMEOUT")):void 0,retries:c=Number(G("ANTHROPIC_RETRIES")??0),retryDelay:h=Number(G("ANTHROPIC_RETRY_DELAY")??1000),headers:u={},fetch:d=globalThis.fetch,generatePrompt:_=zi,summarizePrompt:f=Bi}={}){super();if(!e)throw Error("AnthropicLLMAdapter requires an apiKey");this.apiKey=e,this.model=t,this.baseUrl=i,this.apiVersion=s,this.maxTokens=n,this.temperature=r,this.topP=a,this.topK=l,this.timeout=o,this.retries=c,this.retryDelay=h,this.headers=u,this._fetch=d,this.generatePrompt=_,this.summarizePrompt=f}async generate(e,t){let i=(await this._post({model:this.model,max_tokens:this.maxTokens,temperature:0,system:`${this.generatePrompt}
|
|
78
|
-
Schema: ${JSON.stringify(e)}`,messages:[{role:"user",content:t}]})).content[0].text.trim().replace(/^```(?:json)?\n?/,"").replace(/\n?```$/,"");return JSON.parse(i)}async summarize(e){return(await this._post({model:this.model,max_tokens:this.maxTokens,temperature:this.temperature,...this.topP!==void 0&&{top_p:this.topP},...this.topK!==void 0&&{top_k:this.topK},system:this.summarizePrompt,messages:[{role:"user",content:e}]})).content[0].text.trim()}async _post(e){let t;for(let i=0;i<=this.retries;i++){let s=this.timeout!=null?new AbortController:null,n=s?setTimeout(()=>s.abort(),this.timeout):null;try{let r=await this._fetch(this.baseUrl,{method:"POST",headers:{"Content-Type":"application/json","x-api-key":this.apiKey,"anthropic-version":this.apiVersion,...this.headers},body:JSON.stringify(e),...s&&{signal:s.signal}});if(!r.ok){let a=(await r.text()).slice(0,200);throw Error(`Anthropic API error ${r.status}: ${a}`)}return r.json()}catch(r){if(t=r,i<this.retries)await new Promise((a)=>setTimeout(a,this.retryDelay*2**i))}finally{if(n!==null)clearTimeout(n)}}throw t}}var ae=(e)=>globalThis.process?.env?.[e]??globalThis.Deno?.env?.get(e);class Tr extends zt{constructor({model:e=ae("OLLAMA_MODEL")??"llama3.2",host:t=ae("OLLAMA_HOST")??"http://localhost:11434",temperature:i=Number(ae("OLLAMA_TEMPERATURE")??0.3),topP:s=ae("OLLAMA_TOP_P")!=null?Number(ae("OLLAMA_TOP_P")):void 0,topK:n=ae("OLLAMA_TOP_K")!=null?Number(ae("OLLAMA_TOP_K")):void 0,timeout:r=ae("OLLAMA_TIMEOUT")!=null?Number(ae("OLLAMA_TIMEOUT")):void 0,retries:a=Number(ae("OLLAMA_RETRIES")??0),retryDelay:l=Number(ae("OLLAMA_RETRY_DELAY")??1000),headers:o={},fetch:c=globalThis.fetch,generatePrompt:h=zi,summarizePrompt:u=Bi}={}){super();this.model=e,this.host=t,this.temperature=i,this.topP=s,this.topK=n,this.timeout=r,this.retries=a,this.retryDelay=l,this.headers=o,this._fetch=c,this.generatePrompt=h,this.summarizePrompt=u}async generate(e,t){let i=`${this.generatePrompt}
|
|
79
|
-
Schema: ${JSON.stringify(e)}
|
|
80
|
-
Query: ${t}`,s=await this._post({model:this.model,prompt:i,format:"json",options:{temperature:0},stream:!1});return JSON.parse(s.response)}async summarize(e){return(await this._post({model:this.model,prompt:`${this.summarizePrompt}
|
|
81
|
-
|
|
82
|
-
${e}`,options:{temperature:this.temperature,...this.topP!==void 0&&{top_p:this.topP},...this.topK!==void 0&&{top_k:this.topK}},stream:!1})).response.trim()}async _post(e){let t;for(let i=0;i<=this.retries;i++){let s=this.timeout!=null?new AbortController:null,n=s?setTimeout(()=>s.abort(),this.timeout):null;try{let r=await this._fetch(`${this.host}/api/generate`,{method:"POST",headers:{"Content-Type":"application/json",...this.headers},body:JSON.stringify(e),...s&&{signal:s.signal}});if(!r.ok){let a=(await r.text()).slice(0,200);throw Error(`Ollama API error ${r.status}: ${a}`)}return r.json()}catch(r){if(t=r,i<this.retries)await new Promise((a)=>setTimeout(a,this.retryDelay*2**i))}finally{if(n!==null)clearTimeout(n)}}throw t}}function hc({provider:e,apiKey:t,embedModel:i,model:s,host:n,embedBaseUrl:r,dimensions:a,organization:l,embedTimeout:o,embedRetries:c,embedRetryDelay:h}){let u=i||s;switch(e){case"openai":return new Er({apiKey:t,model:u,baseUrl:r,...a!==void 0&&{dimensions:a},...l!==void 0&&{organization:l},...o!==void 0&&{timeout:o},...c!==void 0&&{retries:c},...h!==void 0&&{retryDelay:h}});case"ollama":return new Sr({model:u,host:n,...o!==void 0&&{timeout:o},...c!==void 0&&{retries:c},...h!==void 0&&{retryDelay:h}});default:throw new fe("ERR_SKALEX_ADAPTER_UNKNOWN_PROVIDER",`Unknown AI provider: "${e}". Supported: "openai", "ollama".`,{provider:e})}}function dc({provider:e,apiKey:t,model:i,host:s,baseUrl:n,apiVersion:r,temperature:a,maxTokens:l,topP:o,topK:c,organization:h,timeout:u,retries:d,retryDelay:_,seed:f,generatePrompt:p,summarizePrompt:m}){if(!i)return null;switch(e){case"openai":return new Ir({apiKey:t,model:i,baseUrl:n,...l!==void 0&&{maxTokens:l},...a!==void 0&&{temperature:a},...o!==void 0&&{topP:o},...h!==void 0&&{organization:h},...u!==void 0&&{timeout:u},...d!==void 0&&{retries:d},..._!==void 0&&{retryDelay:_},...f!==void 0&&{seed:f},...p!==void 0&&{generatePrompt:p},...m!==void 0&&{summarizePrompt:m}});case"anthropic":return new br({apiKey:t,model:i,baseUrl:n,apiVersion:r,...l!==void 0&&{maxTokens:l},...a!==void 0&&{temperature:a},...o!==void 0&&{topP:o},...c!==void 0&&{topK:c},...u!==void 0&&{timeout:u},...d!==void 0&&{retries:d},..._!==void 0&&{retryDelay:_},...p!==void 0&&{generatePrompt:p},...m!==void 0&&{summarizePrompt:m}});case"ollama":return new Tr({model:i,host:s,...a!==void 0&&{temperature:a},...o!==void 0&&{topP:o},...c!==void 0&&{topK:c},...u!==void 0&&{timeout:u},...d!==void 0&&{retries:d},..._!==void 0&&{retryDelay:_},...p!==void 0&&{generatePrompt:p},...m!==void 0&&{summarizePrompt:m}});default:return null}}var pi="AES-GCM",gt=12,be=32,uc=new TextEncoder,_c=new TextDecoder;class Or extends Fi{constructor(e,t){super();if(this._adapter=e,this._rawKey=typeof t==="string"?fc(t):Uint8Array.from(t),this._rawKey.length!==be)throw new fe("ERR_SKALEX_ADAPTER_INVALID_KEY",`EncryptedAdapter: key must be ${be} bytes (${be*2} hex chars), got ${this._rawKey.length}`);this._cryptoKey=null}async read(e){let t=await this._adapter.read(e);if(!t)return null;return this._decrypt(t)}async write(e,t){return this._adapter.write(e,await this._encrypt(t))}async delete(e){return this._adapter.delete(e)}async list(){return this._adapter.list()}async writeAll(e){let t=[];for(let{name:i,data:s}of e)t.push({name:i,data:await this._encrypt(s)});return this._adapter.writeAll(t)}join(...e){return this._adapter.join?.(...e)}ensureDir(e){return this._adapter.ensureDir?.(e)}async writeRaw(e,t){return this._adapter.writeRaw?.(e,await this._encrypt(t))}async readRaw(e){let t=await this._adapter.readRaw?.(e);if(!t)return null;return this._decrypt(t)}async _getKey(){if(!this._cryptoKey)this._cryptoKey=await globalThis.crypto.subtle.importKey("raw",this._rawKey,{name:pi},!1,["encrypt","decrypt"]);return this._cryptoKey}async _encrypt(e){let t=await this._getKey(),i=globalThis.crypto.getRandomValues(new Uint8Array(gt)),s=uc.encode(e),n=await globalThis.crypto.subtle.encrypt({name:pi,iv:i,tagLength:128},t,s),r=new Uint8Array(gt+n.byteLength);return r.set(i,0),r.set(new Uint8Array(n),gt),pc(r)}async _decrypt(e){let t=await this._getKey(),i=mc(e),s=i.slice(0,gt),n=i.slice(gt),r=await globalThis.crypto.subtle.decrypt({name:pi,iv:s,tagLength:128},t,n);return _c.decode(r)}}function fc(e){if(e.length!==be*2)throw new fe("ERR_SKALEX_ADAPTER_INVALID_KEY",`EncryptedAdapter: hex key must be ${be*2} characters (${be} bytes)`);if(!/^[0-9a-fA-F]+$/.test(e))throw new fe("ERR_SKALEX_ADAPTER_INVALID_KEY","EncryptedAdapter: hex key contains invalid characters");let t=new Uint8Array(be);for(let i=0;i<be;i++)t[i]=parseInt(e.slice(i*2,i*2+2),16);return t}function pc(e){let t="";for(let i=0;i<e.length;i++)t+=String.fromCharCode(e[i]);return globalThis.btoa(t)}function mc(e){let t=globalThis.atob(e),i=new Uint8Array(t.length);for(let s=0;s<t.length;s++)i[s]=t.charCodeAt(s);return i}var gc=4;class Nr{constructor(e,t){this.sessionId=e,this._db=t,this._col=t.useCollection(`_memory_${e}`)}async remember(e){let t=await this._col.insertOne({text:e,sessionId:this.sessionId},{embed:"text"}),i=this._db._memoryConfig?.maxEntries;if(i&&this._col._data.length>i)await this.compress({threshold:0});return t}async recall(e,{limit:t=10,minScore:i=0}={}){return this._col.search(e,{limit:t,minScore:i})}async history({since:e,limit:t}={}){let i=e?{createdAt:{$gte:new Date(e)}}:{},s={sort:{createdAt:1}};if(t)s.limit=t;let{docs:n}=await this._col.find(i,s);return n}async forget(e){return this._col.deleteOne({_id:e})}tokenCount(){let e=this._col._data;return{tokens:e.reduce((t,i)=>t+this._docTokens(i),0),count:e.length}}context({tokens:e=this._db._memoryConfig?.contextTokens??4000}={}){let t=this._sortedData("desc"),i=[],s=0;for(let n of t){let r=this._docTokens(n);if(s+r>e)break;i.push(n.text),s+=r}return i.reverse().join(`
|
|
83
|
-
`)}async compress({threshold:e=this._db._memoryConfig?.compressionThreshold??8000,keepRecent:t=this._db._memoryConfig?.keepRecent??10}={}){let{tokens:i}=this.tokenCount();if(i<=e)return;if(!this._db._aiAdapter)throw Error('memory.compress() requires a language model adapter. Configure { ai: { model: "..." } }.');let s=this._sortedData("asc"),n=Math.max(0,s.length-t),r=s.slice(0,n);if(r.length===0)return;let a=r.map((o)=>o.text).join(`
|
|
84
|
-
`),l=await this._db._aiAdapter.summarize(a);await this._col.deleteMany({_id:{$in:r.map((o)=>o._id)}}),await this._col.insertOne({text:l,sessionId:this.sessionId,compressed:!0})}_docTokens(e){return Math.ceil((e.text||"").length/gc)}_sortedData(e){return[...this._col._data].sort(e==="asc"?(t,i)=>new Date(t.createdAt)-new Date(i.createdAt):(t,i)=>new Date(i.createdAt)-new Date(t.createdAt))}}class $r{constructor(e){this._db=e,this._restoring=!1}get _col(){return this._db.useCollection("_changelog")}async log(e,t,i,s=null,n=null){if(this._restoring)return;let r={op:e,collection:t,docId:i._id,doc:{...i},timestamp:new Date};if(s)r.prev={...s};if(n)r.session=n;await this._col.insertOne(r)}async query(e,{since:t,limit:i,session:s}={}){let n={collection:e};if(t)n.timestamp={$gte:new Date(t)};if(s)n.session=s;let r={sort:{timestamp:1}};if(i)r.limit=i;let{docs:a}=await this._col.find(n,r);return a}async restore(e,t,{_id:i}={}){let s=new Date(t),n=this._db.useCollection(e),r=(await this.query(e,{})).filter((l)=>new Date(l.timestamp)<=s);if(i){let l=r.filter((c)=>c.docId===i);if(l.length===0)return;let o=l[l.length-1];this._restoring=!0;try{if(o.op==="delete"){if(await n.findOne({_id:i}))await n.deleteOne({_id:i});return}if(await n.findOne({_id:i})){let{_id:c,createdAt:h,updatedAt:u,...d}=o.doc;await n.updateOne({_id:i},d)}else await n.insertOne({...o.doc})}finally{this._restoring=!1}await this._db.saveData(e);return}let a=new Map;for(let l of r)if(l.op==="insert"||l.op==="update")a.set(l.docId,{doc:l.doc,deleted:!1});else if(l.op==="delete")a.set(l.docId,{doc:null,deleted:!0});this._restoring=!0;try{await n.deleteMany({});for(let[,{doc:l,deleted:o}]of a)if(!o&&l)await n.insertOne({...l})}finally{this._restoring=!1}await this._db.saveData(e)}}function yc(e){let t=5381;for(let i=0;i<e.length;i++)t=(t<<5)+t+e.charCodeAt(i)|0;return(t>>>0).toString(16).padStart(8,"0")}class Dr{constructor({maxSize:e=500,ttl:t=0}={}){this._cache=new Map,this._maxSize=e,this._ttl=t}_key(e,t,i){return yc(JSON.stringify({collectionName:e,schema:t,query:i}))}get(e,t,i){let s=this._key(e,t,i),n=this._cache.get(s);if(!n)return;if(this._ttl>0&&Date.now()-n.ts>this._ttl){this._cache.delete(s);return}return n.filter}set(e,t,i,s){let n=this._key(e,t,i);if(this._cache.size>=this._maxSize&&!this._cache.has(n))this._cache.delete(this._cache.keys().next().value);this._cache.set(n,{filter:s,ts:Date.now()})}toJSON(){return Object.fromEntries([...this._cache.entries()].map(([e,t])=>[e,t]))}fromJSON(e){if(!e||typeof e!=="object")return;for(let[t,i]of Object.entries(e))this._cache.set(t,i)}get size(){return this._cache.size}}function wc(e,{regexMaxLength:t=500}={}){if(typeof e!=="object"||e===null)return e;let i={};for(let s of Object.keys(e)){let n=e[s];if(n&&typeof n==="object"&&!Array.isArray(n)){let r={};for(let a of Object.keys(n))if(a==="$regex"&&typeof n.$regex==="string"){if(n.$regex.length>t)throw Error(`$regex pattern too long (max ${t} characters)`);if(/\([^)]*[+*][^)]*\)[+*{]/.test(n.$regex))throw Error("$regex pattern rejected: nested quantifiers risk ReDoS");try{r.$regex=new RegExp(n.$regex)}catch{throw Error(`invalid $regex pattern: "${n.$regex}"`)}}else if(["$gt","$gte","$lt","$lte"].includes(a)&&Ac(n[a]))r[a]=new Date(n[a]);else r[a]=n[a];i[s]=r}else i[s]=n}return i}function Ac(e){if(typeof e!=="string")return!1;return/\d{4}-\d{2}-\d{2}/.test(e)&&!isNaN(Date.parse(e))}function xc(e,t){let i=[];if(!t||typeof e!=="object"||e===null)return i;for(let s of Object.keys(e)){if(s.startsWith("$"))continue;if(!(s.split(".")[0]in t))i.push(`Unknown field referenced in generated filter: "${s}"`)}return i}class Cr{constructor(){this._listeners=new Map}on(e,t){if(!this._listeners.has(e))this._listeners.set(e,new Set);return this._listeners.get(e).add(t),()=>this._listeners.get(e)?.delete(t)}off(e,t){this._listeners.get(e)?.delete(t)}emit(e,t){for(let i of[e,"*"]){let s=this._listeners.get(i);if(!s)continue;for(let n of s)try{n(t)}catch(r){}}}removeAll(e){if(e)this._listeners.delete(e);else this._listeners.clear()}listenerCount(e){return this._listeners.get(e)?.size??0}}class Rr{constructor({threshold:e=100,maxEntries:t=500}={}){this._threshold=e,this._maxEntries=t,this._entries=[]}record({collection:e,op:t,filter:i,query:s,duration:n,resultCount:r}){if(n<this._threshold)return;let a={collection:e,op:t,duration:n,resultCount:r,timestamp:new Date};if(i!==void 0)a.filter=i;if(s!==void 0)a.query=s;if(this._entries.push(a),this._entries.length>this._maxEntries)this._entries.shift()}entries({limit:e,minDuration:t,collection:i}={}){let s=this._entries;if(i)s=s.filter((n)=>n.collection===i);if(t)s=s.filter((n)=>n.duration>=t);if(e)s=s.slice(-e);return s}get size(){return this._entries.length}clear(){this._entries=[]}}class kr{constructor(){this._sessions=new Map}_ensure(e){if(!this._sessions.has(e))this._sessions.set(e,{reads:0,writes:0,lastActive:null});return this._sessions.get(e)}recordRead(e){if(!e)return;let t=this._ensure(e);t.reads++,t.lastActive=new Date}recordWrite(e){if(!e)return;let t=this._ensure(e);t.writes++,t.lastActive=new Date}get(e){let t=this._sessions.get(e);if(!t)return null;return{sessionId:e,...t}}all(){return[...this._sessions.entries()].map(([e,t])=>({sessionId:e,...t}))}clear(e){if(e)this._sessions.delete(e);else this._sessions.clear()}}class Lr{constructor(){this._plugins=[]}register(e){if(typeof e!=="object"||e===null)throw TypeError("Plugin must be a non-null object.");this._plugins.push(e)}async run(e,t){for(let i of this._plugins)if(typeof i[e]==="function")await i[e](t)}get size(){return this._plugins.length}}function Ye(e){if(typeof e!=="string"||!e.trim())throw Error("collection name must be a non-empty string");if(/[/\\]/.test(e)||e.includes("..")||e.includes("\x00"))throw Error(`invalid collection name: "${e}"`);if(e.trim().startsWith("_"))throw Error(`access to system collection "${e}" is not permitted`);return e.trim()}var Hs=[{name:"skalex_collections",description:"List all collection names in the database.",inputSchema:{type:"object",properties:{}},scope:"read"},{name:"skalex_schema",description:"Return the schema for a collection as a { field: type } map. Returns null if the collection is empty.",inputSchema:{type:"object",properties:{collection:{type:"string",description:"Collection name."}},required:["collection"]},scope:"read"},{name:"skalex_find",description:"Find documents in a collection that match a filter.",inputSchema:{type:"object",properties:{collection:{type:"string",description:"Collection name."},filter:{type:"object",description:"Query filter (MongoDB-style operators supported)."},limit:{type:"number",description:"Maximum number of results. Default: 20."},sort:{type:"object",description:"Sort descriptor: { field: 1 } for ascending, { field: -1 } for descending."}},required:["collection"]},scope:"read"},{name:"skalex_insert",description:"Insert a single document into a collection.",inputSchema:{type:"object",properties:{collection:{type:"string",description:"Collection name."},doc:{type:"object",description:"Document to insert."}},required:["collection","doc"]},scope:"write"},{name:"skalex_update",description:"Update the first document matching a filter.",inputSchema:{type:"object",properties:{collection:{type:"string",description:"Collection name."},filter:{type:"object",description:"Query filter to identify the document."},update:{type:"object",description:"Fields to update (direct assignment, $inc, $push supported)."},many:{type:"boolean",description:"If true, update all matching documents. Default: false."}},required:["collection","filter","update"]},scope:"write"},{name:"skalex_delete",description:"Delete the first document matching a filter.",inputSchema:{type:"object",properties:{collection:{type:"string",description:"Collection name."},filter:{type:"object",description:"Query filter to identify the document."},many:{type:"boolean",description:"If true, delete all matching documents. Default: false."}},required:["collection","filter"]},scope:"write"},{name:"skalex_search",description:"Semantic similarity search. Requires an embedding adapter to be configured.",inputSchema:{type:"object",properties:{collection:{type:"string",description:"Collection name."},query:{type:"string",description:"Natural-language query string to embed and compare."},limit:{type:"number",description:"Maximum number of results. Default: 10."},minScore:{type:"number",description:"Minimum cosine similarity score [0, 1]. Default: 0."},filter:{type:"object",description:"Optional structured pre-filter (hybrid search)."}},required:["collection","query"]},scope:"read"},{name:"skalex_ask",description:"Translate a natural-language question into a filter and query a collection. Requires a language model adapter.",inputSchema:{type:"object",properties:{collection:{type:"string",description:"Collection name."},question:{type:"string",description:"Natural-language question about the data."},limit:{type:"number",description:"Maximum number of results. Default: 20."}},required:["collection","question"]},scope:"read"}];async function vc(e,t,i){switch(e){case"skalex_collections":return Object.keys(i.collections).filter((s)=>!s.startsWith("_"));case"skalex_schema":return i.schema(Ye(t.collection))??null;case"skalex_find":{let s=i.useCollection(Ye(t.collection)),n={};if(t.limit)n.limit=t.limit;if(t.sort)n.sort=t.sort;return s.find(t.filter||{},n)}case"skalex_insert":return i.useCollection(Ye(t.collection)).insertOne(t.doc||{});case"skalex_update":{let s=i.useCollection(Ye(t.collection));if(t.many)return s.updateMany(t.filter||{},t.update||{});return s.updateOne(t.filter||{},t.update||{})}case"skalex_delete":{let s=i.useCollection(Ye(t.collection));if(t.many)return s.deleteMany(t.filter||{});return s.deleteOne(t.filter||{})}case"skalex_search":return i.useCollection(Ye(t.collection)).search(t.query,{limit:t.limit??10,minScore:t.minScore??0,filter:t.filter});case"skalex_ask":return i.ask(Ye(t.collection),t.question,{limit:t.limit??20});default:throw Object.assign(Error(`Unknown tool: ${e}`),{code:"NOT_FOUND"})}}var Xi="2.0",Hi=-32700,Ec=-32600,Vs=-32601,Sc=-32602,Ic=-32603;function ot(e,t){return{jsonrpc:Xi,id:e,result:t}}function De(e,t,i,s){return{jsonrpc:Xi,id:e,error:{code:t,message:i}}}function bc(e){try{let t=JSON.parse(e);if(typeof t!=="object"||t===null||t.jsonrpc!==Xi)return{parseError:De(null,Ec,"Invalid JSON-RPC request")};return{msg:t}}catch(t){return{parseError:De(null,Hi,"Parse error")}}}function Tc(e){return{content:[{type:"text",text:e}]}}function Js(e){return{content:[{type:"text",text:e}],isError:!0}}class Pr{constructor({port:e=3000,host:t="127.0.0.1",allowedOrigin:i=null,maxBodySize:s=1048576}={}){this._port=e,this._host=t,this._allowedOrigin=i,this._maxBodySize=s,this._clients=new Set,this._onMessage=null,this._server=null}onMessage(e){this._onMessage=e}send(e){let t=`data: ${JSON.stringify(e)}
|
|
85
|
-
|
|
86
|
-
`;for(let i of this._clients)try{i.write(t)}catch(s){this._clients.delete(i)}}start(){return new Promise((e,t)=>{this._server=Co.createServer((i,s)=>{if(this._allowedOrigin)s.setHeader("Access-Control-Allow-Origin",this._allowedOrigin),s.setHeader("Access-Control-Allow-Methods","GET, POST, OPTIONS"),s.setHeader("Access-Control-Allow-Headers","Content-Type");if(i.method==="OPTIONS"){s.writeHead(204),s.end();return}if(i.method==="GET"&&i.url==="/sse")this._handleSSE(i,s);else if(i.method==="POST"&&i.url==="/message")this._handleMessage(i,s);else s.writeHead(404,{"Content-Type":"text/plain"}),s.end("Not Found")}),this._server.on("error",t),this._server.listen(this._port,this._host,()=>e())})}stop(){return new Promise((e)=>{if(!this._server){e();return}for(let t of this._clients)try{t.end()}catch(i){}this._clients.clear(),this._server.close(()=>e()),this._server=null})}_handleSSE(e,t){t.writeHead(200,{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}),t.write(`event: endpoint
|
|
87
|
-
data: /message
|
|
88
|
-
|
|
89
|
-
`),this._clients.add(t),e.on("close",()=>{this._clients.delete(t)})}async _handleMessage(e,t){let i="";e.setEncoding("utf8"),e.on("data",(s)=>{if(i+=s,i.length>this._maxBodySize)e.destroy(),t.writeHead(413,{"Content-Type":"application/json"}),t.end(JSON.stringify({error:"Request body too large"}))}),e.on("end",async()=>{t.writeHead(202,{"Content-Type":"application/json"}),t.end("{}");let s;try{s=JSON.parse(i)}catch(n){this.send(De(null,Hi,"Parse error"));return}if(this._onMessage)await this._onMessage(s).catch(()=>{})})}get port(){return this._port}get host(){return this._host}get url(){return`http://${this._host}:${this._port}`}get sseUrl(){return`${this.url}/sse`}}class Mr{constructor(){this._onMessage=null,this._buffer="",this._started=!1}onMessage(e){this._onMessage=e}send(e){process.stdout.write(JSON.stringify(e)+`
|
|
90
|
-
`)}start(){if(this._started)return;this._started=!0,process.stdin.setEncoding("utf8"),process.stdin.on("data",(e)=>{this._buffer+=e;let t;while((t=this._buffer.indexOf(`
|
|
91
|
-
`))!==-1){let i=this._buffer.slice(0,t).trim();if(this._buffer=this._buffer.slice(t+1),i&&this._onMessage){let s;try{s=JSON.parse(i)}catch(n){this.send(De(null,Hi,"Parse error"));continue}this._onMessage(s).catch(()=>{})}}}),process.stdin.on("end",()=>{process.exit(0)})}stop(){process.stdin.removeAllListeners("data"),process.stdin.removeAllListeners("end"),this._started=!1}}var Oc={name:"skalex",version:"4.0.0-alpha"},Nc="2024-11-05";class jr{constructor(e,t={}){this._db=e,this._transport=t.transport||"stdio",this._port=t.port||3000,this._host=t.host||"127.0.0.1",this._allowedOrigin=t.allowedOrigin??null,this._maxBodySize=t.maxBodySize??1048576,this._scopes=t.scopes||{"*":["read"]},this._t=null}async listen(){if(this._transport==="http")this._t=new Pr({port:this._port,host:this._host,allowedOrigin:this._allowedOrigin,maxBodySize:this._maxBodySize});else this._t=new Mr;this._t.onMessage((e)=>this._handleMessage(e)),await this._t.start()}async connect(e){if(this._t=e,this._t.onMessage((t)=>this._handleMessage(t)),typeof this._t.start==="function")await this._t.start()}async close(){if(this._t&&typeof this._t.stop==="function")await this._t.stop();this._t=null}get transport(){return this._transport}get url(){return this._t?.url}async _handleMessage(e){let{msg:t,parseError:i}=typeof e==="string"?bc(e):{msg:e};if(i){this._send(i);return}let{id:s,method:n,params:r}=t;if(s===void 0){if(n==="notifications/initialized")return;return}try{switch(n){case"initialize":this._send(ot(s,{protocolVersion:Nc,capabilities:{tools:{}},serverInfo:Oc}));break;case"tools/list":this._send(ot(s,{tools:this._visibleTools()}));break;case"tools/call":await this._handleToolCall(s,r);break;case"ping":this._send(ot(s,{}));break;default:this._send(De(s,Vs,`Method not found: ${n}`))}}catch(a){this._send(De(s,Ic,a.message||"Internal error"))}}async _handleToolCall(e,t){let i=t?.name,s=t?.arguments??{};if(!i){this._send(De(e,Sc,"tools/call requires params.name"));return}let n=Hs.find((a)=>a.name===i);if(!n){this._send(De(e,Vs,`Unknown tool: ${i}`));return}let r=s.collection||s.collection_name||null;if(!this._hasScope(r,n.scope)){this._send(ot(e,Js(`Access denied: "${i}" requires "${n.scope}" scope on collection "${r}".`)));return}try{let a=await vc(i,s,this._db);this._send(ot(e,Tc(JSON.stringify(a,null,2))))}catch(a){this._send(ot(e,Js(a.message||String(a))))}}_visibleTools(){return Hs.filter((e)=>{let t=this._scopes["*"];if(t&&(t.includes(e.scope)||t.includes("admin")))return!0;for(let[,i]of Object.entries(this._scopes))if(i.includes(e.scope)||i.includes("admin"))return!0;return!1})}_hasScope(e,t){let i=(s)=>s.includes("admin")||s.includes(t);if(e&&this._scopes[e])return i(this._scopes[e]);if(this._scopes["*"])return i(this._scopes["*"]);return!1}_send(e){this._t?.send(e)}}var $t="migrations",Ys=(e)=>JSON.stringify(e,function(t,i){let s=this[t];if(s instanceof Date)return{__skalex_date__:s.toISOString()};if(typeof i==="bigint")return{__skalex_bigint__:i.toString()};return i}),Ws=(e)=>JSON.parse(e,(t,i)=>{if(i&&typeof i==="object"){if("__skalex_bigint__"in i)return BigInt(i.__skalex_bigint__);if("__skalex_date__"in i)return new Date(i.__skalex_date__)}return i});class ie{constructor({path:e="./.db",format:t="gz",debug:i=!1,adapter:s,ai:n,encrypt:r,slowQueryLog:a,queryCache:l,memory:o,logger:c,plugins:h,llmAdapter:u,embeddingAdapter:d,regexMaxLength:_,idGenerator:f,serializer:p,deserializer:m,autoSave:w,ttlSweepInterval:v,lenientLoad:R}={}){if(this.dataDirectory=e,this.dataFormat=t,this.debug=i,!ie._errorsAttached)ie.SkalexError=qe,ie.ValidationError=D,ie.UniqueConstraintError=Ie,ie.TransactionError=ye,ie.PersistenceError=Le,ie.AdapterError=fe,ie.QueryError=Ze,ie._errorsAttached=!0;this._adapterConfig=s??null;let E=s||new yr({dir:e,format:t});if(r)E=new Or(E,r.key);if(this.fs=E,this._registry=new vr(gr),this.collections=this._registry.stores,this._collectionInstances=this._registry._instances,this._migrations=new wr,this._connectPromise=null,this.isConnected=!1,this._aiConfig=n||null,this._encryptConfig=r||null,this._pluginsConfig=Array.isArray(h)?h:null,this._memoryConfig=o||null,this._regexMaxLength=_??500,this._idGenerator=f??null,this._serializer=p??Ys,this._deserializer=m??Ws,this._autoSave=w??!1,this._txManager=new xr,this._ttlSweepInterval=v??0,this._ttlTimer=null,this._logger=c??qs,this._embeddingAdapter=d??(n?hc(n):null),this._aiAdapter=u??(n?dc(n):null),this._persistence=new Ar({adapter:this.fs,serializer:this._serializer,deserializer:this._deserializer,logger:this._logger,debug:this.debug,lenientLoad:R??!1}),this._changeLog=new $r(this),this._queryCache=new Dr(l||{}),this._eventBus=new Cr,this._queryLog=a?new Rr(a):null,this._sessionStats=new kr,this._plugins=new Lr,Array.isArray(h))for(let A of h)this._plugins.register(A)}async connect(){if(this._connectPromise)return this._connectPromise;return this._connectPromise=this._doConnect(),this._connectPromise}async _doConnect(){try{await this.loadData();let e=this._getMeta();if(e.queryCache)this._queryCache.fromJSON(e.queryCache);if(this._migrations._migrations.length>0){let t=e.appliedVersions||[],i=await this._migrations.run((s)=>this.useCollection(`_migration_${s}`),t);this._saveMeta({appliedVersions:i})}if(this._sweepTtl(),this._ttlSweepInterval>0){if(this._ttlTimer=setInterval(()=>this._sweepTtl(),this._ttlSweepInterval),this._ttlTimer?.unref)this._ttlTimer.unref()}this.isConnected=!0,this._log("> - Connected to the database (\u221A)")}catch(e){throw this._logger(`Error connecting to the database: ${e}`,"error"),e}}async disconnect(){try{if(this._ttlTimer)clearInterval(this._ttlTimer),this._ttlTimer=null;await this.saveData(),this._registry.clear(),this.collections=this._registry.stores,this._collectionInstances=this._registry._instances,this._connectPromise=null,this.isConnected=!1,this._log("> - Disconnected from the database (\u221A)")}catch(e){throw this._logger(`Error disconnecting from the database: ${e}`,"error"),e}}async _ensureConnected(){if(this.isConnected)return;return this.connect()}useCollection(e){return this._registry.get(e,this)}createCollection(e,t={}){return this._registry.create(e,t,this)}_createCollectionStore(e,t={}){this._registry.createStore(e,t)}async renameCollection(e,t){this._registry.rename(e,t),await this.saveData(t),await this.fs.delete(e)}async loadData(){await this._persistence.loadAll(this.collections,{parseSchema:_r,buildIndex:this._registry.buildIndex,IndexEngine:Pt});for(let e in this._collectionInstances){let t=this.collections[e];if(t&&this._collectionInstances[e]._store!==t)this._collectionInstances[e]._store=t}}async saveData(e){await this._persistence.save(this.collections,e)}buildIndex(e,t){return this._registry.buildIndex(e,t)}addMigration(e){this._migrations.add(e)}migrationStatus(){let e=this._getMeta();return this._migrations.status(e.appliedVersions||[])}namespace(e){if(this._adapterConfig)throw new fe("ERR_SKALEX_ADAPTER_NAMESPACE_REQUIRES_FS","namespace() requires the default FsAdapter. When a custom storage adapter is configured, create a separate Skalex instance with your adapter instead.");let t=String(e).replace(/[^a-zA-Z0-9_-]/g,"_");if(!t)throw new D("ERR_SKALEX_VALIDATION_NAMESPACE_ID","namespace: id must contain at least one alphanumeric character",{id:e});return new ie({path:`${this.dataDirectory}/${t}`,format:this.dataFormat,debug:this.debug,ai:this._aiConfig||void 0,encrypt:this._encryptConfig||void 0,slowQueryLog:this._queryLog?{threshold:this._queryLog._threshold,maxEntries:this._queryLog._maxEntries}:void 0,queryCache:this._queryCache?{maxSize:this._queryCache._maxSize,ttl:this._queryCache._ttl}:void 0,plugins:this._pluginsConfig||void 0,memory:this._memoryConfig||void 0,logger:this._logger!==qs?this._logger:void 0,llmAdapter:this._aiAdapter&&!this._aiConfig?this._aiAdapter:void 0,embeddingAdapter:this._embeddingAdapter&&!this._aiConfig?this._embeddingAdapter:void 0,regexMaxLength:this._regexMaxLength!==500?this._regexMaxLength:void 0,idGenerator:this._idGenerator||void 0,serializer:this._serializer!==Ys?this._serializer:void 0,deserializer:this._deserializer!==Ws?this._deserializer:void 0,autoSave:this._autoSave||void 0,ttlSweepInterval:this._ttlSweepInterval||void 0})}use(e){this._plugins.register(e)}watch(e){return this._eventBus.on("*",e)}sessionStats(e){if(e!==void 0)return this._sessionStats.get(e);return this._sessionStats.all()}get _inTransaction(){return this._txManager.active}_emitEvent(e,t){if(!this._txManager.defer(()=>this._eventBus.emit(e,t)))this._eventBus.emit(e,t)}async _runAfterHook(e,t){if(!this._txManager.defer(()=>this._plugins.run(e,t)))await this._plugins.run(e,t)}async _logChange(e,t,i,s,n){if(!this._txManager.defer(()=>this._changeLog.log(e,t,i,s,n)))await this._changeLog.log(e,t,i,s,n)}async transaction(e,t={}){return this._txManager.run(e,this,t)}async seed(e,{reset:t=!1}={}){for(let[i,s]of Object.entries(e)){if(t&&this.collections[i])this._applySnapshot(i,{data:[],index:new Map}),delete this._collectionInstances[i];await this.useCollection(i).insertMany(s)}await this.saveData()}dump(){return this._registry.dump()}inspect(e){return this._registry.inspect(e)}async import(e){let t=await this.fs.readRaw(e),i;try{i=JSON.parse(t)}catch{throw new Le("ERR_SKALEX_PERSISTENCE_INVALID_JSON",`import: invalid JSON in file "${e}"`,{filePath:e})}let s=e.split("/").pop().replace(/\.[^.]+$/,"");return this.useCollection(s).insertMany(Array.isArray(i)?i:[i],{save:!0})}async embed(e){if(!this._embeddingAdapter)throw new fe("ERR_SKALEX_ADAPTER_EMBEDDING_REQUIRED","db.embed() requires an AI adapter. Pass { ai: { provider, apiKey } } to the Skalex constructor.");return this._embeddingAdapter.embed(e)}async ask(e,t,{limit:i=20}={}){if(!this._aiAdapter)throw new fe("ERR_SKALEX_ADAPTER_LLM_REQUIRED",'db.ask() requires a language model adapter. Configure { ai: { provider, model: "..." } }.');let s=this.useCollection(e),n=this.schema(e),r=this._queryCache.get(e,n,t);if(!r){r=await this._aiAdapter.generate(n,t);let a=xc(r,n);if(a.length)a.forEach((l)=>this._log(`[ask] ${l}`));this._queryCache.set(e,n,t,r),this._saveMeta({queryCache:this._queryCache.toJSON()})}return s.find(wc(r,{regexMaxLength:this._regexMaxLength}),{limit:i})}schema(e){return this._registry.schema(e)}useMemory(e){return new Nr(e,this)}changelog(){return this._changeLog}async restore(e,t,i={}){return this._changeLog.restore(e,t,i)}stats(e){return this._registry.stats(e)}slowQueries(e={}){if(!this._queryLog)return[];return this._queryLog.entries(e)}slowQueryCount(){return this._queryLog?this._queryLog.size:0}clearSlowQueries(){this._queryLog?.clear()}mcp(e={}){return new jr(this,e)}_getMeta(){let e=this.collections._meta;if(!e)return{};return e.index.get($t)||{}}_saveMeta(e){if(!this.collections._meta)this._createCollectionStore("_meta");let t=this.collections._meta,i=t.index.get($t);if(i)Object.assign(i,e);else{let s={_id:$t,...e};t.data.push(s),t.index.set($t,s)}this._persistence.markDirty(this.collections,"_meta")}_sweepTtl(){for(let e in this.collections){let t=this.collections[e],i=sc(t.data,t.index,t.fieldIndex?(s)=>t.fieldIndex.remove(s):null);if(i>0)this._persistence.markDirty(this.collections,e),this._log(`TTL sweep: removed ${i} expired docs from "${e}"`)}}_buildCollectionContext(){let e=this;return{ensureConnected:()=>e._ensureConnected(),get txManager(){return e._txManager},get plugins(){return e._plugins},get eventBus(){return e._eventBus},get sessionStats(){return e._sessionStats},get queryLog(){return e._queryLog},get logger(){return e._logger},get persistence(){return e._persistence},get collections(){return e.collections},embed:(t)=>e.embed(t),get idGenerator(){return e._idGenerator},get autoSave(){return e._autoSave},saveCollection:(t)=>e.saveData(t),snapshotCollection:(t)=>e._snapshotCollection(t),getCollection:(t)=>e.useCollection(t),emitEvent:(t,i)=>e._emitEvent(t,i),runAfterHook:(t,i)=>e._runAfterHook(t,i),logChange:(t,i,s,n,r)=>e._logChange(t,i,s,n,r),get fs(){return e.fs},get dataDirectory(){return e.dataDirectory}}}_log(e){if(this.debug)this._logger(e,"info")}_snapshotCollection(e){return{data:structuredClone(e.data)}}_applySnapshot(e,t){let i=this.collections[e];if(!i)return;if(i.data=t.data,i.index=this.buildIndex(t.data,"_id"),i.fieldIndex)i.fieldIndex.buildFromData(t.data)}}function $c(){let e=Date.now().toString(16),t;try{t=Ko.randomBytes(8).toString("hex")}catch{let i=new Uint8Array(8);crypto.getRandomValues(i),t=Array.from(i).map((s)=>s.toString(16).padStart(2,"0")).join("")}return`${e}${t}`}function Qs(e,t){let i=e instanceof Error?e.message:e;if(t==="error")console.error(i);else console.log(i)}var Gs=new Set(["__proto__","constructor","prototype"]);function Bt(e,t){if(!t.includes(".")){if(Gs.has(t))return;return e[t]}let i=e;for(let s of t.split(".")){if(Gs.has(s))return;if(i==null)return;i=i[s]}return i}class Ke extends Error{constructor(e,t,i={}){super(t);this.name=this.constructor.name,this.code=e,this.details=i}}class C extends Ke{}class Te extends Ke{}class we extends Ke{}class Pe extends Ke{}class pe extends Ke{}class et extends Ke{}function Si(e,t){if(e===t)return!0;if(e==null||t==null)return!1;if(typeof e!==typeof t)return!1;if(e instanceof Date&&t instanceof Date)return e.getTime()===t.getTime();if(e instanceof RegExp&&t instanceof RegExp)return e.source===t.source&&e.flags===t.flags;if(Array.isArray(e)){if(!Array.isArray(t)||e.length!==t.length)return!1;for(let i=0;i<e.length;i++)if(!Si(e[i],t[i]))return!1;return!0}if(typeof e==="object"){if(Array.isArray(t))return!1;let i=Object.keys(e);if(i.length!==Object.keys(t).length)return!1;for(let s of i){if(!Object.prototype.hasOwnProperty.call(t,s))return!1;if(!Si(e[s],t[s]))return!1}return!0}return!1}function Y(e,t){if(typeof t==="function")return t(e);if(t==null)return!0;if(typeof t==="object"&&Object.keys(t).length===0)return!0;if("$or"in t){let i=t.$or;if(!Array.isArray(i))throw new et("ERR_SKALEX_QUERY_INVALID_OPERATOR","$or must be an array of filters",{operator:"$or"});if(!i.some((s)=>Y(e,s)))return!1}if("$and"in t){let i=t.$and;if(!Array.isArray(i))throw new et("ERR_SKALEX_QUERY_INVALID_OPERATOR","$and must be an array of filters",{operator:"$and"});if(!i.every((s)=>Y(e,s)))return!1}if("$not"in t){if(Y(e,t.$not))return!1}for(let i in t){if(i==="$or"||i==="$and"||i==="$not")continue;let s=t[i],n;try{n=Bt(e,i)}catch{return!1}if(s instanceof RegExp){if(!s.test(String(n)))return!1}else if(typeof s==="object"&&s!==null){if(Object.keys(s).some((r)=>r.startsWith("$"))){if("$eq"in s&&n!==s.$eq)return!1;if("$ne"in s&&n===s.$ne)return!1;if("$gt"in s&&!(n>s.$gt))return!1;if("$lt"in s&&!(n<s.$lt))return!1;if("$gte"in s&&!(n>=s.$gte))return!1;if("$lte"in s&&!(n<=s.$lte))return!1;if("$in"in s&&!s.$in.includes(n))return!1;if("$nin"in s&&s.$nin.includes(n))return!1;if("$regex"in s){if(!(s.$regex instanceof RegExp?s.$regex:new RegExp(s.$regex)).test(String(n)))return!1}if("$fn"in s&&!s.$fn(n))return!1}else if(!Si(n,s))return!1}else if(n!==s)return!1}return!0}function Dc(e,t=new Set){if(typeof e!=="object"||e===null||typeof e==="function")return e;let i=[],s=[],n=[],r=[],a=[];for(let o in e){if(o==="$or"||o==="$and"||o==="$not"){a.push(o);continue}let c=e[o];if(t.has(o))i.push(o);else if(c instanceof RegExp||typeof c==="object"&&c!==null&&(("$regex"in c)||("$fn"in c))||typeof c==="function")r.push(o);else if(typeof c==="object"&&c!==null&&(("$gt"in c)||("$lt"in c)||("$gte"in c)||("$lte"in c)||("$ne"in c)||("$in"in c)||("$nin"in c)))n.push(o);else s.push(o)}let l={};for(let o of[...i,...s,...n,...r,...a])l[o]=e[o];return l}var Ii=new Set(["string","number","boolean","object","array","date","any"]);function Vi(e){if(Array.isArray(e))return"array";if(e instanceof Date)return"date";return typeof e}function qr(e){let t=new Map,i=[];for(let[s,n]of Object.entries(e)){let r;if(typeof n==="string"){if(!Ii.has(n))throw new C("ERR_SKALEX_VALIDATION_UNKNOWN_TYPE",`Unknown schema type "${n}" for field "${s}"`,{field:s,type:n});r={type:n,required:!1,unique:!1}}else if(typeof n==="object"&&n!==null){let{type:a="any",required:l=!1,unique:o=!1,enum:c}=n;if(!Ii.has(a))throw new C("ERR_SKALEX_VALIDATION_UNKNOWN_TYPE",`Unknown schema type "${a}" for field "${s}"`,{field:s,type:a});if(r={type:a,required:l,unique:o,enum:c},o)i.push(s)}else throw new C("ERR_SKALEX_VALIDATION_INVALID_SCHEMA",`Invalid schema definition for field "${s}"`,{field:s});t.set(s,r)}return{fields:t,uniqueFields:i}}function Zs(e,t,i=!1){let s=[];for(let[n,r]of t){let a=e[n],l=a===void 0||a===null;if(r.required&&l){s.push(`Field "${n}" is required`);continue}if(l)continue;if(r.type!=="any"){let o=Vi(a);if(o!==r.type)s.push(`Field "${n}" must be of type "${r.type}", got "${o}"`)}if(r.enum&&!r.enum.includes(a))s.push(`Field "${n}" must be one of [${r.enum.map((o)=>JSON.stringify(o)).join(", ")}], got ${JSON.stringify(a)}`)}if(i){for(let n of Object.keys(e))if(!n.startsWith("_")&&!t.has(n))s.push(`Unknown field "${n}" (strict mode)`)}return s}function en(e,t){let i={};for(let[s,n]of Object.entries(e)){if(s.startsWith("_")){i[s]=n;continue}if(!t.has(s))continue;let r=t.get(s);if(r.type!=="any"){if(Vi(n)!==r.type)continue}if(r.enum&&!r.enum.includes(n))continue;i[s]=n}return i}function Cc(e){let t={};for(let[i,s]of Object.entries(e)){if(i.startsWith("_"))continue;let n=Vi(s);t[i]=Ii.has(n)?n:"any"}return t}function Rc(e){if(typeof e==="number"){if(e<=0)throw new C("ERR_SKALEX_VALIDATION_TTL",`TTL must be positive, got ${e}`,{ttl:e});return e*1000}if(typeof e!=="string")throw new C("ERR_SKALEX_VALIDATION_TTL",`Invalid TTL value: ${e}`,{ttl:e});let t=e.match(/^(\d+(?:\.\d+)?)(ms|s|m|h|d)$/);if(!t)throw new C("ERR_SKALEX_VALIDATION_TTL_FORMAT",`Invalid TTL format: "${e}". Use e.g. 300 (seconds), "30m", "24h", "7d"`,{ttl:e});let i=parseFloat(t[1]);if(i<=0)throw new C("ERR_SKALEX_VALIDATION_TTL",`TTL must be positive, got "${e}"`,{ttl:e});let s=t[2],n=i*{ms:1,s:1000,m:60000,h:3600000,d:86400000}[s];if(!isFinite(n))throw new C("ERR_SKALEX_VALIDATION_TTL",`TTL value "${e}" is too large`,{ttl:e});return n}function kc(e){return new Date(Date.now()+Rc(e))}function Lc(e,t,i=null){let s=Date.now(),n=0,r=e.length;while(r--){let a=e[r];if(a._expiresAt&&new Date(a._expiresAt).getTime()<=s){if(e.splice(r,1),t.delete(a._id),i)i(a);n++}}return n}function tn(e,t){if(e.length!==t.length)throw new et("ERR_SKALEX_QUERY_VECTOR_MISMATCH",`Vector dimension mismatch: ${e.length} vs ${t.length}`,{expected:e.length,got:t.length});let i=0,s=0,n=0;for(let a=0;a<e.length;a++)i+=e[a]*t[a],s+=e[a]*e[a],n+=t[a]*t[a];let r=Math.sqrt(s*n);return r===0?0:i/r}function X(e){if(!("_vector"in e))return{...e};let{_vector:t,...i}=e;return i}function Pc(e){return e.length}function Mc(e,t){let i=0;for(let s of e){let n=Bt(s,t);if(typeof n==="number"&&!isNaN(n))i+=n}return i}function jc(e,t){let i=0,s=0;for(let n of e){let r=Bt(n,t);if(typeof r==="number"&&!isNaN(r))i+=r,s++}return s===0?null:i/s}function qc(e,t){let i=Object.create(null);for(let s of e){let n=String(Bt(s,t)??"__null__");if(!i[n])i[n]=[];i[n].push(s)}return i}class Kr{constructor(e){this._col=e}get _ctx(){return this._col._ctx}async execute({op:e,beforeHook:t,afterHook:i,hookPayload:s,mutate:n,afterHookPayload:r,save:a,session:l}){let o=this._ctx;await o.ensureConnected(),this._col._txSnapshotIfNeeded();let c=o.txManager,h=c.active&&this._col._activeTxId===c.context?.id,u=h?c.context.id:null,d=this._col._createdInTxId,_=()=>{if(u!==null&&c._abortedIds.has(u))throw new we("ERR_SKALEX_TX_ABORTED",`Transaction ${u} was aborted. No further mutations allowed.`);if(d!==null&&c._abortedIds.has(d))throw new we("ERR_SKALEX_TX_ABORTED",`Transaction ${d} was aborted. Collection obtained inside that transaction cannot be used for further mutations.`)};if(h||d!==null)_();if(t)await o.plugins.run(t,s);let{docs:f,prevDocs:p=[]}=await n(_);if(o.persistence.markDirty(o.collections,this._col.name),await this._col._saveIfNeeded(a),this._col._changelogEnabled)for(let m=0;m<f.length;m++)await o.logChange(e,this._col.name,f[m],p[m]??null,l||null);if(!h||!c.defer(()=>o.sessionStats.recordWrite(l)))o.sessionStats.recordWrite(l);for(let m of f)o.emitEvent(this._col.name,{op:e,collection:this._col.name,doc:X(m)});if(i)if(r)await o.runAfterHook(i,r(f));else for(let m of f)await o.runAfterHook(i,{collection:this._col.name,doc:X(m)});return{docs:f,prevDocs:p}}}function Kc(e){let t={};for(let i in e){if(i.startsWith("$"))continue;let s=e[i];if(s&&typeof s==="object"&&!Array.isArray(s)){let n=Object.keys(s);if(n.length===1&&n[0]==="$eq")t[i]=s.$eq}else t[i]=s}return t}var Fr=new Set(["__proto__","constructor","prototype"]);function Ur(e){if(typeof e!=="object"||e===null||Array.isArray(e))return e;let t={};for(let i of Object.keys(e)){if(Fr.has(i))continue;t[i]=Ur(e[i])}return t}class zr{constructor(e,t){this.name=e.collectionName,this.database=t,this._ctx=t._buildCollectionContext(),this._store=e,this._pipeline=new Kr(this),this._createdInTxId=t._txManager.context?.id??null,this._activeTxId=null}get _data(){return this._store.data}set _data(e){this._store.data=e}get _index(){return this._store.index}get _fieldIndex(){return this._store.fieldIndex||null}get _schema(){return this._store.schema?this._store.schema.fields:null}get _changelogEnabled(){return this._store.changelog===!0}get _softDelete(){return this._store.softDelete===!0}get _versioning(){return this._store.versioning===!0}get _strict(){return this._store.strict===!0}get _onSchemaError(){return this._store.onSchemaError??"throw"}get _defaultTtl(){return this._store.defaultTtl||null}get _defaultEmbed(){return this._store.defaultEmbed||null}get _maxDocs(){return this._store.maxDocs||null}async insertOne(e,t={}){let{save:i,ifNotExists:s,ttl:n,embed:r,session:a}=t;if(s){await this._ctx.ensureConnected();let o=this._findRaw(e);if(o)return X({...o})}let{docs:l}=await this._insertCore([e],{ttl:n,embed:r,session:a,save:i});return X(l[0])}async insertMany(e,t={}){let{save:i,ttl:s,embed:n,session:r}=t,{docs:a}=await this._insertCore(e,{ttl:s,embed:n,session:r,save:i});return a.map(X)}async _insertCore(e,{ttl:t,embed:i,session:s,save:n}){return this._pipeline.execute({op:"insert",beforeHook:null,afterHook:"afterInsert",hookPayload:null,save:n,session:s,mutate:async(r)=>{let a=[],l=new Set;for(let o of e){let c=this._applyValidation(o);await this._ctx.plugins.run("beforeInsert",{collection:this.name,doc:c});let h=await this._buildDoc(c,{ttl:t,embed:i});if(this._index.has(h._id)||l.has(h._id))throw new Te("ERR_SKALEX_UNIQUE_DUPLICATE_ID",`Duplicate _id "${h._id}" in collection "${this.name}"`,{id:h._id,collection:this.name});l.add(h._id),a.push(h)}if(r(),this._fieldIndex)this._fieldIndex.assertUniqueBatch(a);for(let o of a)this._addToIndex(o);this._data.push(...a);for(let o of a)this._index.set(o._id,o);return this._enforceCapAfterInsert(),{docs:a}}})}async updateOne(e,t,i={}){await this._ctx.ensureConnected();let s=this._findRaw(e);if(!s)return null;let{save:n,session:r}=i,{docs:a}=await this._updateCore([s],e,t,{save:n,session:r});return X(a[0])}async updateMany(e,t,i={}){await this._ctx.ensureConnected();let s=this._findAllRaw(e);if(s.length===0)return[];let{save:n,session:r}=i,{docs:a}=await this._updateCore(s,e,t,{save:n,session:r});return a.map(X)}async _updateCore(e,t,i,{save:s,session:n}){return this._pipeline.execute({op:"update",beforeHook:"beforeUpdate",afterHook:"afterUpdate",hookPayload:{collection:this.name,filter:t,update:i},save:s,session:n,afterHookPayload:(r)=>({collection:this.name,filter:t,update:i,result:r.length===1?r[0]:r}),mutate:async(r)=>{let a=this._changelogEnabled?e.map((h)=>structuredClone(h)):e.map(()=>null),l=e.map((h)=>this._prepareUpdatedDoc(h,i));this._assertUniqueCandidates(e,l),r();let o=new Set(e),c=new Map;for(let h=0;h<this._data.length;h++)if(o.has(this._data[h]))c.set(this._data[h],h);for(let h=0;h<e.length;h++)this._commitUpdatedDoc(e[h],l[h],c);return{docs:l,prevDocs:a}}})}applyUpdate(e,t){for(let i in t){if(Fr.has(i))continue;if(i==="_id"||i==="createdAt")continue;let s=t[i];if(Array.isArray(s))e[i]=s;else if(typeof s==="object"&&s!==null){let n=Object.keys(s);if(n.some((r)=>r.startsWith("$"))){for(let r of n)if(r==="$inc"&&typeof e[i]==="number")e[i]+=s[r];else if(r==="$push"){if(!Array.isArray(e[i]))e[i]=[];e[i].push(s[r])}}else e[i]=Ur(s)}else e[i]=s}if(e.updatedAt=new Date,this._versioning)e._version=(e._version??0)+1;return e}_prepareUpdatedDoc(e,t){let i=structuredClone(e),s=structuredClone(e);if(this.applyUpdate(s,t),!this._schema)return s;let n=Zs(s,this._schema,this._strict);if(!n.length)return s;switch(this._onSchemaError){case"throw":throw new C("ERR_SKALEX_VALIDATION_UPDATE",`Update validation failed on doc "${e._id}": ${n.join("; ")}`,{id:e._id,errors:n});case"strip":return this._stripCandidateToValid(s,i);default:return this._ctx.logger(`[${this.name}] Update validation warning: ${n.join("; ")}`,"warn"),s}}_stripCandidateToValid(e,t){let i=en(e,this._schema),s=structuredClone(t);for(let[n,r]of Object.entries(i)){if(n.startsWith("_"))continue;s[n]=r}if(s.updatedAt=e.updatedAt,this._versioning)s._version=e._version;return s}_commitUpdatedDoc(e,t,i){let s=i?i.get(e):this._data.indexOf(e);if(s===void 0||s===-1)throw new Pe("ERR_SKALEX_PERSISTENCE_DOC_MISSING",`Document "${e._id}" no longer exists in collection "${this.name}"`,{id:e._id,collection:this.name});this._data[s]=t,this._index.set(t._id,t),this._updateInIndex(e,t)}_assertUniqueCandidates(e,t){this._fieldIndex?.assertUniqueCandidates(e,t)}async upsert(e,t,i={}){if(await this._ctx.ensureConnected(),this._findRaw(e))return this.updateOne(e,t,i);return this.insertOne({...Kc(e),...t},i)}async upsertMany(e,t,i={}){await this._ctx.ensureConnected();let{save:s,...n}=i,r=[];for(let a of e)r.push(await this.upsert({[t]:a[t]},a,{...n,save:!1}));return await this._saveIfNeeded(s),r}async restore(e,t={}){if(!this._softDelete)throw new et("ERR_SKALEX_QUERY_SOFT_DELETE_REQUIRED",`restore() requires softDelete on "${this.name}"`,{collection:this.name});await this._ctx.ensureConnected();let{save:i,session:s}=t,n=this._findRaw(e,{includeDeleted:!0});if(!n||!n._deletedAt)return null;let{docs:r}=await this._pipeline.execute({op:"restore",beforeHook:null,afterHook:null,hookPayload:null,save:i,session:s,mutate:async(a)=>{return a(),delete n._deletedAt,n.updatedAt=new Date,this._index.set(n._id,n),{docs:[n]}}});return X(r[0])}watch(e,t){if(typeof e==="function")t=e,e=null;if(t)return this._ctx.eventBus.on(this.name,(i)=>{if(!e||Y(i.doc,e))t(i)});return this._watchIterator(e)}_watchIterator(e){let t=[],i=null,s=!1,n=this._ctx.eventBus.on(this.name,(r)=>{if(e&&!Y(r.doc,e))return;if(i){let a=i;i=null,a({value:r,done:!1})}else t.push(r)});return{[Symbol.asyncIterator](){return this},next(){if(t.length>0)return Promise.resolve({value:t.shift(),done:!1});if(s)return Promise.resolve({value:void 0,done:!0});return new Promise((r)=>{i=r})},return(){if(s=!0,n(),i){let r=i;i=null,r({value:void 0,done:!0})}return Promise.resolve({value:void 0,done:!0})}}}async count(e={}){return await this._ctx.ensureConnected(),Pc(this._findAllRaw(e))}async sum(e,t={}){return await this._ctx.ensureConnected(),Mc(this._findAllRaw(t),e)}async avg(e,t={}){return await this._ctx.ensureConnected(),jc(this._findAllRaw(t),e)}async groupBy(e,t={}){return await this._ctx.ensureConnected(),qc(this._findAllRaw(t),e)}async findOne(e,t={}){await this._ctx.ensureConnected();let{populate:i,select:s,includeDeleted:n=!1}=t,r=this._findRaw(e,{includeDeleted:n});if(!r)return null;let a=this._projectDoc(r,s);if(i)await this._populateDoc(a,r,i);return a}async find(e,t={}){await this._ctx.ensureConnected();let i=Date.now(),{populate:s,select:n,sort:r,page:a=1,limit:l,session:o,includeDeleted:c=!1}=t;await this._ctx.plugins.run("beforeFind",{collection:this.name,filter:e,options:t});let h=this._getCandidates(e),u=Dc(e,this._fieldIndex?this._fieldIndex.indexedFields:new Set),d=[];for(let f of h){if(this._softDelete&&f._deletedAt&&!c)continue;if(!Y(f,u))continue;let p=this._projectDoc(f,n);if(s)await this._populateDoc(p,f,s);d.push(p)}if(r){let f=Object.keys(r);d.sort((p,m)=>{for(let w of f){let v=r[w];if(p[w]<m[w])return-v;if(p[w]>m[w])return v}return 0})}let _;if(l){let f=d.length,p=Math.ceil(f/l),m=(a-1)*l;d=d.slice(m,m+l),_={page:a,totalDocs:f,totalPages:p}}return this._ctx.queryLog?.record({collection:this.name,op:"find",filter:e,duration:Date.now()-i,resultCount:d.length}),this._ctx.sessionStats.recordRead(o),await this._ctx.plugins.run("afterFind",{collection:this.name,filter:e,options:t,docs:d}),_?{docs:d,..._}:{docs:d}}async search(e,{filter:t,limit:i=10,minScore:s=0,session:n}={}){await this._ctx.ensureConnected();let r=Date.now();await this._ctx.plugins.run("beforeSearch",{collection:this.name,query:e,options:{filter:t,limit:i,minScore:s}});let a=await this._ctx.embed(e),l=t?this._findAllRaw(t):this._data,o=[];for(let d of l){if(!d._vector)continue;let _=tn(a,d._vector);if(_>=s)o.push({doc:d,score:_})}o.sort((d,_)=>_.score-d.score);let c=o.slice(0,i);this._ctx.queryLog?.record({collection:this.name,op:"search",query:e,duration:Date.now()-r,resultCount:c.length}),this._ctx.sessionStats.recordRead(n);let h=c.map((d)=>X(d.doc)),u=c.map((d)=>d.score);return await this._ctx.plugins.run("afterSearch",{collection:this.name,query:e,options:{filter:t,limit:i,minScore:s},docs:h,scores:u}),{docs:h,scores:u}}async similar(e,{limit:t=10,minScore:i=0}={}){await this._ctx.ensureConnected();let s=this._index.get(e);if(!s||!s._vector)return{docs:[],scores:[]};let n=[];for(let a of this._data){if(a._id===e||!a._vector)continue;let l=tn(s._vector,a._vector);if(l>=i)n.push({doc:a,score:l})}n.sort((a,l)=>l.score-a.score);let r=n.slice(0,t);return{docs:r.map((a)=>X(a.doc)),scores:r.map((a)=>a.score)}}async deleteOne(e,t={}){await this._ctx.ensureConnected();let{save:i,session:s}=t;if(this._softDelete){let r=this._findRaw(e);if(!r)return null;let{docs:a}=await this._deleteCore("soft",[r],e,{save:i,session:s});return X(a[0])}let{docs:n}=await this._deleteCore("hard",null,e,{save:i,session:s});if(n.length===0)return null;return X(n[0])}async deleteMany(e,t={}){await this._ctx.ensureConnected();let{save:i,session:s}=t;if(this._softDelete){let r=this._findAllRaw(e);if(r.length===0)return[];let{docs:a}=await this._deleteCore("soft",r,e,{save:i,session:s});return a.map(X)}let{docs:n}=await this._deleteCore("hardMany",null,e,{save:i,session:s});return n.map(X)}async _deleteCore(e,t,i,{save:s,session:n}){return this._pipeline.execute({op:"delete",beforeHook:"beforeDelete",afterHook:"afterDelete",hookPayload:{collection:this.name,filter:i},save:s,session:n,afterHookPayload:(r)=>({collection:this.name,filter:i,result:r.length===1?r[0]:r}),mutate:async(r)=>{if(r(),e==="soft"){let o=new Date;for(let c of t)c._deletedAt=o,c.updatedAt=o,this._index.set(c._id,c);return{docs:t}}if(e==="hard"){let o=this._findIndex(i);if(o===-1)return{docs:[]};let c=this._data.splice(o,1)[0];return this._index.delete(c._id),this._removeFromIndex(c),{docs:[c]}}let a=[],l=[];for(let o of this._data)if(Y(o,i))a.push(o),this._index.delete(o._id),this._removeFromIndex(o);else l.push(o);return this._data=l,{docs:a}}})}async export(e={},t={}){let{dir:i,name:s,format:n="json"}=t;try{let r=this._data.filter((h)=>Y(h,e));if(r.length===0)throw new et("ERR_SKALEX_QUERY_EXPORT_EMPTY",`export(): no documents matched the filter in "${this.name}"`,{collection:this.name});let a;if(n==="json")a=JSON.stringify(r,null,2);else{let h=(_)=>{if(_==null)return"";let f=typeof _==="object"?JSON.stringify(_):String(_);return f.includes(",")||f.includes('"')||f.includes(`
|
|
92
|
-
`)||f.includes("\r")?`"${f.replace(/"/g,'""')}"`:f},u=Object.keys(r[0]).map(h).join(","),d=r.map((_)=>Object.values(_).map(h).join(","));a=[u,...d].join(`
|
|
93
|
-
`)}if(typeof this._ctx.fs.writeRaw!=="function")throw new pe("ERR_SKALEX_ADAPTER_NO_RAW_WRITE","export() requires a file-system adapter (FsAdapter). The current adapter does not support raw file writes.");let l=i||`${this._ctx.dataDirectory}/exports`,o=`${s||this.name}.${n}`,c=this._ctx.fs.join(l,o);this._ctx.fs.ensureDir(l),await this._ctx.fs.writeRaw(c,a)}catch(r){throw this._ctx.logger(`Error exporting "${this.name}": ${r.message}`,"error"),r}}_txSnapshotIfNeeded(){let e=this._ctx.txManager;if(!e.active)return;if(this._activeTxId!==e.context?.id)return;e.snapshotIfNeeded(this.name,this._store,(t)=>this._ctx.snapshotCollection(t))}async _saveIfNeeded(e){let t=this._ctx.txManager;if(t.active&&this._activeTxId===t.context?.id)return;if(e??this._ctx.autoSave)await this._ctx.saveCollection(this.name)}_applyValidation(e){if(!this._schema)return e;let t=Zs(e,this._schema,this._strict);if(!t.length)return e;switch(this._onSchemaError){case"warn":return this._ctx.logger(`[${this.name}] Validation warning: ${t.join("; ")}`,"warn"),e;case"strip":return en(e,this._schema);default:throw new C("ERR_SKALEX_VALIDATION_FAILED",`Validation failed: ${t.join("; ")}`,{errors:t})}}_enforceCapAfterInsert(){let e=this._maxDocs;if(!e||this._data.length<=e)return;let t=this._data.splice(0,this._data.length-e);for(let i of t)this._index.delete(i._id),this._removeFromIndex(i)}_findRaw(e,{includeDeleted:t=!1}={}){if(typeof e==="function"){for(let i of this._data){if(this._softDelete&&i._deletedAt&&!t)continue;if(e(i))return i}return null}if(e._id){let i=this._index.get(e._id)||null;if(!i)return null;if(this._softDelete&&i._deletedAt&&!t)return null;if(Object.keys(e).length>1)return Y(i,e)?i:null;return i}if(this._fieldIndex)for(let i in e){if(i==="$or"||i==="$and"||i==="$not")continue;let s=e[i];if(typeof s!=="object"||s===null){let n=this._fieldIndex._lookupIterable(i,s);if(n!==null){for(let r of n){if(this._softDelete&&r._deletedAt&&!t)continue;if(Y(r,e))return r}return null}}}for(let i of this._data){if(this._softDelete&&i._deletedAt&&!t)continue;if(Y(i,e))return i}return null}_findAllRaw(e,{includeDeleted:t=!1}={}){if(e&&typeof e!=="function"&&e._id){let s=this._index.get(e._id);if(!s)return[];if(this._softDelete&&s._deletedAt&&!t)return[];return Y(s,e)?[s]:[]}let i=[];for(let s of this._getCandidates(e)){if(this._softDelete&&s._deletedAt&&!t)continue;if(Y(s,e))i.push(s)}return i}_getCandidates(e){if(!this._fieldIndex)return this._data;if(this._fieldIndex._compoundIndexes.size>0){let t={};for(let i in e){if(i==="$or"||i==="$and"||i==="$not")continue;let s=e[i];if(typeof s!=="object"||s===null)t[i]=s}if(Object.keys(t).length>=2){let i=this._fieldIndex.lookupCompound(t);if(i!==null)return i}}for(let t in e){if(t==="$or"||t==="$and"||t==="$not")continue;let i=e[t];if(typeof i!=="object"||i===null){let s=this._fieldIndex._lookupIterable(t,i);if(s!==null)return s}}return this._data}_findIndex(e){for(let t=0;t<this._data.length;t++)if(Y(this._data[t],e))return t;return-1}_addToIndex(e){if(this._fieldIndex)this._fieldIndex.add(e)}_removeFromIndex(e){if(this._fieldIndex)this._fieldIndex.remove(e)}_updateInIndex(e,t){if(this._fieldIndex)this._fieldIndex.update(e,t)}async _buildDoc(e,{ttl:t,embed:i}={}){let s={...e,_id:e._id??(this._ctx.idGenerator??$c)(),createdAt:new Date,updatedAt:new Date},n=t??this._defaultTtl;if(n)s._expiresAt=kc(n);let r=i??this._defaultEmbed;if(r){let a=typeof r==="function"?r(s):s[r];s._vector=await this._ctx.embed(String(a))}if(this._versioning)s._version=1;return s}_projectDoc(e,t){if(t){let s={};for(let n of t){if(n==="_vector")continue;s[n]=e[n]}return s}let i={...e};return delete i._vector,i}async _populateDoc(e,t,i){for(let s of i){let n=await this._ctx.getCollection(s).findOne({_id:t[s]});if(n)e[s]=n}}}class Ji{async read(e){throw Error("StorageAdapter.read() not implemented")}async write(e,t){throw Error("StorageAdapter.write() not implemented")}async delete(e){throw Error("StorageAdapter.delete() not implemented")}async list(){throw Error("StorageAdapter.list() not implemented")}async writeAll(e){for(let{name:t,data:i}of e)await this.write(t,i)}}class Br extends Ji{constructor({dir:e,format:t="gz"}){super();this.dir=He.resolve(e),this.format=t,this._ensureDir(this.dir)}_ensureDir(e){if(!F.existsSync(e))F.mkdirSync(e,{recursive:!0})}_filePath(e){return He.join(this.dir,`${e}.${this.format}`)}async read(e){let t=this._filePath(e);try{let i=await F.promises.readFile(t);if(this.format==="gz")i=ci.inflateSync(i);return i.toString("utf8")}catch(i){if(i.code==="ENOENT")return null;throw i}}async write(e,t){let i=this._filePath(e),s=He.join(this.dir,`${e}_${globalThis.crypto.randomUUID()}.tmp.${this.format}`);if(this.format==="gz"){let n=ci.deflateSync(t);await F.promises.writeFile(s,n)}else await F.promises.writeFile(s,t,"utf8");await F.promises.rename(s,i)}async writeAll(e){let t=[];try{for(let{name:i,data:s}of e){let n=this._filePath(i),r=He.join(this.dir,`${i}_${globalThis.crypto.randomUUID()}.tmp.${this.format}`);if(this.format==="gz"){let a=ci.deflateSync(s);await F.promises.writeFile(r,a)}else await F.promises.writeFile(r,s,"utf8");t.push({tmp:r,fp:n})}for(let{tmp:i,fp:s}of t)await F.promises.rename(i,s)}catch(i){for(let{tmp:s}of t)try{await F.promises.unlink(s)}catch{}throw i}}async delete(e){let t=this._filePath(e);try{await F.promises.unlink(t)}catch(i){if(i.code!=="ENOENT")throw i}}async list(){try{let e=await F.promises.readdir(this.dir),t=`.${this.format}`;return e.filter((i)=>i.endsWith(t)&&!i.includes(".tmp.")).map((i)=>i.slice(0,-t.length))}catch(e){if(e.code==="ENOENT")return[];throw e}}join(...e){return He.join(...e)}ensureDir(e){this._ensureDir(e)}async writeRaw(e,t){this._ensureDir(He.dirname(e)),await F.promises.writeFile(e,t,"utf8")}async readRaw(e){return F.promises.readFile(He.resolve(e),"utf8")}}class Xr{constructor(){this._migrations=[]}add(e){let{version:t,up:i}=e;if(typeof t!=="number"||t<1)throw new C("ERR_SKALEX_VALIDATION_MIGRATION",`Migration version must be a positive integer, got ${t}`,{version:t});if(typeof i!=="function")throw new C("ERR_SKALEX_VALIDATION_MIGRATION",`Migration version ${t} must have an "up" function`,{version:t});if(this._migrations.some((s)=>s.version===t))throw new C("ERR_SKALEX_VALIDATION_MIGRATION_DUPLICATE",`Migration version ${t} is already registered`,{version:t});this._migrations.push({...e}),this._migrations.sort((s,n)=>s.version-n.version)}async run(e,t=[]){let i=new Set(t),s=this._migrations.filter((n)=>!i.has(n.version));for(let n of s){let r=e(n.version);await n.up(r),i.add(n.version)}return[...i].sort((n,r)=>n-r)}status(e=[]){let t=new Set(e),i=this._migrations.map((s)=>s.version).filter((s)=>!t.has(s));return{current:e.length?Math.max(...e):0,applied:[...t].sort((s,n)=>s-n),pending:i}}}var sn={[Symbol.iterator](){return{next(){return{done:!0}}}},size:0};function nn(e){return{[Symbol.iterator](){return e[Symbol.iterator]()},get size(){return e.size}}}function mi(e){return e.map((t)=>{if(t===null||t===void 0)return"\x00";if(typeof t==="boolean")return t?"\x01T":"\x01F";if(typeof t==="number")return`\x02${t}`;return`\x03${String(t)}`}).join("\x1F")}class Mt{constructor(e=[],t=[]){this._fields=new Set,this._compoundFields=[];for(let i of e)if(Array.isArray(i)){for(let s of i)this._validateFieldName(s);this._compoundFields.push(i)}else this._validateFieldName(i),this._fields.add(i);for(let i of t)this._validateFieldName(i);this._uniqueFields=new Set(t),this._indexedFields=new Set([...this._fields,...this._uniqueFields]),this._fieldIndexes=new Map,this._uniqueIndexes=new Map;for(let i of this._fields)this._fieldIndexes.set(i,new Map);for(let i of this._uniqueFields)if(this._uniqueIndexes.set(i,new Map),!this._fieldIndexes.has(i))this._fieldIndexes.set(i,new Map);this._compoundIndexes=new Map;for(let i of this._compoundFields)this._compoundIndexes.set(i.join("\x00"),{fields:i,map:new Map})}get indexedFields(){return this._indexedFields}buildFromData(e){for(let[,t]of this._fieldIndexes)t.clear();for(let[,t]of this._uniqueIndexes)t.clear();for(let[,t]of this._compoundIndexes)t.map.clear();for(let t of e)this._indexDoc(t)}add(e){this._checkUnique(e,null),this._indexDoc(e)}remove(e){for(let[t,i]of this._fieldIndexes){let s=e[t];if(s!==void 0){let n=i.get(s);if(n){if(n.delete(e),n.size===0)i.delete(s)}}}for(let[t,i]of this._uniqueIndexes){let s=e[t];if(s!==void 0)i.delete(s)}for(let[,t]of this._compoundIndexes){let i=mi(t.fields.map((n)=>e[n])),s=t.map.get(i);if(s){if(s.delete(e),s.size===0)t.map.delete(i)}}}update(e,t){this._checkUnique(t,e),this.remove(e);try{this._indexDoc(t)}catch(i){try{this._indexDoc(e)}catch{}throw i}}lookup(e,t){let i=this._fieldIndexes.get(e);if(!i)return null;let s=i.get(t);return s?[...s]:[]}_lookupIterable(e,t){let i=this._fieldIndexes.get(e);if(!i)return null;let s=i.get(t);return s?nn(s):sn}lookupCompound(e){let t=Object.keys(e).sort();for(let[,i]of this._compoundIndexes){let s=[...i.fields].sort();if(s.length!==t.length)continue;if(s.every((n,r)=>n===t[r])){let n=mi(i.fields.map((a)=>e[a])),r=i.map.get(n);return r?nn(r):sn}}return null}isUniqueTaken(e,t){let i=this._uniqueIndexes.get(e);if(!i)return!1;return i.has(t)}assertUniqueCandidates(e,t){if(this._uniqueFields.size===0)return;let i=new Set(e.map((s)=>s._id));for(let s of this._uniqueFields){let n=new Set,r=this._uniqueIndexes.get(s);if(r){for(let[l,o]of r.entries())if(!i.has(o._id))n.add(l)}let a=new Map;for(let l=0;l<t.length;l++){let o=t[l][s];if(o===void 0)continue;if(n.has(o))throw new Te("ERR_SKALEX_UNIQUE_VIOLATION",`Unique constraint violation: field "${s}" value "${o}" already exists`,{field:s,value:o});let c=a.get(o);if(c&&c!==e[l]._id)throw new Te("ERR_SKALEX_UNIQUE_VIOLATION",`Unique constraint violation: field "${s}" value "${o}" already exists`,{field:s,value:o});a.set(o,e[l]._id)}}}assertUniqueBatch(e){if(this._uniqueFields.size===0)return;for(let t of this._uniqueFields){let i=this._uniqueIndexes.get(t),s=new Set;for(let n of e){let r=n[t];if(r===void 0)continue;if(i&&i.has(r))throw new Te("ERR_SKALEX_UNIQUE_VIOLATION",`Unique constraint violation: field "${t}" value "${r}" already exists`,{field:t,value:r});if(s.has(r))throw new Te("ERR_SKALEX_UNIQUE_VIOLATION",`Unique constraint violation: duplicate "${t}" value "${r}" within batch`,{field:t,value:r});s.add(r)}}}_validateFieldName(e){if(e.includes("."))throw new C("ERR_SKALEX_VALIDATION_INDEX_DOT_PATH",`Index fields cannot use dot-notation: "${e}". Use a flat field name.`,{field:e})}_indexDoc(e){for(let[t,i]of this._fieldIndexes){let s=e[t];if(s!==void 0){if(!i.has(s))i.set(s,new Set);i.get(s).add(e)}}for(let[t,i]of this._uniqueIndexes){let s=e[t];if(s!==void 0)i.set(s,e)}for(let[,t]of this._compoundIndexes){for(let s of t.fields){let n=e[s];if(n!==void 0&&n!==null&&typeof n==="object")throw new C("ERR_SKALEX_VALIDATION_COMPOUND_INDEX",`Compound index field "${s}" must be a scalar value (string, number, or boolean), got ${Array.isArray(n)?"array":typeof n}`,{field:s})}let i=mi(t.fields.map((s)=>e[s]));if(!t.map.has(i))t.map.set(i,new Set);t.map.get(i).add(e)}}_checkUnique(e,t){for(let i of this._uniqueFields){let s=e[i];if(s===void 0)continue;let n=this._uniqueIndexes.get(i);if(!n)continue;let r=n.get(s);if(r&&r._id!==t?._id)throw new Te("ERR_SKALEX_UNIQUE_VIOLATION",`Unique constraint violation: field "${i}" value "${s}" already exists`,{field:i,value:s})}}}var Dt="_flush",Ct="migrations";class Hr{constructor({adapter:e,serializer:t,deserializer:i,logger:s,debug:n=!1,lenientLoad:r=!1}){this._adapter=e,this._serializer=t,this._deserializer=i,this._logger=s,this._debug=n,this._lenientLoad=r,this._saveLock=Promise.resolve()}_withSaveLock(e){let t=this._saveLock.then(e);return this._saveLock=t.catch(()=>{}),t}async loadAll(e,{parseSchema:t,buildIndex:i,IndexEngine:s}){try{let n=await this._adapter.list();await Promise.all(n.map(async(r)=>{try{let a=await this._adapter.read(r);if(!a)return;let l=this._deserializer(a),{collectionName:o,data:c}=l;if(!o)return;let h=e[o],u=h?.rawSchema??l.rawSchema??null,d=h?.schema??(u?t(u):null),_=h?.changelog??l.changelog??!1,f=h?.softDelete??l.softDelete??!1,p=h?.versioning??l.versioning??!1,m=h?.strict??l.strict??!1,w=h?.onSchemaError??l.onSchemaError??"throw",v=h?.defaultTtl??l.defaultTtl??null,R=h?.defaultEmbed??l.defaultEmbed??null,E=h?.maxDocs??l.maxDocs??null,A=h?h.fieldIndex:null;if(!A&&d?.uniqueFields?.length)A=new s([],d.uniqueFields);let Ue=i(c,"_id");if(A)A.buildFromData(c);e[o]={collectionName:o,data:c,index:Ue,isSaving:!1,_pendingSave:!1,_dirty:!1,schema:d,rawSchema:u,fieldIndex:A,changelog:_,softDelete:f,versioning:p,strict:m,onSchemaError:w,defaultTtl:v,defaultEmbed:R,maxDocs:E}}catch(a){if(a.code==="ENOENT")return;if(this._lenientLoad){this._logger(`WARNING: Could not load collection "${r}": ${a.message}. Collection will be empty.`,"error");return}throw new Pe("ERR_SKALEX_PERSISTENCE_CORRUPT",`Failed to load collection "${r}": ${a.message}`,{collection:r})}})),this._detectIncompleteFlush(e),await this._cleanOrphanTempFiles()}catch(n){if(n.code!=="ENOENT")throw this._logger(`Error loading data: ${n}`,"error"),n}}async save(e,t){if(t)await this._saveOne(e,t);else await Promise.all(Object.keys(e).map((i)=>this._saveOne(e,i)))}async saveDirty(e){let t=Object.keys(e).filter((i)=>e[i]._dirty);if(t.length===0)return;await Promise.all(t.map((i)=>this._saveOne(e,i)))}saveAtomic(e,t){return this._withSaveLock(async()=>{if(t.length===0)return;this._writeFlushSentinel(e,t);let i=new Set(t);i.add("_meta");let s=[...i].filter((n)=>e[n]).map((n)=>({name:n,data:this._serializeCollection(e[n])}));try{await this._adapter.writeAll(s)}catch(n){throw new Pe("ERR_SKALEX_PERSISTENCE_FLUSH_FAILED",`Batch save failed during writeAll: ${n.message}`,{collections:t})}for(let n of t)if(e[n])e[n]._dirty=!1;try{this._clearFlushSentinel(e),await this._adapter.write("_meta",this._serializeCollection(e._meta))}catch(n){this._logger(`WARNING: Batch data committed but sentinel clear failed: ${n.message}. Next load will report an incomplete flush (false positive).`,"error")}})}markDirty(e,t){let i=e[t];if(i)i._dirty=!0}_serializeCollection(e){let t={collectionName:e.collectionName,data:e.data};if(e.rawSchema)t.rawSchema=e.rawSchema;if(e.changelog)t.changelog=e.changelog;if(e.softDelete)t.softDelete=e.softDelete;if(e.versioning)t.versioning=e.versioning;if(e.strict)t.strict=e.strict;if(e.onSchemaError!=="throw")t.onSchemaError=e.onSchemaError;if(e.defaultTtl)t.defaultTtl=e.defaultTtl;if(e.defaultEmbed)t.defaultEmbed=e.defaultEmbed;if(e.maxDocs)t.maxDocs=e.maxDocs;return this._serializer(t)}async _saveOne(e,t){let i=e[t];if(!i)return;if(i.isSaving)return i._pendingSave=!0,new Promise((s,n)=>{(i._saveWaiters??=[]).push({resolve:s,reject:n})});i.isSaving=!0,i._pendingSave=!1;try{await this._writeCollection(t,i);while(i._pendingSave)i._pendingSave=!1,await this._writeCollection(t,i);this._resolveSaveWaiters(i)}catch(s){throw this._rejectSaveWaiters(i,s),s}finally{i.isSaving=!1}}async _writeCollection(e,t){try{await this._adapter.write(e,this._serializeCollection(t)),t._dirty=!1}catch(i){throw this._logger(`Error saving "${e}": ${i.message}`,"error"),i}}_resolveSaveWaiters(e){let t=e._saveWaiters?.splice(0)??[];for(let i of t)i.resolve()}_rejectSaveWaiters(e,t){let i=e._saveWaiters?.splice(0)??[];for(let s of i)s.reject(t)}_writeFlushSentinel(e,t){let i=this._getOrCreateMeta(e);i[Dt]={startedAt:new Date().toISOString(),collections:t,completedAt:null}}_clearFlushSentinel(e){let t=this._getOrCreateMeta(e);if(t[Dt])t[Dt].completedAt=new Date().toISOString()}_detectIncompleteFlush(e){let t=e._meta;if(!t)return;let i=t.index.get(Ct);if(!i)return;let s=i[Dt];if(!s)return;if(s.startedAt&&!s.completedAt)this._logger(`WARNING: Incomplete flush detected (started ${s.startedAt}, collections: ${s.collections?.join(", ")}). Data may be inconsistent.`,"error")}async _cleanOrphanTempFiles(){if(typeof this._adapter.join!=="function")return;if(typeof this._adapter.list!=="function")return;try{let e=await import("fs"),t=await import("path"),i=this._adapter.dir;if(!i)return;let s=(await e.default.promises.readdir(i)).filter((n)=>n.includes(".tmp."));for(let n of s){let r=t.default.join(i,n);try{await e.default.promises.unlink(r),this._log(`Cleaned orphan temp file: ${n}`)}catch{}}}catch{}}_getOrCreateMeta(e){if(!e._meta)e._meta={collectionName:"_meta",data:[],index:new Map,isSaving:!1,_pendingSave:!1,_dirty:!1,schema:null,rawSchema:null,fieldIndex:null,changelog:!1,softDelete:!1,versioning:!1,strict:!1,onSchemaError:"throw",defaultTtl:null,defaultEmbed:null,maxDocs:null};let t=e._meta,i=t.index.get(Ct);if(!i)i={_id:Ct},t.data.push(i),t.index.set(Ct,i);return i}_log(e){if(this._debug)this._logger(e,"info")}}var Fc=0;class Vr{constructor(){this._txLock=Promise.resolve(),this._ctx=null,this._abortedIds=new Set}get active(){return this._ctx!==null&&!this._ctx.aborted}get context(){return this._ctx}async run(e,t,{timeout:i=0}={}){let s=async()=>{await t._ensureConnected();let r=new Set(Object.keys(t.collections)),a={id:++Fc,startedAt:Date.now(),aborted:!1,preExisting:r,touchedCollections:new Set,snapshots:new Map,deferredEffects:[],timeout:i};this._ctx=a;let l=null,o=i>0?new Promise((u,d)=>{l=setTimeout(()=>{a.aborted=!0,d(new we("ERR_SKALEX_TX_TIMEOUT",`Transaction ${a.id} timed out after ${i}ms`))},i)}):null,c=this,h=new Proxy(t,{get(u,d){if(d==="_txId")return a.id;if(a!==c._ctx)throw new we("ERR_SKALEX_TX_STALE_PROXY",`Transaction ${a.id} has ended. This proxy is no longer usable.`);if(d==="collections")throw new we("ERR_SKALEX_TX_DIRECT_ACCESS","Direct access to db.collections inside transaction() is not covered by rollback. Use the collection API (db.useCollection) instead.");if(d==="useCollection")return(f)=>{let p=u.useCollection(f);return p._activeTxId=a.id,p};let _=Reflect.get(u,d);return typeof _==="function"?_.bind(u):_}});try{let u=e(h),d=o?await Promise.race([u,o]):await u;if(a.aborted)throw new we("ERR_SKALEX_TX_ABORTED",`Transaction ${a.id} was aborted`);let _=[...a.touchedCollections];if(_.length>0)await t._persistence.saveAtomic(t.collections,_);for(let f of a.deferredEffects)await f();return d}catch(u){for(let[d,_]of a.snapshots)if(a.preExisting.has(d)){if(t._applySnapshot(d,_),t.collections[d])t.collections[d]._dirty=_._dirty}for(let d in t.collections)if(!a.preExisting.has(d))delete t.collections[d],delete t._collectionInstances[d];throw u}finally{if(l)clearTimeout(l);if(a.aborted)this._abortedIds.add(a.id);this._ctx=null;for(let u in t._collectionInstances){let d=t._collectionInstances[u];if(d._createdInTxId===a.id)d._createdInTxId=null;if(d._activeTxId===a.id)d._activeTxId=null}}},n=this._txLock.then(s);return this._txLock=n.catch(()=>{}),n}snapshotIfNeeded(e,t,i){let s=this._ctx;if(!s)return;if(this._assertCtxNotAborted(s),!s.snapshots.has(e)){let n=i(t);n._dirty=t._dirty??!1,s.snapshots.set(e,n)}s.touchedCollections.add(e)}assertNotAborted(){let e=this._ctx;if(e)this._assertCtxNotAborted(e)}_assertCtxNotAborted(e){if(e.aborted)throw new we("ERR_SKALEX_TX_ABORTED",`Transaction ${e.id} was aborted. No further mutations allowed.`)}defer(e){if(this._ctx)return this._ctx.deferredEffects.push(e),!0;return!1}}class Jr{constructor(e){this._CollectionClass=e,this.stores={},this._instances={}}get(e,t){if(this._instances[e])return this._instances[e];if(!this.stores[e])this.createStore(e);let i=new this._CollectionClass(this.stores[e],t);return this._instances[e]=i,i}create(e,t={},i){this.createStore(e,t);let s=new this._CollectionClass(this.stores[e],i);return this._instances[e]=s,s}createStore(e,{schema:t,indexes:i=[],changelog:s=!1,softDelete:n=!1,versioning:r=!1,strict:a=!1,onSchemaError:l="throw",defaultTtl:o=null,defaultEmbed:c=null,maxDocs:h=null}={}){let u=null,d=null;if(t){u=qr(t);let _=u.uniqueFields;if(i.length||_.length)d=new Mt(i,_)}else if(i.length)d=new Mt(i,[]);this.stores[e]={collectionName:e,data:[],index:new Map,isSaving:!1,_pendingSave:!1,_dirty:!1,schema:u,rawSchema:t||null,fieldIndex:d,changelog:s,softDelete:n,versioning:r,strict:a,onSchemaError:l,defaultTtl:o,defaultEmbed:c,maxDocs:h}}rename(e,t){if(!this.stores[e])throw new Pe("ERR_SKALEX_PERSISTENCE_COLLECTION_NOT_FOUND",`Collection "${e}" not found`,{collection:e});if(this.stores[t])throw new Pe("ERR_SKALEX_PERSISTENCE_COLLECTION_EXISTS",`Collection "${t}" already exists`,{collection:t});let i=this.stores[e];if(i.collectionName=t,this.stores[t]=i,delete this.stores[e],this._instances[e]){let s=this._instances[e];s.name=t,this._instances[t]=s,delete this._instances[e]}}buildIndex(e,t){let i=new Map;for(let s of e)i.set(s[t],s);return i}inspect(e){if(e){let i=this.stores[e];if(!i)return null;return{name:e,count:i.data.length,schema:i.schema?Object.fromEntries(i.schema.fields):null,indexes:i.fieldIndex?[...i.fieldIndex.indexedFields]:[],softDelete:i.softDelete??!1,versioning:i.versioning??!1,strict:i.strict??!1,onSchemaError:i.onSchemaError??"throw",maxDocs:i.maxDocs??null}}let t={};for(let i in this.stores)t[i]=this.inspect(i);return t}dump(){let e={};for(let t in this.stores)if(!t.startsWith("_"))e[t]=structuredClone(this.stores[t].data);return e}schema(e){let t=this.stores[e];if(!t)return null;if(t.schema)return Object.fromEntries([...t.schema.fields.entries()].map(([i,s])=>[i,s.type]));if(t.data.length>0)return Cc(t.data[0]);return null}stats(e){let t=(i)=>{let s=this.stores[i];if(!s)return null;let n=s.data.length,r=0;for(let a of s.data)try{r+=JSON.stringify(a).length}catch(l){}return{collection:i,count:n,estimatedSize:r,avgDocSize:n>0?Math.round(r/n):0}};if(e)return t(e);return Object.keys(this.stores).map(t)}clear(){this.stores={},this._instances={}}}class Yi{async embed(e){throw Error("EmbeddingAdapter.embed() not implemented")}}var de=(e)=>globalThis.process?.env?.[e]??globalThis.Deno?.env?.get(e);class Yr extends Yi{constructor({apiKey:e=de("OPENAI_API_KEY"),model:t=de("OPENAI_EMBED_MODEL")??"text-embedding-3-small",baseUrl:i=de("OPENAI_EMBED_BASE_URL")??"https://api.openai.com/v1/embeddings",dimensions:s=de("OPENAI_EMBED_DIMENSIONS")!=null?Number(de("OPENAI_EMBED_DIMENSIONS")):void 0,organization:n=de("OPENAI_ORGANIZATION")??void 0,timeout:r=de("OPENAI_EMBED_TIMEOUT")!=null?Number(de("OPENAI_EMBED_TIMEOUT")):void 0,retries:a=Number(de("OPENAI_EMBED_RETRIES")??0),retryDelay:l=Number(de("OPENAI_EMBED_RETRY_DELAY")??1000),headers:o={},fetch:c=globalThis.fetch}={}){super();if(!e)throw Error("OpenAIEmbeddingAdapter requires an apiKey");this.apiKey=e,this.model=t,this.baseUrl=i,this.dimensions=s,this.organization=n,this.timeout=r,this.retries=a,this.retryDelay=l,this.headers=o,this._fetch=c}async embed(e){let t;for(let i=0;i<=this.retries;i++){let s=this.timeout!=null?new AbortController:null,n=s?setTimeout(()=>s.abort(),this.timeout):null;try{let r=await this._fetch(this.baseUrl,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`,...this.organization&&{"OpenAI-Organization":this.organization},...this.headers},body:JSON.stringify({input:e,model:this.model,...this.dimensions!==void 0&&{dimensions:this.dimensions}}),...s&&{signal:s.signal}});if(!r.ok){let a=(await r.text()).slice(0,200);throw Error(`OpenAI embedding API error ${r.status}: ${a}`)}return(await r.json()).data[0].embedding}catch(r){if(t=r,i<this.retries)await new Promise((a)=>setTimeout(a,this.retryDelay*2**i))}finally{if(n!==null)clearTimeout(n)}}throw t}}var lt=(e)=>globalThis.process?.env?.[e]??globalThis.Deno?.env?.get(e);class Wr extends Yi{constructor({model:e=lt("OLLAMA_EMBED_MODEL")??"nomic-embed-text",host:t=lt("OLLAMA_HOST")??"http://localhost:11434",timeout:i=lt("OLLAMA_EMBED_TIMEOUT")!=null?Number(lt("OLLAMA_EMBED_TIMEOUT")):void 0,retries:s=Number(lt("OLLAMA_EMBED_RETRIES")??0),retryDelay:n=Number(lt("OLLAMA_EMBED_RETRY_DELAY")??1000),headers:r={},fetch:a=globalThis.fetch}={}){super();this.model=e,this.host=t,this.timeout=i,this.retries=s,this.retryDelay=n,this.headers=r,this._fetch=a}async embed(e){let t;for(let i=0;i<=this.retries;i++){let s=this.timeout!=null?new AbortController:null,n=s?setTimeout(()=>s.abort(),this.timeout):null;try{let r=await this._fetch(`${this.host}/api/embeddings`,{method:"POST",headers:{"Content-Type":"application/json",...this.headers},body:JSON.stringify({model:this.model,prompt:e}),...s&&{signal:s.signal}});if(!r.ok){let a=(await r.text()).slice(0,200);throw Error(`Ollama embedding API error ${r.status}: ${a}`)}return(await r.json()).embedding}catch(r){if(t=r,i<this.retries)await new Promise((a)=>setTimeout(a,this.retryDelay*2**i))}finally{if(n!==null)clearTimeout(n)}}throw t}}class Xt{async generate(e,t){throw Error("LLMAdapter.generate() not implemented")}async summarize(e){throw Error("LLMAdapter.summarize() not implemented")}}var Wi=`You are a database query assistant. Convert the user's natural language request into a JSON filter object for a document database.
|
|
94
|
-
|
|
95
|
-
Rules:
|
|
96
|
-
- Return ONLY a valid JSON object, nothing else
|
|
97
|
-
- Use standard query operators: $eq, $ne, $gt, $gte, $lt, $lte, $in, $nin, $regex
|
|
98
|
-
- For simple equality, use plain key-value pairs: {"field": "value"}
|
|
99
|
-
- For numeric comparisons use operators: {"age": {"$gt": 18}}
|
|
100
|
-
- For text search use $regex: {"name": {"$regex": "pattern"}}
|
|
101
|
-
- Return {} to match all documents
|
|
102
|
-
- No explanations, no markdown, no code fences`,Qi="Summarise the following memory entries into one concise paragraph. Preserve all important facts.",M=(e)=>globalThis.process?.env?.[e]??globalThis.Deno?.env?.get(e);class Qr extends Xt{constructor({apiKey:e=M("OPENAI_API_KEY"),model:t=M("OPENAI_MODEL")??"gpt-4o-mini",baseUrl:i=M("OPENAI_BASE_URL")??"https://api.openai.com/v1/chat/completions",maxTokens:s=M("OPENAI_MAX_TOKENS")!=null?Number(M("OPENAI_MAX_TOKENS")):void 0,temperature:n=Number(M("OPENAI_TEMPERATURE")??0.3),topP:r=M("OPENAI_TOP_P")!=null?Number(M("OPENAI_TOP_P")):void 0,organization:a=M("OPENAI_ORGANIZATION")??void 0,timeout:l=M("OPENAI_TIMEOUT")!=null?Number(M("OPENAI_TIMEOUT")):void 0,retries:o=Number(M("OPENAI_RETRIES")??0),retryDelay:c=Number(M("OPENAI_RETRY_DELAY")??1000),headers:h={},fetch:u=globalThis.fetch,seed:d=M("OPENAI_SEED")!=null?Number(M("OPENAI_SEED")):void 0,generatePrompt:_=Wi,summarizePrompt:f=Qi}={}){super();if(!e)throw Error("OpenAILLMAdapter requires an apiKey");this.apiKey=e,this.model=t,this.baseUrl=i,this.maxTokens=s,this.temperature=n,this.topP=r,this.organization=a,this.timeout=l,this.retries=o,this.retryDelay=c,this.headers=h,this._fetch=u,this.seed=d,this.generatePrompt=_,this.summarizePrompt=f}async generate(e,t){let i=await this._post({model:this.model,messages:[{role:"system",content:`${this.generatePrompt}
|
|
103
|
-
Schema: ${JSON.stringify(e)}`},{role:"user",content:t}],response_format:{type:"json_object"},temperature:0,...this.maxTokens!==void 0&&{max_tokens:this.maxTokens},...this.seed!==void 0&&{seed:this.seed}});return JSON.parse(i.choices[0].message.content)}async summarize(e){return(await this._post({model:this.model,messages:[{role:"system",content:this.summarizePrompt},{role:"user",content:e}],temperature:this.temperature,...this.maxTokens!==void 0&&{max_tokens:this.maxTokens},...this.topP!==void 0&&{top_p:this.topP},...this.seed!==void 0&&{seed:this.seed}})).choices[0].message.content.trim()}async _post(e){let t;for(let i=0;i<=this.retries;i++){let s=this.timeout!=null?new AbortController:null,n=s?setTimeout(()=>s.abort(),this.timeout):null;try{let r=await this._fetch(this.baseUrl,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`,...this.organization&&{"OpenAI-Organization":this.organization},...this.headers},body:JSON.stringify(e),...s&&{signal:s.signal}});if(!r.ok){let a=(await r.text()).slice(0,200);throw Error(`OpenAI API error ${r.status}: ${a}`)}return r.json()}catch(r){if(t=r,i<this.retries)await new Promise((a)=>setTimeout(a,this.retryDelay*2**i))}finally{if(n!==null)clearTimeout(n)}}throw t}}var Z=(e)=>globalThis.process?.env?.[e]??globalThis.Deno?.env?.get(e);class Gr extends Xt{constructor({apiKey:e=Z("ANTHROPIC_API_KEY"),model:t=Z("ANTHROPIC_MODEL")??"claude-haiku-4-5",baseUrl:i=Z("ANTHROPIC_BASE_URL")??"https://api.anthropic.com/v1/messages",apiVersion:s="2023-06-01",maxTokens:n=Number(Z("ANTHROPIC_MAX_TOKENS")??1024),temperature:r=Number(Z("ANTHROPIC_TEMPERATURE")??0.3),topP:a=Z("ANTHROPIC_TOP_P")!=null?Number(Z("ANTHROPIC_TOP_P")):void 0,topK:l=Z("ANTHROPIC_TOP_K")!=null?Number(Z("ANTHROPIC_TOP_K")):void 0,timeout:o=Z("ANTHROPIC_TIMEOUT")!=null?Number(Z("ANTHROPIC_TIMEOUT")):void 0,retries:c=Number(Z("ANTHROPIC_RETRIES")??0),retryDelay:h=Number(Z("ANTHROPIC_RETRY_DELAY")??1000),headers:u={},fetch:d=globalThis.fetch,generatePrompt:_=Wi,summarizePrompt:f=Qi}={}){super();if(!e)throw Error("AnthropicLLMAdapter requires an apiKey");this.apiKey=e,this.model=t,this.baseUrl=i,this.apiVersion=s,this.maxTokens=n,this.temperature=r,this.topP=a,this.topK=l,this.timeout=o,this.retries=c,this.retryDelay=h,this.headers=u,this._fetch=d,this.generatePrompt=_,this.summarizePrompt=f}async generate(e,t){let i=(await this._post({model:this.model,max_tokens:this.maxTokens,temperature:0,system:`${this.generatePrompt}
|
|
104
|
-
Schema: ${JSON.stringify(e)}`,messages:[{role:"user",content:t}]})).content[0].text.trim().replace(/^```(?:json)?\n?/,"").replace(/\n?```$/,"");return JSON.parse(i)}async summarize(e){return(await this._post({model:this.model,max_tokens:this.maxTokens,temperature:this.temperature,...this.topP!==void 0&&{top_p:this.topP},...this.topK!==void 0&&{top_k:this.topK},system:this.summarizePrompt,messages:[{role:"user",content:e}]})).content[0].text.trim()}async _post(e){let t;for(let i=0;i<=this.retries;i++){let s=this.timeout!=null?new AbortController:null,n=s?setTimeout(()=>s.abort(),this.timeout):null;try{let r=await this._fetch(this.baseUrl,{method:"POST",headers:{"Content-Type":"application/json","x-api-key":this.apiKey,"anthropic-version":this.apiVersion,...this.headers},body:JSON.stringify(e),...s&&{signal:s.signal}});if(!r.ok){let a=(await r.text()).slice(0,200);throw Error(`Anthropic API error ${r.status}: ${a}`)}return r.json()}catch(r){if(t=r,i<this.retries)await new Promise((a)=>setTimeout(a,this.retryDelay*2**i))}finally{if(n!==null)clearTimeout(n)}}throw t}}var oe=(e)=>globalThis.process?.env?.[e]??globalThis.Deno?.env?.get(e);class Zr extends Xt{constructor({model:e=oe("OLLAMA_MODEL")??"llama3.2",host:t=oe("OLLAMA_HOST")??"http://localhost:11434",temperature:i=Number(oe("OLLAMA_TEMPERATURE")??0.3),topP:s=oe("OLLAMA_TOP_P")!=null?Number(oe("OLLAMA_TOP_P")):void 0,topK:n=oe("OLLAMA_TOP_K")!=null?Number(oe("OLLAMA_TOP_K")):void 0,timeout:r=oe("OLLAMA_TIMEOUT")!=null?Number(oe("OLLAMA_TIMEOUT")):void 0,retries:a=Number(oe("OLLAMA_RETRIES")??0),retryDelay:l=Number(oe("OLLAMA_RETRY_DELAY")??1000),headers:o={},fetch:c=globalThis.fetch,generatePrompt:h=Wi,summarizePrompt:u=Qi}={}){super();this.model=e,this.host=t,this.temperature=i,this.topP=s,this.topK=n,this.timeout=r,this.retries=a,this.retryDelay=l,this.headers=o,this._fetch=c,this.generatePrompt=h,this.summarizePrompt=u}async generate(e,t){let i=`${this.generatePrompt}
|
|
105
|
-
Schema: ${JSON.stringify(e)}
|
|
106
|
-
Query: ${t}`,s=await this._post({model:this.model,prompt:i,format:"json",options:{temperature:0},stream:!1});return JSON.parse(s.response)}async summarize(e){return(await this._post({model:this.model,prompt:`${this.summarizePrompt}
|
|
107
|
-
|
|
108
|
-
${e}`,options:{temperature:this.temperature,...this.topP!==void 0&&{top_p:this.topP},...this.topK!==void 0&&{top_k:this.topK}},stream:!1})).response.trim()}async _post(e){let t;for(let i=0;i<=this.retries;i++){let s=this.timeout!=null?new AbortController:null,n=s?setTimeout(()=>s.abort(),this.timeout):null;try{let r=await this._fetch(`${this.host}/api/generate`,{method:"POST",headers:{"Content-Type":"application/json",...this.headers},body:JSON.stringify(e),...s&&{signal:s.signal}});if(!r.ok){let a=(await r.text()).slice(0,200);throw Error(`Ollama API error ${r.status}: ${a}`)}return r.json()}catch(r){if(t=r,i<this.retries)await new Promise((a)=>setTimeout(a,this.retryDelay*2**i))}finally{if(n!==null)clearTimeout(n)}}throw t}}function Uc({provider:e,apiKey:t,embedModel:i,model:s,host:n,embedBaseUrl:r,dimensions:a,organization:l,embedTimeout:o,embedRetries:c,embedRetryDelay:h}){let u=i||s;switch(e){case"openai":return new Yr({apiKey:t,model:u,baseUrl:r,...a!==void 0&&{dimensions:a},...l!==void 0&&{organization:l},...o!==void 0&&{timeout:o},...c!==void 0&&{retries:c},...h!==void 0&&{retryDelay:h}});case"ollama":return new Wr({model:u,host:n,...o!==void 0&&{timeout:o},...c!==void 0&&{retries:c},...h!==void 0&&{retryDelay:h}});default:throw new pe("ERR_SKALEX_ADAPTER_UNKNOWN_PROVIDER",`Unknown AI provider: "${e}". Supported: "openai", "ollama".`,{provider:e})}}function zc({provider:e,apiKey:t,model:i,host:s,baseUrl:n,apiVersion:r,temperature:a,maxTokens:l,topP:o,topK:c,organization:h,timeout:u,retries:d,retryDelay:_,seed:f,generatePrompt:p,summarizePrompt:m}){if(!i)return null;switch(e){case"openai":return new Qr({apiKey:t,model:i,baseUrl:n,...l!==void 0&&{maxTokens:l},...a!==void 0&&{temperature:a},...o!==void 0&&{topP:o},...h!==void 0&&{organization:h},...u!==void 0&&{timeout:u},...d!==void 0&&{retries:d},..._!==void 0&&{retryDelay:_},...f!==void 0&&{seed:f},...p!==void 0&&{generatePrompt:p},...m!==void 0&&{summarizePrompt:m}});case"anthropic":return new Gr({apiKey:t,model:i,baseUrl:n,apiVersion:r,...l!==void 0&&{maxTokens:l},...a!==void 0&&{temperature:a},...o!==void 0&&{topP:o},...c!==void 0&&{topK:c},...u!==void 0&&{timeout:u},...d!==void 0&&{retries:d},..._!==void 0&&{retryDelay:_},...p!==void 0&&{generatePrompt:p},...m!==void 0&&{summarizePrompt:m}});case"ollama":return new Zr({model:i,host:s,...a!==void 0&&{temperature:a},...o!==void 0&&{topP:o},...c!==void 0&&{topK:c},...u!==void 0&&{timeout:u},...d!==void 0&&{retries:d},..._!==void 0&&{retryDelay:_},...p!==void 0&&{generatePrompt:p},...m!==void 0&&{summarizePrompt:m}});default:return null}}var gi="AES-GCM",yt=12,Oe=32,Bc=new TextEncoder,Xc=new TextDecoder;class ea extends Ji{constructor(e,t){super();if(this._adapter=e,this._rawKey=typeof t==="string"?Hc(t):Uint8Array.from(t),this._rawKey.length!==Oe)throw new pe("ERR_SKALEX_ADAPTER_INVALID_KEY",`EncryptedAdapter: key must be ${Oe} bytes (${Oe*2} hex chars), got ${this._rawKey.length}`);this._cryptoKey=null}async read(e){let t=await this._adapter.read(e);if(!t)return null;return this._decrypt(t)}async write(e,t){return this._adapter.write(e,await this._encrypt(t))}async delete(e){return this._adapter.delete(e)}async list(){return this._adapter.list()}async writeAll(e){let t=[];for(let{name:i,data:s}of e)t.push({name:i,data:await this._encrypt(s)});return this._adapter.writeAll(t)}join(...e){return this._adapter.join?.(...e)}ensureDir(e){return this._adapter.ensureDir?.(e)}async writeRaw(e,t){return this._adapter.writeRaw?.(e,await this._encrypt(t))}async readRaw(e){let t=await this._adapter.readRaw?.(e);if(!t)return null;return this._decrypt(t)}async _getKey(){if(!this._cryptoKey)this._cryptoKey=await globalThis.crypto.subtle.importKey("raw",this._rawKey,{name:gi},!1,["encrypt","decrypt"]);return this._cryptoKey}async _encrypt(e){let t=await this._getKey(),i=globalThis.crypto.getRandomValues(new Uint8Array(yt)),s=Bc.encode(e),n=await globalThis.crypto.subtle.encrypt({name:gi,iv:i,tagLength:128},t,s),r=new Uint8Array(yt+n.byteLength);return r.set(i,0),r.set(new Uint8Array(n),yt),Vc(r)}async _decrypt(e){let t=await this._getKey(),i=Jc(e),s=i.slice(0,yt),n=i.slice(yt),r=await globalThis.crypto.subtle.decrypt({name:gi,iv:s,tagLength:128},t,n);return Xc.decode(r)}}function Hc(e){if(e.length!==Oe*2)throw new pe("ERR_SKALEX_ADAPTER_INVALID_KEY",`EncryptedAdapter: hex key must be ${Oe*2} characters (${Oe} bytes)`);if(!/^[0-9a-fA-F]+$/.test(e))throw new pe("ERR_SKALEX_ADAPTER_INVALID_KEY","EncryptedAdapter: hex key contains invalid characters");let t=new Uint8Array(Oe);for(let i=0;i<Oe;i++)t[i]=parseInt(e.slice(i*2,i*2+2),16);return t}function Vc(e){let t="";for(let i=0;i<e.length;i++)t+=String.fromCharCode(e[i]);return globalThis.btoa(t)}function Jc(e){let t=globalThis.atob(e),i=new Uint8Array(t.length);for(let s=0;s<t.length;s++)i[s]=t.charCodeAt(s);return i}var Yc=4;class ta{constructor(e,t){this.sessionId=e,this._db=t,this._col=t.useCollection(`_memory_${e}`)}async remember(e){let t=await this._col.insertOne({text:e,sessionId:this.sessionId},{embed:"text"}),i=this._db._memoryConfig?.maxEntries;if(i&&this._col._data.length>i)await this.compress({threshold:0});return t}async recall(e,{limit:t=10,minScore:i=0}={}){return this._col.search(e,{limit:t,minScore:i})}async history({since:e,limit:t}={}){let i=e?{createdAt:{$gte:new Date(e)}}:{},s={sort:{createdAt:1}};if(t)s.limit=t;let{docs:n}=await this._col.find(i,s);return n}async forget(e){return this._col.deleteOne({_id:e})}tokenCount(){let e=this._col._data;return{tokens:e.reduce((t,i)=>t+this._docTokens(i),0),count:e.length}}context({tokens:e=this._db._memoryConfig?.contextTokens??4000}={}){let t=this._sortedData("desc"),i=[],s=0;for(let n of t){let r=this._docTokens(n);if(s+r>e)break;i.push(n.text),s+=r}return i.reverse().join(`
|
|
109
|
-
`)}async compress({threshold:e=this._db._memoryConfig?.compressionThreshold??8000,keepRecent:t=this._db._memoryConfig?.keepRecent??10}={}){let{tokens:i}=this.tokenCount();if(i<=e)return;if(!this._db._aiAdapter)throw Error('memory.compress() requires a language model adapter. Configure { ai: { model: "..." } }.');let s=this._sortedData("asc"),n=Math.max(0,s.length-t),r=s.slice(0,n);if(r.length===0)return;let a=r.map((o)=>o.text).join(`
|
|
110
|
-
`),l=await this._db._aiAdapter.summarize(a);await this._col.deleteMany({_id:{$in:r.map((o)=>o._id)}}),await this._col.insertOne({text:l,sessionId:this.sessionId,compressed:!0})}_docTokens(e){return Math.ceil((e.text||"").length/Yc)}_sortedData(e){return[...this._col._data].sort(e==="asc"?(t,i)=>new Date(t.createdAt)-new Date(i.createdAt):(t,i)=>new Date(i.createdAt)-new Date(t.createdAt))}}class ia{constructor(e){this._db=e,this._restoring=!1}get _col(){return this._db.useCollection("_changelog")}async log(e,t,i,s=null,n=null){if(this._restoring)return;let r={op:e,collection:t,docId:i._id,doc:{...i},timestamp:new Date};if(s)r.prev={...s};if(n)r.session=n;await this._col.insertOne(r)}async query(e,{since:t,limit:i,session:s}={}){let n={collection:e};if(t)n.timestamp={$gte:new Date(t)};if(s)n.session=s;let r={sort:{timestamp:1}};if(i)r.limit=i;let{docs:a}=await this._col.find(n,r);return a}async restore(e,t,{_id:i}={}){let s=new Date(t),n=this._db.useCollection(e),r=(await this.query(e,{})).filter((l)=>new Date(l.timestamp)<=s);if(i){let l=r.filter((c)=>c.docId===i);if(l.length===0)return;let o=l[l.length-1];this._restoring=!0;try{if(o.op==="delete"){if(await n.findOne({_id:i}))await n.deleteOne({_id:i});return}if(await n.findOne({_id:i})){let{_id:c,createdAt:h,updatedAt:u,...d}=o.doc;await n.updateOne({_id:i},d)}else await n.insertOne({...o.doc})}finally{this._restoring=!1}await this._db.saveData(e);return}let a=new Map;for(let l of r)if(l.op==="insert"||l.op==="update")a.set(l.docId,{doc:l.doc,deleted:!1});else if(l.op==="delete")a.set(l.docId,{doc:null,deleted:!0});this._restoring=!0;try{await n.deleteMany({});for(let[,{doc:l,deleted:o}]of a)if(!o&&l)await n.insertOne({...l})}finally{this._restoring=!1}await this._db.saveData(e)}}function Wc(e){let t=5381;for(let i=0;i<e.length;i++)t=(t<<5)+t+e.charCodeAt(i)|0;return(t>>>0).toString(16).padStart(8,"0")}class sa{constructor({maxSize:e=500,ttl:t=0}={}){this._cache=new Map,this._maxSize=e,this._ttl=t}_key(e,t,i){return Wc(JSON.stringify({collectionName:e,schema:t,query:i}))}get(e,t,i){let s=this._key(e,t,i),n=this._cache.get(s);if(!n)return;if(this._ttl>0&&Date.now()-n.ts>this._ttl){this._cache.delete(s);return}return n.filter}set(e,t,i,s){let n=this._key(e,t,i);if(this._cache.size>=this._maxSize&&!this._cache.has(n))this._cache.delete(this._cache.keys().next().value);this._cache.set(n,{filter:s,ts:Date.now()})}toJSON(){return Object.fromEntries([...this._cache.entries()].map(([e,t])=>[e,t]))}fromJSON(e){if(!e||typeof e!=="object")return;for(let[t,i]of Object.entries(e))this._cache.set(t,i)}get size(){return this._cache.size}}function Qc(e,{regexMaxLength:t=500}={}){if(typeof e!=="object"||e===null)return e;let i={};for(let s of Object.keys(e)){let n=e[s];if(n&&typeof n==="object"&&!Array.isArray(n)){let r={};for(let a of Object.keys(n))if(a==="$regex"&&typeof n.$regex==="string"){if(n.$regex.length>t)throw Error(`$regex pattern too long (max ${t} characters)`);if(/\([^)]*[+*][^)]*\)[+*{]/.test(n.$regex))throw Error("$regex pattern rejected: nested quantifiers risk ReDoS");try{r.$regex=new RegExp(n.$regex)}catch{throw Error(`invalid $regex pattern: "${n.$regex}"`)}}else if(["$gt","$gte","$lt","$lte"].includes(a)&&Gc(n[a]))r[a]=new Date(n[a]);else r[a]=n[a];i[s]=r}else i[s]=n}return i}function Gc(e){if(typeof e!=="string")return!1;return/\d{4}-\d{2}-\d{2}/.test(e)&&!isNaN(Date.parse(e))}function Zc(e,t){let i=[];if(!t||typeof e!=="object"||e===null)return i;for(let s of Object.keys(e)){if(s.startsWith("$"))continue;if(!(s.split(".")[0]in t))i.push(`Unknown field referenced in generated filter: "${s}"`)}return i}class na{constructor(){this._listeners=new Map}on(e,t){if(!this._listeners.has(e))this._listeners.set(e,new Set);return this._listeners.get(e).add(t),()=>this._listeners.get(e)?.delete(t)}off(e,t){this._listeners.get(e)?.delete(t)}emit(e,t){for(let i of[e,"*"]){let s=this._listeners.get(i);if(!s)continue;for(let n of s)try{n(t)}catch(r){}}}removeAll(e){if(e)this._listeners.delete(e);else this._listeners.clear()}listenerCount(e){return this._listeners.get(e)?.size??0}}class ra{constructor({threshold:e=100,maxEntries:t=500}={}){this._threshold=e,this._maxEntries=t,this._entries=[]}record({collection:e,op:t,filter:i,query:s,duration:n,resultCount:r}){if(n<this._threshold)return;let a={collection:e,op:t,duration:n,resultCount:r,timestamp:new Date};if(i!==void 0)a.filter=i;if(s!==void 0)a.query=s;if(this._entries.push(a),this._entries.length>this._maxEntries)this._entries.shift()}entries({limit:e,minDuration:t,collection:i}={}){let s=this._entries;if(i)s=s.filter((n)=>n.collection===i);if(t)s=s.filter((n)=>n.duration>=t);if(e)s=s.slice(-e);return s}get size(){return this._entries.length}clear(){this._entries=[]}}class aa{constructor(){this._sessions=new Map}_ensure(e){if(!this._sessions.has(e))this._sessions.set(e,{reads:0,writes:0,lastActive:null});return this._sessions.get(e)}recordRead(e){if(!e)return;let t=this._ensure(e);t.reads++,t.lastActive=new Date}recordWrite(e){if(!e)return;let t=this._ensure(e);t.writes++,t.lastActive=new Date}get(e){let t=this._sessions.get(e);if(!t)return null;return{sessionId:e,...t}}all(){return[...this._sessions.entries()].map(([e,t])=>({sessionId:e,...t}))}clear(e){if(e)this._sessions.delete(e);else this._sessions.clear()}}class oa{constructor(){this._plugins=[]}register(e){if(typeof e!=="object"||e===null)throw TypeError("Plugin must be a non-null object.");this._plugins.push(e)}async run(e,t){for(let i of this._plugins)if(typeof i[e]==="function")await i[e](t)}get size(){return this._plugins.length}}function We(e){if(typeof e!=="string"||!e.trim())throw Error("collection name must be a non-empty string");if(/[/\\]/.test(e)||e.includes("..")||e.includes("\x00"))throw Error(`invalid collection name: "${e}"`);if(e.trim().startsWith("_"))throw Error(`access to system collection "${e}" is not permitted`);return e.trim()}var rn=[{name:"skalex_collections",description:"List all collection names in the database.",inputSchema:{type:"object",properties:{}},scope:"read"},{name:"skalex_schema",description:"Return the schema for a collection as a { field: type } map. Returns null if the collection is empty.",inputSchema:{type:"object",properties:{collection:{type:"string",description:"Collection name."}},required:["collection"]},scope:"read"},{name:"skalex_find",description:"Find documents in a collection that match a filter.",inputSchema:{type:"object",properties:{collection:{type:"string",description:"Collection name."},filter:{type:"object",description:"Query filter (MongoDB-style operators supported)."},limit:{type:"number",description:"Maximum number of results. Default: 20."},sort:{type:"object",description:"Sort descriptor: { field: 1 } for ascending, { field: -1 } for descending."}},required:["collection"]},scope:"read"},{name:"skalex_insert",description:"Insert a single document into a collection.",inputSchema:{type:"object",properties:{collection:{type:"string",description:"Collection name."},doc:{type:"object",description:"Document to insert."}},required:["collection","doc"]},scope:"write"},{name:"skalex_update",description:"Update the first document matching a filter.",inputSchema:{type:"object",properties:{collection:{type:"string",description:"Collection name."},filter:{type:"object",description:"Query filter to identify the document."},update:{type:"object",description:"Fields to update (direct assignment, $inc, $push supported)."},many:{type:"boolean",description:"If true, update all matching documents. Default: false."}},required:["collection","filter","update"]},scope:"write"},{name:"skalex_delete",description:"Delete the first document matching a filter.",inputSchema:{type:"object",properties:{collection:{type:"string",description:"Collection name."},filter:{type:"object",description:"Query filter to identify the document."},many:{type:"boolean",description:"If true, delete all matching documents. Default: false."}},required:["collection","filter"]},scope:"write"},{name:"skalex_search",description:"Semantic similarity search. Requires an embedding adapter to be configured.",inputSchema:{type:"object",properties:{collection:{type:"string",description:"Collection name."},query:{type:"string",description:"Natural-language query string to embed and compare."},limit:{type:"number",description:"Maximum number of results. Default: 10."},minScore:{type:"number",description:"Minimum cosine similarity score [0, 1]. Default: 0."},filter:{type:"object",description:"Optional structured pre-filter (hybrid search)."}},required:["collection","query"]},scope:"read"},{name:"skalex_ask",description:"Translate a natural-language question into a filter and query a collection. Requires a language model adapter.",inputSchema:{type:"object",properties:{collection:{type:"string",description:"Collection name."},question:{type:"string",description:"Natural-language question about the data."},limit:{type:"number",description:"Maximum number of results. Default: 20."}},required:["collection","question"]},scope:"read"}];async function eh(e,t,i){switch(e){case"skalex_collections":return Object.keys(i.collections).filter((s)=>!s.startsWith("_"));case"skalex_schema":return i.schema(We(t.collection))??null;case"skalex_find":{let s=i.useCollection(We(t.collection)),n={};if(t.limit)n.limit=t.limit;if(t.sort)n.sort=t.sort;return s.find(t.filter||{},n)}case"skalex_insert":return i.useCollection(We(t.collection)).insertOne(t.doc||{});case"skalex_update":{let s=i.useCollection(We(t.collection));if(t.many)return s.updateMany(t.filter||{},t.update||{});return s.updateOne(t.filter||{},t.update||{})}case"skalex_delete":{let s=i.useCollection(We(t.collection));if(t.many)return s.deleteMany(t.filter||{});return s.deleteOne(t.filter||{})}case"skalex_search":return i.useCollection(We(t.collection)).search(t.query,{limit:t.limit??10,minScore:t.minScore??0,filter:t.filter});case"skalex_ask":return i.ask(We(t.collection),t.question,{limit:t.limit??20});default:throw Object.assign(Error(`Unknown tool: ${e}`),{code:"NOT_FOUND"})}}var Gi="2.0",Zi=-32700,th=-32600,an=-32601,ih=-32602,sh=-32603;function ct(e,t){return{jsonrpc:Gi,id:e,result:t}}function Ce(e,t,i,s){return{jsonrpc:Gi,id:e,error:{code:t,message:i}}}function nh(e){try{let t=JSON.parse(e);if(typeof t!=="object"||t===null||t.jsonrpc!==Gi)return{parseError:Ce(null,th,"Invalid JSON-RPC request")};return{msg:t}}catch(t){return{parseError:Ce(null,Zi,"Parse error")}}}function rh(e){return{content:[{type:"text",text:e}]}}function on(e){return{content:[{type:"text",text:e}],isError:!0}}class la{constructor({port:e=3000,host:t="127.0.0.1",allowedOrigin:i=null,maxBodySize:s=1048576}={}){this._port=e,this._host=t,this._allowedOrigin=i,this._maxBodySize=s,this._clients=new Set,this._onMessage=null,this._server=null}onMessage(e){this._onMessage=e}send(e){let t=`data: ${JSON.stringify(e)}
|
|
111
|
-
|
|
112
|
-
`;for(let i of this._clients)try{i.write(t)}catch(s){this._clients.delete(i)}}start(){return new Promise((e,t)=>{this._server=Fo.createServer((i,s)=>{if(this._allowedOrigin)s.setHeader("Access-Control-Allow-Origin",this._allowedOrigin),s.setHeader("Access-Control-Allow-Methods","GET, POST, OPTIONS"),s.setHeader("Access-Control-Allow-Headers","Content-Type");if(i.method==="OPTIONS"){s.writeHead(204),s.end();return}if(i.method==="GET"&&i.url==="/sse")this._handleSSE(i,s);else if(i.method==="POST"&&i.url==="/message")this._handleMessage(i,s);else s.writeHead(404,{"Content-Type":"text/plain"}),s.end("Not Found")}),this._server.on("error",t),this._server.listen(this._port,this._host,()=>e())})}stop(){return new Promise((e)=>{if(!this._server){e();return}for(let t of this._clients)try{t.end()}catch(i){}this._clients.clear(),this._server.close(()=>e()),this._server=null})}_handleSSE(e,t){t.writeHead(200,{"Content-Type":"text/event-stream","Cache-Control":"no-cache",Connection:"keep-alive"}),t.write(`event: endpoint
|
|
113
|
-
data: /message
|
|
114
|
-
|
|
115
|
-
`),this._clients.add(t),e.on("close",()=>{this._clients.delete(t)})}async _handleMessage(e,t){let i="";e.setEncoding("utf8"),e.on("data",(s)=>{if(i+=s,i.length>this._maxBodySize)e.destroy(),t.writeHead(413,{"Content-Type":"application/json"}),t.end(JSON.stringify({error:"Request body too large"}))}),e.on("end",async()=>{t.writeHead(202,{"Content-Type":"application/json"}),t.end("{}");let s;try{s=JSON.parse(i)}catch(n){this.send(Ce(null,Zi,"Parse error"));return}if(this._onMessage)await this._onMessage(s).catch(()=>{})})}get port(){return this._port}get host(){return this._host}get url(){return`http://${this._host}:${this._port}`}get sseUrl(){return`${this.url}/sse`}}class ca{constructor(){this._onMessage=null,this._buffer="",this._started=!1}onMessage(e){this._onMessage=e}send(e){process.stdout.write(JSON.stringify(e)+`
|
|
116
|
-
`)}start(){if(this._started)return;this._started=!0,process.stdin.setEncoding("utf8"),process.stdin.on("data",(e)=>{this._buffer+=e;let t;while((t=this._buffer.indexOf(`
|
|
117
|
-
`))!==-1){let i=this._buffer.slice(0,t).trim();if(this._buffer=this._buffer.slice(t+1),i&&this._onMessage){let s;try{s=JSON.parse(i)}catch(n){this.send(Ce(null,Zi,"Parse error"));continue}this._onMessage(s).catch(()=>{})}}}),process.stdin.on("end",()=>{process.exit(0)})}stop(){process.stdin.removeAllListeners("data"),process.stdin.removeAllListeners("end"),this._started=!1}}var ah={name:"skalex",version:"4.0.0-alpha"},oh="2024-11-05";class ha{constructor(e,t={}){this._db=e,this._transport=t.transport||"stdio",this._port=t.port||3000,this._host=t.host||"127.0.0.1",this._allowedOrigin=t.allowedOrigin??null,this._maxBodySize=t.maxBodySize??1048576,this._scopes=t.scopes||{"*":["read"]},this._t=null}async listen(){if(this._transport==="http")this._t=new la({port:this._port,host:this._host,allowedOrigin:this._allowedOrigin,maxBodySize:this._maxBodySize});else this._t=new ca;this._t.onMessage((e)=>this._handleMessage(e)),await this._t.start()}async connect(e){if(this._t=e,this._t.onMessage((t)=>this._handleMessage(t)),typeof this._t.start==="function")await this._t.start()}async close(){if(this._t&&typeof this._t.stop==="function")await this._t.stop();this._t=null}get transport(){return this._transport}get url(){return this._t?.url}async _handleMessage(e){let{msg:t,parseError:i}=typeof e==="string"?nh(e):{msg:e};if(i){this._send(i);return}let{id:s,method:n,params:r}=t;if(s===void 0){if(n==="notifications/initialized")return;return}try{switch(n){case"initialize":this._send(ct(s,{protocolVersion:oh,capabilities:{tools:{}},serverInfo:ah}));break;case"tools/list":this._send(ct(s,{tools:this._visibleTools()}));break;case"tools/call":await this._handleToolCall(s,r);break;case"ping":this._send(ct(s,{}));break;default:this._send(Ce(s,an,`Method not found: ${n}`))}}catch(a){this._send(Ce(s,sh,a.message||"Internal error"))}}async _handleToolCall(e,t){let i=t?.name,s=t?.arguments??{};if(!i){this._send(Ce(e,ih,"tools/call requires params.name"));return}let n=rn.find((a)=>a.name===i);if(!n){this._send(Ce(e,an,`Unknown tool: ${i}`));return}let r=s.collection||s.collection_name||null;if(!this._hasScope(r,n.scope)){this._send(ct(e,on(`Access denied: "${i}" requires "${n.scope}" scope on collection "${r}".`)));return}try{let a=await eh(i,s,this._db);this._send(ct(e,rh(JSON.stringify(a,null,2))))}catch(a){this._send(ct(e,on(a.message||String(a))))}}_visibleTools(){return rn.filter((e)=>{let t=this._scopes["*"];if(t&&(t.includes(e.scope)||t.includes("admin")))return!0;for(let[,i]of Object.entries(this._scopes))if(i.includes(e.scope)||i.includes("admin"))return!0;return!1})}_hasScope(e,t){let i=(s)=>s.includes("admin")||s.includes(t);if(e&&this._scopes[e])return i(this._scopes[e]);if(this._scopes["*"])return i(this._scopes["*"]);return!1}_send(e){this._t?.send(e)}}var Rt="migrations",ln=(e)=>JSON.stringify(e,function(t,i){let s=this[t];if(s instanceof Date)return{__skalex_date__:s.toISOString()};if(typeof i==="bigint")return{__skalex_bigint__:i.toString()};return i}),cn=(e)=>JSON.parse(e,(t,i)=>{if(i&&typeof i==="object"){if("__skalex_bigint__"in i)return BigInt(i.__skalex_bigint__);if("__skalex_date__"in i)return new Date(i.__skalex_date__)}return i});class se{constructor({path:e="./.db",format:t="gz",debug:i=!1,adapter:s,ai:n,encrypt:r,slowQueryLog:a,queryCache:l,memory:o,logger:c,plugins:h,llmAdapter:u,embeddingAdapter:d,regexMaxLength:_,idGenerator:f,serializer:p,deserializer:m,autoSave:w,ttlSweepInterval:v,lenientLoad:R}={}){if(this.dataDirectory=e,this.dataFormat=t,this.debug=i,!se._errorsAttached)se.SkalexError=Ke,se.ValidationError=C,se.UniqueConstraintError=Te,se.TransactionError=we,se.PersistenceError=Pe,se.AdapterError=pe,se.QueryError=et,se._errorsAttached=!0;this._adapterConfig=s??null;let E=s||new Br({dir:e,format:t});if(r)E=new ea(E,r.key);if(this.fs=E,this._registry=new Jr(zr),this.collections=this._registry.stores,this._collectionInstances=this._registry._instances,this._migrations=new Xr,this._connectPromise=null,this.isConnected=!1,this._aiConfig=n||null,this._encryptConfig=r||null,this._pluginsConfig=Array.isArray(h)?h:null,this._memoryConfig=o||null,this._regexMaxLength=_??500,this._idGenerator=f??null,this._serializer=p??ln,this._deserializer=m??cn,this._autoSave=w??!1,this._txManager=new Vr,this._ttlSweepInterval=v??0,this._ttlTimer=null,this._logger=c??Qs,this._embeddingAdapter=d??(n?Uc(n):null),this._aiAdapter=u??(n?zc(n):null),this._persistence=new Hr({adapter:this.fs,serializer:this._serializer,deserializer:this._deserializer,logger:this._logger,debug:this.debug,lenientLoad:R??!1}),this._changeLog=new ia(this),this._queryCache=new sa(l||{}),this._eventBus=new na,this._queryLog=a?new ra(a):null,this._sessionStats=new aa,this._plugins=new oa,Array.isArray(h))for(let A of h)this._plugins.register(A)}async connect(){if(this._connectPromise)return this._connectPromise;return this._connectPromise=this._doConnect(),this._connectPromise}async _doConnect(){try{await this.loadData();let e=this._getMeta();if(e.queryCache)this._queryCache.fromJSON(e.queryCache);if(this._migrations._migrations.length>0){let t=e.appliedVersions||[],i=await this._migrations.run((s)=>this.useCollection(`_migration_${s}`),t);this._saveMeta({appliedVersions:i})}if(this._sweepTtl(),this._ttlSweepInterval>0){if(this._ttlTimer=setInterval(()=>this._sweepTtl(),this._ttlSweepInterval),this._ttlTimer?.unref)this._ttlTimer.unref()}this.isConnected=!0,this._log("> - Connected to the database (\u221A)")}catch(e){throw this._logger(`Error connecting to the database: ${e}`,"error"),e}}async disconnect(){try{if(this._ttlTimer)clearInterval(this._ttlTimer),this._ttlTimer=null;await this.saveData(),this._registry.clear(),this.collections=this._registry.stores,this._collectionInstances=this._registry._instances,this._connectPromise=null,this.isConnected=!1,this._log("> - Disconnected from the database (\u221A)")}catch(e){throw this._logger(`Error disconnecting from the database: ${e}`,"error"),e}}async _ensureConnected(){if(this.isConnected)return;return this.connect()}useCollection(e){return this._registry.get(e,this)}createCollection(e,t={}){return this._registry.create(e,t,this)}_createCollectionStore(e,t={}){this._registry.createStore(e,t)}async renameCollection(e,t){this._registry.rename(e,t),await this.saveData(t),await this.fs.delete(e)}async loadData(){await this._persistence.loadAll(this.collections,{parseSchema:qr,buildIndex:this._registry.buildIndex,IndexEngine:Mt});for(let e in this._collectionInstances){let t=this.collections[e];if(t&&this._collectionInstances[e]._store!==t)this._collectionInstances[e]._store=t}}async saveData(e){await this._persistence.save(this.collections,e)}buildIndex(e,t){return this._registry.buildIndex(e,t)}addMigration(e){this._migrations.add(e)}migrationStatus(){let e=this._getMeta();return this._migrations.status(e.appliedVersions||[])}namespace(e){if(this._adapterConfig)throw new pe("ERR_SKALEX_ADAPTER_NAMESPACE_REQUIRES_FS","namespace() requires the default FsAdapter. When a custom storage adapter is configured, create a separate Skalex instance with your adapter instead.");let t=String(e).replace(/[^a-zA-Z0-9_-]/g,"_");if(!t)throw new C("ERR_SKALEX_VALIDATION_NAMESPACE_ID","namespace: id must contain at least one alphanumeric character",{id:e});return new se({path:`${this.dataDirectory}/${t}`,format:this.dataFormat,debug:this.debug,ai:this._aiConfig||void 0,encrypt:this._encryptConfig||void 0,slowQueryLog:this._queryLog?{threshold:this._queryLog._threshold,maxEntries:this._queryLog._maxEntries}:void 0,queryCache:this._queryCache?{maxSize:this._queryCache._maxSize,ttl:this._queryCache._ttl}:void 0,plugins:this._pluginsConfig||void 0,memory:this._memoryConfig||void 0,logger:this._logger!==Qs?this._logger:void 0,llmAdapter:this._aiAdapter&&!this._aiConfig?this._aiAdapter:void 0,embeddingAdapter:this._embeddingAdapter&&!this._aiConfig?this._embeddingAdapter:void 0,regexMaxLength:this._regexMaxLength!==500?this._regexMaxLength:void 0,idGenerator:this._idGenerator||void 0,serializer:this._serializer!==ln?this._serializer:void 0,deserializer:this._deserializer!==cn?this._deserializer:void 0,autoSave:this._autoSave||void 0,ttlSweepInterval:this._ttlSweepInterval||void 0})}use(e){this._plugins.register(e)}watch(e){return this._eventBus.on("*",e)}sessionStats(e){if(e!==void 0)return this._sessionStats.get(e);return this._sessionStats.all()}get _inTransaction(){return this._txManager.active}_emitEvent(e,t){if(!this._txManager.defer(()=>this._eventBus.emit(e,t)))this._eventBus.emit(e,t)}async _runAfterHook(e,t){if(!this._txManager.defer(()=>this._plugins.run(e,t)))await this._plugins.run(e,t)}async _logChange(e,t,i,s,n){if(!this._txManager.defer(()=>this._changeLog.log(e,t,i,s,n)))await this._changeLog.log(e,t,i,s,n)}async transaction(e,t={}){return this._txManager.run(e,this,t)}async seed(e,{reset:t=!1}={}){for(let[i,s]of Object.entries(e)){if(t&&this.collections[i])this._applySnapshot(i,{data:[],index:new Map}),delete this._collectionInstances[i];await this.useCollection(i).insertMany(s)}await this.saveData()}dump(){return this._registry.dump()}inspect(e){return this._registry.inspect(e)}async import(e){let t=await this.fs.readRaw(e),i;try{i=JSON.parse(t)}catch{throw new Pe("ERR_SKALEX_PERSISTENCE_INVALID_JSON",`import: invalid JSON in file "${e}"`,{filePath:e})}let s=e.split("/").pop().replace(/\.[^.]+$/,"");return this.useCollection(s).insertMany(Array.isArray(i)?i:[i],{save:!0})}async embed(e){if(!this._embeddingAdapter)throw new pe("ERR_SKALEX_ADAPTER_EMBEDDING_REQUIRED","db.embed() requires an AI adapter. Pass { ai: { provider, apiKey } } to the Skalex constructor.");return this._embeddingAdapter.embed(e)}async ask(e,t,{limit:i=20}={}){if(!this._aiAdapter)throw new pe("ERR_SKALEX_ADAPTER_LLM_REQUIRED",'db.ask() requires a language model adapter. Configure { ai: { provider, model: "..." } }.');let s=this.useCollection(e),n=this.schema(e),r=this._queryCache.get(e,n,t);if(!r){r=await this._aiAdapter.generate(n,t);let a=Zc(r,n);if(a.length)a.forEach((l)=>this._log(`[ask] ${l}`));this._queryCache.set(e,n,t,r),this._saveMeta({queryCache:this._queryCache.toJSON()})}return s.find(Qc(r,{regexMaxLength:this._regexMaxLength}),{limit:i})}schema(e){return this._registry.schema(e)}useMemory(e){return new ta(e,this)}changelog(){return this._changeLog}async restore(e,t,i={}){return this._changeLog.restore(e,t,i)}stats(e){return this._registry.stats(e)}slowQueries(e={}){if(!this._queryLog)return[];return this._queryLog.entries(e)}slowQueryCount(){return this._queryLog?this._queryLog.size:0}clearSlowQueries(){this._queryLog?.clear()}mcp(e={}){return new ha(this,e)}_getMeta(){let e=this.collections._meta;if(!e)return{};return e.index.get(Rt)||{}}_saveMeta(e){if(!this.collections._meta)this._createCollectionStore("_meta");let t=this.collections._meta,i=t.index.get(Rt);if(i)Object.assign(i,e);else{let s={_id:Rt,...e};t.data.push(s),t.index.set(Rt,s)}this._persistence.markDirty(this.collections,"_meta")}_sweepTtl(){for(let e in this.collections){let t=this.collections[e],i=Lc(t.data,t.index,t.fieldIndex?(s)=>t.fieldIndex.remove(s):null);if(i>0)this._persistence.markDirty(this.collections,e),this._log(`TTL sweep: removed ${i} expired docs from "${e}"`)}}_buildCollectionContext(){let e=this;return{ensureConnected:()=>e._ensureConnected(),get txManager(){return e._txManager},get plugins(){return e._plugins},get eventBus(){return e._eventBus},get sessionStats(){return e._sessionStats},get queryLog(){return e._queryLog},get logger(){return e._logger},get persistence(){return e._persistence},get collections(){return e.collections},embed:(t)=>e.embed(t),get idGenerator(){return e._idGenerator},get autoSave(){return e._autoSave},saveCollection:(t)=>e.saveData(t),snapshotCollection:(t)=>e._snapshotCollection(t),getCollection:(t)=>e.useCollection(t),emitEvent:(t,i)=>e._emitEvent(t,i),runAfterHook:(t,i)=>e._runAfterHook(t,i),logChange:(t,i,s,n,r)=>e._logChange(t,i,s,n,r),get fs(){return e.fs},get dataDirectory(){return e.dataDirectory}}}_log(e){if(this.debug)this._logger(e,"info")}_snapshotCollection(e){return{data:structuredClone(e.data)}}_applySnapshot(e,t){let i=this.collections[e];if(!i)return;if(i.data=t.data,i.index=this.buildIndex(t.data,"_id"),i.fieldIndex)i.fieldIndex.buildFromData(t.data)}}class da{}var lh=new Map;function ch(e,t){lh.set(e,t)}var hn=()=>new Date().toISOString();function I(e){if(!e)return e;let{_id:t,createdAt:i,updatedAt:s,_version:n,_expiresAt:r,...a}=e,l={...a};if(l.createdAt==null&&i!=null)l.createdAt=i instanceof Date?i.toISOString():String(i);if(l.updatedAt==null&&s!=null)l.updatedAt=s instanceof Date?s.toISOString():String(s);return l}class es extends da{dataDir;encryptionKey;lockPath;lockFd=null;db;tasks;config;gitRepos;bookmarks;notifications;pluginState;constructor(e){super();this.dataDir=e.dataDir,this.encryptionKey=e.encryptionKey,this.lockPath=qo(e.dataDir,".agent-db.lock")}static async create(e){if(!ko(e.dataDir))Lo(e.dataDir,{recursive:!0});let t=new es(e);t.acquireLock();try{await t.init()}catch(i){throw t.releaseLock(),i}return t}acquireLock(){try{this.lockFd=Po(this.lockPath,"wx",384),jo(this.lockFd,JSON.stringify({pid:process.pid,createdAt:new Date().toISOString()}));return}catch(t){if(t.code!=="EEXIST")throw t}let e=!1;try{let t=JSON.parse(Mo(this.lockPath,"utf8"));if(!t.pid)e=!0;else try{process.kill(t.pid,0)}catch{e=!0}}catch{e=!0}if(e){try{fs(this.lockPath)}catch{}this.acquireLock();return}throw Error(`Agent database is already open by another process (${this.lockPath})`)}releaseLock(){if(this.lockFd!==null){try{Ro(this.lockFd)}catch{}this.lockFd=null}try{fs(this.lockPath)}catch{}}async init(){this.db=new se({path:this.dataDir,encrypt:{key:this.encryptionKey},autoSave:!0,format:"gz",lenientLoad:!0}),this.tasks=this.db.createCollection("tasks",{schema:{id:{type:"string",required:!0,unique:!0},type:{type:"string",required:!0,enum:["command","script","file_operation"]},status:{type:"string",required:!0,enum:["pending","running","completed","failed"]},payload:{type:"string",required:!0},result:{type:"string"},error:{type:"string"},calendarTaskId:{type:"string"},exitCode:{type:"number"},timeout:{type:"number"}},indexes:["status","type"]}),this.config=this.db.createCollection("agent_config",{schema:{key:{type:"string",required:!0,unique:!0},value:{type:"string",required:!0}}}),this.gitRepos=this.db.createCollection("git_repositories",{schema:{id:{type:"string",required:!0,unique:!0},path:{type:"string",required:!0,unique:!0},name:{type:"string",required:!0},parentPath:{type:"string"},isSubmodule:{type:"boolean"},projectType:{type:"string"},vitePort:{type:"number"},lastScanned:{type:"string"}},indexes:["parentPath"]}),this.bookmarks=this.db.createCollection("bookmarked_commands",{schema:{id:{type:"string",required:!0,unique:!0},projectId:{type:"string"},command:{type:"string",required:!0},description:{type:"string"},category:{type:"string"}},indexes:["projectId","category"]}),this.notifications=this.db.createCollection("notifications",{schema:{id:{type:"string",required:!0,unique:!0},sessionName:{type:"string"},projectId:{type:"string"},type:{type:"string",required:!0,enum:["info","success","warning","error"]},title:{type:"string",required:!0},message:{type:"string",required:!0},status:{type:"string",required:!0,enum:["unread","read"]}},indexes:["status","projectId"]}),this.pluginState=this.db.createCollection("plugin_state",{schema:{pluginName:{type:"string",required:!0},key:{type:"string",required:!0},value:{type:"string",required:!0}},indexes:["pluginName"]}),await this.db.connect()}async close(){if(this.db?.isConnected)await this.db.disconnect();this.releaseLock()}getDbPath(){return this.dataDir}async createTask(e){let t={id:e.id,type:e.type,status:e.status,payload:e.payload};if(e.result!=null)t.result=e.result;if(e.error!=null)t.error=e.error;if(e.calendarTaskId!=null)t.calendarTaskId=e.calendarTaskId;if(e.exitCode!=null)t.exitCode=e.exitCode;if(e.timeout!=null)t.timeout=e.timeout;let i=await this.tasks.insertOne(t);return I(i)}async getTask(e){let t=await this.tasks.findOne({id:e});return t?I(t):void 0}async getAllTasks(){return(await this.tasks.find(void 0,{sort:{createdAt:-1}})).docs.map((e)=>I(e))}async getPendingTasks(){return(await this.tasks.find({status:"pending"},{sort:{createdAt:1}})).docs.map((e)=>I(e))}async updateTask(e,t){let i={};for(let[s,n]of Object.entries(t)){if(s==="id"||s==="createdAt"||s==="updatedAt")continue;i[s]=n??null}if(Object.keys(i).length===0)return;await this.tasks.updateOne({id:e},i)}async cancelTask(e){let t=await this.tasks.findOne({id:e});if(!t||t.status!=="pending"&&t.status!=="running")return!1;return await this.tasks.updateOne({id:e},{status:"failed",error:"Cancelled"}),!0}async getConfig(e){return(await this.config.findOne({key:e}))?.value}async setConfig(e,t){await this.config.upsert({key:e},{key:e,value:t})}async deleteConfig(e){return await this.config.deleteOne({key:e})!==null}async getAllConfig(){let e=await this.config.find(),t={};for(let i of e.docs)t[i.key]=i.value;return t}async bulkSetConfig(e){for(let[t,i]of Object.entries(e))await this.config.upsert({key:t},{key:t,value:i})}async getConfigStatus(){let e=await this.config.find(),t=null;for(let i of e.docs){let s=i.updatedAt,n=s instanceof Date?s.toISOString():typeof s==="string"?s:null;if(n&&(!t||n>t))t=n}return{totalKeys:e.docs.length,lastUpdated:t}}async createGitRepository(e){let t={id:e.id,path:e.path,name:e.name,isSubmodule:Boolean(e.isSubmodule),lastScanned:hn()};if(e.parentPath!=null)t.parentPath=e.parentPath;if(e.projectType!=null)t.projectType=e.projectType;if(e.vitePort!=null)t.vitePort=e.vitePort;let i=await this.gitRepos.insertOne(t);return I(i)}async getGitRepository(e){let t=await this.gitRepos.findOne({id:e});return t?I(t):void 0}async getGitRepositoryByPath(e){let t=await this.gitRepos.findOne({path:e});return t?I(t):void 0}async getAllGitRepositories(){return(await this.gitRepos.find(void 0,{sort:{path:1}})).docs.map((e)=>I(e))}async updateGitRepository(e,t){let i={lastScanned:hn()};for(let[s,n]of Object.entries(t)){if(s==="id"||s==="createdAt"||s==="updatedAt")continue;if(s==="isSubmodule"){i[s]=Boolean(n);continue}i[s]=n??null}await this.gitRepos.updateOne({id:e},i)}async deleteGitRepository(e){return await this.gitRepos.deleteOne({id:e})!==null}async fixGitHierarchy(){let e=await this.getAllGitRepositories(),t=0;for(let i of e){if(i.isSubmodule)continue;let s=e.find((n)=>n.id!==i.id&&i.path.startsWith(`${n.path}/`)&&!n.isSubmodule);if(s&&i.parentPath!==s.path)await this.updateGitRepository(i.id,{parentPath:s.path}),t++}return{fixed:t}}async createBookmarkedCommand(e){let t={id:e.id,command:e.command};if(e.projectId!=null)t.projectId=e.projectId;if(e.description!=null)t.description=e.description;if(e.category!=null)t.category=e.category;let i=await this.bookmarks.insertOne(t);return I(i)}async getBookmarkedCommand(e){let t=await this.bookmarks.findOne({id:e});return t?I(t):void 0}async getAllBookmarkedCommands(){return(await this.bookmarks.find(void 0,{sort:{createdAt:-1}})).docs.map((e)=>I(e))}async getBookmarkedCommandsByProject(e){let t=e===null?{projectId:{$fn:(i)=>i==null}}:{projectId:e};return(await this.bookmarks.find(t,{sort:{createdAt:-1}})).docs.map((i)=>I(i))}async getBookmarkedCommandsByCategory(e){return(await this.bookmarks.find({category:e},{sort:{createdAt:-1}})).docs.map((t)=>I(t))}async updateBookmarkedCommand(e,t){let i={};for(let[s,n]of Object.entries(t)){if(s==="id"||s==="createdAt"||s==="updatedAt")continue;i[s]=n??null}if(Object.keys(i).length===0)return;await this.bookmarks.updateOne({id:e},i)}async deleteBookmarkedCommand(e){return await this.bookmarks.deleteOne({id:e})!==null}async executeBookmarkedCommand(e){return this.getBookmarkedCommand(e)}async createNotification(e){let t={id:e.id,type:e.type,title:e.title,message:e.message,status:e.status};if(e.sessionName!=null)t.sessionName=e.sessionName;if(e.projectId!=null)t.projectId=e.projectId;let i=await this.notifications.insertOne(t);return I(i)}async getNotification(e){let t=await this.notifications.findOne({id:e});return t?I(t):void 0}async getAllNotifications(){return(await this.notifications.find(void 0,{sort:{createdAt:-1}})).docs.map((e)=>I(e))}async getNotificationsByProject(e){let t=e===null?{projectId:{$fn:(i)=>i==null}}:{projectId:e};return(await this.notifications.find(t,{sort:{createdAt:-1}})).docs.map((i)=>I(i))}async getGlobalNotifications(){return this.getNotificationsByProject(null)}async getUnreadNotifications(){return(await this.notifications.find({status:"unread"},{sort:{createdAt:-1}})).docs.map((e)=>I(e))}async updateNotificationStatus(e,t){await this.notifications.updateOne({id:e},{status:t})}async markAllNotificationsRead(){return(await this.notifications.updateMany({status:"unread"},{status:"read"})).length}async deleteNotification(e){return await this.notifications.deleteOne({id:e})!==null}async clearOldNotifications(e=30){let t=new Date(Date.now()-e*24*60*60*1000);return(await this.notifications.deleteMany({createdAt:{$fn:(i)=>i instanceof Date&&i<t}})).length}async getPluginState(e,t){return(await this.pluginState.findOne({$and:[{pluginName:e},{key:t}]}))?.value}async setPluginState(e,t,i){await this.pluginState.upsert({$and:[{pluginName:e},{key:t}]},{pluginName:e,key:t,value:i})}async deletePluginState(e,t){return await this.pluginState.deleteOne({$and:[{pluginName:e},{key:t}]})!==null}async getAllPluginState(e){return(await this.pluginState.find({pluginName:e},{sort:{key:1}})).docs.map((t)=>{let i=t.updatedAt,s=i instanceof Date?i.toISOString():typeof i==="string"?i:void 0;return{key:t.key,value:t.value,updatedAt:s}})}async deleteAllPluginState(e){return(await this.pluginState.deleteMany({pluginName:e})).length}}var ua=(e)=>es.create(e);ch("skalex",ua);var hh=new Map;function _a(e,t){hh.set(e,t)}class fa{}_a("skalex",ua);var dn=()=>new Date().toISOString();function b(e){if(!e)return e;let{_id:t,createdAt:i,updatedAt:s,_version:n,_expiresAt:r,...a}=e,l={...a};if(l.createdAt==null&&i!=null)l.createdAt=i instanceof Date?i.toISOString():String(i);if(l.updatedAt==null&&s!=null)l.updatedAt=s instanceof Date?s.toISOString():String(s);return l}class ts extends fa{dataDir;encryptionKey;lockPath;lockFd=null;db;tasks;config;gitRepos;bookmarks;notifications;pluginState;constructor(e){super();this.dataDir=e.dataDir,this.encryptionKey=e.encryptionKey,this.lockPath=$o(e.dataDir,".agent-db.lock")}static async create(e){if(!Io(e.dataDir))bo(e.dataDir,{recursive:!0});let t=new ts(e);t.acquireLock();try{await t.init()}catch(i){throw t.releaseLock(),i}return t}acquireLock(){try{this.lockFd=To(this.lockPath,"wx",384),No(this.lockFd,JSON.stringify({pid:process.pid,createdAt:new Date().toISOString()}));return}catch(t){if(t.code!=="EEXIST")throw t}let e=!1;try{let t=JSON.parse(Oo(this.lockPath,"utf8"));if(!t.pid)e=!0;else try{process.kill(t.pid,0)}catch{e=!0}}catch{e=!0}if(e){try{_s(this.lockPath)}catch{}this.acquireLock();return}throw Error(`Agent database is already open by another process (${this.lockPath})`)}releaseLock(){if(this.lockFd!==null){try{So(this.lockFd)}catch{}this.lockFd=null}try{_s(this.lockPath)}catch{}}async init(){this.db=new ie({path:this.dataDir,encrypt:{key:this.encryptionKey},autoSave:!0,format:"gz",lenientLoad:!0}),this.tasks=this.db.createCollection("tasks",{schema:{id:{type:"string",required:!0,unique:!0},type:{type:"string",required:!0,enum:["command","script","file_operation"]},status:{type:"string",required:!0,enum:["pending","running","completed","failed"]},payload:{type:"string",required:!0},result:{type:"string"},error:{type:"string"},calendarTaskId:{type:"string"},exitCode:{type:"number"},timeout:{type:"number"}},indexes:["status","type"]}),this.config=this.db.createCollection("agent_config",{schema:{key:{type:"string",required:!0,unique:!0},value:{type:"string",required:!0}}}),this.gitRepos=this.db.createCollection("git_repositories",{schema:{id:{type:"string",required:!0,unique:!0},path:{type:"string",required:!0,unique:!0},name:{type:"string",required:!0},parentPath:{type:"string"},isSubmodule:{type:"boolean"},projectType:{type:"string"},vitePort:{type:"number"},lastScanned:{type:"string"}},indexes:["parentPath"]}),this.bookmarks=this.db.createCollection("bookmarked_commands",{schema:{id:{type:"string",required:!0,unique:!0},projectId:{type:"string"},command:{type:"string",required:!0},description:{type:"string"},category:{type:"string"}},indexes:["projectId","category"]}),this.notifications=this.db.createCollection("notifications",{schema:{id:{type:"string",required:!0,unique:!0},sessionName:{type:"string"},projectId:{type:"string"},type:{type:"string",required:!0,enum:["info","success","warning","error"]},title:{type:"string",required:!0},message:{type:"string",required:!0},status:{type:"string",required:!0,enum:["unread","read"]}},indexes:["status","projectId"]}),this.pluginState=this.db.createCollection("plugin_state",{schema:{pluginName:{type:"string",required:!0},key:{type:"string",required:!0},value:{type:"string",required:!0}},indexes:["pluginName"]}),await this.db.connect()}async close(){if(this.db?.isConnected)await this.db.disconnect();this.releaseLock()}getDbPath(){return this.dataDir}async createTask(e){let t={id:e.id,type:e.type,status:e.status,payload:e.payload};if(e.result!=null)t.result=e.result;if(e.error!=null)t.error=e.error;if(e.calendarTaskId!=null)t.calendarTaskId=e.calendarTaskId;if(e.exitCode!=null)t.exitCode=e.exitCode;if(e.timeout!=null)t.timeout=e.timeout;let i=await this.tasks.insertOne(t);return b(i)}async getTask(e){let t=await this.tasks.findOne({id:e});return t?b(t):void 0}async getAllTasks(){return(await this.tasks.find(void 0,{sort:{createdAt:-1}})).docs.map((e)=>b(e))}async getPendingTasks(){return(await this.tasks.find({status:"pending"},{sort:{createdAt:1}})).docs.map((e)=>b(e))}async updateTask(e,t){let i={};for(let[s,n]of Object.entries(t)){if(s==="id"||s==="createdAt"||s==="updatedAt")continue;i[s]=n??null}if(Object.keys(i).length===0)return;await this.tasks.updateOne({id:e},i)}async cancelTask(e){let t=await this.tasks.findOne({id:e});if(!t||t.status!=="pending"&&t.status!=="running")return!1;return await this.tasks.updateOne({id:e},{status:"failed",error:"Cancelled"}),!0}async getConfig(e){return(await this.config.findOne({key:e}))?.value}async setConfig(e,t){await this.config.upsert({key:e},{key:e,value:t})}async deleteConfig(e){return await this.config.deleteOne({key:e})!==null}async getAllConfig(){let e=await this.config.find(),t={};for(let i of e.docs)t[i.key]=i.value;return t}async bulkSetConfig(e){for(let[t,i]of Object.entries(e))await this.config.upsert({key:t},{key:t,value:i})}async getConfigStatus(){let e=await this.config.find(),t=null;for(let i of e.docs){let s=i.updatedAt,n=s instanceof Date?s.toISOString():typeof s==="string"?s:null;if(n&&(!t||n>t))t=n}return{totalKeys:e.docs.length,lastUpdated:t}}async createGitRepository(e){let t={id:e.id,path:e.path,name:e.name,isSubmodule:Boolean(e.isSubmodule),lastScanned:dn()};if(e.parentPath!=null)t.parentPath=e.parentPath;if(e.projectType!=null)t.projectType=e.projectType;if(e.vitePort!=null)t.vitePort=e.vitePort;let i=await this.gitRepos.insertOne(t);return b(i)}async getGitRepository(e){let t=await this.gitRepos.findOne({id:e});return t?b(t):void 0}async getGitRepositoryByPath(e){let t=await this.gitRepos.findOne({path:e});return t?b(t):void 0}async getAllGitRepositories(){return(await this.gitRepos.find(void 0,{sort:{path:1}})).docs.map((e)=>b(e))}async updateGitRepository(e,t){let i={lastScanned:dn()};for(let[s,n]of Object.entries(t)){if(s==="id"||s==="createdAt"||s==="updatedAt")continue;if(s==="isSubmodule"){i[s]=Boolean(n);continue}i[s]=n??null}await this.gitRepos.updateOne({id:e},i)}async deleteGitRepository(e){return await this.gitRepos.deleteOne({id:e})!==null}async fixGitHierarchy(){let e=await this.getAllGitRepositories(),t=0;for(let i of e){if(i.isSubmodule)continue;let s=e.find((n)=>n.id!==i.id&&i.path.startsWith(`${n.path}/`)&&!n.isSubmodule);if(s&&i.parentPath!==s.path)await this.updateGitRepository(i.id,{parentPath:s.path}),t++}return{fixed:t}}async createBookmarkedCommand(e){let t={id:e.id,command:e.command};if(e.projectId!=null)t.projectId=e.projectId;if(e.description!=null)t.description=e.description;if(e.category!=null)t.category=e.category;let i=await this.bookmarks.insertOne(t);return b(i)}async getBookmarkedCommand(e){let t=await this.bookmarks.findOne({id:e});return t?b(t):void 0}async getAllBookmarkedCommands(){return(await this.bookmarks.find(void 0,{sort:{createdAt:-1}})).docs.map((e)=>b(e))}async getBookmarkedCommandsByProject(e){let t=e===null?{projectId:{$fn:(i)=>i==null}}:{projectId:e};return(await this.bookmarks.find(t,{sort:{createdAt:-1}})).docs.map((i)=>b(i))}async getBookmarkedCommandsByCategory(e){return(await this.bookmarks.find({category:e},{sort:{createdAt:-1}})).docs.map((t)=>b(t))}async updateBookmarkedCommand(e,t){let i={};for(let[s,n]of Object.entries(t)){if(s==="id"||s==="createdAt"||s==="updatedAt")continue;i[s]=n??null}if(Object.keys(i).length===0)return;await this.bookmarks.updateOne({id:e},i)}async deleteBookmarkedCommand(e){return await this.bookmarks.deleteOne({id:e})!==null}async executeBookmarkedCommand(e){return this.getBookmarkedCommand(e)}async createNotification(e){let t={id:e.id,type:e.type,title:e.title,message:e.message,status:e.status};if(e.sessionName!=null)t.sessionName=e.sessionName;if(e.projectId!=null)t.projectId=e.projectId;let i=await this.notifications.insertOne(t);return b(i)}async getNotification(e){let t=await this.notifications.findOne({id:e});return t?b(t):void 0}async getAllNotifications(){return(await this.notifications.find(void 0,{sort:{createdAt:-1}})).docs.map((e)=>b(e))}async getNotificationsByProject(e){let t=e===null?{projectId:{$fn:(i)=>i==null}}:{projectId:e};return(await this.notifications.find(t,{sort:{createdAt:-1}})).docs.map((i)=>b(i))}async getGlobalNotifications(){return this.getNotificationsByProject(null)}async getUnreadNotifications(){return(await this.notifications.find({status:"unread"},{sort:{createdAt:-1}})).docs.map((e)=>b(e))}async updateNotificationStatus(e,t){await this.notifications.updateOne({id:e},{status:t})}async markAllNotificationsRead(){return(await this.notifications.updateMany({status:"unread"},{status:"read"})).length}async deleteNotification(e){return await this.notifications.deleteOne({id:e})!==null}async clearOldNotifications(e=30){let t=new Date(Date.now()-e*24*60*60*1000);return(await this.notifications.deleteMany({createdAt:{$fn:(i)=>i instanceof Date&&i<t}})).length}async getPluginState(e,t){return(await this.pluginState.findOne({$and:[{pluginName:e},{key:t}]}))?.value}async setPluginState(e,t,i){await this.pluginState.upsert({$and:[{pluginName:e},{key:t}]},{pluginName:e,key:t,value:i})}async deletePluginState(e,t){return await this.pluginState.deleteOne({$and:[{pluginName:e},{key:t}]})!==null}async getAllPluginState(e){return(await this.pluginState.find({pluginName:e},{sort:{key:1}})).docs.map((t)=>{let i=t.updatedAt,s=i instanceof Date?i.toISOString():typeof i==="string"?i:void 0;return{key:t.key,value:t.value,updatedAt:s}})}async deleteAllPluginState(e){return(await this.pluginState.deleteMany({pluginName:e})).length}}var pa=(e)=>ts.create(e);_a("skalex",pa);var dh=new Map;function ma(e,t){dh.set(e,t)}class ga{}ma("skalex",pa);var un=()=>new Date().toISOString();function T(e){if(!e)return e;let{_id:t,createdAt:i,updatedAt:s,_version:n,_expiresAt:r,...a}=e,l={...a};if(l.createdAt==null&&i!=null)l.createdAt=i instanceof Date?i.toISOString():String(i);if(l.updatedAt==null&&s!=null)l.updatedAt=s instanceof Date?s.toISOString():String(s);return l}class is extends ga{dataDir;encryptionKey;lockPath;lockFd=null;db;tasks;config;gitRepos;bookmarks;notifications;pluginState;constructor(e){super();this.dataDir=e.dataDir,this.encryptionKey=e.encryptionKey,this.lockPath=xo(e.dataDir,".agent-db.lock")}static async create(e){if(!mo(e.dataDir))go(e.dataDir,{recursive:!0});let t=new is(e);t.acquireLock();try{await t.init()}catch(i){throw t.releaseLock(),i}return t}acquireLock(){try{this.lockFd=yo(this.lockPath,"wx",384),Ao(this.lockFd,JSON.stringify({pid:process.pid,createdAt:new Date().toISOString()}));return}catch(t){if(t.code!=="EEXIST")throw t}let e=!1;try{let t=JSON.parse(wo(this.lockPath,"utf8"));if(!t.pid)e=!0;else try{process.kill(t.pid,0)}catch{e=!0}}catch{e=!0}if(e){try{us(this.lockPath)}catch{}this.acquireLock();return}throw Error(`Agent database is already open by another process (${this.lockPath})`)}releaseLock(){if(this.lockFd!==null){try{po(this.lockFd)}catch{}this.lockFd=null}try{us(this.lockPath)}catch{}}async init(){this.db=new te({path:this.dataDir,encrypt:{key:this.encryptionKey},autoSave:!0,format:"gz",lenientLoad:!0}),this.tasks=this.db.createCollection("tasks",{schema:{id:{type:"string",required:!0,unique:!0},type:{type:"string",required:!0,enum:["command","script","file_operation"]},status:{type:"string",required:!0,enum:["pending","running","completed","failed"]},payload:{type:"string",required:!0},result:{type:"string"},error:{type:"string"},calendarTaskId:{type:"string"},exitCode:{type:"number"},timeout:{type:"number"}},indexes:["status","type"]}),this.config=this.db.createCollection("agent_config",{schema:{key:{type:"string",required:!0,unique:!0},value:{type:"string",required:!0}}}),this.gitRepos=this.db.createCollection("git_repositories",{schema:{id:{type:"string",required:!0,unique:!0},path:{type:"string",required:!0,unique:!0},name:{type:"string",required:!0},parentPath:{type:"string"},isSubmodule:{type:"boolean"},projectType:{type:"string"},vitePort:{type:"number"},lastScanned:{type:"string"}},indexes:["parentPath"]}),this.bookmarks=this.db.createCollection("bookmarked_commands",{schema:{id:{type:"string",required:!0,unique:!0},projectId:{type:"string"},command:{type:"string",required:!0},description:{type:"string"},category:{type:"string"}},indexes:["projectId","category"]}),this.notifications=this.db.createCollection("notifications",{schema:{id:{type:"string",required:!0,unique:!0},sessionName:{type:"string"},projectId:{type:"string"},type:{type:"string",required:!0,enum:["info","success","warning","error"]},title:{type:"string",required:!0},message:{type:"string",required:!0},status:{type:"string",required:!0,enum:["unread","read"]}},indexes:["status","projectId"]}),this.pluginState=this.db.createCollection("plugin_state",{schema:{pluginName:{type:"string",required:!0},key:{type:"string",required:!0},value:{type:"string",required:!0}},indexes:["pluginName"]}),await this.db.connect()}async close(){if(this.db?.isConnected)await this.db.disconnect();this.releaseLock()}getDbPath(){return this.dataDir}async createTask(e){let t={id:e.id,type:e.type,status:e.status,payload:e.payload};if(e.result!=null)t.result=e.result;if(e.error!=null)t.error=e.error;if(e.calendarTaskId!=null)t.calendarTaskId=e.calendarTaskId;if(e.exitCode!=null)t.exitCode=e.exitCode;if(e.timeout!=null)t.timeout=e.timeout;let i=await this.tasks.insertOne(t);return T(i)}async getTask(e){let t=await this.tasks.findOne({id:e});return t?T(t):void 0}async getAllTasks(){return(await this.tasks.find(void 0,{sort:{createdAt:-1}})).docs.map((e)=>T(e))}async getPendingTasks(){return(await this.tasks.find({status:"pending"},{sort:{createdAt:1}})).docs.map((e)=>T(e))}async updateTask(e,t){let i={};for(let[s,n]of Object.entries(t)){if(s==="id"||s==="createdAt"||s==="updatedAt")continue;i[s]=n??null}if(Object.keys(i).length===0)return;await this.tasks.updateOne({id:e},i)}async cancelTask(e){let t=await this.tasks.findOne({id:e});if(!t||t.status!=="pending"&&t.status!=="running")return!1;return await this.tasks.updateOne({id:e},{status:"failed",error:"Cancelled"}),!0}async getConfig(e){return(await this.config.findOne({key:e}))?.value}async setConfig(e,t){await this.config.upsert({key:e},{key:e,value:t})}async deleteConfig(e){return await this.config.deleteOne({key:e})!==null}async getAllConfig(){let e=await this.config.find(),t={};for(let i of e.docs)t[i.key]=i.value;return t}async bulkSetConfig(e){for(let[t,i]of Object.entries(e))await this.config.upsert({key:t},{key:t,value:i})}async getConfigStatus(){let e=await this.config.find(),t=null;for(let i of e.docs){let s=i.updatedAt,n=s instanceof Date?s.toISOString():typeof s==="string"?s:null;if(n&&(!t||n>t))t=n}return{totalKeys:e.docs.length,lastUpdated:t}}async createGitRepository(e){let t={id:e.id,path:e.path,name:e.name,isSubmodule:Boolean(e.isSubmodule),lastScanned:un()};if(e.parentPath!=null)t.parentPath=e.parentPath;if(e.projectType!=null)t.projectType=e.projectType;if(e.vitePort!=null)t.vitePort=e.vitePort;let i=await this.gitRepos.insertOne(t);return T(i)}async getGitRepository(e){let t=await this.gitRepos.findOne({id:e});return t?T(t):void 0}async getGitRepositoryByPath(e){let t=await this.gitRepos.findOne({path:e});return t?T(t):void 0}async getAllGitRepositories(){return(await this.gitRepos.find(void 0,{sort:{path:1}})).docs.map((e)=>T(e))}async updateGitRepository(e,t){let i={lastScanned:un()};for(let[s,n]of Object.entries(t)){if(s==="id"||s==="createdAt"||s==="updatedAt")continue;if(s==="isSubmodule"){i[s]=Boolean(n);continue}i[s]=n??null}await this.gitRepos.updateOne({id:e},i)}async deleteGitRepository(e){return await this.gitRepos.deleteOne({id:e})!==null}async fixGitHierarchy(){let e=await this.getAllGitRepositories(),t=0;for(let i of e){if(i.isSubmodule)continue;let s=e.find((n)=>n.id!==i.id&&i.path.startsWith(`${n.path}/`)&&!n.isSubmodule);if(s&&i.parentPath!==s.path)await this.updateGitRepository(i.id,{parentPath:s.path}),t++}return{fixed:t}}async createBookmarkedCommand(e){let t={id:e.id,command:e.command};if(e.projectId!=null)t.projectId=e.projectId;if(e.description!=null)t.description=e.description;if(e.category!=null)t.category=e.category;let i=await this.bookmarks.insertOne(t);return T(i)}async getBookmarkedCommand(e){let t=await this.bookmarks.findOne({id:e});return t?T(t):void 0}async getAllBookmarkedCommands(){return(await this.bookmarks.find(void 0,{sort:{createdAt:-1}})).docs.map((e)=>T(e))}async getBookmarkedCommandsByProject(e){let t=e===null?{projectId:{$fn:(i)=>i==null}}:{projectId:e};return(await this.bookmarks.find(t,{sort:{createdAt:-1}})).docs.map((i)=>T(i))}async getBookmarkedCommandsByCategory(e){return(await this.bookmarks.find({category:e},{sort:{createdAt:-1}})).docs.map((t)=>T(t))}async updateBookmarkedCommand(e,t){let i={};for(let[s,n]of Object.entries(t)){if(s==="id"||s==="createdAt"||s==="updatedAt")continue;i[s]=n??null}if(Object.keys(i).length===0)return;await this.bookmarks.updateOne({id:e},i)}async deleteBookmarkedCommand(e){return await this.bookmarks.deleteOne({id:e})!==null}async executeBookmarkedCommand(e){return this.getBookmarkedCommand(e)}async createNotification(e){let t={id:e.id,type:e.type,title:e.title,message:e.message,status:e.status};if(e.sessionName!=null)t.sessionName=e.sessionName;if(e.projectId!=null)t.projectId=e.projectId;let i=await this.notifications.insertOne(t);return T(i)}async getNotification(e){let t=await this.notifications.findOne({id:e});return t?T(t):void 0}async getAllNotifications(){return(await this.notifications.find(void 0,{sort:{createdAt:-1}})).docs.map((e)=>T(e))}async getNotificationsByProject(e){let t=e===null?{projectId:{$fn:(i)=>i==null}}:{projectId:e};return(await this.notifications.find(t,{sort:{createdAt:-1}})).docs.map((i)=>T(i))}async getGlobalNotifications(){return this.getNotificationsByProject(null)}async getUnreadNotifications(){return(await this.notifications.find({status:"unread"},{sort:{createdAt:-1}})).docs.map((e)=>T(e))}async updateNotificationStatus(e,t){await this.notifications.updateOne({id:e},{status:t})}async markAllNotificationsRead(){return(await this.notifications.updateMany({status:"unread"},{status:"read"})).length}async deleteNotification(e){return await this.notifications.deleteOne({id:e})!==null}async clearOldNotifications(e=30){let t=new Date(Date.now()-e*24*60*60*1000);return(await this.notifications.deleteMany({createdAt:{$fn:(i)=>i instanceof Date&&i<t}})).length}async getPluginState(e,t){return(await this.pluginState.findOne({$and:[{pluginName:e},{key:t}]}))?.value}async setPluginState(e,t,i){await this.pluginState.upsert({$and:[{pluginName:e},{key:t}]},{pluginName:e,key:t,value:i})}async deletePluginState(e,t){return await this.pluginState.deleteOne({$and:[{pluginName:e},{key:t}]})!==null}async getAllPluginState(e){return(await this.pluginState.find({pluginName:e},{sort:{key:1}})).docs.map((t)=>{let i=t.updatedAt,s=i instanceof Date?i.toISOString():typeof i==="string"?i:void 0;return{key:t.key,value:t.value,updatedAt:s}})}async deleteAllPluginState(e){return(await this.pluginState.deleteMany({pluginName:e})).length}}var ya=(e)=>is.create(e);ma("skalex",ya);var uh=new Map;function wa(e,t){uh.set(e,t)}class Aa{}wa("skalex",ya);var _n=()=>new Date().toISOString();function O(e){if(!e)return e;let{_id:t,createdAt:i,updatedAt:s,_version:n,_expiresAt:r,...a}=e,l={...a};if(l.createdAt==null&&i!=null)l.createdAt=i instanceof Date?i.toISOString():String(i);if(l.updatedAt==null&&s!=null)l.updatedAt=s instanceof Date?s.toISOString():String(s);return l}class ss extends Aa{dataDir;encryptionKey;lockPath;lockFd=null;db;tasks;config;gitRepos;bookmarks;notifications;pluginState;constructor(e){super();this.dataDir=e.dataDir,this.encryptionKey=e.encryptionKey,this.lockPath=uo(e.dataDir,".agent-db.lock")}static async create(e){if(!ao(e.dataDir))oo(e.dataDir,{recursive:!0});let t=new ss(e);t.acquireLock();try{await t.init()}catch(i){throw t.releaseLock(),i}return t}acquireLock(){try{this.lockFd=lo(this.lockPath,"wx",384),ho(this.lockFd,JSON.stringify({pid:process.pid,createdAt:new Date().toISOString()}));return}catch(t){if(t.code!=="EEXIST")throw t}let e=!1;try{let t=JSON.parse(co(this.lockPath,"utf8"));if(!t.pid)e=!0;else try{process.kill(t.pid,0)}catch{e=!0}}catch{e=!0}if(e){try{ds(this.lockPath)}catch{}this.acquireLock();return}throw Error(`Agent database is already open by another process (${this.lockPath})`)}releaseLock(){if(this.lockFd!==null){try{ro(this.lockFd)}catch{}this.lockFd=null}try{ds(this.lockPath)}catch{}}async init(){this.db=new ee({path:this.dataDir,encrypt:{key:this.encryptionKey},autoSave:!0,format:"gz",lenientLoad:!0}),this.tasks=this.db.createCollection("tasks",{schema:{id:{type:"string",required:!0,unique:!0},type:{type:"string",required:!0,enum:["command","script","file_operation"]},status:{type:"string",required:!0,enum:["pending","running","completed","failed"]},payload:{type:"string",required:!0},result:{type:"string"},error:{type:"string"},calendarTaskId:{type:"string"},exitCode:{type:"number"},timeout:{type:"number"}},indexes:["status","type"]}),this.config=this.db.createCollection("agent_config",{schema:{key:{type:"string",required:!0,unique:!0},value:{type:"string",required:!0}}}),this.gitRepos=this.db.createCollection("git_repositories",{schema:{id:{type:"string",required:!0,unique:!0},path:{type:"string",required:!0,unique:!0},name:{type:"string",required:!0},parentPath:{type:"string"},isSubmodule:{type:"boolean"},projectType:{type:"string"},vitePort:{type:"number"},lastScanned:{type:"string"}},indexes:["parentPath"]}),this.bookmarks=this.db.createCollection("bookmarked_commands",{schema:{id:{type:"string",required:!0,unique:!0},projectId:{type:"string"},command:{type:"string",required:!0},description:{type:"string"},category:{type:"string"}},indexes:["projectId","category"]}),this.notifications=this.db.createCollection("notifications",{schema:{id:{type:"string",required:!0,unique:!0},sessionName:{type:"string"},projectId:{type:"string"},type:{type:"string",required:!0,enum:["info","success","warning","error"]},title:{type:"string",required:!0},message:{type:"string",required:!0},status:{type:"string",required:!0,enum:["unread","read"]}},indexes:["status","projectId"]}),this.pluginState=this.db.createCollection("plugin_state",{schema:{pluginName:{type:"string",required:!0},key:{type:"string",required:!0},value:{type:"string",required:!0}},indexes:["pluginName"]}),await this.db.connect()}async close(){if(this.db?.isConnected)await this.db.disconnect();this.releaseLock()}getDbPath(){return this.dataDir}async createTask(e){let t={id:e.id,type:e.type,status:e.status,payload:e.payload};if(e.result!=null)t.result=e.result;if(e.error!=null)t.error=e.error;if(e.calendarTaskId!=null)t.calendarTaskId=e.calendarTaskId;if(e.exitCode!=null)t.exitCode=e.exitCode;if(e.timeout!=null)t.timeout=e.timeout;let i=await this.tasks.insertOne(t);return O(i)}async getTask(e){let t=await this.tasks.findOne({id:e});return t?O(t):void 0}async getAllTasks(){return(await this.tasks.find(void 0,{sort:{createdAt:-1}})).docs.map((e)=>O(e))}async getPendingTasks(){return(await this.tasks.find({status:"pending"},{sort:{createdAt:1}})).docs.map((e)=>O(e))}async updateTask(e,t){let i={};for(let[s,n]of Object.entries(t)){if(s==="id"||s==="createdAt"||s==="updatedAt")continue;i[s]=n??null}if(Object.keys(i).length===0)return;await this.tasks.updateOne({id:e},i)}async cancelTask(e){let t=await this.tasks.findOne({id:e});if(!t||t.status!=="pending"&&t.status!=="running")return!1;return await this.tasks.updateOne({id:e},{status:"failed",error:"Cancelled"}),!0}async getConfig(e){return(await this.config.findOne({key:e}))?.value}async setConfig(e,t){await this.config.upsert({key:e},{key:e,value:t})}async deleteConfig(e){return await this.config.deleteOne({key:e})!==null}async getAllConfig(){let e=await this.config.find(),t={};for(let i of e.docs)t[i.key]=i.value;return t}async bulkSetConfig(e){for(let[t,i]of Object.entries(e))await this.config.upsert({key:t},{key:t,value:i})}async getConfigStatus(){let e=await this.config.find(),t=null;for(let i of e.docs){let s=i.updatedAt,n=s instanceof Date?s.toISOString():typeof s==="string"?s:null;if(n&&(!t||n>t))t=n}return{totalKeys:e.docs.length,lastUpdated:t}}async createGitRepository(e){let t={id:e.id,path:e.path,name:e.name,isSubmodule:Boolean(e.isSubmodule),lastScanned:_n()};if(e.parentPath!=null)t.parentPath=e.parentPath;if(e.projectType!=null)t.projectType=e.projectType;if(e.vitePort!=null)t.vitePort=e.vitePort;let i=await this.gitRepos.insertOne(t);return O(i)}async getGitRepository(e){let t=await this.gitRepos.findOne({id:e});return t?O(t):void 0}async getGitRepositoryByPath(e){let t=await this.gitRepos.findOne({path:e});return t?O(t):void 0}async getAllGitRepositories(){return(await this.gitRepos.find(void 0,{sort:{path:1}})).docs.map((e)=>O(e))}async updateGitRepository(e,t){let i={lastScanned:_n()};for(let[s,n]of Object.entries(t)){if(s==="id"||s==="createdAt"||s==="updatedAt")continue;if(s==="isSubmodule"){i[s]=Boolean(n);continue}i[s]=n??null}await this.gitRepos.updateOne({id:e},i)}async deleteGitRepository(e){return await this.gitRepos.deleteOne({id:e})!==null}async fixGitHierarchy(){let e=await this.getAllGitRepositories(),t=0;for(let i of e){if(i.isSubmodule)continue;let s=e.find((n)=>n.id!==i.id&&i.path.startsWith(`${n.path}/`)&&!n.isSubmodule);if(s&&i.parentPath!==s.path)await this.updateGitRepository(i.id,{parentPath:s.path}),t++}return{fixed:t}}async createBookmarkedCommand(e){let t={id:e.id,command:e.command};if(e.projectId!=null)t.projectId=e.projectId;if(e.description!=null)t.description=e.description;if(e.category!=null)t.category=e.category;let i=await this.bookmarks.insertOne(t);return O(i)}async getBookmarkedCommand(e){let t=await this.bookmarks.findOne({id:e});return t?O(t):void 0}async getAllBookmarkedCommands(){return(await this.bookmarks.find(void 0,{sort:{createdAt:-1}})).docs.map((e)=>O(e))}async getBookmarkedCommandsByProject(e){let t=e===null?{projectId:{$fn:(i)=>i==null}}:{projectId:e};return(await this.bookmarks.find(t,{sort:{createdAt:-1}})).docs.map((i)=>O(i))}async getBookmarkedCommandsByCategory(e){return(await this.bookmarks.find({category:e},{sort:{createdAt:-1}})).docs.map((t)=>O(t))}async updateBookmarkedCommand(e,t){let i={};for(let[s,n]of Object.entries(t)){if(s==="id"||s==="createdAt"||s==="updatedAt")continue;i[s]=n??null}if(Object.keys(i).length===0)return;await this.bookmarks.updateOne({id:e},i)}async deleteBookmarkedCommand(e){return await this.bookmarks.deleteOne({id:e})!==null}async executeBookmarkedCommand(e){return this.getBookmarkedCommand(e)}async createNotification(e){let t={id:e.id,type:e.type,title:e.title,message:e.message,status:e.status};if(e.sessionName!=null)t.sessionName=e.sessionName;if(e.projectId!=null)t.projectId=e.projectId;let i=await this.notifications.insertOne(t);return O(i)}async getNotification(e){let t=await this.notifications.findOne({id:e});return t?O(t):void 0}async getAllNotifications(){return(await this.notifications.find(void 0,{sort:{createdAt:-1}})).docs.map((e)=>O(e))}async getNotificationsByProject(e){let t=e===null?{projectId:{$fn:(i)=>i==null}}:{projectId:e};return(await this.notifications.find(t,{sort:{createdAt:-1}})).docs.map((i)=>O(i))}async getGlobalNotifications(){return this.getNotificationsByProject(null)}async getUnreadNotifications(){return(await this.notifications.find({status:"unread"},{sort:{createdAt:-1}})).docs.map((e)=>O(e))}async updateNotificationStatus(e,t){await this.notifications.updateOne({id:e},{status:t})}async markAllNotificationsRead(){return(await this.notifications.updateMany({status:"unread"},{status:"read"})).length}async deleteNotification(e){return await this.notifications.deleteOne({id:e})!==null}async clearOldNotifications(e=30){let t=new Date(Date.now()-e*24*60*60*1000);return(await this.notifications.deleteMany({createdAt:{$fn:(i)=>i instanceof Date&&i<t}})).length}async getPluginState(e,t){return(await this.pluginState.findOne({$and:[{pluginName:e},{key:t}]}))?.value}async setPluginState(e,t,i){await this.pluginState.upsert({$and:[{pluginName:e},{key:t}]},{pluginName:e,key:t,value:i})}async deletePluginState(e,t){return await this.pluginState.deleteOne({$and:[{pluginName:e},{key:t}]})!==null}async getAllPluginState(e){return(await this.pluginState.find({pluginName:e},{sort:{key:1}})).docs.map((t)=>{let i=t.updatedAt,s=i instanceof Date?i.toISOString():typeof i==="string"?i:void 0;return{key:t.key,value:t.value,updatedAt:s}})}async deleteAllPluginState(e){return(await this.pluginState.deleteMany({pluginName:e})).length}}var xa=(e)=>ss.create(e);wa("skalex",xa);var ns=new Map;function _h(e,t){ns.set(e,t)}function fn(e){return ns.get(e)}function fh(){return Array.from(ns.keys())}async function ph(e){if(e.adapterFactory)return e.adapterFactory;let t=e.adapterName??process.env.VIBE_STORAGE_ADAPTER??"skalex";if(t.startsWith("custom:")){let s=t.slice(7);if(!s)throw Error("VIBE_STORAGE_ADAPTER=custom: requires a module path after the colon");let n=await import(s),r=n.default??n.createAdapter;if(typeof r!=="function")throw Error(`Custom storage adapter at ${s} must export a default function (or \`createAdapter\`) of type AgentStorageAdapterFactory`);return r}let i=fn(t);if(!i){let s={skalex:"@vibecontrols/vibe-plugin-storage-skalex",postgres:"@vibecontrols/vibe-plugin-storage-postgres",postgresql:"@vibecontrols/vibe-plugin-storage-postgres"}[t];if(s)try{await import(s),i=fn(t)}catch{}}if(!i)throw Error(`Unknown storage adapter: "${t}". Registered adapters: ${fh().join(", ")||`(none \u2014 did you install @vibecontrols/vibe-plugin-storage-${t})?`}`);return i}async function va(e){if(!e.encryptionKey)throw Error("createAgentDatabase(): encryptionKey is required. Storage is always encrypted at rest.");if(!e.dbPath)throw Error("createAgentDatabase(): dbPath is required.");return(await ph(e))({dataDir:e.dbPath,encryptionKey:e.encryptionKey,adapterOptions:e.adapterOptions})}_h("skalex",xa);var mh="@vibecontrols/vibe-plugin-storage",gd=gh();function gh(){try{let e=ri(no(import.meta.url));for(let t=0;t<10&&e&&e!==ri(e);t++){let i=so(e,"package.json");if(to(i))try{let s=JSON.parse(io(i,"utf8"));if(s.name===mh&&typeof s.version==="string")return s.version}catch{}e=ri(e)}}catch{}return"0.0.0"}import Jt from"os";import{mkdirSync as yh}from"fs";import{join as wh}from"path";var ht="profile-reconciler",Ah=60000,xh=5000,vh=30000,Eh=15,Sh=200,Ih=`
|
|
118
|
-
query VibecontrolsAgentProfiles($agentId: ID!) {
|
|
119
|
-
vibecontrolsAgentProfiles(agentId: $agentId) {
|
|
120
|
-
id
|
|
121
|
-
name
|
|
122
|
-
status
|
|
123
|
-
isDefault
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
`,bh=`
|
|
127
|
-
mutation UpdateVibecontrolsAgentProfile($id: ID!, $input: UpdateVibecontrolsAgentProfileInput!) {
|
|
128
|
-
updateVibecontrolsAgentProfile(id: $id, input: $input) {
|
|
129
|
-
id
|
|
130
|
-
status
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
`;function Ht(e){return wh(Ia(),"agents",Vt(e))}async function $a(e){try{let t=await wt({dir:Ht(e),scope:Vt(e)});return Boolean(t.clientId&&t.clientSecret)}catch{return!1}}async function Th(e){for(let t=0;t<Eh;t++){if(await $a(e))return!0;await new Promise((i)=>setTimeout(i,Sh))}return!1}async function Oh(e,t,i){let s,n=new Promise((r,a)=>{s=setTimeout(()=>a(Error(`${i} timed out after ${t}ms`)),t)});try{return await Promise.race([e,n])}finally{if(s)clearTimeout(s)}}var dt=null,rs=!1;async function Ea(e,t){try{await x.workspaceQuery(bh,{id:e,input:{status:t}})}catch{}}async function Sa(e){if(!x.isConfigured()||rs)return;rs=!0;let t=g().logger;try{let i=await e.getConfig("gateway-auth:agentRecordId");if(!i)return;let s=await x.workspaceQuery(Ih,{agentId:i});if(s.errors?.length)return;let n=s.data?.vibecontrolsAgentProfiles??[];if(n.length===0)return;let r=g().name,a;try{a=await wt({dir:Ht(r),scope:r})}catch{a=void 0}for(let l of n){if(l.isDefault||l.name===r||l.name==="default")continue;let o=Ta.get(l.name);if(o&&o.getBootState()==="ready"){if(l.status!=="ready")await Ea(l.id,"ready");continue}if(a?.clientId&&a.clientSecret&&!await $a(l.name))try{if(yh(Ht(l.name),{recursive:!0}),ba({globalGatewayUrl:a.globalGatewayUrl,workspaceGatewayUrl:a.workspaceGatewayUrl,workspaceId:a.workspaceId,organizationId:a.organizationId,clientId:a.clientId,clientSecret:a.clientSecret},{dir:Ht(l.name),scope:Vt(l.name)}),!await Th(l.name)){t.warn(ht,`Seeded creds for profile '${l.name}' not yet readable; retrying next tick`);continue}}catch(c){t.warn(ht,`Failed to seed config for profile '${l.name}'`,{error:c instanceof Error?c.message:String(c)});continue}t.info(ht,`Reconciling desired profile '${l.name}' from control plane`);try{let c=Oa(l.name),h=await Oh(Na(c),vh,`attach '${l.name}'`),u=c.getBootState();await Ea(l.id,u),t.info(ht,`Profile '${l.name}' reconciled`,{ok:h.ok,status:u,error:h.error})}catch(c){t.warn(ht,`Failed to reconcile profile '${l.name}'`,{error:c instanceof Error?c.message:String(c)})}}}catch(i){g().logger.debug(ht,`Reconcile pass failed: ${i instanceof Error?i.message:String(i)}`)}finally{rs=!1}}function ut(e,t){if(dt)return;let i=Number(process.env.VIBE_PROFILE_RECONCILE_MS),s=Number.isFinite(i)&&i>0?Math.max(i,xh):t??Ah;Sa(e),dt=setInterval(()=>{Sa(e)},s),dt.unref?.()}function bd(){if(dt)clearInterval(dt),dt=null}var Fe=10,as=3000,y="auto-report",Ma=`
|
|
134
|
-
mutation RegisterInstalledAgent($workspaceId: ID!, $input: RegisterInstalledAgentInput!) {
|
|
135
|
-
registerInstalledAgent(workspaceId: $workspaceId, input: $input) {
|
|
136
|
-
id
|
|
137
|
-
name
|
|
138
|
-
hostname
|
|
139
|
-
tunnelUrl
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
`,xt=`
|
|
143
|
-
mutation UpdateVibecontrolsAgent($id: ID!, $input: UpdateVibecontrolsAgentInput!) {
|
|
144
|
-
updateVibecontrolsAgent(id: $id, input: $input) {
|
|
145
|
-
id
|
|
146
|
-
tunnelUrl
|
|
147
|
-
tunnelStatus
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
`,Wt=`
|
|
151
|
-
query FindExistingAgent($workspaceId: ID!) {
|
|
152
|
-
vibecontrolsAgents(filter: { workspaceId: $workspaceId }) {
|
|
153
|
-
id
|
|
154
|
-
hostname
|
|
155
|
-
tunnelUrl
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
`,Nh=`
|
|
159
|
-
mutation AgentHeartbeat($id: ID!) {
|
|
160
|
-
vibecontrolsAgentHeartbeat(id: $id) {
|
|
161
|
-
id
|
|
162
|
-
isActive
|
|
163
|
-
lastHeartbeat
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
`,$h=`
|
|
167
|
-
mutation DeactivateVibecontrolsAgent($id: ID!) {
|
|
168
|
-
deactivateVibecontrolsAgent(id: $id) {
|
|
169
|
-
id
|
|
170
|
-
isActive
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
`;async function Dh(e){if(process.env.AGENT_TUNNEL_URL){let t=La(process.env.AGENT_TUNNEL_URL);if(!t)return g().logger.warn(y,"Ignoring invalid AGENT_TUNNEL_URL"),null;return g().logger.debug(y,`Using AGENT_TUNNEL_URL from environment: ${t}`),t}for(let t=1;t<=Fe;t++){let i=[],s=e.getProvider("tunnel");if(s)i.push(s);for(let n of e.listProvidersForType("tunnel")){let r=e.getProviderByName("tunnel",n.pluginName);if(r&&!i.includes(r))i.push(r)}for(let n of i){if(!n.getActiveTunnelUrl)continue;let r=await n.getActiveTunnelUrl(),a=Ra(r);if(a)return g().logger.debug(y,`Tunnel URL available on attempt ${t}: ${a}`),a;if(r)g().logger.warn(y,"Tunnel provider returned invalid URL",{url:r})}if(t<Fe)g().logger.debug(y,`Tunnel URL not ready, retrying in ${as/1000}s (attempt ${t}/${Fe})`),await new Promise((n)=>setTimeout(n,as))}return g().logger.warn(y,`Tunnel URL not available after ${Fe} attempts`),null}async function os(e,t){let i=await e.getConfig("gateway-auth:agentRecordId");if(i)return i;let s=await e.getConfig("gateway-auth:workspaceId");if(!s)return null;let n=await x.workspaceQuery(Wt,{workspaceId:s});if(n.errors?.length)return g().logger.warn(y,"Failed to discover existing agent record",{errors:n.errors.map((l)=>l.message)}),null;let r=t.toLowerCase(),a=n.data?.vibecontrolsAgents?.find((l)=>l.hostname.toLowerCase()===r);if(!a)return null;return await e.setConfig("gateway-auth:agentRecordId",a.id),g().logger.info(y,"Recovered existing agent record ID",{agentRecordId:a.id,hostname:t}),a.id}async function Ch(e,t){try{let i=await x.workspaceQuery(Wt,{workspaceId:e});if(i.errors?.length)return!0;let s=i.data?.vibecontrolsAgents;if(!s)return!0;return s.some((n)=>n.id===t)}catch{return!0}}async function Qt(e,t){if(!x.isConfigured())return;let i=await os(e,process.env.VIBE_AGENT_NAME||Jt.hostname());if(!i)return;if(t!=="killed"){g().logger.debug(y,`Skipping '${t}' lifecycle sync \u2014 startup auto-report already refreshes the live agent record and the current backend schema has no dedicated lifecycle field.`);return}let s=await x.workspaceQuery($h,{id:i});if(s.errors?.length)g().logger.warn(y,`Failed to report lifecycle state '${t}'`,{errors:s.errors.map((n)=>n.message)});else g().logger.info(y,`Reported lifecycle state: ${t}`)}async function Da(e,t,i){let s=i||x.getConfig()?.workspaceId||"";for(let n=1;n<=3;n++){await new Promise((o)=>setTimeout(o,1500));let l=(await x.workspaceQuery(Wt,{workspaceId:s})).data?.vibecontrolsAgents?.find((o)=>o.id===e)?.tunnelUrl??null;if(l===t)return g().logger.info(y,`Verified: backend tunnelUrl matches (attempt ${n})`),!0;g().logger.warn(y,`Verification failed (attempt ${n}/3): backend has '${l}', expected '${t}' \u2014 retrying update`),await x.workspaceQuery(xt,{id:e,input:{tunnelUrl:t||null,tunnelStatus:t?"CONNECTED":"DISCONNECTED"}})}return g().logger.error(y,"Failed to verify tunnel URL update after 3 attempts"),!1}async function Ca(e,t){try{let i=await x.workspaceQuery(Ma,{workspaceId:e,input:{name:t.hostname,hostname:t.hostname,platform:t.platform,architecture:t.arch,tunnelUrl:t.tunnelUrl||null,agentApiKey:t.apiKey}});if(i.errors?.length)g().logger.debug(y,"API key re-assertion returned errors (non-fatal)",{errors:i.errors.map((s)=>s.message)});else g().logger.debug(y,"Re-asserted agent API key to backend")}catch(i){g().logger.debug(y,`API key re-assertion failed (non-fatal): ${i instanceof Error?i.message:String(i)}`)}}async function Yt(e){try{let t=await x.workspaceQuery(Nh,{id:e});if(t.errors?.length)g().logger.debug(y,"Heartbeat returned errors",{errors:t.errors.map((i)=>i.message)});else g().logger.debug(y,"Heartbeat sent successfully")}catch(t){g().logger.debug(y,`Heartbeat failed: ${t instanceof Error?t.message:String(t)}`)}}var Rh=`
|
|
174
|
-
mutation NotifyVibecontrolsAgentPluginChange($agentId: ID!) {
|
|
175
|
-
notifyVibecontrolsAgentPluginChange(agentId: $agentId)
|
|
176
|
-
}
|
|
177
|
-
`;async function kd(e){try{if(!x.isConfigured())return;let t=await e.getConfig("gateway-auth:agentRecordId");if(!t)return;let i=await x.workspaceQuery(Rh,{agentId:t});if(i.errors?.length)g().logger.debug(y,"Plugin-change notify returned errors",{errors:i.errors.map((s)=>s.message)})}catch(t){g().logger.debug(y,`Plugin-change notify failed: ${t instanceof Error?t.message:String(t)}`)}}var ft=null,tt=null,_t=!1;function At(e,t,i){if(ft)return;let s=Number(process.env.VIBE_TUNNEL_SYNC_MS),n=Number.isFinite(s)&&s>0?s:i??60000;ft=setInterval(async()=>{if(!x.isConfigured())return;try{let r=t.getProvider("tunnel");if(!r||!r.getActiveTunnelUrl)return;let a=ka(await r.getActiveTunnelUrl()),l=await e.getConfig("gateway-auth:agentRecordId");if(!l)return;if(await Yt(l),!a){if(!_t)_t=!(await x.workspaceQuery(xt,{id:l,input:{tunnelStatus:"DISCONNECTED"}})).errors?.length,g().logger.warn(y,"No active tunnel URL \u2014 reported tunnel as DISCONNECTED",{reported:_t});return}let o=_t,c=x.getConfig()?.workspaceId||"",h;if(c)h=(await x.workspaceQuery(Wt,{workspaceId:c})).data?.vibecontrolsAgents?.find((p)=>p.id===l)?.tunnelUrl??null;let u=a!==tt,d=h!==void 0&&h!==a;if(!u&&!d&&!o)return;g().logger.info(y,"Re-publishing tunnel URL",{reason:o?"tunnel-recovered":d?"backend-drift":"local-change",backend:h,local:a,lastReported:tt});let _=await x.workspaceQuery(xt,{id:l,input:{tunnelUrl:a,tunnelStatus:"CONNECTED"}});if(!_.errors?.length)tt=a,_t=!1,g().logger.info(y,"Tunnel URL synced to backend",{tunnelUrl:a});else g().logger.warn(y,"Tunnel URL sync returned errors",{errors:_.errors.map((f)=>f.message)})}catch(r){g().logger.debug(y,`Tunnel sync check failed: ${r instanceof Error?r.message:String(r)}`)}},n),ft.unref?.()}function Ld(){if(ft)clearInterval(ft),ft=null;_t=!1,tt=null}async function Pd(e,t,i){if(!x.isConfigured()){g().logger.debug(y,"Gateway auth not configured \u2014 skipping auto-report");return}let s=await e.getConfig("gateway-auth:workspaceId");if(!s){g().logger.warn(y,"No workspaceId configured \u2014 skipping auto-report. "+"Configure via POST /api/agent/gateway-auth with workspaceId.");return}g().logger.info(y,"Starting auto-report to backend...");try{let n=process.env.VIBE_AGENT_NAME||Jt.hostname(),r=Jt.platform(),a=Jt.arch(),l=Pa(),o=await Dh(t),c=await os(e,n);if(c){g().logger.info(y,`Updating agent record ${c} via workspace gateway`);let u=await x.workspaceQuery(xt,{id:c,input:{tunnelUrl:o||null,tunnelStatus:o?"CONNECTED":"DISCONNECTED",agentApiKey:i}});if(u.errors?.length){if(g().logger.warn(y,"Update mutation returned errors",{errors:u.errors.map((_)=>_.message)}),await Ch(s,c)){g().logger.warn(y,"Agent record still exists \u2014 treating update failure as transient; will retry on next sync (NOT registering a duplicate)",{agentRecordId:c}),await Ca(s,{hostname:n,platform:r,arch:a,tunnelUrl:o,apiKey:i}),await Yt(c),tt=o,At(e,t),ut(e);return}g().logger.info(y,"Cached agent record no longer exists \u2014 clearing cache and registering fresh",{agentRecordId:c}),await e.setConfig("gateway-auth:agentRecordId",""),c=null}else{g().logger.info(y,"Agent record updated successfully",{agentRecordId:c,tunnelUrl:o}),await Da(c,o,s),await Ca(s,{hostname:n,platform:r,arch:a,tunnelUrl:o,apiKey:i}),await Yt(c),tt=o,At(e,t),ut(e);return}}g().logger.info(y,`Registering installed agent via workspace gateway (hostname: ${n})`);for(let u=1;u<=Fe&&!c;u++){if(u>1){let p=await os(e,n);if(p){c=p,g().logger.info(y,"Adopted an agent record discovered after a retry (not registering a duplicate)",{agentRecordId:c});break}}let d=await x.workspaceQuery(Ma,{workspaceId:s,input:{name:n,hostname:n,platform:r,architecture:a,tunnelUrl:o||null,agentApiKey:i}}),_=d.errors?.length?void 0:d.data?.registerInstalledAgent?.id;if(_){await e.setConfig("gateway-auth:agentRecordId",_),c=_,g().logger.info(y,"Agent registered successfully",{agentRecordId:_,name:d.data?.registerInstalledAgent?.name,tunnelUrl:o,version:l,attempt:u});break}let f=d.errors?.length?d.errors.map((p)=>p.message):["registerInstalledAgent returned no id"];if(u<Fe){let p=Math.min(30000,as*2**(u-1));g().logger.warn(y,`Register installed agent failed (attempt ${u}/${Fe}) \u2014 retrying in ${Math.round(p/1000)}s`,{errors:f}),await new Promise((m)=>setTimeout(m,p));continue}g().logger.error(y,"Register installed agent failed after retries \u2014 agent is running but NOT registered with the workspace",{errors:f}),g().recordDegradedReason("agent-registration",`registerInstalledAgent failed after ${Fe} attempts: ${f.join("; ")}`)}if(!c){At(e,t),ut(e);return}let h=await x.workspaceQuery(xt,{id:c,input:{tunnelUrl:o||null,tunnelStatus:o?"CONNECTED":"DISCONNECTED"}});if(h.errors?.length)g().logger.warn(y,"Post-registration agent update returned errors",{errors:h.errors.map((u)=>u.message)});else g().logger.info(y,"Post-registration agent sync completed",{agentRecordId:c,tunnelUrl:o});if(await Da(c,o,s),await Yt(c),tt=o,At(e,t),ut(e),process.env.VIBE_BRIDGE_DISABLED!=="1")try{let{bridgeClient:u}=await import("./bridge-client-bgkr7bcn.js"),d=process.env.VIBECONTROLS_PROFILE||"default";u.start(c,d)}catch(u){g().logger.warn(y,`bridge-client start failed: ${u instanceof Error?u.message:String(u)}`)}}catch(n){g().logger.error(y,`Auto-report failed: ${n instanceof Error?n.message:String(n)}`);try{At(e,t),ut(e)}catch{}}}var S="lifecycle";class ls{serviceRegistry;pluginManager;db;app;hostServices;appStopFn;state="running";transition=Promise.resolve();constructor(e,t,i,s,n,r){this.serviceRegistry=e;this.pluginManager=t;this.db=i;this.app=s;this.hostServices=n;this.appStopFn=r}getState(){return this.state}enqueueTransition(e,t){let i=this.transition.then(t,t);return this.transition=i.catch((s)=>{g().logger.warn(S,`Lifecycle transition '${e}' failed`,{error:String(s)})}),i}async stop(){return this.enqueueTransition("stop",async()=>{if(this.state==="stopped"){g().logger.warn(S,"Agent is already stopped");return}g().logger.info(S,"Stopping agent..."),this.state="stopped";let e=this.serviceRegistry.getProvider("session");if(e)try{let t=await e.list();for(let i of t)if(i.status==="active")try{await e.kill(i.id),g().logger.debug(S,`Killed session ${i.id}`)}catch(s){g().logger.warn(S,`Failed to kill session ${i.id}`,{error:String(s)})}}catch(t){g().logger.warn(S,"Failed to list/stop sessions",{error:String(t)})}try{await this.pluginManager.dispatchServerStop()}catch(t){g().logger.warn(S,"Error during plugin stop dispatch",{error:String(t)})}g().logger.info(S,"Agent stopped (tunnel + HTTP still alive)");try{await Qt(this.db,"stopped")}catch(t){g().logger.warn(S,"Failed to report stopped state to backend",{error:String(t)})}})}async start(){return this.enqueueTransition("start",async()=>{if(this.state==="running"){g().logger.warn(S,"Agent is already running");return}g().logger.info(S,"Starting agent..."),this.state="running";try{await this.pluginManager.dispatchServerStart(this.app,this.hostServices),await this.pluginManager.dispatchServerReady(this.app,this.hostServices)}catch(e){g().logger.warn(S,"Error during plugin start dispatch",{error:String(e)})}g().logger.info(S,"Agent started");try{await Qt(this.db,"running")}catch(e){g().logger.warn(S,"Failed to report running state to backend",{error:String(e)})}})}async softStop(){return this.enqueueTransition("softStop",async()=>{g().logger.info(S,"Soft-stopping agent (hot-reload)...");try{await this.pluginManager.dispatchServerStop({reason:"reload"})}catch(e){g().logger.warn(S,"Error during plugin soft-stop dispatch",{error:String(e)})}try{await this.appStopFn({reason:"reload"})}catch{}g().logger.info(S,"Agent soft-stopped \u2014 exiting for reload"),process.exit(0)})}async prepareForReset(){return this.enqueueTransition("prepareForReset",async()=>{g().logger.info(S,"Preparing agent for reset..."),this.state="stopped";try{await this.pluginManager.dispatchServerStop({reason:"shutdown"})}catch(e){g().logger.warn(S,"Error during reset stop dispatch",{error:String(e)})}})}async kill(){return this.enqueueTransition("kill",async()=>{g().logger.info(S,"Killing agent...");try{await Promise.race([Qt(this.db,"killed"),new Promise((t,i)=>setTimeout(()=>i(Error("Timeout")),5000))])}catch(t){g().logger.warn(S,"Failed to report killed state to backend",{error:String(t)})}let e=this.serviceRegistry.getProvider("tunnel");if(e)try{let t=await e.list();for(let i of t)try{await e.stop(i.id)}catch{}}catch(t){g().logger.warn(S,"Failed to stop tunnels",{error:String(t)})}try{await this.appStopFn()}catch{}g().logger.info(S,"Agent killed \u2014 exiting process"),process.exit(0)})}}async function kh(e){let t=await Lh(e);if(t)return t;return Ph(e.name)}async function Lh(e){let t;try{t=await wt({dir:e.dataDir,scope:e.name})}catch{return null}if(!t.clientId||!t.clientSecret||!t.globalGatewayUrl||!t.workspaceGatewayUrl||!t.workspaceId)return null;return{clientId:t.clientId,clientSecret:t.clientSecret,globalGatewayUrl:t.globalGatewayUrl,workspaceGatewayUrl:t.workspaceGatewayUrl,workspaceId:t.workspaceId,organizationId:t.organizationId}}function Ph(e){let t=e.replace(/[^A-Za-z0-9_]/g,"_").toUpperCase(),i=(o)=>process.env[`VIBE_${t}_${o}`],s=i("CLIENT_ID"),n=i("CLIENT_SECRET"),r=i("GLOBAL_GATEWAY_URL"),a=i("WORKSPACE_GATEWAY_URL"),l=i("WORKSPACE_ID");if(!s||!n||!r||!a||!l)return null;return{clientId:s,clientSecret:n,globalGatewayUrl:r,workspaceGatewayUrl:a,workspaceId:l,organizationId:i("ORGANIZATION_ID")}}async function Na(e){let t=await kh(e);if(!t)return{ok:!1,error:`no creds for profile '${e.name}' (run gateway-auth against this profile or set VIBE_${e.name.toUpperCase()}_* env vars)`};e.setBootState("initializing");let i;try{let l=await hs(t);i=await va({dbPath:e.dataDir,encryptionKey:l})}catch(l){return e.setBootState("awaiting-config"),{ok:!1,error:l instanceof Error?l.message:String(l)}}let s=new Fa(i);await s.hydrateDefaultsFromDb();let n=new Ba(i);await n.loadCorePlugins();try{await n.loadAll()}catch(l){e.logger.warn("secondary-profile-attach","failed to load some external plugins for secondary profile",{error:l instanceof Error?l.message:String(l)})}let r={storage:{async get(l,o){return await i.getPluginState(l,o)??null},async set(l,o,c){await i.setPluginState(l,o,c)},async delete(l,o){return i.deletePluginState(l,o)},async list(l){return await i.getAllPluginState(l)},async deleteAll(l){return i.deleteAllPluginState(l)}},logger:e.logger,serviceRegistry:s,getProvider:(l)=>s.getProvider(l),getAgentBaseUrl:()=>process.env.AGENT_URL||"http://localhost:0",getAgentVersion:()=>"0.0.0",broadcast:()=>{},workspaceQuery:async()=>({}),isGatewayConfigured:()=>!0,getAgentRecordId:async()=>await i.getConfig("gateway-auth:agentRecordId")??null,getWorkspaceId:async()=>await i.getConfig("gateway-auth:workspaceId")??null,getConfig:async(l)=>await i.getConfig(l)??void 0,getPluginRegistry:()=>"",getDataDir:()=>e.dataDir,cliContributors:new Ua,audit:{emit:(l,o)=>e.audit.emit("agent",l,o??{})},telemetry:{emit:()=>{}},os:ja(),iframeBridge:za()},a=new ls(s,n,i,{},r,async()=>{});s.registerService("agent","lifecycle",a),e.attachServices({db:i,serviceRegistry:s,pluginManager:n,lifecycle:a,hostServices:r});try{Ka(e)}catch(l){e.logger.warn("secondary-profile-attach","failed to mount per-profile plugin routes",{error:l instanceof Error?l.message:String(l)})}try{await e.keyVault.bindApiKey(qa())}catch{}return e.setBootState("ready"),{ok:!0}}
|
|
178
|
-
export{va as u,hs as v,ls as w,Na as x,bd as y,kd as z,Ld as A,Pd as B};
|