@zibby/agent-workflow 0.1.2 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -33,27 +33,26 @@ graph
33
33
 
34
34
  A complete loop — generate, run locally, deploy to cloud, trigger remotely, watch logs. No global install needed:
35
35
 
36
- ```bash
37
- # 1. Project scaffold (.zibby/, config, graph.mjs)
38
- npx @zibby/cli init
36
+ No setup step. The first command bootstraps `.zibby/workflows/` for you.
39
37
 
40
- # 2. Generate a new workflow under .zibby/workflows/my-pipeline/
41
- npx @zibby/cli g workflow my-pipeline
38
+ ```bash
39
+ # 1. Generate a workflow — creates .zibby/workflows/my-pipeline/ + graph.mjs
40
+ npx @zibby/cli workflow new my-pipeline
42
41
 
43
- # 3. Run it locally — names are folder names, not cloud identifiers
44
- npx @zibby/cli start my-pipeline
42
+ # 2. Run it locally — names are folder names, not cloud identifiers
43
+ npx @zibby/cli workflow start my-pipeline
45
44
 
46
- # 4. Ship it to Zibby Cloud (returns a UUID + caches it in .zibby-deploy.json)
45
+ # 3. Ship it to Zibby Cloud (returns a UUID + caches it in .zibby-deploy.json)
47
46
  npx @zibby/cli login
48
- npx @zibby/cli deploy my-pipeline
47
+ npx @zibby/cli workflow deploy my-pipeline
49
48
 
50
- # 5. Trigger a remote run by UUID. Tail the logs Heroku-style.
51
- npx @zibby/cli trigger <uuid> # uuid printed by `deploy` or `workflow list`
52
- npx @zibby/cli logs -t
49
+ # 4. Trigger a remote run by UUID. Tail the logs Heroku-style.
50
+ npx @zibby/cli workflow trigger <uuid> # uuid printed by `deploy` or `workflow list`
51
+ npx @zibby/cli workflow logs -t
53
52
 
