@blue-labs/document-processor 1.27.0 → 1.27.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/BlueDocumentProcessor.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.mjs +211 -205
- package/dist/testUtils.d.ts.map +1 -1
- package/dist/utils/checkpoint.d.ts.map +1 -1
- package/dist/utils/document.d.ts +8 -5
- package/dist/utils/document.d.ts.map +1 -1
- package/dist/utils/initialized.d.ts.map +1 -1
- package/package.json +3 -3
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BlueDocumentProcessor.d.ts","sourceRoot":"","sources":["../src/BlueDocumentProcessor.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EACjB,YAAY,EAEZ,gBAAgB,EAChB,gBAAgB,EACjB,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"BlueDocumentProcessor.d.ts","sourceRoot":"","sources":["../src/BlueDocumentProcessor.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EACjB,YAAY,EAEZ,gBAAgB,EAChB,gBAAgB,EACjB,MAAM,SAAS,CAAC;AAmBjB,OAAO,EAAE,IAAI,EAAE,MAAM,qBAAqB,CAAC;AAI3C;;;;;GAKG;AACH,qBAAa,qBAAqB;IAc9B,OAAO,CAAC,QAAQ,CAAC,IAAI;IAbvB,OAAO,CAAC,WAAW,CAAK;IACxB,OAAO,CAAC,YAAY,CAAK;IACzB,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAmB;IAC5C,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAY;IAClC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAc;IACrC,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAyB;IAEzD;;;;OAIG;gBAEgB,IAAI,EAAE,IAAI,EAC3B,UAAU,GAAE,iBAAiB,EAAsB;IAkBrD;;;;;OAKG;IACH,QAAQ,CAAC,EAAE,EAAE,iBAAiB,EAAE,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI;IAIzD;;;;;OAKG;IACG,UAAU,CAAC,QAAQ,EAAE,YAAY,GAAG,OAAO,CAAC,gBAAgB,CAAC;IAuBnE;;;;;;OAMG;IACG,aAAa,CACjB,QAAQ,EAAE,YAAY,EACtB,QAAQ,EAAE,gBAAgB,EAAE,GAC3B,OAAO,CAAC,gBAAgB,CAAC;IA8B5B;;OAEG;YACW,UAAU;CAmFzB"}
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
"use strict";var he=Object.defineProperty;var de=(c,e,t)=>e in c?he(c,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):c[e]=t;var l=(c,e,t)=>de(c,typeof e!="symbol"?e+"":e,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d=require("@blue-repository/core-dev"),f=require("@blue-labs/language"),v=require("@blue-labs/shared-utils"),P=require("@blue-repository/myos-dev"),pe=(c,e)=>({on:(t,n)=>({end:()=>n(null)})});async function me(c){return new Promise((e,t)=>{pe().on("error",t).end()})}const b=(...c)=>c.map((e,t)=>{if(typeof e!="string")return"";if(t===0&&e==="/")return"/";const n=t>0?e.replace(/^\/+/,""):e;return t<c.length-1?n.replace(/\/+$/,""):n}).filter(Boolean).join("/").replace(/\/{2,}/g,"/");class z{constructor(e,t,n,o){l(this,"actions",[]);this.getDocument=e,this.taskInfo=t,this.blue=n,this.onFlush=o}get(e){const t=this.getDocument(),n=b(this.taskInfo.nodePath,e);return t.get(n)}addPatch(e){this.actions.push({kind:"patch",patch:{...e,path:b(this.taskInfo.nodePath,e.path)}})}emitEvent(e){const t=this.taskInfo.event,n=t.trace??[],o={...e,originNodePath:e.originNodePath??this.taskInfo.nodePath,rootEvent:e.rootEvent??t.rootEvent??t,trace:[...n]};this.actions.push({kind:"event",event:o})}async flush(){var t;if(!this.actions.length)return[];const e=[...this.actions];return this.actions.length=0,await((t=this.onFlush)==null?void 0:t.call(this,e)),e}getNodePath(){return this.taskInfo.nodePath}resolvePath(e){return b(this.taskInfo.nodePath,e)}getTaskInfo(){return this.taskInfo}getBlue(){return this.blue}loadExternalModule(e){if(!/^https?:\/\//.test(e))throw new Error("Only http/https URLs are allowed");return me()}loadBlueContent(e){throw new Error("Not implemented")}}class L extends Error{constructor(e,t){super(`Cannot apply patch ${JSON.stringify(e)}`),this.patch=e,this.cause=t,this.name="PatchApplicationError"}}class _ extends Error{constructor(e,t,n){super(`Patch ${JSON.stringify(e)} touches "${e.op==="move"||e.op==="copy"?`${e.from} → ${e.path}`:e.path}" which is inside embedded document "${t}" (Process Embedded @ "${n}")`),this.patch=e,this.offendingPath=t,this.contractNodePath=n,this.name="EmbeddedDocumentModificationError"}}class $ extends Error{constructor(e,t){super(`Failed to evaluate expression "${e}"`),this.code=e,this.cause=t,this.name="ExpressionEvaluationError"}}class A extends Error{constructor(e,t){super(`Failed to evaluate code block "${e}"`),this.code=e,this.cause=t,this.name="CodeBlockEvaluationError"}}function O(c,e,t="/",n=[]){const o=c.getContracts()??{};for(const[r,a]of Object.entries(o))if(f.BlueNodeTypeSchema.isTypeOf(a,d.ProcessEmbeddedSchema)){const h=e.nodeToSchemaOutput(a,d.ProcessEmbeddedSchema).paths??[];for(const u of h)n.push({absPath:b(t,u),contractPath:b(t,`contracts/${r}`)})}for(const[r,a]of Object.entries(c.getProperties()??{}))O(a,e,b(t,r),n);return n}function D(c,e){return c===e||c.startsWith(e.endsWith("/")?e:e+"/")}function W(c,e){if(!e.length)return c;let t=c.clone();for(const n of e)try{t=f.applyBlueNodePatch(t,n,!0)}catch(o){throw new L(n,o)}return v.deepFreeze(t)}function y(c){return c!=null}function C(c){return c instanceof f.BlueNode}class fe{constructor(e=[],t=(n,o)=>n<o?-1:n>o?1:0){l(this,"data");l(this,"length");l(this,"compare");if(this.data=e,this.length=this.data.length,this.compare=t,this.length>0)for(let n=(this.length>>1)-1;n>=0;n--)this._down(n)}push(e){this.data.push(e),this._up(this.length++)}pop(){if(this.length===0)return;const e=this.data[0],t=this.data.pop();return--this.length>0&&(this.data[0]=t,this._down(0)),e}peek(){return this.data[0]}_up(e){const{data:t,compare:n}=this,o=t[e];for(;e>0;){const r=e-1>>1,a=t[r];if(n(o,a)>=0)break;t[e]=a,e=r}t[e]=o}_down(e){const{data:t,compare:n}=this,o=this.length>>1,r=t[e];for(;e<o;){let a=(e<<1)+1;const s=a+1;if(s<this.length&&n(t[s],t[a])<0&&(a=s),n(t[a],r)>=0)break;t[e]=t[a],e=a}t[e]=r}}const ye=(c,e,t,n,o,r)=>[-c,e,t,n,o,r],ge=(c,e)=>{for(let t=0;t<c.key.length;t++){const n=c.key[t],o=e.key[t];if(n!==o)return typeof n=="number"&&typeof o=="number"?n-o:typeof n=="string"&&typeof o=="string"?n.localeCompare(o):0}return 0};class Ee{constructor(){l(this,"queue");this.queue=new fe([],ge)}push(e){this.queue.push(e)}pop(){return this.queue.pop()}get length(){return this.queue.length}}class ve{constructor(e=[]){l(this,"processors",new Map);l(this,"typeOrder",new Map);e.forEach((t,n)=>this.register(t,n))}register(e,t){if(this.processors.has(e.contractBlueId))throw new Error(`Processor for ${e.contractType} already registered`);this.processors.set(e.contractBlueId,e),this.typeOrder.has(e.contractBlueId)||this.typeOrder.set(e.contractBlueId,t??this.typeOrder.size)}get(e){if(!e)return;const t=e.getBlueId();if(t)return this.processors.get(t)}orderOf(e){if(!e)return 0;const t=e.getBlueId();return t?this.typeOrder.get(t)??0:0}values(){return this.processors.values()}}class we{constructor(){l(this,"isEnabled");this.isEnabled=process.env.TRACE_BLUE_ENABLED==="true"}makeHop(e,t){return`${e}#${t}`}shouldTrace(){return this.isEnabled}addHop(e,t,n){if(!this.shouldTrace())return{...e};const o=[...e.trace??[],this.makeHop(t,n)];return{...e,trace:o}}getTrace(e){return e.trace??[]}clearTrace(e){return{...e,trace:[]}}isTracingEnabled(){return this.isEnabled}}const Se=64;class be{constructor(e,t,n,o,r){l(this,"traceManager");this.blue=e,this.registry=t,this.queue=n,this.getNextTaskId=o,this.getNextEventSeq=r,this.traceManager=new we}async route(e,t,n,o,r=0){var i;if(n.seq===void 0&&(n.seq=this.getNextEventSeq()),t.length===0){if(n.dispatchPath){const h=n.dispatchPath.split("/").filter(Boolean),u={...n};return delete u.dispatchPath,this.route(e,h,u,o,r)}if(n.source==="channel"&&n.originNodePath&&n.originNodePath!=="/"){const h=((i=n.originNodePath)==null?void 0:i.split("/").filter(Boolean))??[];return this.route(e,h,n,o,r)}}const a=b("/",t.join("/")),s=e.get(a);C(s)&&await this.traverseContracts({doc:e,node:s,nodePath:a,event:n,afterTaskId:o,pathSegments:t,inlineDepth:r})}async traverseContracts(e){const{doc:t,node:n,nodePath:o,event:r,afterTaskId:a,pathSegments:s,inlineDepth:i}=e;if(!this.shouldSkipForChannel(r,o))for(const[h,u]of Object.entries(n.getContracts()??{})){if(!u.getType())continue;const p=this.registry.get(u.getType());if(!p){console.warn(`No processor registered for contract: ${h}`);continue}const m={nodePath:o,contractName:h,contractNode:u,event:r},g=new z(()=>t,m,this.blue);if(p.supports(r,u,g,h))switch(p.role){case"adapter":await this.processAdapter({cp:p,event:r,contractNode:u,ctx:g,contractName:h,doc:t,afterTaskId:a,inlineDepth:i});break;case"handler":this.scheduleHandler({contractNode:u,contractName:h,nodePath:o,event:r,depth:s.length,afterTaskId:a});break}}}async processAdapter(e){var w;const{cp:t,event:n,contractNode:o,ctx:r,contractName:a,doc:s,afterTaskId:i,inlineDepth:h}=e;if(h>=Se)throw new Error("Adapter recursion limit reached");const u=this.traceManager.addHop(n,((w=r.getTaskInfo())==null?void 0:w.nodePath)??"",a);await t.handle(u,o,r,a);const p=await r.flush();if(p.find(T=>T.kind==="patch"))throw new Error(`Contract "${a}" (adapter) attempted to patch the document`);const g=p.filter(T=>T.kind==="event");for(const T of g)await this.route(s,[],T.event,i,h+1)}scheduleHandler(e){const{contractNode:t,contractName:n,nodePath:o,event:r,depth:a,afterTaskId:s}=e,i=t.getType();if(!i){console.warn(`Contract node type is not defined for: ${n}`);return}const h=this.registry.orderOf(i),u=t.get("/order"),p=f.isBigNumber(u)?u.toNumber():0,m=this.getNextTaskId()+s,g=ye(a,r.seq,h,p,n,m),w=this.traceManager.addHop(r,o,n);this.queue.push({key:g,nodePath:o,contractName:n,contractNode:t,event:w})}shouldSkipForChannel(e,t){return e.source==="channel"&&!!e.originNodePath&&e.originNodePath!==t}}const Te=(c,e,t)=>{(t instanceof L||t instanceof _)&&console.error(`[Blue] Failed to apply patches for contract "${c}" on event ${JSON.stringify(e)}`,t)};function j(c,e){const t=c.clone(),n=o=>{if(!C(o))return;const r=o.getContracts();(!(r!=null&&r.checkpoint)||!f.BlueNodeTypeSchema.isTypeOf(r.checkpoint,d.ChannelEventCheckpointSchema))&&o.addContract("checkpoint",e.jsonValueToNode({type:{name:"Channel Event Checkpoint",blueId:d.blueIds["Channel Event Checkpoint"]},lastEvents:{}}))};n(t);for(const{absPath:o}of O(t,e)){const r=t.get(o);C(r)&&n(r)}return v.deepFreeze(t)}function Pe(c,e){const t=c.clone();return C(t)&&(H(t,e)||t.addContract("initialized",e.jsonValueToNode({type:{name:"Initialized Marker",blueId:d.blueIds["Initialized Marker"]}}))),v.deepFreeze(t)}function H(c,e){const t=c.getContracts();return Object.values(t??{}).some(n=>e.isTypeOf(n,d.InitializedMarkerSchema,{checkSchemaExtensions:!0}))}class R{constructor(e){l(this,"contractType","Channel Event Checkpoint");l(this,"contractBlueId",d.blueIds["Channel Event Checkpoint"]);l(this,"role","handler");this.cache=e}supports(e){var t,n;return e.source==="channel"&&((t=e.rootEvent)==null?void 0:t.payload)===e.payload&&((n=e.rootEvent)==null?void 0:n.source)==="external"}async handle(e,t,n){var a;if(!e.channelName||!((a=e.rootEvent)!=null&&a.seq))return;const o=await n.getBlue().calculateBlueId(e.rootEvent.payload),r=n.getNodePath().replace(/\/contracts\/checkpoint$/,"");this.cache.record(r,e,o)}}const x=(c,e)=>c.get(e)!==void 0;class Ce{constructor(){l(this,"firstSeen",new Map)}record(e,t,n){const o=e;this.firstSeen.has(o)||this.firstSeen.set(o,{docBase:e,event:t,eventBlueId:n})}flush(e){const t=[];for(const{docBase:n,event:o,eventBlueId:r}of this.firstSeen.values()){if(!o.channelName)continue;const a=b(n,"contracts/checkpoint/lastEvents",o.channelName),s=`${a}/blueId`;x(e,a)?t.push({op:x(e,s)?"replace":"add",path:s,val:r}):t.push({op:"add",path:a,val:{blueId:r}})}return t}clear(){this.firstSeen.clear()}}class k{constructor(){l(this,"role","adapter")}baseSupports(e){return e.source!=="channel"}}class Q extends k{constructor(){super(...arguments);l(this,"contractType","Composite Timeline Channel");l(this,"contractBlueId",d.blueIds["Composite Timeline Channel"])}supports(t,n,o){const r=o.getBlue().nodeToSchemaOutput(n,d.CompositeTimelineChannelSchema);return!r.channels||!t.channelName?!1:r.channels.includes(t.channelName)}handle(t,n,o,r){o.emitEvent({payload:t.payload,channelName:r,source:"channel"})}}class K extends k{constructor(){super(...arguments);l(this,"contractType","Document Update Channel");l(this,"contractBlueId",d.blueIds["Document Update Channel"])}supports(t,n,o,r){if(!this.baseSupports(t))return!1;const a=o.getBlue().nodeToSchemaOutput(n,d.DocumentUpdateChannelSchema),s=t.payload.get("/path");if(!s||t.channelName===r)return!1;const i=a.path;return v.isNonNullable(i)&&s===o.resolvePath(i)}handle(t,n,o,r){const a=t.payload;a&&o.emitEvent({payload:a,channelName:r,source:"channel"})}}class X extends k{constructor(){super(...arguments);l(this,"contractType","Embedded Node Channel");l(this,"contractBlueId",d.blueIds["Embedded Node Channel"])}supports(t,n,o){if(!this.baseSupports(t))return!1;const r=o.getBlue().nodeToSchemaOutput(n,d.EmbeddedNodeChannelSchema);return y(t.originNodePath)&&y(r.path)&&t.originNodePath===o.resolvePath(r.path)}handle(t,n,o,r){const a=o.getBlue().nodeToSchemaOutput(n,d.EmbeddedNodeChannelSchema),{originNodePath:s,payload:i}=t;y(a.path)&&s===o.resolvePath(a.path)&&o.emitEvent({payload:i,channelName:r,source:"channel"})}}class Y{constructor(){l(this,"contractType","Initialized Marker");l(this,"contractBlueId",d.blueIds["Initialized Marker"]);l(this,"role","marker")}supports(){return!1}handle(){}}class Z extends k{constructor(){super(...arguments);l(this,"contractType","Lifecycle Event Channel");l(this,"contractBlueId",d.blueIds["Lifecycle Event Channel"])}supports(t,n,o){return!this.baseSupports(t)||!this.isLifecycleEvent(t,o)?!1:this.isEventPatternMatch(t,n,o)}handle(t,n,o,r){o.emitEvent({payload:t.payload,channelName:r,source:"channel"})}isLifecycleEvent(t,n){const o=n.getBlue(),r=t.payload;return o.isTypeOf(r,d.LifecycleEventSchema,{checkSchemaExtensions:!0})}isEventPatternMatch(t,n,o){var a;const r=(a=n.getProperties())==null?void 0:a.event;if(!r)return!0;try{const s=o.getBlue(),i=s.nodeToJson(t.payload),h=s.nodeToJson(r);return v.deepContains(i,h)}catch(s){return console.warn("Error during lifecycle event pattern matching:",s),!1}}}const J=(c,e)=>{const t=e.getBlue();return t.isTypeOf(c.payload,d.TimelineEntrySchema)||t.isTypeOf(c.payload,P.MyOSTimelineEntrySchema)};class G extends k{constructor(){super(...arguments);l(this,"contractType","MyOS Timeline Channel");l(this,"contractBlueId",P.blueIds["MyOS Timeline Channel"])}supports(t,n,o){var m;if(!this.baseSupports(t)||!J(t,o))return!1;const a=o.getBlue().nodeToSchemaOutput(t.payload,P.MyOSTimelineEntrySchema),s=o.getBlue().nodeToSchemaOutput(n,P.MyOSTimelineChannelSchema),i=(m=a.timeline)==null?void 0:m.timelineId,h=y(s.timelineId)&&y(i),u=y(s.account)&&y(a.account),p=y(s.email)&&y(a.email);return h&&i===s.timelineId||u&&a.account===s.account||p&&a.email===s.email}handle(t,n,o,r){J(t,o)&&o.emitEvent({payload:t.payload,channelName:r,source:"channel"})}}class ee extends k{constructor(){super(...arguments);l(this,"contractType","MyOS Agent Channel");l(this,"contractBlueId",P.blueIds["MyOS Agent Channel"])}supports(t,n,o){if(!this.baseSupports(t))return!1;try{const{myosAgentEvent:r,myosAgentChannel:a}=this.parseEventAndChannel(t,n,o);return this.isAgentMatch(r,a)&&this.isEventPatternMatch(r,a,o)}catch(r){return console.warn("Error in MyOSAgentChannelProcessor.supports:",r),!1}}handle(t,n,o,r){o.emitEvent({payload:t.payload,channelName:r,source:"channel"})}parseEventAndChannel(t,n,o){const r=o.getBlue(),a=r.nodeToSchemaOutput(t.payload,P.MyOSAgentEventSchema),s=r.nodeToSchemaOutput(n,P.MyOSAgentChannelSchema);return{myosAgentEvent:a,myosAgentChannel:s}}isAgentMatch(t,n){var a;const o=t.agentId,r=(a=n.agent)==null?void 0:a.agentId;return y(o)&&y(r)&&o===r}isEventPatternMatch(t,n,o){const r=n.event;if(!r)return!0;const a=t.event;if(!a)return!1;try{const s=o.getBlue(),i=s.nodeToJson(a),h=s.nodeToJson(r);return v.deepContains(i,h)}catch(s){return console.warn("Error during event pattern matching:",s),!1}}}class te{constructor(){l(this,"contractType","Operation");l(this,"contractBlueId",d.blueIds.Operation);l(this,"role","adapter")}supports(e,t,n,o){const a=n.getBlue().nodeToSchemaOutput(t,d.OperationSchema),s=this.parseEventPayload(e,n);return y(s==null?void 0:s.operation)&&s.operation===o&&e.source==="channel"&&e.channelName===a.channel}async handle(e,t,n,o){n.emitEvent({payload:e.payload,channelName:o,source:"channel"})}parseEventPayload(e,t){const n=t.getBlue();if(n.isTypeOf(e.payload,d.TimelineEntrySchema,{checkSchemaExtensions:!0})){const o=n.nodeToSchemaOutput(e.payload,d.TimelineEntrySchema);if(o.message)return n.nodeToSchemaOutput(o.message,d.OperationRequestSchema)}return null}}class ne{constructor(){l(this,"contractType","Process Embedded");l(this,"role","adapter");l(this,"contractBlueId",d.blueIds["Process Embedded"])}supports(e){return e.source!=="channel"}handle(e,t,n){const o=n.getBlue().nodeToSchemaOutput(t,d.ProcessEmbeddedSchema);for(const r of o.paths??[])n.emitEvent({...e,dispatchPath:n.resolvePath(r)})}}const oe=!process.env.SKIP_ISOLATED_VM;let S=null;if(oe)try{S=require("isolated-vm")}catch{console.warn("isolated-vm not available, using fallback evaluation method")}function F(c){return/\bimport\s.+\sfrom\s+['"][^'"]+['"]/.test(c)||/\bexport\s+/.test(c)}class N{static async evaluate({code:e,ctx:t,bindings:n={},options:o={}}){return!S||!oe?this.evaluateSimple(e,n,o):this.evaluateSecure(e,n,t,o)}static async evaluateSimple(e,t,n={}){if(F(e))throw new Error("Static import/export syntax requires isolated-vm – start Node without SKIP_ISOLATED_VM.");try{if(n.isCodeBlock){const o=Object.keys(t);return await(await new Function(...o,`return async function codeBlock(${o.join(", ")}) { ${e} }`)(...o.map(s=>t[s])))(...o.map(s=>t[s]))}else return new Function(...Object.keys(t),`return ${e};`)(...Object.values(t))}catch(o){throw n.isCodeBlock?new A(e,o):new $(e,o)}}static async evaluateSecure(e,t,n,o={}){if(!S)throw new Error("isolated-vm not available");const r=new S.Isolate({memoryLimit:32}),a=await r.createContext(),s=a.global;try{await this.setupIsolateEnvironment(s,t);const i=new Map,h=this.createModuleResolver(r,a,i,n);let u;return F(e)?u=await this.evaluateESModule(r,a,e,o,h):u=await this.evaluateSimpleScript(r,a,e,t,o),this.deepClone(u)}catch(i){throw o.isCodeBlock?new A(e,i):new $(e,i)}finally{a.release(),r.dispose()}}static async setupIsolateEnvironment(e,t){if(!S)throw new Error("isolated-vm not available");const n=new S.Callback((...o)=>console.log(...o));await e.set("log",n);for(const[o,r]of Object.entries(t))typeof r=="function"?await e.set(o,new S.Callback(r)):await e.set(o,new S.ExternalCopy(r).copyInto())}static createModuleResolver(e,t,n,o){return async r=>{if(n.has(r))return n.get(r);if(r.startsWith("blue:")){const a=r.slice(5),s=o.loadBlueContent;if(typeof s!="function")throw new Error(`ProcessingContext is missing a loadBlueContent(blueId) implementation (needed for ${r})`);const i=await s(a),h=await e.compileModule(i);return n.set(r,h),await h.instantiate(t,this.createModuleResolver(e,t,n,o)),h}if(/^https?:\/\//.test(r)){let a;if(typeof o.loadExternalModule=="function")a=await o.loadExternalModule(r);else throw new Error(`ProcessingContext is missing a loadExternalModule(url) implementation (needed for ${r})`);const s=await e.compileModule(a);return n.set(r,s),await s.instantiate(t,this.createModuleResolver(e,t,n,o)),s}throw new Error(`Unsupported module specifier "${r}"`)}}static async evaluateSimpleScript(e,t,n,o,r){const a=Object.keys(o).join(", "),s=Object.keys(o).map(u=>u),i=`(async (${a}) => { ${r.isCodeBlock?n:`return (${n});`} })(${s.join(", ")})`;return await(await e.compileScript(i)).run(t,{timeout:r.timeout??500,promise:!0,copy:!0,release:!0})}static async evaluateESModule(e,t,n,o,r){let a=n;if(o.isCodeBlock){const h=/^\s*(import\s.+?;|export\s.+?;)/gm,u=(n.match(h)||[]).join(`
|
|
1
|
+
"use strict";var pe=Object.defineProperty;var me=(c,e,t)=>e in c?pe(c,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):c[e]=t;var l=(c,e,t)=>me(c,typeof e!="symbol"?e+"":e,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d=require("@blue-repository/core-dev"),f=require("@blue-labs/language"),P=require("@blue-labs/shared-utils"),T=require("@blue-repository/myos-dev"),fe=(c,e)=>({on:(t,n)=>({end:()=>n(null)})});async function ye(c){return new Promise((e,t)=>{fe().on("error",t).end()})}const b=(...c)=>c.map((e,t)=>{if(typeof e!="string")return"";if(t===0&&e==="/")return"/";const n=t>0?e.replace(/^\/+/,""):e;return t<c.length-1?n.replace(/\/+$/,""):n}).filter(Boolean).join("/").replace(/\/{2,}/g,"/");class _{constructor(e,t,n,o){l(this,"actions",[]);this.getDocument=e,this.taskInfo=t,this.blue=n,this.onFlush=o}get(e){const t=this.getDocument(),n=b(this.taskInfo.nodePath,e);return t.get(n)}addPatch(e){this.actions.push({kind:"patch",patch:{...e,path:b(this.taskInfo.nodePath,e.path)}})}emitEvent(e){const t=this.taskInfo.event,n=t.trace??[],o={...e,originNodePath:e.originNodePath??this.taskInfo.nodePath,rootEvent:e.rootEvent??t.rootEvent??t,trace:[...n]};this.actions.push({kind:"event",event:o})}async flush(){var t;if(!this.actions.length)return[];const e=[...this.actions];return this.actions.length=0,await((t=this.onFlush)==null?void 0:t.call(this,e)),e}getNodePath(){return this.taskInfo.nodePath}resolvePath(e){return b(this.taskInfo.nodePath,e)}getTaskInfo(){return this.taskInfo}getBlue(){return this.blue}loadExternalModule(e){if(!/^https?:\/\//.test(e))throw new Error("Only http/https URLs are allowed");return ye()}loadBlueContent(e){throw new Error("Not implemented")}}class H extends Error{constructor(e,t){super(`Cannot apply patch ${JSON.stringify(e)}`),this.patch=e,this.cause=t,this.name="PatchApplicationError"}}class R extends Error{constructor(e,t,n){super(`Patch ${JSON.stringify(e)} touches "${e.op==="move"||e.op==="copy"?`${e.from} → ${e.path}`:e.path}" which is inside embedded document "${t}" (Process Embedded @ "${n}")`),this.patch=e,this.offendingPath=t,this.contractNodePath=n,this.name="EmbeddedDocumentModificationError"}}class A extends Error{constructor(e,t){super(`Failed to evaluate expression "${e}"`),this.code=e,this.cause=t,this.name="ExpressionEvaluationError"}}class W extends Error{constructor(e,t){super(`Failed to evaluate code block "${e}"`),this.code=e,this.cause=t,this.name="CodeBlockEvaluationError"}}function N(c){return P.deepFreeze(c)}function B(c){return c.clone()}function q(c,e,t="/",n=[]){const o=c.getContracts()??{};for(const[r,a]of Object.entries(o))if(f.BlueNodeTypeSchema.isTypeOf(a,d.ProcessEmbeddedSchema)){const h=e.nodeToSchemaOutput(a,d.ProcessEmbeddedSchema).paths??[];for(const u of h)n.push({absPath:b(t,u),contractPath:b(t,`contracts/${r}`)})}for(const[r,a]of Object.entries(c.getProperties()??{}))q(a,e,b(t,r),n);return n}function j(c,e){return c===e||c.startsWith(e.endsWith("/")?e:e+"/")}function x(c,e){if(!e.length)return c;let t=B(c);for(const n of e)try{t=f.applyBlueNodePatch(t,n,!0)}catch(o){throw new H(n,o)}return N(t)}function y(c){return c!=null}function C(c){return c instanceof f.BlueNode}class ge{constructor(e=[],t=(n,o)=>n<o?-1:n>o?1:0){l(this,"data");l(this,"length");l(this,"compare");if(this.data=e,this.length=this.data.length,this.compare=t,this.length>0)for(let n=(this.length>>1)-1;n>=0;n--)this._down(n)}push(e){this.data.push(e),this._up(this.length++)}pop(){if(this.length===0)return;const e=this.data[0],t=this.data.pop();return--this.length>0&&(this.data[0]=t,this._down(0)),e}peek(){return this.data[0]}_up(e){const{data:t,compare:n}=this,o=t[e];for(;e>0;){const r=e-1>>1,a=t[r];if(n(o,a)>=0)break;t[e]=a,e=r}t[e]=o}_down(e){const{data:t,compare:n}=this,o=this.length>>1,r=t[e];for(;e<o;){let a=(e<<1)+1;const s=a+1;if(s<this.length&&n(t[s],t[a])<0&&(a=s),n(t[a],r)>=0)break;t[e]=t[a],e=a}t[e]=r}}const Ee=(c,e,t,n,o,r)=>[-c,e,t,n,o,r],ve=(c,e)=>{for(let t=0;t<c.key.length;t++){const n=c.key[t],o=e.key[t];if(n!==o)return typeof n=="number"&&typeof o=="number"?n-o:typeof n=="string"&&typeof o=="string"?n.localeCompare(o):0}return 0};class we{constructor(){l(this,"queue");this.queue=new ge([],ve)}push(e){this.queue.push(e)}pop(){return this.queue.pop()}get length(){return this.queue.length}}class be{constructor(e=[]){l(this,"processors",new Map);l(this,"typeOrder",new Map);e.forEach((t,n)=>this.register(t,n))}register(e,t){if(this.processors.has(e.contractBlueId))throw new Error(`Processor for ${e.contractType} already registered`);this.processors.set(e.contractBlueId,e),this.typeOrder.has(e.contractBlueId)||this.typeOrder.set(e.contractBlueId,t??this.typeOrder.size)}get(e){if(!e)return;const t=e.getBlueId();if(t)return this.processors.get(t)}orderOf(e){if(!e)return 0;const t=e.getBlueId();return t?this.typeOrder.get(t)??0:0}values(){return this.processors.values()}}class Se{constructor(){l(this,"isEnabled");this.isEnabled=process.env.TRACE_BLUE_ENABLED==="true"}makeHop(e,t){return`${e}#${t}`}shouldTrace(){return this.isEnabled}addHop(e,t,n){if(!this.shouldTrace())return{...e};const o=[...e.trace??[],this.makeHop(t,n)];return{...e,trace:o}}getTrace(e){return e.trace??[]}clearTrace(e){return{...e,trace:[]}}isTracingEnabled(){return this.isEnabled}}const Te=64;class Pe{constructor(e,t,n,o,r){l(this,"traceManager");this.blue=e,this.registry=t,this.queue=n,this.getNextTaskId=o,this.getNextEventSeq=r,this.traceManager=new Se}async route(e,t,n,o,r=0){var i;if(n.seq===void 0&&(n.seq=this.getNextEventSeq()),t.length===0){if(n.dispatchPath){const h=n.dispatchPath.split("/").filter(Boolean),u={...n};return delete u.dispatchPath,this.route(e,h,u,o,r)}if(n.source==="channel"&&n.originNodePath&&n.originNodePath!=="/"){const h=((i=n.originNodePath)==null?void 0:i.split("/").filter(Boolean))??[];return this.route(e,h,n,o,r)}}const a=b("/",t.join("/")),s=e.get(a);C(s)&&await this.traverseContracts({doc:e,node:s,nodePath:a,event:n,afterTaskId:o,pathSegments:t,inlineDepth:r})}async traverseContracts(e){const{doc:t,node:n,nodePath:o,event:r,afterTaskId:a,pathSegments:s,inlineDepth:i}=e;if(!this.shouldSkipForChannel(r,o))for(const[h,u]of Object.entries(n.getContracts()??{})){if(!u.getType())continue;const p=this.registry.get(u.getType());if(!p){console.warn(`No processor registered for contract: ${h}`);continue}const m={nodePath:o,contractName:h,contractNode:u,event:r},g=new _(()=>t,m,this.blue);if(p.supports(r,u,g,h))switch(p.role){case"adapter":await this.processAdapter({cp:p,event:r,contractNode:u,ctx:g,contractName:h,doc:t,afterTaskId:a,inlineDepth:i});break;case"handler":this.scheduleHandler({contractNode:u,contractName:h,nodePath:o,event:r,depth:s.length,afterTaskId:a});break}}}async processAdapter(e){var v;const{cp:t,event:n,contractNode:o,ctx:r,contractName:a,doc:s,afterTaskId:i,inlineDepth:h}=e;if(h>=Te)throw new Error("Adapter recursion limit reached");const u=this.traceManager.addHop(n,((v=r.getTaskInfo())==null?void 0:v.nodePath)??"",a);await t.handle(u,o,r,a);const p=await r.flush();if(p.find(S=>S.kind==="patch"))throw new Error(`Contract "${a}" (adapter) attempted to patch the document`);const g=p.filter(S=>S.kind==="event");for(const S of g)await this.route(s,[],S.event,i,h+1)}scheduleHandler(e){const{contractNode:t,contractName:n,nodePath:o,event:r,depth:a,afterTaskId:s}=e,i=t.getType();if(!i){console.warn(`Contract node type is not defined for: ${n}`);return}const h=this.registry.orderOf(i),u=t.get("/order"),p=f.isBigNumber(u)?u.toNumber():0,m=this.getNextTaskId()+s,g=Ee(a,r.seq,h,p,n,m),v=this.traceManager.addHop(r,o,n);this.queue.push({key:g,nodePath:o,contractName:n,contractNode:t,event:v})}shouldSkipForChannel(e,t){return e.source==="channel"&&!!e.originNodePath&&e.originNodePath!==t}}const Ce=(c,e,t)=>{(t instanceof H||t instanceof R)&&console.error(`[Blue] Failed to apply patches for contract "${c}" on event ${JSON.stringify(e)}`,t)};function J(c,e){const t=B(c),n=o=>{if(!C(o))return;const r=o.getContracts();(!(r!=null&&r.checkpoint)||!f.BlueNodeTypeSchema.isTypeOf(r.checkpoint,d.ChannelEventCheckpointSchema))&&o.addContract("checkpoint",e.jsonValueToNode({type:{name:"Channel Event Checkpoint",blueId:d.blueIds["Channel Event Checkpoint"]},lastEvents:{}}))};n(t);for(const{absPath:o}of q(t,e)){const r=t.get(o);C(r)&&n(r)}return N(t)}function ke(c,e){const t=B(c);return C(t)&&(Q(t,e)||t.addContract("initialized",e.jsonValueToNode({type:{name:"Initialized Marker",blueId:d.blueIds["Initialized Marker"]}}))),N(t)}function Q(c,e){const t=c.getContracts();return Object.values(t??{}).some(n=>e.isTypeOf(n,d.InitializedMarkerSchema,{checkSchemaExtensions:!0}))}class K{constructor(e){l(this,"contractType","Channel Event Checkpoint");l(this,"contractBlueId",d.blueIds["Channel Event Checkpoint"]);l(this,"role","handler");this.cache=e}supports(e){var t,n;return e.source==="channel"&&((t=e.rootEvent)==null?void 0:t.payload)===e.payload&&((n=e.rootEvent)==null?void 0:n.source)==="external"}async handle(e,t,n){var a;if(!e.channelName||!((a=e.rootEvent)!=null&&a.seq))return;const o=await n.getBlue().calculateBlueId(e.rootEvent.payload),r=n.getNodePath().replace(/\/contracts\/checkpoint$/,"");this.cache.record(r,e,o)}}const U=(c,e)=>c.get(e)!==void 0;class Ie{constructor(){l(this,"firstSeen",new Map)}record(e,t,n){const o=e;this.firstSeen.has(o)||this.firstSeen.set(o,{docBase:e,event:t,eventBlueId:n})}flush(e){const t=[];for(const{docBase:n,event:o,eventBlueId:r}of this.firstSeen.values()){if(!o.channelName)continue;const a=b(n,"contracts/checkpoint/lastEvents",o.channelName),s=`${a}/blueId`;U(e,a)?t.push({op:U(e,s)?"replace":"add",path:s,val:r}):t.push({op:"add",path:a,val:{blueId:r}})}return t}clear(){this.firstSeen.clear()}}class k{constructor(){l(this,"role","adapter")}baseSupports(e){return e.source!=="channel"}}class X extends k{constructor(){super(...arguments);l(this,"contractType","Composite Timeline Channel");l(this,"contractBlueId",d.blueIds["Composite Timeline Channel"])}supports(t,n,o){const r=o.getBlue().nodeToSchemaOutput(n,d.CompositeTimelineChannelSchema);return!r.channels||!t.channelName?!1:r.channels.includes(t.channelName)}handle(t,n,o,r){o.emitEvent({payload:t.payload,channelName:r,source:"channel"})}}class Y extends k{constructor(){super(...arguments);l(this,"contractType","Document Update Channel");l(this,"contractBlueId",d.blueIds["Document Update Channel"])}supports(t,n,o,r){if(!this.baseSupports(t))return!1;const a=o.getBlue().nodeToSchemaOutput(n,d.DocumentUpdateChannelSchema),s=t.payload.get("/path");if(!s||t.channelName===r)return!1;const i=a.path;return P.isNonNullable(i)&&s===o.resolvePath(i)}handle(t,n,o,r){const a=t.payload;a&&o.emitEvent({payload:a,channelName:r,source:"channel"})}}class Z extends k{constructor(){super(...arguments);l(this,"contractType","Embedded Node Channel");l(this,"contractBlueId",d.blueIds["Embedded Node Channel"])}supports(t,n,o){if(!this.baseSupports(t))return!1;const r=o.getBlue().nodeToSchemaOutput(n,d.EmbeddedNodeChannelSchema);return y(t.originNodePath)&&y(r.path)&&t.originNodePath===o.resolvePath(r.path)}handle(t,n,o,r){const a=o.getBlue().nodeToSchemaOutput(n,d.EmbeddedNodeChannelSchema),{originNodePath:s,payload:i}=t;y(a.path)&&s===o.resolvePath(a.path)&&o.emitEvent({payload:i,channelName:r,source:"channel"})}}class G{constructor(){l(this,"contractType","Initialized Marker");l(this,"contractBlueId",d.blueIds["Initialized Marker"]);l(this,"role","marker")}supports(){return!1}handle(){}}class ee extends k{constructor(){super(...arguments);l(this,"contractType","Lifecycle Event Channel");l(this,"contractBlueId",d.blueIds["Lifecycle Event Channel"])}supports(t,n,o){return!this.baseSupports(t)||!this.isLifecycleEvent(t,o)?!1:this.isEventPatternMatch(t,n,o)}handle(t,n,o,r){o.emitEvent({payload:t.payload,channelName:r,source:"channel"})}isLifecycleEvent(t,n){const o=n.getBlue(),r=t.payload;return o.isTypeOf(r,d.LifecycleEventSchema,{checkSchemaExtensions:!0})}isEventPatternMatch(t,n,o){var a;const r=(a=n.getProperties())==null?void 0:a.event;if(!r)return!0;try{const s=o.getBlue(),i=s.nodeToJson(t.payload),h=s.nodeToJson(r);return P.deepContains(i,h)}catch(s){return console.warn("Error during lifecycle event pattern matching:",s),!1}}}const V=(c,e)=>{const t=e.getBlue();return t.isTypeOf(c.payload,d.TimelineEntrySchema)||t.isTypeOf(c.payload,T.MyOSTimelineEntrySchema)};class te extends k{constructor(){super(...arguments);l(this,"contractType","MyOS Timeline Channel");l(this,"contractBlueId",T.blueIds["MyOS Timeline Channel"])}supports(t,n,o){var m;if(!this.baseSupports(t)||!V(t,o))return!1;const a=o.getBlue().nodeToSchemaOutput(t.payload,T.MyOSTimelineEntrySchema),s=o.getBlue().nodeToSchemaOutput(n,T.MyOSTimelineChannelSchema),i=(m=a.timeline)==null?void 0:m.timelineId,h=y(s.timelineId)&&y(i),u=y(s.account)&&y(a.account),p=y(s.email)&&y(a.email);return h&&i===s.timelineId||u&&a.account===s.account||p&&a.email===s.email}handle(t,n,o,r){V(t,o)&&o.emitEvent({payload:t.payload,channelName:r,source:"channel"})}}class ne extends k{constructor(){super(...arguments);l(this,"contractType","MyOS Agent Channel");l(this,"contractBlueId",T.blueIds["MyOS Agent Channel"])}supports(t,n,o){if(!this.baseSupports(t))return!1;try{const{myosAgentEvent:r,myosAgentChannel:a}=this.parseEventAndChannel(t,n,o);return this.isAgentMatch(r,a)&&this.isEventPatternMatch(r,a,o)}catch(r){return console.warn("Error in MyOSAgentChannelProcessor.supports:",r),!1}}handle(t,n,o,r){o.emitEvent({payload:t.payload,channelName:r,source:"channel"})}parseEventAndChannel(t,n,o){const r=o.getBlue(),a=r.nodeToSchemaOutput(t.payload,T.MyOSAgentEventSchema),s=r.nodeToSchemaOutput(n,T.MyOSAgentChannelSchema);return{myosAgentEvent:a,myosAgentChannel:s}}isAgentMatch(t,n){var a;const o=t.agentId,r=(a=n.agent)==null?void 0:a.agentId;return y(o)&&y(r)&&o===r}isEventPatternMatch(t,n,o){const r=n.event;if(!r)return!0;const a=t.event;if(!a)return!1;try{const s=o.getBlue(),i=s.nodeToJson(a),h=s.nodeToJson(r);return P.deepContains(i,h)}catch(s){return console.warn("Error during event pattern matching:",s),!1}}}class oe{constructor(){l(this,"contractType","Operation");l(this,"contractBlueId",d.blueIds.Operation);l(this,"role","adapter")}supports(e,t,n,o){const a=n.getBlue().nodeToSchemaOutput(t,d.OperationSchema),s=this.parseEventPayload(e,n);return y(s==null?void 0:s.operation)&&s.operation===o&&e.source==="channel"&&e.channelName===a.channel}async handle(e,t,n,o){n.emitEvent({payload:e.payload,channelName:o,source:"channel"})}parseEventPayload(e,t){const n=t.getBlue();if(n.isTypeOf(e.payload,d.TimelineEntrySchema,{checkSchemaExtensions:!0})){const o=n.nodeToSchemaOutput(e.payload,d.TimelineEntrySchema);if(o.message)return n.nodeToSchemaOutput(o.message,d.OperationRequestSchema)}return null}}class re{constructor(){l(this,"contractType","Process Embedded");l(this,"role","adapter");l(this,"contractBlueId",d.blueIds["Process Embedded"])}supports(e){return e.source!=="channel"}handle(e,t,n){const o=n.getBlue().nodeToSchemaOutput(t,d.ProcessEmbeddedSchema);for(const r of o.paths??[])n.emitEvent({...e,dispatchPath:n.resolvePath(r)})}}const ae=!process.env.SKIP_ISOLATED_VM;let w=null;if(ae)try{w=require("isolated-vm")}catch{console.warn("isolated-vm not available, using fallback evaluation method")}function F(c){return/\bimport\s.+\sfrom\s+['"][^'"]+['"]/.test(c)||/\bexport\s+/.test(c)}class O{static async evaluate({code:e,ctx:t,bindings:n={},options:o={}}){return!w||!ae?this.evaluateSimple(e,n,o):this.evaluateSecure(e,n,t,o)}static async evaluateSimple(e,t,n={}){if(F(e))throw new Error("Static import/export syntax requires isolated-vm – start Node without SKIP_ISOLATED_VM.");try{if(n.isCodeBlock){const o=Object.keys(t);return await(await new Function(...o,`return async function codeBlock(${o.join(", ")}) { ${e} }`)(...o.map(s=>t[s])))(...o.map(s=>t[s]))}else return new Function(...Object.keys(t),`return ${e};`)(...Object.values(t))}catch(o){throw n.isCodeBlock?new W(e,o):new A(e,o)}}static async evaluateSecure(e,t,n,o={}){if(!w)throw new Error("isolated-vm not available");const r=new w.Isolate({memoryLimit:32}),a=await r.createContext(),s=a.global;try{await this.setupIsolateEnvironment(s,t);const i=new Map,h=this.createModuleResolver(r,a,i,n);let u;return F(e)?u=await this.evaluateESModule(r,a,e,o,h):u=await this.evaluateSimpleScript(r,a,e,t,o),this.deepClone(u)}catch(i){throw o.isCodeBlock?new W(e,i):new A(e,i)}finally{a.release(),r.dispose()}}static async setupIsolateEnvironment(e,t){if(!w)throw new Error("isolated-vm not available");const n=new w.Callback((...o)=>console.log(...o));await e.set("log",n);for(const[o,r]of Object.entries(t))typeof r=="function"?await e.set(o,new w.Callback(r)):await e.set(o,new w.ExternalCopy(r).copyInto())}static createModuleResolver(e,t,n,o){return async r=>{if(n.has(r))return n.get(r);if(r.startsWith("blue:")){const a=r.slice(5),s=o.loadBlueContent;if(typeof s!="function")throw new Error(`ProcessingContext is missing a loadBlueContent(blueId) implementation (needed for ${r})`);const i=await s(a),h=await e.compileModule(i);return n.set(r,h),await h.instantiate(t,this.createModuleResolver(e,t,n,o)),h}if(/^https?:\/\//.test(r)){let a;if(typeof o.loadExternalModule=="function")a=await o.loadExternalModule(r);else throw new Error(`ProcessingContext is missing a loadExternalModule(url) implementation (needed for ${r})`);const s=await e.compileModule(a);return n.set(r,s),await s.instantiate(t,this.createModuleResolver(e,t,n,o)),s}throw new Error(`Unsupported module specifier "${r}"`)}}static async evaluateSimpleScript(e,t,n,o,r){const a=Object.keys(o).join(", "),s=Object.keys(o).map(u=>u),i=`(async (${a}) => { ${r.isCodeBlock?n:`return (${n});`} })(${s.join(", ")})`;return await(await e.compileScript(i)).run(t,{timeout:r.timeout??500,promise:!0,copy:!0,release:!0})}static async evaluateESModule(e,t,n,o,r){let a=n;if(o.isCodeBlock){const h=/^\s*(import\s.+?;|export\s.+?;)/gm,u=(n.match(h)||[]).join(`
|
|
2
2
|
`),p=n.replace(h,"").trim();a=`
|
|
3
3
|
${u}
|
|
4
4
|
const run = function() {
|
|
5
5
|
${p}
|
|
6
6
|
};
|
|
7
7
|
export default run();
|
|
8
|
-
`}const s=await e.compileModule(a);return await s.instantiate(t,r),await s.evaluate({timeout:o.timeout??500,promise:!0,reference:!0,release:!0}),await s.namespace.get("default",{timeout:o.timeout??500,promise:!0,copy:!0,release:!0})}static deepClone(e){return typeof e>"u"?e:JSON.parse(JSON.stringify(e))}}class
|
|
8
|
+
`}const s=await e.compileModule(a);return await s.instantiate(t,r),await s.evaluate({timeout:o.timeout??500,promise:!0,reference:!0,release:!0}),await s.namespace.get("default",{timeout:o.timeout??500,promise:!0,copy:!0,release:!0})}static deepClone(e){return typeof e>"u"?e:JSON.parse(JSON.stringify(e))}}class M{static createStandardBindings(e,t,n){const o=e.getBlue(),r=o.nodeToJson(t.payload,"simple"),a=f.isBigNumber(r)?r.toNumber():r;return{document:s=>{const i=e.get(s);return f.isBigNumber(i)?i.toNumber():C(i)?o.nodeToJson(i,"original"):i},event:a,steps:n}}}function L(c,e){const{op:t,path:n,val:o,from:r}=c;if((t==="move"||t==="copy")&&!r)throw new Error(`${t} operation requires 'from' path`);if((t==="add"||t==="replace")&&o===void 0)throw new Error(`${t} operation requires 'val' property`);const a={type:"Document Update",op:t,path:n};return o!==void 0&&(a.val=o),r!==void 0&&(a.from=r),e.jsonValueToNode(a)}function Ne(c){return c.jsonValueToNode({type:"Document Processing Initiated"})}class Be{constructor(){l(this,"stepType","Update Document")}supports(e){return f.BlueNodeTypeSchema.isTypeOf(e,d.UpdateDocumentSchema)}async execute(e,t,n,o,r){const a=n.getBlue();if(!f.BlueNodeTypeSchema.isTypeOf(e,d.UpdateDocumentSchema))return;const s=await this.evaluateChangeset(e.get("/changeset"),n,t,r),i=f.applyBlueNodePatch(e,{op:"replace",path:"/changeset",val:s}),h=n.getBlue().nodeToSchemaOutput(i,d.UpdateDocumentSchema);for(const u of h.changeset??[]){if(!u.path)continue;const p=u.val;if((u.op==="replace"||u.op==="add")&&P.isNonNullable(p)){const m=await this.evaluateChangeValue(p,n,t,r);n.addPatch({op:u.op,path:u.path,val:m}),n.emitEvent({payload:L({op:u.op,path:n.resolvePath(u.path),val:a.nodeToJson(m,"original")},a)})}u.op==="remove"&&(n.addPatch({op:u.op,path:u.path}),n.emitEvent({payload:L({op:u.op,path:n.resolvePath(u.path),val:null},a)}))}}async evaluateChangeset(e,t,n,o){const r=t.getBlue();if(typeof e=="string"&&e.startsWith("${")&&e.endsWith("}")){const a=e.slice(2,-1),s=await O.evaluate({code:a,ctx:t,bindings:M.createStandardBindings(t,n,o)});return r.jsonValueToNode(s??null)}if(C(e))return e;throw new Error("Invalid changeset: expected a string or document node")}async evaluateChangeValue(e,t,n,o){const r=e.getValue(),a=t.getBlue();if(typeof r=="string"&&r.startsWith("${")&&r.endsWith("}")){const s=r.slice(2,-1),i=await O.evaluate({code:s,ctx:t,bindings:M.createStandardBindings(t,n,o)});return a.jsonValueToNode(i??null)}return e}}class Oe{constructor(){l(this,"stepType","Trigger Event")}supports(e){return f.BlueNodeTypeSchema.isTypeOf(e,d.TriggerEventSchema)}async execute(e,t,n){const o=n.getBlue();if(!f.BlueNodeTypeSchema.isTypeOf(e,d.TriggerEventSchema))return;const r=o.nodeToSchemaOutput(e,d.TriggerEventSchema);r.event&&n.emitEvent({payload:r.event})}}class Me{constructor(){l(this,"stepType","JavaScript Code")}supports(e){return f.BlueNodeTypeSchema.isTypeOf(e,d.JavaScriptCodeSchema)}async execute(e,t,n,o,r){if(!f.BlueNodeTypeSchema.isTypeOf(e,d.JavaScriptCodeSchema))return;const a=n.getBlue(),s=a.nodeToSchemaOutput(e,d.JavaScriptCodeSchema);if(!s.code)throw new Error("JavaScript code is required");const i=await O.evaluate({code:s.code,ctx:n,bindings:M.createStandardBindings(n,t,r),options:{isCodeBlock:!0,timeout:500}});if(i&&typeof i=="object"&&"events"in i){const h=i;if(Array.isArray(h.events))for(const u of h.events)n.emitEvent({payload:a.jsonValueToNode(u)})}return i}}const qe=[new Be,new Oe,new Me];class ${constructor(e=qe){l(this,"contractType","Sequential Workflow");l(this,"contractBlueId",d.blueIds["Sequential Workflow"]);l(this,"role","handler");l(this,"executors",[]);this.executors=e}registerExecutor(e){this.executors.push(e)}supports(e,t,n){const a=n.getBlue().nodeToSchemaOutput(t,d.SequentialWorkflowSchema).channel;return e.source==="channel"&&e.channelName===a}async handle(e,t,n,o){var s;const r={},a=(s=t.getProperties())==null?void 0:s.steps.getItems();for(const[i,h]of(a??[]).entries()){const u=this.executors.find(m=>m.supports(h));if(!u)throw new Error(`Unsupported workflow step type "${h.getType()}"`);const p=await u.execute(h,e,n,o,r);if(p!==void 0){const m=h.getName(),g=typeof m=="string"?m:`Step${i+1}`;r[g]=p}await n.flush()}}}class se{constructor(e){l(this,"contractType","Sequential Workflow Operation");l(this,"contractBlueId",d.blueIds["Sequential Workflow Operation"]);l(this,"role","handler");l(this,"sequentialWorkflowProcessor");this.sequentialWorkflowProcessor=e||new $}supports(e,t,n){const a=n.getBlue().nodeToSchemaOutput(t,d.SequentialWorkflowOperationSchema).operation,s=e.channelName;return e.source==="channel"&&P.isNonNullable(s)&&P.isNonNullable(a)&&s===a}async handle(e,t,n,o){try{await this.sequentialWorkflowProcessor.handle(e,t,n,o)}catch(r){throw console.error("Error in SequentialWorkflowOperationProcessor.handle:",r),r}}}const z=c=>f.BlueNodeTypeSchema.isTypeOf(c.payload,d.TimelineEntrySchema);class ce extends k{constructor(){super(...arguments);l(this,"contractType","Timeline Channel");l(this,"contractBlueId",d.blueIds["Timeline Channel"])}supports(t,n,o){var u;if(!this.baseSupports(t)||!z(t))return!1;const a=o.getBlue().nodeToSchemaOutput(t.payload,d.TimelineEntrySchema),s=o.getBlue().nodeToSchemaOutput(n,d.TimelineChannelSchema),i=(u=a.timeline)==null?void 0:u.timelineId;return y(s.timelineId)&&y(i)&&i===s.timelineId}handle(t,n,o,r){z(t)&&o.emitEvent({payload:t.payload,channelName:r,source:"channel"})}}const $e=[new re,new Z,new Y,new ce,new te,new ne,new X,new ee,new oe,new $,new se,new G];class De{constructor(e,t=$e){l(this,"taskCounter",0);l(this,"eventCounter",0);l(this,"registry");l(this,"queue");l(this,"router");l(this,"checkpointCache",new Ie);this.blue=e,this.registry=new be(t),this.queue=new we,this.router=new Pe(this.blue,this.registry,this.queue,()=>++this.taskCounter,()=>++this.eventCounter),this.register(new K(this.checkpointCache),9999)}register(e,t){this.registry.register(e,t)}async initialize(e){let t=J(N(e),this.blue);const n={payload:Ne(this.blue)},o=[n.payload];await this.router.route(t,[],n,0);const r=await this.drainQueue(t);return t=r.state,o.push(...r.emitted),t=ke(t,this.blue),{state:B(t),emitted:o}}async processEvents(e,t){let n=J(N(e),this.blue);const o=[];if(!Q(n,this.blue))throw new Error("Document is not initialized");for(const r of t)try{const a={payload:r,source:"external"};await this.router.route(n,[],a,0);const s=await this.drainQueue(n);n=s.state,o.push(...s.emitted);const i=this.checkpointCache.flush(n);i.length&&(n=x(n,i))}finally{this.checkpointCache.clear()}return{state:B(n),emitted:o}}async drainQueue(e){var a;let t=e;const n=[],o=1e4;let r=0;for(;this.queue.length;){if(++r>o)throw new Error("Possible cycle – too many iterations");const s=this.queue.pop(),{nodePath:i,contractName:h,contractNode:u,event:p}=s,m=t.get(i);if(!C(m)||!((a=m.getContracts())!=null&&a[h])||!u.getType())continue;const g=this.registry.get(u.getType());if(!g){console.warn(`No processor registered for contract: ${h}`);continue}const v=new _(()=>t,s,this.blue,async S=>{for(const E of S)if(E.kind==="patch"){const ie=q(t,this.blue);for(const I of ie){const le=E.patch.op==="move"||E.patch.op==="copy"?[E.patch.from,E.patch.path]:[E.patch.path],D=v.getNodePath(),ue=le.some(de=>j(de,I.absPath)),he=j(D,I.absPath);if(ue&&!he)throw new R(E.patch,I.absPath,D)}try{t=x(t,[E.patch])}catch(I){throw Ce(h,p,I),I}}else E.kind==="event"&&(n.push(E.event.payload),await this.router.route(t,[],E.event,s.key[5]))});await g.handle(p,u,v,h),await v.flush()}return{state:t,emitted:n}}}exports.BlueDocumentProcessor=De;exports.ChannelEventCheckpointProcessor=K;exports.CompositeTimelineChannelProcessor=X;exports.DocumentUpdateChannelProcessor=Y;exports.EmbeddedNodeChannelProcessor=Z;exports.InitializedMarkerProcessor=G;exports.LifecycleEventChannelProcessor=ee;exports.MyOSAgentChannelProcessor=ne;exports.MyOSTimelineChannelProcessor=te;exports.OperationProcessor=oe;exports.ProcessEmbeddedProcessor=re;exports.SequentialWorkflowOperationProcessor=se;exports.SequentialWorkflowProcessor=$;exports.TimelineChannelProcessor=ce;
|
package/dist/index.mjs
CHANGED
|
@@ -1,31 +1,31 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var l = (c, e, t) =>
|
|
4
|
-
import { ProcessEmbeddedSchema as
|
|
5
|
-
import { applyBlueNodePatch as
|
|
6
|
-
import { deepFreeze as
|
|
7
|
-
import { blueIds as
|
|
8
|
-
const
|
|
1
|
+
var he = Object.defineProperty;
|
|
2
|
+
var de = (c, e, t) => e in c ? he(c, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : c[e] = t;
|
|
3
|
+
var l = (c, e, t) => de(c, typeof e != "symbol" ? e + "" : e, t);
|
|
4
|
+
import { ProcessEmbeddedSchema as q, ChannelEventCheckpointSchema as pe, blueIds as f, InitializedMarkerSchema as me, CompositeTimelineChannelSchema as fe, DocumentUpdateChannelSchema as ye, EmbeddedNodeChannelSchema as J, LifecycleEventSchema as ge, TimelineEntrySchema as I, OperationSchema as Ee, OperationRequestSchema as we, UpdateDocumentSchema as B, TriggerEventSchema as M, JavaScriptCodeSchema as $, SequentialWorkflowSchema as ve, SequentialWorkflowOperationSchema as be, TimelineChannelSchema as Te } from "@blue-repository/core-dev";
|
|
5
|
+
import { applyBlueNodePatch as X, BlueNodeTypeSchema as E, BlueNode as Se, isBigNumber as A } from "@blue-labs/language";
|
|
6
|
+
import { deepFreeze as Pe, isNonNullable as O, deepContains as Y } from "@blue-labs/shared-utils";
|
|
7
|
+
import { blueIds as Z, MyOSTimelineEntrySchema as G, MyOSTimelineChannelSchema as Ce, MyOSAgentEventSchema as ke, MyOSAgentChannelSchema as Ie } from "@blue-repository/myos-dev";
|
|
8
|
+
const Ne = (c, e) => ({
|
|
9
9
|
on: (t, n) => ({ end: () => n(null) })
|
|
10
10
|
});
|
|
11
|
-
async function
|
|
11
|
+
async function Oe(c) {
|
|
12
12
|
return new Promise((e, t) => {
|
|
13
|
-
|
|
13
|
+
Ne().on("error", t).end();
|
|
14
14
|
});
|
|
15
15
|
}
|
|
16
|
-
const
|
|
16
|
+
const b = (...c) => c.map((e, t) => {
|
|
17
17
|
if (typeof e != "string") return "";
|
|
18
18
|
if (t === 0 && e === "/") return "/";
|
|
19
19
|
const n = t > 0 ? e.replace(/^\/+/, "") : e;
|
|
20
20
|
return t < c.length - 1 ? n.replace(/\/+$/, "") : n;
|
|
21
21
|
}).filter(Boolean).join("/").replace(/\/{2,}/g, "/");
|
|
22
|
-
class
|
|
22
|
+
class ee {
|
|
23
23
|
constructor(e, t, n, o) {
|
|
24
24
|
l(this, "actions", []);
|
|
25
25
|
this.getDocument = e, this.taskInfo = t, this.blue = n, this.onFlush = o;
|
|
26
26
|
}
|
|
27
27
|
get(e) {
|
|
28
|
-
const t = this.getDocument(), n =
|
|
28
|
+
const t = this.getDocument(), n = b(this.taskInfo.nodePath, e);
|
|
29
29
|
return t.get(n);
|
|
30
30
|
}
|
|
31
31
|
addPatch(e) {
|
|
@@ -33,7 +33,7 @@ class G {
|
|
|
33
33
|
kind: "patch",
|
|
34
34
|
patch: {
|
|
35
35
|
...e,
|
|
36
|
-
path:
|
|
36
|
+
path: b(this.taskInfo.nodePath, e.path)
|
|
37
37
|
}
|
|
38
38
|
});
|
|
39
39
|
}
|
|
@@ -56,7 +56,7 @@ class G {
|
|
|
56
56
|
return this.taskInfo.nodePath;
|
|
57
57
|
}
|
|
58
58
|
resolvePath(e) {
|
|
59
|
-
return
|
|
59
|
+
return b(this.taskInfo.nodePath, e);
|
|
60
60
|
}
|
|
61
61
|
getTaskInfo() {
|
|
62
62
|
return this.taskInfo;
|
|
@@ -68,76 +68,82 @@ class G {
|
|
|
68
68
|
loadExternalModule(e) {
|
|
69
69
|
if (!/^https?:\/\//.test(e))
|
|
70
70
|
throw new Error("Only http/https URLs are allowed");
|
|
71
|
-
return
|
|
71
|
+
return Oe();
|
|
72
72
|
}
|
|
73
73
|
loadBlueContent(e) {
|
|
74
74
|
throw new Error("Not implemented");
|
|
75
75
|
}
|
|
76
76
|
}
|
|
77
|
-
class
|
|
77
|
+
class te extends Error {
|
|
78
78
|
constructor(e, t) {
|
|
79
79
|
super(`Cannot apply patch ${JSON.stringify(e)}`), this.patch = e, this.cause = t, this.name = "PatchApplicationError";
|
|
80
80
|
}
|
|
81
81
|
}
|
|
82
|
-
class
|
|
82
|
+
class ne extends Error {
|
|
83
83
|
constructor(e, t, n) {
|
|
84
84
|
super(
|
|
85
85
|
`Patch ${JSON.stringify(e)} touches "${e.op === "move" || e.op === "copy" ? `${e.from} → ${e.path}` : e.path}" which is inside embedded document "${t}" (Process Embedded @ "${n}")`
|
|
86
86
|
), this.patch = e, this.offendingPath = t, this.contractNodePath = n, this.name = "EmbeddedDocumentModificationError";
|
|
87
87
|
}
|
|
88
88
|
}
|
|
89
|
-
class
|
|
89
|
+
class V extends Error {
|
|
90
90
|
constructor(e, t) {
|
|
91
91
|
super(`Failed to evaluate expression "${e}"`), this.code = e, this.cause = t, this.name = "ExpressionEvaluationError";
|
|
92
92
|
}
|
|
93
93
|
}
|
|
94
|
-
class
|
|
94
|
+
class F extends Error {
|
|
95
95
|
constructor(e, t) {
|
|
96
96
|
super(`Failed to evaluate code block "${e}"`), this.code = e, this.cause = t, this.name = "CodeBlockEvaluationError";
|
|
97
97
|
}
|
|
98
98
|
}
|
|
99
|
-
function
|
|
99
|
+
function k(c) {
|
|
100
|
+
return Pe(c);
|
|
101
|
+
}
|
|
102
|
+
function N(c) {
|
|
103
|
+
return c.clone();
|
|
104
|
+
}
|
|
105
|
+
function j(c, e, t = "/", n = []) {
|
|
100
106
|
const o = c.getContracts() ?? {};
|
|
101
107
|
for (const [r, a] of Object.entries(o))
|
|
102
108
|
if (E.isTypeOf(
|
|
103
109
|
a,
|
|
104
|
-
|
|
110
|
+
q
|
|
105
111
|
)) {
|
|
106
112
|
const h = e.nodeToSchemaOutput(
|
|
107
113
|
a,
|
|
108
|
-
|
|
114
|
+
q
|
|
109
115
|
).paths ?? [];
|
|
110
116
|
for (const u of h)
|
|
111
117
|
n.push({
|
|
112
|
-
absPath:
|
|
113
|
-
contractPath:
|
|
118
|
+
absPath: b(t, u),
|
|
119
|
+
contractPath: b(t, `contracts/${r}`)
|
|
114
120
|
});
|
|
115
121
|
}
|
|
116
122
|
for (const [r, a] of Object.entries(c.getProperties() ?? {}))
|
|
117
|
-
|
|
123
|
+
j(a, e, b(t, r), n);
|
|
118
124
|
return n;
|
|
119
125
|
}
|
|
120
|
-
function
|
|
126
|
+
function L(c, e) {
|
|
121
127
|
return c === e || c.startsWith(e.endsWith("/") ? e : e + "/");
|
|
122
128
|
}
|
|
123
|
-
function
|
|
129
|
+
function U(c, e) {
|
|
124
130
|
if (!e.length) return c;
|
|
125
|
-
let t = c
|
|
131
|
+
let t = N(c);
|
|
126
132
|
for (const n of e)
|
|
127
133
|
try {
|
|
128
|
-
t =
|
|
134
|
+
t = X(t, n, !0);
|
|
129
135
|
} catch (o) {
|
|
130
|
-
throw new
|
|
136
|
+
throw new te(n, o);
|
|
131
137
|
}
|
|
132
|
-
return
|
|
138
|
+
return k(t);
|
|
133
139
|
}
|
|
134
140
|
function m(c) {
|
|
135
141
|
return c != null;
|
|
136
142
|
}
|
|
137
143
|
function S(c) {
|
|
138
|
-
return c instanceof
|
|
144
|
+
return c instanceof Se;
|
|
139
145
|
}
|
|
140
|
-
class
|
|
146
|
+
class Be {
|
|
141
147
|
constructor(e = [], t = (n, o) => n < o ? -1 : n > o ? 1 : 0) {
|
|
142
148
|
l(this, "data");
|
|
143
149
|
l(this, "length");
|
|
@@ -176,14 +182,14 @@ class Ne {
|
|
|
176
182
|
t[e] = r;
|
|
177
183
|
}
|
|
178
184
|
}
|
|
179
|
-
const
|
|
185
|
+
const Me = (c, e, t, n, o, r) => [
|
|
180
186
|
-c,
|
|
181
187
|
e,
|
|
182
188
|
t,
|
|
183
189
|
n,
|
|
184
190
|
o,
|
|
185
191
|
r
|
|
186
|
-
],
|
|
192
|
+
], $e = (c, e) => {
|
|
187
193
|
for (let t = 0; t < c.key.length; t++) {
|
|
188
194
|
const n = c.key[t], o = e.key[t];
|
|
189
195
|
if (n !== o)
|
|
@@ -191,13 +197,13 @@ const Oe = (c, e, t, n, o, r) => [
|
|
|
191
197
|
}
|
|
192
198
|
return 0;
|
|
193
199
|
};
|
|
194
|
-
class
|
|
200
|
+
class qe {
|
|
195
201
|
/**
|
|
196
202
|
* Creates a new task queue with the task key comparator
|
|
197
203
|
*/
|
|
198
204
|
constructor() {
|
|
199
205
|
l(this, "queue");
|
|
200
|
-
this.queue = new
|
|
206
|
+
this.queue = new Be([], $e);
|
|
201
207
|
}
|
|
202
208
|
/**
|
|
203
209
|
* Adds a task to the queue
|
|
@@ -222,7 +228,7 @@ class Me {
|
|
|
222
228
|
return this.queue.length;
|
|
223
229
|
}
|
|
224
230
|
}
|
|
225
|
-
class
|
|
231
|
+
class Ae {
|
|
226
232
|
/**
|
|
227
233
|
* Creates a new contract registry
|
|
228
234
|
*
|
|
@@ -277,7 +283,7 @@ class $e {
|
|
|
277
283
|
return this.processors.values();
|
|
278
284
|
}
|
|
279
285
|
}
|
|
280
|
-
class
|
|
286
|
+
class De {
|
|
281
287
|
constructor() {
|
|
282
288
|
l(this, "isEnabled");
|
|
283
289
|
this.isEnabled = process.env.TRACE_BLUE_ENABLED === "true";
|
|
@@ -344,8 +350,8 @@ class qe {
|
|
|
344
350
|
return this.isEnabled;
|
|
345
351
|
}
|
|
346
352
|
}
|
|
347
|
-
const
|
|
348
|
-
class
|
|
353
|
+
const We = 64;
|
|
354
|
+
class je {
|
|
349
355
|
/**
|
|
350
356
|
* Creates a new event router
|
|
351
357
|
*
|
|
@@ -356,7 +362,7 @@ class De {
|
|
|
356
362
|
*/
|
|
357
363
|
constructor(e, t, n, o, r) {
|
|
358
364
|
l(this, "traceManager");
|
|
359
|
-
this.blue = e, this.registry = t, this.queue = n, this.getNextTaskId = o, this.getNextEventSeq = r, this.traceManager = new
|
|
365
|
+
this.blue = e, this.registry = t, this.queue = n, this.getNextTaskId = o, this.getNextEventSeq = r, this.traceManager = new De();
|
|
360
366
|
}
|
|
361
367
|
/**
|
|
362
368
|
* Routes an event to matching contracts in the document
|
|
@@ -379,7 +385,7 @@ class De {
|
|
|
379
385
|
return this.route(e, h, n, o, r);
|
|
380
386
|
}
|
|
381
387
|
}
|
|
382
|
-
const a =
|
|
388
|
+
const a = b("/", t.join("/")), s = e.get(a);
|
|
383
389
|
S(s) && await this.traverseContracts({
|
|
384
390
|
doc: e,
|
|
385
391
|
node: s,
|
|
@@ -418,7 +424,7 @@ class De {
|
|
|
418
424
|
contractName: h,
|
|
419
425
|
contractNode: u,
|
|
420
426
|
event: r
|
|
421
|
-
}, y = new
|
|
427
|
+
}, y = new ee(() => t, p, this.blue);
|
|
422
428
|
if (d.supports(r, u, y, h))
|
|
423
429
|
switch (d.role) {
|
|
424
430
|
case "adapter":
|
|
@@ -461,7 +467,7 @@ class De {
|
|
|
461
467
|
afterTaskId: i,
|
|
462
468
|
inlineDepth: h
|
|
463
469
|
} = e;
|
|
464
|
-
if (h >=
|
|
470
|
+
if (h >= We)
|
|
465
471
|
throw new Error("Adapter recursion limit reached");
|
|
466
472
|
const u = this.traceManager.addHop(
|
|
467
473
|
n,
|
|
@@ -470,13 +476,13 @@ class De {
|
|
|
470
476
|
);
|
|
471
477
|
await t.handle(u, o, r, a);
|
|
472
478
|
const d = await r.flush();
|
|
473
|
-
if (d.find((
|
|
479
|
+
if (d.find((T) => T.kind === "patch"))
|
|
474
480
|
throw new Error(
|
|
475
481
|
`Contract "${a}" (adapter) attempted to patch the document`
|
|
476
482
|
);
|
|
477
|
-
const y = d.filter((
|
|
478
|
-
for (const
|
|
479
|
-
await this.route(s, [],
|
|
483
|
+
const y = d.filter((T) => T.kind === "event");
|
|
484
|
+
for (const T of y)
|
|
485
|
+
await this.route(s, [], T.event, i, h + 1);
|
|
480
486
|
}
|
|
481
487
|
/**
|
|
482
488
|
* Schedules a handler contract for future execution
|
|
@@ -487,7 +493,7 @@ class De {
|
|
|
487
493
|
console.warn(`Contract node type is not defined for: ${n}`);
|
|
488
494
|
return;
|
|
489
495
|
}
|
|
490
|
-
const h = this.registry.orderOf(i), u = t.get("/order"), d =
|
|
496
|
+
const h = this.registry.orderOf(i), u = t.get("/order"), d = A(u) ? u.toNumber() : 0, p = this.getNextTaskId() + s, y = Me(
|
|
491
497
|
a,
|
|
492
498
|
r.seq,
|
|
493
499
|
h,
|
|
@@ -510,19 +516,19 @@ class De {
|
|
|
510
516
|
return e.source === "channel" && !!e.originNodePath && e.originNodePath !== t;
|
|
511
517
|
}
|
|
512
518
|
}
|
|
513
|
-
const
|
|
514
|
-
(t instanceof
|
|
519
|
+
const xe = (c, e, t) => {
|
|
520
|
+
(t instanceof te || t instanceof ne) && console.error(
|
|
515
521
|
`[Blue] Failed to apply patches for contract "${c}" on event ${JSON.stringify(e)}`,
|
|
516
522
|
t
|
|
517
523
|
);
|
|
518
524
|
};
|
|
519
|
-
function
|
|
520
|
-
const t = c
|
|
525
|
+
function _(c, e) {
|
|
526
|
+
const t = N(c), n = (o) => {
|
|
521
527
|
if (!S(o)) return;
|
|
522
528
|
const r = o.getContracts();
|
|
523
529
|
(!(r != null && r.checkpoint) || !E.isTypeOf(
|
|
524
530
|
r.checkpoint,
|
|
525
|
-
|
|
531
|
+
pe
|
|
526
532
|
)) && o.addContract(
|
|
527
533
|
"checkpoint",
|
|
528
534
|
e.jsonValueToNode({
|
|
@@ -535,15 +541,15 @@ function U(c, e) {
|
|
|
535
541
|
);
|
|
536
542
|
};
|
|
537
543
|
n(t);
|
|
538
|
-
for (const { absPath: o } of
|
|
544
|
+
for (const { absPath: o } of j(t, e)) {
|
|
539
545
|
const r = t.get(o);
|
|
540
546
|
S(r) && n(r);
|
|
541
547
|
}
|
|
542
|
-
return
|
|
548
|
+
return k(t);
|
|
543
549
|
}
|
|
544
|
-
function
|
|
545
|
-
const t = c
|
|
546
|
-
return S(t)
|
|
550
|
+
function Je(c, e) {
|
|
551
|
+
const t = N(c);
|
|
552
|
+
return S(t) && (oe(t, e) || t.addContract(
|
|
547
553
|
"initialized",
|
|
548
554
|
e.jsonValueToNode({
|
|
549
555
|
type: {
|
|
@@ -551,17 +557,17 @@ function je(c, e) {
|
|
|
551
557
|
blueId: f["Initialized Marker"]
|
|
552
558
|
}
|
|
553
559
|
})
|
|
554
|
-
),
|
|
560
|
+
)), k(t);
|
|
555
561
|
}
|
|
556
|
-
function
|
|
562
|
+
function oe(c, e) {
|
|
557
563
|
const t = c.getContracts();
|
|
558
564
|
return Object.values(t ?? {}).some(
|
|
559
|
-
(n) => e.isTypeOf(n,
|
|
565
|
+
(n) => e.isTypeOf(n, me, {
|
|
560
566
|
checkSchemaExtensions: !0
|
|
561
567
|
})
|
|
562
568
|
);
|
|
563
569
|
}
|
|
564
|
-
class
|
|
570
|
+
class Ve {
|
|
565
571
|
constructor(e) {
|
|
566
572
|
l(this, "contractType", "Channel Event Checkpoint");
|
|
567
573
|
l(this, "contractBlueId", f["Channel Event Checkpoint"]);
|
|
@@ -579,8 +585,8 @@ class xe {
|
|
|
579
585
|
this.cache.record(r, e, o);
|
|
580
586
|
}
|
|
581
587
|
}
|
|
582
|
-
const
|
|
583
|
-
class
|
|
588
|
+
const z = (c, e) => c.get(e) !== void 0;
|
|
589
|
+
class Fe {
|
|
584
590
|
constructor() {
|
|
585
591
|
l(this, "firstSeen", /* @__PURE__ */ new Map());
|
|
586
592
|
}
|
|
@@ -593,13 +599,13 @@ class Je {
|
|
|
593
599
|
const t = [];
|
|
594
600
|
for (const { docBase: n, event: o, eventBlueId: r } of this.firstSeen.values()) {
|
|
595
601
|
if (!o.channelName) continue;
|
|
596
|
-
const a =
|
|
602
|
+
const a = b(
|
|
597
603
|
n,
|
|
598
604
|
"contracts/checkpoint/lastEvents",
|
|
599
605
|
o.channelName
|
|
600
606
|
), s = `${a}/blueId`;
|
|
601
|
-
|
|
602
|
-
op:
|
|
607
|
+
z(e, a) ? t.push({
|
|
608
|
+
op: z(e, s) ? "replace" : "add",
|
|
603
609
|
path: s,
|
|
604
610
|
val: r
|
|
605
611
|
}) : t.push({
|
|
@@ -626,14 +632,14 @@ class P {
|
|
|
626
632
|
return e.source !== "channel";
|
|
627
633
|
}
|
|
628
634
|
}
|
|
629
|
-
class
|
|
635
|
+
class Le extends P {
|
|
630
636
|
constructor() {
|
|
631
637
|
super(...arguments);
|
|
632
638
|
l(this, "contractType", "Composite Timeline Channel");
|
|
633
639
|
l(this, "contractBlueId", f["Composite Timeline Channel"]);
|
|
634
640
|
}
|
|
635
641
|
supports(t, n, o) {
|
|
636
|
-
const r = o.getBlue().nodeToSchemaOutput(n,
|
|
642
|
+
const r = o.getBlue().nodeToSchemaOutput(n, fe);
|
|
637
643
|
return !r.channels || !t.channelName ? !1 : r.channels.includes(t.channelName);
|
|
638
644
|
}
|
|
639
645
|
handle(t, n, o, r) {
|
|
@@ -644,7 +650,7 @@ class Ve extends P {
|
|
|
644
650
|
});
|
|
645
651
|
}
|
|
646
652
|
}
|
|
647
|
-
class
|
|
653
|
+
class Ue extends P {
|
|
648
654
|
constructor() {
|
|
649
655
|
super(...arguments);
|
|
650
656
|
l(this, "contractType", "Document Update Channel");
|
|
@@ -652,10 +658,10 @@ class Fe extends P {
|
|
|
652
658
|
}
|
|
653
659
|
supports(t, n, o, r) {
|
|
654
660
|
if (!this.baseSupports(t)) return !1;
|
|
655
|
-
const a = o.getBlue().nodeToSchemaOutput(n,
|
|
661
|
+
const a = o.getBlue().nodeToSchemaOutput(n, ye), s = t.payload.get("/path");
|
|
656
662
|
if (!s || t.channelName === r) return !1;
|
|
657
663
|
const i = a.path;
|
|
658
|
-
return
|
|
664
|
+
return O(i) && s === o.resolvePath(i);
|
|
659
665
|
}
|
|
660
666
|
handle(t, n, o, r) {
|
|
661
667
|
const a = t.payload;
|
|
@@ -666,7 +672,7 @@ class Fe extends P {
|
|
|
666
672
|
});
|
|
667
673
|
}
|
|
668
674
|
}
|
|
669
|
-
class
|
|
675
|
+
class _e extends P {
|
|
670
676
|
constructor() {
|
|
671
677
|
super(...arguments);
|
|
672
678
|
l(this, "contractType", "Embedded Node Channel");
|
|
@@ -674,11 +680,11 @@ class Le extends P {
|
|
|
674
680
|
}
|
|
675
681
|
supports(t, n, o) {
|
|
676
682
|
if (!this.baseSupports(t)) return !1;
|
|
677
|
-
const r = o.getBlue().nodeToSchemaOutput(n,
|
|
683
|
+
const r = o.getBlue().nodeToSchemaOutput(n, J);
|
|
678
684
|
return m(t.originNodePath) && m(r.path) && t.originNodePath === o.resolvePath(r.path);
|
|
679
685
|
}
|
|
680
686
|
handle(t, n, o, r) {
|
|
681
|
-
const a = o.getBlue().nodeToSchemaOutput(n,
|
|
687
|
+
const a = o.getBlue().nodeToSchemaOutput(n, J), { originNodePath: s, payload: i } = t;
|
|
682
688
|
m(a.path) && s === o.resolvePath(a.path) && o.emitEvent({
|
|
683
689
|
payload: i,
|
|
684
690
|
channelName: r,
|
|
@@ -686,7 +692,7 @@ class Le extends P {
|
|
|
686
692
|
});
|
|
687
693
|
}
|
|
688
694
|
}
|
|
689
|
-
class
|
|
695
|
+
class ze {
|
|
690
696
|
constructor() {
|
|
691
697
|
l(this, "contractType", "Initialized Marker");
|
|
692
698
|
l(this, "contractBlueId", f["Initialized Marker"]);
|
|
@@ -698,7 +704,7 @@ class Ue {
|
|
|
698
704
|
handle() {
|
|
699
705
|
}
|
|
700
706
|
}
|
|
701
|
-
class
|
|
707
|
+
class He extends P {
|
|
702
708
|
constructor() {
|
|
703
709
|
super(...arguments);
|
|
704
710
|
l(this, "contractType", "Lifecycle Event Channel");
|
|
@@ -719,7 +725,7 @@ class _e extends P {
|
|
|
719
725
|
*/
|
|
720
726
|
isLifecycleEvent(t, n) {
|
|
721
727
|
const o = n.getBlue(), r = t.payload;
|
|
722
|
-
return o.isTypeOf(r,
|
|
728
|
+
return o.isTypeOf(r, ge, {
|
|
723
729
|
checkSchemaExtensions: !0
|
|
724
730
|
});
|
|
725
731
|
}
|
|
@@ -733,44 +739,44 @@ class _e extends P {
|
|
|
733
739
|
return !0;
|
|
734
740
|
try {
|
|
735
741
|
const s = o.getBlue(), i = s.nodeToJson(t.payload), h = s.nodeToJson(r);
|
|
736
|
-
return
|
|
742
|
+
return Y(i, h);
|
|
737
743
|
} catch (s) {
|
|
738
744
|
return console.warn("Error during lifecycle event pattern matching:", s), !1;
|
|
739
745
|
}
|
|
740
746
|
}
|
|
741
747
|
}
|
|
742
|
-
const
|
|
748
|
+
const H = (c, e) => {
|
|
743
749
|
const t = e.getBlue();
|
|
744
|
-
return t.isTypeOf(c.payload,
|
|
750
|
+
return t.isTypeOf(c.payload, I) || t.isTypeOf(c.payload, G);
|
|
745
751
|
};
|
|
746
|
-
class
|
|
752
|
+
class Re extends P {
|
|
747
753
|
constructor() {
|
|
748
754
|
super(...arguments);
|
|
749
755
|
l(this, "contractType", "MyOS Timeline Channel");
|
|
750
|
-
l(this, "contractBlueId",
|
|
756
|
+
l(this, "contractBlueId", Z["MyOS Timeline Channel"]);
|
|
751
757
|
}
|
|
752
758
|
supports(t, n, o) {
|
|
753
759
|
var p;
|
|
754
|
-
if (!this.baseSupports(t) || !
|
|
760
|
+
if (!this.baseSupports(t) || !H(t, o)) return !1;
|
|
755
761
|
const a = o.getBlue().nodeToSchemaOutput(
|
|
756
762
|
t.payload,
|
|
757
|
-
|
|
758
|
-
), s = o.getBlue().nodeToSchemaOutput(n,
|
|
763
|
+
G
|
|
764
|
+
), s = o.getBlue().nodeToSchemaOutput(n, Ce), i = (p = a.timeline) == null ? void 0 : p.timelineId, h = m(s.timelineId) && m(i), u = m(s.account) && m(a.account), d = m(s.email) && m(a.email);
|
|
759
765
|
return h && i === s.timelineId || u && a.account === s.account || d && a.email === s.email;
|
|
760
766
|
}
|
|
761
767
|
handle(t, n, o, r) {
|
|
762
|
-
|
|
768
|
+
H(t, o) && o.emitEvent({
|
|
763
769
|
payload: t.payload,
|
|
764
770
|
channelName: r,
|
|
765
771
|
source: "channel"
|
|
766
772
|
});
|
|
767
773
|
}
|
|
768
774
|
}
|
|
769
|
-
class
|
|
775
|
+
class Qe extends P {
|
|
770
776
|
constructor() {
|
|
771
777
|
super(...arguments);
|
|
772
778
|
l(this, "contractType", "MyOS Agent Channel");
|
|
773
|
-
l(this, "contractBlueId",
|
|
779
|
+
l(this, "contractBlueId", Z["MyOS Agent Channel"]);
|
|
774
780
|
}
|
|
775
781
|
supports(t, n, o) {
|
|
776
782
|
if (!this.baseSupports(t)) return !1;
|
|
@@ -799,10 +805,10 @@ class He extends P {
|
|
|
799
805
|
parseEventAndChannel(t, n, o) {
|
|
800
806
|
const r = o.getBlue(), a = r.nodeToSchemaOutput(
|
|
801
807
|
t.payload,
|
|
802
|
-
|
|
808
|
+
ke
|
|
803
809
|
), s = r.nodeToSchemaOutput(
|
|
804
810
|
n,
|
|
805
|
-
|
|
811
|
+
Ie
|
|
806
812
|
);
|
|
807
813
|
return { myosAgentEvent: a, myosAgentChannel: s };
|
|
808
814
|
}
|
|
@@ -839,20 +845,20 @@ class He extends P {
|
|
|
839
845
|
return !1;
|
|
840
846
|
try {
|
|
841
847
|
const s = o.getBlue(), i = s.nodeToJson(a), h = s.nodeToJson(r);
|
|
842
|
-
return
|
|
848
|
+
return Y(i, h);
|
|
843
849
|
} catch (s) {
|
|
844
850
|
return console.warn("Error during event pattern matching:", s), !1;
|
|
845
851
|
}
|
|
846
852
|
}
|
|
847
853
|
}
|
|
848
|
-
class
|
|
854
|
+
class Ke {
|
|
849
855
|
constructor() {
|
|
850
856
|
l(this, "contractType", "Operation");
|
|
851
857
|
l(this, "contractBlueId", f.Operation);
|
|
852
858
|
l(this, "role", "adapter");
|
|
853
859
|
}
|
|
854
860
|
supports(e, t, n, o) {
|
|
855
|
-
const a = n.getBlue().nodeToSchemaOutput(t,
|
|
861
|
+
const a = n.getBlue().nodeToSchemaOutput(t, Ee), s = this.parseEventPayload(e, n);
|
|
856
862
|
return m(s == null ? void 0 : s.operation) && s.operation === o && e.source === "channel" && e.channelName === a.channel;
|
|
857
863
|
}
|
|
858
864
|
async handle(e, t, n, o) {
|
|
@@ -864,23 +870,23 @@ class Re {
|
|
|
864
870
|
}
|
|
865
871
|
parseEventPayload(e, t) {
|
|
866
872
|
const n = t.getBlue();
|
|
867
|
-
if (n.isTypeOf(e.payload,
|
|
873
|
+
if (n.isTypeOf(e.payload, I, {
|
|
868
874
|
checkSchemaExtensions: !0
|
|
869
875
|
})) {
|
|
870
876
|
const o = n.nodeToSchemaOutput(
|
|
871
877
|
e.payload,
|
|
872
|
-
|
|
878
|
+
I
|
|
873
879
|
);
|
|
874
880
|
if (o.message)
|
|
875
881
|
return n.nodeToSchemaOutput(
|
|
876
882
|
o.message,
|
|
877
|
-
|
|
883
|
+
we
|
|
878
884
|
);
|
|
879
885
|
}
|
|
880
886
|
return null;
|
|
881
887
|
}
|
|
882
888
|
}
|
|
883
|
-
class
|
|
889
|
+
class Xe {
|
|
884
890
|
constructor() {
|
|
885
891
|
l(this, "contractType", "Process Embedded");
|
|
886
892
|
l(this, "role", "adapter");
|
|
@@ -890,7 +896,7 @@ class Qe {
|
|
|
890
896
|
return e.source !== "channel";
|
|
891
897
|
}
|
|
892
898
|
handle(e, t, n) {
|
|
893
|
-
const o = n.getBlue().nodeToSchemaOutput(t,
|
|
899
|
+
const o = n.getBlue().nodeToSchemaOutput(t, q);
|
|
894
900
|
for (const r of o.paths ?? [])
|
|
895
901
|
n.emitEvent({
|
|
896
902
|
...e,
|
|
@@ -898,18 +904,18 @@ class Qe {
|
|
|
898
904
|
});
|
|
899
905
|
}
|
|
900
906
|
}
|
|
901
|
-
const
|
|
907
|
+
const re = !process.env.SKIP_ISOLATED_VM;
|
|
902
908
|
let v = null;
|
|
903
|
-
if (
|
|
909
|
+
if (re)
|
|
904
910
|
try {
|
|
905
911
|
v = require("isolated-vm");
|
|
906
912
|
} catch {
|
|
907
913
|
console.warn("isolated-vm not available, using fallback evaluation method");
|
|
908
914
|
}
|
|
909
|
-
function
|
|
915
|
+
function R(c) {
|
|
910
916
|
return /\bimport\s.+\sfrom\s+['"][^'"]+['"]/.test(c) || /\bexport\s+/.test(c);
|
|
911
917
|
}
|
|
912
|
-
class
|
|
918
|
+
class D {
|
|
913
919
|
/**
|
|
914
920
|
* Main evaluation method - chooses between secure and simple evaluation strategies
|
|
915
921
|
*/
|
|
@@ -919,14 +925,14 @@ class A {
|
|
|
919
925
|
bindings: n = {},
|
|
920
926
|
options: o = {}
|
|
921
927
|
}) {
|
|
922
|
-
return !v || !
|
|
928
|
+
return !v || !re ? this.evaluateSimple(e, n, o) : this.evaluateSecure(e, n, t, o);
|
|
923
929
|
}
|
|
924
930
|
/**
|
|
925
931
|
* Fallback evaluation using Node's Function constructor
|
|
926
932
|
* Used when isolated-vm is not available
|
|
927
933
|
*/
|
|
928
934
|
static async evaluateSimple(e, t, n = {}) {
|
|
929
|
-
if (
|
|
935
|
+
if (R(e))
|
|
930
936
|
throw new Error(
|
|
931
937
|
"Static import/export syntax requires isolated-vm – start Node without SKIP_ISOLATED_VM."
|
|
932
938
|
);
|
|
@@ -947,7 +953,7 @@ class A {
|
|
|
947
953
|
`return ${e};`
|
|
948
954
|
)(...Object.values(t));
|
|
949
955
|
} catch (o) {
|
|
950
|
-
throw n.isCodeBlock ? new
|
|
956
|
+
throw n.isCodeBlock ? new F(e, o) : new V(e, o);
|
|
951
957
|
}
|
|
952
958
|
}
|
|
953
959
|
/**
|
|
@@ -965,7 +971,7 @@ class A {
|
|
|
965
971
|
n
|
|
966
972
|
);
|
|
967
973
|
let u;
|
|
968
|
-
return
|
|
974
|
+
return R(e) ? u = await this.evaluateESModule(
|
|
969
975
|
r,
|
|
970
976
|
a,
|
|
971
977
|
e,
|
|
@@ -979,7 +985,7 @@ class A {
|
|
|
979
985
|
o
|
|
980
986
|
), this.deepClone(u);
|
|
981
987
|
} catch (i) {
|
|
982
|
-
throw o.isCodeBlock ? new
|
|
988
|
+
throw o.isCodeBlock ? new F(e, i) : new V(e, i);
|
|
983
989
|
} finally {
|
|
984
990
|
a.release(), r.dispose();
|
|
985
991
|
}
|
|
@@ -1085,23 +1091,23 @@ class A {
|
|
|
1085
1091
|
return typeof e > "u" ? e : JSON.parse(JSON.stringify(e));
|
|
1086
1092
|
}
|
|
1087
1093
|
}
|
|
1088
|
-
class
|
|
1094
|
+
class W {
|
|
1089
1095
|
/**
|
|
1090
1096
|
* Creates standard bindings for workflow step execution
|
|
1091
1097
|
*/
|
|
1092
1098
|
static createStandardBindings(e, t, n) {
|
|
1093
|
-
const o = e.getBlue(), r = o.nodeToJson(t.payload, "simple"), a =
|
|
1099
|
+
const o = e.getBlue(), r = o.nodeToJson(t.payload, "simple"), a = A(r) ? r.toNumber() : r;
|
|
1094
1100
|
return {
|
|
1095
1101
|
document: (s) => {
|
|
1096
1102
|
const i = e.get(s);
|
|
1097
|
-
return
|
|
1103
|
+
return A(i) ? i.toNumber() : S(i) ? o.nodeToJson(i, "original") : i;
|
|
1098
1104
|
},
|
|
1099
1105
|
event: a,
|
|
1100
1106
|
steps: n
|
|
1101
1107
|
};
|
|
1102
1108
|
}
|
|
1103
1109
|
}
|
|
1104
|
-
function
|
|
1110
|
+
function Q(c, e) {
|
|
1105
1111
|
const { op: t, path: n, val: o, from: r } = c;
|
|
1106
1112
|
if ((t === "move" || t === "copy") && !r)
|
|
1107
1113
|
throw new Error(`${t} operation requires 'from' path`);
|
|
@@ -1114,35 +1120,35 @@ function R(c, e) {
|
|
|
1114
1120
|
};
|
|
1115
1121
|
return o !== void 0 && (a.val = o), r !== void 0 && (a.from = r), e.jsonValueToNode(a);
|
|
1116
1122
|
}
|
|
1117
|
-
function
|
|
1123
|
+
function Ye(c) {
|
|
1118
1124
|
return c.jsonValueToNode({
|
|
1119
1125
|
type: "Document Processing Initiated"
|
|
1120
1126
|
});
|
|
1121
1127
|
}
|
|
1122
|
-
class
|
|
1128
|
+
class Ze {
|
|
1123
1129
|
constructor() {
|
|
1124
1130
|
l(this, "stepType", "Update Document");
|
|
1125
1131
|
}
|
|
1126
1132
|
supports(e) {
|
|
1127
|
-
return E.isTypeOf(e,
|
|
1133
|
+
return E.isTypeOf(e, B);
|
|
1128
1134
|
}
|
|
1129
1135
|
async execute(e, t, n, o, r) {
|
|
1130
1136
|
const a = n.getBlue();
|
|
1131
|
-
if (!E.isTypeOf(e,
|
|
1137
|
+
if (!E.isTypeOf(e, B)) return;
|
|
1132
1138
|
const s = await this.evaluateChangeset(
|
|
1133
1139
|
e.get("/changeset"),
|
|
1134
1140
|
n,
|
|
1135
1141
|
t,
|
|
1136
1142
|
r
|
|
1137
|
-
), i =
|
|
1143
|
+
), i = X(e, {
|
|
1138
1144
|
op: "replace",
|
|
1139
1145
|
path: "/changeset",
|
|
1140
1146
|
val: s
|
|
1141
|
-
}), h = n.getBlue().nodeToSchemaOutput(i,
|
|
1147
|
+
}), h = n.getBlue().nodeToSchemaOutput(i, B);
|
|
1142
1148
|
for (const u of h.changeset ?? []) {
|
|
1143
1149
|
if (!u.path) continue;
|
|
1144
1150
|
const d = u.val;
|
|
1145
|
-
if ((u.op === "replace" || u.op === "add") &&
|
|
1151
|
+
if ((u.op === "replace" || u.op === "add") && O(d)) {
|
|
1146
1152
|
const p = await this.evaluateChangeValue(
|
|
1147
1153
|
d,
|
|
1148
1154
|
n,
|
|
@@ -1154,7 +1160,7 @@ class Xe {
|
|
|
1154
1160
|
path: u.path,
|
|
1155
1161
|
val: p
|
|
1156
1162
|
}), n.emitEvent({
|
|
1157
|
-
payload:
|
|
1163
|
+
payload: Q(
|
|
1158
1164
|
{
|
|
1159
1165
|
op: u.op,
|
|
1160
1166
|
path: n.resolvePath(u.path),
|
|
@@ -1165,7 +1171,7 @@ class Xe {
|
|
|
1165
1171
|
});
|
|
1166
1172
|
}
|
|
1167
1173
|
u.op === "remove" && (n.addPatch({ op: u.op, path: u.path }), n.emitEvent({
|
|
1168
|
-
payload:
|
|
1174
|
+
payload: Q(
|
|
1169
1175
|
{
|
|
1170
1176
|
op: u.op,
|
|
1171
1177
|
path: n.resolvePath(u.path),
|
|
@@ -1179,10 +1185,10 @@ class Xe {
|
|
|
1179
1185
|
async evaluateChangeset(e, t, n, o) {
|
|
1180
1186
|
const r = t.getBlue();
|
|
1181
1187
|
if (typeof e == "string" && e.startsWith("${") && e.endsWith("}")) {
|
|
1182
|
-
const a = e.slice(2, -1), s = await
|
|
1188
|
+
const a = e.slice(2, -1), s = await D.evaluate({
|
|
1183
1189
|
code: a,
|
|
1184
1190
|
ctx: t,
|
|
1185
|
-
bindings:
|
|
1191
|
+
bindings: W.createStandardBindings(
|
|
1186
1192
|
t,
|
|
1187
1193
|
n,
|
|
1188
1194
|
o
|
|
@@ -1197,10 +1203,10 @@ class Xe {
|
|
|
1197
1203
|
async evaluateChangeValue(e, t, n, o) {
|
|
1198
1204
|
const r = e.getValue(), a = t.getBlue();
|
|
1199
1205
|
if (typeof r == "string" && r.startsWith("${") && r.endsWith("}")) {
|
|
1200
|
-
const s = r.slice(2, -1), i = await
|
|
1206
|
+
const s = r.slice(2, -1), i = await D.evaluate({
|
|
1201
1207
|
code: s,
|
|
1202
1208
|
ctx: t,
|
|
1203
|
-
bindings:
|
|
1209
|
+
bindings: W.createStandardBindings(
|
|
1204
1210
|
t,
|
|
1205
1211
|
n,
|
|
1206
1212
|
o
|
|
@@ -1211,41 +1217,41 @@ class Xe {
|
|
|
1211
1217
|
return e;
|
|
1212
1218
|
}
|
|
1213
1219
|
}
|
|
1214
|
-
class
|
|
1220
|
+
class Ge {
|
|
1215
1221
|
constructor() {
|
|
1216
1222
|
l(this, "stepType", "Trigger Event");
|
|
1217
1223
|
}
|
|
1218
1224
|
supports(e) {
|
|
1219
|
-
return E.isTypeOf(e,
|
|
1225
|
+
return E.isTypeOf(e, M);
|
|
1220
1226
|
}
|
|
1221
1227
|
async execute(e, t, n) {
|
|
1222
1228
|
const o = n.getBlue();
|
|
1223
|
-
if (!E.isTypeOf(e,
|
|
1224
|
-
const r = o.nodeToSchemaOutput(e,
|
|
1229
|
+
if (!E.isTypeOf(e, M)) return;
|
|
1230
|
+
const r = o.nodeToSchemaOutput(e, M);
|
|
1225
1231
|
r.event && n.emitEvent({
|
|
1226
1232
|
payload: r.event
|
|
1227
1233
|
});
|
|
1228
1234
|
}
|
|
1229
1235
|
}
|
|
1230
|
-
class
|
|
1236
|
+
class et {
|
|
1231
1237
|
constructor() {
|
|
1232
1238
|
l(this, "stepType", "JavaScript Code");
|
|
1233
1239
|
}
|
|
1234
1240
|
supports(e) {
|
|
1235
|
-
return E.isTypeOf(e,
|
|
1241
|
+
return E.isTypeOf(e, $);
|
|
1236
1242
|
}
|
|
1237
1243
|
async execute(e, t, n, o, r) {
|
|
1238
|
-
if (!E.isTypeOf(e,
|
|
1244
|
+
if (!E.isTypeOf(e, $)) return;
|
|
1239
1245
|
const a = n.getBlue(), s = a.nodeToSchemaOutput(
|
|
1240
1246
|
e,
|
|
1241
|
-
|
|
1247
|
+
$
|
|
1242
1248
|
);
|
|
1243
1249
|
if (!s.code)
|
|
1244
1250
|
throw new Error("JavaScript code is required");
|
|
1245
|
-
const i = await
|
|
1251
|
+
const i = await D.evaluate({
|
|
1246
1252
|
code: s.code,
|
|
1247
1253
|
ctx: n,
|
|
1248
|
-
bindings:
|
|
1254
|
+
bindings: W.createStandardBindings(n, t, r),
|
|
1249
1255
|
options: {
|
|
1250
1256
|
isCodeBlock: !0,
|
|
1251
1257
|
timeout: 500
|
|
@@ -1262,13 +1268,13 @@ class Ze {
|
|
|
1262
1268
|
return i;
|
|
1263
1269
|
}
|
|
1264
1270
|
}
|
|
1265
|
-
const
|
|
1266
|
-
new
|
|
1267
|
-
new
|
|
1268
|
-
new
|
|
1271
|
+
const tt = [
|
|
1272
|
+
new Ze(),
|
|
1273
|
+
new Ge(),
|
|
1274
|
+
new et()
|
|
1269
1275
|
];
|
|
1270
|
-
class
|
|
1271
|
-
constructor(e =
|
|
1276
|
+
class ae {
|
|
1277
|
+
constructor(e = tt) {
|
|
1272
1278
|
l(this, "contractType", "Sequential Workflow");
|
|
1273
1279
|
l(this, "contractBlueId", f["Sequential Workflow"]);
|
|
1274
1280
|
l(this, "role", "handler");
|
|
@@ -1282,7 +1288,7 @@ class re {
|
|
|
1282
1288
|
supports(e, t, n) {
|
|
1283
1289
|
const a = n.getBlue().nodeToSchemaOutput(
|
|
1284
1290
|
t,
|
|
1285
|
-
|
|
1291
|
+
ve
|
|
1286
1292
|
).channel;
|
|
1287
1293
|
return e.source === "channel" && e.channelName === a;
|
|
1288
1294
|
}
|
|
@@ -1308,20 +1314,20 @@ class re {
|
|
|
1308
1314
|
}
|
|
1309
1315
|
}
|
|
1310
1316
|
}
|
|
1311
|
-
class
|
|
1317
|
+
class nt {
|
|
1312
1318
|
constructor(e) {
|
|
1313
1319
|
l(this, "contractType", "Sequential Workflow Operation");
|
|
1314
1320
|
l(this, "contractBlueId", f["Sequential Workflow Operation"]);
|
|
1315
1321
|
l(this, "role", "handler");
|
|
1316
1322
|
l(this, "sequentialWorkflowProcessor");
|
|
1317
|
-
this.sequentialWorkflowProcessor = e || new
|
|
1323
|
+
this.sequentialWorkflowProcessor = e || new ae();
|
|
1318
1324
|
}
|
|
1319
1325
|
supports(e, t, n) {
|
|
1320
1326
|
const a = n.getBlue().nodeToSchemaOutput(
|
|
1321
1327
|
t,
|
|
1322
|
-
|
|
1328
|
+
be
|
|
1323
1329
|
).operation, s = e.channelName;
|
|
1324
|
-
return e.source === "channel" &&
|
|
1330
|
+
return e.source === "channel" && O(s) && O(a) && s === a;
|
|
1325
1331
|
}
|
|
1326
1332
|
async handle(e, t, n, o) {
|
|
1327
1333
|
try {
|
|
@@ -1334,8 +1340,8 @@ class et {
|
|
|
1334
1340
|
}
|
|
1335
1341
|
}
|
|
1336
1342
|
}
|
|
1337
|
-
const
|
|
1338
|
-
class
|
|
1343
|
+
const K = (c) => E.isTypeOf(c.payload, I);
|
|
1344
|
+
class ot extends P {
|
|
1339
1345
|
constructor() {
|
|
1340
1346
|
super(...arguments);
|
|
1341
1347
|
l(this, "contractType", "Timeline Channel");
|
|
@@ -1343,59 +1349,59 @@ class tt extends P {
|
|
|
1343
1349
|
}
|
|
1344
1350
|
supports(t, n, o) {
|
|
1345
1351
|
var u;
|
|
1346
|
-
if (!this.baseSupports(t) || !
|
|
1352
|
+
if (!this.baseSupports(t) || !K(t)) return !1;
|
|
1347
1353
|
const a = o.getBlue().nodeToSchemaOutput(
|
|
1348
1354
|
t.payload,
|
|
1349
|
-
|
|
1355
|
+
I
|
|
1350
1356
|
), s = o.getBlue().nodeToSchemaOutput(n, Te), i = (u = a.timeline) == null ? void 0 : u.timelineId;
|
|
1351
1357
|
return m(s.timelineId) && m(i) && i === s.timelineId;
|
|
1352
1358
|
}
|
|
1353
1359
|
handle(t, n, o, r) {
|
|
1354
|
-
|
|
1360
|
+
K(t) && o.emitEvent({
|
|
1355
1361
|
payload: t.payload,
|
|
1356
1362
|
channelName: r,
|
|
1357
1363
|
source: "channel"
|
|
1358
1364
|
});
|
|
1359
1365
|
}
|
|
1360
1366
|
}
|
|
1361
|
-
const
|
|
1362
|
-
new
|
|
1367
|
+
const rt = [
|
|
1368
|
+
new Xe(),
|
|
1363
1369
|
// channels
|
|
1364
|
-
new Le(),
|
|
1365
|
-
new Fe(),
|
|
1366
|
-
new tt(),
|
|
1367
|
-
new ze(),
|
|
1368
|
-
new He(),
|
|
1369
|
-
new Ve(),
|
|
1370
1370
|
new _e(),
|
|
1371
|
+
new Ue(),
|
|
1372
|
+
new ot(),
|
|
1371
1373
|
new Re(),
|
|
1374
|
+
new Qe(),
|
|
1375
|
+
new Le(),
|
|
1376
|
+
new He(),
|
|
1377
|
+
new Ke(),
|
|
1372
1378
|
// sequential workflows
|
|
1373
|
-
new
|
|
1374
|
-
new
|
|
1379
|
+
new ae(),
|
|
1380
|
+
new nt(),
|
|
1375
1381
|
// markers
|
|
1376
|
-
new
|
|
1382
|
+
new ze()
|
|
1377
1383
|
];
|
|
1378
|
-
class
|
|
1384
|
+
class ht {
|
|
1379
1385
|
/**
|
|
1380
1386
|
* Creates a new document processor
|
|
1381
1387
|
*
|
|
1382
1388
|
* @param processors - Initial list of processors to register
|
|
1383
1389
|
*/
|
|
1384
|
-
constructor(e, t =
|
|
1390
|
+
constructor(e, t = rt) {
|
|
1385
1391
|
l(this, "taskCounter", 0);
|
|
1386
1392
|
l(this, "eventCounter", 0);
|
|
1387
1393
|
l(this, "registry");
|
|
1388
1394
|
l(this, "queue");
|
|
1389
1395
|
l(this, "router");
|
|
1390
|
-
l(this, "checkpointCache", new
|
|
1391
|
-
this.blue = e, this.registry = new
|
|
1396
|
+
l(this, "checkpointCache", new Fe());
|
|
1397
|
+
this.blue = e, this.registry = new Ae(t), this.queue = new qe(), this.router = new je(
|
|
1392
1398
|
this.blue,
|
|
1393
1399
|
this.registry,
|
|
1394
1400
|
this.queue,
|
|
1395
1401
|
() => ++this.taskCounter,
|
|
1396
1402
|
() => ++this.eventCounter
|
|
1397
1403
|
), this.register(
|
|
1398
|
-
new
|
|
1404
|
+
new Ve(this.checkpointCache),
|
|
1399
1405
|
9999
|
|
1400
1406
|
);
|
|
1401
1407
|
}
|
|
@@ -1415,13 +1421,13 @@ class lt {
|
|
|
1415
1421
|
* @returns Processing result with final state and emitted events
|
|
1416
1422
|
*/
|
|
1417
1423
|
async initialize(e) {
|
|
1418
|
-
let t =
|
|
1424
|
+
let t = _(k(e), this.blue);
|
|
1419
1425
|
const n = {
|
|
1420
|
-
payload:
|
|
1426
|
+
payload: Ye(this.blue)
|
|
1421
1427
|
}, o = [n.payload];
|
|
1422
1428
|
await this.router.route(t, [], n, 0);
|
|
1423
1429
|
const r = await this.drainQueue(t);
|
|
1424
|
-
return t = r.state, o.push(...r.emitted), t =
|
|
1430
|
+
return t = r.state, o.push(...r.emitted), t = Je(t, this.blue), { state: N(t), emitted: o };
|
|
1425
1431
|
}
|
|
1426
1432
|
/**
|
|
1427
1433
|
* Processes a batch of events against the document
|
|
@@ -1431,9 +1437,9 @@ class lt {
|
|
|
1431
1437
|
* @returns Processing result with final state and emitted events
|
|
1432
1438
|
*/
|
|
1433
1439
|
async processEvents(e, t) {
|
|
1434
|
-
let n =
|
|
1440
|
+
let n = _(k(e), this.blue);
|
|
1435
1441
|
const o = [];
|
|
1436
|
-
if (!
|
|
1442
|
+
if (!oe(n, this.blue))
|
|
1437
1443
|
throw new Error("Document is not initialized");
|
|
1438
1444
|
for (const r of t)
|
|
1439
1445
|
try {
|
|
@@ -1442,11 +1448,11 @@ class lt {
|
|
|
1442
1448
|
const s = await this.drainQueue(n);
|
|
1443
1449
|
n = s.state, o.push(...s.emitted);
|
|
1444
1450
|
const i = this.checkpointCache.flush(n);
|
|
1445
|
-
i.length && (n =
|
|
1451
|
+
i.length && (n = U(n, i));
|
|
1446
1452
|
} finally {
|
|
1447
1453
|
this.checkpointCache.clear();
|
|
1448
1454
|
}
|
|
1449
|
-
return { state: n, emitted: o };
|
|
1455
|
+
return { state: N(n), emitted: o };
|
|
1450
1456
|
}
|
|
1451
1457
|
/**
|
|
1452
1458
|
* Drains the task queue and applies all actions
|
|
@@ -1466,32 +1472,32 @@ class lt {
|
|
|
1466
1472
|
console.warn(`No processor registered for contract: ${h}`);
|
|
1467
1473
|
continue;
|
|
1468
1474
|
}
|
|
1469
|
-
const w = new
|
|
1475
|
+
const w = new ee(
|
|
1470
1476
|
() => t,
|
|
1471
1477
|
s,
|
|
1472
1478
|
this.blue,
|
|
1473
|
-
async (
|
|
1474
|
-
for (const g of
|
|
1479
|
+
async (T) => {
|
|
1480
|
+
for (const g of T)
|
|
1475
1481
|
if (g.kind === "patch") {
|
|
1476
|
-
const
|
|
1477
|
-
for (const C of
|
|
1478
|
-
const
|
|
1479
|
-
(
|
|
1480
|
-
),
|
|
1481
|
-
|
|
1482
|
+
const se = j(t, this.blue);
|
|
1483
|
+
for (const C of se) {
|
|
1484
|
+
const ce = g.patch.op === "move" || g.patch.op === "copy" ? [g.patch.from, g.patch.path] : [g.patch.path], x = w.getNodePath(), ie = ce.some(
|
|
1485
|
+
(ue) => L(ue, C.absPath)
|
|
1486
|
+
), le = L(
|
|
1487
|
+
x,
|
|
1482
1488
|
C.absPath
|
|
1483
1489
|
);
|
|
1484
|
-
if (
|
|
1485
|
-
throw new
|
|
1490
|
+
if (ie && !le)
|
|
1491
|
+
throw new ne(
|
|
1486
1492
|
g.patch,
|
|
1487
1493
|
C.absPath,
|
|
1488
|
-
|
|
1494
|
+
x
|
|
1489
1495
|
);
|
|
1490
1496
|
}
|
|
1491
1497
|
try {
|
|
1492
|
-
t =
|
|
1498
|
+
t = U(t, [g.patch]);
|
|
1493
1499
|
} catch (C) {
|
|
1494
|
-
throw
|
|
1500
|
+
throw xe(h, d, C), C;
|
|
1495
1501
|
}
|
|
1496
1502
|
} else g.kind === "event" && (n.push(g.event.payload), await this.router.route(t, [], g.event, s.key[5]));
|
|
1497
1503
|
}
|
|
@@ -1502,18 +1508,18 @@ class lt {
|
|
|
1502
1508
|
}
|
|
1503
1509
|
}
|
|
1504
1510
|
export {
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1511
|
+
ht as BlueDocumentProcessor,
|
|
1512
|
+
Ve as ChannelEventCheckpointProcessor,
|
|
1513
|
+
Le as CompositeTimelineChannelProcessor,
|
|
1514
|
+
Ue as DocumentUpdateChannelProcessor,
|
|
1515
|
+
_e as EmbeddedNodeChannelProcessor,
|
|
1516
|
+
ze as InitializedMarkerProcessor,
|
|
1517
|
+
He as LifecycleEventChannelProcessor,
|
|
1518
|
+
Qe as MyOSAgentChannelProcessor,
|
|
1519
|
+
Re as MyOSTimelineChannelProcessor,
|
|
1520
|
+
Ke as OperationProcessor,
|
|
1521
|
+
Xe as ProcessEmbeddedProcessor,
|
|
1522
|
+
nt as SequentialWorkflowOperationProcessor,
|
|
1523
|
+
ae as SequentialWorkflowProcessor,
|
|
1524
|
+
ot as TimelineChannelProcessor
|
|
1519
1525
|
};
|
package/dist/testUtils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"testUtils.d.ts","sourceRoot":"","sources":["../src/testUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,IAAI,EAAE,MAAM,qBAAqB,CAAC;AAChD,OAAO,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAC;AAEhE,eAAO,MAAM,gBAAgB,QACtB,OAAO,QACN;IACJ,IAAI,EAAE,IAAI,CAAC;IACX,iBAAiB,EAAE,qBAAqB,CAAC;CAC1C;;
|
|
1
|
+
{"version":3,"file":"testUtils.d.ts","sourceRoot":"","sources":["../src/testUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,IAAI,EAAE,MAAM,qBAAqB,CAAC;AAChD,OAAO,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAC;AAEhE,eAAO,MAAM,gBAAgB,QACtB,OAAO,QACN;IACJ,IAAI,EAAE,IAAI,CAAC;IACX,iBAAiB,EAAE,qBAAqB,CAAC;CAC1C;;EAaF,CAAC;AAEF,eAAO,MAAM,oBAAoB,QAC1B,MAAM,QACL;IACJ,IAAI,EAAE,IAAI,CAAC;IACX,iBAAiB,EAAE,qBAAqB,CAAC;CAC1C;;EAaF,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"checkpoint.d.ts","sourceRoot":"","sources":["../../src/utils/checkpoint.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"checkpoint.d.ts","sourceRoot":"","sources":["../../src/utils/checkpoint.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAGxC,OAAO,EAAE,IAAI,EAAsB,MAAM,qBAAqB,CAAC;AAM/D,wBAAgB,yBAAyB,CAAC,GAAG,EAAE,YAAY,EAAE,IAAI,EAAE,IAAI,0CAyCtE"}
|
package/dist/utils/document.d.ts
CHANGED
|
@@ -1,19 +1,22 @@
|
|
|
1
1
|
import { DocumentNode } from '../types';
|
|
2
2
|
import { BlueNodePatch, Blue } from '@blue-labs/language';
|
|
3
|
-
export declare const ENABLE_IMMUTABILITY = true;
|
|
4
3
|
type EmbeddedSpec = {
|
|
5
4
|
absPath: string;
|
|
6
5
|
contractPath: string;
|
|
7
6
|
};
|
|
7
|
+
/**
|
|
8
|
+
* Makes a document immutable during processing for safety
|
|
9
|
+
*/
|
|
10
|
+
export declare function freeze(doc: DocumentNode): DocumentNode;
|
|
11
|
+
/**
|
|
12
|
+
* Creates a mutable copy of a document for processing
|
|
13
|
+
*/
|
|
14
|
+
export declare function mutable(doc: DocumentNode): DocumentNode;
|
|
8
15
|
export declare function collectEmbeddedPaths(doc: DocumentNode, blue: Blue, base?: string, out?: EmbeddedSpec[]): EmbeddedSpec[];
|
|
9
16
|
export declare function isInside(target: string, root: string): boolean;
|
|
10
17
|
/**
|
|
11
18
|
* Apply a collection of patches to a document
|
|
12
19
|
*/
|
|
13
20
|
export declare function applyPatches(document: DocumentNode, patches: BlueNodePatch[]): DocumentNode;
|
|
14
|
-
/**
|
|
15
|
-
* Create an immutable copy of a document if immutability is enabled
|
|
16
|
-
*/
|
|
17
|
-
export declare function createImmutableDocument(document: DocumentNode): DocumentNode;
|
|
18
21
|
export {};
|
|
19
22
|
//# sourceMappingURL=document.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"document.d.ts","sourceRoot":"","sources":["../../src/utils/document.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAIxC,OAAO,EAEL,KAAK,aAAa,EAElB,IAAI,EACL,MAAM,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"document.d.ts","sourceRoot":"","sources":["../../src/utils/document.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAIxC,OAAO,EAEL,KAAK,aAAa,EAElB,IAAI,EACL,MAAM,qBAAqB,CAAC;AAM7B,KAAK,YAAY,GAAG;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,YAAY,EAAE,MAAM,CAAA;CAAE,CAAC;AAE9D;;GAEG;AACH,wBAAgB,MAAM,CAAC,GAAG,EAAE,YAAY,GAAG,YAAY,CAEtD;AAED;;GAEG;AACH,wBAAgB,OAAO,CAAC,GAAG,EAAE,YAAY,GAAG,YAAY,CAEvD;AAED,wBAAgB,oBAAoB,CAClC,GAAG,EAAE,YAAY,EACjB,IAAI,EAAE,IAAI,EACV,IAAI,SAAM,EACV,GAAG,GAAE,YAAY,EAAO,GACvB,YAAY,EAAE,CA4BhB;AAKD,wBAAgB,QAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAI9D;AAED;;GAEG;AACH,wBAAgB,YAAY,CAC1B,QAAQ,EAAE,YAAY,EACtB,OAAO,EAAE,aAAa,EAAE,GACvB,YAAY,CAcd"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"initialized.d.ts","sourceRoot":"","sources":["../../src/utils/initialized.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"initialized.d.ts","sourceRoot":"","sources":["../../src/utils/initialized.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAGxC,OAAO,EAAE,IAAI,EAAE,MAAM,qBAAqB,CAAC;AAG3C,wBAAgB,yBAAyB,CACvC,GAAG,EAAE,YAAY,EACjB,IAAI,EAAE,IAAI,GACT,YAAY,CAsBd;AAED,wBAAgB,aAAa,CAAC,GAAG,EAAE,YAAY,EAAE,IAAI,EAAE,IAAI,GAAG,OAAO,CAOpE"}
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blue-labs/document-processor",
|
|
3
|
-
"version": "1.27.
|
|
3
|
+
"version": "1.27.1",
|
|
4
4
|
"dependencies": {
|
|
5
|
-
"@blue-labs/language": "1.27.
|
|
6
|
-
"@blue-labs/shared-utils": "1.27.
|
|
5
|
+
"@blue-labs/language": "1.27.1",
|
|
6
|
+
"@blue-labs/shared-utils": "1.27.1",
|
|
7
7
|
"isolated-vm": "^5.0.4"
|
|
8
8
|
},
|
|
9
9
|
"main": "./dist/index.js",
|