@zibby/agent-workflow 0.2.0 → 0.3.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,4 +1,4 @@
1
- var D=Object.defineProperty;var T=(e,o)=>()=>(e&&(o=e(e=0)),o);var L=(e,o)=>{for(var n in o)D(e,n,{get:o[n],enumerable:!0})};function v(e){return M.get(e)||null}var $,M,S=T(()=>{$=Symbol.for("@zibby/agent-workflow.skills");globalThis[$]||(globalThis[$]=new Map);M=globalThis[$]});var _,F,N,k,x=T(()=>{_=()=>{},F={debug:_,info:_,warn:(...e)=>console.warn("[workflow]",...e),error:(...e)=>console.error("[workflow]",...e)},N={impl:F},k={debug:(...e)=>N.impl.debug?.(...e),info:(...e)=>N.impl.info?.(...e),warn:(...e)=>N.impl.warn?.(...e),error:(...e)=>N.impl.error?.(...e)}});var A=T(()=>{});var I={};L(I,{getAgentStrategy:()=>C,invokeAgent:()=>B,listStrategies:()=>z,registerStrategy:()=>G});function G(e){if(!e||typeof e.getName!="function"||typeof e.invoke!="function")throw new Error("strategy must implement getName() and invoke() (AgentStrategy shape)");let o=m.findIndex(n=>n.getName()===e.getName());o>=0?m[o]=e:m.push(e)}function z(){return m.map(e=>e.getName())}function C(e={}){let{state:o={},preferredAgent:n=null}=e,r=n||o.agentType||process.env.AGENT_TYPE;if(!r){let s=m.map(t=>t.getName()).join(", ")||"none registered";throw new Error(`No agent specified. Set agentType in state or AGENT_TYPE env var. Available: ${s}`)}k.debug(`[workflow] agent selection: requested=${r}`);let i=m.find(s=>s.getName()===r);if(!i){let s=m.map(t=>t.getName()).join(", ")||"none registered";throw new Error(`Unknown agent '${r}'. Available: ${s}`)}if(!i.canHandle(e))throw new Error(`Agent '${r}' is not available in this environment. Check credentials/environment.`);return k.debug(`[workflow] using agent: ${i.getName()}`),i}async function B(e,o={},n={}){let r=C(o),i=o.state?.config||n.config||{},s=i.models||{},t=n.nodeName&&s[n.nodeName]||null,a=s.default||null,c=i.agent?.[r.name]?.model||null,l=t||a||c||n.model||null,p={...n,model:l,workspace:o.state?.workspace||n.workspace,schema:n.schema||o.schema,images:n.images||o.images||[],skills:n.skills||o.skills||[],config:i},u=e,g=p.skills||[];if(g.length>0&&!n.skipPromptFragments){let d=g.map(y=>{let f=v(y)?.promptFragment;return typeof f=="function"?f():f}).filter(Boolean);d.length>0&&(u+=`
1
+ var L=Object.defineProperty;var k=(e,o)=>()=>(e&&(o=e(e=0)),o);var M=(e,o)=>{for(var n in o)L(e,n,{get:o[n],enumerable:!0})};function S(e){return G.get(e)||null}var $,G,v=k(()=>{$=Symbol.for("@zibby/agent-workflow.skills");globalThis[$]||(globalThis[$]=new Map);G=globalThis[$]});var _,z,N,T,x=k(()=>{_=()=>{},z={debug:_,info:_,warn:(...e)=>console.warn("[workflow]",...e),error:(...e)=>console.error("[workflow]",...e)},N={impl:z},T={debug:(...e)=>N.impl.debug?.(...e),info:(...e)=>N.impl.info?.(...e),warn:(...e)=>N.impl.warn?.(...e),error:(...e)=>N.impl.error?.(...e)}});var C=k(()=>{});var j={};M(j,{getAgentStrategy:()=>I,invokeAgent:()=>Y,listStrategies:()=>B,registerStrategy:()=>F});function F(e){if(!e||typeof e.getName!="function"||typeof e.invoke!="function")throw new Error("strategy must implement getName() and invoke() (AgentStrategy shape)");let o=m.findIndex(n=>n.getName()===e.getName());o>=0?m[o]=e:m.push(e)}function B(){return m.map(e=>e.getName())}function I(e={}){let{state:o={},preferredAgent:n=null}=e,r=n||o.agentType||process.env.AGENT_TYPE;if(!r){let s=m.map(t=>t.getName()).join(", ")||"none registered";throw new Error(`No agent specified. Set agentType in state or AGENT_TYPE env var. Available: ${s}`)}T.debug(`[workflow] agent selection: requested=${r}`);let i=m.find(s=>s.getName()===r);if(!i){let s=m.map(t=>t.getName()).join(", ")||"none registered";throw new Error(`Unknown agent '${r}'. Available: ${s}`)}if(!i.canHandle(e))throw new Error(`Agent '${r}' is not available in this environment. Check credentials/environment.`);return T.debug(`[workflow] using agent: ${i.getName()}`),i}async function Y(e,o={},n={}){let r=I(o),i=o.state?.config||n.config||{},s=i.models||{},t=n.nodeName&&s[n.nodeName]||null,a=s.default||null,c=i.agent?.[r.name]?.model||null,l=t||a||c||n.model||null,p={...n,model:l,workspace:o.state?.workspace||n.workspace,schema:n.schema||o.schema,images:n.images||o.images||[],skills:n.skills||o.skills||[],config:i},u=e,g=p.skills||[];if(g.length>0&&!n.skipPromptFragments){let d=g.map(y=>{let f=S(y)?.promptFragment;return typeof f=="function"?f():f}).filter(Boolean);d.length>0&&(u+=`
2
2
 
3
3
  ${d.join(`
4
4
 
@@ -9,7 +9,7 @@ PRIORITY OVERRIDE \u2014 THE FOLLOWING INSTRUCTIONS TAKE PRECEDENCE OVER ALL PRE
9
9
  \u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501
10
10
 
11
11
  ${h}
12
- `),k.debug(`[workflow] prompt length: ${u.length} chars`),r.invoke(u,p)}var E,m,j=T(()=>{A();x();S();E=Symbol.for("@zibby/agent-workflow.strategies");globalThis[E]||(globalThis[E]=[]);m=globalThis[E]});S();x();var b={};var O=new Map;function U(e,o){O.set(e,o)}function P(e){let o=O.get(e);return o?o.factory&&typeof o.create=="function"?o.create.toString():typeof o.execute=="function"?o.execute.toString():typeof o=="function"?o.toString():null:null}U("ai_agent",{name:"ai_agent",factory:!0,create:(e,o={})=>({name:e,_isCustomCode:!0,execute:async n=>{let r=n?._coreInvokeAgent;r||(r=(await Promise.resolve().then(()=>(j(),I))).invokeAgent);let i=o.extraPromptInstructions||"Execute the task based on the current state.",s=V(i,n),t=await r(s,{cwd:n.workspace||process.cwd(),model:n.model,tools:o.resolvedTools||null});return{success:!0,output:{raw:t,nodeId:e},raw:typeof t=="string"?t:t.raw}}})});function V(e,o){let n=/@([\w.]+)/g,r=new Set,i;for(;(i=n.exec(e))!==null;)r.add(i[1]);if(r.size===0)return e;let s=[],t=new Set;for(let a of r){let c=a.split(".")[0];if(t.has(c))continue;let l=a.split(".").reduce((g,h)=>g?.[h],o);if(l===void 0)continue;let p=typeof l=="string"?l:l?.raw??JSON.stringify(l,null,2),u=a.replace(/_/g," ").replace(/\b\w/g,g=>g.toUpperCase());s.push(`## ${u}
12
+ `),T.debug(`[workflow] prompt length: ${u.length} chars`),r.invoke(u,p)}var E,m,O=k(()=>{C();x();v();E=Symbol.for("@zibby/agent-workflow.strategies");globalThis[E]||(globalThis[E]=[]);m=globalThis[E]});v();x();var A={};var b=Symbol.for("@zibby/agent-workflow.nodes");globalThis[b]||(globalThis[b]=new Map);var R=globalThis[b];function U(e,o){R.set(e,o)}function P(e){let o=R.get(e);return o?o.factory&&typeof o.create=="function"?o.create.toString():typeof o.execute=="function"?o.execute.toString():typeof o=="function"?o.toString():null:null}U("ai_agent",{name:"ai_agent",factory:!0,create:(e,o={})=>({name:e,_isCustomCode:!0,execute:async n=>{let r=n?._coreInvokeAgent;r||(r=(await Promise.resolve().then(()=>(O(),j))).invokeAgent);let i=o.extraPromptInstructions||"Execute the task based on the current state.",s=V(i,n),t=await r(s,{cwd:n.workspace||process.cwd(),model:n.model,tools:o.resolvedTools||null});return{success:!0,output:{raw:t,nodeId:e},raw:typeof t=="string"?t:t.raw}}})});function V(e,o){let n=/@([\w.]+)/g,r=new Set,i;for(;(i=n.exec(e))!==null;)r.add(i[1]);if(r.size===0)return e;let s=[],t=new Set;for(let a of r){let c=a.split(".")[0];if(t.has(c))continue;let l=a.split(".").reduce((g,h)=>g?.[h],o);if(l===void 0)continue;let p=typeof l=="string"?l:l?.raw??JSON.stringify(l,null,2),u=a.replace(/_/g," ").replace(/\b\w/g,g=>g.toUpperCase());s.push(`## ${u}
13
13
  ${p}`),a.includes(".")||t.add(c)}return s.length===0?e:`${e}
14
14
 
15
15
  ---
@@ -17,13 +17,13 @@ ${p}`),a.includes(".")||t.add(c)}return s.length===0?e:`${e}
17
17
 
18
18
  ${s.join(`
19
19
 
20
- `)}`}function ge(e,o={}){let{nodes:n,edges:r,nodeConfigs:i={}}=e,s=new Set,t=[],a=new Map;for(let f of n){let w=f.data?.nodeType||f.type;a.set(f.id,w),w==="decision"?s.add(f.id):t.push({id:f.id,nodeType:w,label:f.data?.label||f.id})}let c=t.some(f=>{let w=i[f.id]||{};return!w.customCode&&!w.executeCode}),{toolsPerNode:l,toolIdsByVar:p}=q(t,i),{simpleEdges:u,conditionalEdges:g}=Q(r,s),h=X(t,r,s),d=[],y=o.workflowType||"workflow";return d.push(Y(o)),d.push(H(y,{usesRegisteredNodes:c})),d.push(W(p)),d.push(J(y)),d.push(K(t,i)),d.push(Z(t,h,u,g,l,y)),d.filter(Boolean).join(`
21
- `)}function me(e){let o={};for(let[n,r]of Object.entries(e)){let{tools:i,...s}=r;Object.keys(s).length>0&&(o[n]=s)}return o}function Y(e){let o=e.workflowType||"workflow";return["// Generated workflow",`// ${e.projectId?`Project: ${e.projectId} | `:""}Type: ${o} | Version: ${e.version??0}`,`// Downloaded: ${new Date().toISOString()}`,""].join(`
22
- `)}function H(e,{usesRegisteredNodes:o=!0}={}){let n=["import { WorkflowGraph, invokeAgent, getResolvedToolDefinitions } from '@zibby/agent-workflow';"];return o&&n.push("// import './register-nodes.js'; // register custom node types here"),n.push("import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'node:fs';","import { join, dirname } from 'node:path';","import { fileURLToPath } from 'node:url';",""),n.join(`
20
+ `)}`}function me(e,o={}){let{nodes:n,edges:r,nodeConfigs:i={}}=e,s=new Set,t=[],a=new Map;for(let f of n){let w=f.data?.nodeType||f.type;a.set(f.id,w),w==="decision"?s.add(f.id):t.push({id:f.id,nodeType:w,label:f.data?.label||f.id})}let c=t.some(f=>{let w=i[f.id]||{};return!w.customCode&&!w.executeCode}),{toolsPerNode:l,toolIdsByVar:p}=Q(t,i),{simpleEdges:u,conditionalEdges:g}=X(r,s),h=ee(t,r,s),d=[],y=o.workflowType||"workflow";return d.push(H(o)),d.push(K(y,{usesRegisteredNodes:c})),d.push(W(p)),d.push(J(y)),d.push(Z(t,i)),d.push(q(t,h,u,g,l,y)),d.filter(Boolean).join(`
21
+ `)}function he(e){let o={};for(let[n,r]of Object.entries(e)){let{tools:i,...s}=r;Object.keys(s).length>0&&(o[n]=s)}return o}function H(e){let o=e.workflowType||"workflow";return["// Generated workflow",`// ${e.projectId?`Project: ${e.projectId} | `:""}Type: ${o} | Version: ${e.version??0}`,`// Downloaded: ${new Date().toISOString()}`,""].join(`
22
+ `)}function K(e,{usesRegisteredNodes:o=!0}={}){let n=["import { WorkflowGraph, invokeAgent, getResolvedToolDefinitions } from '@zibby/agent-workflow';"];return o&&n.push("// import './register-nodes.js'; // register custom node types here"),n.push("import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'node:fs';","import { join, dirname } from 'node:path';","import { fileURLToPath } from 'node:url';",""),n.join(`
23
23
  `)}function W(e){if(e.size===0)return"";let o=["// \u2500\u2500 Tool Bindings \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"];for(let[n,r]of e)o.push(`const ${n} = getResolvedToolDefinitions(${JSON.stringify(r)}); // ${r.join(", ")}`);return o.push(""),o.join(`
24
24
  `)}function J(e){return["// \u2500\u2500 Node Configs \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500","const __filename = fileURLToPath(import.meta.url);","const __dirname = dirname(__filename);",`const configPath = join(__dirname, 'workflow-${e}.config.json');`,"const nodeConfigs = existsSync(configPath) ? JSON.parse(readFileSync(configPath, 'utf-8')) : {};",""].join(`
25
- `)}function K(e,o){let n=["// \u2500\u2500 Node Implementations \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500",""];for(let r of e){let i=R(r.id),s=o[r.id]?.customCode;if(s)n.push(`// @custom \u2014 modified from default "${r.nodeType}" template`),n.push(`const ${i}_execute = ${s};`);else{let t=P(r.nodeType);t?(n.push(`// Default "${r.nodeType}" implementation`),n.push(`const ${i}_execute = ${t};`)):(n.push(`// No template for "${r.nodeType}" \u2014 passthrough`),n.push(`const ${i}_execute = async (state) => ({ success: true, output: {}, raw: null });`))}n.push("")}return n.join(`
26
- `)}function Z(e,o,n,r,i,s){let t=["// \u2500\u2500 Graph Builder \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"];t.push("export function buildGraph(options = {}) {"),t.push(" const graph = new WorkflowGraph(options);",""),t.push(" // Nodes");for(let c of e){let l=R(c.id);t.push(` graph.addNode('${c.id}', { name: '${c.id}', execute: ${l}_execute });`),t.push(` graph.setNodeType('${c.id}', '${c.nodeType}');`)}t.push("",` graph.setEntryPoint('${o}');`,""),(n.length>0||r.length>0)&&t.push(" // Edges");for(let c of n)t.push(` graph.addEdge('${c.source}', '${c.target}');`);for(let c of r){let l=c.code.split(`
25
+ `)}function Z(e,o){let n=["// \u2500\u2500 Node Implementations \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500",""];for(let r of e){let i=D(r.id),s=o[r.id]?.customCode;if(s)n.push(`// @custom \u2014 modified from default "${r.nodeType}" template`),n.push(`const ${i}_execute = ${s};`);else{let t=P(r.nodeType);t?(n.push(`// Default "${r.nodeType}" implementation`),n.push(`const ${i}_execute = ${t};`)):(n.push(`// No template for "${r.nodeType}" \u2014 passthrough`),n.push(`const ${i}_execute = async (state) => ({ success: true, output: {}, raw: null });`))}n.push("")}return n.join(`
26
+ `)}function q(e,o,n,r,i,s){let t=["// \u2500\u2500 Graph Builder \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"];t.push("export function buildGraph(options = {}) {"),t.push(" const graph = new WorkflowGraph(options);",""),t.push(" // Nodes");for(let c of e){let l=D(c.id);t.push(` graph.addNode('${c.id}', { name: '${c.id}', execute: ${l}_execute });`),t.push(` graph.setNodeType('${c.id}', '${c.nodeType}');`)}t.push("",` graph.setEntryPoint('${o}');`,""),(n.length>0||r.length>0)&&t.push(" // Edges");for(let c of n)t.push(` graph.addEdge('${c.source}', '${c.target}');`);for(let c of r){let l=c.code.split(`
27
27
  `).map((p,u)=>u===0?p:` ${p}`).join(`
28
28
  `);t.push(` graph.addConditionalEdges('${c.source}', ${l});`)}let a=[];for(let c of e){let l=i.get(c.id);l&&a.push(` '${c.id}': ${l},`)}return a.length>0&&t.push(""," graph.resolvedToolsMap = {",...a," };"),t.push(""," return graph;","}",""),t.push("export { nodeConfigs };",""),t.join(`
29
- `)}function q(e,o){let n=new Map,r=new Map;for(let i of e){let s=o[i.id]?.tools,t;if(Array.isArray(s)&&s.length>0)t=[...s].sort();else{let a=b[i.nodeType];a?.length>0&&(t=[...a].sort())}if(t){let a=`${t.map(c=>c.replace(/[^a-zA-Z0-9]/g,"")).join("And")}Tools`;n.set(i.id,a),r.has(a)||r.set(a,t)}}return{toolsPerNode:n,toolIdsByVar:r}}function Q(e,o){let n=[],r=[],i=new Map,s=new Set;for(let t of e)i.has(t.source)||i.set(t.source,[]),i.get(t.source).push(t);for(let t of e)if(!o.has(t.source))if(o.has(t.target)){if(s.has(t.target))continue;s.add(t.target);let c=(i.get(t.target)||[]).find(l=>l.data?.conditionalCode||l.conditionalCode);c&&r.push({source:t.source,code:c.data?.conditionalCode||c.conditionalCode})}else n.push({source:t.source,target:t.target});return{simpleEdges:n,conditionalEdges:r}}function X(e,o,n){let r=new Set;for(let s of o)n.has(s.target)||r.add(s.target);let i=e.find(s=>!r.has(s.id));return i?i.id:e[0]?.id}function R(e){return e.replace(/[^a-zA-Z0-9]/g,"_")}export{me as generateNodeConfigsJson,ge as generateWorkflowCode};
29
+ `)}function Q(e,o){let n=new Map,r=new Map;for(let i of e){let s=o[i.id]?.tools,t;if(Array.isArray(s)&&s.length>0)t=[...s].sort();else{let a=A[i.nodeType];a?.length>0&&(t=[...a].sort())}if(t){let a=`${t.map(c=>c.replace(/[^a-zA-Z0-9]/g,"")).join("And")}Tools`;n.set(i.id,a),r.has(a)||r.set(a,t)}}return{toolsPerNode:n,toolIdsByVar:r}}function X(e,o){let n=[],r=[],i=new Map,s=new Set;for(let t of e)i.has(t.source)||i.set(t.source,[]),i.get(t.source).push(t);for(let t of e)if(!o.has(t.source))if(o.has(t.target)){if(s.has(t.target))continue;s.add(t.target);let c=(i.get(t.target)||[]).find(l=>l.data?.conditionalCode||l.conditionalCode);c&&r.push({source:t.source,code:c.data?.conditionalCode||c.conditionalCode})}else n.push({source:t.source,target:t.target});return{simpleEdges:n,conditionalEdges:r}}function ee(e,o,n){let r=new Set;for(let s of o)n.has(s.target)||r.add(s.target);let i=e.find(s=>!r.has(s.id));return i?i.id:e[0]?.id}function D(e){return e.replace(/[^a-zA-Z0-9]/g,"_")}export{he as generateNodeConfigsJson,me as generateWorkflowCode};
@@ -8,17 +8,9 @@ export const SESSION_INFO_FILE: ".session-info.json";
8
8
  * Written by any consumer (CLI Ctrl+C handler, IDE plugin, desktop app) to
9
9
  * request that an in-flight workflow stop at the next abort-checkpoint.
10
10
  * WorkflowGraph polls for this file between nodes and exits cleanly.
11
- *
12
- * The engine accepts BOTH this generic name and the legacy Studio-specific
13
- * name during the AbortSignal-migration BC window. New consumers should
14
- * use STOP_REQUEST_FILE.
11
+ * Consumers should prefer the AbortSignal contract (`graph.run({ signal })`).
15
12
  */
16
13
  export const STOP_REQUEST_FILE: ".zibby-stop";
17
- /**
18
- * @deprecated Use STOP_REQUEST_FILE. Will be removed in v2 once the
19
- * AbortSignal contract is universally adopted across consumers.
20
- */
21
- export const STUDIO_STOP_REQUEST_FILE: ".zibby-studio-stop";
22
14
  export const RESULT_FILE: "result.json";
23
15
  export const RAW_OUTPUT_FILE: "raw_stream_output.txt";
24
16
  export const EVENTS_FILE: "events.json";
package/dist/constants.js CHANGED
@@ -1 +1 @@
1
- var o=".zibby/output",t="sessions",s=".session-info.json",_=".zibby-stop",E=".zibby-studio-stop",I="result.json",r="raw_stream_output.txt",e="events.json",S={BROWSER:"browser",JIRA:"jira",GITHUB:"github",SLACK:"slack",MEMORY:"memory",CHAT_MEMORY:"chat-memory",RUNNER:"runner",SKILL_INSTALLER:"skill-installer",CORE_TOOLS:"core-tools",WORKFLOW_BUILDER:"workflow-builder"},L=["CI_JOB_ID","GITHUB_RUN_ID","CIRCLE_WORKFLOW_ID","BUILD_ID"];export{L as CI_ENV_VARS,o as DEFAULT_OUTPUT_BASE,e as EVENTS_FILE,r as RAW_OUTPUT_FILE,I as RESULT_FILE,t as SESSIONS_DIR,s as SESSION_INFO_FILE,S as SKILLS,_ as STOP_REQUEST_FILE,E as STUDIO_STOP_REQUEST_FILE};
1
+ var o=".zibby/output",t="sessions",s=".session-info.json",_=".zibby-stop",I="result.json",r="raw_stream_output.txt",E="events.json",e={BROWSER:"browser",JIRA:"jira",GITHUB:"github",SLACK:"slack",MEMORY:"memory",CHAT_MEMORY:"chat-memory",RUNNER:"runner",SKILL_INSTALLER:"skill-installer",CORE_TOOLS:"core-tools",WORKFLOW_BUILDER:"workflow-builder"},L=["CI_JOB_ID","GITHUB_RUN_ID","CIRCLE_WORKFLOW_ID","BUILD_ID"];export{L as CI_ENV_VARS,o as DEFAULT_OUTPUT_BASE,E as EVENTS_FILE,r as RAW_OUTPUT_FILE,I as RESULT_FILE,t as SESSIONS_DIR,s as SESSION_INFO_FILE,e as SKILLS,_ as STOP_REQUEST_FILE};
@@ -1,40 +1,40 @@
1
- var ut=Object.defineProperty;var le=(o=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(o,{get:(e,t)=>(typeof require<"u"?require:e)[t]}):o)(function(o){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+o+'" is not supported')});var q=(o,e)=>()=>(o&&(e=o(o=0)),e);var Oe=(o,e)=>{for(var t in e)ut(o,t,{get:e[t],enumerable:!0})};var ke,ft,ee,w,U=q(()=>{ke=()=>{},ft={debug:ke,info:ke,warn:(...o)=>console.warn("[workflow]",...o),error:(...o)=>console.error("[workflow]",...o)},ee={impl:ft},w={debug:(...o)=>ee.impl.debug?.(...o),info:(...o)=>ee.impl.info?.(...o),warn:(...o)=>ee.impl.warn?.(...o),error:(...o)=>ee.impl.error?.(...o)}});var Ze=q(()=>{});var je={};Oe(je,{clearSkills:()=>yt,getAllSkills:()=>wt,getSkill:()=>H,hasSkill:()=>St,listSkillIds:()=>_t,registerSkill:()=>gt});function gt(o){if(!o||typeof o.id!="string")throw new Error("Skill definition must include a string id");F.set(o.id,Object.freeze({...o}))}function H(o){return F.get(o)||null}function St(o){return F.has(o)}function wt(){return new Map(F)}function _t(){return Array.from(F.keys())}function yt(){F.clear()}var he,F,oe=q(()=>{he=Symbol.for("@zibby/agent-workflow.skills");globalThis[he]||(globalThis[he]=new Map);F=globalThis[he]});var K={};Oe(K,{getAgentStrategy:()=>Ue,invokeAgent:()=>Nt,listStrategies:()=>It,registerStrategy:()=>Et});function Et(o){if(!o||typeof o.getName!="function"||typeof o.invoke!="function")throw new Error("strategy must implement getName() and invoke() (AgentStrategy shape)");let e=W.findIndex(t=>t.getName()===o.getName());e>=0?W[e]=o:W.push(o)}function It(){return W.map(o=>o.getName())}function Ue(o={}){let{state:e={},preferredAgent:t=null}=o,r=t||e.agentType||process.env.AGENT_TYPE;if(!r){let i=W.map(a=>a.getName()).join(", ")||"none registered";throw new Error(`No agent specified. Set agentType in state or AGENT_TYPE env var. Available: ${i}`)}w.debug(`[workflow] agent selection: requested=${r}`);let s=W.find(i=>i.getName()===r);if(!s){let i=W.map(a=>a.getName()).join(", ")||"none registered";throw new Error(`Unknown agent '${r}'. Available: ${i}`)}if(!s.canHandle(o))throw new Error(`Agent '${r}' is not available in this environment. Check credentials/environment.`);return w.debug(`[workflow] using agent: ${s.getName()}`),s}async function Nt(o,e={},t={}){let r=Ue(e),s=e.state?.config||t.config||{},i=s.models||{},a=t.nodeName&&i[t.nodeName]||null,n=i.default||null,c=s.agent?.[r.name]?.model||null,d=a||n||c||t.model||null,h={...t,model:d,workspace:e.state?.workspace||t.workspace,schema:t.schema||e.schema,images:t.images||e.images||[],skills:t.skills||e.skills||[],config:s},u=o,l=h.skills||[];if(l.length>0&&!t.skipPromptFragments){let _=l.map(S=>{let $=H(S)?.promptFragment;return typeof $=="function"?$():$}).filter(Boolean);_.length>0&&(u+=`
1
+ var it=Object.defineProperty;var ie=(o=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(o,{get:(e,t)=>(typeof require<"u"?require:e)[t]}):o)(function(o){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+o+'" is not supported')});var V=(o,e)=>()=>(o&&(e=o(o=0)),e);var ke=(o,e)=>{for(var t in e)it(o,t,{get:e[t],enumerable:!0})};var Te,ct,Q,S,G=V(()=>{Te=()=>{},ct={debug:Te,info:Te,warn:(...o)=>console.warn("[workflow]",...o),error:(...o)=>console.error("[workflow]",...o)},Q={impl:ct},S={debug:(...o)=>Q.impl.debug?.(...o),info:(...o)=>Q.impl.info?.(...o),warn:(...o)=>Q.impl.warn?.(...o),error:(...o)=>Q.impl.error?.(...o)}});var Le=V(()=>{});var Fe={};ke(Fe,{clearSkills:()=>mt,getAllSkills:()=>ft,getSkill:()=>U,hasSkill:()=>pt,listSkillIds:()=>ht,registerSkill:()=>dt});function dt(o){if(!o||typeof o.id!="string")throw new Error("Skill definition must include a string id");F.set(o.id,Object.freeze({...o}))}function U(o){return F.get(o)||null}function pt(o){return F.has(o)}function ft(){return new Map(F)}function ht(){return Array.from(F.keys())}function mt(){F.clear()}var de,F,te=V(()=>{de=Symbol.for("@zibby/agent-workflow.skills");globalThis[de]||(globalThis[de]=new Map);F=globalThis[de]});var H={};ke(H,{getAgentStrategy:()=>je,invokeAgent:()=>St,listStrategies:()=>wt,registerStrategy:()=>gt});function gt(o){if(!o||typeof o.getName!="function"||typeof o.invoke!="function")throw new Error("strategy must implement getName() and invoke() (AgentStrategy shape)");let e=R.findIndex(t=>t.getName()===o.getName());e>=0?R[e]=o:R.push(o)}function wt(){return R.map(o=>o.getName())}function je(o={}){let{state:e={},preferredAgent:t=null}=o,r=t||e.agentType||process.env.AGENT_TYPE;if(!r){let i=R.map(a=>a.getName()).join(", ")||"none registered";throw new Error(`No agent specified. Set agentType in state or AGENT_TYPE env var. Available: ${i}`)}S.debug(`[workflow] agent selection: requested=${r}`);let s=R.find(i=>i.getName()===r);if(!s){let i=R.map(a=>a.getName()).join(", ")||"none registered";throw new Error(`Unknown agent '${r}'. Available: ${i}`)}if(!s.canHandle(o))throw new Error(`Agent '${r}' is not available in this environment. Check credentials/environment.`);return S.debug(`[workflow] using agent: ${s.getName()}`),s}async function St(o,e={},t={}){let r=je(e),s=e.state?.config||t.config||{},i=s.models||{},a=t.nodeName&&i[t.nodeName]||null,n=i.default||null,c=s.agent?.[r.name]?.model||null,u=a||n||c||t.model||null,h={...t,model:u,workspace:e.state?.workspace||t.workspace,schema:t.schema||e.schema,images:t.images||e.images||[],skills:t.skills||e.skills||[],config:s},d=o,l=h.skills||[];if(l.length>0&&!t.skipPromptFragments){let _=l.map(w=>{let N=U(w)?.promptFragment;return typeof N=="function"?N():N}).filter(Boolean);_.length>0&&(d+=`
2
2
 
3
3
  ${_.join(`
4
4
 
5
- `)}`)}let m=e.state?._currentNodeConfig?.extraPromptInstructions?.trim();return m&&(u+=`
5
+ `)}`)}let m=e.state?._currentNodeConfig?.extraPromptInstructions?.trim();return m&&(d+=`
6
6
 
7
7
  \u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501
8
8
  PRIORITY OVERRIDE \u2014 THE FOLLOWING INSTRUCTIONS TAKE PRECEDENCE OVER ALL PREVIOUS CONTENT
9
9
  \u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501
10
10
 
11
11
  ${m}
12
- `),w.debug(`[workflow] prompt length: ${u.length} chars`),r.invoke(u,h)}var me,W,z=q(()=>{Ze();U();oe();me=Symbol.for("@zibby/agent-workflow.strategies");globalThis[me]||(globalThis[me]=[]);W=globalThis[me]});var pt=new Set(["__proto__","constructor","prototype"]);function de(o){if(pt.has(o))throw new Error(`Invalid state key: "${o}"`)}var Q=class{constructor(e={}){this._state=Object.create(null),Object.assign(this._state,{messages:[],errors:[],artifacts:{},metadata:{},...e}),this._history=[]}get(e){return this._state[e]}set(e,t){de(e),this._history.push({...this._state}),this._state[e]=t}update(e){let t=Object.getOwnPropertyNames(e);for(let r of t)de(r);this._history.push({...this._state});for(let r of t)this._state[r]=e[r]}append(e,t){de(e),this._history.push({...this._state}),Array.isArray(this._state[e])||(this._state[e]=[]),this._state[e].push(t)}getAll(){return{...this._state}}rollback(){this._history.length>0&&(this._state=this._history.pop())}};var X=class{constructor(e){this.schema=e}parse(e){let t=e.match(/```json\s*([\s\S]*?)\s*```/);if(t)return this.validate(JSON.parse(t[1]));let r=[e.match(/\{[\s\S]*?\}/),e.match(/\{[\s\S]*\}/)].filter(Boolean).map(s=>s[0]);for(let s of r)try{return this.validate(JSON.parse(s))}catch(i){if(!(i instanceof SyntaxError))throw i}return this.validate({result:e.trim()})}validate(e){let t=[];for(let[r,s]of Object.entries(this.schema)){if(s.required&&!(r in e)&&t.push(`Missing required field: ${r}`),r in e&&s.type){let i=typeof e[r];i!==s.type&&t.push(`Field '${r}' expected ${s.type}, got ${i}`)}if(s.validate&&r in e){let i=s.validate(e[r]);i&&t.push(`Field '${r}': ${i}`)}}if(t.length>0)throw new Error(`Output validation failed:
12
+ `),S.debug(`[workflow] prompt length: ${d.length} chars`),r.invoke(d,h)}var pe,R,J=V(()=>{Le();G();te();pe=Symbol.for("@zibby/agent-workflow.strategies");globalThis[pe]||(globalThis[pe]=[]);R=globalThis[pe]});var at=new Set(["__proto__","constructor","prototype"]);function ae(o){if(at.has(o))throw new Error(`Invalid state key: "${o}"`)}var q=class{constructor(e={}){this._state=Object.create(null),Object.assign(this._state,{messages:[],errors:[],artifacts:{},metadata:{},...e}),this._history=[]}get(e){return this._state[e]}set(e,t){ae(e),this._history.push({...this._state}),this._state[e]=t}update(e){let t=Object.getOwnPropertyNames(e);for(let r of t)ae(r);this._history.push({...this._state});for(let r of t)this._state[r]=e[r]}append(e,t){ae(e),this._history.push({...this._state}),Array.isArray(this._state[e])||(this._state[e]=[]),this._state[e].push(t)}getAll(){return{...this._state}}rollback(){this._history.length>0&&(this._state=this._history.pop())}};var X=class{constructor(e){this.schema=e}parse(e){let t=e.match(/```json\s*([\s\S]*?)\s*```/);if(t)return this.validate(JSON.parse(t[1]));let r=[e.match(/\{[\s\S]*?\}/),e.match(/\{[\s\S]*\}/)].filter(Boolean).map(s=>s[0]);for(let s of r)try{return this.validate(JSON.parse(s))}catch(i){if(!(i instanceof SyntaxError))throw i}return this.validate({result:e.trim()})}validate(e){let t=[];for(let[r,s]of Object.entries(this.schema)){if(s.required&&!(r in e)&&t.push(`Missing required field: ${r}`),r in e&&s.type){let i=typeof e[r];i!==s.type&&t.push(`Field '${r}' expected ${s.type}, got ${i}`)}if(s.validate&&r in e){let i=s.validate(e[r]);i&&t.push(`Field '${r}': ${i}`)}}if(t.length>0)throw new Error(`Output validation failed:
13
13
  ${t.join(`
14
- `)}`);return e}};U();import{writeFileSync as ge,readFileSync as Ge,existsSync as He,mkdirSync as vt}from"node:fs";import{join as Se,dirname as $t}from"node:path";import I from"chalk";var ht="__WORKFLOW_GRAPH_LOG__",G=I.gray("\u2502"),mt=I.gray("\u250C"),xe=I.gray("\u2514"),ue=I.green("\u25C6"),Ce=I.hex("#c084fc")("\u25C6"),Pe=I.hex("#2dd4bf")("\u25C6"),pe=I.red("\u25C6"),Re=`${G} `,Be=2;function We(o){return o<1e3?`${o}ms`:`${(o/1e3).toFixed(1)}s`}function Me(o,e){return(t,r,s)=>{if(typeof t!="string")return o(t,r,s);let i=process.stdout.columns||120,a="";for(let n=0;n<t.length;n++){let c=t[n];e.lineStart&&(a+=Re,e.col=Be,e.lineStart=!1),c===`
14
+ `)}`);return e}};G();import{writeFileSync as fe,readFileSync as Ye,existsSync as Ge,mkdirSync as _t}from"node:fs";import{join as he,dirname as yt}from"node:path";import E from"chalk";var lt="__WORKFLOW_GRAPH_LOG__",Z=E.gray("\u2502"),ut=E.gray("\u250C"),xe=E.gray("\u2514"),ce=E.green("\u25C6"),Ae=E.hex("#c084fc")("\u25C6"),Oe=E.hex("#2dd4bf")("\u25C6"),le=E.red("\u25C6"),Ce=`${Z} `,Pe=2;function Re(o){return o<1e3?`${o}ms`:`${(o/1e3).toFixed(1)}s`}function We(o,e){return(t,r,s)=>{if(typeof t!="string")return o(t,r,s);let i=process.stdout.columns||120,a="";for(let n=0;n<t.length;n++){let c=t[n];e.lineStart&&(a+=Ce,e.col=Pe,e.lineStart=!1),c===`
15
15
  `?(a+=c,e.lineStart=!0,e.col=0,e.inEsc=!1):c==="\x1B"?(e.inEsc=!0,a+=c):e.inEsc?(a+=c,(c>="A"&&c<="Z"||c>="a"&&c<="z")&&(e.inEsc=!1)):(e.col++,a+=c,e.col>=i&&(a+=`
16
- ${Re}`,e.col=Be))}return o(a,r,s)}}var fe=class{constructor(){this._currentNode=null,this._origStdoutWrite=null,this._origStderrWrite=null;let e=String(process.env.ZIBBY_EMIT_GRAPH_MARKERS||"").trim()==="1"||String(process.env.ZIBBY_WORKFLOW_GRAPH_LOG_MARKERS||"").trim()==="1",t=!e&&String(process.env.ZIBBY_RUN_SOURCE||"").trim().toLowerCase()==="studio";t&&process.env.ZIBBY_NO_DEPRECATION_WARNINGS!=="1"&&console.warn("[zibby/agent-workflow] `ZIBBY_RUN_SOURCE=studio` env var is deprecated for graph-marker emission. Set `ZIBBY_EMIT_GRAPH_MARKERS=1` instead. The Studio-specific value will be ignored in v2. Suppress with ZIBBY_NO_DEPRECATION_WARNINGS=1."),this._emitWorkflowGraphMarkers=e||t}get isInsideNode(){return this._currentNode!==null}_startIntercepting(){this._origStdoutWrite=process.stdout.write.bind(process.stdout),this._origStderrWrite=process.stderr.write.bind(process.stderr);let e={lineStart:!0,col:0,inEsc:!1},t={lineStart:!0,col:0,inEsc:!1};this._outState=e,this._errState=t,process.stdout.write=Me(this._origStdoutWrite,e),process.stderr.write=Me(this._origStderrWrite,t)}_stopIntercepting(){this._origStdoutWrite&&(this._outState&&!this._outState.lineStart&&this._origStdoutWrite(`
16
+ ${Ce}`,e.col=Pe))}return o(a,r,s)}}var ue=class{constructor(){this._currentNode=null,this._origStdoutWrite=null,this._origStderrWrite=null,this._emitWorkflowGraphMarkers=String(process.env.ZIBBY_EMIT_GRAPH_MARKERS||"").trim()==="1"||String(process.env.ZIBBY_WORKFLOW_GRAPH_LOG_MARKERS||"").trim()==="1"}get isInsideNode(){return this._currentNode!==null}_startIntercepting(){this._origStdoutWrite=process.stdout.write.bind(process.stdout),this._origStderrWrite=process.stderr.write.bind(process.stderr);let e={lineStart:!0,col:0,inEsc:!1},t={lineStart:!0,col:0,inEsc:!1};this._outState=e,this._errState=t,process.stdout.write=We(this._origStdoutWrite,e),process.stderr.write=We(this._origStderrWrite,t)}_stopIntercepting(){this._origStdoutWrite&&(this._outState&&!this._outState.lineStart&&this._origStdoutWrite(`
17
17
  `),process.stdout.write=this._origStdoutWrite),this._origStderrWrite&&(this._errState&&!this._errState.lineStart&&this._origStderrWrite(`
18
18
  `),process.stderr.write=this._origStderrWrite),this._origStdoutWrite=null,this._origStderrWrite=null}_rawWrite(e){(this._origStdoutWrite||process.stdout.write.bind(process.stdout))(`${e}
19
- `)}_emitGraphLogMarker(e){if(!this._emitWorkflowGraphMarkers)return;let t=`${ht}${JSON.stringify(e)}
19
+ `)}_emitGraphLogMarker(e){if(!this._emitWorkflowGraphMarkers)return;let t=`${lt}${JSON.stringify(e)}
20
20
  `;this._origStdoutWrite?this._origStdoutWrite(t):process.stdout.write(t)}_writeDot(e,t){this._origStdoutWrite?(this._outState&&!this._outState.lineStart&&(this._origStdoutWrite(`
21
21
  `),this._outState.lineStart=!0,this._outState.col=0),this._origStdoutWrite(`${e} ${t}
22
22
  `)):process.stdout.write.bind(process.stdout)(`${e} ${t}
23
- `)}step(e){this._origStdoutWrite?this._writeDot(ue,e):process.stdout.write.bind(process.stdout)(`${G} ${ue} ${e}
24
- `)}stepInfo(e){this.step(e)}stepTool(e){this._origStdoutWrite?this._writeDot(Ce,e):process.stdout.write.bind(process.stdout)(`${G} ${Ce} ${e}
25
- `)}stepMemory(e){let t=I.hex("#2dd4bf")(e);this._origStdoutWrite?this._writeDot(Pe,t):process.stdout.write.bind(process.stdout)(`${G} ${Pe} ${t}
26
- `)}stepFail(e){this._origStdoutWrite?this._writeDot(pe,I.red(e)):process.stdout.write.bind(process.stdout)(`${G} ${pe} ${I.red(e)}
27
- `)}nodeStart(e){this._currentNode=e,this._emitGraphLogMarker({phase:"node_begin",node:e}),this._rawWrite(`${mt} ${e}`),this._startIntercepting()}nodeComplete(e,t={}){this._stopIntercepting();let{duration:r,details:s}=t;if(s)for(let a of s)this._rawWrite(`${ue} ${a}`);let i=r?I.dim(` ${We(r)}`):"";this._rawWrite(`${xe} ${I.green("done")}${i}`),this._emitGraphLogMarker({phase:"node_end",node:e}),this._rawWrite("")}nodeFailed(e,t,r={}){this._stopIntercepting();let{duration:s}=r,i=s?I.dim(` ${We(s)}`):"";this._rawWrite(`${pe} ${I.red(t)}`),this._rawWrite(`${xe} ${I.red("failed")}${i}`),this._emitGraphLogMarker({phase:"node_end",node:e}),this._rawWrite("")}route(e,t){this._rawWrite(I.dim(` ${e} \u2192 ${t}`)),this._rawWrite("")}graphComplete(){this._rawWrite(I.green.bold("\u2713 Workflow completed"))}},N=new fe;var te=".zibby/output",De="sessions",L=".session-info.json",Le=".zibby-stop",Fe=".zibby-studio-stop";var Ye=["CI_JOB_ID","GITHUB_RUN_ID","CIRCLE_WORKFLOW_ID","BUILD_ID"];var Y=class{constructor(e){if(this.config=e,this.name=e.name,this.prompt=e.prompt,this.outputSchema=e.outputSchema,!this.outputSchema&&!e._isCustomCode)throw new Error(`Node '${this.name}' must define outputSchema (Zod schema). This defines the contract for what the node returns to state.`);this.isZodSchema=this.outputSchema&&typeof this.outputSchema._def<"u",this.parser=e.outputSchema&&!this.isZodSchema?new X(e.outputSchema):null,this.retries=e.retries||0,this.onComplete=e.onComplete,this.customExecute=e.execute}async execute(e,t){let r=()=>t&&typeof t.getAll=="function"?t.getAll():e,s=u=>t&&typeof t.get=="function"?t.get(u):e?.[u];if(typeof this.customExecute=="function"){w.debug(`[workflow] node '${this.name}': custom execute (skipping LLM)`);try{let u=await this.customExecute(e);return typeof u=="object"&&u!==null&&u.success===!1?{success:!1,error:u.error||"Node execution failed",raw:u.raw||null}:this.isZodSchema?(w.debug(`[workflow] node '${this.name}': validating output schema`),{success:!0,output:this.outputSchema.parse(u),raw:null}):{success:!0,output:u,raw:null}}catch(u){return w.error(`[workflow] node '${this.name}' failed: ${u.message}`),u.name==="ZodError"&&w.error(`Schema errors: ${JSON.stringify(u.issues||u.errors,null,2)}`),{success:!1,error:u.message,raw:null}}}let i=typeof this.prompt=="function"?this.prompt(r()):this.prompt,a=s("_skillHints");a&&(i=`${a}
23
+ `)}step(e){this._origStdoutWrite?this._writeDot(ce,e):process.stdout.write.bind(process.stdout)(`${Z} ${ce} ${e}
24
+ `)}stepInfo(e){this.step(e)}stepTool(e){this._origStdoutWrite?this._writeDot(Ae,e):process.stdout.write.bind(process.stdout)(`${Z} ${Ae} ${e}
25
+ `)}stepMemory(e){let t=E.hex("#2dd4bf")(e);this._origStdoutWrite?this._writeDot(Oe,t):process.stdout.write.bind(process.stdout)(`${Z} ${Oe} ${t}
26
+ `)}stepFail(e){this._origStdoutWrite?this._writeDot(le,E.red(e)):process.stdout.write.bind(process.stdout)(`${Z} ${le} ${E.red(e)}
27
+ `)}nodeStart(e){this._currentNode=e,this._emitGraphLogMarker({phase:"node_begin",node:e}),this._rawWrite(`${ut} ${e}`),this._startIntercepting()}nodeComplete(e,t={}){this._stopIntercepting();let{duration:r,details:s}=t;if(s)for(let a of s)this._rawWrite(`${ce} ${a}`);let i=r?E.dim(` ${Re(r)}`):"";this._rawWrite(`${xe} ${E.green("done")}${i}`),this._emitGraphLogMarker({phase:"node_end",node:e}),this._rawWrite("")}nodeFailed(e,t,r={}){this._stopIntercepting();let{duration:s}=r,i=s?E.dim(` ${Re(s)}`):"";this._rawWrite(`${le} ${E.red(t)}`),this._rawWrite(`${xe} ${E.red("failed")}${i}`),this._emitGraphLogMarker({phase:"node_end",node:e}),this._rawWrite("")}route(e,t){this._rawWrite(E.dim(` ${e} \u2192 ${t}`)),this._rawWrite("")}graphComplete(){this._rawWrite(E.green.bold("\u2713 Workflow completed"))}},v=new ue;var ee=".zibby/output",Me="sessions",L=".session-info.json",Be=".zibby-stop";var De=["CI_JOB_ID","GITHUB_RUN_ID","CIRCLE_WORKFLOW_ID","BUILD_ID"];var j=class{constructor(e){if(this.config=e,this.name=e.name,this.prompt=e.prompt,this.outputSchema=e.outputSchema,!this.outputSchema&&!e._isCustomCode)throw new Error(`Node '${this.name}' must define outputSchema (Zod schema). This defines the contract for what the node returns to state.`);this.isZodSchema=this.outputSchema&&typeof this.outputSchema._def<"u",this.parser=e.outputSchema&&!this.isZodSchema?new X(e.outputSchema):null,this.retries=e.retries||0,this.onComplete=e.onComplete,this.customExecute=e.execute}async execute(e,t){let r=()=>t&&typeof t.getAll=="function"?t.getAll():e,s=d=>t&&typeof t.get=="function"?t.get(d):e?.[d];if(typeof this.customExecute=="function"){S.debug(`[workflow] node '${this.name}': custom execute (skipping LLM)`);try{let d=await this.customExecute(e);return typeof d=="object"&&d!==null&&d.success===!1?{success:!1,error:d.error||"Node execution failed",raw:d.raw||null}:this.isZodSchema?(S.debug(`[workflow] node '${this.name}': validating output schema`),{success:!0,output:this.outputSchema.parse(d),raw:null}):{success:!0,output:d,raw:null}}catch(d){return S.error(`[workflow] node '${this.name}' failed: ${d.message}`),d.name==="ZodError"&&S.error(`Schema errors: ${JSON.stringify(d.issues||d.errors,null,2)}`),{success:!1,error:d.message,raw:null}}}let i=typeof this.prompt=="function"?this.prompt(r()):this.prompt,a=s("_skillHints");a&&(i=`${a}
28
28
 
29
- ${i}`);let n=r(),c=n.cwd||process.cwd(),d=n.sessionPath;try{if(d){let u=Se(d,L);if(He(u)){let m=JSON.parse(Ge(u,"utf-8"));m.currentNode=this.name,ge(u,JSON.stringify(m,null,2),"utf-8")}let l=Se(d,"..",L);if(He(l))try{let m=JSON.parse(Ge(l,"utf-8"));m.currentNode=this.name,ge(l,JSON.stringify(m,null,2),"utf-8")}catch{}}}catch(u){w.debug(`[workflow] could not update session info: ${u.message}`)}let h=null;for(let u=0;u<=this.retries;u++)try{w.debug(`[workflow] node '${this.name}' attempt ${u}`);let l=r().config||{},m=l.agents||{},_=this.config.agent??m[this.name]??null,S={state:r()};_&&(S.preferredAgent=_);let $={workspace:c,schema:this.isZodSchema?this.outputSchema:null,skills:this.config.skills||[],sessionPath:d,config:l,nodeName:this.name,timeout:this.config?.timeout||3e5},f=e?._coreInvokeAgent;f||(f=(await Promise.resolve().then(()=>(z(),K))).invokeAgent);let v=await f(i,S,$),y,A;if(typeof v=="string"?(y=v,A=null):v.structured?(y=v.raw||JSON.stringify(v.structured,null,2),A=v.structured):(y=v.raw||JSON.stringify(v,null,2),A=v.extracted||null),d)try{let p=Se(d,this.name,"raw_stream_output.txt");vt($t(p),{recursive:!0}),ge(p,typeof y=="string"?y:JSON.stringify(y),"utf-8")}catch(p){w.debug(`[workflow] could not save raw output: ${p.message}`)}if(this.isZodSchema&&A){w.info(`[workflow] node '${this.name}': output validated: ${JSON.stringify(A,null,2)}`);let p=A;if(typeof this.onComplete=="function")try{p=await this.onComplete(r(),A)}catch(R){w.warn(`[workflow] onComplete hook failed: ${R.message}`)}return{success:!0,output:p,raw:y}}if(typeof this.onComplete=="function")try{return{success:!0,output:await this.onComplete(r(),{raw:y}),raw:y}}catch(p){throw new Error(`onComplete failed: ${p.message}`,{cause:p})}if(this.parser){let p=this.parser.parse(y);return w.info(`[workflow] node '${this.name}': parsed output: ${JSON.stringify(p,null,2)}`),N.step("Output parsed"),{success:!0,output:p,raw:y}}return{success:!0,output:y,raw:y}}catch(l){h=l,u<this.retries&&w.info(`[workflow] node '${this.name}' failed, retrying (${u+1}/${this.retries})\u2026`)}return{success:!1,error:h.message,raw:null}}},re=class extends Y{constructor(e){super({...e,_isCustomCode:!0}),this.condition=e.condition}async execute(e,t){let r=t&&typeof t.getAll=="function"?t.getAll():e;return{success:!0,output:{nextNode:this.condition(r)},raw:null}}};import{existsSync as Ke,readFileSync as bt}from"node:fs";import{join as we,dirname as ze}from"node:path";var se=class{static async loadContext(e,t,r={}){let s={},i=r.filenames||["CONTEXT.md","AGENTS.md"];if(e){let n=ze(we(t,e));for(let c of i){let d=await this.findAndMergeContextFiles(c,n,t);if(d){let h=c.replace(/\.[^.]+$/,"").toLowerCase();s[h]=d}}}let a=r.discovery||{};for(let[n,c]of Object.entries(a))try{let d=we(t,c);Ke(d)&&(s[n]=await this.loadFile(d))}catch(d){console.warn(`[workflow] could not load context '${n}' from '${c}': ${d.message}`)}return s}static async findAndMergeContextFiles(e,t,r){let s=[],i=t;for(;i.startsWith(r);){let a=we(i,e);if(Ke(a))try{s.unshift(await this.loadFile(a))}catch(c){console.warn(`[workflow] could not load ${e} from ${a}: ${c.message}`)}let n=ze(i);if(n===i)break;i=n}return s.length===0?null:s.every(a=>typeof a=="string")?s.join(`
29
+ ${i}`);let n=r(),c=n.cwd||process.cwd(),u=n.sessionPath;try{if(u){let d=he(u,L);if(Ge(d)){let m=JSON.parse(Ye(d,"utf-8"));m.currentNode=this.name,fe(d,JSON.stringify(m,null,2),"utf-8")}let l=he(u,"..",L);if(Ge(l))try{let m=JSON.parse(Ye(l,"utf-8"));m.currentNode=this.name,fe(l,JSON.stringify(m,null,2),"utf-8")}catch{}}}catch(d){S.debug(`[workflow] could not update session info: ${d.message}`)}let h=null;for(let d=0;d<=this.retries;d++)try{S.debug(`[workflow] node '${this.name}' attempt ${d}`);let l=r().config||{},m=l.agents||{},_=this.config.agent??m[this.name]??null,w={state:r()};_&&(w.preferredAgent=_);let N={workspace:c,schema:this.isZodSchema?this.outputSchema:null,skills:this.config.skills||[],sessionPath:u,config:l,nodeName:this.name,timeout:this.config?.timeout||3e5},f=e?._coreInvokeAgent;f||(f=(await Promise.resolve().then(()=>(J(),H))).invokeAgent);let $=await f(i,w,N),y,x;if(typeof $=="string"?(y=$,x=null):$.structured?(y=$.raw||JSON.stringify($.structured,null,2),x=$.structured):(y=$.raw||JSON.stringify($,null,2),x=$.extracted||null),u)try{let p=he(u,this.name,"raw_stream_output.txt");_t(yt(p),{recursive:!0}),fe(p,typeof y=="string"?y:JSON.stringify(y),"utf-8")}catch(p){S.debug(`[workflow] could not save raw output: ${p.message}`)}if(this.isZodSchema&&x){S.info(`[workflow] node '${this.name}': output validated: ${JSON.stringify(x,null,2)}`);let p=x;if(typeof this.onComplete=="function")try{p=await this.onComplete(r(),x)}catch(M){S.warn(`[workflow] onComplete hook failed: ${M.message}`)}return{success:!0,output:p,raw:y}}if(typeof this.onComplete=="function")try{return{success:!0,output:await this.onComplete(r(),{raw:y}),raw:y}}catch(p){throw new Error(`onComplete failed: ${p.message}`,{cause:p})}if(this.parser){let p=this.parser.parse(y);return S.info(`[workflow] node '${this.name}': parsed output: ${JSON.stringify(p,null,2)}`),v.step("Output parsed"),{success:!0,output:p,raw:y}}return{success:!0,output:y,raw:y}}catch(l){h=l,d<this.retries&&S.info(`[workflow] node '${this.name}' failed, retrying (${d+1}/${this.retries})\u2026`)}return{success:!1,error:h.message,raw:null}}},oe=class extends j{constructor(e){super({...e,_isCustomCode:!0}),this.condition=e.condition}async execute(e,t){let r=t&&typeof t.getAll=="function"?t.getAll():e;return{success:!0,output:{nextNode:this.condition(r)},raw:null}}};import{existsSync as Ze,readFileSync as Et}from"node:fs";import{join as me,dirname as Ue}from"node:path";var re=class{static async loadContext(e,t,r={}){let s={},i=r.filenames||["CONTEXT.md","AGENTS.md"];if(e){let n=Ue(me(t,e));for(let c of i){let u=await this.findAndMergeContextFiles(c,n,t);if(u){let h=c.replace(/\.[^.]+$/,"").toLowerCase();s[h]=u}}}let a=r.discovery||{};for(let[n,c]of Object.entries(a))try{let u=me(t,c);Ze(u)&&(s[n]=await this.loadFile(u))}catch(u){console.warn(`[workflow] could not load context '${n}' from '${c}': ${u.message}`)}return s}static async findAndMergeContextFiles(e,t,r){let s=[],i=t;for(;i.startsWith(r);){let a=me(i,e);if(Ze(a))try{s.unshift(await this.loadFile(a))}catch(c){console.warn(`[workflow] could not load ${e} from ${a}: ${c.message}`)}let n=Ue(i);if(n===i)break;i=n}return s.length===0?null:s.every(a=>typeof a=="string")?s.join(`
30
30
 
31
31
  ---
32
32
 
33
- `):s.every(a=>typeof a=="object")?Object.assign({},...s):s[s.length-1]}static async loadFile(e){let t=bt(e,"utf-8");if(e.endsWith(".json"))return JSON.parse(t);if(e.endsWith(".js")||e.endsWith(".mjs")){let{pathToFileURL:r}=await import("url"),s=await import(r(e).href);return s.default||s}return t}};import{mkdirSync as Xe,existsSync as ne,writeFileSync as Je,unlinkSync as Ve}from"node:fs";import{join as P,resolve as et}from"node:path";import{config as Tt}from"dotenv";import{zodToJsonSchema as qe}from"zod-to-json-schema";import At from"handlebars";var Qe=new Set;function ie(o,e){Qe.has(o)||(Qe.add(o),process.env.ZIBBY_NO_DEPRECATION_WARNINGS!=="1"&&console.warn(`[zibby/agent-workflow] ${e}`))}function Ot({traceFrom:o,sessionId:e,sessionPath:t,idSource:r,mkdirFresh:s}){if(!(process.env.ZIBBY_SESSION_LOG==="1"||process.env.ZIBBY_SESSION_LOG==="true"))return;let a=typeof process.ppid=="number"?process.ppid:"n/a",n=`[zibby:session] from=${o} pid=${process.pid} ppid=${a} sessionId=${e} source=${r} mkdir=${s?"yes":"no"} path=${t}`;if(console.log(n),process.env.ZIBBY_TRACE_SESSION==="1"||process.env.ZIBBY_TRACE_SESSION==="true"){let h=(new Error("session trace").stack||"").split(`
33
+ `):s.every(a=>typeof a=="object")?Object.assign({},...s):s[s.length-1]}static async loadFile(e){let t=Et(e,"utf-8");if(e.endsWith(".json"))return JSON.parse(t);if(e.endsWith(".js")||e.endsWith(".mjs")){let{pathToFileURL:r}=await import("url"),s=await import(r(e).href);return s.default||s}return t}};import{mkdirSync as Ke,existsSync as ge,writeFileSync as He,unlinkSync as $t}from"node:fs";import{join as W,resolve as ze}from"node:path";import{config as It}from"dotenv";import{zodToJsonSchema as Je}from"zod-to-json-schema";import vt from"handlebars";function Nt({traceFrom:o,sessionId:e,sessionPath:t,idSource:r,mkdirFresh:s}){if(!(process.env.ZIBBY_SESSION_LOG==="1"||process.env.ZIBBY_SESSION_LOG==="true"))return;let a=typeof process.ppid=="number"?process.ppid:"n/a",n=`[zibby:session] from=${o} pid=${process.pid} ppid=${a} sessionId=${e} source=${r} mkdir=${s?"yes":"no"} path=${t}`;if(console.log(n),process.env.ZIBBY_TRACE_SESSION==="1"||process.env.ZIBBY_TRACE_SESSION==="true"){let h=(new Error("session trace").stack||"").split(`
34
34
  `).slice(2,14).join(`
35
35
  `);console.log(`[zibby:session] stack (${o}):
36
- ${h}`)}}function kt(){return process.env.ZIBBY_TRUST_SESSION_ENV==="1"||process.env.ZIBBY_TRUST_SESSION_ENV==="true"||process.env.ZIBBY_KEEP_SESSION_ENV==="1"||process.env.ZIBBY_KEEP_SESSION_ENV==="true"?!0:process.env.ZIBBY_RUN_SOURCE==="studio"?(ie("legacy-zibby-run-source","`ZIBBY_RUN_SOURCE=studio` env var is deprecated. Set `ZIBBY_TRUST_SESSION_ENV=1` (and `ZIBBY_PIN_SESSION_PATH=1` / `ZIBBY_EMIT_GRAPH_MARKERS=1` if you need those gates) instead. The Studio-specific value will be ignored in v2. Suppress with ZIBBY_NO_DEPRECATION_WARNINGS=1."),!0):!1}function xt(){let o=process.env.ZIBBY_PIN_SESSION_PATH==="1"||process.env.ZIBBY_PIN_SESSION_PATH==="true",e=!o&&process.env.ZIBBY_RUN_SOURCE==="studio";if(e&&ie("legacy-zibby-run-source","`ZIBBY_RUN_SOURCE=studio` env var is deprecated. Set `ZIBBY_PIN_SESSION_PATH=1` (and `ZIBBY_TRUST_SESSION_ENV=1` / `ZIBBY_EMIT_GRAPH_MARKERS=1` if you need those gates) instead. The Studio-specific value will be ignored in v2. Suppress with ZIBBY_NO_DEPRECATION_WARNINGS=1."),!o&&!e)return;let t=process.env.ZIBBY_SESSION_PATH;if(!(t==null||String(t).trim()===""))try{return et(String(t).trim())}catch{return String(t).trim()}}var Ct=xt;function Pt(){kt()||(delete process.env.ZIBBY_SESSION_PATH,delete process.env.ZIBBY_SESSION_ID)}function Rt({sessionPath:o,sessionId:e}){o&&typeof o=="string"&&(process.env.ZIBBY_SESSION_PATH=o),e!=null&&String(e).trim()!==""&&(process.env.ZIBBY_SESSION_ID=String(e).trim())}function Bt(o={}){let e=Ye.map(i=>process.env[i]).find(Boolean),t=Math.random().toString(36).slice(2,6),r=e||`${Date.now()}_${t}`,s=o.paths?.sessionPrefix;return s?`${s}_${r}`:r}function Wt({cwd:o=process.cwd(),config:e={},initialState:t={},traceFrom:r="resolveWorkflowSession"}={}){let s=t.sessionPath,i=t.sessionTimestamp,a="initialState.sessionPath";if(!s&&process.env.ZIBBY_SESSION_PATH)try{let d=et(String(process.env.ZIBBY_SESSION_PATH));d&&(s=d,a="ZIBBY_SESSION_PATH")}catch{}let n;if(s)n=String(s).split(/[/\\]/).filter(Boolean).pop(),i==null&&(i=Date.now());else{let d=process.env.ZIBBY_SESSION_ID&&String(process.env.ZIBBY_SESSION_ID).trim();if(d)n=d,a="ZIBBY_SESSION_ID";else{let u=e.sessionId!=null?String(e.sessionId).trim():"";u&&u!=="last"?(n=u,a="config.sessionId"):(n=Bt(e),a="generated")}i=i??Date.now();let h=e.paths?.output||te;s=P(o,h,De,n)}let c=!ne(s);return c&&Xe(s,{recursive:!0}),(c||a!=="initialState.sessionPath")&&Ot({traceFrom:r,sessionId:n,sessionPath:s,idSource:a,mkdirFresh:c}),Rt({sessionPath:s,sessionId:n}),{sessionPath:s,sessionId:n,sessionTimestamp:i}}var ae=class{constructor(e={}){this.nodes=new Map,this.edges=new Map,this.entryPoint=null,this.middleware=Array.isArray(e.middleware)?[...e.middleware]:[],e.nodeMiddleware&&this.middleware.push(e.nodeMiddleware),this.nodeTypeMap=new Map,this.conditionalCodeMap=new Map,this.stateSchema=e.stateSchema||null,this.nodePrompts=new Map,this.nodeOptions=new Map,this._invokeAgent=e.invokeAgent||null,this._compiledPrompts=new Map}setStateSchema(e){return this.stateSchema=e,this}getStateSchema(){return this.stateSchema}addNode(e,t,r={}){let s=t instanceof Y?t:new Y(t);return s.name=e,this.nodes.set(e,s),r.prompt&&this.nodePrompts.set(e,r.prompt),Object.keys(r).length>0&&this.nodeOptions.set(e,r),this}addConditionalNode(e,t){return this.nodes.set(e,new re({...t,name:e})),this}addEdge(e,t){return this.edges.set(e,t),this}setNodeType(e,t){return this.nodeTypeMap.set(e,t),this}addConditionalEdges(e,t,{labels:r}={}){return this.edges.set(e,{conditional:!0,routes:t,labels:r}),typeof t=="function"&&this.conditionalCodeMap.set(e,t.toString()),this}setEntryPoint(e){return this.entryPoint=e,this}use(e){return typeof e=="function"&&this.middleware.push(e),this}_composeMiddleware(e,t,r,s,i){let a=r;for(let n=e.length-1;n>=0;n--){let c=e[n],d=a;a=()=>c(t,d,s,i)}return a()}serialize(){let e=[],t={};for(let[i,a]of this.nodes){let n=this.nodeTypeMap.get(i)||i;e.push({id:i,type:n,data:{nodeType:n,label:i}});let c={};a._isCustomCode&&typeof a.execute=="function"&&(c.customCode=a.execute.toString());let d=this.nodePrompts.get(i);if(d&&(c.prompt=d),typeof a.customExecute=="function"&&(c.executeCode=a.customExecute.toString()),a.outputSchema)try{if(typeof a.outputSchema._def<"u"){let u=qe(a.outputSchema,{target:"openApi3"});c.outputSchema={jsonSchema:u,variables:this._flattenJsonSchemaToVariables(u)}}else c.outputSchema={schema:a.outputSchema}}catch(u){console.warn(`[workflow] failed to convert schema for ${i}:`,u.message)}let h=(this.resolvedToolsMap||{})[i];h?.toolIds&&(c.tools=h.toolIds),Object.keys(c).length>0&&(t[i]=c)}let r=[];for(let[i,a]of this.edges)if(typeof a=="string")r.push({source:i,target:a});else if(a.conditional){let n=this.conditionalCodeMap.get(i)||a.routes.toString(),c=this._inferConditionalTargets(a.routes),d=a.labels||{};for(let h of c){let u={source:i,target:h,data:{conditionalCode:n}};d[h]&&(u.label=d[h]),r.push(u)}}let s=null;if(this.stateSchema)try{s=qe(this.stateSchema,{target:"openApi3"})}catch{s=this.stateSchema}return{nodes:e,edges:r,nodeConfigs:t,stateSchema:s}}_inferConditionalTargets(e){let t=e.toString(),r=new Set,s=/return\s+['"]([^'"]+)['"]/g,i;for(;(i=s.exec(t))!==null;)r.add(i[1]);return[...r]}_flattenJsonSchemaToVariables(e,t=""){let r=e;if(e.$ref&&e.definitions){let s=e.$ref.replace("#/definitions/","");r=e.definitions[s]||e}return this._flattenSchema(r,t)}_flattenSchema(e,t=""){if(!e||typeof e!="object")return[];let r=[],s=e.properties||{},i=e.required||[];for(let[a,n]of Object.entries(s)){let c=t?`${t}.${a}`:a;r.push({path:c,type:n.type||"unknown",label:n.description||this._formatLabel(a),optional:!i.includes(a)}),n.type==="object"&&n.properties&&r.push(...this._flattenSchema(n,c)),n.type==="array"&&n.items?.type==="object"&&n.items.properties&&r.push(...this._flattenSchema(n.items,`${c}[]`))}return r}_formatLabel(e){return e.replace(/([A-Z])/g," $1").replace(/^./,t=>t.toUpperCase()).trim()}_summarizeNodeOutput(e,t){if(!t||typeof t!="object")return[];let r=[];t.success!==void 0&&r.push(`Result: ${t.success?"passed":"failed"}`);for(let[s,i]of Object.entries(t))if(!(s==="success"||s==="raw"||s==="nextNode")){if(typeof i=="string"&&i.length<=80)r.push(`${s}: ${i}`);else if(Array.isArray(i)){let a=i.length,n=i.filter(d=>d?.passed===!0).length,c=i.some(d=>d?.passed!==void 0);r.push(c?`${s}: ${n}/${a} passed${a-n?`, ${a-n} failed`:""}`:`${s}: ${a} items`)}if(r.length>=4)break}return r}async run(e,t={},r={}){if(!this.entryPoint)throw new Error("No entry point set for graph");let s=new AbortController;r.signal&&(r.signal.aborted?s.abort():r.signal.addEventListener("abort",()=>s.abort(),{once:!0}));let i=r.strategyAbortTimeoutMs??t.config?.strategyAbortTimeoutMs??5e3,a=t.cwd||process.cwd();Tt({path:P(a,".env")});let n=t.config||{};if(!n||Object.keys(n).length===0)try{let g=P(a,".zibby.config.js");ne(g)&&(n=(await import(g)).default||{})}catch{}process.env.EXECUTION_ID&&!n.agent?.strictMode&&(n.agent={...n.agent,strictMode:!0});let c=t.agentType;if(!c){let g=n?.agent;g?.provider?c=g.provider:g?.gemini?c="gemini":g?.claude?c="claude":g?.cursor?c="cursor":g?.codex?c="codex":c=process.env.AGENT_TYPE||"cursor"}let d=t.contextConfig||e?.config?.contextConfig||e?.config?.context||n?.context||{};if(this.stateSchema){let g=this.stateSchema.safeParse(t);if(!g.success){let T=g.error.issues.map(O=>`${O.path.join(".")}: ${O.message}`);throw console.error("\u274C Initial state validation failed:"),T.forEach(O=>console.error(` - ${O}`)),new Error(`State validation failed: ${T.join(", ")}`)}N.step("State validated against schema")}let h=Ct(),u=t.sessionPath||h;u||Pt();let{sessionPath:l,sessionTimestamp:m,sessionId:_}=Wt({cwd:a,config:n,traceFrom:"WorkflowGraph.run",initialState:{sessionPath:u,sessionTimestamp:t.sessionTimestamp}});N.step(`Session ${_}`);let S=await se.loadContext(t.specPath||"",a,d);Object.keys(S).length>0&&N.step(`Context loaded: ${Object.keys(S).join(", ")}`);let $=t.outputPath;!$&&t.specPath&&(e?.calculateOutputPath?$=e.calculateOutputPath(t.specPath):console.warn(`\u26A0\uFE0F outputPath not resolved (specPath=${t.specPath})`));let f=new Q({...t,config:n,agentType:c,outputPath:$,sessionPath:l,sessionTimestamp:m,context:S,resolvedTools:this.resolvedToolsMap||{},_signal:s.signal}),v=new Map;try{await import("@zibby/skills")}catch{}let{getSkill:y}=await Promise.resolve().then(()=>(oe(),je)),A=new Set;for(let[,g]of this.nodes)for(let T of g.config?.skills||[])A.add(T);for(let g of A){let T=y(g);if(typeof T?.middleware=="function")try{let O=await T.middleware();typeof O=="function"&&v.set(g,O)}catch{}}let p=this.entryPoint,R=[],Ee=n?.recursionLimit??100,nt=0;try{for(;p&&p!=="END";){if(++nt>Ee)throw new Error(`Workflow exceeded recursion limit (${Ee}) \u2014 likely a cyclic conditional route. Set config.recursionLimit if you need a higher cap.`);let T=P(l,Le),O=P(l,Fe);if(ne(T)){try{Ve(T)}catch{}s.abort()}if(ne(O)){try{Ve(O)}catch{}s.abort(),ie("legacy-stop-file","Detected legacy `.zibby-studio-stop` file. Consumers should migrate to either `.zibby-stop` (renamed) or pass an AbortSignal to graph.run. The legacy filename will be removed in v2. Suppress with ZIBBY_NO_DEPRECATION_WARNINGS=1.")}if(s.signal.aborted)return console.warn(`
37
- \u{1F6D1} External stop requested \u2014 ending workflow.`),N.step("Workflow stopped externally"),{success:!0,state:f.getAll(),executionLog:R,stoppedExternally:!0,stoppedByStudio:!0};let Z=this.nodes.get(p);if(!Z)throw new Error(`Node '${p}' not found in graph`);let Ie=JSON.stringify({sessionPath:l,sessionTimestamp:m,currentNode:p,createdAt:new Date().toISOString(),config:f.get("config")}),it=P(l,L);Je(it,Ie,"utf-8");let Ne=f.get("config")?.paths?.output||te,at=P(a,Ne,L);Xe(P(a,Ne),{recursive:!0});try{Je(at,Ie,"utf-8")}catch{}let ve=t.onPipelineProgress;if(typeof ve=="function")try{ve({cwd:a,sessionPath:l,sessionId:_,outputBase:f.get("config")?.paths?.output||te,currentNode:p})}catch{}let ct=(this.resolvedToolsMap||{})[p]||null;f.set("_currentNodeTools",ct);let lt=f.get("nodeConfigs")||{};f.set("_currentNodeConfig",lt[p]||{}),N.nodeStart(p);let $e=Date.now(),J=this.nodePrompts.get(p);if(!this._invokeAgent){let k=await Promise.resolve().then(()=>(z(),K));this._invokeAgent=k.invokeAgent}let dt=this._invokeAgent,be=async(k,x,E={})=>{let b=dt(k,x,{...E,signal:s.signal});return b.catch(()=>{}),s.signal.aborted?b:Promise.race([b,new Promise((M,D)=>{let B=()=>{setTimeout(()=>{let j=new Error(`Strategy ignored AbortSignal \u2014 engine deadman fired after ${i}ms`);j.name="AbortError",D(j)},i)};s.signal.addEventListener("abort",B,{once:!0})})])},Te={state:f,invokeAgent:async(k={},x={})=>{let E=x.prompt||"";if(J){let b=this._compiledPrompts.get(p);b||(b=At.compile(J,{noEscape:!0}),this._compiledPrompts.set(p,b));try{E=b(k)}catch(M){throw console.error(`\u274C Template rendering failed for node '${p}':`,M.message),new Error(`Template rendering failed: ${M.message}`,{cause:M})}}else if(!E)throw new Error(`No prompt template configured for node '${p}' and no prompt provided in options`);return be(E,{state:f.getAll(),images:x.images||[]},{model:x.model||f.get("model"),workspace:f.get("workspace"),schema:x.schema,...x,signal:s.signal})},_coreInvokeAgent:be,agent:e,nodeId:p,promptTemplate:J,getPromptTemplate:()=>J,...f.getAll()};try{let k=(Z.config?.skills||[]).map(B=>v.get(B)).filter(Boolean),x=[...this.middleware,...k],E;x.length>0?E=await this._composeMiddleware(x,p,async()=>Z.execute(Te,f),f.getAll(),f):E=await Z.execute(Te,f);let b=Date.now()-$e;if(R.push({node:p,success:E.success,duration:b,timestamp:new Date().toISOString()}),!E.success){if(String(E.error||"").includes("Stopped from Zibby Studio"))return ie("legacy-error-string",'Strategy returned the legacy `Stopped from Zibby Studio` error string. Strategies should reject with `Error.name === "AbortError"` instead. The string-match fallback will be removed in v2. Suppress with ZIBBY_NO_DEPRECATION_WARNINGS=1.'),N.step("Workflow stopped externally"),f.set("stoppedExternally",!0),f.set("stoppedByStudio",!0),{success:!0,state:f.getAll(),executionLog:R,stoppedExternally:!0,stoppedByStudio:!0};if(s.signal.aborted)return N.step("Workflow stopped externally"),{success:!0,state:f.getAll(),executionLog:R,stoppedExternally:!0,stoppedByStudio:!0};f.append("errors",{node:p,error:E.error});let j=Z.config?.retries||0,Ae=`${p}_retries`,V=f.getAll()[Ae]||0;if(V<j){N.stepInfo(`Retrying (attempt ${V+1}/${j})`),f.update({[Ae]:V+1,[`${p}_raw`]:E.raw});continue}throw N.nodeFailed(p,E.error,{duration:b}),new Error(`Node '${p}' failed after ${V} attempts: ${E.error}`)}f.update({[p]:E.output});let M=this._summarizeNodeOutput(p,E.output);N.nodeComplete(p,{duration:b,details:M});let D=this.edges.get(p);if(!D)p="END";else if(D.conditional){let B=D.routes(f.getAll());N.route(p,B),p=B}else p=D}catch(k){throw N.isInsideNode&&N.nodeFailed(p,k.message,{duration:Date.now()-$e}),f.set("failed",!0),f.set("failedAt",p),k}}N.graphComplete();let g={success:!0,state:f.getAll(),executionLog:R};return e&&typeof e.onComplete=="function"&&await e.onComplete(g),g}finally{if(e&&typeof e.cleanup=="function")try{await e.cleanup()}catch(g){console.warn(`[workflow] agent.cleanup() failed: ${g.message}`)}}}};var _e=new Map;function Mt(o,e){_e.set(o,e)}function tt(o){return _e.get(o)}function ye(o){return _e.has(o)}Mt("ai_agent",{name:"ai_agent",factory:!0,create:(o,e={})=>({name:o,_isCustomCode:!0,execute:async t=>{let r=t?._coreInvokeAgent;r||(r=(await Promise.resolve().then(()=>(z(),K))).invokeAgent);let s=e.extraPromptInstructions||"Execute the task based on the current state.",i=Dt(s,t),a=await r(i,{cwd:t.workspace||process.cwd(),model:t.model,tools:e.resolvedTools||null});return{success:!0,output:{raw:a,nodeId:o},raw:typeof a=="string"?a:a.raw}}})});function Dt(o,e){let t=/@([\w.]+)/g,r=new Set,s;for(;(s=t.exec(o))!==null;)r.add(s[1]);if(r.size===0)return o;let i=[],a=new Set;for(let n of r){let c=n.split(".")[0];if(a.has(c))continue;let d=n.split(".").reduce((l,m)=>l?.[m],e);if(d===void 0)continue;let h=typeof d=="string"?d:d?.raw??JSON.stringify(d,null,2),u=n.replace(/_/g," ").replace(/\b\w/g,l=>l.toUpperCase());i.push(`## ${u}
36
+ ${h}`)}}function bt(){return process.env.ZIBBY_TRUST_SESSION_ENV==="1"||process.env.ZIBBY_TRUST_SESSION_ENV==="true"||process.env.ZIBBY_KEEP_SESSION_ENV==="1"||process.env.ZIBBY_KEEP_SESSION_ENV==="true"}function kt(){if(!(process.env.ZIBBY_PIN_SESSION_PATH==="1"||process.env.ZIBBY_PIN_SESSION_PATH==="true"))return;let e=process.env.ZIBBY_SESSION_PATH;if(!(e==null||String(e).trim()===""))try{return ze(String(e).trim())}catch{return String(e).trim()}}function Tt(){bt()||(delete process.env.ZIBBY_SESSION_PATH,delete process.env.ZIBBY_SESSION_ID)}function xt({sessionPath:o,sessionId:e}){o&&typeof o=="string"&&(process.env.ZIBBY_SESSION_PATH=o),e!=null&&String(e).trim()!==""&&(process.env.ZIBBY_SESSION_ID=String(e).trim())}function At(o={}){let e=De.map(i=>process.env[i]).find(Boolean),t=Math.random().toString(36).slice(2,6),r=e||`${Date.now()}_${t}`,s=o.paths?.sessionPrefix;return s?`${s}_${r}`:r}function Ot({cwd:o=process.cwd(),config:e={},initialState:t={},traceFrom:r="resolveWorkflowSession"}={}){let s=t.sessionPath,i=t.sessionTimestamp,a="initialState.sessionPath";if(!s&&process.env.ZIBBY_SESSION_PATH)try{let u=ze(String(process.env.ZIBBY_SESSION_PATH));u&&(s=u,a="ZIBBY_SESSION_PATH")}catch{}let n;if(s)n=String(s).split(/[/\\]/).filter(Boolean).pop(),i==null&&(i=Date.now());else{let u=process.env.ZIBBY_SESSION_ID&&String(process.env.ZIBBY_SESSION_ID).trim();if(u)n=u,a="ZIBBY_SESSION_ID";else{let d=e.sessionId!=null?String(e.sessionId).trim():"";d&&d!=="last"?(n=d,a="config.sessionId"):(n=At(e),a="generated")}i=i??Date.now();let h=e.paths?.output||ee;s=W(o,h,Me,n)}let c=!ge(s);return c&&Ke(s,{recursive:!0}),(c||a!=="initialState.sessionPath")&&Nt({traceFrom:r,sessionId:n,sessionPath:s,idSource:a,mkdirFresh:c}),xt({sessionPath:s,sessionId:n}),{sessionPath:s,sessionId:n,sessionTimestamp:i}}var se=class{constructor(e={}){this.nodes=new Map,this.edges=new Map,this.entryPoint=null,this.middleware=Array.isArray(e.middleware)?[...e.middleware]:[],e.nodeMiddleware&&this.middleware.push(e.nodeMiddleware),this.nodeTypeMap=new Map,this.conditionalCodeMap=new Map,this.stateSchema=e.stateSchema||null,this.nodePrompts=new Map,this.nodeOptions=new Map,this._invokeAgent=e.invokeAgent||null,this._compiledPrompts=new Map}setStateSchema(e){return this.stateSchema=e,this}getStateSchema(){return this.stateSchema}addNode(e,t,r={}){let s=t instanceof j?t:new j(t);return s.name=e,this.nodes.set(e,s),r.prompt&&this.nodePrompts.set(e,r.prompt),Object.keys(r).length>0&&this.nodeOptions.set(e,r),this}addConditionalNode(e,t){return this.nodes.set(e,new oe({...t,name:e})),this}addEdge(e,t){return this.edges.set(e,t),this}setNodeType(e,t){return this.nodeTypeMap.set(e,t),this}addConditionalEdges(e,t,{labels:r}={}){return this.edges.set(e,{conditional:!0,routes:t,labels:r}),typeof t=="function"&&this.conditionalCodeMap.set(e,t.toString()),this}setEntryPoint(e){return this.entryPoint=e,this}use(e){return typeof e=="function"&&this.middleware.push(e),this}_composeMiddleware(e,t,r,s,i){let a=r;for(let n=e.length-1;n>=0;n--){let c=e[n],u=a;a=()=>c(t,u,s,i)}return a()}serialize(){let e=[],t={};for(let[i,a]of this.nodes){let n=this.nodeTypeMap.get(i)||i;e.push({id:i,type:n,data:{nodeType:n,label:i}});let c={};a._isCustomCode&&typeof a.execute=="function"&&(c.customCode=a.execute.toString());let u=this.nodePrompts.get(i);if(u&&(c.prompt=u),typeof a.customExecute=="function"&&(c.executeCode=a.customExecute.toString()),a.outputSchema)try{if(typeof a.outputSchema._def<"u"){let d=Je(a.outputSchema,{target:"openApi3"});c.outputSchema={jsonSchema:d,variables:this._flattenJsonSchemaToVariables(d)}}else c.outputSchema={schema:a.outputSchema}}catch(d){console.warn(`[workflow] failed to convert schema for ${i}:`,d.message)}let h=(this.resolvedToolsMap||{})[i];h?.toolIds&&(c.tools=h.toolIds),Object.keys(c).length>0&&(t[i]=c)}let r=[];for(let[i,a]of this.edges)if(typeof a=="string")r.push({source:i,target:a});else if(a.conditional){let n=this.conditionalCodeMap.get(i)||a.routes.toString(),c=this._inferConditionalTargets(a.routes),u=a.labels||{};for(let h of c){let d={source:i,target:h,data:{conditionalCode:n}};u[h]&&(d.label=u[h]),r.push(d)}}let s=null;if(this.stateSchema)try{s=Je(this.stateSchema,{target:"openApi3"})}catch{s=this.stateSchema}return{nodes:e,edges:r,nodeConfigs:t,stateSchema:s}}_inferConditionalTargets(e){let t=e.toString(),r=new Set,s=/return\s+['"]([^'"]+)['"]/g,i;for(;(i=s.exec(t))!==null;)r.add(i[1]);return[...r]}_flattenJsonSchemaToVariables(e,t=""){let r=e;if(e.$ref&&e.definitions){let s=e.$ref.replace("#/definitions/","");r=e.definitions[s]||e}return this._flattenSchema(r,t)}_flattenSchema(e,t=""){if(!e||typeof e!="object")return[];let r=[],s=e.properties||{},i=e.required||[];for(let[a,n]of Object.entries(s)){let c=t?`${t}.${a}`:a;r.push({path:c,type:n.type||"unknown",label:n.description||this._formatLabel(a),optional:!i.includes(a)}),n.type==="object"&&n.properties&&r.push(...this._flattenSchema(n,c)),n.type==="array"&&n.items?.type==="object"&&n.items.properties&&r.push(...this._flattenSchema(n.items,`${c}[]`))}return r}_formatLabel(e){return e.replace(/([A-Z])/g," $1").replace(/^./,t=>t.toUpperCase()).trim()}_summarizeNodeOutput(e,t){if(!t||typeof t!="object")return[];let r=[];t.success!==void 0&&r.push(`Result: ${t.success?"passed":"failed"}`);for(let[s,i]of Object.entries(t))if(!(s==="success"||s==="raw"||s==="nextNode")){if(typeof i=="string"&&i.length<=80)r.push(`${s}: ${i}`);else if(Array.isArray(i)){let a=i.length,n=i.filter(u=>u?.passed===!0).length,c=i.some(u=>u?.passed!==void 0);r.push(c?`${s}: ${n}/${a} passed${a-n?`, ${a-n} failed`:""}`:`${s}: ${a} items`)}if(r.length>=4)break}return r}async run(e,t={},r={}){if(!this.entryPoint)throw new Error("No entry point set for graph");let s=new AbortController;r.signal&&(r.signal.aborted?s.abort():r.signal.addEventListener("abort",()=>s.abort(),{once:!0}));let i=r.strategyAbortTimeoutMs??t.config?.strategyAbortTimeoutMs??5e3,a=t.cwd||process.cwd();It({path:W(a,".env")});let n=t.config||{};if(!n||Object.keys(n).length===0)try{let g=W(a,".zibby.config.js");ge(g)&&(n=(await import(g)).default||{})}catch{}process.env.EXECUTION_ID&&!n.agent?.strictMode&&(n.agent={...n.agent,strictMode:!0});let c=t.agentType;if(!c){let g=n?.agent;g?.provider?c=g.provider:g?.gemini?c="gemini":g?.claude?c="claude":g?.cursor?c="cursor":g?.codex?c="codex":c=process.env.AGENT_TYPE||"cursor"}let u=t.contextConfig||e?.config?.contextConfig||e?.config?.context||n?.context||{};if(this.stateSchema){let g=this.stateSchema.safeParse(t);if(!g.success){let T=g.error.issues.map(b=>`${b.path.join(".")}: ${b.message}`);throw console.error("\u274C Initial state validation failed:"),T.forEach(b=>console.error(` - ${b}`)),new Error(`State validation failed: ${T.join(", ")}`)}v.step("State validated against schema")}let h=kt(),d=t.sessionPath||h;d||Tt();let{sessionPath:l,sessionTimestamp:m,sessionId:_}=Ot({cwd:a,config:n,traceFrom:"WorkflowGraph.run",initialState:{sessionPath:d,sessionTimestamp:t.sessionTimestamp}});v.step(`Session ${_}`);let w=await re.loadContext(t.specPath||"",a,u);Object.keys(w).length>0&&v.step(`Context loaded: ${Object.keys(w).join(", ")}`);let N=t.outputPath;!N&&t.specPath&&(e?.calculateOutputPath?N=e.calculateOutputPath(t.specPath):console.warn(`\u26A0\uFE0F outputPath not resolved (specPath=${t.specPath})`));let f=new q({...t,config:n,agentType:c,outputPath:N,sessionPath:l,sessionTimestamp:m,context:w,resolvedTools:this.resolvedToolsMap||{},_signal:s.signal}),$=new Map;try{await import("@zibby/skills")}catch{}let{getSkill:y}=await Promise.resolve().then(()=>(te(),Fe)),x=new Set;for(let[,g]of this.nodes)for(let T of g.config?.skills||[])x.add(T);for(let g of x){let T=y(g);if(typeof T?.middleware=="function")try{let b=await T.middleware();typeof b=="function"&&$.set(g,b)}catch{}}let p=this.entryPoint,M=[],ye=n?.recursionLimit??100,et=0;try{for(;p&&p!=="END";){if(++et>ye)throw new Error(`Workflow exceeded recursion limit (${ye}) \u2014 likely a cyclic conditional route. Set config.recursionLimit if you need a higher cap.`);let T=W(l,Be);if(ge(T)){try{$t(T)}catch{}s.abort()}if(s.signal.aborted)return console.warn(`
37
+ \u{1F6D1} External stop requested \u2014 ending workflow.`),v.step("Workflow stopped externally"),{success:!0,state:f.getAll(),executionLog:M,stoppedExternally:!0};let b=this.nodes.get(p);if(!b)throw new Error(`Node '${p}' not found in graph`);let Ee=JSON.stringify({sessionPath:l,sessionTimestamp:m,currentNode:p,createdAt:new Date().toISOString(),config:f.get("config")}),tt=W(l,L);He(tt,Ee,"utf-8");let $e=f.get("config")?.paths?.output||ee,ot=W(a,$e,L);Ke(W(a,$e),{recursive:!0});try{He(ot,Ee,"utf-8")}catch{}let Ie=t.onPipelineProgress;if(typeof Ie=="function")try{Ie({cwd:a,sessionPath:l,sessionId:_,outputBase:f.get("config")?.paths?.output||ee,currentNode:p})}catch{}let rt=(this.resolvedToolsMap||{})[p]||null;f.set("_currentNodeTools",rt);let st=f.get("nodeConfigs")||{};f.set("_currentNodeConfig",st[p]||{}),v.nodeStart(p);let ve=Date.now(),K=this.nodePrompts.get(p);if(!this._invokeAgent){let A=await Promise.resolve().then(()=>(J(),H));this._invokeAgent=A.invokeAgent}let nt=this._invokeAgent,Ne=async(A,O,I={})=>{let k=nt(A,O,{...I,signal:s.signal});return k.catch(()=>{}),s.signal.aborted?k:Promise.race([k,new Promise((B,D)=>{let P=()=>{setTimeout(()=>{let Y=new Error(`Strategy ignored AbortSignal \u2014 engine deadman fired after ${i}ms`);Y.name="AbortError",D(Y)},i)};s.signal.addEventListener("abort",P,{once:!0})})])},be={state:f,invokeAgent:async(A={},O={})=>{let I=O.prompt||"";if(K){let k=this._compiledPrompts.get(p);k||(k=vt.compile(K,{noEscape:!0}),this._compiledPrompts.set(p,k));try{I=k(A)}catch(B){throw console.error(`\u274C Template rendering failed for node '${p}':`,B.message),new Error(`Template rendering failed: ${B.message}`,{cause:B})}}else if(!I)throw new Error(`No prompt template configured for node '${p}' and no prompt provided in options`);return Ne(I,{state:f.getAll(),images:O.images||[]},{model:O.model||f.get("model"),workspace:f.get("workspace"),schema:O.schema,...O,signal:s.signal})},_coreInvokeAgent:Ne,agent:e,nodeId:p,promptTemplate:K,getPromptTemplate:()=>K,...f.getAll()};try{let A=(b.config?.skills||[]).map(P=>$.get(P)).filter(Boolean),O=[...this.middleware,...A],I;O.length>0?I=await this._composeMiddleware(O,p,async()=>b.execute(be,f),f.getAll(),f):I=await b.execute(be,f);let k=Date.now()-ve;if(M.push({node:p,success:I.success,duration:k,timestamp:new Date().toISOString()}),!I.success){if(s.signal.aborted)return v.step("Workflow stopped externally"),{success:!0,state:f.getAll(),executionLog:M,stoppedExternally:!0};f.append("errors",{node:p,error:I.error});let P=b.config?.retries||0,Y=`${p}_retries`,z=f.getAll()[Y]||0;if(z<P){v.stepInfo(`Retrying (attempt ${z+1}/${P})`),f.update({[Y]:z+1,[`${p}_raw`]:I.raw});continue}throw v.nodeFailed(p,I.error,{duration:k}),new Error(`Node '${p}' failed after ${z} attempts: ${I.error}`)}f.update({[p]:I.output});let B=this._summarizeNodeOutput(p,I.output);v.nodeComplete(p,{duration:k,details:B});let D=this.edges.get(p);if(!D)p="END";else if(D.conditional){let P=D.routes(f.getAll());v.route(p,P),p=P}else p=D}catch(A){throw v.isInsideNode&&v.nodeFailed(p,A.message,{duration:Date.now()-ve}),f.set("failed",!0),f.set("failedAt",p),A}}v.graphComplete();let g={success:!0,state:f.getAll(),executionLog:M};return e&&typeof e.onComplete=="function"&&await e.onComplete(g),g}finally{if(e&&typeof e.cleanup=="function")try{await e.cleanup()}catch(g){console.warn(`[workflow] agent.cleanup() failed: ${g.message}`)}}}};var we=Symbol.for("@zibby/agent-workflow.nodes");globalThis[we]||(globalThis[we]=new Map);var Se=globalThis[we];function Ct(o,e){Se.set(o,e)}function Ve(o){return Se.get(o)}function _e(o){return Se.has(o)}Ct("ai_agent",{name:"ai_agent",factory:!0,create:(o,e={})=>({name:o,_isCustomCode:!0,execute:async t=>{let r=t?._coreInvokeAgent;r||(r=(await Promise.resolve().then(()=>(J(),H))).invokeAgent);let s=e.extraPromptInstructions||"Execute the task based on the current state.",i=Pt(s,t),a=await r(i,{cwd:t.workspace||process.cwd(),model:t.model,tools:e.resolvedTools||null});return{success:!0,output:{raw:a,nodeId:o},raw:typeof a=="string"?a:a.raw}}})});function Pt(o,e){let t=/@([\w.]+)/g,r=new Set,s;for(;(s=t.exec(o))!==null;)r.add(s[1]);if(r.size===0)return o;let i=[],a=new Set;for(let n of r){let c=n.split(".")[0];if(a.has(c))continue;let u=n.split(".").reduce((l,m)=>l?.[m],e);if(u===void 0)continue;let h=typeof u=="string"?u:u?.raw??JSON.stringify(u,null,2),d=n.replace(/_/g," ").replace(/\b\w/g,l=>l.toUpperCase());i.push(`## ${d}
38
38
  ${h}`),n.includes(".")||a.add(c)}return i.length===0?o:`${o}
39
39
 
40
40
  ---
@@ -42,4 +42,4 @@ ${h}`),n.includes(".")||a.add(c)}return i.length===0?o:`${o}
42
42
 
43
43
  ${i.join(`
44
44
 
45
- `)}`}oe();U();var Lt={};function rt(o,e){if(Array.isArray(e))return ot(e);let t=Lt[o];return!t||t.length===0?null:ot(t)}function ot(o){if(!Array.isArray(o)||o.length===0)return null;let e=[],t={},r=[];for(let s of o){let i=H(s);if(!i){w.warn(`[workflow] unknown skill "${s}" \u2014 skipping`);continue}r.push(s);for(let a of i.tools||[])e.push({name:a.name,description:a.description,input_schema:a.input_schema||{type:"object",properties:{}}});if(!t[i.serverName])if(typeof i.resolve=="function"){let a=i.resolve();a&&(t[i.serverName]={...a,toolPrefix:s})}else{let a={};for(let n of i.envKeys||[]){let c=process.env[n];c&&(a[n]=c)}t[i.serverName]={command:i.command,args:[...i.args||[]],env:a,toolPrefix:s}}}return r.length===0?null:{toolIds:r,claudeTools:e,mcpServers:t}}U();function xo(o,e={}){let{nodes:t,edges:r,nodeConfigs:s={}}=o;if(!Array.isArray(t)||t.length===0)throw new C("Graph must have at least one node");if(!Array.isArray(r))throw new C("Graph edges must be an array");let i=new ae(e);e.stateSchema&&i.setStateSchema(e.stateSchema);let a=new Set,n=new Map,c={};for(let l of t){let m=ce(l);n.set(l.id,{...l,resolvedType:m}),m==="decision"&&a.add(l.id)}for(let[l,m]of n){if(a.has(l))continue;let _=m.resolvedType,S=s[l]||{},$=rt(_,S.tools);$&&(c[l]=$);let f={};S.prompt&&(f.prompt=S.prompt);let v=ye(_);if(w.debug(`[workflow] compiler: node "${l}" type="${_}" registered=${v}`),S.customCode&&!v)i.addNode(l,st(l,S.customCode,S),f),i.setNodeType(l,_);else if(v){let y=tt(_);y.factory?i.addNode(l,y.create(l,{...S,resolvedTools:$}),f):i.addNode(l,y,f),i.setNodeType(l,_)}else if(S.executeCode)i.addNode(l,st(l,S.executeCode,S),f),i.setNodeType(l,_);else throw new C(`Unknown node type "${_}" for node "${l}". Did you forget to register it?`)}i.resolvedToolsMap=c;let d=new Set;for(let l of r)a.has(l.target)||d.add(l.target);let h=t.find(l=>!a.has(l.id)&&!d.has(l.id));if(!h)throw new C("Could not determine entry point: no node without incoming edges found");i.setEntryPoint(h.id);let u=Ft(r,"source");for(let l of r)if(!a.has(l.source))if(a.has(l.target)){let m=l.target,_=u.get(m)||[];if(_.length===0)throw new C(`Decision node "${m}" has no outgoing edges`);let S=Yt(m,_,a);i.addConditionalEdges(l.source,S)}else i.addEdge(l.source,l.target);return i}function Co(o){let e=[];if(!o||typeof o!="object")return{valid:!1,errors:["Config must be a non-null object"]};if((!Array.isArray(o.nodes)||o.nodes.length===0)&&e.push("Graph must have at least one node"),Array.isArray(o.edges)||e.push("Graph edges must be an array"),e.length>0)return{valid:!1,errors:e};let t=o.nodeConfigs||{};for(let n of o.nodes){let c=ce(n);if(c==="decision"||ye(c))continue;let d=t[n.id]||{};d.customCode||d.executeCode||e.push(`Unknown node type "${c}" for node "${n.id}". Register it or provide customCode/executeCode.`)}let r=new Set(o.nodes.map(n=>n.id));for(let n of o.edges)r.has(n.source)||e.push(`Edge references unknown source node "${n.source}"`),r.has(n.target)||e.push(`Edge references unknown target node "${n.target}"`);let s=new Set(o.nodes.filter(n=>ce(n)==="decision").map(n=>n.id)),i=new Set;for(let n of o.edges)s.has(n.target)||i.add(n.target);let a=o.nodes.filter(n=>!s.has(n.id)&&!i.has(n.id));a.length===0?e.push("No entry point found (every node has incoming edges)"):a.length>1&&e.push(`Multiple entry points found: ${a.map(n=>n.id).join(", ")}`);for(let n of s){let c=o.edges.filter(h=>h.source===n);c.length===0&&e.push(`Decision node "${n}" has no outgoing edges`),c.some(h=>h.data?.conditionalCode||h.conditionalCode)||e.push(`Decision node "${n}" outgoing edges have no conditionalCode`)}return{valid:e.length===0,errors:e}}function Po(o){return!o||!Array.isArray(o.nodes)?[]:o.nodes.filter(e=>ce(e)!=="decision").map(e=>e.id)}function ce(o){let e=o.data?.nodeType||o.data?.type||o.type;return e==="workflowNode"||e==="custom"||e==="default"?o.id:e}function Ft(o,e){let t=new Map;for(let r of o){let s=r[e];t.has(s)||t.set(s,[]),t.get(s).push(r)}return t}function Yt(o,e,t){let r=e.find(n=>n.data?.conditionalCode||n.conditionalCode);if(!r)throw new C(`Decision node "${o}" has no conditionalCode on its outgoing edges`);let s=r.data?.conditionalCode||r.conditionalCode,i=new Set(e.map(n=>n.target).filter(n=>!t.has(n))),a;try{let c=new Function(`return (${s})`)();a=d=>{let h=c(d);return i.has(h)||w.warn(`[workflow] conditional route from "${o}" returned "${h}" which is not in valid targets: ${[...i].join(", ")}`),h}}catch(n){throw new C(`Failed to compile conditionalCode for "${o}": ${n.message}`)}return a}function st(o,e,t={}){let r;try{r=new Function("invokeAgent","require","console",`return (${e})`)}catch(a){throw new C(`Failed to compile customCode for node "${o}": ${a.message}`)}let s=r(async(...a)=>{let{invokeAgent:n}=await Promise.resolve().then(()=>(z(),K));return n(...a)},typeof le<"u"?le:void 0,console),i=null;return t.outputSchema&&(i=t.outputSchema.jsonSchema||t.outputSchema),{name:o,_isCustomCode:!0,outputSchema:i,execute:async a=>{try{let n=await s(a);return typeof n=="object"&&"success"in n?n:{success:!0,output:n,raw:null}}catch(n){return{success:!1,error:n.message,raw:null}}}}}var C=class extends Error{constructor(e){super(e),this.name="CompilationError"}};export{C as CompilationError,xo as compileGraph,Po as extractSteps,Co as validateGraphConfig};
45
+ `)}`}te();G();var Rt={};function Xe(o,e){if(Array.isArray(e))return qe(e);let t=Rt[o];return!t||t.length===0?null:qe(t)}function qe(o){if(!Array.isArray(o)||o.length===0)return null;let e=[],t={},r=[];for(let s of o){let i=U(s);if(!i){S.warn(`[workflow] unknown skill "${s}" \u2014 skipping`);continue}r.push(s);for(let a of i.tools||[])e.push({name:a.name,description:a.description,input_schema:a.input_schema||{type:"object",properties:{}}});if(!t[i.serverName])if(typeof i.resolve=="function"){let a=i.resolve();a&&(t[i.serverName]={...a,toolPrefix:s})}else{let a={};for(let n of i.envKeys||[]){let c=process.env[n];c&&(a[n]=c)}t[i.serverName]={command:i.command,args:[...i.args||[]],env:a,toolPrefix:s}}}return r.length===0?null:{toolIds:r,claudeTools:e,mcpServers:t}}G();function bo(o,e={}){let{nodes:t,edges:r,nodeConfigs:s={}}=o;if(!Array.isArray(t)||t.length===0)throw new C("Graph must have at least one node");if(!Array.isArray(r))throw new C("Graph edges must be an array");let i=new se(e);e.stateSchema&&i.setStateSchema(e.stateSchema);let a=new Set,n=new Map,c={};for(let l of t){let m=ne(l);n.set(l.id,{...l,resolvedType:m}),m==="decision"&&a.add(l.id)}for(let[l,m]of n){if(a.has(l))continue;let _=m.resolvedType,w=s[l]||{},N=Xe(_,w.tools);N&&(c[l]=N);let f={};w.prompt&&(f.prompt=w.prompt);let $=_e(_);if(S.debug(`[workflow] compiler: node "${l}" type="${_}" registered=${$}`),w.customCode&&!$)i.addNode(l,Qe(l,w.customCode,w),f),i.setNodeType(l,_);else if($){let y=Ve(_);y.factory?i.addNode(l,y.create(l,{...w,resolvedTools:N}),f):i.addNode(l,y,f),i.setNodeType(l,_)}else if(w.executeCode)i.addNode(l,Qe(l,w.executeCode,w),f),i.setNodeType(l,_);else throw new C(`Unknown node type "${_}" for node "${l}". Did you forget to register it?`)}i.resolvedToolsMap=c;let u=new Set;for(let l of r)a.has(l.target)||u.add(l.target);let h=t.find(l=>!a.has(l.id)&&!u.has(l.id));if(!h)throw new C("Could not determine entry point: no node without incoming edges found");i.setEntryPoint(h.id);let d=Wt(r,"source");for(let l of r)if(!a.has(l.source))if(a.has(l.target)){let m=l.target,_=d.get(m)||[];if(_.length===0)throw new C(`Decision node "${m}" has no outgoing edges`);let w=Mt(m,_,a);i.addConditionalEdges(l.source,w)}else i.addEdge(l.source,l.target);return i}function ko(o){let e=[];if(!o||typeof o!="object")return{valid:!1,errors:["Config must be a non-null object"]};if((!Array.isArray(o.nodes)||o.nodes.length===0)&&e.push("Graph must have at least one node"),Array.isArray(o.edges)||e.push("Graph edges must be an array"),e.length>0)return{valid:!1,errors:e};let t=o.nodeConfigs||{};for(let n of o.nodes){let c=ne(n);if(c==="decision"||_e(c))continue;let u=t[n.id]||{};u.customCode||u.executeCode||e.push(`Unknown node type "${c}" for node "${n.id}". Register it or provide customCode/executeCode.`)}let r=new Set(o.nodes.map(n=>n.id));for(let n of o.edges)r.has(n.source)||e.push(`Edge references unknown source node "${n.source}"`),r.has(n.target)||e.push(`Edge references unknown target node "${n.target}"`);let s=new Set(o.nodes.filter(n=>ne(n)==="decision").map(n=>n.id)),i=new Set;for(let n of o.edges)s.has(n.target)||i.add(n.target);let a=o.nodes.filter(n=>!s.has(n.id)&&!i.has(n.id));a.length===0?e.push("No entry point found (every node has incoming edges)"):a.length>1&&e.push(`Multiple entry points found: ${a.map(n=>n.id).join(", ")}`);for(let n of s){let c=o.edges.filter(h=>h.source===n);c.length===0&&e.push(`Decision node "${n}" has no outgoing edges`),c.some(h=>h.data?.conditionalCode||h.conditionalCode)||e.push(`Decision node "${n}" outgoing edges have no conditionalCode`)}return{valid:e.length===0,errors:e}}function To(o){return!o||!Array.isArray(o.nodes)?[]:o.nodes.filter(e=>ne(e)!=="decision").map(e=>e.id)}function ne(o){let e=o.data?.nodeType||o.data?.type||o.type;return e==="workflowNode"||e==="custom"||e==="default"?o.id:e}function Wt(o,e){let t=new Map;for(let r of o){let s=r[e];t.has(s)||t.set(s,[]),t.get(s).push(r)}return t}function Mt(o,e,t){let r=e.find(n=>n.data?.conditionalCode||n.conditionalCode);if(!r)throw new C(`Decision node "${o}" has no conditionalCode on its outgoing edges`);let s=r.data?.conditionalCode||r.conditionalCode,i=new Set(e.map(n=>n.target).filter(n=>!t.has(n))),a;try{let c=new Function(`return (${s})`)();a=u=>{let h=c(u);return i.has(h)||S.warn(`[workflow] conditional route from "${o}" returned "${h}" which is not in valid targets: ${[...i].join(", ")}`),h}}catch(n){throw new C(`Failed to compile conditionalCode for "${o}": ${n.message}`)}return a}function Qe(o,e,t={}){let r;try{r=new Function("invokeAgent","require","console",`return (${e})`)}catch(a){throw new C(`Failed to compile customCode for node "${o}": ${a.message}`)}let s=r(async(...a)=>{let{invokeAgent:n}=await Promise.resolve().then(()=>(J(),H));return n(...a)},typeof ie<"u"?ie:void 0,console),i=null;return t.outputSchema&&(i=t.outputSchema.jsonSchema||t.outputSchema),{name:o,_isCustomCode:!0,outputSchema:i,execute:async a=>{try{let n=await s(a);return typeof n=="object"&&"success"in n?n:{success:!0,output:n,raw:null}}catch(n){return{success:!1,error:n.message,raw:null}}}}}var C=class extends Error{constructor(e){super(e),this.name="CompilationError"}};export{C as CompilationError,bo as compileGraph,To as extractSteps,ko as validateGraphConfig};
package/dist/graph.d.ts CHANGED
@@ -1,10 +1,9 @@
1
1
  /**
2
2
  * Returns true when the host process has spawned this workflow with
3
3
  * ZIBBY_SESSION_* env vars that should be preserved (not cleared by
4
- * `clearInheritedSessionEnvForFreshRun`). True when ANY of:
4
+ * `clearInheritedSessionEnvForFreshRun`). Set either:
5
5
  * - `ZIBBY_TRUST_SESSION_ENV=1` (canonical, consumer-agnostic)
6
- * - `ZIBBY_KEEP_SESSION_ENV=1` (legacy CLI-side opt-in)
7
- * - `ZIBBY_RUN_SOURCE=studio` (legacy Studio-specific; deprecated)
6
+ * - `ZIBBY_KEEP_SESSION_ENV=1` (legacy CLI-side opt-in, equivalent)
8
7
  */
9
8
  export function shouldTrustInheritedSessionEnv(): boolean;
10
9
  /**
@@ -12,10 +11,9 @@ export function shouldTrustInheritedSessionEnv(): boolean;
12
11
  * app spawning the CLI with ZIBBY_SESSION_PATH already set), return its
13
12
  * resolved absolute path. Returns undefined when the host hasn't pinned one.
14
13
  *
15
- * Gated on `ZIBBY_PIN_SESSION_PATH=1` (canonical) OR the legacy
16
- * `ZIBBY_RUN_SOURCE=studio` (deprecated) so an unrelated process that
17
- * happens to have ZIBBY_SESSION_PATH in its environment doesn't accidentally
18
- * land in someone else's session folder.
14
+ * Gated on `ZIBBY_PIN_SESSION_PATH=1` so an unrelated process that happens
15
+ * to have `ZIBBY_SESSION_PATH` in its environment doesn't accidentally land
16
+ * in someone else's session folder.
19
17
  */
20
18
  export function readPinnedSessionPathFromEnv(): any;
21
19
  /** Drop stale shell exports so graph + children do not write to an old session folder. */
@@ -47,17 +45,6 @@ export function resolveWorkflowSession({ cwd, config, initialState, traceFrom, }
47
45
  sessionId: any;
48
46
  sessionTimestamp: any;
49
47
  };
50
- /**
51
- * If the host has pinned a specific session folder via env (e.g. a desktop
52
- * app spawning the CLI with ZIBBY_SESSION_PATH already set), return its
53
- * resolved absolute path. Returns undefined when the host hasn't pinned one.
54
- *
55
- * Gated on `ZIBBY_PIN_SESSION_PATH=1` (canonical) OR the legacy
56
- * `ZIBBY_RUN_SOURCE=studio` (deprecated) so an unrelated process that
57
- * happens to have ZIBBY_SESSION_PATH in its environment doesn't accidentally
58
- * land in someone else's session folder.
59
- */
60
- export function readStudioPinnedSessionPathFromEnv(): any;
61
48
  export class WorkflowGraph {
62
49
  constructor(options?: {});
63
50
  nodes: Map<any, any>;
package/dist/graph.js CHANGED
@@ -1,37 +1,37 @@
1
- var ot=Object.defineProperty;var H=(i,e)=>()=>(i&&(e=i(i=0)),e);var Ne=(i,e)=>{for(var t in e)ot(i,t,{get:e[t],enumerable:!0})};var be,st,J,w,oe=H(()=>{be=()=>{},st={debug:be,info:be,warn:(...i)=>console.warn("[workflow]",...i),error:(...i)=>console.error("[workflow]",...i)},J={impl:st},w={debug:(...i)=>J.impl.debug?.(...i),info:(...i)=>J.impl.info?.(...i),warn:(...i)=>J.impl.warn?.(...i),error:(...i)=>J.impl.error?.(...i)}});var We=H(()=>{});var Me={};Ne(Me,{clearSkills:()=>dt,getAllSkills:()=>lt,getSkill:()=>ae,hasSkill:()=>ct,listSkillIds:()=>ut,registerSkill:()=>at});function at(i){if(!i||typeof i.id!="string")throw new Error("Skill definition must include a string id");L.set(i.id,Object.freeze({...i}))}function ae(i){return L.get(i)||null}function ct(i){return L.has(i)}function lt(){return new Map(L)}function ut(){return Array.from(L.keys())}function dt(){L.clear()}var ie,L,ce=H(()=>{ie=Symbol.for("@zibby/agent-workflow.skills");globalThis[ie]||(globalThis[ie]=new Map);L=globalThis[ie]});var ue={};Ne(ue,{getAgentStrategy:()=>De,invokeAgent:()=>ht,listStrategies:()=>ft,registerStrategy:()=>pt});function pt(i){if(!i||typeof i.getName!="function"||typeof i.invoke!="function")throw new Error("strategy must implement getName() and invoke() (AgentStrategy shape)");let e=C.findIndex(t=>t.getName()===i.getName());e>=0?C[e]=i:C.push(i)}function ft(){return C.map(i=>i.getName())}function De(i={}){let{state:e={},preferredAgent:t=null}=i,r=t||e.agentType||process.env.AGENT_TYPE;if(!r){let s=C.map(n=>n.getName()).join(", ")||"none registered";throw new Error(`No agent specified. Set agentType in state or AGENT_TYPE env var. Available: ${s}`)}w.debug(`[workflow] agent selection: requested=${r}`);let o=C.find(s=>s.getName()===r);if(!o){let s=C.map(n=>n.getName()).join(", ")||"none registered";throw new Error(`Unknown agent '${r}'. Available: ${s}`)}if(!o.canHandle(i))throw new Error(`Agent '${r}' is not available in this environment. Check credentials/environment.`);return w.debug(`[workflow] using agent: ${o.getName()}`),o}async function ht(i,e={},t={}){let r=De(e),o=e.state?.config||t.config||{},s=o.models||{},n=t.nodeName&&s[t.nodeName]||null,a=s.default||null,c=o.agent?.[r.name]?.model||null,d=n||a||c||t.model||null,h={...t,model:d,workspace:e.state?.workspace||t.workspace,schema:t.schema||e.schema,images:t.images||e.images||[],skills:t.skills||e.skills||[],config:o},l=i,m=h.skills||[];if(m.length>0&&!t.skipPromptFragments){let k=m.map(P=>{let A=ae(P)?.promptFragment;return typeof A=="function"?A():A}).filter(Boolean);k.length>0&&(l+=`
1
+ var zt=Object.defineProperty;var G=(i,t)=>()=>(i&&(t=i(i=0)),t);var yt=(i,t)=>{for(var e in t)zt(i,e,{get:t[e],enumerable:!0})};var Et,Xt,K,_,Q=G(()=>{Et=()=>{},Xt={debug:Et,info:Et,warn:(...i)=>console.warn("[workflow]",...i),error:(...i)=>console.error("[workflow]",...i)},K={impl:Xt},_={debug:(...i)=>K.impl.debug?.(...i),info:(...i)=>K.impl.info?.(...i),warn:(...i)=>K.impl.warn?.(...i),error:(...i)=>K.impl.error?.(...i)}});var Pt=G(()=>{});var Ct={};yt(Ct,{clearSkills:()=>ne,getAllSkills:()=>se,getSkill:()=>rt,hasSkill:()=>oe,listSkillIds:()=>re,registerSkill:()=>ee});function ee(i){if(!i||typeof i.id!="string")throw new Error("Skill definition must include a string id");D.set(i.id,Object.freeze({...i}))}function rt(i){return D.get(i)||null}function oe(i){return D.has(i)}function se(){return new Map(D)}function re(){return Array.from(D.keys())}function ne(){D.clear()}var st,D,nt=G(()=>{st=Symbol.for("@zibby/agent-workflow.skills");globalThis[st]||(globalThis[st]=new Map);D=globalThis[st]});var at={};yt(at,{getAgentStrategy:()=>Rt,invokeAgent:()=>ce,listStrategies:()=>ae,registerStrategy:()=>ie});function ie(i){if(!i||typeof i.getName!="function"||typeof i.invoke!="function")throw new Error("strategy must implement getName() and invoke() (AgentStrategy shape)");let t=C.findIndex(e=>e.getName()===i.getName());t>=0?C[t]=i:C.push(i)}function ae(){return C.map(i=>i.getName())}function Rt(i={}){let{state:t={},preferredAgent:e=null}=i,s=e||t.agentType||process.env.AGENT_TYPE;if(!s){let r=C.map(n=>n.getName()).join(", ")||"none registered";throw new Error(`No agent specified. Set agentType in state or AGENT_TYPE env var. Available: ${r}`)}_.debug(`[workflow] agent selection: requested=${s}`);let o=C.find(r=>r.getName()===s);if(!o){let r=C.map(n=>n.getName()).join(", ")||"none registered";throw new Error(`Unknown agent '${s}'. Available: ${r}`)}if(!o.canHandle(i))throw new Error(`Agent '${s}' is not available in this environment. Check credentials/environment.`);return _.debug(`[workflow] using agent: ${o.getName()}`),o}async function ce(i,t={},e={}){let s=Rt(t),o=t.state?.config||e.config||{},r=o.models||{},n=e.nodeName&&r[e.nodeName]||null,a=r.default||null,c=o.agent?.[s.name]?.model||null,d=n||a||c||e.model||null,h={...e,model:d,workspace:t.state?.workspace||e.workspace,schema:e.schema||t.schema,images:e.images||t.images||[],skills:e.skills||t.skills||[],config:o},l=i,m=h.skills||[];if(m.length>0&&!e.skipPromptFragments){let x=m.map(P=>{let T=rt(P)?.promptFragment;return typeof T=="function"?T():T}).filter(Boolean);x.length>0&&(l+=`
2
2
 
3
- ${k.join(`
3
+ ${x.join(`
4
4
 
5
- `)}`)}let E=e.state?._currentNodeConfig?.extraPromptInstructions?.trim();return E&&(l+=`
5
+ `)}`)}let E=t.state?._currentNodeConfig?.extraPromptInstructions?.trim();return E&&(l+=`
6
6
 
7
7
  \u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501
8
8
  PRIORITY OVERRIDE \u2014 THE FOLLOWING INSTRUCTIONS TAKE PRECEDENCE OVER ALL PREVIOUS CONTENT
9
9
  \u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501
10
10
 
11
11
  ${E}
12
- `),w.debug(`[workflow] prompt length: ${l.length} chars`),r.invoke(l,h)}var le,C,de=H(()=>{We();oe();ce();le=Symbol.for("@zibby/agent-workflow.strategies");globalThis[le]||(globalThis[le]=[]);C=globalThis[le]});var rt=new Set(["__proto__","constructor","prototype"]);function te(i){if(rt.has(i))throw new Error(`Invalid state key: "${i}"`)}var K=class{constructor(e={}){this._state=Object.create(null),Object.assign(this._state,{messages:[],errors:[],artifacts:{},metadata:{},...e}),this._history=[]}get(e){return this._state[e]}set(e,t){te(e),this._history.push({...this._state}),this._state[e]=t}update(e){let t=Object.getOwnPropertyNames(e);for(let r of t)te(r);this._history.push({...this._state});for(let r of t)this._state[r]=e[r]}append(e,t){te(e),this._history.push({...this._state}),Array.isArray(this._state[e])||(this._state[e]=[]),this._state[e].push(t)}getAll(){return{...this._state}}rollback(){this._history.length>0&&(this._state=this._history.pop())}};var z=class{constructor(e){this.schema=e}parse(e){let t=e.match(/```json\s*([\s\S]*?)\s*```/);if(t)return this.validate(JSON.parse(t[1]));let r=[e.match(/\{[\s\S]*?\}/),e.match(/\{[\s\S]*\}/)].filter(Boolean).map(o=>o[0]);for(let o of r)try{return this.validate(JSON.parse(o))}catch(s){if(!(s instanceof SyntaxError))throw s}return this.validate({result:e.trim()})}validate(e){let t=[];for(let[r,o]of Object.entries(this.schema)){if(o.required&&!(r in e)&&t.push(`Missing required field: ${r}`),r in e&&o.type){let s=typeof e[r];s!==o.type&&t.push(`Field '${r}' expected ${o.type}, got ${s}`)}if(o.validate&&r in e){let s=o.validate(e[r]);s&&t.push(`Field '${r}': ${s}`)}}if(t.length>0)throw new Error(`Output validation failed:
13
- ${t.join(`
14
- `)}`);return e}};oe();import{writeFileSync as pe,readFileSync as Le,existsSync as Ye,mkdirSync as mt}from"node:fs";import{join as fe,dirname as gt}from"node:path";import S from"chalk";var nt="__WORKFLOW_GRAPH_LOG__",U=S.gray("\u2502"),it=S.gray("\u250C"),Oe=S.gray("\u2514"),re=S.green("\u25C6"),$e=S.hex("#c084fc")("\u25C6"),Te=S.hex("#2dd4bf")("\u25C6"),se=S.red("\u25C6"),ve=`${U} `,Ae=2;function ke(i){return i<1e3?`${i}ms`:`${(i/1e3).toFixed(1)}s`}function Pe(i,e){return(t,r,o)=>{if(typeof t!="string")return i(t,r,o);let s=process.stdout.columns||120,n="";for(let a=0;a<t.length;a++){let c=t[a];e.lineStart&&(n+=ve,e.col=Ae,e.lineStart=!1),c===`
15
- `?(n+=c,e.lineStart=!0,e.col=0,e.inEsc=!1):c==="\x1B"?(e.inEsc=!0,n+=c):e.inEsc?(n+=c,(c>="A"&&c<="Z"||c>="a"&&c<="z")&&(e.inEsc=!1)):(e.col++,n+=c,e.col>=s&&(n+=`
16
- ${ve}`,e.col=Ae))}return i(n,r,o)}}var ne=class{constructor(){this._currentNode=null,this._origStdoutWrite=null,this._origStderrWrite=null;let e=String(process.env.ZIBBY_EMIT_GRAPH_MARKERS||"").trim()==="1"||String(process.env.ZIBBY_WORKFLOW_GRAPH_LOG_MARKERS||"").trim()==="1",t=!e&&String(process.env.ZIBBY_RUN_SOURCE||"").trim().toLowerCase()==="studio";t&&process.env.ZIBBY_NO_DEPRECATION_WARNINGS!=="1"&&console.warn("[zibby/agent-workflow] `ZIBBY_RUN_SOURCE=studio` env var is deprecated for graph-marker emission. Set `ZIBBY_EMIT_GRAPH_MARKERS=1` instead. The Studio-specific value will be ignored in v2. Suppress with ZIBBY_NO_DEPRECATION_WARNINGS=1."),this._emitWorkflowGraphMarkers=e||t}get isInsideNode(){return this._currentNode!==null}_startIntercepting(){this._origStdoutWrite=process.stdout.write.bind(process.stdout),this._origStderrWrite=process.stderr.write.bind(process.stderr);let e={lineStart:!0,col:0,inEsc:!1},t={lineStart:!0,col:0,inEsc:!1};this._outState=e,this._errState=t,process.stdout.write=Pe(this._origStdoutWrite,e),process.stderr.write=Pe(this._origStderrWrite,t)}_stopIntercepting(){this._origStdoutWrite&&(this._outState&&!this._outState.lineStart&&this._origStdoutWrite(`
12
+ `),_.debug(`[workflow] prompt length: ${l.length} chars`),s.invoke(l,h)}var it,C,ct=G(()=>{Pt();Q();nt();it=Symbol.for("@zibby/agent-workflow.strategies");globalThis[it]||(globalThis[it]=[]);C=globalThis[it]});var qt=new Set(["__proto__","constructor","prototype"]);function X(i){if(qt.has(i))throw new Error(`Invalid state key: "${i}"`)}var H=class{constructor(t={}){this._state=Object.create(null),Object.assign(this._state,{messages:[],errors:[],artifacts:{},metadata:{},...t}),this._history=[]}get(t){return this._state[t]}set(t,e){X(t),this._history.push({...this._state}),this._state[t]=e}update(t){let e=Object.getOwnPropertyNames(t);for(let s of e)X(s);this._history.push({...this._state});for(let s of e)this._state[s]=t[s]}append(t,e){X(t),this._history.push({...this._state}),Array.isArray(this._state[t])||(this._state[t]=[]),this._state[t].push(e)}getAll(){return{...this._state}}rollback(){this._history.length>0&&(this._state=this._history.pop())}};var J=class{constructor(t){this.schema=t}parse(t){let e=t.match(/```json\s*([\s\S]*?)\s*```/);if(e)return this.validate(JSON.parse(e[1]));let s=[t.match(/\{[\s\S]*?\}/),t.match(/\{[\s\S]*\}/)].filter(Boolean).map(o=>o[0]);for(let o of s)try{return this.validate(JSON.parse(o))}catch(r){if(!(r instanceof SyntaxError))throw r}return this.validate({result:t.trim()})}validate(t){let e=[];for(let[s,o]of Object.entries(this.schema)){if(o.required&&!(s in t)&&e.push(`Missing required field: ${s}`),s in t&&o.type){let r=typeof t[s];r!==o.type&&e.push(`Field '${s}' expected ${o.type}, got ${r}`)}if(o.validate&&s in t){let r=o.validate(t[s]);r&&e.push(`Field '${s}': ${r}`)}}if(e.length>0)throw new Error(`Output validation failed:
13
+ ${e.join(`
14
+ `)}`);return t}};Q();import{writeFileSync as lt,readFileSync as Wt,existsSync as Bt,mkdirSync as le}from"node:fs";import{join as ut,dirname as ue}from"node:path";import g from"chalk";var Qt="__WORKFLOW_GRAPH_LOG__",Y=g.gray("\u2502"),te=g.gray("\u250C"),It=g.gray("\u2514"),tt=g.green("\u25C6"),$t=g.hex("#c084fc")("\u25C6"),bt=g.hex("#2dd4bf")("\u25C6"),et=g.red("\u25C6"),Nt=`${Y} `,Ot=2;function kt(i){return i<1e3?`${i}ms`:`${(i/1e3).toFixed(1)}s`}function vt(i,t){return(e,s,o)=>{if(typeof e!="string")return i(e,s,o);let r=process.stdout.columns||120,n="";for(let a=0;a<e.length;a++){let c=e[a];t.lineStart&&(n+=Nt,t.col=Ot,t.lineStart=!1),c===`
15
+ `?(n+=c,t.lineStart=!0,t.col=0,t.inEsc=!1):c==="\x1B"?(t.inEsc=!0,n+=c):t.inEsc?(n+=c,(c>="A"&&c<="Z"||c>="a"&&c<="z")&&(t.inEsc=!1)):(t.col++,n+=c,t.col>=r&&(n+=`
16
+ ${Nt}`,t.col=Ot))}return i(n,s,o)}}var ot=class{constructor(){this._currentNode=null,this._origStdoutWrite=null,this._origStderrWrite=null,this._emitWorkflowGraphMarkers=String(process.env.ZIBBY_EMIT_GRAPH_MARKERS||"").trim()==="1"||String(process.env.ZIBBY_WORKFLOW_GRAPH_LOG_MARKERS||"").trim()==="1"}get isInsideNode(){return this._currentNode!==null}_startIntercepting(){this._origStdoutWrite=process.stdout.write.bind(process.stdout),this._origStderrWrite=process.stderr.write.bind(process.stderr);let t={lineStart:!0,col:0,inEsc:!1},e={lineStart:!0,col:0,inEsc:!1};this._outState=t,this._errState=e,process.stdout.write=vt(this._origStdoutWrite,t),process.stderr.write=vt(this._origStderrWrite,e)}_stopIntercepting(){this._origStdoutWrite&&(this._outState&&!this._outState.lineStart&&this._origStdoutWrite(`
17
17
  `),process.stdout.write=this._origStdoutWrite),this._origStderrWrite&&(this._errState&&!this._errState.lineStart&&this._origStderrWrite(`
18
- `),process.stderr.write=this._origStderrWrite),this._origStdoutWrite=null,this._origStderrWrite=null}_rawWrite(e){(this._origStdoutWrite||process.stdout.write.bind(process.stdout))(`${e}
19
- `)}_emitGraphLogMarker(e){if(!this._emitWorkflowGraphMarkers)return;let t=`${nt}${JSON.stringify(e)}
20
- `;this._origStdoutWrite?this._origStdoutWrite(t):process.stdout.write(t)}_writeDot(e,t){this._origStdoutWrite?(this._outState&&!this._outState.lineStart&&(this._origStdoutWrite(`
21
- `),this._outState.lineStart=!0,this._outState.col=0),this._origStdoutWrite(`${e} ${t}
22
- `)):process.stdout.write.bind(process.stdout)(`${e} ${t}
23
- `)}step(e){this._origStdoutWrite?this._writeDot(re,e):process.stdout.write.bind(process.stdout)(`${U} ${re} ${e}
24
- `)}stepInfo(e){this.step(e)}stepTool(e){this._origStdoutWrite?this._writeDot($e,e):process.stdout.write.bind(process.stdout)(`${U} ${$e} ${e}
25
- `)}stepMemory(e){let t=S.hex("#2dd4bf")(e);this._origStdoutWrite?this._writeDot(Te,t):process.stdout.write.bind(process.stdout)(`${U} ${Te} ${t}
26
- `)}stepFail(e){this._origStdoutWrite?this._writeDot(se,S.red(e)):process.stdout.write.bind(process.stdout)(`${U} ${se} ${S.red(e)}
27
- `)}nodeStart(e){this._currentNode=e,this._emitGraphLogMarker({phase:"node_begin",node:e}),this._rawWrite(`${it} ${e}`),this._startIntercepting()}nodeComplete(e,t={}){this._stopIntercepting();let{duration:r,details:o}=t;if(o)for(let n of o)this._rawWrite(`${re} ${n}`);let s=r?S.dim(` ${ke(r)}`):"";this._rawWrite(`${Oe} ${S.green("done")}${s}`),this._emitGraphLogMarker({phase:"node_end",node:e}),this._rawWrite("")}nodeFailed(e,t,r={}){this._stopIntercepting();let{duration:o}=r,s=o?S.dim(` ${ke(o)}`):"";this._rawWrite(`${se} ${S.red(t)}`),this._rawWrite(`${Oe} ${S.red("failed")}${s}`),this._emitGraphLogMarker({phase:"node_end",node:e}),this._rawWrite("")}route(e,t){this._rawWrite(S.dim(` ${e} \u2192 ${t}`)),this._rawWrite("")}graphComplete(){this._rawWrite(S.green.bold("\u2713 Workflow completed"))}},_=new ne;var V=".zibby/output",Be="sessions",D=".session-info.json",Re=".zibby-stop",xe=".zibby-studio-stop";var Ce=["CI_JOB_ID","GITHUB_RUN_ID","CIRCLE_WORKFLOW_ID","BUILD_ID"];var Y=class{constructor(e){if(this.config=e,this.name=e.name,this.prompt=e.prompt,this.outputSchema=e.outputSchema,!this.outputSchema&&!e._isCustomCode)throw new Error(`Node '${this.name}' must define outputSchema (Zod schema). This defines the contract for what the node returns to state.`);this.isZodSchema=this.outputSchema&&typeof this.outputSchema._def<"u",this.parser=e.outputSchema&&!this.isZodSchema?new z(e.outputSchema):null,this.retries=e.retries||0,this.onComplete=e.onComplete,this.customExecute=e.execute}async execute(e,t){let r=()=>t&&typeof t.getAll=="function"?t.getAll():e,o=l=>t&&typeof t.get=="function"?t.get(l):e?.[l];if(typeof this.customExecute=="function"){w.debug(`[workflow] node '${this.name}': custom execute (skipping LLM)`);try{let l=await this.customExecute(e);return typeof l=="object"&&l!==null&&l.success===!1?{success:!1,error:l.error||"Node execution failed",raw:l.raw||null}:this.isZodSchema?(w.debug(`[workflow] node '${this.name}': validating output schema`),{success:!0,output:this.outputSchema.parse(l),raw:null}):{success:!0,output:l,raw:null}}catch(l){return w.error(`[workflow] node '${this.name}' failed: ${l.message}`),l.name==="ZodError"&&w.error(`Schema errors: ${JSON.stringify(l.issues||l.errors,null,2)}`),{success:!1,error:l.message,raw:null}}}let s=typeof this.prompt=="function"?this.prompt(r()):this.prompt,n=o("_skillHints");n&&(s=`${n}
18
+ `),process.stderr.write=this._origStderrWrite),this._origStdoutWrite=null,this._origStderrWrite=null}_rawWrite(t){(this._origStdoutWrite||process.stdout.write.bind(process.stdout))(`${t}
19
+ `)}_emitGraphLogMarker(t){if(!this._emitWorkflowGraphMarkers)return;let e=`${Qt}${JSON.stringify(t)}
20
+ `;this._origStdoutWrite?this._origStdoutWrite(e):process.stdout.write(e)}_writeDot(t,e){this._origStdoutWrite?(this._outState&&!this._outState.lineStart&&(this._origStdoutWrite(`
21
+ `),this._outState.lineStart=!0,this._outState.col=0),this._origStdoutWrite(`${t} ${e}
22
+ `)):process.stdout.write.bind(process.stdout)(`${t} ${e}
23
+ `)}step(t){this._origStdoutWrite?this._writeDot(tt,t):process.stdout.write.bind(process.stdout)(`${Y} ${tt} ${t}
24
+ `)}stepInfo(t){this.step(t)}stepTool(t){this._origStdoutWrite?this._writeDot($t,t):process.stdout.write.bind(process.stdout)(`${Y} ${$t} ${t}
25
+ `)}stepMemory(t){let e=g.hex("#2dd4bf")(t);this._origStdoutWrite?this._writeDot(bt,e):process.stdout.write.bind(process.stdout)(`${Y} ${bt} ${e}
26
+ `)}stepFail(t){this._origStdoutWrite?this._writeDot(et,g.red(t)):process.stdout.write.bind(process.stdout)(`${Y} ${et} ${g.red(t)}
27
+ `)}nodeStart(t){this._currentNode=t,this._emitGraphLogMarker({phase:"node_begin",node:t}),this._rawWrite(`${te} ${t}`),this._startIntercepting()}nodeComplete(t,e={}){this._stopIntercepting();let{duration:s,details:o}=e;if(o)for(let n of o)this._rawWrite(`${tt} ${n}`);let r=s?g.dim(` ${kt(s)}`):"";this._rawWrite(`${It} ${g.green("done")}${r}`),this._emitGraphLogMarker({phase:"node_end",node:t}),this._rawWrite("")}nodeFailed(t,e,s={}){this._stopIntercepting();let{duration:o}=s,r=o?g.dim(` ${kt(o)}`):"";this._rawWrite(`${et} ${g.red(e)}`),this._rawWrite(`${It} ${g.red("failed")}${r}`),this._emitGraphLogMarker({phase:"node_end",node:t}),this._rawWrite("")}route(t,e){this._rawWrite(g.dim(` ${t} \u2192 ${e}`)),this._rawWrite("")}graphComplete(){this._rawWrite(g.green.bold("\u2713 Workflow completed"))}},w=new ot;var V=".zibby/output",Tt="sessions",L=".session-info.json",At=".zibby-stop";var xt=["CI_JOB_ID","GITHUB_RUN_ID","CIRCLE_WORKFLOW_ID","BUILD_ID"];var F=class{constructor(t){if(this.config=t,this.name=t.name,this.prompt=t.prompt,this.outputSchema=t.outputSchema,!this.outputSchema&&!t._isCustomCode)throw new Error(`Node '${this.name}' must define outputSchema (Zod schema). This defines the contract for what the node returns to state.`);this.isZodSchema=this.outputSchema&&typeof this.outputSchema._def<"u",this.parser=t.outputSchema&&!this.isZodSchema?new J(t.outputSchema):null,this.retries=t.retries||0,this.onComplete=t.onComplete,this.customExecute=t.execute}async execute(t,e){let s=()=>e&&typeof e.getAll=="function"?e.getAll():t,o=l=>e&&typeof e.get=="function"?e.get(l):t?.[l];if(typeof this.customExecute=="function"){_.debug(`[workflow] node '${this.name}': custom execute (skipping LLM)`);try{let l=await this.customExecute(t);return typeof l=="object"&&l!==null&&l.success===!1?{success:!1,error:l.error||"Node execution failed",raw:l.raw||null}:this.isZodSchema?(_.debug(`[workflow] node '${this.name}': validating output schema`),{success:!0,output:this.outputSchema.parse(l),raw:null}):{success:!0,output:l,raw:null}}catch(l){return _.error(`[workflow] node '${this.name}' failed: ${l.message}`),l.name==="ZodError"&&_.error(`Schema errors: ${JSON.stringify(l.issues||l.errors,null,2)}`),{success:!1,error:l.message,raw:null}}}let r=typeof this.prompt=="function"?this.prompt(s()):this.prompt,n=o("_skillHints");n&&(r=`${n}
28
28
 
29
- ${s}`);let a=r(),c=a.cwd||process.cwd(),d=a.sessionPath;try{if(d){let l=fe(d,D);if(Ye(l)){let E=JSON.parse(Le(l,"utf-8"));E.currentNode=this.name,pe(l,JSON.stringify(E,null,2),"utf-8")}let m=fe(d,"..",D);if(Ye(m))try{let E=JSON.parse(Le(m,"utf-8"));E.currentNode=this.name,pe(m,JSON.stringify(E,null,2),"utf-8")}catch{}}}catch(l){w.debug(`[workflow] could not update session info: ${l.message}`)}let h=null;for(let l=0;l<=this.retries;l++)try{w.debug(`[workflow] node '${this.name}' attempt ${l}`);let m=r().config||{},E=m.agents||{},k=this.config.agent??E[this.name]??null,P={state:r()};k&&(P.preferredAgent=k);let A={workspace:c,schema:this.isZodSchema?this.outputSchema:null,skills:this.config.skills||[],sessionPath:d,config:m,nodeName:this.name,timeout:this.config?.timeout||3e5},p=e?._coreInvokeAgent;p||(p=(await Promise.resolve().then(()=>(de(),ue))).invokeAgent);let I=await p(s,P,A),y,O;if(typeof I=="string"?(y=I,O=null):I.structured?(y=I.raw||JSON.stringify(I.structured,null,2),O=I.structured):(y=I.raw||JSON.stringify(I,null,2),O=I.extracted||null),d)try{let u=fe(d,this.name,"raw_stream_output.txt");mt(gt(u),{recursive:!0}),pe(u,typeof y=="string"?y:JSON.stringify(y),"utf-8")}catch(u){w.debug(`[workflow] could not save raw output: ${u.message}`)}if(this.isZodSchema&&O){w.info(`[workflow] node '${this.name}': output validated: ${JSON.stringify(O,null,2)}`);let u=O;if(typeof this.onComplete=="function")try{u=await this.onComplete(r(),O)}catch(R){w.warn(`[workflow] onComplete hook failed: ${R.message}`)}return{success:!0,output:u,raw:y}}if(typeof this.onComplete=="function")try{return{success:!0,output:await this.onComplete(r(),{raw:y}),raw:y}}catch(u){throw new Error(`onComplete failed: ${u.message}`,{cause:u})}if(this.parser){let u=this.parser.parse(y);return w.info(`[workflow] node '${this.name}': parsed output: ${JSON.stringify(u,null,2)}`),_.step("Output parsed"),{success:!0,output:u,raw:y}}return{success:!0,output:y,raw:y}}catch(m){h=m,l<this.retries&&w.info(`[workflow] node '${this.name}' failed, retrying (${l+1}/${this.retries})\u2026`)}return{success:!1,error:h.message,raw:null}}},q=class extends Y{constructor(e){super({...e,_isCustomCode:!0}),this.condition=e.condition}async execute(e,t){let r=t&&typeof t.getAll=="function"?t.getAll():e;return{success:!0,output:{nextNode:this.condition(r)},raw:null}}};import{existsSync as Ze,readFileSync as St}from"node:fs";import{join as he,dirname as Fe}from"node:path";var Q=class{static async loadContext(e,t,r={}){let o={},s=r.filenames||["CONTEXT.md","AGENTS.md"];if(e){let a=Fe(he(t,e));for(let c of s){let d=await this.findAndMergeContextFiles(c,a,t);if(d){let h=c.replace(/\.[^.]+$/,"").toLowerCase();o[h]=d}}}let n=r.discovery||{};for(let[a,c]of Object.entries(n))try{let d=he(t,c);Ze(d)&&(o[a]=await this.loadFile(d))}catch(d){console.warn(`[workflow] could not load context '${a}' from '${c}': ${d.message}`)}return o}static async findAndMergeContextFiles(e,t,r){let o=[],s=t;for(;s.startsWith(r);){let n=he(s,e);if(Ze(n))try{o.unshift(await this.loadFile(n))}catch(c){console.warn(`[workflow] could not load ${e} from ${n}: ${c.message}`)}let a=Fe(s);if(a===s)break;s=a}return o.length===0?null:o.every(n=>typeof n=="string")?o.join(`
29
+ ${r}`);let a=s(),c=a.cwd||process.cwd(),d=a.sessionPath;try{if(d){let l=ut(d,L);if(Bt(l)){let E=JSON.parse(Wt(l,"utf-8"));E.currentNode=this.name,lt(l,JSON.stringify(E,null,2),"utf-8")}let m=ut(d,"..",L);if(Bt(m))try{let E=JSON.parse(Wt(m,"utf-8"));E.currentNode=this.name,lt(m,JSON.stringify(E,null,2),"utf-8")}catch{}}}catch(l){_.debug(`[workflow] could not update session info: ${l.message}`)}let h=null;for(let l=0;l<=this.retries;l++)try{_.debug(`[workflow] node '${this.name}' attempt ${l}`);let m=s().config||{},E=m.agents||{},x=this.config.agent??E[this.name]??null,P={state:s()};x&&(P.preferredAgent=x);let T={workspace:c,schema:this.isZodSchema?this.outputSchema:null,skills:this.config.skills||[],sessionPath:d,config:m,nodeName:this.name,timeout:this.config?.timeout||3e5},p=t?._coreInvokeAgent;p||(p=(await Promise.resolve().then(()=>(ct(),at))).invokeAgent);let $=await p(r,P,T),y,O;if(typeof $=="string"?(y=$,O=null):$.structured?(y=$.raw||JSON.stringify($.structured,null,2),O=$.structured):(y=$.raw||JSON.stringify($,null,2),O=$.extracted||null),d)try{let u=ut(d,this.name,"raw_stream_output.txt");le(ue(u),{recursive:!0}),lt(u,typeof y=="string"?y:JSON.stringify(y),"utf-8")}catch(u){_.debug(`[workflow] could not save raw output: ${u.message}`)}if(this.isZodSchema&&O){_.info(`[workflow] node '${this.name}': output validated: ${JSON.stringify(O,null,2)}`);let u=O;if(typeof this.onComplete=="function")try{u=await this.onComplete(s(),O)}catch(W){_.warn(`[workflow] onComplete hook failed: ${W.message}`)}return{success:!0,output:u,raw:y}}if(typeof this.onComplete=="function")try{return{success:!0,output:await this.onComplete(s(),{raw:y}),raw:y}}catch(u){throw new Error(`onComplete failed: ${u.message}`,{cause:u})}if(this.parser){let u=this.parser.parse(y);return _.info(`[workflow] node '${this.name}': parsed output: ${JSON.stringify(u,null,2)}`),w.step("Output parsed"),{success:!0,output:u,raw:y}}return{success:!0,output:y,raw:y}}catch(m){h=m,l<this.retries&&_.info(`[workflow] node '${this.name}' failed, retrying (${l+1}/${this.retries})\u2026`)}return{success:!1,error:h.message,raw:null}}},z=class extends F{constructor(t){super({...t,_isCustomCode:!0}),this.condition=t.condition}async execute(t,e){let s=e&&typeof e.getAll=="function"?e.getAll():t;return{success:!0,output:{nextNode:this.condition(s)},raw:null}}};import{existsSync as Mt,readFileSync as de}from"node:fs";import{join as dt,dirname as Lt}from"node:path";var q=class{static async loadContext(t,e,s={}){let o={},r=s.filenames||["CONTEXT.md","AGENTS.md"];if(t){let a=Lt(dt(e,t));for(let c of r){let d=await this.findAndMergeContextFiles(c,a,e);if(d){let h=c.replace(/\.[^.]+$/,"").toLowerCase();o[h]=d}}}let n=s.discovery||{};for(let[a,c]of Object.entries(n))try{let d=dt(e,c);Mt(d)&&(o[a]=await this.loadFile(d))}catch(d){console.warn(`[workflow] could not load context '${a}' from '${c}': ${d.message}`)}return o}static async findAndMergeContextFiles(t,e,s){let o=[],r=e;for(;r.startsWith(s);){let n=dt(r,t);if(Mt(n))try{o.unshift(await this.loadFile(n))}catch(c){console.warn(`[workflow] could not load ${t} from ${n}: ${c.message}`)}let a=Lt(r);if(a===r)break;r=a}return o.length===0?null:o.every(n=>typeof n=="string")?o.join(`
30
30
 
31
31
  ---
32
32
 
33
- `):o.every(n=>typeof n=="object")?Object.assign({},...o):o[o.length-1]}static async loadFile(e){let t=St(e,"utf-8");if(e.endsWith(".json"))return JSON.parse(t);if(e.endsWith(".js")||e.endsWith(".mjs")){let{pathToFileURL:r}=await import("url"),o=await import(r(e).href);return o.default||o}return t}};import{mkdirSync as ze,existsSync as X,writeFileSync as Ue,unlinkSync as je}from"node:fs";import{join as B,resolve as Je}from"node:path";import{config as _t}from"dotenv";import{zodToJsonSchema as Ge}from"zod-to-json-schema";import wt from"handlebars";var He=new Set;function ee(i,e){He.has(i)||(He.add(i),process.env.ZIBBY_NO_DEPRECATION_WARNINGS!=="1"&&console.warn(`[zibby/agent-workflow] ${e}`))}function yt({traceFrom:i,sessionId:e,sessionPath:t,idSource:r,mkdirFresh:o}){if(!(process.env.ZIBBY_SESSION_LOG==="1"||process.env.ZIBBY_SESSION_LOG==="true"))return;let n=typeof process.ppid=="number"?process.ppid:"n/a",a=`[zibby:session] from=${i} pid=${process.pid} ppid=${n} sessionId=${e} source=${r} mkdir=${o?"yes":"no"} path=${t}`;if(console.log(a),process.env.ZIBBY_TRACE_SESSION==="1"||process.env.ZIBBY_TRACE_SESSION==="true"){let h=(new Error("session trace").stack||"").split(`
33
+ `):o.every(n=>typeof n=="object")?Object.assign({},...o):o[o.length-1]}static async loadFile(t){let e=de(t,"utf-8");if(t.endsWith(".json"))return JSON.parse(e);if(t.endsWith(".js")||t.endsWith(".mjs")){let{pathToFileURL:s}=await import("url"),o=await import(s(t).href);return o.default||o}return e}};import{mkdirSync as Yt,existsSync as pt,writeFileSync as Dt,unlinkSync as pe}from"node:fs";import{join as R,resolve as Zt}from"node:path";import{config as fe}from"dotenv";import{zodToJsonSchema as Ft}from"zod-to-json-schema";import he from"handlebars";function me({traceFrom:i,sessionId:t,sessionPath:e,idSource:s,mkdirFresh:o}){if(!(process.env.ZIBBY_SESSION_LOG==="1"||process.env.ZIBBY_SESSION_LOG==="true"))return;let n=typeof process.ppid=="number"?process.ppid:"n/a",a=`[zibby:session] from=${i} pid=${process.pid} ppid=${n} sessionId=${t} source=${s} mkdir=${o?"yes":"no"} path=${e}`;if(console.log(a),process.env.ZIBBY_TRACE_SESSION==="1"||process.env.ZIBBY_TRACE_SESSION==="true"){let h=(new Error("session trace").stack||"").split(`
34
34
  `).slice(2,14).join(`
35
35
  `);console.log(`[zibby:session] stack (${i}):
36
- ${h}`)}}function Et(){return process.env.ZIBBY_TRUST_SESSION_ENV==="1"||process.env.ZIBBY_TRUST_SESSION_ENV==="true"||process.env.ZIBBY_KEEP_SESSION_ENV==="1"||process.env.ZIBBY_KEEP_SESSION_ENV==="true"?!0:process.env.ZIBBY_RUN_SOURCE==="studio"?(ee("legacy-zibby-run-source","`ZIBBY_RUN_SOURCE=studio` env var is deprecated. Set `ZIBBY_TRUST_SESSION_ENV=1` (and `ZIBBY_PIN_SESSION_PATH=1` / `ZIBBY_EMIT_GRAPH_MARKERS=1` if you need those gates) instead. The Studio-specific value will be ignored in v2. Suppress with ZIBBY_NO_DEPRECATION_WARNINGS=1."),!0):!1}function It(){let i=process.env.ZIBBY_PIN_SESSION_PATH==="1"||process.env.ZIBBY_PIN_SESSION_PATH==="true",e=!i&&process.env.ZIBBY_RUN_SOURCE==="studio";if(e&&ee("legacy-zibby-run-source","`ZIBBY_RUN_SOURCE=studio` env var is deprecated. Set `ZIBBY_PIN_SESSION_PATH=1` (and `ZIBBY_TRUST_SESSION_ENV=1` / `ZIBBY_EMIT_GRAPH_MARKERS=1` if you need those gates) instead. The Studio-specific value will be ignored in v2. Suppress with ZIBBY_NO_DEPRECATION_WARNINGS=1."),!i&&!e)return;let t=process.env.ZIBBY_SESSION_PATH;if(!(t==null||String(t).trim()===""))try{return Je(String(t).trim())}catch{return String(t).trim()}}var Nt=It;function bt(){Et()||(delete process.env.ZIBBY_SESSION_PATH,delete process.env.ZIBBY_SESSION_ID)}function Ot({sessionPath:i,sessionId:e}){i&&typeof i=="string"&&(process.env.ZIBBY_SESSION_PATH=i),e!=null&&String(e).trim()!==""&&(process.env.ZIBBY_SESSION_ID=String(e).trim())}function $t(i={}){let e=Ce.map(s=>process.env[s]).find(Boolean),t=Math.random().toString(36).slice(2,6),r=e||`${Date.now()}_${t}`,o=i.paths?.sessionPrefix;return o?`${o}_${r}`:r}function Tt({cwd:i=process.cwd(),config:e={},initialState:t={},traceFrom:r="resolveWorkflowSession"}={}){let o=t.sessionPath,s=t.sessionTimestamp,n="initialState.sessionPath";if(!o&&process.env.ZIBBY_SESSION_PATH)try{let d=Je(String(process.env.ZIBBY_SESSION_PATH));d&&(o=d,n="ZIBBY_SESSION_PATH")}catch{}let a;if(o)a=String(o).split(/[/\\]/).filter(Boolean).pop(),s==null&&(s=Date.now());else{let d=process.env.ZIBBY_SESSION_ID&&String(process.env.ZIBBY_SESSION_ID).trim();if(d)a=d,n="ZIBBY_SESSION_ID";else{let l=e.sessionId!=null?String(e.sessionId).trim():"";l&&l!=="last"?(a=l,n="config.sessionId"):(a=$t(e),n="generated")}s=s??Date.now();let h=e.paths?.output||V;o=B(i,h,Be,a)}let c=!X(o);return c&&ze(o,{recursive:!0}),(c||n!=="initialState.sessionPath")&&yt({traceFrom:r,sessionId:a,sessionPath:o,idSource:n,mkdirFresh:c}),Ot({sessionPath:o,sessionId:a}),{sessionPath:o,sessionId:a,sessionTimestamp:s}}var Ke=class{constructor(e={}){this.nodes=new Map,this.edges=new Map,this.entryPoint=null,this.middleware=Array.isArray(e.middleware)?[...e.middleware]:[],e.nodeMiddleware&&this.middleware.push(e.nodeMiddleware),this.nodeTypeMap=new Map,this.conditionalCodeMap=new Map,this.stateSchema=e.stateSchema||null,this.nodePrompts=new Map,this.nodeOptions=new Map,this._invokeAgent=e.invokeAgent||null,this._compiledPrompts=new Map}setStateSchema(e){return this.stateSchema=e,this}getStateSchema(){return this.stateSchema}addNode(e,t,r={}){let o=t instanceof Y?t:new Y(t);return o.name=e,this.nodes.set(e,o),r.prompt&&this.nodePrompts.set(e,r.prompt),Object.keys(r).length>0&&this.nodeOptions.set(e,r),this}addConditionalNode(e,t){return this.nodes.set(e,new q({...t,name:e})),this}addEdge(e,t){return this.edges.set(e,t),this}setNodeType(e,t){return this.nodeTypeMap.set(e,t),this}addConditionalEdges(e,t,{labels:r}={}){return this.edges.set(e,{conditional:!0,routes:t,labels:r}),typeof t=="function"&&this.conditionalCodeMap.set(e,t.toString()),this}setEntryPoint(e){return this.entryPoint=e,this}use(e){return typeof e=="function"&&this.middleware.push(e),this}_composeMiddleware(e,t,r,o,s){let n=r;for(let a=e.length-1;a>=0;a--){let c=e[a],d=n;n=()=>c(t,d,o,s)}return n()}serialize(){let e=[],t={};for(let[s,n]of this.nodes){let a=this.nodeTypeMap.get(s)||s;e.push({id:s,type:a,data:{nodeType:a,label:s}});let c={};n._isCustomCode&&typeof n.execute=="function"&&(c.customCode=n.execute.toString());let d=this.nodePrompts.get(s);if(d&&(c.prompt=d),typeof n.customExecute=="function"&&(c.executeCode=n.customExecute.toString()),n.outputSchema)try{if(typeof n.outputSchema._def<"u"){let l=Ge(n.outputSchema,{target:"openApi3"});c.outputSchema={jsonSchema:l,variables:this._flattenJsonSchemaToVariables(l)}}else c.outputSchema={schema:n.outputSchema}}catch(l){console.warn(`[workflow] failed to convert schema for ${s}:`,l.message)}let h=(this.resolvedToolsMap||{})[s];h?.toolIds&&(c.tools=h.toolIds),Object.keys(c).length>0&&(t[s]=c)}let r=[];for(let[s,n]of this.edges)if(typeof n=="string")r.push({source:s,target:n});else if(n.conditional){let a=this.conditionalCodeMap.get(s)||n.routes.toString(),c=this._inferConditionalTargets(n.routes),d=n.labels||{};for(let h of c){let l={source:s,target:h,data:{conditionalCode:a}};d[h]&&(l.label=d[h]),r.push(l)}}let o=null;if(this.stateSchema)try{o=Ge(this.stateSchema,{target:"openApi3"})}catch{o=this.stateSchema}return{nodes:e,edges:r,nodeConfigs:t,stateSchema:o}}_inferConditionalTargets(e){let t=e.toString(),r=new Set,o=/return\s+['"]([^'"]+)['"]/g,s;for(;(s=o.exec(t))!==null;)r.add(s[1]);return[...r]}_flattenJsonSchemaToVariables(e,t=""){let r=e;if(e.$ref&&e.definitions){let o=e.$ref.replace("#/definitions/","");r=e.definitions[o]||e}return this._flattenSchema(r,t)}_flattenSchema(e,t=""){if(!e||typeof e!="object")return[];let r=[],o=e.properties||{},s=e.required||[];for(let[n,a]of Object.entries(o)){let c=t?`${t}.${n}`:n;r.push({path:c,type:a.type||"unknown",label:a.description||this._formatLabel(n),optional:!s.includes(n)}),a.type==="object"&&a.properties&&r.push(...this._flattenSchema(a,c)),a.type==="array"&&a.items?.type==="object"&&a.items.properties&&r.push(...this._flattenSchema(a.items,`${c}[]`))}return r}_formatLabel(e){return e.replace(/([A-Z])/g," $1").replace(/^./,t=>t.toUpperCase()).trim()}_summarizeNodeOutput(e,t){if(!t||typeof t!="object")return[];let r=[];t.success!==void 0&&r.push(`Result: ${t.success?"passed":"failed"}`);for(let[o,s]of Object.entries(t))if(!(o==="success"||o==="raw"||o==="nextNode")){if(typeof s=="string"&&s.length<=80)r.push(`${o}: ${s}`);else if(Array.isArray(s)){let n=s.length,a=s.filter(d=>d?.passed===!0).length,c=s.some(d=>d?.passed!==void 0);r.push(c?`${o}: ${a}/${n} passed${n-a?`, ${n-a} failed`:""}`:`${o}: ${n} items`)}if(r.length>=4)break}return r}async run(e,t={},r={}){if(!this.entryPoint)throw new Error("No entry point set for graph");let o=new AbortController;r.signal&&(r.signal.aborted?o.abort():r.signal.addEventListener("abort",()=>o.abort(),{once:!0}));let s=r.strategyAbortTimeoutMs??t.config?.strategyAbortTimeoutMs??5e3,n=t.cwd||process.cwd();_t({path:B(n,".env")});let a=t.config||{};if(!a||Object.keys(a).length===0)try{let f=B(n,".zibby.config.js");X(f)&&(a=(await import(f)).default||{})}catch{}process.env.EXECUTION_ID&&!a.agent?.strictMode&&(a.agent={...a.agent,strictMode:!0});let c=t.agentType;if(!c){let f=a?.agent;f?.provider?c=f.provider:f?.gemini?c="gemini":f?.claude?c="claude":f?.cursor?c="cursor":f?.codex?c="codex":c=process.env.AGENT_TYPE||"cursor"}let d=t.contextConfig||e?.config?.contextConfig||e?.config?.context||a?.context||{};if(this.stateSchema){let f=this.stateSchema.safeParse(t);if(!f.success){let b=f.error.issues.map($=>`${$.path.join(".")}: ${$.message}`);throw console.error("\u274C Initial state validation failed:"),b.forEach($=>console.error(` - ${$}`)),new Error(`State validation failed: ${b.join(", ")}`)}_.step("State validated against schema")}let h=Nt(),l=t.sessionPath||h;l||bt();let{sessionPath:m,sessionTimestamp:E,sessionId:k}=Tt({cwd:n,config:a,traceFrom:"WorkflowGraph.run",initialState:{sessionPath:l,sessionTimestamp:t.sessionTimestamp}});_.step(`Session ${k}`);let P=await Q.loadContext(t.specPath||"",n,d);Object.keys(P).length>0&&_.step(`Context loaded: ${Object.keys(P).join(", ")}`);let A=t.outputPath;!A&&t.specPath&&(e?.calculateOutputPath?A=e.calculateOutputPath(t.specPath):console.warn(`\u26A0\uFE0F outputPath not resolved (specPath=${t.specPath})`));let p=new K({...t,config:a,agentType:c,outputPath:A,sessionPath:m,sessionTimestamp:E,context:P,resolvedTools:this.resolvedToolsMap||{},_signal:o.signal}),I=new Map;try{await import("@zibby/skills")}catch{}let{getSkill:y}=await Promise.resolve().then(()=>(ce(),Me)),O=new Set;for(let[,f]of this.nodes)for(let b of f.config?.skills||[])O.add(b);for(let f of O){let b=y(f);if(typeof b?.middleware=="function")try{let $=await b.middleware();typeof $=="function"&&I.set(f,$)}catch{}}let u=this.entryPoint,R=[],me=a?.recursionLimit??100,Ve=0;try{for(;u&&u!=="END";){if(++Ve>me)throw new Error(`Workflow exceeded recursion limit (${me}) \u2014 likely a cyclic conditional route. Set config.recursionLimit if you need a higher cap.`);let b=B(m,Re),$=B(m,xe);if(X(b)){try{je(b)}catch{}o.abort()}if(X($)){try{je($)}catch{}o.abort(),ee("legacy-stop-file","Detected legacy `.zibby-studio-stop` file. Consumers should migrate to either `.zibby-stop` (renamed) or pass an AbortSignal to graph.run. The legacy filename will be removed in v2. Suppress with ZIBBY_NO_DEPRECATION_WARNINGS=1.")}if(o.signal.aborted)return console.warn(`
37
- \u{1F6D1} External stop requested \u2014 ending workflow.`),_.step("Workflow stopped externally"),{success:!0,state:p.getAll(),executionLog:R,stoppedExternally:!0,stoppedByStudio:!0};let Z=this.nodes.get(u);if(!Z)throw new Error(`Node '${u}' not found in graph`);let ge=JSON.stringify({sessionPath:m,sessionTimestamp:E,currentNode:u,createdAt:new Date().toISOString(),config:p.get("config")}),qe=B(m,D);Ue(qe,ge,"utf-8");let Se=p.get("config")?.paths?.output||V,Qe=B(n,Se,D);ze(B(n,Se),{recursive:!0});try{Ue(Qe,ge,"utf-8")}catch{}let _e=t.onPipelineProgress;if(typeof _e=="function")try{_e({cwd:n,sessionPath:m,sessionId:k,outputBase:p.get("config")?.paths?.output||V,currentNode:u})}catch{}let Xe=(this.resolvedToolsMap||{})[u]||null;p.set("_currentNodeTools",Xe);let et=p.get("nodeConfigs")||{};p.set("_currentNodeConfig",et[u]||{}),_.nodeStart(u);let we=Date.now(),j=this.nodePrompts.get(u);if(!this._invokeAgent){let T=await Promise.resolve().then(()=>(de(),ue));this._invokeAgent=T.invokeAgent}let tt=this._invokeAgent,ye=async(T,v,g={})=>{let N=tt(T,v,{...g,signal:o.signal});return N.catch(()=>{}),o.signal.aborted?N:Promise.race([N,new Promise((W,M)=>{let x=()=>{setTimeout(()=>{let F=new Error(`Strategy ignored AbortSignal \u2014 engine deadman fired after ${s}ms`);F.name="AbortError",M(F)},s)};o.signal.addEventListener("abort",x,{once:!0})})])},Ee={state:p,invokeAgent:async(T={},v={})=>{let g=v.prompt||"";if(j){let N=this._compiledPrompts.get(u);N||(N=wt.compile(j,{noEscape:!0}),this._compiledPrompts.set(u,N));try{g=N(T)}catch(W){throw console.error(`\u274C Template rendering failed for node '${u}':`,W.message),new Error(`Template rendering failed: ${W.message}`,{cause:W})}}else if(!g)throw new Error(`No prompt template configured for node '${u}' and no prompt provided in options`);return ye(g,{state:p.getAll(),images:v.images||[]},{model:v.model||p.get("model"),workspace:p.get("workspace"),schema:v.schema,...v,signal:o.signal})},_coreInvokeAgent:ye,agent:e,nodeId:u,promptTemplate:j,getPromptTemplate:()=>j,...p.getAll()};try{let T=(Z.config?.skills||[]).map(x=>I.get(x)).filter(Boolean),v=[...this.middleware,...T],g;v.length>0?g=await this._composeMiddleware(v,u,async()=>Z.execute(Ee,p),p.getAll(),p):g=await Z.execute(Ee,p);let N=Date.now()-we;if(R.push({node:u,success:g.success,duration:N,timestamp:new Date().toISOString()}),!g.success){if(String(g.error||"").includes("Stopped from Zibby Studio"))return ee("legacy-error-string",'Strategy returned the legacy `Stopped from Zibby Studio` error string. Strategies should reject with `Error.name === "AbortError"` instead. The string-match fallback will be removed in v2. Suppress with ZIBBY_NO_DEPRECATION_WARNINGS=1.'),_.step("Workflow stopped externally"),p.set("stoppedExternally",!0),p.set("stoppedByStudio",!0),{success:!0,state:p.getAll(),executionLog:R,stoppedExternally:!0,stoppedByStudio:!0};if(o.signal.aborted)return _.step("Workflow stopped externally"),{success:!0,state:p.getAll(),executionLog:R,stoppedExternally:!0,stoppedByStudio:!0};p.append("errors",{node:u,error:g.error});let F=Z.config?.retries||0,Ie=`${u}_retries`,G=p.getAll()[Ie]||0;if(G<F){_.stepInfo(`Retrying (attempt ${G+1}/${F})`),p.update({[Ie]:G+1,[`${u}_raw`]:g.raw});continue}throw _.nodeFailed(u,g.error,{duration:N}),new Error(`Node '${u}' failed after ${G} attempts: ${g.error}`)}p.update({[u]:g.output});let W=this._summarizeNodeOutput(u,g.output);_.nodeComplete(u,{duration:N,details:W});let M=this.edges.get(u);if(!M)u="END";else if(M.conditional){let x=M.routes(p.getAll());_.route(u,x),u=x}else u=M}catch(T){throw _.isInsideNode&&_.nodeFailed(u,T.message,{duration:Date.now()-we}),p.set("failed",!0),p.set("failedAt",u),T}}_.graphComplete();let f={success:!0,state:p.getAll(),executionLog:R};return e&&typeof e.onComplete=="function"&&await e.onComplete(f),f}finally{if(e&&typeof e.cleanup=="function")try{await e.cleanup()}catch(f){console.warn(`[workflow] agent.cleanup() failed: ${f.message}`)}}}};export{Ke as WorkflowGraph,bt as clearInheritedSessionEnvForFreshRun,$t as generateWorkflowSessionId,It as readPinnedSessionPathFromEnv,Nt as readStudioPinnedSessionPathFromEnv,Tt as resolveWorkflowSession,Et as shouldTrustInheritedSessionEnv,Ot as syncProcessEnvToSession};
36
+ ${h}`)}}function ge(){return process.env.ZIBBY_TRUST_SESSION_ENV==="1"||process.env.ZIBBY_TRUST_SESSION_ENV==="true"||process.env.ZIBBY_KEEP_SESSION_ENV==="1"||process.env.ZIBBY_KEEP_SESSION_ENV==="true"}function Se(){if(!(process.env.ZIBBY_PIN_SESSION_PATH==="1"||process.env.ZIBBY_PIN_SESSION_PATH==="true"))return;let t=process.env.ZIBBY_SESSION_PATH;if(!(t==null||String(t).trim()===""))try{return Zt(String(t).trim())}catch{return String(t).trim()}}function _e(){ge()||(delete process.env.ZIBBY_SESSION_PATH,delete process.env.ZIBBY_SESSION_ID)}function we({sessionPath:i,sessionId:t}){i&&typeof i=="string"&&(process.env.ZIBBY_SESSION_PATH=i),t!=null&&String(t).trim()!==""&&(process.env.ZIBBY_SESSION_ID=String(t).trim())}function ye(i={}){let t=xt.map(r=>process.env[r]).find(Boolean),e=Math.random().toString(36).slice(2,6),s=t||`${Date.now()}_${e}`,o=i.paths?.sessionPrefix;return o?`${o}_${s}`:s}function Ee({cwd:i=process.cwd(),config:t={},initialState:e={},traceFrom:s="resolveWorkflowSession"}={}){let o=e.sessionPath,r=e.sessionTimestamp,n="initialState.sessionPath";if(!o&&process.env.ZIBBY_SESSION_PATH)try{let d=Zt(String(process.env.ZIBBY_SESSION_PATH));d&&(o=d,n="ZIBBY_SESSION_PATH")}catch{}let a;if(o)a=String(o).split(/[/\\]/).filter(Boolean).pop(),r==null&&(r=Date.now());else{let d=process.env.ZIBBY_SESSION_ID&&String(process.env.ZIBBY_SESSION_ID).trim();if(d)a=d,n="ZIBBY_SESSION_ID";else{let l=t.sessionId!=null?String(t.sessionId).trim():"";l&&l!=="last"?(a=l,n="config.sessionId"):(a=ye(t),n="generated")}r=r??Date.now();let h=t.paths?.output||V;o=R(i,h,Tt,a)}let c=!pt(o);return c&&Yt(o,{recursive:!0}),(c||n!=="initialState.sessionPath")&&me({traceFrom:s,sessionId:a,sessionPath:o,idSource:n,mkdirFresh:c}),we({sessionPath:o,sessionId:a}),{sessionPath:o,sessionId:a,sessionTimestamp:r}}var jt=class{constructor(t={}){this.nodes=new Map,this.edges=new Map,this.entryPoint=null,this.middleware=Array.isArray(t.middleware)?[...t.middleware]:[],t.nodeMiddleware&&this.middleware.push(t.nodeMiddleware),this.nodeTypeMap=new Map,this.conditionalCodeMap=new Map,this.stateSchema=t.stateSchema||null,this.nodePrompts=new Map,this.nodeOptions=new Map,this._invokeAgent=t.invokeAgent||null,this._compiledPrompts=new Map}setStateSchema(t){return this.stateSchema=t,this}getStateSchema(){return this.stateSchema}addNode(t,e,s={}){let o=e instanceof F?e:new F(e);return o.name=t,this.nodes.set(t,o),s.prompt&&this.nodePrompts.set(t,s.prompt),Object.keys(s).length>0&&this.nodeOptions.set(t,s),this}addConditionalNode(t,e){return this.nodes.set(t,new z({...e,name:t})),this}addEdge(t,e){return this.edges.set(t,e),this}setNodeType(t,e){return this.nodeTypeMap.set(t,e),this}addConditionalEdges(t,e,{labels:s}={}){return this.edges.set(t,{conditional:!0,routes:e,labels:s}),typeof e=="function"&&this.conditionalCodeMap.set(t,e.toString()),this}setEntryPoint(t){return this.entryPoint=t,this}use(t){return typeof t=="function"&&this.middleware.push(t),this}_composeMiddleware(t,e,s,o,r){let n=s;for(let a=t.length-1;a>=0;a--){let c=t[a],d=n;n=()=>c(e,d,o,r)}return n()}serialize(){let t=[],e={};for(let[r,n]of this.nodes){let a=this.nodeTypeMap.get(r)||r;t.push({id:r,type:a,data:{nodeType:a,label:r}});let c={};n._isCustomCode&&typeof n.execute=="function"&&(c.customCode=n.execute.toString());let d=this.nodePrompts.get(r);if(d&&(c.prompt=d),typeof n.customExecute=="function"&&(c.executeCode=n.customExecute.toString()),n.outputSchema)try{if(typeof n.outputSchema._def<"u"){let l=Ft(n.outputSchema,{target:"openApi3"});c.outputSchema={jsonSchema:l,variables:this._flattenJsonSchemaToVariables(l)}}else c.outputSchema={schema:n.outputSchema}}catch(l){console.warn(`[workflow] failed to convert schema for ${r}:`,l.message)}let h=(this.resolvedToolsMap||{})[r];h?.toolIds&&(c.tools=h.toolIds),Object.keys(c).length>0&&(e[r]=c)}let s=[];for(let[r,n]of this.edges)if(typeof n=="string")s.push({source:r,target:n});else if(n.conditional){let a=this.conditionalCodeMap.get(r)||n.routes.toString(),c=this._inferConditionalTargets(n.routes),d=n.labels||{};for(let h of c){let l={source:r,target:h,data:{conditionalCode:a}};d[h]&&(l.label=d[h]),s.push(l)}}let o=null;if(this.stateSchema)try{o=Ft(this.stateSchema,{target:"openApi3"})}catch{o=this.stateSchema}return{nodes:t,edges:s,nodeConfigs:e,stateSchema:o}}_inferConditionalTargets(t){let e=t.toString(),s=new Set,o=/return\s+['"]([^'"]+)['"]/g,r;for(;(r=o.exec(e))!==null;)s.add(r[1]);return[...s]}_flattenJsonSchemaToVariables(t,e=""){let s=t;if(t.$ref&&t.definitions){let o=t.$ref.replace("#/definitions/","");s=t.definitions[o]||t}return this._flattenSchema(s,e)}_flattenSchema(t,e=""){if(!t||typeof t!="object")return[];let s=[],o=t.properties||{},r=t.required||[];for(let[n,a]of Object.entries(o)){let c=e?`${e}.${n}`:n;s.push({path:c,type:a.type||"unknown",label:a.description||this._formatLabel(n),optional:!r.includes(n)}),a.type==="object"&&a.properties&&s.push(...this._flattenSchema(a,c)),a.type==="array"&&a.items?.type==="object"&&a.items.properties&&s.push(...this._flattenSchema(a.items,`${c}[]`))}return s}_formatLabel(t){return t.replace(/([A-Z])/g," $1").replace(/^./,e=>e.toUpperCase()).trim()}_summarizeNodeOutput(t,e){if(!e||typeof e!="object")return[];let s=[];e.success!==void 0&&s.push(`Result: ${e.success?"passed":"failed"}`);for(let[o,r]of Object.entries(e))if(!(o==="success"||o==="raw"||o==="nextNode")){if(typeof r=="string"&&r.length<=80)s.push(`${o}: ${r}`);else if(Array.isArray(r)){let n=r.length,a=r.filter(d=>d?.passed===!0).length,c=r.some(d=>d?.passed!==void 0);s.push(c?`${o}: ${a}/${n} passed${n-a?`, ${n-a} failed`:""}`:`${o}: ${n} items`)}if(s.length>=4)break}return s}async run(t,e={},s={}){if(!this.entryPoint)throw new Error("No entry point set for graph");let o=new AbortController;s.signal&&(s.signal.aborted?o.abort():s.signal.addEventListener("abort",()=>o.abort(),{once:!0}));let r=s.strategyAbortTimeoutMs??e.config?.strategyAbortTimeoutMs??5e3,n=e.cwd||process.cwd();fe({path:R(n,".env")});let a=e.config||{};if(!a||Object.keys(a).length===0)try{let f=R(n,".zibby.config.js");pt(f)&&(a=(await import(f)).default||{})}catch{}process.env.EXECUTION_ID&&!a.agent?.strictMode&&(a.agent={...a.agent,strictMode:!0});let c=e.agentType;if(!c){let f=a?.agent;f?.provider?c=f.provider:f?.gemini?c="gemini":f?.claude?c="claude":f?.cursor?c="cursor":f?.codex?c="codex":c=process.env.AGENT_TYPE||"cursor"}let d=e.contextConfig||t?.config?.contextConfig||t?.config?.context||a?.context||{};if(this.stateSchema){let f=this.stateSchema.safeParse(e);if(!f.success){let N=f.error.issues.map(I=>`${I.path.join(".")}: ${I.message}`);throw console.error("\u274C Initial state validation failed:"),N.forEach(I=>console.error(` - ${I}`)),new Error(`State validation failed: ${N.join(", ")}`)}w.step("State validated against schema")}let h=Se(),l=e.sessionPath||h;l||_e();let{sessionPath:m,sessionTimestamp:E,sessionId:x}=Ee({cwd:n,config:a,traceFrom:"WorkflowGraph.run",initialState:{sessionPath:l,sessionTimestamp:e.sessionTimestamp}});w.step(`Session ${x}`);let P=await q.loadContext(e.specPath||"",n,d);Object.keys(P).length>0&&w.step(`Context loaded: ${Object.keys(P).join(", ")}`);let T=e.outputPath;!T&&e.specPath&&(t?.calculateOutputPath?T=t.calculateOutputPath(e.specPath):console.warn(`\u26A0\uFE0F outputPath not resolved (specPath=${e.specPath})`));let p=new H({...e,config:a,agentType:c,outputPath:T,sessionPath:m,sessionTimestamp:E,context:P,resolvedTools:this.resolvedToolsMap||{},_signal:o.signal}),$=new Map;try{await import("@zibby/skills")}catch{}let{getSkill:y}=await Promise.resolve().then(()=>(nt(),Ct)),O=new Set;for(let[,f]of this.nodes)for(let N of f.config?.skills||[])O.add(N);for(let f of O){let N=y(f);if(typeof N?.middleware=="function")try{let I=await N.middleware();typeof I=="function"&&$.set(f,I)}catch{}}let u=this.entryPoint,W=[],ft=a?.recursionLimit??100,Ut=0;try{for(;u&&u!=="END";){if(++Ut>ft)throw new Error(`Workflow exceeded recursion limit (${ft}) \u2014 likely a cyclic conditional route. Set config.recursionLimit if you need a higher cap.`);let N=R(m,At);if(pt(N)){try{pe(N)}catch{}o.abort()}if(o.signal.aborted)return console.warn(`
37
+ \u{1F6D1} External stop requested \u2014 ending workflow.`),w.step("Workflow stopped externally"),{success:!0,state:p.getAll(),executionLog:W,stoppedExternally:!0};let I=this.nodes.get(u);if(!I)throw new Error(`Node '${u}' not found in graph`);let ht=JSON.stringify({sessionPath:m,sessionTimestamp:E,currentNode:u,createdAt:new Date().toISOString(),config:p.get("config")}),Gt=R(m,L);Dt(Gt,ht,"utf-8");let mt=p.get("config")?.paths?.output||V,Ht=R(n,mt,L);Yt(R(n,mt),{recursive:!0});try{Dt(Ht,ht,"utf-8")}catch{}let gt=e.onPipelineProgress;if(typeof gt=="function")try{gt({cwd:n,sessionPath:m,sessionId:x,outputBase:p.get("config")?.paths?.output||V,currentNode:u})}catch{}let Jt=(this.resolvedToolsMap||{})[u]||null;p.set("_currentNodeTools",Jt);let Kt=p.get("nodeConfigs")||{};p.set("_currentNodeConfig",Kt[u]||{}),w.nodeStart(u);let St=Date.now(),Z=this.nodePrompts.get(u);if(!this._invokeAgent){let k=await Promise.resolve().then(()=>(ct(),at));this._invokeAgent=k.invokeAgent}let Vt=this._invokeAgent,_t=async(k,v,S={})=>{let b=Vt(k,v,{...S,signal:o.signal});return b.catch(()=>{}),o.signal.aborted?b:Promise.race([b,new Promise((B,M)=>{let A=()=>{setTimeout(()=>{let j=new Error(`Strategy ignored AbortSignal \u2014 engine deadman fired after ${r}ms`);j.name="AbortError",M(j)},r)};o.signal.addEventListener("abort",A,{once:!0})})])},wt={state:p,invokeAgent:async(k={},v={})=>{let S=v.prompt||"";if(Z){let b=this._compiledPrompts.get(u);b||(b=he.compile(Z,{noEscape:!0}),this._compiledPrompts.set(u,b));try{S=b(k)}catch(B){throw console.error(`\u274C Template rendering failed for node '${u}':`,B.message),new Error(`Template rendering failed: ${B.message}`,{cause:B})}}else if(!S)throw new Error(`No prompt template configured for node '${u}' and no prompt provided in options`);return _t(S,{state:p.getAll(),images:v.images||[]},{model:v.model||p.get("model"),workspace:p.get("workspace"),schema:v.schema,...v,signal:o.signal})},_coreInvokeAgent:_t,agent:t,nodeId:u,promptTemplate:Z,getPromptTemplate:()=>Z,...p.getAll()};try{let k=(I.config?.skills||[]).map(A=>$.get(A)).filter(Boolean),v=[...this.middleware,...k],S;v.length>0?S=await this._composeMiddleware(v,u,async()=>I.execute(wt,p),p.getAll(),p):S=await I.execute(wt,p);let b=Date.now()-St;if(W.push({node:u,success:S.success,duration:b,timestamp:new Date().toISOString()}),!S.success){if(o.signal.aborted)return w.step("Workflow stopped externally"),{success:!0,state:p.getAll(),executionLog:W,stoppedExternally:!0};p.append("errors",{node:u,error:S.error});let A=I.config?.retries||0,j=`${u}_retries`,U=p.getAll()[j]||0;if(U<A){w.stepInfo(`Retrying (attempt ${U+1}/${A})`),p.update({[j]:U+1,[`${u}_raw`]:S.raw});continue}throw w.nodeFailed(u,S.error,{duration:b}),new Error(`Node '${u}' failed after ${U} attempts: ${S.error}`)}p.update({[u]:S.output});let B=this._summarizeNodeOutput(u,S.output);w.nodeComplete(u,{duration:b,details:B});let M=this.edges.get(u);if(!M)u="END";else if(M.conditional){let A=M.routes(p.getAll());w.route(u,A),u=A}else u=M}catch(k){throw w.isInsideNode&&w.nodeFailed(u,k.message,{duration:Date.now()-St}),p.set("failed",!0),p.set("failedAt",u),k}}w.graphComplete();let f={success:!0,state:p.getAll(),executionLog:W};return t&&typeof t.onComplete=="function"&&await t.onComplete(f),f}finally{if(t&&typeof t.cleanup=="function")try{await t.cleanup()}catch(f){console.warn(`[workflow] agent.cleanup() failed: ${f.message}`)}}}};export{jt as WorkflowGraph,_e as clearInheritedSessionEnvForFreshRun,ye as generateWorkflowSessionId,Se as readPinnedSessionPathFromEnv,Ee as resolveWorkflowSession,ge as shouldTrustInheritedSessionEnv,we as syncProcessEnvToSession};
package/dist/index.d.ts CHANGED
@@ -2,7 +2,7 @@ export { WorkflowState } from "./state.js";
2
2
  export { ContextLoader } from "./context-loader.js";
3
3
  export { AgentStrategy } from "./agents/base.js";
4
4
  export { setLogger } from "./logger.js";
5
- export { WorkflowGraph, generateWorkflowSessionId, resolveWorkflowSession, shouldTrustInheritedSessionEnv, readPinnedSessionPathFromEnv, readStudioPinnedSessionPathFromEnv, clearInheritedSessionEnvForFreshRun, syncProcessEnvToSession } from "./graph.js";
5
+ export { WorkflowGraph, generateWorkflowSessionId, resolveWorkflowSession, shouldTrustInheritedSessionEnv, readPinnedSessionPathFromEnv, clearInheritedSessionEnvForFreshRun, syncProcessEnvToSession } from "./graph.js";
6
6
  export { Node, ConditionalNode } from "./node.js";
7
7
  export { OutputParser, SchemaTypes } from "./output-parser.js";
8
8
  export { compileGraph, validateGraphConfig, extractSteps, CompilationError } from "./graph-compiler.js";
@@ -11,5 +11,5 @@ export { registerSkill, getSkill, hasSkill, getAllSkills, listSkillIds, clearSki
11
11
  export { resolveNodeTools, getResolvedToolDefinitions, NODE_DEFAULT_TOOLS } from "./tool-resolver.js";
12
12
  export { registerStrategy, listStrategies, getAgentStrategy, invokeAgent } from "./strategy-registry.js";
13
13
  export { generateWorkflowCode, generateNodeConfigsJson } from "./code-generator.js";
14
- export { SKILLS, DEFAULT_OUTPUT_BASE, SESSIONS_DIR, SESSION_INFO_FILE, STOP_REQUEST_FILE, STUDIO_STOP_REQUEST_FILE, RESULT_FILE, RAW_OUTPUT_FILE, EVENTS_FILE, CI_ENV_VARS } from "./constants.js";
14
+ export { SKILLS, DEFAULT_OUTPUT_BASE, SESSIONS_DIR, SESSION_INFO_FILE, STOP_REQUEST_FILE, RESULT_FILE, RAW_OUTPUT_FILE, EVENTS_FILE, CI_ENV_VARS } from "./constants.js";
15
15
  export { timeline, Timeline, WORKFLOW_GRAPH_LOG_MARKER_PREFIX } from "./timeline.js";
package/dist/index.js CHANGED
@@ -1,40 +1,40 @@
1
- var Pt=Object.defineProperty;var fe=(o=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(o,{get:(e,t)=>(typeof require<"u"?require:e)[t]}):o)(function(o){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+o+'" is not supported')});var ie=(o,e)=>()=>(o&&(e=o(o=0)),e);var je=(o,e)=>{for(var t in e)Pt(o,t,{get:e[t],enumerable:!0})};function Wt(o){K.impl={...Ze,...o}}var Ue,Ze,K,y,j=ie(()=>{Ue=()=>{},Ze={debug:Ue,info:Ue,warn:(...o)=>console.warn("[workflow]",...o),error:(...o)=>console.error("[workflow]",...o)},K={impl:Ze};y={debug:(...o)=>K.impl.debug?.(...o),info:(...o)=>K.impl.info?.(...o),warn:(...o)=>K.impl.warn?.(...o),error:(...o)=>K.impl.error?.(...o)}});var Ee,Ie=ie(()=>{Ee=class{constructor(e,t,s=0){this.name=e,this.description=t,this.priority=s}async invoke(e,t={}){throw new Error(`${this.constructor.name}.invoke() must be implemented`)}canHandle(e){throw new Error(`${this.constructor.name}.canHandle() must be implemented`)}getName(){return this.name}getDescription(){return this.description}getPriority(){return this.priority}}});var rt={};je(rt,{clearSkills:()=>ot,getAllSkills:()=>et,getSkill:()=>Z,hasSkill:()=>Xe,listSkillIds:()=>tt,registerSkill:()=>Qe});function Qe(o){if(!o||typeof o.id!="string")throw new Error("Skill definition must include a string id");U.set(o.id,Object.freeze({...o}))}function Z(o){return U.get(o)||null}function Xe(o){return U.has(o)}function et(){return new Map(U)}function tt(){return Array.from(U.keys())}function ot(){U.clear()}var Ne,U,Q=ie(()=>{Ne=Symbol.for("@zibby/agent-workflow.skills");globalThis[Ne]||(globalThis[Ne]=new Map);U=globalThis[Ne]});var X={};je(X,{getAgentStrategy:()=>$e,invokeAgent:()=>it,listStrategies:()=>nt,registerStrategy:()=>st});function st(o){if(!o||typeof o.getName!="function"||typeof o.invoke!="function")throw new Error("strategy must implement getName() and invoke() (AgentStrategy shape)");let e=D.findIndex(t=>t.getName()===o.getName());e>=0?D[e]=o:D.push(o)}function nt(){return D.map(o=>o.getName())}function $e(o={}){let{state:e={},preferredAgent:t=null}=o,s=t||e.agentType||process.env.AGENT_TYPE;if(!s){let i=D.map(r=>r.getName()).join(", ")||"none registered";throw new Error(`No agent specified. Set agentType in state or AGENT_TYPE env var. Available: ${i}`)}y.debug(`[workflow] agent selection: requested=${s}`);let n=D.find(i=>i.getName()===s);if(!n){let i=D.map(r=>r.getName()).join(", ")||"none registered";throw new Error(`Unknown agent '${s}'. Available: ${i}`)}if(!n.canHandle(o))throw new Error(`Agent '${s}' is not available in this environment. Check credentials/environment.`);return y.debug(`[workflow] using agent: ${n.getName()}`),n}async function it(o,e={},t={}){let s=$e(e),n=e.state?.config||t.config||{},i=n.models||{},r=t.nodeName&&i[t.nodeName]||null,a=i.default||null,c=n.agent?.[s.name]?.model||null,l=r||a||c||t.model||null,h={...t,model:l,workspace:e.state?.workspace||t.workspace,schema:t.schema||e.schema,images:t.images||e.images||[],skills:t.skills||e.skills||[],config:n},u=o,d=h.skills||[];if(d.length>0&&!t.skipPromptFragments){let g=d.map(S=>{let w=Z(S)?.promptFragment;return typeof w=="function"?w():w}).filter(Boolean);g.length>0&&(u+=`
1
+ var kt=Object.defineProperty;var de=(o=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(o,{get:(e,t)=>(typeof require<"u"?require:e)[t]}):o)(function(o){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+o+'" is not supported')});var ne=(o,e)=>()=>(o&&(e=o(o=0)),e);var Fe=(o,e)=>{for(var t in e)kt(o,t,{get:e[t],enumerable:!0})};function At(o){K.impl={...De,...o}}var Me,De,K,_,j=ne(()=>{Me=()=>{},De={debug:Me,info:Me,warn:(...o)=>console.warn("[workflow]",...o),error:(...o)=>console.error("[workflow]",...o)},K={impl:De};_={debug:(...o)=>K.impl.debug?.(...o),info:(...o)=>K.impl.info?.(...o),warn:(...o)=>K.impl.warn?.(...o),error:(...o)=>K.impl.error?.(...o)}});var Se,we=ne(()=>{Se=class{constructor(e,t,s=0){this.name=e,this.description=t,this.priority=s}async invoke(e,t={}){throw new Error(`${this.constructor.name}.invoke() must be implemented`)}canHandle(e){throw new Error(`${this.constructor.name}.canHandle() must be implemented`)}getName(){return this.name}getDescription(){return this.description}getPriority(){return this.priority}}});var et={};Fe(et,{clearSkills:()=>Qe,getAllSkills:()=>qe,getSkill:()=>U,hasSkill:()=>ze,listSkillIds:()=>Xe,registerSkill:()=>Ve});function Ve(o){if(!o||typeof o.id!="string")throw new Error("Skill definition must include a string id");G.set(o.id,Object.freeze({...o}))}function U(o){return G.get(o)||null}function ze(o){return G.has(o)}function qe(){return new Map(G)}function Xe(){return Array.from(G.keys())}function Qe(){G.clear()}var ye,G,q=ne(()=>{ye=Symbol.for("@zibby/agent-workflow.skills");globalThis[ye]||(globalThis[ye]=new Map);G=globalThis[ye]});var X={};Fe(X,{getAgentStrategy:()=>Ee,invokeAgent:()=>rt,listStrategies:()=>ot,registerStrategy:()=>tt});function tt(o){if(!o||typeof o.getName!="function"||typeof o.invoke!="function")throw new Error("strategy must implement getName() and invoke() (AgentStrategy shape)");let e=W.findIndex(t=>t.getName()===o.getName());e>=0?W[e]=o:W.push(o)}function ot(){return W.map(o=>o.getName())}function Ee(o={}){let{state:e={},preferredAgent:t=null}=o,s=t||e.agentType||process.env.AGENT_TYPE;if(!s){let i=W.map(r=>r.getName()).join(", ")||"none registered";throw new Error(`No agent specified. Set agentType in state or AGENT_TYPE env var. Available: ${i}`)}_.debug(`[workflow] agent selection: requested=${s}`);let n=W.find(i=>i.getName()===s);if(!n){let i=W.map(r=>r.getName()).join(", ")||"none registered";throw new Error(`Unknown agent '${s}'. Available: ${i}`)}if(!n.canHandle(o))throw new Error(`Agent '${s}' is not available in this environment. Check credentials/environment.`);return _.debug(`[workflow] using agent: ${n.getName()}`),n}async function rt(o,e={},t={}){let s=Ee(e),n=e.state?.config||t.config||{},i=n.models||{},r=t.nodeName&&i[t.nodeName]||null,a=i.default||null,c=n.agent?.[s.name]?.model||null,l=r||a||c||t.model||null,h={...t,model:l,workspace:e.state?.workspace||t.workspace,schema:t.schema||e.schema,images:t.images||e.images||[],skills:t.skills||e.skills||[],config:n},u=o,d=h.skills||[];if(d.length>0&&!t.skipPromptFragments){let m=d.map(S=>{let w=U(S)?.promptFragment;return typeof w=="function"?w():w}).filter(Boolean);m.length>0&&(u+=`
2
2
 
3
- ${g.join(`
3
+ ${m.join(`
4
4
 
5
- `)}`)}let m=e.state?._currentNodeConfig?.extraPromptInstructions?.trim();return m&&(u+=`
5
+ `)}`)}let g=e.state?._currentNodeConfig?.extraPromptInstructions?.trim();return g&&(u+=`
6
6
 
7
7
  \u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501
8
8
  PRIORITY OVERRIDE \u2014 THE FOLLOWING INSTRUCTIONS TAKE PRECEDENCE OVER ALL PREVIOUS CONTENT
9
9
  \u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501
10
10
 
11
- ${m}
12
- `),y.debug(`[workflow] prompt length: ${u.length} chars`),s.invoke(u,h)}var Te,D,Y=ie(()=>{Ie();j();Q();Te=Symbol.for("@zibby/agent-workflow.strategies");globalThis[Te]||(globalThis[Te]=[]);D=globalThis[Te]});var Rt=new Set(["__proto__","constructor","prototype"]);function he(o){if(Rt.has(o))throw new Error(`Invalid state key: "${o}"`)}var z=class{constructor(e={}){this._state=Object.create(null),Object.assign(this._state,{messages:[],errors:[],artifacts:{},metadata:{},...e}),this._history=[]}get(e){return this._state[e]}set(e,t){he(e),this._history.push({...this._state}),this._state[e]=t}update(e){let t=Object.getOwnPropertyNames(e);for(let s of t)he(s);this._history.push({...this._state});for(let s of t)this._state[s]=e[s]}append(e,t){he(e),this._history.push({...this._state}),Array.isArray(this._state[e])||(this._state[e]=[]),this._state[e].push(t)}getAll(){return{...this._state}}rollback(){this._history.length>0&&(this._state=this._history.pop())}};var J=class{constructor(e){this.schema=e}parse(e){let t=e.match(/```json\s*([\s\S]*?)\s*```/);if(t)return this.validate(JSON.parse(t[1]));let s=[e.match(/\{[\s\S]*?\}/),e.match(/\{[\s\S]*\}/)].filter(Boolean).map(n=>n[0]);for(let n of s)try{return this.validate(JSON.parse(n))}catch(i){if(!(i instanceof SyntaxError))throw i}return this.validate({result:e.trim()})}validate(e){let t=[];for(let[s,n]of Object.entries(this.schema)){if(n.required&&!(s in e)&&t.push(`Missing required field: ${s}`),s in e&&n.type){let i=typeof e[s];i!==n.type&&t.push(`Field '${s}' expected ${n.type}, got ${i}`)}if(n.validate&&s in e){let i=n.validate(e[s]);i&&t.push(`Field '${s}': ${i}`)}}if(t.length>0)throw new Error(`Output validation failed:
11
+ ${g}
12
+ `),_.debug(`[workflow] prompt length: ${u.length} chars`),s.invoke(u,h)}var _e,W,Y=ne(()=>{we();j();q();_e=Symbol.for("@zibby/agent-workflow.strategies");globalThis[_e]||(globalThis[_e]=[]);W=globalThis[_e]});var xt=new Set(["__proto__","constructor","prototype"]);function ue(o){if(xt.has(o))throw new Error(`Invalid state key: "${o}"`)}var J=class{constructor(e={}){this._state=Object.create(null),Object.assign(this._state,{messages:[],errors:[],artifacts:{},metadata:{},...e}),this._history=[]}get(e){return this._state[e]}set(e,t){ue(e),this._history.push({...this._state}),this._state[e]=t}update(e){let t=Object.getOwnPropertyNames(e);for(let s of t)ue(s);this._history.push({...this._state});for(let s of t)this._state[s]=e[s]}append(e,t){ue(e),this._history.push({...this._state}),Array.isArray(this._state[e])||(this._state[e]=[]),this._state[e].push(t)}getAll(){return{...this._state}}rollback(){this._history.length>0&&(this._state=this._history.pop())}};var H=class{constructor(e){this.schema=e}parse(e){let t=e.match(/```json\s*([\s\S]*?)\s*```/);if(t)return this.validate(JSON.parse(t[1]));let s=[e.match(/\{[\s\S]*?\}/),e.match(/\{[\s\S]*\}/)].filter(Boolean).map(n=>n[0]);for(let n of s)try{return this.validate(JSON.parse(n))}catch(i){if(!(i instanceof SyntaxError))throw i}return this.validate({result:e.trim()})}validate(e){let t=[];for(let[s,n]of Object.entries(this.schema)){if(n.required&&!(s in e)&&t.push(`Missing required field: ${s}`),s in e&&n.type){let i=typeof e[s];i!==n.type&&t.push(`Field '${s}' expected ${n.type}, got ${i}`)}if(n.validate&&s in e){let i=n.validate(e[s]);i&&t.push(`Field '${s}': ${i}`)}}if(t.length>0)throw new Error(`Output validation failed:
13
13
  ${t.join(`
14
- `)}`);return e}},Bt={string:(o=!0)=>({type:"string",required:o}),number:(o=!0)=>({type:"number",required:o}),boolean:(o=!0)=>({type:"boolean",required:o}),array:(o=!0)=>({type:"object",required:o,validate:e=>Array.isArray(e)?null:"must be an array"}),enum:(o,e=!0)=>({type:"string",required:e,validate:t=>o.includes(t)?null:`must be one of: ${o.join(", ")}`})};j();import{writeFileSync as ve,readFileSync as at,existsSync as ct,mkdirSync as Ut}from"node:fs";import{join as Oe,dirname as Zt}from"node:path";import T from"chalk";var qe="__WORKFLOW_GRAPH_LOG__",V=T.gray("\u2502"),Dt=T.gray("\u250C"),Ye=T.gray("\u2514"),ge=T.green("\u25C6"),Ge=T.hex("#c084fc")("\u25C6"),He=T.hex("#2dd4bf")("\u25C6"),me=T.red("\u25C6"),ze=`${V} `,Je=2;function Ke(o){return o<1e3?`${o}ms`:`${(o/1e3).toFixed(1)}s`}function Ve(o,e){return(t,s,n)=>{if(typeof t!="string")return o(t,s,n);let i=process.stdout.columns||120,r="";for(let a=0;a<t.length;a++){let c=t[a];e.lineStart&&(r+=ze,e.col=Je,e.lineStart=!1),c===`
14
+ `)}`);return e}},bt={string:(o=!0)=>({type:"string",required:o}),number:(o=!0)=>({type:"number",required:o}),boolean:(o=!0)=>({type:"boolean",required:o}),array:(o=!0)=>({type:"object",required:o,validate:e=>Array.isArray(e)?null:"must be an array"}),enum:(o,e=!0)=>({type:"string",required:e,validate:t=>o.includes(t)?null:`must be one of: ${o.join(", ")}`})};j();import{writeFileSync as $e,readFileSync as st,existsSync as nt,mkdirSync as Lt}from"node:fs";import{join as Te,dirname as Bt}from"node:path";import $ from"chalk";var Ke="__WORKFLOW_GRAPH_LOG__",V=$.gray("\u2502"),Ot=$.gray("\u250C"),je=$.gray("\u2514"),pe=$.green("\u25C6"),Ge=$.hex("#c084fc")("\u25C6"),Ue=$.hex("#2dd4bf")("\u25C6"),fe=$.red("\u25C6"),Ye=`${V} `,Ze=2;function Je(o){return o<1e3?`${o}ms`:`${(o/1e3).toFixed(1)}s`}function He(o,e){return(t,s,n)=>{if(typeof t!="string")return o(t,s,n);let i=process.stdout.columns||120,r="";for(let a=0;a<t.length;a++){let c=t[a];e.lineStart&&(r+=Ye,e.col=Ze,e.lineStart=!1),c===`
15
15
  `?(r+=c,e.lineStart=!0,e.col=0,e.inEsc=!1):c==="\x1B"?(e.inEsc=!0,r+=c):e.inEsc?(r+=c,(c>="A"&&c<="Z"||c>="a"&&c<="z")&&(e.inEsc=!1)):(e.col++,r+=c,e.col>=i&&(r+=`
16
- ${ze}`,e.col=Je))}return o(r,s,n)}}var ae=class{constructor(){this._currentNode=null,this._origStdoutWrite=null,this._origStderrWrite=null;let e=String(process.env.ZIBBY_EMIT_GRAPH_MARKERS||"").trim()==="1"||String(process.env.ZIBBY_WORKFLOW_GRAPH_LOG_MARKERS||"").trim()==="1",t=!e&&String(process.env.ZIBBY_RUN_SOURCE||"").trim().toLowerCase()==="studio";t&&process.env.ZIBBY_NO_DEPRECATION_WARNINGS!=="1"&&console.warn("[zibby/agent-workflow] `ZIBBY_RUN_SOURCE=studio` env var is deprecated for graph-marker emission. Set `ZIBBY_EMIT_GRAPH_MARKERS=1` instead. The Studio-specific value will be ignored in v2. Suppress with ZIBBY_NO_DEPRECATION_WARNINGS=1."),this._emitWorkflowGraphMarkers=e||t}get isInsideNode(){return this._currentNode!==null}_startIntercepting(){this._origStdoutWrite=process.stdout.write.bind(process.stdout),this._origStderrWrite=process.stderr.write.bind(process.stderr);let e={lineStart:!0,col:0,inEsc:!1},t={lineStart:!0,col:0,inEsc:!1};this._outState=e,this._errState=t,process.stdout.write=Ve(this._origStdoutWrite,e),process.stderr.write=Ve(this._origStderrWrite,t)}_stopIntercepting(){this._origStdoutWrite&&(this._outState&&!this._outState.lineStart&&this._origStdoutWrite(`
16
+ ${Ye}`,e.col=Ze))}return o(r,s,n)}}var ie=class{constructor(){this._currentNode=null,this._origStdoutWrite=null,this._origStderrWrite=null,this._emitWorkflowGraphMarkers=String(process.env.ZIBBY_EMIT_GRAPH_MARKERS||"").trim()==="1"||String(process.env.ZIBBY_WORKFLOW_GRAPH_LOG_MARKERS||"").trim()==="1"}get isInsideNode(){return this._currentNode!==null}_startIntercepting(){this._origStdoutWrite=process.stdout.write.bind(process.stdout),this._origStderrWrite=process.stderr.write.bind(process.stderr);let e={lineStart:!0,col:0,inEsc:!1},t={lineStart:!0,col:0,inEsc:!1};this._outState=e,this._errState=t,process.stdout.write=He(this._origStdoutWrite,e),process.stderr.write=He(this._origStderrWrite,t)}_stopIntercepting(){this._origStdoutWrite&&(this._outState&&!this._outState.lineStart&&this._origStdoutWrite(`
17
17
  `),process.stdout.write=this._origStdoutWrite),this._origStderrWrite&&(this._errState&&!this._errState.lineStart&&this._origStderrWrite(`
18
18
  `),process.stderr.write=this._origStderrWrite),this._origStdoutWrite=null,this._origStderrWrite=null}_rawWrite(e){(this._origStdoutWrite||process.stdout.write.bind(process.stdout))(`${e}
19
- `)}_emitGraphLogMarker(e){if(!this._emitWorkflowGraphMarkers)return;let t=`${qe}${JSON.stringify(e)}
19
+ `)}_emitGraphLogMarker(e){if(!this._emitWorkflowGraphMarkers)return;let t=`${Ke}${JSON.stringify(e)}
20
20
  `;this._origStdoutWrite?this._origStdoutWrite(t):process.stdout.write(t)}_writeDot(e,t){this._origStdoutWrite?(this._outState&&!this._outState.lineStart&&(this._origStdoutWrite(`
21
21
  `),this._outState.lineStart=!0,this._outState.col=0),this._origStdoutWrite(`${e} ${t}
22
22
  `)):process.stdout.write.bind(process.stdout)(`${e} ${t}
23
- `)}step(e){this._origStdoutWrite?this._writeDot(ge,e):process.stdout.write.bind(process.stdout)(`${V} ${ge} ${e}
23
+ `)}step(e){this._origStdoutWrite?this._writeDot(pe,e):process.stdout.write.bind(process.stdout)(`${V} ${pe} ${e}
24
24
  `)}stepInfo(e){this.step(e)}stepTool(e){this._origStdoutWrite?this._writeDot(Ge,e):process.stdout.write.bind(process.stdout)(`${V} ${Ge} ${e}
25
- `)}stepMemory(e){let t=T.hex("#2dd4bf")(e);this._origStdoutWrite?this._writeDot(He,t):process.stdout.write.bind(process.stdout)(`${V} ${He} ${t}
26
- `)}stepFail(e){this._origStdoutWrite?this._writeDot(me,T.red(e)):process.stdout.write.bind(process.stdout)(`${V} ${me} ${T.red(e)}
27
- `)}nodeStart(e){this._currentNode=e,this._emitGraphLogMarker({phase:"node_begin",node:e}),this._rawWrite(`${Dt} ${e}`),this._startIntercepting()}nodeComplete(e,t={}){this._stopIntercepting();let{duration:s,details:n}=t;if(n)for(let r of n)this._rawWrite(`${ge} ${r}`);let i=s?T.dim(` ${Ke(s)}`):"";this._rawWrite(`${Ye} ${T.green("done")}${i}`),this._emitGraphLogMarker({phase:"node_end",node:e}),this._rawWrite("")}nodeFailed(e,t,s={}){this._stopIntercepting();let{duration:n}=s,i=n?T.dim(` ${Ke(n)}`):"";this._rawWrite(`${me} ${T.red(t)}`),this._rawWrite(`${Ye} ${T.red("failed")}${i}`),this._emitGraphLogMarker({phase:"node_end",node:e}),this._rawWrite("")}route(e,t){this._rawWrite(T.dim(` ${e} \u2192 ${t}`)),this._rawWrite("")}graphComplete(){this._rawWrite(T.green.bold("\u2713 Workflow completed"))}},I=new ae;var q=".zibby/output",Se="sessions",W=".session-info.json",we=".zibby-stop",_e=".zibby-studio-stop",Lt="result.json",Mt="raw_stream_output.txt",Ft="events.json",jt={BROWSER:"browser",JIRA:"jira",GITHUB:"github",SLACK:"slack",MEMORY:"memory",CHAT_MEMORY:"chat-memory",RUNNER:"runner",SKILL_INSTALLER:"skill-installer",CORE_TOOLS:"core-tools",WORKFLOW_BUILDER:"workflow-builder"},ye=["CI_JOB_ID","GITHUB_RUN_ID","CIRCLE_WORKFLOW_ID","BUILD_ID"];var L=class{constructor(e){if(this.config=e,this.name=e.name,this.prompt=e.prompt,this.outputSchema=e.outputSchema,!this.outputSchema&&!e._isCustomCode)throw new Error(`Node '${this.name}' must define outputSchema (Zod schema). This defines the contract for what the node returns to state.`);this.isZodSchema=this.outputSchema&&typeof this.outputSchema._def<"u",this.parser=e.outputSchema&&!this.isZodSchema?new J(e.outputSchema):null,this.retries=e.retries||0,this.onComplete=e.onComplete,this.customExecute=e.execute}async execute(e,t){let s=()=>t&&typeof t.getAll=="function"?t.getAll():e,n=u=>t&&typeof t.get=="function"?t.get(u):e?.[u];if(typeof this.customExecute=="function"){y.debug(`[workflow] node '${this.name}': custom execute (skipping LLM)`);try{let u=await this.customExecute(e);return typeof u=="object"&&u!==null&&u.success===!1?{success:!1,error:u.error||"Node execution failed",raw:u.raw||null}:this.isZodSchema?(y.debug(`[workflow] node '${this.name}': validating output schema`),{success:!0,output:this.outputSchema.parse(u),raw:null}):{success:!0,output:u,raw:null}}catch(u){return y.error(`[workflow] node '${this.name}' failed: ${u.message}`),u.name==="ZodError"&&y.error(`Schema errors: ${JSON.stringify(u.issues||u.errors,null,2)}`),{success:!1,error:u.message,raw:null}}}let i=typeof this.prompt=="function"?this.prompt(s()):this.prompt,r=n("_skillHints");r&&(i=`${r}
25
+ `)}stepMemory(e){let t=$.hex("#2dd4bf")(e);this._origStdoutWrite?this._writeDot(Ue,t):process.stdout.write.bind(process.stdout)(`${V} ${Ue} ${t}
26
+ `)}stepFail(e){this._origStdoutWrite?this._writeDot(fe,$.red(e)):process.stdout.write.bind(process.stdout)(`${V} ${fe} ${$.red(e)}
27
+ `)}nodeStart(e){this._currentNode=e,this._emitGraphLogMarker({phase:"node_begin",node:e}),this._rawWrite(`${Ot} ${e}`),this._startIntercepting()}nodeComplete(e,t={}){this._stopIntercepting();let{duration:s,details:n}=t;if(n)for(let r of n)this._rawWrite(`${pe} ${r}`);let i=s?$.dim(` ${Je(s)}`):"";this._rawWrite(`${je} ${$.green("done")}${i}`),this._emitGraphLogMarker({phase:"node_end",node:e}),this._rawWrite("")}nodeFailed(e,t,s={}){this._stopIntercepting();let{duration:n}=s,i=n?$.dim(` ${Je(n)}`):"";this._rawWrite(`${fe} ${$.red(t)}`),this._rawWrite(`${je} ${$.red("failed")}${i}`),this._emitGraphLogMarker({phase:"node_end",node:e}),this._rawWrite("")}route(e,t){this._rawWrite($.dim(` ${e} \u2192 ${t}`)),this._rawWrite("")}graphComplete(){this._rawWrite($.green.bold("\u2713 Workflow completed"))}},T=new ie;var z=".zibby/output",he="sessions",R=".session-info.json",me=".zibby-stop",Ct="result.json",Pt="raw_stream_output.txt",Rt="events.json",Wt={BROWSER:"browser",JIRA:"jira",GITHUB:"github",SLACK:"slack",MEMORY:"memory",CHAT_MEMORY:"chat-memory",RUNNER:"runner",SKILL_INSTALLER:"skill-installer",CORE_TOOLS:"core-tools",WORKFLOW_BUILDER:"workflow-builder"},ge=["CI_JOB_ID","GITHUB_RUN_ID","CIRCLE_WORKFLOW_ID","BUILD_ID"];var L=class{constructor(e){if(this.config=e,this.name=e.name,this.prompt=e.prompt,this.outputSchema=e.outputSchema,!this.outputSchema&&!e._isCustomCode)throw new Error(`Node '${this.name}' must define outputSchema (Zod schema). This defines the contract for what the node returns to state.`);this.isZodSchema=this.outputSchema&&typeof this.outputSchema._def<"u",this.parser=e.outputSchema&&!this.isZodSchema?new H(e.outputSchema):null,this.retries=e.retries||0,this.onComplete=e.onComplete,this.customExecute=e.execute}async execute(e,t){let s=()=>t&&typeof t.getAll=="function"?t.getAll():e,n=u=>t&&typeof t.get=="function"?t.get(u):e?.[u];if(typeof this.customExecute=="function"){_.debug(`[workflow] node '${this.name}': custom execute (skipping LLM)`);try{let u=await this.customExecute(e);return typeof u=="object"&&u!==null&&u.success===!1?{success:!1,error:u.error||"Node execution failed",raw:u.raw||null}:this.isZodSchema?(_.debug(`[workflow] node '${this.name}': validating output schema`),{success:!0,output:this.outputSchema.parse(u),raw:null}):{success:!0,output:u,raw:null}}catch(u){return _.error(`[workflow] node '${this.name}' failed: ${u.message}`),u.name==="ZodError"&&_.error(`Schema errors: ${JSON.stringify(u.issues||u.errors,null,2)}`),{success:!1,error:u.message,raw:null}}}let i=typeof this.prompt=="function"?this.prompt(s()):this.prompt,r=n("_skillHints");r&&(i=`${r}
28
28
 
29
- ${i}`);let a=s(),c=a.cwd||process.cwd(),l=a.sessionPath;try{if(l){let u=Oe(l,W);if(ct(u)){let m=JSON.parse(at(u,"utf-8"));m.currentNode=this.name,ve(u,JSON.stringify(m,null,2),"utf-8")}let d=Oe(l,"..",W);if(ct(d))try{let m=JSON.parse(at(d,"utf-8"));m.currentNode=this.name,ve(d,JSON.stringify(m,null,2),"utf-8")}catch{}}}catch(u){y.debug(`[workflow] could not update session info: ${u.message}`)}let h=null;for(let u=0;u<=this.retries;u++)try{y.debug(`[workflow] node '${this.name}' attempt ${u}`);let d=s().config||{},m=d.agents||{},g=this.config.agent??m[this.name]??null,S={state:s()};g&&(S.preferredAgent=g);let w={workspace:c,schema:this.isZodSchema?this.outputSchema:null,skills:this.config.skills||[],sessionPath:l,config:d,nodeName:this.name,timeout:this.config?.timeout||3e5},f=e?._coreInvokeAgent;f||(f=(await Promise.resolve().then(()=>(Y(),X))).invokeAgent);let $=await f(i,S,w),E,A;if(typeof $=="string"?(E=$,A=null):$.structured?(E=$.raw||JSON.stringify($.structured,null,2),A=$.structured):(E=$.raw||JSON.stringify($,null,2),A=$.extracted||null),l)try{let p=Oe(l,this.name,"raw_stream_output.txt");Ut(Zt(p),{recursive:!0}),ve(p,typeof E=="string"?E:JSON.stringify(E),"utf-8")}catch(p){y.debug(`[workflow] could not save raw output: ${p.message}`)}if(this.isZodSchema&&A){y.info(`[workflow] node '${this.name}': output validated: ${JSON.stringify(A,null,2)}`);let p=A;if(typeof this.onComplete=="function")try{p=await this.onComplete(s(),A)}catch(R){y.warn(`[workflow] onComplete hook failed: ${R.message}`)}return{success:!0,output:p,raw:E}}if(typeof this.onComplete=="function")try{return{success:!0,output:await this.onComplete(s(),{raw:E}),raw:E}}catch(p){throw new Error(`onComplete failed: ${p.message}`,{cause:p})}if(this.parser){let p=this.parser.parse(E);return y.info(`[workflow] node '${this.name}': parsed output: ${JSON.stringify(p,null,2)}`),I.step("Output parsed"),{success:!0,output:p,raw:E}}return{success:!0,output:E,raw:E}}catch(d){h=d,u<this.retries&&y.info(`[workflow] node '${this.name}' failed, retrying (${u+1}/${this.retries})\u2026`)}return{success:!1,error:h.message,raw:null}}},ee=class extends L{constructor(e){super({...e,_isCustomCode:!0}),this.condition=e.condition}async execute(e,t){let s=t&&typeof t.getAll=="function"?t.getAll():e;return{success:!0,output:{nextNode:this.condition(s)},raw:null}}};import{existsSync as lt,readFileSync as Yt}from"node:fs";import{join as ke,dirname as dt}from"node:path";var te=class{static async loadContext(e,t,s={}){let n={},i=s.filenames||["CONTEXT.md","AGENTS.md"];if(e){let a=dt(ke(t,e));for(let c of i){let l=await this.findAndMergeContextFiles(c,a,t);if(l){let h=c.replace(/\.[^.]+$/,"").toLowerCase();n[h]=l}}}let r=s.discovery||{};for(let[a,c]of Object.entries(r))try{let l=ke(t,c);lt(l)&&(n[a]=await this.loadFile(l))}catch(l){console.warn(`[workflow] could not load context '${a}' from '${c}': ${l.message}`)}return n}static async findAndMergeContextFiles(e,t,s){let n=[],i=t;for(;i.startsWith(s);){let r=ke(i,e);if(lt(r))try{n.unshift(await this.loadFile(r))}catch(c){console.warn(`[workflow] could not load ${e} from ${r}: ${c.message}`)}let a=dt(i);if(a===i)break;i=a}return n.length===0?null:n.every(r=>typeof r=="string")?n.join(`
29
+ ${i}`);let a=s(),c=a.cwd||process.cwd(),l=a.sessionPath;try{if(l){let u=Te(l,R);if(nt(u)){let g=JSON.parse(st(u,"utf-8"));g.currentNode=this.name,$e(u,JSON.stringify(g,null,2),"utf-8")}let d=Te(l,"..",R);if(nt(d))try{let g=JSON.parse(st(d,"utf-8"));g.currentNode=this.name,$e(d,JSON.stringify(g,null,2),"utf-8")}catch{}}}catch(u){_.debug(`[workflow] could not update session info: ${u.message}`)}let h=null;for(let u=0;u<=this.retries;u++)try{_.debug(`[workflow] node '${this.name}' attempt ${u}`);let d=s().config||{},g=d.agents||{},m=this.config.agent??g[this.name]??null,S={state:s()};m&&(S.preferredAgent=m);let w={workspace:c,schema:this.isZodSchema?this.outputSchema:null,skills:this.config.skills||[],sessionPath:l,config:d,nodeName:this.name,timeout:this.config?.timeout||3e5},f=e?._coreInvokeAgent;f||(f=(await Promise.resolve().then(()=>(Y(),X))).invokeAgent);let I=await f(i,S,w),E,A;if(typeof I=="string"?(E=I,A=null):I.structured?(E=I.raw||JSON.stringify(I.structured,null,2),A=I.structured):(E=I.raw||JSON.stringify(I,null,2),A=I.extracted||null),l)try{let p=Te(l,this.name,"raw_stream_output.txt");Lt(Bt(p),{recursive:!0}),$e(p,typeof E=="string"?E:JSON.stringify(E),"utf-8")}catch(p){_.debug(`[workflow] could not save raw output: ${p.message}`)}if(this.isZodSchema&&A){_.info(`[workflow] node '${this.name}': output validated: ${JSON.stringify(A,null,2)}`);let p=A;if(typeof this.onComplete=="function")try{p=await this.onComplete(s(),A)}catch(F){_.warn(`[workflow] onComplete hook failed: ${F.message}`)}return{success:!0,output:p,raw:E}}if(typeof this.onComplete=="function")try{return{success:!0,output:await this.onComplete(s(),{raw:E}),raw:E}}catch(p){throw new Error(`onComplete failed: ${p.message}`,{cause:p})}if(this.parser){let p=this.parser.parse(E);return _.info(`[workflow] node '${this.name}': parsed output: ${JSON.stringify(p,null,2)}`),T.step("Output parsed"),{success:!0,output:p,raw:E}}return{success:!0,output:E,raw:E}}catch(d){h=d,u<this.retries&&_.info(`[workflow] node '${this.name}' failed, retrying (${u+1}/${this.retries})\u2026`)}return{success:!1,error:h.message,raw:null}}},Q=class extends L{constructor(e){super({...e,_isCustomCode:!0}),this.condition=e.condition}async execute(e,t){let s=t&&typeof t.getAll=="function"?t.getAll():e;return{success:!0,output:{nextNode:this.condition(s)},raw:null}}};import{existsSync as it,readFileSync as Ft}from"node:fs";import{join as Ie,dirname as at}from"node:path";var ee=class{static async loadContext(e,t,s={}){let n={},i=s.filenames||["CONTEXT.md","AGENTS.md"];if(e){let a=at(Ie(t,e));for(let c of i){let l=await this.findAndMergeContextFiles(c,a,t);if(l){let h=c.replace(/\.[^.]+$/,"").toLowerCase();n[h]=l}}}let r=s.discovery||{};for(let[a,c]of Object.entries(r))try{let l=Ie(t,c);it(l)&&(n[a]=await this.loadFile(l))}catch(l){console.warn(`[workflow] could not load context '${a}' from '${c}': ${l.message}`)}return n}static async findAndMergeContextFiles(e,t,s){let n=[],i=t;for(;i.startsWith(s);){let r=Ie(i,e);if(it(r))try{n.unshift(await this.loadFile(r))}catch(c){console.warn(`[workflow] could not load ${e} from ${r}: ${c.message}`)}let a=at(i);if(a===i)break;i=a}return n.length===0?null:n.every(r=>typeof r=="string")?n.join(`
30
30
 
31
31
  ---
32
32
 
33
- `):n.every(r=>typeof r=="object")?Object.assign({},...n):n[n.length-1]}static async loadFile(e){let t=Yt(e,"utf-8");if(e.endsWith(".json"))return JSON.parse(t);if(e.endsWith(".js")||e.endsWith(".mjs")){let{pathToFileURL:s}=await import("url"),n=await import(s(e).href);return n.default||n}return t}};import{mkdirSync as gt,existsSync as ce,writeFileSync as ut,unlinkSync as pt}from"node:fs";import{join as P,resolve as mt}from"node:path";import{config as Gt}from"dotenv";import{zodToJsonSchema as ft}from"zod-to-json-schema";import Ht from"handlebars";var ht=new Set;function le(o,e){ht.has(o)||(ht.add(o),process.env.ZIBBY_NO_DEPRECATION_WARNINGS!=="1"&&console.warn(`[zibby/agent-workflow] ${e}`))}function zt({traceFrom:o,sessionId:e,sessionPath:t,idSource:s,mkdirFresh:n}){if(!(process.env.ZIBBY_SESSION_LOG==="1"||process.env.ZIBBY_SESSION_LOG==="true"))return;let r=typeof process.ppid=="number"?process.ppid:"n/a",a=`[zibby:session] from=${o} pid=${process.pid} ppid=${r} sessionId=${e} source=${s} mkdir=${n?"yes":"no"} path=${t}`;if(console.log(a),process.env.ZIBBY_TRACE_SESSION==="1"||process.env.ZIBBY_TRACE_SESSION==="true"){let h=(new Error("session trace").stack||"").split(`
33
+ `):n.every(r=>typeof r=="object")?Object.assign({},...n):n[n.length-1]}static async loadFile(e){let t=Ft(e,"utf-8");if(e.endsWith(".json"))return JSON.parse(t);if(e.endsWith(".js")||e.endsWith(".mjs")){let{pathToFileURL:s}=await import("url"),n=await import(s(e).href);return n.default||n}return t}};import{mkdirSync as dt,existsSync as Ne,writeFileSync as ct,unlinkSync as Mt}from"node:fs";import{join as B,resolve as ut}from"node:path";import{config as Dt}from"dotenv";import{zodToJsonSchema as lt}from"zod-to-json-schema";import jt from"handlebars";function Gt({traceFrom:o,sessionId:e,sessionPath:t,idSource:s,mkdirFresh:n}){if(!(process.env.ZIBBY_SESSION_LOG==="1"||process.env.ZIBBY_SESSION_LOG==="true"))return;let r=typeof process.ppid=="number"?process.ppid:"n/a",a=`[zibby:session] from=${o} pid=${process.pid} ppid=${r} sessionId=${e} source=${s} mkdir=${n?"yes":"no"} path=${t}`;if(console.log(a),process.env.ZIBBY_TRACE_SESSION==="1"||process.env.ZIBBY_TRACE_SESSION==="true"){let h=(new Error("session trace").stack||"").split(`
34
34
  `).slice(2,14).join(`
35
35
  `);console.log(`[zibby:session] stack (${o}):
36
- ${h}`)}}function St(){return process.env.ZIBBY_TRUST_SESSION_ENV==="1"||process.env.ZIBBY_TRUST_SESSION_ENV==="true"||process.env.ZIBBY_KEEP_SESSION_ENV==="1"||process.env.ZIBBY_KEEP_SESSION_ENV==="true"?!0:process.env.ZIBBY_RUN_SOURCE==="studio"?(le("legacy-zibby-run-source","`ZIBBY_RUN_SOURCE=studio` env var is deprecated. Set `ZIBBY_TRUST_SESSION_ENV=1` (and `ZIBBY_PIN_SESSION_PATH=1` / `ZIBBY_EMIT_GRAPH_MARKERS=1` if you need those gates) instead. The Studio-specific value will be ignored in v2. Suppress with ZIBBY_NO_DEPRECATION_WARNINGS=1."),!0):!1}function wt(){let o=process.env.ZIBBY_PIN_SESSION_PATH==="1"||process.env.ZIBBY_PIN_SESSION_PATH==="true",e=!o&&process.env.ZIBBY_RUN_SOURCE==="studio";if(e&&le("legacy-zibby-run-source","`ZIBBY_RUN_SOURCE=studio` env var is deprecated. Set `ZIBBY_PIN_SESSION_PATH=1` (and `ZIBBY_TRUST_SESSION_ENV=1` / `ZIBBY_EMIT_GRAPH_MARKERS=1` if you need those gates) instead. The Studio-specific value will be ignored in v2. Suppress with ZIBBY_NO_DEPRECATION_WARNINGS=1."),!o&&!e)return;let t=process.env.ZIBBY_SESSION_PATH;if(!(t==null||String(t).trim()===""))try{return mt(String(t).trim())}catch{return String(t).trim()}}var _t=wt;function yt(){St()||(delete process.env.ZIBBY_SESSION_PATH,delete process.env.ZIBBY_SESSION_ID)}function Et({sessionPath:o,sessionId:e}){o&&typeof o=="string"&&(process.env.ZIBBY_SESSION_PATH=o),e!=null&&String(e).trim()!==""&&(process.env.ZIBBY_SESSION_ID=String(e).trim())}function It(o={}){let e=ye.map(i=>process.env[i]).find(Boolean),t=Math.random().toString(36).slice(2,6),s=e||`${Date.now()}_${t}`,n=o.paths?.sessionPrefix;return n?`${n}_${s}`:s}function Nt({cwd:o=process.cwd(),config:e={},initialState:t={},traceFrom:s="resolveWorkflowSession"}={}){let n=t.sessionPath,i=t.sessionTimestamp,r="initialState.sessionPath";if(!n&&process.env.ZIBBY_SESSION_PATH)try{let l=mt(String(process.env.ZIBBY_SESSION_PATH));l&&(n=l,r="ZIBBY_SESSION_PATH")}catch{}let a;if(n)a=String(n).split(/[/\\]/).filter(Boolean).pop(),i==null&&(i=Date.now());else{let l=process.env.ZIBBY_SESSION_ID&&String(process.env.ZIBBY_SESSION_ID).trim();if(l)a=l,r="ZIBBY_SESSION_ID";else{let u=e.sessionId!=null?String(e.sessionId).trim():"";u&&u!=="last"?(a=u,r="config.sessionId"):(a=It(e),r="generated")}i=i??Date.now();let h=e.paths?.output||q;n=P(o,h,Se,a)}let c=!ce(n);return c&&gt(n,{recursive:!0}),(c||r!=="initialState.sessionPath")&&zt({traceFrom:s,sessionId:a,sessionPath:n,idSource:r,mkdirFresh:c}),Et({sessionPath:n,sessionId:a}),{sessionPath:n,sessionId:a,sessionTimestamp:i}}var oe=class{constructor(e={}){this.nodes=new Map,this.edges=new Map,this.entryPoint=null,this.middleware=Array.isArray(e.middleware)?[...e.middleware]:[],e.nodeMiddleware&&this.middleware.push(e.nodeMiddleware),this.nodeTypeMap=new Map,this.conditionalCodeMap=new Map,this.stateSchema=e.stateSchema||null,this.nodePrompts=new Map,this.nodeOptions=new Map,this._invokeAgent=e.invokeAgent||null,this._compiledPrompts=new Map}setStateSchema(e){return this.stateSchema=e,this}getStateSchema(){return this.stateSchema}addNode(e,t,s={}){let n=t instanceof L?t:new L(t);return n.name=e,this.nodes.set(e,n),s.prompt&&this.nodePrompts.set(e,s.prompt),Object.keys(s).length>0&&this.nodeOptions.set(e,s),this}addConditionalNode(e,t){return this.nodes.set(e,new ee({...t,name:e})),this}addEdge(e,t){return this.edges.set(e,t),this}setNodeType(e,t){return this.nodeTypeMap.set(e,t),this}addConditionalEdges(e,t,{labels:s}={}){return this.edges.set(e,{conditional:!0,routes:t,labels:s}),typeof t=="function"&&this.conditionalCodeMap.set(e,t.toString()),this}setEntryPoint(e){return this.entryPoint=e,this}use(e){return typeof e=="function"&&this.middleware.push(e),this}_composeMiddleware(e,t,s,n,i){let r=s;for(let a=e.length-1;a>=0;a--){let c=e[a],l=r;r=()=>c(t,l,n,i)}return r()}serialize(){let e=[],t={};for(let[i,r]of this.nodes){let a=this.nodeTypeMap.get(i)||i;e.push({id:i,type:a,data:{nodeType:a,label:i}});let c={};r._isCustomCode&&typeof r.execute=="function"&&(c.customCode=r.execute.toString());let l=this.nodePrompts.get(i);if(l&&(c.prompt=l),typeof r.customExecute=="function"&&(c.executeCode=r.customExecute.toString()),r.outputSchema)try{if(typeof r.outputSchema._def<"u"){let u=ft(r.outputSchema,{target:"openApi3"});c.outputSchema={jsonSchema:u,variables:this._flattenJsonSchemaToVariables(u)}}else c.outputSchema={schema:r.outputSchema}}catch(u){console.warn(`[workflow] failed to convert schema for ${i}:`,u.message)}let h=(this.resolvedToolsMap||{})[i];h?.toolIds&&(c.tools=h.toolIds),Object.keys(c).length>0&&(t[i]=c)}let s=[];for(let[i,r]of this.edges)if(typeof r=="string")s.push({source:i,target:r});else if(r.conditional){let a=this.conditionalCodeMap.get(i)||r.routes.toString(),c=this._inferConditionalTargets(r.routes),l=r.labels||{};for(let h of c){let u={source:i,target:h,data:{conditionalCode:a}};l[h]&&(u.label=l[h]),s.push(u)}}let n=null;if(this.stateSchema)try{n=ft(this.stateSchema,{target:"openApi3"})}catch{n=this.stateSchema}return{nodes:e,edges:s,nodeConfigs:t,stateSchema:n}}_inferConditionalTargets(e){let t=e.toString(),s=new Set,n=/return\s+['"]([^'"]+)['"]/g,i;for(;(i=n.exec(t))!==null;)s.add(i[1]);return[...s]}_flattenJsonSchemaToVariables(e,t=""){let s=e;if(e.$ref&&e.definitions){let n=e.$ref.replace("#/definitions/","");s=e.definitions[n]||e}return this._flattenSchema(s,t)}_flattenSchema(e,t=""){if(!e||typeof e!="object")return[];let s=[],n=e.properties||{},i=e.required||[];for(let[r,a]of Object.entries(n)){let c=t?`${t}.${r}`:r;s.push({path:c,type:a.type||"unknown",label:a.description||this._formatLabel(r),optional:!i.includes(r)}),a.type==="object"&&a.properties&&s.push(...this._flattenSchema(a,c)),a.type==="array"&&a.items?.type==="object"&&a.items.properties&&s.push(...this._flattenSchema(a.items,`${c}[]`))}return s}_formatLabel(e){return e.replace(/([A-Z])/g," $1").replace(/^./,t=>t.toUpperCase()).trim()}_summarizeNodeOutput(e,t){if(!t||typeof t!="object")return[];let s=[];t.success!==void 0&&s.push(`Result: ${t.success?"passed":"failed"}`);for(let[n,i]of Object.entries(t))if(!(n==="success"||n==="raw"||n==="nextNode")){if(typeof i=="string"&&i.length<=80)s.push(`${n}: ${i}`);else if(Array.isArray(i)){let r=i.length,a=i.filter(l=>l?.passed===!0).length,c=i.some(l=>l?.passed!==void 0);s.push(c?`${n}: ${a}/${r} passed${r-a?`, ${r-a} failed`:""}`:`${n}: ${r} items`)}if(s.length>=4)break}return s}async run(e,t={},s={}){if(!this.entryPoint)throw new Error("No entry point set for graph");let n=new AbortController;s.signal&&(s.signal.aborted?n.abort():s.signal.addEventListener("abort",()=>n.abort(),{once:!0}));let i=s.strategyAbortTimeoutMs??t.config?.strategyAbortTimeoutMs??5e3,r=t.cwd||process.cwd();Gt({path:P(r,".env")});let a=t.config||{};if(!a||Object.keys(a).length===0)try{let _=P(r,".zibby.config.js");ce(_)&&(a=(await import(_)).default||{})}catch{}process.env.EXECUTION_ID&&!a.agent?.strictMode&&(a.agent={...a.agent,strictMode:!0});let c=t.agentType;if(!c){let _=a?.agent;_?.provider?c=_.provider:_?.gemini?c="gemini":_?.claude?c="claude":_?.cursor?c="cursor":_?.codex?c="codex":c=process.env.AGENT_TYPE||"cursor"}let l=t.contextConfig||e?.config?.contextConfig||e?.config?.context||a?.context||{};if(this.stateSchema){let _=this.stateSchema.safeParse(t);if(!_.success){let O=_.error.issues.map(b=>`${b.path.join(".")}: ${b.message}`);throw console.error("\u274C Initial state validation failed:"),O.forEach(b=>console.error(` - ${b}`)),new Error(`State validation failed: ${O.join(", ")}`)}I.step("State validated against schema")}let h=_t(),u=t.sessionPath||h;u||yt();let{sessionPath:d,sessionTimestamp:m,sessionId:g}=Nt({cwd:r,config:a,traceFrom:"WorkflowGraph.run",initialState:{sessionPath:u,sessionTimestamp:t.sessionTimestamp}});I.step(`Session ${g}`);let S=await te.loadContext(t.specPath||"",r,l);Object.keys(S).length>0&&I.step(`Context loaded: ${Object.keys(S).join(", ")}`);let w=t.outputPath;!w&&t.specPath&&(e?.calculateOutputPath?w=e.calculateOutputPath(t.specPath):console.warn(`\u26A0\uFE0F outputPath not resolved (specPath=${t.specPath})`));let f=new z({...t,config:a,agentType:c,outputPath:w,sessionPath:d,sessionTimestamp:m,context:S,resolvedTools:this.resolvedToolsMap||{},_signal:n.signal}),$=new Map;try{await import("@zibby/skills")}catch{}let{getSkill:E}=await Promise.resolve().then(()=>(Q(),rt)),A=new Set;for(let[,_]of this.nodes)for(let O of _.config?.skills||[])A.add(O);for(let _ of A){let O=E(_);if(typeof O?.middleware=="function")try{let b=await O.middleware();typeof b=="function"&&$.set(_,b)}catch{}}let p=this.entryPoint,R=[],Pe=a?.recursionLimit??100,Ot=0;try{for(;p&&p!=="END";){if(++Ot>Pe)throw new Error(`Workflow exceeded recursion limit (${Pe}) \u2014 likely a cyclic conditional route. Set config.recursionLimit if you need a higher cap.`);let O=P(d,we),b=P(d,_e);if(ce(O)){try{pt(O)}catch{}n.abort()}if(ce(b)){try{pt(b)}catch{}n.abort(),le("legacy-stop-file","Detected legacy `.zibby-studio-stop` file. Consumers should migrate to either `.zibby-stop` (renamed) or pass an AbortSignal to graph.run. The legacy filename will be removed in v2. Suppress with ZIBBY_NO_DEPRECATION_WARNINGS=1.")}if(n.signal.aborted)return console.warn(`
37
- \u{1F6D1} External stop requested \u2014 ending workflow.`),I.step("Workflow stopped externally"),{success:!0,state:f.getAll(),executionLog:R,stoppedExternally:!0,stoppedByStudio:!0};let G=this.nodes.get(p);if(!G)throw new Error(`Node '${p}' not found in graph`);let Re=JSON.stringify({sessionPath:d,sessionTimestamp:m,currentNode:p,createdAt:new Date().toISOString(),config:f.get("config")}),kt=P(d,W);ut(kt,Re,"utf-8");let Be=f.get("config")?.paths?.output||q,At=P(r,Be,W);gt(P(r,Be),{recursive:!0});try{ut(At,Re,"utf-8")}catch{}let We=t.onPipelineProgress;if(typeof We=="function")try{We({cwd:r,sessionPath:d,sessionId:g,outputBase:f.get("config")?.paths?.output||q,currentNode:p})}catch{}let bt=(this.resolvedToolsMap||{})[p]||null;f.set("_currentNodeTools",bt);let xt=f.get("nodeConfigs")||{};f.set("_currentNodeConfig",xt[p]||{}),I.nodeStart(p);let De=Date.now(),se=this.nodePrompts.get(p);if(!this._invokeAgent){let x=await Promise.resolve().then(()=>(Y(),X));this._invokeAgent=x.invokeAgent}let Ct=this._invokeAgent,Le=async(x,C,N={})=>{let v=Ct(x,C,{...N,signal:n.signal});return v.catch(()=>{}),n.signal.aborted?v:Promise.race([v,new Promise((M,F)=>{let B=()=>{setTimeout(()=>{let H=new Error(`Strategy ignored AbortSignal \u2014 engine deadman fired after ${i}ms`);H.name="AbortError",F(H)},i)};n.signal.addEventListener("abort",B,{once:!0})})])},Me={state:f,invokeAgent:async(x={},C={})=>{let N=C.prompt||"";if(se){let v=this._compiledPrompts.get(p);v||(v=Ht.compile(se,{noEscape:!0}),this._compiledPrompts.set(p,v));try{N=v(x)}catch(M){throw console.error(`\u274C Template rendering failed for node '${p}':`,M.message),new Error(`Template rendering failed: ${M.message}`,{cause:M})}}else if(!N)throw new Error(`No prompt template configured for node '${p}' and no prompt provided in options`);return Le(N,{state:f.getAll(),images:C.images||[]},{model:C.model||f.get("model"),workspace:f.get("workspace"),schema:C.schema,...C,signal:n.signal})},_coreInvokeAgent:Le,agent:e,nodeId:p,promptTemplate:se,getPromptTemplate:()=>se,...f.getAll()};try{let x=(G.config?.skills||[]).map(B=>$.get(B)).filter(Boolean),C=[...this.middleware,...x],N;C.length>0?N=await this._composeMiddleware(C,p,async()=>G.execute(Me,f),f.getAll(),f):N=await G.execute(Me,f);let v=Date.now()-De;if(R.push({node:p,success:N.success,duration:v,timestamp:new Date().toISOString()}),!N.success){if(String(N.error||"").includes("Stopped from Zibby Studio"))return le("legacy-error-string",'Strategy returned the legacy `Stopped from Zibby Studio` error string. Strategies should reject with `Error.name === "AbortError"` instead. The string-match fallback will be removed in v2. Suppress with ZIBBY_NO_DEPRECATION_WARNINGS=1.'),I.step("Workflow stopped externally"),f.set("stoppedExternally",!0),f.set("stoppedByStudio",!0),{success:!0,state:f.getAll(),executionLog:R,stoppedExternally:!0,stoppedByStudio:!0};if(n.signal.aborted)return I.step("Workflow stopped externally"),{success:!0,state:f.getAll(),executionLog:R,stoppedExternally:!0,stoppedByStudio:!0};f.append("errors",{node:p,error:N.error});let H=G.config?.retries||0,Fe=`${p}_retries`,ne=f.getAll()[Fe]||0;if(ne<H){I.stepInfo(`Retrying (attempt ${ne+1}/${H})`),f.update({[Fe]:ne+1,[`${p}_raw`]:N.raw});continue}throw I.nodeFailed(p,N.error,{duration:v}),new Error(`Node '${p}' failed after ${ne} attempts: ${N.error}`)}f.update({[p]:N.output});let M=this._summarizeNodeOutput(p,N.output);I.nodeComplete(p,{duration:v,details:M});let F=this.edges.get(p);if(!F)p="END";else if(F.conditional){let B=F.routes(f.getAll());I.route(p,B),p=B}else p=F}catch(x){throw I.isInsideNode&&I.nodeFailed(p,x.message,{duration:Date.now()-De}),f.set("failed",!0),f.set("failedAt",p),x}}I.graphComplete();let _={success:!0,state:f.getAll(),executionLog:R};return e&&typeof e.onComplete=="function"&&await e.onComplete(_),_}finally{if(e&&typeof e.cleanup=="function")try{await e.cleanup()}catch(_){console.warn(`[workflow] agent.cleanup() failed: ${_.message}`)}}}};var re=new Map;function Tt(o,e){re.set(o,e)}function Ae(o){return re.get(o)}function de(o){return re.has(o)}function Jt(){return Array.from(re.keys())}function be(o){let e=re.get(o);return e?e.factory&&typeof e.create=="function"?e.create.toString():typeof e.execute=="function"?e.execute.toString():typeof e=="function"?e.toString():null:null}Tt("ai_agent",{name:"ai_agent",factory:!0,create:(o,e={})=>({name:o,_isCustomCode:!0,execute:async t=>{let s=t?._coreInvokeAgent;s||(s=(await Promise.resolve().then(()=>(Y(),X))).invokeAgent);let n=e.extraPromptInstructions||"Execute the task based on the current state.",i=Kt(n,t),r=await s(i,{cwd:t.workspace||process.cwd(),model:t.model,tools:e.resolvedTools||null});return{success:!0,output:{raw:r,nodeId:o},raw:typeof r=="string"?r:r.raw}}})});function Kt(o,e){let t=/@([\w.]+)/g,s=new Set,n;for(;(n=t.exec(o))!==null;)s.add(n[1]);if(s.size===0)return o;let i=[],r=new Set;for(let a of s){let c=a.split(".")[0];if(r.has(c))continue;let l=a.split(".").reduce((d,m)=>d?.[m],e);if(l===void 0)continue;let h=typeof l=="string"?l:l?.raw??JSON.stringify(l,null,2),u=a.replace(/_/g," ").replace(/\b\w/g,d=>d.toUpperCase());i.push(`## ${u}
36
+ ${h}`)}}function pt(){return process.env.ZIBBY_TRUST_SESSION_ENV==="1"||process.env.ZIBBY_TRUST_SESSION_ENV==="true"||process.env.ZIBBY_KEEP_SESSION_ENV==="1"||process.env.ZIBBY_KEEP_SESSION_ENV==="true"}function ft(){if(!(process.env.ZIBBY_PIN_SESSION_PATH==="1"||process.env.ZIBBY_PIN_SESSION_PATH==="true"))return;let e=process.env.ZIBBY_SESSION_PATH;if(!(e==null||String(e).trim()===""))try{return ut(String(e).trim())}catch{return String(e).trim()}}function ht(){pt()||(delete process.env.ZIBBY_SESSION_PATH,delete process.env.ZIBBY_SESSION_ID)}function mt({sessionPath:o,sessionId:e}){o&&typeof o=="string"&&(process.env.ZIBBY_SESSION_PATH=o),e!=null&&String(e).trim()!==""&&(process.env.ZIBBY_SESSION_ID=String(e).trim())}function gt(o={}){let e=ge.map(i=>process.env[i]).find(Boolean),t=Math.random().toString(36).slice(2,6),s=e||`${Date.now()}_${t}`,n=o.paths?.sessionPrefix;return n?`${n}_${s}`:s}function St({cwd:o=process.cwd(),config:e={},initialState:t={},traceFrom:s="resolveWorkflowSession"}={}){let n=t.sessionPath,i=t.sessionTimestamp,r="initialState.sessionPath";if(!n&&process.env.ZIBBY_SESSION_PATH)try{let l=ut(String(process.env.ZIBBY_SESSION_PATH));l&&(n=l,r="ZIBBY_SESSION_PATH")}catch{}let a;if(n)a=String(n).split(/[/\\]/).filter(Boolean).pop(),i==null&&(i=Date.now());else{let l=process.env.ZIBBY_SESSION_ID&&String(process.env.ZIBBY_SESSION_ID).trim();if(l)a=l,r="ZIBBY_SESSION_ID";else{let u=e.sessionId!=null?String(e.sessionId).trim():"";u&&u!=="last"?(a=u,r="config.sessionId"):(a=gt(e),r="generated")}i=i??Date.now();let h=e.paths?.output||z;n=B(o,h,he,a)}let c=!Ne(n);return c&&dt(n,{recursive:!0}),(c||r!=="initialState.sessionPath")&&Gt({traceFrom:s,sessionId:a,sessionPath:n,idSource:r,mkdirFresh:c}),mt({sessionPath:n,sessionId:a}),{sessionPath:n,sessionId:a,sessionTimestamp:i}}var te=class{constructor(e={}){this.nodes=new Map,this.edges=new Map,this.entryPoint=null,this.middleware=Array.isArray(e.middleware)?[...e.middleware]:[],e.nodeMiddleware&&this.middleware.push(e.nodeMiddleware),this.nodeTypeMap=new Map,this.conditionalCodeMap=new Map,this.stateSchema=e.stateSchema||null,this.nodePrompts=new Map,this.nodeOptions=new Map,this._invokeAgent=e.invokeAgent||null,this._compiledPrompts=new Map}setStateSchema(e){return this.stateSchema=e,this}getStateSchema(){return this.stateSchema}addNode(e,t,s={}){let n=t instanceof L?t:new L(t);return n.name=e,this.nodes.set(e,n),s.prompt&&this.nodePrompts.set(e,s.prompt),Object.keys(s).length>0&&this.nodeOptions.set(e,s),this}addConditionalNode(e,t){return this.nodes.set(e,new Q({...t,name:e})),this}addEdge(e,t){return this.edges.set(e,t),this}setNodeType(e,t){return this.nodeTypeMap.set(e,t),this}addConditionalEdges(e,t,{labels:s}={}){return this.edges.set(e,{conditional:!0,routes:t,labels:s}),typeof t=="function"&&this.conditionalCodeMap.set(e,t.toString()),this}setEntryPoint(e){return this.entryPoint=e,this}use(e){return typeof e=="function"&&this.middleware.push(e),this}_composeMiddleware(e,t,s,n,i){let r=s;for(let a=e.length-1;a>=0;a--){let c=e[a],l=r;r=()=>c(t,l,n,i)}return r()}serialize(){let e=[],t={};for(let[i,r]of this.nodes){let a=this.nodeTypeMap.get(i)||i;e.push({id:i,type:a,data:{nodeType:a,label:i}});let c={};r._isCustomCode&&typeof r.execute=="function"&&(c.customCode=r.execute.toString());let l=this.nodePrompts.get(i);if(l&&(c.prompt=l),typeof r.customExecute=="function"&&(c.executeCode=r.customExecute.toString()),r.outputSchema)try{if(typeof r.outputSchema._def<"u"){let u=lt(r.outputSchema,{target:"openApi3"});c.outputSchema={jsonSchema:u,variables:this._flattenJsonSchemaToVariables(u)}}else c.outputSchema={schema:r.outputSchema}}catch(u){console.warn(`[workflow] failed to convert schema for ${i}:`,u.message)}let h=(this.resolvedToolsMap||{})[i];h?.toolIds&&(c.tools=h.toolIds),Object.keys(c).length>0&&(t[i]=c)}let s=[];for(let[i,r]of this.edges)if(typeof r=="string")s.push({source:i,target:r});else if(r.conditional){let a=this.conditionalCodeMap.get(i)||r.routes.toString(),c=this._inferConditionalTargets(r.routes),l=r.labels||{};for(let h of c){let u={source:i,target:h,data:{conditionalCode:a}};l[h]&&(u.label=l[h]),s.push(u)}}let n=null;if(this.stateSchema)try{n=lt(this.stateSchema,{target:"openApi3"})}catch{n=this.stateSchema}return{nodes:e,edges:s,nodeConfigs:t,stateSchema:n}}_inferConditionalTargets(e){let t=e.toString(),s=new Set,n=/return\s+['"]([^'"]+)['"]/g,i;for(;(i=n.exec(t))!==null;)s.add(i[1]);return[...s]}_flattenJsonSchemaToVariables(e,t=""){let s=e;if(e.$ref&&e.definitions){let n=e.$ref.replace("#/definitions/","");s=e.definitions[n]||e}return this._flattenSchema(s,t)}_flattenSchema(e,t=""){if(!e||typeof e!="object")return[];let s=[],n=e.properties||{},i=e.required||[];for(let[r,a]of Object.entries(n)){let c=t?`${t}.${r}`:r;s.push({path:c,type:a.type||"unknown",label:a.description||this._formatLabel(r),optional:!i.includes(r)}),a.type==="object"&&a.properties&&s.push(...this._flattenSchema(a,c)),a.type==="array"&&a.items?.type==="object"&&a.items.properties&&s.push(...this._flattenSchema(a.items,`${c}[]`))}return s}_formatLabel(e){return e.replace(/([A-Z])/g," $1").replace(/^./,t=>t.toUpperCase()).trim()}_summarizeNodeOutput(e,t){if(!t||typeof t!="object")return[];let s=[];t.success!==void 0&&s.push(`Result: ${t.success?"passed":"failed"}`);for(let[n,i]of Object.entries(t))if(!(n==="success"||n==="raw"||n==="nextNode")){if(typeof i=="string"&&i.length<=80)s.push(`${n}: ${i}`);else if(Array.isArray(i)){let r=i.length,a=i.filter(l=>l?.passed===!0).length,c=i.some(l=>l?.passed!==void 0);s.push(c?`${n}: ${a}/${r} passed${r-a?`, ${r-a} failed`:""}`:`${n}: ${r} items`)}if(s.length>=4)break}return s}async run(e,t={},s={}){if(!this.entryPoint)throw new Error("No entry point set for graph");let n=new AbortController;s.signal&&(s.signal.aborted?n.abort():s.signal.addEventListener("abort",()=>n.abort(),{once:!0}));let i=s.strategyAbortTimeoutMs??t.config?.strategyAbortTimeoutMs??5e3,r=t.cwd||process.cwd();Dt({path:B(r,".env")});let a=t.config||{};if(!a||Object.keys(a).length===0)try{let y=B(r,".zibby.config.js");Ne(y)&&(a=(await import(y)).default||{})}catch{}process.env.EXECUTION_ID&&!a.agent?.strictMode&&(a.agent={...a.agent,strictMode:!0});let c=t.agentType;if(!c){let y=a?.agent;y?.provider?c=y.provider:y?.gemini?c="gemini":y?.claude?c="claude":y?.cursor?c="cursor":y?.codex?c="codex":c=process.env.AGENT_TYPE||"cursor"}let l=t.contextConfig||e?.config?.contextConfig||e?.config?.context||a?.context||{};if(this.stateSchema){let y=this.stateSchema.safeParse(t);if(!y.success){let x=y.error.issues.map(v=>`${v.path.join(".")}: ${v.message}`);throw console.error("\u274C Initial state validation failed:"),x.forEach(v=>console.error(` - ${v}`)),new Error(`State validation failed: ${x.join(", ")}`)}T.step("State validated against schema")}let h=ft(),u=t.sessionPath||h;u||ht();let{sessionPath:d,sessionTimestamp:g,sessionId:m}=St({cwd:r,config:a,traceFrom:"WorkflowGraph.run",initialState:{sessionPath:u,sessionTimestamp:t.sessionTimestamp}});T.step(`Session ${m}`);let S=await ee.loadContext(t.specPath||"",r,l);Object.keys(S).length>0&&T.step(`Context loaded: ${Object.keys(S).join(", ")}`);let w=t.outputPath;!w&&t.specPath&&(e?.calculateOutputPath?w=e.calculateOutputPath(t.specPath):console.warn(`\u26A0\uFE0F outputPath not resolved (specPath=${t.specPath})`));let f=new J({...t,config:a,agentType:c,outputPath:w,sessionPath:d,sessionTimestamp:g,context:S,resolvedTools:this.resolvedToolsMap||{},_signal:n.signal}),I=new Map;try{await import("@zibby/skills")}catch{}let{getSkill:E}=await Promise.resolve().then(()=>(q(),et)),A=new Set;for(let[,y]of this.nodes)for(let x of y.config?.skills||[])A.add(x);for(let y of A){let x=E(y);if(typeof x?.middleware=="function")try{let v=await x.middleware();typeof v=="function"&&I.set(y,v)}catch{}}let p=this.entryPoint,F=[],Oe=a?.recursionLimit??100,Et=0;try{for(;p&&p!=="END";){if(++Et>Oe)throw new Error(`Workflow exceeded recursion limit (${Oe}) \u2014 likely a cyclic conditional route. Set config.recursionLimit if you need a higher cap.`);let x=B(d,me);if(Ne(x)){try{Mt(x)}catch{}n.abort()}if(n.signal.aborted)return console.warn(`
37
+ \u{1F6D1} External stop requested \u2014 ending workflow.`),T.step("Workflow stopped externally"),{success:!0,state:f.getAll(),executionLog:F,stoppedExternally:!0};let v=this.nodes.get(p);if(!v)throw new Error(`Node '${p}' not found in graph`);let Ce=JSON.stringify({sessionPath:d,sessionTimestamp:g,currentNode:p,createdAt:new Date().toISOString(),config:f.get("config")}),$t=B(d,R);ct($t,Ce,"utf-8");let Pe=f.get("config")?.paths?.output||z,Tt=B(r,Pe,R);dt(B(r,Pe),{recursive:!0});try{ct(Tt,Ce,"utf-8")}catch{}let Re=t.onPipelineProgress;if(typeof Re=="function")try{Re({cwd:r,sessionPath:d,sessionId:m,outputBase:f.get("config")?.paths?.output||z,currentNode:p})}catch{}let It=(this.resolvedToolsMap||{})[p]||null;f.set("_currentNodeTools",It);let Nt=f.get("nodeConfigs")||{};f.set("_currentNodeConfig",Nt[p]||{}),T.nodeStart(p);let We=Date.now(),re=this.nodePrompts.get(p);if(!this._invokeAgent){let O=await Promise.resolve().then(()=>(Y(),X));this._invokeAgent=O.invokeAgent}let vt=this._invokeAgent,Le=async(O,C,N={})=>{let k=vt(O,C,{...N,signal:n.signal});return k.catch(()=>{}),n.signal.aborted?k:Promise.race([k,new Promise((M,D)=>{let P=()=>{setTimeout(()=>{let Z=new Error(`Strategy ignored AbortSignal \u2014 engine deadman fired after ${i}ms`);Z.name="AbortError",D(Z)},i)};n.signal.addEventListener("abort",P,{once:!0})})])},Be={state:f,invokeAgent:async(O={},C={})=>{let N=C.prompt||"";if(re){let k=this._compiledPrompts.get(p);k||(k=jt.compile(re,{noEscape:!0}),this._compiledPrompts.set(p,k));try{N=k(O)}catch(M){throw console.error(`\u274C Template rendering failed for node '${p}':`,M.message),new Error(`Template rendering failed: ${M.message}`,{cause:M})}}else if(!N)throw new Error(`No prompt template configured for node '${p}' and no prompt provided in options`);return Le(N,{state:f.getAll(),images:C.images||[]},{model:C.model||f.get("model"),workspace:f.get("workspace"),schema:C.schema,...C,signal:n.signal})},_coreInvokeAgent:Le,agent:e,nodeId:p,promptTemplate:re,getPromptTemplate:()=>re,...f.getAll()};try{let O=(v.config?.skills||[]).map(P=>I.get(P)).filter(Boolean),C=[...this.middleware,...O],N;C.length>0?N=await this._composeMiddleware(C,p,async()=>v.execute(Be,f),f.getAll(),f):N=await v.execute(Be,f);let k=Date.now()-We;if(F.push({node:p,success:N.success,duration:k,timestamp:new Date().toISOString()}),!N.success){if(n.signal.aborted)return T.step("Workflow stopped externally"),{success:!0,state:f.getAll(),executionLog:F,stoppedExternally:!0};f.append("errors",{node:p,error:N.error});let P=v.config?.retries||0,Z=`${p}_retries`,se=f.getAll()[Z]||0;if(se<P){T.stepInfo(`Retrying (attempt ${se+1}/${P})`),f.update({[Z]:se+1,[`${p}_raw`]:N.raw});continue}throw T.nodeFailed(p,N.error,{duration:k}),new Error(`Node '${p}' failed after ${se} attempts: ${N.error}`)}f.update({[p]:N.output});let M=this._summarizeNodeOutput(p,N.output);T.nodeComplete(p,{duration:k,details:M});let D=this.edges.get(p);if(!D)p="END";else if(D.conditional){let P=D.routes(f.getAll());T.route(p,P),p=P}else p=D}catch(O){throw T.isInsideNode&&T.nodeFailed(p,O.message,{duration:Date.now()-We}),f.set("failed",!0),f.set("failedAt",p),O}}T.graphComplete();let y={success:!0,state:f.getAll(),executionLog:F};return e&&typeof e.onComplete=="function"&&await e.onComplete(y),y}finally{if(e&&typeof e.cleanup=="function")try{await e.cleanup()}catch(y){console.warn(`[workflow] agent.cleanup() failed: ${y.message}`)}}}};var ve=Symbol.for("@zibby/agent-workflow.nodes");globalThis[ve]||(globalThis[ve]=new Map);var oe=globalThis[ve];function wt(o,e){oe.set(o,e)}function ke(o){return oe.get(o)}function ae(o){return oe.has(o)}function Ut(){return Array.from(oe.keys())}function xe(o){let e=oe.get(o);return e?e.factory&&typeof e.create=="function"?e.create.toString():typeof e.execute=="function"?e.execute.toString():typeof e=="function"?e.toString():null:null}wt("ai_agent",{name:"ai_agent",factory:!0,create:(o,e={})=>({name:o,_isCustomCode:!0,execute:async t=>{let s=t?._coreInvokeAgent;s||(s=(await Promise.resolve().then(()=>(Y(),X))).invokeAgent);let n=e.extraPromptInstructions||"Execute the task based on the current state.",i=Yt(n,t),r=await s(i,{cwd:t.workspace||process.cwd(),model:t.model,tools:e.resolvedTools||null});return{success:!0,output:{raw:r,nodeId:o},raw:typeof r=="string"?r:r.raw}}})});function Yt(o,e){let t=/@([\w.]+)/g,s=new Set,n;for(;(n=t.exec(o))!==null;)s.add(n[1]);if(s.size===0)return o;let i=[],r=new Set;for(let a of s){let c=a.split(".")[0];if(r.has(c))continue;let l=a.split(".").reduce((d,g)=>d?.[g],e);if(l===void 0)continue;let h=typeof l=="string"?l:l?.raw??JSON.stringify(l,null,2),u=a.replace(/_/g," ").replace(/\b\w/g,d=>d.toUpperCase());i.push(`## ${u}
38
38
  ${h}`),a.includes(".")||r.add(c)}return i.length===0?o:`${o}
39
39
 
40
40
  ---
@@ -42,13 +42,13 @@ ${h}`),a.includes(".")||r.add(c)}return i.length===0?o:`${o}
42
42
 
43
43
  ${i.join(`
44
44
 
45
- `)}`}Q();j();var ue={};function Ce(o,e){if(Array.isArray(e))return xe(e);let t=ue[o];return!t||t.length===0?null:xe(t)}function xe(o){if(!Array.isArray(o)||o.length===0)return null;let e=[],t={},s=[];for(let n of o){let i=Z(n);if(!i){y.warn(`[workflow] unknown skill "${n}" \u2014 skipping`);continue}s.push(n);for(let r of i.tools||[])e.push({name:r.name,description:r.description,input_schema:r.input_schema||{type:"object",properties:{}}});if(!t[i.serverName])if(typeof i.resolve=="function"){let r=i.resolve();r&&(t[i.serverName]={...r,toolPrefix:n})}else{let r={};for(let a of i.envKeys||[]){let c=process.env[a];c&&(r[a]=c)}t[i.serverName]={command:i.command,args:[...i.args||[]],env:r,toolPrefix:n}}}return s.length===0?null:{toolIds:s,claudeTools:e,mcpServers:t}}j();function Vt(o,e={}){let{nodes:t,edges:s,nodeConfigs:n={}}=o;if(!Array.isArray(t)||t.length===0)throw new k("Graph must have at least one node");if(!Array.isArray(s))throw new k("Graph edges must be an array");let i=new oe(e);e.stateSchema&&i.setStateSchema(e.stateSchema);let r=new Set,a=new Map,c={};for(let d of t){let m=pe(d);a.set(d.id,{...d,resolvedType:m}),m==="decision"&&r.add(d.id)}for(let[d,m]of a){if(r.has(d))continue;let g=m.resolvedType,S=n[d]||{},w=Ce(g,S.tools);w&&(c[d]=w);let f={};S.prompt&&(f.prompt=S.prompt);let $=de(g);if(y.debug(`[workflow] compiler: node "${d}" type="${g}" registered=${$}`),S.customCode&&!$)i.addNode(d,$t(d,S.customCode,S),f),i.setNodeType(d,g);else if($){let E=Ae(g);E.factory?i.addNode(d,E.create(d,{...S,resolvedTools:w}),f):i.addNode(d,E,f),i.setNodeType(d,g)}else if(S.executeCode)i.addNode(d,$t(d,S.executeCode,S),f),i.setNodeType(d,g);else throw new k(`Unknown node type "${g}" for node "${d}". Did you forget to register it?`)}i.resolvedToolsMap=c;let l=new Set;for(let d of s)r.has(d.target)||l.add(d.target);let h=t.find(d=>!r.has(d.id)&&!l.has(d.id));if(!h)throw new k("Could not determine entry point: no node without incoming edges found");i.setEntryPoint(h.id);let u=Xt(s,"source");for(let d of s)if(!r.has(d.source))if(r.has(d.target)){let m=d.target,g=u.get(m)||[];if(g.length===0)throw new k(`Decision node "${m}" has no outgoing edges`);let S=eo(m,g,r);i.addConditionalEdges(d.source,S)}else i.addEdge(d.source,d.target);return i}function qt(o){let e=[];if(!o||typeof o!="object")return{valid:!1,errors:["Config must be a non-null object"]};if((!Array.isArray(o.nodes)||o.nodes.length===0)&&e.push("Graph must have at least one node"),Array.isArray(o.edges)||e.push("Graph edges must be an array"),e.length>0)return{valid:!1,errors:e};let t=o.nodeConfigs||{};for(let a of o.nodes){let c=pe(a);if(c==="decision"||de(c))continue;let l=t[a.id]||{};l.customCode||l.executeCode||e.push(`Unknown node type "${c}" for node "${a.id}". Register it or provide customCode/executeCode.`)}let s=new Set(o.nodes.map(a=>a.id));for(let a of o.edges)s.has(a.source)||e.push(`Edge references unknown source node "${a.source}"`),s.has(a.target)||e.push(`Edge references unknown target node "${a.target}"`);let n=new Set(o.nodes.filter(a=>pe(a)==="decision").map(a=>a.id)),i=new Set;for(let a of o.edges)n.has(a.target)||i.add(a.target);let r=o.nodes.filter(a=>!n.has(a.id)&&!i.has(a.id));r.length===0?e.push("No entry point found (every node has incoming edges)"):r.length>1&&e.push(`Multiple entry points found: ${r.map(a=>a.id).join(", ")}`);for(let a of n){let c=o.edges.filter(h=>h.source===a);c.length===0&&e.push(`Decision node "${a}" has no outgoing edges`),c.some(h=>h.data?.conditionalCode||h.conditionalCode)||e.push(`Decision node "${a}" outgoing edges have no conditionalCode`)}return{valid:e.length===0,errors:e}}function Qt(o){return!o||!Array.isArray(o.nodes)?[]:o.nodes.filter(e=>pe(e)!=="decision").map(e=>e.id)}function pe(o){let e=o.data?.nodeType||o.data?.type||o.type;return e==="workflowNode"||e==="custom"||e==="default"?o.id:e}function Xt(o,e){let t=new Map;for(let s of o){let n=s[e];t.has(n)||t.set(n,[]),t.get(n).push(s)}return t}function eo(o,e,t){let s=e.find(a=>a.data?.conditionalCode||a.conditionalCode);if(!s)throw new k(`Decision node "${o}" has no conditionalCode on its outgoing edges`);let n=s.data?.conditionalCode||s.conditionalCode,i=new Set(e.map(a=>a.target).filter(a=>!t.has(a))),r;try{let c=new Function(`return (${n})`)();r=l=>{let h=c(l);return i.has(h)||y.warn(`[workflow] conditional route from "${o}" returned "${h}" which is not in valid targets: ${[...i].join(", ")}`),h}}catch(a){throw new k(`Failed to compile conditionalCode for "${o}": ${a.message}`)}return r}function $t(o,e,t={}){let s;try{s=new Function("invokeAgent","require","console",`return (${e})`)}catch(r){throw new k(`Failed to compile customCode for node "${o}": ${r.message}`)}let n=s(async(...r)=>{let{invokeAgent:a}=await Promise.resolve().then(()=>(Y(),X));return a(...r)},typeof fe<"u"?fe:void 0,console),i=null;return t.outputSchema&&(i=t.outputSchema.jsonSchema||t.outputSchema),{name:o,_isCustomCode:!0,outputSchema:i,execute:async r=>{try{let a=await n(r);return typeof a=="object"&&"success"in a?a:{success:!0,output:a,raw:null}}catch(a){return{success:!1,error:a.message,raw:null}}}}}var k=class extends Error{constructor(e){super(e),this.name="CompilationError"}};Q();Ie();Y();function to(o,e={}){let{nodes:t,edges:s,nodeConfigs:n={}}=o,i=new Set,r=[],a=new Map;for(let w of t){let f=w.data?.nodeType||w.type;a.set(w.id,f),f==="decision"?i.add(w.id):r.push({id:w.id,nodeType:f,label:w.data?.label||w.id})}let c=r.some(w=>{let f=n[w.id]||{};return!f.customCode&&!f.executeCode}),{toolsPerNode:l,toolIdsByVar:h}=lo(r,n),{simpleEdges:u,conditionalEdges:d}=uo(s,i),m=po(r,s,i),g=[],S=e.workflowType||"workflow";return g.push(ro(e)),g.push(so(S,{usesRegisteredNodes:c})),g.push(no(h)),g.push(io(S)),g.push(ao(r,n)),g.push(co(r,m,u,d,l,S)),g.filter(Boolean).join(`
46
- `)}function oo(o){let e={};for(let[t,s]of Object.entries(o)){let{tools:n,...i}=s;Object.keys(i).length>0&&(e[t]=i)}return e}function ro(o){let e=o.workflowType||"workflow";return["// Generated workflow",`// ${o.projectId?`Project: ${o.projectId} | `:""}Type: ${e} | Version: ${o.version??0}`,`// Downloaded: ${new Date().toISOString()}`,""].join(`
47
- `)}function so(o,{usesRegisteredNodes:e=!0}={}){let t=["import { WorkflowGraph, invokeAgent, getResolvedToolDefinitions } from '@zibby/agent-workflow';"];return e&&t.push("// import './register-nodes.js'; // register custom node types here"),t.push("import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'node:fs';","import { join, dirname } from 'node:path';","import { fileURLToPath } from 'node:url';",""),t.join(`
48
- `)}function no(o){if(o.size===0)return"";let e=["// \u2500\u2500 Tool Bindings \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"];for(let[t,s]of o)e.push(`const ${t} = getResolvedToolDefinitions(${JSON.stringify(s)}); // ${s.join(", ")}`);return e.push(""),e.join(`
49
- `)}function io(o){return["// \u2500\u2500 Node Configs \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500","const __filename = fileURLToPath(import.meta.url);","const __dirname = dirname(__filename);",`const configPath = join(__dirname, 'workflow-${o}.config.json');`,"const nodeConfigs = existsSync(configPath) ? JSON.parse(readFileSync(configPath, 'utf-8')) : {};",""].join(`
50
- `)}function ao(o,e){let t=["// \u2500\u2500 Node Implementations \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500",""];for(let s of o){let n=vt(s.id),i=e[s.id]?.customCode;if(i)t.push(`// @custom \u2014 modified from default "${s.nodeType}" template`),t.push(`const ${n}_execute = ${i};`);else{let r=be(s.nodeType);r?(t.push(`// Default "${s.nodeType}" implementation`),t.push(`const ${n}_execute = ${r};`)):(t.push(`// No template for "${s.nodeType}" \u2014 passthrough`),t.push(`const ${n}_execute = async (state) => ({ success: true, output: {}, raw: null });`))}t.push("")}return t.join(`
51
- `)}function co(o,e,t,s,n,i){let r=["// \u2500\u2500 Graph Builder \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"];r.push("export function buildGraph(options = {}) {"),r.push(" const graph = new WorkflowGraph(options);",""),r.push(" // Nodes");for(let c of o){let l=vt(c.id);r.push(` graph.addNode('${c.id}', { name: '${c.id}', execute: ${l}_execute });`),r.push(` graph.setNodeType('${c.id}', '${c.nodeType}');`)}r.push("",` graph.setEntryPoint('${e}');`,""),(t.length>0||s.length>0)&&r.push(" // Edges");for(let c of t)r.push(` graph.addEdge('${c.source}', '${c.target}');`);for(let c of s){let l=c.code.split(`
45
+ `)}`}q();j();var ce={};function Ae(o,e){if(Array.isArray(e))return be(e);let t=ce[o];return!t||t.length===0?null:be(t)}function be(o){if(!Array.isArray(o)||o.length===0)return null;let e=[],t={},s=[];for(let n of o){let i=U(n);if(!i){_.warn(`[workflow] unknown skill "${n}" \u2014 skipping`);continue}s.push(n);for(let r of i.tools||[])e.push({name:r.name,description:r.description,input_schema:r.input_schema||{type:"object",properties:{}}});if(!t[i.serverName])if(typeof i.resolve=="function"){let r=i.resolve();r&&(t[i.serverName]={...r,toolPrefix:n})}else{let r={};for(let a of i.envKeys||[]){let c=process.env[a];c&&(r[a]=c)}t[i.serverName]={command:i.command,args:[...i.args||[]],env:r,toolPrefix:n}}}return s.length===0?null:{toolIds:s,claudeTools:e,mcpServers:t}}j();function Zt(o,e={}){let{nodes:t,edges:s,nodeConfigs:n={}}=o;if(!Array.isArray(t)||t.length===0)throw new b("Graph must have at least one node");if(!Array.isArray(s))throw new b("Graph edges must be an array");let i=new te(e);e.stateSchema&&i.setStateSchema(e.stateSchema);let r=new Set,a=new Map,c={};for(let d of t){let g=le(d);a.set(d.id,{...d,resolvedType:g}),g==="decision"&&r.add(d.id)}for(let[d,g]of a){if(r.has(d))continue;let m=g.resolvedType,S=n[d]||{},w=Ae(m,S.tools);w&&(c[d]=w);let f={};S.prompt&&(f.prompt=S.prompt);let I=ae(m);if(_.debug(`[workflow] compiler: node "${d}" type="${m}" registered=${I}`),S.customCode&&!I)i.addNode(d,yt(d,S.customCode,S),f),i.setNodeType(d,m);else if(I){let E=ke(m);E.factory?i.addNode(d,E.create(d,{...S,resolvedTools:w}),f):i.addNode(d,E,f),i.setNodeType(d,m)}else if(S.executeCode)i.addNode(d,yt(d,S.executeCode,S),f),i.setNodeType(d,m);else throw new b(`Unknown node type "${m}" for node "${d}". Did you forget to register it?`)}i.resolvedToolsMap=c;let l=new Set;for(let d of s)r.has(d.target)||l.add(d.target);let h=t.find(d=>!r.has(d.id)&&!l.has(d.id));if(!h)throw new b("Could not determine entry point: no node without incoming edges found");i.setEntryPoint(h.id);let u=Kt(s,"source");for(let d of s)if(!r.has(d.source))if(r.has(d.target)){let g=d.target,m=u.get(g)||[];if(m.length===0)throw new b(`Decision node "${g}" has no outgoing edges`);let S=Vt(g,m,r);i.addConditionalEdges(d.source,S)}else i.addEdge(d.source,d.target);return i}function Jt(o){let e=[];if(!o||typeof o!="object")return{valid:!1,errors:["Config must be a non-null object"]};if((!Array.isArray(o.nodes)||o.nodes.length===0)&&e.push("Graph must have at least one node"),Array.isArray(o.edges)||e.push("Graph edges must be an array"),e.length>0)return{valid:!1,errors:e};let t=o.nodeConfigs||{};for(let a of o.nodes){let c=le(a);if(c==="decision"||ae(c))continue;let l=t[a.id]||{};l.customCode||l.executeCode||e.push(`Unknown node type "${c}" for node "${a.id}". Register it or provide customCode/executeCode.`)}let s=new Set(o.nodes.map(a=>a.id));for(let a of o.edges)s.has(a.source)||e.push(`Edge references unknown source node "${a.source}"`),s.has(a.target)||e.push(`Edge references unknown target node "${a.target}"`);let n=new Set(o.nodes.filter(a=>le(a)==="decision").map(a=>a.id)),i=new Set;for(let a of o.edges)n.has(a.target)||i.add(a.target);let r=o.nodes.filter(a=>!n.has(a.id)&&!i.has(a.id));r.length===0?e.push("No entry point found (every node has incoming edges)"):r.length>1&&e.push(`Multiple entry points found: ${r.map(a=>a.id).join(", ")}`);for(let a of n){let c=o.edges.filter(h=>h.source===a);c.length===0&&e.push(`Decision node "${a}" has no outgoing edges`),c.some(h=>h.data?.conditionalCode||h.conditionalCode)||e.push(`Decision node "${a}" outgoing edges have no conditionalCode`)}return{valid:e.length===0,errors:e}}function Ht(o){return!o||!Array.isArray(o.nodes)?[]:o.nodes.filter(e=>le(e)!=="decision").map(e=>e.id)}function le(o){let e=o.data?.nodeType||o.data?.type||o.type;return e==="workflowNode"||e==="custom"||e==="default"?o.id:e}function Kt(o,e){let t=new Map;for(let s of o){let n=s[e];t.has(n)||t.set(n,[]),t.get(n).push(s)}return t}function Vt(o,e,t){let s=e.find(a=>a.data?.conditionalCode||a.conditionalCode);if(!s)throw new b(`Decision node "${o}" has no conditionalCode on its outgoing edges`);let n=s.data?.conditionalCode||s.conditionalCode,i=new Set(e.map(a=>a.target).filter(a=>!t.has(a))),r;try{let c=new Function(`return (${n})`)();r=l=>{let h=c(l);return i.has(h)||_.warn(`[workflow] conditional route from "${o}" returned "${h}" which is not in valid targets: ${[...i].join(", ")}`),h}}catch(a){throw new b(`Failed to compile conditionalCode for "${o}": ${a.message}`)}return r}function yt(o,e,t={}){let s;try{s=new Function("invokeAgent","require","console",`return (${e})`)}catch(r){throw new b(`Failed to compile customCode for node "${o}": ${r.message}`)}let n=s(async(...r)=>{let{invokeAgent:a}=await Promise.resolve().then(()=>(Y(),X));return a(...r)},typeof de<"u"?de:void 0,console),i=null;return t.outputSchema&&(i=t.outputSchema.jsonSchema||t.outputSchema),{name:o,_isCustomCode:!0,outputSchema:i,execute:async r=>{try{let a=await n(r);return typeof a=="object"&&"success"in a?a:{success:!0,output:a,raw:null}}catch(a){return{success:!1,error:a.message,raw:null}}}}}var b=class extends Error{constructor(e){super(e),this.name="CompilationError"}};q();we();Y();function zt(o,e={}){let{nodes:t,edges:s,nodeConfigs:n={}}=o,i=new Set,r=[],a=new Map;for(let w of t){let f=w.data?.nodeType||w.type;a.set(w.id,f),f==="decision"?i.add(w.id):r.push({id:w.id,nodeType:f,label:w.data?.label||w.id})}let c=r.some(w=>{let f=n[w.id]||{};return!f.customCode&&!f.executeCode}),{toolsPerNode:l,toolIdsByVar:h}=so(r,n),{simpleEdges:u,conditionalEdges:d}=no(s,i),g=io(r,s,i),m=[],S=e.workflowType||"workflow";return m.push(Xt(e)),m.push(Qt(S,{usesRegisteredNodes:c})),m.push(eo(h)),m.push(to(S)),m.push(oo(r,n)),m.push(ro(r,g,u,d,l,S)),m.filter(Boolean).join(`
46
+ `)}function qt(o){let e={};for(let[t,s]of Object.entries(o)){let{tools:n,...i}=s;Object.keys(i).length>0&&(e[t]=i)}return e}function Xt(o){let e=o.workflowType||"workflow";return["// Generated workflow",`// ${o.projectId?`Project: ${o.projectId} | `:""}Type: ${e} | Version: ${o.version??0}`,`// Downloaded: ${new Date().toISOString()}`,""].join(`
47
+ `)}function Qt(o,{usesRegisteredNodes:e=!0}={}){let t=["import { WorkflowGraph, invokeAgent, getResolvedToolDefinitions } from '@zibby/agent-workflow';"];return e&&t.push("// import './register-nodes.js'; // register custom node types here"),t.push("import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'node:fs';","import { join, dirname } from 'node:path';","import { fileURLToPath } from 'node:url';",""),t.join(`
48
+ `)}function eo(o){if(o.size===0)return"";let e=["// \u2500\u2500 Tool Bindings \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"];for(let[t,s]of o)e.push(`const ${t} = getResolvedToolDefinitions(${JSON.stringify(s)}); // ${s.join(", ")}`);return e.push(""),e.join(`
49
+ `)}function to(o){return["// \u2500\u2500 Node Configs \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500","const __filename = fileURLToPath(import.meta.url);","const __dirname = dirname(__filename);",`const configPath = join(__dirname, 'workflow-${o}.config.json');`,"const nodeConfigs = existsSync(configPath) ? JSON.parse(readFileSync(configPath, 'utf-8')) : {};",""].join(`
50
+ `)}function oo(o,e){let t=["// \u2500\u2500 Node Implementations \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500",""];for(let s of o){let n=_t(s.id),i=e[s.id]?.customCode;if(i)t.push(`// @custom \u2014 modified from default "${s.nodeType}" template`),t.push(`const ${n}_execute = ${i};`);else{let r=xe(s.nodeType);r?(t.push(`// Default "${s.nodeType}" implementation`),t.push(`const ${n}_execute = ${r};`)):(t.push(`// No template for "${s.nodeType}" \u2014 passthrough`),t.push(`const ${n}_execute = async (state) => ({ success: true, output: {}, raw: null });`))}t.push("")}return t.join(`
51
+ `)}function ro(o,e,t,s,n,i){let r=["// \u2500\u2500 Graph Builder \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"];r.push("export function buildGraph(options = {}) {"),r.push(" const graph = new WorkflowGraph(options);",""),r.push(" // Nodes");for(let c of o){let l=_t(c.id);r.push(` graph.addNode('${c.id}', { name: '${c.id}', execute: ${l}_execute });`),r.push(` graph.setNodeType('${c.id}', '${c.nodeType}');`)}r.push("",` graph.setEntryPoint('${e}');`,""),(t.length>0||s.length>0)&&r.push(" // Edges");for(let c of t)r.push(` graph.addEdge('${c.source}', '${c.target}');`);for(let c of s){let l=c.code.split(`
52
52
  `).map((h,u)=>u===0?h:` ${h}`).join(`
53
53
  `);r.push(` graph.addConditionalEdges('${c.source}', ${l});`)}let a=[];for(let c of o){let l=n.get(c.id);l&&a.push(` '${c.id}': ${l},`)}return a.length>0&&r.push(""," graph.resolvedToolsMap = {",...a," };"),r.push(""," return graph;","}",""),r.push("export { nodeConfigs };",""),r.join(`
54
- `)}function lo(o,e){let t=new Map,s=new Map;for(let n of o){let i=e[n.id]?.tools,r;if(Array.isArray(i)&&i.length>0)r=[...i].sort();else{let a=ue[n.nodeType];a?.length>0&&(r=[...a].sort())}if(r){let a=`${r.map(c=>c.replace(/[^a-zA-Z0-9]/g,"")).join("And")}Tools`;t.set(n.id,a),s.has(a)||s.set(a,r)}}return{toolsPerNode:t,toolIdsByVar:s}}function uo(o,e){let t=[],s=[],n=new Map,i=new Set;for(let r of o)n.has(r.source)||n.set(r.source,[]),n.get(r.source).push(r);for(let r of o)if(!e.has(r.source))if(e.has(r.target)){if(i.has(r.target))continue;i.add(r.target);let c=(n.get(r.target)||[]).find(l=>l.data?.conditionalCode||l.conditionalCode);c&&s.push({source:r.source,code:c.data?.conditionalCode||c.conditionalCode})}else t.push({source:r.source,target:r.target});return{simpleEdges:t,conditionalEdges:s}}function po(o,e,t){let s=new Set;for(let i of e)t.has(i.target)||s.add(i.target);let n=o.find(i=>!s.has(i.id));return n?n.id:o[0]?.id}function vt(o){return o.replace(/[^a-zA-Z0-9]/g,"_")}j();export{Ee as AgentStrategy,ye as CI_ENV_VARS,k as CompilationError,ee as ConditionalNode,te as ContextLoader,q as DEFAULT_OUTPUT_BASE,Ft as EVENTS_FILE,ue as NODE_DEFAULT_TOOLS,L as Node,J as OutputParser,Mt as RAW_OUTPUT_FILE,Lt as RESULT_FILE,Se as SESSIONS_DIR,W as SESSION_INFO_FILE,jt as SKILLS,we as STOP_REQUEST_FILE,_e as STUDIO_STOP_REQUEST_FILE,Bt as SchemaTypes,ae as Timeline,qe as WORKFLOW_GRAPH_LOG_MARKER_PREFIX,oe as WorkflowGraph,z as WorkflowState,yt as clearInheritedSessionEnvForFreshRun,ot as clearSkills,Vt as compileGraph,Qt as extractSteps,oo as generateNodeConfigsJson,to as generateWorkflowCode,It as generateWorkflowSessionId,$e as getAgentStrategy,et as getAllSkills,Ae as getNodeImpl,be as getNodeTemplate,xe as getResolvedToolDefinitions,Z as getSkill,de as hasNode,Xe as hasSkill,it as invokeAgent,Jt as listNodeTypes,tt as listSkillIds,nt as listStrategies,wt as readPinnedSessionPathFromEnv,_t as readStudioPinnedSessionPathFromEnv,Tt as registerNode,Qe as registerSkill,st as registerStrategy,Ce as resolveNodeTools,Nt as resolveWorkflowSession,Wt as setLogger,St as shouldTrustInheritedSessionEnv,Et as syncProcessEnvToSession,I as timeline,qt as validateGraphConfig};
54
+ `)}function so(o,e){let t=new Map,s=new Map;for(let n of o){let i=e[n.id]?.tools,r;if(Array.isArray(i)&&i.length>0)r=[...i].sort();else{let a=ce[n.nodeType];a?.length>0&&(r=[...a].sort())}if(r){let a=`${r.map(c=>c.replace(/[^a-zA-Z0-9]/g,"")).join("And")}Tools`;t.set(n.id,a),s.has(a)||s.set(a,r)}}return{toolsPerNode:t,toolIdsByVar:s}}function no(o,e){let t=[],s=[],n=new Map,i=new Set;for(let r of o)n.has(r.source)||n.set(r.source,[]),n.get(r.source).push(r);for(let r of o)if(!e.has(r.source))if(e.has(r.target)){if(i.has(r.target))continue;i.add(r.target);let c=(n.get(r.target)||[]).find(l=>l.data?.conditionalCode||l.conditionalCode);c&&s.push({source:r.source,code:c.data?.conditionalCode||c.conditionalCode})}else t.push({source:r.source,target:r.target});return{simpleEdges:t,conditionalEdges:s}}function io(o,e,t){let s=new Set;for(let i of e)t.has(i.target)||s.add(i.target);let n=o.find(i=>!s.has(i.id));return n?n.id:o[0]?.id}function _t(o){return o.replace(/[^a-zA-Z0-9]/g,"_")}j();export{Se as AgentStrategy,ge as CI_ENV_VARS,b as CompilationError,Q as ConditionalNode,ee as ContextLoader,z as DEFAULT_OUTPUT_BASE,Rt as EVENTS_FILE,ce as NODE_DEFAULT_TOOLS,L as Node,H as OutputParser,Pt as RAW_OUTPUT_FILE,Ct as RESULT_FILE,he as SESSIONS_DIR,R as SESSION_INFO_FILE,Wt as SKILLS,me as STOP_REQUEST_FILE,bt as SchemaTypes,ie as Timeline,Ke as WORKFLOW_GRAPH_LOG_MARKER_PREFIX,te as WorkflowGraph,J as WorkflowState,ht as clearInheritedSessionEnvForFreshRun,Qe as clearSkills,Zt as compileGraph,Ht as extractSteps,qt as generateNodeConfigsJson,zt as generateWorkflowCode,gt as generateWorkflowSessionId,Ee as getAgentStrategy,qe as getAllSkills,ke as getNodeImpl,xe as getNodeTemplate,be as getResolvedToolDefinitions,U as getSkill,ae as hasNode,ze as hasSkill,rt as invokeAgent,Ut as listNodeTypes,Xe as listSkillIds,ot as listStrategies,ft as readPinnedSessionPathFromEnv,wt as registerNode,Ve as registerSkill,tt as registerStrategy,Ae as resolveNodeTools,St as resolveWorkflowSession,At as setLogger,pt as shouldTrustInheritedSessionEnv,mt as syncProcessEnvToSession,T as timeline,Jt as validateGraphConfig};
@@ -1,5 +1,5 @@
1
1
  export function registerNode(type: any, impl: any): void;
2
2
  export function getNodeImpl(type: any): any;
3
- export function hasNode(type: any): boolean;
3
+ export function hasNode(type: any): any;
4
4
  export function listNodeTypes(): any[];
5
5
  export function getNodeTemplate(type: any): any;
@@ -1,15 +1,15 @@
1
- var O=Object.defineProperty;var w=(e,t)=>()=>(e&&(t=e(e=0)),t);var C=(e,t)=>{for(var n in t)O(e,n,{get:t[n],enumerable:!0})};var x=w(()=>{});var S,P,h,k,T=w(()=>{S=()=>{},P={debug:S,info:S,warn:(...e)=>console.warn("[workflow]",...e),error:(...e)=>console.error("[workflow]",...e)},h={impl:P},k={debug:(...e)=>h.impl.debug?.(...e),info:(...e)=>h.impl.info?.(...e),warn:(...e)=>h.impl.warn?.(...e),error:(...e)=>h.impl.error?.(...e)}});function v(e){return Y.get(e)||null}var b,Y,A=w(()=>{b=Symbol.for("@zibby/agent-workflow.skills");globalThis[b]||(globalThis[b]=new Map);Y=globalThis[b]});var I={};C(I,{getAgentStrategy:()=>_,invokeAgent:()=>G,listStrategies:()=>j,registerStrategy:()=>M});function M(e){if(!e||typeof e.getName!="function"||typeof e.invoke!="function")throw new Error("strategy must implement getName() and invoke() (AgentStrategy shape)");let t=c.findIndex(n=>n.getName()===e.getName());t>=0?c[t]=e:c.push(e)}function j(){return c.map(e=>e.getName())}function _(e={}){let{state:t={},preferredAgent:n=null}=e,r=n||t.agentType||process.env.AGENT_TYPE;if(!r){let o=c.map(s=>s.getName()).join(", ")||"none registered";throw new Error(`No agent specified. Set agentType in state or AGENT_TYPE env var. Available: ${o}`)}k.debug(`[workflow] agent selection: requested=${r}`);let i=c.find(o=>o.getName()===r);if(!i){let o=c.map(s=>s.getName()).join(", ")||"none registered";throw new Error(`Unknown agent '${r}'. Available: ${o}`)}if(!i.canHandle(e))throw new Error(`Agent '${r}' is not available in this environment. Check credentials/environment.`);return k.debug(`[workflow] using agent: ${i.getName()}`),i}async function G(e,t={},n={}){let r=_(t),i=t.state?.config||n.config||{},o=i.models||{},s=n.nodeName&&o[n.nodeName]||null,l=o.default||null,m=i.agent?.[r.name]?.model||null,a=s||l||m||n.model||null,p={...n,model:a,workspace:t.state?.workspace||n.workspace,schema:n.schema||t.schema,images:n.images||t.images||[],skills:n.skills||t.skills||[],config:i},u=e,g=p.skills||[];if(g.length>0&&!n.skipPromptFragments){let E=g.map(R=>{let y=v(R)?.promptFragment;return typeof y=="function"?y():y}).filter(Boolean);E.length>0&&(u+=`
1
+ var C=Object.defineProperty;var w=(e,t)=>()=>(e&&(t=e(e=0)),t);var P=(e,t)=>{for(var n in t)C(e,n,{get:t[n],enumerable:!0})};var T=w(()=>{});var x,Y,h,k,v=w(()=>{x=()=>{},Y={debug:x,info:x,warn:(...e)=>console.warn("[workflow]",...e),error:(...e)=>console.error("[workflow]",...e)},h={impl:Y},k={debug:(...e)=>h.impl.debug?.(...e),info:(...e)=>h.impl.info?.(...e),warn:(...e)=>h.impl.warn?.(...e),error:(...e)=>h.impl.error?.(...e)}});function _(e){return G.get(e)||null}var y,G,A=w(()=>{y=Symbol.for("@zibby/agent-workflow.skills");globalThis[y]||(globalThis[y]=new Map);G=globalThis[y]});var R={};P(R,{getAgentStrategy:()=>I,invokeAgent:()=>z,listStrategies:()=>j,registerStrategy:()=>M});function M(e){if(!e||typeof e.getName!="function"||typeof e.invoke!="function")throw new Error("strategy must implement getName() and invoke() (AgentStrategy shape)");let t=c.findIndex(n=>n.getName()===e.getName());t>=0?c[t]=e:c.push(e)}function j(){return c.map(e=>e.getName())}function I(e={}){let{state:t={},preferredAgent:n=null}=e,r=n||t.agentType||process.env.AGENT_TYPE;if(!r){let o=c.map(s=>s.getName()).join(", ")||"none registered";throw new Error(`No agent specified. Set agentType in state or AGENT_TYPE env var. Available: ${o}`)}k.debug(`[workflow] agent selection: requested=${r}`);let i=c.find(o=>o.getName()===r);if(!i){let o=c.map(s=>s.getName()).join(", ")||"none registered";throw new Error(`Unknown agent '${r}'. Available: ${o}`)}if(!i.canHandle(e))throw new Error(`Agent '${r}' is not available in this environment. Check credentials/environment.`);return k.debug(`[workflow] using agent: ${i.getName()}`),i}async function z(e,t={},n={}){let r=I(t),i=t.state?.config||n.config||{},o=i.models||{},s=n.nodeName&&o[n.nodeName]||null,l=o.default||null,m=i.agent?.[r.name]?.model||null,a=s||l||m||n.model||null,p={...n,model:a,workspace:t.state?.workspace||n.workspace,schema:n.schema||t.schema,images:n.images||t.images||[],skills:n.skills||t.skills||[],config:i},g=e,u=p.skills||[];if(u.length>0&&!n.skipPromptFragments){let S=u.map(O=>{let b=_(O)?.promptFragment;return typeof b=="function"?b():b}).filter(Boolean);S.length>0&&(g+=`
2
2
 
3
- ${E.join(`
3
+ ${S.join(`
4
4
 
5
- `)}`)}let d=t.state?._currentNodeConfig?.extraPromptInstructions?.trim();return d&&(u+=`
5
+ `)}`)}let d=t.state?._currentNodeConfig?.extraPromptInstructions?.trim();return d&&(g+=`
6
6
 
7
7
  \u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501
8
8
  PRIORITY OVERRIDE \u2014 THE FOLLOWING INSTRUCTIONS TAKE PRECEDENCE OVER ALL PREVIOUS CONTENT
9
9
  \u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501
10
10
 
11
11
  ${d}
12
- `),k.debug(`[workflow] prompt length: ${u.length} chars`),r.invoke(u,p)}var N,c,$=w(()=>{x();T();A();N=Symbol.for("@zibby/agent-workflow.strategies");globalThis[N]||(globalThis[N]=[]);c=globalThis[N]});var f=new Map;function L(e,t){f.set(e,t)}function J(e){return f.get(e)}function W(e){return f.has(e)}function Q(){return Array.from(f.keys())}function X(e){let t=f.get(e);return t?t.factory&&typeof t.create=="function"?t.create.toString():typeof t.execute=="function"?t.execute.toString():typeof t=="function"?t.toString():null:null}L("ai_agent",{name:"ai_agent",factory:!0,create:(e,t={})=>({name:e,_isCustomCode:!0,execute:async n=>{let r=n?._coreInvokeAgent;r||(r=(await Promise.resolve().then(()=>($(),I))).invokeAgent);let i=t.extraPromptInstructions||"Execute the task based on the current state.",o=z(i,n),s=await r(o,{cwd:n.workspace||process.cwd(),model:n.model,tools:t.resolvedTools||null});return{success:!0,output:{raw:s,nodeId:e},raw:typeof s=="string"?s:s.raw}}})});function z(e,t){let n=/@([\w.]+)/g,r=new Set,i;for(;(i=n.exec(e))!==null;)r.add(i[1]);if(r.size===0)return e;let o=[],s=new Set;for(let l of r){let m=l.split(".")[0];if(s.has(m))continue;let a=l.split(".").reduce((g,d)=>g?.[d],t);if(a===void 0)continue;let p=typeof a=="string"?a:a?.raw??JSON.stringify(a,null,2),u=l.replace(/_/g," ").replace(/\b\w/g,g=>g.toUpperCase());o.push(`## ${u}
12
+ `),k.debug(`[workflow] prompt length: ${g.length} chars`),r.invoke(g,p)}var E,c,$=w(()=>{T();v();A();E=Symbol.for("@zibby/agent-workflow.strategies");globalThis[E]||(globalThis[E]=[]);c=globalThis[E]});var N=Symbol.for("@zibby/agent-workflow.nodes");globalThis[N]||(globalThis[N]=new Map);var f=globalThis[N];function L(e,t){f.set(e,t)}function W(e){return f.get(e)}function Q(e){return f.has(e)}function X(){return Array.from(f.keys())}function Z(e){let t=f.get(e);return t?t.factory&&typeof t.create=="function"?t.create.toString():typeof t.execute=="function"?t.execute.toString():typeof t=="function"?t.toString():null:null}L("ai_agent",{name:"ai_agent",factory:!0,create:(e,t={})=>({name:e,_isCustomCode:!0,execute:async n=>{let r=n?._coreInvokeAgent;r||(r=(await Promise.resolve().then(()=>($(),R))).invokeAgent);let i=t.extraPromptInstructions||"Execute the task based on the current state.",o=H(i,n),s=await r(o,{cwd:n.workspace||process.cwd(),model:n.model,tools:t.resolvedTools||null});return{success:!0,output:{raw:s,nodeId:e},raw:typeof s=="string"?s:s.raw}}})});function H(e,t){let n=/@([\w.]+)/g,r=new Set,i;for(;(i=n.exec(e))!==null;)r.add(i[1]);if(r.size===0)return e;let o=[],s=new Set;for(let l of r){let m=l.split(".")[0];if(s.has(m))continue;let a=l.split(".").reduce((u,d)=>u?.[d],t);if(a===void 0)continue;let p=typeof a=="string"?a:a?.raw??JSON.stringify(a,null,2),g=l.replace(/_/g," ").replace(/\b\w/g,u=>u.toUpperCase());o.push(`## ${g}
13
13
  ${p}`),l.includes(".")||s.add(m)}return o.length===0?e:`${e}
14
14
 
15
15
  ---
@@ -17,4 +17,4 @@ ${p}`),l.includes(".")||s.add(m)}return o.length===0?e:`${e}
17
17
 
18
18
  ${o.join(`
19
19
 
20
- `)}`}export{J as getNodeImpl,X as getNodeTemplate,W as hasNode,Q as listNodeTypes,L as registerNode};
20
+ `)}`}export{W as getNodeImpl,Z as getNodeTemplate,Q as hasNode,X as listNodeTypes,L as registerNode};
package/dist/node.js CHANGED
@@ -1,6 +1,6 @@
1
- var st=Object.defineProperty;var b=(r,t)=>()=>(r&&(t=r(r=0)),t);var it=(r,t)=>{for(var e in t)st(r,e,{get:t[e],enumerable:!0})};var D,nt,k,c,W=b(()=>{D=()=>{},nt={debug:D,info:D,warn:(...r)=>console.warn("[workflow]",...r),error:(...r)=>console.error("[workflow]",...r)},k={impl:nt},c={debug:(...r)=>k.impl.debug?.(...r),info:(...r)=>k.impl.info?.(...r),warn:(...r)=>k.impl.warn?.(...r),error:(...r)=>k.impl.error?.(...r)}});var Z=b(()=>{});function z(r){return ct.get(r)||null}var L,ct,V=b(()=>{L=Symbol.for("@zibby/agent-workflow.skills");globalThis[L]||(globalThis[L]=new Map);ct=globalThis[L]});var Q={};it(Q,{getAgentStrategy:()=>q,invokeAgent:()=>dt,listStrategies:()=>pt,registerStrategy:()=>ut});function ut(r){if(!r||typeof r.getName!="function"||typeof r.invoke!="function")throw new Error("strategy must implement getName() and invoke() (AgentStrategy shape)");let t=w.findIndex(e=>e.getName()===r.getName());t>=0?w[t]=r:w.push(r)}function pt(){return w.map(r=>r.getName())}function q(r={}){let{state:t={},preferredAgent:e=null}=r,o=e||t.agentType||process.env.AGENT_TYPE;if(!o){let n=w.map(a=>a.getName()).join(", ")||"none registered";throw new Error(`No agent specified. Set agentType in state or AGENT_TYPE env var. Available: ${n}`)}c.debug(`[workflow] agent selection: requested=${o}`);let i=w.find(n=>n.getName()===o);if(!i){let n=w.map(a=>a.getName()).join(", ")||"none registered";throw new Error(`Unknown agent '${o}'. Available: ${n}`)}if(!i.canHandle(r))throw new Error(`Agent '${o}' is not available in this environment. Check credentials/environment.`);return c.debug(`[workflow] using agent: ${i.getName()}`),i}async function dt(r,t={},e={}){let o=q(t),i=t.state?.config||e.config||{},n=i.models||{},a=e.nodeName&&n[e.nodeName]||null,g=n.default||null,p=i.agent?.[o.name]?.model||null,_=a||g||p||e.model||null,E={...e,model:_,workspace:t.state?.workspace||e.workspace,schema:e.schema||t.schema,images:e.images||t.images||[],skills:e.skills||t.skills||[],config:i},s=r,h=E.skills||[];if(h.length>0&&!e.skipPromptFragments){let y=h.map(O=>{let $=z(O)?.promptFragment;return typeof $=="function"?$():$}).filter(Boolean);y.length>0&&(s+=`
1
+ var st=Object.defineProperty;var I=(r,t)=>()=>(r&&(t=r(r=0)),t);var it=(r,t)=>{for(var e in t)st(r,e,{get:t[e],enumerable:!0})};var D,nt,N,u,R=I(()=>{D=()=>{},nt={debug:D,info:D,warn:(...r)=>console.warn("[workflow]",...r),error:(...r)=>console.error("[workflow]",...r)},N={impl:nt},u={debug:(...r)=>N.impl.debug?.(...r),info:(...r)=>N.impl.info?.(...r),warn:(...r)=>N.impl.warn?.(...r),error:(...r)=>N.impl.error?.(...r)}});var Z=I(()=>{});function V(r){return ut.get(r)||null}var v,ut,z=I(()=>{v=Symbol.for("@zibby/agent-workflow.skills");globalThis[v]||(globalThis[v]=new Map);ut=globalThis[v]});var X={};it(X,{getAgentStrategy:()=>q,invokeAgent:()=>dt,listStrategies:()=>pt,registerStrategy:()=>ct});function ct(r){if(!r||typeof r.getName!="function"||typeof r.invoke!="function")throw new Error("strategy must implement getName() and invoke() (AgentStrategy shape)");let t=S.findIndex(e=>e.getName()===r.getName());t>=0?S[t]=r:S.push(r)}function pt(){return S.map(r=>r.getName())}function q(r={}){let{state:t={},preferredAgent:e=null}=r,o=e||t.agentType||process.env.AGENT_TYPE;if(!o){let n=S.map(a=>a.getName()).join(", ")||"none registered";throw new Error(`No agent specified. Set agentType in state or AGENT_TYPE env var. Available: ${n}`)}u.debug(`[workflow] agent selection: requested=${o}`);let i=S.find(n=>n.getName()===o);if(!i){let n=S.map(a=>a.getName()).join(", ")||"none registered";throw new Error(`Unknown agent '${o}'. Available: ${n}`)}if(!i.canHandle(r))throw new Error(`Agent '${o}' is not available in this environment. Check credentials/environment.`);return u.debug(`[workflow] using agent: ${i.getName()}`),i}async function dt(r,t={},e={}){let o=q(t),i=t.state?.config||e.config||{},n=i.models||{},a=e.nodeName&&n[e.nodeName]||null,g=n.default||null,p=i.agent?.[o.name]?.model||null,w=a||g||p||e.model||null,y={...e,model:w,workspace:t.state?.workspace||e.workspace,schema:e.schema||t.schema,images:e.images||t.images||[],skills:e.skills||t.skills||[],config:i},s=r,h=y.skills||[];if(h.length>0&&!e.skipPromptFragments){let E=h.map(b=>{let $=V(b)?.promptFragment;return typeof $=="function"?$():$}).filter(Boolean);E.length>0&&(s+=`
2
2
 
3
- ${y.join(`
3
+ ${E.join(`
4
4
 
5
5
  `)}`)}let f=t.state?._currentNodeConfig?.extraPromptInstructions?.trim();return f&&(s+=`
6
6
 
@@ -9,21 +9,21 @@ PRIORITY OVERRIDE \u2014 THE FOLLOWING INSTRUCTIONS TAKE PRECEDENCE OVER ALL PRE
9
9
  \u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501
10
10
 
11
11
  ${f}
12
- `),c.debug(`[workflow] prompt length: ${s.length} chars`),o.invoke(s,E)}var P,w,X=b(()=>{Z();W();V();P=Symbol.for("@zibby/agent-workflow.strategies");globalThis[P]||(globalThis[P]=[]);w=globalThis[P]});var N=class{constructor(t){this.schema=t}parse(t){let e=t.match(/```json\s*([\s\S]*?)\s*```/);if(e)return this.validate(JSON.parse(e[1]));let o=[t.match(/\{[\s\S]*?\}/),t.match(/\{[\s\S]*\}/)].filter(Boolean).map(i=>i[0]);for(let i of o)try{return this.validate(JSON.parse(i))}catch(n){if(!(n instanceof SyntaxError))throw n}return this.validate({result:t.trim()})}validate(t){let e=[];for(let[o,i]of Object.entries(this.schema)){if(i.required&&!(o in t)&&e.push(`Missing required field: ${o}`),o in t&&i.type){let n=typeof t[o];n!==i.type&&e.push(`Field '${o}' expected ${i.type}, got ${n}`)}if(i.validate&&o in t){let n=i.validate(t[o]);n&&e.push(`Field '${o}': ${n}`)}}if(e.length>0)throw new Error(`Output validation failed:
12
+ `),u.debug(`[workflow] prompt length: ${s.length} chars`),o.invoke(s,y)}var P,S,Q=I(()=>{Z();R();z();P=Symbol.for("@zibby/agent-workflow.strategies");globalThis[P]||(globalThis[P]=[]);S=globalThis[P]});var k=class{constructor(t){this.schema=t}parse(t){let e=t.match(/```json\s*([\s\S]*?)\s*```/);if(e)return this.validate(JSON.parse(e[1]));let o=[t.match(/\{[\s\S]*?\}/),t.match(/\{[\s\S]*\}/)].filter(Boolean).map(i=>i[0]);for(let i of o)try{return this.validate(JSON.parse(i))}catch(n){if(!(n instanceof SyntaxError))throw n}return this.validate({result:t.trim()})}validate(t){let e=[];for(let[o,i]of Object.entries(this.schema)){if(i.required&&!(o in t)&&e.push(`Missing required field: ${o}`),o in t&&i.type){let n=typeof t[o];n!==i.type&&e.push(`Field '${o}' expected ${i.type}, got ${n}`)}if(i.validate&&o in t){let n=i.validate(t[o]);n&&e.push(`Field '${o}': ${n}`)}}if(e.length>0)throw new Error(`Output validation failed:
13
13
  ${e.join(`
14
- `)}`);return t}};W();import{writeFileSync as C,readFileSync as tt,existsSync as et,mkdirSync as ht}from"node:fs";import{join as F,dirname as ft}from"node:path";import l from"chalk";var at="__WORKFLOW_GRAPH_LOG__",I=l.gray("\u2502"),lt=l.gray("\u250C"),B=l.gray("\u2514"),T=l.green("\u25C6"),G=l.hex("#c084fc")("\u25C6"),U=l.hex("#2dd4bf")("\u25C6"),A=l.red("\u25C6"),Y=`${I} `,j=2;function J(r){return r<1e3?`${r}ms`:`${(r/1e3).toFixed(1)}s`}function H(r,t){return(e,o,i)=>{if(typeof e!="string")return r(e,o,i);let n=process.stdout.columns||120,a="";for(let g=0;g<e.length;g++){let p=e[g];t.lineStart&&(a+=Y,t.col=j,t.lineStart=!1),p===`
14
+ `)}`);return t}};R();import{writeFileSync as C,readFileSync as tt,existsSync as et,mkdirSync as ht}from"node:fs";import{join as F,dirname as ft}from"node:path";import l from"chalk";var at="__WORKFLOW_GRAPH_LOG__",O=l.gray("\u2502"),lt=l.gray("\u250C"),G=l.gray("\u2514"),x=l.green("\u25C6"),U=l.hex("#c084fc")("\u25C6"),j=l.hex("#2dd4bf")("\u25C6"),T=l.red("\u25C6"),J=`${O} `,H=2;function K(r){return r<1e3?`${r}ms`:`${(r/1e3).toFixed(1)}s`}function B(r,t){return(e,o,i)=>{if(typeof e!="string")return r(e,o,i);let n=process.stdout.columns||120,a="";for(let g=0;g<e.length;g++){let p=e[g];t.lineStart&&(a+=J,t.col=H,t.lineStart=!1),p===`
15
15
  `?(a+=p,t.lineStart=!0,t.col=0,t.inEsc=!1):p==="\x1B"?(t.inEsc=!0,a+=p):t.inEsc?(a+=p,(p>="A"&&p<="Z"||p>="a"&&p<="z")&&(t.inEsc=!1)):(t.col++,a+=p,t.col>=n&&(a+=`
16
- ${Y}`,t.col=j))}return r(a,o,i)}}var x=class{constructor(){this._currentNode=null,this._origStdoutWrite=null,this._origStderrWrite=null;let t=String(process.env.ZIBBY_EMIT_GRAPH_MARKERS||"").trim()==="1"||String(process.env.ZIBBY_WORKFLOW_GRAPH_LOG_MARKERS||"").trim()==="1",e=!t&&String(process.env.ZIBBY_RUN_SOURCE||"").trim().toLowerCase()==="studio";e&&process.env.ZIBBY_NO_DEPRECATION_WARNINGS!=="1"&&console.warn("[zibby/agent-workflow] `ZIBBY_RUN_SOURCE=studio` env var is deprecated for graph-marker emission. Set `ZIBBY_EMIT_GRAPH_MARKERS=1` instead. The Studio-specific value will be ignored in v2. Suppress with ZIBBY_NO_DEPRECATION_WARNINGS=1."),this._emitWorkflowGraphMarkers=t||e}get isInsideNode(){return this._currentNode!==null}_startIntercepting(){this._origStdoutWrite=process.stdout.write.bind(process.stdout),this._origStderrWrite=process.stderr.write.bind(process.stderr);let t={lineStart:!0,col:0,inEsc:!1},e={lineStart:!0,col:0,inEsc:!1};this._outState=t,this._errState=e,process.stdout.write=H(this._origStdoutWrite,t),process.stderr.write=H(this._origStderrWrite,e)}_stopIntercepting(){this._origStdoutWrite&&(this._outState&&!this._outState.lineStart&&this._origStdoutWrite(`
16
+ ${J}`,t.col=H))}return r(a,o,i)}}var A=class{constructor(){this._currentNode=null,this._origStdoutWrite=null,this._origStderrWrite=null,this._emitWorkflowGraphMarkers=String(process.env.ZIBBY_EMIT_GRAPH_MARKERS||"").trim()==="1"||String(process.env.ZIBBY_WORKFLOW_GRAPH_LOG_MARKERS||"").trim()==="1"}get isInsideNode(){return this._currentNode!==null}_startIntercepting(){this._origStdoutWrite=process.stdout.write.bind(process.stdout),this._origStderrWrite=process.stderr.write.bind(process.stderr);let t={lineStart:!0,col:0,inEsc:!1},e={lineStart:!0,col:0,inEsc:!1};this._outState=t,this._errState=e,process.stdout.write=B(this._origStdoutWrite,t),process.stderr.write=B(this._origStderrWrite,e)}_stopIntercepting(){this._origStdoutWrite&&(this._outState&&!this._outState.lineStart&&this._origStdoutWrite(`
17
17
  `),process.stdout.write=this._origStdoutWrite),this._origStderrWrite&&(this._errState&&!this._errState.lineStart&&this._origStderrWrite(`
18
18
  `),process.stderr.write=this._origStderrWrite),this._origStdoutWrite=null,this._origStderrWrite=null}_rawWrite(t){(this._origStdoutWrite||process.stdout.write.bind(process.stdout))(`${t}
19
19
  `)}_emitGraphLogMarker(t){if(!this._emitWorkflowGraphMarkers)return;let e=`${at}${JSON.stringify(t)}
20
20
  `;this._origStdoutWrite?this._origStdoutWrite(e):process.stdout.write(e)}_writeDot(t,e){this._origStdoutWrite?(this._outState&&!this._outState.lineStart&&(this._origStdoutWrite(`
21
21
  `),this._outState.lineStart=!0,this._outState.col=0),this._origStdoutWrite(`${t} ${e}
22
22
  `)):process.stdout.write.bind(process.stdout)(`${t} ${e}
23
- `)}step(t){this._origStdoutWrite?this._writeDot(T,t):process.stdout.write.bind(process.stdout)(`${I} ${T} ${t}
24
- `)}stepInfo(t){this.step(t)}stepTool(t){this._origStdoutWrite?this._writeDot(G,t):process.stdout.write.bind(process.stdout)(`${I} ${G} ${t}
25
- `)}stepMemory(t){let e=l.hex("#2dd4bf")(t);this._origStdoutWrite?this._writeDot(U,e):process.stdout.write.bind(process.stdout)(`${I} ${U} ${e}
26
- `)}stepFail(t){this._origStdoutWrite?this._writeDot(A,l.red(t)):process.stdout.write.bind(process.stdout)(`${I} ${A} ${l.red(t)}
27
- `)}nodeStart(t){this._currentNode=t,this._emitGraphLogMarker({phase:"node_begin",node:t}),this._rawWrite(`${lt} ${t}`),this._startIntercepting()}nodeComplete(t,e={}){this._stopIntercepting();let{duration:o,details:i}=e;if(i)for(let a of i)this._rawWrite(`${T} ${a}`);let n=o?l.dim(` ${J(o)}`):"";this._rawWrite(`${B} ${l.green("done")}${n}`),this._emitGraphLogMarker({phase:"node_end",node:t}),this._rawWrite("")}nodeFailed(t,e,o={}){this._stopIntercepting();let{duration:i}=o,n=i?l.dim(` ${J(i)}`):"";this._rawWrite(`${A} ${l.red(e)}`),this._rawWrite(`${B} ${l.red("failed")}${n}`),this._emitGraphLogMarker({phase:"node_end",node:t}),this._rawWrite("")}route(t,e){this._rawWrite(l.dim(` ${t} \u2192 ${e}`)),this._rawWrite("")}graphComplete(){this._rawWrite(l.green.bold("\u2713 Workflow completed"))}},K=new x;var v=".session-info.json";var M=class{constructor(t){if(this.config=t,this.name=t.name,this.prompt=t.prompt,this.outputSchema=t.outputSchema,!this.outputSchema&&!t._isCustomCode)throw new Error(`Node '${this.name}' must define outputSchema (Zod schema). This defines the contract for what the node returns to state.`);this.isZodSchema=this.outputSchema&&typeof this.outputSchema._def<"u",this.parser=t.outputSchema&&!this.isZodSchema?new N(t.outputSchema):null,this.retries=t.retries||0,this.onComplete=t.onComplete,this.customExecute=t.execute}async execute(t,e){let o=()=>e&&typeof e.getAll=="function"?e.getAll():t,i=s=>e&&typeof e.get=="function"?e.get(s):t?.[s];if(typeof this.customExecute=="function"){c.debug(`[workflow] node '${this.name}': custom execute (skipping LLM)`);try{let s=await this.customExecute(t);return typeof s=="object"&&s!==null&&s.success===!1?{success:!1,error:s.error||"Node execution failed",raw:s.raw||null}:this.isZodSchema?(c.debug(`[workflow] node '${this.name}': validating output schema`),{success:!0,output:this.outputSchema.parse(s),raw:null}):{success:!0,output:s,raw:null}}catch(s){return c.error(`[workflow] node '${this.name}' failed: ${s.message}`),s.name==="ZodError"&&c.error(`Schema errors: ${JSON.stringify(s.issues||s.errors,null,2)}`),{success:!1,error:s.message,raw:null}}}let n=typeof this.prompt=="function"?this.prompt(o()):this.prompt,a=i("_skillHints");a&&(n=`${a}
23
+ `)}step(t){this._origStdoutWrite?this._writeDot(x,t):process.stdout.write.bind(process.stdout)(`${O} ${x} ${t}
24
+ `)}stepInfo(t){this.step(t)}stepTool(t){this._origStdoutWrite?this._writeDot(U,t):process.stdout.write.bind(process.stdout)(`${O} ${U} ${t}
25
+ `)}stepMemory(t){let e=l.hex("#2dd4bf")(t);this._origStdoutWrite?this._writeDot(j,e):process.stdout.write.bind(process.stdout)(`${O} ${j} ${e}
26
+ `)}stepFail(t){this._origStdoutWrite?this._writeDot(T,l.red(t)):process.stdout.write.bind(process.stdout)(`${O} ${T} ${l.red(t)}
27
+ `)}nodeStart(t){this._currentNode=t,this._emitGraphLogMarker({phase:"node_begin",node:t}),this._rawWrite(`${lt} ${t}`),this._startIntercepting()}nodeComplete(t,e={}){this._stopIntercepting();let{duration:o,details:i}=e;if(i)for(let a of i)this._rawWrite(`${x} ${a}`);let n=o?l.dim(` ${K(o)}`):"";this._rawWrite(`${G} ${l.green("done")}${n}`),this._emitGraphLogMarker({phase:"node_end",node:t}),this._rawWrite("")}nodeFailed(t,e,o={}){this._stopIntercepting();let{duration:i}=o,n=i?l.dim(` ${K(i)}`):"";this._rawWrite(`${T} ${l.red(e)}`),this._rawWrite(`${G} ${l.red("failed")}${n}`),this._emitGraphLogMarker({phase:"node_end",node:t}),this._rawWrite("")}route(t,e){this._rawWrite(l.dim(` ${t} \u2192 ${e}`)),this._rawWrite("")}graphComplete(){this._rawWrite(l.green.bold("\u2713 Workflow completed"))}},Y=new A;var L=".session-info.json";var M=class{constructor(t){if(this.config=t,this.name=t.name,this.prompt=t.prompt,this.outputSchema=t.outputSchema,!this.outputSchema&&!t._isCustomCode)throw new Error(`Node '${this.name}' must define outputSchema (Zod schema). This defines the contract for what the node returns to state.`);this.isZodSchema=this.outputSchema&&typeof this.outputSchema._def<"u",this.parser=t.outputSchema&&!this.isZodSchema?new k(t.outputSchema):null,this.retries=t.retries||0,this.onComplete=t.onComplete,this.customExecute=t.execute}async execute(t,e){let o=()=>e&&typeof e.getAll=="function"?e.getAll():t,i=s=>e&&typeof e.get=="function"?e.get(s):t?.[s];if(typeof this.customExecute=="function"){u.debug(`[workflow] node '${this.name}': custom execute (skipping LLM)`);try{let s=await this.customExecute(t);return typeof s=="object"&&s!==null&&s.success===!1?{success:!1,error:s.error||"Node execution failed",raw:s.raw||null}:this.isZodSchema?(u.debug(`[workflow] node '${this.name}': validating output schema`),{success:!0,output:this.outputSchema.parse(s),raw:null}):{success:!0,output:s,raw:null}}catch(s){return u.error(`[workflow] node '${this.name}' failed: ${s.message}`),s.name==="ZodError"&&u.error(`Schema errors: ${JSON.stringify(s.issues||s.errors,null,2)}`),{success:!1,error:s.message,raw:null}}}let n=typeof this.prompt=="function"?this.prompt(o()):this.prompt,a=i("_skillHints");a&&(n=`${a}
28
28
 
29
- ${n}`);let g=o(),p=g.cwd||process.cwd(),_=g.sessionPath;try{if(_){let s=F(_,v);if(et(s)){let f=JSON.parse(tt(s,"utf-8"));f.currentNode=this.name,C(s,JSON.stringify(f,null,2),"utf-8")}let h=F(_,"..",v);if(et(h))try{let f=JSON.parse(tt(h,"utf-8"));f.currentNode=this.name,C(h,JSON.stringify(f,null,2),"utf-8")}catch{}}}catch(s){c.debug(`[workflow] could not update session info: ${s.message}`)}let E=null;for(let s=0;s<=this.retries;s++)try{c.debug(`[workflow] node '${this.name}' attempt ${s}`);let h=o().config||{},f=h.agents||{},y=this.config.agent??f[this.name]??null,O={state:o()};y&&(O.preferredAgent=y);let $={workspace:p,schema:this.isZodSchema?this.outputSchema:null,skills:this.config.skills||[],sessionPath:_,config:h,nodeName:this.name,timeout:this.config?.timeout||3e5},R=t?._coreInvokeAgent;R||(R=(await Promise.resolve().then(()=>(X(),Q))).invokeAgent);let m=await R(n,O,$),d,S;if(typeof m=="string"?(d=m,S=null):m.structured?(d=m.raw||JSON.stringify(m.structured,null,2),S=m.structured):(d=m.raw||JSON.stringify(m,null,2),S=m.extracted||null),_)try{let u=F(_,this.name,"raw_stream_output.txt");ht(ft(u),{recursive:!0}),C(u,typeof d=="string"?d:JSON.stringify(d),"utf-8")}catch(u){c.debug(`[workflow] could not save raw output: ${u.message}`)}if(this.isZodSchema&&S){c.info(`[workflow] node '${this.name}': output validated: ${JSON.stringify(S,null,2)}`);let u=S;if(typeof this.onComplete=="function")try{u=await this.onComplete(o(),S)}catch(ot){c.warn(`[workflow] onComplete hook failed: ${ot.message}`)}return{success:!0,output:u,raw:d}}if(typeof this.onComplete=="function")try{return{success:!0,output:await this.onComplete(o(),{raw:d}),raw:d}}catch(u){throw new Error(`onComplete failed: ${u.message}`,{cause:u})}if(this.parser){let u=this.parser.parse(d);return c.info(`[workflow] node '${this.name}': parsed output: ${JSON.stringify(u,null,2)}`),K.step("Output parsed"),{success:!0,output:u,raw:d}}return{success:!0,output:d,raw:d}}catch(h){E=h,s<this.retries&&c.info(`[workflow] node '${this.name}' failed, retrying (${s+1}/${this.retries})\u2026`)}return{success:!1,error:E.message,raw:null}}},rt=class extends M{constructor(t){super({...t,_isCustomCode:!0}),this.condition=t.condition}async execute(t,e){let o=e&&typeof e.getAll=="function"?e.getAll():t;return{success:!0,output:{nextNode:this.condition(o)},raw:null}}};export{rt as ConditionalNode,M as Node};
29
+ ${n}`);let g=o(),p=g.cwd||process.cwd(),w=g.sessionPath;try{if(w){let s=F(w,L);if(et(s)){let f=JSON.parse(tt(s,"utf-8"));f.currentNode=this.name,C(s,JSON.stringify(f,null,2),"utf-8")}let h=F(w,"..",L);if(et(h))try{let f=JSON.parse(tt(h,"utf-8"));f.currentNode=this.name,C(h,JSON.stringify(f,null,2),"utf-8")}catch{}}}catch(s){u.debug(`[workflow] could not update session info: ${s.message}`)}let y=null;for(let s=0;s<=this.retries;s++)try{u.debug(`[workflow] node '${this.name}' attempt ${s}`);let h=o().config||{},f=h.agents||{},E=this.config.agent??f[this.name]??null,b={state:o()};E&&(b.preferredAgent=E);let $={workspace:p,schema:this.isZodSchema?this.outputSchema:null,skills:this.config.skills||[],sessionPath:w,config:h,nodeName:this.name,timeout:this.config?.timeout||3e5},W=t?._coreInvokeAgent;W||(W=(await Promise.resolve().then(()=>(Q(),X))).invokeAgent);let m=await W(n,b,$),d,_;if(typeof m=="string"?(d=m,_=null):m.structured?(d=m.raw||JSON.stringify(m.structured,null,2),_=m.structured):(d=m.raw||JSON.stringify(m,null,2),_=m.extracted||null),w)try{let c=F(w,this.name,"raw_stream_output.txt");ht(ft(c),{recursive:!0}),C(c,typeof d=="string"?d:JSON.stringify(d),"utf-8")}catch(c){u.debug(`[workflow] could not save raw output: ${c.message}`)}if(this.isZodSchema&&_){u.info(`[workflow] node '${this.name}': output validated: ${JSON.stringify(_,null,2)}`);let c=_;if(typeof this.onComplete=="function")try{c=await this.onComplete(o(),_)}catch(ot){u.warn(`[workflow] onComplete hook failed: ${ot.message}`)}return{success:!0,output:c,raw:d}}if(typeof this.onComplete=="function")try{return{success:!0,output:await this.onComplete(o(),{raw:d}),raw:d}}catch(c){throw new Error(`onComplete failed: ${c.message}`,{cause:c})}if(this.parser){let c=this.parser.parse(d);return u.info(`[workflow] node '${this.name}': parsed output: ${JSON.stringify(c,null,2)}`),Y.step("Output parsed"),{success:!0,output:c,raw:d}}return{success:!0,output:d,raw:d}}catch(h){y=h,s<this.retries&&u.info(`[workflow] node '${this.name}' failed, retrying (${s+1}/${this.retries})\u2026`)}return{success:!1,error:y.message,raw:null}}},rt=class extends M{constructor(t){super({...t,_isCustomCode:!0}),this.condition=t.condition}async execute(t,e){let o=e&&typeof e.getAll=="function"?e.getAll():t;return{success:!0,output:{nextNode:this.condition(o)},raw:null}}};export{rt as ConditionalNode,M as Node};
package/dist/timeline.js CHANGED
@@ -1,14 +1,14 @@
1
- import e from"chalk";var I="__WORKFLOW_GRAPH_LOG__",c=e.gray("\u2502"),R=e.gray("\u250C"),p=e.gray("\u2514"),h=e.green("\u25C6"),S=e.hex("#c084fc")("\u25C6"),w=e.hex("#2dd4bf")("\u25C6"),l=e.red("\u25C6"),W=`${c} `,f=2;function g(_){return _<1e3?`${_}ms`:`${(_/1e3).toFixed(1)}s`}function $(_,t){return(r,o,n)=>{if(typeof r!="string")return _(r,o,n);let d=process.stdout.columns||120,s="";for(let u=0;u<r.length;u++){let i=r[u];t.lineStart&&(s+=W,t.col=f,t.lineStart=!1),i===`
2
- `?(s+=i,t.lineStart=!0,t.col=0,t.inEsc=!1):i==="\x1B"?(t.inEsc=!0,s+=i):t.inEsc?(s+=i,(i>="A"&&i<="Z"||i>="a"&&i<="z")&&(t.inEsc=!1)):(t.col++,s+=i,t.col>=d&&(s+=`
3
- ${W}`,t.col=f))}return _(s,o,n)}}var a=class{constructor(){this._currentNode=null,this._origStdoutWrite=null,this._origStderrWrite=null;let t=String(process.env.ZIBBY_EMIT_GRAPH_MARKERS||"").trim()==="1"||String(process.env.ZIBBY_WORKFLOW_GRAPH_LOG_MARKERS||"").trim()==="1",r=!t&&String(process.env.ZIBBY_RUN_SOURCE||"").trim().toLowerCase()==="studio";r&&process.env.ZIBBY_NO_DEPRECATION_WARNINGS!=="1"&&console.warn("[zibby/agent-workflow] `ZIBBY_RUN_SOURCE=studio` env var is deprecated for graph-marker emission. Set `ZIBBY_EMIT_GRAPH_MARKERS=1` instead. The Studio-specific value will be ignored in v2. Suppress with ZIBBY_NO_DEPRECATION_WARNINGS=1."),this._emitWorkflowGraphMarkers=t||r}get isInsideNode(){return this._currentNode!==null}_startIntercepting(){this._origStdoutWrite=process.stdout.write.bind(process.stdout),this._origStderrWrite=process.stderr.write.bind(process.stderr);let t={lineStart:!0,col:0,inEsc:!1},r={lineStart:!0,col:0,inEsc:!1};this._outState=t,this._errState=r,process.stdout.write=$(this._origStdoutWrite,t),process.stderr.write=$(this._origStderrWrite,r)}_stopIntercepting(){this._origStdoutWrite&&(this._outState&&!this._outState.lineStart&&this._origStdoutWrite(`
1
+ import e from"chalk";var I="__WORKFLOW_GRAPH_LOG__",c=e.gray("\u2502"),E=e.gray("\u250C"),a=e.gray("\u2514"),u=e.green("\u25C6"),S=e.hex("#c084fc")("\u25C6"),W=e.hex("#2dd4bf")("\u25C6"),l=e.red("\u25C6"),w=`${c} `,f=2;function g(d){return d<1e3?`${d}ms`:`${(d/1e3).toFixed(1)}s`}function $(d,t){return(r,o,n)=>{if(typeof r!="string")return d(r,o,n);let _=process.stdout.columns||120,s="";for(let h=0;h<r.length;h++){let i=r[h];t.lineStart&&(s+=w,t.col=f,t.lineStart=!1),i===`
2
+ `?(s+=i,t.lineStart=!0,t.col=0,t.inEsc=!1):i==="\x1B"?(t.inEsc=!0,s+=i):t.inEsc?(s+=i,(i>="A"&&i<="Z"||i>="a"&&i<="z")&&(t.inEsc=!1)):(t.col++,s+=i,t.col>=_&&(s+=`
3
+ ${w}`,t.col=f))}return d(s,o,n)}}var p=class{constructor(){this._currentNode=null,this._origStdoutWrite=null,this._origStderrWrite=null,this._emitWorkflowGraphMarkers=String(process.env.ZIBBY_EMIT_GRAPH_MARKERS||"").trim()==="1"||String(process.env.ZIBBY_WORKFLOW_GRAPH_LOG_MARKERS||"").trim()==="1"}get isInsideNode(){return this._currentNode!==null}_startIntercepting(){this._origStdoutWrite=process.stdout.write.bind(process.stdout),this._origStderrWrite=process.stderr.write.bind(process.stderr);let t={lineStart:!0,col:0,inEsc:!1},r={lineStart:!0,col:0,inEsc:!1};this._outState=t,this._errState=r,process.stdout.write=$(this._origStdoutWrite,t),process.stderr.write=$(this._origStderrWrite,r)}_stopIntercepting(){this._origStdoutWrite&&(this._outState&&!this._outState.lineStart&&this._origStdoutWrite(`
4
4
  `),process.stdout.write=this._origStdoutWrite),this._origStderrWrite&&(this._errState&&!this._errState.lineStart&&this._origStderrWrite(`
5
5
  `),process.stderr.write=this._origStderrWrite),this._origStdoutWrite=null,this._origStderrWrite=null}_rawWrite(t){(this._origStdoutWrite||process.stdout.write.bind(process.stdout))(`${t}
6
6
  `)}_emitGraphLogMarker(t){if(!this._emitWorkflowGraphMarkers)return;let r=`${I}${JSON.stringify(t)}
7
7
  `;this._origStdoutWrite?this._origStdoutWrite(r):process.stdout.write(r)}_writeDot(t,r){this._origStdoutWrite?(this._outState&&!this._outState.lineStart&&(this._origStdoutWrite(`
8
8
  `),this._outState.lineStart=!0,this._outState.col=0),this._origStdoutWrite(`${t} ${r}
9
9
  `)):process.stdout.write.bind(process.stdout)(`${t} ${r}
10
- `)}step(t){this._origStdoutWrite?this._writeDot(h,t):process.stdout.write.bind(process.stdout)(`${c} ${h} ${t}
10
+ `)}step(t){this._origStdoutWrite?this._writeDot(u,t):process.stdout.write.bind(process.stdout)(`${c} ${u} ${t}
11
11
  `)}stepInfo(t){this.step(t)}stepTool(t){this._origStdoutWrite?this._writeDot(S,t):process.stdout.write.bind(process.stdout)(`${c} ${S} ${t}
12
- `)}stepMemory(t){let r=e.hex("#2dd4bf")(t);this._origStdoutWrite?this._writeDot(w,r):process.stdout.write.bind(process.stdout)(`${c} ${w} ${r}
12
+ `)}stepMemory(t){let r=e.hex("#2dd4bf")(t);this._origStdoutWrite?this._writeDot(W,r):process.stdout.write.bind(process.stdout)(`${c} ${W} ${r}
13
13
  `)}stepFail(t){this._origStdoutWrite?this._writeDot(l,e.red(t)):process.stdout.write.bind(process.stdout)(`${c} ${l} ${e.red(t)}
14
- `)}nodeStart(t){this._currentNode=t,this._emitGraphLogMarker({phase:"node_begin",node:t}),this._rawWrite(`${R} ${t}`),this._startIntercepting()}nodeComplete(t,r={}){this._stopIntercepting();let{duration:o,details:n}=r;if(n)for(let s of n)this._rawWrite(`${h} ${s}`);let d=o?e.dim(` ${g(o)}`):"";this._rawWrite(`${p} ${e.green("done")}${d}`),this._emitGraphLogMarker({phase:"node_end",node:t}),this._rawWrite("")}nodeFailed(t,r,o={}){this._stopIntercepting();let{duration:n}=o,d=n?e.dim(` ${g(n)}`):"";this._rawWrite(`${l} ${e.red(r)}`),this._rawWrite(`${p} ${e.red("failed")}${d}`),this._emitGraphLogMarker({phase:"node_end",node:t}),this._rawWrite("")}route(t,r){this._rawWrite(e.dim(` ${t} \u2192 ${r}`)),this._rawWrite("")}graphComplete(){this._rawWrite(e.green.bold("\u2713 Workflow completed"))}},E=new a;var P=E;export{a as Timeline,I as WORKFLOW_GRAPH_LOG_MARKER_PREFIX,P as default,E as timeline};
14
+ `)}nodeStart(t){this._currentNode=t,this._emitGraphLogMarker({phase:"node_begin",node:t}),this._rawWrite(`${E} ${t}`),this._startIntercepting()}nodeComplete(t,r={}){this._stopIntercepting();let{duration:o,details:n}=r;if(n)for(let s of n)this._rawWrite(`${u} ${s}`);let _=o?e.dim(` ${g(o)}`):"";this._rawWrite(`${a} ${e.green("done")}${_}`),this._emitGraphLogMarker({phase:"node_end",node:t}),this._rawWrite("")}nodeFailed(t,r,o={}){this._stopIntercepting();let{duration:n}=o,_=n?e.dim(` ${g(n)}`):"";this._rawWrite(`${l} ${e.red(r)}`),this._rawWrite(`${a} ${e.red("failed")}${_}`),this._emitGraphLogMarker({phase:"node_end",node:t}),this._rawWrite("")}route(t,r){this._rawWrite(e.dim(` ${t} \u2192 ${r}`)),this._rawWrite("")}graphComplete(){this._rawWrite(e.green.bold("\u2713 Workflow completed"))}},O=new p;var P=O;export{p as Timeline,I as WORKFLOW_GRAPH_LOG_MARKER_PREFIX,P as default,O as timeline};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zibby/agent-workflow",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Graph-based AI agent workflow orchestration. Bring your own agent strategies.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",