54
- # 6. Manage the fleet
55
- npx @zibby/cli workflow list # local + deployed (shows UUIDs)
56
- npx @zibby/cli workflow delete <uuid> # tear one down
53
+ # 5. Manage the fleet
54
+ npx @zibby/cli workflow list # local + deployed (shows UUIDs)
55
+ npx @zibby/cli workflow delete <uuid> # tear one down
57
56
  ```
58
57
 
59
58
  Prefer to install once instead of `npx` every time:
@@ -67,21 +66,23 @@ zibby --help
67
66
 
68
67
  ## The CLI: full workflow lifecycle
69
68
 
69
+ All workflow operations live under `zibby workflow <verb>` for consistency. The bare top-level forms (`zibby start`, `zibby deploy`, `zibby trigger`, `zibby logs`) are kept as backward-compat aliases.
70
+
70
71
  | Command | What it does |
71
72
  |---|---|
72
- | `zibby init` | Scaffold a workflow project `.zibby/graph.mjs`, `.zibby.config.js`, sane defaults. |
73
- | `zibby g workflow <name>` | **Generate** a new custom workflow under `.zibby/workflows/<name>/`. |
74
- | `zibby start <name>` | Run a workflow **locally** with hot-reload (defaults to port 3848). Name = folder under `.zibby/workflows/`. |
73
+ | `zibby workflow new <name>` | **Generate** a new custom workflow under `.zibby/workflows/<name>/`. Auto-creates `.zibby/` if missing — no separate init step required. |
74
+ | `zibby workflow start <name>` | Run a workflow **locally** with hot-reload (defaults to port 3848). Name = folder under `.zibby/workflows/`. |
75
75
  | `zibby login` / `logout` / `status` | Cloud auth. |
76
- | `zibby deploy [name]` | **Deploy** a workflow to Zibby Cloud (interactive picker if name omitted). |
77
- | `zibby trigger <uuid>` | **Run** a deployed workflow in the cloud. UUID is canonical (names are local-only). Get UUIDs from `workflow list` or the `deploy` output. |
78
- | `zibby logs [jobId] -t` | Tail **logs** from a run, Heroku-style. `-t` to follow live. |
76
+ | `zibby workflow deploy [name]` | **Deploy** a workflow to Zibby Cloud (interactive picker if name omitted). |
77
+ | `zibby workflow trigger <uuid>` | **Run** a deployed workflow in the cloud. UUID is canonical (names are local-only). Get UUIDs from `workflow list` or the `deploy` output. |
78
+ | `zibby workflow logs [jobId] -t` | Tail **logs** from a run, Heroku-style. `-t` to follow live. |
79
79
  | `zibby workflow list` | **List** local + deployed workflows. |
80
+ | `zibby workflow download <uuid>` | **Pull** a deployed workflow back to local — edit + redeploy. |
80
81
  | `zibby workflow delete <uuid>` | **Delete** a deployed workflow. |
81
82
 
82
83
  **Local** runs land in `.zibby/output/sessions/<id>/` with raw outputs, parsed JSON, and a JSONL execution log — replay-friendly. **Cloud** runs use the same on-disk format, fronted by the trigger/logs commands.
83
84
 
84
- **Local vs cloud identity**: workflow folder names (`my-pipeline`) are *local* — used by `start`, `g workflow`, `deploy`. Cloud workflows are identified by **UUID** — used by `trigger`, `logs`, `workflow delete`. After your first `deploy`, the UUID is cached in `.zibby/workflows/<name>/.zibby-deploy.json` (commit it to git so collaborators share the same canonical reference).
85
+ **Local vs cloud identity**: workflow folder names (`my-pipeline`) are *local* — used by `workflow new`, `workflow start`, `workflow deploy`. Cloud workflows are identified by **UUID** — used by `workflow trigger`, `workflow logs`, `workflow download`, `workflow delete`. After your first `deploy`, the UUID is cached in `.zibby/workflows/<name>/.zibby-deploy.json` (commit it to git so collaborators share the same canonical reference).
85
86
 
86
87
  The CLI also integrates with [Zibby Studio](https://zibby.app) — a desktop UI for visualising live runs, pinning sessions, and stopping a workflow from a button.
87
88
 
@@ -133,11 +134,11 @@ See [`examples/`](./examples/) for runnable demos of each pattern.
133
134
 
134
135
  | | What it does | Why this is different |
135
136
  |---|---|---|
136
- | **LangGraph** | Orchestrates LLM messagesReAct loops, tool-call dispatch, prompt-level state machines. | We work one level *above*. A node hands off to a full agent (Claude Code, Cursor) that already owns its message loop and tools. You compose agent calls, not LLM messages. |
137
- | **n8n / Zapier** | Visual no-code SaaS automation — wire APIs together. | Code-first, AI-agent-native, no UI. n8n connects Slack to Notion. We compose agents that read code, run tests, file PRs. |
137
+ | **LangGraph** | Python-first graph runtime over LangChain nodes are LangChain agents or LLM calls, state is shared via the graph. | Our nodes hand off to **external coding-agent CLIs** (Claude Code, cursor-agent, OpenAI Codex SDK) — independent processes that own their own tool use, multi-turn loops, and file edits. JS-first, no Python interop, no LangChain assembly. |
138
+ | **n8n / Zapier** | Visual workflow editor — wire SaaS APIs together. | Code-first, no UI. Built around composing coding-agent CLIs against your repo, not connecting SaaS APIs. |
138
139
  | **CrewAI / AutoGen** | Multi-agent role-play — agents converse to solve a task. | No agent debate. Each node is a discrete, schema-validated invocation. Deterministic edges, retry-friendly. |
139
140
 
140
- If you've used LangGraph and wished the nodes could be *agents that already know how to use tools* instead of LLM call sites you have to wire up — this is that.
141
+ If you want to compose Claude Code + Cursor + Codex into one pipeline with structured handoff between them JS, no Python, no LangChain — this is that.
141
142
 
142
143
  ---
143
144
 
@@ -1,4 +1,4 @@
1
- var R=Object.defineProperty;var N=(e,o)=>()=>(e&&(o=e(e=0)),o);var D=(e,o)=>{for(var t in o)R(e,t,{get:o[t],enumerable:!0})};function v(e){return L.get(e)||null}var L,x=N(()=>{L=new Map});var _,M,T,k,S=N(()=>{_=()=>{},M={debug:_,info:_,warn:(...e)=>console.warn("[workflow]",...e),error:(...e)=>console.error("[workflow]",...e)},T={impl:M},k={debug:(...e)=>T.impl.debug?.(...e),info:(...e)=>T.impl.info?.(...e),warn:(...e)=>T.impl.warn?.(...e),error:(...e)=>T.impl.error?.(...e)}});var $,A=N(()=>{$=class{constructor(o,t,r=0){this.name=o,this.description=t,this.priority=r}async invoke(o,t={}){throw new Error(`${this.constructor.name}.invoke() must be implemented`)}canHandle(o){throw new Error(`${this.constructor.name}.canHandle() must be implemented`)}getName(){return this.name}getDescription(){return this.description}getPriority(){return this.priority}}});var j={};D(j,{getAgentStrategy:()=>C,invokeAgent:()=>G,listStrategies:()=>B,registerStrategy:()=>F});function F(e){if(!(e instanceof $))throw new Error("strategy must be an instance of AgentStrategy");let o=m.findIndex(t=>t.getName()===e.getName());o>=0?m[o]=e:m.push(e)}function B(){return m.map(e=>e.getName())}function C(e={}){let{state:o={},preferredAgent:t=null}=e,r=t||o.agentType||process.env.AGENT_TYPE;if(!r){let s=m.map(n=>n.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(n=>n.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 G(e,o={},t={}){let r=C(o),i=o.state?.config||t.config||{},s=i.models||{},n=t.nodeName&&s[t.nodeName]||null,a=s.default||null,c=i.agent?.[r.name]?.model||null,l=n||a||c||t.model||null,p={...t,model:l,workspace:o.state?.workspace||t.workspace,schema:t.schema||o.schema,images:t.images||o.images||[],skills:t.skills||o.skills||[],config:i},f=e,g=p.skills||[];if(g.length>0&&!t.skipPromptFragments){let d=g.map(y=>{let u=v(y)?.promptFragment;return typeof u=="function"?u():u}).filter(Boolean);d.length>0&&(f+=`
1
+ var R=Object.defineProperty;var N=(e,o)=>()=>(e&&(o=e(e=0)),o);var D=(e,o)=>{for(var n in o)R(e,n,{get:o[n],enumerable:!0})};function $(e){return L.get(e)||null}var L,v=N(()=>{L=new Map});var _,M,T,k,x=N(()=>{_=()=>{},M={debug:_,info:_,warn:(...e)=>console.warn("[workflow]",...e),error:(...e)=>console.error("[workflow]",...e)},T={impl:M},k={debug:(...e)=>T.impl.debug?.(...e),info:(...e)=>T.impl.info?.(...e),warn:(...e)=>T.impl.warn?.(...e),error:(...e)=>T.impl.error?.(...e)}});var A=N(()=>{});var C={};D(C,{getAgentStrategy:()=>b,invokeAgent:()=>G,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 b(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 G(e,o={},n={}){let r=b(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},f=e,g=p.skills||[];if(g.length>0&&!n.skipPromptFragments){let d=g.map(y=>{let u=$(y)?.promptFragment;return typeof u=="function"?u():u}).filter(Boolean);d.length>0&&(f+=`
2
2
 
3
3
  ${d.join(`
4
4
 
@@ -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
  ${h}
12
- `),k.debug(`[workflow] prompt length: ${f.length} chars`),r.invoke(f,p)}var m,b=N(()=>{A();S();x();m=[]});x();S();var E={};var I=new Map;function U(e,o){I.set(e,o)}function O(e){let o=I.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 t=>{let r=t?._coreInvokeAgent;r||(r=(await Promise.resolve().then(()=>(b(),j))).invokeAgent);let i=o.extraPromptInstructions||"Execute the task based on the current state.",s=V(i,t),n=await r(s,{cwd:t.workspace||process.cwd(),model:t.model,tools:o.resolvedTools||null});return{success:!0,output:{raw:n,nodeId:e},raw:typeof n=="string"?n:n.raw}}})});function V(e,o){let t=/@([\w.]+)/g,r=new Set,i;for(;(i=t.exec(e))!==null;)r.add(i[1]);if(r.size===0)return e;let s=[],n=new Set;for(let a of r){let c=a.split(".")[0];if(n.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),f=a.replace(/_/g," ").replace(/\b\w/g,g=>g.toUpperCase());s.push(`## ${f}
13
- ${p}`),a.includes(".")||n.add(c)}return s.length===0?e:`${e}
12
+ `),k.debug(`[workflow] prompt length: ${f.length} chars`),r.invoke(f,p)}var S,m,j=N(()=>{A();x();v();S=Symbol.for("@zibby/agent-workflow.strategies");globalThis[S]||(globalThis[S]=[]);m=globalThis[S]});v();x();var E={};var I=new Map;function z(e,o){I.set(e,o)}function O(e){let o=I.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}z("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(),C))).invokeAgent);let i=o.extraPromptInstructions||"Execute the task based on the current state.",s=U(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 U(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),f=a.replace(/_/g," ").replace(/\b\w/g,g=>g.toUpperCase());s.push(`## ${f}
13
+ ${p}`),a.includes(".")||t.add(c)}return s.length===0?e:`${e}
14
14
 
15
15
  ---
16
16
  # Referenced Context
17
17
 
18
18
  ${s.join(`
19
19
 
20
- `)}`}function de(e,o={}){let{nodes:t,edges:r,nodeConfigs:i={}}=e,s=new Set,n=[],a=new Map;for(let u of t){let w=u.data?.nodeType||u.type;a.set(u.id,w),w==="decision"?s.add(u.id):n.push({id:u.id,nodeType:w,label:u.data?.label||u.id})}let c=n.some(u=>{let w=i[u.id]||{};return!w.customCode&&!w.executeCode}),{toolsPerNode:l,toolIdsByVar:p}=Z(n,i),{simpleEdges:f,conditionalEdges:g}=q(r,s),h=Q(n,r,s),d=[],y=o.workflowType||"workflow";return d.push(z(o)),d.push(H(y,{usesRegisteredNodes:c})),d.push(W(p)),d.push(J(y)),d.push(Y(n,i)),d.push(K(n,h,f,g,l,y)),d.filter(Boolean).join(`
21
- `)}function pe(e){let o={};for(let[t,r]of Object.entries(e)){let{tools:i,...s}=r;Object.keys(s).length>0&&(o[t]=s)}return o}function z(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 t=["import { WorkflowGraph, invokeAgent, getResolvedToolDefinitions } from '@zibby/agent-workflow';"];return o&&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(`
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[t,r]of e)o.push(`const ${t} = getResolvedToolDefinitions(${JSON.stringify(r)}); // ${r.join(", ")}`);return o.push(""),o.join(`
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 Y(e,o){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 r of e){let i=P(r.id),s=o[r.id]?.customCode;if(s)t.push(`// @custom \u2014 modified from default "${r.nodeType}" template`),t.push(`const ${i}_execute = ${s};`);else{let n=O(r.nodeType);n?(t.push(`// Default "${r.nodeType}" implementation`),t.push(`const ${i}_execute = ${n};`)):(t.push(`// No template for "${r.nodeType}" \u2014 passthrough`),t.push(`const ${i}_execute = async (state) => ({ success: true, output: {}, raw: null });`))}t.push("")}return t.join(`
26
- `)}function K(e,o,t,r,i,s){let n=["// \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"];n.push("export function buildGraph(options = {}) {"),n.push(" const graph = new WorkflowGraph(options);",""),n.push(" // Nodes");for(let c of e){let l=P(c.id);n.push(` graph.addNode('${c.id}', { name: '${c.id}', execute: ${l}_execute });`),n.push(` graph.setNodeType('${c.id}', '${c.nodeType}');`)}n.push("",` graph.setEntryPoint('${o}');`,""),(t.length>0||r.length>0)&&n.push(" // Edges");for(let c of t)n.push(` graph.addEdge('${c.source}', '${c.target}');`);for(let c of r){let l=c.code.split(`
20
+ `)}`}function pe(e,o={}){let{nodes:n,edges:r,nodeConfigs:i={}}=e,s=new Set,t=[],a=new Map;for(let u of n){let w=u.data?.nodeType||u.type;a.set(u.id,w),w==="decision"?s.add(u.id):t.push({id:u.id,nodeType:w,label:u.data?.label||u.id})}let c=t.some(u=>{let w=i[u.id]||{};return!w.customCode&&!w.executeCode}),{toolsPerNode:l,toolIdsByVar:p}=Z(t,i),{simpleEdges:f,conditionalEdges:g}=q(r,s),h=Q(t,r,s),d=[],y=o.workflowType||"workflow";return d.push(V(o)),d.push(H(y,{usesRegisteredNodes:c})),d.push(W(p)),d.push(Y(y)),d.push(J(t,i)),d.push(K(t,h,f,g,l,y)),d.filter(Boolean).join(`
21
+ `)}function ge(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 V(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(`
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
+ `)}function Y(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 J(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=P(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=O(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 K(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=P(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,f)=>f===0?p:` ${p}`).join(`
28
- `);n.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&&n.push(""," graph.resolvedToolsMap = {",...a," };"),n.push(""," return graph;","}",""),n.push("export { nodeConfigs };",""),n.join(`
29
- `)}function Z(e,o){let t=new Map,r=new Map;for(let i of e){let s=o[i.id]?.tools,n;if(Array.isArray(s)&&s.length>0)n=[...s].sort();else{let a=E[i.nodeType];a?.length>0&&(n=[...a].sort())}if(n){let a=`${n.map(c=>c.replace(/[^a-zA-Z0-9]/g,"")).join("And")}Tools`;t.set(i.id,a),r.has(a)||r.set(a,n)}}return{toolsPerNode:t,toolIdsByVar:r}}function q(e,o){let t=[],r=[],i=new Map,s=new Set;for(let n of e)i.has(n.source)||i.set(n.source,[]),i.get(n.source).push(n);for(let n of e)if(!o.has(n.source))if(o.has(n.target)){if(s.has(n.target))continue;s.add(n.target);let c=(i.get(n.target)||[]).find(l=>l.data?.conditionalCode||l.conditionalCode);c&&r.push({source:n.source,code:c.data?.conditionalCode||c.conditionalCode})}else t.push({source:n.source,target:n.target});return{simpleEdges:t,conditionalEdges:r}}function Q(e,o,t){let r=new Set;for(let s of o)t.has(s.target)||r.add(s.target);let i=e.find(s=>!r.has(s.id));return i?i.id:e[0]?.id}function P(e){return e.replace(/[^a-zA-Z0-9]/g,"_")}export{pe as generateNodeConfigsJson,de as generateWorkflowCode};
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 Z(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=E[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 Q(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 P(e){return e.replace(/[^a-zA-Z0-9]/g,"_")}export{ge as generateNodeConfigsJson,pe as generateWorkflowCode};
@@ -1,4 +1,4 @@
1
- var Qe=Object.defineProperty;var se=(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 J=(o,e)=>()=>(o&&(e=o(o=0)),e);var $e=(o,e)=>{for(var t in e)Qe(o,t,{get:e[t],enumerable:!0})};var Ee,tt,V,w,D=J(()=>{Ee=()=>{},tt={debug:Ee,info:Ee,warn:(...o)=>console.warn("[workflow]",...o),error:(...o)=>console.error("[workflow]",...o)},V={impl:tt},w={debug:(...o)=>V.impl.debug?.(...o),info:(...o)=>V.impl.info?.(...o),warn:(...o)=>V.impl.warn?.(...o),error:(...o)=>V.impl.error?.(...o)}});var q,Pe=J(()=>{q=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 Re={};$e(Re,{clearSkills:()=>at,getAllSkills:()=>it,getSkill:()=>F,hasSkill:()=>nt,listSkillIds:()=>ct,registerSkill:()=>rt});function rt(o){if(!o||typeof o.id!="string")throw new Error("Skill definition must include a string id");B.set(o.id,Object.freeze({...o}))}function F(o){return B.get(o)||null}function nt(o){return B.has(o)}function it(){return new Map(B)}function ct(){return Array.from(B.keys())}function at(){B.clear()}var B,X=J(()=>{B=new Map});var j={};$e(j,{getAgentStrategy:()=>Be,invokeAgent:()=>dt,listStrategies:()=>ut,registerStrategy:()=>lt});function lt(o){if(!(o instanceof q))throw new Error("strategy must be an instance of AgentStrategy");let e=C.findIndex(t=>t.getName()===o.getName());e>=0?C[e]=o:C.push(o)}function ut(){return C.map(o=>o.getName())}function Be(o={}){let{state:e={},preferredAgent:t=null}=o,s=t||e.agentType||process.env.AGENT_TYPE;if(!s){let r=C.map(i=>i.getName()).join(", ")||"none registered";throw new Error(`No agent specified. Set agentType in state or AGENT_TYPE env var. Available: ${r}`)}w.debug(`[workflow] agent selection: requested=${s}`);let n=C.find(r=>r.getName()===s);if(!n){let r=C.map(i=>i.getName()).join(", ")||"none registered";throw new Error(`Unknown agent '${s}'. Available: ${r}`)}if(!n.canHandle(o))throw new Error(`Agent '${s}' is not available in this environment. Check credentials/environment.`);return w.debug(`[workflow] using agent: ${n.getName()}`),n}async function dt(o,e={},t={}){let s=Be(e),n=e.state?.config||t.config||{},r=n.models||{},i=t.nodeName&&r[t.nodeName]||null,c=r.default||null,a=n.agent?.[s.name]?.model||null,l=i||c||a||t.model||null,m={...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},d=o,u=m.skills||[];if(u.length>0&&!t.skipPromptFragments){let p=u.map(_=>{let N=F(_)?.promptFragment;return typeof N=="function"?N():N}).filter(Boolean);p.length>0&&(d+=`
1
+ var Qe=Object.defineProperty;var oe=(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 J=(o,e)=>()=>(o&&(e=o(o=0)),e);var $e=(o,e)=>{for(var t in e)Qe(o,t,{get:e[t],enumerable:!0})};var Ee,tt,z,w,D=J(()=>{Ee=()=>{},tt={debug:Ee,info:Ee,warn:(...o)=>console.warn("[workflow]",...o),error:(...o)=>console.error("[workflow]",...o)},z={impl:tt},w={debug:(...o)=>z.impl.debug?.(...o),info:(...o)=>z.impl.info?.(...o),warn:(...o)=>z.impl.warn?.(...o),error:(...o)=>z.impl.error?.(...o)}});var Pe=J(()=>{});var Re={};$e(Re,{clearSkills:()=>ct,getAllSkills:()=>it,getSkill:()=>L,hasSkill:()=>nt,listSkillIds:()=>at,registerSkill:()=>st});function st(o){if(!o||typeof o.id!="string")throw new Error("Skill definition must include a string id");W.set(o.id,Object.freeze({...o}))}function L(o){return W.get(o)||null}function nt(o){return W.has(o)}function it(){return new Map(W)}function at(){return Array.from(W.keys())}function ct(){W.clear()}var W,q=J(()=>{W=new Map});var j={};$e(j,{getAgentStrategy:()=>We,invokeAgent:()=>dt,listStrategies:()=>ut,registerStrategy:()=>lt});function lt(o){if(!o||typeof o.getName!="function"||typeof o.invoke!="function")throw new Error("strategy must implement getName() and invoke() (AgentStrategy shape)");let e=C.findIndex(t=>t.getName()===o.getName());e>=0?C[e]=o:C.push(o)}function ut(){return C.map(o=>o.getName())}function We(o={}){let{state:e={},preferredAgent:t=null}=o,r=t||e.agentType||process.env.AGENT_TYPE;if(!r){let s=C.map(a=>a.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 n=C.find(s=>s.getName()===r);if(!n){let s=C.map(a=>a.getName()).join(", ")||"none registered";throw new Error(`Unknown agent '${r}'. Available: ${s}`)}if(!n.canHandle(o))throw new Error(`Agent '${r}' is not available in this environment. Check credentials/environment.`);return w.debug(`[workflow] using agent: ${n.getName()}`),n}async function dt(o,e={},t={}){let r=We(e),n=e.state?.config||t.config||{},s=n.models||{},a=t.nodeName&&s[t.nodeName]||null,i=s.default||null,c=n.agent?.[r.name]?.model||null,l=a||i||c||t.model||null,m={...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},d=o,u=m.skills||[];if(u.length>0&&!t.skipPromptFragments){let p=u.map(_=>{let N=L(_)?.promptFragment;return typeof N=="function"?N():N}).filter(Boolean);p.length>0&&(d+=`
2
2
 
3
3
  ${p.join(`
4
4
 
@@ -9,37 +9,37 @@ 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
  ${g}
12
- `),w.debug(`[workflow] prompt length: ${d.length} chars`),s.invoke(d,m)}var C,U=J(()=>{Pe();D();X();C=[]});var et=new Set(["__proto__","constructor","prototype"]);function re(o){if(et.has(o))throw new Error(`Invalid state key: "${o}"`)}var H=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){re(e),this._history.push({...this._state}),this._state[e]=t}update(e){let t=Object.getOwnPropertyNames(e);for(let s of t)re(s);this._history.push({...this._state});for(let s of t)this._state[s]=e[s]}append(e,t){re(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 K=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]*\}/);return s?this.validate(JSON.parse(s[0])):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 r=typeof e[s];r!==n.type&&t.push(`Field '${s}' expected ${n.type}, got ${r}`)}if(n.validate&&s in e){let r=n.validate(e[s]);r&&t.push(`Field '${s}': ${r}`)}}if(t.length>0)throw new Error(`Output validation failed:
12
+ `),w.debug(`[workflow] prompt length: ${d.length} chars`),r.invoke(d,m)}var ae,C,U=J(()=>{Pe();D();q();ae=Symbol.for("@zibby/agent-workflow.strategies");globalThis[ae]||(globalThis[ae]=[]);C=globalThis[ae]});var et=new Set(["__proto__","constructor","prototype"]);function re(o){if(et.has(o))throw new Error(`Invalid state key: "${o}"`)}var H=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){re(e),this._history.push({...this._state}),this._state[e]=t}update(e){let t=Object.getOwnPropertyNames(e);for(let r of t)re(r);this._history.push({...this._state});for(let r of t)this._state[r]=e[r]}append(e,t){re(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 K=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]*\}/);return r?this.validate(JSON.parse(r[0])):this.validate({result:e.trim()})}validate(e){let t=[];for(let[r,n]of Object.entries(this.schema)){if(n.required&&!(r in e)&&t.push(`Missing required field: ${r}`),r in e&&n.type){let s=typeof e[r];s!==n.type&&t.push(`Field '${r}' expected ${n.type}, got ${s}`)}if(n.validate&&r in e){let s=n.validate(e[r]);s&&t.push(`Field '${r}': ${s}`)}}if(t.length>0)throw new Error(`Output validation failed:
13
13
  ${t.join(`
14
- `)}`);return e}};D();import{writeFileSync as ae,readFileSync as We,existsSync as Me,mkdirSync as pt}from"node:fs";import{join as le,dirname as ft}from"node:path";import y from"chalk";var ot="__WORKFLOW_GRAPH_LOG__",L=y.gray("\u2502"),st=y.gray("\u250C"),Ie=y.gray("\u2514"),ne=y.green("\u25C6"),ve=y.hex("#c084fc")("\u25C6"),Ne=y.hex("#2dd4bf")("\u25C6"),ie=y.red("\u25C6"),ke=`${L} `,Oe=2;function xe(o){return o<1e3?`${o}ms`:`${(o/1e3).toFixed(1)}s`}function Te(o,e){return(t,s,n)=>{if(typeof t!="string")return o(t,s,n);let r=process.stdout.columns||120,i="";for(let c=0;c<t.length;c++){let a=t[c];e.lineStart&&(i+=ke,e.col=Oe,e.lineStart=!1),a===`
15
- `?(i+=a,e.lineStart=!0,e.col=0,e.inEsc=!1):a==="\x1B"?(e.inEsc=!0,i+=a):e.inEsc?(i+=a,(a>="A"&&a<="Z"||a>="a"&&a<="z")&&(e.inEsc=!1)):(e.col++,i+=a,e.col>=r&&(i+=`
16
- ${ke}`,e.col=Oe))}return o(i,s,n)}}var ce=class{constructor(){this._currentNode=null,this._origStdoutWrite=null,this._origStderrWrite=null;let e=String(process.env.ZIBBY_RUN_SOURCE||"").trim().toLowerCase(),t=String(process.env.ZIBBY_WORKFLOW_GRAPH_LOG_MARKERS||"").trim()==="1";this._emitWorkflowGraphMarkers=t||e==="studio"}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=Te(this._origStdoutWrite,e),process.stderr.write=Te(this._origStderrWrite,t)}_stopIntercepting(){this._origStdoutWrite&&(this._outState&&!this._outState.lineStart&&this._origStdoutWrite(`
14
+ `)}`);return e}};D();import{writeFileSync as ce,readFileSync as Me,existsSync as Be,mkdirSync as pt}from"node:fs";import{join as le,dirname as ft}from"node:path";import y from"chalk";var ot="__WORKFLOW_GRAPH_LOG__",F=y.gray("\u2502"),rt=y.gray("\u250C"),Ie=y.gray("\u2514"),se=y.green("\u25C6"),ve=y.hex("#c084fc")("\u25C6"),Ne=y.hex("#2dd4bf")("\u25C6"),ne=y.red("\u25C6"),ke=`${F} `,xe=2;function Oe(o){return o<1e3?`${o}ms`:`${(o/1e3).toFixed(1)}s`}function Te(o,e){return(t,r,n)=>{if(typeof t!="string")return o(t,r,n);let s=process.stdout.columns||120,a="";for(let i=0;i<t.length;i++){let c=t[i];e.lineStart&&(a+=ke,e.col=xe,e.lineStart=!1),c===`
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>=s&&(a+=`
16
+ ${ke}`,e.col=xe))}return o(a,r,n)}}var ie=class{constructor(){this._currentNode=null,this._origStdoutWrite=null,this._origStderrWrite=null;let e=String(process.env.ZIBBY_RUN_SOURCE||"").trim().toLowerCase(),t=String(process.env.ZIBBY_WORKFLOW_GRAPH_LOG_MARKERS||"").trim()==="1";this._emitWorkflowGraphMarkers=t||e==="studio"}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=Te(this._origStdoutWrite,e),process.stderr.write=Te(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
19
  `)}_emitGraphLogMarker(e){if(!this._emitWorkflowGraphMarkers)return;let t=`${ot}${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(ne,e):process.stdout.write.bind(process.stdout)(`${L} ${ne} ${e}
24
- `)}stepInfo(e){this.step(e)}stepTool(e){this._origStdoutWrite?this._writeDot(ve,e):process.stdout.write.bind(process.stdout)(`${L} ${ve} ${e}
25
- `)}stepMemory(e){let t=y.hex("#2dd4bf")(e);this._origStdoutWrite?this._writeDot(Ne,t):process.stdout.write.bind(process.stdout)(`${L} ${Ne} ${t}
26
- `)}stepFail(e){this._origStdoutWrite?this._writeDot(ie,y.red(e)):process.stdout.write.bind(process.stdout)(`${L} ${ie} ${y.red(e)}
27
- `)}nodeStart(e){this._currentNode=e,this._emitGraphLogMarker({phase:"node_begin",node:e}),this._rawWrite(`${st} ${e}`),this._startIntercepting()}nodeComplete(e,t={}){this._stopIntercepting();let{duration:s,details:n}=t;if(n)for(let i of n)this._rawWrite(`${ne} ${i}`);let r=s?y.dim(` ${xe(s)}`):"";this._rawWrite(`${Ie} ${y.green("done")}${r}`),this._emitGraphLogMarker({phase:"node_end",node:e}),this._rawWrite("")}nodeFailed(e,t,s={}){this._stopIntercepting();let{duration:n}=s,r=n?y.dim(` ${xe(n)}`):"";this._rawWrite(`${ie} ${y.red(t)}`),this._rawWrite(`${Ie} ${y.red("failed")}${r}`),this._emitGraphLogMarker({phase:"node_end",node:e}),this._rawWrite("")}route(e,t){this._rawWrite(y.dim(` ${e} \u2192 ${t}`)),this._rawWrite("")}graphComplete(){this._rawWrite(y.green.bold("\u2713 Workflow completed"))}},E=new ce;var z=".zibby/output",Ae="sessions",R=".session-info.json",be=".zibby-studio-stop";var Ce=["CI_JOB_ID","GITHUB_RUN_ID","CIRCLE_WORKFLOW_ID","BUILD_ID"];var W=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 K(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=d=>t&&typeof t.get=="function"?t.get(d):e?.[d];if(typeof this.customExecute=="function"){w.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?(w.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 w.error(`[workflow] node '${this.name}' failed: ${d.message}`),d.name==="ZodError"&&w.error(`Schema errors: ${JSON.stringify(d.errors,null,2)}`),{success:!1,error:d.message,raw:null}}}let r=typeof this.prompt=="function"?this.prompt(s()):this.prompt,i=n("_skillHints");i&&(r=`${i}
23
+ `)}step(e){this._origStdoutWrite?this._writeDot(se,e):process.stdout.write.bind(process.stdout)(`${F} ${se} ${e}
24
+ `)}stepInfo(e){this.step(e)}stepTool(e){this._origStdoutWrite?this._writeDot(ve,e):process.stdout.write.bind(process.stdout)(`${F} ${ve} ${e}
25
+ `)}stepMemory(e){let t=y.hex("#2dd4bf")(e);this._origStdoutWrite?this._writeDot(Ne,t):process.stdout.write.bind(process.stdout)(`${F} ${Ne} ${t}
26
+ `)}stepFail(e){this._origStdoutWrite?this._writeDot(ne,y.red(e)):process.stdout.write.bind(process.stdout)(`${F} ${ne} ${y.red(e)}
27
+ `)}nodeStart(e){this._currentNode=e,this._emitGraphLogMarker({phase:"node_begin",node:e}),this._rawWrite(`${rt} ${e}`),this._startIntercepting()}nodeComplete(e,t={}){this._stopIntercepting();let{duration:r,details:n}=t;if(n)for(let a of n)this._rawWrite(`${se} ${a}`);let s=r?y.dim(` ${Oe(r)}`):"";this._rawWrite(`${Ie} ${y.green("done")}${s}`),this._emitGraphLogMarker({phase:"node_end",node:e}),this._rawWrite("")}nodeFailed(e,t,r={}){this._stopIntercepting();let{duration:n}=r,s=n?y.dim(` ${Oe(n)}`):"";this._rawWrite(`${ne} ${y.red(t)}`),this._rawWrite(`${Ie} ${y.red("failed")}${s}`),this._emitGraphLogMarker({phase:"node_end",node:e}),this._rawWrite("")}route(e,t){this._rawWrite(y.dim(` ${e} \u2192 ${t}`)),this._rawWrite("")}graphComplete(){this._rawWrite(y.green.bold("\u2713 Workflow completed"))}},E=new ie;var V=".zibby/output",be="sessions",R=".session-info.json",Ae=".zibby-studio-stop";var Ce=["CI_JOB_ID","GITHUB_RUN_ID","CIRCLE_WORKFLOW_ID","BUILD_ID"];var M=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 K(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,n=d=>t&&typeof t.get=="function"?t.get(d):e?.[d];if(typeof this.customExecute=="function"){w.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?(w.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 w.error(`[workflow] node '${this.name}' failed: ${d.message}`),d.name==="ZodError"&&w.error(`Schema errors: ${JSON.stringify(d.errors,null,2)}`),{success:!1,error:d.message,raw:null}}}let s=typeof this.prompt=="function"?this.prompt(r()):this.prompt,a=n("_skillHints");a&&(s=`${a}
28
28
 
29
- ${r}`);let c=s(),a=c.cwd||process.cwd(),l=c.sessionPath;try{if(l){let d=le(l,R);if(Me(d)){let g=JSON.parse(We(d,"utf-8"));g.currentNode=this.name,ae(d,JSON.stringify(g,null,2),"utf-8")}let u=le(l,"..",R);if(Me(u))try{let g=JSON.parse(We(u,"utf-8"));g.currentNode=this.name,ae(u,JSON.stringify(g,null,2),"utf-8")}catch{}}}catch(d){w.debug(`[workflow] could not update session info: ${d.message}`)}let m=null;for(let d=0;d<=this.retries;d++)try{w.debug(`[workflow] node '${this.name}' attempt ${d}`);let u=s().config||{},g=u.agents||{},p=this.config.agent??g[this.name]??null,_={state:s()};p&&(_.preferredAgent=p);let N={workspace:a,schema:this.isZodSchema?this.outputSchema:null,skills:this.config.skills||[],sessionPath:l,config:u,nodeName:this.name,timeout:this.config?.timeout||3e5},v=e?._coreInvokeAgent;v||(v=(await Promise.resolve().then(()=>(U(),j))).invokeAgent);let f=await v(r,_,N),S,k;if(typeof f=="string"?(S=f,k=null):f.structured?(S=f.raw||JSON.stringify(f.structured,null,2),k=f.structured):(S=f.raw||JSON.stringify(f,null,2),k=f.extracted||null),l)try{let h=le(l,this.name,"raw_stream_output.txt");pt(ft(h),{recursive:!0}),ae(h,typeof S=="string"?S:JSON.stringify(S),"utf-8")}catch(h){w.debug(`[workflow] could not save raw output: ${h.message}`)}if(this.isZodSchema&&k){w.info(`[workflow] node '${this.name}': output validated: ${JSON.stringify(k,null,2)}`);let h=k;if(typeof this.onComplete=="function")try{h=await this.onComplete(s(),k)}catch($){w.warn(`[workflow] onComplete hook failed: ${$.message}`)}return{success:!0,output:h,raw:S}}if(typeof this.onComplete=="function")try{return{success:!0,output:await this.onComplete(s(),{raw:S}),raw:S}}catch(h){throw new Error(`onComplete failed: ${h.message}`,{cause:h})}if(this.parser){let h=this.parser.parse(S);return w.info(`[workflow] node '${this.name}': parsed output: ${JSON.stringify(h,null,2)}`),E.step("Output parsed"),{success:!0,output:h,raw:S}}return{success:!0,output:S,raw:S}}catch(u){m=u,d<this.retries&&w.info(`[workflow] node '${this.name}' failed, retrying (${d+1}/${this.retries})\u2026`)}return{success:!1,error:m.message,raw:null}}},Q=class extends W{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 De,readFileSync as ht}from"node:fs";import{join as ue,dirname as Le}from"node:path";var ee=class{static async loadContext(e,t,s={}){let n={},r=s.filenames||["CONTEXT.md","AGENTS.md"];if(e){let c=Le(ue(t,e));for(let a of r){let l=await this.findAndMergeContextFiles(a,c,t);if(l){let m=a.replace(/\.[^.]+$/,"").toLowerCase();n[m]=l}}}let i=s.discovery||{};for(let[c,a]of Object.entries(i))try{let l=ue(t,a);De(l)&&(n[c]=await this.loadFile(l))}catch(l){console.warn(`[workflow] could not load context '${c}' from '${a}': ${l.message}`)}return n}static async findAndMergeContextFiles(e,t,s){let n=[],r=t;for(;r.startsWith(s);){let i=ue(r,e);if(De(i))try{n.unshift(await this.loadFile(i))}catch(a){console.warn(`[workflow] could not load ${e} from ${i}: ${a.message}`)}let c=Le(r);if(c===r)break;r=c}return n.length===0?null:n.every(i=>typeof i=="string")?n.join(`
29
+ ${s}`);let i=r(),c=i.cwd||process.cwd(),l=i.sessionPath;try{if(l){let d=le(l,R);if(Be(d)){let g=JSON.parse(Me(d,"utf-8"));g.currentNode=this.name,ce(d,JSON.stringify(g,null,2),"utf-8")}let u=le(l,"..",R);if(Be(u))try{let g=JSON.parse(Me(u,"utf-8"));g.currentNode=this.name,ce(u,JSON.stringify(g,null,2),"utf-8")}catch{}}}catch(d){w.debug(`[workflow] could not update session info: ${d.message}`)}let m=null;for(let d=0;d<=this.retries;d++)try{w.debug(`[workflow] node '${this.name}' attempt ${d}`);let u=r().config||{},g=u.agents||{},p=this.config.agent??g[this.name]??null,_={state:r()};p&&(_.preferredAgent=p);let N={workspace:c,schema:this.isZodSchema?this.outputSchema:null,skills:this.config.skills||[],sessionPath:l,config:u,nodeName:this.name,timeout:this.config?.timeout||3e5},v=e?._coreInvokeAgent;v||(v=(await Promise.resolve().then(()=>(U(),j))).invokeAgent);let f=await v(s,_,N),S,k;if(typeof f=="string"?(S=f,k=null):f.structured?(S=f.raw||JSON.stringify(f.structured,null,2),k=f.structured):(S=f.raw||JSON.stringify(f,null,2),k=f.extracted||null),l)try{let h=le(l,this.name,"raw_stream_output.txt");pt(ft(h),{recursive:!0}),ce(h,typeof S=="string"?S:JSON.stringify(S),"utf-8")}catch(h){w.debug(`[workflow] could not save raw output: ${h.message}`)}if(this.isZodSchema&&k){w.info(`[workflow] node '${this.name}': output validated: ${JSON.stringify(k,null,2)}`);let h=k;if(typeof this.onComplete=="function")try{h=await this.onComplete(r(),k)}catch($){w.warn(`[workflow] onComplete hook failed: ${$.message}`)}return{success:!0,output:h,raw:S}}if(typeof this.onComplete=="function")try{return{success:!0,output:await this.onComplete(r(),{raw:S}),raw:S}}catch(h){throw new Error(`onComplete failed: ${h.message}`,{cause:h})}if(this.parser){let h=this.parser.parse(S);return w.info(`[workflow] node '${this.name}': parsed output: ${JSON.stringify(h,null,2)}`),E.step("Output parsed"),{success:!0,output:h,raw:S}}return{success:!0,output:S,raw:S}}catch(u){m=u,d<this.retries&&w.info(`[workflow] node '${this.name}' failed, retrying (${d+1}/${this.retries})\u2026`)}return{success:!1,error:m.message,raw:null}}},X=class extends M{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 De,readFileSync as ht}from"node:fs";import{join as ue,dirname as Fe}from"node:path";var Q=class{static async loadContext(e,t,r={}){let n={},s=r.filenames||["CONTEXT.md","AGENTS.md"];if(e){let i=Fe(ue(t,e));for(let c of s){let l=await this.findAndMergeContextFiles(c,i,t);if(l){let m=c.replace(/\.[^.]+$/,"").toLowerCase();n[m]=l}}}let a=r.discovery||{};for(let[i,c]of Object.entries(a))try{let l=ue(t,c);De(l)&&(n[i]=await this.loadFile(l))}catch(l){console.warn(`[workflow] could not load context '${i}' from '${c}': ${l.message}`)}return n}static async findAndMergeContextFiles(e,t,r){let n=[],s=t;for(;s.startsWith(r);){let a=ue(s,e);if(De(a))try{n.unshift(await this.loadFile(a))}catch(c){console.warn(`[workflow] could not load ${e} from ${a}: ${c.message}`)}let i=Fe(s);if(i===s)break;s=i}return n.length===0?null:n.every(a=>typeof a=="string")?n.join(`
30
30
 
31
31
  ---
32
32
 
33
- `):n.every(i=>typeof i=="object")?Object.assign({},...n):n[n.length-1]}static async loadFile(e){let t=ht(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 Ue,existsSync as de,writeFileSync as Fe,unlinkSync as mt}from"node:fs";import{join as P,resolve as Ze}from"node:path";import{config as gt}from"dotenv";import{zodToJsonSchema as je}from"zod-to-json-schema";import St from"handlebars";function wt({traceFrom:o,sessionId:e,sessionPath:t,idSource:s,mkdirFresh:n}){if(process.env.ZIBBY_SESSION_LOG==="0"||process.env.ZIBBY_SESSION_LOG==="false")return;let r=typeof process.ppid=="number"?process.ppid:"n/a",i=`[zibby:session] from=${o} pid=${process.pid} ppid=${r} sessionId=${e} source=${s} mkdir=${n?"yes":"no"} path=${t}`;if(console.log(i),(process.env.ZIBBY_TRACE_SESSION==="1"||process.env.ZIBBY_TRACE_SESSION==="true")&&process.env.ZIBBY_SESSION_LOG!=="0"&&process.env.ZIBBY_SESSION_LOG!=="false"){let l=(new Error("session trace").stack||"").split(`
33
+ `):n.every(a=>typeof a=="object")?Object.assign({},...n):n[n.length-1]}static async loadFile(e){let t=ht(e,"utf-8");if(e.endsWith(".json"))return JSON.parse(t);if(e.endsWith(".js")||e.endsWith(".mjs")){let{pathToFileURL:r}=await import("url"),n=await import(r(e).href);return n.default||n}return t}};import{mkdirSync as Ue,existsSync as de,writeFileSync as Le,unlinkSync as mt}from"node:fs";import{join as P,resolve as Ye}from"node:path";import{config as gt}from"dotenv";import{zodToJsonSchema as je}from"zod-to-json-schema";import St from"handlebars";function wt({traceFrom:o,sessionId:e,sessionPath:t,idSource:r,mkdirFresh:n}){if(!(process.env.ZIBBY_SESSION_LOG==="1"||process.env.ZIBBY_SESSION_LOG==="true"))return;let a=typeof process.ppid=="number"?process.ppid:"n/a",i=`[zibby:session] from=${o} pid=${process.pid} ppid=${a} sessionId=${e} source=${r} mkdir=${n?"yes":"no"} path=${t}`;if(console.log(i),process.env.ZIBBY_TRACE_SESSION==="1"||process.env.ZIBBY_TRACE_SESSION==="true"){let m=(new Error("session trace").stack||"").split(`
34
34
  `).slice(2,14).join(`
35
35
  `);console.log(`[zibby:session] stack (${o}):
36
- ${l}`)}}function _t(){return process.env.ZIBBY_RUN_SOURCE==="studio"||process.env.ZIBBY_KEEP_SESSION_ENV==="1"||process.env.ZIBBY_KEEP_SESSION_ENV==="true"}function yt(){if(process.env.ZIBBY_RUN_SOURCE!=="studio")return;let o=process.env.ZIBBY_SESSION_PATH;if(!(o==null||String(o).trim()===""))try{return Ze(String(o).trim())}catch{return String(o).trim()}}function $t(){_t()||(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=Ce.map(r=>process.env[r]).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 vt({cwd:o=process.cwd(),config:e={},initialState:t={},traceFrom:s="resolveWorkflowSession"}={}){let n=t.sessionPath,r=t.sessionTimestamp,i="initialState.sessionPath";if(!n&&process.env.ZIBBY_SESSION_PATH)try{let l=Ze(String(process.env.ZIBBY_SESSION_PATH));l&&(n=l,i="ZIBBY_SESSION_PATH")}catch{}let c;if(n)c=String(n).split(/[/\\]/).filter(Boolean).pop(),r==null&&(r=Date.now());else{let l=process.env.ZIBBY_SESSION_ID&&String(process.env.ZIBBY_SESSION_ID).trim();if(l)c=l,i="ZIBBY_SESSION_ID";else{let d=e.sessionId!=null?String(e.sessionId).trim():"";d&&d!=="last"?(c=d,i="config.sessionId"):(c=It(e),i="generated")}r=r??Date.now();let m=e.paths?.output||z;n=P(o,m,Ae,c)}let a=!de(n);return a&&Ue(n,{recursive:!0}),(a||i!=="initialState.sessionPath")&&wt({traceFrom:s,sessionId:c,sessionPath:n,idSource:i,mkdirFresh:a}),Et({sessionPath:n,sessionId:c}),{sessionPath:n,sessionId:c,sessionTimestamp:r}}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}setStateSchema(e){return this.stateSchema=e,this}getStateSchema(){return this.stateSchema}addNode(e,t,s={}){let n=t instanceof W?t:new W(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,r){let i=s;for(let c=e.length-1;c>=0;c--){let a=e[c],l=i;i=()=>a(t,l,n,r)}return i()}serialize(){let e=[],t={};for(let[r,i]of this.nodes){let c=this.nodeTypeMap.get(r)||r;e.push({id:r,type:c,data:{nodeType:c,label:r}});let a={};i._isCustomCode&&typeof i.execute=="function"&&(a.customCode=i.execute.toString());let l=this.nodePrompts.get(r);if(l&&(a.prompt=l),typeof i.customExecute=="function"&&(a.executeCode=i.customExecute.toString()),i.outputSchema)try{if(typeof i.outputSchema._def<"u"){let d=je(i.outputSchema,{target:"openApi3"});a.outputSchema={jsonSchema:d,variables:this._flattenJsonSchemaToVariables(d)}}else a.outputSchema={schema:i.outputSchema}}catch(d){console.warn(`[workflow] failed to convert schema for ${r}:`,d.message)}let m=(this.resolvedToolsMap||{})[r];m?.toolIds&&(a.tools=m.toolIds),Object.keys(a).length>0&&(t[r]=a)}let s=[];for(let[r,i]of this.edges)if(typeof i=="string")s.push({source:r,target:i});else if(i.conditional){let c=this.conditionalCodeMap.get(r)||i.routes.toString(),a=this._inferConditionalTargets(i.routes),l=i.labels||{};for(let m of a){let d={source:r,target:m,data:{conditionalCode:c}};l[m]&&(d.label=l[m]),s.push(d)}}let n=null;if(this.stateSchema)try{n=je(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,r;for(;(r=n.exec(t))!==null;)s.add(r[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||{},r=e.required||[];for(let[i,c]of Object.entries(n)){let a=t?`${t}.${i}`:i;s.push({path:a,type:c.type||"unknown",label:c.description||this._formatLabel(i),optional:!r.includes(i)}),c.type==="object"&&c.properties&&s.push(...this._flattenSchema(c,a)),c.type==="array"&&c.items?.type==="object"&&c.items.properties&&s.push(...this._flattenSchema(c.items,`${a}[]`))}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,r]of Object.entries(t))if(!(n==="success"||n==="raw"||n==="nextNode")){if(typeof r=="string"&&r.length<=80)s.push(`${n}: ${r}`);else if(Array.isArray(r)){let i=r.length,c=r.filter(l=>l?.passed===!0).length,a=r.some(l=>l?.passed!==void 0);s.push(a?`${n}: ${c}/${i} passed${i-c?`, ${i-c} failed`:""}`:`${n}: ${i} items`)}if(s.length>=4)break}return s}async run(e,t={}){if(!this.entryPoint)throw new Error("No entry point set for graph");let s=t.cwd||process.cwd();gt({path:P(s,".env")});let n=t.config||{};if(!n||Object.keys(n).length===0)try{let h=P(s,".zibby.config.js");de(h)&&(n=(await import(h)).default||{})}catch{}process.env.EXECUTION_ID&&!n.agent?.strictMode&&(n.agent={...n.agent,strictMode:!0});let r=t.agentType;if(!r){let h=n?.agent;h?.provider?r=h.provider:h?.gemini?r="gemini":h?.claude?r="claude":h?.cursor?r="cursor":h?.codex?r="codex":r=process.env.AGENT_TYPE||"cursor"}let i=t.contextConfig||e?.config?.contextConfig||e?.config?.context||n?.context||{};if(this.stateSchema){let h=this.stateSchema.safeParse(t);if(!h.success){let $=h.error.issues.map(O=>`${O.path.join(".")}: ${O.message}`);throw console.error("\u274C Initial state validation failed:"),$.forEach(O=>console.error(` - ${O}`)),new Error(`State validation failed: ${$.join(", ")}`)}E.step("State validated against schema")}let c=yt(),a=t.sessionPath||c;a||$t();let{sessionPath:l,sessionTimestamp:m,sessionId:d}=vt({cwd:s,config:n,traceFrom:"WorkflowGraph.run",initialState:{sessionPath:a,sessionTimestamp:t.sessionTimestamp}});E.step(`Session ${d}`);let u=await ee.loadContext(t.specPath||"",s,i);Object.keys(u).length>0&&E.step(`Context loaded: ${Object.keys(u).join(", ")}`);let g=t.outputPath;!g&&t.specPath&&(e?.calculateOutputPath?g=e.calculateOutputPath(t.specPath):console.warn(`\u26A0\uFE0F outputPath not resolved (specPath=${t.specPath})`));let p=new H({...t,config:n,agentType:r,outputPath:g,sessionPath:l,sessionTimestamp:m,context:u,resolvedTools:this.resolvedToolsMap||{}}),_=new Map;try{await import("@zibby/skills")}catch{}let{getSkill:N}=await Promise.resolve().then(()=>(X(),Re)),v=new Set;for(let[,h]of this.nodes)for(let $ of h.config?.skills||[])v.add($);for(let h of v){let $=N(h);if(typeof $?.middleware=="function")try{let O=await $.middleware();typeof O=="function"&&_.set(h,O)}catch{}}let f=this.entryPoint,S=[];for(;f&&f!=="END";){let h=P(l,be);if(de(h)){console.warn(`
37
- \u{1F6D1} Studio stop requested \u2014 ending workflow.`);try{mt(h)}catch{}if(e&&typeof e.cleanup=="function")try{await e.cleanup()}catch{}return E.step("Workflow stopped by Studio"),{success:!0,state:p.getAll(),executionLog:S,stoppedByStudio:!0}}let $=this.nodes.get(f);if(!$)throw new Error(`Node '${f}' not found in graph`);let O=JSON.stringify({sessionPath:l,sessionTimestamp:m,currentNode:f,createdAt:new Date().toISOString(),config:p.get("config")}),Ke=P(l,R);Fe(Ke,O,"utf-8");let he=p.get("config")?.paths?.output||z,Ve=P(s,he,R);Ue(P(s,he),{recursive:!0});try{Fe(Ve,O,"utf-8")}catch{}let me=t.onPipelineProgress;if(typeof me=="function")try{me({cwd:s,sessionPath:l,sessionId:d,outputBase:p.get("config")?.paths?.output||z,currentNode:f})}catch{}let ze=(this.resolvedToolsMap||{})[f]||null;p.set("_currentNodeTools",ze);let qe=p.get("nodeConfigs")||{};p.set("_currentNodeConfig",qe[f]||{}),E.nodeStart(f);let ge=Date.now(),Z=this.nodePrompts.get(f);if(!this._invokeAgent){let T=await Promise.resolve().then(()=>(U(),j));this._invokeAgent=T.invokeAgent}let Se=this._invokeAgent,we={state:p,invokeAgent:async(T={},A={})=>{let I=A.prompt||"";if(Z)try{I=St.compile(Z,{noEscape:!0})(T)}catch(b){throw console.error(`\u274C Template rendering failed for node '${f}':`,b.message),new Error(`Template rendering failed: ${b.message}`,{cause:b})}else if(!I)throw new Error(`No prompt template configured for node '${f}' and no prompt provided in options`);return Se(I,{state:p.getAll(),images:A.images||[]},{model:A.model||p.get("model"),workspace:p.get("workspace"),schema:A.schema,...A})},_coreInvokeAgent:Se,agent:e,nodeId:f,promptTemplate:Z,getPromptTemplate:()=>Z,...p.getAll()};try{let T=($.config?.skills||[]).map(M=>_.get(M)).filter(Boolean),A=[...this.middleware,...T],I;A.length>0?I=await this._composeMiddleware(A,f,async()=>$.execute(we,p),p.getAll(),p):I=await $.execute(we,p);let b=Date.now()-ge;if(S.push({node:f,success:I.success,duration:b,timestamp:new Date().toISOString()}),!I.success){if(String(I.error||"").includes("Stopped from Zibby Studio")){if(E.step("Workflow stopped by Studio"),p.set("stoppedByStudio",!0),e&&typeof e.cleanup=="function")try{await e.cleanup()}catch{}return{success:!0,state:p.getAll(),executionLog:S,stoppedByStudio:!0}}p.append("errors",{node:f,error:I.error});let _e=$.config?.retries||0,ye=`${f}_retries`,G=p.getAll()[ye]||0;if(G<_e){E.stepInfo(`Retrying (attempt ${G+1}/${_e})`),p.update({[ye]:G+1,[`${f}_raw`]:I.raw});continue}throw E.nodeFailed(f,I.error,{duration:b}),new Error(`Node '${f}' failed after ${G} attempts: ${I.error}`)}p.update({[f]:I.output});let Xe=this._summarizeNodeOutput(f,I.output);E.nodeComplete(f,{duration:b,details:Xe});let Y=this.edges.get(f);if(!Y)f="END";else if(Y.conditional){let M=Y.routes(p.getAll());E.route(f,M),f=M}else f=Y}catch(T){throw E.isInsideNode&&E.nodeFailed(f,T.message,{duration:Date.now()-ge}),p.set("failed",!0),p.set("failedAt",f),T}}E.graphComplete();let k={success:!0,state:p.getAll(),executionLog:S};return e&&typeof e.onComplete=="function"&&await e.onComplete(k),k}};var pe=new Map;function Nt(o,e){pe.set(o,e)}function Ye(o){return pe.get(o)}function fe(o){return pe.has(o)}Nt("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(()=>(U(),j))).invokeAgent);let n=e.extraPromptInstructions||"Execute the task based on the current state.",r=kt(n,t),i=await s(r,{cwd:t.workspace||process.cwd(),model:t.model,tools:e.resolvedTools||null});return{success:!0,output:{raw:i,nodeId:o},raw:typeof i=="string"?i:i.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 r=[],i=new Set;for(let c of s){let a=c.split(".")[0];if(i.has(a))continue;let l=c.split(".").reduce((u,g)=>u?.[g],e);if(l===void 0)continue;let m=typeof l=="string"?l:l?.raw??JSON.stringify(l,null,2),d=c.replace(/_/g," ").replace(/\b\w/g,u=>u.toUpperCase());r.push(`## ${d}
38
- ${m}`),c.includes(".")||i.add(a)}return r.length===0?o:`${o}
36
+ ${m}`)}}function _t(){return process.env.ZIBBY_RUN_SOURCE==="studio"||process.env.ZIBBY_KEEP_SESSION_ENV==="1"||process.env.ZIBBY_KEEP_SESSION_ENV==="true"}function yt(){if(process.env.ZIBBY_RUN_SOURCE!=="studio")return;let o=process.env.ZIBBY_SESSION_PATH;if(!(o==null||String(o).trim()===""))try{return Ye(String(o).trim())}catch{return String(o).trim()}}function $t(){_t()||(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=Ce.map(s=>process.env[s]).find(Boolean),t=Math.random().toString(36).slice(2,6),r=e||`${Date.now()}_${t}`,n=o.paths?.sessionPrefix;return n?`${n}_${r}`:r}function vt({cwd:o=process.cwd(),config:e={},initialState:t={},traceFrom:r="resolveWorkflowSession"}={}){let n=t.sessionPath,s=t.sessionTimestamp,a="initialState.sessionPath";if(!n&&process.env.ZIBBY_SESSION_PATH)try{let l=Ye(String(process.env.ZIBBY_SESSION_PATH));l&&(n=l,a="ZIBBY_SESSION_PATH")}catch{}let i;if(n)i=String(n).split(/[/\\]/).filter(Boolean).pop(),s==null&&(s=Date.now());else{let l=process.env.ZIBBY_SESSION_ID&&String(process.env.ZIBBY_SESSION_ID).trim();if(l)i=l,a="ZIBBY_SESSION_ID";else{let d=e.sessionId!=null?String(e.sessionId).trim():"";d&&d!=="last"?(i=d,a="config.sessionId"):(i=It(e),a="generated")}s=s??Date.now();let m=e.paths?.output||V;n=P(o,m,be,i)}let c=!de(n);return c&&Ue(n,{recursive:!0}),(c||a!=="initialState.sessionPath")&&wt({traceFrom:r,sessionId:i,sessionPath:n,idSource:a,mkdirFresh:c}),Et({sessionPath:n,sessionId:i}),{sessionPath:n,sessionId:i,sessionTimestamp:s}}var ee=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}setStateSchema(e){return this.stateSchema=e,this}getStateSchema(){return this.stateSchema}addNode(e,t,r={}){let n=t instanceof M?t:new M(t);return n.name=e,this.nodes.set(e,n),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 X({...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,n,s){let a=r;for(let i=e.length-1;i>=0;i--){let c=e[i],l=a;a=()=>c(t,l,n,s)}return a()}serialize(){let e=[],t={};for(let[s,a]of this.nodes){let i=this.nodeTypeMap.get(s)||s;e.push({id:s,type:i,data:{nodeType:i,label:s}});let c={};a._isCustomCode&&typeof a.execute=="function"&&(c.customCode=a.execute.toString());let l=this.nodePrompts.get(s);if(l&&(c.prompt=l),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 ${s}:`,d.message)}let m=(this.resolvedToolsMap||{})[s];m?.toolIds&&(c.tools=m.toolIds),Object.keys(c).length>0&&(t[s]=c)}let r=[];for(let[s,a]of this.edges)if(typeof a=="string")r.push({source:s,target:a});else if(a.conditional){let i=this.conditionalCodeMap.get(s)||a.routes.toString(),c=this._inferConditionalTargets(a.routes),l=a.labels||{};for(let m of c){let d={source:s,target:m,data:{conditionalCode:i}};l[m]&&(d.label=l[m]),r.push(d)}}let n=null;if(this.stateSchema)try{n=je(this.stateSchema,{target:"openApi3"})}catch{n=this.stateSchema}return{nodes:e,edges:r,nodeConfigs:t,stateSchema:n}}_inferConditionalTargets(e){let t=e.toString(),r=new Set,n=/return\s+['"]([^'"]+)['"]/g,s;for(;(s=n.exec(t))!==null;)r.add(s[1]);return[...r]}_flattenJsonSchemaToVariables(e,t=""){let r=e;if(e.$ref&&e.definitions){let n=e.$ref.replace("#/definitions/","");r=e.definitions[n]||e}return this._flattenSchema(r,t)}_flattenSchema(e,t=""){if(!e||typeof e!="object")return[];let r=[],n=e.properties||{},s=e.required||[];for(let[a,i]of Object.entries(n)){let c=t?`${t}.${a}`:a;r.push({path:c,type:i.type||"unknown",label:i.description||this._formatLabel(a),optional:!s.includes(a)}),i.type==="object"&&i.properties&&r.push(...this._flattenSchema(i,c)),i.type==="array"&&i.items?.type==="object"&&i.items.properties&&r.push(...this._flattenSchema(i.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[n,s]of Object.entries(t))if(!(n==="success"||n==="raw"||n==="nextNode")){if(typeof s=="string"&&s.length<=80)r.push(`${n}: ${s}`);else if(Array.isArray(s)){let a=s.length,i=s.filter(l=>l?.passed===!0).length,c=s.some(l=>l?.passed!==void 0);r.push(c?`${n}: ${i}/${a} passed${a-i?`, ${a-i} failed`:""}`:`${n}: ${a} items`)}if(r.length>=4)break}return r}async run(e,t={}){if(!this.entryPoint)throw new Error("No entry point set for graph");let r=t.cwd||process.cwd();gt({path:P(r,".env")});let n=t.config||{};if(!n||Object.keys(n).length===0)try{let h=P(r,".zibby.config.js");de(h)&&(n=(await import(h)).default||{})}catch{}process.env.EXECUTION_ID&&!n.agent?.strictMode&&(n.agent={...n.agent,strictMode:!0});let s=t.agentType;if(!s){let h=n?.agent;h?.provider?s=h.provider:h?.gemini?s="gemini":h?.claude?s="claude":h?.cursor?s="cursor":h?.codex?s="codex":s=process.env.AGENT_TYPE||"cursor"}let a=t.contextConfig||e?.config?.contextConfig||e?.config?.context||n?.context||{};if(this.stateSchema){let h=this.stateSchema.safeParse(t);if(!h.success){let $=h.error.issues.map(x=>`${x.path.join(".")}: ${x.message}`);throw console.error("\u274C Initial state validation failed:"),$.forEach(x=>console.error(` - ${x}`)),new Error(`State validation failed: ${$.join(", ")}`)}E.step("State validated against schema")}let i=yt(),c=t.sessionPath||i;c||$t();let{sessionPath:l,sessionTimestamp:m,sessionId:d}=vt({cwd:r,config:n,traceFrom:"WorkflowGraph.run",initialState:{sessionPath:c,sessionTimestamp:t.sessionTimestamp}});E.step(`Session ${d}`);let u=await Q.loadContext(t.specPath||"",r,a);Object.keys(u).length>0&&E.step(`Context loaded: ${Object.keys(u).join(", ")}`);let g=t.outputPath;!g&&t.specPath&&(e?.calculateOutputPath?g=e.calculateOutputPath(t.specPath):console.warn(`\u26A0\uFE0F outputPath not resolved (specPath=${t.specPath})`));let p=new H({...t,config:n,agentType:s,outputPath:g,sessionPath:l,sessionTimestamp:m,context:u,resolvedTools:this.resolvedToolsMap||{}}),_=new Map;try{await import("@zibby/skills")}catch{}let{getSkill:N}=await Promise.resolve().then(()=>(q(),Re)),v=new Set;for(let[,h]of this.nodes)for(let $ of h.config?.skills||[])v.add($);for(let h of v){let $=N(h);if(typeof $?.middleware=="function")try{let x=await $.middleware();typeof x=="function"&&_.set(h,x)}catch{}}let f=this.entryPoint,S=[];for(;f&&f!=="END";){let h=P(l,Ae);if(de(h)){console.warn(`
37
+ \u{1F6D1} Studio stop requested \u2014 ending workflow.`);try{mt(h)}catch{}if(e&&typeof e.cleanup=="function")try{await e.cleanup()}catch{}return E.step("Workflow stopped by Studio"),{success:!0,state:p.getAll(),executionLog:S,stoppedByStudio:!0}}let $=this.nodes.get(f);if(!$)throw new Error(`Node '${f}' not found in graph`);let x=JSON.stringify({sessionPath:l,sessionTimestamp:m,currentNode:f,createdAt:new Date().toISOString(),config:p.get("config")}),Ke=P(l,R);Le(Ke,x,"utf-8");let he=p.get("config")?.paths?.output||V,ze=P(r,he,R);Ue(P(r,he),{recursive:!0});try{Le(ze,x,"utf-8")}catch{}let me=t.onPipelineProgress;if(typeof me=="function")try{me({cwd:r,sessionPath:l,sessionId:d,outputBase:p.get("config")?.paths?.output||V,currentNode:f})}catch{}let Ve=(this.resolvedToolsMap||{})[f]||null;p.set("_currentNodeTools",Ve);let qe=p.get("nodeConfigs")||{};p.set("_currentNodeConfig",qe[f]||{}),E.nodeStart(f);let ge=Date.now(),Y=this.nodePrompts.get(f);if(!this._invokeAgent){let T=await Promise.resolve().then(()=>(U(),j));this._invokeAgent=T.invokeAgent}let Se=this._invokeAgent,we={state:p,invokeAgent:async(T={},b={})=>{let I=b.prompt||"";if(Y)try{I=St.compile(Y,{noEscape:!0})(T)}catch(A){throw console.error(`\u274C Template rendering failed for node '${f}':`,A.message),new Error(`Template rendering failed: ${A.message}`,{cause:A})}else if(!I)throw new Error(`No prompt template configured for node '${f}' and no prompt provided in options`);return Se(I,{state:p.getAll(),images:b.images||[]},{model:b.model||p.get("model"),workspace:p.get("workspace"),schema:b.schema,...b})},_coreInvokeAgent:Se,agent:e,nodeId:f,promptTemplate:Y,getPromptTemplate:()=>Y,...p.getAll()};try{let T=($.config?.skills||[]).map(B=>_.get(B)).filter(Boolean),b=[...this.middleware,...T],I;b.length>0?I=await this._composeMiddleware(b,f,async()=>$.execute(we,p),p.getAll(),p):I=await $.execute(we,p);let A=Date.now()-ge;if(S.push({node:f,success:I.success,duration:A,timestamp:new Date().toISOString()}),!I.success){if(String(I.error||"").includes("Stopped from Zibby Studio")){if(E.step("Workflow stopped by Studio"),p.set("stoppedByStudio",!0),e&&typeof e.cleanup=="function")try{await e.cleanup()}catch{}return{success:!0,state:p.getAll(),executionLog:S,stoppedByStudio:!0}}p.append("errors",{node:f,error:I.error});let _e=$.config?.retries||0,ye=`${f}_retries`,G=p.getAll()[ye]||0;if(G<_e){E.stepInfo(`Retrying (attempt ${G+1}/${_e})`),p.update({[ye]:G+1,[`${f}_raw`]:I.raw});continue}throw E.nodeFailed(f,I.error,{duration:A}),new Error(`Node '${f}' failed after ${G} attempts: ${I.error}`)}p.update({[f]:I.output});let Xe=this._summarizeNodeOutput(f,I.output);E.nodeComplete(f,{duration:A,details:Xe});let Z=this.edges.get(f);if(!Z)f="END";else if(Z.conditional){let B=Z.routes(p.getAll());E.route(f,B),f=B}else f=Z}catch(T){throw E.isInsideNode&&E.nodeFailed(f,T.message,{duration:Date.now()-ge}),p.set("failed",!0),p.set("failedAt",f),T}}E.graphComplete();let k={success:!0,state:p.getAll(),executionLog:S};return e&&typeof e.onComplete=="function"&&await e.onComplete(k),k}};var pe=new Map;function Nt(o,e){pe.set(o,e)}function Ze(o){return pe.get(o)}function fe(o){return pe.has(o)}Nt("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(()=>(U(),j))).invokeAgent);let n=e.extraPromptInstructions||"Execute the task based on the current state.",s=kt(n,t),a=await r(s,{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 kt(o,e){let t=/@([\w.]+)/g,r=new Set,n;for(;(n=t.exec(o))!==null;)r.add(n[1]);if(r.size===0)return o;let s=[],a=new Set;for(let i of r){let c=i.split(".")[0];if(a.has(c))continue;let l=i.split(".").reduce((u,g)=>u?.[g],e);if(l===void 0)continue;let m=typeof l=="string"?l:l?.raw??JSON.stringify(l,null,2),d=i.replace(/_/g," ").replace(/\b\w/g,u=>u.toUpperCase());s.push(`## ${d}
38
+ ${m}`),i.includes(".")||a.add(c)}return s.length===0?o:`${o}
39
39
 
40
40
  ---
41
41
  # Referenced Context
42
42
 
43
- ${r.join(`
43
+ ${s.join(`
44
44
 
45
- `)}`}X();D();var Ot={};function Je(o,e){if(Array.isArray(e))return Ge(e);let t=Ot[o];return!t||t.length===0?null:Ge(t)}function Ge(o){if(!Array.isArray(o)||o.length===0)return null;let e=[],t={},s=[];for(let n of o){let r=F(n);if(!r){w.warn(`[workflow] unknown skill "${n}" \u2014 skipping`);continue}s.push(n);for(let i of r.tools||[])e.push({name:i.name,description:i.description,input_schema:i.input_schema||{type:"object",properties:{}}});if(!t[r.serverName])if(typeof r.resolve=="function"){let i=r.resolve();i&&(t[r.serverName]={...i,toolPrefix:n})}else{let i={};for(let c of r.envKeys||[]){let a=process.env[c];a&&(i[c]=a)}t[r.serverName]={command:r.command,args:[...r.args||[]],env:i,toolPrefix:n}}}return s.length===0?null:{toolIds:s,claudeTools:e,mcpServers:t}}D();function wo(o,e={}){let{nodes:t,edges:s,nodeConfigs:n={}}=o;if(!Array.isArray(t)||t.length===0)throw new x("Graph must have at least one node");if(!Array.isArray(s))throw new x("Graph edges must be an array");let r=new te(e);e.stateSchema&&r.setStateSchema(e.stateSchema);let i=new Set,c=new Map,a={};for(let u of t){let g=oe(u);c.set(u.id,{...u,resolvedType:g}),g==="decision"&&i.add(u.id)}for(let[u,g]of c){if(i.has(u))continue;let p=g.resolvedType,_=n[u]||{},N=Je(p,_.tools);N&&(a[u]=N);let v={};_.prompt&&(v.prompt=_.prompt);let f=fe(p);if(w.debug(`[workflow] compiler: node "${u}" type="${p}" registered=${f}`),_.customCode&&!f)r.addNode(u,He(u,_.customCode,_),v),r.setNodeType(u,p);else if(f){let S=Ye(p);S.factory?r.addNode(u,S.create(u,{..._,resolvedTools:N}),v):r.addNode(u,S,v),r.setNodeType(u,p)}else if(_.executeCode)r.addNode(u,He(u,_.executeCode,_),v),r.setNodeType(u,p);else throw new x(`Unknown node type "${p}" for node "${u}". Did you forget to register it?`)}r.resolvedToolsMap=a;let l=new Set;for(let u of s)i.has(u.target)||l.add(u.target);let m=t.find(u=>!i.has(u.id)&&!l.has(u.id));if(!m)throw new x("Could not determine entry point: no node without incoming edges found");r.setEntryPoint(m.id);let d=xt(s,"source");for(let u of s)if(!i.has(u.source))if(i.has(u.target)){let g=u.target,p=d.get(g)||[];if(p.length===0)throw new x(`Decision node "${g}" has no outgoing edges`);let _=Tt(g,p,i);r.addConditionalEdges(u.source,_)}else r.addEdge(u.source,u.target);return r}function _o(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 c of o.nodes){let a=oe(c);if(a==="decision"||fe(a))continue;let l=t[c.id]||{};l.customCode||l.executeCode||e.push(`Unknown node type "${a}" for node "${c.id}". Register it or provide customCode/executeCode.`)}let s=new Set(o.nodes.map(c=>c.id));for(let c of o.edges)s.has(c.source)||e.push(`Edge references unknown source node "${c.source}"`),s.has(c.target)||e.push(`Edge references unknown target node "${c.target}"`);let n=new Set(o.nodes.filter(c=>oe(c)==="decision").map(c=>c.id)),r=new Set;for(let c of o.edges)n.has(c.target)||r.add(c.target);let i=o.nodes.filter(c=>!n.has(c.id)&&!r.has(c.id));i.length===0?e.push("No entry point found (every node has incoming edges)"):i.length>1&&e.push(`Multiple entry points found: ${i.map(c=>c.id).join(", ")}`);for(let c of n){let a=o.edges.filter(m=>m.source===c);a.length===0&&e.push(`Decision node "${c}" has no outgoing edges`),a.some(m=>m.data?.conditionalCode||m.conditionalCode)||e.push(`Decision node "${c}" outgoing edges have no conditionalCode`)}return{valid:e.length===0,errors:e}}function yo(o){return!o||!Array.isArray(o.nodes)?[]:o.nodes.filter(e=>oe(e)!=="decision").map(e=>e.id)}function oe(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 Tt(o,e,t){let s=e.find(c=>c.data?.conditionalCode||c.conditionalCode);if(!s)throw new x(`Decision node "${o}" has no conditionalCode on its outgoing edges`);let n=s.data?.conditionalCode||s.conditionalCode,r=new Set(e.map(c=>c.target).filter(c=>!t.has(c))),i;try{let a=new Function(`return (${n})`)();i=l=>{let m=a(l);return r.has(m)||w.warn(`[workflow] conditional route from "${o}" returned "${m}" which is not in valid targets: ${[...r].join(", ")}`),m}}catch(c){throw new x(`Failed to compile conditionalCode for "${o}": ${c.message}`)}return i}function He(o,e,t={}){let s;try{s=new Function("invokeAgent","require","console",`return (${e})`)}catch(i){throw new x(`Failed to compile customCode for node "${o}": ${i.message}`)}let n=s(async(...i)=>{let{invokeAgent:c}=await Promise.resolve().then(()=>(U(),j));return c(...i)},typeof se<"u"?se:void 0,console),r=null;return t.outputSchema&&(r=t.outputSchema.jsonSchema||t.outputSchema),{name:o,_isCustomCode:!0,outputSchema:r,execute:async i=>{try{let c=await n(i);return typeof c=="object"&&"success"in c?c:{success:!0,output:c,raw:null}}catch(c){return{success:!1,error:c.message,raw:null}}}}}var x=class extends Error{constructor(e){super(e),this.name="CompilationError"}};export{x as CompilationError,wo as compileGraph,yo as extractSteps,_o as validateGraphConfig};
45
+ `)}`}q();D();var xt={};function Je(o,e){if(Array.isArray(e))return Ge(e);let t=xt[o];return!t||t.length===0?null:Ge(t)}function Ge(o){if(!Array.isArray(o)||o.length===0)return null;let e=[],t={},r=[];for(let n of o){let s=L(n);if(!s){w.warn(`[workflow] unknown skill "${n}" \u2014 skipping`);continue}r.push(n);for(let a of s.tools||[])e.push({name:a.name,description:a.description,input_schema:a.input_schema||{type:"object",properties:{}}});if(!t[s.serverName])if(typeof s.resolve=="function"){let a=s.resolve();a&&(t[s.serverName]={...a,toolPrefix:n})}else{let a={};for(let i of s.envKeys||[]){let c=process.env[i];c&&(a[i]=c)}t[s.serverName]={command:s.command,args:[...s.args||[]],env:a,toolPrefix:n}}}return r.length===0?null:{toolIds:r,claudeTools:e,mcpServers:t}}D();function _o(o,e={}){let{nodes:t,edges:r,nodeConfigs:n={}}=o;if(!Array.isArray(t)||t.length===0)throw new O("Graph must have at least one node");if(!Array.isArray(r))throw new O("Graph edges must be an array");let s=new ee(e);e.stateSchema&&s.setStateSchema(e.stateSchema);let a=new Set,i=new Map,c={};for(let u of t){let g=te(u);i.set(u.id,{...u,resolvedType:g}),g==="decision"&&a.add(u.id)}for(let[u,g]of i){if(a.has(u))continue;let p=g.resolvedType,_=n[u]||{},N=Je(p,_.tools);N&&(c[u]=N);let v={};_.prompt&&(v.prompt=_.prompt);let f=fe(p);if(w.debug(`[workflow] compiler: node "${u}" type="${p}" registered=${f}`),_.customCode&&!f)s.addNode(u,He(u,_.customCode,_),v),s.setNodeType(u,p);else if(f){let S=Ze(p);S.factory?s.addNode(u,S.create(u,{..._,resolvedTools:N}),v):s.addNode(u,S,v),s.setNodeType(u,p)}else if(_.executeCode)s.addNode(u,He(u,_.executeCode,_),v),s.setNodeType(u,p);else throw new O(`Unknown node type "${p}" for node "${u}". Did you forget to register it?`)}s.resolvedToolsMap=c;let l=new Set;for(let u of r)a.has(u.target)||l.add(u.target);let m=t.find(u=>!a.has(u.id)&&!l.has(u.id));if(!m)throw new O("Could not determine entry point: no node without incoming edges found");s.setEntryPoint(m.id);let d=Ot(r,"source");for(let u of r)if(!a.has(u.source))if(a.has(u.target)){let g=u.target,p=d.get(g)||[];if(p.length===0)throw new O(`Decision node "${g}" has no outgoing edges`);let _=Tt(g,p,a);s.addConditionalEdges(u.source,_)}else s.addEdge(u.source,u.target);return s}function yo(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 i of o.nodes){let c=te(i);if(c==="decision"||fe(c))continue;let l=t[i.id]||{};l.customCode||l.executeCode||e.push(`Unknown node type "${c}" for node "${i.id}". Register it or provide customCode/executeCode.`)}let r=new Set(o.nodes.map(i=>i.id));for(let i of o.edges)r.has(i.source)||e.push(`Edge references unknown source node "${i.source}"`),r.has(i.target)||e.push(`Edge references unknown target node "${i.target}"`);let n=new Set(o.nodes.filter(i=>te(i)==="decision").map(i=>i.id)),s=new Set;for(let i of o.edges)n.has(i.target)||s.add(i.target);let a=o.nodes.filter(i=>!n.has(i.id)&&!s.has(i.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(i=>i.id).join(", ")}`);for(let i of n){let c=o.edges.filter(m=>m.source===i);c.length===0&&e.push(`Decision node "${i}" has no outgoing edges`),c.some(m=>m.data?.conditionalCode||m.conditionalCode)||e.push(`Decision node "${i}" outgoing edges have no conditionalCode`)}return{valid:e.length===0,errors:e}}function $o(o){return!o||!Array.isArray(o.nodes)?[]:o.nodes.filter(e=>te(e)!=="decision").map(e=>e.id)}function te(o){let e=o.data?.nodeType||o.data?.type||o.type;return e==="workflowNode"||e==="custom"||e==="default"?o.id:e}function Ot(o,e){let t=new Map;for(let r of o){let n=r[e];t.has(n)||t.set(n,[]),t.get(n).push(r)}return t}function Tt(o,e,t){let r=e.find(i=>i.data?.conditionalCode||i.conditionalCode);if(!r)throw new O(`Decision node "${o}" has no conditionalCode on its outgoing edges`);let n=r.data?.conditionalCode||r.conditionalCode,s=new Set(e.map(i=>i.target).filter(i=>!t.has(i))),a;try{let c=new Function(`return (${n})`)();a=l=>{let m=c(l);return s.has(m)||w.warn(`[workflow] conditional route from "${o}" returned "${m}" which is not in valid targets: ${[...s].join(", ")}`),m}}catch(i){throw new O(`Failed to compile conditionalCode for "${o}": ${i.message}`)}return a}function He(o,e,t={}){let r;try{r=new Function("invokeAgent","require","console",`return (${e})`)}catch(a){throw new O(`Failed to compile customCode for node "${o}": ${a.message}`)}let n=r(async(...a)=>{let{invokeAgent:i}=await Promise.resolve().then(()=>(U(),j));return i(...a)},typeof oe<"u"?oe:void 0,console),s=null;return t.outputSchema&&(s=t.outputSchema.jsonSchema||t.outputSchema),{name:o,_isCustomCode:!0,outputSchema:s,execute:async a=>{try{let i=await n(a);return typeof i=="object"&&"success"in i?i:{success:!0,output:i,raw:null}}catch(i){return{success:!1,error:i.message,raw:null}}}}}var O=class extends Error{constructor(e){super(e),this.name="CompilationError"}};export{O as CompilationError,_o as compileGraph,$o as extractSteps,yo as validateGraphConfig};
package/dist/graph.js CHANGED
@@ -1,4 +1,4 @@
1
- var Yt=Object.defineProperty;var j=(n,t)=>()=>(n&&(t=n(n=0)),t);var mt=(n,t)=>{for(var e in t)Yt(n,e,{get:t[e],enumerable:!0})};var gt,Jt,Y,w,z=j(()=>{gt=()=>{},Jt={debug:gt,info:gt,warn:(...n)=>console.warn("[workflow]",...n),error:(...n)=>console.error("[workflow]",...n)},Y={impl:Jt},w={debug:(...n)=>Y.impl.debug?.(...n),info:(...n)=>Y.impl.info?.(...n),warn:(...n)=>Y.impl.warn?.(...n),error:(...n)=>Y.impl.error?.(...n)}});var J,vt=j(()=>{J=class{constructor(t,e,o=0){this.name=t,this.description=e,this.priority=o}async invoke(t,e={}){throw new Error(`${this.constructor.name}.invoke() must be implemented`)}canHandle(t){throw new Error(`${this.constructor.name}.canHandle() must be implemented`)}getName(){return this.name}getDescription(){return this.description}getPriority(){return this.priority}}});var bt={};mt(bt,{clearSkills:()=>Qt,getAllSkills:()=>qt,getSkill:()=>tt,hasSkill:()=>zt,listSkillIds:()=>Xt,registerSkill:()=>Vt});function Vt(n){if(!n||typeof n.id!="string")throw new Error("Skill definition must include a string id");B.set(n.id,Object.freeze({...n}))}function tt(n){return B.get(n)||null}function zt(n){return B.has(n)}function qt(){return new Map(B)}function Xt(){return Array.from(B.keys())}function Qt(){B.clear()}var B,et=j(()=>{B=new Map});var ot={};mt(ot,{getAgentStrategy:()=>Tt,invokeAgent:()=>oe,listStrategies:()=>ee,registerStrategy:()=>te});function te(n){if(!(n instanceof J))throw new Error("strategy must be an instance of AgentStrategy");let t=x.findIndex(e=>e.getName()===n.getName());t>=0?x[t]=n:x.push(n)}function ee(){return x.map(n=>n.getName())}function Tt(n={}){let{state:t={},preferredAgent:e=null}=n,o=e||t.agentType||process.env.AGENT_TYPE;if(!o){let r=x.map(i=>i.getName()).join(", ")||"none registered";throw new Error(`No agent specified. Set agentType in state or AGENT_TYPE env var. Available: ${r}`)}w.debug(`[workflow] agent selection: requested=${o}`);let s=x.find(r=>r.getName()===o);if(!s){let r=x.map(i=>i.getName()).join(", ")||"none registered";throw new Error(`Unknown agent '${o}'. Available: ${r}`)}if(!s.canHandle(n))throw new Error(`Agent '${o}' is not available in this environment. Check credentials/environment.`);return w.debug(`[workflow] using agent: ${s.getName()}`),s}async function oe(n,t={},e={}){let o=Tt(t),s=t.state?.config||e.config||{},r=s.models||{},i=e.nodeName&&r[e.nodeName]||null,a=r.default||null,c=s.agent?.[o.name]?.model||null,u=i||a||c||e.model||null,h={...e,model:u,workspace:t.state?.workspace||e.workspace,schema:e.schema||t.schema,images:e.images||t.images||[],skills:e.skills||t.skills||[],config:s},l=n,S=h.skills||[];if(S.length>0&&!e.skipPromptFragments){let f=S.map(v=>{let b=tt(v)?.promptFragment;return typeof b=="function"?b():b}).filter(Boolean);f.length>0&&(l+=`
1
+ var Zt=Object.defineProperty;var j=(n,t)=>()=>(n&&(t=n(n=0)),t);var mt=(n,t)=>{for(var e in t)Zt(n,e,{get:t[e],enumerable:!0})};var gt,Jt,Z,_,V=j(()=>{gt=()=>{},Jt={debug:gt,info:gt,warn:(...n)=>console.warn("[workflow]",...n),error:(...n)=>console.error("[workflow]",...n)},Z={impl:Jt},_={debug:(...n)=>Z.impl.debug?.(...n),info:(...n)=>Z.impl.info?.(...n),warn:(...n)=>Z.impl.warn?.(...n),error:(...n)=>Z.impl.error?.(...n)}});var bt=j(()=>{});var vt={};mt(vt,{clearSkills:()=>Qt,getAllSkills:()=>qt,getSkill:()=>Q,hasSkill:()=>zt,listSkillIds:()=>Xt,registerSkill:()=>Vt});function Vt(n){if(!n||typeof n.id!="string")throw new Error("Skill definition must include a string id");R.set(n.id,Object.freeze({...n}))}function Q(n){return R.get(n)||null}function zt(n){return R.has(n)}function qt(){return new Map(R)}function Xt(){return Array.from(R.keys())}function Qt(){R.clear()}var R,tt=j(()=>{R=new Map});var ot={};mt(ot,{getAgentStrategy:()=>Tt,invokeAgent:()=>oe,listStrategies:()=>ee,registerStrategy:()=>te});function te(n){if(!n||typeof n.getName!="function"||typeof n.invoke!="function")throw new Error("strategy must implement getName() and invoke() (AgentStrategy shape)");let t=x.findIndex(e=>e.getName()===n.getName());t>=0?x[t]=n:x.push(n)}function ee(){return x.map(n=>n.getName())}function Tt(n={}){let{state:t={},preferredAgent:e=null}=n,o=e||t.agentType||process.env.AGENT_TYPE;if(!o){let r=x.map(i=>i.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=${o}`);let s=x.find(r=>r.getName()===o);if(!s){let r=x.map(i=>i.getName()).join(", ")||"none registered";throw new Error(`Unknown agent '${o}'. Available: ${r}`)}if(!s.canHandle(n))throw new Error(`Agent '${o}' is not available in this environment. Check credentials/environment.`);return _.debug(`[workflow] using agent: ${s.getName()}`),s}async function oe(n,t={},e={}){let o=Tt(t),s=t.state?.config||e.config||{},r=s.models||{},i=e.nodeName&&r[e.nodeName]||null,a=r.default||null,c=s.agent?.[o.name]?.model||null,u=i||a||c||e.model||null,h={...e,model:u,workspace:t.state?.workspace||e.workspace,schema:e.schema||t.schema,images:e.images||t.images||[],skills:e.skills||t.skills||[],config:s},l=n,S=h.skills||[];if(S.length>0&&!e.skipPromptFragments){let f=S.map(b=>{let v=Q(b)?.promptFragment;return typeof v=="function"?v():v}).filter(Boolean);f.length>0&&(l+=`
2
2
 
3
3
  ${f.join(`
4
4
 
@@ -9,29 +9,29 @@ 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
  ${I}
12
- `),w.debug(`[workflow] prompt length: ${l.length} chars`),o.invoke(l,h)}var x,st=j(()=>{vt();z();et();x=[]});var Gt=new Set(["__proto__","constructor","prototype"]);function V(n){if(Gt.has(n))throw new Error(`Invalid state key: "${n}"`)}var U=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){V(t),this._history.push({...this._state}),this._state[t]=e}update(t){let e=Object.getOwnPropertyNames(t);for(let o of e)V(o);this._history.push({...this._state});for(let o of e)this._state[o]=t[o]}append(t,e){V(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 Z=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]*\}/);return o?this.validate(JSON.parse(o[0])):this.validate({result:t.trim()})}validate(t){let e=[];for(let[o,s]of Object.entries(this.schema)){if(s.required&&!(o in t)&&e.push(`Missing required field: ${o}`),o in t&&s.type){let r=typeof t[o];r!==s.type&&e.push(`Field '${o}' expected ${s.type}, got ${r}`)}if(s.validate&&o in t){let r=s.validate(t[o]);r&&e.push(`Field '${o}': ${r}`)}}if(e.length>0)throw new Error(`Output validation failed:
12
+ `),_.debug(`[workflow] prompt length: ${l.length} chars`),o.invoke(l,h)}var et,x,st=j(()=>{bt();V();tt();et=Symbol.for("@zibby/agent-workflow.strategies");globalThis[et]||(globalThis[et]=[]);x=globalThis[et]});var Gt=new Set(["__proto__","constructor","prototype"]);function K(n){if(Gt.has(n))throw new Error(`Invalid state key: "${n}"`)}var U=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){K(t),this._history.push({...this._state}),this._state[t]=e}update(t){let e=Object.getOwnPropertyNames(t);for(let o of e)K(o);this._history.push({...this._state});for(let o of e)this._state[o]=t[o]}append(t,e){K(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 Y=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]*\}/);return o?this.validate(JSON.parse(o[0])):this.validate({result:t.trim()})}validate(t){let e=[];for(let[o,s]of Object.entries(this.schema)){if(s.required&&!(o in t)&&e.push(`Missing required field: ${o}`),o in t&&s.type){let r=typeof t[o];r!==s.type&&e.push(`Field '${o}' expected ${s.type}, got ${r}`)}if(s.validate&&o in t){let r=s.validate(t[o]);r&&e.push(`Field '${o}': ${r}`)}}if(e.length>0)throw new Error(`Output validation failed:
13
13
  ${e.join(`
14
- `)}`);return t}};z();import{writeFileSync as rt,readFileSync as xt,existsSync as At,mkdirSync as se}from"node:fs";import{join as nt,dirname as re}from"node:path";import g from"chalk";var Ht="__WORKFLOW_GRAPH_LOG__",M=g.gray("\u2502"),Kt=g.gray("\u250C"),St=g.gray("\u2514"),q=g.green("\u25C6"),_t=g.hex("#c084fc")("\u25C6"),wt=g.hex("#2dd4bf")("\u25C6"),X=g.red("\u25C6"),yt=`${M} `,Et=2;function It(n){return n<1e3?`${n}ms`:`${(n/1e3).toFixed(1)}s`}function $t(n,t){return(e,o,s)=>{if(typeof e!="string")return n(e,o,s);let r=process.stdout.columns||120,i="";for(let a=0;a<e.length;a++){let c=e[a];t.lineStart&&(i+=yt,t.col=Et,t.lineStart=!1),c===`
14
+ `)}`);return t}};V();import{writeFileSync as rt,readFileSync as xt,existsSync as At,mkdirSync as se}from"node:fs";import{join as nt,dirname as re}from"node:path";import g from"chalk";var Ht="__WORKFLOW_GRAPH_LOG__",M=g.gray("\u2502"),Kt=g.gray("\u250C"),St=g.gray("\u2514"),z=g.green("\u25C6"),wt=g.hex("#c084fc")("\u25C6"),_t=g.hex("#2dd4bf")("\u25C6"),q=g.red("\u25C6"),yt=`${M} `,Et=2;function It(n){return n<1e3?`${n}ms`:`${(n/1e3).toFixed(1)}s`}function $t(n,t){return(e,o,s)=>{if(typeof e!="string")return n(e,o,s);let r=process.stdout.columns||120,i="";for(let a=0;a<e.length;a++){let c=e[a];t.lineStart&&(i+=yt,t.col=Et,t.lineStart=!1),c===`
15
15
  `?(i+=c,t.lineStart=!0,t.col=0,t.inEsc=!1):c==="\x1B"?(t.inEsc=!0,i+=c):t.inEsc?(i+=c,(c>="A"&&c<="Z"||c>="a"&&c<="z")&&(t.inEsc=!1)):(t.col++,i+=c,t.col>=r&&(i+=`
16
- ${yt}`,t.col=Et))}return n(i,o,s)}}var Q=class{constructor(){this._currentNode=null,this._origStdoutWrite=null,this._origStderrWrite=null;let t=String(process.env.ZIBBY_RUN_SOURCE||"").trim().toLowerCase(),e=String(process.env.ZIBBY_WORKFLOW_GRAPH_LOG_MARKERS||"").trim()==="1";this._emitWorkflowGraphMarkers=e||t==="studio"}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=$t(this._origStdoutWrite,t),process.stderr.write=$t(this._origStderrWrite,e)}_stopIntercepting(){this._origStdoutWrite&&(this._outState&&!this._outState.lineStart&&this._origStdoutWrite(`
16
+ ${yt}`,t.col=Et))}return n(i,o,s)}}var X=class{constructor(){this._currentNode=null,this._origStdoutWrite=null,this._origStderrWrite=null;let t=String(process.env.ZIBBY_RUN_SOURCE||"").trim().toLowerCase(),e=String(process.env.ZIBBY_WORKFLOW_GRAPH_LOG_MARKERS||"").trim()==="1";this._emitWorkflowGraphMarkers=e||t==="studio"}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=$t(this._origStdoutWrite,t),process.stderr.write=$t(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=`${Ht}${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(q,t):process.stdout.write.bind(process.stdout)(`${M} ${q} ${t}
24
- `)}stepInfo(t){this.step(t)}stepTool(t){this._origStdoutWrite?this._writeDot(_t,t):process.stdout.write.bind(process.stdout)(`${M} ${_t} ${t}
25
- `)}stepMemory(t){let e=g.hex("#2dd4bf")(t);this._origStdoutWrite?this._writeDot(wt,e):process.stdout.write.bind(process.stdout)(`${M} ${wt} ${e}
26
- `)}stepFail(t){this._origStdoutWrite?this._writeDot(X,g.red(t)):process.stdout.write.bind(process.stdout)(`${M} ${X} ${g.red(t)}
27
- `)}nodeStart(t){this._currentNode=t,this._emitGraphLogMarker({phase:"node_begin",node:t}),this._rawWrite(`${Kt} ${t}`),this._startIntercepting()}nodeComplete(t,e={}){this._stopIntercepting();let{duration:o,details:s}=e;if(s)for(let i of s)this._rawWrite(`${q} ${i}`);let r=o?g.dim(` ${It(o)}`):"";this._rawWrite(`${St} ${g.green("done")}${r}`),this._emitGraphLogMarker({phase:"node_end",node:t}),this._rawWrite("")}nodeFailed(t,e,o={}){this._stopIntercepting();let{duration:s}=o,r=s?g.dim(` ${It(s)}`):"";this._rawWrite(`${X} ${g.red(e)}`),this._rawWrite(`${St} ${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"))}},y=new Q;var G=".zibby/output",Ot="sessions",C=".session-info.json",Nt=".zibby-studio-stop";var kt=["CI_JOB_ID","GITHUB_RUN_ID","CIRCLE_WORKFLOW_ID","BUILD_ID"];var W=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 Z(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,s=l=>e&&typeof e.get=="function"?e.get(l):t?.[l];if(typeof this.customExecute=="function"){w.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?(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.errors,null,2)}`),{success:!1,error:l.message,raw:null}}}let r=typeof this.prompt=="function"?this.prompt(o()):this.prompt,i=s("_skillHints");i&&(r=`${i}
23
+ `)}step(t){this._origStdoutWrite?this._writeDot(z,t):process.stdout.write.bind(process.stdout)(`${M} ${z} ${t}
24
+ `)}stepInfo(t){this.step(t)}stepTool(t){this._origStdoutWrite?this._writeDot(wt,t):process.stdout.write.bind(process.stdout)(`${M} ${wt} ${t}
25
+ `)}stepMemory(t){let e=g.hex("#2dd4bf")(t);this._origStdoutWrite?this._writeDot(_t,e):process.stdout.write.bind(process.stdout)(`${M} ${_t} ${e}
26
+ `)}stepFail(t){this._origStdoutWrite?this._writeDot(q,g.red(t)):process.stdout.write.bind(process.stdout)(`${M} ${q} ${g.red(t)}
27
+ `)}nodeStart(t){this._currentNode=t,this._emitGraphLogMarker({phase:"node_begin",node:t}),this._rawWrite(`${Kt} ${t}`),this._startIntercepting()}nodeComplete(t,e={}){this._stopIntercepting();let{duration:o,details:s}=e;if(s)for(let i of s)this._rawWrite(`${z} ${i}`);let r=o?g.dim(` ${It(o)}`):"";this._rawWrite(`${St} ${g.green("done")}${r}`),this._emitGraphLogMarker({phase:"node_end",node:t}),this._rawWrite("")}nodeFailed(t,e,o={}){this._stopIntercepting();let{duration:s}=o,r=s?g.dim(` ${It(s)}`):"";this._rawWrite(`${q} ${g.red(e)}`),this._rawWrite(`${St} ${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"))}},y=new X;var G=".zibby/output",Ot="sessions",C=".session-info.json",Nt=".zibby-studio-stop";var kt=["CI_JOB_ID","GITHUB_RUN_ID","CIRCLE_WORKFLOW_ID","BUILD_ID"];var W=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 Y(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,s=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.errors,null,2)}`),{success:!1,error:l.message,raw:null}}}let r=typeof this.prompt=="function"?this.prompt(o()):this.prompt,i=s("_skillHints");i&&(r=`${i}
28
28
 
29
- ${r}`);let a=o(),c=a.cwd||process.cwd(),u=a.sessionPath;try{if(u){let l=nt(u,C);if(At(l)){let I=JSON.parse(xt(l,"utf-8"));I.currentNode=this.name,rt(l,JSON.stringify(I,null,2),"utf-8")}let S=nt(u,"..",C);if(At(S))try{let I=JSON.parse(xt(S,"utf-8"));I.currentNode=this.name,rt(S,JSON.stringify(I,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 S=o().config||{},I=S.agents||{},f=this.config.agent??I[this.name]??null,v={state:o()};f&&(v.preferredAgent=f);let b={workspace:c,schema:this.isZodSchema?this.outputSchema:null,skills:this.config.skills||[],sessionPath:u,config:S,nodeName:this.name,timeout:this.config?.timeout||3e5},P=t?._coreInvokeAgent;P||(P=(await Promise.resolve().then(()=>(st(),ot))).invokeAgent);let d=await P(r,v,b),m,$;if(typeof d=="string"?(m=d,$=null):d.structured?(m=d.raw||JSON.stringify(d.structured,null,2),$=d.structured):(m=d.raw||JSON.stringify(d,null,2),$=d.extracted||null),u)try{let p=nt(u,this.name,"raw_stream_output.txt");se(re(p),{recursive:!0}),rt(p,typeof m=="string"?m:JSON.stringify(m),"utf-8")}catch(p){w.debug(`[workflow] could not save raw output: ${p.message}`)}if(this.isZodSchema&&$){w.info(`[workflow] node '${this.name}': output validated: ${JSON.stringify($,null,2)}`);let p=$;if(typeof this.onComplete=="function")try{p=await this.onComplete(o(),$)}catch(_){w.warn(`[workflow] onComplete hook failed: ${_.message}`)}return{success:!0,output:p,raw:m}}if(typeof this.onComplete=="function")try{return{success:!0,output:await this.onComplete(o(),{raw:m}),raw:m}}catch(p){throw new Error(`onComplete failed: ${p.message}`,{cause:p})}if(this.parser){let p=this.parser.parse(m);return w.info(`[workflow] node '${this.name}': parsed output: ${JSON.stringify(p,null,2)}`),y.step("Output parsed"),{success:!0,output:p,raw:m}}return{success:!0,output:m,raw:m}}catch(S){h=S,l<this.retries&&w.info(`[workflow] node '${this.name}' failed, retrying (${l+1}/${this.retries})\u2026`)}return{success:!1,error:h.message,raw:null}}},H=class extends W{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}}};import{existsSync as Pt,readFileSync as ne}from"node:fs";import{join as it,dirname as Ct}from"node:path";var K=class{static async loadContext(t,e,o={}){let s={},r=o.filenames||["CONTEXT.md","AGENTS.md"];if(t){let a=Ct(it(e,t));for(let c of r){let u=await this.findAndMergeContextFiles(c,a,e);if(u){let h=c.replace(/\.[^.]+$/,"").toLowerCase();s[h]=u}}}let i=o.discovery||{};for(let[a,c]of Object.entries(i))try{let u=it(e,c);Pt(u)&&(s[a]=await this.loadFile(u))}catch(u){console.warn(`[workflow] could not load context '${a}' from '${c}': ${u.message}`)}return s}static async findAndMergeContextFiles(t,e,o){let s=[],r=e;for(;r.startsWith(o);){let i=it(r,t);if(Pt(i))try{s.unshift(await this.loadFile(i))}catch(c){console.warn(`[workflow] could not load ${t} from ${i}: ${c.message}`)}let a=Ct(r);if(a===r)break;r=a}return s.length===0?null:s.every(i=>typeof i=="string")?s.join(`
29
+ ${r}`);let a=o(),c=a.cwd||process.cwd(),u=a.sessionPath;try{if(u){let l=nt(u,C);if(At(l)){let I=JSON.parse(xt(l,"utf-8"));I.currentNode=this.name,rt(l,JSON.stringify(I,null,2),"utf-8")}let S=nt(u,"..",C);if(At(S))try{let I=JSON.parse(xt(S,"utf-8"));I.currentNode=this.name,rt(S,JSON.stringify(I,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 S=o().config||{},I=S.agents||{},f=this.config.agent??I[this.name]??null,b={state:o()};f&&(b.preferredAgent=f);let v={workspace:c,schema:this.isZodSchema?this.outputSchema:null,skills:this.config.skills||[],sessionPath:u,config:S,nodeName:this.name,timeout:this.config?.timeout||3e5},P=t?._coreInvokeAgent;P||(P=(await Promise.resolve().then(()=>(st(),ot))).invokeAgent);let d=await P(r,b,v),m,$;if(typeof d=="string"?(m=d,$=null):d.structured?(m=d.raw||JSON.stringify(d.structured,null,2),$=d.structured):(m=d.raw||JSON.stringify(d,null,2),$=d.extracted||null),u)try{let p=nt(u,this.name,"raw_stream_output.txt");se(re(p),{recursive:!0}),rt(p,typeof m=="string"?m:JSON.stringify(m),"utf-8")}catch(p){_.debug(`[workflow] could not save raw output: ${p.message}`)}if(this.isZodSchema&&$){_.info(`[workflow] node '${this.name}': output validated: ${JSON.stringify($,null,2)}`);let p=$;if(typeof this.onComplete=="function")try{p=await this.onComplete(o(),$)}catch(w){_.warn(`[workflow] onComplete hook failed: ${w.message}`)}return{success:!0,output:p,raw:m}}if(typeof this.onComplete=="function")try{return{success:!0,output:await this.onComplete(o(),{raw:m}),raw:m}}catch(p){throw new Error(`onComplete failed: ${p.message}`,{cause:p})}if(this.parser){let p=this.parser.parse(m);return _.info(`[workflow] node '${this.name}': parsed output: ${JSON.stringify(p,null,2)}`),y.step("Output parsed"),{success:!0,output:p,raw:m}}return{success:!0,output:m,raw:m}}catch(S){h=S,l<this.retries&&_.info(`[workflow] node '${this.name}' failed, retrying (${l+1}/${this.retries})\u2026`)}return{success:!1,error:h.message,raw:null}}},J=class extends W{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}}};import{existsSync as Pt,readFileSync as ne}from"node:fs";import{join as it,dirname as Ct}from"node:path";var H=class{static async loadContext(t,e,o={}){let s={},r=o.filenames||["CONTEXT.md","AGENTS.md"];if(t){let a=Ct(it(e,t));for(let c of r){let u=await this.findAndMergeContextFiles(c,a,e);if(u){let h=c.replace(/\.[^.]+$/,"").toLowerCase();s[h]=u}}}let i=o.discovery||{};for(let[a,c]of Object.entries(i))try{let u=it(e,c);Pt(u)&&(s[a]=await this.loadFile(u))}catch(u){console.warn(`[workflow] could not load context '${a}' from '${c}': ${u.message}`)}return s}static async findAndMergeContextFiles(t,e,o){let s=[],r=e;for(;r.startsWith(o);){let i=it(r,t);if(Pt(i))try{s.unshift(await this.loadFile(i))}catch(c){console.warn(`[workflow] could not load ${t} from ${i}: ${c.message}`)}let a=Ct(r);if(a===r)break;r=a}return s.length===0?null:s.every(i=>typeof i=="string")?s.join(`
30
30
 
31
31
  ---
32
32
 
33
- `):s.every(i=>typeof i=="object")?Object.assign({},...s):s[s.length-1]}static async loadFile(t){let e=ne(t,"utf-8");if(t.endsWith(".json"))return JSON.parse(e);if(t.endsWith(".js")||t.endsWith(".mjs")){let{pathToFileURL:o}=await import("url"),s=await import(o(t).href);return s.default||s}return e}};import{mkdirSync as Mt,existsSync as at,writeFileSync as Bt,unlinkSync as ie}from"node:fs";import{join as A,resolve as Lt}from"node:path";import{config as ae}from"dotenv";import{zodToJsonSchema as Wt}from"zod-to-json-schema";import ce from"handlebars";function le({traceFrom:n,sessionId:t,sessionPath:e,idSource:o,mkdirFresh:s}){if(process.env.ZIBBY_SESSION_LOG==="0"||process.env.ZIBBY_SESSION_LOG==="false")return;let r=typeof process.ppid=="number"?process.ppid:"n/a",i=`[zibby:session] from=${n} pid=${process.pid} ppid=${r} sessionId=${t} source=${o} mkdir=${s?"yes":"no"} path=${e}`;if(console.log(i),(process.env.ZIBBY_TRACE_SESSION==="1"||process.env.ZIBBY_TRACE_SESSION==="true")&&process.env.ZIBBY_SESSION_LOG!=="0"&&process.env.ZIBBY_SESSION_LOG!=="false"){let u=(new Error("session trace").stack||"").split(`
33
+ `):s.every(i=>typeof i=="object")?Object.assign({},...s):s[s.length-1]}static async loadFile(t){let e=ne(t,"utf-8");if(t.endsWith(".json"))return JSON.parse(e);if(t.endsWith(".js")||t.endsWith(".mjs")){let{pathToFileURL:o}=await import("url"),s=await import(o(t).href);return s.default||s}return e}};import{mkdirSync as Mt,existsSync as at,writeFileSync as Rt,unlinkSync as ie}from"node:fs";import{join as A,resolve as Lt}from"node:path";import{config as ae}from"dotenv";import{zodToJsonSchema as Wt}from"zod-to-json-schema";import ce from"handlebars";function le({traceFrom:n,sessionId:t,sessionPath:e,idSource:o,mkdirFresh:s}){if(!(process.env.ZIBBY_SESSION_LOG==="1"||process.env.ZIBBY_SESSION_LOG==="true"))return;let i=typeof process.ppid=="number"?process.ppid:"n/a",a=`[zibby:session] from=${n} pid=${process.pid} ppid=${i} sessionId=${t} source=${o} mkdir=${s?"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 (${n}):
36
- ${u}`)}}function ue(){return process.env.ZIBBY_RUN_SOURCE==="studio"||process.env.ZIBBY_KEEP_SESSION_ENV==="1"||process.env.ZIBBY_KEEP_SESSION_ENV==="true"}function de(){if(process.env.ZIBBY_RUN_SOURCE!=="studio")return;let n=process.env.ZIBBY_SESSION_PATH;if(!(n==null||String(n).trim()===""))try{return Lt(String(n).trim())}catch{return String(n).trim()}}function pe(){ue()||(delete process.env.ZIBBY_SESSION_PATH,delete process.env.ZIBBY_SESSION_ID)}function fe({sessionPath:n,sessionId:t}){n&&typeof n=="string"&&(process.env.ZIBBY_SESSION_PATH=n),t!=null&&String(t).trim()!==""&&(process.env.ZIBBY_SESSION_ID=String(t).trim())}function he(n={}){let t=kt.map(r=>process.env[r]).find(Boolean),e=Math.random().toString(36).slice(2,6),o=t||`${Date.now()}_${e}`,s=n.paths?.sessionPrefix;return s?`${s}_${o}`:o}function me({cwd:n=process.cwd(),config:t={},initialState:e={},traceFrom:o="resolveWorkflowSession"}={}){let s=e.sessionPath,r=e.sessionTimestamp,i="initialState.sessionPath";if(!s&&process.env.ZIBBY_SESSION_PATH)try{let u=Lt(String(process.env.ZIBBY_SESSION_PATH));u&&(s=u,i="ZIBBY_SESSION_PATH")}catch{}let a;if(s)a=String(s).split(/[/\\]/).filter(Boolean).pop(),r==null&&(r=Date.now());else{let u=process.env.ZIBBY_SESSION_ID&&String(process.env.ZIBBY_SESSION_ID).trim();if(u)a=u,i="ZIBBY_SESSION_ID";else{let l=t.sessionId!=null?String(t.sessionId).trim():"";l&&l!=="last"?(a=l,i="config.sessionId"):(a=he(t),i="generated")}r=r??Date.now();let h=t.paths?.output||G;s=A(n,h,Ot,a)}let c=!at(s);return c&&Mt(s,{recursive:!0}),(c||i!=="initialState.sessionPath")&&le({traceFrom:o,sessionId:a,sessionPath:s,idSource:i,mkdirFresh:c}),fe({sessionPath:s,sessionId:a}),{sessionPath:s,sessionId:a,sessionTimestamp:r}}var Rt=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}setStateSchema(t){return this.stateSchema=t,this}getStateSchema(){return this.stateSchema}addNode(t,e,o={}){let s=e instanceof W?e:new W(e);return s.name=t,this.nodes.set(t,s),o.prompt&&this.nodePrompts.set(t,o.prompt),Object.keys(o).length>0&&this.nodeOptions.set(t,o),this}addConditionalNode(t,e){return this.nodes.set(t,new H({...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:o}={}){return this.edges.set(t,{conditional:!0,routes:e,labels:o}),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,o,s,r){let i=o;for(let a=t.length-1;a>=0;a--){let c=t[a],u=i;i=()=>c(e,u,s,r)}return i()}serialize(){let t=[],e={};for(let[r,i]of this.nodes){let a=this.nodeTypeMap.get(r)||r;t.push({id:r,type:a,data:{nodeType:a,label:r}});let c={};i._isCustomCode&&typeof i.execute=="function"&&(c.customCode=i.execute.toString());let u=this.nodePrompts.get(r);if(u&&(c.prompt=u),typeof i.customExecute=="function"&&(c.executeCode=i.customExecute.toString()),i.outputSchema)try{if(typeof i.outputSchema._def<"u"){let l=Wt(i.outputSchema,{target:"openApi3"});c.outputSchema={jsonSchema:l,variables:this._flattenJsonSchemaToVariables(l)}}else c.outputSchema={schema:i.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 o=[];for(let[r,i]of this.edges)if(typeof i=="string")o.push({source:r,target:i});else if(i.conditional){let a=this.conditionalCodeMap.get(r)||i.routes.toString(),c=this._inferConditionalTargets(i.routes),u=i.labels||{};for(let h of c){let l={source:r,target:h,data:{conditionalCode:a}};u[h]&&(l.label=u[h]),o.push(l)}}let s=null;if(this.stateSchema)try{s=Wt(this.stateSchema,{target:"openApi3"})}catch{s=this.stateSchema}return{nodes:t,edges:o,nodeConfigs:e,stateSchema:s}}_inferConditionalTargets(t){let e=t.toString(),o=new Set,s=/return\s+['"]([^'"]+)['"]/g,r;for(;(r=s.exec(e))!==null;)o.add(r[1]);return[...o]}_flattenJsonSchemaToVariables(t,e=""){let o=t;if(t.$ref&&t.definitions){let s=t.$ref.replace("#/definitions/","");o=t.definitions[s]||t}return this._flattenSchema(o,e)}_flattenSchema(t,e=""){if(!t||typeof t!="object")return[];let o=[],s=t.properties||{},r=t.required||[];for(let[i,a]of Object.entries(s)){let c=e?`${e}.${i}`:i;o.push({path:c,type:a.type||"unknown",label:a.description||this._formatLabel(i),optional:!r.includes(i)}),a.type==="object"&&a.properties&&o.push(...this._flattenSchema(a,c)),a.type==="array"&&a.items?.type==="object"&&a.items.properties&&o.push(...this._flattenSchema(a.items,`${c}[]`))}return o}_formatLabel(t){return t.replace(/([A-Z])/g," $1").replace(/^./,e=>e.toUpperCase()).trim()}_summarizeNodeOutput(t,e){if(!e||typeof e!="object")return[];let o=[];e.success!==void 0&&o.push(`Result: ${e.success?"passed":"failed"}`);for(let[s,r]of Object.entries(e))if(!(s==="success"||s==="raw"||s==="nextNode")){if(typeof r=="string"&&r.length<=80)o.push(`${s}: ${r}`);else if(Array.isArray(r)){let i=r.length,a=r.filter(u=>u?.passed===!0).length,c=r.some(u=>u?.passed!==void 0);o.push(c?`${s}: ${a}/${i} passed${i-a?`, ${i-a} failed`:""}`:`${s}: ${i} items`)}if(o.length>=4)break}return o}async run(t,e={}){if(!this.entryPoint)throw new Error("No entry point set for graph");let o=e.cwd||process.cwd();ae({path:A(o,".env")});let s=e.config||{};if(!s||Object.keys(s).length===0)try{let p=A(o,".zibby.config.js");at(p)&&(s=(await import(p)).default||{})}catch{}process.env.EXECUTION_ID&&!s.agent?.strictMode&&(s.agent={...s.agent,strictMode:!0});let r=e.agentType;if(!r){let p=s?.agent;p?.provider?r=p.provider:p?.gemini?r="gemini":p?.claude?r="claude":p?.cursor?r="cursor":p?.codex?r="codex":r=process.env.AGENT_TYPE||"cursor"}let i=e.contextConfig||t?.config?.contextConfig||t?.config?.context||s?.context||{};if(this.stateSchema){let p=this.stateSchema.safeParse(e);if(!p.success){let _=p.error.issues.map(O=>`${O.path.join(".")}: ${O.message}`);throw console.error("\u274C Initial state validation failed:"),_.forEach(O=>console.error(` - ${O}`)),new Error(`State validation failed: ${_.join(", ")}`)}y.step("State validated against schema")}let a=de(),c=e.sessionPath||a;c||pe();let{sessionPath:u,sessionTimestamp:h,sessionId:l}=me({cwd:o,config:s,traceFrom:"WorkflowGraph.run",initialState:{sessionPath:c,sessionTimestamp:e.sessionTimestamp}});y.step(`Session ${l}`);let S=await K.loadContext(e.specPath||"",o,i);Object.keys(S).length>0&&y.step(`Context loaded: ${Object.keys(S).join(", ")}`);let I=e.outputPath;!I&&e.specPath&&(t?.calculateOutputPath?I=t.calculateOutputPath(e.specPath):console.warn(`\u26A0\uFE0F outputPath not resolved (specPath=${e.specPath})`));let f=new U({...e,config:s,agentType:r,outputPath:I,sessionPath:u,sessionTimestamp:h,context:S,resolvedTools:this.resolvedToolsMap||{}}),v=new Map;try{await import("@zibby/skills")}catch{}let{getSkill:b}=await Promise.resolve().then(()=>(et(),bt)),P=new Set;for(let[,p]of this.nodes)for(let _ of p.config?.skills||[])P.add(_);for(let p of P){let _=b(p);if(typeof _?.middleware=="function")try{let O=await _.middleware();typeof O=="function"&&v.set(p,O)}catch{}}let d=this.entryPoint,m=[];for(;d&&d!=="END";){let p=A(u,Nt);if(at(p)){console.warn(`
37
- \u{1F6D1} Studio stop requested \u2014 ending workflow.`);try{ie(p)}catch{}if(t&&typeof t.cleanup=="function")try{await t.cleanup()}catch{}return y.step("Workflow stopped by Studio"),{success:!0,state:f.getAll(),executionLog:m,stoppedByStudio:!0}}let _=this.nodes.get(d);if(!_)throw new Error(`Node '${d}' not found in graph`);let O=JSON.stringify({sessionPath:u,sessionTimestamp:h,currentNode:d,createdAt:new Date().toISOString(),config:f.get("config")}),Dt=A(u,C);Bt(Dt,O,"utf-8");let ct=f.get("config")?.paths?.output||G,Ft=A(o,ct,C);Mt(A(o,ct),{recursive:!0});try{Bt(Ft,O,"utf-8")}catch{}let lt=e.onPipelineProgress;if(typeof lt=="function")try{lt({cwd:o,sessionPath:u,sessionId:l,outputBase:f.get("config")?.paths?.output||G,currentNode:d})}catch{}let jt=(this.resolvedToolsMap||{})[d]||null;f.set("_currentNodeTools",jt);let Ut=f.get("nodeConfigs")||{};f.set("_currentNodeConfig",Ut[d]||{}),y.nodeStart(d);let ut=Date.now(),L=this.nodePrompts.get(d);if(!this._invokeAgent){let N=await Promise.resolve().then(()=>(st(),ot));this._invokeAgent=N.invokeAgent}let dt=this._invokeAgent,pt={state:f,invokeAgent:async(N={},k={})=>{let E=k.prompt||"";if(L)try{E=ce.compile(L,{noEscape:!0})(N)}catch(T){throw console.error(`\u274C Template rendering failed for node '${d}':`,T.message),new Error(`Template rendering failed: ${T.message}`,{cause:T})}else if(!E)throw new Error(`No prompt template configured for node '${d}' and no prompt provided in options`);return dt(E,{state:f.getAll(),images:k.images||[]},{model:k.model||f.get("model"),workspace:f.get("workspace"),schema:k.schema,...k})},_coreInvokeAgent:dt,agent:t,nodeId:d,promptTemplate:L,getPromptTemplate:()=>L,...f.getAll()};try{let N=(_.config?.skills||[]).map(R=>v.get(R)).filter(Boolean),k=[...this.middleware,...N],E;k.length>0?E=await this._composeMiddleware(k,d,async()=>_.execute(pt,f),f.getAll(),f):E=await _.execute(pt,f);let T=Date.now()-ut;if(m.push({node:d,success:E.success,duration:T,timestamp:new Date().toISOString()}),!E.success){if(String(E.error||"").includes("Stopped from Zibby Studio")){if(y.step("Workflow stopped by Studio"),f.set("stoppedByStudio",!0),t&&typeof t.cleanup=="function")try{await t.cleanup()}catch{}return{success:!0,state:f.getAll(),executionLog:m,stoppedByStudio:!0}}f.append("errors",{node:d,error:E.error});let ft=_.config?.retries||0,ht=`${d}_retries`,F=f.getAll()[ht]||0;if(F<ft){y.stepInfo(`Retrying (attempt ${F+1}/${ft})`),f.update({[ht]:F+1,[`${d}_raw`]:E.raw});continue}throw y.nodeFailed(d,E.error,{duration:T}),new Error(`Node '${d}' failed after ${F} attempts: ${E.error}`)}f.update({[d]:E.output});let Zt=this._summarizeNodeOutput(d,E.output);y.nodeComplete(d,{duration:T,details:Zt});let D=this.edges.get(d);if(!D)d="END";else if(D.conditional){let R=D.routes(f.getAll());y.route(d,R),d=R}else d=D}catch(N){throw y.isInsideNode&&y.nodeFailed(d,N.message,{duration:Date.now()-ut}),f.set("failed",!0),f.set("failedAt",d),N}}y.graphComplete();let $={success:!0,state:f.getAll(),executionLog:m};return t&&typeof t.onComplete=="function"&&await t.onComplete($),$}};export{Rt as WorkflowGraph,pe as clearInheritedSessionEnvForFreshRun,he as generateWorkflowSessionId,de as readStudioPinnedSessionPathFromEnv,me as resolveWorkflowSession,ue as shouldTrustInheritedSessionEnv,fe as syncProcessEnvToSession};
36
+ ${h}`)}}function ue(){return process.env.ZIBBY_RUN_SOURCE==="studio"||process.env.ZIBBY_KEEP_SESSION_ENV==="1"||process.env.ZIBBY_KEEP_SESSION_ENV==="true"}function de(){if(process.env.ZIBBY_RUN_SOURCE!=="studio")return;let n=process.env.ZIBBY_SESSION_PATH;if(!(n==null||String(n).trim()===""))try{return Lt(String(n).trim())}catch{return String(n).trim()}}function pe(){ue()||(delete process.env.ZIBBY_SESSION_PATH,delete process.env.ZIBBY_SESSION_ID)}function fe({sessionPath:n,sessionId:t}){n&&typeof n=="string"&&(process.env.ZIBBY_SESSION_PATH=n),t!=null&&String(t).trim()!==""&&(process.env.ZIBBY_SESSION_ID=String(t).trim())}function he(n={}){let t=kt.map(r=>process.env[r]).find(Boolean),e=Math.random().toString(36).slice(2,6),o=t||`${Date.now()}_${e}`,s=n.paths?.sessionPrefix;return s?`${s}_${o}`:o}function me({cwd:n=process.cwd(),config:t={},initialState:e={},traceFrom:o="resolveWorkflowSession"}={}){let s=e.sessionPath,r=e.sessionTimestamp,i="initialState.sessionPath";if(!s&&process.env.ZIBBY_SESSION_PATH)try{let u=Lt(String(process.env.ZIBBY_SESSION_PATH));u&&(s=u,i="ZIBBY_SESSION_PATH")}catch{}let a;if(s)a=String(s).split(/[/\\]/).filter(Boolean).pop(),r==null&&(r=Date.now());else{let u=process.env.ZIBBY_SESSION_ID&&String(process.env.ZIBBY_SESSION_ID).trim();if(u)a=u,i="ZIBBY_SESSION_ID";else{let l=t.sessionId!=null?String(t.sessionId).trim():"";l&&l!=="last"?(a=l,i="config.sessionId"):(a=he(t),i="generated")}r=r??Date.now();let h=t.paths?.output||G;s=A(n,h,Ot,a)}let c=!at(s);return c&&Mt(s,{recursive:!0}),(c||i!=="initialState.sessionPath")&&le({traceFrom:o,sessionId:a,sessionPath:s,idSource:i,mkdirFresh:c}),fe({sessionPath:s,sessionId:a}),{sessionPath:s,sessionId:a,sessionTimestamp:r}}var Bt=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}setStateSchema(t){return this.stateSchema=t,this}getStateSchema(){return this.stateSchema}addNode(t,e,o={}){let s=e instanceof W?e:new W(e);return s.name=t,this.nodes.set(t,s),o.prompt&&this.nodePrompts.set(t,o.prompt),Object.keys(o).length>0&&this.nodeOptions.set(t,o),this}addConditionalNode(t,e){return this.nodes.set(t,new J({...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:o}={}){return this.edges.set(t,{conditional:!0,routes:e,labels:o}),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,o,s,r){let i=o;for(let a=t.length-1;a>=0;a--){let c=t[a],u=i;i=()=>c(e,u,s,r)}return i()}serialize(){let t=[],e={};for(let[r,i]of this.nodes){let a=this.nodeTypeMap.get(r)||r;t.push({id:r,type:a,data:{nodeType:a,label:r}});let c={};i._isCustomCode&&typeof i.execute=="function"&&(c.customCode=i.execute.toString());let u=this.nodePrompts.get(r);if(u&&(c.prompt=u),typeof i.customExecute=="function"&&(c.executeCode=i.customExecute.toString()),i.outputSchema)try{if(typeof i.outputSchema._def<"u"){let l=Wt(i.outputSchema,{target:"openApi3"});c.outputSchema={jsonSchema:l,variables:this._flattenJsonSchemaToVariables(l)}}else c.outputSchema={schema:i.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 o=[];for(let[r,i]of this.edges)if(typeof i=="string")o.push({source:r,target:i});else if(i.conditional){let a=this.conditionalCodeMap.get(r)||i.routes.toString(),c=this._inferConditionalTargets(i.routes),u=i.labels||{};for(let h of c){let l={source:r,target:h,data:{conditionalCode:a}};u[h]&&(l.label=u[h]),o.push(l)}}let s=null;if(this.stateSchema)try{s=Wt(this.stateSchema,{target:"openApi3"})}catch{s=this.stateSchema}return{nodes:t,edges:o,nodeConfigs:e,stateSchema:s}}_inferConditionalTargets(t){let e=t.toString(),o=new Set,s=/return\s+['"]([^'"]+)['"]/g,r;for(;(r=s.exec(e))!==null;)o.add(r[1]);return[...o]}_flattenJsonSchemaToVariables(t,e=""){let o=t;if(t.$ref&&t.definitions){let s=t.$ref.replace("#/definitions/","");o=t.definitions[s]||t}return this._flattenSchema(o,e)}_flattenSchema(t,e=""){if(!t||typeof t!="object")return[];let o=[],s=t.properties||{},r=t.required||[];for(let[i,a]of Object.entries(s)){let c=e?`${e}.${i}`:i;o.push({path:c,type:a.type||"unknown",label:a.description||this._formatLabel(i),optional:!r.includes(i)}),a.type==="object"&&a.properties&&o.push(...this._flattenSchema(a,c)),a.type==="array"&&a.items?.type==="object"&&a.items.properties&&o.push(...this._flattenSchema(a.items,`${c}[]`))}return o}_formatLabel(t){return t.replace(/([A-Z])/g," $1").replace(/^./,e=>e.toUpperCase()).trim()}_summarizeNodeOutput(t,e){if(!e||typeof e!="object")return[];let o=[];e.success!==void 0&&o.push(`Result: ${e.success?"passed":"failed"}`);for(let[s,r]of Object.entries(e))if(!(s==="success"||s==="raw"||s==="nextNode")){if(typeof r=="string"&&r.length<=80)o.push(`${s}: ${r}`);else if(Array.isArray(r)){let i=r.length,a=r.filter(u=>u?.passed===!0).length,c=r.some(u=>u?.passed!==void 0);o.push(c?`${s}: ${a}/${i} passed${i-a?`, ${i-a} failed`:""}`:`${s}: ${i} items`)}if(o.length>=4)break}return o}async run(t,e={}){if(!this.entryPoint)throw new Error("No entry point set for graph");let o=e.cwd||process.cwd();ae({path:A(o,".env")});let s=e.config||{};if(!s||Object.keys(s).length===0)try{let p=A(o,".zibby.config.js");at(p)&&(s=(await import(p)).default||{})}catch{}process.env.EXECUTION_ID&&!s.agent?.strictMode&&(s.agent={...s.agent,strictMode:!0});let r=e.agentType;if(!r){let p=s?.agent;p?.provider?r=p.provider:p?.gemini?r="gemini":p?.claude?r="claude":p?.cursor?r="cursor":p?.codex?r="codex":r=process.env.AGENT_TYPE||"cursor"}let i=e.contextConfig||t?.config?.contextConfig||t?.config?.context||s?.context||{};if(this.stateSchema){let p=this.stateSchema.safeParse(e);if(!p.success){let w=p.error.issues.map(O=>`${O.path.join(".")}: ${O.message}`);throw console.error("\u274C Initial state validation failed:"),w.forEach(O=>console.error(` - ${O}`)),new Error(`State validation failed: ${w.join(", ")}`)}y.step("State validated against schema")}let a=de(),c=e.sessionPath||a;c||pe();let{sessionPath:u,sessionTimestamp:h,sessionId:l}=me({cwd:o,config:s,traceFrom:"WorkflowGraph.run",initialState:{sessionPath:c,sessionTimestamp:e.sessionTimestamp}});y.step(`Session ${l}`);let S=await H.loadContext(e.specPath||"",o,i);Object.keys(S).length>0&&y.step(`Context loaded: ${Object.keys(S).join(", ")}`);let I=e.outputPath;!I&&e.specPath&&(t?.calculateOutputPath?I=t.calculateOutputPath(e.specPath):console.warn(`\u26A0\uFE0F outputPath not resolved (specPath=${e.specPath})`));let f=new U({...e,config:s,agentType:r,outputPath:I,sessionPath:u,sessionTimestamp:h,context:S,resolvedTools:this.resolvedToolsMap||{}}),b=new Map;try{await import("@zibby/skills")}catch{}let{getSkill:v}=await Promise.resolve().then(()=>(tt(),vt)),P=new Set;for(let[,p]of this.nodes)for(let w of p.config?.skills||[])P.add(w);for(let p of P){let w=v(p);if(typeof w?.middleware=="function")try{let O=await w.middleware();typeof O=="function"&&b.set(p,O)}catch{}}let d=this.entryPoint,m=[];for(;d&&d!=="END";){let p=A(u,Nt);if(at(p)){console.warn(`
37
+ \u{1F6D1} Studio stop requested \u2014 ending workflow.`);try{ie(p)}catch{}if(t&&typeof t.cleanup=="function")try{await t.cleanup()}catch{}return y.step("Workflow stopped by Studio"),{success:!0,state:f.getAll(),executionLog:m,stoppedByStudio:!0}}let w=this.nodes.get(d);if(!w)throw new Error(`Node '${d}' not found in graph`);let O=JSON.stringify({sessionPath:u,sessionTimestamp:h,currentNode:d,createdAt:new Date().toISOString(),config:f.get("config")}),Dt=A(u,C);Rt(Dt,O,"utf-8");let ct=f.get("config")?.paths?.output||G,Ft=A(o,ct,C);Mt(A(o,ct),{recursive:!0});try{Rt(Ft,O,"utf-8")}catch{}let lt=e.onPipelineProgress;if(typeof lt=="function")try{lt({cwd:o,sessionPath:u,sessionId:l,outputBase:f.get("config")?.paths?.output||G,currentNode:d})}catch{}let jt=(this.resolvedToolsMap||{})[d]||null;f.set("_currentNodeTools",jt);let Ut=f.get("nodeConfigs")||{};f.set("_currentNodeConfig",Ut[d]||{}),y.nodeStart(d);let ut=Date.now(),L=this.nodePrompts.get(d);if(!this._invokeAgent){let N=await Promise.resolve().then(()=>(st(),ot));this._invokeAgent=N.invokeAgent}let dt=this._invokeAgent,pt={state:f,invokeAgent:async(N={},k={})=>{let E=k.prompt||"";if(L)try{E=ce.compile(L,{noEscape:!0})(N)}catch(T){throw console.error(`\u274C Template rendering failed for node '${d}':`,T.message),new Error(`Template rendering failed: ${T.message}`,{cause:T})}else if(!E)throw new Error(`No prompt template configured for node '${d}' and no prompt provided in options`);return dt(E,{state:f.getAll(),images:k.images||[]},{model:k.model||f.get("model"),workspace:f.get("workspace"),schema:k.schema,...k})},_coreInvokeAgent:dt,agent:t,nodeId:d,promptTemplate:L,getPromptTemplate:()=>L,...f.getAll()};try{let N=(w.config?.skills||[]).map(B=>b.get(B)).filter(Boolean),k=[...this.middleware,...N],E;k.length>0?E=await this._composeMiddleware(k,d,async()=>w.execute(pt,f),f.getAll(),f):E=await w.execute(pt,f);let T=Date.now()-ut;if(m.push({node:d,success:E.success,duration:T,timestamp:new Date().toISOString()}),!E.success){if(String(E.error||"").includes("Stopped from Zibby Studio")){if(y.step("Workflow stopped by Studio"),f.set("stoppedByStudio",!0),t&&typeof t.cleanup=="function")try{await t.cleanup()}catch{}return{success:!0,state:f.getAll(),executionLog:m,stoppedByStudio:!0}}f.append("errors",{node:d,error:E.error});let ft=w.config?.retries||0,ht=`${d}_retries`,F=f.getAll()[ht]||0;if(F<ft){y.stepInfo(`Retrying (attempt ${F+1}/${ft})`),f.update({[ht]:F+1,[`${d}_raw`]:E.raw});continue}throw y.nodeFailed(d,E.error,{duration:T}),new Error(`Node '${d}' failed after ${F} attempts: ${E.error}`)}f.update({[d]:E.output});let Yt=this._summarizeNodeOutput(d,E.output);y.nodeComplete(d,{duration:T,details:Yt});let D=this.edges.get(d);if(!D)d="END";else if(D.conditional){let B=D.routes(f.getAll());y.route(d,B),d=B}else d=D}catch(N){throw y.isInsideNode&&y.nodeFailed(d,N.message,{duration:Date.now()-ut}),f.set("failed",!0),f.set("failedAt",d),N}}y.graphComplete();let $={success:!0,state:f.getAll(),executionLog:m};return t&&typeof t.onComplete=="function"&&await t.onComplete($),$}};export{Bt as WorkflowGraph,pe as clearInheritedSessionEnvForFreshRun,he as generateWorkflowSessionId,de as readStudioPinnedSessionPathFromEnv,me as resolveWorkflowSession,ue as shouldTrustInheritedSessionEnv,fe as syncProcessEnvToSession};
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- var yt=Object.defineProperty;var ae=(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 oe=(o,e)=>()=>(o&&(e=o(o=0)),e);var Ce=(o,e)=>{for(var t in e)yt(o,t,{get:e[t],enumerable:!0})};function Et(o){G.impl={...Pe,...o}}var be,Pe,G,_,B=oe(()=>{be=()=>{},Pe={debug:be,info:be,warn:(...o)=>console.warn("[workflow]",...o),error:(...o)=>console.error("[workflow]",...o)},G={impl:Pe};_={debug:(...o)=>G.impl.debug?.(...o),info:(...o)=>G.impl.info?.(...o),warn:(...o)=>G.impl.warn?.(...o),error:(...o)=>G.impl.error?.(...o)}});var J,he=oe(()=>{J=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 He={};Ce(He,{clearSkills:()=>Je,getAllSkills:()=>Ze,getSkill:()=>D,hasSkill:()=>Ge,listSkillIds:()=>Ye,registerSkill:()=>Ue});function Ue(o){if(!o||typeof o.id!="string")throw new Error("Skill definition must include a string id");L.set(o.id,Object.freeze({...o}))}function D(o){return L.get(o)||null}function Ge(o){return L.has(o)}function Ze(){return new Map(L)}function Ye(){return Array.from(L.keys())}function Je(){L.clear()}var L,H=oe(()=>{L=new Map});var V={};Ce(V,{getAgentStrategy:()=>me,invokeAgent:()=>ze,listStrategies:()=>Ke,registerStrategy:()=>Ve});function Ve(o){if(!(o instanceof J))throw new Error("strategy must be an instance of AgentStrategy");let e=P.findIndex(t=>t.getName()===o.getName());e>=0?P[e]=o:P.push(o)}function Ke(){return P.map(o=>o.getName())}function me(o={}){let{state:e={},preferredAgent:t=null}=o,s=t||e.agentType||process.env.AGENT_TYPE;if(!s){let n=P.map(r=>r.getName()).join(", ")||"none registered";throw new Error(`No agent specified. Set agentType in state or AGENT_TYPE env var. Available: ${n}`)}_.debug(`[workflow] agent selection: requested=${s}`);let i=P.find(n=>n.getName()===s);if(!i){let n=P.map(r=>r.getName()).join(", ")||"none registered";throw new Error(`Unknown agent '${s}'. Available: ${n}`)}if(!i.canHandle(o))throw new Error(`Agent '${s}' is not available in this environment. Check credentials/environment.`);return _.debug(`[workflow] using agent: ${i.getName()}`),i}async function ze(o,e={},t={}){let s=me(e),i=e.state?.config||t.config||{},n=i.models||{},r=t.nodeName&&n[t.nodeName]||null,a=n.default||null,c=i.agent?.[s.name]?.model||null,d=r||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:i},u=o,l=h.skills||[];if(l.length>0&&!t.skipPromptFragments){let p=l.map(S=>{let y=D(S)?.promptFragment;return typeof y=="function"?y():y}).filter(Boolean);p.length>0&&(u+=`
1
+ var _t=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 te=(o,e)=>()=>(o&&(e=o(o=0)),e);var be=(o,e)=>{for(var t in e)_t(o,t,{get:e[t],enumerable:!0})};function Nt(o){G.impl={...Re,...o}}var Pe,Re,G,_,L=te(()=>{Pe=()=>{},Re={debug:Pe,info:Pe,warn:(...o)=>console.warn("[workflow]",...o),error:(...o)=>console.error("[workflow]",...o)},G={impl:Re};_={debug:(...o)=>G.impl.debug?.(...o),info:(...o)=>G.impl.info?.(...o),warn:(...o)=>G.impl.warn?.(...o),error:(...o)=>G.impl.error?.(...o)}});var fe,he=te(()=>{fe=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 Ve={};be(Ve,{clearSkills:()=>He,getAllSkills:()=>Ye,getSkill:()=>F,hasSkill:()=>Ze,listSkillIds:()=>Je,registerSkill:()=>Ge});function Ge(o){if(!o||typeof o.id!="string")throw new Error("Skill definition must include a string id");D.set(o.id,Object.freeze({...o}))}function F(o){return D.get(o)||null}function Ze(o){return D.has(o)}function Ye(){return new Map(D)}function Je(){return Array.from(D.keys())}function He(){D.clear()}var D,J=te(()=>{D=new Map});var H={};be(H,{getAgentStrategy:()=>ge,invokeAgent:()=>qe,listStrategies:()=>ze,registerStrategy:()=>Ke});function Ke(o){if(!o||typeof o.getName!="function"||typeof o.invoke!="function")throw new Error("strategy must implement getName() and invoke() (AgentStrategy shape)");let e=P.findIndex(t=>t.getName()===o.getName());e>=0?P[e]=o:P.push(o)}function ze(){return P.map(o=>o.getName())}function ge(o={}){let{state:e={},preferredAgent:t=null}=o,s=t||e.agentType||process.env.AGENT_TYPE;if(!s){let n=P.map(r=>r.getName()).join(", ")||"none registered";throw new Error(`No agent specified. Set agentType in state or AGENT_TYPE env var. Available: ${n}`)}_.debug(`[workflow] agent selection: requested=${s}`);let i=P.find(n=>n.getName()===s);if(!i){let n=P.map(r=>r.getName()).join(", ")||"none registered";throw new Error(`Unknown agent '${s}'. Available: ${n}`)}if(!i.canHandle(o))throw new Error(`Agent '${s}' is not available in this environment. Check credentials/environment.`);return _.debug(`[workflow] using agent: ${i.getName()}`),i}async function qe(o,e={},t={}){let s=ge(e),i=e.state?.config||t.config||{},n=i.models||{},r=t.nodeName&&n[t.nodeName]||null,a=n.default||null,c=i.agent?.[s.name]?.model||null,d=r||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:i},u=o,l=h.skills||[];if(l.length>0&&!t.skipPromptFragments){let p=l.map(S=>{let y=F(S)?.promptFragment;return typeof y=="function"?y():y}).filter(Boolean);p.length>0&&(u+=`
2
2
 
3
3
  ${p.join(`
4
4
 
@@ -9,32 +9,32 @@ 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
  ${g}
12
- `),_.debug(`[workflow] prompt length: ${u.length} chars`),s.invoke(u,h)}var P,F=oe(()=>{he();B();H();P=[]});var _t=new Set(["__proto__","constructor","prototype"]);function ce(o){if(_t.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){ce(e),this._history.push({...this._state}),this._state[e]=t}update(e){let t=Object.getOwnPropertyNames(e);for(let s of t)ce(s);this._history.push({...this._state});for(let s of t)this._state[s]=e[s]}append(e,t){ce(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 U=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]*\}/);return s?this.validate(JSON.parse(s[0])):this.validate({result:e.trim()})}validate(e){let t=[];for(let[s,i]of Object.entries(this.schema)){if(i.required&&!(s in e)&&t.push(`Missing required field: ${s}`),s in e&&i.type){let n=typeof e[s];n!==i.type&&t.push(`Field '${s}' expected ${i.type}, got ${n}`)}if(i.validate&&s in e){let n=i.validate(e[s]);n&&t.push(`Field '${s}': ${n}`)}}if(t.length>0)throw new Error(`Output validation failed:
12
+ `),_.debug(`[workflow] prompt length: ${u.length} chars`),s.invoke(u,h)}var me,P,B=te(()=>{he();L();J();me=Symbol.for("@zibby/agent-workflow.strategies");globalThis[me]||(globalThis[me]=[]);P=globalThis[me]});var $t=new Set(["__proto__","constructor","prototype"]);function ae(o){if($t.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){ae(e),this._history.push({...this._state}),this._state[e]=t}update(e){let t=Object.getOwnPropertyNames(e);for(let s of t)ae(s);this._history.push({...this._state});for(let s of t)this._state[s]=e[s]}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 U=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]*\}/);return s?this.validate(JSON.parse(s[0])):this.validate({result:e.trim()})}validate(e){let t=[];for(let[s,i]of Object.entries(this.schema)){if(i.required&&!(s in e)&&t.push(`Missing required field: ${s}`),s in e&&i.type){let n=typeof e[s];n!==i.type&&t.push(`Field '${s}' expected ${i.type}, got ${n}`)}if(i.validate&&s in e){let n=i.validate(e[s]);n&&t.push(`Field '${s}': ${n}`)}}if(t.length>0)throw new Error(`Output validation failed:
13
13
  ${t.join(`
14
- `)}`);return e}},$t={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(", ")}`})};B();import{writeFileSync as ge,readFileSync as qe,existsSync as Xe,mkdirSync as xt}from"node:fs";import{join as Se,dirname as Ot}from"node:path";import E from"chalk";var je="__WORKFLOW_GRAPH_LOG__",Z=E.gray("\u2502"),It=E.gray("\u250C"),Re=E.gray("\u2514"),de=E.green("\u25C6"),We=E.hex("#c084fc")("\u25C6"),Be=E.hex("#2dd4bf")("\u25C6"),le=E.red("\u25C6"),Le=`${Z} `,De=2;function Fe(o){return o<1e3?`${o}ms`:`${(o/1e3).toFixed(1)}s`}function Me(o,e){return(t,s,i)=>{if(typeof t!="string")return o(t,s,i);let n=process.stdout.columns||120,r="";for(let a=0;a<t.length;a++){let c=t[a];e.lineStart&&(r+=Le,e.col=De,e.lineStart=!1),c===`
14
+ `)}`);return e}},Et={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(", ")}`})};L();import{writeFileSync as Se,readFileSync as Xe,existsSync as Qe,mkdirSync as Ot}from"node:fs";import{join as we,dirname as At}from"node:path";import E from"chalk";var Ue="__WORKFLOW_GRAPH_LOG__",Z=E.gray("\u2502"),It=E.gray("\u250C"),We=E.gray("\u2514"),ce=E.green("\u25C6"),Le=E.hex("#c084fc")("\u25C6"),De=E.hex("#2dd4bf")("\u25C6"),de=E.red("\u25C6"),Fe=`${Z} `,Be=2;function Me(o){return o<1e3?`${o}ms`:`${(o/1e3).toFixed(1)}s`}function je(o,e){return(t,s,i)=>{if(typeof t!="string")return o(t,s,i);let n=process.stdout.columns||120,r="";for(let a=0;a<t.length;a++){let c=t[a];e.lineStart&&(r+=Fe,e.col=Be,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>=n&&(r+=`
16
- ${Le}`,e.col=De))}return o(r,s,i)}}var se=class{constructor(){this._currentNode=null,this._origStdoutWrite=null,this._origStderrWrite=null;let e=String(process.env.ZIBBY_RUN_SOURCE||"").trim().toLowerCase(),t=String(process.env.ZIBBY_WORKFLOW_GRAPH_LOG_MARKERS||"").trim()==="1";this._emitWorkflowGraphMarkers=t||e==="studio"}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
+ ${Fe}`,e.col=Be))}return o(r,s,i)}}var oe=class{constructor(){this._currentNode=null,this._origStdoutWrite=null,this._origStderrWrite=null;let e=String(process.env.ZIBBY_RUN_SOURCE||"").trim().toLowerCase(),t=String(process.env.ZIBBY_WORKFLOW_GRAPH_LOG_MARKERS||"").trim()==="1";this._emitWorkflowGraphMarkers=t||e==="studio"}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=je(this._origStdoutWrite,e),process.stderr.write=je(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=`${je}${JSON.stringify(e)}
19
+ `)}_emitGraphLogMarker(e){if(!this._emitWorkflowGraphMarkers)return;let t=`${Ue}${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(de,e):process.stdout.write.bind(process.stdout)(`${Z} ${de} ${e}
24
- `)}stepInfo(e){this.step(e)}stepTool(e){this._origStdoutWrite?this._writeDot(We,e):process.stdout.write.bind(process.stdout)(`${Z} ${We} ${e}
25
- `)}stepMemory(e){let t=E.hex("#2dd4bf")(e);this._origStdoutWrite?this._writeDot(Be,t):process.stdout.write.bind(process.stdout)(`${Z} ${Be} ${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(`${It} ${e}`),this._startIntercepting()}nodeComplete(e,t={}){this._stopIntercepting();let{duration:s,details:i}=t;if(i)for(let r of i)this._rawWrite(`${de} ${r}`);let n=s?E.dim(` ${Fe(s)}`):"";this._rawWrite(`${Re} ${E.green("done")}${n}`),this._emitGraphLogMarker({phase:"node_end",node:e}),this._rawWrite("")}nodeFailed(e,t,s={}){this._stopIntercepting();let{duration:i}=s,n=i?E.dim(` ${Fe(i)}`):"";this._rawWrite(`${le} ${E.red(t)}`),this._rawWrite(`${Re} ${E.red("failed")}${n}`),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"))}},I=new se;var Y=".zibby/output",ue="sessions",b=".session-info.json",pe=".zibby-studio-stop",Nt="result.json",Tt="raw_stream_output.txt",vt="events.json",kt={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"},fe=["CI_JOB_ID","GITHUB_RUN_ID","CIRCLE_WORKFLOW_ID","BUILD_ID"];var R=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 U(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,i=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.errors,null,2)}`),{success:!1,error:u.message,raw:null}}}let n=typeof this.prompt=="function"?this.prompt(s()):this.prompt,r=i("_skillHints");r&&(n=`${r}
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(Le,e):process.stdout.write.bind(process.stdout)(`${Z} ${Le} ${e}
25
+ `)}stepMemory(e){let t=E.hex("#2dd4bf")(e);this._origStdoutWrite?this._writeDot(De,t):process.stdout.write.bind(process.stdout)(`${Z} ${De} ${t}
26
+ `)}stepFail(e){this._origStdoutWrite?this._writeDot(de,E.red(e)):process.stdout.write.bind(process.stdout)(`${Z} ${de} ${E.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:s,details:i}=t;if(i)for(let r of i)this._rawWrite(`${ce} ${r}`);let n=s?E.dim(` ${Me(s)}`):"";this._rawWrite(`${We} ${E.green("done")}${n}`),this._emitGraphLogMarker({phase:"node_end",node:e}),this._rawWrite("")}nodeFailed(e,t,s={}){this._stopIntercepting();let{duration:i}=s,n=i?E.dim(` ${Me(i)}`):"";this._rawWrite(`${de} ${E.red(t)}`),this._rawWrite(`${We} ${E.red("failed")}${n}`),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"))}},N=new oe;var Y=".zibby/output",le="sessions",b=".session-info.json",ue=".zibby-studio-stop",Tt="result.json",vt="raw_stream_output.txt",kt="events.json",xt={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"},pe=["CI_JOB_ID","GITHUB_RUN_ID","CIRCLE_WORKFLOW_ID","BUILD_ID"];var R=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 U(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,i=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.errors,null,2)}`),{success:!1,error:u.message,raw:null}}}let n=typeof this.prompt=="function"?this.prompt(s()):this.prompt,r=i("_skillHints");r&&(n=`${r}
28
28
 
29
- ${n}`);let a=s(),c=a.cwd||process.cwd(),d=a.sessionPath;try{if(d){let u=Se(d,b);if(Xe(u)){let g=JSON.parse(qe(u,"utf-8"));g.currentNode=this.name,ge(u,JSON.stringify(g,null,2),"utf-8")}let l=Se(d,"..",b);if(Xe(l))try{let g=JSON.parse(qe(l,"utf-8"));g.currentNode=this.name,ge(l,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 l=s().config||{},g=l.agents||{},p=this.config.agent??g[this.name]??null,S={state:s()};p&&(S.preferredAgent=p);let y={workspace:c,schema:this.isZodSchema?this.outputSchema:null,skills:this.config.skills||[],sessionPath:d,config:l,nodeName:this.name,timeout:this.config?.timeout||3e5},$=e?._coreInvokeAgent;$||($=(await Promise.resolve().then(()=>(F(),V))).invokeAgent);let f=await $(n,S,y),w,k;if(typeof f=="string"?(w=f,k=null):f.structured?(w=f.raw||JSON.stringify(f.structured,null,2),k=f.structured):(w=f.raw||JSON.stringify(f,null,2),k=f.extracted||null),d)try{let m=Se(d,this.name,"raw_stream_output.txt");xt(Ot(m),{recursive:!0}),ge(m,typeof w=="string"?w:JSON.stringify(w),"utf-8")}catch(m){_.debug(`[workflow] could not save raw output: ${m.message}`)}if(this.isZodSchema&&k){_.info(`[workflow] node '${this.name}': output validated: ${JSON.stringify(k,null,2)}`);let m=k;if(typeof this.onComplete=="function")try{m=await this.onComplete(s(),k)}catch(N){_.warn(`[workflow] onComplete hook failed: ${N.message}`)}return{success:!0,output:m,raw:w}}if(typeof this.onComplete=="function")try{return{success:!0,output:await this.onComplete(s(),{raw:w}),raw:w}}catch(m){throw new Error(`onComplete failed: ${m.message}`,{cause:m})}if(this.parser){let m=this.parser.parse(w);return _.info(`[workflow] node '${this.name}': parsed output: ${JSON.stringify(m,null,2)}`),I.step("Output parsed"),{success:!0,output:m,raw:w}}return{success:!0,output:w,raw:w}}catch(l){h=l,u<this.retries&&_.info(`[workflow] node '${this.name}' failed, retrying (${u+1}/${this.retries})\u2026`)}return{success:!1,error:h.message,raw:null}}},K=class extends R{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 Qe,readFileSync as At}from"node:fs";import{join as we,dirname as et}from"node:path";var z=class{static async loadContext(e,t,s={}){let i={},n=s.filenames||["CONTEXT.md","AGENTS.md"];if(e){let a=et(we(t,e));for(let c of n){let d=await this.findAndMergeContextFiles(c,a,t);if(d){let h=c.replace(/\.[^.]+$/,"").toLowerCase();i[h]=d}}}let r=s.discovery||{};for(let[a,c]of Object.entries(r))try{let d=we(t,c);Qe(d)&&(i[a]=await this.loadFile(d))}catch(d){console.warn(`[workflow] could not load context '${a}' from '${c}': ${d.message}`)}return i}static async findAndMergeContextFiles(e,t,s){let i=[],n=t;for(;n.startsWith(s);){let r=we(n,e);if(Qe(r))try{i.unshift(await this.loadFile(r))}catch(c){console.warn(`[workflow] could not load ${e} from ${r}: ${c.message}`)}let a=et(n);if(a===n)break;n=a}return i.length===0?null:i.every(r=>typeof r=="string")?i.join(`
29
+ ${n}`);let a=s(),c=a.cwd||process.cwd(),d=a.sessionPath;try{if(d){let u=we(d,b);if(Qe(u)){let g=JSON.parse(Xe(u,"utf-8"));g.currentNode=this.name,Se(u,JSON.stringify(g,null,2),"utf-8")}let l=we(d,"..",b);if(Qe(l))try{let g=JSON.parse(Xe(l,"utf-8"));g.currentNode=this.name,Se(l,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 l=s().config||{},g=l.agents||{},p=this.config.agent??g[this.name]??null,S={state:s()};p&&(S.preferredAgent=p);let y={workspace:c,schema:this.isZodSchema?this.outputSchema:null,skills:this.config.skills||[],sessionPath:d,config:l,nodeName:this.name,timeout:this.config?.timeout||3e5},$=e?._coreInvokeAgent;$||($=(await Promise.resolve().then(()=>(B(),H))).invokeAgent);let f=await $(n,S,y),w,k;if(typeof f=="string"?(w=f,k=null):f.structured?(w=f.raw||JSON.stringify(f.structured,null,2),k=f.structured):(w=f.raw||JSON.stringify(f,null,2),k=f.extracted||null),d)try{let m=we(d,this.name,"raw_stream_output.txt");Ot(At(m),{recursive:!0}),Se(m,typeof w=="string"?w:JSON.stringify(w),"utf-8")}catch(m){_.debug(`[workflow] could not save raw output: ${m.message}`)}if(this.isZodSchema&&k){_.info(`[workflow] node '${this.name}': output validated: ${JSON.stringify(k,null,2)}`);let m=k;if(typeof this.onComplete=="function")try{m=await this.onComplete(s(),k)}catch(I){_.warn(`[workflow] onComplete hook failed: ${I.message}`)}return{success:!0,output:m,raw:w}}if(typeof this.onComplete=="function")try{return{success:!0,output:await this.onComplete(s(),{raw:w}),raw:w}}catch(m){throw new Error(`onComplete failed: ${m.message}`,{cause:m})}if(this.parser){let m=this.parser.parse(w);return _.info(`[workflow] node '${this.name}': parsed output: ${JSON.stringify(m,null,2)}`),N.step("Output parsed"),{success:!0,output:m,raw:w}}return{success:!0,output:w,raw:w}}catch(l){h=l,u<this.retries&&_.info(`[workflow] node '${this.name}' failed, retrying (${u+1}/${this.retries})\u2026`)}return{success:!1,error:h.message,raw:null}}},V=class extends R{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 et,readFileSync as Ct}from"node:fs";import{join as ye,dirname as tt}from"node:path";var K=class{static async loadContext(e,t,s={}){let i={},n=s.filenames||["CONTEXT.md","AGENTS.md"];if(e){let a=tt(ye(t,e));for(let c of n){let d=await this.findAndMergeContextFiles(c,a,t);if(d){let h=c.replace(/\.[^.]+$/,"").toLowerCase();i[h]=d}}}let r=s.discovery||{};for(let[a,c]of Object.entries(r))try{let d=ye(t,c);et(d)&&(i[a]=await this.loadFile(d))}catch(d){console.warn(`[workflow] could not load context '${a}' from '${c}': ${d.message}`)}return i}static async findAndMergeContextFiles(e,t,s){let i=[],n=t;for(;n.startsWith(s);){let r=ye(n,e);if(et(r))try{i.unshift(await this.loadFile(r))}catch(c){console.warn(`[workflow] could not load ${e} from ${r}: ${c.message}`)}let a=tt(n);if(a===n)break;n=a}return i.length===0?null:i.every(r=>typeof r=="string")?i.join(`
30
30
 
31
31
  ---
32
32
 
33
- `):i.every(r=>typeof r=="object")?Object.assign({},...i):i[i.length-1]}static async loadFile(e){let t=At(e,"utf-8");if(e.endsWith(".json"))return JSON.parse(t);if(e.endsWith(".js")||e.endsWith(".mjs")){let{pathToFileURL:s}=await import("url"),i=await import(s(e).href);return i.default||i}return t}};import{mkdirSync as st,existsSync as ye,writeFileSync as tt,unlinkSync as Ct}from"node:fs";import{join as W,resolve as rt}from"node:path";import{config as bt}from"dotenv";import{zodToJsonSchema as ot}from"zod-to-json-schema";import Pt from"handlebars";function Rt({traceFrom:o,sessionId:e,sessionPath:t,idSource:s,mkdirFresh:i}){if(process.env.ZIBBY_SESSION_LOG==="0"||process.env.ZIBBY_SESSION_LOG==="false")return;let n=typeof process.ppid=="number"?process.ppid:"n/a",r=`[zibby:session] from=${o} pid=${process.pid} ppid=${n} sessionId=${e} source=${s} mkdir=${i?"yes":"no"} path=${t}`;if(console.log(r),(process.env.ZIBBY_TRACE_SESSION==="1"||process.env.ZIBBY_TRACE_SESSION==="true")&&process.env.ZIBBY_SESSION_LOG!=="0"&&process.env.ZIBBY_SESSION_LOG!=="false"){let d=(new Error("session trace").stack||"").split(`
33
+ `):i.every(r=>typeof r=="object")?Object.assign({},...i):i[i.length-1]}static async loadFile(e){let t=Ct(e,"utf-8");if(e.endsWith(".json"))return JSON.parse(t);if(e.endsWith(".js")||e.endsWith(".mjs")){let{pathToFileURL:s}=await import("url"),i=await import(s(e).href);return i.default||i}return t}};import{mkdirSync as rt,existsSync as _e,writeFileSync as ot,unlinkSync as bt}from"node:fs";import{join as W,resolve as nt}from"node:path";import{config as Pt}from"dotenv";import{zodToJsonSchema as st}from"zod-to-json-schema";import Rt from"handlebars";function Wt({traceFrom:o,sessionId:e,sessionPath:t,idSource:s,mkdirFresh:i}){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=${i?"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
- ${d}`)}}function nt(){return process.env.ZIBBY_RUN_SOURCE==="studio"||process.env.ZIBBY_KEEP_SESSION_ENV==="1"||process.env.ZIBBY_KEEP_SESSION_ENV==="true"}function it(){if(process.env.ZIBBY_RUN_SOURCE!=="studio")return;let o=process.env.ZIBBY_SESSION_PATH;if(!(o==null||String(o).trim()===""))try{return rt(String(o).trim())}catch{return String(o).trim()}}function at(){nt()||(delete process.env.ZIBBY_SESSION_PATH,delete process.env.ZIBBY_SESSION_ID)}function ct({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 dt(o={}){let e=fe.map(n=>process.env[n]).find(Boolean),t=Math.random().toString(36).slice(2,6),s=e||`${Date.now()}_${t}`,i=o.paths?.sessionPrefix;return i?`${i}_${s}`:s}function lt({cwd:o=process.cwd(),config:e={},initialState:t={},traceFrom:s="resolveWorkflowSession"}={}){let i=t.sessionPath,n=t.sessionTimestamp,r="initialState.sessionPath";if(!i&&process.env.ZIBBY_SESSION_PATH)try{let d=rt(String(process.env.ZIBBY_SESSION_PATH));d&&(i=d,r="ZIBBY_SESSION_PATH")}catch{}let a;if(i)a=String(i).split(/[/\\]/).filter(Boolean).pop(),n==null&&(n=Date.now());else{let d=process.env.ZIBBY_SESSION_ID&&String(process.env.ZIBBY_SESSION_ID).trim();if(d)a=d,r="ZIBBY_SESSION_ID";else{let u=e.sessionId!=null?String(e.sessionId).trim():"";u&&u!=="last"?(a=u,r="config.sessionId"):(a=dt(e),r="generated")}n=n??Date.now();let h=e.paths?.output||Y;i=W(o,h,ue,a)}let c=!ye(i);return c&&st(i,{recursive:!0}),(c||r!=="initialState.sessionPath")&&Rt({traceFrom:s,sessionId:a,sessionPath:i,idSource:r,mkdirFresh:c}),ct({sessionPath:i,sessionId:a}),{sessionPath:i,sessionId:a,sessionTimestamp:n}}var q=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}setStateSchema(e){return this.stateSchema=e,this}getStateSchema(){return this.stateSchema}addNode(e,t,s={}){let i=t instanceof R?t:new R(t);return i.name=e,this.nodes.set(e,i),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 K({...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,i,n){let r=s;for(let a=e.length-1;a>=0;a--){let c=e[a],d=r;r=()=>c(t,d,i,n)}return r()}serialize(){let e=[],t={};for(let[n,r]of this.nodes){let a=this.nodeTypeMap.get(n)||n;e.push({id:n,type:a,data:{nodeType:a,label:n}});let c={};r._isCustomCode&&typeof r.execute=="function"&&(c.customCode=r.execute.toString());let d=this.nodePrompts.get(n);if(d&&(c.prompt=d),typeof r.customExecute=="function"&&(c.executeCode=r.customExecute.toString()),r.outputSchema)try{if(typeof r.outputSchema._def<"u"){let u=ot(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 ${n}:`,u.message)}let h=(this.resolvedToolsMap||{})[n];h?.toolIds&&(c.tools=h.toolIds),Object.keys(c).length>0&&(t[n]=c)}let s=[];for(let[n,r]of this.edges)if(typeof r=="string")s.push({source:n,target:r});else if(r.conditional){let a=this.conditionalCodeMap.get(n)||r.routes.toString(),c=this._inferConditionalTargets(r.routes),d=r.labels||{};for(let h of c){let u={source:n,target:h,data:{conditionalCode:a}};d[h]&&(u.label=d[h]),s.push(u)}}let i=null;if(this.stateSchema)try{i=ot(this.stateSchema,{target:"openApi3"})}catch{i=this.stateSchema}return{nodes:e,edges:s,nodeConfigs:t,stateSchema:i}}_inferConditionalTargets(e){let t=e.toString(),s=new Set,i=/return\s+['"]([^'"]+)['"]/g,n;for(;(n=i.exec(t))!==null;)s.add(n[1]);return[...s]}_flattenJsonSchemaToVariables(e,t=""){let s=e;if(e.$ref&&e.definitions){let i=e.$ref.replace("#/definitions/","");s=e.definitions[i]||e}return this._flattenSchema(s,t)}_flattenSchema(e,t=""){if(!e||typeof e!="object")return[];let s=[],i=e.properties||{},n=e.required||[];for(let[r,a]of Object.entries(i)){let c=t?`${t}.${r}`:r;s.push({path:c,type:a.type||"unknown",label:a.description||this._formatLabel(r),optional:!n.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[i,n]of Object.entries(t))if(!(i==="success"||i==="raw"||i==="nextNode")){if(typeof n=="string"&&n.length<=80)s.push(`${i}: ${n}`);else if(Array.isArray(n)){let r=n.length,a=n.filter(d=>d?.passed===!0).length,c=n.some(d=>d?.passed!==void 0);s.push(c?`${i}: ${a}/${r} passed${r-a?`, ${r-a} failed`:""}`:`${i}: ${r} items`)}if(s.length>=4)break}return s}async run(e,t={}){if(!this.entryPoint)throw new Error("No entry point set for graph");let s=t.cwd||process.cwd();bt({path:W(s,".env")});let i=t.config||{};if(!i||Object.keys(i).length===0)try{let m=W(s,".zibby.config.js");ye(m)&&(i=(await import(m)).default||{})}catch{}process.env.EXECUTION_ID&&!i.agent?.strictMode&&(i.agent={...i.agent,strictMode:!0});let n=t.agentType;if(!n){let m=i?.agent;m?.provider?n=m.provider:m?.gemini?n="gemini":m?.claude?n="claude":m?.cursor?n="cursor":m?.codex?n="codex":n=process.env.AGENT_TYPE||"cursor"}let r=t.contextConfig||e?.config?.contextConfig||e?.config?.context||i?.context||{};if(this.stateSchema){let m=this.stateSchema.safeParse(t);if(!m.success){let N=m.error.issues.map(x=>`${x.path.join(".")}: ${x.message}`);throw console.error("\u274C Initial state validation failed:"),N.forEach(x=>console.error(` - ${x}`)),new Error(`State validation failed: ${N.join(", ")}`)}I.step("State validated against schema")}let a=it(),c=t.sessionPath||a;c||at();let{sessionPath:d,sessionTimestamp:h,sessionId:u}=lt({cwd:s,config:i,traceFrom:"WorkflowGraph.run",initialState:{sessionPath:c,sessionTimestamp:t.sessionTimestamp}});I.step(`Session ${u}`);let l=await z.loadContext(t.specPath||"",s,r);Object.keys(l).length>0&&I.step(`Context loaded: ${Object.keys(l).join(", ")}`);let g=t.outputPath;!g&&t.specPath&&(e?.calculateOutputPath?g=e.calculateOutputPath(t.specPath):console.warn(`\u26A0\uFE0F outputPath not resolved (specPath=${t.specPath})`));let p=new j({...t,config:i,agentType:n,outputPath:g,sessionPath:d,sessionTimestamp:h,context:l,resolvedTools:this.resolvedToolsMap||{}}),S=new Map;try{await import("@zibby/skills")}catch{}let{getSkill:y}=await Promise.resolve().then(()=>(H(),He)),$=new Set;for(let[,m]of this.nodes)for(let N of m.config?.skills||[])$.add(N);for(let m of $){let N=y(m);if(typeof N?.middleware=="function")try{let x=await N.middleware();typeof x=="function"&&S.set(m,x)}catch{}}let f=this.entryPoint,w=[];for(;f&&f!=="END";){let m=W(d,pe);if(ye(m)){console.warn(`
37
- \u{1F6D1} Studio stop requested \u2014 ending workflow.`);try{Ct(m)}catch{}if(e&&typeof e.cleanup=="function")try{await e.cleanup()}catch{}return I.step("Workflow stopped by Studio"),{success:!0,state:p.getAll(),executionLog:w,stoppedByStudio:!0}}let N=this.nodes.get(f);if(!N)throw new Error(`Node '${f}' not found in graph`);let x=JSON.stringify({sessionPath:d,sessionTimestamp:h,currentNode:f,createdAt:new Date().toISOString(),config:p.get("config")}),ht=W(d,b);tt(ht,x,"utf-8");let Ne=p.get("config")?.paths?.output||Y,mt=W(s,Ne,b);st(W(s,Ne),{recursive:!0});try{tt(mt,x,"utf-8")}catch{}let Te=t.onPipelineProgress;if(typeof Te=="function")try{Te({cwd:s,sessionPath:d,sessionId:u,outputBase:p.get("config")?.paths?.output||Y,currentNode:f})}catch{}let gt=(this.resolvedToolsMap||{})[f]||null;p.set("_currentNodeTools",gt);let St=p.get("nodeConfigs")||{};p.set("_currentNodeConfig",St[f]||{}),I.nodeStart(f);let ve=Date.now(),Q=this.nodePrompts.get(f);if(!this._invokeAgent){let O=await Promise.resolve().then(()=>(F(),V));this._invokeAgent=O.invokeAgent}let ke=this._invokeAgent,xe={state:p,invokeAgent:async(O={},A={})=>{let T=A.prompt||"";if(Q)try{T=Pt.compile(Q,{noEscape:!0})(O)}catch(C){throw console.error(`\u274C Template rendering failed for node '${f}':`,C.message),new Error(`Template rendering failed: ${C.message}`,{cause:C})}else if(!T)throw new Error(`No prompt template configured for node '${f}' and no prompt provided in options`);return ke(T,{state:p.getAll(),images:A.images||[]},{model:A.model||p.get("model"),workspace:p.get("workspace"),schema:A.schema,...A})},_coreInvokeAgent:ke,agent:e,nodeId:f,promptTemplate:Q,getPromptTemplate:()=>Q,...p.getAll()};try{let O=(N.config?.skills||[]).map(M=>S.get(M)).filter(Boolean),A=[...this.middleware,...O],T;A.length>0?T=await this._composeMiddleware(A,f,async()=>N.execute(xe,p),p.getAll(),p):T=await N.execute(xe,p);let C=Date.now()-ve;if(w.push({node:f,success:T.success,duration:C,timestamp:new Date().toISOString()}),!T.success){if(String(T.error||"").includes("Stopped from Zibby Studio")){if(I.step("Workflow stopped by Studio"),p.set("stoppedByStudio",!0),e&&typeof e.cleanup=="function")try{await e.cleanup()}catch{}return{success:!0,state:p.getAll(),executionLog:w,stoppedByStudio:!0}}p.append("errors",{node:f,error:T.error});let Oe=N.config?.retries||0,Ae=`${f}_retries`,te=p.getAll()[Ae]||0;if(te<Oe){I.stepInfo(`Retrying (attempt ${te+1}/${Oe})`),p.update({[Ae]:te+1,[`${f}_raw`]:T.raw});continue}throw I.nodeFailed(f,T.error,{duration:C}),new Error(`Node '${f}' failed after ${te} attempts: ${T.error}`)}p.update({[f]:T.output});let wt=this._summarizeNodeOutput(f,T.output);I.nodeComplete(f,{duration:C,details:wt});let ee=this.edges.get(f);if(!ee)f="END";else if(ee.conditional){let M=ee.routes(p.getAll());I.route(f,M),f=M}else f=ee}catch(O){throw I.isInsideNode&&I.nodeFailed(f,O.message,{duration:Date.now()-ve}),p.set("failed",!0),p.set("failedAt",f),O}}I.graphComplete();let k={success:!0,state:p.getAll(),executionLog:w};return e&&typeof e.onComplete=="function"&&await e.onComplete(k),k}};var X=new Map;function ut(o,e){X.set(o,e)}function _e(o){return X.get(o)}function re(o){return X.has(o)}function Wt(){return Array.from(X.keys())}function $e(o){let e=X.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}ut("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(()=>(F(),V))).invokeAgent);let i=e.extraPromptInstructions||"Execute the task based on the current state.",n=Bt(i,t),r=await s(n,{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 Bt(o,e){let t=/@([\w.]+)/g,s=new Set,i;for(;(i=t.exec(o))!==null;)s.add(i[1]);if(s.size===0)return o;let n=[],r=new Set;for(let a of s){let c=a.split(".")[0];if(r.has(c))continue;let d=a.split(".").reduce((l,g)=>l?.[g],e);if(d===void 0)continue;let h=typeof d=="string"?d:d?.raw??JSON.stringify(d,null,2),u=a.replace(/_/g," ").replace(/\b\w/g,l=>l.toUpperCase());n.push(`## ${u}
36
+ ${h}`)}}function it(){return process.env.ZIBBY_RUN_SOURCE==="studio"||process.env.ZIBBY_KEEP_SESSION_ENV==="1"||process.env.ZIBBY_KEEP_SESSION_ENV==="true"}function at(){if(process.env.ZIBBY_RUN_SOURCE!=="studio")return;let o=process.env.ZIBBY_SESSION_PATH;if(!(o==null||String(o).trim()===""))try{return nt(String(o).trim())}catch{return String(o).trim()}}function ct(){it()||(delete process.env.ZIBBY_SESSION_PATH,delete process.env.ZIBBY_SESSION_ID)}function dt({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 lt(o={}){let e=pe.map(n=>process.env[n]).find(Boolean),t=Math.random().toString(36).slice(2,6),s=e||`${Date.now()}_${t}`,i=o.paths?.sessionPrefix;return i?`${i}_${s}`:s}function ut({cwd:o=process.cwd(),config:e={},initialState:t={},traceFrom:s="resolveWorkflowSession"}={}){let i=t.sessionPath,n=t.sessionTimestamp,r="initialState.sessionPath";if(!i&&process.env.ZIBBY_SESSION_PATH)try{let d=nt(String(process.env.ZIBBY_SESSION_PATH));d&&(i=d,r="ZIBBY_SESSION_PATH")}catch{}let a;if(i)a=String(i).split(/[/\\]/).filter(Boolean).pop(),n==null&&(n=Date.now());else{let d=process.env.ZIBBY_SESSION_ID&&String(process.env.ZIBBY_SESSION_ID).trim();if(d)a=d,r="ZIBBY_SESSION_ID";else{let u=e.sessionId!=null?String(e.sessionId).trim():"";u&&u!=="last"?(a=u,r="config.sessionId"):(a=lt(e),r="generated")}n=n??Date.now();let h=e.paths?.output||Y;i=W(o,h,le,a)}let c=!_e(i);return c&&rt(i,{recursive:!0}),(c||r!=="initialState.sessionPath")&&Wt({traceFrom:s,sessionId:a,sessionPath:i,idSource:r,mkdirFresh:c}),dt({sessionPath:i,sessionId:a}),{sessionPath:i,sessionId:a,sessionTimestamp:n}}var z=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}setStateSchema(e){return this.stateSchema=e,this}getStateSchema(){return this.stateSchema}addNode(e,t,s={}){let i=t instanceof R?t:new R(t);return i.name=e,this.nodes.set(e,i),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 V({...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,i,n){let r=s;for(let a=e.length-1;a>=0;a--){let c=e[a],d=r;r=()=>c(t,d,i,n)}return r()}serialize(){let e=[],t={};for(let[n,r]of this.nodes){let a=this.nodeTypeMap.get(n)||n;e.push({id:n,type:a,data:{nodeType:a,label:n}});let c={};r._isCustomCode&&typeof r.execute=="function"&&(c.customCode=r.execute.toString());let d=this.nodePrompts.get(n);if(d&&(c.prompt=d),typeof r.customExecute=="function"&&(c.executeCode=r.customExecute.toString()),r.outputSchema)try{if(typeof r.outputSchema._def<"u"){let u=st(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 ${n}:`,u.message)}let h=(this.resolvedToolsMap||{})[n];h?.toolIds&&(c.tools=h.toolIds),Object.keys(c).length>0&&(t[n]=c)}let s=[];for(let[n,r]of this.edges)if(typeof r=="string")s.push({source:n,target:r});else if(r.conditional){let a=this.conditionalCodeMap.get(n)||r.routes.toString(),c=this._inferConditionalTargets(r.routes),d=r.labels||{};for(let h of c){let u={source:n,target:h,data:{conditionalCode:a}};d[h]&&(u.label=d[h]),s.push(u)}}let i=null;if(this.stateSchema)try{i=st(this.stateSchema,{target:"openApi3"})}catch{i=this.stateSchema}return{nodes:e,edges:s,nodeConfigs:t,stateSchema:i}}_inferConditionalTargets(e){let t=e.toString(),s=new Set,i=/return\s+['"]([^'"]+)['"]/g,n;for(;(n=i.exec(t))!==null;)s.add(n[1]);return[...s]}_flattenJsonSchemaToVariables(e,t=""){let s=e;if(e.$ref&&e.definitions){let i=e.$ref.replace("#/definitions/","");s=e.definitions[i]||e}return this._flattenSchema(s,t)}_flattenSchema(e,t=""){if(!e||typeof e!="object")return[];let s=[],i=e.properties||{},n=e.required||[];for(let[r,a]of Object.entries(i)){let c=t?`${t}.${r}`:r;s.push({path:c,type:a.type||"unknown",label:a.description||this._formatLabel(r),optional:!n.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[i,n]of Object.entries(t))if(!(i==="success"||i==="raw"||i==="nextNode")){if(typeof n=="string"&&n.length<=80)s.push(`${i}: ${n}`);else if(Array.isArray(n)){let r=n.length,a=n.filter(d=>d?.passed===!0).length,c=n.some(d=>d?.passed!==void 0);s.push(c?`${i}: ${a}/${r} passed${r-a?`, ${r-a} failed`:""}`:`${i}: ${r} items`)}if(s.length>=4)break}return s}async run(e,t={}){if(!this.entryPoint)throw new Error("No entry point set for graph");let s=t.cwd||process.cwd();Pt({path:W(s,".env")});let i=t.config||{};if(!i||Object.keys(i).length===0)try{let m=W(s,".zibby.config.js");_e(m)&&(i=(await import(m)).default||{})}catch{}process.env.EXECUTION_ID&&!i.agent?.strictMode&&(i.agent={...i.agent,strictMode:!0});let n=t.agentType;if(!n){let m=i?.agent;m?.provider?n=m.provider:m?.gemini?n="gemini":m?.claude?n="claude":m?.cursor?n="cursor":m?.codex?n="codex":n=process.env.AGENT_TYPE||"cursor"}let r=t.contextConfig||e?.config?.contextConfig||e?.config?.context||i?.context||{};if(this.stateSchema){let m=this.stateSchema.safeParse(t);if(!m.success){let I=m.error.issues.map(x=>`${x.path.join(".")}: ${x.message}`);throw console.error("\u274C Initial state validation failed:"),I.forEach(x=>console.error(` - ${x}`)),new Error(`State validation failed: ${I.join(", ")}`)}N.step("State validated against schema")}let a=at(),c=t.sessionPath||a;c||ct();let{sessionPath:d,sessionTimestamp:h,sessionId:u}=ut({cwd:s,config:i,traceFrom:"WorkflowGraph.run",initialState:{sessionPath:c,sessionTimestamp:t.sessionTimestamp}});N.step(`Session ${u}`);let l=await K.loadContext(t.specPath||"",s,r);Object.keys(l).length>0&&N.step(`Context loaded: ${Object.keys(l).join(", ")}`);let g=t.outputPath;!g&&t.specPath&&(e?.calculateOutputPath?g=e.calculateOutputPath(t.specPath):console.warn(`\u26A0\uFE0F outputPath not resolved (specPath=${t.specPath})`));let p=new j({...t,config:i,agentType:n,outputPath:g,sessionPath:d,sessionTimestamp:h,context:l,resolvedTools:this.resolvedToolsMap||{}}),S=new Map;try{await import("@zibby/skills")}catch{}let{getSkill:y}=await Promise.resolve().then(()=>(J(),Ve)),$=new Set;for(let[,m]of this.nodes)for(let I of m.config?.skills||[])$.add(I);for(let m of $){let I=y(m);if(typeof I?.middleware=="function")try{let x=await I.middleware();typeof x=="function"&&S.set(m,x)}catch{}}let f=this.entryPoint,w=[];for(;f&&f!=="END";){let m=W(d,ue);if(_e(m)){console.warn(`
37
+ \u{1F6D1} Studio stop requested \u2014 ending workflow.`);try{bt(m)}catch{}if(e&&typeof e.cleanup=="function")try{await e.cleanup()}catch{}return N.step("Workflow stopped by Studio"),{success:!0,state:p.getAll(),executionLog:w,stoppedByStudio:!0}}let I=this.nodes.get(f);if(!I)throw new Error(`Node '${f}' not found in graph`);let x=JSON.stringify({sessionPath:d,sessionTimestamp:h,currentNode:f,createdAt:new Date().toISOString(),config:p.get("config")}),mt=W(d,b);ot(mt,x,"utf-8");let Te=p.get("config")?.paths?.output||Y,gt=W(s,Te,b);rt(W(s,Te),{recursive:!0});try{ot(gt,x,"utf-8")}catch{}let ve=t.onPipelineProgress;if(typeof ve=="function")try{ve({cwd:s,sessionPath:d,sessionId:u,outputBase:p.get("config")?.paths?.output||Y,currentNode:f})}catch{}let St=(this.resolvedToolsMap||{})[f]||null;p.set("_currentNodeTools",St);let wt=p.get("nodeConfigs")||{};p.set("_currentNodeConfig",wt[f]||{}),N.nodeStart(f);let ke=Date.now(),X=this.nodePrompts.get(f);if(!this._invokeAgent){let O=await Promise.resolve().then(()=>(B(),H));this._invokeAgent=O.invokeAgent}let xe=this._invokeAgent,Oe={state:p,invokeAgent:async(O={},A={})=>{let T=A.prompt||"";if(X)try{T=Rt.compile(X,{noEscape:!0})(O)}catch(C){throw console.error(`\u274C Template rendering failed for node '${f}':`,C.message),new Error(`Template rendering failed: ${C.message}`,{cause:C})}else if(!T)throw new Error(`No prompt template configured for node '${f}' and no prompt provided in options`);return xe(T,{state:p.getAll(),images:A.images||[]},{model:A.model||p.get("model"),workspace:p.get("workspace"),schema:A.schema,...A})},_coreInvokeAgent:xe,agent:e,nodeId:f,promptTemplate:X,getPromptTemplate:()=>X,...p.getAll()};try{let O=(I.config?.skills||[]).map(M=>S.get(M)).filter(Boolean),A=[...this.middleware,...O],T;A.length>0?T=await this._composeMiddleware(A,f,async()=>I.execute(Oe,p),p.getAll(),p):T=await I.execute(Oe,p);let C=Date.now()-ke;if(w.push({node:f,success:T.success,duration:C,timestamp:new Date().toISOString()}),!T.success){if(String(T.error||"").includes("Stopped from Zibby Studio")){if(N.step("Workflow stopped by Studio"),p.set("stoppedByStudio",!0),e&&typeof e.cleanup=="function")try{await e.cleanup()}catch{}return{success:!0,state:p.getAll(),executionLog:w,stoppedByStudio:!0}}p.append("errors",{node:f,error:T.error});let Ae=I.config?.retries||0,Ce=`${f}_retries`,ee=p.getAll()[Ce]||0;if(ee<Ae){N.stepInfo(`Retrying (attempt ${ee+1}/${Ae})`),p.update({[Ce]:ee+1,[`${f}_raw`]:T.raw});continue}throw N.nodeFailed(f,T.error,{duration:C}),new Error(`Node '${f}' failed after ${ee} attempts: ${T.error}`)}p.update({[f]:T.output});let yt=this._summarizeNodeOutput(f,T.output);N.nodeComplete(f,{duration:C,details:yt});let Q=this.edges.get(f);if(!Q)f="END";else if(Q.conditional){let M=Q.routes(p.getAll());N.route(f,M),f=M}else f=Q}catch(O){throw N.isInsideNode&&N.nodeFailed(f,O.message,{duration:Date.now()-ke}),p.set("failed",!0),p.set("failedAt",f),O}}N.graphComplete();let k={success:!0,state:p.getAll(),executionLog:w};return e&&typeof e.onComplete=="function"&&await e.onComplete(k),k}};var q=new Map;function pt(o,e){q.set(o,e)}function $e(o){return q.get(o)}function se(o){return q.has(o)}function Lt(){return Array.from(q.keys())}function Ee(o){let e=q.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}pt("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(()=>(B(),H))).invokeAgent);let i=e.extraPromptInstructions||"Execute the task based on the current state.",n=Dt(i,t),r=await s(n,{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 Dt(o,e){let t=/@([\w.]+)/g,s=new Set,i;for(;(i=t.exec(o))!==null;)s.add(i[1]);if(s.size===0)return o;let n=[],r=new Set;for(let a of s){let c=a.split(".")[0];if(r.has(c))continue;let d=a.split(".").reduce((l,g)=>l?.[g],e);if(d===void 0)continue;let h=typeof d=="string"?d:d?.raw??JSON.stringify(d,null,2),u=a.replace(/_/g," ").replace(/\b\w/g,l=>l.toUpperCase());n.push(`## ${u}
38
38
  ${h}`),a.includes(".")||r.add(c)}return n.length===0?o:`${o}
39
39
 
40
40
  ---
@@ -42,13 +42,13 @@ ${h}`),a.includes(".")||r.add(c)}return n.length===0?o:`${o}
42
42
 
43
43
  ${n.join(`
44
44
 
45
- `)}`}H();B();var ne={};function Ie(o,e){if(Array.isArray(e))return Ee(e);let t=ne[o];return!t||t.length===0?null:Ee(t)}function Ee(o){if(!Array.isArray(o)||o.length===0)return null;let e=[],t={},s=[];for(let i of o){let n=D(i);if(!n){_.warn(`[workflow] unknown skill "${i}" \u2014 skipping`);continue}s.push(i);for(let r of n.tools||[])e.push({name:r.name,description:r.description,input_schema:r.input_schema||{type:"object",properties:{}}});if(!t[n.serverName])if(typeof n.resolve=="function"){let r=n.resolve();r&&(t[n.serverName]={...r,toolPrefix:i})}else{let r={};for(let a of n.envKeys||[]){let c=process.env[a];c&&(r[a]=c)}t[n.serverName]={command:n.command,args:[...n.args||[]],env:r,toolPrefix:i}}}return s.length===0?null:{toolIds:s,claudeTools:e,mcpServers:t}}B();function Lt(o,e={}){let{nodes:t,edges:s,nodeConfigs:i={}}=o;if(!Array.isArray(t)||t.length===0)throw new v("Graph must have at least one node");if(!Array.isArray(s))throw new v("Graph edges must be an array");let n=new q(e);e.stateSchema&&n.setStateSchema(e.stateSchema);let r=new Set,a=new Map,c={};for(let l of t){let g=ie(l);a.set(l.id,{...l,resolvedType:g}),g==="decision"&&r.add(l.id)}for(let[l,g]of a){if(r.has(l))continue;let p=g.resolvedType,S=i[l]||{},y=Ie(p,S.tools);y&&(c[l]=y);let $={};S.prompt&&($.prompt=S.prompt);let f=re(p);if(_.debug(`[workflow] compiler: node "${l}" type="${p}" registered=${f}`),S.customCode&&!f)n.addNode(l,pt(l,S.customCode,S),$),n.setNodeType(l,p);else if(f){let w=_e(p);w.factory?n.addNode(l,w.create(l,{...S,resolvedTools:y}),$):n.addNode(l,w,$),n.setNodeType(l,p)}else if(S.executeCode)n.addNode(l,pt(l,S.executeCode,S),$),n.setNodeType(l,p);else throw new v(`Unknown node type "${p}" for node "${l}". Did you forget to register it?`)}n.resolvedToolsMap=c;let d=new Set;for(let l of s)r.has(l.target)||d.add(l.target);let h=t.find(l=>!r.has(l.id)&&!d.has(l.id));if(!h)throw new v("Could not determine entry point: no node without incoming edges found");n.setEntryPoint(h.id);let u=Mt(s,"source");for(let l of s)if(!r.has(l.source))if(r.has(l.target)){let g=l.target,p=u.get(g)||[];if(p.length===0)throw new v(`Decision node "${g}" has no outgoing edges`);let S=jt(g,p,r);n.addConditionalEdges(l.source,S)}else n.addEdge(l.source,l.target);return n}function Dt(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=ie(a);if(c==="decision"||re(c))continue;let d=t[a.id]||{};d.customCode||d.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 i=new Set(o.nodes.filter(a=>ie(a)==="decision").map(a=>a.id)),n=new Set;for(let a of o.edges)i.has(a.target)||n.add(a.target);let r=o.nodes.filter(a=>!i.has(a.id)&&!n.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 i){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 Ft(o){return!o||!Array.isArray(o.nodes)?[]:o.nodes.filter(e=>ie(e)!=="decision").map(e=>e.id)}function ie(o){let e=o.data?.nodeType||o.data?.type||o.type;return e==="workflowNode"||e==="custom"||e==="default"?o.id:e}function Mt(o,e){let t=new Map;for(let s of o){let i=s[e];t.has(i)||t.set(i,[]),t.get(i).push(s)}return t}function jt(o,e,t){let s=e.find(a=>a.data?.conditionalCode||a.conditionalCode);if(!s)throw new v(`Decision node "${o}" has no conditionalCode on its outgoing edges`);let i=s.data?.conditionalCode||s.conditionalCode,n=new Set(e.map(a=>a.target).filter(a=>!t.has(a))),r;try{let c=new Function(`return (${i})`)();r=d=>{let h=c(d);return n.has(h)||_.warn(`[workflow] conditional route from "${o}" returned "${h}" which is not in valid targets: ${[...n].join(", ")}`),h}}catch(a){throw new v(`Failed to compile conditionalCode for "${o}": ${a.message}`)}return r}function pt(o,e,t={}){let s;try{s=new Function("invokeAgent","require","console",`return (${e})`)}catch(r){throw new v(`Failed to compile customCode for node "${o}": ${r.message}`)}let i=s(async(...r)=>{let{invokeAgent:a}=await Promise.resolve().then(()=>(F(),V));return a(...r)},typeof ae<"u"?ae:void 0,console),n=null;return t.outputSchema&&(n=t.outputSchema.jsonSchema||t.outputSchema),{name:o,_isCustomCode:!0,outputSchema:n,execute:async r=>{try{let a=await i(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 v=class extends Error{constructor(e){super(e),this.name="CompilationError"}};H();he();F();function Ut(o,e={}){let{nodes:t,edges:s,nodeConfigs:i={}}=o,n=new Set,r=[],a=new Map;for(let y of t){let $=y.data?.nodeType||y.type;a.set(y.id,$),$==="decision"?n.add(y.id):r.push({id:y.id,nodeType:$,label:y.data?.label||y.id})}let c=r.some(y=>{let $=i[y.id]||{};return!$.customCode&&!$.executeCode}),{toolsPerNode:d,toolIdsByVar:h}=zt(r,i),{simpleEdges:u,conditionalEdges:l}=qt(s,n),g=Xt(r,s,n),p=[],S=e.workflowType||"workflow";return p.push(Zt(e)),p.push(Yt(S,{usesRegisteredNodes:c})),p.push(Jt(h)),p.push(Ht(S)),p.push(Vt(r,i)),p.push(Kt(r,g,u,l,d,S)),p.filter(Boolean).join(`
46
- `)}function Gt(o){let e={};for(let[t,s]of Object.entries(o)){let{tools:i,...n}=s;Object.keys(n).length>0&&(e[t]=n)}return e}function Zt(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 Yt(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 Jt(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 Ht(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 Vt(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 i=ft(s.id),n=e[s.id]?.customCode;if(n)t.push(`// @custom \u2014 modified from default "${s.nodeType}" template`),t.push(`const ${i}_execute = ${n};`);else{let r=$e(s.nodeType);r?(t.push(`// Default "${s.nodeType}" implementation`),t.push(`const ${i}_execute = ${r};`)):(t.push(`// No template for "${s.nodeType}" \u2014 passthrough`),t.push(`const ${i}_execute = async (state) => ({ success: true, output: {}, raw: null });`))}t.push("")}return t.join(`
51
- `)}function Kt(o,e,t,s,i,n){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 d=ft(c.id);r.push(` graph.addNode('${c.id}', { name: '${c.id}', execute: ${d}_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 d=c.code.split(`
45
+ `)}`}J();L();var re={};function Ie(o,e){if(Array.isArray(e))return Ne(e);let t=re[o];return!t||t.length===0?null:Ne(t)}function Ne(o){if(!Array.isArray(o)||o.length===0)return null;let e=[],t={},s=[];for(let i of o){let n=F(i);if(!n){_.warn(`[workflow] unknown skill "${i}" \u2014 skipping`);continue}s.push(i);for(let r of n.tools||[])e.push({name:r.name,description:r.description,input_schema:r.input_schema||{type:"object",properties:{}}});if(!t[n.serverName])if(typeof n.resolve=="function"){let r=n.resolve();r&&(t[n.serverName]={...r,toolPrefix:i})}else{let r={};for(let a of n.envKeys||[]){let c=process.env[a];c&&(r[a]=c)}t[n.serverName]={command:n.command,args:[...n.args||[]],env:r,toolPrefix:i}}}return s.length===0?null:{toolIds:s,claudeTools:e,mcpServers:t}}L();function Ft(o,e={}){let{nodes:t,edges:s,nodeConfigs:i={}}=o;if(!Array.isArray(t)||t.length===0)throw new v("Graph must have at least one node");if(!Array.isArray(s))throw new v("Graph edges must be an array");let n=new z(e);e.stateSchema&&n.setStateSchema(e.stateSchema);let r=new Set,a=new Map,c={};for(let l of t){let g=ne(l);a.set(l.id,{...l,resolvedType:g}),g==="decision"&&r.add(l.id)}for(let[l,g]of a){if(r.has(l))continue;let p=g.resolvedType,S=i[l]||{},y=Ie(p,S.tools);y&&(c[l]=y);let $={};S.prompt&&($.prompt=S.prompt);let f=se(p);if(_.debug(`[workflow] compiler: node "${l}" type="${p}" registered=${f}`),S.customCode&&!f)n.addNode(l,ft(l,S.customCode,S),$),n.setNodeType(l,p);else if(f){let w=$e(p);w.factory?n.addNode(l,w.create(l,{...S,resolvedTools:y}),$):n.addNode(l,w,$),n.setNodeType(l,p)}else if(S.executeCode)n.addNode(l,ft(l,S.executeCode,S),$),n.setNodeType(l,p);else throw new v(`Unknown node type "${p}" for node "${l}". Did you forget to register it?`)}n.resolvedToolsMap=c;let d=new Set;for(let l of s)r.has(l.target)||d.add(l.target);let h=t.find(l=>!r.has(l.id)&&!d.has(l.id));if(!h)throw new v("Could not determine entry point: no node without incoming edges found");n.setEntryPoint(h.id);let u=jt(s,"source");for(let l of s)if(!r.has(l.source))if(r.has(l.target)){let g=l.target,p=u.get(g)||[];if(p.length===0)throw new v(`Decision node "${g}" has no outgoing edges`);let S=Ut(g,p,r);n.addConditionalEdges(l.source,S)}else n.addEdge(l.source,l.target);return n}function Bt(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=ne(a);if(c==="decision"||se(c))continue;let d=t[a.id]||{};d.customCode||d.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 i=new Set(o.nodes.filter(a=>ne(a)==="decision").map(a=>a.id)),n=new Set;for(let a of o.edges)i.has(a.target)||n.add(a.target);let r=o.nodes.filter(a=>!i.has(a.id)&&!n.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 i){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 Mt(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 jt(o,e){let t=new Map;for(let s of o){let i=s[e];t.has(i)||t.set(i,[]),t.get(i).push(s)}return t}function Ut(o,e,t){let s=e.find(a=>a.data?.conditionalCode||a.conditionalCode);if(!s)throw new v(`Decision node "${o}" has no conditionalCode on its outgoing edges`);let i=s.data?.conditionalCode||s.conditionalCode,n=new Set(e.map(a=>a.target).filter(a=>!t.has(a))),r;try{let c=new Function(`return (${i})`)();r=d=>{let h=c(d);return n.has(h)||_.warn(`[workflow] conditional route from "${o}" returned "${h}" which is not in valid targets: ${[...n].join(", ")}`),h}}catch(a){throw new v(`Failed to compile conditionalCode for "${o}": ${a.message}`)}return r}function ft(o,e,t={}){let s;try{s=new Function("invokeAgent","require","console",`return (${e})`)}catch(r){throw new v(`Failed to compile customCode for node "${o}": ${r.message}`)}let i=s(async(...r)=>{let{invokeAgent:a}=await Promise.resolve().then(()=>(B(),H));return a(...r)},typeof ie<"u"?ie:void 0,console),n=null;return t.outputSchema&&(n=t.outputSchema.jsonSchema||t.outputSchema),{name:o,_isCustomCode:!0,outputSchema:n,execute:async r=>{try{let a=await i(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 v=class extends Error{constructor(e){super(e),this.name="CompilationError"}};J();he();B();function Gt(o,e={}){let{nodes:t,edges:s,nodeConfigs:i={}}=o,n=new Set,r=[],a=new Map;for(let y of t){let $=y.data?.nodeType||y.type;a.set(y.id,$),$==="decision"?n.add(y.id):r.push({id:y.id,nodeType:$,label:y.data?.label||y.id})}let c=r.some(y=>{let $=i[y.id]||{};return!$.customCode&&!$.executeCode}),{toolsPerNode:d,toolIdsByVar:h}=qt(r,i),{simpleEdges:u,conditionalEdges:l}=Xt(s,n),g=Qt(r,s,n),p=[],S=e.workflowType||"workflow";return p.push(Yt(e)),p.push(Jt(S,{usesRegisteredNodes:c})),p.push(Ht(h)),p.push(Vt(S)),p.push(Kt(r,i)),p.push(zt(r,g,u,l,d,S)),p.filter(Boolean).join(`
46
+ `)}function Zt(o){let e={};for(let[t,s]of Object.entries(o)){let{tools:i,...n}=s;Object.keys(n).length>0&&(e[t]=n)}return e}function Yt(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 Jt(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 Ht(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 Vt(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 Kt(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 i=ht(s.id),n=e[s.id]?.customCode;if(n)t.push(`// @custom \u2014 modified from default "${s.nodeType}" template`),t.push(`const ${i}_execute = ${n};`);else{let r=Ee(s.nodeType);r?(t.push(`// Default "${s.nodeType}" implementation`),t.push(`const ${i}_execute = ${r};`)):(t.push(`// No template for "${s.nodeType}" \u2014 passthrough`),t.push(`const ${i}_execute = async (state) => ({ success: true, output: {}, raw: null });`))}t.push("")}return t.join(`
51
+ `)}function zt(o,e,t,s,i,n){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 d=ht(c.id);r.push(` graph.addNode('${c.id}', { name: '${c.id}', execute: ${d}_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 d=c.code.split(`
52
52
  `).map((h,u)=>u===0?h:` ${h}`).join(`
53
53
  `);r.push(` graph.addConditionalEdges('${c.source}', ${d});`)}let a=[];for(let c of o){let d=i.get(c.id);d&&a.push(` '${c.id}': ${d},`)}return a.length>0&&r.push(""," graph.resolvedToolsMap = {",...a," };"),r.push(""," return graph;","}",""),r.push("export { nodeConfigs };",""),r.join(`
54
- `)}function zt(o,e){let t=new Map,s=new Map;for(let i of o){let n=e[i.id]?.tools,r;if(Array.isArray(n)&&n.length>0)r=[...n].sort();else{let a=ne[i.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(i.id,a),s.has(a)||s.set(a,r)}}return{toolsPerNode:t,toolIdsByVar:s}}function qt(o,e){let t=[],s=[],i=new Map,n=new Set;for(let r of o)i.has(r.source)||i.set(r.source,[]),i.get(r.source).push(r);for(let r of o)if(!e.has(r.source))if(e.has(r.target)){if(n.has(r.target))continue;n.add(r.target);let c=(i.get(r.target)||[]).find(d=>d.data?.conditionalCode||d.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 Xt(o,e,t){let s=new Set;for(let n of e)t.has(n.target)||s.add(n.target);let i=o.find(n=>!s.has(n.id));return i?i.id:o[0]?.id}function ft(o){return o.replace(/[^a-zA-Z0-9]/g,"_")}B();export{J as AgentStrategy,fe as CI_ENV_VARS,v as CompilationError,K as ConditionalNode,z as ContextLoader,Y as DEFAULT_OUTPUT_BASE,vt as EVENTS_FILE,ne as NODE_DEFAULT_TOOLS,R as Node,U as OutputParser,Tt as RAW_OUTPUT_FILE,Nt as RESULT_FILE,ue as SESSIONS_DIR,b as SESSION_INFO_FILE,kt as SKILLS,pe as STUDIO_STOP_REQUEST_FILE,$t as SchemaTypes,se as Timeline,je as WORKFLOW_GRAPH_LOG_MARKER_PREFIX,q as WorkflowGraph,j as WorkflowState,at as clearInheritedSessionEnvForFreshRun,Je as clearSkills,Lt as compileGraph,Ft as extractSteps,Gt as generateNodeConfigsJson,Ut as generateWorkflowCode,dt as generateWorkflowSessionId,me as getAgentStrategy,Ze as getAllSkills,_e as getNodeImpl,$e as getNodeTemplate,Ee as getResolvedToolDefinitions,D as getSkill,re as hasNode,Ge as hasSkill,ze as invokeAgent,Wt as listNodeTypes,Ye as listSkillIds,Ke as listStrategies,it as readStudioPinnedSessionPathFromEnv,ut as registerNode,Ue as registerSkill,Ve as registerStrategy,Ie as resolveNodeTools,lt as resolveWorkflowSession,Et as setLogger,nt as shouldTrustInheritedSessionEnv,ct as syncProcessEnvToSession,I as timeline,Dt as validateGraphConfig};
54
+ `)}function qt(o,e){let t=new Map,s=new Map;for(let i of o){let n=e[i.id]?.tools,r;if(Array.isArray(n)&&n.length>0)r=[...n].sort();else{let a=re[i.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(i.id,a),s.has(a)||s.set(a,r)}}return{toolsPerNode:t,toolIdsByVar:s}}function Xt(o,e){let t=[],s=[],i=new Map,n=new Set;for(let r of o)i.has(r.source)||i.set(r.source,[]),i.get(r.source).push(r);for(let r of o)if(!e.has(r.source))if(e.has(r.target)){if(n.has(r.target))continue;n.add(r.target);let c=(i.get(r.target)||[]).find(d=>d.data?.conditionalCode||d.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 Qt(o,e,t){let s=new Set;for(let n of e)t.has(n.target)||s.add(n.target);let i=o.find(n=>!s.has(n.id));return i?i.id:o[0]?.id}function ht(o){return o.replace(/[^a-zA-Z0-9]/g,"_")}L();export{fe as AgentStrategy,pe as CI_ENV_VARS,v as CompilationError,V as ConditionalNode,K as ContextLoader,Y as DEFAULT_OUTPUT_BASE,kt as EVENTS_FILE,re as NODE_DEFAULT_TOOLS,R as Node,U as OutputParser,vt as RAW_OUTPUT_FILE,Tt as RESULT_FILE,le as SESSIONS_DIR,b as SESSION_INFO_FILE,xt as SKILLS,ue as STUDIO_STOP_REQUEST_FILE,Et as SchemaTypes,oe as Timeline,Ue as WORKFLOW_GRAPH_LOG_MARKER_PREFIX,z as WorkflowGraph,j as WorkflowState,ct as clearInheritedSessionEnvForFreshRun,He as clearSkills,Ft as compileGraph,Mt as extractSteps,Zt as generateNodeConfigsJson,Gt as generateWorkflowCode,lt as generateWorkflowSessionId,ge as getAgentStrategy,Ye as getAllSkills,$e as getNodeImpl,Ee as getNodeTemplate,Ne as getResolvedToolDefinitions,F as getSkill,se as hasNode,Ze as hasSkill,qe as invokeAgent,Lt as listNodeTypes,Je as listSkillIds,ze as listStrategies,at as readStudioPinnedSessionPathFromEnv,pt as registerNode,Ge as registerSkill,Ke as registerStrategy,Ie as resolveNodeTools,ut as resolveWorkflowSession,Nt as setLogger,it as shouldTrustInheritedSessionEnv,dt as syncProcessEnvToSession,N as timeline,Bt as validateGraphConfig};
@@ -1,6 +1,6 @@
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 h,E=w(()=>{h=class{constructor(t,n,r=0){this.name=t,this.description=n,this.priority=r}async invoke(t,n={}){throw new Error(`${this.constructor.name}.invoke() must be implemented`)}canHandle(t){throw new Error(`${this.constructor.name}.canHandle() must be implemented`)}getName(){return this.name}getDescription(){return this.description}getPriority(){return this.priority}}});var S,P,k,y,v=w(()=>{S=()=>{},P={debug:S,info:S,warn:(...e)=>console.warn("[workflow]",...e),error:(...e)=>console.error("[workflow]",...e)},k={impl:P},y={debug:(...e)=>k.impl.debug?.(...e),info:(...e)=>k.impl.info?.(...e),warn:(...e)=>k.impl.warn?.(...e),error:(...e)=>k.impl.error?.(...e)}});function A(e){return R.get(e)||null}var R,b=w(()=>{R=new Map});var T={};C(T,{getAgentStrategy:()=>_,invokeAgent:()=>L,listStrategies:()=>j,registerStrategy:()=>M});function M(e){if(!(e instanceof h))throw new Error("strategy must be an instance of AgentStrategy");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}`)}y.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 y.debug(`[workflow] using agent: ${i.getName()}`),i}async function L(e,t={},n={}){let r=_(t),i=t.state?.config||n.config||{},o=i.models||{},s=n.nodeName&&o[n.nodeName]||null,a=o.default||null,p=i.agent?.[r.name]?.model||null,l=s||a||p||n.model||null,m={...n,model:l,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=m.skills||[];if(g.length>0&&!n.skipPromptFragments){let N=g.map(I=>{let x=A(I)?.promptFragment;return typeof x=="function"?x():x}).filter(Boolean);N.length>0&&(u+=`
1
+ var R=Object.defineProperty;var w=(e,t)=>()=>(e&&(t=e(e=0)),t);var O=(e,t)=>{for(var n in t)R(e,n,{get:t[n],enumerable:!0})};var E=w(()=>{});var S,C,h,k,b=w(()=>{S=()=>{},C={debug:S,info:S,warn:(...e)=>console.warn("[workflow]",...e),error:(...e)=>console.error("[workflow]",...e)},h={impl:C},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 P.get(e)||null}var P,T=w(()=>{P=new Map});var _={};O(_,{getAgentStrategy:()=>A,invokeAgent:()=>L,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 A(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 L(e,t={},n={}){let r=A(t),i=t.state?.config||n.config||{},o=i.models||{},s=n.nodeName&&o[n.nodeName]||null,l=o.default||null,p=i.agent?.[r.name]?.model||null,a=s||l||p||n.model||null,m={...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=m.skills||[];if(g.length>0&&!n.skipPromptFragments){let x=g.map($=>{let y=v($)?.promptFragment;return typeof y=="function"?y():y}).filter(Boolean);x.length>0&&(u+=`
2
2
 
3
- ${N.join(`
3
+ ${x.join(`
4
4
 
5
5
  `)}`)}let d=t.state?._currentNodeConfig?.extraPromptInstructions?.trim();return d&&(u+=`
6
6
 
@@ -9,12 +9,12 @@ 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
  ${d}
12
- `),y.debug(`[workflow] prompt length: ${u.length} chars`),r.invoke(u,m)}var c,$=w(()=>{E();v();b();c=[]});var f=new Map;function H(e,t){f.set(e,t)}function B(e){return f.get(e)}function J(e){return f.has(e)}function K(){return Array.from(f.keys())}function W(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}H("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(()=>($(),T))).invokeAgent);let i=t.extraPromptInstructions||"Execute the task based on the current state.",o=U(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 U(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 a of r){let p=a.split(".")[0];if(s.has(p))continue;let l=a.split(".").reduce((g,d)=>g?.[d],t);if(l===void 0)continue;let m=typeof l=="string"?l:l?.raw??JSON.stringify(l,null,2),u=a.replace(/_/g," ").replace(/\b\w/g,g=>g.toUpperCase());o.push(`## ${u}
13
- ${m}`),a.includes(".")||s.add(p)}return o.length===0?e:`${e}
12
+ `),k.debug(`[workflow] prompt length: ${u.length} chars`),r.invoke(u,m)}var N,c,I=w(()=>{E();b();T();N=Symbol.for("@zibby/agent-workflow.strategies");globalThis[N]||(globalThis[N]=[]);c=globalThis[N]});var f=new Map;function Y(e,t){f.set(e,t)}function B(e){return f.get(e)}function J(e){return f.has(e)}function W(){return Array.from(f.keys())}function Q(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}Y("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=G(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 G(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 p=l.split(".")[0];if(s.has(p))continue;let a=l.split(".").reduce((g,d)=>g?.[d],t);if(a===void 0)continue;let m=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}
13
+ ${m}`),l.includes(".")||s.add(p)}return o.length===0?e:`${e}
14
14
 
15
15
  ---
16
16
  # Referenced Context
17
17
 
18
18
  ${o.join(`
19
19
 
20
- `)}`}export{B as getNodeImpl,W as getNodeTemplate,J as hasNode,K as listNodeTypes,H as registerNode};
20
+ `)}`}export{B as getNodeImpl,Q as getNodeTemplate,J as hasNode,W as listNodeTypes,Y as registerNode};
package/dist/node.js CHANGED
@@ -1,4 +1,4 @@
1
- var ot=Object.defineProperty;var k=(r,t)=>()=>(r&&(t=r(r=0)),t);var st=(r,t)=>{for(var e in t)ot(r,e,{get:t[e],enumerable:!0})};var M,it,W,c,R=k(()=>{M=()=>{},it={debug:M,info:M,warn:(...r)=>console.warn("[workflow]",...r),error:(...r)=>console.error("[workflow]",...r)},W={impl:it},c={debug:(...r)=>W.impl.debug?.(...r),info:(...r)=>W.impl.info?.(...r),warn:(...r)=>W.impl.warn?.(...r),error:(...r)=>W.impl.error?.(...r)}});var b,Z=k(()=>{b=class{constructor(t,e,o=0){this.name=t,this.description=e,this.priority=o}async invoke(t,e={}){throw new Error(`${this.constructor.name}.invoke() must be implemented`)}canHandle(t){throw new Error(`${this.constructor.name}.canHandle() must be implemented`)}getName(){return this.name}getDescription(){return this.description}getPriority(){return this.priority}}});function V(r){return ut.get(r)||null}var ut,Y=k(()=>{ut=new Map});var q={};st(q,{getAgentStrategy:()=>z,invokeAgent:()=>pt,listStrategies:()=>lt,registerStrategy:()=>ct});function ct(r){if(!(r instanceof b))throw new Error("strategy must be an instance of AgentStrategy");let t=S.findIndex(e=>e.getName()===r.getName());t>=0?S[t]=r:S.push(r)}function lt(){return S.map(r=>r.getName())}function z(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}`)}c.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 c.debug(`[workflow] using agent: ${i.getName()}`),i}async function pt(r,t={},e={}){let o=z(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,$={...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=$.skills||[];if(h.length>0&&!e.skipPromptFragments){let y=h.map(I=>{let E=V(I)?.promptFragment;return typeof E=="function"?E():E}).filter(Boolean);y.length>0&&(s+=`
1
+ var ot=Object.defineProperty;var k=(r,t)=>()=>(r&&(t=r(r=0)),t);var st=(r,t)=>{for(var e in t)ot(r,e,{get:t[e],enumerable:!0})};var M,it,N,c,x=k(()=>{M=()=>{},it={debug:M,info:M,warn:(...r)=>console.warn("[workflow]",...r),error:(...r)=>console.error("[workflow]",...r)},N={impl:it},c={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 Y=k(()=>{});function Z(r){return ut.get(r)||null}var ut,V=k(()=>{ut=new Map});var q={};st(q,{getAgentStrategy:()=>z,invokeAgent:()=>pt,listStrategies:()=>lt,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 lt(){return S.map(r=>r.getName())}function z(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}`)}c.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 c.debug(`[workflow] using agent: ${i.getName()}`),i}async function pt(r,t={},e={}){let o=z(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,$={...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=$.skills||[];if(h.length>0&&!e.skipPromptFragments){let y=h.map(I=>{let E=Z(I)?.promptFragment;return typeof E=="function"?E():E}).filter(Boolean);y.length>0&&(s+=`
2
2
 
3
3
  ${y.join(`
4
4
 
@@ -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,$)}var S,X=k(()=>{Z();R();Y();S=[]});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]*\}/);return o?this.validate(JSON.parse(o[0])):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
+ `),c.debug(`[workflow] prompt length: ${s.length} chars`),o.invoke(s,$)}var v,S,X=k(()=>{Y();x();V();v=Symbol.for("@zibby/agent-workflow.strategies");globalThis[v]||(globalThis[v]=[]);S=globalThis[v]});var b=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]*\}/);return o?this.validate(JSON.parse(o[0])):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}};R();import{writeFileSync as C,readFileSync as Q,existsSync as tt,mkdirSync as dt}from"node:fs";import{join as P,dirname as ht}from"node:path";import u from"chalk";var nt="__WORKFLOW_GRAPH_LOG__",O=u.gray("\u2502"),at=u.gray("\u250C"),D=u.gray("\u2514"),A=u.green("\u25C6"),U=u.hex("#c084fc")("\u25C6"),j=u.hex("#2dd4bf")("\u25C6"),L=u.red("\u25C6"),G=`${O} `,J=2;function H(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+=G,t.col=J,t.lineStart=!1),p===`
14
+ `)}`);return t}};x();import{writeFileSync as C,readFileSync as Q,existsSync as tt,mkdirSync as dt}from"node:fs";import{join as P,dirname as ht}from"node:path";import u from"chalk";var nt="__WORKFLOW_GRAPH_LOG__",O=u.gray("\u2502"),at=u.gray("\u250C"),D=u.gray("\u2514"),R=u.green("\u25C6"),U=u.hex("#c084fc")("\u25C6"),G=u.hex("#2dd4bf")("\u25C6"),A=u.red("\u25C6"),j=`${O} `,J=2;function H(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=J,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
- ${G}`,t.col=J))}return r(a,o,i)}}var T=class{constructor(){this._currentNode=null,this._origStdoutWrite=null,this._origStderrWrite=null;let t=String(process.env.ZIBBY_RUN_SOURCE||"").trim().toLowerCase(),e=String(process.env.ZIBBY_WORKFLOW_GRAPH_LOG_MARKERS||"").trim()==="1";this._emitWorkflowGraphMarkers=e||t==="studio"}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(`
16
+ ${j}`,t.col=J))}return r(a,o,i)}}var T=class{constructor(){this._currentNode=null,this._origStdoutWrite=null,this._origStderrWrite=null;let t=String(process.env.ZIBBY_RUN_SOURCE||"").trim().toLowerCase(),e=String(process.env.ZIBBY_WORKFLOW_GRAPH_LOG_MARKERS||"").trim()==="1";this._emitWorkflowGraphMarkers=e||t==="studio"}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=`${nt}${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(A,t):process.stdout.write.bind(process.stdout)(`${O} ${A} ${t}
23
+ `)}step(t){this._origStdoutWrite?this._writeDot(R,t):process.stdout.write.bind(process.stdout)(`${O} ${R} ${t}
24
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=u.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(L,u.red(t)):process.stdout.write.bind(process.stdout)(`${O} ${L} ${u.red(t)}
27
- `)}nodeStart(t){this._currentNode=t,this._emitGraphLogMarker({phase:"node_begin",node:t}),this._rawWrite(`${at} ${t}`),this._startIntercepting()}nodeComplete(t,e={}){this._stopIntercepting();let{duration:o,details:i}=e;if(i)for(let a of i)this._rawWrite(`${A} ${a}`);let n=o?u.dim(` ${H(o)}`):"";this._rawWrite(`${D} ${u.green("done")}${n}`),this._emitGraphLogMarker({phase:"node_end",node:t}),this._rawWrite("")}nodeFailed(t,e,o={}){this._stopIntercepting();let{duration:i}=o,n=i?u.dim(` ${H(i)}`):"";this._rawWrite(`${L} ${u.red(e)}`),this._rawWrite(`${D} ${u.red("failed")}${n}`),this._emitGraphLogMarker({phase:"node_end",node:t}),this._rawWrite("")}route(t,e){this._rawWrite(u.dim(` ${t} \u2192 ${e}`)),this._rawWrite("")}graphComplete(){this._rawWrite(u.green.bold("\u2713 Workflow completed"))}},K=new T;var v=".session-info.json";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 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.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}
25
+ `)}stepMemory(t){let e=u.hex("#2dd4bf")(t);this._origStdoutWrite?this._writeDot(G,e):process.stdout.write.bind(process.stdout)(`${O} ${G} ${e}
26
+ `)}stepFail(t){this._origStdoutWrite?this._writeDot(A,u.red(t)):process.stdout.write.bind(process.stdout)(`${O} ${A} ${u.red(t)}
27
+ `)}nodeStart(t){this._currentNode=t,this._emitGraphLogMarker({phase:"node_begin",node:t}),this._rawWrite(`${at} ${t}`),this._startIntercepting()}nodeComplete(t,e={}){this._stopIntercepting();let{duration:o,details:i}=e;if(i)for(let a of i)this._rawWrite(`${R} ${a}`);let n=o?u.dim(` ${H(o)}`):"";this._rawWrite(`${D} ${u.green("done")}${n}`),this._emitGraphLogMarker({phase:"node_end",node:t}),this._rawWrite("")}nodeFailed(t,e,o={}){this._stopIntercepting();let{duration:i}=o,n=i?u.dim(` ${H(i)}`):"";this._rawWrite(`${A} ${u.red(e)}`),this._rawWrite(`${D} ${u.red("failed")}${n}`),this._emitGraphLogMarker({phase:"node_end",node:t}),this._rawWrite("")}route(t,e){this._rawWrite(u.dim(` ${t} \u2192 ${e}`)),this._rawWrite("")}graphComplete(){this._rawWrite(u.green.bold("\u2713 Workflow completed"))}},K=new T;var L=".session-info.json";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 b(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.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(),w=g.sessionPath;try{if(w){let s=P(w,v);if(tt(s)){let f=JSON.parse(Q(s,"utf-8"));f.currentNode=this.name,C(s,JSON.stringify(f,null,2),"utf-8")}let h=P(w,"..",v);if(tt(h))try{let f=JSON.parse(Q(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 $=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,I={state:o()};y&&(I.preferredAgent=y);let E={workspace:p,schema:this.isZodSchema?this.outputSchema:null,skills:this.config.skills||[],sessionPath:w,config:h,nodeName:this.name,timeout:this.config?.timeout||3e5},x=t?._coreInvokeAgent;x||(x=(await Promise.resolve().then(()=>(X(),q))).invokeAgent);let m=await x(n,I,E),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 l=P(w,this.name,"raw_stream_output.txt");dt(ht(l),{recursive:!0}),C(l,typeof d=="string"?d:JSON.stringify(d),"utf-8")}catch(l){c.debug(`[workflow] could not save raw output: ${l.message}`)}if(this.isZodSchema&&_){c.info(`[workflow] node '${this.name}': output validated: ${JSON.stringify(_,null,2)}`);let l=_;if(typeof this.onComplete=="function")try{l=await this.onComplete(o(),_)}catch(rt){c.warn(`[workflow] onComplete hook failed: ${rt.message}`)}return{success:!0,output:l,raw:d}}if(typeof this.onComplete=="function")try{return{success:!0,output:await this.onComplete(o(),{raw:d}),raw:d}}catch(l){throw new Error(`onComplete failed: ${l.message}`,{cause:l})}if(this.parser){let l=this.parser.parse(d);return c.info(`[workflow] node '${this.name}': parsed output: ${JSON.stringify(l,null,2)}`),K.step("Output parsed"),{success:!0,output:l,raw:d}}return{success:!0,output:d,raw:d}}catch(h){$=h,s<this.retries&&c.info(`[workflow] node '${this.name}' failed, retrying (${s+1}/${this.retries})\u2026`)}return{success:!1,error:$.message,raw:null}}},et=class extends F{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{et as ConditionalNode,F as Node};
29
+ ${n}`);let g=o(),p=g.cwd||process.cwd(),w=g.sessionPath;try{if(w){let s=P(w,L);if(tt(s)){let f=JSON.parse(Q(s,"utf-8"));f.currentNode=this.name,C(s,JSON.stringify(f,null,2),"utf-8")}let h=P(w,"..",L);if(tt(h))try{let f=JSON.parse(Q(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 $=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,I={state:o()};y&&(I.preferredAgent=y);let E={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(()=>(X(),q))).invokeAgent);let m=await W(n,I,E),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 l=P(w,this.name,"raw_stream_output.txt");dt(ht(l),{recursive:!0}),C(l,typeof d=="string"?d:JSON.stringify(d),"utf-8")}catch(l){c.debug(`[workflow] could not save raw output: ${l.message}`)}if(this.isZodSchema&&_){c.info(`[workflow] node '${this.name}': output validated: ${JSON.stringify(_,null,2)}`);let l=_;if(typeof this.onComplete=="function")try{l=await this.onComplete(o(),_)}catch(rt){c.warn(`[workflow] onComplete hook failed: ${rt.message}`)}return{success:!0,output:l,raw:d}}if(typeof this.onComplete=="function")try{return{success:!0,output:await this.onComplete(o(),{raw:d}),raw:d}}catch(l){throw new Error(`onComplete failed: ${l.message}`,{cause:l})}if(this.parser){let l=this.parser.parse(d);return c.info(`[workflow] node '${this.name}': parsed output: ${JSON.stringify(l,null,2)}`),K.step("Output parsed"),{success:!0,output:l,raw:d}}return{success:!0,output:d,raw:d}}catch(h){$=h,s<this.retries&&c.info(`[workflow] node '${this.name}' failed, retrying (${s+1}/${this.retries})\u2026`)}return{success:!1,error:$.message,raw:null}}},et=class extends F{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{et as ConditionalNode,F as Node};
@@ -1,5 +1,12 @@
1
1
  /**
2
2
  * Register an agent strategy. Re-registering by name replaces the existing entry.
3
+ *
4
+ * IMPORTANT: this is a duck-type check, NOT `instanceof AgentStrategy`. With
5
+ * dual-package scenarios (multiple copies of @zibby/agent-workflow loaded),
6
+ * `instanceof` would compare against THIS module's AgentStrategy class while
7
+ * the strategy was constructed in ANOTHER module's class — same shape, but
8
+ * not the same identity, so instanceof fails. Duck-typing avoids that trap.
9
+ *
3
10
  * @param {AgentStrategy} strategy
4
11
  */
5
12
  export function registerStrategy(strategy: AgentStrategy): void;
@@ -1,12 +1,12 @@
1
- var c=class{constructor(n,t,r=0){this.name=n,this.description=t,this.priority=r}async invoke(n,t={}){throw new Error(`${this.constructor.name}.invoke() must be implemented`)}canHandle(n){throw new Error(`${this.constructor.name}.canHandle() must be implemented`)}getName(){return this.name}getDescription(){return this.description}getPriority(){return this.priority}};var h=()=>{},y={debug:h,info:h,warn:(...e)=>console.warn("[workflow]",...e),error:(...e)=>console.error("[workflow]",...e)},g={impl:y};var m={debug:(...e)=>g.impl.debug?.(...e),info:(...e)=>g.impl.info?.(...e),warn:(...e)=>g.impl.warn?.(...e),error:(...e)=>g.impl.error?.(...e)};var v=new Map;function k(e){return v.get(e)||null}var s=[];function P(e){if(!(e instanceof c))throw new Error("strategy must be an instance of AgentStrategy");let n=s.findIndex(t=>t.getName()===e.getName());n>=0?s[n]=e:s.push(e)}function R(){return s.map(e=>e.getName())}function A(e={}){let{state:n={},preferredAgent:t=null}=e,r=t||n.agentType||process.env.AGENT_TYPE;if(!r){let i=s.map(a=>a.getName()).join(", ")||"none registered";throw new Error(`No agent specified. Set agentType in state or AGENT_TYPE env var. Available: ${i}`)}m.debug(`[workflow] agent selection: requested=${r}`);let o=s.find(i=>i.getName()===r);if(!o){let i=s.map(a=>a.getName()).join(", ")||"none registered";throw new Error(`Unknown agent '${r}'. Available: ${i}`)}if(!o.canHandle(e))throw new Error(`Agent '${r}' is not available in this environment. Check credentials/environment.`);return m.debug(`[workflow] using agent: ${o.getName()}`),o}async function C(e,n={},t={}){let r=A(n),o=n.state?.config||t.config||{},i=o.models||{},a=t.nodeName&&i[t.nodeName]||null,E=i.default||null,N=o.agent?.[r.name]?.model||null,b=a||E||N||t.model||null,u={...t,model:b,workspace:n.state?.workspace||t.workspace,schema:t.schema||n.schema,images:t.images||n.images||[],skills:t.skills||n.skills||[],config:o},l=e,p=u.skills||[];if(p.length>0&&!t.skipPromptFragments){let w=p.map(S=>{let f=k(S)?.promptFragment;return typeof f=="function"?f():f}).filter(Boolean);w.length>0&&(l+=`
1
+ var h=()=>{},y={debug:h,info:h,warn:(...e)=>console.warn("[workflow]",...e),error:(...e)=>console.error("[workflow]",...e)},g={impl:y};var c={debug:(...e)=>g.impl.debug?.(...e),info:(...e)=>g.impl.info?.(...e),warn:(...e)=>g.impl.warn?.(...e),error:(...e)=>g.impl.error?.(...e)};var T=new Map;function k(e){return T.get(e)||null}var f=Symbol.for("@zibby/agent-workflow.strategies");globalThis[f]||(globalThis[f]=[]);var s=globalThis[f];function P(e){if(!e||typeof e.getName!="function"||typeof e.invoke!="function")throw new Error("strategy must implement getName() and invoke() (AgentStrategy shape)");let n=s.findIndex(t=>t.getName()===e.getName());n>=0?s[n]=e:s.push(e)}function C(){return s.map(e=>e.getName())}function v(e={}){let{state:n={},preferredAgent:t=null}=e,r=t||n.agentType||process.env.AGENT_TYPE;if(!r){let i=s.map(l=>l.getName()).join(", ")||"none registered";throw new Error(`No agent specified. Set agentType in state or AGENT_TYPE env var. Available: ${i}`)}c.debug(`[workflow] agent selection: requested=${r}`);let o=s.find(i=>i.getName()===r);if(!o){let i=s.map(l=>l.getName()).join(", ")||"none registered";throw new Error(`Unknown agent '${r}'. Available: ${i}`)}if(!o.canHandle(e))throw new Error(`Agent '${r}' is not available in this environment. Check credentials/environment.`);return c.debug(`[workflow] using agent: ${o.getName()}`),o}async function L(e,n={},t={}){let r=v(n),o=n.state?.config||t.config||{},i=o.models||{},l=t.nodeName&&i[t.nodeName]||null,E=i.default||null,N=o.agent?.[r.name]?.model||null,b=l||E||N||t.model||null,u={...t,model:b,workspace:n.state?.workspace||t.workspace,schema:t.schema||n.schema,images:t.images||n.images||[],skills:t.skills||n.skills||[],config:o},a=e,p=u.skills||[];if(p.length>0&&!t.skipPromptFragments){let w=p.map(S=>{let m=k(S)?.promptFragment;return typeof m=="function"?m():m}).filter(Boolean);w.length>0&&(a+=`
2
2
 
3
3
  ${w.join(`
4
4
 
5
- `)}`)}let d=n.state?._currentNodeConfig?.extraPromptInstructions?.trim();return d&&(l+=`
5
+ `)}`)}let d=n.state?._currentNodeConfig?.extraPromptInstructions?.trim();return d&&(a+=`
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
- `),m.debug(`[workflow] prompt length: ${l.length} chars`),r.invoke(l,u)}export{A as getAgentStrategy,C as invokeAgent,R as listStrategies,P as registerStrategy};
12
+ `),c.debug(`[workflow] prompt length: ${a.length} chars`),r.invoke(a,u)}export{v as getAgentStrategy,L as invokeAgent,C as listStrategies,P as registerStrategy};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zibby/agent-workflow",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "Graph-based AI agent workflow orchestration. Bring your own agent strategies.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",