@jagreehal/workflow 1.5.0 → 1.6.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/README.md +664 -350
- package/dist/core.cjs +1 -1
- package/dist/core.cjs.map +1 -1
- package/dist/core.d.cts +179 -1
- package/dist/core.d.ts +179 -1
- package/dist/core.js +1 -1
- package/dist/core.js.map +1 -1
- package/dist/index.cjs +5 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +5 -5
- package/dist/index.js.map +1 -1
- package/dist/visualize.cjs +6 -6
- package/dist/visualize.cjs.map +1 -1
- package/dist/visualize.js +6 -6
- package/dist/visualize.js.map +1 -1
- package/dist/workflow.cjs +1 -1
- package/dist/workflow.cjs.map +1 -1
- package/dist/workflow.js +1 -1
- package/dist/workflow.js.map +1 -1
- package/docs/coming-from-neverthrow.md +920 -0
- package/docs/visualize-examples.md +330 -0
- package/package.json +2 -1
package/dist/visualize.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
"use strict";var X=Object.defineProperty;var fe=Object.getOwnPropertyDescriptor;var he=Object.getOwnPropertyNames;var me=Object.prototype.hasOwnProperty;var ge=(e,n)=>{for(var t in n)X(e,t,{get:n[t],enumerable:!0})}
|
|
2
|
-
`)}}}function J(e,n,t,r){let s=[];for(let o of e)
|
|
3
|
-
`)}}}function Z(e,n,t){if(
|
|
4
|
-
`),
|
|
5
|
-
`).length)}function
|
|
6
|
-
`),
|
|
1
|
+
"use strict";var X=Object.defineProperty;var fe=Object.getOwnPropertyDescriptor;var he=Object.getOwnPropertyNames;var me=Object.prototype.hasOwnProperty;var ge=(e,n)=>{for(var t in n)X(e,t,{get:n[t],enumerable:!0})},$e=(e,n,t,r)=>{if(n&&typeof n=="object"||typeof n=="function")for(let s of he(n))!me.call(e,s)&&s!==t&&X(e,s,{get:()=>n[s],enumerable:!(r=fe(n,s))||r.enumerable});return e};var ke=e=>$e(X({},"__esModule",{value:!0}),e);var Ae={};ge(Ae,{asciiRenderer:()=>W,createEventCollector:()=>Ve,createIRBuilder:()=>F,createLiveVisualizer:()=>le,createParallelDetector:()=>re,createVisualizer:()=>H,defaultColorScheme:()=>M,detectParallelGroups:()=>z,hasChildren:()=>Se,isDecisionNode:()=>A,isParallelNode:()=>P,isRaceNode:()=>V,isSequenceNode:()=>ye,isStepNode:()=>C,mermaidRenderer:()=>j,trackDecision:()=>Y,trackIf:()=>ue,trackSwitch:()=>pe,visualizeEvents:()=>Pe});module.exports=ke(Ae);function N(e){if(e<1e3)return`${Math.round(e)}ms`;if(e<6e4)return`${(e/1e3).toFixed(1).replace(/\.0$/,"")}s`;let n=Math.floor(e/6e4),t=Math.round(e%6e4/1e3);return t===0?`${n}m`:`${n}m ${t}s`}function Q(){return`node_${Date.now()}_${Math.random().toString(36).slice(2,8)}`}function we(e){for(let n of e)if((n.type==="parallel"||n.type==="race"||n.type==="sequence")&&!n.id.startsWith("detected_")||"decisionId"in n)return!0;return!1}function z(e,n={}){if(we(e))return e;let{minOverlapMs:t=0,maxGapMs:r=5}=n,s=[],o=[];for(let c=0;c<e.length;c++){let a=e[c];a.type==="step"&&a.startTs!==void 0?s.push({node:a,startTs:a.startTs,endTs:a.endTs??a.startTs+(a.durationMs??0),originalIndex:c}):o.push({node:a,originalIndex:c})}if(s.length<=1)return e;s.sort((c,a)=>c.startTs-a.startTs);let u=[],h=[s[0]];for(let c=1;c<s.length;c++){let a=s[c],l=Math.min(...h.map(y=>y.startTs)),g=Math.max(...h.map(y=>y.endTs)),$=a.startTs<=l+r,k=a.startTs<g;if(!$&&!k){u.push(h),h=[a];continue}let w=k?Math.min(a.endTs,g)-a.startTs:0;$||w>=t?h.push(a):(u.push(h),h=[a])}u.push(h);let p=[];for(let c of u){let a=Math.min(...c.map(l=>l.originalIndex));if(c.length===1)p.push({node:c[0].node,position:a});else{let l=c.map(w=>w.node),g=Math.min(...c.map(w=>w.startTs)),$=Math.max(...c.map(w=>w.endTs)),k={type:"parallel",id:`detected_parallel_${g}`,name:`${l.length} parallel steps`,state:be(l),mode:"all",children:l,startTs:g,endTs:$,durationMs:$-g};p.push({node:k,position:a})}}for(let{node:c,originalIndex:a}of o)p.push({node:c,position:a});return p.sort((c,a)=>c.position-a.position),p.map(c=>c.node)}function be(e){return e.some(o=>o.state==="error")?"error":e.some(o=>o.state==="running")?"running":e.some(o=>o.state==="pending")?"pending":(e.every(o=>o.state==="success"||o.state==="cached"),"success")}function re(e={}){return{detect:n=>z(n,e)}}function F(e={}){let{detectParallel:n=!0,parallelDetection:t}=e,r,s,o="pending",u,h,p=new Map,c=[],a=[],l=[],g=Date.now(),$=g;function k(i){return i.stepId??i.stepKey??i.name??Q()}function w(i){if(a.length>0){let d=a[a.length-1];for(let E of d.branches.values())if(E.taken){E.children.push(i),$=Date.now();return}let m=Array.from(d.branches.values())[0];if(m){m.children.push(i),$=Date.now();return}}c.length>0?c[c.length-1].children.push(i):l.push(i),$=Date.now()}function y(i){switch(i.type){case"workflow_start":r=i.workflowId,s=i.ts,o="running",g=Date.now(),$=g;break;case"workflow_success":o="success",h=i.durationMs,$=Date.now();break;case"workflow_error":o="error",u=i.error,h=i.durationMs,$=Date.now();break;case"step_start":{let d=k(i);p.set(d,{id:d,name:i.name,key:i.stepKey,startTs:i.ts,retryCount:0,timedOut:!1}),$=Date.now();break}case"step_success":{let d=k(i),m=p.get(d);if(m){let E={type:"step",id:m.id,name:m.name,key:m.key,state:"success",startTs:m.startTs,endTs:i.ts,durationMs:i.durationMs,...m.retryCount>0&&{retryCount:m.retryCount},...m.timedOut&&{timedOut:!0,timeoutMs:m.timeoutMs}};w(E),p.delete(d)}break}case"step_error":{let d=k(i),m=p.get(d);if(m){let E={type:"step",id:m.id,name:m.name,key:m.key,state:"error",startTs:m.startTs,endTs:i.ts,durationMs:i.durationMs,error:i.error,...m.retryCount>0&&{retryCount:m.retryCount},...m.timedOut&&{timedOut:!0,timeoutMs:m.timeoutMs}};w(E),p.delete(d)}break}case"step_aborted":{let d=k(i),m=p.get(d);if(m){let E={type:"step",id:m.id,name:m.name,key:m.key,state:"aborted",startTs:m.startTs,endTs:i.ts,durationMs:i.durationMs,...m.retryCount>0&&{retryCount:m.retryCount},...m.timedOut&&{timedOut:!0,timeoutMs:m.timeoutMs}};w(E),p.delete(d)}break}case"step_cache_hit":{let m={type:"step",id:k(i),name:i.name,key:i.stepKey,state:"cached",startTs:i.ts,endTs:i.ts,durationMs:0};w(m);break}case"step_cache_miss":break;case"step_complete":break;case"step_timeout":{let d=k(i),m=p.get(d);m&&(m.timedOut=!0,m.timeoutMs=i.timeoutMs),$=Date.now();break}case"step_retry":{let d=k(i),m=p.get(d);m&&(m.retryCount=(i.attempt??1)-1),$=Date.now();break}case"step_retries_exhausted":$=Date.now();break;case"step_skipped":{let m={type:"step",id:k(i),name:i.name,key:i.stepKey,state:"skipped",startTs:i.ts,endTs:i.ts,durationMs:0};w(m);break}}}function x(i){if(i.type==="scope_start")c.push({id:i.scopeId,name:i.name,type:i.scopeType,startTs:i.ts,children:[]}),$=Date.now();else if(i.type==="scope_end"){let d=c.pop();if(d){let m=d.type==="race"?{type:"race",id:d.id,name:d.name,state:R(d.children),startTs:d.startTs,endTs:i.ts,durationMs:i.durationMs,children:d.children,winnerId:i.winnerId}:{type:"parallel",id:d.id,name:d.name,state:R(d.children),startTs:d.startTs,endTs:i.ts,durationMs:i.durationMs,children:d.children,mode:d.type==="allSettled"?"allSettled":"all"};w(m)}}}function T(i){if(i.type==="decision_start")a.push({id:i.decisionId,name:i.name,condition:i.condition,decisionValue:i.decisionValue,startTs:i.ts,branches:new Map}),$=Date.now();else if(i.type==="decision_branch"){let d=a[a.length-1];if(d&&d.id===i.decisionId){let m=i.branchLabel,E=d.branches.get(m);E?E.taken=i.taken:d.branches.set(m,{label:i.branchLabel,condition:i.condition,taken:i.taken,children:[]}),$=Date.now()}}else if(i.type==="decision_end"){let d=a.pop();if(d&&d.id===i.decisionId){let m=Array.from(d.branches.values()),E={type:"decision",id:d.id,name:d.name,state:R(m.flatMap(I=>I.taken?I.children:[])),startTs:d.startTs,endTs:i.ts,durationMs:i.durationMs,condition:d.condition,decisionValue:d.decisionValue,branchTaken:i.branchTaken??d.branchTaken,branches:m};w(E)}}}function R(i){return i.length===0?"success":i.some(I=>I.state==="error")?"error":i.every(I=>I.state==="success"||I.state==="cached")?"success":i.some(I=>I.state==="running")?"running":"pending"}function b(){let i=[...l];for(let[,d]of p)i.push({type:"step",id:d.id,name:d.name,key:d.key,state:"running",startTs:d.startTs,...d.retryCount>0&&{retryCount:d.retryCount},...d.timedOut&&{timedOut:!0,timeoutMs:d.timeoutMs}});return i}function O(){let i=b();return n&&(i=z(i,t)),{root:{type:"workflow",id:r??Q(),workflowId:r??"unknown",state:o,startTs:s,durationMs:h,children:i,error:u},metadata:{createdAt:g,lastUpdatedAt:$}}}function B(){r=void 0,s=void 0,o="pending",u=void 0,h=void 0,p.clear(),c.length=0,a.length=0,l=[],g=Date.now(),$=g}return{handleEvent:y,handleScopeEvent:x,handleDecisionEvent:T,getIR:O,reset:B,get hasActiveSteps(){return p.size>0},get state(){return o}}}function C(e){return e.type==="step"}function ye(e){return e.type==="sequence"}function P(e){return e.type==="parallel"}function V(e){return e.type==="race"}function A(e){return e.type==="decision"}function Se(e){return"children"in e||e.type==="decision"&&"branches"in e}var ee="\x1B[0m",ve="\x1B[1m",ie="\x1B[2m",Ee="\x1B[31m",xe="\x1B[32m",De="\x1B[33m",Ie="\x1B[34m",oe="\x1B[90m",Ne="\x1B[37m";function q(e,n){return n?`${n}${e}${ee}`:e}function G(e){return`${ve}${e}${ee}`}function v(e){return`${ie}${e}${ee}`}var M={pending:Ne,running:De,success:xe,error:Ee,aborted:oe,cached:Ie,skipped:ie+oe};function Te(e){switch(e){case"pending":return"\u25CB";case"running":return"\u27F3";case"success":return"\u2713";case"error":return"\u2717";case"aborted":return"\u2298";case"cached":return"\u21BA";case"skipped":return"\u2298"}}function U(e,n){let t=Te(e);return q(t,n[e])}function te(e,n,t){return q(e,t[n])}function se(e){return e.replace(/\x1b\[[0-9;]*m/g,"")}var f={topLeft:"\u250C",topRight:"\u2510",bottomLeft:"\u2514",bottomRight:"\u2518",horizontal:"\u2500",vertical:"\u2502",teeRight:"\u251C",teeLeft:"\u2524",teeDown:"\u252C",teeUp:"\u2534",cross:"\u253C"};function ae(e,n){let t=se(e).length,r=Math.max(0,n-t);return e+" ".repeat(r)}function Re(e,n){if(!n)return f.horizontal.repeat(e);let t=` ${n} `,r=e-t.length;if(r<4)return f.horizontal.repeat(e);let s=2,o=r-s;return f.horizontal.repeat(s)+t+f.horizontal.repeat(o)}function W(){return{name:"ascii",supportsLive:!0,render(e,n){let t={...M,...n.colors},r=n.terminalWidth??60,s=r-4,o=[],u=e.root.name??"workflow",h=G(u);o.push(`${f.topLeft}${Re(r-2,h)}${f.topRight}`),o.push(`${f.vertical}${" ".repeat(r-2)}${f.vertical}`);let p=J(e.root.children,n,t,0);for(let c of p)o.push(`${f.vertical} ${ae(c,s)}${f.vertical}`);if(o.push(`${f.vertical}${" ".repeat(r-2)}${f.vertical}`),e.root.durationMs!==void 0&&n.showTimings){let c=e.root.state==="success"?"Completed":"Failed",l=`${te(c,e.root.state,t)} in ${N(e.root.durationMs)}`;o.push(`${f.vertical} ${ae(l,s)}${f.vertical}`),o.push(`${f.vertical}${" ".repeat(r-2)}${f.vertical}`)}return o.push(`${f.bottomLeft}${f.horizontal.repeat(r-2)}${f.bottomRight}`),o.join(`
|
|
2
|
+
`)}}}function J(e,n,t,r){let s=[];for(let o of e)C(o)?s.push(ne(o,n,t)):P(o)?s.push(...Oe(o,n,t,r)):V(o)?s.push(...Me(o,n,t,r)):A(o)&&s.push(...Ce(o,n,t,r));return s}function ne(e,n,t){let r=U(e.state,t),s=e.name??e.key??"step",o=te(s,e.state,t),u=`${r} ${o}`;if(n.showKeys&&e.key&&(u+=v(` [key: ${e.key}]`)),e.input!==void 0){let h=typeof e.input=="string"?e.input:JSON.stringify(e.input).slice(0,30);u+=v(` [in: ${h}${h.length>=30?"...":""}]`)}if(e.output!==void 0&&e.state==="success"){let h=typeof e.output=="string"?e.output:JSON.stringify(e.output).slice(0,30);u+=v(` [out: ${h}${h.length>=30?"...":""}]`)}if(n.showTimings&&e.durationMs!==void 0&&(u+=v(` [${N(e.durationMs)}]`)),e.retryCount!==void 0&&e.retryCount>0&&(u+=v(` [${e.retryCount} ${e.retryCount===1?"retry":"retries"}]`)),e.timedOut){let h=e.timeoutMs!==void 0?` ${e.timeoutMs}ms`:"";u+=v(` [timeout${h}]`)}return u}function Oe(e,n,t,r){let s=[],o=" ".repeat(r),u=U(e.state,t),h=e.name??"parallel",p=e.mode==="allSettled"?" (allSettled)":"";if(s.push(`${o}${f.teeRight}${f.teeDown}${f.horizontal} ${u} ${G(h)}${p}`),e.children.length===0)s.push(`${o}${f.vertical} ${v("(operations not individually tracked)")}`),s.push(`${o}${f.vertical} ${v("(wrap each operation with step() to see individual steps)")}`);else for(let c=0;c<e.children.length;c++){let a=e.children[c],g=c===e.children.length-1?`${o}${f.vertical} ${f.bottomLeft}`:`${o}${f.vertical} ${f.teeRight}`;if(C(a))s.push(`${g} ${ne(a,n,t)}`);else{let $=J([a],n,t,r+1);for(let k of $)s.push(`${o}${f.vertical} ${k}`)}}return n.showTimings&&e.durationMs!==void 0&&s.push(`${o}${f.bottomLeft}${f.horizontal}${f.horizontal} ${v(`[${N(e.durationMs)}]`)}`),s}function Me(e,n,t,r){let s=[],o=" ".repeat(r),u=U(e.state,t),h=e.name??"race";if(s.push(`${o}${f.teeRight}\u26A1 ${u} ${G(h)}`),e.children.length===0)s.push(`${o}${f.vertical} ${v("(operations not individually tracked)")}`),s.push(`${o}${f.vertical} ${v("(wrap each operation with step() to see individual steps)")}`);else for(let p=0;p<e.children.length;p++){let c=e.children[p],l=p===e.children.length-1?`${o}${f.vertical} ${f.bottomLeft}`:`${o}${f.vertical} ${f.teeRight}`,$=e.winnerId&&c.id===e.winnerId?v(" (winner)"):"";if(C(c))s.push(`${l} ${ne(c,n,t)}${$}`);else{let k=J([c],n,t,r+1);for(let w of k)s.push(`${o}${f.vertical} ${w}`)}}return n.showTimings&&e.durationMs!==void 0&&s.push(`${o}${f.bottomLeft}${f.horizontal}${f.horizontal} ${v(`[${N(e.durationMs)}]`)}`),s}function Ce(e,n,t,r){let s=[],o=" ".repeat(r),u=U(e.state,t),h=e.name??"decision",p=e.condition?v(` (${e.condition})`):"",c=e.decisionValue!==void 0?v(` = ${String(e.decisionValue)}`):"",a=e.branchTaken!==void 0?v(` \u2192 ${String(e.branchTaken)}`):"";s.push(`${o}${f.teeRight}${f.teeDown}${f.horizontal} ${u} ${G(h)}${p}${c}${a}`);for(let l=0;l<e.branches.length;l++){let g=e.branches[l],k=l===e.branches.length-1?`${o}${f.vertical} ${f.bottomLeft}`:`${o}${f.vertical} ${f.teeRight}`,w=g.taken?"\u2713":"\u2298",y=g.taken?t.success:t.skipped,x=q(`${w} ${g.label}`,y),T=g.condition?v(` (${g.condition})`):"";if(s.push(`${k} ${x}${T}`),g.children.length>0){let R=J(g.children,n,t,r+1);for(let b of R)s.push(`${o}${f.vertical} ${b}`)}else g.taken||s.push(`${o}${f.vertical} ${v("(skipped)")}`)}return n.showTimings&&e.durationMs!==void 0&&s.push(`${o}${f.bottomLeft}${f.horizontal}${f.horizontal} ${v(`[${N(e.durationMs)}]`)}`),s}function _e(){return[" classDef pending fill:#f3f4f6,stroke:#9ca3af,stroke-width:2px,color:#374151"," classDef running fill:#fef3c7,stroke:#f59e0b,stroke-width:3px,color:#92400e"," classDef success fill:#d1fae5,stroke:#10b981,stroke-width:3px,color:#065f46"," classDef error fill:#fee2e2,stroke:#ef4444,stroke-width:3px,color:#991b1b"," classDef aborted fill:#f3f4f6,stroke:#6b7280,stroke-width:2px,color:#4b5563,stroke-dasharray: 5 5"," classDef cached fill:#dbeafe,stroke:#3b82f6,stroke-width:3px,color:#1e40af"," classDef skipped fill:#f9fafb,stroke:#d1d5db,stroke-width:2px,color:#6b7280,stroke-dasharray: 5 5"]}var ce=0;function K(e="node"){return`${e}_${++ce}`}function We(){ce=0}function D(e){return e.replace(/[{}[\]()]/g,"").replace(/[<>]/g,"").replace(/"/g,"'").trim()}function de(e){return D(e).replace(/[[\]]/g,"")}function j(){return{name:"mermaid",supportsLive:!1,render(e,n){We();let t=[];t.push("flowchart TD");let r="start";t.push(` ${r}(("\u25B6 Start"))`);let s=r;for(let o of e.root.children){let u=Z(o,n,t);t.push(` ${s} --> ${u.entryId}`),s=u.exitId}if(e.root.state==="success"||e.root.state==="error"){let o="finish",u=e.root.state==="success"?"\u2713":"\u2717",h=e.root.state==="success"?"Done":"Failed",p=`(("${u} ${h}"))`,c=e.root.state==="success"?":::success":":::error";t.push(` ${o}${p}${c}`),t.push(` ${s} --> ${o}`)}return t.push(""),t.push(..._e()),t.join(`
|
|
3
|
+
`)}}}function Z(e,n,t){if(C(e))return Be(e,n,t);if(P(e))return Le(e,n,t);if(V(e))return Fe(e,n,t);if(A(e))return ze(e,n,t);let r=K("unknown");return t.push(` ${r}[Unknown Node]`),{entryId:r,exitId:r}}function Be(e,n,t){let r=e.key?`step_${e.key.replace(/[^a-zA-Z0-9]/g,"_")}`:K("step"),s=D(e.name??e.key??"Step"),o=n.showTimings&&e.durationMs!==void 0?` ${N(e.durationMs)}`:"",u="";switch(e.state){case"success":u="\u2713 ";break;case"error":u="\u2717 ";break;case"cached":u="\u{1F4BE} ";break;case"running":u="\u23F3 ";break;case"skipped":u="\u2298 ";break}let h="";if(e.input!==void 0){let g=typeof e.input=="string"?D(e.input):D(JSON.stringify(e.input).slice(0,20));h+=`\\nin: ${g}`}if(e.output!==void 0&&e.state==="success"){let g=typeof e.output=="string"?D(e.output):D(JSON.stringify(e.output).slice(0,20));h+=`\\nout: ${g}`}let p="";if(e.retryCount!==void 0&&e.retryCount>0&&(p+=`\\n\u21BB ${e.retryCount} retr${e.retryCount===1?"y":"ies"}`),e.timedOut){let g=e.timeoutMs!==void 0?`${e.timeoutMs}ms`:"";p+=`\\n\u23F1 timeout ${g}`}let c=(u+s+h+p+o).trim(),a=e.state,l;switch(e.state){case"error":l=`{{${c}}}`;break;case"cached":l=`[(${c})]`;break;case"skipped":l=`[${c}]:::skipped`;break;default:l=`[${c}]`}return t.push(` ${r}${l}:::${a}`),{entryId:r,exitId:r}}function Le(e,n,t){let r=K("parallel"),s=`${r}_fork`,o=`${r}_join`,u=de(e.name??"Parallel"),h=e.mode==="allSettled"?" (allSettled)":"";if(e.children.length===0){let a=r,l=D(`${u}${h}`),g="operations not individually tracked",$=n.showTimings&&e.durationMs!==void 0?` ${N(e.durationMs)}`:"";return t.push(` ${a}[${l}${$}\\n${g}]:::${e.state}`),{entryId:a,exitId:a}}t.push(` subgraph ${r}["${u}${h}"]`),t.push(" direction TB"),t.push(` ${s}{"\u26A1 Fork"}`);let p=[];for(let a of e.children){let l=Z(a,n,t);t.push(` ${s} --> ${l.entryId}`),p.push(l.exitId)}t.push(` ${o}{"\u2713 Join"}`);for(let a of p)t.push(` ${a} --> ${o}`);t.push(" end");let c=e.state;return t.push(` class ${r} ${c}`),{entryId:s,exitId:o}}function Fe(e,n,t){let r=K("race"),s=`${r}_start`,o=`${r}_end`,u=de(e.name??"Race");if(e.children.length===0){let a=r,l=D(u),g="operations not individually tracked",$=n.showTimings&&e.durationMs!==void 0?` ${N(e.durationMs)}`:"";return t.push(` ${a}[\u26A1 ${l}${$}\\n${g}]:::${e.state}`),{entryId:a,exitId:a}}t.push(` subgraph ${r}["\u26A1 ${u}"]`),t.push(" direction TB"),t.push(` ${s}(("\u{1F3C1} Start"))`);let h=[],p;for(let a of e.children){let l=Z(a,n,t),g=C(a)&&e.winnerId===a.id;t.push(` ${s} --> ${l.entryId}`),g&&(p=l.exitId),h.push({exitId:l.exitId,isWinner:g})}t.push(` ${o}(("\u2713 First"))`);for(let{exitId:a,isWinner:l}of h)l&&p?t.push(` ${a} ==>|\u{1F3C6} Winner| ${o}`):e.winnerId?t.push(` ${a} -. cancelled .-> ${o}`):t.push(` ${a} --> ${o}`);t.push(" end");let c=e.state;return t.push(` class ${r} ${c}`),{entryId:s,exitId:o}}function ze(e,n,t){let r=e.key?`decision_${e.key.replace(/[^a-zA-Z0-9]/g,"_")}`:K("decision"),s=D(e.condition??"condition"),o=e.decisionValue!==void 0?` = ${D(String(e.decisionValue)).slice(0,30)}`:"",u=`${s}${o}`.trim();t.push(` ${r}{${u}}`);let h=[],p;for(let c of e.branches){let a=`${r}_${c.label.replace(/[^a-zA-Z0-9]/g,"_")}`,l=D(c.label),g=c.taken?`${l} \u2713`:`${l} skipped`,$=c.taken?":::success":":::skipped";t.push(` ${a}[${g}]${$}`);let k=c.condition?`|${D(c.condition).replace(/\|/g,"")}|`:"";if(t.push(` ${r} -->${k} ${a}`),c.children.length>0){let w=a;for(let y of c.children){let x=Z(y,n,t);t.push(` ${w} --> ${x.entryId}`),w=x.exitId}h.push(w),c.taken&&(p=w)}else h.push(a),c.taken&&(p=a)}return p?{entryId:r,exitId:p}:{entryId:r,exitId:r}}var _={clearToEnd:"\x1B[J",cursorUp:e=>`\x1B[${e}A`,cursorToStart:"\x1B[G",hideCursor:"\x1B[?25l",showCursor:"\x1B[?25h",saveCursor:"\x1B[s",restoreCursor:"\x1B[u"};function le(e={}){let{workflowName:n,detectParallel:t=!0,showTimings:r=!0,showKeys:s=!1,colors:o,stream:u=process.stdout,updateInterval:h=100}=e,p=F({detectParallel:t}),c=W(),a={showTimings:r,showKeys:s,terminalWidth:u.columns??80,colors:{...M,...o}},l=!1,g="",$=0,k=null,w=!1;function y(S){u.writable&&u.write(S)}function x(){if(!l)return;let S=B(),L=c.render(S,a);L!==g&&($>0&&(y(_.cursorUp($)),y(_.cursorToStart),y(_.clearToEnd)),y(L),y(`
|
|
4
|
+
`),g=L,$=L.split(`
|
|
5
|
+
`).length)}function T(){l&&(w=!0,k===null&&(k=setTimeout(()=>{k=null,w&&(w=!1,x())},h)))}function R(S){if(S.type==="scope_start"||S.type==="scope_end"){b(S);return}p.handleEvent(S),l&&(S.type==="workflow_start"||S.type==="workflow_success"||S.type==="workflow_error"?x():T())}function b(S){p.handleScopeEvent(S),l&&T()}function O(S){p.handleDecisionEvent(S),l&&T()}function B(){let S=p.getIR();return n&&!S.root.name&&(S.root.name=n),S}function i(){return c.render(B(),a)}function d(){l||(l=!0,g="",$=0,y(_.hideCursor),x())}function m(){if(!l)return;l=!1,k!==null&&(clearTimeout(k),k=null);let S=B(),L=c.render(S,a);$>0&&(y(_.cursorUp($)),y(_.cursorToStart),y(_.clearToEnd)),y(L),y(`
|
|
6
|
+
`),y(_.showCursor)}function E(){k!==null&&(clearTimeout(k),k=null),w=!1,x()}function I(){p.reset(),g="",$=0}return{handleEvent:R,handleScopeEvent:b,handleDecisionEvent:O,getIR:B,render:i,start:d,stop:m,refresh:E,reset:I}}function Y(e,n={}){let{condition:t,value:r,name:s,workflowId:o=crypto.randomUUID(),emit:u}=n,h=Date.now(),p,c=[];function a(g,$,k){c.push({label:g,condition:k,taken:$}),$&&(p=g),u?.({type:"decision_branch",workflowId:o,decisionId:e,branchLabel:g,condition:k,taken:$,ts:Date.now()})}function l(){let g=Date.now()-h;u?.({type:"decision_end",workflowId:o,decisionId:e,branchTaken:p,ts:Date.now(),durationMs:g})}return u?.({type:"decision_start",workflowId:o,decisionId:e,condition:t,decisionValue:r,name:s,ts:h}),{takeBranch:a,end:l,getBranchTaken:()=>p,getBranches:()=>[...c]}}function ue(e,n,t={}){let r=Y(e,{...t,condition:t.condition??String(n),value:t.value??n});return{...r,condition:n,then:()=>{r.takeBranch("if",!0)},else:()=>{r.takeBranch("else",!0)}}}function pe(e,n,t={}){let r=Y(e,{...t,condition:t.condition??`switch(${String(n)})`,value:n});return{...r,value:n,case:(s,o)=>{r.takeBranch(`case '${s}'`,o,`value === '${s}'`)},default:s=>{r.takeBranch("default",s)}}}function H(e={}){let{workflowName:n,detectParallel:t=!0,showTimings:r=!0,showKeys:s=!1,colors:o}=e,u=F({detectParallel:t}),h=new Set,p=W(),c=j(),a={showTimings:r,showKeys:s,terminalWidth:process.stdout?.columns??80,colors:{...M,...o}};function l(){if(h.size>0){let b=u.getIR();for(let O of h)O(b)}}function g(b){if(b.type==="scope_start"||b.type==="scope_end"){$(b);return}u.handleEvent(b),b.type,l()}function $(b){u.handleScopeEvent(b),l()}function k(b){u.handleDecisionEvent(b),l()}function w(){let b=u.getIR();return n&&!b.root.name&&(b.root.name=n),b}function y(){let b=w();return p.render(b,a)}function x(b){let O=w();switch(b){case"ascii":return p.render(O,a);case"mermaid":return c.render(O,a);case"json":return JSON.stringify(O,null,2);default:throw new Error(`Unknown format: ${b}`)}}function T(){u.reset(),l()}function R(b){return h.add(b),()=>h.delete(b)}return{handleEvent:g,handleScopeEvent:$,handleDecisionEvent:k,getIR:w,render:y,renderAs:x,reset:T,onUpdate:R}}function Pe(e,n={}){let t=H(n);for(let r of e)r.type.startsWith("decision_")?t.handleDecisionEvent(r):t.handleEvent(r);return t.render()}function Ve(e={}){let n=[];return{handleEvent:t=>{n.push(t)},handleDecisionEvent:t=>{n.push(t)},getEvents:()=>[...n],getWorkflowEvents:()=>n.filter(t=>!t.type.startsWith("decision_")),getDecisionEvents:()=>n.filter(t=>t.type.startsWith("decision_")),clear:()=>{n.length=0},visualize:()=>{let t=H(e);for(let r of n)r.type.startsWith("decision_")?t.handleDecisionEvent(r):t.handleEvent(r);return t.render()},visualizeAs:t=>{let r=H(e);for(let s of n)s.type.startsWith("decision_")?r.handleDecisionEvent(s):r.handleEvent(s);return r.renderAs(t)}}}0&&(module.exports={asciiRenderer,createEventCollector,createIRBuilder,createLiveVisualizer,createParallelDetector,createVisualizer,defaultColorScheme,detectParallelGroups,hasChildren,isDecisionNode,isParallelNode,isRaceNode,isSequenceNode,isStepNode,mermaidRenderer,trackDecision,trackIf,trackSwitch,visualizeEvents});
|
|
7
7
|
//# sourceMappingURL=visualize.cjs.map
|