@zibby/agent-workflow 0.4.0 → 0.4.1
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/dist/constants.d.ts +4 -0
- package/dist/constants.js +1 -1
- package/dist/graph-compiler.js +7 -7
- package/dist/graph.js +7 -7
- package/dist/index.js +9 -9
- package/dist/node.js +10 -10
- package/package.json +1 -1
package/dist/constants.d.ts
CHANGED
|
@@ -18,7 +18,11 @@ export namespace SKILLS {
|
|
|
18
18
|
let BROWSER: string;
|
|
19
19
|
let JIRA: string;
|
|
20
20
|
let GITHUB: string;
|
|
21
|
+
let GITLAB: string;
|
|
22
|
+
let GIT: string;
|
|
21
23
|
let SLACK: string;
|
|
24
|
+
let LARK: string;
|
|
25
|
+
let SENTRY: string;
|
|
22
26
|
let MEMORY: string;
|
|
23
27
|
let CHAT_MEMORY: string;
|
|
24
28
|
let RUNNER: string;
|
package/dist/constants.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var o=".zibby/output",t="sessions",s=".session-info.json",
|
|
1
|
+
var o=".zibby/output",t="sessions",s=".session-info.json",I=".zibby-stop",_="result.json",r="raw_stream_output.txt",E="events.json",e={BROWSER:"browser",JIRA:"jira",GITHUB:"github",GITLAB:"gitlab",GIT:"git",SLACK:"slack",LARK:"lark",SENTRY:"sentry",MEMORY:"memory",CHAT_MEMORY:"chat-memory",RUNNER:"runner",SKILL_INSTALLER:"skill-installer",CORE_TOOLS:"core-tools",WORKFLOW_BUILDER:"workflow-builder",SESSION:"session"},S=["CI_JOB_ID","GITHUB_RUN_ID","CIRCLE_WORKFLOW_ID","BUILD_ID"];export{S as CI_ENV_VARS,o as DEFAULT_OUTPUT_BASE,E as EVENTS_FILE,r as RAW_OUTPUT_FILE,_ as RESULT_FILE,t as SESSIONS_DIR,s as SESSION_INFO_FILE,e as SKILLS,I as STOP_REQUEST_FILE};
|
package/dist/graph-compiler.js
CHANGED
|
@@ -11,9 +11,9 @@ PRIORITY OVERRIDE \u2014 THE FOLLOWING INSTRUCTIONS TAKE PRECEDENCE OVER ALL PRE
|
|
|
11
11
|
${m}
|
|
12
12
|
`),E.debug(`[workflow] prompt length: ${f.length} chars`),r.invoke(f,d)}var ge,j,q=Q(()=>{Je();U();se();ge=Symbol.for("@zibby/agent-workflow.strategies");globalThis[ge]||(globalThis[ge]=[]);j=globalThis[ge]});var ht=new Set(["__proto__","constructor","prototype"]);function de(o){if(ht.has(o))throw new Error(`Invalid state key: "${o}"`)}var ee=class{constructor(e={}){this._state=Object.create(null),Object.assign(this._state,{messages:[],errors:[],artifacts:{},metadata:{},...e}),this._history=[]}get(e){return this._state[e]}set(e,t){de(e),this._history.push({...this._state}),this._state[e]=t}update(e){let t=Object.getOwnPropertyNames(e);for(let r of t)de(r);this._history.push({...this._state});for(let r of t)this._state[r]=e[r]}append(e,t){de(e),this._history.push({...this._state}),Array.isArray(this._state[e])||(this._state[e]=[]),this._state[e].push(t)}getAll(){return{...this._state}}rollback(){this._history.length>0&&(this._state=this._history.pop())}};var te=class{constructor(e){this.schema=e}parse(e){let t=e.match(/```json\s*([\s\S]*?)\s*```/);if(t)return this.validate(JSON.parse(t[1]));let r=[e.match(/\{[\s\S]*?\}/),e.match(/\{[\s\S]*\}/)].filter(Boolean).map(i=>i[0]);for(let i of r)try{return this.validate(JSON.parse(i))}catch(s){if(!(s instanceof SyntaxError))throw s}return this.validate({result:e.trim()})}validate(e){let t=[];for(let[r,i]of Object.entries(this.schema)){if(i.required&&!(r in e)&&t.push(`Missing required field: ${r}`),r in e&&i.type){let s=typeof e[r];s!==i.type&&t.push(`Field '${r}' expected ${i.type}, got ${s}`)}if(i.validate&&r in e){let s=i.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}};U();import{writeFileSync as Se,readFileSync as He,existsSync as Ke,mkdirSync as Tt}from"node:fs";import{join as we,dirname as kt}from"node:path";import T from"chalk";var gt="__WORKFLOW_GRAPH_LOG__",Z=T.gray("\u2502"),St=T.gray("\u250C"),Re=T.gray("\u2514"),pe=T.green("\u25C6"),Me=T.hex("#c084fc")("\u25C6"),Be=T.hex("#2dd4bf")("\u25C6"),fe=T.red("\u25C6"),je=`${Z} `,
|
|
14
|
+
`)}`);return e}};U();import{writeFileSync as Se,readFileSync as He,existsSync as Ke,mkdirSync as Tt}from"node:fs";import{join as we,dirname as kt}from"node:path";import T from"chalk";var gt="__WORKFLOW_GRAPH_LOG__",Z=T.gray("\u2502"),St=T.gray("\u250C"),Re=T.gray("\u2514"),pe=T.green("\u25C6"),Me=T.hex("#c084fc")("\u25C6"),Be=T.hex("#2dd4bf")("\u25C6"),fe=T.red("\u25C6"),je=`${Z} `,Le=2;function De(o){return o<1e3?`${o}ms`:`${(o/1e3).toFixed(1)}s`}function We(o,e){return(t,r,i)=>{if(typeof t!="string")return o(t,r,i);let s=process.stdout.columns||120,a="";for(let n=0;n<t.length;n++){let c=t[n];e.lineStart&&(a+=je,e.col=Le,e.lineStart=!1),c===`
|
|
15
15
|
`?(a+=c,e.lineStart=!0,e.col=0,e.inEsc=!1):c==="\x1B"?(e.inEsc=!0,a+=c):e.inEsc?(a+=c,(c>="A"&&c<="Z"||c>="a"&&c<="z")&&(e.inEsc=!1)):(e.col++,a+=c,e.col>=s&&(a+=`
|
|
16
|
-
${je}`,e.col=
|
|
16
|
+
${je}`,e.col=Le))}return o(a,r,i)}}var he=class{constructor(){this._currentNode=null,this._origStdoutWrite=null,this._origStderrWrite=null,this._emitWorkflowGraphMarkers=String(process.env.ZIBBY_EMIT_GRAPH_MARKERS||"").trim()==="1"||String(process.env.ZIBBY_WORKFLOW_GRAPH_LOG_MARKERS||"").trim()==="1"}get isInsideNode(){return this._currentNode!==null}_startIntercepting(){this._origStdoutWrite=process.stdout.write.bind(process.stdout),this._origStderrWrite=process.stderr.write.bind(process.stderr);let e={lineStart:!0,col:0,inEsc:!1},t={lineStart:!0,col:0,inEsc:!1};this._outState=e,this._errState=t,process.stdout.write=We(this._origStdoutWrite,e),process.stderr.write=We(this._origStderrWrite,t)}_stopIntercepting(){this._origStdoutWrite&&(this._outState&&!this._outState.lineStart&&this._origStdoutWrite(`
|
|
17
17
|
`),process.stdout.write=this._origStdoutWrite),this._origStderrWrite&&(this._errState&&!this._errState.lineStart&&this._origStderrWrite(`
|
|
18
18
|
`),process.stderr.write=this._origStderrWrite),this._origStdoutWrite=null,this._origStderrWrite=null}_rawWrite(e){(this._origStdoutWrite||process.stdout.write.bind(process.stdout))(`${e}
|
|
19
19
|
`)}_emitGraphLogMarker(e){if(!this._emitWorkflowGraphMarkers)return;let t=`${gt}${JSON.stringify(e)}
|
|
@@ -24,17 +24,17 @@ ${je}`,e.col=De))}return o(a,r,i)}}var he=class{constructor(){this._currentNode=
|
|
|
24
24
|
`)}stepInfo(e){this.step(e)}stepTool(e){this._origStdoutWrite?this._writeDot(Me,e):process.stdout.write.bind(process.stdout)(`${Z} ${Me} ${e}
|
|
25
25
|
`)}stepMemory(e){let t=T.hex("#2dd4bf")(e);this._origStdoutWrite?this._writeDot(Be,t):process.stdout.write.bind(process.stdout)(`${Z} ${Be} ${t}
|
|
26
26
|
`)}stepFail(e){this._origStdoutWrite?this._writeDot(fe,T.red(e)):process.stdout.write.bind(process.stdout)(`${Z} ${fe} ${T.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:r,details:i}=t;if(i)for(let a of i)this._rawWrite(`${pe} ${a}`);let s=r?T.dim(` ${
|
|
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:r,details:i}=t;if(i)for(let a of i)this._rawWrite(`${pe} ${a}`);let s=r?T.dim(` ${De(r)}`):"";this._rawWrite(`${Re} ${T.green("done")}${s}`),this._emitGraphLogMarker({phase:"node_end",node:e}),this._rawWrite("")}nodeFailed(e,t,r={}){this._stopIntercepting();let{duration:i}=r,s=i?T.dim(` ${De(i)}`):"";this._rawWrite(`${fe} ${T.red(t)}`),this._rawWrite(`${Re} ${T.red("failed")}${s}`),this._emitGraphLogMarker({phase:"node_end",node:e}),this._rawWrite("")}route(e,t){this._rawWrite(T.dim(` ${e} \u2192 ${t}`)),this._rawWrite("")}graphComplete(){}},k=new he;var re=".zibby/output",Fe="sessions",G=".session-info.json",Ue=".zibby-stop";var Ge=["CI_JOB_ID","GITHUB_RUN_ID","CIRCLE_WORKFLOW_ID","BUILD_ID"];var B=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 te(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,i=f=>t&&typeof t.get=="function"?t.get(f):e?.[f];if(typeof this.customExecute=="function"){E.debug(`[workflow] node '${this.name}': custom execute (skipping LLM)`);try{let f=await this.customExecute(e);return typeof f=="object"&&f!==null&&f.success===!1?{success:!1,error:f.error||"Node execution failed",raw:f.raw||null}:this.isZodSchema?(E.debug(`[workflow] node '${this.name}': validating output schema`),{success:!0,output:this.outputSchema.parse(f),raw:null}):{success:!0,output:f,raw:null}}catch(f){return E.error(`[workflow] node '${this.name}' failed: ${f.message}`),f.name==="ZodError"&&E.error(`Schema errors: ${JSON.stringify(f.issues||f.errors,null,2)}`),{success:!1,error:f.message,raw:null}}}let s=typeof this.prompt=="function"?this.prompt(r()):this.prompt,a=i("_skillHints");a&&(s=`${a}
|
|
28
28
|
|
|
29
|
-
${s}`);let n=r(),c=n.cwd||process.cwd(),l=n.sessionPath;try{if(l){let f=we(l,G);if(Ke(f)){let m=JSON.parse(He(f,"utf-8"));m.currentNode=this.name,Se(f,JSON.stringify(m,null,2),"utf-8")}let u=we(l,"..",G);if(Ke(u))try{let m=JSON.parse(He(u,"utf-8"));m.currentNode=this.name,Se(u,JSON.stringify(m,null,2),"utf-8")}catch{}}}catch(f){E.debug(`[workflow] could not update session info: ${f.message}`)}let d=null;for(let f=0;f<=this.retries;f++)try{E.debug(`[workflow] node '${this.name}' attempt ${f}`);let u=r().config||{},m=u.agents||{},w=this.config.agent??m[this.name]??null,h={state:r()};w&&(h.preferredAgent=w);let v={workspace:c,schema:this.isZodSchema?this.outputSchema:null,skills:this.config.skills||[],sessionPath:l,config:u,nodeName:this.name,timeout:this.config?.timeout||3e5},b=e?._coreInvokeAgent;b||(b=(await Promise.resolve().then(()=>(q(),K))).invokeAgent);let p=await b(s,h,v),y,$;if(typeof p=="string"?(y=p,$=null):p.structured?(y=p.raw||JSON.stringify(p.structured,null,2),$=p.structured):(y=p.raw||JSON.stringify(p,null,2),$=p.extracted||null),l)try{let g=we(l,this.name,"raw_stream_output.txt");Tt(kt(g),{recursive:!0}),Se(g,typeof y=="string"?y:JSON.stringify(y),"utf-8")}catch(g){E.debug(`[workflow] could not save raw output: ${g.message}`)}if(this.isZodSchema&&$){E.info(`[workflow] node '${this.name}': output validated: ${JSON.stringify($,null,2)}`);let g=$;if(typeof this.onComplete=="function")try{g=await this.onComplete(r(),$)}catch(C){E.warn(`[workflow] onComplete hook failed: ${C.message}`)}return{success:!0,output:g,raw:y}}if(typeof this.onComplete=="function")try{return{success:!0,output:await this.onComplete(r(),{raw:y}),raw:y}}catch(g){throw new Error(`onComplete failed: ${g.message}`,{cause:g})}if(this.parser){let g=this.parser.parse(y);return E.info(`[workflow] node '${this.name}': parsed output: ${JSON.stringify(g,null,2)}`),k.step("Output parsed"),{success:!0,output:g,raw:y}}return{success:!0,output:y,raw:y}}catch(u){d=u,f<this.retries&&E.info(`[workflow] node '${this.name}' failed, retrying (${f+1}/${this.retries})\u2026`)}return{success:!1,error:d.message,raw:null}}},ne=class extends B{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}}};U();var At=2e3,xt=600*1e3,Nt=new Set(["completed","failed","canceled","timeout"]);function Ot(){let o=process.env.PROGRESS_API_URL;if(!o)throw new Error("Sub-graph dispatch requires PROGRESS_API_URL env var (set automatically on cloud runs). Sub-graphs are not supported in local in-process runs yet \u2014 deploy the parent and child to cloud.");return o.replace(/\/executions\/?$/,"")}function Pt(){let o=process.env.PROJECT_ID;if(!o)throw new Error("Sub-graph dispatch requires PROJECT_ID env var.");return o}function Ct(){let o=process.env.PROJECT_API_TOKEN;if(!o)throw new Error("Sub-graph dispatch requires PROJECT_API_TOKEN env var.");return o}function Rt(){return process.env.EXECUTION_ID||null}function Mt(o,e){return e==null?o:typeof e=="function"?e(o):typeof e=="string"?e.split(".").reduce((t,r)=>t==null?t:t[r],o):o}async function qe(o,e={}){if(!o||typeof o!="string")throw new Error("dispatchSubgraph: workflowName (string) is required");let t=Ot(),r=Pt(),i=Ct(),s=Rt(),a=`${t}/projects/${encodeURIComponent(r)}/workflows/${encodeURIComponent(o)}/trigger`,n={input:e.input||{},...s?{parentExecutionId:s}:{},...e.conversationId?{conversationId:e.conversationId}:{}};E.info(`[sub-graph] dispatching '${o}' (${e.async?"async":"sync"}) from parent ${s||"<none>"}`);let c=await fetch(a,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${i}`},body:JSON.stringify(n)});if(!c.ok){let p=null,y="";try{p=await c.json(),y=p?.error||p?.message||JSON.stringify(p)}catch{y=await c.text().catch(()=>"")}if(c.status===429){let g=p?.quotaInfo||{},C=new Error(`Sub-graph '${o}' blocked by execution quota (${g.used??"?"}/${g.limit??"?"} on plan ${g.planId||"unknown"}). Sub-workflow runs count toward the same monthly cap as user-triggered runs.`);throw C.code="SUBGRAPH_QUOTA_EXCEEDED",C.status=429,C.subgraph=o,C.quotaInfo=g,C}if(c.status===400){let g=new Error(`Sub-graph '${o}' rejected input: ${y}`);throw g.code="SUBGRAPH_INVALID_INPUT",g.status=400,g.subgraph=o,g.validationErrors=p?.validationErrors||null,g.missing=p?.missing||null,g}let $=new Error(`Sub-graph '${o}' trigger rejected (${c.status}): ${y}`);throw $.code="SUBGRAPH_TRIGGER_FAILED",$.status=c.status,$.subgraph=o,$}let l=await c.json(),d=l?.data?.jobId||l?.jobId;if(!d)throw new Error(`Sub-graph '${o}' trigger returned no jobId: ${JSON.stringify(l).slice(0,200)}`);if(e.async)return E.info(`[sub-graph] async dispatch of '${o}' \u2192 jobId=${d} (not waiting)`),{jobId:d,status:"accepted",workflow:o};let f=Number.isFinite(e.timeoutMs)?e.timeoutMs:xt,u=Number.isFinite(e.pollIntervalMs)?e.pollIntervalMs:At,m=`${t}/executions/${encodeURIComponent(d)}`,w=Date.now()+f,h="accepted",v=0;for(;Date.now()<w;){await new Promise(g=>setTimeout(g,u)),v+=1;let p=await fetch(m,{headers:{Authorization:`Bearer ${i}`}});if(!p.ok){if(p.status>=500){E.warn(`[sub-graph] status poll for ${d} returned ${p.status}, will retry`);continue}throw new Error(`Sub-graph status poll failed for ${d}: ${p.status}`)}let y=await p.json(),$=y?.data||y?.execution||y;if(h=$?.status||h,Nt.has(h)){if(h!=="completed"){let
|
|
29
|
+
${s}`);let n=r(),c=n.cwd||process.cwd(),l=n.sessionPath;try{if(l){let f=we(l,G);if(Ke(f)){let m=JSON.parse(He(f,"utf-8"));m.currentNode=this.name,Se(f,JSON.stringify(m,null,2),"utf-8")}let u=we(l,"..",G);if(Ke(u))try{let m=JSON.parse(He(u,"utf-8"));m.currentNode=this.name,Se(u,JSON.stringify(m,null,2),"utf-8")}catch{}}}catch(f){E.debug(`[workflow] could not update session info: ${f.message}`)}let d=null;for(let f=0;f<=this.retries;f++)try{E.debug(`[workflow] node '${this.name}' attempt ${f}`);let u=r().config||{},m=u.agents||{},w=this.config.agent??m[this.name]??null,h={state:r()};w&&(h.preferredAgent=w);let v={workspace:c,schema:this.isZodSchema?this.outputSchema:null,skills:this.config.skills||[],sessionPath:l,config:u,nodeName:this.name,timeout:this.config?.timeout||3e5},b=e?._coreInvokeAgent;b||(b=(await Promise.resolve().then(()=>(q(),K))).invokeAgent);let p=await b(s,h,v),y,$;if(typeof p=="string"?(y=p,$=null):p.structured?(y=p.raw||JSON.stringify(p.structured,null,2),$=p.structured):(y=p.raw||JSON.stringify(p,null,2),$=p.extracted||null),l)try{let g=we(l,this.name,"raw_stream_output.txt");Tt(kt(g),{recursive:!0}),Se(g,typeof y=="string"?y:JSON.stringify(y),"utf-8")}catch(g){E.debug(`[workflow] could not save raw output: ${g.message}`)}if(this.isZodSchema&&$){E.info(`[workflow] node '${this.name}': output validated: ${JSON.stringify($,null,2)}`);let g=$;if(typeof this.onComplete=="function")try{g=await this.onComplete(r(),$)}catch(C){E.warn(`[workflow] onComplete hook failed: ${C.message}`)}return{success:!0,output:g,raw:y}}if(typeof this.onComplete=="function")try{return{success:!0,output:await this.onComplete(r(),{raw:y}),raw:y}}catch(g){throw new Error(`onComplete failed: ${g.message}`,{cause:g})}if(this.parser){let g=this.parser.parse(y);return E.info(`[workflow] node '${this.name}': parsed output: ${JSON.stringify(g,null,2)}`),k.step("Output parsed"),{success:!0,output:g,raw:y}}return{success:!0,output:y,raw:y}}catch(u){d=u,f<this.retries&&E.info(`[workflow] node '${this.name}' failed, retrying (${f+1}/${this.retries})\u2026`)}return{success:!1,error:d.message,raw:null}}},ne=class extends B{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}}};U();var At=2e3,xt=600*1e3,Nt=new Set(["completed","failed","canceled","timeout"]);function Ot(){let o=process.env.PROGRESS_API_URL;if(!o)throw new Error("Sub-graph dispatch requires PROGRESS_API_URL env var (set automatically on cloud runs). Sub-graphs are not supported in local in-process runs yet \u2014 deploy the parent and child to cloud.");return o.replace(/\/executions\/?$/,"")}function Pt(){let o=process.env.PROJECT_ID;if(!o)throw new Error("Sub-graph dispatch requires PROJECT_ID env var.");return o}function Ct(){let o=process.env.PROJECT_API_TOKEN;if(!o)throw new Error("Sub-graph dispatch requires PROJECT_API_TOKEN env var.");return o}function Rt(){return process.env.EXECUTION_ID||null}function Mt(o,e){return e==null?o:typeof e=="function"?e(o):typeof e=="string"?e.split(".").reduce((t,r)=>t==null?t:t[r],o):o}async function qe(o,e={}){if(!o||typeof o!="string")throw new Error("dispatchSubgraph: workflowName (string) is required");let t=Ot(),r=Pt(),i=Ct(),s=Rt(),a=`${t}/projects/${encodeURIComponent(r)}/workflows/${encodeURIComponent(o)}/trigger`,n={input:e.input||{},...s?{parentExecutionId:s}:{},...e.conversationId?{conversationId:e.conversationId}:{}};E.info(`[sub-graph] dispatching '${o}' (${e.async?"async":"sync"}) from parent ${s||"<none>"}`);let c=await fetch(a,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${i}`},body:JSON.stringify(n)});if(!c.ok){let p=null,y="";try{p=await c.json(),y=p?.error||p?.message||JSON.stringify(p)}catch{y=await c.text().catch(()=>"")}if(c.status===429){let g=p?.quotaInfo||{},C=new Error(`Sub-graph '${o}' blocked by execution quota (${g.used??"?"}/${g.limit??"?"} on plan ${g.planId||"unknown"}). Sub-workflow runs count toward the same monthly cap as user-triggered runs.`);throw C.code="SUBGRAPH_QUOTA_EXCEEDED",C.status=429,C.subgraph=o,C.quotaInfo=g,C}if(c.status===400){let g=new Error(`Sub-graph '${o}' rejected input: ${y}`);throw g.code="SUBGRAPH_INVALID_INPUT",g.status=400,g.subgraph=o,g.validationErrors=p?.validationErrors||null,g.missing=p?.missing||null,g}let $=new Error(`Sub-graph '${o}' trigger rejected (${c.status}): ${y}`);throw $.code="SUBGRAPH_TRIGGER_FAILED",$.status=c.status,$.subgraph=o,$}let l=await c.json(),d=l?.data?.jobId||l?.jobId;if(!d)throw new Error(`Sub-graph '${o}' trigger returned no jobId: ${JSON.stringify(l).slice(0,200)}`);if(e.async)return E.info(`[sub-graph] async dispatch of '${o}' \u2192 jobId=${d} (not waiting)`),{jobId:d,status:"accepted",workflow:o};let f=Number.isFinite(e.timeoutMs)?e.timeoutMs:xt,u=Number.isFinite(e.pollIntervalMs)?e.pollIntervalMs:At,m=`${t}/executions/${encodeURIComponent(d)}`,w=Date.now()+f,h="accepted",v=0;for(;Date.now()<w;){await new Promise(g=>setTimeout(g,u)),v+=1;let p=await fetch(m,{headers:{Authorization:`Bearer ${i}`}});if(!p.ok){if(p.status>=500){E.warn(`[sub-graph] status poll for ${d} returned ${p.status}, will retry`);continue}throw new Error(`Sub-graph status poll failed for ${d}: ${p.status}`)}let y=await p.json(),$=y?.data||y?.execution||y;if(h=$?.status||h,Nt.has(h)){if(h!=="completed"){let D=new Error(`Sub-graph '${o}' (${d}) ended in status '${h}'`);throw D.subgraphJobId=d,D.subgraphStatus=h,D}let g=$?.finalState||$?.state||{},C=Mt(g,e.output);return E.info(`[sub-graph] '${o}' (${d}) completed after ${v} polls`),C}}let b=new Error(`Sub-graph '${o}' (${d}) timed out after ${Math.round(f/1e3)}s (last status: ${h})`);throw b.subgraphJobId=d,b.subgraphStatus=h,b}import{existsSync as ze,readFileSync as Bt}from"node:fs";import{join as ye,dirname as Ve}from"node:path";var ie=class{static async loadContext(e,t,r={}){let i={},s=r.filenames||["CONTEXT.md","AGENTS.md"];if(e){let n=Ve(ye(t,e));for(let c of s){let l=await this.findAndMergeContextFiles(c,n,t);if(l){let d=c.replace(/\.[^.]+$/,"").toLowerCase();i[d]=l}}}let a=r.discovery||{};for(let[n,c]of Object.entries(a))try{let l=ye(t,c);ze(l)&&(i[n]=await this.loadFile(l))}catch(l){console.warn(`[workflow] could not load context '${n}' from '${c}': ${l.message}`)}return i}static async findAndMergeContextFiles(e,t,r){let i=[],s=t;for(;s.startsWith(r);){let a=ye(s,e);if(ze(a))try{i.unshift(await this.loadFile(a))}catch(c){console.warn(`[workflow] could not load ${e} from ${a}: ${c.message}`)}let n=Ve(s);if(n===s)break;s=n}return i.length===0?null:i.every(a=>typeof a=="string")?i.join(`
|
|
30
30
|
|
|
31
31
|
---
|
|
32
32
|
|
|
33
|
-
`):i.every(a=>typeof a=="object")?Object.assign({},...i):i[i.length-1]}static async loadFile(e){let t=Bt(e,"utf-8");if(e.endsWith(".json"))return JSON.parse(t);if(e.endsWith(".js")||e.endsWith(".mjs")){let{pathToFileURL:r}=await import("url"),i=await import(r(e).href);return i.default||i}return t}};import{mkdirSync as et,existsSync as _e,writeFileSync as Xe,unlinkSync as jt}from"node:fs";import{join as
|
|
33
|
+
`):i.every(a=>typeof a=="object")?Object.assign({},...i):i[i.length-1]}static async loadFile(e){let t=Bt(e,"utf-8");if(e.endsWith(".json"))return JSON.parse(t);if(e.endsWith(".js")||e.endsWith(".mjs")){let{pathToFileURL:r}=await import("url"),i=await import(r(e).href);return i.default||i}return t}};import{mkdirSync as et,existsSync as _e,writeFileSync as Xe,unlinkSync as jt}from"node:fs";import{join as L,resolve as tt}from"node:path";import{config as Lt}from"dotenv";import{zodToJsonSchema as Qe}from"zod-to-json-schema";import Dt from"handlebars";function Wt({traceFrom:o,sessionId:e,sessionPath:t,idSource:r,mkdirFresh:i}){if(!(process.env.ZIBBY_SESSION_LOG==="1"||process.env.ZIBBY_SESSION_LOG==="true"))return;let a=typeof process.ppid=="number"?process.ppid:"n/a",n=`[zibby:session] from=${o} pid=${process.pid} ppid=${a} sessionId=${e} source=${r} mkdir=${i?"yes":"no"} path=${t}`;if(console.log(n),process.env.ZIBBY_TRACE_SESSION==="1"||process.env.ZIBBY_TRACE_SESSION==="true"){let d=(new Error("session trace").stack||"").split(`
|
|
34
34
|
`).slice(2,14).join(`
|
|
35
35
|
`);console.log(`[zibby:session] stack (${o}):
|
|
36
|
-
${d}`)}}function Ft(){return process.env.ZIBBY_TRUST_SESSION_ENV==="1"||process.env.ZIBBY_TRUST_SESSION_ENV==="true"||process.env.ZIBBY_KEEP_SESSION_ENV==="1"||process.env.ZIBBY_KEEP_SESSION_ENV==="true"}function Ut(){if(!(process.env.ZIBBY_PIN_SESSION_PATH==="1"||process.env.ZIBBY_PIN_SESSION_PATH==="true"))return;let e=process.env.ZIBBY_SESSION_PATH;if(!(e==null||String(e).trim()===""))try{return tt(String(e).trim())}catch{return String(e).trim()}}function Gt(){Ft()||(delete process.env.ZIBBY_SESSION_PATH,delete process.env.ZIBBY_SESSION_ID)}function Jt({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 Yt(o={}){let e=Ge.map(s=>process.env[s]).find(Boolean),t=Math.random().toString(36).slice(2,6),r=e||`${Date.now()}_${t}`,i=o.paths?.sessionPrefix;return i?`${i}_${r}`:r}function Zt({cwd:o=process.cwd(),config:e={},initialState:t={},traceFrom:r="resolveWorkflowSession"}={}){let i=t.sessionPath,s=t.sessionTimestamp,a="initialState.sessionPath";if(!i&&process.env.ZIBBY_SESSION_PATH)try{let l=tt(String(process.env.ZIBBY_SESSION_PATH));l&&(i=l,a="ZIBBY_SESSION_PATH")}catch{}let n;if(i)n=String(i).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)n=l,a="ZIBBY_SESSION_ID";else{let f=e.sessionId!=null?String(e.sessionId).trim():"";f&&f!=="last"?(n=f,a="config.sessionId"):(n=Yt(e),a="generated")}s=s??Date.now();let d=e.paths?.output||re;i=
|
|
37
|
-
\u{1F6D1} External stop requested \u2014 ending workflow.`),k.step("Workflow stopped externally"),{success:!0,state:p.getAll(),executionLog:z,stoppedExternally:!0};let x=this.nodes.get(S);if(!x)throw new Error(`Node '${S}' not found in graph`);let Te=JSON.stringify({sessionPath:m,sessionTimestamp:w,currentNode:S,createdAt:new Date().toISOString(),config:p.get("config")}),at=
|
|
36
|
+
${d}`)}}function Ft(){return process.env.ZIBBY_TRUST_SESSION_ENV==="1"||process.env.ZIBBY_TRUST_SESSION_ENV==="true"||process.env.ZIBBY_KEEP_SESSION_ENV==="1"||process.env.ZIBBY_KEEP_SESSION_ENV==="true"}function Ut(){if(!(process.env.ZIBBY_PIN_SESSION_PATH==="1"||process.env.ZIBBY_PIN_SESSION_PATH==="true"))return;let e=process.env.ZIBBY_SESSION_PATH;if(!(e==null||String(e).trim()===""))try{return tt(String(e).trim())}catch{return String(e).trim()}}function Gt(){Ft()||(delete process.env.ZIBBY_SESSION_PATH,delete process.env.ZIBBY_SESSION_ID)}function Jt({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 Yt(o={}){let e=Ge.map(s=>process.env[s]).find(Boolean),t=Math.random().toString(36).slice(2,6),r=e||`${Date.now()}_${t}`,i=o.paths?.sessionPrefix;return i?`${i}_${r}`:r}function Zt({cwd:o=process.cwd(),config:e={},initialState:t={},traceFrom:r="resolveWorkflowSession"}={}){let i=t.sessionPath,s=t.sessionTimestamp,a="initialState.sessionPath";if(!i&&process.env.ZIBBY_SESSION_PATH)try{let l=tt(String(process.env.ZIBBY_SESSION_PATH));l&&(i=l,a="ZIBBY_SESSION_PATH")}catch{}let n;if(i)n=String(i).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)n=l,a="ZIBBY_SESSION_ID";else{let f=e.sessionId!=null?String(e.sessionId).trim():"";f&&f!=="last"?(n=f,a="config.sessionId"):(n=Yt(e),a="generated")}s=s??Date.now();let d=e.paths?.output||re;i=L(o,d,Fe,n)}let c=!_e(i);return c&&et(i,{recursive:!0}),(c||a!=="initialState.sessionPath")&&Wt({traceFrom:r,sessionId:n,sessionPath:i,idSource:a,mkdirFresh:c}),Jt({sessionPath:i,sessionId:n}),{sessionPath:i,sessionId:n,sessionTimestamp:s}}var ae=class{constructor(e={}){this.nodes=new Map,this.edges=new Map,this.entryPoint=null,this.middleware=Array.isArray(e.middleware)?[...e.middleware]:[],e.nodeMiddleware&&this.middleware.push(e.nodeMiddleware),this.nodeTypeMap=new Map,this.conditionalCodeMap=new Map,this.stateSchema=e.stateSchema||null,this.inputSchema=e.inputSchema||null,this.contextSchema=e.contextSchema||null,this.nodePrompts=new Map,this.nodeOptions=new Map,this._invokeAgent=e.invokeAgent||null,this._compiledPrompts=new Map}setInputSchema(e){return this.inputSchema=e,this}setContextSchema(e){return this.contextSchema=e,this}setStateSchema(e){return this.stateSchema=e,this}getInputSchema(){return this.inputSchema}getContextSchema(){return this.contextSchema}getStateSchema(){return this.stateSchema}_runtimeSchema(){if(this.inputSchema&&this.contextSchema)try{return this.inputSchema.merge(this.contextSchema)}catch{}return this.inputSchema&&!this.contextSchema?this.inputSchema:this.stateSchema}addNode(e,t,r={}){if(!(t instanceof B)&&t&&typeof t=="object"&&typeof t.workflow=="string"){let s=t,a={name:e,_isCustomCode:!0,retries:s.retries,onComplete:s.onComplete,execute:async c=>{let l=c?.state&&typeof c.state.getAll=="function"?c.state.getAll():c,d;return typeof s.input=="function"?d=s.input(l):s.input&&typeof s.input=="object"?d=s.input:d={},qe(s.workflow,{input:d,async:s.async===!0,conversationId:typeof s.conversationId=="function"?s.conversationId(l):s.conversationId,output:s.output,timeoutMs:s.timeoutMs,pollIntervalMs:s.pollIntervalMs})}},n=new B(a);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}let i=t instanceof B?t:new B(t);return i.name=e,this.nodes.set(e,i),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 ne({...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,i,s){let a=r;for(let n=e.length-1;n>=0;n--){let c=e[n],l=a;a=()=>c(t,l,i,s)}return a()}serialize(){let e=[],t={};for(let[l,d]of this.nodes){let f=this.nodeTypeMap.get(l)||l;e.push({id:l,type:f,data:{nodeType:f,label:l}});let u={};d._isCustomCode&&typeof d.execute=="function"&&(u.customCode=d.execute.toString());let m=this.nodePrompts.get(l);if(m&&(u.prompt=m),typeof d.customExecute=="function"&&(u.executeCode=d.customExecute.toString()),d.outputSchema)try{if(typeof d.outputSchema._def<"u"){let h=Qe(d.outputSchema,{target:"openApi3"});u.outputSchema={jsonSchema:h,variables:this._flattenJsonSchemaToVariables(h)}}else u.outputSchema={schema:d.outputSchema}}catch(h){console.warn(`[workflow] failed to convert schema for ${l}:`,h.message)}let w=(this.resolvedToolsMap||{})[l];w?.toolIds&&(u.tools=w.toolIds),Object.keys(u).length>0&&(t[l]=u)}let r=[];for(let[l,d]of this.edges)if(typeof d=="string")r.push({source:l,target:d});else if(d.conditional){let f=this.conditionalCodeMap.get(l)||d.routes.toString(),u=this._inferConditionalTargets(d.routes),m=d.labels||{};for(let w of u){let h={source:l,target:w,data:{conditionalCode:f}};m[w]&&(h.label=m[w]),r.push(h)}}let i=l=>{if(!l)return null;try{return Qe(l,{target:"openApi3"})}catch{return null}},s=this._runtimeSchema(),a=i(s||this.stateSchema),n=i(this.inputSchema),c=i(this.contextSchema);return{nodes:e,edges:r,nodeConfigs:t,stateSchema:a,inputSchema:n,contextSchema:c}}_inferConditionalTargets(e){let t=e.toString(),r=new Set,i=/return\s+['"]([^'"]+)['"]/g,s;for(;(s=i.exec(t))!==null;)r.add(s[1]);return[...r]}_flattenJsonSchemaToVariables(e,t=""){let r=e;if(e.$ref&&e.definitions){let i=e.$ref.replace("#/definitions/","");r=e.definitions[i]||e}return this._flattenSchema(r,t)}_flattenSchema(e,t=""){if(!e||typeof e!="object")return[];let r=[],i=e.properties||{},s=e.required||[];for(let[a,n]of Object.entries(i)){let c=t?`${t}.${a}`:a;r.push({path:c,type:n.type||"unknown",label:n.description||this._formatLabel(a),optional:!s.includes(a)}),n.type==="object"&&n.properties&&r.push(...this._flattenSchema(n,c)),n.type==="array"&&n.items?.type==="object"&&n.items.properties&&r.push(...this._flattenSchema(n.items,`${c}[]`))}return r}_formatLabel(e){return e.replace(/([A-Z])/g," $1").replace(/^./,t=>t.toUpperCase()).trim()}_summarizeNodeOutput(e,t){if(!t||typeof t!="object")return[];let r=[];t.success!==void 0&&r.push(`Result: ${t.success?"passed":"failed"}`);for(let[i,s]of Object.entries(t))if(!(i==="success"||i==="raw"||i==="nextNode")){if(typeof s=="string"&&s.length<=80)r.push(`${i}: ${s}`);else if(Array.isArray(s)){let a=s.length,n=s.filter(l=>l?.passed===!0).length,c=s.some(l=>l?.passed!==void 0);r.push(c?`${i}: ${n}/${a} passed${a-n?`, ${a-n} failed`:""}`:`${i}: ${a} items`)}if(r.length>=4)break}return r}async run(e,t={},r={}){if(!this.entryPoint)throw new Error("No entry point set for graph");let i=new AbortController;r.signal&&(r.signal.aborted?i.abort():r.signal.addEventListener("abort",()=>i.abort(),{once:!0}));let s=r.strategyAbortTimeoutMs??t.config?.strategyAbortTimeoutMs??5e3,a=t.cwd||process.cwd();Lt({path:L(a,".env")});let n=t.config||{};if(!n||Object.keys(n).length===0)try{let _=L(a,".zibby.config.js");_e(_)&&(n=(await import(_)).default||{})}catch{}process.env.EXECUTION_ID&&!n.agent?.strictMode&&(n.agent={...n.agent,strictMode:!0});let c=t.agentType;if(!c){let _=n?.agent;_?.provider?c=_.provider:_?.gemini?c="gemini":_?.claude?c="claude":_?.cursor?c="cursor":_?.codex?c="codex":c=process.env.AGENT_TYPE||"cursor"}let l=t.contextConfig||e?.config?.contextConfig||e?.config?.context||n?.context||{},d=this._runtimeSchema();if(d){let _=d.safeParse(t);if(!_.success){let A=_.error.issues.map(x=>`${x.path.join(".")}: ${x.message}`);throw console.error("\u274C Initial state validation failed:"),A.forEach(x=>console.error(` - ${x}`)),new Error(`State validation failed: ${A.join(", ")}`)}k.step("State validated against schema")}let f=Ut(),u=t.sessionPath||f;u||Gt();let{sessionPath:m,sessionTimestamp:w,sessionId:h}=Zt({cwd:a,config:n,traceFrom:"WorkflowGraph.run",initialState:{sessionPath:u,sessionTimestamp:t.sessionTimestamp}});k.step(`Session ${h}`);let v=await ie.loadContext(t.specPath||"",a,l);Object.keys(v).length>0&&k.step(`Context loaded: ${Object.keys(v).join(", ")}`);let b=t.outputPath;!b&&t.specPath&&(e?.calculateOutputPath?b=e.calculateOutputPath(t.specPath):console.warn(`\u26A0\uFE0F outputPath not resolved (specPath=${t.specPath})`));let p=new ee({...t,config:n,agentType:c,outputPath:b,sessionPath:m,sessionTimestamp:w,context:v,resolvedTools:this.resolvedToolsMap||{},_signal:i.signal}),y=new Map;try{await import("@zibby/skills")}catch{}let{getSkill:$}=await Promise.resolve().then(()=>(se(),Ye)),g=n.skills&&typeof n.skills=="object"?n.skills:{},C=Object.values(g).filter(_=>_&&typeof _=="object"&&typeof _.id=="string"),D=_=>{for(let A of C)if(A.id===_)return A;return $(_)},be=new Set;for(let[,_]of this.nodes)for(let A of _.config?.skills||[])be.add(A);for(let _ of be){let A=D(_);if(typeof A?.middleware=="function")try{let x=await A.middleware();typeof x=="function"&&y.set(_,x)}catch{}}let S=this.entryPoint,z=[],ve=n?.recursionLimit??100,it=0;try{for(;S&&S!=="END";){if(++it>ve)throw new Error(`Workflow exceeded recursion limit (${ve}) \u2014 likely a cyclic conditional route. Set config.recursionLimit if you need a higher cap.`);let A=L(m,Ue);if(_e(A)){try{jt(A)}catch{}i.abort()}if(i.signal.aborted)return console.warn(`
|
|
37
|
+
\u{1F6D1} External stop requested \u2014 ending workflow.`),k.step("Workflow stopped externally"),{success:!0,state:p.getAll(),executionLog:z,stoppedExternally:!0};let x=this.nodes.get(S);if(!x)throw new Error(`Node '${S}' not found in graph`);let Te=JSON.stringify({sessionPath:m,sessionTimestamp:w,currentNode:S,createdAt:new Date().toISOString(),config:p.get("config")}),at=L(m,G);Xe(at,Te,"utf-8");let ke=p.get("config")?.paths?.output||re,ct=L(a,ke,G);et(L(a,ke),{recursive:!0});try{Xe(ct,Te,"utf-8")}catch{}let Ae=t.onPipelineProgress;if(typeof Ae=="function")try{Ae({cwd:a,sessionPath:m,sessionId:h,outputBase:p.get("config")?.paths?.output||re,currentNode:S})}catch{}let lt=(this.resolvedToolsMap||{})[S]||null;p.set("_currentNodeTools",lt);let ut=p.get("nodeConfigs")||{};p.set("_currentNodeConfig",ut[S]||{}),k.nodeStart(S);let xe=Date.now(),V=this.nodePrompts.get(S);if(!this._invokeAgent){let N=await Promise.resolve().then(()=>(q(),K));this._invokeAgent=N.invokeAgent}let dt=this._invokeAgent,le={},pt=x.config?.skills||[];for(let N of pt){let O=D(N);if(typeof O?.invokeAgentOptions=="function")try{let I=O.invokeAgentOptions(p.getAll(),{agentType:p.get("agentType"),nodeName:S});I&&typeof I=="object"&&(le={...le,...I})}catch(I){console.warn(`[graph] skill '${N}' invokeAgentOptions threw: ${I.message}`)}}let Ne=async(N,O,I={})=>{let P=dt(N,O,{...le,...I,signal:i.signal});return P.catch(()=>{}),i.signal.aborted?P:Promise.race([P,new Promise((W,F)=>{let M=()=>{setTimeout(()=>{let Y=new Error(`Strategy ignored AbortSignal \u2014 engine deadman fired after ${s}ms`);Y.name="AbortError",F(Y)},s)};i.signal.addEventListener("abort",M,{once:!0})})])},Oe={state:p,invokeAgent:async(N={},O={})=>{let I=O.prompt||"";if(V){let P=this._compiledPrompts.get(S);P||(P=Dt.compile(V,{noEscape:!0}),this._compiledPrompts.set(S,P));try{I=P(N)}catch(W){throw console.error(`\u274C Template rendering failed for node '${S}':`,W.message),new Error(`Template rendering failed: ${W.message}`,{cause:W})}}else if(!I)throw new Error(`No prompt template configured for node '${S}' and no prompt provided in options`);return Ne(I,{state:p.getAll(),images:O.images||[]},{model:O.model||p.get("model"),workspace:p.get("workspace"),schema:O.schema,...O,signal:i.signal})},_coreInvokeAgent:Ne,agent:e,nodeId:S,promptTemplate:V,getPromptTemplate:()=>V,...p.getAll()};try{let N=(x.config?.skills||[]).map(M=>y.get(M)).filter(Boolean),O=[...this.middleware,...N],I;O.length>0?I=await this._composeMiddleware(O,S,async()=>x.execute(Oe,p),p.getAll(),p):I=await x.execute(Oe,p);let P=Date.now()-xe;if(z.push({node:S,success:I.success,duration:P,timestamp:new Date().toISOString()}),!I.success){if(i.signal.aborted)return k.step("Workflow stopped externally"),{success:!0,state:p.getAll(),executionLog:z,stoppedExternally:!0};p.append("errors",{node:S,error:I.error});let M=x.config?.retries||0,Y=`${S}_retries`,X=p.getAll()[Y]||0;if(X<M){k.stepInfo(`Retrying (attempt ${X+1}/${M})`),p.update({[Y]:X+1,[`${S}_raw`]:I.raw});continue}throw k.nodeFailed(S,I.error,{duration:P}),new Error(`Node '${S}' failed after ${X} attempts: ${I.error}`)}p.update({[S]:I.output});let W=this._summarizeNodeOutput(S,I.output);k.nodeComplete(S,{duration:P,details:W});let F=this.edges.get(S);if(!F)S="END";else if(F.conditional){let M=F.routes(p.getAll());k.route(S,M),S=M}else S=F}catch(N){throw k.isInsideNode&&k.nodeFailed(S,N.message,{duration:Date.now()-xe}),p.set("failed",!0),p.set("failedAt",S),N}}k.graphComplete();let _={success:!0,state:p.getAll(),executionLog:z};return e&&typeof e.onComplete=="function"&&await e.onComplete(_),_}finally{if(e&&typeof e.cleanup=="function")try{await e.cleanup()}catch(_){console.warn(`[workflow] agent.cleanup() failed: ${_.message}`)}}}};var Ee=Symbol.for("@zibby/agent-workflow.nodes");globalThis[Ee]||(globalThis[Ee]=new Map);var Ie=globalThis[Ee];function Ht(o,e){Ie.set(o,e)}function ot(o){return Ie.get(o)}function $e(o){return Ie.has(o)}Ht("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(()=>(q(),K))).invokeAgent);let i=e.extraPromptInstructions||"Execute the task based on the current state.",s=Kt(i,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,i;for(;(i=t.exec(o))!==null;)r.add(i[1]);if(r.size===0)return o;let s=[],a=new Set;for(let n of r){let c=n.split(".")[0];if(a.has(c))continue;let l=n.split(".").reduce((u,m)=>u?.[m],e);if(l===void 0)continue;let d=typeof l=="string"?l:l?.raw??JSON.stringify(l,null,2),f=n.replace(/_/g," ").replace(/\b\w/g,u=>u.toUpperCase());s.push(`## ${f}
|
|
38
38
|
${d}`),n.includes(".")||a.add(c)}return s.length===0?o:`${o}
|
|
39
39
|
|
|
40
40
|
---
|
package/dist/graph.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var oe=Object.defineProperty;var K=(s,t)=>()=>(s&&(t=s(s=0)),t);var vt=(s,t)=>{for(var e in t)oe(s,e,{get:t[e],enumerable:!0})};var
|
|
1
|
+
var oe=Object.defineProperty;var K=(s,t)=>()=>(s&&(t=s(s=0)),t);var vt=(s,t)=>{for(var e in t)oe(s,e,{get:t[e],enumerable:!0})};var Tt,ne,q,I,X=K(()=>{Tt=()=>{},ne={debug:Tt,info:Tt,warn:(...s)=>console.warn("[workflow]",...s),error:(...s)=>console.error("[workflow]",...s)},q={impl:ne},I={debug:(...s)=>q.impl.debug?.(...s),info:(...s)=>q.impl.info?.(...s),warn:(...s)=>q.impl.warn?.(...s),error:(...s)=>q.impl.error?.(...s)}});var Lt=K(()=>{});var Wt={};vt(Wt,{clearSkills:()=>de,getAllSkills:()=>ue,getSkill:()=>ct,hasSkill:()=>le,listSkillIds:()=>pe,registerSkill:()=>ce});function ce(s){if(!s||typeof s.id!="string")throw new Error("Skill definition must include a string id");U.set(s.id,Object.freeze({...s}))}function ct(s){return U.get(s)||null}function le(s){return U.has(s)}function ue(){return new Map(U)}function pe(){return Array.from(U.keys())}function de(){U.clear()}var at,U,lt=K(()=>{at=Symbol.for("@zibby/agent-workflow.skills");globalThis[at]||(globalThis[at]=new Map);U=globalThis[at]});var pt={};vt(pt,{getAgentStrategy:()=>jt,invokeAgent:()=>me,listStrategies:()=>fe,registerStrategy:()=>he});function he(s){if(!s||typeof s.getName!="function"||typeof s.invoke!="function")throw new Error("strategy must implement getName() and invoke() (AgentStrategy shape)");let t=B.findIndex(e=>e.getName()===s.getName());t>=0?B[t]=s:B.push(s)}function fe(){return B.map(s=>s.getName())}function jt(s={}){let{state:t={},preferredAgent:e=null}=s,o=e||t.agentType||process.env.AGENT_TYPE;if(!o){let n=B.map(a=>a.getName()).join(", ")||"none registered";throw new Error(`No agent specified. Set agentType in state or AGENT_TYPE env var. Available: ${n}`)}I.debug(`[workflow] agent selection: requested=${o}`);let r=B.find(n=>n.getName()===o);if(!r){let n=B.map(a=>a.getName()).join(", ")||"none registered";throw new Error(`Unknown agent '${o}'. Available: ${n}`)}if(!r.canHandle(s))throw new Error(`Agent '${o}' is not available in this environment. Check credentials/environment.`);return I.debug(`[workflow] using agent: ${r.getName()}`),r}async function me(s,t={},e={}){let o=jt(t),r=t.state?.config||e.config||{},n=r.models||{},a=e.nodeName&&n[e.nodeName]||null,i=n.default||null,c=r.agent?.[o.name]?.model||null,l=a||i||c||e.model||null,p={...e,model:l,workspace:t.state?.workspace||e.workspace,schema:e.schema||t.schema,images:e.images||t.images||[],skills:e.skills||t.skills||[],config:r},d=s,g=p.skills||[];if(g.length>0&&!e.skipPromptFragments){let $=g.map(w=>{let N=ct(w)?.promptFragment;return typeof N=="function"?N():N}).filter(Boolean);$.length>0&&(d+=`
|
|
2
2
|
|
|
3
3
|
${$.join(`
|
|
4
4
|
|
|
@@ -11,7 +11,7 @@ PRIORITY OVERRIDE \u2014 THE FOLLOWING INSTRUCTIONS TAKE PRECEDENCE OVER ALL PRE
|
|
|
11
11
|
${_}
|
|
12
12
|
`),I.debug(`[workflow] prompt length: ${d.length} chars`),o.invoke(d,p)}var ut,B,dt=K(()=>{Lt();X();lt();ut=Symbol.for("@zibby/agent-workflow.strategies");globalThis[ut]||(globalThis[ut]=[]);B=globalThis[ut]});var re=new Set(["__proto__","constructor","prototype"]);function ot(s){if(re.has(s))throw new Error(`Invalid state key: "${s}"`)}var V=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){ot(t),this._history.push({...this._state}),this._state[t]=e}update(t){let e=Object.getOwnPropertyNames(t);for(let o of e)ot(o);this._history.push({...this._state});for(let o of e)this._state[o]=t[o]}append(t,e){ot(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]*?\}/),t.match(/\{[\s\S]*\}/)].filter(Boolean).map(r=>r[0]);for(let r of o)try{return this.validate(JSON.parse(r))}catch(n){if(!(n instanceof SyntaxError))throw n}return this.validate({result:t.trim()})}validate(t){let e=[];for(let[o,r]of Object.entries(this.schema)){if(r.required&&!(o in t)&&e.push(`Missing required field: ${o}`),o in t&&r.type){let n=typeof t[o];n!==r.type&&e.push(`Field '${o}' expected ${r.type}, got ${n}`)}if(r.validate&&o in t){let n=r.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}};X();import{writeFileSync as ht,readFileSync as Dt,existsSync as Ft,mkdirSync as ge}from"node:fs";import{join as ft,dirname as Se}from"node:path";import b from"chalk";var ie="__WORKFLOW_GRAPH_LOG__",Y=b.gray("\u2502"),ae=b.gray("\u250C"),
|
|
14
|
+
`)}`);return t}};X();import{writeFileSync as ht,readFileSync as Dt,existsSync as Ft,mkdirSync as ge}from"node:fs";import{join as ft,dirname as Se}from"node:path";import b from"chalk";var ie="__WORKFLOW_GRAPH_LOG__",Y=b.gray("\u2502"),ae=b.gray("\u250C"),Ot=b.gray("\u2514"),rt=b.green("\u25C6"),At=b.hex("#c084fc")("\u25C6"),kt=b.hex("#2dd4bf")("\u25C6"),nt=b.red("\u25C6"),Nt=`${Y} `,xt=2;function Pt(s){return s<1e3?`${s}ms`:`${(s/1e3).toFixed(1)}s`}function Rt(s,t){return(e,o,r)=>{if(typeof e!="string")return s(e,o,r);let n=process.stdout.columns||120,a="";for(let i=0;i<e.length;i++){let c=e[i];t.lineStart&&(a+=Nt,t.col=xt,t.lineStart=!1),c===`
|
|
15
15
|
`?(a+=c,t.lineStart=!0,t.col=0,t.inEsc=!1):c==="\x1B"?(t.inEsc=!0,a+=c):t.inEsc?(a+=c,(c>="A"&&c<="Z"||c>="a"&&c<="z")&&(t.inEsc=!1)):(t.col++,a+=c,t.col>=n&&(a+=`
|
|
16
16
|
${Nt}`,t.col=xt))}return s(a,o,r)}}var it=class{constructor(){this._currentNode=null,this._origStdoutWrite=null,this._origStderrWrite=null,this._emitWorkflowGraphMarkers=String(process.env.ZIBBY_EMIT_GRAPH_MARKERS||"").trim()==="1"||String(process.env.ZIBBY_WORKFLOW_GRAPH_LOG_MARKERS||"").trim()==="1"}get isInsideNode(){return this._currentNode!==null}_startIntercepting(){this._origStdoutWrite=process.stdout.write.bind(process.stdout),this._origStderrWrite=process.stderr.write.bind(process.stderr);let t={lineStart:!0,col:0,inEsc:!1},e={lineStart:!0,col:0,inEsc:!1};this._outState=t,this._errState=e,process.stdout.write=Rt(this._origStdoutWrite,t),process.stderr.write=Rt(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(`
|
|
@@ -24,14 +24,14 @@ ${Nt}`,t.col=xt))}return s(a,o,r)}}var it=class{constructor(){this._currentNode=
|
|
|
24
24
|
`)}stepInfo(t){this.step(t)}stepTool(t){this._origStdoutWrite?this._writeDot(At,t):process.stdout.write.bind(process.stdout)(`${Y} ${At} ${t}
|
|
25
25
|
`)}stepMemory(t){let e=b.hex("#2dd4bf")(t);this._origStdoutWrite?this._writeDot(kt,e):process.stdout.write.bind(process.stdout)(`${Y} ${kt} ${e}
|
|
26
26
|
`)}stepFail(t){this._origStdoutWrite?this._writeDot(nt,b.red(t)):process.stdout.write.bind(process.stdout)(`${Y} ${nt} ${b.red(t)}
|
|
27
|
-
`)}nodeStart(t){this._currentNode=t,this._emitGraphLogMarker({phase:"node_begin",node:t}),this._rawWrite(`${ae} ${t}`),this._startIntercepting()}nodeComplete(t,e={}){this._stopIntercepting();let{duration:o,details:r}=e;if(r)for(let a of r)this._rawWrite(`${rt} ${a}`);let n=o?b.dim(` ${Pt(o)}`):"";this._rawWrite(`${
|
|
27
|
+
`)}nodeStart(t){this._currentNode=t,this._emitGraphLogMarker({phase:"node_begin",node:t}),this._rawWrite(`${ae} ${t}`),this._startIntercepting()}nodeComplete(t,e={}){this._stopIntercepting();let{duration:o,details:r}=e;if(r)for(let a of r)this._rawWrite(`${rt} ${a}`);let n=o?b.dim(` ${Pt(o)}`):"";this._rawWrite(`${Ot} ${b.green("done")}${n}`),this._emitGraphLogMarker({phase:"node_end",node:t}),this._rawWrite("")}nodeFailed(t,e,o={}){this._stopIntercepting();let{duration:r}=o,n=r?b.dim(` ${Pt(r)}`):"";this._rawWrite(`${nt} ${b.red(e)}`),this._rawWrite(`${Ot} ${b.red("failed")}${n}`),this._emitGraphLogMarker({phase:"node_end",node:t}),this._rawWrite("")}route(t,e){this._rawWrite(b.dim(` ${t} \u2192 ${e}`)),this._rawWrite("")}graphComplete(){}},v=new it;var Q=".zibby/output",Ct="sessions",F=".session-info.json",Mt=".zibby-stop";var Bt=["CI_JOB_ID","GITHUB_RUN_ID","CIRCLE_WORKFLOW_ID","BUILD_ID"];var M=class{constructor(t){if(this.config=t,this.name=t.name,this.prompt=t.prompt,this.outputSchema=t.outputSchema,!this.outputSchema&&!t._isCustomCode)throw new Error(`Node '${this.name}' must define outputSchema (Zod schema). This defines the contract for what the node returns to state.`);this.isZodSchema=this.outputSchema&&typeof this.outputSchema._def<"u",this.parser=t.outputSchema&&!this.isZodSchema?new 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,r=d=>e&&typeof e.get=="function"?e.get(d):t?.[d];if(typeof this.customExecute=="function"){I.debug(`[workflow] node '${this.name}': custom execute (skipping LLM)`);try{let d=await this.customExecute(t);return typeof d=="object"&&d!==null&&d.success===!1?{success:!1,error:d.error||"Node execution failed",raw:d.raw||null}:this.isZodSchema?(I.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 I.error(`[workflow] node '${this.name}' failed: ${d.message}`),d.name==="ZodError"&&I.error(`Schema errors: ${JSON.stringify(d.issues||d.errors,null,2)}`),{success:!1,error:d.message,raw:null}}}let n=typeof this.prompt=="function"?this.prompt(o()):this.prompt,a=r("_skillHints");a&&(n=`${a}
|
|
28
28
|
|
|
29
|
-
${n}`);let i=o(),c=i.cwd||process.cwd(),l=i.sessionPath;try{if(l){let d=ft(l,F);if(Ft(d)){let _=JSON.parse(Dt(d,"utf-8"));_.currentNode=this.name,ht(d,JSON.stringify(_,null,2),"utf-8")}let g=ft(l,"..",F);if(Ft(g))try{let _=JSON.parse(Dt(g,"utf-8"));_.currentNode=this.name,ht(g,JSON.stringify(_,null,2),"utf-8")}catch{}}}catch(d){I.debug(`[workflow] could not update session info: ${d.message}`)}let p=null;for(let d=0;d<=this.retries;d++)try{I.debug(`[workflow] node '${this.name}' attempt ${d}`);let g=o().config||{},_=g.agents||{},$=this.config.agent??_[this.name]??null,w={state:o()};$&&(w.preferredAgent=$);let N={workspace:c,schema:this.isZodSchema?this.outputSchema:null,skills:this.config.skills||[],sessionPath:l,config:g,nodeName:this.name,timeout:this.config?.timeout||3e5},P=t?._coreInvokeAgent;P||(P=(await Promise.resolve().then(()=>(dt(),pt))).invokeAgent);let u=await P(n,w,N),S,E;if(typeof u=="string"?(S=u,E=null):u.structured?(S=u.raw||JSON.stringify(u.structured,null,2),E=u.structured):(S=u.raw||JSON.stringify(u,null,2),E=u.extracted||null),l)try{let h=ft(l,this.name,"raw_stream_output.txt");ge(Se(h),{recursive:!0}),ht(h,typeof S=="string"?S:JSON.stringify(S),"utf-8")}catch(h){I.debug(`[workflow] could not save raw output: ${h.message}`)}if(this.isZodSchema&&E){I.info(`[workflow] node '${this.name}': output validated: ${JSON.stringify(E,null,2)}`);let h=E;if(typeof this.onComplete=="function")try{h=await this.onComplete(o(),E)}catch(R){I.warn(`[workflow] onComplete hook failed: ${R.message}`)}return{success:!0,output:h,raw:S}}if(typeof this.onComplete=="function")try{return{success:!0,output:await this.onComplete(o(),{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 I.info(`[workflow] node '${this.name}': parsed output: ${JSON.stringify(h,null,2)}`),v.step("Output parsed"),{success:!0,output:h,raw:S}}return{success:!0,output:S,raw:S}}catch(g){p=g,d<this.retries&&I.info(`[workflow] node '${this.name}' failed, retrying (${d+1}/${this.retries})\u2026`)}return{success:!1,error:p.message,raw:null}}},tt=class extends M{constructor(t){super({...t,_isCustomCode:!0}),this.condition=t.condition}async execute(t,e){let o=e&&typeof e.getAll=="function"?e.getAll():t;return{success:!0,output:{nextNode:this.condition(o)},raw:null}}};X();var _e=2e3,we=600*1e3,ye=new Set(["completed","failed","canceled","timeout"]);function Ie(){let s=process.env.PROGRESS_API_URL;if(!s)throw new Error("Sub-graph dispatch requires PROGRESS_API_URL env var (set automatically on cloud runs). Sub-graphs are not supported in local in-process runs yet \u2014 deploy the parent and child to cloud.");return s.replace(/\/executions\/?$/,"")}function Ee(){let s=process.env.PROJECT_ID;if(!s)throw new Error("Sub-graph dispatch requires PROJECT_ID env var.");return s}function $e(){let s=process.env.PROJECT_API_TOKEN;if(!s)throw new Error("Sub-graph dispatch requires PROJECT_API_TOKEN env var.");return s}function be(){return process.env.EXECUTION_ID||null}function ve(s,t){return t==null?s:typeof t=="function"?t(s):typeof t=="string"?t.split(".").reduce((e,o)=>e==null?e:e[o],s):s}async function Ut(s,t={}){if(!s||typeof s!="string")throw new Error("dispatchSubgraph: workflowName (string) is required");let e=Ie(),o=Ee(),r=$e(),n=be(),a=`${e}/projects/${encodeURIComponent(o)}/workflows/${encodeURIComponent(s)}/trigger`,i={input:t.input||{},...n?{parentExecutionId:n}:{},...t.conversationId?{conversationId:t.conversationId}:{}};I.info(`[sub-graph] dispatching '${s}' (${t.async?"async":"sync"}) from parent ${n||"<none>"}`);let c=await fetch(a,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${r}`},body:JSON.stringify(i)});if(!c.ok){let u=null,S="";try{u=await c.json(),S=u?.error||u?.message||JSON.stringify(u)}catch{S=await c.text().catch(()=>"")}if(c.status===429){let h=u?.quotaInfo||{},R=new Error(`Sub-graph '${s}' blocked by execution quota (${h.used??"?"}/${h.limit??"?"} on plan ${h.planId||"unknown"}). Sub-workflow runs count toward the same monthly cap as user-triggered runs.`);throw R.code="SUBGRAPH_QUOTA_EXCEEDED",R.status=429,R.subgraph=s,R.quotaInfo=h,R}if(c.status===400){let h=new Error(`Sub-graph '${s}' rejected input: ${S}`);throw h.code="SUBGRAPH_INVALID_INPUT",h.status=400,h.subgraph=s,h.validationErrors=u?.validationErrors||null,h.missing=u?.missing||null,h}let E=new Error(`Sub-graph '${s}' trigger rejected (${c.status}): ${S}`);throw E.code="SUBGRAPH_TRIGGER_FAILED",E.status=c.status,E.subgraph=s,E}let l=await c.json(),p=l?.data?.jobId||l?.jobId;if(!p)throw new Error(`Sub-graph '${s}' trigger returned no jobId: ${JSON.stringify(l).slice(0,200)}`);if(t.async)return I.info(`[sub-graph] async dispatch of '${s}' \u2192 jobId=${p} (not waiting)`),{jobId:p,status:"accepted",workflow:s};let d=Number.isFinite(t.timeoutMs)?t.timeoutMs:we,g=Number.isFinite(t.pollIntervalMs)?t.pollIntervalMs:_e,_=`${e}/executions/${encodeURIComponent(p)}`,$=Date.now()+d,w="accepted",N=0;for(;Date.now()<$;){await new Promise(h=>setTimeout(h,g)),N+=1;let u=await fetch(_,{headers:{Authorization:`Bearer ${r}`}});if(!u.ok){if(u.status>=500){I.warn(`[sub-graph] status poll for ${p} returned ${u.status}, will retry`);continue}throw new Error(`Sub-graph status poll failed for ${p}: ${u.status}`)}let S=await u.json(),E=S?.data||S?.execution||S;if(w=E?.status||w,ye.has(w)){if(w!=="completed"){let W=new Error(`Sub-graph '${s}' (${p}) ended in status '${w}'`);throw W.subgraphJobId=p,W.subgraphStatus=w,W}let h=E?.finalState||E?.state||{},R=ve(h,t.output);return I.info(`[sub-graph] '${s}' (${p}) completed after ${N} polls`),R}}let P=new Error(`Sub-graph '${s}' (${p}) timed out after ${Math.round(d/1e3)}s (last status: ${w})`);throw P.subgraphJobId=p,P.subgraphStatus=w,P}import{existsSync as Jt,readFileSync as
|
|
29
|
+
${n}`);let i=o(),c=i.cwd||process.cwd(),l=i.sessionPath;try{if(l){let d=ft(l,F);if(Ft(d)){let _=JSON.parse(Dt(d,"utf-8"));_.currentNode=this.name,ht(d,JSON.stringify(_,null,2),"utf-8")}let g=ft(l,"..",F);if(Ft(g))try{let _=JSON.parse(Dt(g,"utf-8"));_.currentNode=this.name,ht(g,JSON.stringify(_,null,2),"utf-8")}catch{}}}catch(d){I.debug(`[workflow] could not update session info: ${d.message}`)}let p=null;for(let d=0;d<=this.retries;d++)try{I.debug(`[workflow] node '${this.name}' attempt ${d}`);let g=o().config||{},_=g.agents||{},$=this.config.agent??_[this.name]??null,w={state:o()};$&&(w.preferredAgent=$);let N={workspace:c,schema:this.isZodSchema?this.outputSchema:null,skills:this.config.skills||[],sessionPath:l,config:g,nodeName:this.name,timeout:this.config?.timeout||3e5},P=t?._coreInvokeAgent;P||(P=(await Promise.resolve().then(()=>(dt(),pt))).invokeAgent);let u=await P(n,w,N),S,E;if(typeof u=="string"?(S=u,E=null):u.structured?(S=u.raw||JSON.stringify(u.structured,null,2),E=u.structured):(S=u.raw||JSON.stringify(u,null,2),E=u.extracted||null),l)try{let h=ft(l,this.name,"raw_stream_output.txt");ge(Se(h),{recursive:!0}),ht(h,typeof S=="string"?S:JSON.stringify(S),"utf-8")}catch(h){I.debug(`[workflow] could not save raw output: ${h.message}`)}if(this.isZodSchema&&E){I.info(`[workflow] node '${this.name}': output validated: ${JSON.stringify(E,null,2)}`);let h=E;if(typeof this.onComplete=="function")try{h=await this.onComplete(o(),E)}catch(R){I.warn(`[workflow] onComplete hook failed: ${R.message}`)}return{success:!0,output:h,raw:S}}if(typeof this.onComplete=="function")try{return{success:!0,output:await this.onComplete(o(),{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 I.info(`[workflow] node '${this.name}': parsed output: ${JSON.stringify(h,null,2)}`),v.step("Output parsed"),{success:!0,output:h,raw:S}}return{success:!0,output:S,raw:S}}catch(g){p=g,d<this.retries&&I.info(`[workflow] node '${this.name}' failed, retrying (${d+1}/${this.retries})\u2026`)}return{success:!1,error:p.message,raw:null}}},tt=class extends M{constructor(t){super({...t,_isCustomCode:!0}),this.condition=t.condition}async execute(t,e){let o=e&&typeof e.getAll=="function"?e.getAll():t;return{success:!0,output:{nextNode:this.condition(o)},raw:null}}};X();var _e=2e3,we=600*1e3,ye=new Set(["completed","failed","canceled","timeout"]);function Ie(){let s=process.env.PROGRESS_API_URL;if(!s)throw new Error("Sub-graph dispatch requires PROGRESS_API_URL env var (set automatically on cloud runs). Sub-graphs are not supported in local in-process runs yet \u2014 deploy the parent and child to cloud.");return s.replace(/\/executions\/?$/,"")}function Ee(){let s=process.env.PROJECT_ID;if(!s)throw new Error("Sub-graph dispatch requires PROJECT_ID env var.");return s}function $e(){let s=process.env.PROJECT_API_TOKEN;if(!s)throw new Error("Sub-graph dispatch requires PROJECT_API_TOKEN env var.");return s}function be(){return process.env.EXECUTION_ID||null}function ve(s,t){return t==null?s:typeof t=="function"?t(s):typeof t=="string"?t.split(".").reduce((e,o)=>e==null?e:e[o],s):s}async function Ut(s,t={}){if(!s||typeof s!="string")throw new Error("dispatchSubgraph: workflowName (string) is required");let e=Ie(),o=Ee(),r=$e(),n=be(),a=`${e}/projects/${encodeURIComponent(o)}/workflows/${encodeURIComponent(s)}/trigger`,i={input:t.input||{},...n?{parentExecutionId:n}:{},...t.conversationId?{conversationId:t.conversationId}:{}};I.info(`[sub-graph] dispatching '${s}' (${t.async?"async":"sync"}) from parent ${n||"<none>"}`);let c=await fetch(a,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${r}`},body:JSON.stringify(i)});if(!c.ok){let u=null,S="";try{u=await c.json(),S=u?.error||u?.message||JSON.stringify(u)}catch{S=await c.text().catch(()=>"")}if(c.status===429){let h=u?.quotaInfo||{},R=new Error(`Sub-graph '${s}' blocked by execution quota (${h.used??"?"}/${h.limit??"?"} on plan ${h.planId||"unknown"}). Sub-workflow runs count toward the same monthly cap as user-triggered runs.`);throw R.code="SUBGRAPH_QUOTA_EXCEEDED",R.status=429,R.subgraph=s,R.quotaInfo=h,R}if(c.status===400){let h=new Error(`Sub-graph '${s}' rejected input: ${S}`);throw h.code="SUBGRAPH_INVALID_INPUT",h.status=400,h.subgraph=s,h.validationErrors=u?.validationErrors||null,h.missing=u?.missing||null,h}let E=new Error(`Sub-graph '${s}' trigger rejected (${c.status}): ${S}`);throw E.code="SUBGRAPH_TRIGGER_FAILED",E.status=c.status,E.subgraph=s,E}let l=await c.json(),p=l?.data?.jobId||l?.jobId;if(!p)throw new Error(`Sub-graph '${s}' trigger returned no jobId: ${JSON.stringify(l).slice(0,200)}`);if(t.async)return I.info(`[sub-graph] async dispatch of '${s}' \u2192 jobId=${p} (not waiting)`),{jobId:p,status:"accepted",workflow:s};let d=Number.isFinite(t.timeoutMs)?t.timeoutMs:we,g=Number.isFinite(t.pollIntervalMs)?t.pollIntervalMs:_e,_=`${e}/executions/${encodeURIComponent(p)}`,$=Date.now()+d,w="accepted",N=0;for(;Date.now()<$;){await new Promise(h=>setTimeout(h,g)),N+=1;let u=await fetch(_,{headers:{Authorization:`Bearer ${r}`}});if(!u.ok){if(u.status>=500){I.warn(`[sub-graph] status poll for ${p} returned ${u.status}, will retry`);continue}throw new Error(`Sub-graph status poll failed for ${p}: ${u.status}`)}let S=await u.json(),E=S?.data||S?.execution||S;if(w=E?.status||w,ye.has(w)){if(w!=="completed"){let W=new Error(`Sub-graph '${s}' (${p}) ended in status '${w}'`);throw W.subgraphJobId=p,W.subgraphStatus=w,W}let h=E?.finalState||E?.state||{},R=ve(h,t.output);return I.info(`[sub-graph] '${s}' (${p}) completed after ${N} polls`),R}}let P=new Error(`Sub-graph '${s}' (${p}) timed out after ${Math.round(d/1e3)}s (last status: ${w})`);throw P.subgraphJobId=p,P.subgraphStatus=w,P}import{existsSync as Jt,readFileSync as Te}from"node:fs";import{join as mt,dirname as Yt}from"node:path";var et=class{static async loadContext(t,e,o={}){let r={},n=o.filenames||["CONTEXT.md","AGENTS.md"];if(t){let i=Yt(mt(e,t));for(let c of n){let l=await this.findAndMergeContextFiles(c,i,e);if(l){let p=c.replace(/\.[^.]+$/,"").toLowerCase();r[p]=l}}}let a=o.discovery||{};for(let[i,c]of Object.entries(a))try{let l=mt(e,c);Jt(l)&&(r[i]=await this.loadFile(l))}catch(l){console.warn(`[workflow] could not load context '${i}' from '${c}': ${l.message}`)}return r}static async findAndMergeContextFiles(t,e,o){let r=[],n=e;for(;n.startsWith(o);){let a=mt(n,t);if(Jt(a))try{r.unshift(await this.loadFile(a))}catch(c){console.warn(`[workflow] could not load ${t} from ${a}: ${c.message}`)}let i=Yt(n);if(i===n)break;n=i}return r.length===0?null:r.every(a=>typeof a=="string")?r.join(`
|
|
30
30
|
|
|
31
31
|
---
|
|
32
32
|
|
|
33
|
-
`):r.every(a=>typeof a=="object")?Object.assign({},...r):r[r.length-1]}static async loadFile(t){let e=
|
|
33
|
+
`):r.every(a=>typeof a=="object")?Object.assign({},...r):r[r.length-1]}static async loadFile(t){let e=Te(t,"utf-8");if(t.endsWith(".json"))return JSON.parse(e);if(t.endsWith(".js")||t.endsWith(".mjs")){let{pathToFileURL:o}=await import("url"),r=await import(o(t).href);return r.default||r}return e}};import{mkdirSync as Kt,existsSync as gt,writeFileSync as Gt,unlinkSync as Oe}from"node:fs";import{join as L,resolve as Vt}from"node:path";import{config as Ae}from"dotenv";import{zodToJsonSchema as Zt}from"zod-to-json-schema";import ke from"handlebars";function Ne({traceFrom:s,sessionId:t,sessionPath:e,idSource:o,mkdirFresh:r}){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=${s} pid=${process.pid} ppid=${a} sessionId=${t} source=${o} mkdir=${r?"yes":"no"} path=${e}`;if(console.log(i),process.env.ZIBBY_TRACE_SESSION==="1"||process.env.ZIBBY_TRACE_SESSION==="true"){let p=(new Error("session trace").stack||"").split(`
|
|
34
34
|
`).slice(2,14).join(`
|
|
35
35
|
`);console.log(`[zibby:session] stack (${s}):
|
|
36
|
-
${p}`)}}function xe(){return process.env.ZIBBY_TRUST_SESSION_ENV==="1"||process.env.ZIBBY_TRUST_SESSION_ENV==="true"||process.env.ZIBBY_KEEP_SESSION_ENV==="1"||process.env.ZIBBY_KEEP_SESSION_ENV==="true"}function Pe(){if(!(process.env.ZIBBY_PIN_SESSION_PATH==="1"||process.env.ZIBBY_PIN_SESSION_PATH==="true"))return;let t=process.env.ZIBBY_SESSION_PATH;if(!(t==null||String(t).trim()===""))try{return Vt(String(t).trim())}catch{return String(t).trim()}}function Re(){xe()||(delete process.env.ZIBBY_SESSION_PATH,delete process.env.ZIBBY_SESSION_ID)}function Ce({sessionPath:s,sessionId:t}){s&&typeof s=="string"&&(process.env.ZIBBY_SESSION_PATH=s),t!=null&&String(t).trim()!==""&&(process.env.ZIBBY_SESSION_ID=String(t).trim())}function Me(s={}){let t=Bt.map(n=>process.env[n]).find(Boolean),e=Math.random().toString(36).slice(2,6),o=t||`${Date.now()}_${e}`,r=s.paths?.sessionPrefix;return r?`${r}_${o}`:o}function Be({cwd:s=process.cwd(),config:t={},initialState:e={},traceFrom:o="resolveWorkflowSession"}={}){let r=e.sessionPath,n=e.sessionTimestamp,a="initialState.sessionPath";if(!r&&process.env.ZIBBY_SESSION_PATH)try{let l=Vt(String(process.env.ZIBBY_SESSION_PATH));l&&(r=l,a="ZIBBY_SESSION_PATH")}catch{}let i;if(r)i=String(r).split(/[/\\]/).filter(Boolean).pop(),n==null&&(n=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=t.sessionId!=null?String(t.sessionId).trim():"";d&&d!=="last"?(i=d,a="config.sessionId"):(i=Me(t),a="generated")}n=n??Date.now();let p=t.paths?.output||Q;r=L(s,p,Ct,i)}let c=!gt(r);return c&&Kt(r,{recursive:!0}),(c||a!=="initialState.sessionPath")&&Ne({traceFrom:o,sessionId:i,sessionPath:r,idSource:a,mkdirFresh:c}),Ce({sessionPath:r,sessionId:i}),{sessionPath:r,sessionId:i,sessionTimestamp:n}}var Ht=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.inputSchema=t.inputSchema||null,this.contextSchema=t.contextSchema||null,this.nodePrompts=new Map,this.nodeOptions=new Map,this._invokeAgent=t.invokeAgent||null,this._compiledPrompts=new Map}setInputSchema(t){return this.inputSchema=t,this}setContextSchema(t){return this.contextSchema=t,this}setStateSchema(t){return this.stateSchema=t,this}getInputSchema(){return this.inputSchema}getContextSchema(){return this.contextSchema}getStateSchema(){return this.stateSchema}_runtimeSchema(){if(this.inputSchema&&this.contextSchema)try{return this.inputSchema.merge(this.contextSchema)}catch{}return this.inputSchema&&!this.contextSchema?this.inputSchema:this.stateSchema}addNode(t,e,o={}){if(!(e instanceof M)&&e&&typeof e=="object"&&typeof e.workflow=="string"){let n=e,a={name:t,_isCustomCode:!0,retries:n.retries,onComplete:n.onComplete,execute:async c=>{let l=c?.state&&typeof c.state.getAll=="function"?c.state.getAll():c,p;return typeof n.input=="function"?p=n.input(l):n.input&&typeof n.input=="object"?p=n.input:p={},Ut(n.workflow,{input:p,async:n.async===!0,conversationId:typeof n.conversationId=="function"?n.conversationId(l):n.conversationId,output:n.output,timeoutMs:n.timeoutMs,pollIntervalMs:n.pollIntervalMs})}},i=new M(a);return i.name=t,this.nodes.set(t,i),o.prompt&&this.nodePrompts.set(t,o.prompt),Object.keys(o).length>0&&this.nodeOptions.set(t,o),this}let r=e instanceof M?e:new M(e);return r.name=t,this.nodes.set(t,r),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 tt({...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,r,n){let a=o;for(let i=t.length-1;i>=0;i--){let c=t[i],l=a;a=()=>c(e,l,r,n)}return a()}serialize(){let t=[],e={};for(let[l,p]of this.nodes){let d=this.nodeTypeMap.get(l)||l;t.push({id:l,type:d,data:{nodeType:d,label:l}});let g={};p._isCustomCode&&typeof p.execute=="function"&&(g.customCode=p.execute.toString());let _=this.nodePrompts.get(l);if(_&&(g.prompt=_),typeof p.customExecute=="function"&&(g.executeCode=p.customExecute.toString()),p.outputSchema)try{if(typeof p.outputSchema._def<"u"){let w=
|
|
37
|
-
\u{1F6D1} External stop requested \u2014 ending workflow.`),v.step("Workflow stopped externally"),{success:!0,state:u.getAll(),executionLog:
|
|
36
|
+
${p}`)}}function xe(){return process.env.ZIBBY_TRUST_SESSION_ENV==="1"||process.env.ZIBBY_TRUST_SESSION_ENV==="true"||process.env.ZIBBY_KEEP_SESSION_ENV==="1"||process.env.ZIBBY_KEEP_SESSION_ENV==="true"}function Pe(){if(!(process.env.ZIBBY_PIN_SESSION_PATH==="1"||process.env.ZIBBY_PIN_SESSION_PATH==="true"))return;let t=process.env.ZIBBY_SESSION_PATH;if(!(t==null||String(t).trim()===""))try{return Vt(String(t).trim())}catch{return String(t).trim()}}function Re(){xe()||(delete process.env.ZIBBY_SESSION_PATH,delete process.env.ZIBBY_SESSION_ID)}function Ce({sessionPath:s,sessionId:t}){s&&typeof s=="string"&&(process.env.ZIBBY_SESSION_PATH=s),t!=null&&String(t).trim()!==""&&(process.env.ZIBBY_SESSION_ID=String(t).trim())}function Me(s={}){let t=Bt.map(n=>process.env[n]).find(Boolean),e=Math.random().toString(36).slice(2,6),o=t||`${Date.now()}_${e}`,r=s.paths?.sessionPrefix;return r?`${r}_${o}`:o}function Be({cwd:s=process.cwd(),config:t={},initialState:e={},traceFrom:o="resolveWorkflowSession"}={}){let r=e.sessionPath,n=e.sessionTimestamp,a="initialState.sessionPath";if(!r&&process.env.ZIBBY_SESSION_PATH)try{let l=Vt(String(process.env.ZIBBY_SESSION_PATH));l&&(r=l,a="ZIBBY_SESSION_PATH")}catch{}let i;if(r)i=String(r).split(/[/\\]/).filter(Boolean).pop(),n==null&&(n=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=t.sessionId!=null?String(t.sessionId).trim():"";d&&d!=="last"?(i=d,a="config.sessionId"):(i=Me(t),a="generated")}n=n??Date.now();let p=t.paths?.output||Q;r=L(s,p,Ct,i)}let c=!gt(r);return c&&Kt(r,{recursive:!0}),(c||a!=="initialState.sessionPath")&&Ne({traceFrom:o,sessionId:i,sessionPath:r,idSource:a,mkdirFresh:c}),Ce({sessionPath:r,sessionId:i}),{sessionPath:r,sessionId:i,sessionTimestamp:n}}var Ht=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.inputSchema=t.inputSchema||null,this.contextSchema=t.contextSchema||null,this.nodePrompts=new Map,this.nodeOptions=new Map,this._invokeAgent=t.invokeAgent||null,this._compiledPrompts=new Map}setInputSchema(t){return this.inputSchema=t,this}setContextSchema(t){return this.contextSchema=t,this}setStateSchema(t){return this.stateSchema=t,this}getInputSchema(){return this.inputSchema}getContextSchema(){return this.contextSchema}getStateSchema(){return this.stateSchema}_runtimeSchema(){if(this.inputSchema&&this.contextSchema)try{return this.inputSchema.merge(this.contextSchema)}catch{}return this.inputSchema&&!this.contextSchema?this.inputSchema:this.stateSchema}addNode(t,e,o={}){if(!(e instanceof M)&&e&&typeof e=="object"&&typeof e.workflow=="string"){let n=e,a={name:t,_isCustomCode:!0,retries:n.retries,onComplete:n.onComplete,execute:async c=>{let l=c?.state&&typeof c.state.getAll=="function"?c.state.getAll():c,p;return typeof n.input=="function"?p=n.input(l):n.input&&typeof n.input=="object"?p=n.input:p={},Ut(n.workflow,{input:p,async:n.async===!0,conversationId:typeof n.conversationId=="function"?n.conversationId(l):n.conversationId,output:n.output,timeoutMs:n.timeoutMs,pollIntervalMs:n.pollIntervalMs})}},i=new M(a);return i.name=t,this.nodes.set(t,i),o.prompt&&this.nodePrompts.set(t,o.prompt),Object.keys(o).length>0&&this.nodeOptions.set(t,o),this}let r=e instanceof M?e:new M(e);return r.name=t,this.nodes.set(t,r),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 tt({...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,r,n){let a=o;for(let i=t.length-1;i>=0;i--){let c=t[i],l=a;a=()=>c(e,l,r,n)}return a()}serialize(){let t=[],e={};for(let[l,p]of this.nodes){let d=this.nodeTypeMap.get(l)||l;t.push({id:l,type:d,data:{nodeType:d,label:l}});let g={};p._isCustomCode&&typeof p.execute=="function"&&(g.customCode=p.execute.toString());let _=this.nodePrompts.get(l);if(_&&(g.prompt=_),typeof p.customExecute=="function"&&(g.executeCode=p.customExecute.toString()),p.outputSchema)try{if(typeof p.outputSchema._def<"u"){let w=Zt(p.outputSchema,{target:"openApi3"});g.outputSchema={jsonSchema:w,variables:this._flattenJsonSchemaToVariables(w)}}else g.outputSchema={schema:p.outputSchema}}catch(w){console.warn(`[workflow] failed to convert schema for ${l}:`,w.message)}let $=(this.resolvedToolsMap||{})[l];$?.toolIds&&(g.tools=$.toolIds),Object.keys(g).length>0&&(e[l]=g)}let o=[];for(let[l,p]of this.edges)if(typeof p=="string")o.push({source:l,target:p});else if(p.conditional){let d=this.conditionalCodeMap.get(l)||p.routes.toString(),g=this._inferConditionalTargets(p.routes),_=p.labels||{};for(let $ of g){let w={source:l,target:$,data:{conditionalCode:d}};_[$]&&(w.label=_[$]),o.push(w)}}let r=l=>{if(!l)return null;try{return Zt(l,{target:"openApi3"})}catch{return null}},n=this._runtimeSchema(),a=r(n||this.stateSchema),i=r(this.inputSchema),c=r(this.contextSchema);return{nodes:t,edges:o,nodeConfigs:e,stateSchema:a,inputSchema:i,contextSchema:c}}_inferConditionalTargets(t){let e=t.toString(),o=new Set,r=/return\s+['"]([^'"]+)['"]/g,n;for(;(n=r.exec(e))!==null;)o.add(n[1]);return[...o]}_flattenJsonSchemaToVariables(t,e=""){let o=t;if(t.$ref&&t.definitions){let r=t.$ref.replace("#/definitions/","");o=t.definitions[r]||t}return this._flattenSchema(o,e)}_flattenSchema(t,e=""){if(!t||typeof t!="object")return[];let o=[],r=t.properties||{},n=t.required||[];for(let[a,i]of Object.entries(r)){let c=e?`${e}.${a}`:a;o.push({path:c,type:i.type||"unknown",label:i.description||this._formatLabel(a),optional:!n.includes(a)}),i.type==="object"&&i.properties&&o.push(...this._flattenSchema(i,c)),i.type==="array"&&i.items?.type==="object"&&i.items.properties&&o.push(...this._flattenSchema(i.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[r,n]of Object.entries(e))if(!(r==="success"||r==="raw"||r==="nextNode")){if(typeof n=="string"&&n.length<=80)o.push(`${r}: ${n}`);else if(Array.isArray(n)){let a=n.length,i=n.filter(l=>l?.passed===!0).length,c=n.some(l=>l?.passed!==void 0);o.push(c?`${r}: ${i}/${a} passed${a-i?`, ${a-i} failed`:""}`:`${r}: ${a} items`)}if(o.length>=4)break}return o}async run(t,e={},o={}){if(!this.entryPoint)throw new Error("No entry point set for graph");let r=new AbortController;o.signal&&(o.signal.aborted?r.abort():o.signal.addEventListener("abort",()=>r.abort(),{once:!0}));let n=o.strategyAbortTimeoutMs??e.config?.strategyAbortTimeoutMs??5e3,a=e.cwd||process.cwd();Ae({path:L(a,".env")});let i=e.config||{};if(!i||Object.keys(i).length===0)try{let m=L(a,".zibby.config.js");gt(m)&&(i=(await import(m)).default||{})}catch{}process.env.EXECUTION_ID&&!i.agent?.strictMode&&(i.agent={...i.agent,strictMode:!0});let c=e.agentType;if(!c){let m=i?.agent;m?.provider?c=m.provider:m?.gemini?c="gemini":m?.claude?c="claude":m?.cursor?c="cursor":m?.codex?c="codex":c=process.env.AGENT_TYPE||"cursor"}let l=e.contextConfig||t?.config?.contextConfig||t?.config?.context||i?.context||{},p=this._runtimeSchema();if(p){let m=p.safeParse(e);if(!m.success){let T=m.error.issues.map(O=>`${O.path.join(".")}: ${O.message}`);throw console.error("\u274C Initial state validation failed:"),T.forEach(O=>console.error(` - ${O}`)),new Error(`State validation failed: ${T.join(", ")}`)}v.step("State validated against schema")}let d=Pe(),g=e.sessionPath||d;g||Re();let{sessionPath:_,sessionTimestamp:$,sessionId:w}=Be({cwd:a,config:i,traceFrom:"WorkflowGraph.run",initialState:{sessionPath:g,sessionTimestamp:e.sessionTimestamp}});v.step(`Session ${w}`);let N=await et.loadContext(e.specPath||"",a,l);Object.keys(N).length>0&&v.step(`Context loaded: ${Object.keys(N).join(", ")}`);let P=e.outputPath;!P&&e.specPath&&(t?.calculateOutputPath?P=t.calculateOutputPath(e.specPath):console.warn(`\u26A0\uFE0F outputPath not resolved (specPath=${e.specPath})`));let u=new V({...e,config:i,agentType:c,outputPath:P,sessionPath:_,sessionTimestamp:$,context:N,resolvedTools:this.resolvedToolsMap||{},_signal:r.signal}),S=new Map;try{await import("@zibby/skills")}catch{}let{getSkill:E}=await Promise.resolve().then(()=>(lt(),Wt)),h=i.skills&&typeof i.skills=="object"?i.skills:{},R=Object.values(h).filter(m=>m&&typeof m=="object"&&typeof m.id=="string"),W=m=>{for(let T of R)if(T.id===m)return T;return E(m)},St=new Set;for(let[,m]of this.nodes)for(let T of m.config?.skills||[])St.add(T);for(let m of St){let T=W(m);if(typeof T?.middleware=="function")try{let O=await T.middleware();typeof O=="function"&&S.set(m,O)}catch{}}let f=this.entryPoint,G=[],_t=i?.recursionLimit??100,zt=0;try{for(;f&&f!=="END";){if(++zt>_t)throw new Error(`Workflow exceeded recursion limit (${_t}) \u2014 likely a cyclic conditional route. Set config.recursionLimit if you need a higher cap.`);let T=L(_,Mt);if(gt(T)){try{Oe(T)}catch{}r.abort()}if(r.signal.aborted)return console.warn(`
|
|
37
|
+
\u{1F6D1} External stop requested \u2014 ending workflow.`),v.step("Workflow stopped externally"),{success:!0,state:u.getAll(),executionLog:G,stoppedExternally:!0};let O=this.nodes.get(f);if(!O)throw new Error(`Node '${f}' not found in graph`);let wt=JSON.stringify({sessionPath:_,sessionTimestamp:$,currentNode:f,createdAt:new Date().toISOString(),config:u.get("config")}),qt=L(_,F);Gt(qt,wt,"utf-8");let yt=u.get("config")?.paths?.output||Q,Xt=L(a,yt,F);Kt(L(a,yt),{recursive:!0});try{Gt(Xt,wt,"utf-8")}catch{}let It=e.onPipelineProgress;if(typeof It=="function")try{It({cwd:a,sessionPath:_,sessionId:w,outputBase:u.get("config")?.paths?.output||Q,currentNode:f})}catch{}let Qt=(this.resolvedToolsMap||{})[f]||null;u.set("_currentNodeTools",Qt);let te=u.get("nodeConfigs")||{};u.set("_currentNodeConfig",te[f]||{}),v.nodeStart(f);let Et=Date.now(),Z=this.nodePrompts.get(f);if(!this._invokeAgent){let A=await Promise.resolve().then(()=>(dt(),pt));this._invokeAgent=A.invokeAgent}let ee=this._invokeAgent,st={},se=O.config?.skills||[];for(let A of se){let k=W(A);if(typeof k?.invokeAgentOptions=="function")try{let y=k.invokeAgentOptions(u.getAll(),{agentType:u.get("agentType"),nodeName:f});y&&typeof y=="object"&&(st={...st,...y})}catch(y){console.warn(`[graph] skill '${A}' invokeAgentOptions threw: ${y.message}`)}}let $t=async(A,k,y={})=>{let x=ee(A,k,{...st,...y,signal:r.signal});return x.catch(()=>{}),r.signal.aborted?x:Promise.race([x,new Promise((j,D)=>{let C=()=>{setTimeout(()=>{let J=new Error(`Strategy ignored AbortSignal \u2014 engine deadman fired after ${n}ms`);J.name="AbortError",D(J)},n)};r.signal.addEventListener("abort",C,{once:!0})})])},bt={state:u,invokeAgent:async(A={},k={})=>{let y=k.prompt||"";if(Z){let x=this._compiledPrompts.get(f);x||(x=ke.compile(Z,{noEscape:!0}),this._compiledPrompts.set(f,x));try{y=x(A)}catch(j){throw console.error(`\u274C Template rendering failed for node '${f}':`,j.message),new Error(`Template rendering failed: ${j.message}`,{cause:j})}}else if(!y)throw new Error(`No prompt template configured for node '${f}' and no prompt provided in options`);return $t(y,{state:u.getAll(),images:k.images||[]},{model:k.model||u.get("model"),workspace:u.get("workspace"),schema:k.schema,...k,signal:r.signal})},_coreInvokeAgent:$t,agent:t,nodeId:f,promptTemplate:Z,getPromptTemplate:()=>Z,...u.getAll()};try{let A=(O.config?.skills||[]).map(C=>S.get(C)).filter(Boolean),k=[...this.middleware,...A],y;k.length>0?y=await this._composeMiddleware(k,f,async()=>O.execute(bt,u),u.getAll(),u):y=await O.execute(bt,u);let x=Date.now()-Et;if(G.push({node:f,success:y.success,duration:x,timestamp:new Date().toISOString()}),!y.success){if(r.signal.aborted)return v.step("Workflow stopped externally"),{success:!0,state:u.getAll(),executionLog:G,stoppedExternally:!0};u.append("errors",{node:f,error:y.error});let C=O.config?.retries||0,J=`${f}_retries`,H=u.getAll()[J]||0;if(H<C){v.stepInfo(`Retrying (attempt ${H+1}/${C})`),u.update({[J]:H+1,[`${f}_raw`]:y.raw});continue}throw v.nodeFailed(f,y.error,{duration:x}),new Error(`Node '${f}' failed after ${H} attempts: ${y.error}`)}u.update({[f]:y.output});let j=this._summarizeNodeOutput(f,y.output);v.nodeComplete(f,{duration:x,details:j});let D=this.edges.get(f);if(!D)f="END";else if(D.conditional){let C=D.routes(u.getAll());v.route(f,C),f=C}else f=D}catch(A){throw v.isInsideNode&&v.nodeFailed(f,A.message,{duration:Date.now()-Et}),u.set("failed",!0),u.set("failedAt",f),A}}v.graphComplete();let m={success:!0,state:u.getAll(),executionLog:G};return t&&typeof t.onComplete=="function"&&await t.onComplete(m),m}finally{if(t&&typeof t.cleanup=="function")try{await t.cleanup()}catch(m){console.warn(`[workflow] agent.cleanup() failed: ${m.message}`)}}}};export{Ht as WorkflowGraph,Re as clearInheritedSessionEnvForFreshRun,Me as generateWorkflowSessionId,Pe as readPinnedSessionPathFromEnv,Be as resolveWorkflowSession,xe as shouldTrustInheritedSessionEnv,Ce as syncProcessEnvToSession};
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var Rt=Object.defineProperty;var he=(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 ce=(o,e)=>()=>(o&&(e=o(o=0)),e);var Ge=(o,e)=>{for(var t in e)Rt(o,t,{get:e[t],enumerable:!0})};function
|
|
1
|
+
var Rt=Object.defineProperty;var he=(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 ce=(o,e)=>()=>(o&&(e=o(o=0)),e);var Ge=(o,e)=>{for(var t in e)Rt(o,t,{get:e[t],enumerable:!0})};function Mt(o){z.impl={...Ye,...o}}var Je,Ye,z,$,M=ce(()=>{Je=()=>{},Ye={debug:Je,info:Je,warn:(...o)=>console.warn("[workflow]",...o),error:(...o)=>console.error("[workflow]",...o)},z={impl:Ye};$={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 Ee,$e=ce(()=>{Ee=class{constructor(e,t,r=0){this.name=e,this.description=t,this.priority=r}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 st={};Ge(st,{clearSkills:()=>nt,getAllSkills:()=>ot,getSkill:()=>Y,hasSkill:()=>tt,listSkillIds:()=>rt,registerSkill:()=>et});function et(o){if(!o||typeof o.id!="string")throw new Error("Skill definition must include a string id");J.set(o.id,Object.freeze({...o}))}function Y(o){return J.get(o)||null}function tt(o){return J.has(o)}function ot(){return new Map(J)}function rt(){return Array.from(J.keys())}function nt(){J.clear()}var Ie,J,Q=ce(()=>{Ie=Symbol.for("@zibby/agent-workflow.skills");globalThis[Ie]||(globalThis[Ie]=new Map);J=globalThis[Ie]});var ee={};Ge(ee,{getAgentStrategy:()=>ve,invokeAgent:()=>ct,listStrategies:()=>at,registerStrategy:()=>it});function it(o){if(!o||typeof o.getName!="function"||typeof o.invoke!="function")throw new Error("strategy must implement getName() and invoke() (AgentStrategy shape)");let e=D.findIndex(t=>t.getName()===o.getName());e>=0?D[e]=o:D.push(o)}function at(){return D.map(o=>o.getName())}function ve(o={}){let{state:e={},preferredAgent:t=null}=o,r=t||e.agentType||process.env.AGENT_TYPE;if(!r){let n=D.map(s=>s.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=${r}`);let i=D.find(n=>n.getName()===r);if(!i){let n=D.map(s=>s.getName()).join(", ")||"none registered";throw new Error(`Unknown agent '${r}'. Available: ${n}`)}if(!i.canHandle(o))throw new Error(`Agent '${r}' is not available in this environment. Check credentials/environment.`);return $.debug(`[workflow] using agent: ${i.getName()}`),i}async function ct(o,e={},t={}){let r=ve(e),i=e.state?.config||t.config||{},n=i.models||{},s=t.nodeName&&n[t.nodeName]||null,a=n.default||null,c=i.agent?.[r.name]?.model||null,l=s||a||c||t.model||null,d={...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:i},p=o,u=d.skills||[];if(u.length>0&&!t.skipPromptFragments){let m=u.map(h=>{let E=Y(h)?.promptFragment;return typeof E=="function"?E():E}).filter(Boolean);m.length>0&&(p+=`
|
|
2
2
|
|
|
3
3
|
${m.join(`
|
|
4
4
|
|
|
@@ -9,9 +9,9 @@ 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: ${p.length} chars`),r.invoke(p,d)}var Te,
|
|
12
|
+
`),$.debug(`[workflow] prompt length: ${p.length} chars`),r.invoke(p,d)}var Te,D,Z=ce(()=>{$e();M();Q();Te=Symbol.for("@zibby/agent-workflow.strategies");globalThis[Te]||(globalThis[Te]=[]);D=globalThis[Te]});var Lt=new Set(["__proto__","constructor","prototype"]);function ge(o){if(Lt.has(o))throw new Error(`Invalid state key: "${o}"`)}var K=class{constructor(e={}){this._state=Object.create(null),Object.assign(this._state,{messages:[],errors:[],artifacts:{},metadata:{},...e}),this._history=[]}get(e){return this._state[e]}set(e,t){ge(e),this._history.push({...this._state}),this._state[e]=t}update(e){let t=Object.getOwnPropertyNames(e);for(let r of t)ge(r);this._history.push({...this._state});for(let r of t)this._state[r]=e[r]}append(e,t){ge(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 V=class{constructor(e){this.schema=e}parse(e){let t=e.match(/```json\s*([\s\S]*?)\s*```/);if(t)return this.validate(JSON.parse(t[1]));let r=[e.match(/\{[\s\S]*?\}/),e.match(/\{[\s\S]*\}/)].filter(Boolean).map(i=>i[0]);for(let i of r)try{return this.validate(JSON.parse(i))}catch(n){if(!(n instanceof SyntaxError))throw n}return this.validate({result:e.trim()})}validate(e){let t=[];for(let[r,i]of Object.entries(this.schema)){if(i.required&&!(r in e)&&t.push(`Missing required field: ${r}`),r in e&&i.type){let n=typeof e[r];n!==i.type&&t.push(`Field '${r}' expected ${i.type}, got ${n}`)}if(i.validate&&r in e){let n=i.validate(e[r]);n&&t.push(`Field '${r}': ${n}`)}}if(t.length>0)throw new Error(`Output validation failed:
|
|
13
13
|
${t.join(`
|
|
14
|
-
`)}`);return e}},
|
|
14
|
+
`)}`);return e}},jt={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(", ")}`})};M();import{writeFileSync as be,readFileSync as lt,existsSync as ut,mkdirSync as Gt}from"node:fs";import{join as Ne,dirname as Jt}from"node:path";import N from"chalk";var Qe="__WORKFLOW_GRAPH_LOG__",q=N.gray("\u2502"),Bt=N.gray("\u250C"),Ze=N.gray("\u2514"),me=N.green("\u25C6"),He=N.hex("#c084fc")("\u25C6"),Ke=N.hex("#2dd4bf")("\u25C6"),Se=N.red("\u25C6"),Ve=`${q} `,ze=2;function qe(o){return o<1e3?`${o}ms`:`${(o/1e3).toFixed(1)}s`}function Xe(o,e){return(t,r,i)=>{if(typeof t!="string")return o(t,r,i);let n=process.stdout.columns||120,s="";for(let a=0;a<t.length;a++){let c=t[a];e.lineStart&&(s+=Ve,e.col=ze,e.lineStart=!1),c===`
|
|
15
15
|
`?(s+=c,e.lineStart=!0,e.col=0,e.inEsc=!1):c==="\x1B"?(e.inEsc=!0,s+=c):e.inEsc?(s+=c,(c>="A"&&c<="Z"||c>="a"&&c<="z")&&(e.inEsc=!1)):(e.col++,s+=c,e.col>=n&&(s+=`
|
|
16
16
|
${Ve}`,e.col=ze))}return o(s,r,i)}}var le=class{constructor(){this._currentNode=null,this._origStdoutWrite=null,this._origStderrWrite=null,this._emitWorkflowGraphMarkers=String(process.env.ZIBBY_EMIT_GRAPH_MARKERS||"").trim()==="1"||String(process.env.ZIBBY_WORKFLOW_GRAPH_LOG_MARKERS||"").trim()==="1"}get isInsideNode(){return this._currentNode!==null}_startIntercepting(){this._origStdoutWrite=process.stdout.write.bind(process.stdout),this._origStderrWrite=process.stderr.write.bind(process.stderr);let e={lineStart:!0,col:0,inEsc:!1},t={lineStart:!0,col:0,inEsc:!1};this._outState=e,this._errState=t,process.stdout.write=Xe(this._origStdoutWrite,e),process.stderr.write=Xe(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(`
|
|
@@ -24,17 +24,17 @@ ${Ve}`,e.col=ze))}return o(s,r,i)}}var le=class{constructor(){this._currentNode=
|
|
|
24
24
|
`)}stepInfo(e){this.step(e)}stepTool(e){this._origStdoutWrite?this._writeDot(He,e):process.stdout.write.bind(process.stdout)(`${q} ${He} ${e}
|
|
25
25
|
`)}stepMemory(e){let t=N.hex("#2dd4bf")(e);this._origStdoutWrite?this._writeDot(Ke,t):process.stdout.write.bind(process.stdout)(`${q} ${Ke} ${t}
|
|
26
26
|
`)}stepFail(e){this._origStdoutWrite?this._writeDot(Se,N.red(e)):process.stdout.write.bind(process.stdout)(`${q} ${Se} ${N.red(e)}
|
|
27
|
-
`)}nodeStart(e){this._currentNode=e,this._emitGraphLogMarker({phase:"node_begin",node:e}),this._rawWrite(`${
|
|
27
|
+
`)}nodeStart(e){this._currentNode=e,this._emitGraphLogMarker({phase:"node_begin",node:e}),this._rawWrite(`${Bt} ${e}`),this._startIntercepting()}nodeComplete(e,t={}){this._stopIntercepting();let{duration:r,details:i}=t;if(i)for(let s of i)this._rawWrite(`${me} ${s}`);let n=r?N.dim(` ${qe(r)}`):"";this._rawWrite(`${Ze} ${N.green("done")}${n}`),this._emitGraphLogMarker({phase:"node_end",node:e}),this._rawWrite("")}nodeFailed(e,t,r={}){this._stopIntercepting();let{duration:i}=r,n=i?N.dim(` ${qe(i)}`):"";this._rawWrite(`${Se} ${N.red(t)}`),this._rawWrite(`${Ze} ${N.red("failed")}${n}`),this._emitGraphLogMarker({phase:"node_end",node:e}),this._rawWrite("")}route(e,t){this._rawWrite(N.dim(` ${e} \u2192 ${t}`)),this._rawWrite("")}graphComplete(){}},b=new le;var X=".zibby/output",we="sessions",B=".session-info.json",ye=".zibby-stop",Dt="result.json",Ft="raw_stream_output.txt",Wt="events.json",Ut={BROWSER:"browser",JIRA:"jira",GITHUB:"github",GITLAB:"gitlab",GIT:"git",SLACK:"slack",LARK:"lark",SENTRY:"sentry",MEMORY:"memory",CHAT_MEMORY:"chat-memory",RUNNER:"runner",SKILL_INSTALLER:"skill-installer",CORE_TOOLS:"core-tools",WORKFLOW_BUILDER:"workflow-builder",SESSION:"session"},_e=["CI_JOB_ID","GITHUB_RUN_ID","CIRCLE_WORKFLOW_ID","BUILD_ID"];var j=class{constructor(e){if(this.config=e,this.name=e.name,this.prompt=e.prompt,this.outputSchema=e.outputSchema,!this.outputSchema&&!e._isCustomCode)throw new Error(`Node '${this.name}' must define outputSchema (Zod schema). This defines the contract for what the node returns to state.`);this.isZodSchema=this.outputSchema&&typeof this.outputSchema._def<"u",this.parser=e.outputSchema&&!this.isZodSchema?new V(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,i=p=>t&&typeof t.get=="function"?t.get(p):e?.[p];if(typeof this.customExecute=="function"){$.debug(`[workflow] node '${this.name}': custom execute (skipping LLM)`);try{let p=await this.customExecute(e);return typeof p=="object"&&p!==null&&p.success===!1?{success:!1,error:p.error||"Node execution failed",raw:p.raw||null}:this.isZodSchema?($.debug(`[workflow] node '${this.name}': validating output schema`),{success:!0,output:this.outputSchema.parse(p),raw:null}):{success:!0,output:p,raw:null}}catch(p){return $.error(`[workflow] node '${this.name}' failed: ${p.message}`),p.name==="ZodError"&&$.error(`Schema errors: ${JSON.stringify(p.issues||p.errors,null,2)}`),{success:!1,error:p.message,raw:null}}}let n=typeof this.prompt=="function"?this.prompt(r()):this.prompt,s=i("_skillHints");s&&(n=`${s}
|
|
28
28
|
|
|
29
|
-
${n}`);let a=r(),c=a.cwd||process.cwd(),l=a.sessionPath;try{if(l){let p=Ne(l,
|
|
29
|
+
${n}`);let a=r(),c=a.cwd||process.cwd(),l=a.sessionPath;try{if(l){let p=Ne(l,B);if(ut(p)){let g=JSON.parse(lt(p,"utf-8"));g.currentNode=this.name,be(p,JSON.stringify(g,null,2),"utf-8")}let u=Ne(l,"..",B);if(ut(u))try{let g=JSON.parse(lt(u,"utf-8"));g.currentNode=this.name,be(u,JSON.stringify(g,null,2),"utf-8")}catch{}}}catch(p){$.debug(`[workflow] could not update session info: ${p.message}`)}let d=null;for(let p=0;p<=this.retries;p++)try{$.debug(`[workflow] node '${this.name}' attempt ${p}`);let u=r().config||{},g=u.agents||{},m=this.config.agent??g[this.name]??null,h={state:r()};m&&(h.preferredAgent=m);let E={workspace:c,schema:this.isZodSchema?this.outputSchema:null,skills:this.config.skills||[],sessionPath:l,config:u,nodeName:this.name,timeout:this.config?.timeout||3e5},I=e?._coreInvokeAgent;I||(I=(await Promise.resolve().then(()=>(Z(),ee))).invokeAgent);let f=await I(n,h,E),y,v;if(typeof f=="string"?(y=f,v=null):f.structured?(y=f.raw||JSON.stringify(f.structured,null,2),v=f.structured):(y=f.raw||JSON.stringify(f,null,2),v=f.extracted||null),l)try{let S=Ne(l,this.name,"raw_stream_output.txt");Gt(Jt(S),{recursive:!0}),be(S,typeof y=="string"?y:JSON.stringify(y),"utf-8")}catch(S){$.debug(`[workflow] could not save raw output: ${S.message}`)}if(this.isZodSchema&&v){$.info(`[workflow] node '${this.name}': output validated: ${JSON.stringify(v,null,2)}`);let S=v;if(typeof this.onComplete=="function")try{S=await this.onComplete(r(),v)}catch(P){$.warn(`[workflow] onComplete hook failed: ${P.message}`)}return{success:!0,output:S,raw:y}}if(typeof this.onComplete=="function")try{return{success:!0,output:await this.onComplete(r(),{raw:y}),raw:y}}catch(S){throw new Error(`onComplete failed: ${S.message}`,{cause:S})}if(this.parser){let S=this.parser.parse(y);return $.info(`[workflow] node '${this.name}': parsed output: ${JSON.stringify(S,null,2)}`),b.step("Output parsed"),{success:!0,output:S,raw:y}}return{success:!0,output:y,raw:y}}catch(u){d=u,p<this.retries&&$.info(`[workflow] node '${this.name}' failed, retrying (${p+1}/${this.retries})\u2026`)}return{success:!1,error:d.message,raw:null}}},te=class extends j{constructor(e){super({...e,_isCustomCode:!0}),this.condition=e.condition}async execute(e,t){let r=t&&typeof t.getAll=="function"?t.getAll():e;return{success:!0,output:{nextNode:this.condition(r)},raw:null}}};M();var Yt=2e3,Zt=600*1e3,Ht=new Set(["completed","failed","canceled","timeout"]);function Kt(){let o=process.env.PROGRESS_API_URL;if(!o)throw new Error("Sub-graph dispatch requires PROGRESS_API_URL env var (set automatically on cloud runs). Sub-graphs are not supported in local in-process runs yet \u2014 deploy the parent and child to cloud.");return o.replace(/\/executions\/?$/,"")}function Vt(){let o=process.env.PROJECT_ID;if(!o)throw new Error("Sub-graph dispatch requires PROJECT_ID env var.");return o}function zt(){let o=process.env.PROJECT_API_TOKEN;if(!o)throw new Error("Sub-graph dispatch requires PROJECT_API_TOKEN env var.");return o}function qt(){return process.env.EXECUTION_ID||null}function Xt(o,e){return e==null?o:typeof e=="function"?e(o):typeof e=="string"?e.split(".").reduce((t,r)=>t==null?t:t[r],o):o}async function dt(o,e={}){if(!o||typeof o!="string")throw new Error("dispatchSubgraph: workflowName (string) is required");let t=Kt(),r=Vt(),i=zt(),n=qt(),s=`${t}/projects/${encodeURIComponent(r)}/workflows/${encodeURIComponent(o)}/trigger`,a={input:e.input||{},...n?{parentExecutionId:n}:{},...e.conversationId?{conversationId:e.conversationId}:{}};$.info(`[sub-graph] dispatching '${o}' (${e.async?"async":"sync"}) from parent ${n||"<none>"}`);let c=await fetch(s,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${i}`},body:JSON.stringify(a)});if(!c.ok){let f=null,y="";try{f=await c.json(),y=f?.error||f?.message||JSON.stringify(f)}catch{y=await c.text().catch(()=>"")}if(c.status===429){let S=f?.quotaInfo||{},P=new Error(`Sub-graph '${o}' blocked by execution quota (${S.used??"?"}/${S.limit??"?"} on plan ${S.planId||"unknown"}). Sub-workflow runs count toward the same monthly cap as user-triggered runs.`);throw P.code="SUBGRAPH_QUOTA_EXCEEDED",P.status=429,P.subgraph=o,P.quotaInfo=S,P}if(c.status===400){let S=new Error(`Sub-graph '${o}' rejected input: ${y}`);throw S.code="SUBGRAPH_INVALID_INPUT",S.status=400,S.subgraph=o,S.validationErrors=f?.validationErrors||null,S.missing=f?.missing||null,S}let v=new Error(`Sub-graph '${o}' trigger rejected (${c.status}): ${y}`);throw v.code="SUBGRAPH_TRIGGER_FAILED",v.status=c.status,v.subgraph=o,v}let l=await c.json(),d=l?.data?.jobId||l?.jobId;if(!d)throw new Error(`Sub-graph '${o}' trigger returned no jobId: ${JSON.stringify(l).slice(0,200)}`);if(e.async)return $.info(`[sub-graph] async dispatch of '${o}' \u2192 jobId=${d} (not waiting)`),{jobId:d,status:"accepted",workflow:o};let p=Number.isFinite(e.timeoutMs)?e.timeoutMs:Zt,u=Number.isFinite(e.pollIntervalMs)?e.pollIntervalMs:Yt,g=`${t}/executions/${encodeURIComponent(d)}`,m=Date.now()+p,h="accepted",E=0;for(;Date.now()<m;){await new Promise(S=>setTimeout(S,u)),E+=1;let f=await fetch(g,{headers:{Authorization:`Bearer ${i}`}});if(!f.ok){if(f.status>=500){$.warn(`[sub-graph] status poll for ${d} returned ${f.status}, will retry`);continue}throw new Error(`Sub-graph status poll failed for ${d}: ${f.status}`)}let y=await f.json(),v=y?.data||y?.execution||y;if(h=v?.status||h,Ht.has(h)){if(h!=="completed"){let W=new Error(`Sub-graph '${o}' (${d}) ended in status '${h}'`);throw W.subgraphJobId=d,W.subgraphStatus=h,W}let S=v?.finalState||v?.state||{},P=Xt(S,e.output);return $.info(`[sub-graph] '${o}' (${d}) completed after ${E} polls`),P}}let I=new Error(`Sub-graph '${o}' (${d}) timed out after ${Math.round(p/1e3)}s (last status: ${h})`);throw I.subgraphJobId=d,I.subgraphStatus=h,I}import{existsSync as pt,readFileSync as Qt}from"node:fs";import{join as ke,dirname as ft}from"node:path";var oe=class{static async loadContext(e,t,r={}){let i={},n=r.filenames||["CONTEXT.md","AGENTS.md"];if(e){let a=ft(ke(t,e));for(let c of n){let l=await this.findAndMergeContextFiles(c,a,t);if(l){let d=c.replace(/\.[^.]+$/,"").toLowerCase();i[d]=l}}}let s=r.discovery||{};for(let[a,c]of Object.entries(s))try{let l=ke(t,c);pt(l)&&(i[a]=await this.loadFile(l))}catch(l){console.warn(`[workflow] could not load context '${a}' from '${c}': ${l.message}`)}return i}static async findAndMergeContextFiles(e,t,r){let i=[],n=t;for(;n.startsWith(r);){let s=ke(n,e);if(pt(s))try{i.unshift(await this.loadFile(s))}catch(c){console.warn(`[workflow] could not load ${e} from ${s}: ${c.message}`)}let a=ft(n);if(a===n)break;n=a}return i.length===0?null:i.every(s=>typeof s=="string")?i.join(`
|
|
30
30
|
|
|
31
31
|
---
|
|
32
32
|
|
|
33
33
|
`):i.every(s=>typeof s=="object")?Object.assign({},...i):i[i.length-1]}static async loadFile(e){let t=Qt(e,"utf-8");if(e.endsWith(".json"))return JSON.parse(t);if(e.endsWith(".js")||e.endsWith(".mjs")){let{pathToFileURL:r}=await import("url"),i=await import(r(e).href);return i.default||i}return t}};import{mkdirSync as mt,existsSync as xe,writeFileSync as ht,unlinkSync as eo}from"node:fs";import{join as F,resolve as St}from"node:path";import{config as to}from"dotenv";import{zodToJsonSchema as gt}from"zod-to-json-schema";import oo from"handlebars";function ro({traceFrom:o,sessionId:e,sessionPath:t,idSource:r,mkdirFresh:i}){if(!(process.env.ZIBBY_SESSION_LOG==="1"||process.env.ZIBBY_SESSION_LOG==="true"))return;let s=typeof process.ppid=="number"?process.ppid:"n/a",a=`[zibby:session] from=${o} pid=${process.pid} ppid=${s} sessionId=${e} source=${r} mkdir=${i?"yes":"no"} path=${t}`;if(console.log(a),process.env.ZIBBY_TRACE_SESSION==="1"||process.env.ZIBBY_TRACE_SESSION==="true"){let d=(new Error("session trace").stack||"").split(`
|
|
34
34
|
`).slice(2,14).join(`
|
|
35
35
|
`);console.log(`[zibby:session] stack (${o}):
|
|
36
|
-
${d}`)}}function wt(){return process.env.ZIBBY_TRUST_SESSION_ENV==="1"||process.env.ZIBBY_TRUST_SESSION_ENV==="true"||process.env.ZIBBY_KEEP_SESSION_ENV==="1"||process.env.ZIBBY_KEEP_SESSION_ENV==="true"}function yt(){if(!(process.env.ZIBBY_PIN_SESSION_PATH==="1"||process.env.ZIBBY_PIN_SESSION_PATH==="true"))return;let e=process.env.ZIBBY_SESSION_PATH;if(!(e==null||String(e).trim()===""))try{return St(String(e).trim())}catch{return String(e).trim()}}function _t(){wt()||(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 $t(o={}){let e=_e.map(n=>process.env[n]).find(Boolean),t=Math.random().toString(36).slice(2,6),r=e||`${Date.now()}_${t}`,i=o.paths?.sessionPrefix;return i?`${i}_${r}`:r}function It({cwd:o=process.cwd(),config:e={},initialState:t={},traceFrom:r="resolveWorkflowSession"}={}){let i=t.sessionPath,n=t.sessionTimestamp,s="initialState.sessionPath";if(!i&&process.env.ZIBBY_SESSION_PATH)try{let l=St(String(process.env.ZIBBY_SESSION_PATH));l&&(i=l,s="ZIBBY_SESSION_PATH")}catch{}let a;if(i)a=String(i).split(/[/\\]/).filter(Boolean).pop(),n==null&&(n=Date.now());else{let l=process.env.ZIBBY_SESSION_ID&&String(process.env.ZIBBY_SESSION_ID).trim();if(l)a=l,s="ZIBBY_SESSION_ID";else{let p=e.sessionId!=null?String(e.sessionId).trim():"";p&&p!=="last"?(a=p,s="config.sessionId"):(a=$t(e),s="generated")}n=n??Date.now();let d=e.paths?.output||X;i=F(o,d,we,a)}let c=!xe(i);return c&&mt(i,{recursive:!0}),(c||s!=="initialState.sessionPath")&&ro({traceFrom:r,sessionId:a,sessionPath:i,idSource:s,mkdirFresh:c}),Et({sessionPath:i,sessionId:a}),{sessionPath:i,sessionId:a,sessionTimestamp:n}}var re=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.inputSchema=e.inputSchema||null,this.contextSchema=e.contextSchema||null,this.nodePrompts=new Map,this.nodeOptions=new Map,this._invokeAgent=e.invokeAgent||null,this._compiledPrompts=new Map}setInputSchema(e){return this.inputSchema=e,this}setContextSchema(e){return this.contextSchema=e,this}setStateSchema(e){return this.stateSchema=e,this}getInputSchema(){return this.inputSchema}getContextSchema(){return this.contextSchema}getStateSchema(){return this.stateSchema}_runtimeSchema(){if(this.inputSchema&&this.contextSchema)try{return this.inputSchema.merge(this.contextSchema)}catch{}return this.inputSchema&&!this.contextSchema?this.inputSchema:this.stateSchema}addNode(e,t,r={}){if(!(t instanceof
|
|
37
|
-
\u{1F6D1} External stop requested \u2014 ending workflow.`),b.step("Workflow stopped externally"),{success:!0,state:f.getAll(),executionLog:se,stoppedExternally:!0};let x=this.nodes.get(w);if(!x)throw new Error(`Node '${w}' not found in graph`);let
|
|
36
|
+
${d}`)}}function wt(){return process.env.ZIBBY_TRUST_SESSION_ENV==="1"||process.env.ZIBBY_TRUST_SESSION_ENV==="true"||process.env.ZIBBY_KEEP_SESSION_ENV==="1"||process.env.ZIBBY_KEEP_SESSION_ENV==="true"}function yt(){if(!(process.env.ZIBBY_PIN_SESSION_PATH==="1"||process.env.ZIBBY_PIN_SESSION_PATH==="true"))return;let e=process.env.ZIBBY_SESSION_PATH;if(!(e==null||String(e).trim()===""))try{return St(String(e).trim())}catch{return String(e).trim()}}function _t(){wt()||(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 $t(o={}){let e=_e.map(n=>process.env[n]).find(Boolean),t=Math.random().toString(36).slice(2,6),r=e||`${Date.now()}_${t}`,i=o.paths?.sessionPrefix;return i?`${i}_${r}`:r}function It({cwd:o=process.cwd(),config:e={},initialState:t={},traceFrom:r="resolveWorkflowSession"}={}){let i=t.sessionPath,n=t.sessionTimestamp,s="initialState.sessionPath";if(!i&&process.env.ZIBBY_SESSION_PATH)try{let l=St(String(process.env.ZIBBY_SESSION_PATH));l&&(i=l,s="ZIBBY_SESSION_PATH")}catch{}let a;if(i)a=String(i).split(/[/\\]/).filter(Boolean).pop(),n==null&&(n=Date.now());else{let l=process.env.ZIBBY_SESSION_ID&&String(process.env.ZIBBY_SESSION_ID).trim();if(l)a=l,s="ZIBBY_SESSION_ID";else{let p=e.sessionId!=null?String(e.sessionId).trim():"";p&&p!=="last"?(a=p,s="config.sessionId"):(a=$t(e),s="generated")}n=n??Date.now();let d=e.paths?.output||X;i=F(o,d,we,a)}let c=!xe(i);return c&&mt(i,{recursive:!0}),(c||s!=="initialState.sessionPath")&&ro({traceFrom:r,sessionId:a,sessionPath:i,idSource:s,mkdirFresh:c}),Et({sessionPath:i,sessionId:a}),{sessionPath:i,sessionId:a,sessionTimestamp:n}}var re=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.inputSchema=e.inputSchema||null,this.contextSchema=e.contextSchema||null,this.nodePrompts=new Map,this.nodeOptions=new Map,this._invokeAgent=e.invokeAgent||null,this._compiledPrompts=new Map}setInputSchema(e){return this.inputSchema=e,this}setContextSchema(e){return this.contextSchema=e,this}setStateSchema(e){return this.stateSchema=e,this}getInputSchema(){return this.inputSchema}getContextSchema(){return this.contextSchema}getStateSchema(){return this.stateSchema}_runtimeSchema(){if(this.inputSchema&&this.contextSchema)try{return this.inputSchema.merge(this.contextSchema)}catch{}return this.inputSchema&&!this.contextSchema?this.inputSchema:this.stateSchema}addNode(e,t,r={}){if(!(t instanceof j)&&t&&typeof t=="object"&&typeof t.workflow=="string"){let n=t,s={name:e,_isCustomCode:!0,retries:n.retries,onComplete:n.onComplete,execute:async c=>{let l=c?.state&&typeof c.state.getAll=="function"?c.state.getAll():c,d;return typeof n.input=="function"?d=n.input(l):n.input&&typeof n.input=="object"?d=n.input:d={},dt(n.workflow,{input:d,async:n.async===!0,conversationId:typeof n.conversationId=="function"?n.conversationId(l):n.conversationId,output:n.output,timeoutMs:n.timeoutMs,pollIntervalMs:n.pollIntervalMs})}},a=new j(s);return a.name=e,this.nodes.set(e,a),r.prompt&&this.nodePrompts.set(e,r.prompt),Object.keys(r).length>0&&this.nodeOptions.set(e,r),this}let i=t instanceof j?t:new j(t);return i.name=e,this.nodes.set(e,i),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 te({...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,i,n){let s=r;for(let a=e.length-1;a>=0;a--){let c=e[a],l=s;s=()=>c(t,l,i,n)}return s()}serialize(){let e=[],t={};for(let[l,d]of this.nodes){let p=this.nodeTypeMap.get(l)||l;e.push({id:l,type:p,data:{nodeType:p,label:l}});let u={};d._isCustomCode&&typeof d.execute=="function"&&(u.customCode=d.execute.toString());let g=this.nodePrompts.get(l);if(g&&(u.prompt=g),typeof d.customExecute=="function"&&(u.executeCode=d.customExecute.toString()),d.outputSchema)try{if(typeof d.outputSchema._def<"u"){let h=gt(d.outputSchema,{target:"openApi3"});u.outputSchema={jsonSchema:h,variables:this._flattenJsonSchemaToVariables(h)}}else u.outputSchema={schema:d.outputSchema}}catch(h){console.warn(`[workflow] failed to convert schema for ${l}:`,h.message)}let m=(this.resolvedToolsMap||{})[l];m?.toolIds&&(u.tools=m.toolIds),Object.keys(u).length>0&&(t[l]=u)}let r=[];for(let[l,d]of this.edges)if(typeof d=="string")r.push({source:l,target:d});else if(d.conditional){let p=this.conditionalCodeMap.get(l)||d.routes.toString(),u=this._inferConditionalTargets(d.routes),g=d.labels||{};for(let m of u){let h={source:l,target:m,data:{conditionalCode:p}};g[m]&&(h.label=g[m]),r.push(h)}}let i=l=>{if(!l)return null;try{return gt(l,{target:"openApi3"})}catch{return null}},n=this._runtimeSchema(),s=i(n||this.stateSchema),a=i(this.inputSchema),c=i(this.contextSchema);return{nodes:e,edges:r,nodeConfigs:t,stateSchema:s,inputSchema:a,contextSchema:c}}_inferConditionalTargets(e){let t=e.toString(),r=new Set,i=/return\s+['"]([^'"]+)['"]/g,n;for(;(n=i.exec(t))!==null;)r.add(n[1]);return[...r]}_flattenJsonSchemaToVariables(e,t=""){let r=e;if(e.$ref&&e.definitions){let i=e.$ref.replace("#/definitions/","");r=e.definitions[i]||e}return this._flattenSchema(r,t)}_flattenSchema(e,t=""){if(!e||typeof e!="object")return[];let r=[],i=e.properties||{},n=e.required||[];for(let[s,a]of Object.entries(i)){let c=t?`${t}.${s}`:s;r.push({path:c,type:a.type||"unknown",label:a.description||this._formatLabel(s),optional:!n.includes(s)}),a.type==="object"&&a.properties&&r.push(...this._flattenSchema(a,c)),a.type==="array"&&a.items?.type==="object"&&a.items.properties&&r.push(...this._flattenSchema(a.items,`${c}[]`))}return r}_formatLabel(e){return e.replace(/([A-Z])/g," $1").replace(/^./,t=>t.toUpperCase()).trim()}_summarizeNodeOutput(e,t){if(!t||typeof t!="object")return[];let r=[];t.success!==void 0&&r.push(`Result: ${t.success?"passed":"failed"}`);for(let[i,n]of Object.entries(t))if(!(i==="success"||i==="raw"||i==="nextNode")){if(typeof n=="string"&&n.length<=80)r.push(`${i}: ${n}`);else if(Array.isArray(n)){let s=n.length,a=n.filter(l=>l?.passed===!0).length,c=n.some(l=>l?.passed!==void 0);r.push(c?`${i}: ${a}/${s} passed${s-a?`, ${s-a} failed`:""}`:`${i}: ${s} items`)}if(r.length>=4)break}return r}async run(e,t={},r={}){if(!this.entryPoint)throw new Error("No entry point set for graph");let i=new AbortController;r.signal&&(r.signal.aborted?i.abort():r.signal.addEventListener("abort",()=>i.abort(),{once:!0}));let n=r.strategyAbortTimeoutMs??t.config?.strategyAbortTimeoutMs??5e3,s=t.cwd||process.cwd();to({path:F(s,".env")});let a=t.config||{};if(!a||Object.keys(a).length===0)try{let _=F(s,".zibby.config.js");xe(_)&&(a=(await import(_)).default||{})}catch{}process.env.EXECUTION_ID&&!a.agent?.strictMode&&(a.agent={...a.agent,strictMode:!0});let c=t.agentType;if(!c){let _=a?.agent;_?.provider?c=_.provider:_?.gemini?c="gemini":_?.claude?c="claude":_?.cursor?c="cursor":_?.codex?c="codex":c=process.env.AGENT_TYPE||"cursor"}let l=t.contextConfig||e?.config?.contextConfig||e?.config?.context||a?.context||{},d=this._runtimeSchema();if(d){let _=d.safeParse(t);if(!_.success){let k=_.error.issues.map(x=>`${x.path.join(".")}: ${x.message}`);throw console.error("\u274C Initial state validation failed:"),k.forEach(x=>console.error(` - ${x}`)),new Error(`State validation failed: ${k.join(", ")}`)}b.step("State validated against schema")}let p=yt(),u=t.sessionPath||p;u||_t();let{sessionPath:g,sessionTimestamp:m,sessionId:h}=It({cwd:s,config:a,traceFrom:"WorkflowGraph.run",initialState:{sessionPath:u,sessionTimestamp:t.sessionTimestamp}});b.step(`Session ${h}`);let E=await oe.loadContext(t.specPath||"",s,l);Object.keys(E).length>0&&b.step(`Context loaded: ${Object.keys(E).join(", ")}`);let I=t.outputPath;!I&&t.specPath&&(e?.calculateOutputPath?I=e.calculateOutputPath(t.specPath):console.warn(`\u26A0\uFE0F outputPath not resolved (specPath=${t.specPath})`));let f=new K({...t,config:a,agentType:c,outputPath:I,sessionPath:g,sessionTimestamp:m,context:E,resolvedTools:this.resolvedToolsMap||{},_signal:i.signal}),y=new Map;try{await import("@zibby/skills")}catch{}let{getSkill:v}=await Promise.resolve().then(()=>(Q(),st)),S=a.skills&&typeof a.skills=="object"?a.skills:{},P=Object.values(S).filter(_=>_&&typeof _=="object"&&typeof _.id=="string"),W=_=>{for(let k of P)if(k.id===_)return k;return v(_)},Le=new Set;for(let[,_]of this.nodes)for(let k of _.config?.skills||[])Le.add(k);for(let _ of Le){let k=W(_);if(typeof k?.middleware=="function")try{let x=await k.middleware();typeof x=="function"&&y.set(_,x)}catch{}}let w=this.entryPoint,se=[],je=a?.recursionLimit??100,Nt=0;try{for(;w&&w!=="END";){if(++Nt>je)throw new Error(`Workflow exceeded recursion limit (${je}) \u2014 likely a cyclic conditional route. Set config.recursionLimit if you need a higher cap.`);let k=F(g,ye);if(xe(k)){try{eo(k)}catch{}i.abort()}if(i.signal.aborted)return console.warn(`
|
|
37
|
+
\u{1F6D1} External stop requested \u2014 ending workflow.`),b.step("Workflow stopped externally"),{success:!0,state:f.getAll(),executionLog:se,stoppedExternally:!0};let x=this.nodes.get(w);if(!x)throw new Error(`Node '${w}' not found in graph`);let Me=JSON.stringify({sessionPath:g,sessionTimestamp:m,currentNode:w,createdAt:new Date().toISOString(),config:f.get("config")}),kt=F(g,B);ht(kt,Me,"utf-8");let Be=f.get("config")?.paths?.output||X,xt=F(s,Be,B);mt(F(s,Be),{recursive:!0});try{ht(xt,Me,"utf-8")}catch{}let De=t.onPipelineProgress;if(typeof De=="function")try{De({cwd:s,sessionPath:g,sessionId:h,outputBase:f.get("config")?.paths?.output||X,currentNode:w})}catch{}let At=(this.resolvedToolsMap||{})[w]||null;f.set("_currentNodeTools",At);let Ot=f.get("nodeConfigs")||{};f.set("_currentNodeConfig",Ot[w]||{}),b.nodeStart(w);let Fe=Date.now(),ie=this.nodePrompts.get(w);if(!this._invokeAgent){let A=await Promise.resolve().then(()=>(Z(),ee));this._invokeAgent=A.invokeAgent}let Ct=this._invokeAgent,fe={},Pt=x.config?.skills||[];for(let A of Pt){let O=W(A);if(typeof O?.invokeAgentOptions=="function")try{let T=O.invokeAgentOptions(f.getAll(),{agentType:f.get("agentType"),nodeName:w});T&&typeof T=="object"&&(fe={...fe,...T})}catch(T){console.warn(`[graph] skill '${A}' invokeAgentOptions threw: ${T.message}`)}}let We=async(A,O,T={})=>{let C=Ct(A,O,{...fe,...T,signal:i.signal});return C.catch(()=>{}),i.signal.aborted?C:Promise.race([C,new Promise((U,G)=>{let L=()=>{setTimeout(()=>{let H=new Error(`Strategy ignored AbortSignal \u2014 engine deadman fired after ${n}ms`);H.name="AbortError",G(H)},n)};i.signal.addEventListener("abort",L,{once:!0})})])},Ue={state:f,invokeAgent:async(A={},O={})=>{let T=O.prompt||"";if(ie){let C=this._compiledPrompts.get(w);C||(C=oo.compile(ie,{noEscape:!0}),this._compiledPrompts.set(w,C));try{T=C(A)}catch(U){throw console.error(`\u274C Template rendering failed for node '${w}':`,U.message),new Error(`Template rendering failed: ${U.message}`,{cause:U})}}else if(!T)throw new Error(`No prompt template configured for node '${w}' and no prompt provided in options`);return We(T,{state:f.getAll(),images:O.images||[]},{model:O.model||f.get("model"),workspace:f.get("workspace"),schema:O.schema,...O,signal:i.signal})},_coreInvokeAgent:We,agent:e,nodeId:w,promptTemplate:ie,getPromptTemplate:()=>ie,...f.getAll()};try{let A=(x.config?.skills||[]).map(L=>y.get(L)).filter(Boolean),O=[...this.middleware,...A],T;O.length>0?T=await this._composeMiddleware(O,w,async()=>x.execute(Ue,f),f.getAll(),f):T=await x.execute(Ue,f);let C=Date.now()-Fe;if(se.push({node:w,success:T.success,duration:C,timestamp:new Date().toISOString()}),!T.success){if(i.signal.aborted)return b.step("Workflow stopped externally"),{success:!0,state:f.getAll(),executionLog:se,stoppedExternally:!0};f.append("errors",{node:w,error:T.error});let L=x.config?.retries||0,H=`${w}_retries`,ae=f.getAll()[H]||0;if(ae<L){b.stepInfo(`Retrying (attempt ${ae+1}/${L})`),f.update({[H]:ae+1,[`${w}_raw`]:T.raw});continue}throw b.nodeFailed(w,T.error,{duration:C}),new Error(`Node '${w}' failed after ${ae} attempts: ${T.error}`)}f.update({[w]:T.output});let U=this._summarizeNodeOutput(w,T.output);b.nodeComplete(w,{duration:C,details:U});let G=this.edges.get(w);if(!G)w="END";else if(G.conditional){let L=G.routes(f.getAll());b.route(w,L),w=L}else w=G}catch(A){throw b.isInsideNode&&b.nodeFailed(w,A.message,{duration:Date.now()-Fe}),f.set("failed",!0),f.set("failedAt",w),A}}b.graphComplete();let _={success:!0,state:f.getAll(),executionLog:se};return e&&typeof e.onComplete=="function"&&await e.onComplete(_),_}finally{if(e&&typeof e.cleanup=="function")try{await e.cleanup()}catch(_){console.warn(`[workflow] agent.cleanup() failed: ${_.message}`)}}}};var Ae=Symbol.for("@zibby/agent-workflow.nodes");globalThis[Ae]||(globalThis[Ae]=new Map);var ne=globalThis[Ae];function Tt(o,e){ne.set(o,e)}function Oe(o){return ne.get(o)}function ue(o){return ne.has(o)}function no(){return Array.from(ne.keys())}function Ce(o){let e=ne.get(o);return e?e.factory&&typeof e.create=="function"?e.create.toString():typeof e.execute=="function"?e.execute.toString():typeof e=="function"?e.toString():null:null}Tt("ai_agent",{name:"ai_agent",factory:!0,create:(o,e={})=>({name:o,_isCustomCode:!0,execute:async t=>{let r=t?._coreInvokeAgent;r||(r=(await Promise.resolve().then(()=>(Z(),ee))).invokeAgent);let i=e.extraPromptInstructions||"Execute the task based on the current state.",n=so(i,t),s=await r(n,{cwd:t.workspace||process.cwd(),model:t.model,tools:e.resolvedTools||null});return{success:!0,output:{raw:s,nodeId:o},raw:typeof s=="string"?s:s.raw}}})});function so(o,e){let t=/@([\w.]+)/g,r=new Set,i;for(;(i=t.exec(o))!==null;)r.add(i[1]);if(r.size===0)return o;let n=[],s=new Set;for(let a of r){let c=a.split(".")[0];if(s.has(c))continue;let l=a.split(".").reduce((u,g)=>u?.[g],e);if(l===void 0)continue;let d=typeof l=="string"?l:l?.raw??JSON.stringify(l,null,2),p=a.replace(/_/g," ").replace(/\b\w/g,u=>u.toUpperCase());n.push(`## ${p}
|
|
38
38
|
${d}`),a.includes(".")||s.add(c)}return n.length===0?o:`${o}
|
|
39
39
|
|
|
40
40
|
---
|
|
@@ -42,7 +42,7 @@ ${d}`),a.includes(".")||s.add(c)}return n.length===0?o:`${o}
|
|
|
42
42
|
|
|
43
43
|
${n.join(`
|
|
44
44
|
|
|
45
|
-
`)}`}Q();
|
|
45
|
+
`)}`}Q();M();var de={};function Re(o,e){if(Array.isArray(e))return Pe(e);let t=de[o];return!t||t.length===0?null:Pe(t)}function Pe(o){if(!Array.isArray(o)||o.length===0)return null;let e=[],t={},r=[];for(let i of o){let n=Y(i);if(!n){$.warn(`[workflow] unknown skill "${i}" \u2014 skipping`);continue}r.push(i);for(let s of n.tools||[])e.push({name:s.name,description:s.description,input_schema:s.input_schema||{type:"object",properties:{}}});if(!t[n.serverName])if(typeof n.resolve=="function"){let s=n.resolve();s&&(t[n.serverName]={...s,toolPrefix:i})}else{let s={};for(let a of n.envKeys||[]){let c=process.env[a];c&&(s[a]=c)}t[n.serverName]={command:n.command,args:[...n.args||[]],env:s,toolPrefix:i}}}return r.length===0?null:{toolIds:r,claudeTools:e,mcpServers:t}}M();function io(o,e={}){let{nodes:t,edges:r,nodeConfigs:i={}}=o;if(!Array.isArray(t)||t.length===0)throw new R("Graph must have at least one node");if(!Array.isArray(r))throw new R("Graph edges must be an array");let n=new re(e);e.stateSchema&&n.setStateSchema(e.stateSchema);let s=new Set,a=new Map,c={};for(let u of t){let g=pe(u);a.set(u.id,{...u,resolvedType:g}),g==="decision"&&s.add(u.id)}for(let[u,g]of a){if(s.has(u))continue;let m=g.resolvedType,h=i[u]||{},E=Re(m,h.tools);E&&(c[u]=E);let I={};h.prompt&&(I.prompt=h.prompt);let f=ue(m);if($.debug(`[workflow] compiler: node "${u}" type="${m}" registered=${f}`),h.customCode&&!f)n.addNode(u,vt(u,h.customCode,h),I),n.setNodeType(u,m);else if(f){let y=Oe(m);y.factory?n.addNode(u,y.create(u,{...h,resolvedTools:E}),I):n.addNode(u,y,I),n.setNodeType(u,m)}else if(h.executeCode)n.addNode(u,vt(u,h.executeCode,h),I),n.setNodeType(u,m);else throw new R(`Unknown node type "${m}" for node "${u}". Did you forget to register it?`)}n.resolvedToolsMap=c;let l=new Set;for(let u of r)s.has(u.target)||l.add(u.target);let d=t.find(u=>!s.has(u.id)&&!l.has(u.id));if(!d)throw new R("Could not determine entry point: no node without incoming edges found");n.setEntryPoint(d.id);let p=lo(r,"source");for(let u of r)if(!s.has(u.source))if(s.has(u.target)){let g=u.target,m=p.get(g)||[];if(m.length===0)throw new R(`Decision node "${g}" has no outgoing edges`);let h=uo(g,m,s);n.addConditionalEdges(u.source,h)}else n.addEdge(u.source,u.target);return n}function ao(o){let e=[];if(!o||typeof o!="object")return{valid:!1,errors:["Config must be a non-null object"]};if((!Array.isArray(o.nodes)||o.nodes.length===0)&&e.push("Graph must have at least one node"),Array.isArray(o.edges)||e.push("Graph edges must be an array"),e.length>0)return{valid:!1,errors:e};let t=o.nodeConfigs||{};for(let a of o.nodes){let c=pe(a);if(c==="decision"||ue(c))continue;let l=t[a.id]||{};l.customCode||l.executeCode||e.push(`Unknown node type "${c}" for node "${a.id}". Register it or provide customCode/executeCode.`)}let r=new Set(o.nodes.map(a=>a.id));for(let a of o.edges)r.has(a.source)||e.push(`Edge references unknown source node "${a.source}"`),r.has(a.target)||e.push(`Edge references unknown target node "${a.target}"`);let i=new Set(o.nodes.filter(a=>pe(a)==="decision").map(a=>a.id)),n=new Set;for(let a of o.edges)i.has(a.target)||n.add(a.target);let s=o.nodes.filter(a=>!i.has(a.id)&&!n.has(a.id));s.length===0?e.push("No entry point found (every node has incoming edges)"):s.length>1&&e.push(`Multiple entry points found: ${s.map(a=>a.id).join(", ")}`);for(let a of i){let c=o.edges.filter(d=>d.source===a);c.length===0&&e.push(`Decision node "${a}" has no outgoing edges`),c.some(d=>d.data?.conditionalCode||d.conditionalCode)||e.push(`Decision node "${a}" outgoing edges have no conditionalCode`)}return{valid:e.length===0,errors:e}}function co(o){return!o||!Array.isArray(o.nodes)?[]:o.nodes.filter(e=>pe(e)!=="decision").map(e=>e.id)}function pe(o){let e=o.data?.nodeType||o.data?.type||o.type;return e==="workflowNode"||e==="custom"||e==="default"?o.id:e}function lo(o,e){let t=new Map;for(let r of o){let i=r[e];t.has(i)||t.set(i,[]),t.get(i).push(r)}return t}function uo(o,e,t){let r=e.find(a=>a.data?.conditionalCode||a.conditionalCode);if(!r)throw new R(`Decision node "${o}" has no conditionalCode on its outgoing edges`);let i=r.data?.conditionalCode||r.conditionalCode,n=new Set(e.map(a=>a.target).filter(a=>!t.has(a))),s;try{let c=new Function(`return (${i})`)();s=l=>{let d=c(l);return n.has(d)||$.warn(`[workflow] conditional route from "${o}" returned "${d}" which is not in valid targets: ${[...n].join(", ")}`),d}}catch(a){throw new R(`Failed to compile conditionalCode for "${o}": ${a.message}`)}return s}function vt(o,e,t={}){let r;try{r=new Function("invokeAgent","require","console",`return (${e})`)}catch(s){throw new R(`Failed to compile customCode for node "${o}": ${s.message}`)}let i=r(async(...s)=>{let{invokeAgent:a}=await Promise.resolve().then(()=>(Z(),ee));return a(...s)},typeof he<"u"?he:void 0,console),n=null;return t.outputSchema&&(n=t.outputSchema.jsonSchema||t.outputSchema),{name:o,_isCustomCode:!0,outputSchema:n,execute:async s=>{try{let a=await i(s);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 R=class extends Error{constructor(e){super(e),this.name="CompilationError"}};Q();$e();Z();function po(o,e={}){let{nodes:t,edges:r,nodeConfigs:i={}}=o,n=new Set,s=[],a=new Map;for(let E of t){let I=E.data?.nodeType||E.type;a.set(E.id,I),I==="decision"?n.add(E.id):s.push({id:E.id,nodeType:I,label:E.data?.label||E.id})}let c=s.some(E=>{let I=i[E.id]||{};return!I.customCode&&!I.executeCode}),{toolsPerNode:l,toolIdsByVar:d}=_o(s,i),{simpleEdges:p,conditionalEdges:u}=Eo(r,n),g=$o(s,r,n),m=[],h=e.workflowType||"workflow";return m.push(ho(e)),m.push(go(h,{usesRegisteredNodes:c})),m.push(mo(d)),m.push(So(h)),m.push(wo(s,i)),m.push(yo(s,g,p,u,l,h)),m.filter(Boolean).join(`
|
|
46
46
|
`)}function fo(o){let e={};for(let[t,r]of Object.entries(o)){let{tools:i,...n}=r;Object.keys(n).length>0&&(e[t]=n)}return e}function ho(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
47
|
`)}function go(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
48
|
`)}function mo(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,r]of o)e.push(`const ${t} = getResolvedToolDefinitions(${JSON.stringify(r)}); // ${r.join(", ")}`);return e.push(""),e.join(`
|
|
@@ -51,4 +51,4 @@ ${n.join(`
|
|
|
51
51
|
`)}function yo(o,e,t,r,i,n){let s=["// \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"];s.push("export function buildGraph(options = {}) {"),s.push(" const graph = new WorkflowGraph(options);",""),s.push(" // Nodes");for(let c of o){let l=bt(c.id);s.push(` graph.addNode('${c.id}', { name: '${c.id}', execute: ${l}_execute });`),s.push(` graph.setNodeType('${c.id}', '${c.nodeType}');`)}s.push("",` graph.setEntryPoint('${e}');`,""),(t.length>0||r.length>0)&&s.push(" // Edges");for(let c of t)s.push(` graph.addEdge('${c.source}', '${c.target}');`);for(let c of r){let l=c.code.split(`
|
|
52
52
|
`).map((d,p)=>p===0?d:` ${d}`).join(`
|
|
53
53
|
`);s.push(` graph.addConditionalEdges('${c.source}', ${l});`)}let a=[];for(let c of o){let l=i.get(c.id);l&&a.push(` '${c.id}': ${l},`)}return a.length>0&&s.push(""," graph.resolvedToolsMap = {",...a," };"),s.push(""," return graph;","}",""),s.push("export { nodeConfigs };",""),s.join(`
|
|
54
|
-
`)}function _o(o,e){let t=new Map,r=new Map;for(let i of o){let n=e[i.id]?.tools,s;if(Array.isArray(n)&&n.length>0)s=[...n].sort();else{let a=de[i.nodeType];a?.length>0&&(s=[...a].sort())}if(s){let a=`${s.map(c=>c.replace(/[^a-zA-Z0-9]/g,"")).join("And")}Tools`;t.set(i.id,a),r.has(a)||r.set(a,s)}}return{toolsPerNode:t,toolIdsByVar:r}}function Eo(o,e){let t=[],r=[],i=new Map,n=new Set;for(let s of o)i.has(s.source)||i.set(s.source,[]),i.get(s.source).push(s);for(let s of o)if(!e.has(s.source))if(e.has(s.target)){if(n.has(s.target))continue;n.add(s.target);let c=(i.get(s.target)||[]).find(l=>l.data?.conditionalCode||l.conditionalCode);c&&r.push({source:s.source,code:c.data?.conditionalCode||c.conditionalCode})}else t.push({source:s.source,target:s.target});return{simpleEdges:t,conditionalEdges:r}}function $o(o,e,t){let r=new Set;for(let n of e)t.has(n.target)||r.add(n.target);let i=o.find(n=>!r.has(n.id));return i?i.id:o[0]?.id}function bt(o){return o.replace(/[^a-zA-Z0-9]/g,"_")}
|
|
54
|
+
`)}function _o(o,e){let t=new Map,r=new Map;for(let i of o){let n=e[i.id]?.tools,s;if(Array.isArray(n)&&n.length>0)s=[...n].sort();else{let a=de[i.nodeType];a?.length>0&&(s=[...a].sort())}if(s){let a=`${s.map(c=>c.replace(/[^a-zA-Z0-9]/g,"")).join("And")}Tools`;t.set(i.id,a),r.has(a)||r.set(a,s)}}return{toolsPerNode:t,toolIdsByVar:r}}function Eo(o,e){let t=[],r=[],i=new Map,n=new Set;for(let s of o)i.has(s.source)||i.set(s.source,[]),i.get(s.source).push(s);for(let s of o)if(!e.has(s.source))if(e.has(s.target)){if(n.has(s.target))continue;n.add(s.target);let c=(i.get(s.target)||[]).find(l=>l.data?.conditionalCode||l.conditionalCode);c&&r.push({source:s.source,code:c.data?.conditionalCode||c.conditionalCode})}else t.push({source:s.source,target:s.target});return{simpleEdges:t,conditionalEdges:r}}function $o(o,e,t){let r=new Set;for(let n of e)t.has(n.target)||r.add(n.target);let i=o.find(n=>!r.has(n.id));return i?i.id:o[0]?.id}function bt(o){return o.replace(/[^a-zA-Z0-9]/g,"_")}M();export{Ee as AgentStrategy,_e as CI_ENV_VARS,R as CompilationError,te as ConditionalNode,oe as ContextLoader,X as DEFAULT_OUTPUT_BASE,Wt as EVENTS_FILE,de as NODE_DEFAULT_TOOLS,j as Node,V as OutputParser,Ft as RAW_OUTPUT_FILE,Dt as RESULT_FILE,we as SESSIONS_DIR,B as SESSION_INFO_FILE,Ut as SKILLS,ye as STOP_REQUEST_FILE,jt as SchemaTypes,le as Timeline,Qe as WORKFLOW_GRAPH_LOG_MARKER_PREFIX,re as WorkflowGraph,K as WorkflowState,_t as clearInheritedSessionEnvForFreshRun,nt as clearSkills,io as compileGraph,co as extractSteps,fo as generateNodeConfigsJson,po as generateWorkflowCode,$t as generateWorkflowSessionId,ve as getAgentStrategy,ot as getAllSkills,Oe as getNodeImpl,Ce as getNodeTemplate,Pe as getResolvedToolDefinitions,Y as getSkill,ue as hasNode,tt as hasSkill,ct as invokeAgent,no as listNodeTypes,rt as listSkillIds,at as listStrategies,yt as readPinnedSessionPathFromEnv,Tt as registerNode,et as registerSkill,it as registerStrategy,Re as resolveNodeTools,It as resolveWorkflowSession,Mt as setLogger,wt as shouldTrustInheritedSessionEnv,Et as syncProcessEnvToSession,b as timeline,ao as validateGraphConfig};
|
package/dist/node.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var st=Object.defineProperty;var b=(r,t)=>()=>(r&&(t=r(r=0)),t);var it=(r,t)=>{for(var e in t)st(r,e,{get:t[e],enumerable:!0})};var D,nt,N,l,
|
|
1
|
+
var st=Object.defineProperty;var b=(r,t)=>()=>(r&&(t=r(r=0)),t);var it=(r,t)=>{for(var e in t)st(r,e,{get:t[e],enumerable:!0})};var D,nt,N,l,W=b(()=>{D=()=>{},nt={debug:D,info:D,warn:(...r)=>console.warn("[workflow]",...r),error:(...r)=>console.error("[workflow]",...r)},N={impl:nt},l={debug:(...r)=>N.impl.debug?.(...r),info:(...r)=>N.impl.info?.(...r),warn:(...r)=>N.impl.warn?.(...r),error:(...r)=>N.impl.error?.(...r)}});var Z=b(()=>{});function V(r){return ut.get(r)||null}var v,ut,z=b(()=>{v=Symbol.for("@zibby/agent-workflow.skills");globalThis[v]||(globalThis[v]=new Map);ut=globalThis[v]});var X={};it(X,{getAgentStrategy:()=>q,invokeAgent:()=>dt,listStrategies:()=>pt,registerStrategy:()=>ct});function ct(r){if(!r||typeof r.getName!="function"||typeof r.invoke!="function")throw new Error("strategy must implement getName() and invoke() (AgentStrategy shape)");let t=_.findIndex(e=>e.getName()===r.getName());t>=0?_[t]=r:_.push(r)}function pt(){return _.map(r=>r.getName())}function q(r={}){let{state:t={},preferredAgent:e=null}=r,o=e||t.agentType||process.env.AGENT_TYPE;if(!o){let n=_.map(a=>a.getName()).join(", ")||"none registered";throw new Error(`No agent specified. Set agentType in state or AGENT_TYPE env var. Available: ${n}`)}l.debug(`[workflow] agent selection: requested=${o}`);let i=_.find(n=>n.getName()===o);if(!i){let n=_.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 l.debug(`[workflow] using agent: ${i.getName()}`),i}async function dt(r,t={},e={}){let o=q(t),i=t.state?.config||e.config||{},n=i.models||{},a=e.nodeName&&n[e.nodeName]||null,g=n.default||null,p=i.agent?.[o.name]?.model||null,S=a||g||p||e.model||null,E={...e,model:S,workspace:t.state?.workspace||e.workspace,schema:e.schema||t.schema,images:e.images||t.images||[],skills:e.skills||t.skills||[],config:i},s=r,h=E.skills||[];if(h.length>0&&!e.skipPromptFragments){let y=h.map(O=>{let $=V(O)?.promptFragment;return typeof $=="function"?$():$}).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
|
-
`),l.debug(`[workflow] prompt length: ${s.length} chars`),o.invoke(s,E)}var P,_,Q=b(()=>{Z();
|
|
12
|
+
`),l.debug(`[workflow] prompt length: ${s.length} chars`),o.invoke(s,E)}var P,_,Q=b(()=>{Z();W();z();P=Symbol.for("@zibby/agent-workflow.strategies");globalThis[P]||(globalThis[P]=[]);_=globalThis[P]});var k=class{constructor(t){this.schema=t}parse(t){let e=t.match(/```json\s*([\s\S]*?)\s*```/);if(e)return this.validate(JSON.parse(e[1]));let o=[t.match(/\{[\s\S]*?\}/),t.match(/\{[\s\S]*\}/)].filter(Boolean).map(i=>i[0]);for(let i of o)try{return this.validate(JSON.parse(i))}catch(n){if(!(n instanceof SyntaxError))throw n}return this.validate({result:t.trim()})}validate(t){let e=[];for(let[o,i]of Object.entries(this.schema)){if(i.required&&!(o in t)&&e.push(`Missing required field: ${o}`),o in t&&i.type){let n=typeof t[o];n!==i.type&&e.push(`Field '${o}' expected ${i.type}, got ${n}`)}if(i.validate&&o in t){let n=i.validate(t[o]);n&&e.push(`Field '${o}': ${n}`)}}if(e.length>0)throw new Error(`Output validation failed:
|
|
13
13
|
${e.join(`
|
|
14
|
-
`)}`);return t}};
|
|
14
|
+
`)}`);return t}};W();import{writeFileSync as C,readFileSync as tt,existsSync as et,mkdirSync as ht}from"node:fs";import{join as F,dirname as ft}from"node:path";import u from"chalk";var at="__WORKFLOW_GRAPH_LOG__",I=u.gray("\u2502"),lt=u.gray("\u250C"),G=u.gray("\u2514"),T=u.green("\u25C6"),U=u.hex("#c084fc")("\u25C6"),j=u.hex("#2dd4bf")("\u25C6"),x=u.red("\u25C6"),J=`${I} `,K=2;function B(r){return r<1e3?`${r}ms`:`${(r/1e3).toFixed(1)}s`}function H(r,t){return(e,o,i)=>{if(typeof e!="string")return r(e,o,i);let n=process.stdout.columns||120,a="";for(let g=0;g<e.length;g++){let p=e[g];t.lineStart&&(a+=J,t.col=K,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
|
-
${J}`,t.col=
|
|
16
|
+
${J}`,t.col=K))}return r(a,o,i)}}var A=class{constructor(){this._currentNode=null,this._origStdoutWrite=null,this._origStderrWrite=null,this._emitWorkflowGraphMarkers=String(process.env.ZIBBY_EMIT_GRAPH_MARKERS||"").trim()==="1"||String(process.env.ZIBBY_WORKFLOW_GRAPH_LOG_MARKERS||"").trim()==="1"}get isInsideNode(){return this._currentNode!==null}_startIntercepting(){this._origStdoutWrite=process.stdout.write.bind(process.stdout),this._origStderrWrite=process.stderr.write.bind(process.stderr);let t={lineStart:!0,col:0,inEsc:!1},e={lineStart:!0,col:0,inEsc:!1};this._outState=t,this._errState=e,process.stdout.write=H(this._origStdoutWrite,t),process.stderr.write=H(this._origStderrWrite,e)}_stopIntercepting(){this._origStdoutWrite&&(this._outState&&!this._outState.lineStart&&this._origStdoutWrite(`
|
|
17
17
|
`),process.stdout.write=this._origStdoutWrite),this._origStderrWrite&&(this._errState&&!this._errState.lineStart&&this._origStderrWrite(`
|
|
18
18
|
`),process.stderr.write=this._origStderrWrite),this._origStdoutWrite=null,this._origStderrWrite=null}_rawWrite(t){(this._origStdoutWrite||process.stdout.write.bind(process.stdout))(`${t}
|
|
19
19
|
`)}_emitGraphLogMarker(t){if(!this._emitWorkflowGraphMarkers)return;let e=`${at}${JSON.stringify(t)}
|
|
20
20
|
`;this._origStdoutWrite?this._origStdoutWrite(e):process.stdout.write(e)}_writeDot(t,e){this._origStdoutWrite?(this._outState&&!this._outState.lineStart&&(this._origStdoutWrite(`
|
|
21
21
|
`),this._outState.lineStart=!0,this._outState.col=0),this._origStdoutWrite(`${t} ${e}
|
|
22
22
|
`)):process.stdout.write.bind(process.stdout)(`${t} ${e}
|
|
23
|
-
`)}step(t){this._origStdoutWrite?this._writeDot(
|
|
24
|
-
`)}stepInfo(t){this.step(t)}stepTool(t){this._origStdoutWrite?this._writeDot(U,t):process.stdout.write.bind(process.stdout)(`${
|
|
25
|
-
`)}stepMemory(t){let e=u.hex("#2dd4bf")(t);this._origStdoutWrite?this._writeDot(j,e):process.stdout.write.bind(process.stdout)(`${
|
|
26
|
-
`)}stepFail(t){this._origStdoutWrite?this._writeDot(
|
|
27
|
-
`)}nodeStart(t){this._currentNode=t,this._emitGraphLogMarker({phase:"node_begin",node:t}),this._rawWrite(`${lt} ${t}`),this._startIntercepting()}nodeComplete(t,e={}){this._stopIntercepting();let{duration:o,details:i}=e;if(i)for(let a of i)this._rawWrite(`${
|
|
23
|
+
`)}step(t){this._origStdoutWrite?this._writeDot(T,t):process.stdout.write.bind(process.stdout)(`${I} ${T} ${t}
|
|
24
|
+
`)}stepInfo(t){this.step(t)}stepTool(t){this._origStdoutWrite?this._writeDot(U,t):process.stdout.write.bind(process.stdout)(`${I} ${U} ${t}
|
|
25
|
+
`)}stepMemory(t){let e=u.hex("#2dd4bf")(t);this._origStdoutWrite?this._writeDot(j,e):process.stdout.write.bind(process.stdout)(`${I} ${j} ${e}
|
|
26
|
+
`)}stepFail(t){this._origStdoutWrite?this._writeDot(x,u.red(t)):process.stdout.write.bind(process.stdout)(`${I} ${x} ${u.red(t)}
|
|
27
|
+
`)}nodeStart(t){this._currentNode=t,this._emitGraphLogMarker({phase:"node_begin",node:t}),this._rawWrite(`${lt} ${t}`),this._startIntercepting()}nodeComplete(t,e={}){this._stopIntercepting();let{duration:o,details:i}=e;if(i)for(let a of i)this._rawWrite(`${T} ${a}`);let n=o?u.dim(` ${B(o)}`):"";this._rawWrite(`${G} ${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(` ${B(i)}`):"";this._rawWrite(`${x} ${u.red(e)}`),this._rawWrite(`${G} ${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(){}},Y=new A;var L=".session-info.json";var M=class{constructor(t){if(this.config=t,this.name=t.name,this.prompt=t.prompt,this.outputSchema=t.outputSchema,!this.outputSchema&&!t._isCustomCode)throw new Error(`Node '${this.name}' must define outputSchema (Zod schema). This defines the contract for what the node returns to state.`);this.isZodSchema=this.outputSchema&&typeof this.outputSchema._def<"u",this.parser=t.outputSchema&&!this.isZodSchema?new k(t.outputSchema):null,this.retries=t.retries||0,this.onComplete=t.onComplete,this.customExecute=t.execute}async execute(t,e){let o=()=>e&&typeof e.getAll=="function"?e.getAll():t,i=s=>e&&typeof e.get=="function"?e.get(s):t?.[s];if(typeof this.customExecute=="function"){l.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?(l.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 l.error(`[workflow] node '${this.name}' failed: ${s.message}`),s.name==="ZodError"&&l.error(`Schema errors: ${JSON.stringify(s.issues||s.errors,null,2)}`),{success:!1,error:s.message,raw:null}}}let n=typeof this.prompt=="function"?this.prompt(o()):this.prompt,a=i("_skillHints");a&&(n=`${a}
|
|
28
28
|
|
|
29
|
-
${n}`);let g=o(),p=g.cwd||process.cwd(),
|
|
29
|
+
${n}`);let g=o(),p=g.cwd||process.cwd(),S=g.sessionPath;try{if(S){let s=F(S,L);if(et(s)){let f=JSON.parse(tt(s,"utf-8"));f.currentNode=this.name,C(s,JSON.stringify(f,null,2),"utf-8")}let h=F(S,"..",L);if(et(h))try{let f=JSON.parse(tt(h,"utf-8"));f.currentNode=this.name,C(h,JSON.stringify(f,null,2),"utf-8")}catch{}}}catch(s){l.debug(`[workflow] could not update session info: ${s.message}`)}let E=null;for(let s=0;s<=this.retries;s++)try{l.debug(`[workflow] node '${this.name}' attempt ${s}`);let h=o().config||{},f=h.agents||{},y=this.config.agent??f[this.name]??null,O={state:o()};y&&(O.preferredAgent=y);let $={workspace:p,schema:this.isZodSchema?this.outputSchema:null,skills:this.config.skills||[],sessionPath:S,config:h,nodeName:this.name,timeout:this.config?.timeout||3e5},R=t?._coreInvokeAgent;R||(R=(await Promise.resolve().then(()=>(Q(),X))).invokeAgent);let m=await R(n,O,$),d,w;if(typeof m=="string"?(d=m,w=null):m.structured?(d=m.raw||JSON.stringify(m.structured,null,2),w=m.structured):(d=m.raw||JSON.stringify(m,null,2),w=m.extracted||null),S)try{let c=F(S,this.name,"raw_stream_output.txt");ht(ft(c),{recursive:!0}),C(c,typeof d=="string"?d:JSON.stringify(d),"utf-8")}catch(c){l.debug(`[workflow] could not save raw output: ${c.message}`)}if(this.isZodSchema&&w){l.info(`[workflow] node '${this.name}': output validated: ${JSON.stringify(w,null,2)}`);let c=w;if(typeof this.onComplete=="function")try{c=await this.onComplete(o(),w)}catch(ot){l.warn(`[workflow] onComplete hook failed: ${ot.message}`)}return{success:!0,output:c,raw:d}}if(typeof this.onComplete=="function")try{return{success:!0,output:await this.onComplete(o(),{raw:d}),raw:d}}catch(c){throw new Error(`onComplete failed: ${c.message}`,{cause:c})}if(this.parser){let c=this.parser.parse(d);return l.info(`[workflow] node '${this.name}': parsed output: ${JSON.stringify(c,null,2)}`),Y.step("Output parsed"),{success:!0,output:c,raw:d}}return{success:!0,output:d,raw:d}}catch(h){E=h,s<this.retries&&l.info(`[workflow] node '${this.name}' failed, retrying (${s+1}/${this.retries})\u2026`)}return{success:!1,error:E.message,raw:null}}},rt=class extends M{constructor(t){super({...t,_isCustomCode:!0}),this.condition=t.condition}async execute(t,e){let o=e&&typeof e.getAll=="function"?e.getAll():t;return{success:!0,output:{nextNode:this.condition(o)},raw:null}}};export{rt as ConditionalNode,M as Node};
|