@zibby/agent-workflow 0.4.16 → 0.4.17
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/graph-compiler.js +18 -18
- package/dist/graph.d.ts +17 -0
- package/dist/graph.js +17 -17
- package/dist/index.js +25 -25
- package/package.json +1 -1
package/dist/graph-compiler.js
CHANGED
|
@@ -1,45 +1,45 @@
|
|
|
1
|
-
var
|
|
1
|
+
var Dt=Object.defineProperty;var we=(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 ae=(o,e)=>()=>(o&&(e=o(o=0)),e);var He=(o,e)=>{for(var t in e)Dt(o,t,{get:e[t],enumerable:!0})};var Je,Lt,le,y,F=ae(()=>{Je=()=>{},Lt={debug:Je,info:Je,warn:(...o)=>console.warn("[workflow]",...o),error:(...o)=>console.error("[workflow]",...o)},le={impl:Lt},y={debug:(...o)=>le.impl.debug?.(...o),info:(...o)=>le.impl.info?.(...o),warn:(...o)=>le.impl.warn?.(...o),error:(...o)=>le.impl.error?.(...o)}});var ot=ae(()=>{});var rt={};He(rt,{clearSkills:()=>Yt,getAllSkills:()=>Ht,getSkill:()=>Q,hasSkill:()=>Gt,listSkillIds:()=>Jt,registerSkill:()=>Wt});function Wt(o){if(!o||typeof o.id!="string")throw new Error("Skill definition must include a string id");q.set(o.id,Object.freeze({...o}))}function Q(o){return q.get(o)||null}function Gt(o){return q.has(o)}function Ht(){return new Map(q)}function Jt(){return Array.from(q.keys())}function Yt(){q.clear()}var $e,q,pe=ae(()=>{$e=Symbol.for("@zibby/agent-workflow.skills");globalThis[$e]||(globalThis[$e]=new Map);q=globalThis[$e]});var ee={};He(ee,{getAgentStrategy:()=>nt,invokeAgent:()=>Kt,listStrategies:()=>zt,registerStrategy:()=>Zt});function Zt(o){if(!o||typeof o.getName!="function"||typeof o.invoke!="function")throw new Error("strategy must implement getName() and invoke() (AgentStrategy shape)");let e=W.findIndex(t=>t.getName()===o.getName());e>=0?W[e]=o:W.push(o)}function zt(){return W.map(o=>o.getName())}function nt(o={}){let{state:e={},preferredAgent:t=null}=o,r=t||e.agentType||process.env.AGENT_TYPE;if(!r){let n=W.map(a=>a.getName()).join(", ")||"none registered";throw new Error(`No agent specified. Set agentType in state or AGENT_TYPE env var. Available: ${n}`)}y.debug(`[workflow] agent selection: requested=${r}`);let s=W.find(n=>n.getName()===r);if(!s){let n=W.map(a=>a.getName()).join(", ")||"none registered";throw new Error(`Unknown agent '${r}'. Available: ${n}`)}if(!s.canHandle(o))throw new Error(`Agent '${r}' is not available in this environment. Check credentials/environment.`);return y.debug(`[workflow] using agent: ${s.getName()}`),s}async function Kt(o,e={},t={}){let r=nt(e),s=e.state?.config||t.config||{},n=s.models||{},a=t.nodeName&&n[t.nodeName]||null,i=n.default||null,u=s.agent?.[r.name]?.model||null,l=a||i||u||t.model||null,p={...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:s},c=o,d=p.skills||[];if(d.length>0&&!t.skipPromptFragments){let w=d.map(_=>{let $=Q(_)?.promptFragment;return typeof $=="function"?$():$}).filter(Boolean);w.length>0&&(c+=`
|
|
2
2
|
|
|
3
3
|
${w.join(`
|
|
4
4
|
|
|
5
|
-
`)}`)}let
|
|
5
|
+
`)}`)}let h=e.state?._currentNodeConfig?.extraPromptInstructions?.trim();return h&&(c+=`
|
|
6
6
|
|
|
7
7
|
\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501
|
|
8
8
|
PRIORITY OVERRIDE \u2014 THE FOLLOWING INSTRUCTIONS TAKE PRECEDENCE OVER ALL PREVIOUS CONTENT
|
|
9
9
|
\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501
|
|
10
10
|
|
|
11
|
-
${
|
|
12
|
-
`),y.debug(`[workflow] prompt length: ${
|
|
11
|
+
${h}
|
|
12
|
+
`),y.debug(`[workflow] prompt length: ${c.length} chars`),r.invoke(c,p)}var Te,W,te=ae(()=>{ot();F();pe();Te=Symbol.for("@zibby/agent-workflow.strategies");globalThis[Te]||(globalThis[Te]=[]);W=globalThis[Te]});var jt=new Set(["__proto__","constructor","prototype"]);function _e(o){if(jt.has(o))throw new Error(`Invalid state key: "${o}"`)}var ce=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){_e(e),this._history.push({...this._state}),this._state[e]=t}update(e){let t=Object.getOwnPropertyNames(e);for(let r of t)_e(r);this._history.push({...this._state});for(let r of t)this._state[r]=e[r]}append(e,t){_e(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())}};import G from"handlebars";var ue=class{constructor(e){this.schema=e}parse(e){let t=e.match(/```json\s*([\s\S]*?)\s*```/);if(t)return this.validate(JSON.parse(t[1]));let r=[e.match(/\{[\s\S]*?\}/),e.match(/\{[\s\S]*\}/)].filter(Boolean).map(s=>s[0]);for(let s of r)try{return this.validate(JSON.parse(s))}catch(n){if(!(n instanceof SyntaxError))throw n}return this.validate({result:e.trim()})}validate(e){let t=[];for(let[r,s]of Object.entries(this.schema)){if(s.required&&!(r in e)&&t.push(`Missing required field: ${r}`),r in e&&s.type){let n=typeof e[r];n!==s.type&&t.push(`Field '${r}' expected ${s.type}, got ${n}`)}if(s.validate&&r in e){let n=s.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}};F();import{writeFileSync as ve,readFileSync as
|
|
15
|
-
`?(a+=
|
|
16
|
-
${
|
|
14
|
+
`)}`);return e}};F();import{writeFileSync as ve,readFileSync as st,existsSync as it,mkdirSync as qt}from"node:fs";import{join as Ae,dirname as Vt}from"node:path";import k from"chalk";var Ut="__WORKFLOW_GRAPH_LOG__",X=k.gray("\u2502"),Ft=k.gray("\u250C"),Ye=k.gray("\u2514"),Ie=k.green("\u25C6"),Ze=k.hex("#c084fc")("\u25C6"),ze=k.hex("#2dd4bf")("\u25C6"),Ee=k.red("\u25C6"),Ke=`${X} `,qe=2;function Ve(o){return o<1e3?`${o}ms`:`${(o/1e3).toFixed(1)}s`}function Xe(o,e){return(t,r,s)=>{if(typeof t!="string")return o(t,r,s);let n=process.stdout.columns||120,a="";for(let i=0;i<t.length;i++){let u=t[i];e.lineStart&&(a+=Ke,e.col=qe,e.lineStart=!1),u===`
|
|
15
|
+
`?(a+=u,e.lineStart=!0,e.col=0,e.inEsc=!1):u==="\x1B"?(e.inEsc=!0,a+=u):e.inEsc?(a+=u,(u>="A"&&u<="Z"||u>="a"&&u<="z")&&(e.inEsc=!1)):(e.col++,a+=u,e.col>=n&&(a+=`
|
|
16
|
+
${Ke}`,e.col=qe))}return o(a,r,s)}}var be=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(`
|
|
18
18
|
`),process.stderr.write=this._origStderrWrite),this._origStdoutWrite=null,this._origStderrWrite=null}_rawWrite(e){(this._origStdoutWrite||process.stdout.write.bind(process.stdout))(`${e}
|
|
19
|
-
`)}_emitGraphLogMarker(e){if(!this._emitWorkflowGraphMarkers)return;let t=`${
|
|
19
|
+
`)}_emitGraphLogMarker(e){if(!this._emitWorkflowGraphMarkers)return;let t=`${Ut}${JSON.stringify(e)}
|
|
20
20
|
`;this._origStdoutWrite?this._origStdoutWrite(t):process.stdout.write(t)}_writeDot(e,t){this._origStdoutWrite?(this._outState&&!this._outState.lineStart&&(this._origStdoutWrite(`
|
|
21
21
|
`),this._outState.lineStart=!0,this._outState.col=0),this._origStdoutWrite(`${e} ${t}
|
|
22
22
|
`)):process.stdout.write.bind(process.stdout)(`${e} ${t}
|
|
23
23
|
`)}step(e){this._origStdoutWrite?this._writeDot(Ie,e):process.stdout.write.bind(process.stdout)(`${X} ${Ie} ${e}
|
|
24
|
-
`)}stepInfo(e){this.step(e)}stepTool(e){this._origStdoutWrite?this._writeDot(
|
|
25
|
-
`)}stepMemory(e){let t=
|
|
26
|
-
`)}stepFail(e){this._origStdoutWrite?this._writeDot(Ee,
|
|
27
|
-
`)}nodeStart(e){this._currentNode=e,this._emitGraphLogMarker({phase:"node_begin",node:e}),this._rawWrite(`${
|
|
24
|
+
`)}stepInfo(e){this.step(e)}stepTool(e){this._origStdoutWrite?this._writeDot(Ze,e):process.stdout.write.bind(process.stdout)(`${X} ${Ze} ${e}
|
|
25
|
+
`)}stepMemory(e){let t=k.hex("#2dd4bf")(e);this._origStdoutWrite?this._writeDot(ze,t):process.stdout.write.bind(process.stdout)(`${X} ${ze} ${t}
|
|
26
|
+
`)}stepFail(e){this._origStdoutWrite?this._writeDot(Ee,k.red(e)):process.stdout.write.bind(process.stdout)(`${X} ${Ee} ${k.red(e)}
|
|
27
|
+
`)}nodeStart(e){this._currentNode=e,this._emitGraphLogMarker({phase:"node_begin",node:e}),this._rawWrite(`${Ft} ${e}`),this._startIntercepting()}nodeComplete(e,t={}){this._stopIntercepting();let{duration:r,details:s}=t;if(s)for(let a of s)this._rawWrite(`${Ie} ${a}`);let n=r?k.dim(` ${Ve(r)}`):"";this._rawWrite(`${Ye} ${k.green("done")}${n}`),this._emitGraphLogMarker({phase:"node_end",node:e}),this._rawWrite("")}nodeFailed(e,t,r={}){this._stopIntercepting();let{duration:s}=r,n=s?k.dim(` ${Ve(s)}`):"";this._rawWrite(`${Ee} ${k.red(t)}`),this._rawWrite(`${Ye} ${k.red("failed")}${n}`),this._emitGraphLogMarker({phase:"node_end",node:e}),this._rawWrite("")}route(e,t){this._rawWrite(k.dim(` ${e} \u2192 ${t}`)),this._rawWrite("")}graphComplete(){}},x=new be;var de=".zibby/output",Qe="sessions",K=".session-info.json",et=".zibby-stop";var tt=["CI_JOB_ID","GITHUB_RUN_ID","CIRCLE_WORKFLOW_ID","BUILD_ID"];G.helpers.inc||G.registerHelper("inc",o=>Number(o)+1);G.helpers.json||G.registerHelper("json",o=>JSON.stringify(o,null,2));G.helpers.eq||G.registerHelper("eq",(o,e)=>o===e);var L=class{constructor(e){if(this.config=e,this.name=e.name,this.prompt=e.prompt,this.outputSchema=e.outputSchema,!this.outputSchema&&!e._isCustomCode)throw new Error(`Node '${this.name}' must define outputSchema (Zod schema). This defines the contract for what the node returns to state.`);this.isZodSchema=this.outputSchema&&typeof this.outputSchema._def<"u",this.parser=e.outputSchema&&!this.isZodSchema?new ue(e.outputSchema):null,this.retries=e.retries||0,this.onComplete=e.onComplete,this.customExecute=e.execute}async execute(e,t){let r=()=>t&&typeof t.getAll=="function"?t.getAll():e,s=c=>t&&typeof t.get=="function"?t.get(c):e?.[c];if(typeof this.customExecute=="function"){y.debug(`[workflow] node '${this.name}': custom execute (skipping LLM)`);try{let c=await this.customExecute(e);return typeof c=="object"&&c!==null&&c.success===!1?{success:!1,error:c.error||"Node execution failed",raw:c.raw||null}:this.isZodSchema?(y.debug(`[workflow] node '${this.name}': validating output schema`),{success:!0,output:this.outputSchema.parse(c),raw:null}):{success:!0,output:c,raw:null}}catch(c){return y.error(`[workflow] node '${this.name}' failed: ${c.message}`),c.name==="ZodError"&&y.error(`Schema errors: ${JSON.stringify(c.issues||c.errors,null,2)}`),{success:!1,error:c.message,raw:null}}}let n;typeof this.prompt=="function"?n=this.prompt(r()):typeof this.prompt=="string"&&this.prompt.includes("{{")?(this._compiledPrompt||(this._compiledPrompt=G.compile(this.prompt,{noEscape:!0})),n=this._compiledPrompt(r())):n=this.prompt;let a=s("_skillHints");a&&(n=`${a}
|
|
28
28
|
|
|
29
|
-
${n}`);let i=r(),c=i.cwd||process.cwd(),l=i.sessionPath;try{if(l){let u=Ae(l,K);if(st(u)){let f=JSON.parse(nt(u,"utf-8"));f.currentNode=this.name,ve(u,JSON.stringify(f,null,2),"utf-8")}let d=Ae(l,"..",K);if(st(d))try{let f=JSON.parse(nt(d,"utf-8"));f.currentNode=this.name,ve(d,JSON.stringify(f,null,2),"utf-8")}catch{}}}catch(u){y.debug(`[workflow] could not update session info: ${u.message}`)}let p=null;for(let u=0;u<=this.retries;u++)try{y.debug(`[workflow] node '${this.name}' attempt ${u}`);let d=r().config||{},f=d.agents||{},w=this.config.agent??f[this.name]??null,I={state:r()};w&&(I.preferredAgent=w);let $={workspace:c,schema:this.isZodSchema?this.outputSchema:null,skills:this.config.skills||[],sessionPath:l,config:d,nodeName:this.name,timeout:this.config?.timeout||3e5},_=e?._coreInvokeAgent;_||(_=(await Promise.resolve().then(()=>(te(),ee))).invokeAgent);let h=await _(n,I,$),E,g;if(typeof h=="string"?(E=h,g=null):h.structured?(E=h.raw||JSON.stringify(h.structured,null,2),g=h.structured):(E=h.raw||JSON.stringify(h,null,2),g=h.extracted||null),l)try{let m=Ae(l,this.name,"raw_stream_output.txt");Zt(zt(m),{recursive:!0}),ve(m,typeof E=="string"?E:JSON.stringify(E),"utf-8")}catch(m){y.debug(`[workflow] could not save raw output: ${m.message}`)}if(this.isZodSchema&&g){y.info(`[workflow] node '${this.name}': output validated: ${JSON.stringify(g,null,2)}`);let m=g;if(typeof this.onComplete=="function")try{m=await this.onComplete(r(),g)}catch(R){y.warn(`[workflow] onComplete hook failed: ${R.message}`)}return{success:!0,output:m,raw:E}}if(typeof this.onComplete=="function")try{return{success:!0,output:await this.onComplete(r(),{raw:E}),raw:E}}catch(m){throw new Error(`onComplete failed: ${m.message}`,{cause:m})}if(this.parser){let m=this.parser.parse(E);return y.info(`[workflow] node '${this.name}': parsed output: ${JSON.stringify(m,null,2)}`),x.step("Output parsed"),{success:!0,output:m,raw:E}}return{success:!0,output:E,raw:E}}catch(d){p=d,u<this.retries&&y.info(`[workflow] node '${this.name}' failed, retrying (${u+1}/${this.retries})\u2026`)}return{success:!1,error:p.message,raw:null}}},oe=class extends j{constructor(e){super({...e,_isCustomCode:!0}),this.condition=e.condition}async execute(e,t){let r=t&&typeof t.getAll=="function"?t.getAll():e;return{success:!0,output:{nextNode:this.condition(r)},raw:null}}};F();F();import{mkdirSync as Vt,existsSync as J,statSync as pt,readdirSync as ft,rmSync as Xt}from"node:fs";import{spawn as dt}from"node:child_process";import{join as U}from"node:path";import{pathToFileURL as Qt}from"node:url";import{AsyncLocalStorage as Kt}from"node:async_hooks";var xe=new Kt;function re(){let o=xe.getStore();return o||Object.freeze({executionId:process.env.EXECUTION_ID||null,parentExecutionId:process.env.PARENT_EXECUTION_ID||null,depth:0,conversationId:process.env.ZIBBY_CONVERSATION_ID||null,dispatchMode:process.env.DISPATCH_MODE||null})}function it(o,e){let t=xe.getStore()||re(),r=Object.freeze({executionId:o.executionId,parentExecutionId:o.parentExecutionId??t.executionId??null,depth:(t.depth||0)+(o.executionId!==t.executionId?1:0),conversationId:o.conversationId!==void 0?o.conversationId:t.conversationId??null,dispatchMode:o.dispatchMode??null});return xe.run(r,e)}var ke=new Map,Ne=new Map,at=new Map;function ct(o,e,t={}){if(!o||typeof o!="string")throw new Error("subgraph-registry.register: name required");if(typeof e!="function")throw new Error("subgraph-registry.register: factory must be a function");ke.set(o,e),Ne.set(o,"ready"),at.set(o,{...t,cachedAt:Date.now()})}function ut(o,e){Ne.set(o,"failed"),at.set(o,{error:e?.message||String(e),failedAt:Date.now()}),ke.delete(o)}function lt(o){return Ne.get(o)==="ready"?ke.get(o):null}var fe=process.env.ZIBBY_SUBGRAPH_CACHE_DIR||"/tmp/zibby/subgraphs";function eo(){return`node${(process.versions?.node||"").split(".")[0]||"unknown"}-${process.platform}-${process.arch}`}var k=class extends Error{constructor(e,t){super(`in-process sub-graph fallback: ${e}${t?` (${t})`:""}`),this.fallback=!0,this.reason=e,this.detail=t||null,this.name="SubgraphFallback"}};function to(){let o=(process.env.SUBGRAPH_INTERNAL_URL||"").replace(/\/$/,""),e=(process.env.PROGRESS_API_URL||"").replace(/\/executions\/?$/,""),t=o||e,r=process.env.PROJECT_ID,s=process.env.PROJECT_API_TOKEN;if(!t||!r||!s)throw new k("env","SUBGRAPH_INTERNAL_URL/PROGRESS_API_URL/PROJECT_ID/PROJECT_API_TOKEN missing");return{apiBase:t,projectId:r,authToken:s}}async function oo({apiBase:o,authToken:e,body:t}){let r;try{r=await fetch(`${o}/internal/subgraph/begin`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${e}`},body:JSON.stringify(t)})}catch(n){throw new k("network",`begin fetch failed: ${n.message}`)}let s=null;try{s=await r.json()}catch{}if(!r.ok){if(r.status===404){let n=new Error(`Sub-graph child '${t.childWorkflowType}' not found in project`);throw n.code="SUBGRAPH_NOT_FOUND",n.status=404,n}if(r.status===429){let n=s?.quotaInfo||{},a=new Error(`Sub-graph blocked by quota (${n.used??"?"}/${n.limit??"?"} on ${n.planId||"plan"})`);throw a.code="SUBGRAPH_QUOTA_EXCEEDED",a.status=429,a.quotaInfo=n,a}if(r.status===400&&s?.validationErrors){let n=new Error(`Sub-graph rejected input: ${s?.error||s?.message||"validation failed"}`);throw n.code="SUBGRAPH_INVALID_INPUT",n.status=400,n.validationErrors=s.validationErrors,n.missing=s.missing,n}throw new k("begin-status",`begin returned ${r.status}`)}return s?.data||s}async function H({apiBase:o,authToken:e,payload:t}){try{let r=await fetch(`${o}/internal/subgraph/finalize`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${e}`},body:JSON.stringify(t)});r.ok||y.warn(`[in-process subgraph] finalize returned ${r.status} for ${t.childExecutionId}`)}catch(r){y.warn(`[in-process subgraph] finalize failed: ${r.message}`)}}async function ro(o,e){let t=U(e,".ready"),r=U(e,"graph.mjs");if(J(t)&&J(r))return;Vt(e,{recursive:!0});let s=U(e,".lock"),n=!1;try{let{openSync:a,closeSync:i}=await import("node:fs"),c=a(s,"wx");i(c),n=!0}catch(a){if(a.code!=="EEXIST")throw a}if(!n){let a=Date.now()+3e4;for(;Date.now()<a;){if(J(t)&&J(r))return;await new Promise(i=>setTimeout(i,100))}throw new k("bundle-extract-timeout","sibling extract did not complete within 30s")}try{await new Promise((c,l)=>{let p=dt("curl",["-fsSL",o],{stdio:["ignore","pipe","inherit"]}),u=dt("tar",["-xzf","-","-C",e],{stdio:["pipe","inherit","inherit"]});p.stdout.pipe(u.stdin);let d,f,w=()=>{if(d!==void 0&&f!==void 0){if(d!==0)return l(new Error(`curl exited ${d}`));if(f!==0)return l(new Error(`tar exited ${f}`));c()}};p.on("close",I=>{d=I,w()}),u.on("close",I=>{f=I,w()}),p.on("error",l),u.on("error",l)});let{writeFileSync:a,unlinkSync:i}=await import("node:fs");a(t,"");try{i(s)}catch{}}catch(a){try{let{unlinkSync:i}=await import("node:fs");i(s)}catch{}throw new k("bundle-extract-failed",a.message)}}async function no(o){let e=U(o,"graph.mjs");if(!J(e))throw new k("entry-missing",`graph.mjs missing under ${o}`);let t;try{t=await import(Qt(e).href)}catch(s){throw new k("import-failed",`${s?.code||s?.name||"unknown"}: ${s.message}`)}let r=t.default||Object.values(t).find(s=>typeof s=="function"&&s.prototype?.buildGraph);if(!r)throw new k("entry-class-missing","no buildGraph() class export found");return r}async function ht(o,e={}){if(!o||typeof o!="string")throw new Error("runInProcessSubgraph: workflowName (string) is required");let t=re(),r;try{r=to()}catch(g){throw g}y.debug(`[in-process subgraph] begin '${o}' parent=${t.executionId||"<root>"}`);let s=await oo({apiBase:r.apiBase,authToken:r.authToken,body:{parentExecutionId:t.executionId,childWorkflowType:o,input:e.input||{},...e.conversationId?{conversationId:e.conversationId}:{}}}),{childExecutionId:n,runtimeTag:a,bundlePresignedUrl:i,sourcesPresignedUrl:c,workflowVersion:l,workflowUuid:p,bundleReady:u}=s,d=eo();if(a&&a!==d)throw await H({apiBase:r.apiBase,authToken:r.authToken,payload:{childExecutionId:n,status:"canceled",error:{message:`runtimeTag mismatch: parent=${d} child=${a}`,code:"RUNTIME_MISMATCH"}}}),new k("runtime-mismatch",`${d} vs ${a}`);if(!u||!i)throw await H({apiBase:r.apiBase,authToken:r.authToken,payload:{childExecutionId:n,status:"canceled",error:{message:"bundle not ready for in-process; falling back to HTTP",code:"NO_BUNDLE"}}}),new k("no-bundle","workflow bundle not built yet");let f=lt(o);if(!f){let g=U(fe,`${p}@${l||"0"}`);try{await ro(i,g);try{io()}catch{}}catch(m){throw m.fallback&&await H({apiBase:r.apiBase,authToken:r.authToken,payload:{childExecutionId:n,status:"failed",error:{message:m.message,code:m.reason}}}),m}try{f=await no(g),ct(o,f,{workflowUuid:p,version:l,runtimeTag:a,cacheDir:g})}catch(m){throw ut(o,m),await H({apiBase:r.apiBase,authToken:r.authToken,payload:{childExecutionId:n,status:"failed",error:{message:m.message,code:m.reason||"IMPORT_FAILED"}}}),m.fallback?m:new k("import-failed",m.message)}}let w=Date.now(),$=await(typeof f=="function"&&f.prototype?.buildGraph?new f:f).buildGraph(),_={...e.input||{}},h,E;try{h=await it({executionId:n,parentExecutionId:t.executionId,conversationId:e.conversationId!==void 0?e.conversationId:t.conversationId,dispatchMode:"inprocess"},()=>$.run(e.parentAgent,_,{signal:e.signal})),E=h&&typeof h=="object"&&"state"in h?h.state:h}catch(g){throw await H({apiBase:r.apiBase,authToken:r.authToken,payload:{childExecutionId:n,status:"failed",error:{message:g.message,code:g.code||"CHILD_THREW",stack:g.stack},durationMs:Date.now()-w}}),g}if(h&&typeof h=="object"&&h.stoppedExternally){await H({apiBase:r.apiBase,authToken:r.authToken,payload:{childExecutionId:n,status:"canceled",finalState:E,durationMs:Date.now()-w}});let g=new Error(`Sub-graph '${o}' canceled by parent abort`);throw g.code="SUBGRAPH_CANCELED",g.subgraphJobId=n,g}return await H({apiBase:r.apiBase,authToken:r.authToken,payload:{childExecutionId:n,status:"completed",finalState:E,durationMs:Date.now()-w}}),{finalState:E,executionId:n}}function so(o){let e=0,t=[o];for(;t.length;){let r=t.pop(),s;try{s=pt(r)}catch{continue}if(s.isDirectory()){let n;try{n=ft(r)}catch{continue}for(let a of n)t.push(U(r,a))}else e+=s.size}return e}function io({cap:o=Number(process.env.ZIBBY_SUBGRAPH_CACHE_CAP_BYTES||2*1024*1024*1024)}={}){try{if(!J(fe))return{evicted:0,freedBytes:0};let e=ft(fe),t=[],r=0;for(let i of e){let c=U(fe,i),l;try{l=pt(c)}catch{continue}let p=l.isDirectory()?so(c):l.size;r+=p,t.push({name:i,full:c,size:p,mtimeMs:l.mtimeMs})}if(r<=o)return{evicted:0,freedBytes:0,totalBytes:r};t.sort((i,c)=>i.mtimeMs-c.mtimeMs);let s=Math.floor(o*.7),n=0,a=0;for(let i of t){if(r-n<=s)break;if(!J(U(i.full,".lock")))try{Xt(i.full,{recursive:!0,force:!0}),n+=i.size,a+=1}catch(c){y.debug(`[sub-graph cache] evict skip ${i.name}: ${c.message}`)}}return a>0&&y.info(`[sub-graph cache] evicted ${a} entr(y/ies), freed ${(n/1024/1024).toFixed(1)}MB`),{evicted:a,freedBytes:n,totalBytes:r-n}}catch(e){return y.debug(`[sub-graph cache] evict failed: ${e.message}`),{evicted:0,freedBytes:0}}}var ao=2e3,co=600*1e3,uo=new Set(["completed","failed","canceled","timeout"]);function lo(){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 po(){let o=process.env.PROJECT_ID;if(!o)throw new Error("Sub-graph dispatch requires PROJECT_ID env var.");return o}function fo(){let o=process.env.PROJECT_API_TOKEN;if(!o)throw new Error("Sub-graph dispatch requires PROJECT_API_TOKEN env var.");return o}function ho(){return process.env.EXECUTION_ID||null}function gt(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 mt(o,e={}){if(!o||typeof o!="string")throw new Error("dispatchSubgraph: workflowName (string) is required");let t=re(),r=Number(process.env.ZIBBY_SUBGRAPH_MAX_DEPTH||10);if((t.depth||0)>=r)throw new Error(`dispatchSubgraph('${o}'): sub-graph depth ${t.depth} reached cap of ${r}. Restructure the graph or raise ZIBBY_SUBGRAPH_MAX_DEPTH.`);if(process.env.ZIBBY_INPROCESS_SUBGRAPH!=="0"&&!e.async)try{y.debug(`[sub-graph] trying in-process for '${o}'`);let{finalState:g}=await ht(o,{input:e.input,conversationId:e.conversationId,signal:e.signal,parentAgent:e.parentAgent}),m=gt(g,e.output);return y.info(`[sub-graph] '${o}' completed in-process`),m}catch(g){if(g instanceof k||g?.fallback)y.info(`[sub-graph] in-process fallback for '${o}': ${g.reason||"unknown"} \u2014 using HTTP`);else throw g}let s=lo(),n=po(),a=fo(),i=ho(),c=`${s}/projects/${encodeURIComponent(n)}/workflows/${encodeURIComponent(o)}/trigger`,l={input:e.input||{},...i?{parentExecutionId:i}:{},...e.conversationId?{conversationId:e.conversationId}:{}};y.info(`[sub-graph] dispatching '${o}' (${e.async?"async":"sync"}) from parent ${i||"<none>"}`);let p=await fetch(c,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${a}`},body:JSON.stringify(l)});if(!p.ok){let g=null,m="";try{g=await p.json(),m=g?.error||g?.message||JSON.stringify(g)}catch{m=await p.text().catch(()=>"")}if(p.status===429){let v=g?.quotaInfo||{},M=new Error(`Sub-graph '${o}' blocked by execution quota (${v.used??"?"}/${v.limit??"?"} on plan ${v.planId||"unknown"}). Sub-workflow runs count toward the same monthly cap as user-triggered runs.`);throw M.code="SUBGRAPH_QUOTA_EXCEEDED",M.status=429,M.subgraph=o,M.quotaInfo=v,M}if(p.status===400){let v=new Error(`Sub-graph '${o}' rejected input: ${m}`);throw v.code="SUBGRAPH_INVALID_INPUT",v.status=400,v.subgraph=o,v.validationErrors=g?.validationErrors||null,v.missing=g?.missing||null,v}let R=new Error(`Sub-graph '${o}' trigger rejected (${p.status}): ${m}`);throw R.code="SUBGRAPH_TRIGGER_FAILED",R.status=p.status,R.subgraph=o,R}let u=await p.json(),d=u?.data?.jobId||u?.jobId;if(!d)throw new Error(`Sub-graph '${o}' trigger returned no jobId: ${JSON.stringify(u).slice(0,200)}`);if(e.async)return y.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:co,w=Number.isFinite(e.pollIntervalMs)?e.pollIntervalMs:ao,I=`${s}/executions/${encodeURIComponent(d)}`,$=Date.now()+f,_="accepted",h=0;for(;Date.now()<$;){await new Promise(v=>setTimeout(v,w)),h+=1;let g=await fetch(I,{headers:{Authorization:`Bearer ${a}`}});if(!g.ok){if(g.status>=500){y.warn(`[sub-graph] status poll for ${d} returned ${g.status}, will retry`);continue}throw new Error(`Sub-graph status poll failed for ${d}: ${g.status}`)}let m=await g.json(),R=m?.data||m?.execution||m;if(_=R?.status||_,uo.has(_)){if(_!=="completed"){let S=new Error(`Sub-graph '${o}' (${d}) ended in status '${_}'`);throw S.subgraphJobId=d,S.subgraphStatus=_,S}let v=R?.finalState||R?.state||{},M=gt(v,e.output);return y.info(`[sub-graph] '${o}' (${d}) completed after ${h} polls`),M}}let E=new Error(`Sub-graph '${o}' (${d}) timed out after ${Math.round(f/1e3)}s (last status: ${_})`);throw E.subgraphJobId=d,E.subgraphStatus=_,E}import{existsSync as St,readFileSync as go}from"node:fs";import{join as Oe,dirname as yt}from"node:path";var he=class{static async loadContext(e,t,r={}){let s={},n=r.filenames||["CONTEXT.md","AGENTS.md"];if(e){let i=yt(Oe(t,e));for(let c of n){let l=await this.findAndMergeContextFiles(c,i,t);if(l){let p=c.replace(/\.[^.]+$/,"").toLowerCase();s[p]=l}}}let a=r.discovery||{};for(let[i,c]of Object.entries(a))try{let l=Oe(t,c);St(l)&&(s[i]=await this.loadFile(l))}catch(l){console.warn(`[workflow] could not load context '${i}' from '${c}': ${l.message}`)}return s}static async findAndMergeContextFiles(e,t,r){let s=[],n=t;for(;n.startsWith(r);){let a=Oe(n,e);if(St(a))try{s.unshift(await this.loadFile(a))}catch(c){console.warn(`[workflow] could not load ${e} from ${a}: ${c.message}`)}let i=yt(n);if(i===n)break;n=i}return s.length===0?null:s.every(a=>typeof a=="string")?s.join(`
|
|
29
|
+
${n}`);let i=r(),u=i.cwd||process.cwd(),l=i.sessionPath;try{if(l){let c=Ae(l,K);if(it(c)){let h=JSON.parse(st(c,"utf-8"));h.currentNode=this.name,ve(c,JSON.stringify(h,null,2),"utf-8")}let d=Ae(l,"..",K);if(it(d))try{let h=JSON.parse(st(d,"utf-8"));h.currentNode=this.name,ve(d,JSON.stringify(h,null,2),"utf-8")}catch{}}}catch(c){y.debug(`[workflow] could not update session info: ${c.message}`)}let p=null;for(let c=0;c<=this.retries;c++)try{y.debug(`[workflow] node '${this.name}' attempt ${c}`);let d=r().config||{},h=d.agents||{},w=this.config.agent??h[this.name]??null,_={state:r()};w&&(_.preferredAgent=w);let $={workspace:u,schema:this.isZodSchema?this.outputSchema:null,skills:this.config.skills||[],sessionPath:l,config:d,nodeName:this.name,timeout:this.config?.timeout||3e5},b=e?._coreInvokeAgent;b||(b=(await Promise.resolve().then(()=>(te(),ee))).invokeAgent);let f=await b(n,_,$),I,g;if(typeof f=="string"?(I=f,g=null):f.structured?(I=f.raw||JSON.stringify(f.structured,null,2),g=f.structured):(I=f.raw||JSON.stringify(f,null,2),g=f.extracted||null),l)try{let m=Ae(l,this.name,"raw_stream_output.txt");qt(Vt(m),{recursive:!0}),ve(m,typeof I=="string"?I:JSON.stringify(I),"utf-8")}catch(m){y.debug(`[workflow] could not save raw output: ${m.message}`)}if(this.isZodSchema&&g){y.info(`[workflow] node '${this.name}': output validated: ${JSON.stringify(g,null,2)}`);let m=g;if(typeof this.onComplete=="function")try{m=await this.onComplete(r(),g)}catch(R){y.warn(`[workflow] onComplete hook failed: ${R.message}`)}return{success:!0,output:m,raw:I}}if(typeof this.onComplete=="function")try{return{success:!0,output:await this.onComplete(r(),{raw:I}),raw:I}}catch(m){throw new Error(`onComplete failed: ${m.message}`,{cause:m})}if(this.parser){let m=this.parser.parse(I);return y.info(`[workflow] node '${this.name}': parsed output: ${JSON.stringify(m,null,2)}`),x.step("Output parsed"),{success:!0,output:m,raw:I}}return{success:!0,output:I,raw:I}}catch(d){p=d,c<this.retries&&y.info(`[workflow] node '${this.name}' failed, retrying (${c+1}/${this.retries})\u2026`)}return{success:!1,error:p.message,raw:null}}},oe=class extends L{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}}};F();F();import{mkdirSync as eo,existsSync as J,statSync as ft,readdirSync as ht,rmSync as to}from"node:fs";import{spawn as pt}from"node:child_process";import{join as U}from"node:path";import{pathToFileURL as oo}from"node:url";import{AsyncLocalStorage as Xt}from"node:async_hooks";var ke=new Xt;function re(){let o=ke.getStore();return o||Object.freeze({executionId:process.env.EXECUTION_ID||null,parentExecutionId:process.env.PARENT_EXECUTION_ID||null,depth:0,conversationId:process.env.ZIBBY_CONVERSATION_ID||null,dispatchMode:process.env.DISPATCH_MODE||null})}function at(o,e){let t=ke.getStore()||re(),r=Object.freeze({executionId:o.executionId,parentExecutionId:o.parentExecutionId??t.executionId??null,depth:(t.depth||0)+(o.executionId!==t.executionId?1:0),conversationId:o.conversationId!==void 0?o.conversationId:t.conversationId??null,dispatchMode:o.dispatchMode??null});return ke.run(r,e)}var xe=new Map,Oe=new Map,ct=new Map;function ut(o,e,t={}){if(!o||typeof o!="string")throw new Error("subgraph-registry.register: name required");if(typeof e!="function")throw new Error("subgraph-registry.register: factory must be a function");xe.set(o,e),Oe.set(o,"ready"),ct.set(o,{...t,cachedAt:Date.now()})}function lt(o,e){Oe.set(o,"failed"),ct.set(o,{error:e?.message||String(e),failedAt:Date.now()}),xe.delete(o)}function dt(o){return Oe.get(o)==="ready"?xe.get(o):null}var fe=process.env.ZIBBY_SUBGRAPH_CACHE_DIR||"/tmp/zibby/subgraphs";function ro(){return`node${(process.versions?.node||"").split(".")[0]||"unknown"}-${process.platform}-${process.arch}`}var O=class extends Error{constructor(e,t){super(`in-process sub-graph fallback: ${e}${t?` (${t})`:""}`),this.fallback=!0,this.reason=e,this.detail=t||null,this.name="SubgraphFallback"}};function no(){let o=(process.env.SUBGRAPH_INTERNAL_URL||"").replace(/\/$/,""),e=(process.env.PROGRESS_API_URL||"").replace(/\/executions\/?$/,""),t=o||e,r=process.env.PROJECT_ID,s=process.env.PROJECT_API_TOKEN;if(!t||!r||!s)throw new O("env","SUBGRAPH_INTERNAL_URL/PROGRESS_API_URL/PROJECT_ID/PROJECT_API_TOKEN missing");return{apiBase:t,projectId:r,authToken:s}}async function so({apiBase:o,authToken:e,body:t}){let r;try{r=await fetch(`${o}/internal/subgraph/begin`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${e}`},body:JSON.stringify(t)})}catch(n){throw new O("network",`begin fetch failed: ${n.message}`)}let s=null;try{s=await r.json()}catch{}if(!r.ok){if(r.status===404){let n=new Error(`Sub-graph child '${t.childWorkflowType}' not found in project`);throw n.code="SUBGRAPH_NOT_FOUND",n.status=404,n}if(r.status===429){let n=s?.quotaInfo||{},a=new Error(`Sub-graph blocked by quota (${n.used??"?"}/${n.limit??"?"} on ${n.planId||"plan"})`);throw a.code="SUBGRAPH_QUOTA_EXCEEDED",a.status=429,a.quotaInfo=n,a}if(r.status===400&&s?.validationErrors){let n=new Error(`Sub-graph rejected input: ${s?.error||s?.message||"validation failed"}`);throw n.code="SUBGRAPH_INVALID_INPUT",n.status=400,n.validationErrors=s.validationErrors,n.missing=s.missing,n}throw new O("begin-status",`begin returned ${r.status}`)}return s?.data||s}async function H({apiBase:o,authToken:e,payload:t}){try{let r=await fetch(`${o}/internal/subgraph/finalize`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${e}`},body:JSON.stringify(t)});r.ok||y.warn(`[in-process subgraph] finalize returned ${r.status} for ${t.childExecutionId}`)}catch(r){y.warn(`[in-process subgraph] finalize failed: ${r.message}`)}}async function io(o,e){let t=U(e,".ready"),r=U(e,"graph.mjs");if(J(t)&&J(r))return;eo(e,{recursive:!0});let s=U(e,".lock"),n=!1;try{let{openSync:a,closeSync:i}=await import("node:fs"),u=a(s,"wx");i(u),n=!0}catch(a){if(a.code!=="EEXIST")throw a}if(!n){let a=Date.now()+3e4;for(;Date.now()<a;){if(J(t)&&J(r))return;await new Promise(i=>setTimeout(i,100))}throw new O("bundle-extract-timeout","sibling extract did not complete within 30s")}try{await new Promise((u,l)=>{let p=pt("curl",["-fsSL",o],{stdio:["ignore","pipe","inherit"]}),c=pt("tar",["-xzf","-","-C",e],{stdio:["pipe","inherit","inherit"]});p.stdout.pipe(c.stdin);let d,h,w=()=>{if(d!==void 0&&h!==void 0){if(d!==0)return l(new Error(`curl exited ${d}`));if(h!==0)return l(new Error(`tar exited ${h}`));u()}};p.on("close",_=>{d=_,w()}),c.on("close",_=>{h=_,w()}),p.on("error",l),c.on("error",l)});let{writeFileSync:a,unlinkSync:i}=await import("node:fs");a(t,"");try{i(s)}catch{}}catch(a){try{let{unlinkSync:i}=await import("node:fs");i(s)}catch{}throw new O("bundle-extract-failed",a.message)}}async function ao(o){let e=U(o,"graph.mjs");if(!J(e))throw new O("entry-missing",`graph.mjs missing under ${o}`);let t;try{t=await import(oo(e).href)}catch(s){throw new O("import-failed",`${s?.code||s?.name||"unknown"}: ${s.message}`)}let r=t.default||Object.values(t).find(s=>typeof s=="function"&&s.prototype?.buildGraph);if(!r)throw new O("entry-class-missing","no buildGraph() class export found");return r}async function gt(o,e={}){if(!o||typeof o!="string")throw new Error("runInProcessSubgraph: workflowName (string) is required");let t=re(),r;try{r=no()}catch(g){throw g}y.debug(`[in-process subgraph] begin '${o}' parent=${t.executionId||"<root>"}`);let s=await so({apiBase:r.apiBase,authToken:r.authToken,body:{parentExecutionId:t.executionId,childWorkflowType:o,input:e.input||{},...e.conversationId?{conversationId:e.conversationId}:{}}}),{childExecutionId:n,runtimeTag:a,bundlePresignedUrl:i,sourcesPresignedUrl:u,workflowVersion:l,workflowUuid:p,bundleReady:c}=s,d=ro();if(a&&a!==d)throw await H({apiBase:r.apiBase,authToken:r.authToken,payload:{childExecutionId:n,status:"canceled",error:{message:`runtimeTag mismatch: parent=${d} child=${a}`,code:"RUNTIME_MISMATCH"}}}),new O("runtime-mismatch",`${d} vs ${a}`);if(!c||!i)throw await H({apiBase:r.apiBase,authToken:r.authToken,payload:{childExecutionId:n,status:"canceled",error:{message:"bundle not ready for in-process; falling back to HTTP",code:"NO_BUNDLE"}}}),new O("no-bundle","workflow bundle not built yet");let h=dt(o);if(!h){let g=U(fe,`${p}@${l||"0"}`);try{await io(i,g);try{uo()}catch{}}catch(m){throw m.fallback&&await H({apiBase:r.apiBase,authToken:r.authToken,payload:{childExecutionId:n,status:"failed",error:{message:m.message,code:m.reason}}}),m}try{h=await ao(g),ut(o,h,{workflowUuid:p,version:l,runtimeTag:a,cacheDir:g})}catch(m){throw lt(o,m),await H({apiBase:r.apiBase,authToken:r.authToken,payload:{childExecutionId:n,status:"failed",error:{message:m.message,code:m.reason||"IMPORT_FAILED"}}}),m.fallback?m:new O("import-failed",m.message)}}let w=Date.now(),$=await(typeof h=="function"&&h.prototype?.buildGraph?new h:h).buildGraph(),b={...e.input||{}},f,I;try{f=await at({executionId:n,parentExecutionId:t.executionId,conversationId:e.conversationId!==void 0?e.conversationId:t.conversationId,dispatchMode:"inprocess"},()=>$.run(e.parentAgent,b,{signal:e.signal})),I=f&&typeof f=="object"&&"state"in f?f.state:f}catch(g){throw await H({apiBase:r.apiBase,authToken:r.authToken,payload:{childExecutionId:n,status:"failed",error:{message:g.message,code:g.code||"CHILD_THREW",stack:g.stack},durationMs:Date.now()-w}}),g}if(f&&typeof f=="object"&&f.stoppedExternally){await H({apiBase:r.apiBase,authToken:r.authToken,payload:{childExecutionId:n,status:"canceled",finalState:I,durationMs:Date.now()-w}});let g=new Error(`Sub-graph '${o}' canceled by parent abort`);throw g.code="SUBGRAPH_CANCELED",g.subgraphJobId=n,g}return await H({apiBase:r.apiBase,authToken:r.authToken,payload:{childExecutionId:n,status:"completed",finalState:I,durationMs:Date.now()-w}}),{finalState:I,executionId:n}}function co(o){let e=0,t=[o];for(;t.length;){let r=t.pop(),s;try{s=ft(r)}catch{continue}if(s.isDirectory()){let n;try{n=ht(r)}catch{continue}for(let a of n)t.push(U(r,a))}else e+=s.size}return e}function uo({cap:o=Number(process.env.ZIBBY_SUBGRAPH_CACHE_CAP_BYTES||2*1024*1024*1024)}={}){try{if(!J(fe))return{evicted:0,freedBytes:0};let e=ht(fe),t=[],r=0;for(let i of e){let u=U(fe,i),l;try{l=ft(u)}catch{continue}let p=l.isDirectory()?co(u):l.size;r+=p,t.push({name:i,full:u,size:p,mtimeMs:l.mtimeMs})}if(r<=o)return{evicted:0,freedBytes:0,totalBytes:r};t.sort((i,u)=>i.mtimeMs-u.mtimeMs);let s=Math.floor(o*.7),n=0,a=0;for(let i of t){if(r-n<=s)break;if(!J(U(i.full,".lock")))try{to(i.full,{recursive:!0,force:!0}),n+=i.size,a+=1}catch(u){y.debug(`[sub-graph cache] evict skip ${i.name}: ${u.message}`)}}return a>0&&y.info(`[sub-graph cache] evicted ${a} entr(y/ies), freed ${(n/1024/1024).toFixed(1)}MB`),{evicted:a,freedBytes:n,totalBytes:r-n}}catch(e){return y.debug(`[sub-graph cache] evict failed: ${e.message}`),{evicted:0,freedBytes:0}}}var lo=2e3,po=600*1e3,fo=new Set(["completed","failed","canceled","timeout"]);function ho(){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 go(){let o=process.env.PROJECT_ID;if(!o)throw new Error("Sub-graph dispatch requires PROJECT_ID env var.");return o}function mo(){let o=process.env.PROJECT_API_TOKEN;if(!o)throw new Error("Sub-graph dispatch requires PROJECT_API_TOKEN env var.");return o}function So(){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 St(o,e={}){if(!o||typeof o!="string")throw new Error("dispatchSubgraph: workflowName (string) is required");let t=re(),r=Number(process.env.ZIBBY_SUBGRAPH_MAX_DEPTH||10);if((t.depth||0)>=r)throw new Error(`dispatchSubgraph('${o}'): sub-graph depth ${t.depth} reached cap of ${r}. Restructure the graph or raise ZIBBY_SUBGRAPH_MAX_DEPTH.`);if(process.env.ZIBBY_INPROCESS_SUBGRAPH!=="0"&&!e.async)try{y.debug(`[sub-graph] trying in-process for '${o}'`);let{finalState:g}=await gt(o,{input:e.input,conversationId:e.conversationId,signal:e.signal,parentAgent:e.parentAgent}),m=mt(g,e.output);return y.info(`[sub-graph] '${o}' completed in-process`),m}catch(g){if(g instanceof O||g?.fallback)y.info(`[sub-graph] in-process fallback for '${o}': ${g.reason||"unknown"} \u2014 using HTTP`);else throw g}let s=ho(),n=go(),a=mo(),i=So(),u=`${s}/projects/${encodeURIComponent(n)}/workflows/${encodeURIComponent(o)}/trigger`,l={input:e.input||{},...i?{parentExecutionId:i}:{},...e.conversationId?{conversationId:e.conversationId}:{}};y.info(`[sub-graph] dispatching '${o}' (${e.async?"async":"sync"}) from parent ${i||"<none>"}`);let p=await fetch(u,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${a}`},body:JSON.stringify(l)});if(!p.ok){let g=null,m="";try{g=await p.json(),m=g?.error||g?.message||JSON.stringify(g)}catch{m=await p.text().catch(()=>"")}if(p.status===429){let v=g?.quotaInfo||{},M=new Error(`Sub-graph '${o}' blocked by execution quota (${v.used??"?"}/${v.limit??"?"} on plan ${v.planId||"unknown"}). Sub-workflow runs count toward the same monthly cap as user-triggered runs.`);throw M.code="SUBGRAPH_QUOTA_EXCEEDED",M.status=429,M.subgraph=o,M.quotaInfo=v,M}if(p.status===400){let v=new Error(`Sub-graph '${o}' rejected input: ${m}`);throw v.code="SUBGRAPH_INVALID_INPUT",v.status=400,v.subgraph=o,v.validationErrors=g?.validationErrors||null,v.missing=g?.missing||null,v}let R=new Error(`Sub-graph '${o}' trigger rejected (${p.status}): ${m}`);throw R.code="SUBGRAPH_TRIGGER_FAILED",R.status=p.status,R.subgraph=o,R}let c=await p.json(),d=c?.data?.jobId||c?.jobId;if(!d)throw new Error(`Sub-graph '${o}' trigger returned no jobId: ${JSON.stringify(c).slice(0,200)}`);if(e.async)return y.info(`[sub-graph] async dispatch of '${o}' \u2192 jobId=${d} (not waiting)`),{jobId:d,status:"accepted",workflow:o};let h=Number.isFinite(e.timeoutMs)?e.timeoutMs:po,w=Number.isFinite(e.pollIntervalMs)?e.pollIntervalMs:lo,_=`${s}/executions/${encodeURIComponent(d)}`,$=Date.now()+h,b="accepted",f=0;for(;Date.now()<$;){await new Promise(v=>setTimeout(v,w)),f+=1;let g=await fetch(_,{headers:{Authorization:`Bearer ${a}`}});if(!g.ok){if(g.status>=500){y.warn(`[sub-graph] status poll for ${d} returned ${g.status}, will retry`);continue}throw new Error(`Sub-graph status poll failed for ${d}: ${g.status}`)}let m=await g.json(),R=m?.data||m?.execution||m;if(b=R?.status||b,fo.has(b)){if(b!=="completed"){let S=new Error(`Sub-graph '${o}' (${d}) ended in status '${b}'`);throw S.subgraphJobId=d,S.subgraphStatus=b,S}let v=R?.finalState||R?.state||{},M=mt(v,e.output);return y.info(`[sub-graph] '${o}' (${d}) completed after ${f} polls`),M}}let I=new Error(`Sub-graph '${o}' (${d}) timed out after ${Math.round(h/1e3)}s (last status: ${b})`);throw I.subgraphJobId=d,I.subgraphStatus=b,I}import{existsSync as yt,readFileSync as yo}from"node:fs";import{join as Ne,dirname as wt}from"node:path";var he=class{static async loadContext(e,t,r={}){let s={},n=r.filenames||["CONTEXT.md","AGENTS.md"];if(e){let i=wt(Ne(t,e));for(let u of n){let l=await this.findAndMergeContextFiles(u,i,t);if(l){let p=u.replace(/\.[^.]+$/,"").toLowerCase();s[p]=l}}}let a=r.discovery||{};for(let[i,u]of Object.entries(a))try{let l=Ne(t,u);yt(l)&&(s[i]=await this.loadFile(l))}catch(l){console.warn(`[workflow] could not load context '${i}' from '${u}': ${l.message}`)}return s}static async findAndMergeContextFiles(e,t,r){let s=[],n=t;for(;n.startsWith(r);){let a=Ne(n,e);if(yt(a))try{s.unshift(await this.loadFile(a))}catch(u){console.warn(`[workflow] could not load ${e} from ${a}: ${u.message}`)}let i=wt(n);if(i===n)break;n=i}return s.length===0?null:s.every(a=>typeof a=="string")?s.join(`
|
|
30
30
|
|
|
31
31
|
---
|
|
32
32
|
|
|
33
|
-
`):s.every(a=>typeof a=="object")?Object.assign({},...s):s[s.length-1]}static async loadFile(e){let t=
|
|
33
|
+
`):s.every(a=>typeof a=="object")?Object.assign({},...s):s[s.length-1]}static async loadFile(e){let t=yo(e,"utf-8");if(e.endsWith(".json"))return JSON.parse(t);if(e.endsWith(".js")||e.endsWith(".mjs")){let{pathToFileURL:r}=await import("url"),s=await import(r(e).href);return s.default||s}return t}};import{mkdirSync as Et,existsSync as Pe,writeFileSync as _t,unlinkSync as wo}from"node:fs";import{join as Y,resolve as bt}from"node:path";import{config as _o}from"dotenv";import{zodToJsonSchema as It}from"zod-to-json-schema";import{z as ge}from"zod";import Io from"handlebars";function Eo({traceFrom:o,sessionId:e,sessionPath:t,idSource:r,mkdirFresh:s}){if(!(process.env.ZIBBY_SESSION_LOG==="1"||process.env.ZIBBY_SESSION_LOG==="true"))return;let a=typeof process.ppid=="number"?process.ppid:"n/a",i=`[zibby:session] from=${o} pid=${process.pid} ppid=${a} sessionId=${e} source=${r} mkdir=${s?"yes":"no"} path=${t}`;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 (${o}):
|
|
36
|
-
${p}`)}}function _o(){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 Io(){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 Et(String(e).trim())}catch{return String(e).trim()}}function Eo(){_o()||(delete process.env.ZIBBY_SESSION_PATH,delete process.env.ZIBBY_SESSION_ID)}function bo({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 $o(o={}){let e=et.map(n=>process.env[n]).find(Boolean),t=Math.random().toString(36).slice(2,6),r=e||`${Date.now()}_${t}`,s=o.paths?.sessionPrefix;return s?`${s}_${r}`:r}function To({cwd:o=process.cwd(),config:e={},initialState:t={},traceFrom:r="resolveWorkflowSession"}={}){let s=t.sessionPath,n=t.sessionTimestamp,a="initialState.sessionPath";if(!s&&process.env.ZIBBY_SESSION_PATH)try{let l=Et(String(process.env.ZIBBY_SESSION_PATH));l&&(s=l,a="ZIBBY_SESSION_PATH")}catch{}let i;if(s)i=String(s).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 u=e.sessionId!=null?String(e.sessionId).trim():"";u&&u!=="last"?(i=u,a="config.sessionId"):(i=$o(e),a="generated")}n=n??Date.now();let p=e.paths?.output||de;s=Y(o,p,Xe,i)}let c=!Pe(s);return c&&It(s,{recursive:!0}),(c||a!=="initialState.sessionPath")&&wo({traceFrom:r,sessionId:i,sessionPath:s,idSource:a,mkdirFresh:c}),bo({sessionPath:s,sessionId:i}),{sessionPath:s,sessionId:i,sessionTimestamp:n}}var me=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,a={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,p;return typeof n.input=="function"?p=n.input(l):n.input&&typeof n.input=="object"?p=n.input:p={},mt(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,signal:l?._signal,parentAgent:c?.agent})}},i=new j(a);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}let s=t instanceof j?t:new j(t);return s.name=e,this.nodes.set(e,s),r.prompt?this.nodePrompts.set(e,r.prompt):typeof t?.prompt=="string"&&t.prompt.trim()&&this.nodePrompts.set(e,t.prompt),Object.keys(r).length>0&&this.nodeOptions.set(e,r),this}addConditionalNode(e,t){return this.nodes.set(e,new oe({...t,name:e})),this}addEdge(e,t){return this.edges.set(e,t),this}setNodeType(e,t){return this.nodeTypeMap.set(e,t),this}addConditionalEdges(e,t,{labels:r}={}){return this.edges.set(e,{conditional:!0,routes:t,labels:r}),typeof t=="function"&&this.conditionalCodeMap.set(e,t.toString()),this}setEntryPoint(e){return this.entryPoint=e,this}use(e){return typeof e=="function"&&this.middleware.push(e),this}_composeMiddleware(e,t,r,s,n){let a=r;for(let i=e.length-1;i>=0;i--){let c=e[i],l=a;a=()=>c(t,l,s,n)}return a()}serialize(){let e=[],t={};for(let[p,u]of this.nodes){let d=this.nodeTypeMap.get(p)||(u instanceof oe?"decision":p);e.push({id:p,type:d,data:{nodeType:d,label:p}});let f={};u._isCustomCode&&typeof u.execute=="function"&&(f.customCode=u.execute.toString());let w=this.nodePrompts.get(p);if(w)f.prompt=w;else if(typeof u.prompt=="function")try{let _=u.prompt({});typeof _=="string"&&_.trim()&&(f.prompt=_,f.promptIsCode=!0)}catch{}if(typeof u.customExecute=="function"&&(f.executeCode=u.customExecute.toString()),u.outputSchema)if(typeof u.outputSchema._def<"u"){let _=null;if(typeof ge?.toJSONSchema=="function")try{_=ge.toJSONSchema(u.outputSchema)}catch{}if(!_)try{_=_t(u.outputSchema,{target:"openApi3"})}catch{}f.outputSchema=_?{jsonSchema:_,variables:this._flattenJsonSchemaToVariables(_)}:{schema:u.outputSchema}}else f.outputSchema={schema:u.outputSchema};let I=(this.resolvedToolsMap||{})[p];I?.toolIds&&(f.tools=I.toolIds);let $=Array.isArray(u?.config?.skills)?u.config.skills:Array.isArray(u?.skills)?u.skills:null;$&&$.length>0&&(f.skills=[...$]),Object.keys(f).length>0&&(t[p]=f)}let r=[];for(let[p,u]of this.edges)if(typeof u=="string")r.push({source:p,target:u});else if(u.conditional){let d=this.conditionalCodeMap.get(p)||u.routes.toString(),f=this._inferConditionalTargets(u.routes,u.labels),w=u.labels||{};for(let I of f){let $={source:p,target:I,data:{conditionalCode:d}};w[I]&&($.label=w[I]),r.push($)}}let s=p=>{if(!p)return null;if(typeof ge?.toJSONSchema=="function")try{return ge.toJSONSchema(p)}catch{}try{return _t(p,{target:"openApi3"})}catch{return null}};this.entryPoint&&this.nodes.has(this.entryPoint)&&(e.unshift({id:"START",type:"start",data:{nodeType:"start",label:"Start"}}),r.unshift({source:"START",target:this.entryPoint}));let n=0;for(let p of r)if(p.target==="END"){n+=1;let u=`END__${n}`;p.target=u,e.push({id:u,type:"end",data:{nodeType:"end",label:"End"}})}for(let p of this.nodes.keys())if(!this.edges.has(p)){n+=1;let u=`END__${n}`;e.push({id:u,type:"end",data:{nodeType:"end",label:"End"}}),r.push({source:p,target:u})}let a=this._runtimeSchema(),i=s(a||this.stateSchema),c=s(this.inputSchema),l=s(this.contextSchema);return{nodes:e,edges:r,nodeConfigs:t,stateSchema:i,inputSchema:c,contextSchema:l}}_inferConditionalTargets(e,t){let r=e.toString(),s=new Set,n=/(['"])((?:\\.|(?!\1).)*?)\1|`((?:\\.|[^`$]|\$(?!\{))*?)`/g,a;for(;(a=n.exec(r))!==null;){let l=a[2]!==void 0?a[2]:a[3];l!==void 0&&l!==""&&s.add(l)}let i=new Set(["END","START","__end__","__start__"]);for(let l of this.nodes.keys())i.add(l);if(t&&typeof t=="object")for(let l of Object.keys(t))i.add(l);let c=new Set;for(let l of s)i.has(l)&&c.add(l);if(c.size===0){let l=/return\s+['"]([^'"]+)['"]/g,p;for(;(p=l.exec(r))!==null;)c.add(p[1])}return[...c]}_flattenJsonSchemaToVariables(e,t=""){let r=e;if(e.$ref&&e.definitions){let s=e.$ref.replace("#/definitions/","");r=e.definitions[s]||e}return this._flattenSchema(r,t)}_flattenSchema(e,t=""){if(!e||typeof e!="object")return[];let r=[],s=e.properties||{},n=e.required||[];for(let[a,i]of Object.entries(s)){let c=t?`${t}.${a}`:a;r.push({path:c,type:i.type||"unknown",label:i.description||this._formatLabel(a),optional:!n.includes(a)}),i.type==="object"&&i.properties&&r.push(...this._flattenSchema(i,c)),i.type==="array"&&i.items?.type==="object"&&i.items.properties&&r.push(...this._flattenSchema(i.items,`${c}[]`))}return r}_formatLabel(e){return e.replace(/([A-Z])/g," $1").replace(/^./,t=>t.toUpperCase()).trim()}_summarizeNodeOutput(e,t){if(!t||typeof t!="object")return[];let r=[];t.success!==void 0&&r.push(`Result: ${t.success?"passed":"failed"}`);for(let[s,n]of Object.entries(t))if(!(s==="success"||s==="raw"||s==="nextNode")){if(typeof n=="string"&&n.length<=80)r.push(`${s}: ${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);r.push(c?`${s}: ${i}/${a} passed${a-i?`, ${a-i} failed`:""}`:`${s}: ${a} items`)}if(r.length>=4)break}return r}async run(e,t={},r={}){if(!this.entryPoint)throw new Error("No entry point set for graph");let s=new AbortController;r.signal&&(r.signal.aborted?s.abort():r.signal.addEventListener("abort",()=>s.abort(),{once:!0}));let n=r.strategyAbortTimeoutMs??t.config?.strategyAbortTimeoutMs??5e3,a=t.cwd||process.cwd();So({path:Y(a,".env")});let i=t.config||{};if(!i||Object.keys(i).length===0)try{let b=Y(a,".zibby.config.js");Pe(b)&&(i=(await import(b)).default||{})}catch{}process.env.EXECUTION_ID&&!i.agent?.strictMode&&(i.agent={...i.agent,strictMode:!0});let c=t.agentType;if(!c){let b=i?.agent;b?.provider?c=b.provider:b?.gemini?c="gemini":b?.claude?c="claude":b?.cursor?c="cursor":b?.codex?c="codex":c=process.env.AGENT_TYPE||"cursor"}let l=t.contextConfig||e?.config?.contextConfig||e?.config?.context||i?.context||{},p=this._runtimeSchema();if(p){let b=p.safeParse(t);if(!b.success){let N=b.error.issues.map(O=>`${O.path.join(".")}: ${O.message}`);throw console.error("\u274C Initial state validation failed:"),N.forEach(O=>console.error(` - ${O}`)),new Error(`State validation failed: ${N.join(", ")}`)}x.step("State validated against schema")}let u=Io(),d=t.sessionPath||u;d||Eo();let{sessionPath:f,sessionTimestamp:w,sessionId:I}=To({cwd:a,config:i,traceFrom:"WorkflowGraph.run",initialState:{sessionPath:d,sessionTimestamp:t.sessionTimestamp}});x.step(`Session ${I}`);let $=await he.loadContext(t.specPath||"",a,l);Object.keys($).length>0&&x.step(`Context loaded: ${Object.keys($).join(", ")}`);let _=t.outputPath;!_&&t.specPath&&(e?.calculateOutputPath?_=e.calculateOutputPath(t.specPath):console.warn(`\u26A0\uFE0F outputPath not resolved (specPath=${t.specPath})`));let h=new ce({...t,config:i,agentType:c,outputPath:_,sessionPath:f,sessionTimestamp:w,context:$,resolvedTools:this.resolvedToolsMap||{},_signal:s.signal}),E=new Map;try{await import("@zibby/skills")}catch{}let{getSkill:g}=await Promise.resolve().then(()=>(pe(),ot)),m=i.skills&&typeof i.skills=="object"?i.skills:{},R=Object.values(m).filter(b=>b&&typeof b=="object"&&typeof b.id=="string"),v=b=>{for(let N of R)if(N.id===b)return N;return g(b)},M=new Set;for(let[,b]of this.nodes)for(let N of b.config?.skills||[])M.add(N);for(let b of M){let N=v(b);if(typeof N?.middleware=="function")try{let O=await N.middleware();typeof O=="function"&&E.set(b,O)}catch{}}let S=this.entryPoint,ne=[],Me=i?.recursionLimit??100,At=0;try{for(;S&&S!=="END";){if(++At>Me)throw new Error(`Workflow exceeded recursion limit (${Me}) \u2014 likely a cyclic conditional route. Set config.recursionLimit if you need a higher cap.`);let N=Y(f,Qe);if(Pe(N)){try{mo(N)}catch{}s.abort()}if(s.signal.aborted)return console.warn(`
|
|
37
|
-
\u{1F6D1} External stop requested \u2014 ending workflow.`),x.step("Workflow stopped externally"),{success:!0,state:
|
|
38
|
-
${p}`),i.includes(".")||a.add(
|
|
36
|
+
${p}`)}}function bo(){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 $o(){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 bt(String(e).trim())}catch{return String(e).trim()}}function To(){bo()||(delete process.env.ZIBBY_SESSION_PATH,delete process.env.ZIBBY_SESSION_ID)}function vo({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 Ao(o={}){let e=tt.map(n=>process.env[n]).find(Boolean),t=Math.random().toString(36).slice(2,6),r=e||`${Date.now()}_${t}`,s=o.paths?.sessionPrefix;return s?`${s}_${r}`:r}function ko({cwd:o=process.cwd(),config:e={},initialState:t={},traceFrom:r="resolveWorkflowSession"}={}){let s=t.sessionPath,n=t.sessionTimestamp,a="initialState.sessionPath";if(!s&&process.env.ZIBBY_SESSION_PATH)try{let l=bt(String(process.env.ZIBBY_SESSION_PATH));l&&(s=l,a="ZIBBY_SESSION_PATH")}catch{}let i;if(s)i=String(s).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 c=e.sessionId!=null?String(e.sessionId).trim():"";c&&c!=="last"?(i=c,a="config.sessionId"):(i=Ao(e),a="generated")}n=n??Date.now();let p=e.paths?.output||de;s=Y(o,p,Qe,i)}let u=!Pe(s);return u&&Et(s,{recursive:!0}),(u||a!=="initialState.sessionPath")&&Eo({traceFrom:r,sessionId:i,sessionPath:s,idSource:a,mkdirFresh:u}),vo({sessionPath:s,sessionId:i}),{sessionPath:s,sessionId:i,sessionTimestamp:n}}var me=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{if(typeof this.inputSchema.merge=="function")return this.inputSchema.merge(this.contextSchema);if(typeof this.inputSchema.and=="function")return this.inputSchema.and(this.contextSchema)}catch{}return this.inputSchema&&!this.contextSchema?this.inputSchema:this.stateSchema}addNode(e,t,r={}){if(!(t instanceof L)&&t&&typeof t=="object"&&typeof t.workflow=="string"){let n=t,a={name:e,_isCustomCode:!0,retries:n.retries,onComplete:n.onComplete,execute:async u=>{let l=u?.state&&typeof u.state.getAll=="function"?u.state.getAll():u,p;return typeof n.input=="function"?p=n.input(l):n.input&&typeof n.input=="object"?p=n.input:p={},St(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,signal:l?._signal,parentAgent:u?.agent})}},i=new L(a);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}let s=t instanceof L?t:new L(t);return s.name=e,this.nodes.set(e,s),r.prompt?this.nodePrompts.set(e,r.prompt):typeof t?.prompt=="string"&&t.prompt.trim()&&this.nodePrompts.set(e,t.prompt),Object.keys(r).length>0&&this.nodeOptions.set(e,r),this}addConditionalNode(e,t){return this.nodes.set(e,new oe({...t,name:e})),this}addEdge(e,t){return this.edges.set(e,t),this}setNodeType(e,t){return this.nodeTypeMap.set(e,t),this}addConditionalEdges(e,t,{labels:r}={}){return this.edges.set(e,{conditional:!0,routes:t,labels:r}),typeof t=="function"&&this.conditionalCodeMap.set(e,t.toString()),this}setEntryPoint(e){return this.entryPoint=e,this}use(e){return typeof e=="function"&&this.middleware.push(e),this}_composeMiddleware(e,t,r,s,n){let a=r;for(let i=e.length-1;i>=0;i--){let u=e[i],l=a;a=()=>u(t,l,s,n)}return a()}serialize(){let e=[],t={};for(let[p,c]of this.nodes){let d=this.nodeTypeMap.get(p)||(c instanceof oe?"decision":p);e.push({id:p,type:d,data:{nodeType:d,label:p}});let h={};c._isCustomCode&&typeof c.execute=="function"&&(h.customCode=c.execute.toString());let w=typeof c?.config?.description=="string"&&c.config.description.trim()?c.config.description:typeof c?.description=="string"&&c.description.trim()?c.description:null;w&&(h.description=w);let _=this.nodePrompts.get(p);if(_)h.prompt=_;else if(typeof c.prompt=="function")try{let f=c.prompt({});typeof f=="string"&&f.trim()&&(h.prompt=f,h.promptIsCode=!0)}catch{}if(typeof c.customExecute=="function"&&(h.executeCode=c.customExecute.toString()),c.outputSchema)if(typeof c.outputSchema._def<"u"){let f=null;if(typeof ge?.toJSONSchema=="function")try{f=ge.toJSONSchema(c.outputSchema)}catch{}if(!f)try{f=It(c.outputSchema,{target:"openApi3"})}catch{}h.outputSchema=f?{jsonSchema:f,variables:this._flattenJsonSchemaToVariables(f)}:{schema:c.outputSchema}}else h.outputSchema={schema:c.outputSchema};let $=(this.resolvedToolsMap||{})[p];$?.toolIds&&(h.tools=$.toolIds);let b=Array.isArray(c?.config?.skills)?c.config.skills:Array.isArray(c?.skills)?c.skills:null;b&&b.length>0&&(h.skills=[...b]),Object.keys(h).length>0&&(t[p]=h)}let r=[];for(let[p,c]of this.edges)if(typeof c=="string")r.push({source:p,target:c});else if(c.conditional){let d=this.conditionalCodeMap.get(p)||c.routes.toString(),h=this._inferConditionalTargets(c.routes,c.labels),w=c.labels||{};for(let _ of h){let $={source:p,target:_,data:{conditionalCode:d}};w[_]&&($.label=w[_]),r.push($)}}let s=p=>{if(!p)return null;if(typeof ge?.toJSONSchema=="function")try{return ge.toJSONSchema(p)}catch{}try{return It(p,{target:"openApi3"})}catch{return null}};this.entryPoint&&this.nodes.has(this.entryPoint)&&(e.unshift({id:"START",type:"start",data:{nodeType:"start",label:"Start"}}),r.unshift({source:"START",target:this.entryPoint}));let n=0;for(let p of r)if(p.target==="END"){n+=1;let c=`END__${n}`;p.target=c,e.push({id:c,type:"end",data:{nodeType:"end",label:"End"}})}for(let p of this.nodes.keys())if(!this.edges.has(p)){n+=1;let c=`END__${n}`;e.push({id:c,type:"end",data:{nodeType:"end",label:"End"}}),r.push({source:p,target:c})}let a=this._runtimeSchema(),i=s(a||this.stateSchema),u=s(this.inputSchema),l=s(this.contextSchema);return{nodes:e,edges:r,nodeConfigs:t,stateSchema:i,inputSchema:u,contextSchema:l}}_inferConditionalTargets(e,t){let r=e.toString(),s=new Set,n=/(['"])((?:\\.|(?!\1).)*?)\1|`((?:\\.|[^`$]|\$(?!\{))*?)`/g,a;for(;(a=n.exec(r))!==null;){let l=a[2]!==void 0?a[2]:a[3];l!==void 0&&l!==""&&s.add(l)}let i=new Set(["END","START","__end__","__start__"]);for(let l of this.nodes.keys())i.add(l);if(t&&typeof t=="object")for(let l of Object.keys(t))i.add(l);let u=new Set;for(let l of s)i.has(l)&&u.add(l);if(u.size===0){let l=/return\s+['"]([^'"]+)['"]/g,p;for(;(p=l.exec(r))!==null;)u.add(p[1])}return[...u]}_flattenJsonSchemaToVariables(e,t=""){let r=e;if(e.$ref&&e.definitions){let s=e.$ref.replace("#/definitions/","");r=e.definitions[s]||e}return this._flattenSchema(r,t)}_flattenSchema(e,t=""){if(!e||typeof e!="object")return[];let r=[],s=e.properties||{},n=e.required||[];for(let[a,i]of Object.entries(s)){let u=t?`${t}.${a}`:a;r.push({path:u,type:i.type||"unknown",label:i.description||this._formatLabel(a),optional:!n.includes(a)}),i.type==="object"&&i.properties&&r.push(...this._flattenSchema(i,u)),i.type==="array"&&i.items?.type==="object"&&i.items.properties&&r.push(...this._flattenSchema(i.items,`${u}[]`))}return r}_formatLabel(e){return e.replace(/([A-Z])/g," $1").replace(/^./,t=>t.toUpperCase()).trim()}_summarizeNodeOutput(e,t){if(!t||typeof t!="object")return[];let r=[];t.success!==void 0&&r.push(`Result: ${t.success?"passed":"failed"}`);for(let[s,n]of Object.entries(t))if(!(s==="success"||s==="raw"||s==="nextNode")){if(typeof n=="string"&&n.length<=80)r.push(`${s}: ${n}`);else if(Array.isArray(n)){let a=n.length,i=n.filter(l=>l?.passed===!0).length,u=n.some(l=>l?.passed!==void 0);r.push(u?`${s}: ${i}/${a} passed${a-i?`, ${a-i} failed`:""}`:`${s}: ${a} items`)}if(r.length>=4)break}return r}async run(e,t={},r={}){if(!this.entryPoint)throw new Error("No entry point set for graph");let s=new AbortController;r.signal&&(r.signal.aborted?s.abort():r.signal.addEventListener("abort",()=>s.abort(),{once:!0}));let n=r.strategyAbortTimeoutMs??t.config?.strategyAbortTimeoutMs??5e3,a=t.cwd||process.cwd();_o({path:Y(a,".env")});let i=t.config||{};if(!i||Object.keys(i).length===0)try{let E=Y(a,".zibby.config.js");Pe(E)&&(i=(await import(E)).default||{})}catch{}process.env.EXECUTION_ID&&!i.agent?.strictMode&&(i.agent={...i.agent,strictMode:!0});let u=t.agentType;if(!u){let E=i?.agent;E?.provider?u=E.provider:E?.gemini?u="gemini":E?.claude?u="claude":E?.cursor?u="cursor":E?.codex?u="codex":u=process.env.AGENT_TYPE||"cursor"}let l=t.contextConfig||e?.config?.contextConfig||e?.config?.context||i?.context||{},p=this._runtimeSchema();if(p){let E=p.safeParse(t);if(!E.success){let N=E.error.issues.map(P=>`${P.path.join(".")}: ${P.message}`);throw console.error("\u274C Initial state validation failed:"),N.forEach(P=>console.error(` - ${P}`)),new Error(`State validation failed: ${N.join(", ")}`)}x.step("State validated against schema")}let c=$o(),d=t.sessionPath||c;d||To();let{sessionPath:h,sessionTimestamp:w,sessionId:_}=ko({cwd:a,config:i,traceFrom:"WorkflowGraph.run",initialState:{sessionPath:d,sessionTimestamp:t.sessionTimestamp}});x.step(`Session ${_}`);let $=await he.loadContext(t.specPath||"",a,l);Object.keys($).length>0&&x.step(`Context loaded: ${Object.keys($).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 f=new ce({...t,config:i,agentType:u,outputPath:b,sessionPath:h,sessionTimestamp:w,context:$,resolvedTools:this.resolvedToolsMap||{},_signal:s.signal}),I=new Map;try{await import("@zibby/skills")}catch{}let{getSkill:g}=await Promise.resolve().then(()=>(pe(),rt)),m=i.skills&&typeof i.skills=="object"?i.skills:{},R=Object.values(m).filter(E=>E&&typeof E=="object"&&typeof E.id=="string"),v=E=>{for(let N of R)if(N.id===E)return N;return g(E)},M=new Set;for(let[,E]of this.nodes)for(let N of E.config?.skills||[])M.add(N);for(let E of M){let N=v(E);if(typeof N?.middleware=="function")try{let P=await N.middleware();typeof P=="function"&&I.set(E,P)}catch{}}let S=this.entryPoint,ne=[],Me=i?.recursionLimit??100,kt=0;try{for(;S&&S!=="END";){if(++kt>Me)throw new Error(`Workflow exceeded recursion limit (${Me}) \u2014 likely a cyclic conditional route. Set config.recursionLimit if you need a higher cap.`);let N=Y(h,et);if(Pe(N)){try{wo(N)}catch{}s.abort()}if(s.signal.aborted)return console.warn(`
|
|
37
|
+
\u{1F6D1} External stop requested \u2014 ending workflow.`),x.step("Workflow stopped externally"),{success:!0,state:f.getAll(),executionLog:ne,stoppedExternally:!0};let P=this.nodes.get(S);if(!P)throw new Error(`Node '${S}' not found in graph`);let De=JSON.stringify({sessionPath:h,sessionTimestamp:w,currentNode:S,createdAt:new Date().toISOString(),config:f.get("config")}),xt=Y(h,K);_t(xt,De,"utf-8");let je=f.get("config")?.paths?.output||de,Ot=Y(a,je,K);Et(Y(a,je),{recursive:!0});try{_t(Ot,De,"utf-8")}catch{}let Le=t.onPipelineProgress;if(typeof Le=="function")try{Le({cwd:a,sessionPath:h,sessionId:_,outputBase:f.get("config")?.paths?.output||de,currentNode:S})}catch{}let Nt=(this.resolvedToolsMap||{})[S]||null;f.set("_currentNodeTools",Nt);let Pt=f.get("nodeConfigs")||{};f.set("_currentNodeConfig",Pt[S]||{}),x.nodeStart(S);let Ue=Date.now(),se=this.nodePrompts.get(S);if(!this._invokeAgent){let A=await Promise.resolve().then(()=>(te(),ee));this._invokeAgent=A.invokeAgent}let Ct=this._invokeAgent,ye={},Rt=P.config?.skills||[];for(let A of Rt){let C=v(A);if(typeof C?.invokeAgentOptions=="function")try{let T=C.invokeAgentOptions(f.getAll(),{agentType:f.get("agentType"),nodeName:S});T&&typeof T=="object"&&(ye={...ye,...T})}catch(T){console.warn(`[graph] skill '${A}' invokeAgentOptions threw: ${T.message}`)}}let Fe=async(A,C,T={})=>{let B=Ct(A,C,{...ye,...T,signal:s.signal});return B.catch(()=>{}),s.signal.aborted?B:Promise.race([B,new Promise((Z,z)=>{let j=()=>{setTimeout(()=>{let V=new Error(`Strategy ignored AbortSignal \u2014 engine deadman fired after ${n}ms`);V.name="AbortError",z(V)},n)};s.signal.addEventListener("abort",j,{once:!0})})])},Bt=async(A={},C={})=>{let T=C.prompt||"";if(se){let B=this._compiledPrompts.get(S);B||(B=Io.compile(se,{noEscape:!0}),this._compiledPrompts.set(S,B));try{T=B(A)}catch(Z){throw console.error(`\u274C Template rendering failed for node '${S}':`,Z.message),new Error(`Template rendering failed: ${Z.message}`,{cause:Z})}}else if(!T)throw new Error(`No prompt template configured for node '${S}' and no prompt provided in options`);return Fe(T,{state:f.getAll(),images:C.images||[]},{model:C.model||f.get("model"),workspace:f.get("workspace"),schema:C.schema,...C,signal:s.signal})},We=f.getAll(),Mt=["state","invokeAgent","_coreInvokeAgent","agent","nodeId","promptTemplate","getPromptTemplate"];for(let A of Mt)Object.prototype.hasOwnProperty.call(We,A)&&console.warn(`[workflow] node "${S}": state key "${A}" is shadowed by the engine context prop; read it via context.state.get('${A}')`);let Ge={...We,state:f,invokeAgent:Bt,_coreInvokeAgent:Fe,agent:e,nodeId:S,promptTemplate:se,getPromptTemplate:()=>se};try{let A=(P.config?.skills||[]).map(j=>I.get(j)).filter(Boolean),C=[...this.middleware,...A],T;C.length>0?T=await this._composeMiddleware(C,S,async()=>P.execute(Ge,f),f.getAll(),f):T=await P.execute(Ge,f);let B=Date.now()-Ue;if(ne.push({node:S,success:T.success,duration:B,timestamp:new Date().toISOString()}),!T.success){if(s.signal.aborted)return x.step("Workflow stopped externally"),{success:!0,state:f.getAll(),executionLog:ne,stoppedExternally:!0};f.append("errors",{node:S,error:T.error});let j=P.config?.retries||0,V=`${S}_retries`,ie=f.getAll()[V]||0;if(ie<j){x.stepInfo(`Retrying (attempt ${ie+1}/${j})`),f.update({[V]:ie+1,[`${S}_raw`]:T.raw});continue}throw x.nodeFailed(S,T.error,{duration:B}),new Error(`Node '${S}' failed after ${ie} attempts: ${T.error}`)}f.update({[S]:T.output});let Z=this._summarizeNodeOutput(S,T.output);x.nodeComplete(S,{duration:B,details:Z});let z=this.edges.get(S);if(!z)S="END";else if(z.conditional){let j=z.routes(f.getAll());x.route(S,j),S=j}else S=z}catch(A){throw x.isInsideNode&&x.nodeFailed(S,A.message,{duration:Date.now()-Ue}),f.set("failed",!0),f.set("failedAt",S),A}}x.graphComplete();let E={success:!0,state:f.getAll(),executionLog:ne};return e&&typeof e.onComplete=="function"&&await e.onComplete(E),E}finally{if(e&&typeof e.cleanup=="function")try{await e.cleanup()}catch(E){console.warn(`[workflow] agent.cleanup() failed: ${E.message}`)}}}};var Ce=Symbol.for("@zibby/agent-workflow.nodes");globalThis[Ce]||(globalThis[Ce]=new Map);var Re=globalThis[Ce];function xo(o,e){Re.set(o,e)}function $t(o){return Re.get(o)}function Be(o){return Re.has(o)}xo("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(()=>(te(),ee))).invokeAgent);let s=e.extraPromptInstructions||"Execute the task based on the current state.",n=Oo(s,t),a=await r(n,{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 Oo(o,e){let t=/@([\w.]+)/g,r=new Set,s;for(;(s=t.exec(o))!==null;)r.add(s[1]);if(r.size===0)return o;let n=[],a=new Set;for(let i of r){let u=i.split(".")[0];if(a.has(u))continue;let l=i.split(".").reduce((d,h)=>d?.[h],e);if(l===void 0)continue;let p=typeof l=="string"?l:l?.raw??JSON.stringify(l,null,2),c=i.replace(/_/g," ").replace(/\b\w/g,d=>d.toUpperCase());n.push(`## ${c}
|
|
38
|
+
${p}`),i.includes(".")||a.add(u)}return n.length===0?o:`${o}
|
|
39
39
|
|
|
40
40
|
---
|
|
41
41
|
# Referenced Context
|
|
42
42
|
|
|
43
43
|
${n.join(`
|
|
44
44
|
|
|
45
|
-
`)}`}pe();F();var
|
|
45
|
+
`)}`}pe();F();var No={};function vt(o,e){if(Array.isArray(e))return Tt(e);let t=No[o];return!t||t.length===0?null:Tt(t)}function Tt(o){if(!Array.isArray(o)||o.length===0)return null;let e=[],t={},r=[];for(let s of o){let n=Q(s);if(!n){y.warn(`[workflow] unknown skill "${s}" \u2014 skipping`);continue}r.push(s);for(let a of n.tools||[])e.push({name:a.name,description:a.description,input_schema:a.input_schema||{type:"object",properties:{}}});if(!t[n.serverName])if(typeof n.resolve=="function"){let a=n.resolve();a&&(t[n.serverName]={...a,toolPrefix:s})}else{let a={};for(let i of n.envKeys||[]){let u=process.env[i];u&&(a[i]=u)}t[n.serverName]={command:n.command,args:[...n.args||[]],env:a,toolPrefix:s}}}return r.length===0?null:{toolIds:r,claudeTools:e,mcpServers:t}}F();function Dr(o,e={}){let{nodes:t,edges:r,nodeConfigs:s={}}=o;if(!Array.isArray(t)||t.length===0)throw new D("Graph must have at least one node");if(!Array.isArray(r))throw new D("Graph edges must be an array");let n=new me(e);e.stateSchema&&n.setStateSchema(e.stateSchema);let a=new Set,i=new Map,u={};for(let d of t){let h=Se(d);i.set(d.id,{...d,resolvedType:h}),h==="decision"&&a.add(d.id)}for(let[d,h]of i){if(a.has(d))continue;let w=h.resolvedType,_=s[d]||{},$=vt(w,_.tools);$&&(u[d]=$);let b={};_.prompt&&(b.prompt=_.prompt);let f=Be(w);if(y.debug(`[workflow] compiler: node "${d}" type="${w}" registered=${f}`),_.customCode&&!f)n.addNode(d,At(d,_.customCode,_),b),n.setNodeType(d,w);else if(f){let I=$t(w);I.factory?n.addNode(d,I.create(d,{..._,resolvedTools:$}),b):n.addNode(d,I,b),n.setNodeType(d,w)}else if(_.executeCode)n.addNode(d,At(d,_.executeCode,_),b),n.setNodeType(d,w);else throw new D(`Unknown node type "${w}" for node "${d}". Did you forget to register it?`)}n.resolvedToolsMap=u;let l=new Set;for(let d of r)a.has(d.target)||l.add(d.target);let p=t.find(d=>!a.has(d.id)&&!l.has(d.id));if(!p)throw new D("Could not determine entry point: no node without incoming edges found");n.setEntryPoint(p.id);let c=Po(r,"source");for(let d of r)if(!a.has(d.source))if(a.has(d.target)){let h=d.target,w=c.get(h)||[];if(w.length===0)throw new D(`Decision node "${h}" has no outgoing edges`);let _=Co(h,w,a);n.addConditionalEdges(d.source,_)}else n.addEdge(d.source,d.target);return n}function jr(o){let e=[];if(!o||typeof o!="object")return{valid:!1,errors:["Config must be a non-null object"]};if((!Array.isArray(o.nodes)||o.nodes.length===0)&&e.push("Graph must have at least one node"),Array.isArray(o.edges)||e.push("Graph edges must be an array"),e.length>0)return{valid:!1,errors:e};let t=o.nodeConfigs||{};for(let i of o.nodes){let u=Se(i);if(u==="decision"||Be(u))continue;let l=t[i.id]||{};l.customCode||l.executeCode||e.push(`Unknown node type "${u}" for node "${i.id}". Register it or provide customCode/executeCode.`)}let r=new Set(o.nodes.map(i=>i.id));for(let i of o.edges)r.has(i.source)||e.push(`Edge references unknown source node "${i.source}"`),r.has(i.target)||e.push(`Edge references unknown target node "${i.target}"`);let s=new Set(o.nodes.filter(i=>Se(i)==="decision").map(i=>i.id)),n=new Set;for(let i of o.edges)s.has(i.target)||n.add(i.target);let a=o.nodes.filter(i=>!s.has(i.id)&&!n.has(i.id));a.length===0?e.push("No entry point found (every node has incoming edges)"):a.length>1&&e.push(`Multiple entry points found: ${a.map(i=>i.id).join(", ")}`);for(let i of s){let u=o.edges.filter(p=>p.source===i);u.length===0&&e.push(`Decision node "${i}" has no outgoing edges`),u.some(p=>p.data?.conditionalCode||p.conditionalCode)||e.push(`Decision node "${i}" outgoing edges have no conditionalCode`)}return{valid:e.length===0,errors:e}}function Lr(o){return!o||!Array.isArray(o.nodes)?[]:o.nodes.filter(e=>Se(e)!=="decision").map(e=>e.id)}function Se(o){let e=o.data?.nodeType||o.data?.type||o.type;return e==="workflowNode"||e==="custom"||e==="default"?o.id:e}function Po(o,e){let t=new Map;for(let r of o){let s=r[e];t.has(s)||t.set(s,[]),t.get(s).push(r)}return t}function Co(o,e,t){let r=e.find(i=>i.data?.conditionalCode||i.conditionalCode);if(!r)throw new D(`Decision node "${o}" has no conditionalCode on its outgoing edges`);let s=r.data?.conditionalCode||r.conditionalCode,n=new Set(e.map(i=>i.target).filter(i=>!t.has(i))),a;try{let u=new Function(`return (${s})`)();a=l=>{let p=u(l);return n.has(p)||y.warn(`[workflow] conditional route from "${o}" returned "${p}" which is not in valid targets: ${[...n].join(", ")}`),p}}catch(i){throw new D(`Failed to compile conditionalCode for "${o}": ${i.message}`)}return a}function At(o,e,t={}){let r;try{r=new Function("invokeAgent","require","console",`return (${e})`)}catch(a){throw new D(`Failed to compile customCode for node "${o}": ${a.message}`)}let s=r(async(...a)=>{let{invokeAgent:i}=await Promise.resolve().then(()=>(te(),ee));return i(...a)},typeof we<"u"?we:void 0,console),n=null;return t.outputSchema&&(n=t.outputSchema.jsonSchema||t.outputSchema),{name:o,_isCustomCode:!0,outputSchema:n,execute:async a=>{try{let i=await s(a);return typeof i=="object"&&"success"in i?i:{success:!0,output:i,raw:null}}catch(i){return{success:!1,error:i.message,raw:null}}}}}var D=class extends Error{constructor(e){super(e),this.name="CompilationError"}};export{D as CompilationError,Dr as compileGraph,Lr as extractSteps,jr as validateGraphConfig};
|
package/dist/graph.d.ts
CHANGED
|
@@ -70,6 +70,23 @@ export class WorkflowGraph {
|
|
|
70
70
|
* The schema used at runtime to validate the FULL initial state object
|
|
71
71
|
* passed into graph.run(). Derived from input+context if both are set
|
|
72
72
|
* (the new model); otherwise the legacy stateSchema.
|
|
73
|
+
*
|
|
74
|
+
* Composition depends on the SHAPE of the input schema:
|
|
75
|
+
* - z.object → `inputSchema.merge(contextSchema)`. A flat object schema
|
|
76
|
+
* exposes `.merge`, which produces a single object schema with the union
|
|
77
|
+
* of both shapes. (Unchanged from the original behaviour.)
|
|
78
|
+
* - z.discriminatedUnion / z.union → no `.merge` (a union has `.options`,
|
|
79
|
+
* `_def.type === 'union'`). We compose with `z.intersection(input, ctx)`
|
|
80
|
+
* (equivalently `inputSchema.and(contextSchema)`), which validates the
|
|
81
|
+
* payload against the matching union VARIANT *and* against the context
|
|
82
|
+
* object — so per-variant required fields (e.g. fix→instruction) are
|
|
83
|
+
* enforced at runtime, and the context object's defaults are applied in
|
|
84
|
+
* the parsed result. This is the agent-native multi-scenario shape
|
|
85
|
+
* (e.g. sentry-triage's `z.discriminatedUnion('trigger', [...])`).
|
|
86
|
+
*
|
|
87
|
+
* Tolerant by design: if the input schema is neither shape we recognise, or
|
|
88
|
+
* composition throws for any reason, we fall back to the legacy stateSchema
|
|
89
|
+
* (or null) rather than crashing a run over a validation-wiring issue.
|
|
73
90
|
*/
|
|
74
91
|
_runtimeSchema(): any;
|
|
75
92
|
addNode(name: any, nodeOrConfig: any, options?: {}): this;
|
package/dist/graph.js
CHANGED
|
@@ -1,37 +1,37 @@
|
|
|
1
|
-
var
|
|
1
|
+
var Ae=Object.defineProperty;var ot=(r,t)=>()=>(r&&(t=r(r=0)),t);var Dt=(r,t)=>{for(var e in t)Ae(r,e,{get:t[e],enumerable:!0})};var Lt,xe,it,y,q=ot(()=>{Lt=()=>{},xe={debug:Lt,info:Lt,warn:(...r)=>console.warn("[workflow]",...r),error:(...r)=>console.error("[workflow]",...r)},it={impl:xe},y={debug:(...r)=>it.impl.debug?.(...r),info:(...r)=>it.impl.info?.(...r),warn:(...r)=>it.impl.warn?.(...r),error:(...r)=>it.impl.error?.(...r)}});var Kt=ot(()=>{});var qt={};Dt(qt,{clearSkills:()=>Me,getAllSkills:()=>Be,getSkill:()=>St,hasSkill:()=>Re,listSkillIds:()=>Ce,registerSkill:()=>Ne});function Ne(r){if(!r||typeof r.id!="string")throw new Error("Skill definition must include a string id");z.set(r.id,Object.freeze({...r}))}function St(r){return z.get(r)||null}function Re(r){return z.has(r)}function Be(){return new Map(z)}function Ce(){return Array.from(z.keys())}function Me(){z.clear()}var mt,z,yt=ot(()=>{mt=Symbol.for("@zibby/agent-workflow.skills");globalThis[mt]||(globalThis[mt]=new Map);z=globalThis[mt]});var _t={};Dt(_t,{getAgentStrategy:()=>Vt,invokeAgent:()=>je,listStrategies:()=>Le,registerStrategy:()=>De});function De(r){if(!r||typeof r.getName!="function"||typeof r.invoke!="function")throw new Error("strategy must implement getName() and invoke() (AgentStrategy shape)");let t=U.findIndex(e=>e.getName()===r.getName());t>=0?U[t]=r:U.push(r)}function Le(){return U.map(r=>r.getName())}function Vt(r={}){let{state:t={},preferredAgent:e=null}=r,o=e||t.agentType||process.env.AGENT_TYPE;if(!o){let n=U.map(i=>i.getName()).join(", ")||"none registered";throw new Error(`No agent specified. Set agentType in state or AGENT_TYPE env var. Available: ${n}`)}y.debug(`[workflow] agent selection: requested=${o}`);let s=U.find(n=>n.getName()===o);if(!s){let n=U.map(i=>i.getName()).join(", ")||"none registered";throw new Error(`Unknown agent '${o}'. Available: ${n}`)}if(!s.canHandle(r))throw new Error(`Agent '${o}' is not available in this environment. Check credentials/environment.`);return y.debug(`[workflow] using agent: ${s.getName()}`),s}async function je(r,t={},e={}){let o=Vt(t),s=t.state?.config||e.config||{},n=s.models||{},i=e.nodeName&&n[e.nodeName]||null,a=n.default||null,l=s.agent?.[o.name]?.model||null,u=i||a||l||e.model||null,p={...e,model:u,workspace:t.state?.workspace||e.workspace,schema:e.schema||t.schema,images:e.images||t.images||[],skills:e.skills||t.skills||[],config:s},c=r,g=p.skills||[];if(g.length>0&&!e.skipPromptFragments){let I=g.map($=>{let T=St($)?.promptFragment;return typeof T=="function"?T():T}).filter(Boolean);I.length>0&&(c+=`
|
|
2
2
|
|
|
3
|
-
${
|
|
3
|
+
${I.join(`
|
|
4
4
|
|
|
5
|
-
`)}`)}let
|
|
5
|
+
`)}`)}let f=t.state?._currentNodeConfig?.extraPromptInstructions?.trim();return f&&(c+=`
|
|
6
6
|
|
|
7
7
|
\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501
|
|
8
8
|
PRIORITY OVERRIDE \u2014 THE FOLLOWING INSTRUCTIONS TAKE PRECEDENCE OVER ALL PREVIOUS CONTENT
|
|
9
9
|
\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501
|
|
10
10
|
|
|
11
|
-
${
|
|
12
|
-
`),
|
|
11
|
+
${f}
|
|
12
|
+
`),y.debug(`[workflow] prompt length: ${c.length} chars`),o.invoke(c,p)}var wt,U,It=ot(()=>{Kt();q();yt();wt=Symbol.for("@zibby/agent-workflow.strategies");globalThis[wt]||(globalThis[wt]=[]);U=globalThis[wt]});var ke=new Set(["__proto__","constructor","prototype"]);function dt(r){if(ke.has(r))throw new Error(`Invalid state key: "${r}"`)}var st=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){dt(t),this._history.push({...this._state}),this._state[t]=e}update(t){let e=Object.getOwnPropertyNames(t);for(let o of e)dt(o);this._history.push({...this._state});for(let o of e)this._state[o]=t[o]}append(t,e){dt(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())}};import W from"handlebars";var nt=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(s=>s[0]);for(let s of o)try{return this.validate(JSON.parse(s))}catch(n){if(!(n instanceof SyntaxError))throw n}return this.validate({result:t.trim()})}validate(t){let e=[];for(let[o,s]of Object.entries(this.schema)){if(s.required&&!(o in t)&&e.push(`Missing required field: ${o}`),o in t&&s.type){let n=typeof t[o];n!==s.type&&e.push(`Field '${o}' expected ${s.type}, got ${n}`)}if(s.validate&&o in t){let n=s.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}};q();import{writeFileSync as Et,readFileSync as
|
|
14
|
+
`)}`);return t}};q();import{writeFileSync as Et,readFileSync as Xt,existsSync as Qt,mkdirSync as Ue}from"node:fs";import{join as bt,dirname as We}from"node:path";import k from"chalk";var Oe="__WORKFLOW_GRAPH_LOG__",V=k.gray("\u2502"),Pe=k.gray("\u250C"),jt=k.gray("\u2514"),ft=k.green("\u25C6"),Ut=k.hex("#c084fc")("\u25C6"),Wt=k.hex("#2dd4bf")("\u25C6"),ht=k.red("\u25C6"),Ft=`${V} `,Gt=2;function Ht(r){return r<1e3?`${r}ms`:`${(r/1e3).toFixed(1)}s`}function Jt(r,t){return(e,o,s)=>{if(typeof e!="string")return r(e,o,s);let n=process.stdout.columns||120,i="";for(let a=0;a<e.length;a++){let l=e[a];t.lineStart&&(i+=Ft,t.col=Gt,t.lineStart=!1),l===`
|
|
15
15
|
`?(i+=l,t.lineStart=!0,t.col=0,t.inEsc=!1):l==="\x1B"?(t.inEsc=!0,i+=l):t.inEsc?(i+=l,(l>="A"&&l<="Z"||l>="a"&&l<="z")&&(t.inEsc=!1)):(t.col++,i+=l,t.col>=n&&(i+=`
|
|
16
|
-
${
|
|
16
|
+
${Ft}`,t.col=Gt))}return r(i,o,s)}}var gt=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=Jt(this._origStdoutWrite,t),process.stderr.write=Jt(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
|
-
`)}_emitGraphLogMarker(t){if(!this._emitWorkflowGraphMarkers)return;let e=`${
|
|
19
|
+
`)}_emitGraphLogMarker(t){if(!this._emitWorkflowGraphMarkers)return;let e=`${Oe}${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(
|
|
25
|
-
`)}stepMemory(t){let e=
|
|
26
|
-
`)}stepFail(t){this._origStdoutWrite?this._writeDot(
|
|
27
|
-
`)}nodeStart(t){this._currentNode=t,this._emitGraphLogMarker({phase:"node_begin",node:t}),this._rawWrite(`${
|
|
23
|
+
`)}step(t){this._origStdoutWrite?this._writeDot(ft,t):process.stdout.write.bind(process.stdout)(`${V} ${ft} ${t}
|
|
24
|
+
`)}stepInfo(t){this.step(t)}stepTool(t){this._origStdoutWrite?this._writeDot(Ut,t):process.stdout.write.bind(process.stdout)(`${V} ${Ut} ${t}
|
|
25
|
+
`)}stepMemory(t){let e=k.hex("#2dd4bf")(t);this._origStdoutWrite?this._writeDot(Wt,e):process.stdout.write.bind(process.stdout)(`${V} ${Wt} ${e}
|
|
26
|
+
`)}stepFail(t){this._origStdoutWrite?this._writeDot(ht,k.red(t)):process.stdout.write.bind(process.stdout)(`${V} ${ht} ${k.red(t)}
|
|
27
|
+
`)}nodeStart(t){this._currentNode=t,this._emitGraphLogMarker({phase:"node_begin",node:t}),this._rawWrite(`${Pe} ${t}`),this._startIntercepting()}nodeComplete(t,e={}){this._stopIntercepting();let{duration:o,details:s}=e;if(s)for(let i of s)this._rawWrite(`${ft} ${i}`);let n=o?k.dim(` ${Ht(o)}`):"";this._rawWrite(`${jt} ${k.green("done")}${n}`),this._emitGraphLogMarker({phase:"node_end",node:t}),this._rawWrite("")}nodeFailed(t,e,o={}){this._stopIntercepting();let{duration:s}=o,n=s?k.dim(` ${Ht(s)}`):"";this._rawWrite(`${ht} ${k.red(e)}`),this._rawWrite(`${jt} ${k.red("failed")}${n}`),this._emitGraphLogMarker({phase:"node_end",node:t}),this._rawWrite("")}route(t,e){this._rawWrite(k.dim(` ${t} \u2192 ${e}`)),this._rawWrite("")}graphComplete(){}},x=new gt;var at=".zibby/output",Yt="sessions",Z=".session-info.json",Zt=".zibby-stop";var zt=["CI_JOB_ID","GITHUB_RUN_ID","CIRCLE_WORKFLOW_ID","BUILD_ID"];W.helpers.inc||W.registerHelper("inc",r=>Number(r)+1);W.helpers.json||W.registerHelper("json",r=>JSON.stringify(r,null,2));W.helpers.eq||W.registerHelper("eq",(r,t)=>r===t);var L=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 nt(t.outputSchema):null,this.retries=t.retries||0,this.onComplete=t.onComplete,this.customExecute=t.execute}async execute(t,e){let o=()=>e&&typeof e.getAll=="function"?e.getAll():t,s=c=>e&&typeof e.get=="function"?e.get(c):t?.[c];if(typeof this.customExecute=="function"){y.debug(`[workflow] node '${this.name}': custom execute (skipping LLM)`);try{let c=await this.customExecute(t);return typeof c=="object"&&c!==null&&c.success===!1?{success:!1,error:c.error||"Node execution failed",raw:c.raw||null}:this.isZodSchema?(y.debug(`[workflow] node '${this.name}': validating output schema`),{success:!0,output:this.outputSchema.parse(c),raw:null}):{success:!0,output:c,raw:null}}catch(c){return y.error(`[workflow] node '${this.name}' failed: ${c.message}`),c.name==="ZodError"&&y.error(`Schema errors: ${JSON.stringify(c.issues||c.errors,null,2)}`),{success:!1,error:c.message,raw:null}}}let n;typeof this.prompt=="function"?n=this.prompt(o()):typeof this.prompt=="string"&&this.prompt.includes("{{")?(this._compiledPrompt||(this._compiledPrompt=W.compile(this.prompt,{noEscape:!0})),n=this._compiledPrompt(o())):n=this.prompt;let i=s("_skillHints");i&&(n=`${i}
|
|
28
28
|
|
|
29
|
-
${n}`);let a=s(),l=a.cwd||process.cwd(),u=a.sessionPath;try{if(u){let c=bt(u,Z);if(Xt(c)){let h=JSON.parse(Vt(c,"utf-8"));h.currentNode=this.name,Et(c,JSON.stringify(h,null,2),"utf-8")}let g=bt(u,"..",Z);if(Xt(g))try{let h=JSON.parse(Vt(g,"utf-8"));h.currentNode=this.name,Et(g,JSON.stringify(h,null,2),"utf-8")}catch{}}}catch(c){w.debug(`[workflow] could not update session info: ${c.message}`)}let p=null;for(let c=0;c<=this.retries;c++)try{w.debug(`[workflow] node '${this.name}' attempt ${c}`);let g=s().config||{},h=g.agents||{},E=this.config.agent??h[this.name]??null,$={state:s()};E&&($.preferredAgent=E);let T={workspace:l,schema:this.isZodSchema?this.outputSchema:null,skills:this.config.skills||[],sessionPath:u,config:g,nodeName:this.name,timeout:this.config?.timeout||3e5},y=t?._coreInvokeAgent;y||(y=(await Promise.resolve().then(()=>(It(),_t))).invokeAgent);let d=await y(n,$,T),I,f;if(typeof d=="string"?(I=d,f=null):d.structured?(I=d.raw||JSON.stringify(d.structured,null,2),f=d.structured):(I=d.raw||JSON.stringify(d,null,2),f=d.extracted||null),u)try{let m=bt(u,this.name,"raw_stream_output.txt");De(Le(m),{recursive:!0}),Et(m,typeof I=="string"?I:JSON.stringify(I),"utf-8")}catch(m){w.debug(`[workflow] could not save raw output: ${m.message}`)}if(this.isZodSchema&&f){w.info(`[workflow] node '${this.name}': output validated: ${JSON.stringify(f,null,2)}`);let m=f;if(typeof this.onComplete=="function")try{m=await this.onComplete(s(),f)}catch(B){w.warn(`[workflow] onComplete hook failed: ${B.message}`)}return{success:!0,output:m,raw:I}}if(typeof this.onComplete=="function")try{return{success:!0,output:await this.onComplete(s(),{raw:I}),raw:I}}catch(m){throw new Error(`onComplete failed: ${m.message}`,{cause:m})}if(this.parser){let m=this.parser.parse(I);return w.info(`[workflow] node '${this.name}': parsed output: ${JSON.stringify(m,null,2)}`),k.step("Output parsed"),{success:!0,output:m,raw:I}}return{success:!0,output:I,raw:I}}catch(g){p=g,c<this.retries&&w.info(`[workflow] node '${this.name}' failed, retrying (${c+1}/${this.retries})\u2026`)}return{success:!1,error:p.message,raw:null}}},X=class extends L{constructor(t){super({...t,_isCustomCode:!0}),this.condition=t.condition}async execute(t,e){let s=e&&typeof e.getAll=="function"?e.getAll():t;return{success:!0,output:{nextNode:this.condition(s)},raw:null}}};q();q();import{mkdirSync as We,existsSync as G,statSync as ne,readdirSync as ie,rmSync as Fe}from"node:fs";import{spawn as oe}from"node:child_process";import{join as j}from"node:path";import{pathToFileURL as Ge}from"node:url";import{AsyncLocalStorage as je}from"node:async_hooks";var $t=new je;function Q(){let r=$t.getStore();return r||Object.freeze({executionId:process.env.EXECUTION_ID||null,parentExecutionId:process.env.PARENT_EXECUTION_ID||null,depth:0,conversationId:process.env.ZIBBY_CONVERSATION_ID||null,dispatchMode:process.env.DISPATCH_MODE||null})}function Qt(r,t){let e=$t.getStore()||Q(),s=Object.freeze({executionId:r.executionId,parentExecutionId:r.parentExecutionId??e.executionId??null,depth:(e.depth||0)+(r.executionId!==e.executionId?1:0),conversationId:r.conversationId!==void 0?r.conversationId:e.conversationId??null,dispatchMode:r.dispatchMode??null});return $t.run(s,t)}var Tt=new Map,vt=new Map,te=new Map;function ee(r,t,e={}){if(!r||typeof r!="string")throw new Error("subgraph-registry.register: name required");if(typeof t!="function")throw new Error("subgraph-registry.register: factory must be a function");Tt.set(r,t),vt.set(r,"ready"),te.set(r,{...e,cachedAt:Date.now()})}function re(r,t){vt.set(r,"failed"),te.set(r,{error:t?.message||String(t),failedAt:Date.now()}),Tt.delete(r)}function se(r){return vt.get(r)==="ready"?Tt.get(r):null}var ct=process.env.ZIBBY_SUBGRAPH_CACHE_DIR||"/tmp/zibby/subgraphs";function He(){return`node${(process.versions?.node||"").split(".")[0]||"unknown"}-${process.platform}-${process.arch}`}var O=class extends Error{constructor(t,e){super(`in-process sub-graph fallback: ${t}${e?` (${e})`:""}`),this.fallback=!0,this.reason=t,this.detail=e||null,this.name="SubgraphFallback"}};function Je(){let r=(process.env.SUBGRAPH_INTERNAL_URL||"").replace(/\/$/,""),t=(process.env.PROGRESS_API_URL||"").replace(/\/executions\/?$/,""),e=r||t,s=process.env.PROJECT_ID,o=process.env.PROJECT_API_TOKEN;if(!e||!s||!o)throw new O("env","SUBGRAPH_INTERNAL_URL/PROGRESS_API_URL/PROJECT_ID/PROJECT_API_TOKEN missing");return{apiBase:e,projectId:s,authToken:o}}async function Ye({apiBase:r,authToken:t,body:e}){let s;try{s=await fetch(`${r}/internal/subgraph/begin`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${t}`},body:JSON.stringify(e)})}catch(n){throw new O("network",`begin fetch failed: ${n.message}`)}let o=null;try{o=await s.json()}catch{}if(!s.ok){if(s.status===404){let n=new Error(`Sub-graph child '${e.childWorkflowType}' not found in project`);throw n.code="SUBGRAPH_NOT_FOUND",n.status=404,n}if(s.status===429){let n=o?.quotaInfo||{},i=new Error(`Sub-graph blocked by quota (${n.used??"?"}/${n.limit??"?"} on ${n.planId||"plan"})`);throw i.code="SUBGRAPH_QUOTA_EXCEEDED",i.status=429,i.quotaInfo=n,i}if(s.status===400&&o?.validationErrors){let n=new Error(`Sub-graph rejected input: ${o?.error||o?.message||"validation failed"}`);throw n.code="SUBGRAPH_INVALID_INPUT",n.status=400,n.validationErrors=o.validationErrors,n.missing=o.missing,n}throw new O("begin-status",`begin returned ${s.status}`)}return o?.data||o}async function F({apiBase:r,authToken:t,payload:e}){try{let s=await fetch(`${r}/internal/subgraph/finalize`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${t}`},body:JSON.stringify(e)});s.ok||w.warn(`[in-process subgraph] finalize returned ${s.status} for ${e.childExecutionId}`)}catch(s){w.warn(`[in-process subgraph] finalize failed: ${s.message}`)}}async function Ze(r,t){let e=j(t,".ready"),s=j(t,"graph.mjs");if(G(e)&&G(s))return;We(t,{recursive:!0});let o=j(t,".lock"),n=!1;try{let{openSync:i,closeSync:a}=await import("node:fs"),l=i(o,"wx");a(l),n=!0}catch(i){if(i.code!=="EEXIST")throw i}if(!n){let i=Date.now()+3e4;for(;Date.now()<i;){if(G(e)&&G(s))return;await new Promise(a=>setTimeout(a,100))}throw new O("bundle-extract-timeout","sibling extract did not complete within 30s")}try{await new Promise((l,u)=>{let p=oe("curl",["-fsSL",r],{stdio:["ignore","pipe","inherit"]}),c=oe("tar",["-xzf","-","-C",t],{stdio:["pipe","inherit","inherit"]});p.stdout.pipe(c.stdin);let g,h,E=()=>{if(g!==void 0&&h!==void 0){if(g!==0)return u(new Error(`curl exited ${g}`));if(h!==0)return u(new Error(`tar exited ${h}`));l()}};p.on("close",$=>{g=$,E()}),c.on("close",$=>{h=$,E()}),p.on("error",u),c.on("error",u)});let{writeFileSync:i,unlinkSync:a}=await import("node:fs");i(e,"");try{a(o)}catch{}}catch(i){try{let{unlinkSync:a}=await import("node:fs");a(o)}catch{}throw new O("bundle-extract-failed",i.message)}}async function ze(r){let t=j(r,"graph.mjs");if(!G(t))throw new O("entry-missing",`graph.mjs missing under ${r}`);let e;try{e=await import(Ge(t).href)}catch(o){throw new O("import-failed",`${o?.code||o?.name||"unknown"}: ${o.message}`)}let s=e.default||Object.values(e).find(o=>typeof o=="function"&&o.prototype?.buildGraph);if(!s)throw new O("entry-class-missing","no buildGraph() class export found");return s}async function ae(r,t={}){if(!r||typeof r!="string")throw new Error("runInProcessSubgraph: workflowName (string) is required");let e=Q(),s;try{s=Je()}catch(f){throw f}w.debug(`[in-process subgraph] begin '${r}' parent=${e.executionId||"<root>"}`);let o=await Ye({apiBase:s.apiBase,authToken:s.authToken,body:{parentExecutionId:e.executionId,childWorkflowType:r,input:t.input||{},...t.conversationId?{conversationId:t.conversationId}:{}}}),{childExecutionId:n,runtimeTag:i,bundlePresignedUrl:a,sourcesPresignedUrl:l,workflowVersion:u,workflowUuid:p,bundleReady:c}=o,g=He();if(i&&i!==g)throw await F({apiBase:s.apiBase,authToken:s.authToken,payload:{childExecutionId:n,status:"canceled",error:{message:`runtimeTag mismatch: parent=${g} child=${i}`,code:"RUNTIME_MISMATCH"}}}),new O("runtime-mismatch",`${g} vs ${i}`);if(!c||!a)throw await F({apiBase:s.apiBase,authToken:s.authToken,payload:{childExecutionId:n,status:"canceled",error:{message:"bundle not ready for in-process; falling back to HTTP",code:"NO_BUNDLE"}}}),new O("no-bundle","workflow bundle not built yet");let h=se(r);if(!h){let f=j(ct,`${p}@${u||"0"}`);try{await Ze(a,f);try{qe()}catch{}}catch(m){throw m.fallback&&await F({apiBase:s.apiBase,authToken:s.authToken,payload:{childExecutionId:n,status:"failed",error:{message:m.message,code:m.reason}}}),m}try{h=await ze(f),ee(r,h,{workflowUuid:p,version:u,runtimeTag:i,cacheDir:f})}catch(m){throw re(r,m),await F({apiBase:s.apiBase,authToken:s.authToken,payload:{childExecutionId:n,status:"failed",error:{message:m.message,code:m.reason||"IMPORT_FAILED"}}}),m.fallback?m:new O("import-failed",m.message)}}let E=Date.now(),T=await(typeof h=="function"&&h.prototype?.buildGraph?new h:h).buildGraph(),y={...t.input||{}},d,I;try{d=await Qt({executionId:n,parentExecutionId:e.executionId,conversationId:t.conversationId!==void 0?t.conversationId:e.conversationId,dispatchMode:"inprocess"},()=>T.run(t.parentAgent,y,{signal:t.signal})),I=d&&typeof d=="object"&&"state"in d?d.state:d}catch(f){throw await F({apiBase:s.apiBase,authToken:s.authToken,payload:{childExecutionId:n,status:"failed",error:{message:f.message,code:f.code||"CHILD_THREW",stack:f.stack},durationMs:Date.now()-E}}),f}if(d&&typeof d=="object"&&d.stoppedExternally){await F({apiBase:s.apiBase,authToken:s.authToken,payload:{childExecutionId:n,status:"canceled",finalState:I,durationMs:Date.now()-E}});let f=new Error(`Sub-graph '${r}' canceled by parent abort`);throw f.code="SUBGRAPH_CANCELED",f.subgraphJobId=n,f}return await F({apiBase:s.apiBase,authToken:s.authToken,payload:{childExecutionId:n,status:"completed",finalState:I,durationMs:Date.now()-E}}),{finalState:I,executionId:n}}function Ke(r){let t=0,e=[r];for(;e.length;){let s=e.pop(),o;try{o=ne(s)}catch{continue}if(o.isDirectory()){let n;try{n=ie(s)}catch{continue}for(let i of n)e.push(j(s,i))}else t+=o.size}return t}function qe({cap:r=Number(process.env.ZIBBY_SUBGRAPH_CACHE_CAP_BYTES||2*1024*1024*1024)}={}){try{if(!G(ct))return{evicted:0,freedBytes:0};let t=ie(ct),e=[],s=0;for(let a of t){let l=j(ct,a),u;try{u=ne(l)}catch{continue}let p=u.isDirectory()?Ke(l):u.size;s+=p,e.push({name:a,full:l,size:p,mtimeMs:u.mtimeMs})}if(s<=r)return{evicted:0,freedBytes:0,totalBytes:s};e.sort((a,l)=>a.mtimeMs-l.mtimeMs);let o=Math.floor(r*.7),n=0,i=0;for(let a of e){if(s-n<=o)break;if(!G(j(a.full,".lock")))try{Fe(a.full,{recursive:!0,force:!0}),n+=a.size,i+=1}catch(l){w.debug(`[sub-graph cache] evict skip ${a.name}: ${l.message}`)}}return i>0&&w.info(`[sub-graph cache] evicted ${i} entr(y/ies), freed ${(n/1024/1024).toFixed(1)}MB`),{evicted:i,freedBytes:n,totalBytes:s-n}}catch(t){return w.debug(`[sub-graph cache] evict failed: ${t.message}`),{evicted:0,freedBytes:0}}}var Ve=2e3,Xe=600*1e3,Qe=new Set(["completed","failed","canceled","timeout"]);function tr(){let r=process.env.PROGRESS_API_URL;if(!r)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 r.replace(/\/executions\/?$/,"")}function er(){let r=process.env.PROJECT_ID;if(!r)throw new Error("Sub-graph dispatch requires PROJECT_ID env var.");return r}function rr(){let r=process.env.PROJECT_API_TOKEN;if(!r)throw new Error("Sub-graph dispatch requires PROJECT_API_TOKEN env var.");return r}function sr(){return process.env.EXECUTION_ID||null}function ce(r,t){return t==null?r:typeof t=="function"?t(r):typeof t=="string"?t.split(".").reduce((e,s)=>e==null?e:e[s],r):r}async function le(r,t={}){if(!r||typeof r!="string")throw new Error("dispatchSubgraph: workflowName (string) is required");let e=Q(),s=Number(process.env.ZIBBY_SUBGRAPH_MAX_DEPTH||10);if((e.depth||0)>=s)throw new Error(`dispatchSubgraph('${r}'): sub-graph depth ${e.depth} reached cap of ${s}. Restructure the graph or raise ZIBBY_SUBGRAPH_MAX_DEPTH.`);if(process.env.ZIBBY_INPROCESS_SUBGRAPH!=="0"&&!t.async)try{w.debug(`[sub-graph] trying in-process for '${r}'`);let{finalState:f}=await ae(r,{input:t.input,conversationId:t.conversationId,signal:t.signal,parentAgent:t.parentAgent}),m=ce(f,t.output);return w.info(`[sub-graph] '${r}' completed in-process`),m}catch(f){if(f instanceof O||f?.fallback)w.info(`[sub-graph] in-process fallback for '${r}': ${f.reason||"unknown"} \u2014 using HTTP`);else throw f}let o=tr(),n=er(),i=rr(),a=sr(),l=`${o}/projects/${encodeURIComponent(n)}/workflows/${encodeURIComponent(r)}/trigger`,u={input:t.input||{},...a?{parentExecutionId:a}:{},...t.conversationId?{conversationId:t.conversationId}:{}};w.info(`[sub-graph] dispatching '${r}' (${t.async?"async":"sync"}) from parent ${a||"<none>"}`);let p=await fetch(l,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${i}`},body:JSON.stringify(u)});if(!p.ok){let f=null,m="";try{f=await p.json(),m=f?.error||f?.message||JSON.stringify(f)}catch{m=await p.text().catch(()=>"")}if(p.status===429){let v=f?.quotaInfo||{},M=new Error(`Sub-graph '${r}' blocked by execution quota (${v.used??"?"}/${v.limit??"?"} on plan ${v.planId||"unknown"}). Sub-workflow runs count toward the same monthly cap as user-triggered runs.`);throw M.code="SUBGRAPH_QUOTA_EXCEEDED",M.status=429,M.subgraph=r,M.quotaInfo=v,M}if(p.status===400){let v=new Error(`Sub-graph '${r}' rejected input: ${m}`);throw v.code="SUBGRAPH_INVALID_INPUT",v.status=400,v.subgraph=r,v.validationErrors=f?.validationErrors||null,v.missing=f?.missing||null,v}let B=new Error(`Sub-graph '${r}' trigger rejected (${p.status}): ${m}`);throw B.code="SUBGRAPH_TRIGGER_FAILED",B.status=p.status,B.subgraph=r,B}let c=await p.json(),g=c?.data?.jobId||c?.jobId;if(!g)throw new Error(`Sub-graph '${r}' trigger returned no jobId: ${JSON.stringify(c).slice(0,200)}`);if(t.async)return w.info(`[sub-graph] async dispatch of '${r}' \u2192 jobId=${g} (not waiting)`),{jobId:g,status:"accepted",workflow:r};let h=Number.isFinite(t.timeoutMs)?t.timeoutMs:Xe,E=Number.isFinite(t.pollIntervalMs)?t.pollIntervalMs:Ve,$=`${o}/executions/${encodeURIComponent(g)}`,T=Date.now()+h,y="accepted",d=0;for(;Date.now()<T;){await new Promise(v=>setTimeout(v,E)),d+=1;let f=await fetch($,{headers:{Authorization:`Bearer ${i}`}});if(!f.ok){if(f.status>=500){w.warn(`[sub-graph] status poll for ${g} returned ${f.status}, will retry`);continue}throw new Error(`Sub-graph status poll failed for ${g}: ${f.status}`)}let m=await f.json(),B=m?.data||m?.execution||m;if(y=B?.status||y,Qe.has(y)){if(y!=="completed"){let S=new Error(`Sub-graph '${r}' (${g}) ended in status '${y}'`);throw S.subgraphJobId=g,S.subgraphStatus=y,S}let v=B?.finalState||B?.state||{},M=ce(v,t.output);return w.info(`[sub-graph] '${r}' (${g}) completed after ${d} polls`),M}}let I=new Error(`Sub-graph '${r}' (${g}) timed out after ${Math.round(h/1e3)}s (last status: ${y})`);throw I.subgraphJobId=g,I.subgraphStatus=y,I}import{existsSync as ue,readFileSync as or}from"node:fs";import{join as At,dirname as pe}from"node:path";var lt=class{static async loadContext(t,e,s={}){let o={},n=s.filenames||["CONTEXT.md","AGENTS.md"];if(t){let a=pe(At(e,t));for(let l of n){let u=await this.findAndMergeContextFiles(l,a,e);if(u){let p=l.replace(/\.[^.]+$/,"").toLowerCase();o[p]=u}}}let i=s.discovery||{};for(let[a,l]of Object.entries(i))try{let u=At(e,l);ue(u)&&(o[a]=await this.loadFile(u))}catch(u){console.warn(`[workflow] could not load context '${a}' from '${l}': ${u.message}`)}return o}static async findAndMergeContextFiles(t,e,s){let o=[],n=e;for(;n.startsWith(s);){let i=At(n,t);if(ue(i))try{o.unshift(await this.loadFile(i))}catch(l){console.warn(`[workflow] could not load ${t} from ${i}: ${l.message}`)}let a=pe(n);if(a===n)break;n=a}return o.length===0?null:o.every(i=>typeof i=="string")?o.join(`
|
|
29
|
+
${n}`);let a=o(),l=a.cwd||process.cwd(),u=a.sessionPath;try{if(u){let c=bt(u,Z);if(Qt(c)){let f=JSON.parse(Xt(c,"utf-8"));f.currentNode=this.name,Et(c,JSON.stringify(f,null,2),"utf-8")}let g=bt(u,"..",Z);if(Qt(g))try{let f=JSON.parse(Xt(g,"utf-8"));f.currentNode=this.name,Et(g,JSON.stringify(f,null,2),"utf-8")}catch{}}}catch(c){y.debug(`[workflow] could not update session info: ${c.message}`)}let p=null;for(let c=0;c<=this.retries;c++)try{y.debug(`[workflow] node '${this.name}' attempt ${c}`);let g=o().config||{},f=g.agents||{},I=this.config.agent??f[this.name]??null,$={state:o()};I&&($.preferredAgent=I);let T={workspace:l,schema:this.isZodSchema?this.outputSchema:null,skills:this.config.skills||[],sessionPath:u,config:g,nodeName:this.name,timeout:this.config?.timeout||3e5},E=t?._coreInvokeAgent;E||(E=(await Promise.resolve().then(()=>(It(),_t))).invokeAgent);let d=await E(n,$,T),_,h;if(typeof d=="string"?(_=d,h=null):d.structured?(_=d.raw||JSON.stringify(d.structured,null,2),h=d.structured):(_=d.raw||JSON.stringify(d,null,2),h=d.extracted||null),u)try{let m=bt(u,this.name,"raw_stream_output.txt");Ue(We(m),{recursive:!0}),Et(m,typeof _=="string"?_:JSON.stringify(_),"utf-8")}catch(m){y.debug(`[workflow] could not save raw output: ${m.message}`)}if(this.isZodSchema&&h){y.info(`[workflow] node '${this.name}': output validated: ${JSON.stringify(h,null,2)}`);let m=h;if(typeof this.onComplete=="function")try{m=await this.onComplete(o(),h)}catch(B){y.warn(`[workflow] onComplete hook failed: ${B.message}`)}return{success:!0,output:m,raw:_}}if(typeof this.onComplete=="function")try{return{success:!0,output:await this.onComplete(o(),{raw:_}),raw:_}}catch(m){throw new Error(`onComplete failed: ${m.message}`,{cause:m})}if(this.parser){let m=this.parser.parse(_);return y.info(`[workflow] node '${this.name}': parsed output: ${JSON.stringify(m,null,2)}`),x.step("Output parsed"),{success:!0,output:m,raw:_}}return{success:!0,output:_,raw:_}}catch(g){p=g,c<this.retries&&y.info(`[workflow] node '${this.name}' failed, retrying (${c+1}/${this.retries})\u2026`)}return{success:!1,error:p.message,raw:null}}},X=class extends L{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}}};q();q();import{mkdirSync as He,existsSync as G,statSync as ie,readdirSync as ae,rmSync as Je}from"node:fs";import{spawn as ne}from"node:child_process";import{join as j}from"node:path";import{pathToFileURL as Ye}from"node:url";import{AsyncLocalStorage as Fe}from"node:async_hooks";var $t=new Fe;function Q(){let r=$t.getStore();return r||Object.freeze({executionId:process.env.EXECUTION_ID||null,parentExecutionId:process.env.PARENT_EXECUTION_ID||null,depth:0,conversationId:process.env.ZIBBY_CONVERSATION_ID||null,dispatchMode:process.env.DISPATCH_MODE||null})}function te(r,t){let e=$t.getStore()||Q(),o=Object.freeze({executionId:r.executionId,parentExecutionId:r.parentExecutionId??e.executionId??null,depth:(e.depth||0)+(r.executionId!==e.executionId?1:0),conversationId:r.conversationId!==void 0?r.conversationId:e.conversationId??null,dispatchMode:r.dispatchMode??null});return $t.run(o,t)}var Tt=new Map,vt=new Map,ee=new Map;function re(r,t,e={}){if(!r||typeof r!="string")throw new Error("subgraph-registry.register: name required");if(typeof t!="function")throw new Error("subgraph-registry.register: factory must be a function");Tt.set(r,t),vt.set(r,"ready"),ee.set(r,{...e,cachedAt:Date.now()})}function oe(r,t){vt.set(r,"failed"),ee.set(r,{error:t?.message||String(t),failedAt:Date.now()}),Tt.delete(r)}function se(r){return vt.get(r)==="ready"?Tt.get(r):null}var ct=process.env.ZIBBY_SUBGRAPH_CACHE_DIR||"/tmp/zibby/subgraphs";function Ze(){return`node${(process.versions?.node||"").split(".")[0]||"unknown"}-${process.platform}-${process.arch}`}var O=class extends Error{constructor(t,e){super(`in-process sub-graph fallback: ${t}${e?` (${e})`:""}`),this.fallback=!0,this.reason=t,this.detail=e||null,this.name="SubgraphFallback"}};function ze(){let r=(process.env.SUBGRAPH_INTERNAL_URL||"").replace(/\/$/,""),t=(process.env.PROGRESS_API_URL||"").replace(/\/executions\/?$/,""),e=r||t,o=process.env.PROJECT_ID,s=process.env.PROJECT_API_TOKEN;if(!e||!o||!s)throw new O("env","SUBGRAPH_INTERNAL_URL/PROGRESS_API_URL/PROJECT_ID/PROJECT_API_TOKEN missing");return{apiBase:e,projectId:o,authToken:s}}async function Ke({apiBase:r,authToken:t,body:e}){let o;try{o=await fetch(`${r}/internal/subgraph/begin`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${t}`},body:JSON.stringify(e)})}catch(n){throw new O("network",`begin fetch failed: ${n.message}`)}let s=null;try{s=await o.json()}catch{}if(!o.ok){if(o.status===404){let n=new Error(`Sub-graph child '${e.childWorkflowType}' not found in project`);throw n.code="SUBGRAPH_NOT_FOUND",n.status=404,n}if(o.status===429){let n=s?.quotaInfo||{},i=new Error(`Sub-graph blocked by quota (${n.used??"?"}/${n.limit??"?"} on ${n.planId||"plan"})`);throw i.code="SUBGRAPH_QUOTA_EXCEEDED",i.status=429,i.quotaInfo=n,i}if(o.status===400&&s?.validationErrors){let n=new Error(`Sub-graph rejected input: ${s?.error||s?.message||"validation failed"}`);throw n.code="SUBGRAPH_INVALID_INPUT",n.status=400,n.validationErrors=s.validationErrors,n.missing=s.missing,n}throw new O("begin-status",`begin returned ${o.status}`)}return s?.data||s}async function F({apiBase:r,authToken:t,payload:e}){try{let o=await fetch(`${r}/internal/subgraph/finalize`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${t}`},body:JSON.stringify(e)});o.ok||y.warn(`[in-process subgraph] finalize returned ${o.status} for ${e.childExecutionId}`)}catch(o){y.warn(`[in-process subgraph] finalize failed: ${o.message}`)}}async function qe(r,t){let e=j(t,".ready"),o=j(t,"graph.mjs");if(G(e)&&G(o))return;He(t,{recursive:!0});let s=j(t,".lock"),n=!1;try{let{openSync:i,closeSync:a}=await import("node:fs"),l=i(s,"wx");a(l),n=!0}catch(i){if(i.code!=="EEXIST")throw i}if(!n){let i=Date.now()+3e4;for(;Date.now()<i;){if(G(e)&&G(o))return;await new Promise(a=>setTimeout(a,100))}throw new O("bundle-extract-timeout","sibling extract did not complete within 30s")}try{await new Promise((l,u)=>{let p=ne("curl",["-fsSL",r],{stdio:["ignore","pipe","inherit"]}),c=ne("tar",["-xzf","-","-C",t],{stdio:["pipe","inherit","inherit"]});p.stdout.pipe(c.stdin);let g,f,I=()=>{if(g!==void 0&&f!==void 0){if(g!==0)return u(new Error(`curl exited ${g}`));if(f!==0)return u(new Error(`tar exited ${f}`));l()}};p.on("close",$=>{g=$,I()}),c.on("close",$=>{f=$,I()}),p.on("error",u),c.on("error",u)});let{writeFileSync:i,unlinkSync:a}=await import("node:fs");i(e,"");try{a(s)}catch{}}catch(i){try{let{unlinkSync:a}=await import("node:fs");a(s)}catch{}throw new O("bundle-extract-failed",i.message)}}async function Ve(r){let t=j(r,"graph.mjs");if(!G(t))throw new O("entry-missing",`graph.mjs missing under ${r}`);let e;try{e=await import(Ye(t).href)}catch(s){throw new O("import-failed",`${s?.code||s?.name||"unknown"}: ${s.message}`)}let o=e.default||Object.values(e).find(s=>typeof s=="function"&&s.prototype?.buildGraph);if(!o)throw new O("entry-class-missing","no buildGraph() class export found");return o}async function ce(r,t={}){if(!r||typeof r!="string")throw new Error("runInProcessSubgraph: workflowName (string) is required");let e=Q(),o;try{o=ze()}catch(h){throw h}y.debug(`[in-process subgraph] begin '${r}' parent=${e.executionId||"<root>"}`);let s=await Ke({apiBase:o.apiBase,authToken:o.authToken,body:{parentExecutionId:e.executionId,childWorkflowType:r,input:t.input||{},...t.conversationId?{conversationId:t.conversationId}:{}}}),{childExecutionId:n,runtimeTag:i,bundlePresignedUrl:a,sourcesPresignedUrl:l,workflowVersion:u,workflowUuid:p,bundleReady:c}=s,g=Ze();if(i&&i!==g)throw await F({apiBase:o.apiBase,authToken:o.authToken,payload:{childExecutionId:n,status:"canceled",error:{message:`runtimeTag mismatch: parent=${g} child=${i}`,code:"RUNTIME_MISMATCH"}}}),new O("runtime-mismatch",`${g} vs ${i}`);if(!c||!a)throw await F({apiBase:o.apiBase,authToken:o.authToken,payload:{childExecutionId:n,status:"canceled",error:{message:"bundle not ready for in-process; falling back to HTTP",code:"NO_BUNDLE"}}}),new O("no-bundle","workflow bundle not built yet");let f=se(r);if(!f){let h=j(ct,`${p}@${u||"0"}`);try{await qe(a,h);try{Qe()}catch{}}catch(m){throw m.fallback&&await F({apiBase:o.apiBase,authToken:o.authToken,payload:{childExecutionId:n,status:"failed",error:{message:m.message,code:m.reason}}}),m}try{f=await Ve(h),re(r,f,{workflowUuid:p,version:u,runtimeTag:i,cacheDir:h})}catch(m){throw oe(r,m),await F({apiBase:o.apiBase,authToken:o.authToken,payload:{childExecutionId:n,status:"failed",error:{message:m.message,code:m.reason||"IMPORT_FAILED"}}}),m.fallback?m:new O("import-failed",m.message)}}let I=Date.now(),T=await(typeof f=="function"&&f.prototype?.buildGraph?new f:f).buildGraph(),E={...t.input||{}},d,_;try{d=await te({executionId:n,parentExecutionId:e.executionId,conversationId:t.conversationId!==void 0?t.conversationId:e.conversationId,dispatchMode:"inprocess"},()=>T.run(t.parentAgent,E,{signal:t.signal})),_=d&&typeof d=="object"&&"state"in d?d.state:d}catch(h){throw await F({apiBase:o.apiBase,authToken:o.authToken,payload:{childExecutionId:n,status:"failed",error:{message:h.message,code:h.code||"CHILD_THREW",stack:h.stack},durationMs:Date.now()-I}}),h}if(d&&typeof d=="object"&&d.stoppedExternally){await F({apiBase:o.apiBase,authToken:o.authToken,payload:{childExecutionId:n,status:"canceled",finalState:_,durationMs:Date.now()-I}});let h=new Error(`Sub-graph '${r}' canceled by parent abort`);throw h.code="SUBGRAPH_CANCELED",h.subgraphJobId=n,h}return await F({apiBase:o.apiBase,authToken:o.authToken,payload:{childExecutionId:n,status:"completed",finalState:_,durationMs:Date.now()-I}}),{finalState:_,executionId:n}}function Xe(r){let t=0,e=[r];for(;e.length;){let o=e.pop(),s;try{s=ie(o)}catch{continue}if(s.isDirectory()){let n;try{n=ae(o)}catch{continue}for(let i of n)e.push(j(o,i))}else t+=s.size}return t}function Qe({cap:r=Number(process.env.ZIBBY_SUBGRAPH_CACHE_CAP_BYTES||2*1024*1024*1024)}={}){try{if(!G(ct))return{evicted:0,freedBytes:0};let t=ae(ct),e=[],o=0;for(let a of t){let l=j(ct,a),u;try{u=ie(l)}catch{continue}let p=u.isDirectory()?Xe(l):u.size;o+=p,e.push({name:a,full:l,size:p,mtimeMs:u.mtimeMs})}if(o<=r)return{evicted:0,freedBytes:0,totalBytes:o};e.sort((a,l)=>a.mtimeMs-l.mtimeMs);let s=Math.floor(r*.7),n=0,i=0;for(let a of e){if(o-n<=s)break;if(!G(j(a.full,".lock")))try{Je(a.full,{recursive:!0,force:!0}),n+=a.size,i+=1}catch(l){y.debug(`[sub-graph cache] evict skip ${a.name}: ${l.message}`)}}return i>0&&y.info(`[sub-graph cache] evicted ${i} entr(y/ies), freed ${(n/1024/1024).toFixed(1)}MB`),{evicted:i,freedBytes:n,totalBytes:o-n}}catch(t){return y.debug(`[sub-graph cache] evict failed: ${t.message}`),{evicted:0,freedBytes:0}}}var tr=2e3,er=600*1e3,rr=new Set(["completed","failed","canceled","timeout"]);function or(){let r=process.env.PROGRESS_API_URL;if(!r)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 r.replace(/\/executions\/?$/,"")}function sr(){let r=process.env.PROJECT_ID;if(!r)throw new Error("Sub-graph dispatch requires PROJECT_ID env var.");return r}function nr(){let r=process.env.PROJECT_API_TOKEN;if(!r)throw new Error("Sub-graph dispatch requires PROJECT_API_TOKEN env var.");return r}function ir(){return process.env.EXECUTION_ID||null}function le(r,t){return t==null?r:typeof t=="function"?t(r):typeof t=="string"?t.split(".").reduce((e,o)=>e==null?e:e[o],r):r}async function ue(r,t={}){if(!r||typeof r!="string")throw new Error("dispatchSubgraph: workflowName (string) is required");let e=Q(),o=Number(process.env.ZIBBY_SUBGRAPH_MAX_DEPTH||10);if((e.depth||0)>=o)throw new Error(`dispatchSubgraph('${r}'): sub-graph depth ${e.depth} reached cap of ${o}. Restructure the graph or raise ZIBBY_SUBGRAPH_MAX_DEPTH.`);if(process.env.ZIBBY_INPROCESS_SUBGRAPH!=="0"&&!t.async)try{y.debug(`[sub-graph] trying in-process for '${r}'`);let{finalState:h}=await ce(r,{input:t.input,conversationId:t.conversationId,signal:t.signal,parentAgent:t.parentAgent}),m=le(h,t.output);return y.info(`[sub-graph] '${r}' completed in-process`),m}catch(h){if(h instanceof O||h?.fallback)y.info(`[sub-graph] in-process fallback for '${r}': ${h.reason||"unknown"} \u2014 using HTTP`);else throw h}let s=or(),n=sr(),i=nr(),a=ir(),l=`${s}/projects/${encodeURIComponent(n)}/workflows/${encodeURIComponent(r)}/trigger`,u={input:t.input||{},...a?{parentExecutionId:a}:{},...t.conversationId?{conversationId:t.conversationId}:{}};y.info(`[sub-graph] dispatching '${r}' (${t.async?"async":"sync"}) from parent ${a||"<none>"}`);let p=await fetch(l,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${i}`},body:JSON.stringify(u)});if(!p.ok){let h=null,m="";try{h=await p.json(),m=h?.error||h?.message||JSON.stringify(h)}catch{m=await p.text().catch(()=>"")}if(p.status===429){let v=h?.quotaInfo||{},M=new Error(`Sub-graph '${r}' blocked by execution quota (${v.used??"?"}/${v.limit??"?"} on plan ${v.planId||"unknown"}). Sub-workflow runs count toward the same monthly cap as user-triggered runs.`);throw M.code="SUBGRAPH_QUOTA_EXCEEDED",M.status=429,M.subgraph=r,M.quotaInfo=v,M}if(p.status===400){let v=new Error(`Sub-graph '${r}' rejected input: ${m}`);throw v.code="SUBGRAPH_INVALID_INPUT",v.status=400,v.subgraph=r,v.validationErrors=h?.validationErrors||null,v.missing=h?.missing||null,v}let B=new Error(`Sub-graph '${r}' trigger rejected (${p.status}): ${m}`);throw B.code="SUBGRAPH_TRIGGER_FAILED",B.status=p.status,B.subgraph=r,B}let c=await p.json(),g=c?.data?.jobId||c?.jobId;if(!g)throw new Error(`Sub-graph '${r}' trigger returned no jobId: ${JSON.stringify(c).slice(0,200)}`);if(t.async)return y.info(`[sub-graph] async dispatch of '${r}' \u2192 jobId=${g} (not waiting)`),{jobId:g,status:"accepted",workflow:r};let f=Number.isFinite(t.timeoutMs)?t.timeoutMs:er,I=Number.isFinite(t.pollIntervalMs)?t.pollIntervalMs:tr,$=`${s}/executions/${encodeURIComponent(g)}`,T=Date.now()+f,E="accepted",d=0;for(;Date.now()<T;){await new Promise(v=>setTimeout(v,I)),d+=1;let h=await fetch($,{headers:{Authorization:`Bearer ${i}`}});if(!h.ok){if(h.status>=500){y.warn(`[sub-graph] status poll for ${g} returned ${h.status}, will retry`);continue}throw new Error(`Sub-graph status poll failed for ${g}: ${h.status}`)}let m=await h.json(),B=m?.data||m?.execution||m;if(E=B?.status||E,rr.has(E)){if(E!=="completed"){let S=new Error(`Sub-graph '${r}' (${g}) ended in status '${E}'`);throw S.subgraphJobId=g,S.subgraphStatus=E,S}let v=B?.finalState||B?.state||{},M=le(v,t.output);return y.info(`[sub-graph] '${r}' (${g}) completed after ${d} polls`),M}}let _=new Error(`Sub-graph '${r}' (${g}) timed out after ${Math.round(f/1e3)}s (last status: ${E})`);throw _.subgraphJobId=g,_.subgraphStatus=E,_}import{existsSync as pe,readFileSync as ar}from"node:fs";import{join as At,dirname as de}from"node:path";var lt=class{static async loadContext(t,e,o={}){let s={},n=o.filenames||["CONTEXT.md","AGENTS.md"];if(t){let a=de(At(e,t));for(let l of n){let u=await this.findAndMergeContextFiles(l,a,e);if(u){let p=l.replace(/\.[^.]+$/,"").toLowerCase();s[p]=u}}}let i=o.discovery||{};for(let[a,l]of Object.entries(i))try{let u=At(e,l);pe(u)&&(s[a]=await this.loadFile(u))}catch(u){console.warn(`[workflow] could not load context '${a}' from '${l}': ${u.message}`)}return s}static async findAndMergeContextFiles(t,e,o){let s=[],n=e;for(;n.startsWith(o);){let i=At(n,t);if(pe(i))try{s.unshift(await this.loadFile(i))}catch(l){console.warn(`[workflow] could not load ${t} from ${i}: ${l.message}`)}let a=de(n);if(a===n)break;n=a}return s.length===0?null:s.every(i=>typeof i=="string")?s.join(`
|
|
30
30
|
|
|
31
31
|
---
|
|
32
32
|
|
|
33
|
-
`):
|
|
33
|
+
`):s.every(i=>typeof i=="object")?Object.assign({},...s):s[s.length-1]}static async loadFile(t){let e=ar(t,"utf-8");if(t.endsWith(".json"))return JSON.parse(e);if(t.endsWith(".js")||t.endsWith(".mjs")){let{pathToFileURL:o}=await import("url"),s=await import(o(t).href);return s.default||s}return e}};import{mkdirSync as me,existsSync as kt,writeFileSync as fe,unlinkSync as cr}from"node:fs";import{join as H,resolve as Se}from"node:path";import{config as lr}from"dotenv";import{zodToJsonSchema as he}from"zod-to-json-schema";import{z as ut}from"zod";import ur from"handlebars";function pr({traceFrom:r,sessionId:t,sessionPath:e,idSource:o,mkdirFresh:s}){if(!(process.env.ZIBBY_SESSION_LOG==="1"||process.env.ZIBBY_SESSION_LOG==="true"))return;let i=typeof process.ppid=="number"?process.ppid:"n/a",a=`[zibby:session] from=${r} pid=${process.pid} ppid=${i} sessionId=${t} source=${o} mkdir=${s?"yes":"no"} path=${e}`;if(console.log(a),process.env.ZIBBY_TRACE_SESSION==="1"||process.env.ZIBBY_TRACE_SESSION==="true"){let p=(new Error("session trace").stack||"").split(`
|
|
34
34
|
`).slice(2,14).join(`
|
|
35
35
|
`);console.log(`[zibby:session] stack (${r}):
|
|
36
|
-
${p}`)}}function lr(){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 ur(){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 me(String(t).trim())}catch{return String(t).trim()}}function pr(){lr()||(delete process.env.ZIBBY_SESSION_PATH,delete process.env.ZIBBY_SESSION_ID)}function dr({sessionPath:r,sessionId:t}){r&&typeof r=="string"&&(process.env.ZIBBY_SESSION_PATH=r),t!=null&&String(t).trim()!==""&&(process.env.ZIBBY_SESSION_ID=String(t).trim())}function hr(r={}){let t=Zt.map(n=>process.env[n]).find(Boolean),e=Math.random().toString(36).slice(2,6),s=t||`${Date.now()}_${e}`,o=r.paths?.sessionPrefix;return o?`${o}_${s}`:s}function fr({cwd:r=process.cwd(),config:t={},initialState:e={},traceFrom:s="resolveWorkflowSession"}={}){let o=e.sessionPath,n=e.sessionTimestamp,i="initialState.sessionPath";if(!o&&process.env.ZIBBY_SESSION_PATH)try{let u=me(String(process.env.ZIBBY_SESSION_PATH));u&&(o=u,i="ZIBBY_SESSION_PATH")}catch{}let a;if(o)a=String(o).split(/[/\\]/).filter(Boolean).pop(),n==null&&(n=Date.now());else{let u=process.env.ZIBBY_SESSION_ID&&String(process.env.ZIBBY_SESSION_ID).trim();if(u)a=u,i="ZIBBY_SESSION_ID";else{let c=t.sessionId!=null?String(t.sessionId).trim():"";c&&c!=="last"?(a=c,i="config.sessionId"):(a=hr(t),i="generated")}n=n??Date.now();let p=t.paths?.output||at;o=H(r,p,Jt,a)}let l=!kt(o);return l&&ge(o,{recursive:!0}),(l||i!=="initialState.sessionPath")&&cr({traceFrom:s,sessionId:a,sessionPath:o,idSource:i,mkdirFresh:l}),dr({sessionPath:o,sessionId:a}),{sessionPath:o,sessionId:a,sessionTimestamp:n}}var fe=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,s={}){if(!(e instanceof L)&&e&&typeof e=="object"&&typeof e.workflow=="string"){let n=e,i={name:t,_isCustomCode:!0,retries:n.retries,onComplete:n.onComplete,execute:async l=>{let u=l?.state&&typeof l.state.getAll=="function"?l.state.getAll():l,p;return typeof n.input=="function"?p=n.input(u):n.input&&typeof n.input=="object"?p=n.input:p={},le(n.workflow,{input:p,async:n.async===!0,conversationId:typeof n.conversationId=="function"?n.conversationId(u):n.conversationId,output:n.output,timeoutMs:n.timeoutMs,pollIntervalMs:n.pollIntervalMs,signal:u?._signal,parentAgent:l?.agent})}},a=new L(i);return a.name=t,this.nodes.set(t,a),s.prompt&&this.nodePrompts.set(t,s.prompt),Object.keys(s).length>0&&this.nodeOptions.set(t,s),this}let o=e instanceof L?e:new L(e);return o.name=t,this.nodes.set(t,o),s.prompt?this.nodePrompts.set(t,s.prompt):typeof e?.prompt=="string"&&e.prompt.trim()&&this.nodePrompts.set(t,e.prompt),Object.keys(s).length>0&&this.nodeOptions.set(t,s),this}addConditionalNode(t,e){return this.nodes.set(t,new X({...e,name:t})),this}addEdge(t,e){return this.edges.set(t,e),this}setNodeType(t,e){return this.nodeTypeMap.set(t,e),this}addConditionalEdges(t,e,{labels:s}={}){return this.edges.set(t,{conditional:!0,routes:e,labels:s}),typeof e=="function"&&this.conditionalCodeMap.set(t,e.toString()),this}setEntryPoint(t){return this.entryPoint=t,this}use(t){return typeof t=="function"&&this.middleware.push(t),this}_composeMiddleware(t,e,s,o,n){let i=s;for(let a=t.length-1;a>=0;a--){let l=t[a],u=i;i=()=>l(e,u,o,n)}return i()}serialize(){let t=[],e={};for(let[p,c]of this.nodes){let g=this.nodeTypeMap.get(p)||(c instanceof X?"decision":p);t.push({id:p,type:g,data:{nodeType:g,label:p}});let h={};c._isCustomCode&&typeof c.execute=="function"&&(h.customCode=c.execute.toString());let E=this.nodePrompts.get(p);if(E)h.prompt=E;else if(typeof c.prompt=="function")try{let y=c.prompt({});typeof y=="string"&&y.trim()&&(h.prompt=y,h.promptIsCode=!0)}catch{}if(typeof c.customExecute=="function"&&(h.executeCode=c.customExecute.toString()),c.outputSchema)if(typeof c.outputSchema._def<"u"){let y=null;if(typeof ut?.toJSONSchema=="function")try{y=ut.toJSONSchema(c.outputSchema)}catch{}if(!y)try{y=he(c.outputSchema,{target:"openApi3"})}catch{}h.outputSchema=y?{jsonSchema:y,variables:this._flattenJsonSchemaToVariables(y)}:{schema:c.outputSchema}}else h.outputSchema={schema:c.outputSchema};let $=(this.resolvedToolsMap||{})[p];$?.toolIds&&(h.tools=$.toolIds);let T=Array.isArray(c?.config?.skills)?c.config.skills:Array.isArray(c?.skills)?c.skills:null;T&&T.length>0&&(h.skills=[...T]),Object.keys(h).length>0&&(e[p]=h)}let s=[];for(let[p,c]of this.edges)if(typeof c=="string")s.push({source:p,target:c});else if(c.conditional){let g=this.conditionalCodeMap.get(p)||c.routes.toString(),h=this._inferConditionalTargets(c.routes,c.labels),E=c.labels||{};for(let $ of h){let T={source:p,target:$,data:{conditionalCode:g}};E[$]&&(T.label=E[$]),s.push(T)}}let o=p=>{if(!p)return null;if(typeof ut?.toJSONSchema=="function")try{return ut.toJSONSchema(p)}catch{}try{return he(p,{target:"openApi3"})}catch{return null}};this.entryPoint&&this.nodes.has(this.entryPoint)&&(t.unshift({id:"START",type:"start",data:{nodeType:"start",label:"Start"}}),s.unshift({source:"START",target:this.entryPoint}));let n=0;for(let p of s)if(p.target==="END"){n+=1;let c=`END__${n}`;p.target=c,t.push({id:c,type:"end",data:{nodeType:"end",label:"End"}})}for(let p of this.nodes.keys())if(!this.edges.has(p)){n+=1;let c=`END__${n}`;t.push({id:c,type:"end",data:{nodeType:"end",label:"End"}}),s.push({source:p,target:c})}let i=this._runtimeSchema(),a=o(i||this.stateSchema),l=o(this.inputSchema),u=o(this.contextSchema);return{nodes:t,edges:s,nodeConfigs:e,stateSchema:a,inputSchema:l,contextSchema:u}}_inferConditionalTargets(t,e){let s=t.toString(),o=new Set,n=/(['"])((?:\\.|(?!\1).)*?)\1|`((?:\\.|[^`$]|\$(?!\{))*?)`/g,i;for(;(i=n.exec(s))!==null;){let u=i[2]!==void 0?i[2]:i[3];u!==void 0&&u!==""&&o.add(u)}let a=new Set(["END","START","__end__","__start__"]);for(let u of this.nodes.keys())a.add(u);if(e&&typeof e=="object")for(let u of Object.keys(e))a.add(u);let l=new Set;for(let u of o)a.has(u)&&l.add(u);if(l.size===0){let u=/return\s+['"]([^'"]+)['"]/g,p;for(;(p=u.exec(s))!==null;)l.add(p[1])}return[...l]}_flattenJsonSchemaToVariables(t,e=""){let s=t;if(t.$ref&&t.definitions){let o=t.$ref.replace("#/definitions/","");s=t.definitions[o]||t}return this._flattenSchema(s,e)}_flattenSchema(t,e=""){if(!t||typeof t!="object")return[];let s=[],o=t.properties||{},n=t.required||[];for(let[i,a]of Object.entries(o)){let l=e?`${e}.${i}`:i;s.push({path:l,type:a.type||"unknown",label:a.description||this._formatLabel(i),optional:!n.includes(i)}),a.type==="object"&&a.properties&&s.push(...this._flattenSchema(a,l)),a.type==="array"&&a.items?.type==="object"&&a.items.properties&&s.push(...this._flattenSchema(a.items,`${l}[]`))}return s}_formatLabel(t){return t.replace(/([A-Z])/g," $1").replace(/^./,e=>e.toUpperCase()).trim()}_summarizeNodeOutput(t,e){if(!e||typeof e!="object")return[];let s=[];e.success!==void 0&&s.push(`Result: ${e.success?"passed":"failed"}`);for(let[o,n]of Object.entries(e))if(!(o==="success"||o==="raw"||o==="nextNode")){if(typeof n=="string"&&n.length<=80)s.push(`${o}: ${n}`);else if(Array.isArray(n)){let i=n.length,a=n.filter(u=>u?.passed===!0).length,l=n.some(u=>u?.passed!==void 0);s.push(l?`${o}: ${a}/${i} passed${i-a?`, ${i-a} failed`:""}`:`${o}: ${i} items`)}if(s.length>=4)break}return s}async run(t,e={},s={}){if(!this.entryPoint)throw new Error("No entry point set for graph");let o=new AbortController;s.signal&&(s.signal.aborted?o.abort():s.signal.addEventListener("abort",()=>o.abort(),{once:!0}));let n=s.strategyAbortTimeoutMs??e.config?.strategyAbortTimeoutMs??5e3,i=e.cwd||process.cwd();ir({path:H(i,".env")});let a=e.config||{};if(!a||Object.keys(a).length===0)try{let _=H(i,".zibby.config.js");kt(_)&&(a=(await import(_)).default||{})}catch{}process.env.EXECUTION_ID&&!a.agent?.strictMode&&(a.agent={...a.agent,strictMode:!0});let l=e.agentType;if(!l){let _=a?.agent;_?.provider?l=_.provider:_?.gemini?l="gemini":_?.claude?l="claude":_?.cursor?l="cursor":_?.codex?l="codex":l=process.env.AGENT_TYPE||"cursor"}let u=e.contextConfig||t?.config?.contextConfig||t?.config?.context||a?.context||{},p=this._runtimeSchema();if(p){let _=p.safeParse(e);if(!_.success){let x=_.error.issues.map(P=>`${P.path.join(".")}: ${P.message}`);throw console.error("\u274C Initial state validation failed:"),x.forEach(P=>console.error(` - ${P}`)),new Error(`State validation failed: ${x.join(", ")}`)}k.step("State validated against schema")}let c=ur(),g=e.sessionPath||c;g||pr();let{sessionPath:h,sessionTimestamp:E,sessionId:$}=fr({cwd:i,config:a,traceFrom:"WorkflowGraph.run",initialState:{sessionPath:g,sessionTimestamp:e.sessionTimestamp}});k.step(`Session ${$}`);let T=await lt.loadContext(e.specPath||"",i,u);Object.keys(T).length>0&&k.step(`Context loaded: ${Object.keys(T).join(", ")}`);let y=e.outputPath;!y&&e.specPath&&(t?.calculateOutputPath?y=t.calculateOutputPath(e.specPath):console.warn(`\u26A0\uFE0F outputPath not resolved (specPath=${e.specPath})`));let d=new ot({...e,config:a,agentType:l,outputPath:y,sessionPath:h,sessionTimestamp:E,context:T,resolvedTools:this.resolvedToolsMap||{},_signal:o.signal}),I=new Map;try{await import("@zibby/skills")}catch{}let{getSkill:f}=await Promise.resolve().then(()=>(wt(),Kt)),m=a.skills&&typeof a.skills=="object"?a.skills:{},B=Object.values(m).filter(_=>_&&typeof _=="object"&&typeof _.id=="string"),v=_=>{for(let x of B)if(x.id===_)return x;return f(_)},M=new Set;for(let[,_]of this.nodes)for(let x of _.config?.skills||[])M.add(x);for(let _ of M){let x=v(_);if(typeof x?.middleware=="function")try{let P=await x.middleware();typeof P=="function"&&I.set(_,P)}catch{}}let S=this.entryPoint,tt=[],Ot=a?.recursionLimit??100,Se=0;try{for(;S&&S!=="END";){if(++Se>Ot)throw new Error(`Workflow exceeded recursion limit (${Ot}) \u2014 likely a cyclic conditional route. Set config.recursionLimit if you need a higher cap.`);let x=H(h,Yt);if(kt(x)){try{nr(x)}catch{}o.abort()}if(o.signal.aborted)return console.warn(`
|
|
37
|
-
\u{1F6D1} External stop requested \u2014 ending workflow.`),
|
|
36
|
+
${p}`)}}function dr(){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 fr(){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 Se(String(t).trim())}catch{return String(t).trim()}}function hr(){dr()||(delete process.env.ZIBBY_SESSION_PATH,delete process.env.ZIBBY_SESSION_ID)}function gr({sessionPath:r,sessionId:t}){r&&typeof r=="string"&&(process.env.ZIBBY_SESSION_PATH=r),t!=null&&String(t).trim()!==""&&(process.env.ZIBBY_SESSION_ID=String(t).trim())}function mr(r={}){let t=zt.map(n=>process.env[n]).find(Boolean),e=Math.random().toString(36).slice(2,6),o=t||`${Date.now()}_${e}`,s=r.paths?.sessionPrefix;return s?`${s}_${o}`:o}function Sr({cwd:r=process.cwd(),config:t={},initialState:e={},traceFrom:o="resolveWorkflowSession"}={}){let s=e.sessionPath,n=e.sessionTimestamp,i="initialState.sessionPath";if(!s&&process.env.ZIBBY_SESSION_PATH)try{let u=Se(String(process.env.ZIBBY_SESSION_PATH));u&&(s=u,i="ZIBBY_SESSION_PATH")}catch{}let a;if(s)a=String(s).split(/[/\\]/).filter(Boolean).pop(),n==null&&(n=Date.now());else{let u=process.env.ZIBBY_SESSION_ID&&String(process.env.ZIBBY_SESSION_ID).trim();if(u)a=u,i="ZIBBY_SESSION_ID";else{let c=t.sessionId!=null?String(t.sessionId).trim():"";c&&c!=="last"?(a=c,i="config.sessionId"):(a=mr(t),i="generated")}n=n??Date.now();let p=t.paths?.output||at;s=H(r,p,Yt,a)}let l=!kt(s);return l&&me(s,{recursive:!0}),(l||i!=="initialState.sessionPath")&&pr({traceFrom:o,sessionId:a,sessionPath:s,idSource:i,mkdirFresh:l}),gr({sessionPath:s,sessionId:a}),{sessionPath:s,sessionId:a,sessionTimestamp:n}}var ge=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{if(typeof this.inputSchema.merge=="function")return this.inputSchema.merge(this.contextSchema);if(typeof this.inputSchema.and=="function")return this.inputSchema.and(this.contextSchema)}catch{}return this.inputSchema&&!this.contextSchema?this.inputSchema:this.stateSchema}addNode(t,e,o={}){if(!(e instanceof L)&&e&&typeof e=="object"&&typeof e.workflow=="string"){let n=e,i={name:t,_isCustomCode:!0,retries:n.retries,onComplete:n.onComplete,execute:async l=>{let u=l?.state&&typeof l.state.getAll=="function"?l.state.getAll():l,p;return typeof n.input=="function"?p=n.input(u):n.input&&typeof n.input=="object"?p=n.input:p={},ue(n.workflow,{input:p,async:n.async===!0,conversationId:typeof n.conversationId=="function"?n.conversationId(u):n.conversationId,output:n.output,timeoutMs:n.timeoutMs,pollIntervalMs:n.pollIntervalMs,signal:u?._signal,parentAgent:l?.agent})}},a=new L(i);return a.name=t,this.nodes.set(t,a),o.prompt&&this.nodePrompts.set(t,o.prompt),Object.keys(o).length>0&&this.nodeOptions.set(t,o),this}let s=e instanceof L?e:new L(e);return s.name=t,this.nodes.set(t,s),o.prompt?this.nodePrompts.set(t,o.prompt):typeof e?.prompt=="string"&&e.prompt.trim()&&this.nodePrompts.set(t,e.prompt),Object.keys(o).length>0&&this.nodeOptions.set(t,o),this}addConditionalNode(t,e){return this.nodes.set(t,new X({...e,name:t})),this}addEdge(t,e){return this.edges.set(t,e),this}setNodeType(t,e){return this.nodeTypeMap.set(t,e),this}addConditionalEdges(t,e,{labels:o}={}){return this.edges.set(t,{conditional:!0,routes:e,labels:o}),typeof e=="function"&&this.conditionalCodeMap.set(t,e.toString()),this}setEntryPoint(t){return this.entryPoint=t,this}use(t){return typeof t=="function"&&this.middleware.push(t),this}_composeMiddleware(t,e,o,s,n){let i=o;for(let a=t.length-1;a>=0;a--){let l=t[a],u=i;i=()=>l(e,u,s,n)}return i()}serialize(){let t=[],e={};for(let[p,c]of this.nodes){let g=this.nodeTypeMap.get(p)||(c instanceof X?"decision":p);t.push({id:p,type:g,data:{nodeType:g,label:p}});let f={};c._isCustomCode&&typeof c.execute=="function"&&(f.customCode=c.execute.toString());let I=typeof c?.config?.description=="string"&&c.config.description.trim()?c.config.description:typeof c?.description=="string"&&c.description.trim()?c.description:null;I&&(f.description=I);let $=this.nodePrompts.get(p);if($)f.prompt=$;else if(typeof c.prompt=="function")try{let d=c.prompt({});typeof d=="string"&&d.trim()&&(f.prompt=d,f.promptIsCode=!0)}catch{}if(typeof c.customExecute=="function"&&(f.executeCode=c.customExecute.toString()),c.outputSchema)if(typeof c.outputSchema._def<"u"){let d=null;if(typeof ut?.toJSONSchema=="function")try{d=ut.toJSONSchema(c.outputSchema)}catch{}if(!d)try{d=he(c.outputSchema,{target:"openApi3"})}catch{}f.outputSchema=d?{jsonSchema:d,variables:this._flattenJsonSchemaToVariables(d)}:{schema:c.outputSchema}}else f.outputSchema={schema:c.outputSchema};let T=(this.resolvedToolsMap||{})[p];T?.toolIds&&(f.tools=T.toolIds);let E=Array.isArray(c?.config?.skills)?c.config.skills:Array.isArray(c?.skills)?c.skills:null;E&&E.length>0&&(f.skills=[...E]),Object.keys(f).length>0&&(e[p]=f)}let o=[];for(let[p,c]of this.edges)if(typeof c=="string")o.push({source:p,target:c});else if(c.conditional){let g=this.conditionalCodeMap.get(p)||c.routes.toString(),f=this._inferConditionalTargets(c.routes,c.labels),I=c.labels||{};for(let $ of f){let T={source:p,target:$,data:{conditionalCode:g}};I[$]&&(T.label=I[$]),o.push(T)}}let s=p=>{if(!p)return null;if(typeof ut?.toJSONSchema=="function")try{return ut.toJSONSchema(p)}catch{}try{return he(p,{target:"openApi3"})}catch{return null}};this.entryPoint&&this.nodes.has(this.entryPoint)&&(t.unshift({id:"START",type:"start",data:{nodeType:"start",label:"Start"}}),o.unshift({source:"START",target:this.entryPoint}));let n=0;for(let p of o)if(p.target==="END"){n+=1;let c=`END__${n}`;p.target=c,t.push({id:c,type:"end",data:{nodeType:"end",label:"End"}})}for(let p of this.nodes.keys())if(!this.edges.has(p)){n+=1;let c=`END__${n}`;t.push({id:c,type:"end",data:{nodeType:"end",label:"End"}}),o.push({source:p,target:c})}let i=this._runtimeSchema(),a=s(i||this.stateSchema),l=s(this.inputSchema),u=s(this.contextSchema);return{nodes:t,edges:o,nodeConfigs:e,stateSchema:a,inputSchema:l,contextSchema:u}}_inferConditionalTargets(t,e){let o=t.toString(),s=new Set,n=/(['"])((?:\\.|(?!\1).)*?)\1|`((?:\\.|[^`$]|\$(?!\{))*?)`/g,i;for(;(i=n.exec(o))!==null;){let u=i[2]!==void 0?i[2]:i[3];u!==void 0&&u!==""&&s.add(u)}let a=new Set(["END","START","__end__","__start__"]);for(let u of this.nodes.keys())a.add(u);if(e&&typeof e=="object")for(let u of Object.keys(e))a.add(u);let l=new Set;for(let u of s)a.has(u)&&l.add(u);if(l.size===0){let u=/return\s+['"]([^'"]+)['"]/g,p;for(;(p=u.exec(o))!==null;)l.add(p[1])}return[...l]}_flattenJsonSchemaToVariables(t,e=""){let o=t;if(t.$ref&&t.definitions){let s=t.$ref.replace("#/definitions/","");o=t.definitions[s]||t}return this._flattenSchema(o,e)}_flattenSchema(t,e=""){if(!t||typeof t!="object")return[];let o=[],s=t.properties||{},n=t.required||[];for(let[i,a]of Object.entries(s)){let l=e?`${e}.${i}`:i;o.push({path:l,type:a.type||"unknown",label:a.description||this._formatLabel(i),optional:!n.includes(i)}),a.type==="object"&&a.properties&&o.push(...this._flattenSchema(a,l)),a.type==="array"&&a.items?.type==="object"&&a.items.properties&&o.push(...this._flattenSchema(a.items,`${l}[]`))}return o}_formatLabel(t){return t.replace(/([A-Z])/g," $1").replace(/^./,e=>e.toUpperCase()).trim()}_summarizeNodeOutput(t,e){if(!e||typeof e!="object")return[];let o=[];e.success!==void 0&&o.push(`Result: ${e.success?"passed":"failed"}`);for(let[s,n]of Object.entries(e))if(!(s==="success"||s==="raw"||s==="nextNode")){if(typeof n=="string"&&n.length<=80)o.push(`${s}: ${n}`);else if(Array.isArray(n)){let i=n.length,a=n.filter(u=>u?.passed===!0).length,l=n.some(u=>u?.passed!==void 0);o.push(l?`${s}: ${a}/${i} passed${i-a?`, ${i-a} failed`:""}`:`${s}: ${i} 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 s=new AbortController;o.signal&&(o.signal.aborted?s.abort():o.signal.addEventListener("abort",()=>s.abort(),{once:!0}));let n=o.strategyAbortTimeoutMs??e.config?.strategyAbortTimeoutMs??5e3,i=e.cwd||process.cwd();lr({path:H(i,".env")});let a=e.config||{};if(!a||Object.keys(a).length===0)try{let w=H(i,".zibby.config.js");kt(w)&&(a=(await import(w)).default||{})}catch{}process.env.EXECUTION_ID&&!a.agent?.strictMode&&(a.agent={...a.agent,strictMode:!0});let l=e.agentType;if(!l){let w=a?.agent;w?.provider?l=w.provider:w?.gemini?l="gemini":w?.claude?l="claude":w?.cursor?l="cursor":w?.codex?l="codex":l=process.env.AGENT_TYPE||"cursor"}let u=e.contextConfig||t?.config?.contextConfig||t?.config?.context||a?.context||{},p=this._runtimeSchema();if(p){let w=p.safeParse(e);if(!w.success){let P=w.error.issues.map(N=>`${N.path.join(".")}: ${N.message}`);throw console.error("\u274C Initial state validation failed:"),P.forEach(N=>console.error(` - ${N}`)),new Error(`State validation failed: ${P.join(", ")}`)}x.step("State validated against schema")}let c=fr(),g=e.sessionPath||c;g||hr();let{sessionPath:f,sessionTimestamp:I,sessionId:$}=Sr({cwd:i,config:a,traceFrom:"WorkflowGraph.run",initialState:{sessionPath:g,sessionTimestamp:e.sessionTimestamp}});x.step(`Session ${$}`);let T=await lt.loadContext(e.specPath||"",i,u);Object.keys(T).length>0&&x.step(`Context loaded: ${Object.keys(T).join(", ")}`);let E=e.outputPath;!E&&e.specPath&&(t?.calculateOutputPath?E=t.calculateOutputPath(e.specPath):console.warn(`\u26A0\uFE0F outputPath not resolved (specPath=${e.specPath})`));let d=new st({...e,config:a,agentType:l,outputPath:E,sessionPath:f,sessionTimestamp:I,context:T,resolvedTools:this.resolvedToolsMap||{},_signal:s.signal}),_=new Map;try{await import("@zibby/skills")}catch{}let{getSkill:h}=await Promise.resolve().then(()=>(yt(),qt)),m=a.skills&&typeof a.skills=="object"?a.skills:{},B=Object.values(m).filter(w=>w&&typeof w=="object"&&typeof w.id=="string"),v=w=>{for(let P of B)if(P.id===w)return P;return h(w)},M=new Set;for(let[,w]of this.nodes)for(let P of w.config?.skills||[])M.add(P);for(let w of M){let P=v(w);if(typeof P?.middleware=="function")try{let N=await P.middleware();typeof N=="function"&&_.set(w,N)}catch{}}let S=this.entryPoint,tt=[],xt=a?.recursionLimit??100,ye=0;try{for(;S&&S!=="END";){if(++ye>xt)throw new Error(`Workflow exceeded recursion limit (${xt}) \u2014 likely a cyclic conditional route. Set config.recursionLimit if you need a higher cap.`);let P=H(f,Zt);if(kt(P)){try{cr(P)}catch{}s.abort()}if(s.signal.aborted)return console.warn(`
|
|
37
|
+
\u{1F6D1} External stop requested \u2014 ending workflow.`),x.step("Workflow stopped externally"),{success:!0,state:d.getAll(),executionLog:tt,stoppedExternally:!0};let N=this.nodes.get(S);if(!N)throw new Error(`Node '${S}' not found in graph`);let Ot=JSON.stringify({sessionPath:f,sessionTimestamp:I,currentNode:S,createdAt:new Date().toISOString(),config:d.get("config")}),we=H(f,Z);fe(we,Ot,"utf-8");let Pt=d.get("config")?.paths?.output||at,_e=H(i,Pt,Z);me(H(i,Pt),{recursive:!0});try{fe(_e,Ot,"utf-8")}catch{}let Nt=e.onPipelineProgress;if(typeof Nt=="function")try{Nt({cwd:i,sessionPath:f,sessionId:$,outputBase:d.get("config")?.paths?.output||at,currentNode:S})}catch{}let Ie=(this.resolvedToolsMap||{})[S]||null;d.set("_currentNodeTools",Ie);let Ee=d.get("nodeConfigs")||{};d.set("_currentNodeConfig",Ee[S]||{}),x.nodeStart(S);let Rt=Date.now(),et=this.nodePrompts.get(S);if(!this._invokeAgent){let A=await Promise.resolve().then(()=>(It(),_t));this._invokeAgent=A.invokeAgent}let be=this._invokeAgent,pt={},$e=N.config?.skills||[];for(let A of $e){let R=v(A);if(typeof R?.invokeAgentOptions=="function")try{let b=R.invokeAgentOptions(d.getAll(),{agentType:d.get("agentType"),nodeName:S});b&&typeof b=="object"&&(pt={...pt,...b})}catch(b){console.warn(`[graph] skill '${A}' invokeAgentOptions threw: ${b.message}`)}}let Bt=async(A,R,b={})=>{let C=be(A,R,{...pt,...b,signal:s.signal});return C.catch(()=>{}),s.signal.aborted?C:Promise.race([C,new Promise((J,Y)=>{let D=()=>{setTimeout(()=>{let K=new Error(`Strategy ignored AbortSignal \u2014 engine deadman fired after ${n}ms`);K.name="AbortError",Y(K)},n)};s.signal.addEventListener("abort",D,{once:!0})})])},Te=async(A={},R={})=>{let b=R.prompt||"";if(et){let C=this._compiledPrompts.get(S);C||(C=ur.compile(et,{noEscape:!0}),this._compiledPrompts.set(S,C));try{b=C(A)}catch(J){throw console.error(`\u274C Template rendering failed for node '${S}':`,J.message),new Error(`Template rendering failed: ${J.message}`,{cause:J})}}else if(!b)throw new Error(`No prompt template configured for node '${S}' and no prompt provided in options`);return Bt(b,{state:d.getAll(),images:R.images||[]},{model:R.model||d.get("model"),workspace:d.get("workspace"),schema:R.schema,...R,signal:s.signal})},Ct=d.getAll(),ve=["state","invokeAgent","_coreInvokeAgent","agent","nodeId","promptTemplate","getPromptTemplate"];for(let A of ve)Object.prototype.hasOwnProperty.call(Ct,A)&&console.warn(`[workflow] node "${S}": state key "${A}" is shadowed by the engine context prop; read it via context.state.get('${A}')`);let Mt={...Ct,state:d,invokeAgent:Te,_coreInvokeAgent:Bt,agent:t,nodeId:S,promptTemplate:et,getPromptTemplate:()=>et};try{let A=(N.config?.skills||[]).map(D=>_.get(D)).filter(Boolean),R=[...this.middleware,...A],b;R.length>0?b=await this._composeMiddleware(R,S,async()=>N.execute(Mt,d),d.getAll(),d):b=await N.execute(Mt,d);let C=Date.now()-Rt;if(tt.push({node:S,success:b.success,duration:C,timestamp:new Date().toISOString()}),!b.success){if(s.signal.aborted)return x.step("Workflow stopped externally"),{success:!0,state:d.getAll(),executionLog:tt,stoppedExternally:!0};d.append("errors",{node:S,error:b.error});let D=N.config?.retries||0,K=`${S}_retries`,rt=d.getAll()[K]||0;if(rt<D){x.stepInfo(`Retrying (attempt ${rt+1}/${D})`),d.update({[K]:rt+1,[`${S}_raw`]:b.raw});continue}throw x.nodeFailed(S,b.error,{duration:C}),new Error(`Node '${S}' failed after ${rt} attempts: ${b.error}`)}d.update({[S]:b.output});let J=this._summarizeNodeOutput(S,b.output);x.nodeComplete(S,{duration:C,details:J});let Y=this.edges.get(S);if(!Y)S="END";else if(Y.conditional){let D=Y.routes(d.getAll());x.route(S,D),S=D}else S=Y}catch(A){throw x.isInsideNode&&x.nodeFailed(S,A.message,{duration:Date.now()-Rt}),d.set("failed",!0),d.set("failedAt",S),A}}x.graphComplete();let w={success:!0,state:d.getAll(),executionLog:tt};return t&&typeof t.onComplete=="function"&&await t.onComplete(w),w}finally{if(t&&typeof t.cleanup=="function")try{await t.cleanup()}catch(w){console.warn(`[workflow] agent.cleanup() failed: ${w.message}`)}}}};export{ge as WorkflowGraph,hr as clearInheritedSessionEnvForFreshRun,mr as generateWorkflowSessionId,fr as readPinnedSessionPathFromEnv,Sr as resolveWorkflowSession,dr as shouldTrustInheritedSessionEnv,gr as syncProcessEnvToSession};
|
package/dist/index.js
CHANGED
|
@@ -1,40 +1,40 @@
|
|
|
1
|
-
var
|
|
1
|
+
var ro=Object.defineProperty;var $e=(t=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(t,{get:(e,o)=>(typeof require<"u"?require:e)[o]}):t)(function(t){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+t+'" is not supported')});var ge=(t,e)=>()=>(t&&(e=t(t=0)),e);var ot=(t,e)=>{for(var o in e)ro(t,o,{get:e[o],enumerable:!0})};function ao(t){ne.impl={...rt,...t}}var nt,rt,ne,_,F=ge(()=>{nt=()=>{},rt={debug:nt,info:nt,warn:(...t)=>console.warn("[workflow]",...t),error:(...t)=>console.error("[workflow]",...t)},ne={impl:rt};_={debug:(...t)=>ne.impl.debug?.(...t),info:(...t)=>ne.impl.info?.(...t),warn:(...t)=>ne.impl.warn?.(...t),error:(...t)=>ne.impl.error?.(...t)}});var Ne,Oe=ge(()=>{Ne=class{constructor(e,o,n=0){this.name=e,this.description=o,this.priority=n}async invoke(e,o={}){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 yt={};ot(yt,{clearSkills:()=>St,getAllSkills:()=>gt,getSkill:()=>q,hasSkill:()=>ht,listSkillIds:()=>mt,registerSkill:()=>ft});function ft(t){if(!t||typeof t.id!="string")throw new Error("Skill definition must include a string id");V.set(t.id,Object.freeze({...t}))}function q(t){return V.get(t)||null}function ht(t){return V.has(t)}function gt(){return new Map(V)}function mt(){return Array.from(V.keys())}function St(){V.clear()}var Pe,V,ie=ge(()=>{Pe=Symbol.for("@zibby/agent-workflow.skills");globalThis[Pe]||(globalThis[Pe]=new Map);V=globalThis[Pe]});var ae={};ot(ae,{getAgentStrategy:()=>Re,invokeAgent:()=>It,listStrategies:()=>_t,registerStrategy:()=>wt});function wt(t){if(!t||typeof t.getName!="function"||typeof t.invoke!="function")throw new Error("strategy must implement getName() and invoke() (AgentStrategy shape)");let e=G.findIndex(o=>o.getName()===t.getName());e>=0?G[e]=t:G.push(t)}function _t(){return G.map(t=>t.getName())}function Re(t={}){let{state:e={},preferredAgent:o=null}=t,n=o||e.agentType||process.env.AGENT_TYPE;if(!n){let r=G.map(s=>s.getName()).join(", ")||"none registered";throw new Error(`No agent specified. Set agentType in state or AGENT_TYPE env var. Available: ${r}`)}_.debug(`[workflow] agent selection: requested=${n}`);let i=G.find(r=>r.getName()===n);if(!i){let r=G.map(s=>s.getName()).join(", ")||"none registered";throw new Error(`Unknown agent '${n}'. Available: ${r}`)}if(!i.canHandle(t))throw new Error(`Agent '${n}' is not available in this environment. Check credentials/environment.`);return _.debug(`[workflow] using agent: ${i.getName()}`),i}async function It(t,e={},o={}){let n=Re(e),i=e.state?.config||o.config||{},r=i.models||{},s=o.nodeName&&r[o.nodeName]||null,a=r.default||null,c=i.agent?.[n.name]?.model||null,u=s||a||c||o.model||null,d={...o,model:u,workspace:e.state?.workspace||o.workspace,schema:o.schema||e.schema,images:o.images||e.images||[],skills:o.skills||e.skills||[],config:i},l=t,p=d.skills||[];if(p.length>0&&!o.skipPromptFragments){let m=p.map(y=>{let E=q(y)?.promptFragment;return typeof E=="function"?E():E}).filter(Boolean);m.length>0&&(l+=`
|
|
2
2
|
|
|
3
3
|
${m.join(`
|
|
4
4
|
|
|
5
|
-
`)}`)}let
|
|
5
|
+
`)}`)}let h=e.state?._currentNodeConfig?.extraPromptInstructions?.trim();return h&&(l+=`
|
|
6
6
|
|
|
7
7
|
\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501
|
|
8
8
|
PRIORITY OVERRIDE \u2014 THE FOLLOWING INSTRUCTIONS TAKE PRECEDENCE OVER ALL PREVIOUS CONTENT
|
|
9
9
|
\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501
|
|
10
10
|
|
|
11
|
-
${
|
|
12
|
-
`),
|
|
11
|
+
${h}
|
|
12
|
+
`),_.debug(`[workflow] prompt length: ${l.length} chars`),n.invoke(l,d)}var Ce,G,X=ge(()=>{Oe();F();ie();Ce=Symbol.for("@zibby/agent-workflow.strategies");globalThis[Ce]||(globalThis[Ce]=[]);G=globalThis[Ce]});var so=new Set(["__proto__","constructor","prototype"]);function Te(t){if(so.has(t))throw new Error(`Invalid state key: "${t}"`)}var te=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,o){Te(e),this._history.push({...this._state}),this._state[e]=o}update(e){let o=Object.getOwnPropertyNames(e);for(let n of o)Te(n);this._history.push({...this._state});for(let n of o)this._state[n]=e[n]}append(e,o){Te(e),this._history.push({...this._state}),Array.isArray(this._state[e])||(this._state[e]=[]),this._state[e].push(o)}getAll(){return{...this._state}}rollback(){this._history.length>0&&(this._state=this._history.pop())}};import H from"handlebars";var oe=class{constructor(e){this.schema=e}parse(e){let o=e.match(/```json\s*([\s\S]*?)\s*```/);if(o)return this.validate(JSON.parse(o[1]));let n=[e.match(/\{[\s\S]*?\}/),e.match(/\{[\s\S]*\}/)].filter(Boolean).map(i=>i[0]);for(let i of n)try{return this.validate(JSON.parse(i))}catch(r){if(!(r instanceof SyntaxError))throw r}return this.validate({result:e.trim()})}validate(e){let o=[];for(let[n,i]of Object.entries(this.schema)){if(i.required&&!(n in e)&&o.push(`Missing required field: ${n}`),n in e&&i.type){let r=typeof e[n];r!==i.type&&o.push(`Field '${n}' expected ${i.type}, got ${r}`)}if(i.validate&&n in e){let r=i.validate(e[n]);r&&o.push(`Field '${n}': ${r}`)}}if(o.length>0)throw new Error(`Output validation failed:
|
|
13
13
|
${o.join(`
|
|
14
|
-
`)}`);return e}},
|
|
14
|
+
`)}`);return e}},io={string:(t=!0)=>({type:"string",required:t}),number:(t=!0)=>({type:"number",required:t}),boolean:(t=!0)=>({type:"boolean",required:t}),array:(t=!0)=>({type:"object",required:t,validate:e=>Array.isArray(e)?null:"must be an array"}),enum:(t,e=!0)=>({type:"string",required:e,validate:o=>t.includes(o)?null:`must be one of: ${t.join(", ")}`})};F();import{writeFileSync as Be,readFileSync as Et,existsSync as $t,mkdirSync as ho}from"node:fs";import{join as Me,dirname as go}from"node:path";import A from"chalk";var pt="__WORKFLOW_GRAPH_LOG__",re=A.gray("\u2502"),co=A.gray("\u250C"),st=A.gray("\u2514"),be=A.green("\u25C6"),it=A.hex("#c084fc")("\u25C6"),at=A.hex("#2dd4bf")("\u25C6"),ve=A.red("\u25C6"),ct=`${re} `,lt=2;function ut(t){return t<1e3?`${t}ms`:`${(t/1e3).toFixed(1)}s`}function dt(t,e){return(o,n,i)=>{if(typeof o!="string")return t(o,n,i);let r=process.stdout.columns||120,s="";for(let a=0;a<o.length;a++){let c=o[a];e.lineStart&&(s+=ct,e.col=lt,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>=r&&(s+=`
|
|
16
|
-
${
|
|
16
|
+
${ct}`,e.col=lt))}return t(s,n,i)}}var me=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},o={lineStart:!0,col:0,inEsc:!1};this._outState=e,this._errState=o,process.stdout.write=dt(this._origStdoutWrite,e),process.stderr.write=dt(this._origStderrWrite,o)}_stopIntercepting(){this._origStdoutWrite&&(this._outState&&!this._outState.lineStart&&this._origStdoutWrite(`
|
|
17
17
|
`),process.stdout.write=this._origStdoutWrite),this._origStderrWrite&&(this._errState&&!this._errState.lineStart&&this._origStderrWrite(`
|
|
18
18
|
`),process.stderr.write=this._origStderrWrite),this._origStdoutWrite=null,this._origStderrWrite=null}_rawWrite(e){(this._origStdoutWrite||process.stdout.write.bind(process.stdout))(`${e}
|
|
19
|
-
`)}_emitGraphLogMarker(e){if(!this._emitWorkflowGraphMarkers)return;let o=`${
|
|
19
|
+
`)}_emitGraphLogMarker(e){if(!this._emitWorkflowGraphMarkers)return;let o=`${pt}${JSON.stringify(e)}
|
|
20
20
|
`;this._origStdoutWrite?this._origStdoutWrite(o):process.stdout.write(o)}_writeDot(e,o){this._origStdoutWrite?(this._outState&&!this._outState.lineStart&&(this._origStdoutWrite(`
|
|
21
21
|
`),this._outState.lineStart=!0,this._outState.col=0),this._origStdoutWrite(`${e} ${o}
|
|
22
22
|
`)):process.stdout.write.bind(process.stdout)(`${e} ${o}
|
|
23
23
|
`)}step(e){this._origStdoutWrite?this._writeDot(be,e):process.stdout.write.bind(process.stdout)(`${re} ${be} ${e}
|
|
24
|
-
`)}stepInfo(e){this.step(e)}stepTool(e){this._origStdoutWrite?this._writeDot(
|
|
25
|
-
`)}stepMemory(e){let o=
|
|
26
|
-
`)}stepFail(e){this._origStdoutWrite?this._writeDot(ve,
|
|
27
|
-
`)}nodeStart(e){this._currentNode=e,this._emitGraphLogMarker({phase:"node_begin",node:e}),this._rawWrite(`${
|
|
24
|
+
`)}stepInfo(e){this.step(e)}stepTool(e){this._origStdoutWrite?this._writeDot(it,e):process.stdout.write.bind(process.stdout)(`${re} ${it} ${e}
|
|
25
|
+
`)}stepMemory(e){let o=A.hex("#2dd4bf")(e);this._origStdoutWrite?this._writeDot(at,o):process.stdout.write.bind(process.stdout)(`${re} ${at} ${o}
|
|
26
|
+
`)}stepFail(e){this._origStdoutWrite?this._writeDot(ve,A.red(e)):process.stdout.write.bind(process.stdout)(`${re} ${ve} ${A.red(e)}
|
|
27
|
+
`)}nodeStart(e){this._currentNode=e,this._emitGraphLogMarker({phase:"node_begin",node:e}),this._rawWrite(`${co} ${e}`),this._startIntercepting()}nodeComplete(e,o={}){this._stopIntercepting();let{duration:n,details:i}=o;if(i)for(let s of i)this._rawWrite(`${be} ${s}`);let r=n?A.dim(` ${ut(n)}`):"";this._rawWrite(`${st} ${A.green("done")}${r}`),this._emitGraphLogMarker({phase:"node_end",node:e}),this._rawWrite("")}nodeFailed(e,o,n={}){this._stopIntercepting();let{duration:i}=n,r=i?A.dim(` ${ut(i)}`):"";this._rawWrite(`${ve} ${A.red(o)}`),this._rawWrite(`${st} ${A.red("failed")}${r}`),this._emitGraphLogMarker({phase:"node_end",node:e}),this._rawWrite("")}route(e,o){this._rawWrite(A.dim(` ${e} \u2192 ${o}`)),this._rawWrite("")}graphComplete(){}},k=new me;var se=".zibby/output",xe="sessions",W=".session-info.json",ke=".zibby-stop",lo="result.json",uo="raw_stream_output.txt",po="events.json",fo={BROWSER:"browser",JIRA:"jira",GITHUB:"github",GITLAB:"gitlab",FIGMA:"figma",OPEN_DESIGN:"open-design",GIT:"git",SLACK:"slack",LARK:"lark",CHAT_NOTIFY:"chat_notify",SENTRY:"sentry",MEMORY:"memory",CHAT_MEMORY:"chat-memory",KV_MEMORY:"kv-memory",RUNNER:"runner",SKILL_INSTALLER:"skill-installer",CORE_TOOLS:"core-tools",WORKFLOW_BUILDER:"workflow-builder",SESSION:"session",OPENAI_BILLING:"openai_billing",ANTHROPIC_BILLING:"anthropic_billing",CURSOR_ADMIN:"cursor_admin",NOTION:"notion",LINEAR:"linear",PLANE:"plane"},Ae=["CI_JOB_ID","GITHUB_RUN_ID","CIRCLE_WORKFLOW_ID","BUILD_ID"];H.helpers.inc||H.registerHelper("inc",t=>Number(t)+1);H.helpers.json||H.registerHelper("json",t=>JSON.stringify(t,null,2));H.helpers.eq||H.registerHelper("eq",(t,e)=>t===e);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 oe(e.outputSchema):null,this.retries=e.retries||0,this.onComplete=e.onComplete,this.customExecute=e.execute}async execute(e,o){let n=()=>o&&typeof o.getAll=="function"?o.getAll():e,i=l=>o&&typeof o.get=="function"?o.get(l):e?.[l];if(typeof this.customExecute=="function"){_.debug(`[workflow] node '${this.name}': custom execute (skipping LLM)`);try{let l=await this.customExecute(e);return typeof l=="object"&&l!==null&&l.success===!1?{success:!1,error:l.error||"Node execution failed",raw:l.raw||null}:this.isZodSchema?(_.debug(`[workflow] node '${this.name}': validating output schema`),{success:!0,output:this.outputSchema.parse(l),raw:null}):{success:!0,output:l,raw:null}}catch(l){return _.error(`[workflow] node '${this.name}' failed: ${l.message}`),l.name==="ZodError"&&_.error(`Schema errors: ${JSON.stringify(l.issues||l.errors,null,2)}`),{success:!1,error:l.message,raw:null}}}let r;typeof this.prompt=="function"?r=this.prompt(n()):typeof this.prompt=="string"&&this.prompt.includes("{{")?(this._compiledPrompt||(this._compiledPrompt=H.compile(this.prompt,{noEscape:!0})),r=this._compiledPrompt(n())):r=this.prompt;let s=i("_skillHints");s&&(r=`${s}
|
|
28
28
|
|
|
29
|
-
${r}`);let a=n(),c=a.cwd||process.cwd(),l=a.sessionPath;try{if(l){let u=Me(l,W);if(Et(u)){let f=JSON.parse(It(u,"utf-8"));f.currentNode=this.name,Be(u,JSON.stringify(f,null,2),"utf-8")}let p=Me(l,"..",W);if(Et(p))try{let f=JSON.parse(It(p,"utf-8"));f.currentNode=this.name,Be(p,JSON.stringify(f,null,2),"utf-8")}catch{}}}catch(u){I.debug(`[workflow] could not update session info: ${u.message}`)}let d=null;for(let u=0;u<=this.retries;u++)try{I.debug(`[workflow] node '${this.name}' attempt ${u}`);let p=n().config||{},f=p.agents||{},m=this.config.agent??f[this.name]??null,w={state:n()};m&&(w.preferredAgent=m);let E={workspace:c,schema:this.isZodSchema?this.outputSchema:null,skills:this.config.skills||[],sessionPath:l,config:p,nodeName:this.name,timeout:this.config?.timeout||3e5},S=e?._coreInvokeAgent;S||(S=(await Promise.resolve().then(()=>(X(),ae))).invokeAgent);let h=await S(r,w,E),$,g;if(typeof h=="string"?($=h,g=null):h.structured?($=h.raw||JSON.stringify(h.structured,null,2),g=h.structured):($=h.raw||JSON.stringify(h,null,2),g=h.extracted||null),l)try{let y=Me(l,this.name,"raw_stream_output.txt");uo(po(y),{recursive:!0}),Be(y,typeof $=="string"?$:JSON.stringify($),"utf-8")}catch(y){I.debug(`[workflow] could not save raw output: ${y.message}`)}if(this.isZodSchema&&g){I.info(`[workflow] node '${this.name}': output validated: ${JSON.stringify(g,null,2)}`);let y=g;if(typeof this.onComplete=="function")try{y=await this.onComplete(n(),g)}catch(R){I.warn(`[workflow] onComplete hook failed: ${R.message}`)}return{success:!0,output:y,raw:$}}if(typeof this.onComplete=="function")try{return{success:!0,output:await this.onComplete(n(),{raw:$}),raw:$}}catch(y){throw new Error(`onComplete failed: ${y.message}`,{cause:y})}if(this.parser){let y=this.parser.parse($);return I.info(`[workflow] node '${this.name}': parsed output: ${JSON.stringify(y,null,2)}`),x.step("Output parsed"),{success:!0,output:y,raw:$}}return{success:!0,output:$,raw:$}}catch(p){d=p,u<this.retries&&I.info(`[workflow] node '${this.name}' failed, retrying (${u+1}/${this.retries})\u2026`)}return{success:!1,error:d.message,raw:null}}},Q=class extends j{constructor(e){super({...e,_isCustomCode:!0}),this.condition=e.condition}async execute(e,o){let n=o&&typeof o.getAll=="function"?o.getAll():e;return{success:!0,output:{nextNode:this.condition(n)},raw:null}}};F();F();import{mkdirSync as go,existsSync as Y,statSync as At,readdirSync as Nt,rmSync as mo}from"node:fs";import{spawn as kt}from"node:child_process";import{join as U}from"node:path";import{pathToFileURL as So}from"node:url";import{AsyncLocalStorage as fo}from"node:async_hooks";var De=new fo;function ce(){let t=De.getStore();return t||Object.freeze({executionId:process.env.EXECUTION_ID||null,parentExecutionId:process.env.PARENT_EXECUTION_ID||null,depth:0,conversationId:process.env.ZIBBY_CONVERSATION_ID||null,dispatchMode:process.env.DISPATCH_MODE||null})}function $t(t,e){let o=De.getStore()||ce(),n=Object.freeze({executionId:t.executionId,parentExecutionId:t.parentExecutionId??o.executionId??null,depth:(o.depth||0)+(t.executionId!==o.executionId?1:0),conversationId:t.conversationId!==void 0?t.conversationId:o.conversationId??null,dispatchMode:t.dispatchMode??null});return De.run(n,e)}var Le=new Map,je=new Map,Tt=new Map;function bt(t,e,o={}){if(!t||typeof t!="string")throw new Error("subgraph-registry.register: name required");if(typeof e!="function")throw new Error("subgraph-registry.register: factory must be a function");Le.set(t,e),je.set(t,"ready"),Tt.set(t,{...o,cachedAt:Date.now()})}function vt(t,e){je.set(t,"failed"),Tt.set(t,{error:e?.message||String(e),failedAt:Date.now()}),Le.delete(t)}function xt(t){return je.get(t)==="ready"?Le.get(t):null}var Se=process.env.ZIBBY_SUBGRAPH_CACHE_DIR||"/tmp/zibby/subgraphs";function yo(){return`node${(process.versions?.node||"").split(".")[0]||"unknown"}-${process.platform}-${process.arch}`}var A=class extends Error{constructor(e,o){super(`in-process sub-graph fallback: ${e}${o?` (${o})`:""}`),this.fallback=!0,this.reason=e,this.detail=o||null,this.name="SubgraphFallback"}};function wo(){let t=(process.env.SUBGRAPH_INTERNAL_URL||"").replace(/\/$/,""),e=(process.env.PROGRESS_API_URL||"").replace(/\/executions\/?$/,""),o=t||e,n=process.env.PROJECT_ID,i=process.env.PROJECT_API_TOKEN;if(!o||!n||!i)throw new A("env","SUBGRAPH_INTERNAL_URL/PROGRESS_API_URL/PROJECT_ID/PROJECT_API_TOKEN missing");return{apiBase:o,projectId:n,authToken:i}}async function _o({apiBase:t,authToken:e,body:o}){let n;try{n=await fetch(`${t}/internal/subgraph/begin`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${e}`},body:JSON.stringify(o)})}catch(r){throw new A("network",`begin fetch failed: ${r.message}`)}let i=null;try{i=await n.json()}catch{}if(!n.ok){if(n.status===404){let r=new Error(`Sub-graph child '${o.childWorkflowType}' not found in project`);throw r.code="SUBGRAPH_NOT_FOUND",r.status=404,r}if(n.status===429){let r=i?.quotaInfo||{},s=new Error(`Sub-graph blocked by quota (${r.used??"?"}/${r.limit??"?"} on ${r.planId||"plan"})`);throw s.code="SUBGRAPH_QUOTA_EXCEEDED",s.status=429,s.quotaInfo=r,s}if(n.status===400&&i?.validationErrors){let r=new Error(`Sub-graph rejected input: ${i?.error||i?.message||"validation failed"}`);throw r.code="SUBGRAPH_INVALID_INPUT",r.status=400,r.validationErrors=i.validationErrors,r.missing=i.missing,r}throw new A("begin-status",`begin returned ${n.status}`)}return i?.data||i}async function J({apiBase:t,authToken:e,payload:o}){try{let n=await fetch(`${t}/internal/subgraph/finalize`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${e}`},body:JSON.stringify(o)});n.ok||I.warn(`[in-process subgraph] finalize returned ${n.status} for ${o.childExecutionId}`)}catch(n){I.warn(`[in-process subgraph] finalize failed: ${n.message}`)}}async function Io(t,e){let o=U(e,".ready"),n=U(e,"graph.mjs");if(Y(o)&&Y(n))return;go(e,{recursive:!0});let i=U(e,".lock"),r=!1;try{let{openSync:s,closeSync:a}=await import("node:fs"),c=s(i,"wx");a(c),r=!0}catch(s){if(s.code!=="EEXIST")throw s}if(!r){let s=Date.now()+3e4;for(;Date.now()<s;){if(Y(o)&&Y(n))return;await new Promise(a=>setTimeout(a,100))}throw new A("bundle-extract-timeout","sibling extract did not complete within 30s")}try{await new Promise((c,l)=>{let d=kt("curl",["-fsSL",t],{stdio:["ignore","pipe","inherit"]}),u=kt("tar",["-xzf","-","-C",e],{stdio:["pipe","inherit","inherit"]});d.stdout.pipe(u.stdin);let p,f,m=()=>{if(p!==void 0&&f!==void 0){if(p!==0)return l(new Error(`curl exited ${p}`));if(f!==0)return l(new Error(`tar exited ${f}`));c()}};d.on("close",w=>{p=w,m()}),u.on("close",w=>{f=w,m()}),d.on("error",l),u.on("error",l)});let{writeFileSync:s,unlinkSync:a}=await import("node:fs");s(o,"");try{a(i)}catch{}}catch(s){try{let{unlinkSync:a}=await import("node:fs");a(i)}catch{}throw new A("bundle-extract-failed",s.message)}}async function Eo(t){let e=U(t,"graph.mjs");if(!Y(e))throw new A("entry-missing",`graph.mjs missing under ${t}`);let o;try{o=await import(So(e).href)}catch(i){throw new A("import-failed",`${i?.code||i?.name||"unknown"}: ${i.message}`)}let n=o.default||Object.values(o).find(i=>typeof i=="function"&&i.prototype?.buildGraph);if(!n)throw new A("entry-class-missing","no buildGraph() class export found");return n}async function Ot(t,e={}){if(!t||typeof t!="string")throw new Error("runInProcessSubgraph: workflowName (string) is required");let o=ce(),n;try{n=wo()}catch(g){throw g}I.debug(`[in-process subgraph] begin '${t}' parent=${o.executionId||"<root>"}`);let i=await _o({apiBase:n.apiBase,authToken:n.authToken,body:{parentExecutionId:o.executionId,childWorkflowType:t,input:e.input||{},...e.conversationId?{conversationId:e.conversationId}:{}}}),{childExecutionId:r,runtimeTag:s,bundlePresignedUrl:a,sourcesPresignedUrl:c,workflowVersion:l,workflowUuid:d,bundleReady:u}=i,p=yo();if(s&&s!==p)throw await J({apiBase:n.apiBase,authToken:n.authToken,payload:{childExecutionId:r,status:"canceled",error:{message:`runtimeTag mismatch: parent=${p} child=${s}`,code:"RUNTIME_MISMATCH"}}}),new A("runtime-mismatch",`${p} vs ${s}`);if(!u||!a)throw await J({apiBase:n.apiBase,authToken:n.authToken,payload:{childExecutionId:r,status:"canceled",error:{message:"bundle not ready for in-process; falling back to HTTP",code:"NO_BUNDLE"}}}),new A("no-bundle","workflow bundle not built yet");let f=xt(t);if(!f){let g=U(Se,`${d}@${l||"0"}`);try{await Io(a,g);try{To()}catch{}}catch(y){throw y.fallback&&await J({apiBase:n.apiBase,authToken:n.authToken,payload:{childExecutionId:r,status:"failed",error:{message:y.message,code:y.reason}}}),y}try{f=await Eo(g),bt(t,f,{workflowUuid:d,version:l,runtimeTag:s,cacheDir:g})}catch(y){throw vt(t,y),await J({apiBase:n.apiBase,authToken:n.authToken,payload:{childExecutionId:r,status:"failed",error:{message:y.message,code:y.reason||"IMPORT_FAILED"}}}),y.fallback?y:new A("import-failed",y.message)}}let m=Date.now(),E=await(typeof f=="function"&&f.prototype?.buildGraph?new f:f).buildGraph(),S={...e.input||{}},h,$;try{h=await $t({executionId:r,parentExecutionId:o.executionId,conversationId:e.conversationId!==void 0?e.conversationId:o.conversationId,dispatchMode:"inprocess"},()=>E.run(e.parentAgent,S,{signal:e.signal})),$=h&&typeof h=="object"&&"state"in h?h.state:h}catch(g){throw await J({apiBase:n.apiBase,authToken:n.authToken,payload:{childExecutionId:r,status:"failed",error:{message:g.message,code:g.code||"CHILD_THREW",stack:g.stack},durationMs:Date.now()-m}}),g}if(h&&typeof h=="object"&&h.stoppedExternally){await J({apiBase:n.apiBase,authToken:n.authToken,payload:{childExecutionId:r,status:"canceled",finalState:$,durationMs:Date.now()-m}});let g=new Error(`Sub-graph '${t}' canceled by parent abort`);throw g.code="SUBGRAPH_CANCELED",g.subgraphJobId=r,g}return await J({apiBase:n.apiBase,authToken:n.authToken,payload:{childExecutionId:r,status:"completed",finalState:$,durationMs:Date.now()-m}}),{finalState:$,executionId:r}}function $o(t){let e=0,o=[t];for(;o.length;){let n=o.pop(),i;try{i=At(n)}catch{continue}if(i.isDirectory()){let r;try{r=Nt(n)}catch{continue}for(let s of r)o.push(U(n,s))}else e+=i.size}return e}function To({cap:t=Number(process.env.ZIBBY_SUBGRAPH_CACHE_CAP_BYTES||2*1024*1024*1024)}={}){try{if(!Y(Se))return{evicted:0,freedBytes:0};let e=Nt(Se),o=[],n=0;for(let a of e){let c=U(Se,a),l;try{l=At(c)}catch{continue}let d=l.isDirectory()?$o(c):l.size;n+=d,o.push({name:a,full:c,size:d,mtimeMs:l.mtimeMs})}if(n<=t)return{evicted:0,freedBytes:0,totalBytes:n};o.sort((a,c)=>a.mtimeMs-c.mtimeMs);let i=Math.floor(t*.7),r=0,s=0;for(let a of o){if(n-r<=i)break;if(!Y(U(a.full,".lock")))try{mo(a.full,{recursive:!0,force:!0}),r+=a.size,s+=1}catch(c){I.debug(`[sub-graph cache] evict skip ${a.name}: ${c.message}`)}}return s>0&&I.info(`[sub-graph cache] evicted ${s} entr(y/ies), freed ${(r/1024/1024).toFixed(1)}MB`),{evicted:s,freedBytes:r,totalBytes:n-r}}catch(e){return I.debug(`[sub-graph cache] evict failed: ${e.message}`),{evicted:0,freedBytes:0}}}var bo=2e3,vo=600*1e3,xo=new Set(["completed","failed","canceled","timeout"]);function ko(){let t=process.env.PROGRESS_API_URL;if(!t)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 t.replace(/\/executions\/?$/,"")}function Ao(){let t=process.env.PROJECT_ID;if(!t)throw new Error("Sub-graph dispatch requires PROJECT_ID env var.");return t}function No(){let t=process.env.PROJECT_API_TOKEN;if(!t)throw new Error("Sub-graph dispatch requires PROJECT_API_TOKEN env var.");return t}function Oo(){return process.env.EXECUTION_ID||null}function Pt(t,e){return e==null?t:typeof e=="function"?e(t):typeof e=="string"?e.split(".").reduce((o,n)=>o==null?o:o[n],t):t}async function Fe(t,e={}){if(!t||typeof t!="string")throw new Error("dispatchSubgraph: workflowName (string) is required");let o=ce(),n=Number(process.env.ZIBBY_SUBGRAPH_MAX_DEPTH||10);if((o.depth||0)>=n)throw new Error(`dispatchSubgraph('${t}'): sub-graph depth ${o.depth} reached cap of ${n}. Restructure the graph or raise ZIBBY_SUBGRAPH_MAX_DEPTH.`);if(process.env.ZIBBY_INPROCESS_SUBGRAPH!=="0"&&!e.async)try{I.debug(`[sub-graph] trying in-process for '${t}'`);let{finalState:g}=await Ot(t,{input:e.input,conversationId:e.conversationId,signal:e.signal,parentAgent:e.parentAgent}),y=Pt(g,e.output);return I.info(`[sub-graph] '${t}' completed in-process`),y}catch(g){if(g instanceof A||g?.fallback)I.info(`[sub-graph] in-process fallback for '${t}': ${g.reason||"unknown"} \u2014 using HTTP`);else throw g}let i=ko(),r=Ao(),s=No(),a=Oo(),c=`${i}/projects/${encodeURIComponent(r)}/workflows/${encodeURIComponent(t)}/trigger`,l={input:e.input||{},...a?{parentExecutionId:a}:{},...e.conversationId?{conversationId:e.conversationId}:{}};I.info(`[sub-graph] dispatching '${t}' (${e.async?"async":"sync"}) from parent ${a||"<none>"}`);let d=await fetch(c,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${s}`},body:JSON.stringify(l)});if(!d.ok){let g=null,y="";try{g=await d.json(),y=g?.error||g?.message||JSON.stringify(g)}catch{y=await d.text().catch(()=>"")}if(d.status===429){let v=g?.quotaInfo||{},D=new Error(`Sub-graph '${t}' blocked by execution quota (${v.used??"?"}/${v.limit??"?"} on plan ${v.planId||"unknown"}). Sub-workflow runs count toward the same monthly cap as user-triggered runs.`);throw D.code="SUBGRAPH_QUOTA_EXCEEDED",D.status=429,D.subgraph=t,D.quotaInfo=v,D}if(d.status===400){let v=new Error(`Sub-graph '${t}' rejected input: ${y}`);throw v.code="SUBGRAPH_INVALID_INPUT",v.status=400,v.subgraph=t,v.validationErrors=g?.validationErrors||null,v.missing=g?.missing||null,v}let R=new Error(`Sub-graph '${t}' trigger rejected (${d.status}): ${y}`);throw R.code="SUBGRAPH_TRIGGER_FAILED",R.status=d.status,R.subgraph=t,R}let u=await d.json(),p=u?.data?.jobId||u?.jobId;if(!p)throw new Error(`Sub-graph '${t}' trigger returned no jobId: ${JSON.stringify(u).slice(0,200)}`);if(e.async)return I.info(`[sub-graph] async dispatch of '${t}' \u2192 jobId=${p} (not waiting)`),{jobId:p,status:"accepted",workflow:t};let f=Number.isFinite(e.timeoutMs)?e.timeoutMs:vo,m=Number.isFinite(e.pollIntervalMs)?e.pollIntervalMs:bo,w=`${i}/executions/${encodeURIComponent(p)}`,E=Date.now()+f,S="accepted",h=0;for(;Date.now()<E;){await new Promise(v=>setTimeout(v,m)),h+=1;let g=await fetch(w,{headers:{Authorization:`Bearer ${s}`}});if(!g.ok){if(g.status>=500){I.warn(`[sub-graph] status poll for ${p} returned ${g.status}, will retry`);continue}throw new Error(`Sub-graph status poll failed for ${p}: ${g.status}`)}let y=await g.json(),R=y?.data||y?.execution||y;if(S=R?.status||S,xo.has(S)){if(S!=="completed"){let _=new Error(`Sub-graph '${t}' (${p}) ended in status '${S}'`);throw _.subgraphJobId=p,_.subgraphStatus=S,_}let v=R?.finalState||R?.state||{},D=Pt(v,e.output);return I.info(`[sub-graph] '${t}' (${p}) completed after ${h} polls`),D}}let $=new Error(`Sub-graph '${t}' (${p}) timed out after ${Math.round(f/1e3)}s (last status: ${S})`);throw $.subgraphJobId=p,$.subgraphStatus=S,$}import{existsSync as Ct,readFileSync as Po}from"node:fs";import{join as Ue,dirname as Rt}from"node:path";var le=class{static async loadContext(e,o,n={}){let i={},r=n.filenames||["CONTEXT.md","AGENTS.md"];if(e){let a=Rt(Ue(o,e));for(let c of r){let l=await this.findAndMergeContextFiles(c,a,o);if(l){let d=c.replace(/\.[^.]+$/,"").toLowerCase();i[d]=l}}}let s=n.discovery||{};for(let[a,c]of Object.entries(s))try{let l=Ue(o,c);Ct(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,o,n){let i=[],r=o;for(;r.startsWith(n);){let s=Ue(r,e);if(Ct(s))try{i.unshift(await this.loadFile(s))}catch(c){console.warn(`[workflow] could not load ${e} from ${s}: ${c.message}`)}let a=Rt(r);if(a===r)break;r=a}return i.length===0?null:i.every(s=>typeof s=="string")?i.join(`
|
|
29
|
+
${r}`);let a=n(),c=a.cwd||process.cwd(),u=a.sessionPath;try{if(u){let l=Me(u,W);if($t(l)){let h=JSON.parse(Et(l,"utf-8"));h.currentNode=this.name,Be(l,JSON.stringify(h,null,2),"utf-8")}let p=Me(u,"..",W);if($t(p))try{let h=JSON.parse(Et(p,"utf-8"));h.currentNode=this.name,Be(p,JSON.stringify(h,null,2),"utf-8")}catch{}}}catch(l){_.debug(`[workflow] could not update session info: ${l.message}`)}let d=null;for(let l=0;l<=this.retries;l++)try{_.debug(`[workflow] node '${this.name}' attempt ${l}`);let p=n().config||{},h=p.agents||{},m=this.config.agent??h[this.name]??null,y={state:n()};m&&(y.preferredAgent=m);let E={workspace:c,schema:this.isZodSchema?this.outputSchema:null,skills:this.config.skills||[],sessionPath:u,config:p,nodeName:this.name,timeout:this.config?.timeout||3e5},I=e?._coreInvokeAgent;I||(I=(await Promise.resolve().then(()=>(X(),ae))).invokeAgent);let f=await I(r,y,E),$,g;if(typeof f=="string"?($=f,g=null):f.structured?($=f.raw||JSON.stringify(f.structured,null,2),g=f.structured):($=f.raw||JSON.stringify(f,null,2),g=f.extracted||null),u)try{let S=Me(u,this.name,"raw_stream_output.txt");ho(go(S),{recursive:!0}),Be(S,typeof $=="string"?$:JSON.stringify($),"utf-8")}catch(S){_.debug(`[workflow] could not save raw output: ${S.message}`)}if(this.isZodSchema&&g){_.info(`[workflow] node '${this.name}': output validated: ${JSON.stringify(g,null,2)}`);let S=g;if(typeof this.onComplete=="function")try{S=await this.onComplete(n(),g)}catch(R){_.warn(`[workflow] onComplete hook failed: ${R.message}`)}return{success:!0,output:S,raw:$}}if(typeof this.onComplete=="function")try{return{success:!0,output:await this.onComplete(n(),{raw:$}),raw:$}}catch(S){throw new Error(`onComplete failed: ${S.message}`,{cause:S})}if(this.parser){let S=this.parser.parse($);return _.info(`[workflow] node '${this.name}': parsed output: ${JSON.stringify(S,null,2)}`),k.step("Output parsed"),{success:!0,output:S,raw:$}}return{success:!0,output:$,raw:$}}catch(p){d=p,l<this.retries&&_.info(`[workflow] node '${this.name}' failed, retrying (${l+1}/${this.retries})\u2026`)}return{success:!1,error:d.message,raw:null}}},Q=class extends j{constructor(e){super({...e,_isCustomCode:!0}),this.condition=e.condition}async execute(e,o){let n=o&&typeof o.getAll=="function"?o.getAll():e;return{success:!0,output:{nextNode:this.condition(n)},raw:null}}};F();F();import{mkdirSync as yo,existsSync as Y,statSync as Nt,readdirSync as Ot,rmSync as wo}from"node:fs";import{spawn as At}from"node:child_process";import{join as U}from"node:path";import{pathToFileURL as _o}from"node:url";import{AsyncLocalStorage as mo}from"node:async_hooks";var De=new mo;function ce(){let t=De.getStore();return t||Object.freeze({executionId:process.env.EXECUTION_ID||null,parentExecutionId:process.env.PARENT_EXECUTION_ID||null,depth:0,conversationId:process.env.ZIBBY_CONVERSATION_ID||null,dispatchMode:process.env.DISPATCH_MODE||null})}function Tt(t,e){let o=De.getStore()||ce(),n=Object.freeze({executionId:t.executionId,parentExecutionId:t.parentExecutionId??o.executionId??null,depth:(o.depth||0)+(t.executionId!==o.executionId?1:0),conversationId:t.conversationId!==void 0?t.conversationId:o.conversationId??null,dispatchMode:t.dispatchMode??null});return De.run(n,e)}var Le=new Map,je=new Map,bt=new Map;function vt(t,e,o={}){if(!t||typeof t!="string")throw new Error("subgraph-registry.register: name required");if(typeof e!="function")throw new Error("subgraph-registry.register: factory must be a function");Le.set(t,e),je.set(t,"ready"),bt.set(t,{...o,cachedAt:Date.now()})}function xt(t,e){je.set(t,"failed"),bt.set(t,{error:e?.message||String(e),failedAt:Date.now()}),Le.delete(t)}function kt(t){return je.get(t)==="ready"?Le.get(t):null}var Se=process.env.ZIBBY_SUBGRAPH_CACHE_DIR||"/tmp/zibby/subgraphs";function Io(){return`node${(process.versions?.node||"").split(".")[0]||"unknown"}-${process.platform}-${process.arch}`}var N=class extends Error{constructor(e,o){super(`in-process sub-graph fallback: ${e}${o?` (${o})`:""}`),this.fallback=!0,this.reason=e,this.detail=o||null,this.name="SubgraphFallback"}};function Eo(){let t=(process.env.SUBGRAPH_INTERNAL_URL||"").replace(/\/$/,""),e=(process.env.PROGRESS_API_URL||"").replace(/\/executions\/?$/,""),o=t||e,n=process.env.PROJECT_ID,i=process.env.PROJECT_API_TOKEN;if(!o||!n||!i)throw new N("env","SUBGRAPH_INTERNAL_URL/PROGRESS_API_URL/PROJECT_ID/PROJECT_API_TOKEN missing");return{apiBase:o,projectId:n,authToken:i}}async function $o({apiBase:t,authToken:e,body:o}){let n;try{n=await fetch(`${t}/internal/subgraph/begin`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${e}`},body:JSON.stringify(o)})}catch(r){throw new N("network",`begin fetch failed: ${r.message}`)}let i=null;try{i=await n.json()}catch{}if(!n.ok){if(n.status===404){let r=new Error(`Sub-graph child '${o.childWorkflowType}' not found in project`);throw r.code="SUBGRAPH_NOT_FOUND",r.status=404,r}if(n.status===429){let r=i?.quotaInfo||{},s=new Error(`Sub-graph blocked by quota (${r.used??"?"}/${r.limit??"?"} on ${r.planId||"plan"})`);throw s.code="SUBGRAPH_QUOTA_EXCEEDED",s.status=429,s.quotaInfo=r,s}if(n.status===400&&i?.validationErrors){let r=new Error(`Sub-graph rejected input: ${i?.error||i?.message||"validation failed"}`);throw r.code="SUBGRAPH_INVALID_INPUT",r.status=400,r.validationErrors=i.validationErrors,r.missing=i.missing,r}throw new N("begin-status",`begin returned ${n.status}`)}return i?.data||i}async function J({apiBase:t,authToken:e,payload:o}){try{let n=await fetch(`${t}/internal/subgraph/finalize`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${e}`},body:JSON.stringify(o)});n.ok||_.warn(`[in-process subgraph] finalize returned ${n.status} for ${o.childExecutionId}`)}catch(n){_.warn(`[in-process subgraph] finalize failed: ${n.message}`)}}async function To(t,e){let o=U(e,".ready"),n=U(e,"graph.mjs");if(Y(o)&&Y(n))return;yo(e,{recursive:!0});let i=U(e,".lock"),r=!1;try{let{openSync:s,closeSync:a}=await import("node:fs"),c=s(i,"wx");a(c),r=!0}catch(s){if(s.code!=="EEXIST")throw s}if(!r){let s=Date.now()+3e4;for(;Date.now()<s;){if(Y(o)&&Y(n))return;await new Promise(a=>setTimeout(a,100))}throw new N("bundle-extract-timeout","sibling extract did not complete within 30s")}try{await new Promise((c,u)=>{let d=At("curl",["-fsSL",t],{stdio:["ignore","pipe","inherit"]}),l=At("tar",["-xzf","-","-C",e],{stdio:["pipe","inherit","inherit"]});d.stdout.pipe(l.stdin);let p,h,m=()=>{if(p!==void 0&&h!==void 0){if(p!==0)return u(new Error(`curl exited ${p}`));if(h!==0)return u(new Error(`tar exited ${h}`));c()}};d.on("close",y=>{p=y,m()}),l.on("close",y=>{h=y,m()}),d.on("error",u),l.on("error",u)});let{writeFileSync:s,unlinkSync:a}=await import("node:fs");s(o,"");try{a(i)}catch{}}catch(s){try{let{unlinkSync:a}=await import("node:fs");a(i)}catch{}throw new N("bundle-extract-failed",s.message)}}async function bo(t){let e=U(t,"graph.mjs");if(!Y(e))throw new N("entry-missing",`graph.mjs missing under ${t}`);let o;try{o=await import(_o(e).href)}catch(i){throw new N("import-failed",`${i?.code||i?.name||"unknown"}: ${i.message}`)}let n=o.default||Object.values(o).find(i=>typeof i=="function"&&i.prototype?.buildGraph);if(!n)throw new N("entry-class-missing","no buildGraph() class export found");return n}async function Pt(t,e={}){if(!t||typeof t!="string")throw new Error("runInProcessSubgraph: workflowName (string) is required");let o=ce(),n;try{n=Eo()}catch(g){throw g}_.debug(`[in-process subgraph] begin '${t}' parent=${o.executionId||"<root>"}`);let i=await $o({apiBase:n.apiBase,authToken:n.authToken,body:{parentExecutionId:o.executionId,childWorkflowType:t,input:e.input||{},...e.conversationId?{conversationId:e.conversationId}:{}}}),{childExecutionId:r,runtimeTag:s,bundlePresignedUrl:a,sourcesPresignedUrl:c,workflowVersion:u,workflowUuid:d,bundleReady:l}=i,p=Io();if(s&&s!==p)throw await J({apiBase:n.apiBase,authToken:n.authToken,payload:{childExecutionId:r,status:"canceled",error:{message:`runtimeTag mismatch: parent=${p} child=${s}`,code:"RUNTIME_MISMATCH"}}}),new N("runtime-mismatch",`${p} vs ${s}`);if(!l||!a)throw await J({apiBase:n.apiBase,authToken:n.authToken,payload:{childExecutionId:r,status:"canceled",error:{message:"bundle not ready for in-process; falling back to HTTP",code:"NO_BUNDLE"}}}),new N("no-bundle","workflow bundle not built yet");let h=kt(t);if(!h){let g=U(Se,`${d}@${u||"0"}`);try{await To(a,g);try{xo()}catch{}}catch(S){throw S.fallback&&await J({apiBase:n.apiBase,authToken:n.authToken,payload:{childExecutionId:r,status:"failed",error:{message:S.message,code:S.reason}}}),S}try{h=await bo(g),vt(t,h,{workflowUuid:d,version:u,runtimeTag:s,cacheDir:g})}catch(S){throw xt(t,S),await J({apiBase:n.apiBase,authToken:n.authToken,payload:{childExecutionId:r,status:"failed",error:{message:S.message,code:S.reason||"IMPORT_FAILED"}}}),S.fallback?S:new N("import-failed",S.message)}}let m=Date.now(),E=await(typeof h=="function"&&h.prototype?.buildGraph?new h:h).buildGraph(),I={...e.input||{}},f,$;try{f=await Tt({executionId:r,parentExecutionId:o.executionId,conversationId:e.conversationId!==void 0?e.conversationId:o.conversationId,dispatchMode:"inprocess"},()=>E.run(e.parentAgent,I,{signal:e.signal})),$=f&&typeof f=="object"&&"state"in f?f.state:f}catch(g){throw await J({apiBase:n.apiBase,authToken:n.authToken,payload:{childExecutionId:r,status:"failed",error:{message:g.message,code:g.code||"CHILD_THREW",stack:g.stack},durationMs:Date.now()-m}}),g}if(f&&typeof f=="object"&&f.stoppedExternally){await J({apiBase:n.apiBase,authToken:n.authToken,payload:{childExecutionId:r,status:"canceled",finalState:$,durationMs:Date.now()-m}});let g=new Error(`Sub-graph '${t}' canceled by parent abort`);throw g.code="SUBGRAPH_CANCELED",g.subgraphJobId=r,g}return await J({apiBase:n.apiBase,authToken:n.authToken,payload:{childExecutionId:r,status:"completed",finalState:$,durationMs:Date.now()-m}}),{finalState:$,executionId:r}}function vo(t){let e=0,o=[t];for(;o.length;){let n=o.pop(),i;try{i=Nt(n)}catch{continue}if(i.isDirectory()){let r;try{r=Ot(n)}catch{continue}for(let s of r)o.push(U(n,s))}else e+=i.size}return e}function xo({cap:t=Number(process.env.ZIBBY_SUBGRAPH_CACHE_CAP_BYTES||2*1024*1024*1024)}={}){try{if(!Y(Se))return{evicted:0,freedBytes:0};let e=Ot(Se),o=[],n=0;for(let a of e){let c=U(Se,a),u;try{u=Nt(c)}catch{continue}let d=u.isDirectory()?vo(c):u.size;n+=d,o.push({name:a,full:c,size:d,mtimeMs:u.mtimeMs})}if(n<=t)return{evicted:0,freedBytes:0,totalBytes:n};o.sort((a,c)=>a.mtimeMs-c.mtimeMs);let i=Math.floor(t*.7),r=0,s=0;for(let a of o){if(n-r<=i)break;if(!Y(U(a.full,".lock")))try{wo(a.full,{recursive:!0,force:!0}),r+=a.size,s+=1}catch(c){_.debug(`[sub-graph cache] evict skip ${a.name}: ${c.message}`)}}return s>0&&_.info(`[sub-graph cache] evicted ${s} entr(y/ies), freed ${(r/1024/1024).toFixed(1)}MB`),{evicted:s,freedBytes:r,totalBytes:n-r}}catch(e){return _.debug(`[sub-graph cache] evict failed: ${e.message}`),{evicted:0,freedBytes:0}}}var ko=2e3,Ao=600*1e3,No=new Set(["completed","failed","canceled","timeout"]);function Oo(){let t=process.env.PROGRESS_API_URL;if(!t)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 t.replace(/\/executions\/?$/,"")}function Po(){let t=process.env.PROJECT_ID;if(!t)throw new Error("Sub-graph dispatch requires PROJECT_ID env var.");return t}function Co(){let t=process.env.PROJECT_API_TOKEN;if(!t)throw new Error("Sub-graph dispatch requires PROJECT_API_TOKEN env var.");return t}function Ro(){return process.env.EXECUTION_ID||null}function Ct(t,e){return e==null?t:typeof e=="function"?e(t):typeof e=="string"?e.split(".").reduce((o,n)=>o==null?o:o[n],t):t}async function Fe(t,e={}){if(!t||typeof t!="string")throw new Error("dispatchSubgraph: workflowName (string) is required");let o=ce(),n=Number(process.env.ZIBBY_SUBGRAPH_MAX_DEPTH||10);if((o.depth||0)>=n)throw new Error(`dispatchSubgraph('${t}'): sub-graph depth ${o.depth} reached cap of ${n}. Restructure the graph or raise ZIBBY_SUBGRAPH_MAX_DEPTH.`);if(process.env.ZIBBY_INPROCESS_SUBGRAPH!=="0"&&!e.async)try{_.debug(`[sub-graph] trying in-process for '${t}'`);let{finalState:g}=await Pt(t,{input:e.input,conversationId:e.conversationId,signal:e.signal,parentAgent:e.parentAgent}),S=Ct(g,e.output);return _.info(`[sub-graph] '${t}' completed in-process`),S}catch(g){if(g instanceof N||g?.fallback)_.info(`[sub-graph] in-process fallback for '${t}': ${g.reason||"unknown"} \u2014 using HTTP`);else throw g}let i=Oo(),r=Po(),s=Co(),a=Ro(),c=`${i}/projects/${encodeURIComponent(r)}/workflows/${encodeURIComponent(t)}/trigger`,u={input:e.input||{},...a?{parentExecutionId:a}:{},...e.conversationId?{conversationId:e.conversationId}:{}};_.info(`[sub-graph] dispatching '${t}' (${e.async?"async":"sync"}) from parent ${a||"<none>"}`);let d=await fetch(c,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${s}`},body:JSON.stringify(u)});if(!d.ok){let g=null,S="";try{g=await d.json(),S=g?.error||g?.message||JSON.stringify(g)}catch{S=await d.text().catch(()=>"")}if(d.status===429){let v=g?.quotaInfo||{},D=new Error(`Sub-graph '${t}' blocked by execution quota (${v.used??"?"}/${v.limit??"?"} on plan ${v.planId||"unknown"}). Sub-workflow runs count toward the same monthly cap as user-triggered runs.`);throw D.code="SUBGRAPH_QUOTA_EXCEEDED",D.status=429,D.subgraph=t,D.quotaInfo=v,D}if(d.status===400){let v=new Error(`Sub-graph '${t}' rejected input: ${S}`);throw v.code="SUBGRAPH_INVALID_INPUT",v.status=400,v.subgraph=t,v.validationErrors=g?.validationErrors||null,v.missing=g?.missing||null,v}let R=new Error(`Sub-graph '${t}' trigger rejected (${d.status}): ${S}`);throw R.code="SUBGRAPH_TRIGGER_FAILED",R.status=d.status,R.subgraph=t,R}let l=await d.json(),p=l?.data?.jobId||l?.jobId;if(!p)throw new Error(`Sub-graph '${t}' trigger returned no jobId: ${JSON.stringify(l).slice(0,200)}`);if(e.async)return _.info(`[sub-graph] async dispatch of '${t}' \u2192 jobId=${p} (not waiting)`),{jobId:p,status:"accepted",workflow:t};let h=Number.isFinite(e.timeoutMs)?e.timeoutMs:Ao,m=Number.isFinite(e.pollIntervalMs)?e.pollIntervalMs:ko,y=`${i}/executions/${encodeURIComponent(p)}`,E=Date.now()+h,I="accepted",f=0;for(;Date.now()<E;){await new Promise(v=>setTimeout(v,m)),f+=1;let g=await fetch(y,{headers:{Authorization:`Bearer ${s}`}});if(!g.ok){if(g.status>=500){_.warn(`[sub-graph] status poll for ${p} returned ${g.status}, will retry`);continue}throw new Error(`Sub-graph status poll failed for ${p}: ${g.status}`)}let S=await g.json(),R=S?.data||S?.execution||S;if(I=R?.status||I,No.has(I)){if(I!=="completed"){let w=new Error(`Sub-graph '${t}' (${p}) ended in status '${I}'`);throw w.subgraphJobId=p,w.subgraphStatus=I,w}let v=R?.finalState||R?.state||{},D=Ct(v,e.output);return _.info(`[sub-graph] '${t}' (${p}) completed after ${f} polls`),D}}let $=new Error(`Sub-graph '${t}' (${p}) timed out after ${Math.round(h/1e3)}s (last status: ${I})`);throw $.subgraphJobId=p,$.subgraphStatus=I,$}import{existsSync as Rt,readFileSync as Bo}from"node:fs";import{join as Ue,dirname as Bt}from"node:path";var le=class{static async loadContext(e,o,n={}){let i={},r=n.filenames||["CONTEXT.md","AGENTS.md"];if(e){let a=Bt(Ue(o,e));for(let c of r){let u=await this.findAndMergeContextFiles(c,a,o);if(u){let d=c.replace(/\.[^.]+$/,"").toLowerCase();i[d]=u}}}let s=n.discovery||{};for(let[a,c]of Object.entries(s))try{let u=Ue(o,c);Rt(u)&&(i[a]=await this.loadFile(u))}catch(u){console.warn(`[workflow] could not load context '${a}' from '${c}': ${u.message}`)}return i}static async findAndMergeContextFiles(e,o,n){let i=[],r=o;for(;r.startsWith(n);){let s=Ue(r,e);if(Rt(s))try{i.unshift(await this.loadFile(s))}catch(c){console.warn(`[workflow] could not load ${e} from ${s}: ${c.message}`)}let a=Bt(r);if(a===r)break;r=a}return i.length===0?null:i.every(s=>typeof s=="string")?i.join(`
|
|
30
30
|
|
|
31
31
|
---
|
|
32
32
|
|
|
33
|
-
`):i.every(s=>typeof s=="object")?Object.assign({},...i):i[i.length-1]}static async loadFile(e){let o=
|
|
33
|
+
`):i.every(s=>typeof s=="object")?Object.assign({},...i):i[i.length-1]}static async loadFile(e){let o=Bo(e,"utf-8");if(e.endsWith(".json"))return JSON.parse(o);if(e.endsWith(".js")||e.endsWith(".mjs")){let{pathToFileURL:n}=await import("url"),i=await import(n(e).href);return i.default||i}return o}};import{mkdirSync as Lt,existsSync as We,writeFileSync as Mt,unlinkSync as Mo}from"node:fs";import{join as z,resolve as jt}from"node:path";import{config as Do}from"dotenv";import{zodToJsonSchema as Dt}from"zod-to-json-schema";import{z as ye}from"zod";import Lo from"handlebars";function jo({traceFrom:t,sessionId:e,sessionPath:o,idSource:n,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=${t} pid=${process.pid} ppid=${s} sessionId=${e} source=${n} mkdir=${i?"yes":"no"} path=${o}`;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 (${t}):
|
|
36
|
-
${d}`)}}function jt(){return process.env.ZIBBY_TRUST_SESSION_ENV==="1"||process.env.ZIBBY_TRUST_SESSION_ENV==="true"||process.env.ZIBBY_KEEP_SESSION_ENV==="1"||process.env.ZIBBY_KEEP_SESSION_ENV==="true"}function Ft(){if(!(process.env.ZIBBY_PIN_SESSION_PATH==="1"||process.env.ZIBBY_PIN_SESSION_PATH==="true"))return;let e=process.env.ZIBBY_SESSION_PATH;if(!(e==null||String(e).trim()===""))try{return Lt(String(e).trim())}catch{return String(e).trim()}}function Ut(){jt()||(delete process.env.ZIBBY_SESSION_PATH,delete process.env.ZIBBY_SESSION_ID)}function Wt({sessionPath:t,sessionId:e}){t&&typeof t=="string"&&(process.env.ZIBBY_SESSION_PATH=t),e!=null&&String(e).trim()!==""&&(process.env.ZIBBY_SESSION_ID=String(e).trim())}function Gt(t={}){let e=Ae.map(r=>process.env[r]).find(Boolean),o=Math.random().toString(36).slice(2,6),n=e||`${Date.now()}_${o}`,i=t.paths?.sessionPrefix;return i?`${i}_${n}`:n}function Ht({cwd:t=process.cwd(),config:e={},initialState:o={},traceFrom:n="resolveWorkflowSession"}={}){let i=o.sessionPath,r=o.sessionTimestamp,s="initialState.sessionPath";if(!i&&process.env.ZIBBY_SESSION_PATH)try{let l=Lt(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(),r==null&&(r=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 u=e.sessionId!=null?String(e.sessionId).trim():"";u&&u!=="last"?(a=u,s="config.sessionId"):(a=Gt(e),s="generated")}r=r??Date.now();let d=e.paths?.output||se;i=z(t,d,xe,a)}let c=!We(i);return c&&Dt(i,{recursive:!0}),(c||s!=="initialState.sessionPath")&&Mo({traceFrom:n,sessionId:a,sessionPath:i,idSource:s,mkdirFresh:c}),Wt({sessionPath:i,sessionId:a}),{sessionPath:i,sessionId:a,sessionTimestamp:r}}var ue=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,o,n={}){if(!(o instanceof j)&&o&&typeof o=="object"&&typeof o.workflow=="string"){let r=o,s={name:e,_isCustomCode:!0,retries:r.retries,onComplete:r.onComplete,execute:async c=>{let l=c?.state&&typeof c.state.getAll=="function"?c.state.getAll():c,d;return typeof r.input=="function"?d=r.input(l):r.input&&typeof r.input=="object"?d=r.input:d={},Fe(r.workflow,{input:d,async:r.async===!0,conversationId:typeof r.conversationId=="function"?r.conversationId(l):r.conversationId,output:r.output,timeoutMs:r.timeoutMs,pollIntervalMs:r.pollIntervalMs,signal:l?._signal,parentAgent:c?.agent})}},a=new j(s);return a.name=e,this.nodes.set(e,a),n.prompt&&this.nodePrompts.set(e,n.prompt),Object.keys(n).length>0&&this.nodeOptions.set(e,n),this}let i=o instanceof j?o:new j(o);return i.name=e,this.nodes.set(e,i),n.prompt?this.nodePrompts.set(e,n.prompt):typeof o?.prompt=="string"&&o.prompt.trim()&&this.nodePrompts.set(e,o.prompt),Object.keys(n).length>0&&this.nodeOptions.set(e,n),this}addConditionalNode(e,o){return this.nodes.set(e,new Q({...o,name:e})),this}addEdge(e,o){return this.edges.set(e,o),this}setNodeType(e,o){return this.nodeTypeMap.set(e,o),this}addConditionalEdges(e,o,{labels:n}={}){return this.edges.set(e,{conditional:!0,routes:o,labels:n}),typeof o=="function"&&this.conditionalCodeMap.set(e,o.toString()),this}setEntryPoint(e){return this.entryPoint=e,this}use(e){return typeof e=="function"&&this.middleware.push(e),this}_composeMiddleware(e,o,n,i,r){let s=n;for(let a=e.length-1;a>=0;a--){let c=e[a],l=s;s=()=>c(o,l,i,r)}return s()}serialize(){let e=[],o={};for(let[d,u]of this.nodes){let p=this.nodeTypeMap.get(d)||(u instanceof Q?"decision":d);e.push({id:d,type:p,data:{nodeType:p,label:d}});let f={};u._isCustomCode&&typeof u.execute=="function"&&(f.customCode=u.execute.toString());let m=this.nodePrompts.get(d);if(m)f.prompt=m;else if(typeof u.prompt=="function")try{let S=u.prompt({});typeof S=="string"&&S.trim()&&(f.prompt=S,f.promptIsCode=!0)}catch{}if(typeof u.customExecute=="function"&&(f.executeCode=u.customExecute.toString()),u.outputSchema)if(typeof u.outputSchema._def<"u"){let S=null;if(typeof ye?.toJSONSchema=="function")try{S=ye.toJSONSchema(u.outputSchema)}catch{}if(!S)try{S=Mt(u.outputSchema,{target:"openApi3"})}catch{}f.outputSchema=S?{jsonSchema:S,variables:this._flattenJsonSchemaToVariables(S)}:{schema:u.outputSchema}}else f.outputSchema={schema:u.outputSchema};let w=(this.resolvedToolsMap||{})[d];w?.toolIds&&(f.tools=w.toolIds);let E=Array.isArray(u?.config?.skills)?u.config.skills:Array.isArray(u?.skills)?u.skills:null;E&&E.length>0&&(f.skills=[...E]),Object.keys(f).length>0&&(o[d]=f)}let n=[];for(let[d,u]of this.edges)if(typeof u=="string")n.push({source:d,target:u});else if(u.conditional){let p=this.conditionalCodeMap.get(d)||u.routes.toString(),f=this._inferConditionalTargets(u.routes,u.labels),m=u.labels||{};for(let w of f){let E={source:d,target:w,data:{conditionalCode:p}};m[w]&&(E.label=m[w]),n.push(E)}}let i=d=>{if(!d)return null;if(typeof ye?.toJSONSchema=="function")try{return ye.toJSONSchema(d)}catch{}try{return Mt(d,{target:"openApi3"})}catch{return null}};this.entryPoint&&this.nodes.has(this.entryPoint)&&(e.unshift({id:"START",type:"start",data:{nodeType:"start",label:"Start"}}),n.unshift({source:"START",target:this.entryPoint}));let r=0;for(let d of n)if(d.target==="END"){r+=1;let u=`END__${r}`;d.target=u,e.push({id:u,type:"end",data:{nodeType:"end",label:"End"}})}for(let d of this.nodes.keys())if(!this.edges.has(d)){r+=1;let u=`END__${r}`;e.push({id:u,type:"end",data:{nodeType:"end",label:"End"}}),n.push({source:d,target:u})}let s=this._runtimeSchema(),a=i(s||this.stateSchema),c=i(this.inputSchema),l=i(this.contextSchema);return{nodes:e,edges:n,nodeConfigs:o,stateSchema:a,inputSchema:c,contextSchema:l}}_inferConditionalTargets(e,o){let n=e.toString(),i=new Set,r=/(['"])((?:\\.|(?!\1).)*?)\1|`((?:\\.|[^`$]|\$(?!\{))*?)`/g,s;for(;(s=r.exec(n))!==null;){let l=s[2]!==void 0?s[2]:s[3];l!==void 0&&l!==""&&i.add(l)}let a=new Set(["END","START","__end__","__start__"]);for(let l of this.nodes.keys())a.add(l);if(o&&typeof o=="object")for(let l of Object.keys(o))a.add(l);let c=new Set;for(let l of i)a.has(l)&&c.add(l);if(c.size===0){let l=/return\s+['"]([^'"]+)['"]/g,d;for(;(d=l.exec(n))!==null;)c.add(d[1])}return[...c]}_flattenJsonSchemaToVariables(e,o=""){let n=e;if(e.$ref&&e.definitions){let i=e.$ref.replace("#/definitions/","");n=e.definitions[i]||e}return this._flattenSchema(n,o)}_flattenSchema(e,o=""){if(!e||typeof e!="object")return[];let n=[],i=e.properties||{},r=e.required||[];for(let[s,a]of Object.entries(i)){let c=o?`${o}.${s}`:s;n.push({path:c,type:a.type||"unknown",label:a.description||this._formatLabel(s),optional:!r.includes(s)}),a.type==="object"&&a.properties&&n.push(...this._flattenSchema(a,c)),a.type==="array"&&a.items?.type==="object"&&a.items.properties&&n.push(...this._flattenSchema(a.items,`${c}[]`))}return n}_formatLabel(e){return e.replace(/([A-Z])/g," $1").replace(/^./,o=>o.toUpperCase()).trim()}_summarizeNodeOutput(e,o){if(!o||typeof o!="object")return[];let n=[];o.success!==void 0&&n.push(`Result: ${o.success?"passed":"failed"}`);for(let[i,r]of Object.entries(o))if(!(i==="success"||i==="raw"||i==="nextNode")){if(typeof r=="string"&&r.length<=80)n.push(`${i}: ${r}`);else if(Array.isArray(r)){let s=r.length,a=r.filter(l=>l?.passed===!0).length,c=r.some(l=>l?.passed!==void 0);n.push(c?`${i}: ${a}/${s} passed${s-a?`, ${s-a} failed`:""}`:`${i}: ${s} items`)}if(n.length>=4)break}return n}async run(e,o={},n={}){if(!this.entryPoint)throw new Error("No entry point set for graph");let i=new AbortController;n.signal&&(n.signal.aborted?i.abort():n.signal.addEventListener("abort",()=>i.abort(),{once:!0}));let r=n.strategyAbortTimeoutMs??o.config?.strategyAbortTimeoutMs??5e3,s=o.cwd||process.cwd();Ro({path:z(s,".env")});let a=o.config||{};if(!a||Object.keys(a).length===0)try{let T=z(s,".zibby.config.js");We(T)&&(a=(await import(T)).default||{})}catch{}process.env.EXECUTION_ID&&!a.agent?.strictMode&&(a.agent={...a.agent,strictMode:!0});let c=o.agentType;if(!c){let T=a?.agent;T?.provider?c=T.provider:T?.gemini?c="gemini":T?.claude?c="claude":T?.cursor?c="cursor":T?.codex?c="codex":c=process.env.AGENT_TYPE||"cursor"}let l=o.contextConfig||e?.config?.contextConfig||e?.config?.context||a?.context||{},d=this._runtimeSchema();if(d){let T=d.safeParse(o);if(!T.success){let N=T.error.issues.map(O=>`${O.path.join(".")}: ${O.message}`);throw console.error("\u274C Initial state validation failed:"),N.forEach(O=>console.error(` - ${O}`)),new Error(`State validation failed: ${N.join(", ")}`)}x.step("State validated against schema")}let u=Ft(),p=o.sessionPath||u;p||Ut();let{sessionPath:f,sessionTimestamp:m,sessionId:w}=Ht({cwd:s,config:a,traceFrom:"WorkflowGraph.run",initialState:{sessionPath:p,sessionTimestamp:o.sessionTimestamp}});x.step(`Session ${w}`);let E=await le.loadContext(o.specPath||"",s,l);Object.keys(E).length>0&&x.step(`Context loaded: ${Object.keys(E).join(", ")}`);let S=o.outputPath;!S&&o.specPath&&(e?.calculateOutputPath?S=e.calculateOutputPath(o.specPath):console.warn(`\u26A0\uFE0F outputPath not resolved (specPath=${o.specPath})`));let h=new te({...o,config:a,agentType:c,outputPath:S,sessionPath:f,sessionTimestamp:m,context:E,resolvedTools:this.resolvedToolsMap||{},_signal:i.signal}),$=new Map;try{await import("@zibby/skills")}catch{}let{getSkill:g}=await Promise.resolve().then(()=>(ie(),St)),y=a.skills&&typeof a.skills=="object"?a.skills:{},R=Object.values(y).filter(T=>T&&typeof T=="object"&&typeof T.id=="string"),v=T=>{for(let N of R)if(N.id===T)return N;return g(T)},D=new Set;for(let[,T]of this.nodes)for(let N of T.config?.skills||[])D.add(N);for(let T of D){let N=v(T);if(typeof N?.middleware=="function")try{let O=await N.middleware();typeof O=="function"&&$.set(T,O)}catch{}}let _=this.entryPoint,pe=[],Ze=a?.recursionLimit??100,Zt=0;try{for(;_&&_!=="END";){if(++Zt>Ze)throw new Error(`Workflow exceeded recursion limit (${Ze}) \u2014 likely a cyclic conditional route. Set config.recursionLimit if you need a higher cap.`);let N=z(f,ke);if(We(N)){try{Co(N)}catch{}i.abort()}if(i.signal.aborted)return console.warn(`
|
|
37
|
-
\u{1F6D1} External stop requested \u2014 ending workflow.`),
|
|
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 jt(String(e).trim())}catch{return String(e).trim()}}function Wt(){Ft()||(delete process.env.ZIBBY_SESSION_PATH,delete process.env.ZIBBY_SESSION_ID)}function Gt({sessionPath:t,sessionId:e}){t&&typeof t=="string"&&(process.env.ZIBBY_SESSION_PATH=t),e!=null&&String(e).trim()!==""&&(process.env.ZIBBY_SESSION_ID=String(e).trim())}function Ht(t={}){let e=Ae.map(r=>process.env[r]).find(Boolean),o=Math.random().toString(36).slice(2,6),n=e||`${Date.now()}_${o}`,i=t.paths?.sessionPrefix;return i?`${i}_${n}`:n}function Jt({cwd:t=process.cwd(),config:e={},initialState:o={},traceFrom:n="resolveWorkflowSession"}={}){let i=o.sessionPath,r=o.sessionTimestamp,s="initialState.sessionPath";if(!i&&process.env.ZIBBY_SESSION_PATH)try{let u=jt(String(process.env.ZIBBY_SESSION_PATH));u&&(i=u,s="ZIBBY_SESSION_PATH")}catch{}let a;if(i)a=String(i).split(/[/\\]/).filter(Boolean).pop(),r==null&&(r=Date.now());else{let u=process.env.ZIBBY_SESSION_ID&&String(process.env.ZIBBY_SESSION_ID).trim();if(u)a=u,s="ZIBBY_SESSION_ID";else{let l=e.sessionId!=null?String(e.sessionId).trim():"";l&&l!=="last"?(a=l,s="config.sessionId"):(a=Ht(e),s="generated")}r=r??Date.now();let d=e.paths?.output||se;i=z(t,d,xe,a)}let c=!We(i);return c&&Lt(i,{recursive:!0}),(c||s!=="initialState.sessionPath")&&jo({traceFrom:n,sessionId:a,sessionPath:i,idSource:s,mkdirFresh:c}),Gt({sessionPath:i,sessionId:a}),{sessionPath:i,sessionId:a,sessionTimestamp:r}}var ue=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{if(typeof this.inputSchema.merge=="function")return this.inputSchema.merge(this.contextSchema);if(typeof this.inputSchema.and=="function")return this.inputSchema.and(this.contextSchema)}catch{}return this.inputSchema&&!this.contextSchema?this.inputSchema:this.stateSchema}addNode(e,o,n={}){if(!(o instanceof j)&&o&&typeof o=="object"&&typeof o.workflow=="string"){let r=o,s={name:e,_isCustomCode:!0,retries:r.retries,onComplete:r.onComplete,execute:async c=>{let u=c?.state&&typeof c.state.getAll=="function"?c.state.getAll():c,d;return typeof r.input=="function"?d=r.input(u):r.input&&typeof r.input=="object"?d=r.input:d={},Fe(r.workflow,{input:d,async:r.async===!0,conversationId:typeof r.conversationId=="function"?r.conversationId(u):r.conversationId,output:r.output,timeoutMs:r.timeoutMs,pollIntervalMs:r.pollIntervalMs,signal:u?._signal,parentAgent:c?.agent})}},a=new j(s);return a.name=e,this.nodes.set(e,a),n.prompt&&this.nodePrompts.set(e,n.prompt),Object.keys(n).length>0&&this.nodeOptions.set(e,n),this}let i=o instanceof j?o:new j(o);return i.name=e,this.nodes.set(e,i),n.prompt?this.nodePrompts.set(e,n.prompt):typeof o?.prompt=="string"&&o.prompt.trim()&&this.nodePrompts.set(e,o.prompt),Object.keys(n).length>0&&this.nodeOptions.set(e,n),this}addConditionalNode(e,o){return this.nodes.set(e,new Q({...o,name:e})),this}addEdge(e,o){return this.edges.set(e,o),this}setNodeType(e,o){return this.nodeTypeMap.set(e,o),this}addConditionalEdges(e,o,{labels:n}={}){return this.edges.set(e,{conditional:!0,routes:o,labels:n}),typeof o=="function"&&this.conditionalCodeMap.set(e,o.toString()),this}setEntryPoint(e){return this.entryPoint=e,this}use(e){return typeof e=="function"&&this.middleware.push(e),this}_composeMiddleware(e,o,n,i,r){let s=n;for(let a=e.length-1;a>=0;a--){let c=e[a],u=s;s=()=>c(o,u,i,r)}return s()}serialize(){let e=[],o={};for(let[d,l]of this.nodes){let p=this.nodeTypeMap.get(d)||(l instanceof Q?"decision":d);e.push({id:d,type:p,data:{nodeType:p,label:d}});let h={};l._isCustomCode&&typeof l.execute=="function"&&(h.customCode=l.execute.toString());let m=typeof l?.config?.description=="string"&&l.config.description.trim()?l.config.description:typeof l?.description=="string"&&l.description.trim()?l.description:null;m&&(h.description=m);let y=this.nodePrompts.get(d);if(y)h.prompt=y;else if(typeof l.prompt=="function")try{let f=l.prompt({});typeof f=="string"&&f.trim()&&(h.prompt=f,h.promptIsCode=!0)}catch{}if(typeof l.customExecute=="function"&&(h.executeCode=l.customExecute.toString()),l.outputSchema)if(typeof l.outputSchema._def<"u"){let f=null;if(typeof ye?.toJSONSchema=="function")try{f=ye.toJSONSchema(l.outputSchema)}catch{}if(!f)try{f=Dt(l.outputSchema,{target:"openApi3"})}catch{}h.outputSchema=f?{jsonSchema:f,variables:this._flattenJsonSchemaToVariables(f)}:{schema:l.outputSchema}}else h.outputSchema={schema:l.outputSchema};let E=(this.resolvedToolsMap||{})[d];E?.toolIds&&(h.tools=E.toolIds);let I=Array.isArray(l?.config?.skills)?l.config.skills:Array.isArray(l?.skills)?l.skills:null;I&&I.length>0&&(h.skills=[...I]),Object.keys(h).length>0&&(o[d]=h)}let n=[];for(let[d,l]of this.edges)if(typeof l=="string")n.push({source:d,target:l});else if(l.conditional){let p=this.conditionalCodeMap.get(d)||l.routes.toString(),h=this._inferConditionalTargets(l.routes,l.labels),m=l.labels||{};for(let y of h){let E={source:d,target:y,data:{conditionalCode:p}};m[y]&&(E.label=m[y]),n.push(E)}}let i=d=>{if(!d)return null;if(typeof ye?.toJSONSchema=="function")try{return ye.toJSONSchema(d)}catch{}try{return Dt(d,{target:"openApi3"})}catch{return null}};this.entryPoint&&this.nodes.has(this.entryPoint)&&(e.unshift({id:"START",type:"start",data:{nodeType:"start",label:"Start"}}),n.unshift({source:"START",target:this.entryPoint}));let r=0;for(let d of n)if(d.target==="END"){r+=1;let l=`END__${r}`;d.target=l,e.push({id:l,type:"end",data:{nodeType:"end",label:"End"}})}for(let d of this.nodes.keys())if(!this.edges.has(d)){r+=1;let l=`END__${r}`;e.push({id:l,type:"end",data:{nodeType:"end",label:"End"}}),n.push({source:d,target:l})}let s=this._runtimeSchema(),a=i(s||this.stateSchema),c=i(this.inputSchema),u=i(this.contextSchema);return{nodes:e,edges:n,nodeConfigs:o,stateSchema:a,inputSchema:c,contextSchema:u}}_inferConditionalTargets(e,o){let n=e.toString(),i=new Set,r=/(['"])((?:\\.|(?!\1).)*?)\1|`((?:\\.|[^`$]|\$(?!\{))*?)`/g,s;for(;(s=r.exec(n))!==null;){let u=s[2]!==void 0?s[2]:s[3];u!==void 0&&u!==""&&i.add(u)}let a=new Set(["END","START","__end__","__start__"]);for(let u of this.nodes.keys())a.add(u);if(o&&typeof o=="object")for(let u of Object.keys(o))a.add(u);let c=new Set;for(let u of i)a.has(u)&&c.add(u);if(c.size===0){let u=/return\s+['"]([^'"]+)['"]/g,d;for(;(d=u.exec(n))!==null;)c.add(d[1])}return[...c]}_flattenJsonSchemaToVariables(e,o=""){let n=e;if(e.$ref&&e.definitions){let i=e.$ref.replace("#/definitions/","");n=e.definitions[i]||e}return this._flattenSchema(n,o)}_flattenSchema(e,o=""){if(!e||typeof e!="object")return[];let n=[],i=e.properties||{},r=e.required||[];for(let[s,a]of Object.entries(i)){let c=o?`${o}.${s}`:s;n.push({path:c,type:a.type||"unknown",label:a.description||this._formatLabel(s),optional:!r.includes(s)}),a.type==="object"&&a.properties&&n.push(...this._flattenSchema(a,c)),a.type==="array"&&a.items?.type==="object"&&a.items.properties&&n.push(...this._flattenSchema(a.items,`${c}[]`))}return n}_formatLabel(e){return e.replace(/([A-Z])/g," $1").replace(/^./,o=>o.toUpperCase()).trim()}_summarizeNodeOutput(e,o){if(!o||typeof o!="object")return[];let n=[];o.success!==void 0&&n.push(`Result: ${o.success?"passed":"failed"}`);for(let[i,r]of Object.entries(o))if(!(i==="success"||i==="raw"||i==="nextNode")){if(typeof r=="string"&&r.length<=80)n.push(`${i}: ${r}`);else if(Array.isArray(r)){let s=r.length,a=r.filter(u=>u?.passed===!0).length,c=r.some(u=>u?.passed!==void 0);n.push(c?`${i}: ${a}/${s} passed${s-a?`, ${s-a} failed`:""}`:`${i}: ${s} items`)}if(n.length>=4)break}return n}async run(e,o={},n={}){if(!this.entryPoint)throw new Error("No entry point set for graph");let i=new AbortController;n.signal&&(n.signal.aborted?i.abort():n.signal.addEventListener("abort",()=>i.abort(),{once:!0}));let r=n.strategyAbortTimeoutMs??o.config?.strategyAbortTimeoutMs??5e3,s=o.cwd||process.cwd();Do({path:z(s,".env")});let a=o.config||{};if(!a||Object.keys(a).length===0)try{let T=z(s,".zibby.config.js");We(T)&&(a=(await import(T)).default||{})}catch{}process.env.EXECUTION_ID&&!a.agent?.strictMode&&(a.agent={...a.agent,strictMode:!0});let c=o.agentType;if(!c){let T=a?.agent;T?.provider?c=T.provider:T?.gemini?c="gemini":T?.claude?c="claude":T?.cursor?c="cursor":T?.codex?c="codex":c=process.env.AGENT_TYPE||"cursor"}let u=o.contextConfig||e?.config?.contextConfig||e?.config?.context||a?.context||{},d=this._runtimeSchema();if(d){let T=d.safeParse(o);if(!T.success){let O=T.error.issues.map(P=>`${P.path.join(".")}: ${P.message}`);throw console.error("\u274C Initial state validation failed:"),O.forEach(P=>console.error(` - ${P}`)),new Error(`State validation failed: ${O.join(", ")}`)}k.step("State validated against schema")}let l=Ut(),p=o.sessionPath||l;p||Wt();let{sessionPath:h,sessionTimestamp:m,sessionId:y}=Jt({cwd:s,config:a,traceFrom:"WorkflowGraph.run",initialState:{sessionPath:p,sessionTimestamp:o.sessionTimestamp}});k.step(`Session ${y}`);let E=await le.loadContext(o.specPath||"",s,u);Object.keys(E).length>0&&k.step(`Context loaded: ${Object.keys(E).join(", ")}`);let I=o.outputPath;!I&&o.specPath&&(e?.calculateOutputPath?I=e.calculateOutputPath(o.specPath):console.warn(`\u26A0\uFE0F outputPath not resolved (specPath=${o.specPath})`));let f=new te({...o,config:a,agentType:c,outputPath:I,sessionPath:h,sessionTimestamp:m,context:E,resolvedTools:this.resolvedToolsMap||{},_signal:i.signal}),$=new Map;try{await import("@zibby/skills")}catch{}let{getSkill:g}=await Promise.resolve().then(()=>(ie(),yt)),S=a.skills&&typeof a.skills=="object"?a.skills:{},R=Object.values(S).filter(T=>T&&typeof T=="object"&&typeof T.id=="string"),v=T=>{for(let O of R)if(O.id===T)return O;return g(T)},D=new Set;for(let[,T]of this.nodes)for(let O of T.config?.skills||[])D.add(O);for(let T of D){let O=v(T);if(typeof O?.middleware=="function")try{let P=await O.middleware();typeof P=="function"&&$.set(T,P)}catch{}}let w=this.entryPoint,pe=[],Ze=a?.recursionLimit??100,Kt=0;try{for(;w&&w!=="END";){if(++Kt>Ze)throw new Error(`Workflow exceeded recursion limit (${Ze}) \u2014 likely a cyclic conditional route. Set config.recursionLimit if you need a higher cap.`);let O=z(h,ke);if(We(O)){try{Mo(O)}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:f.getAll(),executionLog:pe,stoppedExternally:!0};let P=this.nodes.get(w);if(!P)throw new Error(`Node '${w}' not found in graph`);let Ke=JSON.stringify({sessionPath:h,sessionTimestamp:m,currentNode:w,createdAt:new Date().toISOString(),config:f.get("config")}),Vt=z(h,W);Mt(Vt,Ke,"utf-8");let Ve=f.get("config")?.paths?.output||se,qt=z(s,Ve,W);Lt(z(s,Ve),{recursive:!0});try{Mt(qt,Ke,"utf-8")}catch{}let qe=o.onPipelineProgress;if(typeof qe=="function")try{qe({cwd:s,sessionPath:h,sessionId:y,outputBase:f.get("config")?.paths?.output||se,currentNode:w})}catch{}let Xt=(this.resolvedToolsMap||{})[w]||null;f.set("_currentNodeTools",Xt);let Qt=f.get("nodeConfigs")||{};f.set("_currentNodeConfig",Qt[w]||{}),k.nodeStart(w);let Xe=Date.now(),fe=this.nodePrompts.get(w);if(!this._invokeAgent){let x=await Promise.resolve().then(()=>(X(),ae));this._invokeAgent=x.invokeAgent}let eo=this._invokeAgent,Ee={},to=P.config?.skills||[];for(let x of to){let C=v(x);if(typeof C?.invokeAgentOptions=="function")try{let b=C.invokeAgentOptions(f.getAll(),{agentType:f.get("agentType"),nodeName:w});b&&typeof b=="object"&&(Ee={...Ee,...b})}catch(b){console.warn(`[graph] skill '${x}' invokeAgentOptions threw: ${b.message}`)}}let Qe=async(x,C,b={})=>{let B=eo(x,C,{...Ee,...b,signal:i.signal});return B.catch(()=>{}),i.signal.aborted?B:Promise.race([B,new Promise((Z,K)=>{let L=()=>{setTimeout(()=>{let ee=new Error(`Strategy ignored AbortSignal \u2014 engine deadman fired after ${r}ms`);ee.name="AbortError",K(ee)},r)};i.signal.addEventListener("abort",L,{once:!0})})])},oo=async(x={},C={})=>{let b=C.prompt||"";if(fe){let B=this._compiledPrompts.get(w);B||(B=Lo.compile(fe,{noEscape:!0}),this._compiledPrompts.set(w,B));try{b=B(x)}catch(Z){throw console.error(`\u274C Template rendering failed for node '${w}':`,Z.message),new Error(`Template rendering failed: ${Z.message}`,{cause:Z})}}else if(!b)throw new Error(`No prompt template configured for node '${w}' and no prompt provided in options`);return Qe(b,{state:f.getAll(),images:C.images||[]},{model:C.model||f.get("model"),workspace:f.get("workspace"),schema:C.schema,...C,signal:i.signal})},et=f.getAll(),no=["state","invokeAgent","_coreInvokeAgent","agent","nodeId","promptTemplate","getPromptTemplate"];for(let x of no)Object.prototype.hasOwnProperty.call(et,x)&&console.warn(`[workflow] node "${w}": state key "${x}" is shadowed by the engine context prop; read it via context.state.get('${x}')`);let tt={...et,state:f,invokeAgent:oo,_coreInvokeAgent:Qe,agent:e,nodeId:w,promptTemplate:fe,getPromptTemplate:()=>fe};try{let x=(P.config?.skills||[]).map(L=>$.get(L)).filter(Boolean),C=[...this.middleware,...x],b;C.length>0?b=await this._composeMiddleware(C,w,async()=>P.execute(tt,f),f.getAll(),f):b=await P.execute(tt,f);let B=Date.now()-Xe;if(pe.push({node:w,success:b.success,duration:B,timestamp:new Date().toISOString()}),!b.success){if(i.signal.aborted)return k.step("Workflow stopped externally"),{success:!0,state:f.getAll(),executionLog:pe,stoppedExternally:!0};f.append("errors",{node:w,error:b.error});let L=P.config?.retries||0,ee=`${w}_retries`,he=f.getAll()[ee]||0;if(he<L){k.stepInfo(`Retrying (attempt ${he+1}/${L})`),f.update({[ee]:he+1,[`${w}_raw`]:b.raw});continue}throw k.nodeFailed(w,b.error,{duration:B}),new Error(`Node '${w}' failed after ${he} attempts: ${b.error}`)}f.update({[w]:b.output});let Z=this._summarizeNodeOutput(w,b.output);k.nodeComplete(w,{duration:B,details:Z});let K=this.edges.get(w);if(!K)w="END";else if(K.conditional){let L=K.routes(f.getAll());k.route(w,L),w=L}else w=K}catch(x){throw k.isInsideNode&&k.nodeFailed(w,x.message,{duration:Date.now()-Xe}),f.set("failed",!0),f.set("failedAt",w),x}}k.graphComplete();let T={success:!0,state:f.getAll(),executionLog:pe};return e&&typeof e.onComplete=="function"&&await e.onComplete(T),T}finally{if(e&&typeof e.cleanup=="function")try{await e.cleanup()}catch(T){console.warn(`[workflow] agent.cleanup() failed: ${T.message}`)}}}};var Ge=Symbol.for("@zibby/agent-workflow.nodes");globalThis[Ge]||(globalThis[Ge]=new Map);var de=globalThis[Ge];function Yt(t,e){de.set(t,e)}function He(t){return de.get(t)}function we(t){return de.has(t)}function Fo(){return Array.from(de.keys())}function Je(t){let e=de.get(t);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}Yt("ai_agent",{name:"ai_agent",factory:!0,create:(t,e={})=>({name:t,_isCustomCode:!0,execute:async o=>{let n=o?._coreInvokeAgent;n||(n=(await Promise.resolve().then(()=>(X(),ae))).invokeAgent);let i=e.extraPromptInstructions||"Execute the task based on the current state.",r=Uo(i,o),s=await n(r,{cwd:o.workspace||process.cwd(),model:o.model,tools:e.resolvedTools||null});return{success:!0,output:{raw:s,nodeId:t},raw:typeof s=="string"?s:s.raw}}})});function Uo(t,e){let o=/@([\w.]+)/g,n=new Set,i;for(;(i=o.exec(t))!==null;)n.add(i[1]);if(n.size===0)return t;let r=[],s=new Set;for(let a of n){let c=a.split(".")[0];if(s.has(c))continue;let u=a.split(".").reduce((p,h)=>p?.[h],e);if(u===void 0)continue;let d=typeof u=="string"?u:u?.raw??JSON.stringify(u,null,2),l=a.replace(/_/g," ").replace(/\b\w/g,p=>p.toUpperCase());r.push(`## ${l}
|
|
38
38
|
${d}`),a.includes(".")||s.add(c)}return r.length===0?t:`${t}
|
|
39
39
|
|
|
40
40
|
---
|
|
@@ -42,13 +42,13 @@ ${d}`),a.includes(".")||s.add(c)}return r.length===0?t:`${t}
|
|
|
42
42
|
|
|
43
43
|
${r.join(`
|
|
44
44
|
|
|
45
|
-
`)}`}ie();F();var _e={};function ze(t,e){if(Array.isArray(e))return Ye(e);let o=_e[t];return!o||o.length===0?null:Ye(o)}function Ye(t){if(!Array.isArray(t)||t.length===0)return null;let e=[],o={},n=[];for(let i of t){let r=q(i);if(!r){
|
|
46
|
-
`)}function
|
|
47
|
-
`)}function
|
|
48
|
-
`)}function
|
|
49
|
-
`)}function
|
|
50
|
-
`)}function
|
|
51
|
-
`)}function
|
|
52
|
-
`).map((d,
|
|
53
|
-
`);s.push(` graph.addConditionalEdges('${c.source}', ${
|
|
54
|
-
`)}function
|
|
45
|
+
`)}`}ie();F();var _e={};function ze(t,e){if(Array.isArray(e))return Ye(e);let o=_e[t];return!o||o.length===0?null:Ye(o)}function Ye(t){if(!Array.isArray(t)||t.length===0)return null;let e=[],o={},n=[];for(let i of t){let r=q(i);if(!r){_.warn(`[workflow] unknown skill "${i}" \u2014 skipping`);continue}n.push(i);for(let s of r.tools||[])e.push({name:s.name,description:s.description,input_schema:s.input_schema||{type:"object",properties:{}}});if(!o[r.serverName])if(typeof r.resolve=="function"){let s=r.resolve();s&&(o[r.serverName]={...s,toolPrefix:i})}else{let s={};for(let a of r.envKeys||[]){let c=process.env[a];c&&(s[a]=c)}o[r.serverName]={command:r.command,args:[...r.args||[]],env:s,toolPrefix:i}}}return n.length===0?null:{toolIds:n,claudeTools:e,mcpServers:o}}F();function Wo(t,e={}){let{nodes:o,edges:n,nodeConfigs:i={}}=t;if(!Array.isArray(o)||o.length===0)throw new M("Graph must have at least one node");if(!Array.isArray(n))throw new M("Graph edges must be an array");let r=new ue(e);e.stateSchema&&r.setStateSchema(e.stateSchema);let s=new Set,a=new Map,c={};for(let p of o){let h=Ie(p);a.set(p.id,{...p,resolvedType:h}),h==="decision"&&s.add(p.id)}for(let[p,h]of a){if(s.has(p))continue;let m=h.resolvedType,y=i[p]||{},E=ze(m,y.tools);E&&(c[p]=E);let I={};y.prompt&&(I.prompt=y.prompt);let f=we(m);if(_.debug(`[workflow] compiler: node "${p}" type="${m}" registered=${f}`),y.customCode&&!f)r.addNode(p,zt(p,y.customCode,y),I),r.setNodeType(p,m);else if(f){let $=He(m);$.factory?r.addNode(p,$.create(p,{...y,resolvedTools:E}),I):r.addNode(p,$,I),r.setNodeType(p,m)}else if(y.executeCode)r.addNode(p,zt(p,y.executeCode,y),I),r.setNodeType(p,m);else throw new M(`Unknown node type "${m}" for node "${p}". Did you forget to register it?`)}r.resolvedToolsMap=c;let u=new Set;for(let p of n)s.has(p.target)||u.add(p.target);let d=o.find(p=>!s.has(p.id)&&!u.has(p.id));if(!d)throw new M("Could not determine entry point: no node without incoming edges found");r.setEntryPoint(d.id);let l=Jo(n,"source");for(let p of n)if(!s.has(p.source))if(s.has(p.target)){let h=p.target,m=l.get(h)||[];if(m.length===0)throw new M(`Decision node "${h}" has no outgoing edges`);let y=Yo(h,m,s);r.addConditionalEdges(p.source,y)}else r.addEdge(p.source,p.target);return r}function Go(t){let e=[];if(!t||typeof t!="object")return{valid:!1,errors:["Config must be a non-null object"]};if((!Array.isArray(t.nodes)||t.nodes.length===0)&&e.push("Graph must have at least one node"),Array.isArray(t.edges)||e.push("Graph edges must be an array"),e.length>0)return{valid:!1,errors:e};let o=t.nodeConfigs||{};for(let a of t.nodes){let c=Ie(a);if(c==="decision"||we(c))continue;let u=o[a.id]||{};u.customCode||u.executeCode||e.push(`Unknown node type "${c}" for node "${a.id}". Register it or provide customCode/executeCode.`)}let n=new Set(t.nodes.map(a=>a.id));for(let a of t.edges)n.has(a.source)||e.push(`Edge references unknown source node "${a.source}"`),n.has(a.target)||e.push(`Edge references unknown target node "${a.target}"`);let i=new Set(t.nodes.filter(a=>Ie(a)==="decision").map(a=>a.id)),r=new Set;for(let a of t.edges)i.has(a.target)||r.add(a.target);let s=t.nodes.filter(a=>!i.has(a.id)&&!r.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=t.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 Ho(t){return!t||!Array.isArray(t.nodes)?[]:t.nodes.filter(e=>Ie(e)!=="decision").map(e=>e.id)}function Ie(t){let e=t.data?.nodeType||t.data?.type||t.type;return e==="workflowNode"||e==="custom"||e==="default"?t.id:e}function Jo(t,e){let o=new Map;for(let n of t){let i=n[e];o.has(i)||o.set(i,[]),o.get(i).push(n)}return o}function Yo(t,e,o){let n=e.find(a=>a.data?.conditionalCode||a.conditionalCode);if(!n)throw new M(`Decision node "${t}" has no conditionalCode on its outgoing edges`);let i=n.data?.conditionalCode||n.conditionalCode,r=new Set(e.map(a=>a.target).filter(a=>!o.has(a))),s;try{let c=new Function(`return (${i})`)();s=u=>{let d=c(u);return r.has(d)||_.warn(`[workflow] conditional route from "${t}" returned "${d}" which is not in valid targets: ${[...r].join(", ")}`),d}}catch(a){throw new M(`Failed to compile conditionalCode for "${t}": ${a.message}`)}return s}function zt(t,e,o={}){let n;try{n=new Function("invokeAgent","require","console",`return (${e})`)}catch(s){throw new M(`Failed to compile customCode for node "${t}": ${s.message}`)}let i=n(async(...s)=>{let{invokeAgent:a}=await Promise.resolve().then(()=>(X(),ae));return a(...s)},typeof $e<"u"?$e:void 0,console),r=null;return o.outputSchema&&(r=o.outputSchema.jsonSchema||o.outputSchema),{name:t,_isCustomCode:!0,outputSchema:r,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 M=class extends Error{constructor(e){super(e),this.name="CompilationError"}};ie();Oe();X();function zo(t,e={}){let{nodes:o,edges:n,nodeConfigs:i={}}=t,r=new Set,s=[],a=new Map;for(let E of o){let I=E.data?.nodeType||E.type;a.set(E.id,I),I==="decision"?r.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:u,toolIdsByVar:d}=tn(s,i),{simpleEdges:l,conditionalEdges:p}=on(n,r),h=nn(s,n,r),m=[],y=e.workflowType||"workflow";return m.push(Ko(e)),m.push(Vo(y,{usesRegisteredNodes:c})),m.push(qo(d)),m.push(Xo(y)),m.push(Qo(s,i)),m.push(en(s,h,l,p,u,y)),m.filter(Boolean).join(`
|
|
46
|
+
`)}function Zo(t){let e={};for(let[o,n]of Object.entries(t)){let{tools:i,...r}=n;Object.keys(r).length>0&&(e[o]=r)}return e}function Ko(t){let e=t.workflowType||"workflow";return["// Generated workflow",`// ${t.projectId?`Project: ${t.projectId} | `:""}Type: ${e} | Version: ${t.version??0}`,`// Downloaded: ${new Date().toISOString()}`,""].join(`
|
|
47
|
+
`)}function Vo(t,{usesRegisteredNodes:e=!0}={}){let o=["import { WorkflowGraph, invokeAgent, getResolvedToolDefinitions } from '@zibby/agent-workflow';"];return e&&o.push("// import './register-nodes.js'; // register custom node types here"),o.push("import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'node:fs';","import { join, dirname } from 'node:path';","import { fileURLToPath } from 'node:url';",""),o.join(`
|
|
48
|
+
`)}function qo(t){if(t.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[o,n]of t)e.push(`const ${o} = getResolvedToolDefinitions(${JSON.stringify(n)}); // ${n.join(", ")}`);return e.push(""),e.join(`
|
|
49
|
+
`)}function Xo(t){return["// \u2500\u2500 Node Configs \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500","const __filename = fileURLToPath(import.meta.url);","const __dirname = dirname(__filename);",`const configPath = join(__dirname, 'workflow-${t}.config.json');`,"const nodeConfigs = existsSync(configPath) ? JSON.parse(readFileSync(configPath, 'utf-8')) : {};",""].join(`
|
|
50
|
+
`)}function Qo(t,e){let o=["// \u2500\u2500 Node Implementations \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500",""];for(let n of t){let i=Zt(n.id),r=e[n.id]?.customCode;if(r)o.push(`// @custom \u2014 modified from default "${n.nodeType}" template`),o.push(`const ${i}_execute = ${r};`);else{let s=Je(n.nodeType);s?(o.push(`// Default "${n.nodeType}" implementation`),o.push(`const ${i}_execute = ${s};`)):(o.push(`// No template for "${n.nodeType}" \u2014 passthrough`),o.push(`const ${i}_execute = async (state) => ({ success: true, output: {}, raw: null });`))}o.push("")}return o.join(`
|
|
51
|
+
`)}function en(t,e,o,n,i,r){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 t){let u=Zt(c.id);s.push(` graph.addNode('${c.id}', { name: '${c.id}', execute: ${u}_execute });`),s.push(` graph.setNodeType('${c.id}', '${c.nodeType}');`)}s.push("",` graph.setEntryPoint('${e}');`,""),(o.length>0||n.length>0)&&s.push(" // Edges");for(let c of o)s.push(` graph.addEdge('${c.source}', '${c.target}');`);for(let c of n){let u=c.code.split(`
|
|
52
|
+
`).map((d,l)=>l===0?d:` ${d}`).join(`
|
|
53
|
+
`);s.push(` graph.addConditionalEdges('${c.source}', ${u});`)}let a=[];for(let c of t){let u=i.get(c.id);u&&a.push(` '${c.id}': ${u},`)}return a.length>0&&s.push(""," graph.resolvedToolsMap = {",...a," };"),s.push(""," return graph;","}",""),s.push("export { nodeConfigs };",""),s.join(`
|
|
54
|
+
`)}function tn(t,e){let o=new Map,n=new Map;for(let i of t){let r=e[i.id]?.tools,s;if(Array.isArray(r)&&r.length>0)s=[...r].sort();else{let a=_e[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`;o.set(i.id,a),n.has(a)||n.set(a,s)}}return{toolsPerNode:o,toolIdsByVar:n}}function on(t,e){let o=[],n=[],i=new Map,r=new Set;for(let s of t)i.has(s.source)||i.set(s.source,[]),i.get(s.source).push(s);for(let s of t)if(!e.has(s.source))if(e.has(s.target)){if(r.has(s.target))continue;r.add(s.target);let c=(i.get(s.target)||[]).find(u=>u.data?.conditionalCode||u.conditionalCode);c&&n.push({source:s.source,code:c.data?.conditionalCode||c.conditionalCode})}else o.push({source:s.source,target:s.target});return{simpleEdges:o,conditionalEdges:n}}function nn(t,e,o){let n=new Set;for(let r of e)o.has(r.target)||n.add(r.target);let i=t.find(r=>!n.has(r.id));return i?i.id:t[0]?.id}function Zt(t){return t.replace(/[^a-zA-Z0-9]/g,"_")}F();export{Ne as AgentStrategy,Ae as CI_ENV_VARS,M as CompilationError,Q as ConditionalNode,le as ContextLoader,se as DEFAULT_OUTPUT_BASE,po as EVENTS_FILE,_e as NODE_DEFAULT_TOOLS,j as Node,oe as OutputParser,uo as RAW_OUTPUT_FILE,lo as RESULT_FILE,xe as SESSIONS_DIR,W as SESSION_INFO_FILE,fo as SKILLS,ke as STOP_REQUEST_FILE,io as SchemaTypes,me as Timeline,pt as WORKFLOW_GRAPH_LOG_MARKER_PREFIX,ue as WorkflowGraph,te as WorkflowState,Wt as clearInheritedSessionEnvForFreshRun,St as clearSkills,Wo as compileGraph,Fe as dispatchSubgraph,Ho as extractSteps,Zo as generateNodeConfigsJson,zo as generateWorkflowCode,Ht as generateWorkflowSessionId,Re as getAgentStrategy,gt as getAllSkills,He as getNodeImpl,Je as getNodeTemplate,Ye as getResolvedToolDefinitions,q as getSkill,we as hasNode,ht as hasSkill,It as invokeAgent,Fo as listNodeTypes,mt as listSkillIds,_t as listStrategies,Ut as readPinnedSessionPathFromEnv,Yt as registerNode,ft as registerSkill,wt as registerStrategy,ze as resolveNodeTools,Jt as resolveWorkflowSession,ao as setLogger,Ft as shouldTrustInheritedSessionEnv,Gt as syncProcessEnvToSession,k as timeline,Go as validateGraphConfig};
|