@zibby/agent-workflow 0.4.2 → 0.4.3
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/exec-context.d.ts +53 -0
- package/dist/exec-context.js +1 -0
- package/dist/graph-compiler.js +17 -17
- package/dist/graph.js +19 -19
- package/dist/in-process-subgraph.d.ts +59 -0
- package/dist/in-process-subgraph.js +1 -0
- package/dist/index.js +37 -37
- package/dist/sub-graph-executor.js +1 -1
- package/dist/subgraph-registry.d.ts +48 -0
- package/dist/subgraph-registry.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Read the active execution context. Returns a frozen object so callers
|
|
3
|
+
* can't mutate the live store (use `runInContext` to push a child scope).
|
|
4
|
+
*
|
|
5
|
+
* @returns {{
|
|
6
|
+
* executionId: string | null,
|
|
7
|
+
* parentExecutionId: string | null,
|
|
8
|
+
* depth: number,
|
|
9
|
+
* conversationId: string | null,
|
|
10
|
+
* dispatchMode: 'cold'|'warm'|'inprocess'|null,
|
|
11
|
+
* }}
|
|
12
|
+
*/
|
|
13
|
+
export function getExecContext(): {
|
|
14
|
+
executionId: string | null;
|
|
15
|
+
parentExecutionId: string | null;
|
|
16
|
+
depth: number;
|
|
17
|
+
conversationId: string | null;
|
|
18
|
+
dispatchMode: "cold" | "warm" | "inprocess" | null;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Run `fn` with a fresh execution context. Nests cleanly: a child
|
|
22
|
+
* scope's depth is parent.depth + 1, parentExecutionId is parent.executionId.
|
|
23
|
+
*
|
|
24
|
+
* Pass partial fields — `runInContext({ executionId: childId }, fn)` reuses
|
|
25
|
+
* the surrounding context for everything else and bumps depth automatically.
|
|
26
|
+
*
|
|
27
|
+
* @template T
|
|
28
|
+
* @param {{
|
|
29
|
+
* executionId: string,
|
|
30
|
+
* parentExecutionId?: string | null,
|
|
31
|
+
* conversationId?: string | null,
|
|
32
|
+
* dispatchMode?: 'cold'|'warm'|'inprocess'|null,
|
|
33
|
+
* }} ctx
|
|
34
|
+
* @param {() => T | Promise<T>} fn
|
|
35
|
+
* @returns {Promise<T> | T}
|
|
36
|
+
*/
|
|
37
|
+
export function runInContext<T>(ctx: {
|
|
38
|
+
executionId: string;
|
|
39
|
+
parentExecutionId?: string | null;
|
|
40
|
+
conversationId?: string | null;
|
|
41
|
+
dispatchMode?: "cold" | "warm" | "inprocess" | null;
|
|
42
|
+
}, fn: () => T | Promise<T>): Promise<T> | T;
|
|
43
|
+
/**
|
|
44
|
+
* Synchronously initialize the root execution context. Use this at the
|
|
45
|
+
* very top of the CLI entrypoint — `runInContext` is the preferred call
|
|
46
|
+
* for any nested scope, but the root needs a way to enter the ALS once
|
|
47
|
+
* without nesting inside another `run()`.
|
|
48
|
+
*
|
|
49
|
+
* Internally identical to `runInContext`, exposed separately to make
|
|
50
|
+
* the entrypoint code read naturally and to document the "top-level"
|
|
51
|
+
* intent.
|
|
52
|
+
*/
|
|
53
|
+
export function withRootContext(ctx: any, fn: any): any;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{AsyncLocalStorage as d}from"node:async_hooks";var t=new d;function c(){let e=t.getStore();return e||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 i(e,o){let n=t.getStore()||c(),r=Object.freeze({executionId:e.executionId,parentExecutionId:e.parentExecutionId??n.executionId??null,depth:(n.depth||0)+(e.executionId!==n.executionId?1:0),conversationId:e.conversationId!==void 0?e.conversationId:n.conversationId??null,dispatchMode:e.dispatchMode??null});return t.run(r,o)}function I(e,o){return t.run(Object.freeze({executionId:e.executionId,parentExecutionId:e.parentExecutionId??null,depth:0,conversationId:e.conversationId??null,dispatchMode:e.dispatchMode??"cold"}),o)}export{c as getExecContext,i as runInContext,I as withRootContext};
|
package/dist/graph-compiler.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
var
|
|
1
|
+
var At=Object.defineProperty;var he=(o=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(o,{get:(e,t)=>(typeof require<"u"?require:e)[t]}):o)(function(o){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+o+'" is not supported')});var re=(o,e)=>()=>(o&&(e=o(o=0)),e);var Ue=(o,e)=>{for(var t in e)At(o,t,{get:e[t],enumerable:!0})};var Fe,Nt,ie,_,L=re(()=>{Fe=()=>{},Nt={debug:Fe,info:Fe,warn:(...o)=>console.warn("[workflow]",...o),error:(...o)=>console.error("[workflow]",...o)},ie={impl:Nt},_={debug:(...o)=>ie.impl.debug?.(...o),info:(...o)=>ie.impl.info?.(...o),warn:(...o)=>ie.impl.warn?.(...o),error:(...o)=>ie.impl.error?.(...o)}});var Xe=re(()=>{});var Qe={};Ue(Qe,{clearSkills:()=>Lt,getAllSkills:()=>Mt,getSkill:()=>K,hasSkill:()=>Bt,listSkillIds:()=>Dt,registerSkill:()=>Rt});function Rt(o){if(!o||typeof o.id!="string")throw new Error("Skill definition must include a string id");Y.set(o.id,Object.freeze({...o}))}function K(o){return Y.get(o)||null}function Bt(o){return Y.has(o)}function Mt(){return new Map(Y)}function Dt(){return Array.from(Y.keys())}function Lt(){Y.clear()}var ye,Y,ce=re(()=>{ye=Symbol.for("@zibby/agent-workflow.skills");globalThis[ye]||(globalThis[ye]=new Map);Y=globalThis[ye]});var q={};Ue(q,{getAgentStrategy:()=>et,invokeAgent:()=>Ft,listStrategies:()=>Ut,registerStrategy:()=>jt});function jt(o){if(!o||typeof o.getName!="function"||typeof o.invoke!="function")throw new Error("strategy must implement getName() and invoke() (AgentStrategy shape)");let e=j.findIndex(t=>t.getName()===o.getName());e>=0?j[e]=o:j.push(o)}function Ut(){return j.map(o=>o.getName())}function et(o={}){let{state:e={},preferredAgent:t=null}=o,n=t||e.agentType||process.env.AGENT_TYPE;if(!n){let s=j.map(a=>a.getName()).join(", ")||"none registered";throw new Error(`No agent specified. Set agentType in state or AGENT_TYPE env var. Available: ${s}`)}_.debug(`[workflow] agent selection: requested=${n}`);let r=j.find(s=>s.getName()===n);if(!r){let s=j.map(a=>a.getName()).join(", ")||"none registered";throw new Error(`Unknown agent '${n}'. Available: ${s}`)}if(!r.canHandle(o))throw new Error(`Agent '${n}' is not available in this environment. Check credentials/environment.`);return _.debug(`[workflow] using agent: ${r.getName()}`),r}async function Ft(o,e={},t={}){let n=et(e),r=e.state?.config||t.config||{},s=r.models||{},a=t.nodeName&&s[t.nodeName]||null,i=s.default||null,c=r.agent?.[n.name]?.model||null,u=a||i||c||t.model||null,d={...t,model:u,workspace:e.state?.workspace||t.workspace,schema:t.schema||e.schema,images:t.images||e.images||[],skills:t.skills||e.skills||[],config:r},f=o,l=d.skills||[];if(l.length>0&&!t.skipPromptFragments){let S=l.map(g=>{let E=K(g)?.promptFragment;return typeof E=="function"?E():E}).filter(Boolean);S.length>0&&(f+=`
|
|
2
2
|
|
|
3
|
-
${
|
|
3
|
+
${S.join(`
|
|
4
4
|
|
|
5
5
|
`)}`)}let m=e.state?._currentNodeConfig?.extraPromptInstructions?.trim();return m&&(f+=`
|
|
6
6
|
|
|
@@ -9,37 +9,37 @@ PRIORITY OVERRIDE \u2014 THE FOLLOWING INSTRUCTIONS TAKE PRECEDENCE OVER ALL PRE
|
|
|
9
9
|
\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501
|
|
10
10
|
|
|
11
11
|
${m}
|
|
12
|
-
`),
|
|
12
|
+
`),_.debug(`[workflow] prompt length: ${f.length} chars`),n.invoke(f,d)}var _e,j,V=re(()=>{Xe();L();ce();_e=Symbol.for("@zibby/agent-workflow.strategies");globalThis[_e]||(globalThis[_e]=[]);j=globalThis[_e]});var Ot=new Set(["__proto__","constructor","prototype"]);function ge(o){if(Ot.has(o))throw new Error(`Invalid state key: "${o}"`)}var ne=class{constructor(e={}){this._state=Object.create(null),Object.assign(this._state,{messages:[],errors:[],artifacts:{},metadata:{},...e}),this._history=[]}get(e){return this._state[e]}set(e,t){ge(e),this._history.push({...this._state}),this._state[e]=t}update(e){let t=Object.getOwnPropertyNames(e);for(let n of t)ge(n);this._history.push({...this._state});for(let n of t)this._state[n]=e[n]}append(e,t){ge(e),this._history.push({...this._state}),Array.isArray(this._state[e])||(this._state[e]=[]),this._state[e].push(t)}getAll(){return{...this._state}}rollback(){this._history.length>0&&(this._state=this._history.pop())}};var se=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 n=[e.match(/\{[\s\S]*?\}/),e.match(/\{[\s\S]*\}/)].filter(Boolean).map(r=>r[0]);for(let r of n)try{return this.validate(JSON.parse(r))}catch(s){if(!(s instanceof SyntaxError))throw s}return this.validate({result:e.trim()})}validate(e){let t=[];for(let[n,r]of Object.entries(this.schema)){if(r.required&&!(n in e)&&t.push(`Missing required field: ${n}`),n in e&&r.type){let s=typeof e[n];s!==r.type&&t.push(`Field '${n}' expected ${r.type}, got ${s}`)}if(r.validate&&n in e){let s=r.validate(e[n]);s&&t.push(`Field '${n}': ${s}`)}}if(t.length>0)throw new Error(`Output validation failed:
|
|
13
13
|
${t.join(`
|
|
14
|
-
`)}`);return e}};
|
|
14
|
+
`)}`);return e}};L();import{writeFileSync as Ie,readFileSync as tt,existsSync as ot,mkdirSync as Wt}from"node:fs";import{join as Ee,dirname as Gt}from"node:path";import v from"chalk";var Pt="__WORKFLOW_GRAPH_LOG__",z=v.gray("\u2502"),Ct=v.gray("\u250C"),We=v.gray("\u2514"),me=v.green("\u25C6"),Ge=v.hex("#c084fc")("\u25C6"),He=v.hex("#2dd4bf")("\u25C6"),Se=v.red("\u25C6"),Je=`${z} `,Ye=2;function Ze(o){return o<1e3?`${o}ms`:`${(o/1e3).toFixed(1)}s`}function ze(o,e){return(t,n,r)=>{if(typeof t!="string")return o(t,n,r);let s=process.stdout.columns||120,a="";for(let i=0;i<t.length;i++){let c=t[i];e.lineStart&&(a+=Je,e.col=Ye,e.lineStart=!1),c===`
|
|
15
15
|
`?(a+=c,e.lineStart=!0,e.col=0,e.inEsc=!1):c==="\x1B"?(e.inEsc=!0,a+=c):e.inEsc?(a+=c,(c>="A"&&c<="Z"||c>="a"&&c<="z")&&(e.inEsc=!1)):(e.col++,a+=c,e.col>=s&&(a+=`
|
|
16
|
-
${
|
|
16
|
+
${Je}`,e.col=Ye))}return o(a,n,r)}}var we=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=ze(this._origStdoutWrite,e),process.stderr.write=ze(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=`${Pt}${JSON.stringify(e)}
|
|
20
20
|
`;this._origStdoutWrite?this._origStdoutWrite(t):process.stdout.write(t)}_writeDot(e,t){this._origStdoutWrite?(this._outState&&!this._outState.lineStart&&(this._origStdoutWrite(`
|
|
21
21
|
`),this._outState.lineStart=!0,this._outState.col=0),this._origStdoutWrite(`${e} ${t}
|
|
22
22
|
`)):process.stdout.write.bind(process.stdout)(`${e} ${t}
|
|
23
|
-
`)}step(e){this._origStdoutWrite?this._writeDot(
|
|
24
|
-
`)}stepInfo(e){this.step(e)}stepTool(e){this._origStdoutWrite?this._writeDot(
|
|
25
|
-
`)}stepMemory(e){let t=
|
|
26
|
-
`)}stepFail(e){this._origStdoutWrite?this._writeDot(
|
|
27
|
-
`)}nodeStart(e){this._currentNode=e,this._emitGraphLogMarker({phase:"node_begin",node:e}),this._rawWrite(`${
|
|
23
|
+
`)}step(e){this._origStdoutWrite?this._writeDot(me,e):process.stdout.write.bind(process.stdout)(`${z} ${me} ${e}
|
|
24
|
+
`)}stepInfo(e){this.step(e)}stepTool(e){this._origStdoutWrite?this._writeDot(Ge,e):process.stdout.write.bind(process.stdout)(`${z} ${Ge} ${e}
|
|
25
|
+
`)}stepMemory(e){let t=v.hex("#2dd4bf")(e);this._origStdoutWrite?this._writeDot(He,t):process.stdout.write.bind(process.stdout)(`${z} ${He} ${t}
|
|
26
|
+
`)}stepFail(e){this._origStdoutWrite?this._writeDot(Se,v.red(e)):process.stdout.write.bind(process.stdout)(`${z} ${Se} ${v.red(e)}
|
|
27
|
+
`)}nodeStart(e){this._currentNode=e,this._emitGraphLogMarker({phase:"node_begin",node:e}),this._rawWrite(`${Ct} ${e}`),this._startIntercepting()}nodeComplete(e,t={}){this._stopIntercepting();let{duration:n,details:r}=t;if(r)for(let a of r)this._rawWrite(`${me} ${a}`);let s=n?v.dim(` ${Ze(n)}`):"";this._rawWrite(`${We} ${v.green("done")}${s}`),this._emitGraphLogMarker({phase:"node_end",node:e}),this._rawWrite("")}nodeFailed(e,t,n={}){this._stopIntercepting();let{duration:r}=n,s=r?v.dim(` ${Ze(r)}`):"";this._rawWrite(`${Se} ${v.red(t)}`),this._rawWrite(`${We} ${v.red("failed")}${s}`),this._emitGraphLogMarker({phase:"node_end",node:e}),this._rawWrite("")}route(e,t){this._rawWrite(v.dim(` ${e} \u2192 ${t}`)),this._rawWrite("")}graphComplete(){}},x=new we;var ae=".zibby/output",Ke="sessions",J=".session-info.json",qe=".zibby-stop";var Ve=["CI_JOB_ID","GITHUB_RUN_ID","CIRCLE_WORKFLOW_ID","BUILD_ID"];var D=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 se(e.outputSchema):null,this.retries=e.retries||0,this.onComplete=e.onComplete,this.customExecute=e.execute}async execute(e,t){let n=()=>t&&typeof t.getAll=="function"?t.getAll():e,r=f=>t&&typeof t.get=="function"?t.get(f):e?.[f];if(typeof this.customExecute=="function"){_.debug(`[workflow] node '${this.name}': custom execute (skipping LLM)`);try{let f=await this.customExecute(e);return typeof f=="object"&&f!==null&&f.success===!1?{success:!1,error:f.error||"Node execution failed",raw:f.raw||null}:this.isZodSchema?(_.debug(`[workflow] node '${this.name}': validating output schema`),{success:!0,output:this.outputSchema.parse(f),raw:null}):{success:!0,output:f,raw:null}}catch(f){return _.error(`[workflow] node '${this.name}' failed: ${f.message}`),f.name==="ZodError"&&_.error(`Schema errors: ${JSON.stringify(f.issues||f.errors,null,2)}`),{success:!1,error:f.message,raw:null}}}let s=typeof this.prompt=="function"?this.prompt(n()):this.prompt,a=r("_skillHints");a&&(s=`${a}
|
|
28
28
|
|
|
29
|
-
${s}`);let n=r(),c=n.cwd||process.cwd(),l=n.sessionPath;try{if(l){let f=we(l,G);if(Ke(f)){let m=JSON.parse(He(f,"utf-8"));m.currentNode=this.name,Se(f,JSON.stringify(m,null,2),"utf-8")}let u=we(l,"..",G);if(Ke(u))try{let m=JSON.parse(He(u,"utf-8"));m.currentNode=this.name,Se(u,JSON.stringify(m,null,2),"utf-8")}catch{}}}catch(f){E.debug(`[workflow] could not update session info: ${f.message}`)}let d=null;for(let f=0;f<=this.retries;f++)try{E.debug(`[workflow] node '${this.name}' attempt ${f}`);let u=r().config||{},m=u.agents||{},w=this.config.agent??m[this.name]??null,h={state:r()};w&&(h.preferredAgent=w);let $={workspace:c,schema:this.isZodSchema?this.outputSchema:null,skills:this.config.skills||[],sessionPath:l,config:u,nodeName:this.name,timeout:this.config?.timeout||3e5},v=e?._coreInvokeAgent;v||(v=(await Promise.resolve().then(()=>(q(),K))).invokeAgent);let p=await v(s,h,$),y,b;if(typeof p=="string"?(y=p,b=null):p.structured?(y=p.raw||JSON.stringify(p.structured,null,2),b=p.structured):(y=p.raw||JSON.stringify(p,null,2),b=p.extracted||null),l)try{let g=we(l,this.name,"raw_stream_output.txt");Tt(kt(g),{recursive:!0}),Se(g,typeof y=="string"?y:JSON.stringify(y),"utf-8")}catch(g){E.debug(`[workflow] could not save raw output: ${g.message}`)}if(this.isZodSchema&&b){E.info(`[workflow] node '${this.name}': output validated: ${JSON.stringify(b,null,2)}`);let g=b;if(typeof this.onComplete=="function")try{g=await this.onComplete(r(),b)}catch(C){E.warn(`[workflow] onComplete hook failed: ${C.message}`)}return{success:!0,output:g,raw:y}}if(typeof this.onComplete=="function")try{return{success:!0,output:await this.onComplete(r(),{raw:y}),raw:y}}catch(g){throw new Error(`onComplete failed: ${g.message}`,{cause:g})}if(this.parser){let g=this.parser.parse(y);return E.info(`[workflow] node '${this.name}': parsed output: ${JSON.stringify(g,null,2)}`),k.step("Output parsed"),{success:!0,output:g,raw:y}}return{success:!0,output:y,raw:y}}catch(u){d=u,f<this.retries&&E.info(`[workflow] node '${this.name}' failed, retrying (${f+1}/${this.retries})\u2026`)}return{success:!1,error:d.message,raw:null}}},ne=class extends B{constructor(e){super({...e,_isCustomCode:!0}),this.condition=e.condition}async execute(e,t){let r=t&&typeof t.getAll=="function"?t.getAll():e;return{success:!0,output:{nextNode:this.condition(r)},raw:null}}};U();var At=2e3,xt=600*1e3,Nt=new Set(["completed","failed","canceled","timeout"]);function Ot(){let o=process.env.PROGRESS_API_URL;if(!o)throw new Error("Sub-graph dispatch requires PROGRESS_API_URL env var (set automatically on cloud runs). Sub-graphs are not supported in local in-process runs yet \u2014 deploy the parent and child to cloud.");return o.replace(/\/executions\/?$/,"")}function Pt(){let o=process.env.PROJECT_ID;if(!o)throw new Error("Sub-graph dispatch requires PROJECT_ID env var.");return o}function Ct(){let o=process.env.PROJECT_API_TOKEN;if(!o)throw new Error("Sub-graph dispatch requires PROJECT_API_TOKEN env var.");return o}function Rt(){return process.env.EXECUTION_ID||null}function Mt(o,e){return e==null?o:typeof e=="function"?e(o):typeof e=="string"?e.split(".").reduce((t,r)=>t==null?t:t[r],o):o}async function qe(o,e={}){if(!o||typeof o!="string")throw new Error("dispatchSubgraph: workflowName (string) is required");let t=Ot(),r=Pt(),i=Ct(),s=Rt(),a=`${t}/projects/${encodeURIComponent(r)}/workflows/${encodeURIComponent(o)}/trigger`,n={input:e.input||{},...s?{parentExecutionId:s}:{},...e.conversationId?{conversationId:e.conversationId}:{}};E.info(`[sub-graph] dispatching '${o}' (${e.async?"async":"sync"}) from parent ${s||"<none>"}`);let c=await fetch(a,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${i}`},body:JSON.stringify(n)});if(!c.ok){let p=null,y="";try{p=await c.json(),y=p?.error||p?.message||JSON.stringify(p)}catch{y=await c.text().catch(()=>"")}if(c.status===429){let g=p?.quotaInfo||{},C=new Error(`Sub-graph '${o}' blocked by execution quota (${g.used??"?"}/${g.limit??"?"} on plan ${g.planId||"unknown"}). Sub-workflow runs count toward the same monthly cap as user-triggered runs.`);throw C.code="SUBGRAPH_QUOTA_EXCEEDED",C.status=429,C.subgraph=o,C.quotaInfo=g,C}if(c.status===400){let g=new Error(`Sub-graph '${o}' rejected input: ${y}`);throw g.code="SUBGRAPH_INVALID_INPUT",g.status=400,g.subgraph=o,g.validationErrors=p?.validationErrors||null,g.missing=p?.missing||null,g}let b=new Error(`Sub-graph '${o}' trigger rejected (${c.status}): ${y}`);throw b.code="SUBGRAPH_TRIGGER_FAILED",b.status=c.status,b.subgraph=o,b}let l=await c.json(),d=l?.data?.jobId||l?.jobId;if(!d)throw new Error(`Sub-graph '${o}' trigger returned no jobId: ${JSON.stringify(l).slice(0,200)}`);if(e.async)return E.info(`[sub-graph] async dispatch of '${o}' \u2192 jobId=${d} (not waiting)`),{jobId:d,status:"accepted",workflow:o};let f=Number.isFinite(e.timeoutMs)?e.timeoutMs:xt,u=Number.isFinite(e.pollIntervalMs)?e.pollIntervalMs:At,m=`${t}/executions/${encodeURIComponent(d)}`,w=Date.now()+f,h="accepted",$=0;for(;Date.now()<w;){await new Promise(g=>setTimeout(g,u)),$+=1;let p=await fetch(m,{headers:{Authorization:`Bearer ${i}`}});if(!p.ok){if(p.status>=500){E.warn(`[sub-graph] status poll for ${d} returned ${p.status}, will retry`);continue}throw new Error(`Sub-graph status poll failed for ${d}: ${p.status}`)}let y=await p.json(),b=y?.data||y?.execution||y;if(h=b?.status||h,Nt.has(h)){if(h!=="completed"){let D=new Error(`Sub-graph '${o}' (${d}) ended in status '${h}'`);throw D.subgraphJobId=d,D.subgraphStatus=h,D}let g=b?.finalState||b?.state||{},C=Mt(g,e.output);return E.info(`[sub-graph] '${o}' (${d}) completed after ${$} polls`),C}}let v=new Error(`Sub-graph '${o}' (${d}) timed out after ${Math.round(f/1e3)}s (last status: ${h})`);throw v.subgraphJobId=d,v.subgraphStatus=h,v}import{existsSync as ze,readFileSync as Bt}from"node:fs";import{join as ye,dirname as Ve}from"node:path";var ie=class{static async loadContext(e,t,r={}){let i={},s=r.filenames||["CONTEXT.md","AGENTS.md"];if(e){let n=Ve(ye(t,e));for(let c of s){let l=await this.findAndMergeContextFiles(c,n,t);if(l){let d=c.replace(/\.[^.]+$/,"").toLowerCase();i[d]=l}}}let a=r.discovery||{};for(let[n,c]of Object.entries(a))try{let l=ye(t,c);ze(l)&&(i[n]=await this.loadFile(l))}catch(l){console.warn(`[workflow] could not load context '${n}' from '${c}': ${l.message}`)}return i}static async findAndMergeContextFiles(e,t,r){let i=[],s=t;for(;s.startsWith(r);){let a=ye(s,e);if(ze(a))try{i.unshift(await this.loadFile(a))}catch(c){console.warn(`[workflow] could not load ${e} from ${a}: ${c.message}`)}let n=Ve(s);if(n===s)break;s=n}return i.length===0?null:i.every(a=>typeof a=="string")?i.join(`
|
|
29
|
+
${s}`);let i=n(),c=i.cwd||process.cwd(),u=i.sessionPath;try{if(u){let f=Ee(u,J);if(ot(f)){let m=JSON.parse(tt(f,"utf-8"));m.currentNode=this.name,Ie(f,JSON.stringify(m,null,2),"utf-8")}let l=Ee(u,"..",J);if(ot(l))try{let m=JSON.parse(tt(l,"utf-8"));m.currentNode=this.name,Ie(l,JSON.stringify(m,null,2),"utf-8")}catch{}}}catch(f){_.debug(`[workflow] could not update session info: ${f.message}`)}let d=null;for(let f=0;f<=this.retries;f++)try{_.debug(`[workflow] node '${this.name}' attempt ${f}`);let l=n().config||{},m=l.agents||{},S=this.config.agent??m[this.name]??null,g={state:n()};S&&(g.preferredAgent=S);let E={workspace:c,schema:this.isZodSchema?this.outputSchema:null,skills:this.config.skills||[],sessionPath:u,config:l,nodeName:this.name,timeout:this.config?.timeout||3e5},T=e?._coreInvokeAgent;T||(T=(await Promise.resolve().then(()=>(V(),q))).invokeAgent);let p=await T(s,g,E),w,b;if(typeof p=="string"?(w=p,b=null):p.structured?(w=p.raw||JSON.stringify(p.structured,null,2),b=p.structured):(w=p.raw||JSON.stringify(p,null,2),b=p.extracted||null),u)try{let h=Ee(u,this.name,"raw_stream_output.txt");Wt(Gt(h),{recursive:!0}),Ie(h,typeof w=="string"?w:JSON.stringify(w),"utf-8")}catch(h){_.debug(`[workflow] could not save raw output: ${h.message}`)}if(this.isZodSchema&&b){_.info(`[workflow] node '${this.name}': output validated: ${JSON.stringify(b,null,2)}`);let h=b;if(typeof this.onComplete=="function")try{h=await this.onComplete(n(),b)}catch(R){_.warn(`[workflow] onComplete hook failed: ${R.message}`)}return{success:!0,output:h,raw:w}}if(typeof this.onComplete=="function")try{return{success:!0,output:await this.onComplete(n(),{raw:w}),raw:w}}catch(h){throw new Error(`onComplete failed: ${h.message}`,{cause:h})}if(this.parser){let h=this.parser.parse(w);return _.info(`[workflow] node '${this.name}': parsed output: ${JSON.stringify(h,null,2)}`),x.step("Output parsed"),{success:!0,output:h,raw:w}}return{success:!0,output:w,raw:w}}catch(l){d=l,f<this.retries&&_.info(`[workflow] node '${this.name}' failed, retrying (${f+1}/${this.retries})\u2026`)}return{success:!1,error:d.message,raw:null}}},ue=class extends D{constructor(e){super({...e,_isCustomCode:!0}),this.condition=e.condition}async execute(e,t){let n=t&&typeof t.getAll=="function"?t.getAll():e;return{success:!0,output:{nextNode:this.condition(n)},raw:null}}};L();L();import{mkdirSync as Yt,existsSync as X,statSync as zo}from"node:fs";import{spawn as ct}from"node:child_process";import{join as Q}from"node:path";import{pathToFileURL as Zt}from"node:url";import{AsyncLocalStorage as Ht}from"node:async_hooks";var be=new Ht;function $e(){let o=be.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 rt(o,e){let t=be.getStore()||$e(),n=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 be.run(n,e)}var Te=new Map,ve=new Map,nt=new Map;function st(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");Te.set(o,e),ve.set(o,"ready"),nt.set(o,{...t,cachedAt:Date.now()})}function it(o,e){ve.set(o,"failed"),nt.set(o,{error:e?.message||String(e),failedAt:Date.now()}),Te.delete(o)}function at(o){return ve.get(o)==="ready"?Te.get(o):null}var zt=process.env.ZIBBY_SUBGRAPH_CACHE_DIR||"/tmp/zibby/subgraphs";function Kt(){return Number(process.env.ZIBBY_SUBGRAPH_MAX_DEPTH||10)}function qt(){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 Vt(){let o=(process.env.SUBGRAPH_INTERNAL_URL||"").replace(/\/$/,""),e=(process.env.PROGRESS_API_URL||"").replace(/\/executions\/?$/,""),t=o||e,n=process.env.PROJECT_ID,r=process.env.PROJECT_API_TOKEN;if(!t||!n||!r)throw new k("env","SUBGRAPH_INTERNAL_URL/PROGRESS_API_URL/PROJECT_ID/PROJECT_API_TOKEN missing");return{apiBase:t,projectId:n,authToken:r}}async function Xt({apiBase:o,authToken:e,body:t}){let n;try{n=await fetch(`${o}/internal/subgraph/begin`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${e}`},body:JSON.stringify(t)})}catch(s){throw new k("network",`begin fetch failed: ${s.message}`)}let r=null;try{r=await n.json()}catch{}if(!n.ok){if(n.status===404){let s=new Error(`Sub-graph child '${t.childWorkflowType}' not found in project`);throw s.code="SUBGRAPH_NOT_FOUND",s.status=404,s}if(n.status===429){let s=r?.quotaInfo||{},a=new Error(`Sub-graph blocked by quota (${s.used??"?"}/${s.limit??"?"} on ${s.planId||"plan"})`);throw a.code="SUBGRAPH_QUOTA_EXCEEDED",a.status=429,a.quotaInfo=s,a}if(n.status===400&&r?.validationErrors){let s=new Error(`Sub-graph rejected input: ${r?.error||r?.message||"validation failed"}`);throw s.code="SUBGRAPH_INVALID_INPUT",s.status=400,s.validationErrors=r.validationErrors,s.missing=r.missing,s}throw new k("begin-status",`begin returned ${n.status}`)}return r?.data||r}async function U({apiBase:o,authToken:e,payload:t}){try{let n=await fetch(`${o}/internal/subgraph/finalize`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${e}`},body:JSON.stringify(t)});n.ok||_.warn(`[in-process subgraph] finalize returned ${n.status} for ${t.childExecutionId}`)}catch(n){_.warn(`[in-process subgraph] finalize failed: ${n.message}`)}}async function Qt(o,e){let t=Q(e,".ready"),n=Q(e,"graph.mjs");if(X(t)&&X(n))return;Yt(e,{recursive:!0});let r=Q(e,".lock"),s=!1;try{let{openSync:a,closeSync:i}=await import("node:fs"),c=a(r,"wx");i(c),s=!0}catch(a){if(a.code!=="EEXIST")throw a}if(!s){let a=Date.now()+3e4;for(;Date.now()<a;){if(X(t)&&X(n))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,u)=>{let d=ct("curl",["-fsSL",o],{stdio:["ignore","pipe","inherit"]}),f=ct("tar",["-xzf","-","-C",e],{stdio:["pipe","inherit","inherit"]});d.stdout.pipe(f.stdin);let l,m,S=()=>{if(l!==void 0&&m!==void 0){if(l!==0)return u(new Error(`curl exited ${l}`));if(m!==0)return u(new Error(`tar exited ${m}`));c()}};d.on("close",g=>{l=g,S()}),f.on("close",g=>{m=g,S()}),d.on("error",u),f.on("error",u)});let{writeFileSync:a,unlinkSync:i}=await import("node:fs");a(t,"");try{i(r)}catch{}}catch(a){try{let{unlinkSync:i}=await import("node:fs");i(r)}catch{}throw new k("bundle-extract-failed",a.message)}}async function eo(o){let e=Q(o,"graph.mjs");if(!X(e))throw new k("entry-missing",`graph.mjs missing under ${o}`);let t;try{t=await import(Zt(e).href)}catch(r){throw new k("import-failed",`${r?.code||r?.name||"unknown"}: ${r.message}`)}let n=t.default||Object.values(t).find(r=>typeof r=="function"&&r.prototype?.buildGraph);if(!n)throw new k("entry-class-missing","no buildGraph() class export found");return n}async function ut(o,e={}){if(!o||typeof o!="string")throw new Error("runInProcessSubgraph: workflowName (string) is required");let t=$e(),n=Kt();if((t.depth||0)>=n)throw new k("depth-exceeded",`depth ${t.depth} \u2265 MAX_DEPTH ${n}`);let r;try{r=Vt()}catch(h){throw h}_.debug(`[in-process subgraph] begin '${o}' parent=${t.executionId||"<root>"}`);let s=await Xt({apiBase:r.apiBase,authToken:r.authToken,body:{parentExecutionId:t.executionId,childWorkflowType:o,input:e.input||{},...e.conversationId?{conversationId:e.conversationId}:{}}}),{childExecutionId:a,runtimeTag:i,bundlePresignedUrl:c,sourcesPresignedUrl:u,workflowVersion:d,workflowUuid:f,bundleReady:l}=s,m=qt();if(i&&i!==m)throw await U({apiBase:r.apiBase,authToken:r.authToken,payload:{childExecutionId:a,status:"canceled",error:{message:`runtimeTag mismatch: parent=${m} child=${i}`,code:"RUNTIME_MISMATCH"}}}),new k("runtime-mismatch",`${m} vs ${i}`);if(!l||!c)throw await U({apiBase:r.apiBase,authToken:r.authToken,payload:{childExecutionId:a,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 S=Q(zt,`${f}@${d||"0"}`);try{await Qt(c,S)}catch(h){throw h.fallback&&await U({apiBase:r.apiBase,authToken:r.authToken,payload:{childExecutionId:a,status:"failed",error:{message:h.message,code:h.reason}}}),h}let g=at(o);if(!g)try{g=await eo(S),st(o,g,{workflowUuid:f,version:d,runtimeTag:i,cacheDir:S})}catch(h){throw it(o,h),await U({apiBase:r.apiBase,authToken:r.authToken,payload:{childExecutionId:a,status:"failed",error:{message:h.message,code:h.reason||"IMPORT_FAILED"}}}),h.fallback?h:new k("import-failed",h.message)}let E=Date.now(),p=await(typeof g=="function"&&g.prototype?.buildGraph?new g:g).buildGraph(),w={...e.input||{}},b;try{b=await rt({executionId:a,parentExecutionId:t.executionId,conversationId:e.conversationId!==void 0?e.conversationId:t.conversationId,dispatchMode:"inprocess"},()=>p.run(e.parentAgent,w,{signal:e.signal}))}catch(h){throw await U({apiBase:r.apiBase,authToken:r.authToken,payload:{childExecutionId:a,status:"failed",error:{message:h.message,code:h.code||"CHILD_THREW",stack:h.stack},durationMs:Date.now()-E}}),h}if(b?.stoppedExternally){await U({apiBase:r.apiBase,authToken:r.authToken,payload:{childExecutionId:a,status:"canceled",finalState:b,durationMs:Date.now()-E}});let h=new Error(`Sub-graph '${o}' canceled by parent abort`);throw h.code="SUBGRAPH_CANCELED",h.subgraphJobId=a,h}return await U({apiBase:r.apiBase,authToken:r.authToken,payload:{childExecutionId:a,status:"completed",finalState:b,durationMs:Date.now()-E}}),{finalState:b,executionId:a}}var to=2e3,oo=600*1e3,ro=new Set(["completed","failed","canceled","timeout"]);function no(){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 so(){let o=process.env.PROJECT_ID;if(!o)throw new Error("Sub-graph dispatch requires PROJECT_ID env var.");return o}function io(){let o=process.env.PROJECT_API_TOKEN;if(!o)throw new Error("Sub-graph dispatch requires PROJECT_API_TOKEN env var.");return o}function ao(){return process.env.EXECUTION_ID||null}function lt(o,e){return e==null?o:typeof e=="function"?e(o):typeof e=="string"?e.split(".").reduce((t,n)=>t==null?t:t[n],o):o}async function dt(o,e={}){if(!o||typeof o!="string")throw new Error("dispatchSubgraph: workflowName (string) is required");if(process.env.ZIBBY_INPROCESS_SUBGRAPH!=="0"&&!e.async)try{_.debug(`[sub-graph] trying in-process for '${o}'`);let{finalState:p}=await ut(o,{input:e.input,conversationId:e.conversationId,signal:e.signal,parentAgent:e.parentAgent}),w=lt(p,e.output);return _.info(`[sub-graph] '${o}' completed in-process`),w}catch(p){if(p instanceof k||p?.fallback)_.info(`[sub-graph] in-process fallback for '${o}': ${p.reason||"unknown"} \u2014 using HTTP`);else throw p}let t=no(),n=so(),r=io(),s=ao(),a=`${t}/projects/${encodeURIComponent(n)}/workflows/${encodeURIComponent(o)}/trigger`,i={input:e.input||{},...s?{parentExecutionId:s}:{},...e.conversationId?{conversationId:e.conversationId}:{}};_.info(`[sub-graph] dispatching '${o}' (${e.async?"async":"sync"}) from parent ${s||"<none>"}`);let c=await fetch(a,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${r}`},body:JSON.stringify(i)});if(!c.ok){let p=null,w="";try{p=await c.json(),w=p?.error||p?.message||JSON.stringify(p)}catch{w=await c.text().catch(()=>"")}if(c.status===429){let h=p?.quotaInfo||{},R=new Error(`Sub-graph '${o}' blocked by execution quota (${h.used??"?"}/${h.limit??"?"} on plan ${h.planId||"unknown"}). Sub-workflow runs count toward the same monthly cap as user-triggered runs.`);throw R.code="SUBGRAPH_QUOTA_EXCEEDED",R.status=429,R.subgraph=o,R.quotaInfo=h,R}if(c.status===400){let h=new Error(`Sub-graph '${o}' rejected input: ${w}`);throw h.code="SUBGRAPH_INVALID_INPUT",h.status=400,h.subgraph=o,h.validationErrors=p?.validationErrors||null,h.missing=p?.missing||null,h}let b=new Error(`Sub-graph '${o}' trigger rejected (${c.status}): ${w}`);throw b.code="SUBGRAPH_TRIGGER_FAILED",b.status=c.status,b.subgraph=o,b}let u=await c.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 _.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:oo,l=Number.isFinite(e.pollIntervalMs)?e.pollIntervalMs:to,m=`${t}/executions/${encodeURIComponent(d)}`,S=Date.now()+f,g="accepted",E=0;for(;Date.now()<S;){await new Promise(h=>setTimeout(h,l)),E+=1;let p=await fetch(m,{headers:{Authorization:`Bearer ${r}`}});if(!p.ok){if(p.status>=500){_.warn(`[sub-graph] status poll for ${d} returned ${p.status}, will retry`);continue}throw new Error(`Sub-graph status poll failed for ${d}: ${p.status}`)}let w=await p.json(),b=w?.data||w?.execution||w;if(g=b?.status||g,ro.has(g)){if(g!=="completed"){let W=new Error(`Sub-graph '${o}' (${d}) ended in status '${g}'`);throw W.subgraphJobId=d,W.subgraphStatus=g,W}let h=b?.finalState||b?.state||{},R=lt(h,e.output);return _.info(`[sub-graph] '${o}' (${d}) completed after ${E} polls`),R}}let T=new Error(`Sub-graph '${o}' (${d}) timed out after ${Math.round(f/1e3)}s (last status: ${g})`);throw T.subgraphJobId=d,T.subgraphStatus=g,T}import{existsSync as pt,readFileSync as co}from"node:fs";import{join as xe,dirname as ft}from"node:path";var le=class{static async loadContext(e,t,n={}){let r={},s=n.filenames||["CONTEXT.md","AGENTS.md"];if(e){let i=ft(xe(t,e));for(let c of s){let u=await this.findAndMergeContextFiles(c,i,t);if(u){let d=c.replace(/\.[^.]+$/,"").toLowerCase();r[d]=u}}}let a=n.discovery||{};for(let[i,c]of Object.entries(a))try{let u=xe(t,c);pt(u)&&(r[i]=await this.loadFile(u))}catch(u){console.warn(`[workflow] could not load context '${i}' from '${c}': ${u.message}`)}return r}static async findAndMergeContextFiles(e,t,n){let r=[],s=t;for(;s.startsWith(n);){let a=xe(s,e);if(pt(a))try{r.unshift(await this.loadFile(a))}catch(c){console.warn(`[workflow] could not load ${e} from ${a}: ${c.message}`)}let i=ft(s);if(i===s)break;s=i}return r.length===0?null:r.every(a=>typeof a=="string")?r.join(`
|
|
30
30
|
|
|
31
31
|
---
|
|
32
32
|
|
|
33
|
-
`):
|
|
33
|
+
`):r.every(a=>typeof a=="object")?Object.assign({},...r):r[r.length-1]}static async loadFile(e){let t=co(e,"utf-8");if(e.endsWith(".json"))return JSON.parse(t);if(e.endsWith(".js")||e.endsWith(".mjs")){let{pathToFileURL:n}=await import("url"),r=await import(n(e).href);return r.default||r}return t}};import{mkdirSync as mt,existsSync as ke,writeFileSync as ht,unlinkSync as uo}from"node:fs";import{join as F,resolve as St}from"node:path";import{config as lo}from"dotenv";import{zodToJsonSchema as gt}from"zod-to-json-schema";import po from"handlebars";function fo({traceFrom:o,sessionId:e,sessionPath:t,idSource:n,mkdirFresh:r}){if(!(process.env.ZIBBY_SESSION_LOG==="1"||process.env.ZIBBY_SESSION_LOG==="true"))return;let a=typeof process.ppid=="number"?process.ppid:"n/a",i=`[zibby:session] from=${o} pid=${process.pid} ppid=${a} sessionId=${e} source=${n} mkdir=${r?"yes":"no"} path=${t}`;if(console.log(i),process.env.ZIBBY_TRACE_SESSION==="1"||process.env.ZIBBY_TRACE_SESSION==="true"){let d=(new Error("session trace").stack||"").split(`
|
|
34
34
|
`).slice(2,14).join(`
|
|
35
35
|
`);console.log(`[zibby:session] stack (${o}):
|
|
36
|
-
${d}`)}}function
|
|
37
|
-
\u{1F6D1} External stop requested \u2014 ending workflow.`),
|
|
38
|
-
${d}`),
|
|
36
|
+
${d}`)}}function ho(){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 go(){if(!(process.env.ZIBBY_PIN_SESSION_PATH==="1"||process.env.ZIBBY_PIN_SESSION_PATH==="true"))return;let e=process.env.ZIBBY_SESSION_PATH;if(!(e==null||String(e).trim()===""))try{return St(String(e).trim())}catch{return String(e).trim()}}function mo(){ho()||(delete process.env.ZIBBY_SESSION_PATH,delete process.env.ZIBBY_SESSION_ID)}function So({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 wo(o={}){let e=Ve.map(s=>process.env[s]).find(Boolean),t=Math.random().toString(36).slice(2,6),n=e||`${Date.now()}_${t}`,r=o.paths?.sessionPrefix;return r?`${r}_${n}`:n}function yo({cwd:o=process.cwd(),config:e={},initialState:t={},traceFrom:n="resolveWorkflowSession"}={}){let r=t.sessionPath,s=t.sessionTimestamp,a="initialState.sessionPath";if(!r&&process.env.ZIBBY_SESSION_PATH)try{let u=St(String(process.env.ZIBBY_SESSION_PATH));u&&(r=u,a="ZIBBY_SESSION_PATH")}catch{}let i;if(r)i=String(r).split(/[/\\]/).filter(Boolean).pop(),s==null&&(s=Date.now());else{let u=process.env.ZIBBY_SESSION_ID&&String(process.env.ZIBBY_SESSION_ID).trim();if(u)i=u,a="ZIBBY_SESSION_ID";else{let f=e.sessionId!=null?String(e.sessionId).trim():"";f&&f!=="last"?(i=f,a="config.sessionId"):(i=wo(e),a="generated")}s=s??Date.now();let d=e.paths?.output||ae;r=F(o,d,Ke,i)}let c=!ke(r);return c&&mt(r,{recursive:!0}),(c||a!=="initialState.sessionPath")&&fo({traceFrom:n,sessionId:i,sessionPath:r,idSource:a,mkdirFresh:c}),So({sessionPath:r,sessionId:i}),{sessionPath:r,sessionId:i,sessionTimestamp:s}}var de=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,n={}){if(!(t instanceof D)&&t&&typeof t=="object"&&typeof t.workflow=="string"){let s=t,a={name:e,_isCustomCode:!0,retries:s.retries,onComplete:s.onComplete,execute:async c=>{let u=c?.state&&typeof c.state.getAll=="function"?c.state.getAll():c,d;return typeof s.input=="function"?d=s.input(u):s.input&&typeof s.input=="object"?d=s.input:d={},dt(s.workflow,{input:d,async:s.async===!0,conversationId:typeof s.conversationId=="function"?s.conversationId(u):s.conversationId,output:s.output,timeoutMs:s.timeoutMs,pollIntervalMs:s.pollIntervalMs,signal:u?._signal,parentAgent:c?.agent})}},i=new D(a);return i.name=e,this.nodes.set(e,i),n.prompt&&this.nodePrompts.set(e,n.prompt),Object.keys(n).length>0&&this.nodeOptions.set(e,n),this}let r=t instanceof D?t:new D(t);return r.name=e,this.nodes.set(e,r),n.prompt&&this.nodePrompts.set(e,n.prompt),Object.keys(n).length>0&&this.nodeOptions.set(e,n),this}addConditionalNode(e,t){return this.nodes.set(e,new ue({...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:n}={}){return this.edges.set(e,{conditional:!0,routes:t,labels:n}),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,n,r,s){let a=n;for(let i=e.length-1;i>=0;i--){let c=e[i],u=a;a=()=>c(t,u,r,s)}return a()}serialize(){let e=[],t={};for(let[u,d]of this.nodes){let f=this.nodeTypeMap.get(u)||u;e.push({id:u,type:f,data:{nodeType:f,label:u}});let l={};d._isCustomCode&&typeof d.execute=="function"&&(l.customCode=d.execute.toString());let m=this.nodePrompts.get(u);if(m&&(l.prompt=m),typeof d.customExecute=="function"&&(l.executeCode=d.customExecute.toString()),d.outputSchema)try{if(typeof d.outputSchema._def<"u"){let E=gt(d.outputSchema,{target:"openApi3"});l.outputSchema={jsonSchema:E,variables:this._flattenJsonSchemaToVariables(E)}}else l.outputSchema={schema:d.outputSchema}}catch(E){console.warn(`[workflow] failed to convert schema for ${u}:`,E.message)}let S=(this.resolvedToolsMap||{})[u];S?.toolIds&&(l.tools=S.toolIds);let g=Array.isArray(d?.config?.skills)?d.config.skills:Array.isArray(d?.skills)?d.skills:null;g&&g.length>0&&(l.skills=[...g]),Object.keys(l).length>0&&(t[u]=l)}let n=[];for(let[u,d]of this.edges)if(typeof d=="string")n.push({source:u,target:d});else if(d.conditional){let f=this.conditionalCodeMap.get(u)||d.routes.toString(),l=this._inferConditionalTargets(d.routes),m=d.labels||{};for(let S of l){let g={source:u,target:S,data:{conditionalCode:f}};m[S]&&(g.label=m[S]),n.push(g)}}let r=u=>{if(!u)return null;try{return gt(u,{target:"openApi3"})}catch{return null}},s=this._runtimeSchema(),a=r(s||this.stateSchema),i=r(this.inputSchema),c=r(this.contextSchema);return{nodes:e,edges:n,nodeConfigs:t,stateSchema:a,inputSchema:i,contextSchema:c}}_inferConditionalTargets(e){let t=e.toString(),n=new Set,r=/return\s+['"]([^'"]+)['"]/g,s;for(;(s=r.exec(t))!==null;)n.add(s[1]);return[...n]}_flattenJsonSchemaToVariables(e,t=""){let n=e;if(e.$ref&&e.definitions){let r=e.$ref.replace("#/definitions/","");n=e.definitions[r]||e}return this._flattenSchema(n,t)}_flattenSchema(e,t=""){if(!e||typeof e!="object")return[];let n=[],r=e.properties||{},s=e.required||[];for(let[a,i]of Object.entries(r)){let c=t?`${t}.${a}`:a;n.push({path:c,type:i.type||"unknown",label:i.description||this._formatLabel(a),optional:!s.includes(a)}),i.type==="object"&&i.properties&&n.push(...this._flattenSchema(i,c)),i.type==="array"&&i.items?.type==="object"&&i.items.properties&&n.push(...this._flattenSchema(i.items,`${c}[]`))}return n}_formatLabel(e){return e.replace(/([A-Z])/g," $1").replace(/^./,t=>t.toUpperCase()).trim()}_summarizeNodeOutput(e,t){if(!t||typeof t!="object")return[];let n=[];t.success!==void 0&&n.push(`Result: ${t.success?"passed":"failed"}`);for(let[r,s]of Object.entries(t))if(!(r==="success"||r==="raw"||r==="nextNode")){if(typeof s=="string"&&s.length<=80)n.push(`${r}: ${s}`);else if(Array.isArray(s)){let a=s.length,i=s.filter(u=>u?.passed===!0).length,c=s.some(u=>u?.passed!==void 0);n.push(c?`${r}: ${i}/${a} passed${a-i?`, ${a-i} failed`:""}`:`${r}: ${a} items`)}if(n.length>=4)break}return n}async run(e,t={},n={}){if(!this.entryPoint)throw new Error("No entry point set for graph");let r=new AbortController;n.signal&&(n.signal.aborted?r.abort():n.signal.addEventListener("abort",()=>r.abort(),{once:!0}));let s=n.strategyAbortTimeoutMs??t.config?.strategyAbortTimeoutMs??5e3,a=t.cwd||process.cwd();lo({path:F(a,".env")});let i=t.config||{};if(!i||Object.keys(i).length===0)try{let I=F(a,".zibby.config.js");ke(I)&&(i=(await import(I)).default||{})}catch{}process.env.EXECUTION_ID&&!i.agent?.strictMode&&(i.agent={...i.agent,strictMode:!0});let c=t.agentType;if(!c){let I=i?.agent;I?.provider?c=I.provider:I?.gemini?c="gemini":I?.claude?c="claude":I?.cursor?c="cursor":I?.codex?c="codex":c=process.env.AGENT_TYPE||"cursor"}let u=t.contextConfig||e?.config?.contextConfig||e?.config?.context||i?.context||{},d=this._runtimeSchema();if(d){let I=d.safeParse(t);if(!I.success){let A=I.error.issues.map(O=>`${O.path.join(".")}: ${O.message}`);throw console.error("\u274C Initial state validation failed:"),A.forEach(O=>console.error(` - ${O}`)),new Error(`State validation failed: ${A.join(", ")}`)}x.step("State validated against schema")}let f=go(),l=t.sessionPath||f;l||mo();let{sessionPath:m,sessionTimestamp:S,sessionId:g}=yo({cwd:a,config:i,traceFrom:"WorkflowGraph.run",initialState:{sessionPath:l,sessionTimestamp:t.sessionTimestamp}});x.step(`Session ${g}`);let E=await le.loadContext(t.specPath||"",a,u);Object.keys(E).length>0&&x.step(`Context loaded: ${Object.keys(E).join(", ")}`);let T=t.outputPath;!T&&t.specPath&&(e?.calculateOutputPath?T=e.calculateOutputPath(t.specPath):console.warn(`\u26A0\uFE0F outputPath not resolved (specPath=${t.specPath})`));let p=new ne({...t,config:i,agentType:c,outputPath:T,sessionPath:m,sessionTimestamp:S,context:E,resolvedTools:this.resolvedToolsMap||{},_signal:r.signal}),w=new Map;try{await import("@zibby/skills")}catch{}let{getSkill:b}=await Promise.resolve().then(()=>(ce(),Qe)),h=i.skills&&typeof i.skills=="object"?i.skills:{},R=Object.values(h).filter(I=>I&&typeof I=="object"&&typeof I.id=="string"),W=I=>{for(let A of R)if(A.id===I)return A;return b(I)},Pe=new Set;for(let[,I]of this.nodes)for(let A of I.config?.skills||[])Pe.add(A);for(let I of Pe){let A=W(I);if(typeof A?.middleware=="function")try{let O=await A.middleware();typeof O=="function"&&w.set(I,O)}catch{}}let y=this.entryPoint,ee=[],Ce=i?.recursionLimit??100,Et=0;try{for(;y&&y!=="END";){if(++Et>Ce)throw new Error(`Workflow exceeded recursion limit (${Ce}) \u2014 likely a cyclic conditional route. Set config.recursionLimit if you need a higher cap.`);let A=F(m,qe);if(ke(A)){try{uo(A)}catch{}r.abort()}if(r.signal.aborted)return console.warn(`
|
|
37
|
+
\u{1F6D1} External stop requested \u2014 ending workflow.`),x.step("Workflow stopped externally"),{success:!0,state:p.getAll(),executionLog:ee,stoppedExternally:!0};let O=this.nodes.get(y);if(!O)throw new Error(`Node '${y}' not found in graph`);let Re=JSON.stringify({sessionPath:m,sessionTimestamp:S,currentNode:y,createdAt:new Date().toISOString(),config:p.get("config")}),bt=F(m,J);ht(bt,Re,"utf-8");let Be=p.get("config")?.paths?.output||ae,$t=F(a,Be,J);mt(F(a,Be),{recursive:!0});try{ht($t,Re,"utf-8")}catch{}let Me=t.onPipelineProgress;if(typeof Me=="function")try{Me({cwd:a,sessionPath:m,sessionId:g,outputBase:p.get("config")?.paths?.output||ae,currentNode:y})}catch{}let Tt=(this.resolvedToolsMap||{})[y]||null;p.set("_currentNodeTools",Tt);let vt=p.get("nodeConfigs")||{};p.set("_currentNodeConfig",vt[y]||{}),x.nodeStart(y);let De=Date.now(),te=this.nodePrompts.get(y);if(!this._invokeAgent){let N=await Promise.resolve().then(()=>(V(),q));this._invokeAgent=N.invokeAgent}let xt=this._invokeAgent,fe={},kt=O.config?.skills||[];for(let N of kt){let P=W(N);if(typeof P?.invokeAgentOptions=="function")try{let $=P.invokeAgentOptions(p.getAll(),{agentType:p.get("agentType"),nodeName:y});$&&typeof $=="object"&&(fe={...fe,...$})}catch($){console.warn(`[graph] skill '${N}' invokeAgentOptions threw: ${$.message}`)}}let Le=async(N,P,$={})=>{let C=xt(N,P,{...fe,...$,signal:r.signal});return C.catch(()=>{}),r.signal.aborted?C:Promise.race([C,new Promise((G,H)=>{let M=()=>{setTimeout(()=>{let Z=new Error(`Strategy ignored AbortSignal \u2014 engine deadman fired after ${s}ms`);Z.name="AbortError",H(Z)},s)};r.signal.addEventListener("abort",M,{once:!0})})])},je={state:p,invokeAgent:async(N={},P={})=>{let $=P.prompt||"";if(te){let C=this._compiledPrompts.get(y);C||(C=po.compile(te,{noEscape:!0}),this._compiledPrompts.set(y,C));try{$=C(N)}catch(G){throw console.error(`\u274C Template rendering failed for node '${y}':`,G.message),new Error(`Template rendering failed: ${G.message}`,{cause:G})}}else if(!$)throw new Error(`No prompt template configured for node '${y}' and no prompt provided in options`);return Le($,{state:p.getAll(),images:P.images||[]},{model:P.model||p.get("model"),workspace:p.get("workspace"),schema:P.schema,...P,signal:r.signal})},_coreInvokeAgent:Le,agent:e,nodeId:y,promptTemplate:te,getPromptTemplate:()=>te,...p.getAll()};try{let N=(O.config?.skills||[]).map(M=>w.get(M)).filter(Boolean),P=[...this.middleware,...N],$;P.length>0?$=await this._composeMiddleware(P,y,async()=>O.execute(je,p),p.getAll(),p):$=await O.execute(je,p);let C=Date.now()-De;if(ee.push({node:y,success:$.success,duration:C,timestamp:new Date().toISOString()}),!$.success){if(r.signal.aborted)return x.step("Workflow stopped externally"),{success:!0,state:p.getAll(),executionLog:ee,stoppedExternally:!0};p.append("errors",{node:y,error:$.error});let M=O.config?.retries||0,Z=`${y}_retries`,oe=p.getAll()[Z]||0;if(oe<M){x.stepInfo(`Retrying (attempt ${oe+1}/${M})`),p.update({[Z]:oe+1,[`${y}_raw`]:$.raw});continue}throw x.nodeFailed(y,$.error,{duration:C}),new Error(`Node '${y}' failed after ${oe} attempts: ${$.error}`)}p.update({[y]:$.output});let G=this._summarizeNodeOutput(y,$.output);x.nodeComplete(y,{duration:C,details:G});let H=this.edges.get(y);if(!H)y="END";else if(H.conditional){let M=H.routes(p.getAll());x.route(y,M),y=M}else y=H}catch(N){throw x.isInsideNode&&x.nodeFailed(y,N.message,{duration:Date.now()-De}),p.set("failed",!0),p.set("failedAt",y),N}}x.graphComplete();let I={success:!0,state:p.getAll(),executionLog:ee};return e&&typeof e.onComplete=="function"&&await e.onComplete(I),I}finally{if(e&&typeof e.cleanup=="function")try{await e.cleanup()}catch(I){console.warn(`[workflow] agent.cleanup() failed: ${I.message}`)}}}};var Ae=Symbol.for("@zibby/agent-workflow.nodes");globalThis[Ae]||(globalThis[Ae]=new Map);var Oe=globalThis[Ae];function _o(o,e){Oe.set(o,e)}function wt(o){return Oe.get(o)}function Ne(o){return Oe.has(o)}_o("ai_agent",{name:"ai_agent",factory:!0,create:(o,e={})=>({name:o,_isCustomCode:!0,execute:async t=>{let n=t?._coreInvokeAgent;n||(n=(await Promise.resolve().then(()=>(V(),q))).invokeAgent);let r=e.extraPromptInstructions||"Execute the task based on the current state.",s=Io(r,t),a=await n(s,{cwd:t.workspace||process.cwd(),model:t.model,tools:e.resolvedTools||null});return{success:!0,output:{raw:a,nodeId:o},raw:typeof a=="string"?a:a.raw}}})});function Io(o,e){let t=/@([\w.]+)/g,n=new Set,r;for(;(r=t.exec(o))!==null;)n.add(r[1]);if(n.size===0)return o;let s=[],a=new Set;for(let i of n){let c=i.split(".")[0];if(a.has(c))continue;let u=i.split(".").reduce((l,m)=>l?.[m],e);if(u===void 0)continue;let d=typeof u=="string"?u:u?.raw??JSON.stringify(u,null,2),f=i.replace(/_/g," ").replace(/\b\w/g,l=>l.toUpperCase());s.push(`## ${f}
|
|
38
|
+
${d}`),i.includes(".")||a.add(c)}return s.length===0?o:`${o}
|
|
39
39
|
|
|
40
40
|
---
|
|
41
41
|
# Referenced Context
|
|
42
42
|
|
|
43
43
|
${s.join(`
|
|
44
44
|
|
|
45
|
-
`)}`}
|
|
45
|
+
`)}`}ce();L();var Eo={};function _t(o,e){if(Array.isArray(e))return yt(e);let t=Eo[o];return!t||t.length===0?null:yt(t)}function yt(o){if(!Array.isArray(o)||o.length===0)return null;let e=[],t={},n=[];for(let r of o){let s=K(r);if(!s){_.warn(`[workflow] unknown skill "${r}" \u2014 skipping`);continue}n.push(r);for(let a of s.tools||[])e.push({name:a.name,description:a.description,input_schema:a.input_schema||{type:"object",properties:{}}});if(!t[s.serverName])if(typeof s.resolve=="function"){let a=s.resolve();a&&(t[s.serverName]={...a,toolPrefix:r})}else{let a={};for(let i of s.envKeys||[]){let c=process.env[i];c&&(a[i]=c)}t[s.serverName]={command:s.command,args:[...s.args||[]],env:a,toolPrefix:r}}}return n.length===0?null:{toolIds:n,claudeTools:e,mcpServers:t}}L();function xr(o,e={}){let{nodes:t,edges:n,nodeConfigs:r={}}=o;if(!Array.isArray(t)||t.length===0)throw new B("Graph must have at least one node");if(!Array.isArray(n))throw new B("Graph edges must be an array");let s=new de(e);e.stateSchema&&s.setStateSchema(e.stateSchema);let a=new Set,i=new Map,c={};for(let l of t){let m=pe(l);i.set(l.id,{...l,resolvedType:m}),m==="decision"&&a.add(l.id)}for(let[l,m]of i){if(a.has(l))continue;let S=m.resolvedType,g=r[l]||{},E=_t(S,g.tools);E&&(c[l]=E);let T={};g.prompt&&(T.prompt=g.prompt);let p=Ne(S);if(_.debug(`[workflow] compiler: node "${l}" type="${S}" registered=${p}`),g.customCode&&!p)s.addNode(l,It(l,g.customCode,g),T),s.setNodeType(l,S);else if(p){let w=wt(S);w.factory?s.addNode(l,w.create(l,{...g,resolvedTools:E}),T):s.addNode(l,w,T),s.setNodeType(l,S)}else if(g.executeCode)s.addNode(l,It(l,g.executeCode,g),T),s.setNodeType(l,S);else throw new B(`Unknown node type "${S}" for node "${l}". Did you forget to register it?`)}s.resolvedToolsMap=c;let u=new Set;for(let l of n)a.has(l.target)||u.add(l.target);let d=t.find(l=>!a.has(l.id)&&!u.has(l.id));if(!d)throw new B("Could not determine entry point: no node without incoming edges found");s.setEntryPoint(d.id);let f=bo(n,"source");for(let l of n)if(!a.has(l.source))if(a.has(l.target)){let m=l.target,S=f.get(m)||[];if(S.length===0)throw new B(`Decision node "${m}" has no outgoing edges`);let g=$o(m,S,a);s.addConditionalEdges(l.source,g)}else s.addEdge(l.source,l.target);return s}function kr(o){let e=[];if(!o||typeof o!="object")return{valid:!1,errors:["Config must be a non-null object"]};if((!Array.isArray(o.nodes)||o.nodes.length===0)&&e.push("Graph must have at least one node"),Array.isArray(o.edges)||e.push("Graph edges must be an array"),e.length>0)return{valid:!1,errors:e};let t=o.nodeConfigs||{};for(let i of o.nodes){let c=pe(i);if(c==="decision"||Ne(c))continue;let u=t[i.id]||{};u.customCode||u.executeCode||e.push(`Unknown node type "${c}" for node "${i.id}". Register it or provide customCode/executeCode.`)}let n=new Set(o.nodes.map(i=>i.id));for(let i of o.edges)n.has(i.source)||e.push(`Edge references unknown source node "${i.source}"`),n.has(i.target)||e.push(`Edge references unknown target node "${i.target}"`);let r=new Set(o.nodes.filter(i=>pe(i)==="decision").map(i=>i.id)),s=new Set;for(let i of o.edges)r.has(i.target)||s.add(i.target);let a=o.nodes.filter(i=>!r.has(i.id)&&!s.has(i.id));a.length===0?e.push("No entry point found (every node has incoming edges)"):a.length>1&&e.push(`Multiple entry points found: ${a.map(i=>i.id).join(", ")}`);for(let i of r){let c=o.edges.filter(d=>d.source===i);c.length===0&&e.push(`Decision node "${i}" has no outgoing edges`),c.some(d=>d.data?.conditionalCode||d.conditionalCode)||e.push(`Decision node "${i}" outgoing edges have no conditionalCode`)}return{valid:e.length===0,errors:e}}function Ar(o){return!o||!Array.isArray(o.nodes)?[]:o.nodes.filter(e=>pe(e)!=="decision").map(e=>e.id)}function pe(o){let e=o.data?.nodeType||o.data?.type||o.type;return e==="workflowNode"||e==="custom"||e==="default"?o.id:e}function bo(o,e){let t=new Map;for(let n of o){let r=n[e];t.has(r)||t.set(r,[]),t.get(r).push(n)}return t}function $o(o,e,t){let n=e.find(i=>i.data?.conditionalCode||i.conditionalCode);if(!n)throw new B(`Decision node "${o}" has no conditionalCode on its outgoing edges`);let r=n.data?.conditionalCode||n.conditionalCode,s=new Set(e.map(i=>i.target).filter(i=>!t.has(i))),a;try{let c=new Function(`return (${r})`)();a=u=>{let d=c(u);return s.has(d)||_.warn(`[workflow] conditional route from "${o}" returned "${d}" which is not in valid targets: ${[...s].join(", ")}`),d}}catch(i){throw new B(`Failed to compile conditionalCode for "${o}": ${i.message}`)}return a}function It(o,e,t={}){let n;try{n=new Function("invokeAgent","require","console",`return (${e})`)}catch(a){throw new B(`Failed to compile customCode for node "${o}": ${a.message}`)}let r=n(async(...a)=>{let{invokeAgent:i}=await Promise.resolve().then(()=>(V(),q));return i(...a)},typeof he<"u"?he:void 0,console),s=null;return t.outputSchema&&(s=t.outputSchema.jsonSchema||t.outputSchema),{name:o,_isCustomCode:!0,outputSchema:s,execute:async a=>{try{let i=await r(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 B=class extends Error{constructor(e){super(e),this.name="CompilationError"}};export{B as CompilationError,xr as compileGraph,Ar as extractSteps,kr as validateGraphConfig};
|