@manifesto-ai/core 2.11.1 → 2.13.0
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/index.js +1 -1
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{z as _t}from"zod";var Ua=_t.enum(["VALIDATION_ERROR","PATH_NOT_FOUND","TYPE_MISMATCH","DIVISION_BY_ZERO","INDEX_OUT_OF_BOUNDS","UNKNOWN_ACTION","ACTION_UNAVAILABLE","INVALID_INPUT","CYCLIC_DEPENDENCY","UNKNOWN_FLOW","CYCLIC_CALL","UNKNOWN_EFFECT","INTERNAL_ERROR"]);function v(e,r,t,n,o,a){return{code:e,message:r,source:{actionId:t,nodePath:n},timestamp:o,context:a}}function Za(e){if(typeof e!="object"||e===null)return!1;let r=e;return typeof r.code=="string"&&typeof r.message=="string"&&typeof r.source=="object"&&r.source!==null&&typeof r.source.actionId=="string"&&typeof r.source.nodePath=="string"&&typeof r.timestamp=="number"}import{z as w}from"zod";function Fe(e){if(e==null)return e;if(Array.isArray(e))return e.map(Fe);if(typeof e=="object"){let r={},t=Object.keys(e).sort();for(let n of t){let o=e[n];o!==void 0&&(r[n]=Fe(o))}return r}return e}function pe(e){let r=Fe(e);return JSON.stringify(r)}function _a(e){return Ie(e)}function Ie(e){if(e===null)return"null";switch(typeof e){case"string":return JSON.stringify(e);case"number":return Number.isFinite(e)?JSON.stringify(e):"null";case"boolean":return e?"true":"false";case"undefined":return"null";case"object":return Array.isArray(e)?Ht(e):Wt(e);default:return"null"}}function Ht(e){return`[${e.map(t=>t===void 0||typeof t=="function"||typeof t=="symbol"?"null":Ie(t)).join(",")}]`}function Wt(e){let r=Object.keys(e).sort(ne),t=[];for(let n of r){let o=e[n];o===void 0||typeof o=="function"||typeof o=="symbol"||t.push(`${JSON.stringify(n)}:${Ie(o)}`)}return`{${t.join(",")}}`}function ne(e,r){let t=Array.from(e),n=Array.from(r),o=Math.min(t.length,n.length);for(let a=0;a<o;a++){let s=t[a].codePointAt(0)??0,p=n[a].codePointAt(0)??0;if(s!==p)return s-p}return t.length-n.length}function Ha(e){return JSON.parse(e)}function Wa(e,r){return pe(e)===pe(r)}function Xe(e,r){if(r==="effective")return e;let t=e.state?.fields??{},n=Object.entries(t).filter(([o])=>!o.startsWith("$"));return n.length===Object.keys(t).length?e:{...e,state:{...e.state,fields:Object.fromEntries(n)}}}async function Qe(e){let r=new TextEncoder().encode(e),t=await crypto.subtle.digest("SHA-256",r);return Array.from(new Uint8Array(t)).map(o=>o.toString(16).padStart(2,"0")).join("")}var Bt=new Uint32Array([1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298]),Kt=new Uint32Array([1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225]);function M(e,r){return e>>>r|e<<32-r}function Gt(e){let r=e.length*8,t=e.length+1,n=t%64<=56?56-t%64:120-t%64,o=t+n+8,a=new Uint8Array(o);a.set(e),a[e.length]=128;let s=Math.floor(r/4294967296),p=r>>>0;a[o-8]=s>>>24&255,a[o-7]=s>>>16&255,a[o-6]=s>>>8&255,a[o-5]=s&255,a[o-4]=p>>>24&255,a[o-3]=p>>>16&255,a[o-2]=p>>>8&255,a[o-1]=p&255;let u=new Uint32Array(Kt),d=new DataView(a.buffer);for(let m=0;m<a.length;m+=64){let E=new Uint32Array(64);for(let R=0;R<16;R++)E[R]=d.getUint32(m+R*4);for(let R=16;R<64;R++){let Ce=M(E[R-15],7)^M(E[R-15],18)^E[R-15]>>>3,Pe=M(E[R-2],17)^M(E[R-2],19)^E[R-2]>>>10;E[R]=E[R-16]+Ce+E[R-7]+Pe>>>0}let C=u[0],I=u[1],F=u[2],A=u[3],q=u[4],H=u[5],J=u[6],ze=u[7];for(let R=0;R<64;R++){let Ce=M(q,6)^M(q,11)^M(q,25),Pe=q&H^~q&J,Je=ze+Ce+Pe+Bt[R]+E[R]>>>0,Ut=M(C,2)^M(C,13)^M(C,22),Zt=C&I^C&F^I&F,Lt=Ut+Zt>>>0;ze=J,J=H,H=q,q=A+Je>>>0,A=F,F=I,I=C,C=Je+Lt>>>0}u[0]=u[0]+C>>>0,u[1]=u[1]+I>>>0,u[2]=u[2]+F>>>0,u[3]=u[3]+A>>>0,u[4]=u[4]+q>>>0,u[5]=u[5]+H>>>0,u[6]=u[6]+J>>>0,u[7]=u[7]+ze>>>0}let h=new Uint8Array(32),b=new DataView(h.buffer);for(let m=0;m<u.length;m++)b.setUint32(m*4,u[m]);return h}function et(e){let r=new TextEncoder().encode(e),t=Gt(r),n="";for(let o of t)n+=o.toString(16).padStart(2,"0");return n}async function Yt(e,r="semantic"){let t=pe(Xe(e,r));return Qe(t)}function De(e,r="semantic"){let t=pe(Xe(e,r));return et(t)}async function Ga(e){return Yt(e,"effective")}function Ya(e){return De(e,"effective")}async function Ja(e,r,t,n){let o=`${e}:${r}:${t}:${n}`;return`req-${(await Qe(o)).slice(0,16)}`}function tt(e,r,t,n){let o=`${e}:${r}:${t}:${n}`;return`req-${et(o).slice(0,16)}`}function rt(e=0){return`trace-${e}`}var Jt=w.enum(["expr","computed","flow","patch","effect","branch","call","halt","error"]),le=w.lazy(()=>w.object({id:w.string(),kind:Jt,sourcePath:w.string(),inputs:w.record(w.string(),w.unknown()),output:w.unknown(),children:w.array(le),timestamp:w.number()})),Xt=w.enum(["complete","effect","halt","error"]),nt=w.object({root:le,nodes:w.record(w.string(),le),intent:w.object({type:w.string(),input:w.unknown()}),baseVersion:w.number(),resultVersion:w.number(),duration:w.number(),terminatedBy:Xt});function ee(e){let r=0;return{timestamp:e,nextId:()=>rt(r++)}}function g(e,r,t,n,o,a=[]){return{id:e.nextId(),kind:r,sourcePath:t,inputs:n,output:o,children:a,timestamp:e.timestamp}}function U(e,r,t,n,o,a){return{snapshot:e,schema:r,currentAction:t,nodePath:n,intentId:o,uuidCounter:0,trace:typeof a=="object"?a:ee(a)}}function X(e,r,t,n){return{...e,$item:r,$index:t,$array:n}}function Oe(e,r){return{...e,snapshot:r}}function Ee(e,r){return{...e,nodePath:r}}function D(e){if(!e)return[];let r=[],t="",n=!1;for(let o of e){if(n){o==="."||o==="\\"?t+=o:t+=`\\${o}`,n=!1;continue}if(o==="\\"){n=!0;continue}if(o==="."){r.push(t),t="";continue}t+=o}return n&&(t+="\\"),r.push(t),r}function ot(e){return e.replaceAll("\\","\\\\").replaceAll(".","\\.")}function oi(...e){return e.filter(Boolean).map(ot).join(".")}function V(e,r){if(!r)return e;let t=D(r),n=e;for(let o of t){if(n==null)return;if(typeof n=="object")n=n[o];else return}return n}function Qt(e,r,t){if(!r)return t;let n=D(r);return at(e,n,t)}function at(e,r,t){if(r.length===0)return t;let[n,...o]=r,a=e!==null&&typeof e=="object"?e:{};if(o.length===0)return{...a,[n]:t};let s=a[n];return{...a,[n]:at(s,o,t)}}function ai(e,r){if(!r)return;let t=D(r);return it(e,t)}function it(e,r){if(r.length===0||e===null||typeof e!="object")return e;let[t,...n]=r,o=e;if(n.length===0){let{[t]:s,...p}=o;return p}let a=o[t];return{...o,[t]:it(a,n)}}function ii(e,r,t){let n=V(e,r),o=n!==null&&typeof n=="object"?{...n,...t}:t;return Qt(e,r,o)}function si(e,r){if(!r)return e!==void 0;let t=D(r),n=e;for(let o of t){if(n==null||typeof n!="object"||!(o in n))return!1;n=n[o]}return!0}function pi(e){let r=D(e);return er(...r.slice(0,-1))}function li(e){let r=D(e);return r[r.length-1]??""}function er(...e){return e.map(ot).join(".")}var tr=new Set(["__proto__","constructor","prototype"]);function ge(e){let r="";for(let t=0;t<e.length;t++){let n=e[t];if(n.kind==="prop"){r+=t===0?n.name:`.${n.name}`;continue}r+=`[${n.index}]`}return r}function st(e){let r=D(e);if(r.length===0)return[{kind:"prop",name:e}];let t=[];for(let n of r){let o=nr(n);if(o){t.push(...o);continue}t.push({kind:"prop",name:n})}return t.length>0?t:[{kind:"prop",name:e}]}function pt(e){return e.every(r=>r.kind!=="prop"||!tr.has(r.name))}function rr(e,r){let t=e;for(let n of r){if(n.kind==="prop"){if(t===null||typeof t!="object"||Array.isArray(t))return;t=t[n.name];continue}if(!Array.isArray(t))return;t=t[n.index]}return t}function ue(e,r,t){return $e(e,r,t)}function ke(e,r){return Ve(e,r)}function Se(e,r,t){let n=rr(e,r),o=qe(n)?{...n,...t}:t;return ue(e,r,o)}function $e(e,r,t){if(r.length===0)return t;let[n,...o]=r;if(n.kind==="prop"){let s=qe(e)?e:{},p=$e(s[n.name],o,t);return{...s,[n.name]:p}}let a=Array.isArray(e)?[...e]:[];return a[n.index]=$e(a[n.index],o,t),a}function Ve(e,r){if(r.length===0)return e;let[t,...n]=r;if(t.kind==="prop"){if(!qe(e))return e;if(n.length===0){let{[t.name]:a,...s}=e;return s}return{...e,[t.name]:Ve(e[t.name],n)}}if(!Array.isArray(e))return e;let o=[...e];return n.length===0?(t.index>=0&&t.index<o.length&&delete o[t.index],o):(o[t.index]=Ve(o[t.index],n),o)}function qe(e){return e!==null&&typeof e=="object"&&!Array.isArray(e)}function nr(e){if(!e.includes("[")||e.includes("\\[")||e.includes("\\]"))return null;let r=[],t=0,n=!1;for(;t<e.length;){let o=e.indexOf("[",t);if(o===-1){let u=e.slice(t);if(u.length===0)break;if(n)return null;r.push({kind:"prop",name:u});break}let a=e.slice(t,o);if(a.length>0){if(n)return null;r.push({kind:"prop",name:a})}let s=e.indexOf("]",o+1);if(s===-1)return null;let p=e.slice(o+1,s);if(!/^[0-9]+$/.test(p))return null;r.push({kind:"index",index:Number(p)}),n=!0,t=s+1}return r.length>0?r:null}import{z as oe}from"zod";var W=oe.string().min(1),mi=(e,r)=>oe.discriminatedUnion("ok",[oe.object({ok:oe.literal(!0),value:e}),oe.object({ok:oe.literal(!1),error:r})]),l=e=>({ok:!0,value:e}),te=e=>({ok:!1,error:e}),ae=e=>e.ok,Z=e=>!e.ok;function f(e,r){switch(e.kind){case"lit":return l(e.value);case"get":return ar(e.path,r);case"eq":return B(e.left,e.right,r,(t,n)=>t===n);case"neq":return B(e.left,e.right,r,(t,n)=>t!==n);case"gt":return B(e.left,e.right,r,(t,n)=>y(t)>y(n));case"gte":return B(e.left,e.right,r,(t,n)=>y(t)>=y(n));case"lt":return B(e.left,e.right,r,(t,n)=>y(t)<y(n));case"lte":return B(e.left,e.right,r,(t,n)=>y(t)<=y(n));case"and":return ir(e.args,r);case"or":return sr(e.args,r);case"not":return pr(e.arg,r);case"if":return lr(e,r);case"add":return B(e.left,e.right,r,(t,n)=>y(t)+y(n));case"sub":return B(e.left,e.right,r,(t,n)=>y(t)-y(n));case"mul":return B(e.left,e.right,r,(t,n)=>y(t)*y(n));case"div":return ur(e.left,e.right,r);case"mod":return cr(e.left,e.right,r);case"min":return fr(e.args,r);case"max":return dr(e.args,r);case"abs":return mr(e.arg,r);case"neg":return yr(e.arg,r);case"floor":return Mr(e.arg,r);case"ceil":return Ur(e.arg,r);case"round":return Zr(e.arg,r);case"sqrt":return Lr(e.arg,r);case"pow":return _r(e.base,e.exponent,r);case"sumArray":return Hr(e.array,r);case"minArray":return Wr(e.array,r);case"maxArray":return Br(e.array,r);case"concat":return xr(e.args,r);case"substring":return hr(e,r);case"trim":return Er(e.str,r);case"toLowerCase":return Kr(e.str,r);case"toUpperCase":return Gr(e.str,r);case"strLen":return Yr(e.str,r);case"startsWith":return Jr(e.str,e.prefix,r);case"endsWith":return Xr(e.str,e.suffix,r);case"strIncludes":return Qr(e.str,e.search,r);case"indexOf":return en(e.str,e.search,r);case"replace":return tn(e.str,e.search,e.replacement,r);case"split":return rn(e.str,e.delimiter,r);case"len":return gr(e.arg,r);case"at":return kr(e.array,e.index,r);case"first":return Sr(e.array,r);case"last":return br(e.array,r);case"slice":return vr(e,r);case"includes":return Nr(e.array,e.item,r);case"filter":return Rr(e.array,e.predicate,r);case"map":return wr(e.array,e.mapper,r);case"find":return Tr(e.array,e.predicate,r);case"every":return jr(e.array,e.predicate,r);case"some":return Ar(e.array,e.predicate,r);case"append":return zr(e.array,e.items,r);case"reverse":return nn(e.array,r);case"unique":return on(e.array,r);case"flat":return an(e.array,r);case"object":return Cr(e.fields,r);case"field":return Pr(e.object,e.property,r);case"keys":return Fr(e.obj,r);case"values":return Ir(e.obj,r);case"entries":return Dr(e.obj,r);case"merge":return Or(e.objects,r);case"hasKey":return sn(e.obj,e.key,r);case"pick":return pn(e.obj,e.keys,r);case"omit":return ln(e.obj,e.keys,r);case"fromEntries":return un(e.entries,r);case"typeof":return $r(e.arg,r);case"isNull":return Vr(e.arg,r);case"coalesce":return qr(e.args,r);case"toString":return cn(e.arg,r);case"toNumber":return fn(e.arg,r);case"toBoolean":return dn(e.arg,r);default:return te(v("INTERNAL_ERROR",`Unknown expression kind: ${e.kind}`,r.currentAction??"",r.nodePath,r.trace.timestamp))}}function y(e){return typeof e=="number"?e:typeof e=="string"?parseFloat(e)||0:typeof e=="boolean"&&e?1:0}function K(e){return e==null?!1:typeof e=="boolean"?e:typeof e=="number"?e!==0:typeof e=="string"?e.length>0:!0}function T(e){return e==null?"":typeof e=="string"?e:String(e)}function or(e,r){let t=`${e}-${r}`,n=0;for(let d=0;d<t.length;d++){let h=t.charCodeAt(d);n=(n<<5)-n+h,n=n&n}let o=Math.abs(n).toString(16).padStart(8,"0"),a=Math.abs(n*31).toString(16).padStart(4,"0"),s=Math.abs(n*37).toString(16).padStart(4,"0"),p=Math.abs(n*41).toString(16).padStart(4,"0"),u=Math.abs(n*43).toString(16).padStart(12,"0");return`${o.slice(0,8)}-${a.slice(0,4)}-4${s.slice(1,4)}-${p.slice(0,4)}-${u.slice(0,12)}`}function ar(e,r){if(e.startsWith("$item")){if(r.$item===void 0)return l(void 0);if(e==="$item")return l(r.$item);let t=e.slice(6);return l(V(r.$item,t))}if(e==="$index")return l(r.$index);if(e==="$array")return l(r.$array);if(e.startsWith("$system.")){let t=e.slice(8);if(t==="uuid"){let n=r.intentId??"no-intent",o=r.uuidCounter??0;return r.uuidCounter!==void 0&&(r.uuidCounter=o+1),l(or(n,o))}return t==="timestamp"?l(new Date(r.snapshot.meta.timestamp).toISOString()):l(void 0)}if(e.startsWith("meta.")){let t=e.slice(5);return t==="intentId"?l(r.intentId):t==="actionName"?l(r.currentAction):l(V(r.snapshot.meta,t))}if(e.startsWith("input.")||e==="input"){let t=e==="input"?"":e.slice(6);return l(t?V(r.snapshot.input,t):r.snapshot.input)}if(Object.prototype.hasOwnProperty.call(r.schema.computed.fields,e))return l(r.snapshot.computed[e]);if(e.startsWith("system.")){let t=e.slice(7);return l(V(r.snapshot.system,t))}return l(V(r.snapshot.data,e))}function B(e,r,t,n){let o=f(e,t);if(!o.ok)return o;let a=f(r,t);return a.ok?l(n(o.value,a.value)):a}function ir(e,r){for(let t of e){let n=f(t,r);if(!n.ok)return n;if(!K(n.value))return l(!1)}return l(!0)}function sr(e,r){for(let t of e){let n=f(t,r);if(!n.ok)return n;if(K(n.value))return l(!0)}return l(!1)}function pr(e,r){let t=f(e,r);return t.ok?l(!K(t.value)):t}function lr(e,r){let t=f(e.cond,r);return t.ok?f(K(t.value)?e.then:e.else,r):t}function ur(e,r,t){let n=f(e,t);if(!n.ok)return n;let o=f(r,t);if(!o.ok)return o;let a=y(o.value);return a===0?l(null):l(y(n.value)/a)}function cr(e,r,t){let n=f(e,t);if(!n.ok)return n;let o=f(r,t);if(!o.ok)return o;let a=y(o.value);return a===0?l(null):l(y(n.value)%a)}function fr(e,r){if(e.length===0)return l(null);let t=[];for(let n of e){let o=f(n,r);if(!o.ok)return o;t.push(y(o.value))}return l(Math.min(...t))}function dr(e,r){if(e.length===0)return l(null);let t=[];for(let n of e){let o=f(n,r);if(!o.ok)return o;t.push(y(o.value))}return l(Math.max(...t))}function mr(e,r){let t=f(e,r);return t.ok?l(Math.abs(y(t.value))):t}function yr(e,r){let t=f(e,r);return t.ok?l(-y(t.value)):t}function xr(e,r){let t=[];for(let a of e){let s=f(a,r);if(!s.ok)return s;t.push(s.value)}if(t.some(a=>Array.isArray(a))){let a=[];for(let s of t)Array.isArray(s)?a.push(...s):s!=null&&a.push(s);return l(a)}let o=t.map(a=>T(a));return l(o.join(""))}function hr(e,r){let t=f(e.str,r);if(!t.ok)return t;let n=f(e.start,r);if(!n.ok)return n;let o=T(t.value),a=y(n.value);if(e.end){let s=f(e.end,r);return s.ok?l(o.substring(a,y(s.value))):s}return l(o.substring(a))}function Er(e,r){let t=f(e,r);return t.ok?l(T(t.value).trim()):t}function gr(e,r){let t=f(e,r);if(!t.ok)return t;let n=t.value;return Array.isArray(n)?l(n.length):typeof n=="string"?l(n.length):typeof n=="object"&&n!==null?l(Object.keys(n).length):l(0)}function kr(e,r,t){let n=f(e,t);if(!n.ok)return n;let o=f(r,t);if(!o.ok)return o;let a=n.value,s=o.value;if(Array.isArray(a)){let p=y(s);return p<0||p>=a.length?l(null):l(a[p])}return typeof a=="object"&&a!==null&&typeof s=="string"?l(a[s]??null):l(null)}function Sr(e,r){let t=f(e,r);if(!t.ok)return t;let n=t.value;return!Array.isArray(n)||n.length===0?l(null):l(n[0])}function br(e,r){let t=f(e,r);if(!t.ok)return t;let n=t.value;return!Array.isArray(n)||n.length===0?l(null):l(n[n.length-1])}function vr(e,r){let t=f(e.array,r);if(!t.ok)return t;let n=f(e.start,r);if(!n.ok)return n;let o=t.value;if(!Array.isArray(o))return l([]);let a=y(n.value);if(e.end){let s=f(e.end,r);return s.ok?l(o.slice(a,y(s.value))):s}return l(o.slice(a))}function Nr(e,r,t){let n=f(e,t);if(!n.ok)return n;let o=f(r,t);if(!o.ok)return o;let a=n.value;return Array.isArray(a)?l(a.includes(o.value)):l(!1)}function Rr(e,r,t){let n=f(e,t);if(!n.ok)return n;let o=n.value;if(!Array.isArray(o))return l([]);let a=[];for(let s=0;s<o.length;s++){let p=X(t,o[s],s,o),u=f(r,p);if(!u.ok)return u;K(u.value)&&a.push(o[s])}return l(a)}function wr(e,r,t){let n=f(e,t);if(!n.ok)return n;let o=n.value;if(!Array.isArray(o))return l([]);let a=[];for(let s=0;s<o.length;s++){let p=X(t,o[s],s,o),u=f(r,p);if(!u.ok)return u;a.push(u.value)}return l(a)}function Tr(e,r,t){let n=f(e,t);if(!n.ok)return n;let o=n.value;if(!Array.isArray(o))return l(null);for(let a=0;a<o.length;a++){let s=X(t,o[a],a,o),p=f(r,s);if(!p.ok)return p;if(K(p.value))return l(o[a])}return l(null)}function jr(e,r,t){let n=f(e,t);if(!n.ok)return n;let o=n.value;if(!Array.isArray(o))return l(!0);for(let a=0;a<o.length;a++){let s=X(t,o[a],a,o),p=f(r,s);if(!p.ok)return p;if(!K(p.value))return l(!1)}return l(!0)}function Ar(e,r,t){let n=f(e,t);if(!n.ok)return n;let o=n.value;if(!Array.isArray(o))return l(!1);for(let a=0;a<o.length;a++){let s=X(t,o[a],a,o),p=f(r,s);if(!p.ok)return p;if(K(p.value))return l(!0)}return l(!1)}function zr(e,r,t){let n=f(e,t);if(!n.ok)return n;let o=n.value,a=Array.isArray(o)?[...o]:[];for(let s of r){let p=f(s,t);if(!p.ok)return p;a.push(p.value)}return l(a)}function Cr(e,r){let t={};for(let n of Object.keys(e).sort(ne)){let o=e[n],a=f(o,r);if(!a.ok)return a;t[n]=a.value}return l(t)}function Pr(e,r,t){let n=f(e,t);if(!n.ok)return n;let o=n.value;return typeof o!="object"||o===null||Array.isArray(o)?l(null):l(o[r]??null)}function Fr(e,r){let t=f(e,r);if(!t.ok)return t;let n=t.value;return typeof n!="object"||n===null?l([]):l(Object.keys(n).sort(ne))}function Ir(e,r){let t=f(e,r);if(!t.ok)return t;let n=t.value;return typeof n!="object"||n===null?l([]):l(Object.keys(n).sort(ne).map(o=>n[o]))}function Dr(e,r){let t=f(e,r);if(!t.ok)return t;let n=t.value;return typeof n!="object"||n===null?l([]):l(Object.keys(n).sort(ne).map(o=>[o,n[o]]))}function Or(e,r){let t={};for(let n of e){let o=f(n,r);if(!o.ok)return o;let a=o.value;typeof a=="object"&&a!==null&&!Array.isArray(a)&&Object.assign(t,a)}return l(t)}function $r(e,r){let t=f(e,r);if(!t.ok)return t;let n=t.value;return n===null?l("null"):Array.isArray(n)?l("array"):l(typeof n)}function Vr(e,r){let t=f(e,r);return t.ok?l(t.value===null||t.value===void 0):t}function qr(e,r){for(let t of e){let n=f(t,r);if(!n.ok||n.value!==null&&n.value!==void 0)return n}return l(null)}function Mr(e,r){let t=f(e,r);return t.ok?l(Math.floor(y(t.value))):t}function Ur(e,r){let t=f(e,r);return t.ok?l(Math.ceil(y(t.value))):t}function Zr(e,r){let t=f(e,r);return t.ok?l(Math.round(y(t.value))):t}function Lr(e,r){let t=f(e,r);if(!t.ok)return t;let n=y(t.value);return n<0?l(null):l(Math.sqrt(n))}function _r(e,r,t){let n=f(e,t);if(!n.ok)return n;let o=f(r,t);if(!o.ok)return o;let a=Math.pow(y(n.value),y(o.value));return Number.isFinite(a)?l(a):l(null)}function Hr(e,r){let t=f(e,r);if(!t.ok)return t;let n=t.value;if(!Array.isArray(n))return l(0);let o=0;for(let a of n)o+=y(a);return l(o)}function Wr(e,r){let t=f(e,r);if(!t.ok)return t;let n=t.value;if(!Array.isArray(n)||n.length===0)return l(null);let o=y(n[0]);for(let a=1;a<n.length;a++){let s=y(n[a]);s<o&&(o=s)}return l(o)}function Br(e,r){let t=f(e,r);if(!t.ok)return t;let n=t.value;if(!Array.isArray(n)||n.length===0)return l(null);let o=y(n[0]);for(let a=1;a<n.length;a++){let s=y(n[a]);s>o&&(o=s)}return l(o)}function Kr(e,r){let t=f(e,r);return t.ok?l(T(t.value).toLowerCase()):t}function Gr(e,r){let t=f(e,r);return t.ok?l(T(t.value).toUpperCase()):t}function Yr(e,r){let t=f(e,r);return t.ok?l(T(t.value).length):t}function Jr(e,r,t){let n=f(e,t);if(!n.ok)return n;let o=f(r,t);return o.ok?l(T(n.value).startsWith(T(o.value))):o}function Xr(e,r,t){let n=f(e,t);if(!n.ok)return n;let o=f(r,t);return o.ok?l(T(n.value).endsWith(T(o.value))):o}function Qr(e,r,t){let n=f(e,t);if(!n.ok)return n;let o=f(r,t);return o.ok?l(T(n.value).includes(T(o.value))):o}function en(e,r,t){let n=f(e,t);if(!n.ok)return n;let o=f(r,t);return o.ok?l(T(n.value).indexOf(T(o.value))):o}function tn(e,r,t,n){let o=f(e,n);if(!o.ok)return o;let a=f(r,n);if(!a.ok)return a;let s=f(t,n);return s.ok?l(T(o.value).replace(T(a.value),T(s.value))):s}function rn(e,r,t){let n=f(e,t);if(!n.ok)return n;let o=f(r,t);if(!o.ok)return o;let a=T(n.value).split(T(o.value));return l(a.length===0?[""]:a)}function nn(e,r){let t=f(e,r);if(!t.ok)return t;let n=t.value;return Array.isArray(n)?l([...n].reverse()):l([])}function on(e,r){let t=f(e,r);if(!t.ok)return t;let n=t.value;if(!Array.isArray(n))return l([]);let o=[],a=[];for(let s of n)o.some(p=>p===s)||(o.push(s),a.push(s));return l(a)}function an(e,r){let t=f(e,r);if(!t.ok)return t;let n=t.value;if(!Array.isArray(n))return l([]);let o=[];for(let a of n)Array.isArray(a)?o.push(...a):o.push(a);return l(o)}function sn(e,r,t){let n=f(e,t);if(!n.ok)return n;let o=f(r,t);if(!o.ok)return o;let a=n.value;if(typeof a!="object"||a===null||Array.isArray(a))return l(!1);let s=o.value;return typeof s!="string"?l(!1):l(Object.prototype.hasOwnProperty.call(a,s))}function pn(e,r,t){let n=f(e,t);if(!n.ok)return n;let o=f(r,t);if(!o.ok)return o;let a=n.value;if(typeof a!="object"||a===null||Array.isArray(a))return l({});let s=o.value;if(!Array.isArray(s))return l({});let p={};for(let u of s)typeof u=="string"&&Object.prototype.hasOwnProperty.call(a,u)&&(p[u]=a[u]);return l(p)}function ln(e,r,t){let n=f(e,t);if(!n.ok)return n;let o=f(r,t);if(!o.ok)return o;let a=n.value;if(typeof a!="object"||a===null||Array.isArray(a))return l({});let s=o.value,p=new Set;if(Array.isArray(s))for(let d of s)typeof d=="string"&&p.add(d);let u={};for(let[d,h]of Object.entries(a))p.has(d)||(u[d]=h);return l(u)}function un(e,r){let t=f(e,r);if(!t.ok)return t;let n=t.value;if(!Array.isArray(n))return l({});let o={};for(let a of n)Array.isArray(a)&&a.length===2&&typeof a[0]=="string"&&(o[a[0]]=a[1]);return l(o)}function cn(e,r){let t=f(e,r);return t.ok?l(T(t.value)):t}function fn(e,r){let t=f(e,r);return t.ok?l(y(t.value)):t}function dn(e,r){let t=f(e,r);return t.ok?l(K(t.value)):t}function mn(e){return/^\d+$/.test(e)}function yn(e,r){let t=L(e,r);return t?t.kind==="primitive"&&t.type==="null"||t.kind==="literal"&&t.value===null:!1}function lt(e,r){let t=L(e,r);if(!t)return null;if(t.kind!=="union")return t;let n=t.types.filter(a=>!yn(a,r));return n.length!==t.types.length?n.length!==1?null:L(n[0],r):t}function L(e,r,t=[]){if(e.kind!=="ref")return e;if(t.includes(e.name))return null;let n=r[e.name];return n?L(n.definition,r,[...t,e.name]):null}function ut(e,r,t){return t?ft(e,r,D(t))!==null:!0}function ct(e,r,t){return ft(e,r,t)!==null}function ft(e,r,t){let n=e;for(let o of t){if(!n)return null;let a=lt(n,r);if(!a)return null;switch(a.kind){case"object":n=a.fields[o]?.type??null;break;case"record":n=a.value;break;case"array":if(!mn(o))return null;n=a.element;break;case"primitive":return null;default:return null}}return n}function be(e,r,t){let n=e;for(let o of t){if(!n)return null;let a=lt(n,r);if(!a)return null;switch(a.kind){case"object":if(o.kind!=="prop")return null;n=a.fields[o.name]?.type??null;break;case"record":if(o.kind!=="prop")return null;n=a.value;break;case"array":if(o.kind!=="index")return null;n=a.element;break;case"primitive":return null;default:return null}}return n}function ve(e,r,t){if(!e.fieldTypes||t.length===0)return null;let[n,...o]=t;if(!n||n.kind!=="prop"||!(n.name in e.fields))return null;let a=e.fieldTypes[n.name];return a?o.length===0?a:be(a,r,o):null}function $(e,r,t,n){if(e===void 0)return n?.allowUndefined?{ok:!0}:{ok:!1,message:"Value is required"};let o=L(r,t);if(!o)return{ok:!1,message:"Unknown type reference"};if(o.kind==="union")return o.types.map(s=>$(e,s,t,n)).find(s=>s.ok)??{ok:!1,message:"Value does not match any union branch"};switch(o.kind){case"primitive":switch(o.type){case"null":return e===null?{ok:!0}:{ok:!1,message:"Expected null"};case"string":return typeof e=="string"?{ok:!0}:{ok:!1,message:"Expected string"};case"number":return typeof e=="number"&&Number.isFinite(e)?{ok:!0}:{ok:!1,message:"Expected number"};case"boolean":return typeof e=="boolean"?{ok:!0}:{ok:!1,message:"Expected boolean"};case"object":return e!==null&&!Array.isArray(e)&&typeof e=="object"?{ok:!0}:{ok:!1,message:"Expected object"};case"array":return Array.isArray(e)?{ok:!0}:{ok:!1,message:"Expected array"};default:return{ok:!1,message:`Unsupported primitive type: ${o.type}`}}case"literal":return Object.is(e,o.value)?{ok:!0}:{ok:!1,message:`Expected literal ${JSON.stringify(o.value)}`};case"array":if(!Array.isArray(e))return{ok:!1,message:"Expected array"};for(let s of e){let p=$(s,o.element,t,{allowUndefined:!1});if(!p.ok)return p}return{ok:!0};case"object":return e===null||Array.isArray(e)||typeof e!="object"?{ok:!1,message:"Expected object"}:xn(e,o.fields,t,n?.allowPartial??!1,n?.allowUndefined??!1);case"record":if(e===null||Array.isArray(e)||typeof e!="object")return{ok:!1,message:"Expected object"};let a=L(o.key,t);if(!a||a.kind!=="primitive"||a.type!=="string")return{ok:!1,message:"Record keys must be strings"};for(let s of Object.values(e)){let p=$(s,o.value,t,{allowUndefined:!1});if(!p.ok)return p}return{ok:!0};case"ref":return{ok:!1,message:"Unresolved type reference"}}}function xn(e,r,t,n,o){let a=new Set(Object.keys(r));for(let s of Object.keys(e))if(!a.has(s))return{ok:!1,message:`Unknown field: ${s}`};for(let[s,p]of Object.entries(r)){if(!(s in e)){if(!n&&!p.optional)return{ok:!1,message:`Missing required field: ${s}`};continue}let u=$(e[s],p.type,t,{allowPartial:n,allowUndefined:o});if(!u.ok)return u}return{ok:!0}}var hn=/^\d+\.\d+\.\d+(?:-[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/,En=/^[a-zA-Z][a-zA-Z0-9+.-]*:/,gn=/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;function dt(e){return En.test(e)||gn.test(e)}function mt(e){return hn.test(e)}function G(e){let r=[],t=n=>{switch(n.kind){case"lit":return;case"get":r.push(n.path);return;case"eq":case"neq":case"gt":case"gte":case"lt":case"lte":case"add":case"sub":case"mul":case"div":case"mod":t(n.left),t(n.right);return;case"pow":t(n.base),t(n.exponent);return;case"and":case"or":case"concat":case"coalesce":case"min":case"max":n.args.forEach(t);return;case"not":case"typeof":case"isNull":case"len":case"abs":case"neg":case"floor":case"ceil":case"round":case"sqrt":case"toString":case"toNumber":case"toBoolean":t(n.arg);return;case"first":case"last":case"reverse":case"unique":case"flat":case"sumArray":case"minArray":case"maxArray":t(n.array);return;case"if":t(n.cond),t(n.then),t(n.else);return;case"substring":t(n.str),t(n.start),n.end&&t(n.end);return;case"trim":case"toLowerCase":case"toUpperCase":case"strLen":t(n.str);return;case"startsWith":t(n.str),t(n.prefix);return;case"endsWith":t(n.str),t(n.suffix);return;case"strIncludes":case"indexOf":t(n.str),t(n.search);return;case"replace":t(n.str),t(n.search),t(n.replacement);return;case"split":t(n.str),t(n.delimiter);return;case"at":t(n.array),t(n.index);return;case"slice":t(n.array),t(n.start),n.end&&t(n.end);return;case"includes":t(n.array),t(n.item);return;case"filter":case"find":case"every":case"some":t(n.array),t(n.predicate);return;case"map":t(n.array),t(n.mapper);return;case"append":t(n.array),n.items.forEach(t);return;case"object":Object.values(n.fields).forEach(t);return;case"field":t(n.object);return;case"keys":case"values":case"entries":t(n.obj);return;case"merge":n.objects.forEach(t);return;case"hasKey":t(n.obj),t(n.key);return;case"pick":case"omit":t(n.obj),t(n.keys);return;case"fromEntries":t(n.entries);return}};return t(e),r}function yt(e){let r=[],t=n=>{switch(n.kind){case"seq":n.steps.forEach(t);return;case"if":r.push(...G(n.cond)),t(n.then),n.else&&t(n.else);return;case"patch":n.op!=="unset"&&n.value&&r.push(...G(n.value));return;case"effect":Object.values(n.params).forEach(o=>{r.push(...G(o))});return;case"fail":n.message&&r.push(...G(n.message));return;case"call":case"halt":return}};return t(e),r}function ce(e,r,t={}){if(e.fieldTypes){if(!r)return!0;let n=D(r),[o,...a]=n;if(!o)return!0;if(!(o in e.fields))return!1;let s=e.fieldTypes[o];if(s&&(a.length===0?!0:ct(s,t,a)))return!0}return Me({type:"object",required:!0,fields:e.fields},r)}function fe(e,r){return Object.prototype.hasOwnProperty.call(e.fields,r)}function Me(e,r){if(!r)return!0;let t=D(r);if(t.length===0)return!0;let n=e;for(let o of t){if(!n)return!1;let a=n.type;if(a==="object"){if(!n.fields)return!0;if(!(o in n.fields))return!1;n=n.fields[o];continue}if(a==="array"){if(!Sn(o))return!1;n=n.items??null;continue}return!1}return!0}function Y(e,r,t){if(e===void 0)return t?.allowUndefined&&r.required===!1?{ok:!0}:{ok:!1,message:"Value is required"};if(e===null)return{ok:!0};let n=r.type;if(typeof n=="object"&&"enum"in n)return n.enum.some(o=>Object.is(o,e))?{ok:!0}:{ok:!1,message:"Value is not in enum"};switch(n){case"null":return e===null?{ok:!0}:{ok:!1,message:"Expected null"};case"string":return typeof e=="string"?{ok:!0}:{ok:!1,message:"Expected string"};case"number":return typeof e=="number"&&Number.isFinite(e)?{ok:!0}:{ok:!1,message:"Expected number"};case"boolean":return typeof e=="boolean"?{ok:!0}:{ok:!1,message:"Expected boolean"};case"object":return e===null||Array.isArray(e)||typeof e!="object"?{ok:!1,message:"Expected object"}:kn(e,r,t?.allowPartial??!1,t?.allowUndefined??!1);case"array":if(!Array.isArray(e))return{ok:!1,message:"Expected array"};if(!r.items)return{ok:!0};for(let o of e){let a=Y(o,r.items,{allowUndefined:!1});if(!a.ok)return a}return{ok:!0}}}function kn(e,r,t,n){if(!r.fields)return{ok:!0};let o=Object.entries(r.fields),a=new Set(o.map(([s])=>s));for(let s of Object.keys(e)){if(!a.has(s))return{ok:!1,message:`Unknown field: ${s}`};let p=r.fields[s],u=Y(e[s],p,{allowPartial:t,allowUndefined:n});if(!u.ok)return u}if(!t){for(let[s,p]of o)if(p.required&&!(s in e))return{ok:!1,message:`Missing required field: ${s}`}}return{ok:!0}}function Ne(e,r){if(r.length===0)return e;let t=e;for(let n of r){if(!t)return null;let o=t.type;if(o==="object"){if(n.kind!=="prop"||!t.fields||!(n.name in t.fields))return null;t=t.fields[n.name];continue}if(o==="array"){if(n.kind!=="index")return null;t=t.items??null;continue}return null}return t}function Sn(e){return/^[0-9]+$/.test(e)}function xt(e){return{snapshot:e,status:"running",patches:[],requirements:[],error:null}}function ht(e,r){let t=e.snapshot.data;switch(r.op){case"set":t=ue(t,r.path,r.value);break;case"unset":t=ke(t,r.path);break;case"merge":t=Se(t,r.path,r.value);break}return{...e,snapshot:{...e.snapshot,data:t},patches:[...e.patches,r]}}function bn(e,r){return{...e,status:"pending",requirements:[...e.requirements,r]}}function P(e,r){return{...e,status:"error",error:r}}function ie(e,r,t,n){if(t.status!=="running")return{state:t,trace:g(r.trace,"flow",n,{},null,[])};switch(e.kind){case"seq":return vn(e.steps,r,t,n);case"if":return Nn(e,r,t,n);case"patch":return Rn(e,r,t,n);case"effect":return wn(e,r,t,n);case"call":return An(e.flow,r,t,n);case"halt":return zn(e.reason,r,t,n);case"fail":return Cn(e,r,t,n);default:return{state:P(t,v("INTERNAL_ERROR",`Unknown flow kind: ${e.kind}`,r.currentAction??"",n,r.trace.timestamp)),trace:g(r.trace,"error",n,{},null,[])}}}async function Oi(e,r,t,n){return ie(e,r,t,n)}function vn(e,r,t,n){let o=t,a=[];for(let s=0;s<e.length;s++){let p=`${n}.steps[${s}]`,u=Ee(Oe(r,o.snapshot),p),d=ie(e[s],u,o,p);if(a.push(d.trace),o=d.state,o.status!=="running")break}return{state:o,trace:g(r.trace,"flow",n,{kind:"seq"},null,a)}}function Nn(e,r,t,n){let o=f(e.cond,r);if(!o.ok)return{state:P(t,o.error),trace:g(r.trace,"branch",n,{cond:!1},null,[])};let a=o.value,s=a!=null&&a!==!1,p=s?`${n}.then`:`${n}.else`,u=s?e.then:e.else;if(!u)return{state:t,trace:g(r.trace,"branch",n,{cond:s},null,[])};let d=Ee(r,p),h=ie(u,d,t,p);return{state:h.state,trace:g(r.trace,"branch",n,{cond:s},null,[h.trace])}}function Rn(e,r,t,n){let o;if(e.op!=="unset"&&e.value){let b=f(e.value,r);if(!b.ok)return{state:P(t,b.error),trace:g(r.trace,"error",n,{},null,[])};o=b.value}let a={type:"object",required:!0,fields:r.schema.state.fields},s=ve(r.schema.state,r.schema.types,e.path),p=s?null:Ne(a,e.path),u=ge(e.path);if(!s&&!p)return{state:P(t,v("PATH_NOT_FOUND",`Unknown patch path: ${u}`,r.currentAction??"",n,r.trace.timestamp)),trace:g(r.trace,"error",n,{},null,[])};if(e.op!=="unset"){let b=s?$(o,s,r.schema.types,{allowPartial:e.op==="merge",allowUndefined:!1}):Y(o,p,{allowPartial:e.op==="merge",allowUndefined:!1});if(!b.ok)return{state:P(t,v("TYPE_MISMATCH",`Invalid patch value at ${u}: ${b.message??"type mismatch"}`,r.currentAction??"",n,r.trace.timestamp)),trace:g(r.trace,"error",n,{},null,[])}}let d=e.op==="unset"?{op:"unset",path:e.path}:e.op==="merge"?{op:"merge",path:e.path,value:o}:{op:"set",path:e.path,value:o};return{state:ht(t,d),trace:g(r.trace,"patch",n,{op:e.op,path:u},o,[])}}function wn(e,r,t,n){if(e.type==="array.map"||e.type==="array.filter")return Tn(e,r,t,n);let o={};for(let[u,d]of Object.entries(e.params)){let h=f(d,r);if(!h.ok)return{state:P(t,h.error),trace:g(r.trace,"error",n,{},null,[])};o[u]=h.value}let s={id:tt(r.snapshot.meta.schemaHash,r.intentId??"",r.currentAction??"",n),type:e.type,params:o,actionId:r.currentAction??"",flowPosition:{nodePath:n,snapshotVersion:r.snapshot.meta.version},createdAt:r.trace.timestamp};return{state:bn(t,s),trace:g(r.trace,"effect",n,{type:e.type},o,[])}}function Tn(e,r,t,n){let{params:o}=e,a=Oe(r,t.snapshot),s=o.source;if(!s)return{state:P(t,v("INVALID_INPUT",`${e.type} requires 'source' parameter`,r.currentAction??"",n,r.trace.timestamp)),trace:g(a.trace,"error",n,{},null,[])};let p=f(s,a);if(!p.ok)return{state:P(t,p.error),trace:g(a.trace,"error",n,{},null,[])};let u=p.value;if(!Array.isArray(u))return{state:P(t,v("TYPE_MISMATCH",`${e.type} source must be an array`,a.currentAction??"",n,a.trace.timestamp)),trace:g(a.trace,"error",n,{},null,[])};let d=o.into;if(!d)return{state:P(t,v("INVALID_INPUT",`${e.type} requires 'into' parameter`,a.currentAction??"",n,a.trace.timestamp)),trace:g(a.trace,"error",n,{},null,[])};let h=f(d,a);if(!h.ok)return{state:P(t,h.error),trace:g(a.trace,"error",n,{},null,[])};let b=jn(h.value);if(!b)return{state:P(t,v("INVALID_INPUT",`${e.type} into must resolve to PatchPath segments or semantic string path`,a.currentAction??"",n,a.trace.timestamp)),trace:g(a.trace,"error",n,{},null,[])};let m=e.type==="array.map"?o.select:o.where;if(!m)return{state:P(t,v("INVALID_INPUT",`${e.type} requires '${e.type==="array.map"?"select":"where"}' parameter`,a.currentAction??"",n,a.trace.timestamp)),trace:g(a.trace,"error",n,{},null,[])};let E=[];for(let F=0;F<u.length;F++){let A=u[F],q=X(a,A,F,u),H=f(m,q);if(!H.ok)return{state:P(t,H.error),trace:g(a.trace,"error",n,{},null,[])};if(e.type==="array.map")E.push(H.value);else{let J=H.value;J!=null&&J!==!1&&E.push(A)}}return{state:ht(t,{op:"set",path:b,value:E}),trace:g(a.trace,"effect",n,{type:e.type,target:b},{count:E.length},[])}}function jn(e){if(typeof e=="string")return st(e);if(!Array.isArray(e))return null;let r=[];for(let t of e){if(!t||typeof t!="object")return null;let n=t.kind;if(n==="prop"){let o=t.name;if(typeof o!="string"||o.length===0)return null;r.push({kind:"prop",name:o});continue}if(n==="index"){let o=t.index;if(!Number.isInteger(o)||o<0)return null;r.push({kind:"index",index:o});continue}return null}return r.length>0?r:null}function An(e,r,t,n){let o=r.schema.actions[e];if(!o)return{state:P(t,v("UNKNOWN_FLOW",`Unknown flow: ${e}`,r.currentAction??"",n,r.trace.timestamp)),trace:g(r.trace,"error",n,{},null,[])};let a=`${n}.call(${e})`,s=Ee(r,a),p=ie(o.flow,s,t,a);return{state:p.state,trace:g(r.trace,"call",n,{flow:e},null,[p.trace])}}function zn(e,r,t,n){return{state:{...t,status:"halted"},trace:g(r.trace,"halt",n,{reason:e},null,[])}}function Cn(e,r,t,n){let o=e.code;if(e.message){let s=f(e.message,r);s.ok&&(o=String(s.value))}let a=v("VALIDATION_ERROR",o,r.currentAction??"",n,r.trace.timestamp,{code:e.code});return{state:P(t,a),trace:g(r.trace,"error",n,{code:e.code},o,[])}}function Re(e){let r=Object.keys(e.fields),t=new Map;for(let[n,o]of Object.entries(e.fields)){let a=o.deps.filter(s=>s in e.fields);t.set(n,a)}return{nodes:r,edges:t}}function we(e){let r=new Map,t=new Map;for(let a of e.nodes)r.set(a,0),t.set(a,[]);for(let[a,s]of e.edges)for(let p of s)t.has(p)&&t.get(p).push(a),r.set(a,(r.get(a)??0)+1);let n=[];for(let[a,s]of r)s===0&&n.push(a);let o=[];for(;n.length>0;){let a=n.shift();o.push(a);for(let s of t.get(a)??[]){let p=(r.get(s)??1)-1;r.set(s,p),p===0&&n.push(s)}}if(o.length!==e.nodes.length){let a=e.nodes.filter(s=>!o.includes(s));return te({code:"V-002",message:`Cyclic dependency detected in computed fields: ${a.join(", ")}`,path:a[0]})}return l(o)}function Et(e){let r=new Set,t=new Set,n=[];function o(a,s){r.add(a),t.add(a);let p=e.edges.get(a)??[];for(let u of p)if(r.has(u)){if(t.has(u)){let d=s.indexOf(u);d!==-1?n.push([...s.slice(d),u]):n.push([...s,u])}}else if(o(u,[...s,u]))return!0;return t.delete(a),!1}for(let a of e.nodes)r.has(a)||o(a,[a]);return n.length>0?n:null}function qi(e,r){let t=new Set,n=[...e.edges.get(r)??[]];for(;n.length>0;){let o=n.shift();t.has(o)||(t.add(o),n.push(...e.edges.get(o)??[]))}return t}function se(e,r){let t=ee(r.meta.timestamp),n=Re(e.computed),o=we(n);if(!o.ok)return te(v("CYCLIC_DEPENDENCY",o.error.message,"",o.error.path??"",t.timestamp));let a={},s={...r,computed:a};for(let p of o.value){let u=e.computed.fields[p];if(!u)continue;let d=U(s,e,null,p,void 0,t),h=f(u.expr,d);if(Z(h))return h;a[p]=h.value,s={...s,computed:{...a}}}return l(a)}function Bi(e,r,t){let n=ee(r.meta.timestamp),o=e.computed.fields[t];if(!o)return te(v("PATH_NOT_FOUND",`Computed field not found: ${t}`,"",t,n.timestamp));let a=U(r,e,null,t,void 0,n);return f(o.expr,a)}function Ue(e,r){let t=se(e,r);return Z(t)?{kind:"error",code:"INTERNAL_ERROR",message:`Error evaluating computed values for legality query: ${t.error.message}`}:{kind:"ok",snapshot:{...r,computed:t.value}}}function Ze(e,r,t,n){let o=e.actions[t];if(!o)return{kind:"error",code:"UNKNOWN_ACTION",message:`Unknown action: ${t}`};if(!o.available)return{kind:"ok",available:!0};let a=U(r,e,null,`actions.${t}.available`,void 0,n),s=f(o.available,a);return Z(s)?{kind:"error",code:"INTERNAL_ERROR",message:`Error evaluating availability: ${s.error.message}`}:typeof s.value!="boolean"?{kind:"error",code:"TYPE_MISMATCH",message:`Availability condition must return boolean, got ${typeof s.value}`}:{kind:"ok",available:s.value}}function Le(e,r,t,n=r.meta.timestamp){let o=Ue(e,r);return o.kind==="error"?o:Ze(e,o.snapshot,t,n)}function gt(e,r,t){let n=Le(e,r,t);if(n.kind==="error")throw new Error(n.message);return n.available}function Pn(e,r,t,n=r.meta.timestamp){let o=e.actions[t.type];if(!o)return{kind:"error",code:"UNKNOWN_ACTION",message:`Unknown action: ${t.type}`};let a=Ue(e,r);if(a.kind==="error")return a;let s=Ze(e,a.snapshot,t.type,n);if(s.kind==="error")return s;if(!s.available)return{kind:"ok",dispatchable:!1};if(!o.dispatchable)return{kind:"ok",dispatchable:!0};let p=U({...a.snapshot,input:t.input},e,t.type,`actions.${t.type}.dispatchable`,t.intentId,n),u=f(o.dispatchable,p);return Z(u)?{kind:"error",code:"INTERNAL_ERROR",message:`Error evaluating dispatchability: ${u.error.message}`}:typeof u.value!="boolean"?{kind:"error",code:"TYPE_MISMATCH",message:`Dispatchability condition must return boolean, got ${typeof u.value}`}:{kind:"ok",dispatchable:u.value}}function kt(e,r,t){let n=Pn(e,r,t);if(n.kind==="error")throw new Error(n.message);return n.dispatchable}function St(e,r){let t=Ue(e,r);if(t.kind==="error")throw new Error(t.message);return Object.keys(e.actions).filter(n=>{let o=Ze(e,t.snapshot,n,r.meta.timestamp);if(o.kind==="error")throw new Error(o.message);return o.available})}function Te(e,r){let t=_e(r,"status"),n=_e(r,"currentAction"),o=_e(r,"lastError"),a=new Set(r.removeRequirementIds??[]),s=r.addRequirements??[],p=Fn(e.system.pendingRequirements,a,s),u={...e.system,status:t?r.status:e.system.status,currentAction:n?r.currentAction??null:e.system.currentAction,lastError:o?r.lastError??null:e.system.lastError,pendingRequirements:p};return In(e.system,u)?{...e,system:u,meta:{...e.meta,version:e.meta.version+1}}:e}function Fn(e,r,t){let n=new Set(t.map(a=>a.id));return[...e.filter(a=>!r.has(a.id)&&!n.has(a.id)),...t]}function In(e,r){return e.status!==r.status||e.currentAction!==r.currentAction||!On(e.lastError,r.lastError)||!Dn(e.pendingRequirements,r.pendingRequirements)}function Dn(e,r){if(e.length!==r.length)return!1;for(let t=0;t<e.length;t++)if(!$n(e[t],r[t]))return!1;return!0}function On(e,r){if(e===r)return!0;if(e===null||r===null||e.code!==r.code||e.message!==r.message||e.timestamp!==r.timestamp||e.source.actionId!==r.source.actionId||e.source.nodePath!==r.source.nodePath)return!1;let t=e.context??{},n=r.context??{};return JSON.stringify(t)===JSON.stringify(n)}function $n(e,r){return e.id===r.id&&e.type===r.type&&e.actionId===r.actionId&&e.createdAt===r.createdAt&&e.flowPosition.nodePath===r.flowPosition.nodePath&&e.flowPosition.snapshotVersion===r.flowPosition.snapshotVersion&&JSON.stringify(e.params)===JSON.stringify(r.params)}function _e(e,r){return Object.prototype.hasOwnProperty.call(e,r)}function He(e,r,t,n){let o=r,a=se(e,r);ae(a)&&(o={...r,computed:a.value});let s=e.actions[t.type];if(!s)return je(o,t,"UNKNOWN_ACTION",`Unknown action: ${t.type}`,n);let p=vt(e,t);if(p)return je(o,t,"INVALID_INPUT",p,n);let u=o.system.currentAction===t.type;if(s.available&&!u){let A=Le(e,o,t.type,n.now);if(A.kind==="error")return je(o,t,A.code,A.message,n);if(!A.available)return je(o,t,"ACTION_UNAVAILABLE",`Action "${t.type}" is not available`,n)}let d={...o,input:t.input,system:{...o.system,status:"computing",currentAction:t.type}},h=U(d,e,t.type,`actions.${t.type}.flow`,t.intentId,n.now),b=xt(d),m=ie(s.flow,h,b,`actions.${t.type}.flow`),E=Vn(m.state.status),C=Un(o,t,E,m.state.error,m.state.requirements),I=[...m.state.patches],F={root:m.trace,nodes:Mn(m.trace),intent:{type:t.type,input:t.input},baseVersion:o.meta.version,resultVersion:Nt(o,I,C),duration:n.durationMs??0,terminatedBy:qn(m.state.status)};return{patches:I,systemDelta:C,trace:F,status:E}}async function bt(e,r,t,n){return He(e,r,t,n)}function vt(e,r){if(!r.intentId||r.intentId==="")return"Intent must have a non-empty intentId";let t=e.actions[r.type];return!t||!t.input&&!t.inputType?null:Zn(e,t.inputType,t.input,r.input)}function Vn(e){switch(e){case"running":case"complete":return"complete";case"pending":return"pending";case"halted":return"halted";case"error":return"error"}}function qn(e){switch(e){case"running":case"complete":return"complete";case"pending":return"effect";case"halted":return"halt";case"error":return"error"}}function Mn(e){let r={};function t(n){r[n.id]=n;for(let o of n.children)t(o)}return t(e),r}function je(e,r,t,n,o){let a=v(t,n,r.type,"",o.now),s={status:"error",currentAction:null,lastError:a,addRequirements:[],removeRequirementIds:[]},p={root:{id:`trace-error-${r.intentId}`,kind:"error",sourcePath:"",inputs:{},output:a,children:[],timestamp:o.now},nodes:{},intent:{type:r.type,input:r.input},baseVersion:e.meta.version,resultVersion:Nt(e,[],s),duration:o.durationMs??0,terminatedBy:"error"};return{patches:[],systemDelta:s,trace:p,status:"error"}}function Un(e,r,t,n,o){let a=t==="error";return{status:t==="pending"?"pending":t==="error"?"error":"idle",currentAction:t==="pending"?r.type:null,lastError:n,addRequirements:[...o],removeRequirementIds:e.system.pendingRequirements.map(p=>p.id)}}function Nt(e,r,t){let n=e.meta.version;return n+=1,Te(e,t)!==e&&(n+=1),n}function Zn(e,r,t,n){if(r){let o=$(n,r,e.types);return o.ok?null:o.message??"Invalid input"}if(!t)return null;if(t.type==="object"){if(typeof n!="object"||n===null||Array.isArray(n))return`Expected object input, got ${typeof n}`;let o=n,a=t.fields??{};for(let[s,p]of Object.entries(a))if(p.required&&!(s in o))return`Missing required field: ${s}`;for(let s of Object.keys(o))if(!(s in a))return`Unknown field: ${s}`;for(let[s,p]of Object.entries(a))if(s in o){let u=Ln(p,o[s],s);if(u)return u}}return null}function Ln(e,r,t){if(r==null)return e.required?`Missing required field: ${t}`:null;switch(e.type){case"string":if(typeof r!="string")return`Expected string for ${t}, got ${typeof r}`;break;case"number":if(typeof r!="number")return`Expected number for ${t}, got ${typeof r}`;break;case"boolean":if(typeof r!="boolean")return`Expected boolean for ${t}, got ${typeof r}`;break;case"array":if(!Array.isArray(r))return`Expected array for ${t}, got ${typeof r}`;break;case"object":if(typeof r!="object"||Array.isArray(r))return`Expected object for ${t}, got ${typeof r}`;break}return null}var _n={type:"object",required:!1};function jt(e,r,t,n){let o=r.data,a=r.system,s=r.input,p=[],u={type:"object",required:!0,fields:e.state.fields};for(let m of t){let E=ge(m.path);if(!pt(m.path)){p.push(v("PATH_NOT_FOUND",`Unsafe patch path: ${E}`,r.system.currentAction??"",E,n.now,{patch:m}));continue}if(Hn(m.path)){if(m.op!=="unset"){if(m.path.length===1){let A=Y(m.value,_n,{allowPartial:m.op==="merge",allowUndefined:!1});if(!A.ok){p.push(v("TYPE_MISMATCH",`Invalid patch value at ${E}: ${A.message??"type mismatch"}`,r.system.currentAction??"",E,n.now,{patch:m}));continue}}if(m.op==="merge"&&!wt(o,m.path)){p.push(v("TYPE_MISMATCH",`Invalid merge target at ${E}: target path must be an object or absent`,r.system.currentAction??"",E,n.now,{patch:m}));continue}}o=Tt(o,m);continue}let I=ve(e.state,e.types,m.path),F=I?null:Ne(u,m.path);if(!I&&!F){p.push(v("PATH_NOT_FOUND",`Unknown patch path: ${E}`,r.system.currentAction??"",E,n.now,{patch:m}));continue}if(m.op==="merge"&&!wt(o,m.path)){p.push(v("TYPE_MISMATCH",`Invalid merge target at ${E}: target path must be an object or absent`,r.system.currentAction??"",E,n.now,{patch:m}));continue}if(m.op!=="unset"){let A=I?$(m.value,I,e.types,{allowPartial:m.op==="merge",allowUndefined:!1}):Y(m.value,F,{allowPartial:m.op==="merge",allowUndefined:!1});if(!A.ok){p.push(v("TYPE_MISMATCH",`Invalid patch value at ${E}: ${A.message??"type mismatch"}`,r.system.currentAction??"",E,n.now,{patch:m}));continue}}o=Tt(o,m)}if(p.length>0){let m=p[p.length-1];a={...a,status:"error",lastError:m}}let d={...r,data:o,system:a,input:s},h=se(e,d),b=r.computed;if(ae(h))b=h.value;else if(Z(h)){let m=h.error;b={},a={...a,status:"error",lastError:m}}return{data:o,computed:b,system:a,input:s,meta:{...r.meta,version:r.meta.version+1,timestamp:n.now,randomSeed:n.randomSeed}}}function Hn(e){let r=e[0];return r.kind!=="prop"?null:r.name.startsWith("$")?r.name:null}function Rt(e){return e!==null&&!Array.isArray(e)&&typeof e=="object"}function wt(e,r){let t=e;for(let n of r){if(t===void 0)return!0;if(n.kind==="prop"){if(!Rt(t))return!1;t=t[n.name];continue}if(!Array.isArray(t))return!1;t=t[n.index]}return t===void 0?!0:Rt(t)}function Tt(e,r){switch(r.op){case"set":return ue(e,r.path,r.value);case"unset":return ke(e,r.path);case"merge":return Se(e,r.path,r.value)}}import{z as O}from"zod";import{z}from"zod";import{z as k}from"zod";var _=k.lazy(()=>k.discriminatedUnion("kind",[k.object({kind:k.literal("primitive"),type:k.string()}),k.object({kind:k.literal("array"),element:_}),k.object({kind:k.literal("record"),key:_,value:_}),k.object({kind:k.literal("object"),fields:k.record(k.string(),k.object({type:_,optional:k.boolean()}))}),k.object({kind:k.literal("union"),types:k.array(_).min(1)}),k.object({kind:k.literal("literal"),value:k.union([k.string(),k.number(),k.boolean(),k.null()])}),k.object({kind:k.literal("ref"),name:k.string()})])),At=k.object({name:k.string(),definition:_});var Wn=z.enum(["string","number","boolean","null","object","array"]),Bn=z.object({enum:z.array(z.unknown()).readonly()}),Kn=z.union([Wn,Bn]),de=z.lazy(()=>z.object({type:Kn,required:z.boolean(),default:z.unknown().optional(),description:z.string().optional(),fields:z.record(z.string(),de).optional(),items:de.optional()})),zt=z.object({fields:z.record(z.string(),de),fieldTypes:z.record(z.string(),_).optional()});import{z as me}from"zod";import{z as i}from"zod";var Gn=i.object({kind:i.literal("lit"),value:i.unknown()}),Yn=i.object({kind:i.literal("get"),path:W}),Jn=i.object({kind:i.literal("eq"),left:i.lazy(()=>c),right:i.lazy(()=>c)}),Xn=i.object({kind:i.literal("neq"),left:i.lazy(()=>c),right:i.lazy(()=>c)}),Qn=i.object({kind:i.literal("gt"),left:i.lazy(()=>c),right:i.lazy(()=>c)}),eo=i.object({kind:i.literal("gte"),left:i.lazy(()=>c),right:i.lazy(()=>c)}),to=i.object({kind:i.literal("lt"),left:i.lazy(()=>c),right:i.lazy(()=>c)}),ro=i.object({kind:i.literal("lte"),left:i.lazy(()=>c),right:i.lazy(()=>c)}),no=i.object({kind:i.literal("and"),args:i.array(i.lazy(()=>c))}),oo=i.object({kind:i.literal("or"),args:i.array(i.lazy(()=>c))}),ao=i.object({kind:i.literal("not"),arg:i.lazy(()=>c)}),io=i.object({kind:i.literal("if"),cond:i.lazy(()=>c),then:i.lazy(()=>c),else:i.lazy(()=>c)}),so=i.object({kind:i.literal("add"),left:i.lazy(()=>c),right:i.lazy(()=>c)}),po=i.object({kind:i.literal("sub"),left:i.lazy(()=>c),right:i.lazy(()=>c)}),lo=i.object({kind:i.literal("mul"),left:i.lazy(()=>c),right:i.lazy(()=>c)}),uo=i.object({kind:i.literal("div"),left:i.lazy(()=>c),right:i.lazy(()=>c)}),co=i.object({kind:i.literal("mod"),left:i.lazy(()=>c),right:i.lazy(()=>c)}),fo=i.object({kind:i.literal("min"),args:i.array(i.lazy(()=>c))}),mo=i.object({kind:i.literal("max"),args:i.array(i.lazy(()=>c))}),yo=i.object({kind:i.literal("abs"),arg:i.lazy(()=>c)}),xo=i.object({kind:i.literal("neg"),arg:i.lazy(()=>c)}),ho=i.object({kind:i.literal("floor"),arg:i.lazy(()=>c)}),Eo=i.object({kind:i.literal("ceil"),arg:i.lazy(()=>c)}),go=i.object({kind:i.literal("round"),arg:i.lazy(()=>c)}),ko=i.object({kind:i.literal("sqrt"),arg:i.lazy(()=>c)}),So=i.object({kind:i.literal("pow"),base:i.lazy(()=>c),exponent:i.lazy(()=>c)}),bo=i.object({kind:i.literal("sumArray"),array:i.lazy(()=>c)}),vo=i.object({kind:i.literal("minArray"),array:i.lazy(()=>c)}),No=i.object({kind:i.literal("maxArray"),array:i.lazy(()=>c)}),Ro=i.object({kind:i.literal("concat"),args:i.array(i.lazy(()=>c))}),wo=i.object({kind:i.literal("substring"),str:i.lazy(()=>c),start:i.lazy(()=>c),end:i.lazy(()=>c).optional()}),To=i.object({kind:i.literal("trim"),str:i.lazy(()=>c)}),jo=i.object({kind:i.literal("toLowerCase"),str:i.lazy(()=>c)}),Ao=i.object({kind:i.literal("toUpperCase"),str:i.lazy(()=>c)}),zo=i.object({kind:i.literal("strLen"),str:i.lazy(()=>c)}),Co=i.object({kind:i.literal("startsWith"),str:i.lazy(()=>c),prefix:i.lazy(()=>c)}),Po=i.object({kind:i.literal("endsWith"),str:i.lazy(()=>c),suffix:i.lazy(()=>c)}),Fo=i.object({kind:i.literal("strIncludes"),str:i.lazy(()=>c),search:i.lazy(()=>c)}),Io=i.object({kind:i.literal("indexOf"),str:i.lazy(()=>c),search:i.lazy(()=>c)}),Do=i.object({kind:i.literal("replace"),str:i.lazy(()=>c),search:i.lazy(()=>c),replacement:i.lazy(()=>c)}),Oo=i.object({kind:i.literal("split"),str:i.lazy(()=>c),delimiter:i.lazy(()=>c)}),$o=i.object({kind:i.literal("len"),arg:i.lazy(()=>c)}),Vo=i.object({kind:i.literal("at"),array:i.lazy(()=>c),index:i.lazy(()=>c)}),qo=i.object({kind:i.literal("first"),array:i.lazy(()=>c)}),Mo=i.object({kind:i.literal("last"),array:i.lazy(()=>c)}),Uo=i.object({kind:i.literal("slice"),array:i.lazy(()=>c),start:i.lazy(()=>c),end:i.lazy(()=>c).optional()}),Zo=i.object({kind:i.literal("includes"),array:i.lazy(()=>c),item:i.lazy(()=>c)}),Lo=i.object({kind:i.literal("filter"),array:i.lazy(()=>c),predicate:i.lazy(()=>c)}),_o=i.object({kind:i.literal("map"),array:i.lazy(()=>c),mapper:i.lazy(()=>c)}),Ho=i.object({kind:i.literal("find"),array:i.lazy(()=>c),predicate:i.lazy(()=>c)}),Wo=i.object({kind:i.literal("every"),array:i.lazy(()=>c),predicate:i.lazy(()=>c)}),Bo=i.object({kind:i.literal("some"),array:i.lazy(()=>c),predicate:i.lazy(()=>c)}),Ko=i.object({kind:i.literal("append"),array:i.lazy(()=>c),items:i.array(i.lazy(()=>c))}),Go=i.object({kind:i.literal("reverse"),array:i.lazy(()=>c)}),Yo=i.object({kind:i.literal("unique"),array:i.lazy(()=>c)}),Jo=i.object({kind:i.literal("flat"),array:i.lazy(()=>c)}),Xo=i.object({kind:i.literal("object"),fields:i.record(i.string(),i.lazy(()=>c))}),Qo=i.object({kind:i.literal("field"),object:i.lazy(()=>c),property:i.string()}),ea=i.object({kind:i.literal("keys"),obj:i.lazy(()=>c)}),ta=i.object({kind:i.literal("values"),obj:i.lazy(()=>c)}),ra=i.object({kind:i.literal("entries"),obj:i.lazy(()=>c)}),na=i.object({kind:i.literal("merge"),objects:i.array(i.lazy(()=>c))}),oa=i.object({kind:i.literal("hasKey"),obj:i.lazy(()=>c),key:i.lazy(()=>c)}),aa=i.object({kind:i.literal("pick"),obj:i.lazy(()=>c),keys:i.lazy(()=>c)}),ia=i.object({kind:i.literal("omit"),obj:i.lazy(()=>c),keys:i.lazy(()=>c)}),sa=i.object({kind:i.literal("fromEntries"),entries:i.lazy(()=>c)}),pa=i.object({kind:i.literal("typeof"),arg:i.lazy(()=>c)}),la=i.object({kind:i.literal("isNull"),arg:i.lazy(()=>c)}),ua=i.object({kind:i.literal("coalesce"),args:i.array(i.lazy(()=>c))}),ca=i.object({kind:i.literal("toString"),arg:i.lazy(()=>c)}),fa=i.object({kind:i.literal("toNumber"),arg:i.lazy(()=>c)}),da=i.object({kind:i.literal("toBoolean"),arg:i.lazy(()=>c)}),c=i.union([Gn,Yn,Jn,Xn,Qn,eo,to,ro,no,oo,ao,io,so,po,lo,uo,co,fo,mo,yo,xo,ho,Eo,go,ko,So,bo,vo,No,Ro,wo,To,jo,Ao,zo,Co,Po,Fo,Io,Do,Oo,$o,Vo,qo,Mo,Uo,Zo,Lo,_o,Ho,Wo,Bo,Ko,Go,Yo,Jo,Xo,Qo,ea,ta,ra,na,oa,aa,ia,sa,pa,la,ua,ca,fa,da]),Ns=i.enum(["lit","get","eq","neq","gt","gte","lt","lte","and","or","not","if","add","sub","mul","div","mod","min","max","abs","neg","floor","ceil","round","sqrt","pow","sumArray","minArray","maxArray","concat","substring","trim","toLowerCase","toUpperCase","strLen","startsWith","endsWith","strIncludes","indexOf","replace","split","len","at","first","last","slice","includes","filter","map","find","every","some","append","reverse","unique","flat","object","field","keys","values","entries","merge","hasKey","pick","omit","fromEntries","typeof","isNull","coalesce","toString","toNumber","toBoolean"]);var ma=me.object({deps:me.array(W),expr:c,description:me.string().optional()}),Ct=me.object({fields:me.record(W,ma)});import{z as Ae}from"zod";import{z as S}from"zod";import{z as N}from"zod";var ya=N.discriminatedUnion("kind",[N.object({kind:N.literal("prop"),name:N.string().min(1)}),N.object({kind:N.literal("index"),index:N.number().int().nonnegative()})]),ye=N.array(ya).min(1),Pt=N.discriminatedUnion("op",[N.object({op:N.literal("set"),path:ye,value:N.unknown()}),N.object({op:N.literal("unset"),path:ye}),N.object({op:N.literal("merge"),path:ye,value:N.record(N.string(),N.unknown())})]),Cs=N.object({type:N.string(),input:N.unknown().optional(),intentId:N.string()});function Ps(e){return{kind:"prop",name:e}}function Fs(e){return{kind:"index",index:e}}function Is(e,r){return{op:"set",path:e,value:r}}function Ds(e){return{op:"unset",path:e}}function Os(e,r){return{op:"merge",path:e,value:r}}var xa=S.enum(["set","unset","merge"]),ha=S.object({kind:S.literal("seq"),steps:S.array(S.lazy(()=>xe))}),Ea=S.object({kind:S.literal("if"),cond:c,then:S.lazy(()=>xe),else:S.lazy(()=>xe).optional()}),ga=S.object({kind:S.literal("patch"),op:xa,path:ye,value:c.optional()}),ka=S.object({kind:S.literal("effect"),type:S.string(),params:S.record(S.string(),c)}),Sa=S.object({kind:S.literal("call"),flow:S.string()}),ba=S.object({kind:S.literal("halt"),reason:S.string().optional()}),va=S.object({kind:S.literal("fail"),code:S.string(),message:c.optional()}),xe=S.union([ha,Ea,ga,ka,Sa,ba,va]),Us=S.enum(["seq","if","patch","effect","call","halt","fail"]);var Ft=Ae.object({flow:xe,input:de.optional(),inputType:_.optional(),params:Ae.array(Ae.string()).readonly().optional(),available:c.optional(),dispatchable:c.optional(),description:Ae.string().optional()});var Na=O.object({name:O.string().optional(),description:O.string().optional(),authors:O.array(O.string()).optional()}),It=O.object({id:O.string(),version:O.string(),hash:O.string(),types:O.record(O.string(),At),state:zt,computed:Ct,actions:O.record(O.string(),Ft),meta:Na.optional()});import{z as j}from"zod";import{z as x}from"zod";var We=x.object({code:x.string(),message:x.string(),source:x.object({actionId:x.string(),nodePath:x.string()}),timestamp:x.number(),context:x.record(x.string(),x.unknown()).optional()}),Ra=x.object({nodePath:x.string(),snapshotVersion:x.number()}),Be=x.object({id:x.string(),type:x.string(),params:x.record(x.string(),x.unknown()),actionId:x.string(),flowPosition:Ra,createdAt:x.number()}),Ke=x.object({status:x.enum(["idle","computing","pending","error"]),lastError:We.nullable(),pendingRequirements:x.array(Be),currentAction:x.string().nullable()}),wa=x.object({version:x.number(),timestamp:x.number(),randomSeed:x.string(),schemaHash:x.string()}),np=x.object({data:x.unknown(),computed:x.record(W,x.unknown()),system:Ke,input:x.unknown(),meta:wa});function Dt(){return{status:"idle",lastError:null,pendingRequirements:[],currentAction:null}}var Ta=j.enum(["complete","pending","halted","error"]),ja=j.object({status:Ke.shape.status.optional(),currentAction:j.string().nullable().optional(),lastError:We.nullable().optional(),addRequirements:j.array(Be),removeRequirementIds:j.array(j.string())}),up=j.object({patches:j.array(Pt),systemDelta:ja,trace:nt,status:Ta}),Aa=j.object({code:j.string(),message:j.string(),path:j.string().optional()}),cp=j.object({valid:j.boolean(),errors:j.array(Aa)}),fp=j.object({value:j.unknown(),trace:le,deps:j.array(W)});function Ot(){return{valid:!0,errors:[]}}function Ge(e){return{valid:!1,errors:e}}function Vt(e){let r=[],t=It.safeParse(e);if(!t.success){let b=t.error.issues.map(m=>({code:"SCHEMA_ERROR",message:m.message,path:m.path.map(String).join(".")}));return Ge(b)}let n=t.data;dt(n.id)||r.push({code:"SCHEMA_ERROR",message:"Schema id must be a valid URI or UUID",path:"id"}),mt(n.version)||r.push({code:"SCHEMA_ERROR",message:"Schema version must follow Semantic Versioning 2.0",path:"version"});let o=n.hash;if(!o)r.push({code:"SCHEMA_ERROR",message:"Schema hash is required",path:"hash"});else{let b=e,{hash:m,...E}=b,C=De(E);o!==C&&r.push({code:"V-008",message:`Schema hash mismatch: expected ${C}, got ${o}`,path:"hash"})}Object.keys(n.state.fields).length===0&&r.push({code:"SCHEMA_ERROR",message:"StateSpec.fields must not be empty",path:"state.fields"}),Object.keys(n.computed.fields).length===0&&r.push({code:"SCHEMA_ERROR",message:"ComputedSpec.fields must not be empty",path:"computed.fields"}),Object.keys(n.actions).length===0&&r.push({code:"SCHEMA_ERROR",message:"actions must not be empty",path:"actions"}),r.push(...Pa(n)),r.push(...Ia(n,"state.fields")),r.push(...Da(n)),r.push(...Oa(n)),r.push(...$a(n)),r.push(...Va(n));let a=Re(n.computed),s=we(a);Z(s)&&r.push(s.error);let p=Et(a);if(p)for(let b of p)r.push({code:"V-002",message:`Cyclic dependency: ${b.join(" -> ")}`,path:b[0]});let u=new Set(Object.keys(n.actions)),d=za(n,u);r.push(...d);let h=Ca(n);return r.push(...h),r.length>0?Ge(r):Ot()}function za(e,r){let t=[];for(let[n,o]of Object.entries(e.actions)){let a=he(o.flow);for(let s of a)r.has(s)||t.push({code:"V-004",message:`Unknown flow reference: "${s}" in action "${n}"`,path:`actions.${n}`})}return t}function he(e){let r=[];switch(e.kind){case"call":r.push(e.flow);break;case"seq":for(let t of e.steps)r.push(...he(t));break;case"if":r.push(...he(e.then)),e.else&&r.push(...he(e.else));break}return r}function Ca(e){let r=[],t=new Map;for(let[s,p]of Object.entries(e.actions))t.set(s,he(p.flow));let n=new Set,o=new Set;function a(s,p){n.add(s),o.add(s);let u=t.get(s)??[];for(let d of u)if(n.has(d)){if(o.has(d))return r.push({code:"V-005",message:`Cyclic call detected: ${[...p,d].join(" -> ")}`,path:`actions.${s}`}),!0}else if(a(d,[...p,d]))return!0;return o.delete(s),!1}for(let s of t.keys())n.has(s)||a(s,[s]);return r}function Pa(e){let r=[];if(e.state.fieldTypes)for(let t of Object.keys(e.state.fieldTypes))t in e.state.fields||r.push({code:"V-010",message:`state.fieldTypes.${t} has no matching state.fields entry`,path:`state.fieldTypes.${t}`}),r.push(...Q(e.state.fieldTypes[t],e.types,`state.fieldTypes.${t}`));for(let[t,n]of Object.entries(e.actions)){if(!n.inputType){n.params&&n.params.length>0&&r.push(...$t(t,n,e.types));continue}r.push(...Q(n.inputType,e.types,`actions.${t}.inputType`)),n.params&&n.params.length>0&&r.push(...$t(t,n,e.types))}return r}function $t(e,r,t){let n=r.params??[];if(n.length===0)return[];let o=[],a=new Set;for(let[u,d]of n.entries()){if(a.has(d)){o.push({code:"V-010",message:`Duplicate parameter name "${d}" is not allowed`,path:`actions.${e}.params.${u}`});continue}a.add(d)}let s=null;if(r.inputType?s=Ye(r.inputType,t):r.input?.type==="object"&&r.input.fields&&(s=Object.keys(r.input.fields)),!s)return[...o,{code:"V-010",message:`actions.${e}.params requires an object-shaped input carrier`,path:`actions.${e}.params`}];let p=new Set(s);return[...o,...n.flatMap((u,d)=>p.has(u)?[]:[{code:"V-010",message:`Parameter "${u}" has no matching input field`,path:`actions.${e}.params.${d}`}])]}function Ye(e,r,t=[]){if(e.kind==="ref"){if(t.includes(e.name))return null;let n=r[e.name];return n?Ye(n.definition,r,[...t,e.name]):null}if(e.kind==="union"){let n=e.types.filter(o=>!Fa(o,r,t));return n.length===1?Ye(n[0],r,t):null}return e.kind==="object"?Object.keys(e.fields):null}function Fa(e,r,t=[]){let n=L(e,r,t);return n?n.kind==="primitive"&&n.type==="null"||n.kind==="literal"&&n.value===null:!1}function Q(e,r,t,n=[]){let o=[];if(e.kind==="ref"){if(n.includes(e.name))return o.push({code:"V-010",message:`Cyclic type reference "${e.name}" in typing seam`,path:t}),o;let a=r[e.name];return!a||!L(e,r)?(o.push({code:"V-010",message:`Unknown type reference "${e.name}" in typing seam`,path:t}),o):(o.push(...Q(a.definition,r,t,[...n,e.name])),o)}switch(e.kind){case"array":return Q(e.element,r,`${t}.element`,n);case"record":{let a=[...Q(e.key,r,`${t}.key`,n),...Q(e.value,r,`${t}.value`,n)],s=L(e.key,r);return s&&(s.kind!=="primitive"||s.type!=="string")&&a.push({code:"V-010",message:"Record typing seams require string keys",path:`${t}.key`}),a}case"object":for(let[a,s]of Object.entries(e.fields))o.push(...Q(s.type,r,`${t}.fields.${a}.type`,n));return o;case"union":for(let[a,s]of e.types.entries())o.push(...Q(s,r,`${t}.types.${a}`,n));return o;default:return o}}function Ia(e,r){let t=[],n=e.state,o=(a,s,p)=>{if(!a.required&&a.default===void 0&&t.push({code:"SCHEMA_ERROR",message:"Optional fields must define a default value",path:s}),a.default!==void 0){if(p){let u=$(a.default,p,e.types);u.ok||t.push({code:"V-009",message:`Default value type mismatch: ${u.message}`,path:s})}else if(a.default===null&&a.required!==!1)t.push({code:"V-009",message:`Default value 'null' is not compatible with required field type '${typeof a.type=="string"?a.type:"enum"}'`,path:s});else if(a.default!==null){let u=Y(a.default,a);u.ok||t.push({code:"V-009",message:`Default value type mismatch: ${u.message}`,path:s})}}if(a.type==="object"&&a.fields)for(let[u,d]of Object.entries(a.fields)){let h=p?be(p,e.types,[{kind:"prop",name:u}]):null;o(d,`${s}.${u}`,h??void 0)}if(a.type==="array"&&a.items){let u=p?be(p,e.types,[{kind:"index",index:0}]):null;o(a.items,`${s}[]`,u??void 0)}};for(let[a,s]of Object.entries(n.fields))o(s,`${r}.${a}`,n.fieldTypes?.[a]);return t}function Da(e){let r=[];for(let[t,n]of Object.entries(e.computed.fields))for(let o of n.deps)fe(e.computed,o)||ce(e.state,o,e.types)||r.push({code:"V-001",message:`Unknown dependency path: ${o}`,path:`computed.fields.${t}`});return r}function Oa(e){let r=[];for(let[t,n]of Object.entries(e.computed.fields)){let a=G(n.expr).filter(s=>s.startsWith("$")||fe(e.computed,s)?!1:s.startsWith("input.")||s.startsWith("system.")?!0:!ce(e.state,s,e.types));for(let s of a)r.push({code:"V-003",message:`Unknown path in computed expression: ${s}`,path:`computed.fields.${t}`})}return r}function $a(e){let r=[];for(let[t,n]of Object.entries(e.computed.fields)){let o=G(n.expr),a=new Set(n.deps),s=new Set(o.filter(p=>p.startsWith("$")||p==="input"||p.startsWith("input.")||p==="system"||p.startsWith("system.")||p==="meta"||p.startsWith("meta.")?!1:fe(e.computed,p)?!0:ce(e.state,p,e.types)));for(let p of s)a.has(p)||r.push({code:"V-001",message:`Missing dependency for computed expression path: ${p}`,path:`computed.fields.${t}`})}return r}function Va(e){let r=[];for(let[t,n]of Object.entries(e.actions)){let o=[...yt(n.flow),...n.available?G(n.available):[],...n.dispatchable?G(n.dispatchable):[]];for(let a of o)if(!a.startsWith("$")){if(a==="input"||a.startsWith("input.")){if(n.inputType){let s=a==="input"?"":a.slice(6);ut(n.inputType,e.types,s)||r.push({code:"V-003",message:`Unknown input path: ${a}`,path:`actions.${t}`})}else if(n.input){let s=a==="input"?"":a.slice(6);Me(n.input,s)||r.push({code:"V-003",message:`Unknown input path: ${a}`,path:`actions.${t}`})}continue}fe(e.computed,a)||a.startsWith("system.")||a==="meta"||a.startsWith("meta.")||ce(e.state,a,e.types)||r.push({code:"V-003",message:`Unknown state path: ${a}`,path:`actions.${t}`})}}return r}function qt(e,r,t){let n=ee(r.meta.timestamp);return Mt(e,r,t,n)}function Mt(e,r,t,n){if(e.computed.fields[t]!==void 0)return qa(e,r,t,n);if(t.startsWith("system.")){let a=V(r.system,t.slice(7));return{value:a,trace:g(n,"expr",t,{path:t},a,[]),deps:[]}}if(t.startsWith("input.")||t==="input"){let a=t==="input"?r.input:V(r.input,t.slice(6));return{value:a,trace:g(n,"expr",t,{path:t},a,[]),deps:[]}}let o=V(r.data,t);return{value:o,trace:g(n,"expr",t,{path:t},o,[]),deps:[]}}function qa(e,r,t,n){let o=e.computed.fields[t];if(!o){let d=r.computed[t];return{value:d,trace:g(n,"computed",t,{path:t},d,[]),deps:[]}}let a=U(r,e,null,t,void 0,n),s=f(o.expr,a),p=ae(s)?s.value:null,u=o.deps.map(d=>Mt(e,r,d,n).trace);return{value:p,trace:g(n,"computed",t,{expr:o.expr},p,u),deps:o.deps}}function jp(e){let r={};for(let[t,n]of Object.entries(e.fields))n.default!==void 0&&(r[t]=n.default);return r}import{z as re}from"zod";var Cp=re.object({now:re.number(),randomSeed:re.string(),env:re.record(re.string(),re.unknown()).optional(),durationMs:re.number().optional()});function il(e,r,t){return{data:e,computed:{},system:Dt(),input:void 0,meta:{version:0,timestamp:t.now,randomSeed:t.randomSeed,schemaHash:r}}}function sl(e,r,t){return t===void 0&&typeof r=="string"?{type:e,input:void 0,intentId:r}:t===void 0?{type:e,input:r,intentId:""}:{type:e,input:r,intentId:t}}function yl(){return{compute:bt,computeSync:He,apply:jt,applySystemDelta:Te,validate:Vt,explain:qt,isActionAvailable:gt,getAvailableActions:St,isIntentDispatchable:kt}}export{yo as AbsExpr,Ft as ActionSpec,so as AddExpr,no as AndExpr,Ko as AppendExpr,Vo as AtExpr,Sa as CallFlow,Eo as CeilExpr,ua as CoalesceExpr,up as ComputeResult,Ta as ComputeStatus,ma as ComputedFieldSpec,Ct as ComputedSpec,Ro as ConcatExpr,Ua as CoreErrorCode,uo as DivExpr,It as DomainSchema,ka as EffectFlow,Po as EndsWithExpr,ra as EntriesExpr,Bn as EnumFieldType,Jn as EqExpr,We as ErrorValue,Wo as EveryExpr,fp as ExplainResult,Ns as ExprKind,c as ExprNodeSchema,va as FailFlow,Qo as FieldExpr,de as FieldSpec,Kn as FieldType,Lo as FilterExpr,Ho as FindExpr,qo as FirstExpr,Jo as FlatExpr,ho as FloorExpr,Us as FlowKind,xe as FlowNodeSchema,Ra as FlowPosition,sa as FromEntriesExpr,Yn as GetExpr,Qn as GtExpr,eo as GteExpr,ba as HaltFlow,oa as HasKeyExpr,Cp as HostContext,io as IfExpr,Ea as IfFlow,Zo as IncludesExpr,Io as IndexOfExpr,Cs as Intent,la as IsNullExpr,ea as KeysExpr,Mo as LastExpr,$o as LenExpr,Gn as LitExpr,to as LtExpr,ro as LteExpr,_o as MapExpr,No as MaxArrayExpr,mo as MaxExpr,na as MergeExpr,vo as MinArrayExpr,fo as MinExpr,co as ModExpr,lo as MulExpr,xo as NegExpr,Xn as NeqExpr,ao as NotExpr,Xo as ObjectExpr,ia as OmitExpr,oo as OrExpr,Pt as Patch,ga as PatchFlow,xa as PatchOp,ye as PatchPath,ya as PatchSegment,aa as PickExpr,So as PowExpr,Wn as PrimitiveFieldType,Do as ReplaceExpr,Be as Requirement,mi as Result,Go as ReverseExpr,go as RoundExpr,Na as SchemaMeta,W as SemanticPath,ha as SeqFlow,Uo as SliceExpr,np as Snapshot,wa as SnapshotMeta,Bo as SomeExpr,Oo as SplitExpr,ko as SqrtExpr,Co as StartsWithExpr,zt as StateSpec,Fo as StrIncludesExpr,zo as StrLenExpr,po as SubExpr,wo as SubstringExpr,bo as SumArrayExpr,ja as SystemDelta,Ke as SystemState,da as ToBooleanExpr,jo as ToLowerCaseExpr,fa as ToNumberExpr,ca as ToStringExpr,Ao as ToUpperCaseExpr,nt as TraceGraph,le as TraceNode,Jt as TraceNodeKind,Xt as TraceTermination,To as TrimExpr,_ as TypeDefinition,At as TypeSpec,pa as TypeofExpr,Yo as UniqueExpr,Aa as ValidationError,cp as ValidationResult,ta as ValuesExpr,jt as apply,Te as applySystemDelta,Re as buildDependencyGraph,Wa as canonicalEqual,ne as compareUnicodeCodePoints,bt as compute,He as computeSync,U as createContext,yl as createCore,v as createError,xt as createFlowState,Dt as createInitialSystemState,sl as createIntent,il as createSnapshot,ee as createTraceContext,g as createTraceNode,Et as detectCycles,te as err,se as evaluateComputed,f as evaluateExpr,Oi as evaluateFlow,ie as evaluateFlowSync,Bi as evaluateSingleComputed,qt as explain,jp as extractDefaults,Ha as fromCanonical,Ja as generateRequirementId,tt as generateRequirementIdSync,rt as generateTraceId,St as getAvailableActions,rr as getByPatchPath,V as getByPath,qi as getTransitiveDeps,si as hasPath,Yt as hashSchema,Ga as hashSchemaEffective,Ya as hashSchemaEffectiveSync,De as hashSchemaSync,Fs as indexSegment,Ge as invalidResult,gt as isActionAvailable,Z as isErr,Za as isErrorValue,kt as isIntentDispatchable,ae as isOk,pt as isSafePatchPath,oi as joinPath,li as lastSegment,Se as mergeAtPatchPath,ii as mergeAtPath,Os as mergePatch,l as ok,pi as parentPath,D as parsePath,ge as patchPathToDisplayString,Ps as propSegment,st as semanticPathToPatchPath,ue as setByPatchPath,Qt as setByPath,Is as setPatch,Qe as sha256,et as sha256Sync,Fe as sortKeys,pe as toCanonical,_a as toJcs,we as topologicalSort,ke as unsetByPatchPath,ai as unsetByPath,Ds as unsetPatch,Ot as validResult,Vt as validate,vt as validateIntentInput,X as withCollectionContext,Ee as withNodePath,Oe as withSnapshot};
|
|
1
|
+
import{z as Ht}from"zod";var Za=Ht.enum(["VALIDATION_ERROR","PATH_NOT_FOUND","TYPE_MISMATCH","DIVISION_BY_ZERO","INDEX_OUT_OF_BOUNDS","UNKNOWN_ACTION","ACTION_UNAVAILABLE","INVALID_INPUT","CYCLIC_DEPENDENCY","UNKNOWN_FLOW","CYCLIC_CALL","UNKNOWN_EFFECT","INTERNAL_ERROR"]);function v(e,r,t,n,o,a){return{code:e,message:r,source:{actionId:t,nodePath:n},timestamp:o,context:a}}function La(e){if(typeof e!="object"||e===null)return!1;let r=e;return typeof r.code=="string"&&typeof r.message=="string"&&typeof r.source=="object"&&r.source!==null&&typeof r.source.actionId=="string"&&typeof r.source.nodePath=="string"&&typeof r.timestamp=="number"}import{z as w}from"zod";function Ie(e){if(e==null)return e;if(Array.isArray(e))return e.map(Ie);if(typeof e=="object"){let r={},t=Object.keys(e).sort();for(let n of t){let o=e[n];o!==void 0&&(r[n]=Ie(o))}return r}return e}function le(e){let r=Ie(e);return JSON.stringify(r)}function Ha(e){return De(e)}function De(e){if(e===null)return"null";switch(typeof e){case"string":return JSON.stringify(e);case"number":return Number.isFinite(e)?JSON.stringify(e):"null";case"boolean":return e?"true":"false";case"undefined":return"null";case"object":return Array.isArray(e)?Wt(e):Bt(e);default:return"null"}}function Wt(e){return`[${e.map(t=>t===void 0||typeof t=="function"||typeof t=="symbol"?"null":De(t)).join(",")}]`}function Bt(e){let r=Object.keys(e).sort(oe),t=[];for(let n of r){let o=e[n];o===void 0||typeof o=="function"||typeof o=="symbol"||t.push(`${JSON.stringify(n)}:${De(o)}`)}return`{${t.join(",")}}`}function oe(e,r){let t=Array.from(e),n=Array.from(r),o=Math.min(t.length,n.length);for(let a=0;a<o;a++){let s=t[a].codePointAt(0)??0,p=n[a].codePointAt(0)??0;if(s!==p)return s-p}return t.length-n.length}function Wa(e){return JSON.parse(e)}function Ba(e,r){return le(e)===le(r)}function Qe(e,r){if(r==="effective")return e;let t=e.state?.fields??{},n=Object.entries(t).filter(([o])=>!o.startsWith("$"));return n.length===Object.keys(t).length?e:{...e,state:{...e.state,fields:Object.fromEntries(n)}}}async function et(e){let r=new TextEncoder().encode(e),t=await crypto.subtle.digest("SHA-256",r);return Array.from(new Uint8Array(t)).map(o=>o.toString(16).padStart(2,"0")).join("")}var Kt=new Uint32Array([1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298]),Gt=new Uint32Array([1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225]);function M(e,r){return e>>>r|e<<32-r}function Yt(e){let r=e.length*8,t=e.length+1,n=t%64<=56?56-t%64:120-t%64,o=t+n+8,a=new Uint8Array(o);a.set(e),a[e.length]=128;let s=Math.floor(r/4294967296),p=r>>>0;a[o-8]=s>>>24&255,a[o-7]=s>>>16&255,a[o-6]=s>>>8&255,a[o-5]=s&255,a[o-4]=p>>>24&255,a[o-3]=p>>>16&255,a[o-2]=p>>>8&255,a[o-1]=p&255;let u=new Uint32Array(Gt),d=new DataView(a.buffer);for(let m=0;m<a.length;m+=64){let E=new Uint32Array(64);for(let R=0;R<16;R++)E[R]=d.getUint32(m+R*4);for(let R=16;R<64;R++){let Pe=M(E[R-15],7)^M(E[R-15],18)^E[R-15]>>>3,Fe=M(E[R-2],17)^M(E[R-2],19)^E[R-2]>>>10;E[R]=E[R-16]+Pe+E[R-7]+Fe>>>0}let C=u[0],I=u[1],F=u[2],A=u[3],q=u[4],H=u[5],J=u[6],Ce=u[7];for(let R=0;R<64;R++){let Pe=M(q,6)^M(q,11)^M(q,25),Fe=q&H^~q&J,Xe=Ce+Pe+Fe+Kt[R]+E[R]>>>0,Zt=M(C,2)^M(C,13)^M(C,22),Lt=C&I^C&F^I&F,_t=Zt+Lt>>>0;Ce=J,J=H,H=q,q=A+Xe>>>0,A=F,F=I,I=C,C=Xe+_t>>>0}u[0]=u[0]+C>>>0,u[1]=u[1]+I>>>0,u[2]=u[2]+F>>>0,u[3]=u[3]+A>>>0,u[4]=u[4]+q>>>0,u[5]=u[5]+H>>>0,u[6]=u[6]+J>>>0,u[7]=u[7]+Ce>>>0}let h=new Uint8Array(32),b=new DataView(h.buffer);for(let m=0;m<u.length;m++)b.setUint32(m*4,u[m]);return h}function tt(e){let r=new TextEncoder().encode(e),t=Yt(r),n="";for(let o of t)n+=o.toString(16).padStart(2,"0");return n}async function Jt(e,r="semantic"){let t=le(Qe(e,r));return et(t)}function Oe(e,r="semantic"){let t=le(Qe(e,r));return tt(t)}async function Ya(e){return Jt(e,"effective")}function Ja(e){return Oe(e,"effective")}async function Xa(e,r,t,n){let o=`${e}:${r}:${t}:${n}`;return`req-${(await et(o)).slice(0,16)}`}function rt(e,r,t,n){let o=`${e}:${r}:${t}:${n}`;return`req-${tt(o).slice(0,16)}`}function nt(e=0){return`trace-${e}`}var Xt=w.enum(["expr","computed","flow","patch","effect","branch","call","halt","error"]),ue=w.lazy(()=>w.object({id:w.string(),kind:Xt,sourcePath:w.string(),inputs:w.record(w.string(),w.unknown()),output:w.unknown(),children:w.array(ue),timestamp:w.number()})),Qt=w.enum(["complete","effect","halt","error"]),ot=w.object({root:ue,nodes:w.record(w.string(),ue),intent:w.object({type:w.string(),input:w.unknown()}),baseVersion:w.number(),resultVersion:w.number(),duration:w.number(),terminatedBy:Qt});function te(e){let r=0;return{timestamp:e,nextId:()=>nt(r++)}}function g(e,r,t,n,o,a=[]){return{id:e.nextId(),kind:r,sourcePath:t,inputs:n,output:o,children:a,timestamp:e.timestamp}}function U(e,r,t,n,o,a){return{snapshot:e,schema:r,currentAction:t,nodePath:n,intentId:o,uuidCounter:0,trace:typeof a=="object"?a:te(a)}}function X(e,r,t,n){return{...e,$item:r,$index:t,$array:n}}function $e(e,r){return{...e,snapshot:r}}function ge(e,r){return{...e,nodePath:r}}function D(e){if(!e)return[];let r=[],t="",n=!1;for(let o of e){if(n){o==="."||o==="\\"?t+=o:t+=`\\${o}`,n=!1;continue}if(o==="\\"){n=!0;continue}if(o==="."){r.push(t),t="";continue}t+=o}return n&&(t+="\\"),r.push(t),r}function at(e){return e.replaceAll("\\","\\\\").replaceAll(".","\\.")}function ai(...e){return e.filter(Boolean).map(at).join(".")}function V(e,r){if(!r)return e;let t=D(r),n=e;for(let o of t){if(n==null)return;if(typeof n=="object")n=n[o];else return}return n}function er(e,r,t){if(!r)return t;let n=D(r);return it(e,n,t)}function it(e,r,t){if(r.length===0)return t;let[n,...o]=r,a=e!==null&&typeof e=="object"?e:{};if(o.length===0)return{...a,[n]:t};let s=a[n];return{...a,[n]:it(s,o,t)}}function ii(e,r){if(!r)return;let t=D(r);return st(e,t)}function st(e,r){if(r.length===0||e===null||typeof e!="object")return e;let[t,...n]=r,o=e;if(n.length===0){let{[t]:s,...p}=o;return p}let a=o[t];return{...o,[t]:st(a,n)}}function si(e,r,t){let n=V(e,r),o=n!==null&&typeof n=="object"?{...n,...t}:t;return er(e,r,o)}function pi(e,r){if(!r)return e!==void 0;let t=D(r),n=e;for(let o of t){if(n==null||typeof n!="object"||!(o in n))return!1;n=n[o]}return!0}function li(e){let r=D(e);return tr(...r.slice(0,-1))}function ui(e){let r=D(e);return r[r.length-1]??""}function tr(...e){return e.map(at).join(".")}var rr=new Set(["__proto__","constructor","prototype"]);function ke(e){let r="";for(let t=0;t<e.length;t++){let n=e[t];if(n.kind==="prop"){r+=t===0?n.name:`.${n.name}`;continue}r+=`[${n.index}]`}return r}function pt(e){let r=D(e);if(r.length===0)return[{kind:"prop",name:e}];let t=[];for(let n of r){let o=or(n);if(o){t.push(...o);continue}t.push({kind:"prop",name:n})}return t.length>0?t:[{kind:"prop",name:e}]}function lt(e){return e.every(r=>r.kind!=="prop"||!rr.has(r.name))}function nr(e,r){let t=e;for(let n of r){if(n.kind==="prop"){if(t===null||typeof t!="object"||Array.isArray(t))return;t=t[n.name];continue}if(!Array.isArray(t))return;t=t[n.index]}return t}function ce(e,r,t){return Ve(e,r,t)}function Se(e,r){return qe(e,r)}function be(e,r,t){let n=nr(e,r),o=Me(n)?{...n,...t}:t;return ce(e,r,o)}function Ve(e,r,t){if(r.length===0)return t;let[n,...o]=r;if(n.kind==="prop"){let s=Me(e)?e:{},p=Ve(s[n.name],o,t);return{...s,[n.name]:p}}let a=Array.isArray(e)?[...e]:[];return a[n.index]=Ve(a[n.index],o,t),a}function qe(e,r){if(r.length===0)return e;let[t,...n]=r;if(t.kind==="prop"){if(!Me(e))return e;if(n.length===0){let{[t.name]:a,...s}=e;return s}return{...e,[t.name]:qe(e[t.name],n)}}if(!Array.isArray(e))return e;let o=[...e];return n.length===0?(t.index>=0&&t.index<o.length&&delete o[t.index],o):(o[t.index]=qe(o[t.index],n),o)}function Me(e){return e!==null&&typeof e=="object"&&!Array.isArray(e)}function or(e){if(!e.includes("[")||e.includes("\\[")||e.includes("\\]"))return null;let r=[],t=0,n=!1;for(;t<e.length;){let o=e.indexOf("[",t);if(o===-1){let u=e.slice(t);if(u.length===0)break;if(n)return null;r.push({kind:"prop",name:u});break}let a=e.slice(t,o);if(a.length>0){if(n)return null;r.push({kind:"prop",name:a})}let s=e.indexOf("]",o+1);if(s===-1)return null;let p=e.slice(o+1,s);if(!/^[0-9]+$/.test(p))return null;r.push({kind:"index",index:Number(p)}),n=!0,t=s+1}return r.length>0?r:null}import{z as ae}from"zod";var W=ae.string().min(1),yi=(e,r)=>ae.discriminatedUnion("ok",[ae.object({ok:ae.literal(!0),value:e}),ae.object({ok:ae.literal(!1),error:r})]),l=e=>({ok:!0,value:e}),re=e=>({ok:!1,error:e}),ie=e=>e.ok,Z=e=>!e.ok;function f(e,r){switch(e.kind){case"lit":return l(e.value);case"get":return ir(e.path,r);case"eq":return B(e.left,e.right,r,(t,n)=>t===n);case"neq":return B(e.left,e.right,r,(t,n)=>t!==n);case"gt":return B(e.left,e.right,r,(t,n)=>y(t)>y(n));case"gte":return B(e.left,e.right,r,(t,n)=>y(t)>=y(n));case"lt":return B(e.left,e.right,r,(t,n)=>y(t)<y(n));case"lte":return B(e.left,e.right,r,(t,n)=>y(t)<=y(n));case"and":return sr(e.args,r);case"or":return pr(e.args,r);case"not":return lr(e.arg,r);case"if":return ur(e,r);case"add":return B(e.left,e.right,r,(t,n)=>y(t)+y(n));case"sub":return B(e.left,e.right,r,(t,n)=>y(t)-y(n));case"mul":return B(e.left,e.right,r,(t,n)=>y(t)*y(n));case"div":return cr(e.left,e.right,r);case"mod":return fr(e.left,e.right,r);case"min":return dr(e.args,r);case"max":return mr(e.args,r);case"abs":return yr(e.arg,r);case"neg":return xr(e.arg,r);case"floor":return Ur(e.arg,r);case"ceil":return Zr(e.arg,r);case"round":return Lr(e.arg,r);case"sqrt":return _r(e.arg,r);case"pow":return Hr(e.base,e.exponent,r);case"sumArray":return Wr(e.array,r);case"minArray":return Br(e.array,r);case"maxArray":return Kr(e.array,r);case"concat":return hr(e.args,r);case"substring":return Er(e,r);case"trim":return gr(e.str,r);case"toLowerCase":return Gr(e.str,r);case"toUpperCase":return Yr(e.str,r);case"strLen":return Jr(e.str,r);case"startsWith":return Xr(e.str,e.prefix,r);case"endsWith":return Qr(e.str,e.suffix,r);case"strIncludes":return en(e.str,e.search,r);case"indexOf":return tn(e.str,e.search,r);case"replace":return rn(e.str,e.search,e.replacement,r);case"split":return nn(e.str,e.delimiter,r);case"len":return kr(e.arg,r);case"at":return Sr(e.array,e.index,r);case"first":return br(e.array,r);case"last":return vr(e.array,r);case"slice":return Nr(e,r);case"includes":return Rr(e.array,e.item,r);case"filter":return wr(e.array,e.predicate,r);case"map":return Tr(e.array,e.mapper,r);case"find":return jr(e.array,e.predicate,r);case"every":return Ar(e.array,e.predicate,r);case"some":return zr(e.array,e.predicate,r);case"append":return Cr(e.array,e.items,r);case"reverse":return on(e.array,r);case"unique":return an(e.array,r);case"flat":return sn(e.array,r);case"object":return Pr(e.fields,r);case"field":return Fr(e.object,e.property,r);case"keys":return Ir(e.obj,r);case"values":return Dr(e.obj,r);case"entries":return Or(e.obj,r);case"merge":return $r(e.objects,r);case"hasKey":return pn(e.obj,e.key,r);case"pick":return ln(e.obj,e.keys,r);case"omit":return un(e.obj,e.keys,r);case"fromEntries":return cn(e.entries,r);case"typeof":return Vr(e.arg,r);case"isNull":return qr(e.arg,r);case"coalesce":return Mr(e.args,r);case"toString":return fn(e.arg,r);case"toNumber":return dn(e.arg,r);case"toBoolean":return mn(e.arg,r);default:return re(v("INTERNAL_ERROR",`Unknown expression kind: ${e.kind}`,r.currentAction??"",r.nodePath,r.trace.timestamp))}}function y(e){return typeof e=="number"?e:typeof e=="string"?parseFloat(e)||0:typeof e=="boolean"&&e?1:0}function K(e){return e==null?!1:typeof e=="boolean"?e:typeof e=="number"?e!==0:typeof e=="string"?e.length>0:!0}function T(e){return e==null?"":typeof e=="string"?e:String(e)}function ar(e,r){let t=`${e}-${r}`,n=0;for(let d=0;d<t.length;d++){let h=t.charCodeAt(d);n=(n<<5)-n+h,n=n&n}let o=Math.abs(n).toString(16).padStart(8,"0"),a=Math.abs(n*31).toString(16).padStart(4,"0"),s=Math.abs(n*37).toString(16).padStart(4,"0"),p=Math.abs(n*41).toString(16).padStart(4,"0"),u=Math.abs(n*43).toString(16).padStart(12,"0");return`${o.slice(0,8)}-${a.slice(0,4)}-4${s.slice(1,4)}-${p.slice(0,4)}-${u.slice(0,12)}`}function ir(e,r){if(e.startsWith("$item")){if(r.$item===void 0)return l(null);if(e==="$item")return l(r.$item);let t=e.slice(6);return l(Q(V(r.$item,t)))}if(e==="$index")return l(r.$index);if(e==="$array")return l(r.$array);if(e.startsWith("$system.")){let t=e.slice(8);if(t==="uuid"){let n=r.intentId??"no-intent",o=r.uuidCounter??0;return r.uuidCounter!==void 0&&(r.uuidCounter=o+1),l(ar(n,o))}return t==="timestamp"?l(new Date(r.snapshot.meta.timestamp).toISOString()):l(null)}if(e.startsWith("meta.")){let t=e.slice(5);return t==="intentId"?l(Q(r.intentId)):t==="actionName"?l(Q(r.currentAction)):l(Q(V(r.snapshot.meta,t)))}if(e.startsWith("input.")||e==="input"){let t=e==="input"?"":e.slice(6);return l(t?Q(V(r.snapshot.input,t)):r.snapshot.input)}if(Object.prototype.hasOwnProperty.call(r.schema.computed.fields,e))return l(Q(r.snapshot.computed[e]));if(e.startsWith("system.")){let t=e.slice(7);return l(Q(V(r.snapshot.system,t)))}return l(Q(V(r.snapshot.data,e)))}function Q(e){return e===void 0?null:e}function B(e,r,t,n){let o=f(e,t);if(!o.ok)return o;let a=f(r,t);return a.ok?l(n(o.value,a.value)):a}function sr(e,r){for(let t of e){let n=f(t,r);if(!n.ok)return n;if(!K(n.value))return l(!1)}return l(!0)}function pr(e,r){for(let t of e){let n=f(t,r);if(!n.ok)return n;if(K(n.value))return l(!0)}return l(!1)}function lr(e,r){let t=f(e,r);return t.ok?l(!K(t.value)):t}function ur(e,r){let t=f(e.cond,r);return t.ok?f(K(t.value)?e.then:e.else,r):t}function cr(e,r,t){let n=f(e,t);if(!n.ok)return n;let o=f(r,t);if(!o.ok)return o;let a=y(o.value);return a===0?l(null):l(y(n.value)/a)}function fr(e,r,t){let n=f(e,t);if(!n.ok)return n;let o=f(r,t);if(!o.ok)return o;let a=y(o.value);return a===0?l(null):l(y(n.value)%a)}function dr(e,r){if(e.length===0)return l(null);let t=[];for(let n of e){let o=f(n,r);if(!o.ok)return o;t.push(y(o.value))}return l(Math.min(...t))}function mr(e,r){if(e.length===0)return l(null);let t=[];for(let n of e){let o=f(n,r);if(!o.ok)return o;t.push(y(o.value))}return l(Math.max(...t))}function yr(e,r){let t=f(e,r);return t.ok?l(Math.abs(y(t.value))):t}function xr(e,r){let t=f(e,r);return t.ok?l(-y(t.value)):t}function hr(e,r){let t=[];for(let a of e){let s=f(a,r);if(!s.ok)return s;t.push(s.value)}if(t.some(a=>Array.isArray(a))){let a=[];for(let s of t)Array.isArray(s)?a.push(...s):s!=null&&a.push(s);return l(a)}let o=t.map(a=>T(a));return l(o.join(""))}function Er(e,r){let t=f(e.str,r);if(!t.ok)return t;let n=f(e.start,r);if(!n.ok)return n;let o=T(t.value),a=y(n.value);if(e.end){let s=f(e.end,r);return s.ok?l(o.substring(a,y(s.value))):s}return l(o.substring(a))}function gr(e,r){let t=f(e,r);return t.ok?l(T(t.value).trim()):t}function kr(e,r){let t=f(e,r);if(!t.ok)return t;let n=t.value;return Array.isArray(n)?l(n.length):typeof n=="string"?l(n.length):typeof n=="object"&&n!==null?l(Object.keys(n).length):l(0)}function Sr(e,r,t){let n=f(e,t);if(!n.ok)return n;let o=f(r,t);if(!o.ok)return o;let a=n.value,s=o.value;if(Array.isArray(a)){let p=y(s);return p<0||p>=a.length?l(null):l(a[p])}return typeof a=="object"&&a!==null&&typeof s=="string"?l(a[s]??null):l(null)}function br(e,r){let t=f(e,r);if(!t.ok)return t;let n=t.value;return!Array.isArray(n)||n.length===0?l(null):l(n[0])}function vr(e,r){let t=f(e,r);if(!t.ok)return t;let n=t.value;return!Array.isArray(n)||n.length===0?l(null):l(n[n.length-1])}function Nr(e,r){let t=f(e.array,r);if(!t.ok)return t;let n=f(e.start,r);if(!n.ok)return n;let o=t.value;if(!Array.isArray(o))return l([]);let a=y(n.value);if(e.end){let s=f(e.end,r);return s.ok?l(o.slice(a,y(s.value))):s}return l(o.slice(a))}function Rr(e,r,t){let n=f(e,t);if(!n.ok)return n;let o=f(r,t);if(!o.ok)return o;let a=n.value;return Array.isArray(a)?l(a.includes(o.value)):l(!1)}function wr(e,r,t){let n=f(e,t);if(!n.ok)return n;let o=n.value;if(!Array.isArray(o))return l([]);let a=[];for(let s=0;s<o.length;s++){let p=X(t,o[s],s,o),u=f(r,p);if(!u.ok)return u;K(u.value)&&a.push(o[s])}return l(a)}function Tr(e,r,t){let n=f(e,t);if(!n.ok)return n;let o=n.value;if(!Array.isArray(o))return l([]);let a=[];for(let s=0;s<o.length;s++){let p=X(t,o[s],s,o),u=f(r,p);if(!u.ok)return u;a.push(u.value)}return l(a)}function jr(e,r,t){let n=f(e,t);if(!n.ok)return n;let o=n.value;if(!Array.isArray(o))return l(null);for(let a=0;a<o.length;a++){let s=X(t,o[a],a,o),p=f(r,s);if(!p.ok)return p;if(K(p.value))return l(o[a])}return l(null)}function Ar(e,r,t){let n=f(e,t);if(!n.ok)return n;let o=n.value;if(!Array.isArray(o))return l(!0);for(let a=0;a<o.length;a++){let s=X(t,o[a],a,o),p=f(r,s);if(!p.ok)return p;if(!K(p.value))return l(!1)}return l(!0)}function zr(e,r,t){let n=f(e,t);if(!n.ok)return n;let o=n.value;if(!Array.isArray(o))return l(!1);for(let a=0;a<o.length;a++){let s=X(t,o[a],a,o),p=f(r,s);if(!p.ok)return p;if(K(p.value))return l(!0)}return l(!1)}function Cr(e,r,t){let n=f(e,t);if(!n.ok)return n;let o=n.value,a=Array.isArray(o)?[...o]:[];for(let s of r){let p=f(s,t);if(!p.ok)return p;a.push(p.value)}return l(a)}function Pr(e,r){let t={};for(let n of Object.keys(e).sort(oe)){let o=e[n],a=f(o,r);if(!a.ok)return a;t[n]=a.value}return l(t)}function Fr(e,r,t){let n=f(e,t);if(!n.ok)return n;let o=n.value;return typeof o!="object"||o===null||Array.isArray(o)?l(null):l(o[r]??null)}function Ir(e,r){let t=f(e,r);if(!t.ok)return t;let n=t.value;return typeof n!="object"||n===null?l([]):l(Object.keys(n).sort(oe))}function Dr(e,r){let t=f(e,r);if(!t.ok)return t;let n=t.value;return typeof n!="object"||n===null?l([]):l(Object.keys(n).sort(oe).map(o=>n[o]))}function Or(e,r){let t=f(e,r);if(!t.ok)return t;let n=t.value;return typeof n!="object"||n===null?l([]):l(Object.keys(n).sort(oe).map(o=>[o,n[o]]))}function $r(e,r){let t={};for(let n of e){let o=f(n,r);if(!o.ok)return o;let a=o.value;typeof a=="object"&&a!==null&&!Array.isArray(a)&&Object.assign(t,a)}return l(t)}function Vr(e,r){let t=f(e,r);if(!t.ok)return t;let n=t.value;return n===null?l("null"):Array.isArray(n)?l("array"):l(typeof n)}function qr(e,r){let t=f(e,r);return t.ok?l(t.value===null||t.value===void 0):t}function Mr(e,r){for(let t of e){let n=f(t,r);if(!n.ok||n.value!==null&&n.value!==void 0)return n}return l(null)}function Ur(e,r){let t=f(e,r);return t.ok?l(Math.floor(y(t.value))):t}function Zr(e,r){let t=f(e,r);return t.ok?l(Math.ceil(y(t.value))):t}function Lr(e,r){let t=f(e,r);return t.ok?l(Math.round(y(t.value))):t}function _r(e,r){let t=f(e,r);if(!t.ok)return t;let n=y(t.value);return n<0?l(null):l(Math.sqrt(n))}function Hr(e,r,t){let n=f(e,t);if(!n.ok)return n;let o=f(r,t);if(!o.ok)return o;let a=Math.pow(y(n.value),y(o.value));return Number.isFinite(a)?l(a):l(null)}function Wr(e,r){let t=f(e,r);if(!t.ok)return t;let n=t.value;if(!Array.isArray(n))return l(0);let o=0;for(let a of n)o+=y(a);return l(o)}function Br(e,r){let t=f(e,r);if(!t.ok)return t;let n=t.value;if(!Array.isArray(n)||n.length===0)return l(null);let o=y(n[0]);for(let a=1;a<n.length;a++){let s=y(n[a]);s<o&&(o=s)}return l(o)}function Kr(e,r){let t=f(e,r);if(!t.ok)return t;let n=t.value;if(!Array.isArray(n)||n.length===0)return l(null);let o=y(n[0]);for(let a=1;a<n.length;a++){let s=y(n[a]);s>o&&(o=s)}return l(o)}function Gr(e,r){let t=f(e,r);return t.ok?l(T(t.value).toLowerCase()):t}function Yr(e,r){let t=f(e,r);return t.ok?l(T(t.value).toUpperCase()):t}function Jr(e,r){let t=f(e,r);return t.ok?l(T(t.value).length):t}function Xr(e,r,t){let n=f(e,t);if(!n.ok)return n;let o=f(r,t);return o.ok?l(T(n.value).startsWith(T(o.value))):o}function Qr(e,r,t){let n=f(e,t);if(!n.ok)return n;let o=f(r,t);return o.ok?l(T(n.value).endsWith(T(o.value))):o}function en(e,r,t){let n=f(e,t);if(!n.ok)return n;let o=f(r,t);return o.ok?l(T(n.value).includes(T(o.value))):o}function tn(e,r,t){let n=f(e,t);if(!n.ok)return n;let o=f(r,t);return o.ok?l(T(n.value).indexOf(T(o.value))):o}function rn(e,r,t,n){let o=f(e,n);if(!o.ok)return o;let a=f(r,n);if(!a.ok)return a;let s=f(t,n);return s.ok?l(T(o.value).replace(T(a.value),T(s.value))):s}function nn(e,r,t){let n=f(e,t);if(!n.ok)return n;let o=f(r,t);if(!o.ok)return o;let a=T(n.value).split(T(o.value));return l(a.length===0?[""]:a)}function on(e,r){let t=f(e,r);if(!t.ok)return t;let n=t.value;return Array.isArray(n)?l([...n].reverse()):l([])}function an(e,r){let t=f(e,r);if(!t.ok)return t;let n=t.value;if(!Array.isArray(n))return l([]);let o=[],a=[];for(let s of n)o.some(p=>p===s)||(o.push(s),a.push(s));return l(a)}function sn(e,r){let t=f(e,r);if(!t.ok)return t;let n=t.value;if(!Array.isArray(n))return l([]);let o=[];for(let a of n)Array.isArray(a)?o.push(...a):o.push(a);return l(o)}function pn(e,r,t){let n=f(e,t);if(!n.ok)return n;let o=f(r,t);if(!o.ok)return o;let a=n.value;if(typeof a!="object"||a===null||Array.isArray(a))return l(!1);let s=o.value;return typeof s!="string"?l(!1):l(Object.prototype.hasOwnProperty.call(a,s))}function ln(e,r,t){let n=f(e,t);if(!n.ok)return n;let o=f(r,t);if(!o.ok)return o;let a=n.value;if(typeof a!="object"||a===null||Array.isArray(a))return l({});let s=o.value;if(!Array.isArray(s))return l({});let p={};for(let u of s)typeof u=="string"&&Object.prototype.hasOwnProperty.call(a,u)&&(p[u]=a[u]);return l(p)}function un(e,r,t){let n=f(e,t);if(!n.ok)return n;let o=f(r,t);if(!o.ok)return o;let a=n.value;if(typeof a!="object"||a===null||Array.isArray(a))return l({});let s=o.value,p=new Set;if(Array.isArray(s))for(let d of s)typeof d=="string"&&p.add(d);let u={};for(let[d,h]of Object.entries(a))p.has(d)||(u[d]=h);return l(u)}function cn(e,r){let t=f(e,r);if(!t.ok)return t;let n=t.value;if(!Array.isArray(n))return l({});let o={};for(let a of n)Array.isArray(a)&&a.length===2&&typeof a[0]=="string"&&(o[a[0]]=a[1]);return l(o)}function fn(e,r){let t=f(e,r);return t.ok?l(T(t.value)):t}function dn(e,r){let t=f(e,r);return t.ok?l(y(t.value)):t}function mn(e,r){let t=f(e,r);return t.ok?l(K(t.value)):t}function yn(e){return/^\d+$/.test(e)}function xn(e,r){let t=L(e,r);return t?t.kind==="primitive"&&t.type==="null"||t.kind==="literal"&&t.value===null:!1}function ut(e,r){let t=L(e,r);if(!t)return null;if(t.kind!=="union")return t;let n=t.types.filter(a=>!xn(a,r));return n.length!==t.types.length?n.length!==1?null:L(n[0],r):t}function L(e,r,t=[]){if(e.kind!=="ref")return e;if(t.includes(e.name))return null;let n=r[e.name];return n?L(n.definition,r,[...t,e.name]):null}function ct(e,r,t){return t?dt(e,r,D(t))!==null:!0}function ft(e,r,t){return dt(e,r,t)!==null}function dt(e,r,t){let n=e;for(let o of t){if(!n)return null;let a=ut(n,r);if(!a)return null;switch(a.kind){case"object":n=a.fields[o]?.type??null;break;case"record":n=a.value;break;case"array":if(!yn(o))return null;n=a.element;break;case"primitive":return null;default:return null}}return n}function ve(e,r,t){let n=e;for(let o of t){if(!n)return null;let a=ut(n,r);if(!a)return null;switch(a.kind){case"object":if(o.kind!=="prop")return null;n=a.fields[o.name]?.type??null;break;case"record":if(o.kind!=="prop")return null;n=a.value;break;case"array":if(o.kind!=="index")return null;n=a.element;break;case"primitive":return null;default:return null}}return n}function Ne(e,r,t){if(!e.fieldTypes||t.length===0)return null;let[n,...o]=t;if(!n||n.kind!=="prop"||!(n.name in e.fields))return null;let a=e.fieldTypes[n.name];return a?o.length===0?a:ve(a,r,o):null}function $(e,r,t,n){if(e===void 0)return n?.allowUndefined?{ok:!0}:{ok:!1,message:"Value is required"};let o=L(r,t);if(!o)return{ok:!1,message:"Unknown type reference"};if(o.kind==="union")return o.types.map(s=>$(e,s,t,n)).find(s=>s.ok)??{ok:!1,message:"Value does not match any union branch"};switch(o.kind){case"primitive":switch(o.type){case"null":return e===null?{ok:!0}:{ok:!1,message:"Expected null"};case"string":return typeof e=="string"?{ok:!0}:{ok:!1,message:"Expected string"};case"number":return typeof e=="number"&&Number.isFinite(e)?{ok:!0}:{ok:!1,message:"Expected number"};case"boolean":return typeof e=="boolean"?{ok:!0}:{ok:!1,message:"Expected boolean"};case"object":return e!==null&&!Array.isArray(e)&&typeof e=="object"?{ok:!0}:{ok:!1,message:"Expected object"};case"array":return Array.isArray(e)?{ok:!0}:{ok:!1,message:"Expected array"};default:return{ok:!1,message:`Unsupported primitive type: ${o.type}`}}case"literal":return Object.is(e,o.value)?{ok:!0}:{ok:!1,message:`Expected literal ${JSON.stringify(o.value)}`};case"array":if(!Array.isArray(e))return{ok:!1,message:"Expected array"};for(let s of e){let p=$(s,o.element,t,{allowUndefined:!1});if(!p.ok)return p}return{ok:!0};case"object":return e===null||Array.isArray(e)||typeof e!="object"?{ok:!1,message:"Expected object"}:hn(e,o.fields,t,n?.allowPartial??!1,n?.allowUndefined??!1);case"record":if(e===null||Array.isArray(e)||typeof e!="object")return{ok:!1,message:"Expected object"};let a=L(o.key,t);if(!a||a.kind!=="primitive"||a.type!=="string")return{ok:!1,message:"Record keys must be strings"};for(let s of Object.values(e)){let p=$(s,o.value,t,{allowUndefined:!1});if(!p.ok)return p}return{ok:!0};case"ref":return{ok:!1,message:"Unresolved type reference"}}}function hn(e,r,t,n,o){let a=new Set(Object.keys(r));for(let s of Object.keys(e))if(!a.has(s))return{ok:!1,message:`Unknown field: ${s}`};for(let[s,p]of Object.entries(r)){if(!(s in e)){if(!n&&!p.optional)return{ok:!1,message:`Missing required field: ${s}`};continue}let u=$(e[s],p.type,t,{allowPartial:n,allowUndefined:o});if(!u.ok)return u}return{ok:!0}}var En=/^\d+\.\d+\.\d+(?:-[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/,gn=/^[a-zA-Z][a-zA-Z0-9+.-]*:/,kn=/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;function mt(e){return gn.test(e)||kn.test(e)}function yt(e){return En.test(e)}function G(e){let r=[],t=n=>{switch(n.kind){case"lit":return;case"get":r.push(n.path);return;case"eq":case"neq":case"gt":case"gte":case"lt":case"lte":case"add":case"sub":case"mul":case"div":case"mod":t(n.left),t(n.right);return;case"pow":t(n.base),t(n.exponent);return;case"and":case"or":case"concat":case"coalesce":case"min":case"max":n.args.forEach(t);return;case"not":case"typeof":case"isNull":case"len":case"abs":case"neg":case"floor":case"ceil":case"round":case"sqrt":case"toString":case"toNumber":case"toBoolean":t(n.arg);return;case"first":case"last":case"reverse":case"unique":case"flat":case"sumArray":case"minArray":case"maxArray":t(n.array);return;case"if":t(n.cond),t(n.then),t(n.else);return;case"substring":t(n.str),t(n.start),n.end&&t(n.end);return;case"trim":case"toLowerCase":case"toUpperCase":case"strLen":t(n.str);return;case"startsWith":t(n.str),t(n.prefix);return;case"endsWith":t(n.str),t(n.suffix);return;case"strIncludes":case"indexOf":t(n.str),t(n.search);return;case"replace":t(n.str),t(n.search),t(n.replacement);return;case"split":t(n.str),t(n.delimiter);return;case"at":t(n.array),t(n.index);return;case"slice":t(n.array),t(n.start),n.end&&t(n.end);return;case"includes":t(n.array),t(n.item);return;case"filter":case"find":case"every":case"some":t(n.array),t(n.predicate);return;case"map":t(n.array),t(n.mapper);return;case"append":t(n.array),n.items.forEach(t);return;case"object":Object.values(n.fields).forEach(t);return;case"field":t(n.object);return;case"keys":case"values":case"entries":t(n.obj);return;case"merge":n.objects.forEach(t);return;case"hasKey":t(n.obj),t(n.key);return;case"pick":case"omit":t(n.obj),t(n.keys);return;case"fromEntries":t(n.entries);return}};return t(e),r}function xt(e){let r=[],t=n=>{switch(n.kind){case"seq":n.steps.forEach(t);return;case"if":r.push(...G(n.cond)),t(n.then),n.else&&t(n.else);return;case"patch":n.op!=="unset"&&n.value&&r.push(...G(n.value));return;case"effect":Object.values(n.params).forEach(o=>{r.push(...G(o))});return;case"fail":n.message&&r.push(...G(n.message));return;case"call":case"halt":return}};return t(e),r}function fe(e,r,t={}){if(e.fieldTypes){if(!r)return!0;let n=D(r),[o,...a]=n;if(!o)return!0;if(!(o in e.fields))return!1;let s=e.fieldTypes[o];if(s&&(a.length===0?!0:ft(s,t,a)))return!0}return Ue({type:"object",required:!0,fields:e.fields},r)}function de(e,r){return Object.prototype.hasOwnProperty.call(e.fields,r)}function Ue(e,r){if(!r)return!0;let t=D(r);if(t.length===0)return!0;let n=e;for(let o of t){if(!n)return!1;let a=n.type;if(a==="object"){if(!n.fields)return!0;if(!(o in n.fields))return!1;n=n.fields[o];continue}if(a==="array"){if(!bn(o))return!1;n=n.items??null;continue}return!1}return!0}function Y(e,r,t){if(e===void 0)return t?.allowUndefined&&r.required===!1?{ok:!0}:{ok:!1,message:"Value is required"};if(e===null)return{ok:!0};let n=r.type;if(typeof n=="object"&&"enum"in n)return n.enum.some(o=>Object.is(o,e))?{ok:!0}:{ok:!1,message:"Value is not in enum"};switch(n){case"null":return e===null?{ok:!0}:{ok:!1,message:"Expected null"};case"string":return typeof e=="string"?{ok:!0}:{ok:!1,message:"Expected string"};case"number":return typeof e=="number"&&Number.isFinite(e)?{ok:!0}:{ok:!1,message:"Expected number"};case"boolean":return typeof e=="boolean"?{ok:!0}:{ok:!1,message:"Expected boolean"};case"object":return e===null||Array.isArray(e)||typeof e!="object"?{ok:!1,message:"Expected object"}:Sn(e,r,t?.allowPartial??!1,t?.allowUndefined??!1);case"array":if(!Array.isArray(e))return{ok:!1,message:"Expected array"};if(!r.items)return{ok:!0};for(let o of e){let a=Y(o,r.items,{allowUndefined:!1});if(!a.ok)return a}return{ok:!0}}}function Sn(e,r,t,n){if(!r.fields)return{ok:!0};let o=Object.entries(r.fields),a=new Set(o.map(([s])=>s));for(let s of Object.keys(e)){if(!a.has(s))return{ok:!1,message:`Unknown field: ${s}`};let p=r.fields[s],u=Y(e[s],p,{allowPartial:t,allowUndefined:n});if(!u.ok)return u}if(!t){for(let[s,p]of o)if(p.required&&!(s in e))return{ok:!1,message:`Missing required field: ${s}`}}return{ok:!0}}function Re(e,r){if(r.length===0)return e;let t=e;for(let n of r){if(!t)return null;let o=t.type;if(o==="object"){if(n.kind!=="prop"||!t.fields||!(n.name in t.fields))return null;t=t.fields[n.name];continue}if(o==="array"){if(n.kind!=="index")return null;t=t.items??null;continue}return null}return t}function bn(e){return/^[0-9]+$/.test(e)}function ht(e){return{snapshot:e,status:"running",patches:[],requirements:[],error:null}}function Et(e,r){let t=e.snapshot.data;switch(r.op){case"set":t=ce(t,r.path,r.value);break;case"unset":t=Se(t,r.path);break;case"merge":t=be(t,r.path,r.value);break}return{...e,snapshot:{...e.snapshot,data:t},patches:[...e.patches,r]}}function vn(e,r){return{...e,status:"pending",requirements:[...e.requirements,r]}}function P(e,r){return{...e,status:"error",error:r}}function se(e,r,t,n){if(t.status!=="running")return{state:t,trace:g(r.trace,"flow",n,{},null,[])};switch(e.kind){case"seq":return Nn(e.steps,r,t,n);case"if":return Rn(e,r,t,n);case"patch":return wn(e,r,t,n);case"effect":return Tn(e,r,t,n);case"call":return zn(e.flow,r,t,n);case"halt":return Cn(e.reason,r,t,n);case"fail":return Pn(e,r,t,n);default:return{state:P(t,v("INTERNAL_ERROR",`Unknown flow kind: ${e.kind}`,r.currentAction??"",n,r.trace.timestamp)),trace:g(r.trace,"error",n,{},null,[])}}}async function $i(e,r,t,n){return se(e,r,t,n)}function Nn(e,r,t,n){let o=t,a=[];for(let s=0;s<e.length;s++){let p=`${n}.steps[${s}]`,u=ge($e(r,o.snapshot),p),d=se(e[s],u,o,p);if(a.push(d.trace),o=d.state,o.status!=="running")break}return{state:o,trace:g(r.trace,"flow",n,{kind:"seq"},null,a)}}function Rn(e,r,t,n){let o=f(e.cond,r);if(!o.ok)return{state:P(t,o.error),trace:g(r.trace,"branch",n,{cond:!1},null,[])};let a=o.value,s=a!=null&&a!==!1,p=s?`${n}.then`:`${n}.else`,u=s?e.then:e.else;if(!u)return{state:t,trace:g(r.trace,"branch",n,{cond:s},null,[])};let d=ge(r,p),h=se(u,d,t,p);return{state:h.state,trace:g(r.trace,"branch",n,{cond:s},null,[h.trace])}}function wn(e,r,t,n){let o;if(e.op!=="unset"&&e.value){let b=f(e.value,r);if(!b.ok)return{state:P(t,b.error),trace:g(r.trace,"error",n,{},null,[])};o=b.value}let a={type:"object",required:!0,fields:r.schema.state.fields},s=Ne(r.schema.state,r.schema.types,e.path),p=s?null:Re(a,e.path),u=ke(e.path);if(!s&&!p)return{state:P(t,v("PATH_NOT_FOUND",`Unknown patch path: ${u}`,r.currentAction??"",n,r.trace.timestamp)),trace:g(r.trace,"error",n,{},null,[])};if(e.op!=="unset"){let b=s?$(o,s,r.schema.types,{allowPartial:e.op==="merge",allowUndefined:!1}):Y(o,p,{allowPartial:e.op==="merge",allowUndefined:!1});if(!b.ok)return{state:P(t,v("TYPE_MISMATCH",`Invalid patch value at ${u}: ${b.message??"type mismatch"}`,r.currentAction??"",n,r.trace.timestamp)),trace:g(r.trace,"error",n,{},null,[])}}let d=e.op==="unset"?{op:"unset",path:e.path}:e.op==="merge"?{op:"merge",path:e.path,value:o}:{op:"set",path:e.path,value:o};return{state:Et(t,d),trace:g(r.trace,"patch",n,{op:e.op,path:u},o,[])}}function Tn(e,r,t,n){if(e.type==="array.map"||e.type==="array.filter")return jn(e,r,t,n);let o={};for(let[u,d]of Object.entries(e.params)){let h=f(d,r);if(!h.ok)return{state:P(t,h.error),trace:g(r.trace,"error",n,{},null,[])};o[u]=h.value}let s={id:rt(r.snapshot.meta.schemaHash,r.intentId??"",r.currentAction??"",n),type:e.type,params:o,actionId:r.currentAction??"",flowPosition:{nodePath:n,snapshotVersion:r.snapshot.meta.version},createdAt:r.trace.timestamp};return{state:vn(t,s),trace:g(r.trace,"effect",n,{type:e.type},o,[])}}function jn(e,r,t,n){let{params:o}=e,a=$e(r,t.snapshot),s=o.source;if(!s)return{state:P(t,v("INVALID_INPUT",`${e.type} requires 'source' parameter`,r.currentAction??"",n,r.trace.timestamp)),trace:g(a.trace,"error",n,{},null,[])};let p=f(s,a);if(!p.ok)return{state:P(t,p.error),trace:g(a.trace,"error",n,{},null,[])};let u=p.value;if(!Array.isArray(u))return{state:P(t,v("TYPE_MISMATCH",`${e.type} source must be an array`,a.currentAction??"",n,a.trace.timestamp)),trace:g(a.trace,"error",n,{},null,[])};let d=o.into;if(!d)return{state:P(t,v("INVALID_INPUT",`${e.type} requires 'into' parameter`,a.currentAction??"",n,a.trace.timestamp)),trace:g(a.trace,"error",n,{},null,[])};let h=f(d,a);if(!h.ok)return{state:P(t,h.error),trace:g(a.trace,"error",n,{},null,[])};let b=An(h.value);if(!b)return{state:P(t,v("INVALID_INPUT",`${e.type} into must resolve to PatchPath segments or semantic string path`,a.currentAction??"",n,a.trace.timestamp)),trace:g(a.trace,"error",n,{},null,[])};let m=e.type==="array.map"?o.select:o.where;if(!m)return{state:P(t,v("INVALID_INPUT",`${e.type} requires '${e.type==="array.map"?"select":"where"}' parameter`,a.currentAction??"",n,a.trace.timestamp)),trace:g(a.trace,"error",n,{},null,[])};let E=[];for(let F=0;F<u.length;F++){let A=u[F],q=X(a,A,F,u),H=f(m,q);if(!H.ok)return{state:P(t,H.error),trace:g(a.trace,"error",n,{},null,[])};if(e.type==="array.map")E.push(H.value);else{let J=H.value;J!=null&&J!==!1&&E.push(A)}}return{state:Et(t,{op:"set",path:b,value:E}),trace:g(a.trace,"effect",n,{type:e.type,target:b},{count:E.length},[])}}function An(e){if(typeof e=="string")return pt(e);if(!Array.isArray(e))return null;let r=[];for(let t of e){if(!t||typeof t!="object")return null;let n=t.kind;if(n==="prop"){let o=t.name;if(typeof o!="string"||o.length===0)return null;r.push({kind:"prop",name:o});continue}if(n==="index"){let o=t.index;if(!Number.isInteger(o)||o<0)return null;r.push({kind:"index",index:o});continue}return null}return r.length>0?r:null}function zn(e,r,t,n){let o=r.schema.actions[e];if(!o)return{state:P(t,v("UNKNOWN_FLOW",`Unknown flow: ${e}`,r.currentAction??"",n,r.trace.timestamp)),trace:g(r.trace,"error",n,{},null,[])};let a=`${n}.call(${e})`,s=ge(r,a),p=se(o.flow,s,t,a);return{state:p.state,trace:g(r.trace,"call",n,{flow:e},null,[p.trace])}}function Cn(e,r,t,n){return{state:{...t,status:"halted"},trace:g(r.trace,"halt",n,{reason:e},null,[])}}function Pn(e,r,t,n){let o=e.code;if(e.message){let s=f(e.message,r);s.ok&&(o=String(s.value))}let a=v("VALIDATION_ERROR",o,r.currentAction??"",n,r.trace.timestamp,{code:e.code});return{state:P(t,a),trace:g(r.trace,"error",n,{code:e.code},o,[])}}function we(e){let r=Object.keys(e.fields),t=new Map;for(let[n,o]of Object.entries(e.fields)){let a=o.deps.filter(s=>s in e.fields);t.set(n,a)}return{nodes:r,edges:t}}function Te(e){let r=new Map,t=new Map;for(let a of e.nodes)r.set(a,0),t.set(a,[]);for(let[a,s]of e.edges)for(let p of s)t.has(p)&&t.get(p).push(a),r.set(a,(r.get(a)??0)+1);let n=[];for(let[a,s]of r)s===0&&n.push(a);let o=[];for(;n.length>0;){let a=n.shift();o.push(a);for(let s of t.get(a)??[]){let p=(r.get(s)??1)-1;r.set(s,p),p===0&&n.push(s)}}if(o.length!==e.nodes.length){let a=e.nodes.filter(s=>!o.includes(s));return re({code:"V-002",message:`Cyclic dependency detected in computed fields: ${a.join(", ")}`,path:a[0]})}return l(o)}function gt(e){let r=new Set,t=new Set,n=[];function o(a,s){r.add(a),t.add(a);let p=e.edges.get(a)??[];for(let u of p)if(r.has(u)){if(t.has(u)){let d=s.indexOf(u);d!==-1?n.push([...s.slice(d),u]):n.push([...s,u])}}else if(o(u,[...s,u]))return!0;return t.delete(a),!1}for(let a of e.nodes)r.has(a)||o(a,[a]);return n.length>0?n:null}function Mi(e,r){let t=new Set,n=[...e.edges.get(r)??[]];for(;n.length>0;){let o=n.shift();t.has(o)||(t.add(o),n.push(...e.edges.get(o)??[]))}return t}function pe(e,r){let t=te(r.meta.timestamp),n=we(e.computed),o=Te(n);if(!o.ok)return re(v("CYCLIC_DEPENDENCY",o.error.message,"",o.error.path??"",t.timestamp));let a={},s={...r,computed:a};for(let p of o.value){let u=e.computed.fields[p];if(!u)continue;let d=U(s,e,null,p,void 0,t),h=f(u.expr,d);if(Z(h))return h;a[p]=h.value,s={...s,computed:{...a}}}return l(a)}function Ki(e,r,t){let n=te(r.meta.timestamp),o=e.computed.fields[t];if(!o)return re(v("PATH_NOT_FOUND",`Computed field not found: ${t}`,"",t,n.timestamp));let a=U(r,e,null,t,void 0,n);return f(o.expr,a)}function Ze(e,r){let t=pe(e,r);return Z(t)?{kind:"error",code:"INTERNAL_ERROR",message:`Error evaluating computed values for legality query: ${t.error.message}`}:{kind:"ok",snapshot:{...r,computed:t.value}}}function Le(e,r,t,n){let o=e.actions[t];if(!o)return{kind:"error",code:"UNKNOWN_ACTION",message:`Unknown action: ${t}`};if(!o.available)return{kind:"ok",available:!0};let a=U(r,e,null,`actions.${t}.available`,void 0,n),s=f(o.available,a);return Z(s)?{kind:"error",code:"INTERNAL_ERROR",message:`Error evaluating availability: ${s.error.message}`}:typeof s.value!="boolean"?{kind:"error",code:"TYPE_MISMATCH",message:`Availability condition must return boolean, got ${typeof s.value}`}:{kind:"ok",available:s.value}}function _e(e,r,t,n=r.meta.timestamp){let o=Ze(e,r);return o.kind==="error"?o:Le(e,o.snapshot,t,n)}function kt(e,r,t){let n=_e(e,r,t);if(n.kind==="error")throw new Error(n.message);return n.available}function Fn(e,r,t,n=r.meta.timestamp){let o=e.actions[t.type];if(!o)return{kind:"error",code:"UNKNOWN_ACTION",message:`Unknown action: ${t.type}`};let a=Ze(e,r);if(a.kind==="error")return a;let s=Le(e,a.snapshot,t.type,n);if(s.kind==="error")return s;if(!s.available)return{kind:"ok",dispatchable:!1};if(!o.dispatchable)return{kind:"ok",dispatchable:!0};let p=U({...a.snapshot,input:t.input},e,t.type,`actions.${t.type}.dispatchable`,t.intentId,n),u=f(o.dispatchable,p);return Z(u)?{kind:"error",code:"INTERNAL_ERROR",message:`Error evaluating dispatchability: ${u.error.message}`}:typeof u.value!="boolean"?{kind:"error",code:"TYPE_MISMATCH",message:`Dispatchability condition must return boolean, got ${typeof u.value}`}:{kind:"ok",dispatchable:u.value}}function St(e,r,t){let n=Fn(e,r,t);if(n.kind==="error")throw new Error(n.message);return n.dispatchable}function bt(e,r){let t=Ze(e,r);if(t.kind==="error")throw new Error(t.message);return Object.keys(e.actions).filter(n=>{let o=Le(e,t.snapshot,n,r.meta.timestamp);if(o.kind==="error")throw new Error(o.message);return o.available})}function je(e,r){let t=He(r,"status"),n=He(r,"currentAction"),o=He(r,"lastError"),a=new Set(r.removeRequirementIds??[]),s=r.addRequirements??[],p=In(e.system.pendingRequirements,a,s),u={...e.system,status:t?r.status:e.system.status,currentAction:n?r.currentAction??null:e.system.currentAction,lastError:o?r.lastError??null:e.system.lastError,pendingRequirements:p};return Dn(e.system,u)?{...e,system:u,meta:{...e.meta,version:e.meta.version+1}}:e}function In(e,r,t){let n=new Set(t.map(a=>a.id));return[...e.filter(a=>!r.has(a.id)&&!n.has(a.id)),...t]}function Dn(e,r){return e.status!==r.status||e.currentAction!==r.currentAction||!$n(e.lastError,r.lastError)||!On(e.pendingRequirements,r.pendingRequirements)}function On(e,r){if(e.length!==r.length)return!1;for(let t=0;t<e.length;t++)if(!Vn(e[t],r[t]))return!1;return!0}function $n(e,r){if(e===r)return!0;if(e===null||r===null||e.code!==r.code||e.message!==r.message||e.timestamp!==r.timestamp||e.source.actionId!==r.source.actionId||e.source.nodePath!==r.source.nodePath)return!1;let t=e.context??{},n=r.context??{};return JSON.stringify(t)===JSON.stringify(n)}function Vn(e,r){return e.id===r.id&&e.type===r.type&&e.actionId===r.actionId&&e.createdAt===r.createdAt&&e.flowPosition.nodePath===r.flowPosition.nodePath&&e.flowPosition.snapshotVersion===r.flowPosition.snapshotVersion&&JSON.stringify(e.params)===JSON.stringify(r.params)}function He(e,r){return Object.prototype.hasOwnProperty.call(e,r)}function We(e,r,t,n){let o=r,a=pe(e,r);ie(a)&&(o={...r,computed:a.value});let s=e.actions[t.type];if(!s)return Ae(o,t,"UNKNOWN_ACTION",`Unknown action: ${t.type}`,n);let p=Nt(e,t);if(p)return Ae(o,t,"INVALID_INPUT",p,n);let u=o.system.currentAction===t.type;if(s.available&&!u){let A=_e(e,o,t.type,n.now);if(A.kind==="error")return Ae(o,t,A.code,A.message,n);if(!A.available)return Ae(o,t,"ACTION_UNAVAILABLE",`Action "${t.type}" is not available`,n)}let d={...o,input:t.input,system:{...o.system,status:"computing",currentAction:t.type}},h=U(d,e,t.type,`actions.${t.type}.flow`,t.intentId,n.now),b=ht(d),m=se(s.flow,h,b,`actions.${t.type}.flow`),E=qn(m.state.status),C=Zn(o,t,E,m.state.error,m.state.requirements),I=[...m.state.patches],F={root:m.trace,nodes:Un(m.trace),intent:{type:t.type,input:t.input},baseVersion:o.meta.version,resultVersion:Rt(o,I,C),duration:n.durationMs??0,terminatedBy:Mn(m.state.status)};return{patches:I,systemDelta:C,trace:F,status:E}}async function vt(e,r,t,n){return We(e,r,t,n)}function Nt(e,r){if(!r.intentId||r.intentId==="")return"Intent must have a non-empty intentId";let t=e.actions[r.type];return!t||!t.input&&!t.inputType?null:Ln(e,t.inputType,t.input,r.input)}function qn(e){switch(e){case"running":case"complete":return"complete";case"pending":return"pending";case"halted":return"halted";case"error":return"error"}}function Mn(e){switch(e){case"running":case"complete":return"complete";case"pending":return"effect";case"halted":return"halt";case"error":return"error"}}function Un(e){let r={};function t(n){r[n.id]=n;for(let o of n.children)t(o)}return t(e),r}function Ae(e,r,t,n,o){let a=v(t,n,r.type,"",o.now),s={status:"error",currentAction:null,lastError:a,addRequirements:[],removeRequirementIds:[]},p={root:{id:`trace-error-${r.intentId}`,kind:"error",sourcePath:"",inputs:{},output:a,children:[],timestamp:o.now},nodes:{},intent:{type:r.type,input:r.input},baseVersion:e.meta.version,resultVersion:Rt(e,[],s),duration:o.durationMs??0,terminatedBy:"error"};return{patches:[],systemDelta:s,trace:p,status:"error"}}function Zn(e,r,t,n,o){let a=t==="error";return{status:t==="pending"?"pending":t==="error"?"error":"idle",currentAction:t==="pending"?r.type:null,lastError:n,addRequirements:[...o],removeRequirementIds:e.system.pendingRequirements.map(p=>p.id)}}function Rt(e,r,t){let n=e.meta.version;return n+=1,je(e,t)!==e&&(n+=1),n}function Ln(e,r,t,n){if(r){let o=$(n,r,e.types);return o.ok?null:o.message??"Invalid input"}if(!t)return null;if(t.type==="object"){if(typeof n!="object"||n===null||Array.isArray(n))return`Expected object input, got ${typeof n}`;let o=n,a=t.fields??{};for(let[s,p]of Object.entries(a))if(p.required&&!(s in o))return`Missing required field: ${s}`;for(let s of Object.keys(o))if(!(s in a))return`Unknown field: ${s}`;for(let[s,p]of Object.entries(a))if(s in o){let u=_n(p,o[s],s);if(u)return u}}return null}function _n(e,r,t){if(r==null)return e.required?`Missing required field: ${t}`:null;switch(e.type){case"string":if(typeof r!="string")return`Expected string for ${t}, got ${typeof r}`;break;case"number":if(typeof r!="number")return`Expected number for ${t}, got ${typeof r}`;break;case"boolean":if(typeof r!="boolean")return`Expected boolean for ${t}, got ${typeof r}`;break;case"array":if(!Array.isArray(r))return`Expected array for ${t}, got ${typeof r}`;break;case"object":if(typeof r!="object"||Array.isArray(r))return`Expected object for ${t}, got ${typeof r}`;break}return null}var Hn={type:"object",required:!1};function At(e,r,t,n){let o=r.data,a=r.system,s=r.input,p=[],u={type:"object",required:!0,fields:e.state.fields};for(let m of t){let E=ke(m.path);if(!lt(m.path)){p.push(v("PATH_NOT_FOUND",`Unsafe patch path: ${E}`,r.system.currentAction??"",E,n.now,{patch:m}));continue}if(Wn(m.path)){if(m.op!=="unset"){if(m.path.length===1){let A=Y(m.value,Hn,{allowPartial:m.op==="merge",allowUndefined:!1});if(!A.ok){p.push(v("TYPE_MISMATCH",`Invalid patch value at ${E}: ${A.message??"type mismatch"}`,r.system.currentAction??"",E,n.now,{patch:m}));continue}}if(m.op==="merge"&&!Tt(o,m.path)){p.push(v("TYPE_MISMATCH",`Invalid merge target at ${E}: target path must be an object or absent`,r.system.currentAction??"",E,n.now,{patch:m}));continue}}o=jt(o,m);continue}let I=Ne(e.state,e.types,m.path),F=I?null:Re(u,m.path);if(!I&&!F){p.push(v("PATH_NOT_FOUND",`Unknown patch path: ${E}`,r.system.currentAction??"",E,n.now,{patch:m}));continue}if(m.op==="merge"&&!Tt(o,m.path)){p.push(v("TYPE_MISMATCH",`Invalid merge target at ${E}: target path must be an object or absent`,r.system.currentAction??"",E,n.now,{patch:m}));continue}if(m.op!=="unset"){let A=I?$(m.value,I,e.types,{allowPartial:m.op==="merge",allowUndefined:!1}):Y(m.value,F,{allowPartial:m.op==="merge",allowUndefined:!1});if(!A.ok){p.push(v("TYPE_MISMATCH",`Invalid patch value at ${E}: ${A.message??"type mismatch"}`,r.system.currentAction??"",E,n.now,{patch:m}));continue}}o=jt(o,m)}if(p.length>0){let m=p[p.length-1];a={...a,status:"error",lastError:m}}let d={...r,data:o,system:a,input:s},h=pe(e,d),b=r.computed;if(ie(h))b=h.value;else if(Z(h)){let m=h.error;b={},a={...a,status:"error",lastError:m}}return{data:o,computed:b,system:a,input:s,meta:{...r.meta,version:r.meta.version+1,timestamp:n.now,randomSeed:n.randomSeed}}}function Wn(e){let r=e[0];return r.kind!=="prop"?null:r.name.startsWith("$")?r.name:null}function wt(e){return e!==null&&!Array.isArray(e)&&typeof e=="object"}function Tt(e,r){let t=e;for(let n of r){if(t===void 0)return!0;if(n.kind==="prop"){if(!wt(t))return!1;t=t[n.name];continue}if(!Array.isArray(t))return!1;t=t[n.index]}return t===void 0?!0:wt(t)}function jt(e,r){switch(r.op){case"set":return ce(e,r.path,r.value);case"unset":return Se(e,r.path);case"merge":return be(e,r.path,r.value)}}import{z as O}from"zod";import{z}from"zod";import{z as k}from"zod";var _=k.lazy(()=>k.discriminatedUnion("kind",[k.object({kind:k.literal("primitive"),type:k.string()}),k.object({kind:k.literal("array"),element:_}),k.object({kind:k.literal("record"),key:_,value:_}),k.object({kind:k.literal("object"),fields:k.record(k.string(),k.object({type:_,optional:k.boolean()}))}),k.object({kind:k.literal("union"),types:k.array(_).min(1)}),k.object({kind:k.literal("literal"),value:k.union([k.string(),k.number(),k.boolean(),k.null()])}),k.object({kind:k.literal("ref"),name:k.string()})])),zt=k.object({name:k.string(),definition:_});var Bn=z.enum(["string","number","boolean","null","object","array"]),Kn=z.object({enum:z.array(z.unknown()).readonly()}),Gn=z.union([Bn,Kn]),me=z.lazy(()=>z.object({type:Gn,required:z.boolean(),default:z.unknown().optional(),description:z.string().optional(),fields:z.record(z.string(),me).optional(),items:me.optional()})),Ct=z.object({fields:z.record(z.string(),me),fieldTypes:z.record(z.string(),_).optional()});import{z as ye}from"zod";import{z as i}from"zod";var Yn=i.object({kind:i.literal("lit"),value:i.unknown()}),Jn=i.object({kind:i.literal("get"),path:W}),Xn=i.object({kind:i.literal("eq"),left:i.lazy(()=>c),right:i.lazy(()=>c)}),Qn=i.object({kind:i.literal("neq"),left:i.lazy(()=>c),right:i.lazy(()=>c)}),eo=i.object({kind:i.literal("gt"),left:i.lazy(()=>c),right:i.lazy(()=>c)}),to=i.object({kind:i.literal("gte"),left:i.lazy(()=>c),right:i.lazy(()=>c)}),ro=i.object({kind:i.literal("lt"),left:i.lazy(()=>c),right:i.lazy(()=>c)}),no=i.object({kind:i.literal("lte"),left:i.lazy(()=>c),right:i.lazy(()=>c)}),oo=i.object({kind:i.literal("and"),args:i.array(i.lazy(()=>c))}),ao=i.object({kind:i.literal("or"),args:i.array(i.lazy(()=>c))}),io=i.object({kind:i.literal("not"),arg:i.lazy(()=>c)}),so=i.object({kind:i.literal("if"),cond:i.lazy(()=>c),then:i.lazy(()=>c),else:i.lazy(()=>c)}),po=i.object({kind:i.literal("add"),left:i.lazy(()=>c),right:i.lazy(()=>c)}),lo=i.object({kind:i.literal("sub"),left:i.lazy(()=>c),right:i.lazy(()=>c)}),uo=i.object({kind:i.literal("mul"),left:i.lazy(()=>c),right:i.lazy(()=>c)}),co=i.object({kind:i.literal("div"),left:i.lazy(()=>c),right:i.lazy(()=>c)}),fo=i.object({kind:i.literal("mod"),left:i.lazy(()=>c),right:i.lazy(()=>c)}),mo=i.object({kind:i.literal("min"),args:i.array(i.lazy(()=>c))}),yo=i.object({kind:i.literal("max"),args:i.array(i.lazy(()=>c))}),xo=i.object({kind:i.literal("abs"),arg:i.lazy(()=>c)}),ho=i.object({kind:i.literal("neg"),arg:i.lazy(()=>c)}),Eo=i.object({kind:i.literal("floor"),arg:i.lazy(()=>c)}),go=i.object({kind:i.literal("ceil"),arg:i.lazy(()=>c)}),ko=i.object({kind:i.literal("round"),arg:i.lazy(()=>c)}),So=i.object({kind:i.literal("sqrt"),arg:i.lazy(()=>c)}),bo=i.object({kind:i.literal("pow"),base:i.lazy(()=>c),exponent:i.lazy(()=>c)}),vo=i.object({kind:i.literal("sumArray"),array:i.lazy(()=>c)}),No=i.object({kind:i.literal("minArray"),array:i.lazy(()=>c)}),Ro=i.object({kind:i.literal("maxArray"),array:i.lazy(()=>c)}),wo=i.object({kind:i.literal("concat"),args:i.array(i.lazy(()=>c))}),To=i.object({kind:i.literal("substring"),str:i.lazy(()=>c),start:i.lazy(()=>c),end:i.lazy(()=>c).optional()}),jo=i.object({kind:i.literal("trim"),str:i.lazy(()=>c)}),Ao=i.object({kind:i.literal("toLowerCase"),str:i.lazy(()=>c)}),zo=i.object({kind:i.literal("toUpperCase"),str:i.lazy(()=>c)}),Co=i.object({kind:i.literal("strLen"),str:i.lazy(()=>c)}),Po=i.object({kind:i.literal("startsWith"),str:i.lazy(()=>c),prefix:i.lazy(()=>c)}),Fo=i.object({kind:i.literal("endsWith"),str:i.lazy(()=>c),suffix:i.lazy(()=>c)}),Io=i.object({kind:i.literal("strIncludes"),str:i.lazy(()=>c),search:i.lazy(()=>c)}),Do=i.object({kind:i.literal("indexOf"),str:i.lazy(()=>c),search:i.lazy(()=>c)}),Oo=i.object({kind:i.literal("replace"),str:i.lazy(()=>c),search:i.lazy(()=>c),replacement:i.lazy(()=>c)}),$o=i.object({kind:i.literal("split"),str:i.lazy(()=>c),delimiter:i.lazy(()=>c)}),Vo=i.object({kind:i.literal("len"),arg:i.lazy(()=>c)}),qo=i.object({kind:i.literal("at"),array:i.lazy(()=>c),index:i.lazy(()=>c)}),Mo=i.object({kind:i.literal("first"),array:i.lazy(()=>c)}),Uo=i.object({kind:i.literal("last"),array:i.lazy(()=>c)}),Zo=i.object({kind:i.literal("slice"),array:i.lazy(()=>c),start:i.lazy(()=>c),end:i.lazy(()=>c).optional()}),Lo=i.object({kind:i.literal("includes"),array:i.lazy(()=>c),item:i.lazy(()=>c)}),_o=i.object({kind:i.literal("filter"),array:i.lazy(()=>c),predicate:i.lazy(()=>c)}),Ho=i.object({kind:i.literal("map"),array:i.lazy(()=>c),mapper:i.lazy(()=>c)}),Wo=i.object({kind:i.literal("find"),array:i.lazy(()=>c),predicate:i.lazy(()=>c)}),Bo=i.object({kind:i.literal("every"),array:i.lazy(()=>c),predicate:i.lazy(()=>c)}),Ko=i.object({kind:i.literal("some"),array:i.lazy(()=>c),predicate:i.lazy(()=>c)}),Go=i.object({kind:i.literal("append"),array:i.lazy(()=>c),items:i.array(i.lazy(()=>c))}),Yo=i.object({kind:i.literal("reverse"),array:i.lazy(()=>c)}),Jo=i.object({kind:i.literal("unique"),array:i.lazy(()=>c)}),Xo=i.object({kind:i.literal("flat"),array:i.lazy(()=>c)}),Qo=i.object({kind:i.literal("object"),fields:i.record(i.string(),i.lazy(()=>c))}),ea=i.object({kind:i.literal("field"),object:i.lazy(()=>c),property:i.string()}),ta=i.object({kind:i.literal("keys"),obj:i.lazy(()=>c)}),ra=i.object({kind:i.literal("values"),obj:i.lazy(()=>c)}),na=i.object({kind:i.literal("entries"),obj:i.lazy(()=>c)}),oa=i.object({kind:i.literal("merge"),objects:i.array(i.lazy(()=>c))}),aa=i.object({kind:i.literal("hasKey"),obj:i.lazy(()=>c),key:i.lazy(()=>c)}),ia=i.object({kind:i.literal("pick"),obj:i.lazy(()=>c),keys:i.lazy(()=>c)}),sa=i.object({kind:i.literal("omit"),obj:i.lazy(()=>c),keys:i.lazy(()=>c)}),pa=i.object({kind:i.literal("fromEntries"),entries:i.lazy(()=>c)}),la=i.object({kind:i.literal("typeof"),arg:i.lazy(()=>c)}),ua=i.object({kind:i.literal("isNull"),arg:i.lazy(()=>c)}),ca=i.object({kind:i.literal("coalesce"),args:i.array(i.lazy(()=>c))}),fa=i.object({kind:i.literal("toString"),arg:i.lazy(()=>c)}),da=i.object({kind:i.literal("toNumber"),arg:i.lazy(()=>c)}),ma=i.object({kind:i.literal("toBoolean"),arg:i.lazy(()=>c)}),c=i.union([Yn,Jn,Xn,Qn,eo,to,ro,no,oo,ao,io,so,po,lo,uo,co,fo,mo,yo,xo,ho,Eo,go,ko,So,bo,vo,No,Ro,wo,To,jo,Ao,zo,Co,Po,Fo,Io,Do,Oo,$o,Vo,qo,Mo,Uo,Zo,Lo,_o,Ho,Wo,Bo,Ko,Go,Yo,Jo,Xo,Qo,ea,ta,ra,na,oa,aa,ia,sa,pa,la,ua,ca,fa,da,ma]),Rs=i.enum(["lit","get","eq","neq","gt","gte","lt","lte","and","or","not","if","add","sub","mul","div","mod","min","max","abs","neg","floor","ceil","round","sqrt","pow","sumArray","minArray","maxArray","concat","substring","trim","toLowerCase","toUpperCase","strLen","startsWith","endsWith","strIncludes","indexOf","replace","split","len","at","first","last","slice","includes","filter","map","find","every","some","append","reverse","unique","flat","object","field","keys","values","entries","merge","hasKey","pick","omit","fromEntries","typeof","isNull","coalesce","toString","toNumber","toBoolean"]);var ya=ye.object({deps:ye.array(W),expr:c,description:ye.string().optional()}),Pt=ye.object({fields:ye.record(W,ya)});import{z as ze}from"zod";import{z as S}from"zod";import{z as N}from"zod";var xa=N.discriminatedUnion("kind",[N.object({kind:N.literal("prop"),name:N.string().min(1)}),N.object({kind:N.literal("index"),index:N.number().int().nonnegative()})]),xe=N.array(xa).min(1),Ft=N.discriminatedUnion("op",[N.object({op:N.literal("set"),path:xe,value:N.unknown()}),N.object({op:N.literal("unset"),path:xe}),N.object({op:N.literal("merge"),path:xe,value:N.record(N.string(),N.unknown())})]),Ps=N.object({type:N.string(),input:N.unknown().optional(),intentId:N.string()});function Fs(e){return{kind:"prop",name:e}}function Is(e){return{kind:"index",index:e}}function Ds(e,r){return{op:"set",path:e,value:r}}function Os(e){return{op:"unset",path:e}}function $s(e,r){return{op:"merge",path:e,value:r}}var ha=S.enum(["set","unset","merge"]),Ea=S.object({kind:S.literal("seq"),steps:S.array(S.lazy(()=>he))}),ga=S.object({kind:S.literal("if"),cond:c,then:S.lazy(()=>he),else:S.lazy(()=>he).optional()}),ka=S.object({kind:S.literal("patch"),op:ha,path:xe,value:c.optional()}),Sa=S.object({kind:S.literal("effect"),type:S.string(),params:S.record(S.string(),c)}),ba=S.object({kind:S.literal("call"),flow:S.string()}),va=S.object({kind:S.literal("halt"),reason:S.string().optional()}),Na=S.object({kind:S.literal("fail"),code:S.string(),message:c.optional()}),he=S.union([Ea,ga,ka,Sa,ba,va,Na]),Zs=S.enum(["seq","if","patch","effect","call","halt","fail"]);var It=ze.object({flow:he,input:me.optional(),inputType:_.optional(),params:ze.array(ze.string()).readonly().optional(),available:c.optional(),dispatchable:c.optional(),description:ze.string().optional()});var Ra=O.object({name:O.string().optional(),description:O.string().optional(),authors:O.array(O.string()).optional()}),Dt=O.object({id:O.string(),version:O.string(),hash:O.string(),types:O.record(O.string(),zt),state:Ct,computed:Pt,actions:O.record(O.string(),It),meta:Ra.optional()});import{z as j}from"zod";import{z as x}from"zod";var Be=x.object({code:x.string(),message:x.string(),source:x.object({actionId:x.string(),nodePath:x.string()}),timestamp:x.number(),context:x.record(x.string(),x.unknown()).optional()}),wa=x.object({nodePath:x.string(),snapshotVersion:x.number()}),Ke=x.object({id:x.string(),type:x.string(),params:x.record(x.string(),x.unknown()),actionId:x.string(),flowPosition:wa,createdAt:x.number()}),Ge=x.object({status:x.enum(["idle","computing","pending","error"]),lastError:Be.nullable(),pendingRequirements:x.array(Ke),currentAction:x.string().nullable()}),Ta=x.object({version:x.number(),timestamp:x.number(),randomSeed:x.string(),schemaHash:x.string()}),op=x.object({data:x.unknown(),computed:x.record(W,x.unknown()),system:Ge,input:x.unknown(),meta:Ta});function Ot(){return{status:"idle",lastError:null,pendingRequirements:[],currentAction:null}}var ja=j.enum(["complete","pending","halted","error"]),Aa=j.object({status:Ge.shape.status.optional(),currentAction:j.string().nullable().optional(),lastError:Be.nullable().optional(),addRequirements:j.array(Ke),removeRequirementIds:j.array(j.string())}),cp=j.object({patches:j.array(Ft),systemDelta:Aa,trace:ot,status:ja}),za=j.object({code:j.string(),message:j.string(),path:j.string().optional()}),fp=j.object({valid:j.boolean(),errors:j.array(za)}),dp=j.object({value:j.unknown(),trace:ue,deps:j.array(W)});function $t(){return{valid:!0,errors:[]}}function Ye(e){return{valid:!1,errors:e}}function qt(e){let r=[],t=Dt.safeParse(e);if(!t.success){let b=t.error.issues.map(m=>({code:"SCHEMA_ERROR",message:m.message,path:m.path.map(String).join(".")}));return Ye(b)}let n=t.data;mt(n.id)||r.push({code:"SCHEMA_ERROR",message:"Schema id must be a valid URI or UUID",path:"id"}),yt(n.version)||r.push({code:"SCHEMA_ERROR",message:"Schema version must follow Semantic Versioning 2.0",path:"version"});let o=n.hash;if(!o)r.push({code:"SCHEMA_ERROR",message:"Schema hash is required",path:"hash"});else{let b=e,{hash:m,...E}=b,C=Oe(E);o!==C&&r.push({code:"V-008",message:`Schema hash mismatch: expected ${C}, got ${o}`,path:"hash"})}Object.keys(n.state.fields).length===0&&r.push({code:"SCHEMA_ERROR",message:"StateSpec.fields must not be empty",path:"state.fields"}),Object.keys(n.computed.fields).length===0&&r.push({code:"SCHEMA_ERROR",message:"ComputedSpec.fields must not be empty",path:"computed.fields"}),Object.keys(n.actions).length===0&&r.push({code:"SCHEMA_ERROR",message:"actions must not be empty",path:"actions"}),r.push(...Fa(n)),r.push(...Da(n,"state.fields")),r.push(...Oa(n)),r.push(...$a(n)),r.push(...Va(n)),r.push(...qa(n));let a=we(n.computed),s=Te(a);Z(s)&&r.push(s.error);let p=gt(a);if(p)for(let b of p)r.push({code:"V-002",message:`Cyclic dependency: ${b.join(" -> ")}`,path:b[0]});let u=new Set(Object.keys(n.actions)),d=Ca(n,u);r.push(...d);let h=Pa(n);return r.push(...h),r.length>0?Ye(r):$t()}function Ca(e,r){let t=[];for(let[n,o]of Object.entries(e.actions)){let a=Ee(o.flow);for(let s of a)r.has(s)||t.push({code:"V-004",message:`Unknown flow reference: "${s}" in action "${n}"`,path:`actions.${n}`})}return t}function Ee(e){let r=[];switch(e.kind){case"call":r.push(e.flow);break;case"seq":for(let t of e.steps)r.push(...Ee(t));break;case"if":r.push(...Ee(e.then)),e.else&&r.push(...Ee(e.else));break}return r}function Pa(e){let r=[],t=new Map;for(let[s,p]of Object.entries(e.actions))t.set(s,Ee(p.flow));let n=new Set,o=new Set;function a(s,p){n.add(s),o.add(s);let u=t.get(s)??[];for(let d of u)if(n.has(d)){if(o.has(d))return r.push({code:"V-005",message:`Cyclic call detected: ${[...p,d].join(" -> ")}`,path:`actions.${s}`}),!0}else if(a(d,[...p,d]))return!0;return o.delete(s),!1}for(let s of t.keys())n.has(s)||a(s,[s]);return r}function Fa(e){let r=[];if(e.state.fieldTypes)for(let t of Object.keys(e.state.fieldTypes))t in e.state.fields||r.push({code:"V-010",message:`state.fieldTypes.${t} has no matching state.fields entry`,path:`state.fieldTypes.${t}`}),r.push(...ee(e.state.fieldTypes[t],e.types,`state.fieldTypes.${t}`));for(let[t,n]of Object.entries(e.actions)){if(!n.inputType){n.params&&n.params.length>0&&r.push(...Vt(t,n,e.types));continue}r.push(...ee(n.inputType,e.types,`actions.${t}.inputType`)),n.params&&n.params.length>0&&r.push(...Vt(t,n,e.types))}return r}function Vt(e,r,t){let n=r.params??[];if(n.length===0)return[];let o=[],a=new Set;for(let[u,d]of n.entries()){if(a.has(d)){o.push({code:"V-010",message:`Duplicate parameter name "${d}" is not allowed`,path:`actions.${e}.params.${u}`});continue}a.add(d)}let s=null;if(r.inputType?s=Je(r.inputType,t):r.input?.type==="object"&&r.input.fields&&(s=Object.keys(r.input.fields)),!s)return[...o,{code:"V-010",message:`actions.${e}.params requires an object-shaped input carrier`,path:`actions.${e}.params`}];let p=new Set(s);return[...o,...n.flatMap((u,d)=>p.has(u)?[]:[{code:"V-010",message:`Parameter "${u}" has no matching input field`,path:`actions.${e}.params.${d}`}])]}function Je(e,r,t=[]){if(e.kind==="ref"){if(t.includes(e.name))return null;let n=r[e.name];return n?Je(n.definition,r,[...t,e.name]):null}if(e.kind==="union"){let n=e.types.filter(o=>!Ia(o,r,t));return n.length===1?Je(n[0],r,t):null}return e.kind==="object"?Object.keys(e.fields):null}function Ia(e,r,t=[]){let n=L(e,r,t);return n?n.kind==="primitive"&&n.type==="null"||n.kind==="literal"&&n.value===null:!1}function ee(e,r,t,n=[]){let o=[];if(e.kind==="ref"){if(n.includes(e.name))return o.push({code:"V-010",message:`Cyclic type reference "${e.name}" in typing seam`,path:t}),o;let a=r[e.name];return!a||!L(e,r)?(o.push({code:"V-010",message:`Unknown type reference "${e.name}" in typing seam`,path:t}),o):(o.push(...ee(a.definition,r,t,[...n,e.name])),o)}switch(e.kind){case"array":return ee(e.element,r,`${t}.element`,n);case"record":{let a=[...ee(e.key,r,`${t}.key`,n),...ee(e.value,r,`${t}.value`,n)],s=L(e.key,r);return s&&(s.kind!=="primitive"||s.type!=="string")&&a.push({code:"V-010",message:"Record typing seams require string keys",path:`${t}.key`}),a}case"object":for(let[a,s]of Object.entries(e.fields))o.push(...ee(s.type,r,`${t}.fields.${a}.type`,n));return o;case"union":for(let[a,s]of e.types.entries())o.push(...ee(s,r,`${t}.types.${a}`,n));return o;default:return o}}function Da(e,r){let t=[],n=e.state,o=(a,s,p)=>{if(!a.required&&a.default===void 0&&t.push({code:"SCHEMA_ERROR",message:"Optional fields must define a default value",path:s}),a.default!==void 0){if(p){let u=$(a.default,p,e.types);u.ok||t.push({code:"V-009",message:`Default value type mismatch: ${u.message}`,path:s})}else if(a.default===null&&a.required!==!1)t.push({code:"V-009",message:`Default value 'null' is not compatible with required field type '${typeof a.type=="string"?a.type:"enum"}'`,path:s});else if(a.default!==null){let u=Y(a.default,a);u.ok||t.push({code:"V-009",message:`Default value type mismatch: ${u.message}`,path:s})}}if(a.type==="object"&&a.fields)for(let[u,d]of Object.entries(a.fields)){let h=p?ve(p,e.types,[{kind:"prop",name:u}]):null;o(d,`${s}.${u}`,h??void 0)}if(a.type==="array"&&a.items){let u=p?ve(p,e.types,[{kind:"index",index:0}]):null;o(a.items,`${s}[]`,u??void 0)}};for(let[a,s]of Object.entries(n.fields))o(s,`${r}.${a}`,n.fieldTypes?.[a]);return t}function Oa(e){let r=[];for(let[t,n]of Object.entries(e.computed.fields))for(let o of n.deps)de(e.computed,o)||fe(e.state,o,e.types)||r.push({code:"V-001",message:`Unknown dependency path: ${o}`,path:`computed.fields.${t}`});return r}function $a(e){let r=[];for(let[t,n]of Object.entries(e.computed.fields)){let a=G(n.expr).filter(s=>s.startsWith("$")||de(e.computed,s)?!1:s.startsWith("input.")||s.startsWith("system.")?!0:!fe(e.state,s,e.types));for(let s of a)r.push({code:"V-003",message:`Unknown path in computed expression: ${s}`,path:`computed.fields.${t}`})}return r}function Va(e){let r=[];for(let[t,n]of Object.entries(e.computed.fields)){let o=G(n.expr),a=new Set(n.deps),s=new Set(o.filter(p=>p.startsWith("$")||p==="input"||p.startsWith("input.")||p==="system"||p.startsWith("system.")||p==="meta"||p.startsWith("meta.")?!1:de(e.computed,p)?!0:fe(e.state,p,e.types)));for(let p of s)a.has(p)||r.push({code:"V-001",message:`Missing dependency for computed expression path: ${p}`,path:`computed.fields.${t}`})}return r}function qa(e){let r=[];for(let[t,n]of Object.entries(e.actions)){let o=[...xt(n.flow),...n.available?G(n.available):[],...n.dispatchable?G(n.dispatchable):[]];for(let a of o)if(!a.startsWith("$")){if(a==="input"||a.startsWith("input.")){if(n.inputType){let s=a==="input"?"":a.slice(6);ct(n.inputType,e.types,s)||r.push({code:"V-003",message:`Unknown input path: ${a}`,path:`actions.${t}`})}else if(n.input){let s=a==="input"?"":a.slice(6);Ue(n.input,s)||r.push({code:"V-003",message:`Unknown input path: ${a}`,path:`actions.${t}`})}continue}de(e.computed,a)||a.startsWith("system.")||a==="meta"||a.startsWith("meta.")||fe(e.state,a,e.types)||r.push({code:"V-003",message:`Unknown state path: ${a}`,path:`actions.${t}`})}}return r}function Mt(e,r,t){let n=te(r.meta.timestamp);return Ut(e,r,t,n)}function Ut(e,r,t,n){if(e.computed.fields[t]!==void 0)return Ma(e,r,t,n);if(t.startsWith("system.")){let a=V(r.system,t.slice(7));return{value:a,trace:g(n,"expr",t,{path:t},a,[]),deps:[]}}if(t.startsWith("input.")||t==="input"){let a=t==="input"?r.input:V(r.input,t.slice(6));return{value:a,trace:g(n,"expr",t,{path:t},a,[]),deps:[]}}let o=V(r.data,t);return{value:o,trace:g(n,"expr",t,{path:t},o,[]),deps:[]}}function Ma(e,r,t,n){let o=e.computed.fields[t];if(!o){let d=r.computed[t];return{value:d,trace:g(n,"computed",t,{path:t},d,[]),deps:[]}}let a=U(r,e,null,t,void 0,n),s=f(o.expr,a),p=ie(s)?s.value:null,u=o.deps.map(d=>Ut(e,r,d,n).trace);return{value:p,trace:g(n,"computed",t,{expr:o.expr},p,u),deps:o.deps}}function Ap(e){let r={};for(let[t,n]of Object.entries(e.fields))n.default!==void 0&&(r[t]=n.default);return r}import{z as ne}from"zod";var Pp=ne.object({now:ne.number(),randomSeed:ne.string(),env:ne.record(ne.string(),ne.unknown()).optional(),durationMs:ne.number().optional()});function sl(e,r,t){return{data:e,computed:{},system:Ot(),input:void 0,meta:{version:0,timestamp:t.now,randomSeed:t.randomSeed,schemaHash:r}}}function pl(e,r,t){return t===void 0&&typeof r=="string"?{type:e,input:void 0,intentId:r}:t===void 0?{type:e,input:r,intentId:""}:{type:e,input:r,intentId:t}}function xl(){return{compute:vt,computeSync:We,apply:At,applySystemDelta:je,validate:qt,explain:Mt,isActionAvailable:kt,getAvailableActions:bt,isIntentDispatchable:St}}export{xo as AbsExpr,It as ActionSpec,po as AddExpr,oo as AndExpr,Go as AppendExpr,qo as AtExpr,ba as CallFlow,go as CeilExpr,ca as CoalesceExpr,cp as ComputeResult,ja as ComputeStatus,ya as ComputedFieldSpec,Pt as ComputedSpec,wo as ConcatExpr,Za as CoreErrorCode,co as DivExpr,Dt as DomainSchema,Sa as EffectFlow,Fo as EndsWithExpr,na as EntriesExpr,Kn as EnumFieldType,Xn as EqExpr,Be as ErrorValue,Bo as EveryExpr,dp as ExplainResult,Rs as ExprKind,c as ExprNodeSchema,Na as FailFlow,ea as FieldExpr,me as FieldSpec,Gn as FieldType,_o as FilterExpr,Wo as FindExpr,Mo as FirstExpr,Xo as FlatExpr,Eo as FloorExpr,Zs as FlowKind,he as FlowNodeSchema,wa as FlowPosition,pa as FromEntriesExpr,Jn as GetExpr,eo as GtExpr,to as GteExpr,va as HaltFlow,aa as HasKeyExpr,Pp as HostContext,so as IfExpr,ga as IfFlow,Lo as IncludesExpr,Do as IndexOfExpr,Ps as Intent,ua as IsNullExpr,ta as KeysExpr,Uo as LastExpr,Vo as LenExpr,Yn as LitExpr,ro as LtExpr,no as LteExpr,Ho as MapExpr,Ro as MaxArrayExpr,yo as MaxExpr,oa as MergeExpr,No as MinArrayExpr,mo as MinExpr,fo as ModExpr,uo as MulExpr,ho as NegExpr,Qn as NeqExpr,io as NotExpr,Qo as ObjectExpr,sa as OmitExpr,ao as OrExpr,Ft as Patch,ka as PatchFlow,ha as PatchOp,xe as PatchPath,xa as PatchSegment,ia as PickExpr,bo as PowExpr,Bn as PrimitiveFieldType,Oo as ReplaceExpr,Ke as Requirement,yi as Result,Yo as ReverseExpr,ko as RoundExpr,Ra as SchemaMeta,W as SemanticPath,Ea as SeqFlow,Zo as SliceExpr,op as Snapshot,Ta as SnapshotMeta,Ko as SomeExpr,$o as SplitExpr,So as SqrtExpr,Po as StartsWithExpr,Ct as StateSpec,Io as StrIncludesExpr,Co as StrLenExpr,lo as SubExpr,To as SubstringExpr,vo as SumArrayExpr,Aa as SystemDelta,Ge as SystemState,ma as ToBooleanExpr,Ao as ToLowerCaseExpr,da as ToNumberExpr,fa as ToStringExpr,zo as ToUpperCaseExpr,ot as TraceGraph,ue as TraceNode,Xt as TraceNodeKind,Qt as TraceTermination,jo as TrimExpr,_ as TypeDefinition,zt as TypeSpec,la as TypeofExpr,Jo as UniqueExpr,za as ValidationError,fp as ValidationResult,ra as ValuesExpr,At as apply,je as applySystemDelta,we as buildDependencyGraph,Ba as canonicalEqual,oe as compareUnicodeCodePoints,vt as compute,We as computeSync,U as createContext,xl as createCore,v as createError,ht as createFlowState,Ot as createInitialSystemState,pl as createIntent,sl as createSnapshot,te as createTraceContext,g as createTraceNode,gt as detectCycles,re as err,pe as evaluateComputed,f as evaluateExpr,$i as evaluateFlow,se as evaluateFlowSync,Ki as evaluateSingleComputed,Mt as explain,Ap as extractDefaults,Wa as fromCanonical,Xa as generateRequirementId,rt as generateRequirementIdSync,nt as generateTraceId,bt as getAvailableActions,nr as getByPatchPath,V as getByPath,Mi as getTransitiveDeps,pi as hasPath,Jt as hashSchema,Ya as hashSchemaEffective,Ja as hashSchemaEffectiveSync,Oe as hashSchemaSync,Is as indexSegment,Ye as invalidResult,kt as isActionAvailable,Z as isErr,La as isErrorValue,St as isIntentDispatchable,ie as isOk,lt as isSafePatchPath,ai as joinPath,ui as lastSegment,be as mergeAtPatchPath,si as mergeAtPath,$s as mergePatch,l as ok,li as parentPath,D as parsePath,ke as patchPathToDisplayString,Fs as propSegment,pt as semanticPathToPatchPath,ce as setByPatchPath,er as setByPath,Ds as setPatch,et as sha256,tt as sha256Sync,Ie as sortKeys,le as toCanonical,Ha as toJcs,Te as topologicalSort,Se as unsetByPatchPath,ii as unsetByPath,Os as unsetPatch,$t as validResult,qt as validate,Nt as validateIntentInput,X as withCollectionContext,ge as withNodePath,$e as withSnapshot};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@manifesto-ai/core",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.13.0",
|
|
4
4
|
"description": "Manifesto Core - Pure semantic calculator for deterministic state computation",
|
|
5
5
|
"author": "eggplantiny <eggplantiny@gmail.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -39,8 +39,8 @@
|
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"typescript": "^5.9.3",
|
|
42
|
-
"vite": "^8.0.
|
|
43
|
-
"vitest": "^4.1.
|
|
42
|
+
"vite": "^8.0.8",
|
|
43
|
+
"vitest": "^4.1.4",
|
|
44
44
|
"zod": "^4.3.6"
|
|
45
45
|
},
|
|
46
46
|
"scripts": {
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
"dev": "tsc --watch",
|
|
49
49
|
"clean": "rm -rf dist",
|
|
50
50
|
"lint": "echo 'lint not configured'",
|
|
51
|
-
"test": "vitest run",
|
|
52
|
-
"test:coverage": "vitest run --coverage"
|
|
51
|
+
"test": "node ../../node_modules/vitest/vitest.mjs run",
|
|
52
|
+
"test:coverage": "node ../../node_modules/vitest/vitest.mjs run --coverage"
|
|
53
53
|
}
|
|
54
54
|
}
|