@jagreehal/workflow 1.3.0 → 1.4.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.
@@ -44,6 +44,12 @@ interface BaseNode {
44
44
  input?: unknown;
45
45
  /** Output value from this step (for decision understanding) */
46
46
  output?: unknown;
47
+ /** Number of retry attempts made (0 = no retries, 1 = one retry, etc.) */
48
+ retryCount?: number;
49
+ /** Whether this step experienced a timeout (may have retried after) */
50
+ timedOut?: boolean;
51
+ /** Timeout duration in ms (if timed out) */
52
+ timeoutMs?: number;
47
53
  }
48
54
  /**
49
55
  * A single step execution node.
@@ -44,6 +44,12 @@ interface BaseNode {
44
44
  input?: unknown;
45
45
  /** Output value from this step (for decision understanding) */
46
46
  output?: unknown;
47
+ /** Number of retry attempts made (0 = no retries, 1 = one retry, etc.) */
48
+ retryCount?: number;
49
+ /** Whether this step experienced a timeout (may have retried after) */
50
+ timedOut?: boolean;
51
+ /** Timeout duration in ms (if timed out) */
52
+ timeoutMs?: number;
47
53
  }
48
54
  /**
49
55
  * A single step execution node.
package/dist/visualize.js CHANGED
@@ -1,7 +1,7 @@
1
- function O(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{maxGapMs:t=5}=n,r=[],a=[];for(let c=0;c<e.length;c++){let s=e[c];s.type==="step"&&s.startTs!==void 0?r.push({node:s,startTs:s.startTs,endTs:s.endTs??s.startTs+(s.durationMs??0),originalIndex:c}):a.push({node:s,originalIndex:c})}if(r.length<=1)return e;r.sort((c,s)=>c.startTs-s.startTs);let o=[],u=[r[0]];for(let c=1;c<r.length;c++){let s=r[c],d=Math.max(...u.map(h=>h.endTs));s.startTs<=d+t?u.push(s):(o.push(u),u=[s])}o.push(u);let f=[];for(let c of o){let s=Math.min(...c.map(d=>d.originalIndex));if(c.length===1)f.push({node:c[0].node,position:s});else{let d=c.map(w=>w.node),h=Math.min(...c.map(w=>w.startTs)),g=Math.max(...c.map(w=>w.endTs)),m={type:"parallel",id:`detected_parallel_${h}`,name:`${d.length} parallel steps`,state:de(d),mode:"all",children:d,startTs:h,endTs:g,durationMs:g-h};f.push({node:m,position:s})}}for(let{node:c,originalIndex:s}of a)f.push({node:c,position:s});return f.sort((c,s)=>c.position-s.position),f.map(c=>c.node)}function de(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 le(e={}){return{detect:n=>A(n,e)}}function z(e={}){let{detectParallel:n=!0,parallelDetection:t}=e,r,a,o="pending",u,f,c=new Map,s=[],d=[],h=[],g=Date.now(),m=g;function w(i){return i.stepId??i.stepKey??i.name??Y()}function $(i){if(d.length>0){let l=d[d.length-1];for(let y of l.branches.values())if(y.taken){y.children.push(i),m=Date.now();return}let k=Array.from(l.branches.values())[0];if(k){k.children.push(i),m=Date.now();return}}s.length>0?s[s.length-1].children.push(i):h.push(i),m=Date.now()}function v(i){switch(i.type){case"workflow_start":r=i.workflowId,a=i.ts,o="running",g=Date.now(),m=g;break;case"workflow_success":o="success",f=i.durationMs,m=Date.now();break;case"workflow_error":o="error",u=i.error,f=i.durationMs,m=Date.now();break;case"step_start":{let l=w(i);c.set(l,{id:l,name:i.name,key:i.stepKey,startTs:i.ts}),m=Date.now();break}case"step_success":{let l=w(i),k=c.get(l);if(k){let y={type:"step",id:k.id,name:k.name,key:k.key,state:"success",startTs:k.startTs,endTs:i.ts,durationMs:i.durationMs};$(y),c.delete(l)}break}case"step_error":{let l=w(i),k=c.get(l);if(k){let y={type:"step",id:k.id,name:k.name,key:k.key,state:"error",startTs:k.startTs,endTs:i.ts,durationMs:i.durationMs,error:i.error};$(y),c.delete(l)}break}case"step_aborted":{let l=w(i),k=c.get(l);if(k){let y={type:"step",id:k.id,name:k.name,key:k.key,state:"aborted",startTs:k.startTs,endTs:i.ts,durationMs:i.durationMs};$(y),c.delete(l)}break}case"step_cache_hit":{let k={type:"step",id:w(i),name:i.name,key:i.stepKey,state:"cached",startTs:i.ts,endTs:i.ts,durationMs:0};$(k);break}case"step_cache_miss":break;case"step_complete":break;case"step_skipped":{let k={type:"step",id:w(i),name:i.name,key:i.stepKey,state:"skipped",startTs:i.ts,endTs:i.ts,durationMs:0};$(k);break}}}function D(i){if(i.type==="scope_start")s.push({id:i.scopeId,name:i.name,type:i.scopeType,startTs:i.ts,children:[]}),m=Date.now();else if(i.type==="scope_end"){let l=s.pop();if(l){let k=l.type==="race"?{type:"race",id:l.id,name:l.name,state:T(l.children),startTs:l.startTs,endTs:i.ts,durationMs:i.durationMs,children:l.children,winnerId:i.winnerId}:{type:"parallel",id:l.id,name:l.name,state:T(l.children),startTs:l.startTs,endTs:i.ts,durationMs:i.durationMs,children:l.children,mode:l.type==="allSettled"?"allSettled":"all"};$(k)}}}function I(i){if(i.type==="decision_start")d.push({id:i.decisionId,name:i.name,condition:i.condition,decisionValue:i.decisionValue,startTs:i.ts,branches:new Map}),m=Date.now();else if(i.type==="decision_branch"){let l=d[d.length-1];if(l&&l.id===i.decisionId){let k=i.branchLabel,y=l.branches.get(k);y?y.taken=i.taken:l.branches.set(k,{label:i.branchLabel,condition:i.condition,taken:i.taken,children:[]}),m=Date.now()}}else if(i.type==="decision_end"){let l=d.pop();if(l&&l.id===i.decisionId){let k=Array.from(l.branches.values()),y={type:"decision",id:l.id,name:l.name,state:T(k.flatMap(x=>x.taken?x.children:[])),startTs:l.startTs,endTs:i.ts,durationMs:i.durationMs,condition:l.condition,decisionValue:l.decisionValue,branchTaken:i.branchTaken??l.branchTaken,branches:k};$(y)}}}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 b(){let i=[...h];for(let[,l]of c)i.push({type:"step",id:l.id,name:l.name,key:l.key,state:"running",startTs:l.startTs});return i}function R(){let i=b();return n&&(i=A(i,t)),{root:{type:"workflow",id:r??Y(),workflowId:r??"unknown",state:o,startTs:a,durationMs:f,children:i,error:u},metadata:{createdAt:g,lastUpdatedAt:m}}}function M(){r=void 0,a=void 0,o="pending",u=void 0,f=void 0,c.clear(),s.length=0,d.length=0,h=[],g=Date.now(),m=g}return{handleEvent:v,handleScopeEvent:D,handleDecisionEvent:I,getIR:R,reset:M,get hasActiveSteps(){return c.size>0},get state(){return o}}}function _(e){return e.type==="step"}function Le(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 Fe(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",ge="\x1B[34m",ne="\x1B[90m",me="\x1B[37m";function j(e,n){return n?`${n}${e}${H}`:e}function C(e){return`${ue}${e}${H}`}function E(e){return`${re}${e}${H}`}var W={pending:me,running:he,success:fe,error:pe,aborted:ne,cached:ge,skipped:re+ne};function we(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=we(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 p={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,r=Math.max(0,n-t);return e+" ".repeat(r)}function ke(e,n){if(!n)return p.horizontal.repeat(e);let t=` ${n} `,r=e-t.length;if(r<4)return p.horizontal.repeat(e);let a=2,o=r-a;return p.horizontal.repeat(a)+t+p.horizontal.repeat(o)}function F(){return{name:"ascii",supportsLive:!0,render(e,n){let t={...W,...n.colors},r=n.terminalWidth??60,a=r-4,o=[],u=e.root.name??"workflow",f=C(u);o.push(`${p.topLeft}${ke(r-2,f)}${p.topRight}`),o.push(`${p.vertical}${" ".repeat(r-2)}${p.vertical}`);let c=q(e.root.children,n,t,0);for(let s of c)o.push(`${p.vertical} ${ie(s,a)}${p.vertical}`);if(o.push(`${p.vertical}${" ".repeat(r-2)}${p.vertical}`),e.root.durationMs!==void 0&&n.showTimings){let s=e.root.state==="success"?"Completed":"Failed",h=`${X(s,e.root.state,t)} in ${O(e.root.durationMs)}`;o.push(`${p.vertical} ${ie(h,a)}${p.vertical}`),o.push(`${p.vertical}${" ".repeat(r-2)}${p.vertical}`)}return o.push(`${p.bottomLeft}${p.horizontal.repeat(r-2)}${p.bottomRight}`),o.join(`
2
- `)}}}function q(e,n,t,r){let a=[];for(let o of e)_(o)?a.push(Q(o,n,t)):G(o)?a.push(...be(o,n,t,r)):U(o)?a.push(...$e(o,n,t,r)):K(o)&&a.push(...Se(o,n,t,r));return a}function Q(e,n,t){let r=P(e.state,t),a=e.name??e.key??"step",o=X(a,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?"...":""}]`)}return n.showTimings&&e.durationMs!==void 0&&(u+=E(` [${O(e.durationMs)}]`)),u}function be(e,n,t,r){let a=[],o=" ".repeat(r),u=P(e.state,t),f=e.name??"parallel",c=e.mode==="allSettled"?" (allSettled)":"";a.push(`${o}${p.teeRight}${p.teeDown}${p.horizontal} ${u} ${C(f)}${c}`);for(let s=0;s<e.children.length;s++){let d=e.children[s],g=s===e.children.length-1?`${o}${p.vertical} ${p.bottomLeft}`:`${o}${p.vertical} ${p.teeRight}`;if(_(d))a.push(`${g} ${Q(d,n,t)}`);else{let m=q([d],n,t,r+1);for(let w of m)a.push(`${o}${p.vertical} ${w}`)}}return n.showTimings&&e.durationMs!==void 0&&a.push(`${o}${p.bottomLeft}${p.horizontal}${p.horizontal} ${E(`[${O(e.durationMs)}]`)}`),a}function $e(e,n,t,r){let a=[],o=" ".repeat(r),u=P(e.state,t),f=e.name??"race";a.push(`${o}${p.teeRight}\u26A1 ${u} ${C(f)}`);for(let c=0;c<e.children.length;c++){let s=e.children[c],h=c===e.children.length-1?`${o}${p.vertical} ${p.bottomLeft}`:`${o}${p.vertical} ${p.teeRight}`,m=e.winnerId&&s.id===e.winnerId?E(" (winner)"):"";if(_(s))a.push(`${h} ${Q(s,n,t)}${m}`);else{let w=q([s],n,t,r+1);for(let $ of w)a.push(`${o}${p.vertical} ${$}`)}}return n.showTimings&&e.durationMs!==void 0&&a.push(`${o}${p.bottomLeft}${p.horizontal}${p.horizontal} ${E(`[${O(e.durationMs)}]`)}`),a}function Se(e,n,t,r){let a=[],o=" ".repeat(r),u=P(e.state,t),f=e.name??"decision",c=e.condition?E(` (${e.condition})`):"",s=e.decisionValue!==void 0?E(` = ${String(e.decisionValue)}`):"",d=e.branchTaken!==void 0?E(` \u2192 ${String(e.branchTaken)}`):"";a.push(`${o}${p.teeRight}${p.teeDown}${p.horizontal} ${u} ${C(f)}${c}${s}${d}`);for(let h=0;h<e.branches.length;h++){let g=e.branches[h],w=h===e.branches.length-1?`${o}${p.vertical} ${p.bottomLeft}`:`${o}${p.vertical} ${p.teeRight}`,$=g.taken?"\u2713":"\u2298",v=g.taken?t.success:t.skipped,D=j(`${$} ${g.label}`,v),I=g.condition?E(` (${g.condition})`):"";if(a.push(`${w} ${D}${I}`),g.children.length>0){let T=q(g.children,n,t,r+1);for(let b of T)a.push(`${o}${p.vertical} ${b}`)}else g.taken||a.push(`${o}${p.vertical} ${E("(skipped)")}`)}return n.showTimings&&e.durationMs!==void 0&&a.push(`${o}${p.bottomLeft}${p.horizontal}${p.horizontal} ${E(`[${O(e.durationMs)}]`)}`),a}function ve(){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 se=0;function V(e="node"){return`${e}_${++se}`}function ye(){se=0}function N(e){return e.replace(/[{}[\]()]/g,"").replace(/[<>]/g,"").replace(/"/g,"'").trim()}function ae(e){return N(e).replace(/[[\]]/g,"")}function J(){return{name:"mermaid",supportsLive:!1,render(e,n){ye();let t=[];t.push("flowchart TD");let r="start";t.push(` ${r}((Start))`);let a=r;for(let o of e.root.children){let u=Z(o,n,t);t.push(` ${a} --> ${u.entryId}`),a=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(` ${a} --> ${o}`)}return t.push(""),t.push(...ve()),t.join(`
3
- `)}}}function Z(e,n,t){if(_(e))return Ee(e,n,t);if(G(e))return De(e,n,t);if(U(e))return xe(e,n,t);if(K(e))return Ne(e,n,t);let r=V("unknown");return t.push(` ${r}[Unknown Node]`),{entryId:r,exitId:r}}function Ee(e,n,t){let r=e.key?`step_${e.key.replace(/[^a-zA-Z0-9]/g,"_")}`:V("step"),a=N(e.name??e.key??"Step"),o=n.showTimings&&e.durationMs!==void 0?` ${O(e.durationMs)}`:"",u="";if(e.input!==void 0){let d=typeof e.input=="string"?N(e.input):N(JSON.stringify(e.input).slice(0,20));u+=`\\nin: ${d}`}if(e.output!==void 0&&e.state==="success"){let d=typeof e.output=="string"?N(e.output):N(JSON.stringify(e.output).slice(0,20));u+=`\\nout: ${d}`}let f=(a+u+o).trim(),c=e.state,s;switch(e.state){case"error":s=`{{${f}}}`;break;case"cached":s=`[(${f})]`;break;case"skipped":s=`[${f}]:::skipped`;break;default:s=`[${f}]`}return t.push(` ${r}${s}:::${c}`),{entryId:r,exitId:r}}function De(e,n,t){let r=V("parallel"),a=`${r}_fork`,o=`${r}_join`,u=ae(e.name??"Parallel");t.push(` subgraph ${r}[${u}]`),t.push(" direction TB"),t.push(` ${a}{Fork}`);let f=[];for(let s of e.children){let d=Z(s,n,t);t.push(` ${a} --> ${d.entryId}`),f.push(d.exitId)}t.push(` ${o}{Join}`);for(let s of f)t.push(` ${s} --> ${o}`);t.push(" end");let c=e.state;return t.push(` class ${r} ${c}`),{entryId:a,exitId:o}}function xe(e,n,t){let r=V("race"),a=`${r}_start`,o=`${r}_end`,u=ae(e.name??"Race");t.push(` subgraph ${r}["\u26A1 ${u}"]`),t.push(" direction TB"),t.push(` ${a}((Race))`);let f=[];for(let s of e.children){let d=Z(s,n,t);t.push(` ${a} --> ${d.entryId}`),f.push(d.exitId),_(s)&&e.winnerId===s.id&&t.push(` ${d.exitId} -. winner .-> ${o}`)}t.push(` ${o}((First))`);for(let s of f)(!e.winnerId||!e.children.some(d=>_(d)&&d.id===e.winnerId))&&t.push(` ${s} --> ${o}`);t.push(" end");let c=e.state;return t.push(` class ${r} ${c}`),{entryId:a,exitId:o}}function Ne(e,n,t){let r=e.key?`decision_${e.key.replace(/[^a-zA-Z0-9]/g,"_")}`:V("decision"),a=N(e.condition??"condition"),o=e.decisionValue!==void 0?` = ${N(String(e.decisionValue)).slice(0,30)}`:"",u=`${a}${o}`.trim();t.push(` ${r}{${u}}`);let f=[],c;for(let s of e.branches){let d=`${r}_${s.label.replace(/[^a-zA-Z0-9]/g,"_")}`,h=N(s.label),g=s.taken?`${h} \u2713`:`${h} skipped`,m=s.taken?":::success":":::skipped";t.push(` ${d}[${g}]${m}`);let w=s.condition?`|${N(s.condition).replace(/\|/g,"")}|`:"";if(t.push(` ${r} -->${w} ${d}`),s.children.length>0){let $=d;for(let v of s.children){let D=Z(v,n,t);t.push(` ${$} --> ${D.entryId}`),$=D.exitId}f.push($),s.taken&&(c=$)}else f.push(d),s.taken&&(c=d)}return c?{entryId:r,exitId:c}:{entryId:r,exitId:r}}var B={clearToEnd:"\x1B[J",cursorUp:e=>`\x1B[${e}A`,cursorToStart:"\x1B[G",hideCursor:"\x1B[?25l",showCursor:"\x1B[?25h",saveCursor:"\x1B[s",restoreCursor:"\x1B[u"};function Ie(e={}){let{workflowName:n,detectParallel:t=!0,showTimings:r=!0,showKeys:a=!1,colors:o,stream:u=process.stdout,updateInterval:f=100}=e,c=z({detectParallel:t}),s=F(),d={showTimings:r,showKeys:a,terminalWidth:u.columns??80,colors:{...W,...o}},h=!1,g="",m=0,w=null,$=!1;function v(S){u.writable&&u.write(S)}function D(){if(!h)return;let S=M(),L=s.render(S,d);L!==g&&(m>0&&(v(B.cursorUp(m)),v(B.cursorToStart),v(B.clearToEnd)),v(L),v(`
4
- `),g=L,m=L.split(`
5
- `).length)}function I(){h&&($=!0,w===null&&(w=setTimeout(()=>{w=null,$&&($=!1,D())},f)))}function T(S){if(S.type==="scope_start"||S.type==="scope_end"){b(S);return}c.handleEvent(S),h&&(S.type==="workflow_start"||S.type==="workflow_success"||S.type==="workflow_error"?D():I())}function b(S){c.handleScopeEvent(S),h&&I()}function R(S){c.handleDecisionEvent(S),h&&I()}function M(){let S=c.getIR();return n&&!S.root.name&&(S.root.name=n),S}function i(){return s.render(M(),d)}function l(){h||(h=!0,g="",m=0,v(B.hideCursor),D())}function k(){if(!h)return;h=!1,w!==null&&(clearTimeout(w),w=null);let S=M(),L=s.render(S,d);m>0&&(v(B.cursorUp(m)),v(B.cursorToStart),v(B.clearToEnd)),v(L),v(`
6
- `),v(B.showCursor)}function y(){w!==null&&(clearTimeout(w),w=null),$=!1,D()}function x(){c.reset(),g="",m=0}return{handleEvent:T,handleScopeEvent:b,handleDecisionEvent:R,getIR:M,render:i,start:l,stop:k,refresh:y,reset:x}}function ee(e,n={}){let{condition:t,value:r,name:a,workflowId:o=crypto.randomUUID(),emit:u}=n,f=Date.now(),c,s=[];function d(g,m,w){s.push({label:g,condition:w,taken:m}),m&&(c=g),u?.({type:"decision_branch",workflowId:o,decisionId:e,branchLabel:g,condition:w,taken:m,ts:Date.now()})}function h(){let g=Date.now()-f;u?.({type:"decision_end",workflowId:o,decisionId:e,branchTaken:c,ts:Date.now(),durationMs:g})}return u?.({type:"decision_start",workflowId:o,decisionId:e,condition:t,decisionValue:r,name:a,ts:f}),{takeBranch:d,end:h,getBranchTaken:()=>c,getBranches:()=>[...s]}}function Te(e,n,t={}){let r=ee(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 Re(e,n,t={}){let r=ee(e,{...t,condition:t.condition??`switch(${String(n)})`,value:n});return{...r,value:n,case:(a,o)=>{r.takeBranch(`case '${a}'`,o,`value === '${a}'`)},default:a=>{r.takeBranch("default",a)}}}function te(e={}){let{workflowName:n,detectParallel:t=!0,showTimings:r=!0,showKeys:a=!1,colors:o}=e,u=z({detectParallel:t}),f=new Set,c=F(),s=J(),d={showTimings:r,showKeys:a,terminalWidth:process.stdout?.columns??80,colors:{...W,...o}};function h(){if(f.size>0){let b=u.getIR();for(let R of f)R(b)}}function g(b){if(b.type==="scope_start"||b.type==="scope_end"){m(b);return}u.handleEvent(b),b.type,h()}function m(b){u.handleScopeEvent(b),h()}function w(b){u.handleDecisionEvent(b),h()}function $(){let b=u.getIR();return n&&!b.root.name&&(b.root.name=n),b}function v(){let b=$();return c.render(b,d)}function D(b){let R=$();switch(b){case"ascii":return c.render(R,d);case"mermaid":return s.render(R,d);case"json":return JSON.stringify(R,null,2);default:throw new Error(`Unknown format: ${b}`)}}function I(){u.reset(),h()}function T(b){return f.add(b),()=>f.delete(b)}return{handleEvent:g,handleScopeEvent:m,handleDecisionEvent:w,getIR:$,render:v,renderAs:D,reset:I,onUpdate:T}}function rt(e,n={}){let t=te(n);for(let r of e)r.type.startsWith("decision_")?t.handleDecisionEvent(r):t.handleEvent(r);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 r of n)r.type.startsWith("decision_")?t.handleDecisionEvent(r):t.handleEvent(r);return t.render()},visualizeAs:t=>{let r=te(e);for(let a of n)a.type.startsWith("decision_")?r.handleDecisionEvent(a):r.handleEvent(a);return r.renderAs(t)}}}export{F as asciiRenderer,ot as createEventCollector,z as createIRBuilder,Ie as createLiveVisualizer,le as createParallelDetector,te as createVisualizer,W as defaultColorScheme,A as detectParallelGroups,Fe as hasChildren,K as isDecisionNode,G as isParallelNode,U as isRaceNode,Le as isSequenceNode,_ as isStepNode,J as mermaidRenderer,ee as trackDecision,Te as trackIf,Re as trackSwitch,rt as visualizeEvents};
1
+ function O(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{maxGapMs:t=5}=n,r=[],c=[];for(let a=0;a<e.length;a++){let s=e[a];s.type==="step"&&s.startTs!==void 0?r.push({node:s,startTs:s.startTs,endTs:s.endTs??s.startTs+(s.durationMs??0),originalIndex:a}):c.push({node:s,originalIndex:a})}if(r.length<=1)return e;r.sort((a,s)=>a.startTs-s.startTs);let o=[],u=[r[0]];for(let a=1;a<r.length;a++){let s=r[a],l=Math.max(...u.map(m=>m.endTs));s.startTs<=l+t?u.push(s):(o.push(u),u=[s])}o.push(u);let f=[];for(let a of o){let s=Math.min(...a.map(l=>l.originalIndex));if(a.length===1)f.push({node:a[0].node,position:s});else{let l=a.map(w=>w.node),m=Math.min(...a.map(w=>w.startTs)),b=Math.max(...a.map(w=>w.endTs)),g={type:"parallel",id:`detected_parallel_${m}`,name:`${l.length} parallel steps`,state:de(l),mode:"all",children:l,startTs:m,endTs:b,durationMs:b-m};f.push({node:g,position:s})}}for(let{node:a,originalIndex:s}of c)f.push({node:a,position:s});return f.sort((a,s)=>a.position-s.position),f.map(a=>a.node)}function de(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 le(e={}){return{detect:n=>A(n,e)}}function F(e={}){let{detectParallel:n=!0,parallelDetection:t}=e,r,c,o="pending",u,f,a=new Map,s=[],l=[],m=[],b=Date.now(),g=b;function w(i){return i.stepId??i.stepKey??i.name??Y()}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}}s.length>0?s[s.length-1].children.push(i):m.push(i),g=Date.now()}function S(i){switch(i.type){case"workflow_start":r=i.workflowId,c=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);a.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=a.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),a.delete(d)}break}case"step_error":{let d=w(i),p=a.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),a.delete(d)}break}case"step_aborted":{let d=w(i),p=a.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),a.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=a.get(d);p&&(p.timedOut=!0,p.timeoutMs=i.timeoutMs),g=Date.now();break}case"step_retry":{let d=w(i),p=a.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")s.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=s.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 a)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=A(i,t)),{root:{type:"workflow",id:r??Y(),workflowId:r??"unknown",state:o,startTs:c,durationMs:f,children:i,error:u},metadata:{createdAt:b,lastUpdatedAt:g}}}function W(){r=void 0,c=void 0,o="pending",u=void 0,f=void 0,a.clear(),s.length=0,l.length=0,m=[],b=Date.now(),g=b}return{handleEvent:S,handleScopeEvent:D,handleDecisionEvent:I,getIR:R,reset:W,get hasActiveSteps(){return a.size>0},get state(){return o}}}function M(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 E(e){return`${re}${e}${H}`}var C={pending:ge,running:he,success:fe,error:pe,aborted:ne,cached:me,skipped:re+ne};function we(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=we(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 h={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,r=Math.max(0,n-t);return e+" ".repeat(r)}function be(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 c=2,o=r-c;return h.horizontal.repeat(c)+t+h.horizontal.repeat(o)}function L(){return{name:"ascii",supportsLive:!0,render(e,n){let t={...C,...n.colors},r=n.terminalWidth??60,c=r-4,o=[],u=e.root.name??"workflow",f=z(u);o.push(`${h.topLeft}${be(r-2,f)}${h.topRight}`),o.push(`${h.vertical}${" ".repeat(r-2)}${h.vertical}`);let a=q(e.root.children,n,t,0);for(let s of a)o.push(`${h.vertical} ${ie(s,c)}${h.vertical}`);if(o.push(`${h.vertical}${" ".repeat(r-2)}${h.vertical}`),e.root.durationMs!==void 0&&n.showTimings){let s=e.root.state==="success"?"Completed":"Failed",m=`${X(s,e.root.state,t)} in ${O(e.root.durationMs)}`;o.push(`${h.vertical} ${ie(m,c)}${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 q(e,n,t,r){let c=[];for(let o of e)M(o)?c.push(Q(o,n,t)):G(o)?c.push(...ke(o,n,t,r)):U(o)?c.push(...ye(o,n,t,r)):K(o)&&c.push(...$e(o,n,t,r));return c}function Q(e,n,t){let r=P(e.state,t),c=e.name??e.key??"step",o=X(c,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(` [${O(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 ke(e,n,t,r){let c=[],o=" ".repeat(r),u=P(e.state,t),f=e.name??"parallel",a=e.mode==="allSettled"?" (allSettled)":"";c.push(`${o}${h.teeRight}${h.teeDown}${h.horizontal} ${u} ${z(f)}${a}`);for(let s=0;s<e.children.length;s++){let l=e.children[s],b=s===e.children.length-1?`${o}${h.vertical} ${h.bottomLeft}`:`${o}${h.vertical} ${h.teeRight}`;if(M(l))c.push(`${b} ${Q(l,n,t)}`);else{let g=q([l],n,t,r+1);for(let w of g)c.push(`${o}${h.vertical} ${w}`)}}return n.showTimings&&e.durationMs!==void 0&&c.push(`${o}${h.bottomLeft}${h.horizontal}${h.horizontal} ${E(`[${O(e.durationMs)}]`)}`),c}function ye(e,n,t,r){let c=[],o=" ".repeat(r),u=P(e.state,t),f=e.name??"race";c.push(`${o}${h.teeRight}\u26A1 ${u} ${z(f)}`);for(let a=0;a<e.children.length;a++){let s=e.children[a],m=a===e.children.length-1?`${o}${h.vertical} ${h.bottomLeft}`:`${o}${h.vertical} ${h.teeRight}`,g=e.winnerId&&s.id===e.winnerId?E(" (winner)"):"";if(M(s))c.push(`${m} ${Q(s,n,t)}${g}`);else{let w=q([s],n,t,r+1);for(let y of w)c.push(`${o}${h.vertical} ${y}`)}}return n.showTimings&&e.durationMs!==void 0&&c.push(`${o}${h.bottomLeft}${h.horizontal}${h.horizontal} ${E(`[${O(e.durationMs)}]`)}`),c}function $e(e,n,t,r){let c=[],o=" ".repeat(r),u=P(e.state,t),f=e.name??"decision",a=e.condition?E(` (${e.condition})`):"",s=e.decisionValue!==void 0?E(` = ${String(e.decisionValue)}`):"",l=e.branchTaken!==void 0?E(` \u2192 ${String(e.branchTaken)}`):"";c.push(`${o}${h.teeRight}${h.teeDown}${h.horizontal} ${u} ${z(f)}${a}${s}${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=j(`${y} ${b.label}`,S),I=b.condition?E(` (${b.condition})`):"";if(c.push(`${w} ${D}${I}`),b.children.length>0){let T=q(b.children,n,t,r+1);for(let k of T)c.push(`${o}${h.vertical} ${k}`)}else b.taken||c.push(`${o}${h.vertical} ${E("(skipped)")}`)}return n.showTimings&&e.durationMs!==void 0&&c.push(`${o}${h.bottomLeft}${h.horizontal}${h.horizontal} ${E(`[${O(e.durationMs)}]`)}`),c}function Se(){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 se=0;function V(e="node"){return`${e}_${++se}`}function ve(){se=0}function N(e){return e.replace(/[{}[\]()]/g,"").replace(/[<>]/g,"").replace(/"/g,"'").trim()}function ae(e){return N(e).replace(/[[\]]/g,"")}function J(){return{name:"mermaid",supportsLive:!1,render(e,n){ve();let t=[];t.push("flowchart TD");let r="start";t.push(` ${r}((Start))`);let c=r;for(let o of e.root.children){let u=Z(o,n,t);t.push(` ${c} --> ${u.entryId}`),c=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(` ${c} --> ${o}`)}return t.push(""),t.push(...Se()),t.join(`
3
+ `)}}}function Z(e,n,t){if(M(e))return Ee(e,n,t);if(G(e))return De(e,n,t);if(U(e))return xe(e,n,t);if(K(e))return Ne(e,n,t);let r=V("unknown");return t.push(` ${r}[Unknown Node]`),{entryId:r,exitId:r}}function Ee(e,n,t){let r=e.key?`step_${e.key.replace(/[^a-zA-Z0-9]/g,"_")}`:V("step"),c=N(e.name??e.key??"Step"),o=n.showTimings&&e.durationMs!==void 0?` ${O(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 a=(c+u+f+o).trim(),s=e.state,l;switch(e.state){case"error":l=`{{${a}}}`;break;case"cached":l=`[(${a})]`;break;case"skipped":l=`[${a}]:::skipped`;break;default:l=`[${a}]`}return t.push(` ${r}${l}:::${s}`),{entryId:r,exitId:r}}function De(e,n,t){let r=V("parallel"),c=`${r}_fork`,o=`${r}_join`,u=ae(e.name??"Parallel");t.push(` subgraph ${r}[${u}]`),t.push(" direction TB"),t.push(` ${c}{Fork}`);let f=[];for(let s of e.children){let l=Z(s,n,t);t.push(` ${c} --> ${l.entryId}`),f.push(l.exitId)}t.push(` ${o}{Join}`);for(let s of f)t.push(` ${s} --> ${o}`);t.push(" end");let a=e.state;return t.push(` class ${r} ${a}`),{entryId:c,exitId:o}}function xe(e,n,t){let r=V("race"),c=`${r}_start`,o=`${r}_end`,u=ae(e.name??"Race");t.push(` subgraph ${r}["\u26A1 ${u}"]`),t.push(" direction TB"),t.push(` ${c}((Race))`);let f=[];for(let s of e.children){let l=Z(s,n,t);t.push(` ${c} --> ${l.entryId}`),f.push(l.exitId),M(s)&&e.winnerId===s.id&&t.push(` ${l.exitId} -. winner .-> ${o}`)}t.push(` ${o}((First))`);for(let s of f)(!e.winnerId||!e.children.some(l=>M(l)&&l.id===e.winnerId))&&t.push(` ${s} --> ${o}`);t.push(" end");let a=e.state;return t.push(` class ${r} ${a}`),{entryId:c,exitId:o}}function Ne(e,n,t){let r=e.key?`decision_${e.key.replace(/[^a-zA-Z0-9]/g,"_")}`:V("decision"),c=N(e.condition??"condition"),o=e.decisionValue!==void 0?` = ${N(String(e.decisionValue)).slice(0,30)}`:"",u=`${c}${o}`.trim();t.push(` ${r}{${u}}`);let f=[],a;for(let s of e.branches){let l=`${r}_${s.label.replace(/[^a-zA-Z0-9]/g,"_")}`,m=N(s.label),b=s.taken?`${m} \u2713`:`${m} skipped`,g=s.taken?":::success":":::skipped";t.push(` ${l}[${b}]${g}`);let w=s.condition?`|${N(s.condition).replace(/\|/g,"")}|`:"";if(t.push(` ${r} -->${w} ${l}`),s.children.length>0){let y=l;for(let S of s.children){let D=Z(S,n,t);t.push(` ${y} --> ${D.entryId}`),y=D.exitId}f.push(y),s.taken&&(a=y)}else f.push(l),s.taken&&(a=l)}return a?{entryId:r,exitId:a}:{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 Ie(e={}){let{workflowName:n,detectParallel:t=!0,showTimings:r=!0,showKeys:c=!1,colors:o,stream:u=process.stdout,updateInterval:f=100}=e,a=F({detectParallel:t}),s=L(),l={showTimings:r,showKeys:c,terminalWidth:u.columns??80,colors:{...C,...o}},m=!1,b="",g=0,w=null,y=!1;function S($){u.writable&&u.write($)}function D(){if(!m)return;let $=W(),B=s.render($,l);B!==b&&(g>0&&(S(_.cursorUp(g)),S(_.cursorToStart),S(_.clearToEnd)),S(B),S(`
4
+ `),b=B,g=B.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}a.handleEvent($),m&&($.type==="workflow_start"||$.type==="workflow_success"||$.type==="workflow_error"?D():I())}function k($){a.handleScopeEvent($),m&&I()}function R($){a.handleDecisionEvent($),m&&I()}function W(){let $=a.getIR();return n&&!$.root.name&&($.root.name=n),$}function i(){return s.render(W(),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 $=W(),B=s.render($,l);g>0&&(S(_.cursorUp(g)),S(_.cursorToStart),S(_.clearToEnd)),S(B),S(`
6
+ `),S(_.showCursor)}function v(){w!==null&&(clearTimeout(w),w=null),y=!1,D()}function x(){a.reset(),b="",g=0}return{handleEvent:T,handleScopeEvent:k,handleDecisionEvent:R,getIR:W,render:i,start:d,stop:p,refresh:v,reset:x}}function ee(e,n={}){let{condition:t,value:r,name:c,workflowId:o=crypto.randomUUID(),emit:u}=n,f=Date.now(),a,s=[];function l(b,g,w){s.push({label:b,condition:w,taken:g}),g&&(a=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:a,ts:Date.now(),durationMs:b})}return u?.({type:"decision_start",workflowId:o,decisionId:e,condition:t,decisionValue:r,name:c,ts:f}),{takeBranch:l,end:m,getBranchTaken:()=>a,getBranches:()=>[...s]}}function Te(e,n,t={}){let r=ee(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 Re(e,n,t={}){let r=ee(e,{...t,condition:t.condition??`switch(${String(n)})`,value:n});return{...r,value:n,case:(c,o)=>{r.takeBranch(`case '${c}'`,o,`value === '${c}'`)},default:c=>{r.takeBranch("default",c)}}}function te(e={}){let{workflowName:n,detectParallel:t=!0,showTimings:r=!0,showKeys:c=!1,colors:o}=e,u=F({detectParallel:t}),f=new Set,a=L(),s=J(),l={showTimings:r,showKeys:c,terminalWidth:process.stdout?.columns??80,colors:{...C,...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 a.render(k,l)}function D(k){let R=y();switch(k){case"ascii":return a.render(R,l);case"mermaid":return s.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 rt(e,n={}){let t=te(n);for(let r of e)r.type.startsWith("decision_")?t.handleDecisionEvent(r):t.handleEvent(r);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 r of n)r.type.startsWith("decision_")?t.handleDecisionEvent(r):t.handleEvent(r);return t.render()},visualizeAs:t=>{let r=te(e);for(let c of n)c.type.startsWith("decision_")?r.handleDecisionEvent(c):r.handleEvent(c);return r.renderAs(t)}}}export{L as asciiRenderer,ot as createEventCollector,F as createIRBuilder,Ie as createLiveVisualizer,le as createParallelDetector,te as createVisualizer,C as defaultColorScheme,A as detectParallelGroups,Le as hasChildren,K as isDecisionNode,G as isParallelNode,U as isRaceNode,Be as isSequenceNode,M as isStepNode,J as mermaidRenderer,ee as trackDecision,Te as trackIf,Re as trackSwitch,rt as visualizeEvents};
7
7
  //# sourceMappingURL=visualize.js.map