@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.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
function
|
|
2
|
-
`)}}}function q(e,n,t,
|
|
3
|
-
`)}}}function Z(e,n,t){if(
|
|
4
|
-
`),
|
|
5
|
-
`).length)}function
|
|
6
|
-
`),
|
|
1
|
+
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 Y(){return`node_${Date.now()}_${Math.random().toString(36).slice(2,8)}`}function ce(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 A(e,n={}){if(ce(e))return e;let{minOverlapMs:t=0,maxGapMs:o=5}=n,a=[],r=[];for(let c=0;c<e.length;c++){let s=e[c];s.type==="step"&&s.startTs!==void 0?a.push({node:s,startTs:s.startTs,endTs:s.endTs??s.startTs+(s.durationMs??0),originalIndex:c}):r.push({node:s,originalIndex:c})}if(a.length<=1)return e;a.sort((c,s)=>c.startTs-s.startTs);let u=[],h=[a[0]];for(let c=1;c<a.length;c++){let s=a[c],l=Math.min(...h.map(y=>y.startTs)),g=Math.max(...h.map(y=>y.endTs)),$=s.startTs<=l+o,k=s.startTs<g;if(!$&&!k){u.push(h),h=[s];continue}let w=k?Math.min(s.endTs,g)-s.startTs:0;$||w>=t?h.push(s):(u.push(h),h=[s])}u.push(h);let p=[];for(let c of u){let s=Math.min(...c.map(l=>l.originalIndex));if(c.length===1)p.push({node:c[0].node,position:s});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:de(l),mode:"all",children:l,startTs:g,endTs:$,durationMs:$-g};p.push({node:k,position:s})}}for(let{node:c,originalIndex:s}of r)p.push({node:c,position:s});return p.sort((c,s)=>c.position-s.position),p.map(c=>c.node)}function de(e){return e.some(r=>r.state==="error")?"error":e.some(r=>r.state==="running")?"running":e.some(r=>r.state==="pending")?"pending":(e.every(r=>r.state==="success"||r.state==="cached"),"success")}function le(e={}){return{detect:n=>A(n,e)}}function F(e={}){let{detectParallel:n=!0,parallelDetection:t}=e,o,a,r="pending",u,h,p=new Map,c=[],s=[],l=[],g=Date.now(),$=g;function k(i){return i.stepId??i.stepKey??i.name??Y()}function w(i){if(s.length>0){let d=s[s.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":o=i.workflowId,a=i.ts,r="running",g=Date.now(),$=g;break;case"workflow_success":r="success",h=i.durationMs,$=Date.now();break;case"workflow_error":r="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")s.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=s[s.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=s.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=A(i,t)),{root:{type:"workflow",id:o??Y(),workflowId:o??"unknown",state:r,startTs:a,durationMs:h,children:i,error:u},metadata:{createdAt:g,lastUpdatedAt:$}}}function W(){o=void 0,a=void 0,r="pending",u=void 0,h=void 0,p.clear(),c.length=0,s.length=0,l=[],g=Date.now(),$=g}return{handleEvent:y,handleScopeEvent:x,handleDecisionEvent:T,getIR:O,reset:W,get hasActiveSteps(){return p.size>0},get state(){return r}}}function _(e){return e.type==="step"}function Be(e){return e.type==="sequence"}function G(e){return e.type==="parallel"}function U(e){return e.type==="race"}function K(e){return e.type==="decision"}function Le(e){return"children"in e||e.type==="decision"&&"branches"in e}var H="\x1B[0m",ue="\x1B[1m",re="\x1B[2m",pe="\x1B[31m",fe="\x1B[32m",he="\x1B[33m",me="\x1B[34m",ne="\x1B[90m",ge="\x1B[37m";function j(e,n){return n?`${n}${e}${H}`:e}function z(e){return`${ue}${e}${H}`}function v(e){return`${re}${e}${H}`}var M={pending:ge,running:he,success:fe,error:pe,aborted:ne,cached:me,skipped:re+ne};function $e(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 P(e,n){let t=$e(e);return j(t,n[e])}function X(e,n,t){return j(e,t[n])}function oe(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 ie(e,n){let t=oe(e).length,o=Math.max(0,n-t);return e+" ".repeat(o)}function ke(e,n){if(!n)return f.horizontal.repeat(e);let t=` ${n} `,o=e-t.length;if(o<4)return f.horizontal.repeat(e);let a=2,r=o-a;return f.horizontal.repeat(a)+t+f.horizontal.repeat(r)}function L(){return{name:"ascii",supportsLive:!0,render(e,n){let t={...M,...n.colors},o=n.terminalWidth??60,a=o-4,r=[],u=e.root.name??"workflow",h=z(u);r.push(`${f.topLeft}${ke(o-2,h)}${f.topRight}`),r.push(`${f.vertical}${" ".repeat(o-2)}${f.vertical}`);let p=q(e.root.children,n,t,0);for(let c of p)r.push(`${f.vertical} ${ie(c,a)}${f.vertical}`);if(r.push(`${f.vertical}${" ".repeat(o-2)}${f.vertical}`),e.root.durationMs!==void 0&&n.showTimings){let c=e.root.state==="success"?"Completed":"Failed",l=`${X(c,e.root.state,t)} in ${N(e.root.durationMs)}`;r.push(`${f.vertical} ${ie(l,a)}${f.vertical}`),r.push(`${f.vertical}${" ".repeat(o-2)}${f.vertical}`)}return r.push(`${f.bottomLeft}${f.horizontal.repeat(o-2)}${f.bottomRight}`),r.join(`
|
|
2
|
+
`)}}}function q(e,n,t,o){let a=[];for(let r of e)_(r)?a.push(Q(r,n,t)):G(r)?a.push(...we(r,n,t,o)):U(r)?a.push(...be(r,n,t,o)):K(r)&&a.push(...ye(r,n,t,o));return a}function Q(e,n,t){let o=P(e.state,t),a=e.name??e.key??"step",r=X(a,e.state,t),u=`${o} ${r}`;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 we(e,n,t,o){let a=[],r=" ".repeat(o),u=P(e.state,t),h=e.name??"parallel",p=e.mode==="allSettled"?" (allSettled)":"";if(a.push(`${r}${f.teeRight}${f.teeDown}${f.horizontal} ${u} ${z(h)}${p}`),e.children.length===0)a.push(`${r}${f.vertical} ${v("(operations not individually tracked)")}`),a.push(`${r}${f.vertical} ${v("(wrap each operation with step() to see individual steps)")}`);else for(let c=0;c<e.children.length;c++){let s=e.children[c],g=c===e.children.length-1?`${r}${f.vertical} ${f.bottomLeft}`:`${r}${f.vertical} ${f.teeRight}`;if(_(s))a.push(`${g} ${Q(s,n,t)}`);else{let $=q([s],n,t,o+1);for(let k of $)a.push(`${r}${f.vertical} ${k}`)}}return n.showTimings&&e.durationMs!==void 0&&a.push(`${r}${f.bottomLeft}${f.horizontal}${f.horizontal} ${v(`[${N(e.durationMs)}]`)}`),a}function be(e,n,t,o){let a=[],r=" ".repeat(o),u=P(e.state,t),h=e.name??"race";if(a.push(`${r}${f.teeRight}\u26A1 ${u} ${z(h)}`),e.children.length===0)a.push(`${r}${f.vertical} ${v("(operations not individually tracked)")}`),a.push(`${r}${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?`${r}${f.vertical} ${f.bottomLeft}`:`${r}${f.vertical} ${f.teeRight}`,$=e.winnerId&&c.id===e.winnerId?v(" (winner)"):"";if(_(c))a.push(`${l} ${Q(c,n,t)}${$}`);else{let k=q([c],n,t,o+1);for(let w of k)a.push(`${r}${f.vertical} ${w}`)}}return n.showTimings&&e.durationMs!==void 0&&a.push(`${r}${f.bottomLeft}${f.horizontal}${f.horizontal} ${v(`[${N(e.durationMs)}]`)}`),a}function ye(e,n,t,o){let a=[],r=" ".repeat(o),u=P(e.state,t),h=e.name??"decision",p=e.condition?v(` (${e.condition})`):"",c=e.decisionValue!==void 0?v(` = ${String(e.decisionValue)}`):"",s=e.branchTaken!==void 0?v(` \u2192 ${String(e.branchTaken)}`):"";a.push(`${r}${f.teeRight}${f.teeDown}${f.horizontal} ${u} ${z(h)}${p}${c}${s}`);for(let l=0;l<e.branches.length;l++){let g=e.branches[l],k=l===e.branches.length-1?`${r}${f.vertical} ${f.bottomLeft}`:`${r}${f.vertical} ${f.teeRight}`,w=g.taken?"\u2713":"\u2298",y=g.taken?t.success:t.skipped,x=j(`${w} ${g.label}`,y),T=g.condition?v(` (${g.condition})`):"";if(a.push(`${k} ${x}${T}`),g.children.length>0){let R=q(g.children,n,t,o+1);for(let b of R)a.push(`${r}${f.vertical} ${b}`)}else g.taken||a.push(`${r}${f.vertical} ${v("(skipped)")}`)}return n.showTimings&&e.durationMs!==void 0&&a.push(`${r}${f.bottomLeft}${f.horizontal}${f.horizontal} ${v(`[${N(e.durationMs)}]`)}`),a}function Se(){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 se=0;function V(e="node"){return`${e}_${++se}`}function ve(){se=0}function D(e){return e.replace(/[{}[\]()]/g,"").replace(/[<>]/g,"").replace(/"/g,"'").trim()}function ae(e){return D(e).replace(/[[\]]/g,"")}function J(){return{name:"mermaid",supportsLive:!1,render(e,n){ve();let t=[];t.push("flowchart TD");let o="start";t.push(` ${o}(("\u25B6 Start"))`);let a=o;for(let r of e.root.children){let u=Z(r,n,t);t.push(` ${a} --> ${u.entryId}`),a=u.exitId}if(e.root.state==="success"||e.root.state==="error"){let r="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(` ${r}${p}${c}`),t.push(` ${a} --> ${r}`)}return t.push(""),t.push(...Se()),t.join(`
|
|
3
|
+
`)}}}function Z(e,n,t){if(_(e))return Ee(e,n,t);if(G(e))return xe(e,n,t);if(U(e))return De(e,n,t);if(K(e))return Ie(e,n,t);let o=V("unknown");return t.push(` ${o}[Unknown Node]`),{entryId:o,exitId:o}}function Ee(e,n,t){let o=e.key?`step_${e.key.replace(/[^a-zA-Z0-9]/g,"_")}`:V("step"),a=D(e.name??e.key??"Step"),r=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+a+h+p+r).trim(),s=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(` ${o}${l}:::${s}`),{entryId:o,exitId:o}}function xe(e,n,t){let o=V("parallel"),a=`${o}_fork`,r=`${o}_join`,u=ae(e.name??"Parallel"),h=e.mode==="allSettled"?" (allSettled)":"";if(e.children.length===0){let s=o,l=D(`${u}${h}`),g="operations not individually tracked",$=n.showTimings&&e.durationMs!==void 0?` ${N(e.durationMs)}`:"";return t.push(` ${s}[${l}${$}\\n${g}]:::${e.state}`),{entryId:s,exitId:s}}t.push(` subgraph ${o}["${u}${h}"]`),t.push(" direction TB"),t.push(` ${a}{"\u26A1 Fork"}`);let p=[];for(let s of e.children){let l=Z(s,n,t);t.push(` ${a} --> ${l.entryId}`),p.push(l.exitId)}t.push(` ${r}{"\u2713 Join"}`);for(let s of p)t.push(` ${s} --> ${r}`);t.push(" end");let c=e.state;return t.push(` class ${o} ${c}`),{entryId:a,exitId:r}}function De(e,n,t){let o=V("race"),a=`${o}_start`,r=`${o}_end`,u=ae(e.name??"Race");if(e.children.length===0){let s=o,l=D(u),g="operations not individually tracked",$=n.showTimings&&e.durationMs!==void 0?` ${N(e.durationMs)}`:"";return t.push(` ${s}[\u26A1 ${l}${$}\\n${g}]:::${e.state}`),{entryId:s,exitId:s}}t.push(` subgraph ${o}["\u26A1 ${u}"]`),t.push(" direction TB"),t.push(` ${a}(("\u{1F3C1} Start"))`);let h=[],p;for(let s of e.children){let l=Z(s,n,t),g=_(s)&&e.winnerId===s.id;t.push(` ${a} --> ${l.entryId}`),g&&(p=l.exitId),h.push({exitId:l.exitId,isWinner:g})}t.push(` ${r}(("\u2713 First"))`);for(let{exitId:s,isWinner:l}of h)l&&p?t.push(` ${s} ==>|\u{1F3C6} Winner| ${r}`):e.winnerId?t.push(` ${s} -. cancelled .-> ${r}`):t.push(` ${s} --> ${r}`);t.push(" end");let c=e.state;return t.push(` class ${o} ${c}`),{entryId:a,exitId:r}}function Ie(e,n,t){let o=e.key?`decision_${e.key.replace(/[^a-zA-Z0-9]/g,"_")}`:V("decision"),a=D(e.condition??"condition"),r=e.decisionValue!==void 0?` = ${D(String(e.decisionValue)).slice(0,30)}`:"",u=`${a}${r}`.trim();t.push(` ${o}{${u}}`);let h=[],p;for(let c of e.branches){let s=`${o}_${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(` ${s}[${g}]${$}`);let k=c.condition?`|${D(c.condition).replace(/\|/g,"")}|`:"";if(t.push(` ${o} -->${k} ${s}`),c.children.length>0){let w=s;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(s),c.taken&&(p=s)}return p?{entryId:o,exitId:p}:{entryId:o,exitId:o}}var C={clearToEnd:"\x1B[J",cursorUp:e=>`\x1B[${e}A`,cursorToStart:"\x1B[G",hideCursor:"\x1B[?25l",showCursor:"\x1B[?25h",saveCursor:"\x1B[s",restoreCursor:"\x1B[u"};function Ne(e={}){let{workflowName:n,detectParallel:t=!0,showTimings:o=!0,showKeys:a=!1,colors:r,stream:u=process.stdout,updateInterval:h=100}=e,p=F({detectParallel:t}),c=L(),s={showTimings:o,showKeys:a,terminalWidth:u.columns??80,colors:{...M,...r}},l=!1,g="",$=0,k=null,w=!1;function y(S){u.writable&&u.write(S)}function x(){if(!l)return;let S=W(),B=c.render(S,s);B!==g&&($>0&&(y(C.cursorUp($)),y(C.cursorToStart),y(C.clearToEnd)),y(B),y(`
|
|
4
|
+
`),g=B,$=B.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 W(){let S=p.getIR();return n&&!S.root.name&&(S.root.name=n),S}function i(){return c.render(W(),s)}function d(){l||(l=!0,g="",$=0,y(C.hideCursor),x())}function m(){if(!l)return;l=!1,k!==null&&(clearTimeout(k),k=null);let S=W(),B=c.render(S,s);$>0&&(y(C.cursorUp($)),y(C.cursorToStart),y(C.clearToEnd)),y(B),y(`
|
|
6
|
+
`),y(C.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:W,render:i,start:d,stop:m,refresh:E,reset:I}}function ee(e,n={}){let{condition:t,value:o,name:a,workflowId:r=crypto.randomUUID(),emit:u}=n,h=Date.now(),p,c=[];function s(g,$,k){c.push({label:g,condition:k,taken:$}),$&&(p=g),u?.({type:"decision_branch",workflowId:r,decisionId:e,branchLabel:g,condition:k,taken:$,ts:Date.now()})}function l(){let g=Date.now()-h;u?.({type:"decision_end",workflowId:r,decisionId:e,branchTaken:p,ts:Date.now(),durationMs:g})}return u?.({type:"decision_start",workflowId:r,decisionId:e,condition:t,decisionValue:o,name:a,ts:h}),{takeBranch:s,end:l,getBranchTaken:()=>p,getBranches:()=>[...c]}}function Te(e,n,t={}){let o=ee(e,{...t,condition:t.condition??String(n),value:t.value??n});return{...o,condition:n,then:()=>{o.takeBranch("if",!0)},else:()=>{o.takeBranch("else",!0)}}}function Re(e,n,t={}){let o=ee(e,{...t,condition:t.condition??`switch(${String(n)})`,value:n});return{...o,value:n,case:(a,r)=>{o.takeBranch(`case '${a}'`,r,`value === '${a}'`)},default:a=>{o.takeBranch("default",a)}}}function te(e={}){let{workflowName:n,detectParallel:t=!0,showTimings:o=!0,showKeys:a=!1,colors:r}=e,u=F({detectParallel:t}),h=new Set,p=L(),c=J(),s={showTimings:o,showKeys:a,terminalWidth:process.stdout?.columns??80,colors:{...M,...r}};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,s)}function x(b){let O=w();switch(b){case"ascii":return p.render(O,s);case"mermaid":return c.render(O,s);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 rt(e,n={}){let t=te(n);for(let o of e)o.type.startsWith("decision_")?t.handleDecisionEvent(o):t.handleEvent(o);return t.render()}function ot(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=te(e);for(let o of n)o.type.startsWith("decision_")?t.handleDecisionEvent(o):t.handleEvent(o);return t.render()},visualizeAs:t=>{let o=te(e);for(let a of n)a.type.startsWith("decision_")?o.handleDecisionEvent(a):o.handleEvent(a);return o.renderAs(t)}}}export{L as asciiRenderer,ot as createEventCollector,F as createIRBuilder,Ne as createLiveVisualizer,le as createParallelDetector,te as createVisualizer,M as defaultColorScheme,A as detectParallelGroups,Le as hasChildren,K as isDecisionNode,G as isParallelNode,U as isRaceNode,Be as isSequenceNode,_ as isStepNode,J as mermaidRenderer,ee as trackDecision,Te as trackIf,Re as trackSwitch,rt as visualizeEvents};
|
|
7
7
|
//# sourceMappingURL=visualize.js.map
|