@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.
@@ -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})},we=(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 be=e=>we(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:()=>$e,isStepNode:()=>O,mermaidRenderer:()=>j,trackDecision:()=>Y,trackIf:()=>ue,trackSwitch:()=>pe,visualizeEvents:()=>Pe});module.exports=be(Ae);function C(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 ke(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(ke(e))return e;let{maxGapMs:t=5}=n,r=[],s=[];for(let c=0;c<e.length;c++){let a=e[c];a.type==="step"&&a.startTs!==void 0?r.push({node:a,startTs:a.startTs,endTs:a.endTs??a.startTs+(a.durationMs??0),originalIndex:c}):s.push({node:a,originalIndex:c})}if(r.length<=1)return e;r.sort((c,a)=>c.startTs-a.startTs);let o=[],u=[r[0]];for(let c=1;c<r.length;c++){let a=r[c],l=Math.max(...u.map(m=>m.endTs));a.startTs<=l+t?u.push(a):(o.push(u),u=[a])}o.push(u);let f=[];for(let c of o){let a=Math.min(...c.map(l=>l.originalIndex));if(c.length===1)f.push({node:c[0].node,position:a});else{let l=c.map(w=>w.node),m=Math.min(...c.map(w=>w.startTs)),b=Math.max(...c.map(w=>w.endTs)),g={type:"parallel",id:`detected_parallel_${m}`,name:`${l.length} parallel steps`,state:ye(l),mode:"all",children:l,startTs:m,endTs:b,durationMs:b-m};f.push({node:g,position:a})}}for(let{node:c,originalIndex:a}of s)f.push({node:c,position:a});return f.sort((c,a)=>c.position-a.position),f.map(c=>c.node)}function ye(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,f,c=new Map,a=[],l=[],m=[],b=Date.now(),g=b;function w(i){return i.stepId??i.stepKey??i.name??Q()}function y(i){if(l.length>0){let d=l[l.length-1];for(let v of d.branches.values())if(v.taken){v.children.push(i),g=Date.now();return}let p=Array.from(d.branches.values())[0];if(p){p.children.push(i),g=Date.now();return}}a.length>0?a[a.length-1].children.push(i):m.push(i),g=Date.now()}function S(i){switch(i.type){case"workflow_start":r=i.workflowId,s=i.ts,o="running",b=Date.now(),g=b;break;case"workflow_success":o="success",f=i.durationMs,g=Date.now();break;case"workflow_error":o="error",u=i.error,f=i.durationMs,g=Date.now();break;case"step_start":{let d=w(i);c.set(d,{id:d,name:i.name,key:i.stepKey,startTs:i.ts,retryCount:0,timedOut:!1}),g=Date.now();break}case"step_success":{let d=w(i),p=c.get(d);if(p){let v={type:"step",id:p.id,name:p.name,key:p.key,state:"success",startTs:p.startTs,endTs:i.ts,durationMs:i.durationMs,...p.retryCount>0&&{retryCount:p.retryCount},...p.timedOut&&{timedOut:!0,timeoutMs:p.timeoutMs}};y(v),c.delete(d)}break}case"step_error":{let d=w(i),p=c.get(d);if(p){let v={type:"step",id:p.id,name:p.name,key:p.key,state:"error",startTs:p.startTs,endTs:i.ts,durationMs:i.durationMs,error:i.error,...p.retryCount>0&&{retryCount:p.retryCount},...p.timedOut&&{timedOut:!0,timeoutMs:p.timeoutMs}};y(v),c.delete(d)}break}case"step_aborted":{let d=w(i),p=c.get(d);if(p){let v={type:"step",id:p.id,name:p.name,key:p.key,state:"aborted",startTs:p.startTs,endTs:i.ts,durationMs:i.durationMs,...p.retryCount>0&&{retryCount:p.retryCount},...p.timedOut&&{timedOut:!0,timeoutMs:p.timeoutMs}};y(v),c.delete(d)}break}case"step_cache_hit":{let p={type:"step",id:w(i),name:i.name,key:i.stepKey,state:"cached",startTs:i.ts,endTs:i.ts,durationMs:0};y(p);break}case"step_cache_miss":break;case"step_complete":break;case"step_timeout":{let d=w(i),p=c.get(d);p&&(p.timedOut=!0,p.timeoutMs=i.timeoutMs),g=Date.now();break}case"step_retry":{let d=w(i),p=c.get(d);p&&(p.retryCount=(i.attempt??1)-1),g=Date.now();break}case"step_retries_exhausted":g=Date.now();break;case"step_skipped":{let p={type:"step",id:w(i),name:i.name,key:i.stepKey,state:"skipped",startTs:i.ts,endTs:i.ts,durationMs:0};y(p);break}}}function D(i){if(i.type==="scope_start")a.push({id:i.scopeId,name:i.name,type:i.scopeType,startTs:i.ts,children:[]}),g=Date.now();else if(i.type==="scope_end"){let d=a.pop();if(d){let p=d.type==="race"?{type:"race",id:d.id,name:d.name,state:T(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:T(d.children),startTs:d.startTs,endTs:i.ts,durationMs:i.durationMs,children:d.children,mode:d.type==="allSettled"?"allSettled":"all"};y(p)}}}function I(i){if(i.type==="decision_start")l.push({id:i.decisionId,name:i.name,condition:i.condition,decisionValue:i.decisionValue,startTs:i.ts,branches:new Map}),g=Date.now();else if(i.type==="decision_branch"){let d=l[l.length-1];if(d&&d.id===i.decisionId){let p=i.branchLabel,v=d.branches.get(p);v?v.taken=i.taken:d.branches.set(p,{label:i.branchLabel,condition:i.condition,taken:i.taken,children:[]}),g=Date.now()}}else if(i.type==="decision_end"){let d=l.pop();if(d&&d.id===i.decisionId){let p=Array.from(d.branches.values()),v={type:"decision",id:d.id,name:d.name,state:T(p.flatMap(x=>x.taken?x.children:[])),startTs:d.startTs,endTs:i.ts,durationMs:i.durationMs,condition:d.condition,decisionValue:d.decisionValue,branchTaken:i.branchTaken??d.branchTaken,branches:p};y(v)}}}function T(i){return i.length===0?"success":i.some(x=>x.state==="error")?"error":i.every(x=>x.state==="success"||x.state==="cached")?"success":i.some(x=>x.state==="running")?"running":"pending"}function k(){let i=[...m];for(let[,d]of c)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 R(){let i=k();return n&&(i=z(i,t)),{root:{type:"workflow",id:r??Q(),workflowId:r??"unknown",state:o,startTs:s,durationMs:f,children:i,error:u},metadata:{createdAt:b,lastUpdatedAt:g}}}function B(){r=void 0,s=void 0,o="pending",u=void 0,f=void 0,c.clear(),a.length=0,l.length=0,m=[],b=Date.now(),g=b}return{handleEvent:S,handleScopeEvent:D,handleDecisionEvent:I,getIR:R,reset:B,get hasActiveSteps(){return c.size>0},get state(){return o}}}function O(e){return e.type==="step"}function $e(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",De="\x1B[32m",xe="\x1B[33m",Ne="\x1B[34m",oe="\x1B[90m",Ie="\x1B[37m";function q(e,n){return n?`${n}${e}${ee}`:e}function G(e){return`${ve}${e}${ee}`}function E(e){return`${ie}${e}${ee}`}var M={pending:Ie,running:xe,success:De,error:Ee,aborted:oe,cached:Ne,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 h={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 h.horizontal.repeat(e);let t=` ${n} `,r=e-t.length;if(r<4)return h.horizontal.repeat(e);let s=2,o=r-s;return h.horizontal.repeat(s)+t+h.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",f=G(u);o.push(`${h.topLeft}${Re(r-2,f)}${h.topRight}`),o.push(`${h.vertical}${" ".repeat(r-2)}${h.vertical}`);let c=J(e.root.children,n,t,0);for(let a of c)o.push(`${h.vertical} ${ae(a,s)}${h.vertical}`);if(o.push(`${h.vertical}${" ".repeat(r-2)}${h.vertical}`),e.root.durationMs!==void 0&&n.showTimings){let a=e.root.state==="success"?"Completed":"Failed",m=`${te(a,e.root.state,t)} in ${C(e.root.durationMs)}`;o.push(`${h.vertical} ${ae(m,s)}${h.vertical}`),o.push(`${h.vertical}${" ".repeat(r-2)}${h.vertical}`)}return o.push(`${h.bottomLeft}${h.horizontal.repeat(r-2)}${h.bottomRight}`),o.join(`
2
- `)}}}function J(e,n,t,r){let s=[];for(let o of e)O(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+=E(` [key: ${e.key}]`)),e.input!==void 0){let f=typeof e.input=="string"?e.input:JSON.stringify(e.input).slice(0,30);u+=E(` [in: ${f}${f.length>=30?"...":""}]`)}if(e.output!==void 0&&e.state==="success"){let f=typeof e.output=="string"?e.output:JSON.stringify(e.output).slice(0,30);u+=E(` [out: ${f}${f.length>=30?"...":""}]`)}if(n.showTimings&&e.durationMs!==void 0&&(u+=E(` [${C(e.durationMs)}]`)),e.retryCount!==void 0&&e.retryCount>0&&(u+=E(` [${e.retryCount} ${e.retryCount===1?"retry":"retries"}]`)),e.timedOut){let f=e.timeoutMs!==void 0?` ${e.timeoutMs}ms`:"";u+=E(` [timeout${f}]`)}return u}function Oe(e,n,t,r){let s=[],o=" ".repeat(r),u=U(e.state,t),f=e.name??"parallel",c=e.mode==="allSettled"?" (allSettled)":"";s.push(`${o}${h.teeRight}${h.teeDown}${h.horizontal} ${u} ${G(f)}${c}`);for(let a=0;a<e.children.length;a++){let l=e.children[a],b=a===e.children.length-1?`${o}${h.vertical} ${h.bottomLeft}`:`${o}${h.vertical} ${h.teeRight}`;if(O(l))s.push(`${b} ${ne(l,n,t)}`);else{let g=J([l],n,t,r+1);for(let w of g)s.push(`${o}${h.vertical} ${w}`)}}return n.showTimings&&e.durationMs!==void 0&&s.push(`${o}${h.bottomLeft}${h.horizontal}${h.horizontal} ${E(`[${C(e.durationMs)}]`)}`),s}function Me(e,n,t,r){let s=[],o=" ".repeat(r),u=U(e.state,t),f=e.name??"race";s.push(`${o}${h.teeRight}\u26A1 ${u} ${G(f)}`);for(let c=0;c<e.children.length;c++){let a=e.children[c],m=c===e.children.length-1?`${o}${h.vertical} ${h.bottomLeft}`:`${o}${h.vertical} ${h.teeRight}`,g=e.winnerId&&a.id===e.winnerId?E(" (winner)"):"";if(O(a))s.push(`${m} ${ne(a,n,t)}${g}`);else{let w=J([a],n,t,r+1);for(let y of w)s.push(`${o}${h.vertical} ${y}`)}}return n.showTimings&&e.durationMs!==void 0&&s.push(`${o}${h.bottomLeft}${h.horizontal}${h.horizontal} ${E(`[${C(e.durationMs)}]`)}`),s}function Ce(e,n,t,r){let s=[],o=" ".repeat(r),u=U(e.state,t),f=e.name??"decision",c=e.condition?E(` (${e.condition})`):"",a=e.decisionValue!==void 0?E(` = ${String(e.decisionValue)}`):"",l=e.branchTaken!==void 0?E(` \u2192 ${String(e.branchTaken)}`):"";s.push(`${o}${h.teeRight}${h.teeDown}${h.horizontal} ${u} ${G(f)}${c}${a}${l}`);for(let m=0;m<e.branches.length;m++){let b=e.branches[m],w=m===e.branches.length-1?`${o}${h.vertical} ${h.bottomLeft}`:`${o}${h.vertical} ${h.teeRight}`,y=b.taken?"\u2713":"\u2298",S=b.taken?t.success:t.skipped,D=q(`${y} ${b.label}`,S),I=b.condition?E(` (${b.condition})`):"";if(s.push(`${w} ${D}${I}`),b.children.length>0){let T=J(b.children,n,t,r+1);for(let k of T)s.push(`${o}${h.vertical} ${k}`)}else b.taken||s.push(`${o}${h.vertical} ${E("(skipped)")}`)}return n.showTimings&&e.durationMs!==void 0&&s.push(`${o}${h.bottomLeft}${h.horizontal}${h.horizontal} ${E(`[${C(e.durationMs)}]`)}`),s}function _e(){return[" classDef pending fill:#e5e7eb,stroke:#9ca3af,color:#374151"," classDef running fill:#fef3c7,stroke:#f59e0b,color:#92400e"," classDef success fill:#d1fae5,stroke:#10b981,color:#065f46"," classDef error fill:#fee2e2,stroke:#ef4444,color:#991b1b"," classDef aborted fill:#f3f4f6,stroke:#6b7280,color:#4b5563"," classDef cached fill:#dbeafe,stroke:#3b82f6,color:#1e40af"," classDef skipped fill:#f9fafb,stroke:#d1d5db,color:#6b7280"]}var ce=0;function K(e="node"){return`${e}_${++ce}`}function We(){ce=0}function N(e){return e.replace(/[{}[\]()]/g,"").replace(/[<>]/g,"").replace(/"/g,"'").trim()}function de(e){return N(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}((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"?"((Done))":"((Failed))",f=e.root.state==="success"?":::success":":::error";t.push(` ${o}${u}${f}`),t.push(` ${s} --> ${o}`)}return t.push(""),t.push(..._e()),t.join(`
3
- `)}}}function Z(e,n,t){if(O(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=N(e.name??e.key??"Step"),o=n.showTimings&&e.durationMs!==void 0?` ${C(e.durationMs)}`:"",u="";if(e.input!==void 0){let m=typeof e.input=="string"?N(e.input):N(JSON.stringify(e.input).slice(0,20));u+=`\\nin: ${m}`}if(e.output!==void 0&&e.state==="success"){let m=typeof e.output=="string"?N(e.output):N(JSON.stringify(e.output).slice(0,20));u+=`\\nout: ${m}`}let f="";if(e.retryCount!==void 0&&e.retryCount>0&&(f+=`\\n\u21BB${e.retryCount}`),e.timedOut){let m=e.timeoutMs!==void 0?`${e.timeoutMs}ms`:"";f+=`\\n\u23F1${m}`}let c=(s+u+f+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");t.push(` subgraph ${r}[${u}]`),t.push(" direction TB"),t.push(` ${s}{Fork}`);let f=[];for(let a of e.children){let l=Z(a,n,t);t.push(` ${s} --> ${l.entryId}`),f.push(l.exitId)}t.push(` ${o}{Join}`);for(let a of f)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");t.push(` subgraph ${r}["\u26A1 ${u}"]`),t.push(" direction TB"),t.push(` ${s}((Race))`);let f=[];for(let a of e.children){let l=Z(a,n,t);t.push(` ${s} --> ${l.entryId}`),f.push(l.exitId),O(a)&&e.winnerId===a.id&&t.push(` ${l.exitId} -. winner .-> ${o}`)}t.push(` ${o}((First))`);for(let a of f)(!e.winnerId||!e.children.some(l=>O(l)&&l.id===e.winnerId))&&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=N(e.condition??"condition"),o=e.decisionValue!==void 0?` = ${N(String(e.decisionValue)).slice(0,30)}`:"",u=`${s}${o}`.trim();t.push(` ${r}{${u}}`);let f=[],c;for(let a of e.branches){let l=`${r}_${a.label.replace(/[^a-zA-Z0-9]/g,"_")}`,m=N(a.label),b=a.taken?`${m} \u2713`:`${m} skipped`,g=a.taken?":::success":":::skipped";t.push(` ${l}[${b}]${g}`);let w=a.condition?`|${N(a.condition).replace(/\|/g,"")}|`:"";if(t.push(` ${r} -->${w} ${l}`),a.children.length>0){let y=l;for(let S of a.children){let D=Z(S,n,t);t.push(` ${y} --> ${D.entryId}`),y=D.exitId}f.push(y),a.taken&&(c=y)}else f.push(l),a.taken&&(c=l)}return c?{entryId:r,exitId:c}:{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:f=100}=e,c=F({detectParallel:t}),a=W(),l={showTimings:r,showKeys:s,terminalWidth:u.columns??80,colors:{...M,...o}},m=!1,b="",g=0,w=null,y=!1;function S($){u.writable&&u.write($)}function D(){if(!m)return;let $=B(),L=a.render($,l);L!==b&&(g>0&&(S(_.cursorUp(g)),S(_.cursorToStart),S(_.clearToEnd)),S(L),S(`
4
- `),b=L,g=L.split(`
5
- `).length)}function I(){m&&(y=!0,w===null&&(w=setTimeout(()=>{w=null,y&&(y=!1,D())},f)))}function T($){if($.type==="scope_start"||$.type==="scope_end"){k($);return}c.handleEvent($),m&&($.type==="workflow_start"||$.type==="workflow_success"||$.type==="workflow_error"?D():I())}function k($){c.handleScopeEvent($),m&&I()}function R($){c.handleDecisionEvent($),m&&I()}function B(){let $=c.getIR();return n&&!$.root.name&&($.root.name=n),$}function i(){return a.render(B(),l)}function d(){m||(m=!0,b="",g=0,S(_.hideCursor),D())}function p(){if(!m)return;m=!1,w!==null&&(clearTimeout(w),w=null);let $=B(),L=a.render($,l);g>0&&(S(_.cursorUp(g)),S(_.cursorToStart),S(_.clearToEnd)),S(L),S(`
6
- `),S(_.showCursor)}function v(){w!==null&&(clearTimeout(w),w=null),y=!1,D()}function x(){c.reset(),b="",g=0}return{handleEvent:T,handleScopeEvent:k,handleDecisionEvent:R,getIR:B,render:i,start:d,stop:p,refresh:v,reset:x}}function Y(e,n={}){let{condition:t,value:r,name:s,workflowId:o=crypto.randomUUID(),emit:u}=n,f=Date.now(),c,a=[];function l(b,g,w){a.push({label:b,condition:w,taken:g}),g&&(c=b),u?.({type:"decision_branch",workflowId:o,decisionId:e,branchLabel:b,condition:w,taken:g,ts:Date.now()})}function m(){let b=Date.now()-f;u?.({type:"decision_end",workflowId:o,decisionId:e,branchTaken:c,ts:Date.now(),durationMs:b})}return u?.({type:"decision_start",workflowId:o,decisionId:e,condition:t,decisionValue:r,name:s,ts:f}),{takeBranch:l,end:m,getBranchTaken:()=>c,getBranches:()=>[...a]}}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}),f=new Set,c=W(),a=j(),l={showTimings:r,showKeys:s,terminalWidth:process.stdout?.columns??80,colors:{...M,...o}};function m(){if(f.size>0){let k=u.getIR();for(let R of f)R(k)}}function b(k){if(k.type==="scope_start"||k.type==="scope_end"){g(k);return}u.handleEvent(k),k.type,m()}function g(k){u.handleScopeEvent(k),m()}function w(k){u.handleDecisionEvent(k),m()}function y(){let k=u.getIR();return n&&!k.root.name&&(k.root.name=n),k}function S(){let k=y();return c.render(k,l)}function D(k){let R=y();switch(k){case"ascii":return c.render(R,l);case"mermaid":return a.render(R,l);case"json":return JSON.stringify(R,null,2);default:throw new Error(`Unknown format: ${k}`)}}function I(){u.reset(),m()}function T(k){return f.add(k),()=>f.delete(k)}return{handleEvent:b,handleScopeEvent:g,handleDecisionEvent:w,getIR:y,render:S,renderAs:D,reset:I,onUpdate:T}}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});
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