@zibby/agent-workflow 0.3.2 → 0.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +48 -0
- package/dist/constants.d.ts +1 -0
- package/dist/constants.js +1 -1
- package/dist/graph-compiler.js +21 -21
- package/dist/graph.js +19 -19
- package/dist/index.js +31 -31
- package/dist/node.js +4 -4
- package/dist/sub-graph-executor.d.ts +43 -0
- package/dist/sub-graph-executor.js +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -156,6 +156,7 @@ If you want to compose Claude Code + Cursor + Codex into one pipeline with struc
|
|
|
156
156
|
|---|---|
|
|
157
157
|
| `WorkflowGraph` | The DAG. `addNode`, `addEdge`, `addConditionalEdges`, `setEntryPoint`. |
|
|
158
158
|
| `Node` | One agent invocation. Config: `prompt`, `outputSchema` (Zod), optional `agent`, `retries`, `skills`. |
|
|
159
|
+
| Sub-graph node | `addNode(name, { workflow: 'other-name', ... })` — dispatches another deployed workflow as a child. Sync (poll + merge) or async (`async: true`, fire-and-forget). See [Sub-graphs](#sub-graphs) below. |
|
|
159
160
|
| `AgentStrategy` | Abstract base. Implement `canHandle(ctx)` and `invoke(prompt, opts)`. |
|
|
160
161
|
| `registerStrategy()` | Tells the engine what agents are available. Selected by node `agent` field → `config.agents[name]` → `state.agentType`. |
|
|
161
162
|
| `WorkflowState` | History-tracked state passed between nodes. `set` / `update` / `append` / `rollback`. |
|
|
@@ -168,6 +169,53 @@ State flows automatically: when node `plan` completes with output `{ tasks: [...
|
|
|
168
169
|
|
|
169
170
|
---
|
|
170
171
|
|
|
172
|
+
## Sub-graphs
|
|
173
|
+
|
|
174
|
+
A **sub-graph node** dispatches another deployed workflow as a child of the current one. Useful when a step is large enough to deserve its own state schema, its own version, and its own activity-tab history — but you want a parent to call it as part of a larger flow.
|
|
175
|
+
|
|
176
|
+
One extra field on the existing node config:
|
|
177
|
+
|
|
178
|
+
```js
|
|
179
|
+
g.addNode('audit', { workflow: 'deep-audit' });
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
That's the entire feature surface. No new imports, no UUID in user code, no separate class. The engine recognizes `workflow:` and turns the node into a sub-graph dispatcher.
|
|
183
|
+
|
|
184
|
+
**Sync vs async** is a single flag:
|
|
185
|
+
|
|
186
|
+
```js
|
|
187
|
+
g.addNode('audit', { workflow: 'deep-audit' }); // sync — parent blocks until child done
|
|
188
|
+
g.addNode('notify', { workflow: 'slack-notifier', async: true }); // fire-and-forget
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
**State plumbing** — each workflow has its own schema; the parent transforms parent state into child input and (optionally) extracts what it needs back out:
|
|
192
|
+
|
|
193
|
+
```js
|
|
194
|
+
g.addNode('audit', {
|
|
195
|
+
workflow: 'deep-audit',
|
|
196
|
+
input: (state) => ({ ticketId: state.ticketId }),
|
|
197
|
+
output: 'auditResult.score', // dot-path on child finalState
|
|
198
|
+
// OR: output: (childState) => ({ score: childState.auditResult.score,
|
|
199
|
+
// label: childState.auditResult.label }),
|
|
200
|
+
retries: 3, // retry whole dispatch on transient failure
|
|
201
|
+
timeoutMs: 5 * 60 * 1000, // give up after 5min (sync mode only)
|
|
202
|
+
});
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
**Errors are typed** so parents can branch:
|
|
206
|
+
|
|
207
|
+
| `err.code` | When |
|
|
208
|
+
|---|---|
|
|
209
|
+
| `SUBGRAPH_INVALID_INPUT` | Parent's `input:` didn't satisfy child's stateSchema — server 400'd before any Fargate spawn |
|
|
210
|
+
| `SUBGRAPH_QUOTA_EXCEEDED` | Account over its execution cap; sub-graph runs count separately |
|
|
211
|
+
| `SUBGRAPH_TRIGGER_FAILED` | Any other dispatch failure |
|
|
212
|
+
|
|
213
|
+
**Same `/trigger` endpoint as user-initiated runs.** The engine POSTs to `/projects/<id>/workflows/<child-name>/trigger` with `parentExecutionId` set. The server's input gate, quota check, and execution accounting all apply identically — a parent that fans out 10 children consumes 11 executions.
|
|
214
|
+
|
|
215
|
+
**Full reference:** [docs.zibby.app/concepts/sub-graphs](https://docs.zibby.app/concepts/sub-graphs)
|
|
216
|
+
|
|
217
|
+
---
|
|
218
|
+
|
|
171
219
|
## Examples
|
|
172
220
|
|
|
173
221
|
| | Shows |
|
package/dist/constants.d.ts
CHANGED
package/dist/constants.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var o=".zibby/output",t="sessions",s=".session-info.json",_=".zibby-stop",I="result.json",
|
|
1
|
+
var o=".zibby/output",t="sessions",s=".session-info.json",_=".zibby-stop",I="result.json",E="raw_stream_output.txt",e="events.json",r={BROWSER:"browser",JIRA:"jira",GITHUB:"github",SLACK:"slack",MEMORY:"memory",CHAT_MEMORY:"chat-memory",RUNNER:"runner",SKILL_INSTALLER:"skill-installer",CORE_TOOLS:"core-tools",WORKFLOW_BUILDER:"workflow-builder",SESSION:"session"},S=["CI_JOB_ID","GITHUB_RUN_ID","CIRCLE_WORKFLOW_ID","BUILD_ID"];export{S as CI_ENV_VARS,o as DEFAULT_OUTPUT_BASE,e as EVENTS_FILE,E as RAW_OUTPUT_FILE,I as RESULT_FILE,t as SESSIONS_DIR,s as SESSION_INFO_FILE,r as SKILLS,_ as STOP_REQUEST_FILE};
|
package/dist/graph-compiler.js
CHANGED
|
@@ -1,45 +1,45 @@
|
|
|
1
|
-
var
|
|
1
|
+
var pt=Object.defineProperty;var ue=(o=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(o,{get:(e,t)=>(typeof require<"u"?require:e)[t]}):o)(function(o){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+o+'" is not supported')});var Q=(o,e)=>()=>(o&&(e=o(o=0)),e);var xe=(o,e)=>{for(var t in e)pt(o,t,{get:e[t],enumerable:!0})};var Pe,ht,oe,E,U=Q(()=>{Pe=()=>{},ht={debug:Pe,info:Pe,warn:(...o)=>console.warn("[workflow]",...o),error:(...o)=>console.error("[workflow]",...o)},oe={impl:ht},E={debug:(...o)=>oe.impl.debug?.(...o),info:(...o)=>oe.impl.info?.(...o),warn:(...o)=>oe.impl.warn?.(...o),error:(...o)=>oe.impl.error?.(...o)}});var Ge=Q(()=>{});var Ye={};xe(Ye,{clearSkills:()=>Et,getAllSkills:()=>yt,getSkill:()=>H,hasSkill:()=>wt,listSkillIds:()=>_t,registerSkill:()=>St});function St(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 H(o){return Y.get(o)||null}function wt(o){return Y.has(o)}function yt(){return new Map(Y)}function _t(){return Array.from(Y.keys())}function Et(){Y.clear()}var ge,Y,se=Q(()=>{ge=Symbol.for("@zibby/agent-workflow.skills");globalThis[ge]||(globalThis[ge]=new Map);Y=globalThis[ge]});var K={};xe(K,{getAgentStrategy:()=>Je,invokeAgent:()=>bt,listStrategies:()=>It,registerStrategy:()=>$t});function $t(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 It(){return j.map(o=>o.getName())}function Je(o={}){let{state:e={},preferredAgent:t=null}=o,s=t||e.agentType||process.env.AGENT_TYPE;if(!s){let r=j.map(a=>a.getName()).join(", ")||"none registered";throw new Error(`No agent specified. Set agentType in state or AGENT_TYPE env var. Available: ${r}`)}E.debug(`[workflow] agent selection: requested=${s}`);let i=j.find(r=>r.getName()===s);if(!i){let r=j.map(a=>a.getName()).join(", ")||"none registered";throw new Error(`Unknown agent '${s}'. Available: ${r}`)}if(!i.canHandle(o))throw new Error(`Agent '${s}' is not available in this environment. Check credentials/environment.`);return E.debug(`[workflow] using agent: ${i.getName()}`),i}async function bt(o,e={},t={}){let s=Je(e),i=e.state?.config||t.config||{},r=i.models||{},a=t.nodeName&&r[t.nodeName]||null,n=r.default||null,c=i.agent?.[s.name]?.model||null,l=a||n||c||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:i},d=o,u=p.skills||[];if(u.length>0&&!t.skipPromptFragments){let $=u.map(m=>{let v=H(m)?.promptFragment;return typeof v=="function"?v():v}).filter(Boolean);$.length>0&&(d+=`
|
|
2
2
|
|
|
3
|
-
${
|
|
3
|
+
${$.join(`
|
|
4
4
|
|
|
5
|
-
`)}`)}let
|
|
5
|
+
`)}`)}let _=e.state?._currentNodeConfig?.extraPromptInstructions?.trim();return _&&(d+=`
|
|
6
6
|
|
|
7
7
|
\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501
|
|
8
8
|
PRIORITY OVERRIDE \u2014 THE FOLLOWING INSTRUCTIONS TAKE PRECEDENCE OVER ALL PREVIOUS CONTENT
|
|
9
9
|
\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501
|
|
10
10
|
|
|
11
|
-
${
|
|
12
|
-
`),
|
|
11
|
+
${_}
|
|
12
|
+
`),E.debug(`[workflow] prompt length: ${d.length} chars`),s.invoke(d,p)}var me,j,q=Q(()=>{Ge();U();se();me=Symbol.for("@zibby/agent-workflow.strategies");globalThis[me]||(globalThis[me]=[]);j=globalThis[me]});var ft=new Set(["__proto__","constructor","prototype"]);function de(o){if(ft.has(o))throw new Error(`Invalid state key: "${o}"`)}var ee=class{constructor(e={}){this._state=Object.create(null),Object.assign(this._state,{messages:[],errors:[],artifacts:{},metadata:{},...e}),this._history=[]}get(e){return this._state[e]}set(e,t){de(e),this._history.push({...this._state}),this._state[e]=t}update(e){let t=Object.getOwnPropertyNames(e);for(let s of t)de(s);this._history.push({...this._state});for(let s of t)this._state[s]=e[s]}append(e,t){de(e),this._history.push({...this._state}),Array.isArray(this._state[e])||(this._state[e]=[]),this._state[e].push(t)}getAll(){return{...this._state}}rollback(){this._history.length>0&&(this._state=this._history.pop())}};var te=class{constructor(e){this.schema=e}parse(e){let t=e.match(/```json\s*([\s\S]*?)\s*```/);if(t)return this.validate(JSON.parse(t[1]));let s=[e.match(/\{[\s\S]*?\}/),e.match(/\{[\s\S]*\}/)].filter(Boolean).map(i=>i[0]);for(let i of s)try{return this.validate(JSON.parse(i))}catch(r){if(!(r instanceof SyntaxError))throw r}return this.validate({result:e.trim()})}validate(e){let t=[];for(let[s,i]of Object.entries(this.schema)){if(i.required&&!(s in e)&&t.push(`Missing required field: ${s}`),s in e&&i.type){let r=typeof e[s];r!==i.type&&t.push(`Field '${s}' expected ${i.type}, got ${r}`)}if(i.validate&&s in e){let r=i.validate(e[s]);r&&t.push(`Field '${s}': ${r}`)}}if(t.length>0)throw new Error(`Output validation failed:
|
|
13
13
|
${t.join(`
|
|
14
|
-
`)}`);return e}};
|
|
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>=
|
|
16
|
-
${
|
|
14
|
+
`)}`);return e}};U();import{writeFileSync as Se,readFileSync as Ze,existsSync as He,mkdirSync as vt}from"node:fs";import{join as we,dirname as Tt}from"node:path";import T from"chalk";var gt="__WORKFLOW_GRAPH_LOG__",Z=T.gray("\u2502"),mt=T.gray("\u250C"),Ce=T.gray("\u2514"),pe=T.green("\u25C6"),Re=T.hex("#c084fc")("\u25C6"),Me=T.hex("#2dd4bf")("\u25C6"),fe=T.red("\u25C6"),Be=`${Z} `,je=2;function De(o){return o<1e3?`${o}ms`:`${(o/1e3).toFixed(1)}s`}function Le(o,e){return(t,s,i)=>{if(typeof t!="string")return o(t,s,i);let r=process.stdout.columns||120,a="";for(let n=0;n<t.length;n++){let c=t[n];e.lineStart&&(a+=Be,e.col=je,e.lineStart=!1),c===`
|
|
15
|
+
`?(a+=c,e.lineStart=!0,e.col=0,e.inEsc=!1):c==="\x1B"?(e.inEsc=!0,a+=c):e.inEsc?(a+=c,(c>="A"&&c<="Z"||c>="a"&&c<="z")&&(e.inEsc=!1)):(e.col++,a+=c,e.col>=r&&(a+=`
|
|
16
|
+
${Be}`,e.col=je))}return o(a,s,i)}}var he=class{constructor(){this._currentNode=null,this._origStdoutWrite=null,this._origStderrWrite=null,this._emitWorkflowGraphMarkers=String(process.env.ZIBBY_EMIT_GRAPH_MARKERS||"").trim()==="1"||String(process.env.ZIBBY_WORKFLOW_GRAPH_LOG_MARKERS||"").trim()==="1"}get isInsideNode(){return this._currentNode!==null}_startIntercepting(){this._origStdoutWrite=process.stdout.write.bind(process.stdout),this._origStderrWrite=process.stderr.write.bind(process.stderr);let e={lineStart:!0,col:0,inEsc:!1},t={lineStart:!0,col:0,inEsc:!1};this._outState=e,this._errState=t,process.stdout.write=Le(this._origStdoutWrite,e),process.stderr.write=Le(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=`${gt}${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(pe,e):process.stdout.write.bind(process.stdout)(`${Z} ${pe} ${e}
|
|
24
|
+
`)}stepInfo(e){this.step(e)}stepTool(e){this._origStdoutWrite?this._writeDot(Re,e):process.stdout.write.bind(process.stdout)(`${Z} ${Re} ${e}
|
|
25
|
+
`)}stepMemory(e){let t=T.hex("#2dd4bf")(e);this._origStdoutWrite?this._writeDot(Me,t):process.stdout.write.bind(process.stdout)(`${Z} ${Me} ${t}
|
|
26
|
+
`)}stepFail(e){this._origStdoutWrite?this._writeDot(fe,T.red(e)):process.stdout.write.bind(process.stdout)(`${Z} ${fe} ${T.red(e)}
|
|
27
|
+
`)}nodeStart(e){this._currentNode=e,this._emitGraphLogMarker({phase:"node_begin",node:e}),this._rawWrite(`${mt} ${e}`),this._startIntercepting()}nodeComplete(e,t={}){this._stopIntercepting();let{duration:s,details:i}=t;if(i)for(let a of i)this._rawWrite(`${pe} ${a}`);let r=s?T.dim(` ${De(s)}`):"";this._rawWrite(`${Ce} ${T.green("done")}${r}`),this._emitGraphLogMarker({phase:"node_end",node:e}),this._rawWrite("")}nodeFailed(e,t,s={}){this._stopIntercepting();let{duration:i}=s,r=i?T.dim(` ${De(i)}`):"";this._rawWrite(`${fe} ${T.red(t)}`),this._rawWrite(`${Ce} ${T.red("failed")}${r}`),this._emitGraphLogMarker({phase:"node_end",node:e}),this._rawWrite("")}route(e,t){this._rawWrite(T.dim(` ${e} \u2192 ${t}`)),this._rawWrite("")}graphComplete(){}},k=new he;var re=".zibby/output",We="sessions",G=".session-info.json",Fe=".zibby-stop";var Ue=["CI_JOB_ID","GITHUB_RUN_ID","CIRCLE_WORKFLOW_ID","BUILD_ID"];var B=class{constructor(e){if(this.config=e,this.name=e.name,this.prompt=e.prompt,this.outputSchema=e.outputSchema,!this.outputSchema&&!e._isCustomCode)throw new Error(`Node '${this.name}' must define outputSchema (Zod schema). This defines the contract for what the node returns to state.`);this.isZodSchema=this.outputSchema&&typeof this.outputSchema._def<"u",this.parser=e.outputSchema&&!this.isZodSchema?new te(e.outputSchema):null,this.retries=e.retries||0,this.onComplete=e.onComplete,this.customExecute=e.execute}async execute(e,t){let s=()=>t&&typeof t.getAll=="function"?t.getAll():e,i=d=>t&&typeof t.get=="function"?t.get(d):e?.[d];if(typeof this.customExecute=="function"){E.debug(`[workflow] node '${this.name}': custom execute (skipping LLM)`);try{let d=await this.customExecute(e);return typeof d=="object"&&d!==null&&d.success===!1?{success:!1,error:d.error||"Node execution failed",raw:d.raw||null}:this.isZodSchema?(E.debug(`[workflow] node '${this.name}': validating output schema`),{success:!0,output:this.outputSchema.parse(d),raw:null}):{success:!0,output:d,raw:null}}catch(d){return E.error(`[workflow] node '${this.name}' failed: ${d.message}`),d.name==="ZodError"&&E.error(`Schema errors: ${JSON.stringify(d.issues||d.errors,null,2)}`),{success:!1,error:d.message,raw:null}}}let r=typeof this.prompt=="function"?this.prompt(s()):this.prompt,a=i("_skillHints");a&&(r=`${a}
|
|
28
28
|
|
|
29
|
-
${
|
|
29
|
+
${r}`);let n=s(),c=n.cwd||process.cwd(),l=n.sessionPath;try{if(l){let d=we(l,G);if(He(d)){let _=JSON.parse(Ze(d,"utf-8"));_.currentNode=this.name,Se(d,JSON.stringify(_,null,2),"utf-8")}let u=we(l,"..",G);if(He(u))try{let _=JSON.parse(Ze(u,"utf-8"));_.currentNode=this.name,Se(u,JSON.stringify(_,null,2),"utf-8")}catch{}}}catch(d){E.debug(`[workflow] could not update session info: ${d.message}`)}let p=null;for(let d=0;d<=this.retries;d++)try{E.debug(`[workflow] node '${this.name}' attempt ${d}`);let u=s().config||{},_=u.agents||{},$=this.config.agent??_[this.name]??null,m={state:s()};$&&(m.preferredAgent=$);let v={workspace:c,schema:this.isZodSchema?this.outputSchema:null,skills:this.config.skills||[],sessionPath:l,config:u,nodeName:this.name,timeout:this.config?.timeout||3e5},f=e?._coreInvokeAgent;f||(f=(await Promise.resolve().then(()=>(q(),K))).invokeAgent);let S=await f(r,m,v),y,b;if(typeof S=="string"?(y=S,b=null):S.structured?(y=S.raw||JSON.stringify(S.structured,null,2),b=S.structured):(y=S.raw||JSON.stringify(S,null,2),b=S.extracted||null),l)try{let h=we(l,this.name,"raw_stream_output.txt");vt(Tt(h),{recursive:!0}),Se(h,typeof y=="string"?y:JSON.stringify(y),"utf-8")}catch(h){E.debug(`[workflow] could not save raw output: ${h.message}`)}if(this.isZodSchema&&b){E.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(s(),b)}catch(P){E.warn(`[workflow] onComplete hook failed: ${P.message}`)}return{success:!0,output:h,raw:y}}if(typeof this.onComplete=="function")try{return{success:!0,output:await this.onComplete(s(),{raw:y}),raw:y}}catch(h){throw new Error(`onComplete failed: ${h.message}`,{cause:h})}if(this.parser){let h=this.parser.parse(y);return E.info(`[workflow] node '${this.name}': parsed output: ${JSON.stringify(h,null,2)}`),k.step("Output parsed"),{success:!0,output:h,raw:y}}return{success:!0,output:y,raw:y}}catch(u){p=u,d<this.retries&&E.info(`[workflow] node '${this.name}' failed, retrying (${d+1}/${this.retries})\u2026`)}return{success:!1,error:p.message,raw:null}}},ne=class extends B{constructor(e){super({...e,_isCustomCode:!0}),this.condition=e.condition}async execute(e,t){let s=t&&typeof t.getAll=="function"?t.getAll():e;return{success:!0,output:{nextNode:this.condition(s)},raw:null}}};U();var kt=2e3,At=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 xt(){let o=process.env.PROJECT_ID;if(!o)throw new Error("Sub-graph dispatch requires PROJECT_ID env var.");return o}function Pt(){let o=process.env.PROJECT_API_TOKEN;if(!o)throw new Error("Sub-graph dispatch requires PROJECT_API_TOKEN env var.");return o}function Ct(){return process.env.EXECUTION_ID||null}function Rt(o,e){return e==null?o:typeof e=="function"?e(o):typeof e=="string"?e.split(".").reduce((t,s)=>t==null?t:t[s],o):o}async function Ke(o,e={}){if(!o||typeof o!="string")throw new Error("dispatchSubgraph: workflowName (string) is required");let t=Ot(),s=xt(),i=Pt(),r=Ct(),a=`${t}/projects/${encodeURIComponent(s)}/workflows/${encodeURIComponent(o)}/trigger`,n={input:e.input||{},...r?{parentExecutionId:r}:{},...e.conversationId?{conversationId:e.conversationId}:{}};E.info(`[sub-graph] dispatching '${o}' (${e.async?"async":"sync"}) from parent ${r||"<none>"}`);let c=await fetch(a,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${i}`},body:JSON.stringify(n)});if(!c.ok){let S=null,y="";try{S=await c.json(),y=S?.error||S?.message||JSON.stringify(S)}catch{y=await c.text().catch(()=>"")}if(c.status===429){let h=S?.quotaInfo||{},P=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 P.code="SUBGRAPH_QUOTA_EXCEEDED",P.status=429,P.subgraph=o,P.quotaInfo=h,P}if(c.status===400){let h=new Error(`Sub-graph '${o}' rejected input: ${y}`);throw h.code="SUBGRAPH_INVALID_INPUT",h.status=400,h.subgraph=o,h.validationErrors=S?.validationErrors||null,h.missing=S?.missing||null,h}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(),p=l?.data?.jobId||l?.jobId;if(!p)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=${p} (not waiting)`),{jobId:p,status:"accepted",workflow:o};let d=Number.isFinite(e.timeoutMs)?e.timeoutMs:At,u=Number.isFinite(e.pollIntervalMs)?e.pollIntervalMs:kt,_=`${t}/executions/${encodeURIComponent(p)}`,$=Date.now()+d,m="accepted",v=0;for(;Date.now()<$;){await new Promise(h=>setTimeout(h,u)),v+=1;let S=await fetch(_,{headers:{Authorization:`Bearer ${i}`}});if(!S.ok){if(S.status>=500){E.warn(`[sub-graph] status poll for ${p} returned ${S.status}, will retry`);continue}throw new Error(`Sub-graph status poll failed for ${p}: ${S.status}`)}let y=await S.json(),b=y?.data||y?.execution||y;if(m=b?.status||m,Nt.has(m)){if(m!=="completed"){let L=new Error(`Sub-graph '${o}' (${p}) ended in status '${m}'`);throw L.subgraphJobId=p,L.subgraphStatus=m,L}let h=b?.finalState||b?.state||{},P=Rt(h,e.output);return E.info(`[sub-graph] '${o}' (${p}) completed after ${v} polls`),P}}let f=new Error(`Sub-graph '${o}' (${p}) timed out after ${Math.round(d/1e3)}s (last status: ${m})`);throw f.subgraphJobId=p,f.subgraphStatus=m,f}import{existsSync as qe,readFileSync as Mt}from"node:fs";import{join as ye,dirname as ze}from"node:path";var ie=class{static async loadContext(e,t,s={}){let i={},r=s.filenames||["CONTEXT.md","AGENTS.md"];if(e){let n=ze(ye(t,e));for(let c of r){let l=await this.findAndMergeContextFiles(c,n,t);if(l){let p=c.replace(/\.[^.]+$/,"").toLowerCase();i[p]=l}}}let a=s.discovery||{};for(let[n,c]of Object.entries(a))try{let l=ye(t,c);qe(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,s){let i=[],r=t;for(;r.startsWith(s);){let a=ye(r,e);if(qe(a))try{i.unshift(await this.loadFile(a))}catch(c){console.warn(`[workflow] could not load ${e} from ${a}: ${c.message}`)}let n=ze(r);if(n===r)break;r=n}return i.length===0?null:i.every(a=>typeof a=="string")?i.join(`
|
|
30
30
|
|
|
31
31
|
---
|
|
32
32
|
|
|
33
|
-
`):
|
|
33
|
+
`):i.every(a=>typeof a=="object")?Object.assign({},...i):i[i.length-1]}static async loadFile(e){let t=Mt(e,"utf-8");if(e.endsWith(".json"))return JSON.parse(t);if(e.endsWith(".js")||e.endsWith(".mjs")){let{pathToFileURL:s}=await import("url"),i=await import(s(e).href);return i.default||i}return t}};import{mkdirSync as Qe,existsSync as _e,writeFileSync as Ve,unlinkSync as Bt}from"node:fs";import{join as D,resolve as et}from"node:path";import{config as jt}from"dotenv";import{zodToJsonSchema as Xe}from"zod-to-json-schema";import Dt from"handlebars";function Lt({traceFrom:o,sessionId:e,sessionPath:t,idSource:s,mkdirFresh:i}){if(!(process.env.ZIBBY_SESSION_LOG==="1"||process.env.ZIBBY_SESSION_LOG==="true"))return;let a=typeof process.ppid=="number"?process.ppid:"n/a",n=`[zibby:session] from=${o} pid=${process.pid} ppid=${a} sessionId=${e} source=${s} mkdir=${i?"yes":"no"} path=${t}`;if(console.log(n),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
|
-
${
|
|
37
|
-
\u{1F6D1} External stop requested \u2014 ending workflow.`),
|
|
38
|
-
${
|
|
36
|
+
${p}`)}}function Wt(){return process.env.ZIBBY_TRUST_SESSION_ENV==="1"||process.env.ZIBBY_TRUST_SESSION_ENV==="true"||process.env.ZIBBY_KEEP_SESSION_ENV==="1"||process.env.ZIBBY_KEEP_SESSION_ENV==="true"}function 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 et(String(e).trim())}catch{return String(e).trim()}}function Ut(){Wt()||(delete process.env.ZIBBY_SESSION_PATH,delete process.env.ZIBBY_SESSION_ID)}function Gt({sessionPath:o,sessionId:e}){o&&typeof o=="string"&&(process.env.ZIBBY_SESSION_PATH=o),e!=null&&String(e).trim()!==""&&(process.env.ZIBBY_SESSION_ID=String(e).trim())}function Yt(o={}){let e=Ue.map(r=>process.env[r]).find(Boolean),t=Math.random().toString(36).slice(2,6),s=e||`${Date.now()}_${t}`,i=o.paths?.sessionPrefix;return i?`${i}_${s}`:s}function Jt({cwd:o=process.cwd(),config:e={},initialState:t={},traceFrom:s="resolveWorkflowSession"}={}){let i=t.sessionPath,r=t.sessionTimestamp,a="initialState.sessionPath";if(!i&&process.env.ZIBBY_SESSION_PATH)try{let l=et(String(process.env.ZIBBY_SESSION_PATH));l&&(i=l,a="ZIBBY_SESSION_PATH")}catch{}let n;if(i)n=String(i).split(/[/\\]/).filter(Boolean).pop(),r==null&&(r=Date.now());else{let l=process.env.ZIBBY_SESSION_ID&&String(process.env.ZIBBY_SESSION_ID).trim();if(l)n=l,a="ZIBBY_SESSION_ID";else{let d=e.sessionId!=null?String(e.sessionId).trim():"";d&&d!=="last"?(n=d,a="config.sessionId"):(n=Yt(e),a="generated")}r=r??Date.now();let p=e.paths?.output||re;i=D(o,p,We,n)}let c=!_e(i);return c&&Qe(i,{recursive:!0}),(c||a!=="initialState.sessionPath")&&Lt({traceFrom:s,sessionId:n,sessionPath:i,idSource:a,mkdirFresh:c}),Gt({sessionPath:i,sessionId:n}),{sessionPath:i,sessionId:n,sessionTimestamp:r}}var ae=class{constructor(e={}){this.nodes=new Map,this.edges=new Map,this.entryPoint=null,this.middleware=Array.isArray(e.middleware)?[...e.middleware]:[],e.nodeMiddleware&&this.middleware.push(e.nodeMiddleware),this.nodeTypeMap=new Map,this.conditionalCodeMap=new Map,this.stateSchema=e.stateSchema||null,this.nodePrompts=new Map,this.nodeOptions=new Map,this._invokeAgent=e.invokeAgent||null,this._compiledPrompts=new Map}setStateSchema(e){return this.stateSchema=e,this}getStateSchema(){return this.stateSchema}addNode(e,t,s={}){if(!(t instanceof B)&&t&&typeof t=="object"&&typeof t.workflow=="string"){let r=t,a={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,p;return typeof r.input=="function"?p=r.input(l):r.input&&typeof r.input=="object"?p=r.input:p={},Ke(r.workflow,{input:p,async:r.async===!0,conversationId:typeof r.conversationId=="function"?r.conversationId(l):r.conversationId,output:r.output,timeoutMs:r.timeoutMs,pollIntervalMs:r.pollIntervalMs})}},n=new B(a);return n.name=e,this.nodes.set(e,n),s.prompt&&this.nodePrompts.set(e,s.prompt),Object.keys(s).length>0&&this.nodeOptions.set(e,s),this}let i=t instanceof B?t:new B(t);return i.name=e,this.nodes.set(e,i),s.prompt&&this.nodePrompts.set(e,s.prompt),Object.keys(s).length>0&&this.nodeOptions.set(e,s),this}addConditionalNode(e,t){return this.nodes.set(e,new ne({...t,name:e})),this}addEdge(e,t){return this.edges.set(e,t),this}setNodeType(e,t){return this.nodeTypeMap.set(e,t),this}addConditionalEdges(e,t,{labels:s}={}){return this.edges.set(e,{conditional:!0,routes:t,labels:s}),typeof t=="function"&&this.conditionalCodeMap.set(e,t.toString()),this}setEntryPoint(e){return this.entryPoint=e,this}use(e){return typeof e=="function"&&this.middleware.push(e),this}_composeMiddleware(e,t,s,i,r){let a=s;for(let n=e.length-1;n>=0;n--){let c=e[n],l=a;a=()=>c(t,l,i,r)}return a()}serialize(){let e=[],t={};for(let[r,a]of this.nodes){let n=this.nodeTypeMap.get(r)||r;e.push({id:r,type:n,data:{nodeType:n,label:r}});let c={};a._isCustomCode&&typeof a.execute=="function"&&(c.customCode=a.execute.toString());let l=this.nodePrompts.get(r);if(l&&(c.prompt=l),typeof a.customExecute=="function"&&(c.executeCode=a.customExecute.toString()),a.outputSchema)try{if(typeof a.outputSchema._def<"u"){let d=Xe(a.outputSchema,{target:"openApi3"});c.outputSchema={jsonSchema:d,variables:this._flattenJsonSchemaToVariables(d)}}else c.outputSchema={schema:a.outputSchema}}catch(d){console.warn(`[workflow] failed to convert schema for ${r}:`,d.message)}let p=(this.resolvedToolsMap||{})[r];p?.toolIds&&(c.tools=p.toolIds),Object.keys(c).length>0&&(t[r]=c)}let s=[];for(let[r,a]of this.edges)if(typeof a=="string")s.push({source:r,target:a});else if(a.conditional){let n=this.conditionalCodeMap.get(r)||a.routes.toString(),c=this._inferConditionalTargets(a.routes),l=a.labels||{};for(let p of c){let d={source:r,target:p,data:{conditionalCode:n}};l[p]&&(d.label=l[p]),s.push(d)}}let i=null;if(this.stateSchema)try{i=Xe(this.stateSchema,{target:"openApi3"})}catch{i=this.stateSchema}return{nodes:e,edges:s,nodeConfigs:t,stateSchema:i}}_inferConditionalTargets(e){let t=e.toString(),s=new Set,i=/return\s+['"]([^'"]+)['"]/g,r;for(;(r=i.exec(t))!==null;)s.add(r[1]);return[...s]}_flattenJsonSchemaToVariables(e,t=""){let s=e;if(e.$ref&&e.definitions){let i=e.$ref.replace("#/definitions/","");s=e.definitions[i]||e}return this._flattenSchema(s,t)}_flattenSchema(e,t=""){if(!e||typeof e!="object")return[];let s=[],i=e.properties||{},r=e.required||[];for(let[a,n]of Object.entries(i)){let c=t?`${t}.${a}`:a;s.push({path:c,type:n.type||"unknown",label:n.description||this._formatLabel(a),optional:!r.includes(a)}),n.type==="object"&&n.properties&&s.push(...this._flattenSchema(n,c)),n.type==="array"&&n.items?.type==="object"&&n.items.properties&&s.push(...this._flattenSchema(n.items,`${c}[]`))}return s}_formatLabel(e){return e.replace(/([A-Z])/g," $1").replace(/^./,t=>t.toUpperCase()).trim()}_summarizeNodeOutput(e,t){if(!t||typeof t!="object")return[];let s=[];t.success!==void 0&&s.push(`Result: ${t.success?"passed":"failed"}`);for(let[i,r]of Object.entries(t))if(!(i==="success"||i==="raw"||i==="nextNode")){if(typeof r=="string"&&r.length<=80)s.push(`${i}: ${r}`);else if(Array.isArray(r)){let a=r.length,n=r.filter(l=>l?.passed===!0).length,c=r.some(l=>l?.passed!==void 0);s.push(c?`${i}: ${n}/${a} passed${a-n?`, ${a-n} failed`:""}`:`${i}: ${a} items`)}if(s.length>=4)break}return s}async run(e,t={},s={}){if(!this.entryPoint)throw new Error("No entry point set for graph");let i=new AbortController;s.signal&&(s.signal.aborted?i.abort():s.signal.addEventListener("abort",()=>i.abort(),{once:!0}));let r=s.strategyAbortTimeoutMs??t.config?.strategyAbortTimeoutMs??5e3,a=t.cwd||process.cwd();jt({path:D(a,".env")});let n=t.config||{};if(!n||Object.keys(n).length===0)try{let w=D(a,".zibby.config.js");_e(w)&&(n=(await import(w)).default||{})}catch{}process.env.EXECUTION_ID&&!n.agent?.strictMode&&(n.agent={...n.agent,strictMode:!0});let c=t.agentType;if(!c){let w=n?.agent;w?.provider?c=w.provider:w?.gemini?c="gemini":w?.claude?c="claude":w?.cursor?c="cursor":w?.codex?c="codex":c=process.env.AGENT_TYPE||"cursor"}let l=t.contextConfig||e?.config?.contextConfig||e?.config?.context||n?.context||{};if(this.stateSchema){let w=this.stateSchema.safeParse(t);if(!w.success){let A=w.error.issues.map(N=>`${N.path.join(".")}: ${N.message}`);throw console.error("\u274C Initial state validation failed:"),A.forEach(N=>console.error(` - ${N}`)),new Error(`State validation failed: ${A.join(", ")}`)}k.step("State validated against schema")}let p=Ft(),d=t.sessionPath||p;d||Ut();let{sessionPath:u,sessionTimestamp:_,sessionId:$}=Jt({cwd:a,config:n,traceFrom:"WorkflowGraph.run",initialState:{sessionPath:d,sessionTimestamp:t.sessionTimestamp}});k.step(`Session ${$}`);let m=await ie.loadContext(t.specPath||"",a,l);Object.keys(m).length>0&&k.step(`Context loaded: ${Object.keys(m).join(", ")}`);let v=t.outputPath;!v&&t.specPath&&(e?.calculateOutputPath?v=e.calculateOutputPath(t.specPath):console.warn(`\u26A0\uFE0F outputPath not resolved (specPath=${t.specPath})`));let f=new ee({...t,config:n,agentType:c,outputPath:v,sessionPath:u,sessionTimestamp:_,context:m,resolvedTools:this.resolvedToolsMap||{},_signal:i.signal}),S=new Map;try{await import("@zibby/skills")}catch{}let{getSkill:y}=await Promise.resolve().then(()=>(se(),Ye)),b=n.skills&&typeof n.skills=="object"?n.skills:{},h=Object.values(b).filter(w=>w&&typeof w=="object"&&typeof w.id=="string"),P=w=>{for(let A of h)if(A.id===w)return A;return y(w)},L=new Set;for(let[,w]of this.nodes)for(let A of w.config?.skills||[])L.add(A);for(let w of L){let A=P(w);if(typeof A?.middleware=="function")try{let N=await A.middleware();typeof N=="function"&&S.set(w,N)}catch{}}let g=this.entryPoint,z=[],be=n?.recursionLimit??100,nt=0;try{for(;g&&g!=="END";){if(++nt>be)throw new Error(`Workflow exceeded recursion limit (${be}) \u2014 likely a cyclic conditional route. Set config.recursionLimit if you need a higher cap.`);let A=D(u,Fe);if(_e(A)){try{Bt(A)}catch{}i.abort()}if(i.signal.aborted)return console.warn(`
|
|
37
|
+
\u{1F6D1} External stop requested \u2014 ending workflow.`),k.step("Workflow stopped externally"),{success:!0,state:f.getAll(),executionLog:z,stoppedExternally:!0};let N=this.nodes.get(g);if(!N)throw new Error(`Node '${g}' not found in graph`);let ve=JSON.stringify({sessionPath:u,sessionTimestamp:_,currentNode:g,createdAt:new Date().toISOString(),config:f.get("config")}),it=D(u,G);Ve(it,ve,"utf-8");let Te=f.get("config")?.paths?.output||re,at=D(a,Te,G);Qe(D(a,Te),{recursive:!0});try{Ve(at,ve,"utf-8")}catch{}let ke=t.onPipelineProgress;if(typeof ke=="function")try{ke({cwd:a,sessionPath:u,sessionId:$,outputBase:f.get("config")?.paths?.output||re,currentNode:g})}catch{}let ct=(this.resolvedToolsMap||{})[g]||null;f.set("_currentNodeTools",ct);let lt=f.get("nodeConfigs")||{};f.set("_currentNodeConfig",lt[g]||{}),k.nodeStart(g);let Ae=Date.now(),V=this.nodePrompts.get(g);if(!this._invokeAgent){let O=await Promise.resolve().then(()=>(q(),K));this._invokeAgent=O.invokeAgent}let ut=this._invokeAgent,le={},dt=N.config?.skills||[];for(let O of dt){let x=P(O);if(typeof x?.invokeAgentOptions=="function")try{let I=x.invokeAgentOptions(f.getAll(),{agentType:f.get("agentType"),nodeName:g});I&&typeof I=="object"&&(le={...le,...I})}catch(I){console.warn(`[graph] skill '${O}' invokeAgentOptions threw: ${I.message}`)}}let Ne=async(O,x,I={})=>{let C=ut(O,x,{...le,...I,signal:i.signal});return C.catch(()=>{}),i.signal.aborted?C:Promise.race([C,new Promise((W,F)=>{let M=()=>{setTimeout(()=>{let J=new Error(`Strategy ignored AbortSignal \u2014 engine deadman fired after ${r}ms`);J.name="AbortError",F(J)},r)};i.signal.addEventListener("abort",M,{once:!0})})])},Oe={state:f,invokeAgent:async(O={},x={})=>{let I=x.prompt||"";if(V){let C=this._compiledPrompts.get(g);C||(C=Dt.compile(V,{noEscape:!0}),this._compiledPrompts.set(g,C));try{I=C(O)}catch(W){throw console.error(`\u274C Template rendering failed for node '${g}':`,W.message),new Error(`Template rendering failed: ${W.message}`,{cause:W})}}else if(!I)throw new Error(`No prompt template configured for node '${g}' and no prompt provided in options`);return Ne(I,{state:f.getAll(),images:x.images||[]},{model:x.model||f.get("model"),workspace:f.get("workspace"),schema:x.schema,...x,signal:i.signal})},_coreInvokeAgent:Ne,agent:e,nodeId:g,promptTemplate:V,getPromptTemplate:()=>V,...f.getAll()};try{let O=(N.config?.skills||[]).map(M=>S.get(M)).filter(Boolean),x=[...this.middleware,...O],I;x.length>0?I=await this._composeMiddleware(x,g,async()=>N.execute(Oe,f),f.getAll(),f):I=await N.execute(Oe,f);let C=Date.now()-Ae;if(z.push({node:g,success:I.success,duration:C,timestamp:new Date().toISOString()}),!I.success){if(i.signal.aborted)return k.step("Workflow stopped externally"),{success:!0,state:f.getAll(),executionLog:z,stoppedExternally:!0};f.append("errors",{node:g,error:I.error});let M=N.config?.retries||0,J=`${g}_retries`,X=f.getAll()[J]||0;if(X<M){k.stepInfo(`Retrying (attempt ${X+1}/${M})`),f.update({[J]:X+1,[`${g}_raw`]:I.raw});continue}throw k.nodeFailed(g,I.error,{duration:C}),new Error(`Node '${g}' failed after ${X} attempts: ${I.error}`)}f.update({[g]:I.output});let W=this._summarizeNodeOutput(g,I.output);k.nodeComplete(g,{duration:C,details:W});let F=this.edges.get(g);if(!F)g="END";else if(F.conditional){let M=F.routes(f.getAll());k.route(g,M),g=M}else g=F}catch(O){throw k.isInsideNode&&k.nodeFailed(g,O.message,{duration:Date.now()-Ae}),f.set("failed",!0),f.set("failedAt",g),O}}k.graphComplete();let w={success:!0,state:f.getAll(),executionLog:z};return e&&typeof e.onComplete=="function"&&await e.onComplete(w),w}finally{if(e&&typeof e.cleanup=="function")try{await e.cleanup()}catch(w){console.warn(`[workflow] agent.cleanup() failed: ${w.message}`)}}}};var Ee=Symbol.for("@zibby/agent-workflow.nodes");globalThis[Ee]||(globalThis[Ee]=new Map);var $e=globalThis[Ee];function Zt(o,e){$e.set(o,e)}function tt(o){return $e.get(o)}function Ie(o){return $e.has(o)}Zt("ai_agent",{name:"ai_agent",factory:!0,create:(o,e={})=>({name:o,_isCustomCode:!0,execute:async t=>{let s=t?._coreInvokeAgent;s||(s=(await Promise.resolve().then(()=>(q(),K))).invokeAgent);let i=e.extraPromptInstructions||"Execute the task based on the current state.",r=Ht(i,t),a=await s(r,{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 Ht(o,e){let t=/@([\w.]+)/g,s=new Set,i;for(;(i=t.exec(o))!==null;)s.add(i[1]);if(s.size===0)return o;let r=[],a=new Set;for(let n of s){let c=n.split(".")[0];if(a.has(c))continue;let l=n.split(".").reduce((u,_)=>u?.[_],e);if(l===void 0)continue;let p=typeof l=="string"?l:l?.raw??JSON.stringify(l,null,2),d=n.replace(/_/g," ").replace(/\b\w/g,u=>u.toUpperCase());r.push(`## ${d}
|
|
38
|
+
${p}`),n.includes(".")||a.add(c)}return r.length===0?o:`${o}
|
|
39
39
|
|
|
40
40
|
---
|
|
41
41
|
# Referenced Context
|
|
42
42
|
|
|
43
|
-
${
|
|
43
|
+
${r.join(`
|
|
44
44
|
|
|
45
|
-
`)}`}
|
|
45
|
+
`)}`}se();U();var Kt={};function rt(o,e){if(Array.isArray(e))return ot(e);let t=Kt[o];return!t||t.length===0?null:ot(t)}function ot(o){if(!Array.isArray(o)||o.length===0)return null;let e=[],t={},s=[];for(let i of o){let r=H(i);if(!r){E.warn(`[workflow] unknown skill "${i}" \u2014 skipping`);continue}s.push(i);for(let a of r.tools||[])e.push({name:a.name,description:a.description,input_schema:a.input_schema||{type:"object",properties:{}}});if(!t[r.serverName])if(typeof r.resolve=="function"){let a=r.resolve();a&&(t[r.serverName]={...a,toolPrefix:i})}else{let a={};for(let n of r.envKeys||[]){let c=process.env[n];c&&(a[n]=c)}t[r.serverName]={command:r.command,args:[...r.args||[]],env:a,toolPrefix:i}}}return s.length===0?null:{toolIds:s,claudeTools:e,mcpServers:t}}U();function Go(o,e={}){let{nodes:t,edges:s,nodeConfigs:i={}}=o;if(!Array.isArray(t)||t.length===0)throw new R("Graph must have at least one node");if(!Array.isArray(s))throw new R("Graph edges must be an array");let r=new ae(e);e.stateSchema&&r.setStateSchema(e.stateSchema);let a=new Set,n=new Map,c={};for(let u of t){let _=ce(u);n.set(u.id,{...u,resolvedType:_}),_==="decision"&&a.add(u.id)}for(let[u,_]of n){if(a.has(u))continue;let $=_.resolvedType,m=i[u]||{},v=rt($,m.tools);v&&(c[u]=v);let f={};m.prompt&&(f.prompt=m.prompt);let S=Ie($);if(E.debug(`[workflow] compiler: node "${u}" type="${$}" registered=${S}`),m.customCode&&!S)r.addNode(u,st(u,m.customCode,m),f),r.setNodeType(u,$);else if(S){let y=tt($);y.factory?r.addNode(u,y.create(u,{...m,resolvedTools:v}),f):r.addNode(u,y,f),r.setNodeType(u,$)}else if(m.executeCode)r.addNode(u,st(u,m.executeCode,m),f),r.setNodeType(u,$);else throw new R(`Unknown node type "${$}" for node "${u}". Did you forget to register it?`)}r.resolvedToolsMap=c;let l=new Set;for(let u of s)a.has(u.target)||l.add(u.target);let p=t.find(u=>!a.has(u.id)&&!l.has(u.id));if(!p)throw new R("Could not determine entry point: no node without incoming edges found");r.setEntryPoint(p.id);let d=qt(s,"source");for(let u of s)if(!a.has(u.source))if(a.has(u.target)){let _=u.target,$=d.get(_)||[];if($.length===0)throw new R(`Decision node "${_}" has no outgoing edges`);let m=zt(_,$,a);r.addConditionalEdges(u.source,m)}else r.addEdge(u.source,u.target);return r}function Yo(o){let e=[];if(!o||typeof o!="object")return{valid:!1,errors:["Config must be a non-null object"]};if((!Array.isArray(o.nodes)||o.nodes.length===0)&&e.push("Graph must have at least one node"),Array.isArray(o.edges)||e.push("Graph edges must be an array"),e.length>0)return{valid:!1,errors:e};let t=o.nodeConfigs||{};for(let n of o.nodes){let c=ce(n);if(c==="decision"||Ie(c))continue;let l=t[n.id]||{};l.customCode||l.executeCode||e.push(`Unknown node type "${c}" for node "${n.id}". Register it or provide customCode/executeCode.`)}let s=new Set(o.nodes.map(n=>n.id));for(let n of o.edges)s.has(n.source)||e.push(`Edge references unknown source node "${n.source}"`),s.has(n.target)||e.push(`Edge references unknown target node "${n.target}"`);let i=new Set(o.nodes.filter(n=>ce(n)==="decision").map(n=>n.id)),r=new Set;for(let n of o.edges)i.has(n.target)||r.add(n.target);let a=o.nodes.filter(n=>!i.has(n.id)&&!r.has(n.id));a.length===0?e.push("No entry point found (every node has incoming edges)"):a.length>1&&e.push(`Multiple entry points found: ${a.map(n=>n.id).join(", ")}`);for(let n of i){let c=o.edges.filter(p=>p.source===n);c.length===0&&e.push(`Decision node "${n}" has no outgoing edges`),c.some(p=>p.data?.conditionalCode||p.conditionalCode)||e.push(`Decision node "${n}" outgoing edges have no conditionalCode`)}return{valid:e.length===0,errors:e}}function Jo(o){return!o||!Array.isArray(o.nodes)?[]:o.nodes.filter(e=>ce(e)!=="decision").map(e=>e.id)}function ce(o){let e=o.data?.nodeType||o.data?.type||o.type;return e==="workflowNode"||e==="custom"||e==="default"?o.id:e}function qt(o,e){let t=new Map;for(let s of o){let i=s[e];t.has(i)||t.set(i,[]),t.get(i).push(s)}return t}function zt(o,e,t){let s=e.find(n=>n.data?.conditionalCode||n.conditionalCode);if(!s)throw new R(`Decision node "${o}" has no conditionalCode on its outgoing edges`);let i=s.data?.conditionalCode||s.conditionalCode,r=new Set(e.map(n=>n.target).filter(n=>!t.has(n))),a;try{let c=new Function(`return (${i})`)();a=l=>{let p=c(l);return r.has(p)||E.warn(`[workflow] conditional route from "${o}" returned "${p}" which is not in valid targets: ${[...r].join(", ")}`),p}}catch(n){throw new R(`Failed to compile conditionalCode for "${o}": ${n.message}`)}return a}function st(o,e,t={}){let s;try{s=new Function("invokeAgent","require","console",`return (${e})`)}catch(a){throw new R(`Failed to compile customCode for node "${o}": ${a.message}`)}let i=s(async(...a)=>{let{invokeAgent:n}=await Promise.resolve().then(()=>(q(),K));return n(...a)},typeof ue<"u"?ue:void 0,console),r=null;return t.outputSchema&&(r=t.outputSchema.jsonSchema||t.outputSchema),{name:o,_isCustomCode:!0,outputSchema:r,execute:async a=>{try{let n=await i(a);return typeof n=="object"&&"success"in n?n:{success:!0,output:n,raw:null}}catch(n){return{success:!1,error:n.message,raw:null}}}}}var R=class extends Error{constructor(e){super(e),this.name="CompilationError"}};export{R as CompilationError,Go as compileGraph,Jo as extractSteps,Yo as validateGraphConfig};
|
package/dist/graph.js
CHANGED
|
@@ -1,37 +1,37 @@
|
|
|
1
|
-
var
|
|
1
|
+
var se=Object.defineProperty;var K=(s,t)=>()=>(s&&(t=s(s=0)),t);var bt=(s,t)=>{for(var e in t)se(s,e,{get:t[e],enumerable:!0})};var vt,re,q,w,X=K(()=>{vt=()=>{},re={debug:vt,info:vt,warn:(...s)=>console.warn("[workflow]",...s),error:(...s)=>console.error("[workflow]",...s)},q={impl:re},w={debug:(...s)=>q.impl.debug?.(...s),info:(...s)=>q.impl.info?.(...s),warn:(...s)=>q.impl.warn?.(...s),error:(...s)=>q.impl.error?.(...s)}});var Bt=K(()=>{});var Lt={};bt(Lt,{clearSkills:()=>pe,getAllSkills:()=>le,getSkill:()=>ct,hasSkill:()=>ce,listSkillIds:()=>ue,registerSkill:()=>ae});function ae(s){if(!s||typeof s.id!="string")throw new Error("Skill definition must include a string id");U.set(s.id,Object.freeze({...s}))}function ct(s){return U.get(s)||null}function ce(s){return U.has(s)}function le(){return new Map(U)}function ue(){return Array.from(U.keys())}function pe(){U.clear()}var at,U,lt=K(()=>{at=Symbol.for("@zibby/agent-workflow.skills");globalThis[at]||(globalThis[at]=new Map);U=globalThis[at]});var pt={};bt(pt,{getAgentStrategy:()=>Wt,invokeAgent:()=>he,listStrategies:()=>fe,registerStrategy:()=>de});function de(s){if(!s||typeof s.getName!="function"||typeof s.invoke!="function")throw new Error("strategy must implement getName() and invoke() (AgentStrategy shape)");let t=B.findIndex(e=>e.getName()===s.getName());t>=0?B[t]=s:B.push(s)}function fe(){return B.map(s=>s.getName())}function Wt(s={}){let{state:t={},preferredAgent:e=null}=s,o=e||t.agentType||process.env.AGENT_TYPE;if(!o){let n=B.map(i=>i.getName()).join(", ")||"none registered";throw new Error(`No agent specified. Set agentType in state or AGENT_TYPE env var. Available: ${n}`)}w.debug(`[workflow] agent selection: requested=${o}`);let r=B.find(n=>n.getName()===o);if(!r){let n=B.map(i=>i.getName()).join(", ")||"none registered";throw new Error(`Unknown agent '${o}'. Available: ${n}`)}if(!r.canHandle(s))throw new Error(`Agent '${o}' is not available in this environment. Check credentials/environment.`);return w.debug(`[workflow] using agent: ${r.getName()}`),r}async function he(s,t={},e={}){let o=Wt(t),r=t.state?.config||e.config||{},n=r.models||{},i=e.nodeName&&n[e.nodeName]||null,a=n.default||null,c=r.agent?.[o.name]?.model||null,u=i||a||c||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:r},l=s,y=p.skills||[];if(y.length>0&&!e.skipPromptFragments){let R=y.map(I=>{let N=ct(I)?.promptFragment;return typeof N=="function"?N():N}).filter(Boolean);R.length>0&&(l+=`
|
|
2
2
|
|
|
3
|
-
${
|
|
3
|
+
${R.join(`
|
|
4
4
|
|
|
5
|
-
`)}`)}let
|
|
5
|
+
`)}`)}let v=t.state?._currentNodeConfig?.extraPromptInstructions?.trim();return v&&(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
|
+
${v}
|
|
12
|
+
`),w.debug(`[workflow] prompt length: ${l.length} chars`),o.invoke(l,p)}var ut,B,dt=K(()=>{Bt();X();lt();ut=Symbol.for("@zibby/agent-workflow.strategies");globalThis[ut]||(globalThis[ut]=[]);B=globalThis[ut]});var oe=new Set(["__proto__","constructor","prototype"]);function ot(s){if(oe.has(s))throw new Error(`Invalid state key: "${s}"`)}var V=class{constructor(t={}){this._state=Object.create(null),Object.assign(this._state,{messages:[],errors:[],artifacts:{},metadata:{},...t}),this._history=[]}get(t){return this._state[t]}set(t,e){ot(t),this._history.push({...this._state}),this._state[t]=e}update(t){let e=Object.getOwnPropertyNames(t);for(let o of e)ot(o);this._history.push({...this._state});for(let o of e)this._state[o]=t[o]}append(t,e){ot(t),this._history.push({...this._state}),Array.isArray(this._state[t])||(this._state[t]=[]),this._state[t].push(e)}getAll(){return{...this._state}}rollback(){this._history.length>0&&(this._state=this._history.pop())}};var z=class{constructor(t){this.schema=t}parse(t){let e=t.match(/```json\s*([\s\S]*?)\s*```/);if(e)return this.validate(JSON.parse(e[1]));let o=[t.match(/\{[\s\S]*?\}/),t.match(/\{[\s\S]*\}/)].filter(Boolean).map(r=>r[0]);for(let r of o)try{return this.validate(JSON.parse(r))}catch(n){if(!(n instanceof SyntaxError))throw n}return this.validate({result:t.trim()})}validate(t){let e=[];for(let[o,r]of Object.entries(this.schema)){if(r.required&&!(o in t)&&e.push(`Missing required field: ${o}`),o in t&&r.type){let n=typeof t[o];n!==r.type&&e.push(`Field '${o}' expected ${r.type}, got ${n}`)}if(r.validate&&o in t){let n=r.validate(t[o]);n&&e.push(`Field '${o}': ${n}`)}}if(e.length>0)throw new Error(`Output validation failed:
|
|
13
13
|
${e.join(`
|
|
14
|
-
`)}`);return t}};
|
|
15
|
-
`?(
|
|
16
|
-
${
|
|
14
|
+
`)}`);return t}};X();import{writeFileSync as ft,readFileSync as jt,existsSync as Dt,mkdirSync as ge}from"node:fs";import{join as ht,dirname as me}from"node:path";import $ from"chalk";var ne="__WORKFLOW_GRAPH_LOG__",J=$.gray("\u2502"),ie=$.gray("\u250C"),Ot=$.gray("\u2514"),rt=$.green("\u25C6"),Tt=$.hex("#c084fc")("\u25C6"),At=$.hex("#2dd4bf")("\u25C6"),nt=$.red("\u25C6"),kt=`${J} `,Nt=2;function Pt(s){return s<1e3?`${s}ms`:`${(s/1e3).toFixed(1)}s`}function xt(s,t){return(e,o,r)=>{if(typeof e!="string")return s(e,o,r);let n=process.stdout.columns||120,i="";for(let a=0;a<e.length;a++){let c=e[a];t.lineStart&&(i+=kt,t.col=Nt,t.lineStart=!1),c===`
|
|
15
|
+
`?(i+=c,t.lineStart=!0,t.col=0,t.inEsc=!1):c==="\x1B"?(t.inEsc=!0,i+=c):t.inEsc?(i+=c,(c>="A"&&c<="Z"||c>="a"&&c<="z")&&(t.inEsc=!1)):(t.col++,i+=c,t.col>=n&&(i+=`
|
|
16
|
+
${kt}`,t.col=Nt))}return s(i,o,r)}}var it=class{constructor(){this._currentNode=null,this._origStdoutWrite=null,this._origStderrWrite=null,this._emitWorkflowGraphMarkers=String(process.env.ZIBBY_EMIT_GRAPH_MARKERS||"").trim()==="1"||String(process.env.ZIBBY_WORKFLOW_GRAPH_LOG_MARKERS||"").trim()==="1"}get isInsideNode(){return this._currentNode!==null}_startIntercepting(){this._origStdoutWrite=process.stdout.write.bind(process.stdout),this._origStderrWrite=process.stderr.write.bind(process.stderr);let t={lineStart:!0,col:0,inEsc:!1},e={lineStart:!0,col:0,inEsc:!1};this._outState=t,this._errState=e,process.stdout.write=xt(this._origStdoutWrite,t),process.stderr.write=xt(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=`${ne}${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(rt,t):process.stdout.write.bind(process.stdout)(`${J} ${rt} ${t}
|
|
24
|
+
`)}stepInfo(t){this.step(t)}stepTool(t){this._origStdoutWrite?this._writeDot(Tt,t):process.stdout.write.bind(process.stdout)(`${J} ${Tt} ${t}
|
|
25
|
+
`)}stepMemory(t){let e=$.hex("#2dd4bf")(t);this._origStdoutWrite?this._writeDot(At,e):process.stdout.write.bind(process.stdout)(`${J} ${At} ${e}
|
|
26
|
+
`)}stepFail(t){this._origStdoutWrite?this._writeDot(nt,$.red(t)):process.stdout.write.bind(process.stdout)(`${J} ${nt} ${$.red(t)}
|
|
27
|
+
`)}nodeStart(t){this._currentNode=t,this._emitGraphLogMarker({phase:"node_begin",node:t}),this._rawWrite(`${ie} ${t}`),this._startIntercepting()}nodeComplete(t,e={}){this._stopIntercepting();let{duration:o,details:r}=e;if(r)for(let i of r)this._rawWrite(`${rt} ${i}`);let n=o?$.dim(` ${Pt(o)}`):"";this._rawWrite(`${Ot} ${$.green("done")}${n}`),this._emitGraphLogMarker({phase:"node_end",node:t}),this._rawWrite("")}nodeFailed(t,e,o={}){this._stopIntercepting();let{duration:r}=o,n=r?$.dim(` ${Pt(r)}`):"";this._rawWrite(`${nt} ${$.red(e)}`),this._rawWrite(`${Ot} ${$.red("failed")}${n}`),this._emitGraphLogMarker({phase:"node_end",node:t}),this._rawWrite("")}route(t,e){this._rawWrite($.dim(` ${t} \u2192 ${e}`)),this._rawWrite("")}graphComplete(){}},b=new it;var Q=".zibby/output",Rt="sessions",F=".session-info.json",Ct=".zibby-stop";var Mt=["CI_JOB_ID","GITHUB_RUN_ID","CIRCLE_WORKFLOW_ID","BUILD_ID"];var M=class{constructor(t){if(this.config=t,this.name=t.name,this.prompt=t.prompt,this.outputSchema=t.outputSchema,!this.outputSchema&&!t._isCustomCode)throw new Error(`Node '${this.name}' must define outputSchema (Zod schema). This defines the contract for what the node returns to state.`);this.isZodSchema=this.outputSchema&&typeof this.outputSchema._def<"u",this.parser=t.outputSchema&&!this.isZodSchema?new z(t.outputSchema):null,this.retries=t.retries||0,this.onComplete=t.onComplete,this.customExecute=t.execute}async execute(t,e){let o=()=>e&&typeof e.getAll=="function"?e.getAll():t,r=l=>e&&typeof e.get=="function"?e.get(l):t?.[l];if(typeof this.customExecute=="function"){w.debug(`[workflow] node '${this.name}': custom execute (skipping LLM)`);try{let l=await this.customExecute(t);return typeof l=="object"&&l!==null&&l.success===!1?{success:!1,error:l.error||"Node execution failed",raw:l.raw||null}:this.isZodSchema?(w.debug(`[workflow] node '${this.name}': validating output schema`),{success:!0,output:this.outputSchema.parse(l),raw:null}):{success:!0,output:l,raw:null}}catch(l){return w.error(`[workflow] node '${this.name}' failed: ${l.message}`),l.name==="ZodError"&&w.error(`Schema errors: ${JSON.stringify(l.issues||l.errors,null,2)}`),{success:!1,error:l.message,raw:null}}}let n=typeof this.prompt=="function"?this.prompt(o()):this.prompt,i=r("_skillHints");i&&(n=`${i}
|
|
28
28
|
|
|
29
|
-
${
|
|
29
|
+
${n}`);let a=o(),c=a.cwd||process.cwd(),u=a.sessionPath;try{if(u){let l=ht(u,F);if(Dt(l)){let v=JSON.parse(jt(l,"utf-8"));v.currentNode=this.name,ft(l,JSON.stringify(v,null,2),"utf-8")}let y=ht(u,"..",F);if(Dt(y))try{let v=JSON.parse(jt(y,"utf-8"));v.currentNode=this.name,ft(y,JSON.stringify(v,null,2),"utf-8")}catch{}}}catch(l){w.debug(`[workflow] could not update session info: ${l.message}`)}let p=null;for(let l=0;l<=this.retries;l++)try{w.debug(`[workflow] node '${this.name}' attempt ${l}`);let y=o().config||{},v=y.agents||{},R=this.config.agent??v[this.name]??null,I={state:o()};R&&(I.preferredAgent=R);let N={workspace:c,schema:this.isZodSchema?this.outputSchema:null,skills:this.config.skills||[],sessionPath:u,config:y,nodeName:this.name,timeout:this.config?.timeout||3e5},d=t?._coreInvokeAgent;d||(d=(await Promise.resolve().then(()=>(dt(),pt))).invokeAgent);let m=await d(n,I,N),S,E;if(typeof m=="string"?(S=m,E=null):m.structured?(S=m.raw||JSON.stringify(m.structured,null,2),E=m.structured):(S=m.raw||JSON.stringify(m,null,2),E=m.extracted||null),u)try{let f=ht(u,this.name,"raw_stream_output.txt");ge(me(f),{recursive:!0}),ft(f,typeof S=="string"?S:JSON.stringify(S),"utf-8")}catch(f){w.debug(`[workflow] could not save raw output: ${f.message}`)}if(this.isZodSchema&&E){w.info(`[workflow] node '${this.name}': output validated: ${JSON.stringify(E,null,2)}`);let f=E;if(typeof this.onComplete=="function")try{f=await this.onComplete(o(),E)}catch(P){w.warn(`[workflow] onComplete hook failed: ${P.message}`)}return{success:!0,output:f,raw:S}}if(typeof this.onComplete=="function")try{return{success:!0,output:await this.onComplete(o(),{raw:S}),raw:S}}catch(f){throw new Error(`onComplete failed: ${f.message}`,{cause:f})}if(this.parser){let f=this.parser.parse(S);return w.info(`[workflow] node '${this.name}': parsed output: ${JSON.stringify(f,null,2)}`),b.step("Output parsed"),{success:!0,output:f,raw:S}}return{success:!0,output:S,raw:S}}catch(y){p=y,l<this.retries&&w.info(`[workflow] node '${this.name}' failed, retrying (${l+1}/${this.retries})\u2026`)}return{success:!1,error:p.message,raw:null}}},tt=class extends M{constructor(t){super({...t,_isCustomCode:!0}),this.condition=t.condition}async execute(t,e){let o=e&&typeof e.getAll=="function"?e.getAll():t;return{success:!0,output:{nextNode:this.condition(o)},raw:null}}};X();var Se=2e3,_e=600*1e3,we=new Set(["completed","failed","canceled","timeout"]);function ye(){let s=process.env.PROGRESS_API_URL;if(!s)throw new Error("Sub-graph dispatch requires PROGRESS_API_URL env var (set automatically on cloud runs). Sub-graphs are not supported in local in-process runs yet \u2014 deploy the parent and child to cloud.");return s.replace(/\/executions\/?$/,"")}function Ee(){let s=process.env.PROJECT_ID;if(!s)throw new Error("Sub-graph dispatch requires PROJECT_ID env var.");return s}function Ie(){let s=process.env.PROJECT_API_TOKEN;if(!s)throw new Error("Sub-graph dispatch requires PROJECT_API_TOKEN env var.");return s}function $e(){return process.env.EXECUTION_ID||null}function be(s,t){return t==null?s:typeof t=="function"?t(s):typeof t=="string"?t.split(".").reduce((e,o)=>e==null?e:e[o],s):s}async function Ft(s,t={}){if(!s||typeof s!="string")throw new Error("dispatchSubgraph: workflowName (string) is required");let e=ye(),o=Ee(),r=Ie(),n=$e(),i=`${e}/projects/${encodeURIComponent(o)}/workflows/${encodeURIComponent(s)}/trigger`,a={input:t.input||{},...n?{parentExecutionId:n}:{},...t.conversationId?{conversationId:t.conversationId}:{}};w.info(`[sub-graph] dispatching '${s}' (${t.async?"async":"sync"}) from parent ${n||"<none>"}`);let c=await fetch(i,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${r}`},body:JSON.stringify(a)});if(!c.ok){let m=null,S="";try{m=await c.json(),S=m?.error||m?.message||JSON.stringify(m)}catch{S=await c.text().catch(()=>"")}if(c.status===429){let f=m?.quotaInfo||{},P=new Error(`Sub-graph '${s}' blocked by execution quota (${f.used??"?"}/${f.limit??"?"} on plan ${f.planId||"unknown"}). Sub-workflow runs count toward the same monthly cap as user-triggered runs.`);throw P.code="SUBGRAPH_QUOTA_EXCEEDED",P.status=429,P.subgraph=s,P.quotaInfo=f,P}if(c.status===400){let f=new Error(`Sub-graph '${s}' rejected input: ${S}`);throw f.code="SUBGRAPH_INVALID_INPUT",f.status=400,f.subgraph=s,f.validationErrors=m?.validationErrors||null,f.missing=m?.missing||null,f}let E=new Error(`Sub-graph '${s}' trigger rejected (${c.status}): ${S}`);throw E.code="SUBGRAPH_TRIGGER_FAILED",E.status=c.status,E.subgraph=s,E}let u=await c.json(),p=u?.data?.jobId||u?.jobId;if(!p)throw new Error(`Sub-graph '${s}' trigger returned no jobId: ${JSON.stringify(u).slice(0,200)}`);if(t.async)return w.info(`[sub-graph] async dispatch of '${s}' \u2192 jobId=${p} (not waiting)`),{jobId:p,status:"accepted",workflow:s};let l=Number.isFinite(t.timeoutMs)?t.timeoutMs:_e,y=Number.isFinite(t.pollIntervalMs)?t.pollIntervalMs:Se,v=`${e}/executions/${encodeURIComponent(p)}`,R=Date.now()+l,I="accepted",N=0;for(;Date.now()<R;){await new Promise(f=>setTimeout(f,y)),N+=1;let m=await fetch(v,{headers:{Authorization:`Bearer ${r}`}});if(!m.ok){if(m.status>=500){w.warn(`[sub-graph] status poll for ${p} returned ${m.status}, will retry`);continue}throw new Error(`Sub-graph status poll failed for ${p}: ${m.status}`)}let S=await m.json(),E=S?.data||S?.execution||S;if(I=E?.status||I,we.has(I)){if(I!=="completed"){let W=new Error(`Sub-graph '${s}' (${p}) ended in status '${I}'`);throw W.subgraphJobId=p,W.subgraphStatus=I,W}let f=E?.finalState||E?.state||{},P=be(f,t.output);return w.info(`[sub-graph] '${s}' (${p}) completed after ${N} polls`),P}}let d=new Error(`Sub-graph '${s}' (${p}) timed out after ${Math.round(l/1e3)}s (last status: ${I})`);throw d.subgraphJobId=p,d.subgraphStatus=I,d}import{existsSync as Ut,readFileSync as ve}from"node:fs";import{join as gt,dirname as Yt}from"node:path";var et=class{static async loadContext(t,e,o={}){let r={},n=o.filenames||["CONTEXT.md","AGENTS.md"];if(t){let a=Yt(gt(e,t));for(let c of n){let u=await this.findAndMergeContextFiles(c,a,e);if(u){let p=c.replace(/\.[^.]+$/,"").toLowerCase();r[p]=u}}}let i=o.discovery||{};for(let[a,c]of Object.entries(i))try{let u=gt(e,c);Ut(u)&&(r[a]=await this.loadFile(u))}catch(u){console.warn(`[workflow] could not load context '${a}' from '${c}': ${u.message}`)}return r}static async findAndMergeContextFiles(t,e,o){let r=[],n=e;for(;n.startsWith(o);){let i=gt(n,t);if(Ut(i))try{r.unshift(await this.loadFile(i))}catch(c){console.warn(`[workflow] could not load ${t} from ${i}: ${c.message}`)}let a=Yt(n);if(a===n)break;n=a}return r.length===0?null:r.every(i=>typeof i=="string")?r.join(`
|
|
30
30
|
|
|
31
31
|
---
|
|
32
32
|
|
|
33
|
-
`):
|
|
33
|
+
`):r.every(i=>typeof i=="object")?Object.assign({},...r):r[r.length-1]}static async loadFile(t){let e=ve(t,"utf-8");if(t.endsWith(".json"))return JSON.parse(e);if(t.endsWith(".js")||t.endsWith(".mjs")){let{pathToFileURL:o}=await import("url"),r=await import(o(t).href);return r.default||r}return e}};import{mkdirSync as Ht,existsSync as mt,writeFileSync as Jt,unlinkSync as Oe}from"node:fs";import{join as L,resolve as Kt}from"node:path";import{config as Te}from"dotenv";import{zodToJsonSchema as Zt}from"zod-to-json-schema";import Ae from"handlebars";function ke({traceFrom:s,sessionId:t,sessionPath:e,idSource:o,mkdirFresh:r}){if(!(process.env.ZIBBY_SESSION_LOG==="1"||process.env.ZIBBY_SESSION_LOG==="true"))return;let i=typeof process.ppid=="number"?process.ppid:"n/a",a=`[zibby:session] from=${s} pid=${process.pid} ppid=${i} sessionId=${t} source=${o} mkdir=${r?"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
|
-
`);console.log(`[zibby:session] stack (${
|
|
36
|
-
${
|
|
37
|
-
\u{1F6D1} External stop requested \u2014 ending workflow.`),
|
|
35
|
+
`);console.log(`[zibby:session] stack (${s}):
|
|
36
|
+
${p}`)}}function Ne(){return process.env.ZIBBY_TRUST_SESSION_ENV==="1"||process.env.ZIBBY_TRUST_SESSION_ENV==="true"||process.env.ZIBBY_KEEP_SESSION_ENV==="1"||process.env.ZIBBY_KEEP_SESSION_ENV==="true"}function Pe(){if(!(process.env.ZIBBY_PIN_SESSION_PATH==="1"||process.env.ZIBBY_PIN_SESSION_PATH==="true"))return;let t=process.env.ZIBBY_SESSION_PATH;if(!(t==null||String(t).trim()===""))try{return Kt(String(t).trim())}catch{return String(t).trim()}}function xe(){Ne()||(delete process.env.ZIBBY_SESSION_PATH,delete process.env.ZIBBY_SESSION_ID)}function Re({sessionPath:s,sessionId:t}){s&&typeof s=="string"&&(process.env.ZIBBY_SESSION_PATH=s),t!=null&&String(t).trim()!==""&&(process.env.ZIBBY_SESSION_ID=String(t).trim())}function Ce(s={}){let t=Mt.map(n=>process.env[n]).find(Boolean),e=Math.random().toString(36).slice(2,6),o=t||`${Date.now()}_${e}`,r=s.paths?.sessionPrefix;return r?`${r}_${o}`:o}function Me({cwd:s=process.cwd(),config:t={},initialState:e={},traceFrom:o="resolveWorkflowSession"}={}){let r=e.sessionPath,n=e.sessionTimestamp,i="initialState.sessionPath";if(!r&&process.env.ZIBBY_SESSION_PATH)try{let u=Kt(String(process.env.ZIBBY_SESSION_PATH));u&&(r=u,i="ZIBBY_SESSION_PATH")}catch{}let a;if(r)a=String(r).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 l=t.sessionId!=null?String(t.sessionId).trim():"";l&&l!=="last"?(a=l,i="config.sessionId"):(a=Ce(t),i="generated")}n=n??Date.now();let p=t.paths?.output||Q;r=L(s,p,Rt,a)}let c=!mt(r);return c&&Ht(r,{recursive:!0}),(c||i!=="initialState.sessionPath")&&ke({traceFrom:o,sessionId:a,sessionPath:r,idSource:i,mkdirFresh:c}),Re({sessionPath:r,sessionId:a}),{sessionPath:r,sessionId:a,sessionTimestamp:n}}var Gt=class{constructor(t={}){this.nodes=new Map,this.edges=new Map,this.entryPoint=null,this.middleware=Array.isArray(t.middleware)?[...t.middleware]:[],t.nodeMiddleware&&this.middleware.push(t.nodeMiddleware),this.nodeTypeMap=new Map,this.conditionalCodeMap=new Map,this.stateSchema=t.stateSchema||null,this.nodePrompts=new Map,this.nodeOptions=new Map,this._invokeAgent=t.invokeAgent||null,this._compiledPrompts=new Map}setStateSchema(t){return this.stateSchema=t,this}getStateSchema(){return this.stateSchema}addNode(t,e,o={}){if(!(e instanceof M)&&e&&typeof e=="object"&&typeof e.workflow=="string"){let n=e,i={name:t,_isCustomCode:!0,retries:n.retries,onComplete:n.onComplete,execute:async c=>{let u=c?.state&&typeof c.state.getAll=="function"?c.state.getAll():c,p;return typeof n.input=="function"?p=n.input(u):n.input&&typeof n.input=="object"?p=n.input:p={},Ft(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})}},a=new M(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 r=e instanceof M?e:new M(e);return r.name=t,this.nodes.set(t,r),o.prompt&&this.nodePrompts.set(t,o.prompt),Object.keys(o).length>0&&this.nodeOptions.set(t,o),this}addConditionalNode(t,e){return this.nodes.set(t,new tt({...e,name:t})),this}addEdge(t,e){return this.edges.set(t,e),this}setNodeType(t,e){return this.nodeTypeMap.set(t,e),this}addConditionalEdges(t,e,{labels:o}={}){return this.edges.set(t,{conditional:!0,routes:e,labels:o}),typeof e=="function"&&this.conditionalCodeMap.set(t,e.toString()),this}setEntryPoint(t){return this.entryPoint=t,this}use(t){return typeof t=="function"&&this.middleware.push(t),this}_composeMiddleware(t,e,o,r,n){let i=o;for(let a=t.length-1;a>=0;a--){let c=t[a],u=i;i=()=>c(e,u,r,n)}return i()}serialize(){let t=[],e={};for(let[n,i]of this.nodes){let a=this.nodeTypeMap.get(n)||n;t.push({id:n,type:a,data:{nodeType:a,label:n}});let c={};i._isCustomCode&&typeof i.execute=="function"&&(c.customCode=i.execute.toString());let u=this.nodePrompts.get(n);if(u&&(c.prompt=u),typeof i.customExecute=="function"&&(c.executeCode=i.customExecute.toString()),i.outputSchema)try{if(typeof i.outputSchema._def<"u"){let l=Zt(i.outputSchema,{target:"openApi3"});c.outputSchema={jsonSchema:l,variables:this._flattenJsonSchemaToVariables(l)}}else c.outputSchema={schema:i.outputSchema}}catch(l){console.warn(`[workflow] failed to convert schema for ${n}:`,l.message)}let p=(this.resolvedToolsMap||{})[n];p?.toolIds&&(c.tools=p.toolIds),Object.keys(c).length>0&&(e[n]=c)}let o=[];for(let[n,i]of this.edges)if(typeof i=="string")o.push({source:n,target:i});else if(i.conditional){let a=this.conditionalCodeMap.get(n)||i.routes.toString(),c=this._inferConditionalTargets(i.routes),u=i.labels||{};for(let p of c){let l={source:n,target:p,data:{conditionalCode:a}};u[p]&&(l.label=u[p]),o.push(l)}}let r=null;if(this.stateSchema)try{r=Zt(this.stateSchema,{target:"openApi3"})}catch{r=this.stateSchema}return{nodes:t,edges:o,nodeConfigs:e,stateSchema:r}}_inferConditionalTargets(t){let e=t.toString(),o=new Set,r=/return\s+['"]([^'"]+)['"]/g,n;for(;(n=r.exec(e))!==null;)o.add(n[1]);return[...o]}_flattenJsonSchemaToVariables(t,e=""){let o=t;if(t.$ref&&t.definitions){let r=t.$ref.replace("#/definitions/","");o=t.definitions[r]||t}return this._flattenSchema(o,e)}_flattenSchema(t,e=""){if(!t||typeof t!="object")return[];let o=[],r=t.properties||{},n=t.required||[];for(let[i,a]of Object.entries(r)){let c=e?`${e}.${i}`:i;o.push({path:c,type:a.type||"unknown",label:a.description||this._formatLabel(i),optional:!n.includes(i)}),a.type==="object"&&a.properties&&o.push(...this._flattenSchema(a,c)),a.type==="array"&&a.items?.type==="object"&&a.items.properties&&o.push(...this._flattenSchema(a.items,`${c}[]`))}return o}_formatLabel(t){return t.replace(/([A-Z])/g," $1").replace(/^./,e=>e.toUpperCase()).trim()}_summarizeNodeOutput(t,e){if(!e||typeof e!="object")return[];let o=[];e.success!==void 0&&o.push(`Result: ${e.success?"passed":"failed"}`);for(let[r,n]of Object.entries(e))if(!(r==="success"||r==="raw"||r==="nextNode")){if(typeof n=="string"&&n.length<=80)o.push(`${r}: ${n}`);else if(Array.isArray(n)){let i=n.length,a=n.filter(u=>u?.passed===!0).length,c=n.some(u=>u?.passed!==void 0);o.push(c?`${r}: ${a}/${i} passed${i-a?`, ${i-a} failed`:""}`:`${r}: ${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 r=new AbortController;o.signal&&(o.signal.aborted?r.abort():o.signal.addEventListener("abort",()=>r.abort(),{once:!0}));let n=o.strategyAbortTimeoutMs??e.config?.strategyAbortTimeoutMs??5e3,i=e.cwd||process.cwd();Te({path:L(i,".env")});let a=e.config||{};if(!a||Object.keys(a).length===0)try{let g=L(i,".zibby.config.js");mt(g)&&(a=(await import(g)).default||{})}catch{}process.env.EXECUTION_ID&&!a.agent?.strictMode&&(a.agent={...a.agent,strictMode:!0});let c=e.agentType;if(!c){let g=a?.agent;g?.provider?c=g.provider:g?.gemini?c="gemini":g?.claude?c="claude":g?.cursor?c="cursor":g?.codex?c="codex":c=process.env.AGENT_TYPE||"cursor"}let u=e.contextConfig||t?.config?.contextConfig||t?.config?.context||a?.context||{};if(this.stateSchema){let g=this.stateSchema.safeParse(e);if(!g.success){let O=g.error.issues.map(T=>`${T.path.join(".")}: ${T.message}`);throw console.error("\u274C Initial state validation failed:"),O.forEach(T=>console.error(` - ${T}`)),new Error(`State validation failed: ${O.join(", ")}`)}b.step("State validated against schema")}let p=Pe(),l=e.sessionPath||p;l||xe();let{sessionPath:y,sessionTimestamp:v,sessionId:R}=Me({cwd:i,config:a,traceFrom:"WorkflowGraph.run",initialState:{sessionPath:l,sessionTimestamp:e.sessionTimestamp}});b.step(`Session ${R}`);let I=await et.loadContext(e.specPath||"",i,u);Object.keys(I).length>0&&b.step(`Context loaded: ${Object.keys(I).join(", ")}`);let N=e.outputPath;!N&&e.specPath&&(t?.calculateOutputPath?N=t.calculateOutputPath(e.specPath):console.warn(`\u26A0\uFE0F outputPath not resolved (specPath=${e.specPath})`));let d=new V({...e,config:a,agentType:c,outputPath:N,sessionPath:y,sessionTimestamp:v,context:I,resolvedTools:this.resolvedToolsMap||{},_signal:r.signal}),m=new Map;try{await import("@zibby/skills")}catch{}let{getSkill:S}=await Promise.resolve().then(()=>(lt(),Lt)),E=a.skills&&typeof a.skills=="object"?a.skills:{},f=Object.values(E).filter(g=>g&&typeof g=="object"&&typeof g.id=="string"),P=g=>{for(let O of f)if(O.id===g)return O;return S(g)},W=new Set;for(let[,g]of this.nodes)for(let O of g.config?.skills||[])W.add(O);for(let g of W){let O=P(g);if(typeof O?.middleware=="function")try{let T=await O.middleware();typeof T=="function"&&m.set(g,T)}catch{}}let h=this.entryPoint,Z=[],St=a?.recursionLimit??100,Vt=0;try{for(;h&&h!=="END";){if(++Vt>St)throw new Error(`Workflow exceeded recursion limit (${St}) \u2014 likely a cyclic conditional route. Set config.recursionLimit if you need a higher cap.`);let O=L(y,Ct);if(mt(O)){try{Oe(O)}catch{}r.abort()}if(r.signal.aborted)return console.warn(`
|
|
37
|
+
\u{1F6D1} External stop requested \u2014 ending workflow.`),b.step("Workflow stopped externally"),{success:!0,state:d.getAll(),executionLog:Z,stoppedExternally:!0};let T=this.nodes.get(h);if(!T)throw new Error(`Node '${h}' not found in graph`);let _t=JSON.stringify({sessionPath:y,sessionTimestamp:v,currentNode:h,createdAt:new Date().toISOString(),config:d.get("config")}),zt=L(y,F);Jt(zt,_t,"utf-8");let wt=d.get("config")?.paths?.output||Q,qt=L(i,wt,F);Ht(L(i,wt),{recursive:!0});try{Jt(qt,_t,"utf-8")}catch{}let yt=e.onPipelineProgress;if(typeof yt=="function")try{yt({cwd:i,sessionPath:y,sessionId:R,outputBase:d.get("config")?.paths?.output||Q,currentNode:h})}catch{}let Xt=(this.resolvedToolsMap||{})[h]||null;d.set("_currentNodeTools",Xt);let Qt=d.get("nodeConfigs")||{};d.set("_currentNodeConfig",Qt[h]||{}),b.nodeStart(h);let Et=Date.now(),G=this.nodePrompts.get(h);if(!this._invokeAgent){let A=await Promise.resolve().then(()=>(dt(),pt));this._invokeAgent=A.invokeAgent}let te=this._invokeAgent,st={},ee=T.config?.skills||[];for(let A of ee){let k=P(A);if(typeof k?.invokeAgentOptions=="function")try{let _=k.invokeAgentOptions(d.getAll(),{agentType:d.get("agentType"),nodeName:h});_&&typeof _=="object"&&(st={...st,..._})}catch(_){console.warn(`[graph] skill '${A}' invokeAgentOptions threw: ${_.message}`)}}let It=async(A,k,_={})=>{let x=te(A,k,{...st,..._,signal:r.signal});return x.catch(()=>{}),r.signal.aborted?x:Promise.race([x,new Promise((j,D)=>{let C=()=>{setTimeout(()=>{let Y=new Error(`Strategy ignored AbortSignal \u2014 engine deadman fired after ${n}ms`);Y.name="AbortError",D(Y)},n)};r.signal.addEventListener("abort",C,{once:!0})})])},$t={state:d,invokeAgent:async(A={},k={})=>{let _=k.prompt||"";if(G){let x=this._compiledPrompts.get(h);x||(x=Ae.compile(G,{noEscape:!0}),this._compiledPrompts.set(h,x));try{_=x(A)}catch(j){throw console.error(`\u274C Template rendering failed for node '${h}':`,j.message),new Error(`Template rendering failed: ${j.message}`,{cause:j})}}else if(!_)throw new Error(`No prompt template configured for node '${h}' and no prompt provided in options`);return It(_,{state:d.getAll(),images:k.images||[]},{model:k.model||d.get("model"),workspace:d.get("workspace"),schema:k.schema,...k,signal:r.signal})},_coreInvokeAgent:It,agent:t,nodeId:h,promptTemplate:G,getPromptTemplate:()=>G,...d.getAll()};try{let A=(T.config?.skills||[]).map(C=>m.get(C)).filter(Boolean),k=[...this.middleware,...A],_;k.length>0?_=await this._composeMiddleware(k,h,async()=>T.execute($t,d),d.getAll(),d):_=await T.execute($t,d);let x=Date.now()-Et;if(Z.push({node:h,success:_.success,duration:x,timestamp:new Date().toISOString()}),!_.success){if(r.signal.aborted)return b.step("Workflow stopped externally"),{success:!0,state:d.getAll(),executionLog:Z,stoppedExternally:!0};d.append("errors",{node:h,error:_.error});let C=T.config?.retries||0,Y=`${h}_retries`,H=d.getAll()[Y]||0;if(H<C){b.stepInfo(`Retrying (attempt ${H+1}/${C})`),d.update({[Y]:H+1,[`${h}_raw`]:_.raw});continue}throw b.nodeFailed(h,_.error,{duration:x}),new Error(`Node '${h}' failed after ${H} attempts: ${_.error}`)}d.update({[h]:_.output});let j=this._summarizeNodeOutput(h,_.output);b.nodeComplete(h,{duration:x,details:j});let D=this.edges.get(h);if(!D)h="END";else if(D.conditional){let C=D.routes(d.getAll());b.route(h,C),h=C}else h=D}catch(A){throw b.isInsideNode&&b.nodeFailed(h,A.message,{duration:Date.now()-Et}),d.set("failed",!0),d.set("failedAt",h),A}}b.graphComplete();let g={success:!0,state:d.getAll(),executionLog:Z};return t&&typeof t.onComplete=="function"&&await t.onComplete(g),g}finally{if(t&&typeof t.cleanup=="function")try{await t.cleanup()}catch(g){console.warn(`[workflow] agent.cleanup() failed: ${g.message}`)}}}};export{Gt as WorkflowGraph,xe as clearInheritedSessionEnvForFreshRun,Ce as generateWorkflowSessionId,Pe as readPinnedSessionPathFromEnv,Me as resolveWorkflowSession,Ne as shouldTrustInheritedSessionEnv,Re as syncProcessEnvToSession};
|
package/dist/index.js
CHANGED
|
@@ -1,54 +1,54 @@
|
|
|
1
|
-
var
|
|
1
|
+
var Pt=Object.defineProperty;var he=(o=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(o,{get:(e,t)=>(typeof require<"u"?require:e)[t]}):o)(function(o){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+o+'" is not supported')});var ce=(o,e)=>()=>(o&&(e=o(o=0)),e);var Ue=(o,e)=>{for(var t in e)Pt(o,t,{get:e[t],enumerable:!0})};function Mt(o){z.impl={...Je,...o}}var Ge,Je,z,I,L=ce(()=>{Ge=()=>{},Je={debug:Ge,info:Ge,warn:(...o)=>console.warn("[workflow]",...o),error:(...o)=>console.error("[workflow]",...o)},z={impl:Je};I={debug:(...o)=>z.impl.debug?.(...o),info:(...o)=>z.impl.info?.(...o),warn:(...o)=>z.impl.warn?.(...o),error:(...o)=>z.impl.error?.(...o)}});var Ee,$e=ce(()=>{Ee=class{constructor(e,t,s=0){this.name=e,this.description=t,this.priority=s}async invoke(e,t={}){throw new Error(`${this.constructor.name}.invoke() must be implemented`)}canHandle(e){throw new Error(`${this.constructor.name}.canHandle() must be implemented`)}getName(){return this.name}getDescription(){return this.description}getPriority(){return this.priority}}});var st={};Ue(st,{clearSkills:()=>rt,getAllSkills:()=>tt,getSkill:()=>Y,hasSkill:()=>et,listSkillIds:()=>ot,registerSkill:()=>Qe});function Qe(o){if(!o||typeof o.id!="string")throw new Error("Skill definition must include a string id");J.set(o.id,Object.freeze({...o}))}function Y(o){return J.get(o)||null}function et(o){return J.has(o)}function tt(){return new Map(J)}function ot(){return Array.from(J.keys())}function rt(){J.clear()}var Ie,J,Q=ce(()=>{Ie=Symbol.for("@zibby/agent-workflow.skills");globalThis[Ie]||(globalThis[Ie]=new Map);J=globalThis[Ie]});var ee={};Ue(ee,{getAgentStrategy:()=>ve,invokeAgent:()=>at,listStrategies:()=>it,registerStrategy:()=>nt});function nt(o){if(!o||typeof o.getName!="function"||typeof o.invoke!="function")throw new Error("strategy must implement getName() and invoke() (AgentStrategy shape)");let e=B.findIndex(t=>t.getName()===o.getName());e>=0?B[e]=o:B.push(o)}function it(){return B.map(o=>o.getName())}function ve(o={}){let{state:e={},preferredAgent:t=null}=o,s=t||e.agentType||process.env.AGENT_TYPE;if(!s){let n=B.map(r=>r.getName()).join(", ")||"none registered";throw new Error(`No agent specified. Set agentType in state or AGENT_TYPE env var. Available: ${n}`)}I.debug(`[workflow] agent selection: requested=${s}`);let i=B.find(n=>n.getName()===s);if(!i){let n=B.map(r=>r.getName()).join(", ")||"none registered";throw new Error(`Unknown agent '${s}'. Available: ${n}`)}if(!i.canHandle(o))throw new Error(`Agent '${s}' is not available in this environment. Check credentials/environment.`);return I.debug(`[workflow] using agent: ${i.getName()}`),i}async function at(o,e={},t={}){let s=ve(e),i=e.state?.config||t.config||{},n=i.models||{},r=t.nodeName&&n[t.nodeName]||null,a=n.default||null,c=i.agent?.[s.name]?.model||null,l=r||a||c||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:i},d=o,u=p.skills||[];if(u.length>0&&!t.skipPromptFragments){let S=u.map(h=>{let $=Y(h)?.promptFragment;return typeof $=="function"?$():$}).filter(Boolean);S.length>0&&(d+=`
|
|
2
2
|
|
|
3
|
-
${
|
|
3
|
+
${S.join(`
|
|
4
4
|
|
|
5
|
-
`)}`)}let
|
|
5
|
+
`)}`)}let _=e.state?._currentNodeConfig?.extraPromptInstructions?.trim();return _&&(d+=`
|
|
6
6
|
|
|
7
7
|
\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501
|
|
8
8
|
PRIORITY OVERRIDE \u2014 THE FOLLOWING INSTRUCTIONS TAKE PRECEDENCE OVER ALL PREVIOUS CONTENT
|
|
9
9
|
\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501
|
|
10
10
|
|
|
11
|
-
${
|
|
12
|
-
`),
|
|
11
|
+
${_}
|
|
12
|
+
`),I.debug(`[workflow] prompt length: ${d.length} chars`),s.invoke(d,p)}var Te,B,Z=ce(()=>{$e();L();Q();Te=Symbol.for("@zibby/agent-workflow.strategies");globalThis[Te]||(globalThis[Te]=[]);B=globalThis[Te]});var Rt=new Set(["__proto__","constructor","prototype"]);function ge(o){if(Rt.has(o))throw new Error(`Invalid state key: "${o}"`)}var K=class{constructor(e={}){this._state=Object.create(null),Object.assign(this._state,{messages:[],errors:[],artifacts:{},metadata:{},...e}),this._history=[]}get(e){return this._state[e]}set(e,t){ge(e),this._history.push({...this._state}),this._state[e]=t}update(e){let t=Object.getOwnPropertyNames(e);for(let s of t)ge(s);this._history.push({...this._state});for(let s of t)this._state[s]=e[s]}append(e,t){ge(e),this._history.push({...this._state}),Array.isArray(this._state[e])||(this._state[e]=[]),this._state[e].push(t)}getAll(){return{...this._state}}rollback(){this._history.length>0&&(this._state=this._history.pop())}};var V=class{constructor(e){this.schema=e}parse(e){let t=e.match(/```json\s*([\s\S]*?)\s*```/);if(t)return this.validate(JSON.parse(t[1]));let s=[e.match(/\{[\s\S]*?\}/),e.match(/\{[\s\S]*\}/)].filter(Boolean).map(i=>i[0]);for(let i of s)try{return this.validate(JSON.parse(i))}catch(n){if(!(n instanceof SyntaxError))throw n}return this.validate({result:e.trim()})}validate(e){let t=[];for(let[s,i]of Object.entries(this.schema)){if(i.required&&!(s in e)&&t.push(`Missing required field: ${s}`),s in e&&i.type){let n=typeof e[s];n!==i.type&&t.push(`Field '${s}' expected ${i.type}, got ${n}`)}if(i.validate&&s in e){let n=i.validate(e[s]);n&&t.push(`Field '${s}': ${n}`)}}if(t.length>0)throw new Error(`Output validation failed:
|
|
13
13
|
${t.join(`
|
|
14
|
-
`)}`);return e}},
|
|
15
|
-
`?(r+=c,e.lineStart=!0,e.col=0,e.inEsc=!1):c==="\x1B"?(e.inEsc=!0,r+=c):e.inEsc?(r+=c,(c>="A"&&c<="Z"||c>="a"&&c<="z")&&(e.inEsc=!1)):(e.col++,r+=c,e.col>=
|
|
16
|
-
${
|
|
14
|
+
`)}`);return e}},jt={string:(o=!0)=>({type:"string",required:o}),number:(o=!0)=>({type:"number",required:o}),boolean:(o=!0)=>({type:"boolean",required:o}),array:(o=!0)=>({type:"object",required:o,validate:e=>Array.isArray(e)?null:"must be an array"}),enum:(o,e=!0)=>({type:"string",required:e,validate:t=>o.includes(t)?null:`must be one of: ${o.join(", ")}`})};L();import{writeFileSync as be,readFileSync as ct,existsSync as lt,mkdirSync as Ut}from"node:fs";import{join as Ne,dirname as Gt}from"node:path";import N from"chalk";var Xe="__WORKFLOW_GRAPH_LOG__",q=N.gray("\u2502"),Lt=N.gray("\u250C"),Ye=N.gray("\u2514"),me=N.green("\u25C6"),Ze=N.hex("#c084fc")("\u25C6"),He=N.hex("#2dd4bf")("\u25C6"),Se=N.red("\u25C6"),Ke=`${q} `,Ve=2;function ze(o){return o<1e3?`${o}ms`:`${(o/1e3).toFixed(1)}s`}function qe(o,e){return(t,s,i)=>{if(typeof t!="string")return o(t,s,i);let n=process.stdout.columns||120,r="";for(let a=0;a<t.length;a++){let c=t[a];e.lineStart&&(r+=Ke,e.col=Ve,e.lineStart=!1),c===`
|
|
15
|
+
`?(r+=c,e.lineStart=!0,e.col=0,e.inEsc=!1):c==="\x1B"?(e.inEsc=!0,r+=c):e.inEsc?(r+=c,(c>="A"&&c<="Z"||c>="a"&&c<="z")&&(e.inEsc=!1)):(e.col++,r+=c,e.col>=n&&(r+=`
|
|
16
|
+
${Ke}`,e.col=Ve))}return o(r,s,i)}}var le=class{constructor(){this._currentNode=null,this._origStdoutWrite=null,this._origStderrWrite=null,this._emitWorkflowGraphMarkers=String(process.env.ZIBBY_EMIT_GRAPH_MARKERS||"").trim()==="1"||String(process.env.ZIBBY_WORKFLOW_GRAPH_LOG_MARKERS||"").trim()==="1"}get isInsideNode(){return this._currentNode!==null}_startIntercepting(){this._origStdoutWrite=process.stdout.write.bind(process.stdout),this._origStderrWrite=process.stderr.write.bind(process.stderr);let e={lineStart:!0,col:0,inEsc:!1},t={lineStart:!0,col:0,inEsc:!1};this._outState=e,this._errState=t,process.stdout.write=qe(this._origStdoutWrite,e),process.stderr.write=qe(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=`${Xe}${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=N.hex("#2dd4bf")(e);this._origStdoutWrite?this._writeDot(
|
|
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)(`${q} ${me} ${e}
|
|
24
|
+
`)}stepInfo(e){this.step(e)}stepTool(e){this._origStdoutWrite?this._writeDot(Ze,e):process.stdout.write.bind(process.stdout)(`${q} ${Ze} ${e}
|
|
25
|
+
`)}stepMemory(e){let t=N.hex("#2dd4bf")(e);this._origStdoutWrite?this._writeDot(He,t):process.stdout.write.bind(process.stdout)(`${q} ${He} ${t}
|
|
26
|
+
`)}stepFail(e){this._origStdoutWrite?this._writeDot(Se,N.red(e)):process.stdout.write.bind(process.stdout)(`${q} ${Se} ${N.red(e)}
|
|
27
|
+
`)}nodeStart(e){this._currentNode=e,this._emitGraphLogMarker({phase:"node_begin",node:e}),this._rawWrite(`${Lt} ${e}`),this._startIntercepting()}nodeComplete(e,t={}){this._stopIntercepting();let{duration:s,details:i}=t;if(i)for(let r of i)this._rawWrite(`${me} ${r}`);let n=s?N.dim(` ${ze(s)}`):"";this._rawWrite(`${Ye} ${N.green("done")}${n}`),this._emitGraphLogMarker({phase:"node_end",node:e}),this._rawWrite("")}nodeFailed(e,t,s={}){this._stopIntercepting();let{duration:i}=s,n=i?N.dim(` ${ze(i)}`):"";this._rawWrite(`${Se} ${N.red(t)}`),this._rawWrite(`${Ye} ${N.red("failed")}${n}`),this._emitGraphLogMarker({phase:"node_end",node:e}),this._rawWrite("")}route(e,t){this._rawWrite(N.dim(` ${e} \u2192 ${t}`)),this._rawWrite("")}graphComplete(){}},b=new le;var X=".zibby/output",we="sessions",D=".session-info.json",ye=".zibby-stop",Dt="result.json",Bt="raw_stream_output.txt",Ft="events.json",Wt={BROWSER:"browser",JIRA:"jira",GITHUB:"github",SLACK:"slack",MEMORY:"memory",CHAT_MEMORY:"chat-memory",RUNNER:"runner",SKILL_INSTALLER:"skill-installer",CORE_TOOLS:"core-tools",WORKFLOW_BUILDER:"workflow-builder",SESSION:"session"},_e=["CI_JOB_ID","GITHUB_RUN_ID","CIRCLE_WORKFLOW_ID","BUILD_ID"];var M=class{constructor(e){if(this.config=e,this.name=e.name,this.prompt=e.prompt,this.outputSchema=e.outputSchema,!this.outputSchema&&!e._isCustomCode)throw new Error(`Node '${this.name}' must define outputSchema (Zod schema). This defines the contract for what the node returns to state.`);this.isZodSchema=this.outputSchema&&typeof this.outputSchema._def<"u",this.parser=e.outputSchema&&!this.isZodSchema?new V(e.outputSchema):null,this.retries=e.retries||0,this.onComplete=e.onComplete,this.customExecute=e.execute}async execute(e,t){let s=()=>t&&typeof t.getAll=="function"?t.getAll():e,i=d=>t&&typeof t.get=="function"?t.get(d):e?.[d];if(typeof this.customExecute=="function"){I.debug(`[workflow] node '${this.name}': custom execute (skipping LLM)`);try{let d=await this.customExecute(e);return typeof d=="object"&&d!==null&&d.success===!1?{success:!1,error:d.error||"Node execution failed",raw:d.raw||null}:this.isZodSchema?(I.debug(`[workflow] node '${this.name}': validating output schema`),{success:!0,output:this.outputSchema.parse(d),raw:null}):{success:!0,output:d,raw:null}}catch(d){return I.error(`[workflow] node '${this.name}' failed: ${d.message}`),d.name==="ZodError"&&I.error(`Schema errors: ${JSON.stringify(d.issues||d.errors,null,2)}`),{success:!1,error:d.message,raw:null}}}let n=typeof this.prompt=="function"?this.prompt(s()):this.prompt,r=i("_skillHints");r&&(n=`${r}
|
|
28
28
|
|
|
29
|
-
${
|
|
29
|
+
${n}`);let a=s(),c=a.cwd||process.cwd(),l=a.sessionPath;try{if(l){let d=Ne(l,D);if(lt(d)){let _=JSON.parse(ct(d,"utf-8"));_.currentNode=this.name,be(d,JSON.stringify(_,null,2),"utf-8")}let u=Ne(l,"..",D);if(lt(u))try{let _=JSON.parse(ct(u,"utf-8"));_.currentNode=this.name,be(u,JSON.stringify(_,null,2),"utf-8")}catch{}}}catch(d){I.debug(`[workflow] could not update session info: ${d.message}`)}let p=null;for(let d=0;d<=this.retries;d++)try{I.debug(`[workflow] node '${this.name}' attempt ${d}`);let u=s().config||{},_=u.agents||{},S=this.config.agent??_[this.name]??null,h={state:s()};S&&(h.preferredAgent=S);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},f=e?._coreInvokeAgent;f||(f=(await Promise.resolve().then(()=>(Z(),ee))).invokeAgent);let w=await f(n,h,$),E,v;if(typeof w=="string"?(E=w,v=null):w.structured?(E=w.raw||JSON.stringify(w.structured,null,2),v=w.structured):(E=w.raw||JSON.stringify(w,null,2),v=w.extracted||null),l)try{let g=Ne(l,this.name,"raw_stream_output.txt");Ut(Gt(g),{recursive:!0}),be(g,typeof E=="string"?E:JSON.stringify(E),"utf-8")}catch(g){I.debug(`[workflow] could not save raw output: ${g.message}`)}if(this.isZodSchema&&v){I.info(`[workflow] node '${this.name}': output validated: ${JSON.stringify(v,null,2)}`);let g=v;if(typeof this.onComplete=="function")try{g=await this.onComplete(s(),v)}catch(C){I.warn(`[workflow] onComplete hook failed: ${C.message}`)}return{success:!0,output:g,raw:E}}if(typeof this.onComplete=="function")try{return{success:!0,output:await this.onComplete(s(),{raw:E}),raw:E}}catch(g){throw new Error(`onComplete failed: ${g.message}`,{cause:g})}if(this.parser){let g=this.parser.parse(E);return I.info(`[workflow] node '${this.name}': parsed output: ${JSON.stringify(g,null,2)}`),b.step("Output parsed"),{success:!0,output:g,raw:E}}return{success:!0,output:E,raw:E}}catch(u){p=u,d<this.retries&&I.info(`[workflow] node '${this.name}' failed, retrying (${d+1}/${this.retries})\u2026`)}return{success:!1,error:p.message,raw:null}}},te=class extends M{constructor(e){super({...e,_isCustomCode:!0}),this.condition=e.condition}async execute(e,t){let s=t&&typeof t.getAll=="function"?t.getAll():e;return{success:!0,output:{nextNode:this.condition(s)},raw:null}}};L();var Jt=2e3,Yt=600*1e3,Zt=new Set(["completed","failed","canceled","timeout"]);function Ht(){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 Kt(){let o=process.env.PROJECT_ID;if(!o)throw new Error("Sub-graph dispatch requires PROJECT_ID env var.");return o}function Vt(){let o=process.env.PROJECT_API_TOKEN;if(!o)throw new Error("Sub-graph dispatch requires PROJECT_API_TOKEN env var.");return o}function zt(){return process.env.EXECUTION_ID||null}function qt(o,e){return e==null?o:typeof e=="function"?e(o):typeof e=="string"?e.split(".").reduce((t,s)=>t==null?t:t[s],o):o}async function ut(o,e={}){if(!o||typeof o!="string")throw new Error("dispatchSubgraph: workflowName (string) is required");let t=Ht(),s=Kt(),i=Vt(),n=zt(),r=`${t}/projects/${encodeURIComponent(s)}/workflows/${encodeURIComponent(o)}/trigger`,a={input:e.input||{},...n?{parentExecutionId:n}:{},...e.conversationId?{conversationId:e.conversationId}:{}};I.info(`[sub-graph] dispatching '${o}' (${e.async?"async":"sync"}) from parent ${n||"<none>"}`);let c=await fetch(r,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${i}`},body:JSON.stringify(a)});if(!c.ok){let w=null,E="";try{w=await c.json(),E=w?.error||w?.message||JSON.stringify(w)}catch{E=await c.text().catch(()=>"")}if(c.status===429){let g=w?.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: ${E}`);throw g.code="SUBGRAPH_INVALID_INPUT",g.status=400,g.subgraph=o,g.validationErrors=w?.validationErrors||null,g.missing=w?.missing||null,g}let v=new Error(`Sub-graph '${o}' trigger rejected (${c.status}): ${E}`);throw v.code="SUBGRAPH_TRIGGER_FAILED",v.status=c.status,v.subgraph=o,v}let l=await c.json(),p=l?.data?.jobId||l?.jobId;if(!p)throw new Error(`Sub-graph '${o}' trigger returned no jobId: ${JSON.stringify(l).slice(0,200)}`);if(e.async)return I.info(`[sub-graph] async dispatch of '${o}' \u2192 jobId=${p} (not waiting)`),{jobId:p,status:"accepted",workflow:o};let d=Number.isFinite(e.timeoutMs)?e.timeoutMs:Yt,u=Number.isFinite(e.pollIntervalMs)?e.pollIntervalMs:Jt,_=`${t}/executions/${encodeURIComponent(p)}`,S=Date.now()+d,h="accepted",$=0;for(;Date.now()<S;){await new Promise(g=>setTimeout(g,u)),$+=1;let w=await fetch(_,{headers:{Authorization:`Bearer ${i}`}});if(!w.ok){if(w.status>=500){I.warn(`[sub-graph] status poll for ${p} returned ${w.status}, will retry`);continue}throw new Error(`Sub-graph status poll failed for ${p}: ${w.status}`)}let E=await w.json(),v=E?.data||E?.execution||E;if(h=v?.status||h,Zt.has(h)){if(h!=="completed"){let W=new Error(`Sub-graph '${o}' (${p}) ended in status '${h}'`);throw W.subgraphJobId=p,W.subgraphStatus=h,W}let g=v?.finalState||v?.state||{},C=qt(g,e.output);return I.info(`[sub-graph] '${o}' (${p}) completed after ${$} polls`),C}}let f=new Error(`Sub-graph '${o}' (${p}) timed out after ${Math.round(d/1e3)}s (last status: ${h})`);throw f.subgraphJobId=p,f.subgraphStatus=h,f}import{existsSync as dt,readFileSync as Xt}from"node:fs";import{join as ke,dirname as pt}from"node:path";var oe=class{static async loadContext(e,t,s={}){let i={},n=s.filenames||["CONTEXT.md","AGENTS.md"];if(e){let a=pt(ke(t,e));for(let c of n){let l=await this.findAndMergeContextFiles(c,a,t);if(l){let p=c.replace(/\.[^.]+$/,"").toLowerCase();i[p]=l}}}let r=s.discovery||{};for(let[a,c]of Object.entries(r))try{let l=ke(t,c);dt(l)&&(i[a]=await this.loadFile(l))}catch(l){console.warn(`[workflow] could not load context '${a}' from '${c}': ${l.message}`)}return i}static async findAndMergeContextFiles(e,t,s){let i=[],n=t;for(;n.startsWith(s);){let r=ke(n,e);if(dt(r))try{i.unshift(await this.loadFile(r))}catch(c){console.warn(`[workflow] could not load ${e} from ${r}: ${c.message}`)}let a=pt(n);if(a===n)break;n=a}return i.length===0?null:i.every(r=>typeof r=="string")?i.join(`
|
|
30
30
|
|
|
31
31
|
---
|
|
32
32
|
|
|
33
|
-
`):
|
|
33
|
+
`):i.every(r=>typeof r=="object")?Object.assign({},...i):i[i.length-1]}static async loadFile(e){let t=Xt(e,"utf-8");if(e.endsWith(".json"))return JSON.parse(t);if(e.endsWith(".js")||e.endsWith(".mjs")){let{pathToFileURL:s}=await import("url"),i=await import(s(e).href);return i.default||i}return t}};import{mkdirSync as gt,existsSync as Ae,writeFileSync as ft,unlinkSync as Qt}from"node:fs";import{join as F,resolve as mt}from"node:path";import{config as eo}from"dotenv";import{zodToJsonSchema as ht}from"zod-to-json-schema";import to from"handlebars";function oo({traceFrom:o,sessionId:e,sessionPath:t,idSource:s,mkdirFresh:i}){if(!(process.env.ZIBBY_SESSION_LOG==="1"||process.env.ZIBBY_SESSION_LOG==="true"))return;let r=typeof process.ppid=="number"?process.ppid:"n/a",a=`[zibby:session] from=${o} pid=${process.pid} ppid=${r} sessionId=${e} source=${s} mkdir=${i?"yes":"no"} path=${t}`;if(console.log(a),process.env.ZIBBY_TRACE_SESSION==="1"||process.env.ZIBBY_TRACE_SESSION==="true"){let p=(new Error("session trace").stack||"").split(`
|
|
34
34
|
`).slice(2,14).join(`
|
|
35
35
|
`);console.log(`[zibby:session] stack (${o}):
|
|
36
|
-
${
|
|
37
|
-
\u{1F6D1} External stop requested \u2014 ending workflow.`)
|
|
38
|
-
${
|
|
36
|
+
${p}`)}}function St(){return process.env.ZIBBY_TRUST_SESSION_ENV==="1"||process.env.ZIBBY_TRUST_SESSION_ENV==="true"||process.env.ZIBBY_KEEP_SESSION_ENV==="1"||process.env.ZIBBY_KEEP_SESSION_ENV==="true"}function wt(){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 mt(String(e).trim())}catch{return String(e).trim()}}function yt(){St()||(delete process.env.ZIBBY_SESSION_PATH,delete process.env.ZIBBY_SESSION_ID)}function _t({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 Et(o={}){let e=_e.map(n=>process.env[n]).find(Boolean),t=Math.random().toString(36).slice(2,6),s=e||`${Date.now()}_${t}`,i=o.paths?.sessionPrefix;return i?`${i}_${s}`:s}function $t({cwd:o=process.cwd(),config:e={},initialState:t={},traceFrom:s="resolveWorkflowSession"}={}){let i=t.sessionPath,n=t.sessionTimestamp,r="initialState.sessionPath";if(!i&&process.env.ZIBBY_SESSION_PATH)try{let l=mt(String(process.env.ZIBBY_SESSION_PATH));l&&(i=l,r="ZIBBY_SESSION_PATH")}catch{}let a;if(i)a=String(i).split(/[/\\]/).filter(Boolean).pop(),n==null&&(n=Date.now());else{let l=process.env.ZIBBY_SESSION_ID&&String(process.env.ZIBBY_SESSION_ID).trim();if(l)a=l,r="ZIBBY_SESSION_ID";else{let d=e.sessionId!=null?String(e.sessionId).trim():"";d&&d!=="last"?(a=d,r="config.sessionId"):(a=Et(e),r="generated")}n=n??Date.now();let p=e.paths?.output||X;i=F(o,p,we,a)}let c=!Ae(i);return c&>(i,{recursive:!0}),(c||r!=="initialState.sessionPath")&&oo({traceFrom:s,sessionId:a,sessionPath:i,idSource:r,mkdirFresh:c}),_t({sessionPath:i,sessionId:a}),{sessionPath:i,sessionId:a,sessionTimestamp:n}}var re=class{constructor(e={}){this.nodes=new Map,this.edges=new Map,this.entryPoint=null,this.middleware=Array.isArray(e.middleware)?[...e.middleware]:[],e.nodeMiddleware&&this.middleware.push(e.nodeMiddleware),this.nodeTypeMap=new Map,this.conditionalCodeMap=new Map,this.stateSchema=e.stateSchema||null,this.nodePrompts=new Map,this.nodeOptions=new Map,this._invokeAgent=e.invokeAgent||null,this._compiledPrompts=new Map}setStateSchema(e){return this.stateSchema=e,this}getStateSchema(){return this.stateSchema}addNode(e,t,s={}){if(!(t instanceof M)&&t&&typeof t=="object"&&typeof t.workflow=="string"){let n=t,r={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={},ut(n.workflow,{input:p,async:n.async===!0,conversationId:typeof n.conversationId=="function"?n.conversationId(l):n.conversationId,output:n.output,timeoutMs:n.timeoutMs,pollIntervalMs:n.pollIntervalMs})}},a=new M(r);return a.name=e,this.nodes.set(e,a),s.prompt&&this.nodePrompts.set(e,s.prompt),Object.keys(s).length>0&&this.nodeOptions.set(e,s),this}let i=t instanceof M?t:new M(t);return i.name=e,this.nodes.set(e,i),s.prompt&&this.nodePrompts.set(e,s.prompt),Object.keys(s).length>0&&this.nodeOptions.set(e,s),this}addConditionalNode(e,t){return this.nodes.set(e,new te({...t,name:e})),this}addEdge(e,t){return this.edges.set(e,t),this}setNodeType(e,t){return this.nodeTypeMap.set(e,t),this}addConditionalEdges(e,t,{labels:s}={}){return this.edges.set(e,{conditional:!0,routes:t,labels:s}),typeof t=="function"&&this.conditionalCodeMap.set(e,t.toString()),this}setEntryPoint(e){return this.entryPoint=e,this}use(e){return typeof e=="function"&&this.middleware.push(e),this}_composeMiddleware(e,t,s,i,n){let r=s;for(let a=e.length-1;a>=0;a--){let c=e[a],l=r;r=()=>c(t,l,i,n)}return r()}serialize(){let e=[],t={};for(let[n,r]of this.nodes){let a=this.nodeTypeMap.get(n)||n;e.push({id:n,type:a,data:{nodeType:a,label:n}});let c={};r._isCustomCode&&typeof r.execute=="function"&&(c.customCode=r.execute.toString());let l=this.nodePrompts.get(n);if(l&&(c.prompt=l),typeof r.customExecute=="function"&&(c.executeCode=r.customExecute.toString()),r.outputSchema)try{if(typeof r.outputSchema._def<"u"){let d=ht(r.outputSchema,{target:"openApi3"});c.outputSchema={jsonSchema:d,variables:this._flattenJsonSchemaToVariables(d)}}else c.outputSchema={schema:r.outputSchema}}catch(d){console.warn(`[workflow] failed to convert schema for ${n}:`,d.message)}let p=(this.resolvedToolsMap||{})[n];p?.toolIds&&(c.tools=p.toolIds),Object.keys(c).length>0&&(t[n]=c)}let s=[];for(let[n,r]of this.edges)if(typeof r=="string")s.push({source:n,target:r});else if(r.conditional){let a=this.conditionalCodeMap.get(n)||r.routes.toString(),c=this._inferConditionalTargets(r.routes),l=r.labels||{};for(let p of c){let d={source:n,target:p,data:{conditionalCode:a}};l[p]&&(d.label=l[p]),s.push(d)}}let i=null;if(this.stateSchema)try{i=ht(this.stateSchema,{target:"openApi3"})}catch{i=this.stateSchema}return{nodes:e,edges:s,nodeConfigs:t,stateSchema:i}}_inferConditionalTargets(e){let t=e.toString(),s=new Set,i=/return\s+['"]([^'"]+)['"]/g,n;for(;(n=i.exec(t))!==null;)s.add(n[1]);return[...s]}_flattenJsonSchemaToVariables(e,t=""){let s=e;if(e.$ref&&e.definitions){let i=e.$ref.replace("#/definitions/","");s=e.definitions[i]||e}return this._flattenSchema(s,t)}_flattenSchema(e,t=""){if(!e||typeof e!="object")return[];let s=[],i=e.properties||{},n=e.required||[];for(let[r,a]of Object.entries(i)){let c=t?`${t}.${r}`:r;s.push({path:c,type:a.type||"unknown",label:a.description||this._formatLabel(r),optional:!n.includes(r)}),a.type==="object"&&a.properties&&s.push(...this._flattenSchema(a,c)),a.type==="array"&&a.items?.type==="object"&&a.items.properties&&s.push(...this._flattenSchema(a.items,`${c}[]`))}return s}_formatLabel(e){return e.replace(/([A-Z])/g," $1").replace(/^./,t=>t.toUpperCase()).trim()}_summarizeNodeOutput(e,t){if(!t||typeof t!="object")return[];let s=[];t.success!==void 0&&s.push(`Result: ${t.success?"passed":"failed"}`);for(let[i,n]of Object.entries(t))if(!(i==="success"||i==="raw"||i==="nextNode")){if(typeof n=="string"&&n.length<=80)s.push(`${i}: ${n}`);else if(Array.isArray(n)){let r=n.length,a=n.filter(l=>l?.passed===!0).length,c=n.some(l=>l?.passed!==void 0);s.push(c?`${i}: ${a}/${r} passed${r-a?`, ${r-a} failed`:""}`:`${i}: ${r} items`)}if(s.length>=4)break}return s}async run(e,t={},s={}){if(!this.entryPoint)throw new Error("No entry point set for graph");let i=new AbortController;s.signal&&(s.signal.aborted?i.abort():s.signal.addEventListener("abort",()=>i.abort(),{once:!0}));let n=s.strategyAbortTimeoutMs??t.config?.strategyAbortTimeoutMs??5e3,r=t.cwd||process.cwd();eo({path:F(r,".env")});let a=t.config||{};if(!a||Object.keys(a).length===0)try{let y=F(r,".zibby.config.js");Ae(y)&&(a=(await import(y)).default||{})}catch{}process.env.EXECUTION_ID&&!a.agent?.strictMode&&(a.agent={...a.agent,strictMode:!0});let c=t.agentType;if(!c){let y=a?.agent;y?.provider?c=y.provider:y?.gemini?c="gemini":y?.claude?c="claude":y?.cursor?c="cursor":y?.codex?c="codex":c=process.env.AGENT_TYPE||"cursor"}let l=t.contextConfig||e?.config?.contextConfig||e?.config?.context||a?.context||{};if(this.stateSchema){let y=this.stateSchema.safeParse(t);if(!y.success){let k=y.error.issues.map(A=>`${A.path.join(".")}: ${A.message}`);throw console.error("\u274C Initial state validation failed:"),k.forEach(A=>console.error(` - ${A}`)),new Error(`State validation failed: ${k.join(", ")}`)}b.step("State validated against schema")}let p=wt(),d=t.sessionPath||p;d||yt();let{sessionPath:u,sessionTimestamp:_,sessionId:S}=$t({cwd:r,config:a,traceFrom:"WorkflowGraph.run",initialState:{sessionPath:d,sessionTimestamp:t.sessionTimestamp}});b.step(`Session ${S}`);let h=await oe.loadContext(t.specPath||"",r,l);Object.keys(h).length>0&&b.step(`Context loaded: ${Object.keys(h).join(", ")}`);let $=t.outputPath;!$&&t.specPath&&(e?.calculateOutputPath?$=e.calculateOutputPath(t.specPath):console.warn(`\u26A0\uFE0F outputPath not resolved (specPath=${t.specPath})`));let f=new K({...t,config:a,agentType:c,outputPath:$,sessionPath:u,sessionTimestamp:_,context:h,resolvedTools:this.resolvedToolsMap||{},_signal:i.signal}),w=new Map;try{await import("@zibby/skills")}catch{}let{getSkill:E}=await Promise.resolve().then(()=>(Q(),st)),v=a.skills&&typeof a.skills=="object"?a.skills:{},g=Object.values(v).filter(y=>y&&typeof y=="object"&&typeof y.id=="string"),C=y=>{for(let k of g)if(k.id===y)return k;return E(y)},W=new Set;for(let[,y]of this.nodes)for(let k of y.config?.skills||[])W.add(k);for(let y of W){let k=C(y);if(typeof k?.middleware=="function")try{let A=await k.middleware();typeof A=="function"&&w.set(y,A)}catch{}}let m=this.entryPoint,ne=[],je=a?.recursionLimit??100,bt=0;try{for(;m&&m!=="END";){if(++bt>je)throw new Error(`Workflow exceeded recursion limit (${je}) \u2014 likely a cyclic conditional route. Set config.recursionLimit if you need a higher cap.`);let k=F(u,ye);if(Ae(k)){try{Qt(k)}catch{}i.abort()}if(i.signal.aborted)return console.warn(`
|
|
37
|
+
\u{1F6D1} External stop requested \u2014 ending workflow.`),b.step("Workflow stopped externally"),{success:!0,state:f.getAll(),executionLog:ne,stoppedExternally:!0};let A=this.nodes.get(m);if(!A)throw new Error(`Node '${m}' not found in graph`);let Me=JSON.stringify({sessionPath:u,sessionTimestamp:_,currentNode:m,createdAt:new Date().toISOString(),config:f.get("config")}),Nt=F(u,D);ft(Nt,Me,"utf-8");let Le=f.get("config")?.paths?.output||X,kt=F(r,Le,D);gt(F(r,Le),{recursive:!0});try{ft(kt,Me,"utf-8")}catch{}let De=t.onPipelineProgress;if(typeof De=="function")try{De({cwd:r,sessionPath:u,sessionId:S,outputBase:f.get("config")?.paths?.output||X,currentNode:m})}catch{}let At=(this.resolvedToolsMap||{})[m]||null;f.set("_currentNodeTools",At);let xt=f.get("nodeConfigs")||{};f.set("_currentNodeConfig",xt[m]||{}),b.nodeStart(m);let Be=Date.now(),ie=this.nodePrompts.get(m);if(!this._invokeAgent){let x=await Promise.resolve().then(()=>(Z(),ee));this._invokeAgent=x.invokeAgent}let Ot=this._invokeAgent,fe={},Ct=A.config?.skills||[];for(let x of Ct){let O=C(x);if(typeof O?.invokeAgentOptions=="function")try{let T=O.invokeAgentOptions(f.getAll(),{agentType:f.get("agentType"),nodeName:m});T&&typeof T=="object"&&(fe={...fe,...T})}catch(T){console.warn(`[graph] skill '${x}' invokeAgentOptions threw: ${T.message}`)}}let Fe=async(x,O,T={})=>{let P=Ot(x,O,{...fe,...T,signal:i.signal});return P.catch(()=>{}),i.signal.aborted?P:Promise.race([P,new Promise((U,G)=>{let j=()=>{setTimeout(()=>{let H=new Error(`Strategy ignored AbortSignal \u2014 engine deadman fired after ${n}ms`);H.name="AbortError",G(H)},n)};i.signal.addEventListener("abort",j,{once:!0})})])},We={state:f,invokeAgent:async(x={},O={})=>{let T=O.prompt||"";if(ie){let P=this._compiledPrompts.get(m);P||(P=to.compile(ie,{noEscape:!0}),this._compiledPrompts.set(m,P));try{T=P(x)}catch(U){throw console.error(`\u274C Template rendering failed for node '${m}':`,U.message),new Error(`Template rendering failed: ${U.message}`,{cause:U})}}else if(!T)throw new Error(`No prompt template configured for node '${m}' and no prompt provided in options`);return Fe(T,{state:f.getAll(),images:O.images||[]},{model:O.model||f.get("model"),workspace:f.get("workspace"),schema:O.schema,...O,signal:i.signal})},_coreInvokeAgent:Fe,agent:e,nodeId:m,promptTemplate:ie,getPromptTemplate:()=>ie,...f.getAll()};try{let x=(A.config?.skills||[]).map(j=>w.get(j)).filter(Boolean),O=[...this.middleware,...x],T;O.length>0?T=await this._composeMiddleware(O,m,async()=>A.execute(We,f),f.getAll(),f):T=await A.execute(We,f);let P=Date.now()-Be;if(ne.push({node:m,success:T.success,duration:P,timestamp:new Date().toISOString()}),!T.success){if(i.signal.aborted)return b.step("Workflow stopped externally"),{success:!0,state:f.getAll(),executionLog:ne,stoppedExternally:!0};f.append("errors",{node:m,error:T.error});let j=A.config?.retries||0,H=`${m}_retries`,ae=f.getAll()[H]||0;if(ae<j){b.stepInfo(`Retrying (attempt ${ae+1}/${j})`),f.update({[H]:ae+1,[`${m}_raw`]:T.raw});continue}throw b.nodeFailed(m,T.error,{duration:P}),new Error(`Node '${m}' failed after ${ae} attempts: ${T.error}`)}f.update({[m]:T.output});let U=this._summarizeNodeOutput(m,T.output);b.nodeComplete(m,{duration:P,details:U});let G=this.edges.get(m);if(!G)m="END";else if(G.conditional){let j=G.routes(f.getAll());b.route(m,j),m=j}else m=G}catch(x){throw b.isInsideNode&&b.nodeFailed(m,x.message,{duration:Date.now()-Be}),f.set("failed",!0),f.set("failedAt",m),x}}b.graphComplete();let y={success:!0,state:f.getAll(),executionLog:ne};return e&&typeof e.onComplete=="function"&&await e.onComplete(y),y}finally{if(e&&typeof e.cleanup=="function")try{await e.cleanup()}catch(y){console.warn(`[workflow] agent.cleanup() failed: ${y.message}`)}}}};var xe=Symbol.for("@zibby/agent-workflow.nodes");globalThis[xe]||(globalThis[xe]=new Map);var se=globalThis[xe];function It(o,e){se.set(o,e)}function Oe(o){return se.get(o)}function ue(o){return se.has(o)}function ro(){return Array.from(se.keys())}function Ce(o){let e=se.get(o);return e?e.factory&&typeof e.create=="function"?e.create.toString():typeof e.execute=="function"?e.execute.toString():typeof e=="function"?e.toString():null:null}It("ai_agent",{name:"ai_agent",factory:!0,create:(o,e={})=>({name:o,_isCustomCode:!0,execute:async t=>{let s=t?._coreInvokeAgent;s||(s=(await Promise.resolve().then(()=>(Z(),ee))).invokeAgent);let i=e.extraPromptInstructions||"Execute the task based on the current state.",n=so(i,t),r=await s(n,{cwd:t.workspace||process.cwd(),model:t.model,tools:e.resolvedTools||null});return{success:!0,output:{raw:r,nodeId:o},raw:typeof r=="string"?r:r.raw}}})});function so(o,e){let t=/@([\w.]+)/g,s=new Set,i;for(;(i=t.exec(o))!==null;)s.add(i[1]);if(s.size===0)return o;let n=[],r=new Set;for(let a of s){let c=a.split(".")[0];if(r.has(c))continue;let l=a.split(".").reduce((u,_)=>u?.[_],e);if(l===void 0)continue;let p=typeof l=="string"?l:l?.raw??JSON.stringify(l,null,2),d=a.replace(/_/g," ").replace(/\b\w/g,u=>u.toUpperCase());n.push(`## ${d}
|
|
38
|
+
${p}`),a.includes(".")||r.add(c)}return n.length===0?o:`${o}
|
|
39
39
|
|
|
40
40
|
---
|
|
41
41
|
# Referenced Context
|
|
42
42
|
|
|
43
|
-
${
|
|
44
|
-
|
|
45
|
-
`)}`}
|
|
46
|
-
`)}function
|
|
47
|
-
`)}function
|
|
48
|
-
`)}function
|
|
49
|
-
`)}function
|
|
50
|
-
`)}function
|
|
51
|
-
`)}function
|
|
52
|
-
`).map((
|
|
53
|
-
`);r.push(` graph.addConditionalEdges('${c.source}', ${l});`)}let a=[];for(let c of o){let l=
|
|
54
|
-
`)}function
|
|
43
|
+
${n.join(`
|
|
44
|
+
|
|
45
|
+
`)}`}Q();L();var de={};function Re(o,e){if(Array.isArray(e))return Pe(e);let t=de[o];return!t||t.length===0?null:Pe(t)}function Pe(o){if(!Array.isArray(o)||o.length===0)return null;let e=[],t={},s=[];for(let i of o){let n=Y(i);if(!n){I.warn(`[workflow] unknown skill "${i}" \u2014 skipping`);continue}s.push(i);for(let r of n.tools||[])e.push({name:r.name,description:r.description,input_schema:r.input_schema||{type:"object",properties:{}}});if(!t[n.serverName])if(typeof n.resolve=="function"){let r=n.resolve();r&&(t[n.serverName]={...r,toolPrefix:i})}else{let r={};for(let a of n.envKeys||[]){let c=process.env[a];c&&(r[a]=c)}t[n.serverName]={command:n.command,args:[...n.args||[]],env:r,toolPrefix:i}}}return s.length===0?null:{toolIds:s,claudeTools:e,mcpServers:t}}L();function no(o,e={}){let{nodes:t,edges:s,nodeConfigs:i={}}=o;if(!Array.isArray(t)||t.length===0)throw new R("Graph must have at least one node");if(!Array.isArray(s))throw new R("Graph edges must be an array");let n=new re(e);e.stateSchema&&n.setStateSchema(e.stateSchema);let r=new Set,a=new Map,c={};for(let u of t){let _=pe(u);a.set(u.id,{...u,resolvedType:_}),_==="decision"&&r.add(u.id)}for(let[u,_]of a){if(r.has(u))continue;let S=_.resolvedType,h=i[u]||{},$=Re(S,h.tools);$&&(c[u]=$);let f={};h.prompt&&(f.prompt=h.prompt);let w=ue(S);if(I.debug(`[workflow] compiler: node "${u}" type="${S}" registered=${w}`),h.customCode&&!w)n.addNode(u,Tt(u,h.customCode,h),f),n.setNodeType(u,S);else if(w){let E=Oe(S);E.factory?n.addNode(u,E.create(u,{...h,resolvedTools:$}),f):n.addNode(u,E,f),n.setNodeType(u,S)}else if(h.executeCode)n.addNode(u,Tt(u,h.executeCode,h),f),n.setNodeType(u,S);else throw new R(`Unknown node type "${S}" for node "${u}". Did you forget to register it?`)}n.resolvedToolsMap=c;let l=new Set;for(let u of s)r.has(u.target)||l.add(u.target);let p=t.find(u=>!r.has(u.id)&&!l.has(u.id));if(!p)throw new R("Could not determine entry point: no node without incoming edges found");n.setEntryPoint(p.id);let d=co(s,"source");for(let u of s)if(!r.has(u.source))if(r.has(u.target)){let _=u.target,S=d.get(_)||[];if(S.length===0)throw new R(`Decision node "${_}" has no outgoing edges`);let h=lo(_,S,r);n.addConditionalEdges(u.source,h)}else n.addEdge(u.source,u.target);return n}function io(o){let e=[];if(!o||typeof o!="object")return{valid:!1,errors:["Config must be a non-null object"]};if((!Array.isArray(o.nodes)||o.nodes.length===0)&&e.push("Graph must have at least one node"),Array.isArray(o.edges)||e.push("Graph edges must be an array"),e.length>0)return{valid:!1,errors:e};let t=o.nodeConfigs||{};for(let a of o.nodes){let c=pe(a);if(c==="decision"||ue(c))continue;let l=t[a.id]||{};l.customCode||l.executeCode||e.push(`Unknown node type "${c}" for node "${a.id}". Register it or provide customCode/executeCode.`)}let s=new Set(o.nodes.map(a=>a.id));for(let a of o.edges)s.has(a.source)||e.push(`Edge references unknown source node "${a.source}"`),s.has(a.target)||e.push(`Edge references unknown target node "${a.target}"`);let i=new Set(o.nodes.filter(a=>pe(a)==="decision").map(a=>a.id)),n=new Set;for(let a of o.edges)i.has(a.target)||n.add(a.target);let r=o.nodes.filter(a=>!i.has(a.id)&&!n.has(a.id));r.length===0?e.push("No entry point found (every node has incoming edges)"):r.length>1&&e.push(`Multiple entry points found: ${r.map(a=>a.id).join(", ")}`);for(let a of i){let c=o.edges.filter(p=>p.source===a);c.length===0&&e.push(`Decision node "${a}" has no outgoing edges`),c.some(p=>p.data?.conditionalCode||p.conditionalCode)||e.push(`Decision node "${a}" outgoing edges have no conditionalCode`)}return{valid:e.length===0,errors:e}}function ao(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 co(o,e){let t=new Map;for(let s of o){let i=s[e];t.has(i)||t.set(i,[]),t.get(i).push(s)}return t}function lo(o,e,t){let s=e.find(a=>a.data?.conditionalCode||a.conditionalCode);if(!s)throw new R(`Decision node "${o}" has no conditionalCode on its outgoing edges`);let i=s.data?.conditionalCode||s.conditionalCode,n=new Set(e.map(a=>a.target).filter(a=>!t.has(a))),r;try{let c=new Function(`return (${i})`)();r=l=>{let p=c(l);return n.has(p)||I.warn(`[workflow] conditional route from "${o}" returned "${p}" which is not in valid targets: ${[...n].join(", ")}`),p}}catch(a){throw new R(`Failed to compile conditionalCode for "${o}": ${a.message}`)}return r}function Tt(o,e,t={}){let s;try{s=new Function("invokeAgent","require","console",`return (${e})`)}catch(r){throw new R(`Failed to compile customCode for node "${o}": ${r.message}`)}let i=s(async(...r)=>{let{invokeAgent:a}=await Promise.resolve().then(()=>(Z(),ee));return a(...r)},typeof he<"u"?he:void 0,console),n=null;return t.outputSchema&&(n=t.outputSchema.jsonSchema||t.outputSchema),{name:o,_isCustomCode:!0,outputSchema:n,execute:async r=>{try{let a=await i(r);return typeof a=="object"&&"success"in a?a:{success:!0,output:a,raw:null}}catch(a){return{success:!1,error:a.message,raw:null}}}}}var R=class extends Error{constructor(e){super(e),this.name="CompilationError"}};Q();$e();Z();function uo(o,e={}){let{nodes:t,edges:s,nodeConfigs:i={}}=o,n=new Set,r=[],a=new Map;for(let $ of t){let f=$.data?.nodeType||$.type;a.set($.id,f),f==="decision"?n.add($.id):r.push({id:$.id,nodeType:f,label:$.data?.label||$.id})}let c=r.some($=>{let f=i[$.id]||{};return!f.customCode&&!f.executeCode}),{toolsPerNode:l,toolIdsByVar:p}=yo(r,i),{simpleEdges:d,conditionalEdges:u}=_o(s,n),_=Eo(r,s,n),S=[],h=e.workflowType||"workflow";return S.push(fo(e)),S.push(ho(h,{usesRegisteredNodes:c})),S.push(go(p)),S.push(mo(h)),S.push(So(r,i)),S.push(wo(r,_,d,u,l,h)),S.filter(Boolean).join(`
|
|
46
|
+
`)}function po(o){let e={};for(let[t,s]of Object.entries(o)){let{tools:i,...n}=s;Object.keys(n).length>0&&(e[t]=n)}return e}function fo(o){let e=o.workflowType||"workflow";return["// Generated workflow",`// ${o.projectId?`Project: ${o.projectId} | `:""}Type: ${e} | Version: ${o.version??0}`,`// Downloaded: ${new Date().toISOString()}`,""].join(`
|
|
47
|
+
`)}function ho(o,{usesRegisteredNodes:e=!0}={}){let t=["import { WorkflowGraph, invokeAgent, getResolvedToolDefinitions } from '@zibby/agent-workflow';"];return e&&t.push("// import './register-nodes.js'; // register custom node types here"),t.push("import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'node:fs';","import { join, dirname } from 'node:path';","import { fileURLToPath } from 'node:url';",""),t.join(`
|
|
48
|
+
`)}function go(o){if(o.size===0)return"";let e=["// \u2500\u2500 Tool Bindings \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"];for(let[t,s]of o)e.push(`const ${t} = getResolvedToolDefinitions(${JSON.stringify(s)}); // ${s.join(", ")}`);return e.push(""),e.join(`
|
|
49
|
+
`)}function mo(o){return["// \u2500\u2500 Node Configs \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500","const __filename = fileURLToPath(import.meta.url);","const __dirname = dirname(__filename);",`const configPath = join(__dirname, 'workflow-${o}.config.json');`,"const nodeConfigs = existsSync(configPath) ? JSON.parse(readFileSync(configPath, 'utf-8')) : {};",""].join(`
|
|
50
|
+
`)}function So(o,e){let t=["// \u2500\u2500 Node Implementations \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500",""];for(let s of o){let i=vt(s.id),n=e[s.id]?.customCode;if(n)t.push(`// @custom \u2014 modified from default "${s.nodeType}" template`),t.push(`const ${i}_execute = ${n};`);else{let r=Ce(s.nodeType);r?(t.push(`// Default "${s.nodeType}" implementation`),t.push(`const ${i}_execute = ${r};`)):(t.push(`// No template for "${s.nodeType}" \u2014 passthrough`),t.push(`const ${i}_execute = async (state) => ({ success: true, output: {}, raw: null });`))}t.push("")}return t.join(`
|
|
51
|
+
`)}function wo(o,e,t,s,i,n){let r=["// \u2500\u2500 Graph Builder \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500"];r.push("export function buildGraph(options = {}) {"),r.push(" const graph = new WorkflowGraph(options);",""),r.push(" // Nodes");for(let c of o){let l=vt(c.id);r.push(` graph.addNode('${c.id}', { name: '${c.id}', execute: ${l}_execute });`),r.push(` graph.setNodeType('${c.id}', '${c.nodeType}');`)}r.push("",` graph.setEntryPoint('${e}');`,""),(t.length>0||s.length>0)&&r.push(" // Edges");for(let c of t)r.push(` graph.addEdge('${c.source}', '${c.target}');`);for(let c of s){let l=c.code.split(`
|
|
52
|
+
`).map((p,d)=>d===0?p:` ${p}`).join(`
|
|
53
|
+
`);r.push(` graph.addConditionalEdges('${c.source}', ${l});`)}let a=[];for(let c of o){let l=i.get(c.id);l&&a.push(` '${c.id}': ${l},`)}return a.length>0&&r.push(""," graph.resolvedToolsMap = {",...a," };"),r.push(""," return graph;","}",""),r.push("export { nodeConfigs };",""),r.join(`
|
|
54
|
+
`)}function yo(o,e){let t=new Map,s=new Map;for(let i of o){let n=e[i.id]?.tools,r;if(Array.isArray(n)&&n.length>0)r=[...n].sort();else{let a=de[i.nodeType];a?.length>0&&(r=[...a].sort())}if(r){let a=`${r.map(c=>c.replace(/[^a-zA-Z0-9]/g,"")).join("And")}Tools`;t.set(i.id,a),s.has(a)||s.set(a,r)}}return{toolsPerNode:t,toolIdsByVar:s}}function _o(o,e){let t=[],s=[],i=new Map,n=new Set;for(let r of o)i.has(r.source)||i.set(r.source,[]),i.get(r.source).push(r);for(let r of o)if(!e.has(r.source))if(e.has(r.target)){if(n.has(r.target))continue;n.add(r.target);let c=(i.get(r.target)||[]).find(l=>l.data?.conditionalCode||l.conditionalCode);c&&s.push({source:r.source,code:c.data?.conditionalCode||c.conditionalCode})}else t.push({source:r.source,target:r.target});return{simpleEdges:t,conditionalEdges:s}}function Eo(o,e,t){let s=new Set;for(let n of e)t.has(n.target)||s.add(n.target);let i=o.find(n=>!s.has(n.id));return i?i.id:o[0]?.id}function vt(o){return o.replace(/[^a-zA-Z0-9]/g,"_")}L();export{Ee as AgentStrategy,_e as CI_ENV_VARS,R as CompilationError,te as ConditionalNode,oe as ContextLoader,X as DEFAULT_OUTPUT_BASE,Ft as EVENTS_FILE,de as NODE_DEFAULT_TOOLS,M as Node,V as OutputParser,Bt as RAW_OUTPUT_FILE,Dt as RESULT_FILE,we as SESSIONS_DIR,D as SESSION_INFO_FILE,Wt as SKILLS,ye as STOP_REQUEST_FILE,jt as SchemaTypes,le as Timeline,Xe as WORKFLOW_GRAPH_LOG_MARKER_PREFIX,re as WorkflowGraph,K as WorkflowState,yt as clearInheritedSessionEnvForFreshRun,rt as clearSkills,no as compileGraph,ao as extractSteps,po as generateNodeConfigsJson,uo as generateWorkflowCode,Et as generateWorkflowSessionId,ve as getAgentStrategy,tt as getAllSkills,Oe as getNodeImpl,Ce as getNodeTemplate,Pe as getResolvedToolDefinitions,Y as getSkill,ue as hasNode,et as hasSkill,at as invokeAgent,ro as listNodeTypes,ot as listSkillIds,it as listStrategies,wt as readPinnedSessionPathFromEnv,It as registerNode,Qe as registerSkill,nt as registerStrategy,Re as resolveNodeTools,$t as resolveWorkflowSession,Mt as setLogger,St as shouldTrustInheritedSessionEnv,_t as syncProcessEnvToSession,b as timeline,io as validateGraphConfig};
|
package/dist/node.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
var st=Object.defineProperty;var b=(r,t)=>()=>(r&&(t=r(r=0)),t);var it=(r,t)=>{for(var e in t)st(r,e,{get:t[e],enumerable:!0})};var D,nt,N,l,R=b(()=>{D=()=>{},nt={debug:D,info:D,warn:(...r)=>console.warn("[workflow]",...r),error:(...r)=>console.error("[workflow]",...r)},N={impl:nt},l={debug:(...r)=>N.impl.debug?.(...r),info:(...r)=>N.impl.info?.(...r),warn:(...r)=>N.impl.warn?.(...r),error:(...r)=>N.impl.error?.(...r)}});var Z=b(()=>{});function V(r){return ut.get(r)||null}var v,ut,z=b(()=>{v=Symbol.for("@zibby/agent-workflow.skills");globalThis[v]||(globalThis[v]=new Map);ut=globalThis[v]});var X={};it(X,{getAgentStrategy:()=>q,invokeAgent:()=>dt,listStrategies:()=>pt,registerStrategy:()=>ct});function ct(r){if(!r||typeof r.getName!="function"||typeof r.invoke!="function")throw new Error("strategy must implement getName() and invoke() (AgentStrategy shape)");let t=
|
|
1
|
+
var st=Object.defineProperty;var b=(r,t)=>()=>(r&&(t=r(r=0)),t);var it=(r,t)=>{for(var e in t)st(r,e,{get:t[e],enumerable:!0})};var D,nt,N,l,R=b(()=>{D=()=>{},nt={debug:D,info:D,warn:(...r)=>console.warn("[workflow]",...r),error:(...r)=>console.error("[workflow]",...r)},N={impl:nt},l={debug:(...r)=>N.impl.debug?.(...r),info:(...r)=>N.impl.info?.(...r),warn:(...r)=>N.impl.warn?.(...r),error:(...r)=>N.impl.error?.(...r)}});var Z=b(()=>{});function V(r){return ut.get(r)||null}var v,ut,z=b(()=>{v=Symbol.for("@zibby/agent-workflow.skills");globalThis[v]||(globalThis[v]=new Map);ut=globalThis[v]});var X={};it(X,{getAgentStrategy:()=>q,invokeAgent:()=>dt,listStrategies:()=>pt,registerStrategy:()=>ct});function ct(r){if(!r||typeof r.getName!="function"||typeof r.invoke!="function")throw new Error("strategy must implement getName() and invoke() (AgentStrategy shape)");let t=_.findIndex(e=>e.getName()===r.getName());t>=0?_[t]=r:_.push(r)}function pt(){return _.map(r=>r.getName())}function q(r={}){let{state:t={},preferredAgent:e=null}=r,o=e||t.agentType||process.env.AGENT_TYPE;if(!o){let n=_.map(a=>a.getName()).join(", ")||"none registered";throw new Error(`No agent specified. Set agentType in state or AGENT_TYPE env var. Available: ${n}`)}l.debug(`[workflow] agent selection: requested=${o}`);let i=_.find(n=>n.getName()===o);if(!i){let n=_.map(a=>a.getName()).join(", ")||"none registered";throw new Error(`Unknown agent '${o}'. Available: ${n}`)}if(!i.canHandle(r))throw new Error(`Agent '${o}' is not available in this environment. Check credentials/environment.`);return l.debug(`[workflow] using agent: ${i.getName()}`),i}async function dt(r,t={},e={}){let o=q(t),i=t.state?.config||e.config||{},n=i.models||{},a=e.nodeName&&n[e.nodeName]||null,g=n.default||null,p=i.agent?.[o.name]?.model||null,w=a||g||p||e.model||null,E={...e,model:w,workspace:t.state?.workspace||e.workspace,schema:e.schema||t.schema,images:e.images||t.images||[],skills:e.skills||t.skills||[],config:i},s=r,h=E.skills||[];if(h.length>0&&!e.skipPromptFragments){let y=h.map(I=>{let $=V(I)?.promptFragment;return typeof $=="function"?$():$}).filter(Boolean);y.length>0&&(s+=`
|
|
2
2
|
|
|
3
|
-
${
|
|
3
|
+
${y.join(`
|
|
4
4
|
|
|
5
5
|
`)}`)}let f=t.state?._currentNodeConfig?.extraPromptInstructions?.trim();return f&&(s+=`
|
|
6
6
|
|
|
@@ -9,7 +9,7 @@ PRIORITY OVERRIDE \u2014 THE FOLLOWING INSTRUCTIONS TAKE PRECEDENCE OVER ALL PRE
|
|
|
9
9
|
\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501
|
|
10
10
|
|
|
11
11
|
${f}
|
|
12
|
-
`),l.debug(`[workflow] prompt length: ${s.length} chars`),o.invoke(s,
|
|
12
|
+
`),l.debug(`[workflow] prompt length: ${s.length} chars`),o.invoke(s,E)}var P,_,Q=b(()=>{Z();R();z();P=Symbol.for("@zibby/agent-workflow.strategies");globalThis[P]||(globalThis[P]=[]);_=globalThis[P]});var k=class{constructor(t){this.schema=t}parse(t){let e=t.match(/```json\s*([\s\S]*?)\s*```/);if(e)return this.validate(JSON.parse(e[1]));let o=[t.match(/\{[\s\S]*?\}/),t.match(/\{[\s\S]*\}/)].filter(Boolean).map(i=>i[0]);for(let i of o)try{return this.validate(JSON.parse(i))}catch(n){if(!(n instanceof SyntaxError))throw n}return this.validate({result:t.trim()})}validate(t){let e=[];for(let[o,i]of Object.entries(this.schema)){if(i.required&&!(o in t)&&e.push(`Missing required field: ${o}`),o in t&&i.type){let n=typeof t[o];n!==i.type&&e.push(`Field '${o}' expected ${i.type}, got ${n}`)}if(i.validate&&o in t){let n=i.validate(t[o]);n&&e.push(`Field '${o}': ${n}`)}}if(e.length>0)throw new Error(`Output validation failed:
|
|
13
13
|
${e.join(`
|
|
14
14
|
`)}`);return t}};R();import{writeFileSync as C,readFileSync as tt,existsSync as et,mkdirSync as ht}from"node:fs";import{join as F,dirname as ft}from"node:path";import u from"chalk";var at="__WORKFLOW_GRAPH_LOG__",O=u.gray("\u2502"),lt=u.gray("\u250C"),G=u.gray("\u2514"),x=u.green("\u25C6"),U=u.hex("#c084fc")("\u25C6"),j=u.hex("#2dd4bf")("\u25C6"),T=u.red("\u25C6"),J=`${O} `,H=2;function K(r){return r<1e3?`${r}ms`:`${(r/1e3).toFixed(1)}s`}function B(r,t){return(e,o,i)=>{if(typeof e!="string")return r(e,o,i);let n=process.stdout.columns||120,a="";for(let g=0;g<e.length;g++){let p=e[g];t.lineStart&&(a+=J,t.col=H,t.lineStart=!1),p===`
|
|
15
15
|
`?(a+=p,t.lineStart=!0,t.col=0,t.inEsc=!1):p==="\x1B"?(t.inEsc=!0,a+=p):t.inEsc?(a+=p,(p>="A"&&p<="Z"||p>="a"&&p<="z")&&(t.inEsc=!1)):(t.col++,a+=p,t.col>=n&&(a+=`
|
|
@@ -26,4 +26,4 @@ ${J}`,t.col=H))}return r(a,o,i)}}var A=class{constructor(){this._currentNode=nul
|
|
|
26
26
|
`)}stepFail(t){this._origStdoutWrite?this._writeDot(T,u.red(t)):process.stdout.write.bind(process.stdout)(`${O} ${T} ${u.red(t)}
|
|
27
27
|
`)}nodeStart(t){this._currentNode=t,this._emitGraphLogMarker({phase:"node_begin",node:t}),this._rawWrite(`${lt} ${t}`),this._startIntercepting()}nodeComplete(t,e={}){this._stopIntercepting();let{duration:o,details:i}=e;if(i)for(let a of i)this._rawWrite(`${x} ${a}`);let n=o?u.dim(` ${K(o)}`):"";this._rawWrite(`${G} ${u.green("done")}${n}`),this._emitGraphLogMarker({phase:"node_end",node:t}),this._rawWrite("")}nodeFailed(t,e,o={}){this._stopIntercepting();let{duration:i}=o,n=i?u.dim(` ${K(i)}`):"";this._rawWrite(`${T} ${u.red(e)}`),this._rawWrite(`${G} ${u.red("failed")}${n}`),this._emitGraphLogMarker({phase:"node_end",node:t}),this._rawWrite("")}route(t,e){this._rawWrite(u.dim(` ${t} \u2192 ${e}`)),this._rawWrite("")}graphComplete(){}},Y=new A;var L=".session-info.json";var M=class{constructor(t){if(this.config=t,this.name=t.name,this.prompt=t.prompt,this.outputSchema=t.outputSchema,!this.outputSchema&&!t._isCustomCode)throw new Error(`Node '${this.name}' must define outputSchema (Zod schema). This defines the contract for what the node returns to state.`);this.isZodSchema=this.outputSchema&&typeof this.outputSchema._def<"u",this.parser=t.outputSchema&&!this.isZodSchema?new k(t.outputSchema):null,this.retries=t.retries||0,this.onComplete=t.onComplete,this.customExecute=t.execute}async execute(t,e){let o=()=>e&&typeof e.getAll=="function"?e.getAll():t,i=s=>e&&typeof e.get=="function"?e.get(s):t?.[s];if(typeof this.customExecute=="function"){l.debug(`[workflow] node '${this.name}': custom execute (skipping LLM)`);try{let s=await this.customExecute(t);return typeof s=="object"&&s!==null&&s.success===!1?{success:!1,error:s.error||"Node execution failed",raw:s.raw||null}:this.isZodSchema?(l.debug(`[workflow] node '${this.name}': validating output schema`),{success:!0,output:this.outputSchema.parse(s),raw:null}):{success:!0,output:s,raw:null}}catch(s){return l.error(`[workflow] node '${this.name}' failed: ${s.message}`),s.name==="ZodError"&&l.error(`Schema errors: ${JSON.stringify(s.issues||s.errors,null,2)}`),{success:!1,error:s.message,raw:null}}}let n=typeof this.prompt=="function"?this.prompt(o()):this.prompt,a=i("_skillHints");a&&(n=`${a}
|
|
28
28
|
|
|
29
|
-
${n}`);let g=o(),p=g.cwd||process.cwd(),w=g.sessionPath;try{if(w){let s=F(w,L);if(et(s)){let f=JSON.parse(tt(s,"utf-8"));f.currentNode=this.name,C(s,JSON.stringify(f,null,2),"utf-8")}let h=F(w,"..",L);if(et(h))try{let f=JSON.parse(tt(h,"utf-8"));f.currentNode=this.name,C(h,JSON.stringify(f,null,2),"utf-8")}catch{}}}catch(s){l.debug(`[workflow] could not update session info: ${s.message}`)}let
|
|
29
|
+
${n}`);let g=o(),p=g.cwd||process.cwd(),w=g.sessionPath;try{if(w){let s=F(w,L);if(et(s)){let f=JSON.parse(tt(s,"utf-8"));f.currentNode=this.name,C(s,JSON.stringify(f,null,2),"utf-8")}let h=F(w,"..",L);if(et(h))try{let f=JSON.parse(tt(h,"utf-8"));f.currentNode=this.name,C(h,JSON.stringify(f,null,2),"utf-8")}catch{}}}catch(s){l.debug(`[workflow] could not update session info: ${s.message}`)}let E=null;for(let s=0;s<=this.retries;s++)try{l.debug(`[workflow] node '${this.name}' attempt ${s}`);let h=o().config||{},f=h.agents||{},y=this.config.agent??f[this.name]??null,I={state:o()};y&&(I.preferredAgent=y);let $={workspace:p,schema:this.isZodSchema?this.outputSchema:null,skills:this.config.skills||[],sessionPath:w,config:h,nodeName:this.name,timeout:this.config?.timeout||3e5},W=t?._coreInvokeAgent;W||(W=(await Promise.resolve().then(()=>(Q(),X))).invokeAgent);let m=await W(n,I,$),d,S;if(typeof m=="string"?(d=m,S=null):m.structured?(d=m.raw||JSON.stringify(m.structured,null,2),S=m.structured):(d=m.raw||JSON.stringify(m,null,2),S=m.extracted||null),w)try{let c=F(w,this.name,"raw_stream_output.txt");ht(ft(c),{recursive:!0}),C(c,typeof d=="string"?d:JSON.stringify(d),"utf-8")}catch(c){l.debug(`[workflow] could not save raw output: ${c.message}`)}if(this.isZodSchema&&S){l.info(`[workflow] node '${this.name}': output validated: ${JSON.stringify(S,null,2)}`);let c=S;if(typeof this.onComplete=="function")try{c=await this.onComplete(o(),S)}catch(ot){l.warn(`[workflow] onComplete hook failed: ${ot.message}`)}return{success:!0,output:c,raw:d}}if(typeof this.onComplete=="function")try{return{success:!0,output:await this.onComplete(o(),{raw:d}),raw:d}}catch(c){throw new Error(`onComplete failed: ${c.message}`,{cause:c})}if(this.parser){let c=this.parser.parse(d);return l.info(`[workflow] node '${this.name}': parsed output: ${JSON.stringify(c,null,2)}`),Y.step("Output parsed"),{success:!0,output:c,raw:d}}return{success:!0,output:d,raw:d}}catch(h){E=h,s<this.retries&&l.info(`[workflow] node '${this.name}' failed, retrying (${s+1}/${this.retries})\u2026`)}return{success:!1,error:E.message,raw:null}}},rt=class extends M{constructor(t){super({...t,_isCustomCode:!0}),this.condition=t.condition}async execute(t,e){let o=e&&typeof e.getAll=="function"?e.getAll():t;return{success:!0,output:{nextNode:this.condition(o)},raw:null}}};export{rt as ConditionalNode,M as Node};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dispatch `workflowName` as a child of the currently-running execution.
|
|
3
|
+
*
|
|
4
|
+
* @param {string} workflowName
|
|
5
|
+
* The workflowType of the child (same project, resolved by name).
|
|
6
|
+
* @param {object} options
|
|
7
|
+
* @param {object} [options.input]
|
|
8
|
+
* Input payload for the child's stateSchema. Server validates before
|
|
9
|
+
* spawning Fargate; invalid input throws SubgraphInputError.
|
|
10
|
+
* @param {boolean} [options.async=false]
|
|
11
|
+
* true = fire-and-forget, returns `{ jobId }` immediately.
|
|
12
|
+
* false = poll until terminal, returns final state.
|
|
13
|
+
* @param {string} [options.conversationId]
|
|
14
|
+
* Override the conversation id seen by the child. Omit to let the
|
|
15
|
+
* child run without one.
|
|
16
|
+
* @param {number} [options.timeoutMs=600000]
|
|
17
|
+
* Sync mode only: how long to poll before giving up.
|
|
18
|
+
* @param {number} [options.pollIntervalMs=2000]
|
|
19
|
+
* Sync mode only: how often to GET the child's execution row.
|
|
20
|
+
* @param {string | ((finalState: object) => any)} [options.output]
|
|
21
|
+
* How to extract the child's result into parent state. String forms
|
|
22
|
+
* are dot-paths on finalState (e.g. 'double.doubled'). Function form
|
|
23
|
+
* gets the full finalState and returns whatever shape the parent
|
|
24
|
+
* wants. Omit to merge the whole child finalState into parent state.
|
|
25
|
+
*
|
|
26
|
+
* @returns {Promise<any>}
|
|
27
|
+
* async: `{ jobId, status: 'accepted' }`
|
|
28
|
+
* sync : the child's final state (or `getPath(state, output)`)
|
|
29
|
+
*
|
|
30
|
+
* @throws {Error}
|
|
31
|
+
* - Network / 5xx errors from the trigger endpoint
|
|
32
|
+
* - 400 if the child's stateSchema rejects the input
|
|
33
|
+
* - Sub-graph reached a non-success terminal status (failed/canceled/timeout)
|
|
34
|
+
* - Sync timeout exceeded
|
|
35
|
+
*/
|
|
36
|
+
export function dispatchSubgraph(workflowName: string, options?: {
|
|
37
|
+
input?: object;
|
|
38
|
+
async?: boolean;
|
|
39
|
+
conversationId?: string;
|
|
40
|
+
timeoutMs?: number;
|
|
41
|
+
pollIntervalMs?: number;
|
|
42
|
+
output?: string | ((finalState: object) => any);
|
|
43
|
+
}): Promise<any>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var _=()=>{},A={debug:_,info:_,warn:(...t)=>console.warn("[workflow]",...t),error:(...t)=>console.error("[workflow]",...t)},l={impl:A};var d={debug:(...t)=>l.impl.debug?.(...t),info:(...t)=>l.impl.info?.(...t),warn:(...t)=>l.impl.warn?.(...t),error:(...t)=>l.impl.error?.(...t)};var P=2e3,m=600*1e3,O=new Set(["completed","failed","canceled","timeout"]);function U(){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 j(){let t=process.env.PROJECT_ID;if(!t)throw new Error("Sub-graph dispatch requires PROJECT_ID env var.");return t}function x(){let t=process.env.PROJECT_API_TOKEN;if(!t)throw new Error("Sub-graph dispatch requires PROJECT_API_TOKEN env var.");return t}function J(){return process.env.EXECUTION_ID||null}function C(t,r){return r==null?t:typeof r=="function"?r(t):typeof r=="string"?r.split(".").reduce((p,g)=>p==null?p:p[g],t):t}async function M(t,r={}){if(!t||typeof t!="string")throw new Error("dispatchSubgraph: workflowName (string) is required");let p=U(),g=j(),$=x(),h=J(),w=`${p}/projects/${encodeURIComponent(g)}/workflows/${encodeURIComponent(t)}/trigger`,T={input:r.input||{},...h?{parentExecutionId:h}:{},...r.conversationId?{conversationId:r.conversationId}:{}};d.info(`[sub-graph] dispatching '${t}' (${r.async?"async":"sync"}) from parent ${h||"<none>"}`);let o=await fetch(w,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${$}`},body:JSON.stringify(T)});if(!o.ok){let n=null,i="";try{n=await o.json(),i=n?.error||n?.message||JSON.stringify(n)}catch{i=await o.text().catch(()=>"")}if(o.status===429){let e=n?.quotaInfo||{},c=new Error(`Sub-graph '${t}' blocked by execution quota (${e.used??"?"}/${e.limit??"?"} on plan ${e.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=t,c.quotaInfo=e,c}if(o.status===400){let e=new Error(`Sub-graph '${t}' rejected input: ${i}`);throw e.code="SUBGRAPH_INVALID_INPUT",e.status=400,e.subgraph=t,e.validationErrors=n?.validationErrors||null,e.missing=n?.missing||null,e}let u=new Error(`Sub-graph '${t}' trigger rejected (${o.status}): ${i}`);throw u.code="SUBGRAPH_TRIGGER_FAILED",u.status=o.status,u.subgraph=t,u}let b=await o.json(),s=b?.data?.jobId||b?.jobId;if(!s)throw new Error(`Sub-graph '${t}' trigger returned no jobId: ${JSON.stringify(b).slice(0,200)}`);if(r.async)return d.info(`[sub-graph] async dispatch of '${t}' \u2192 jobId=${s} (not waiting)`),{jobId:s,status:"accepted",workflow:t};let E=Number.isFinite(r.timeoutMs)?r.timeoutMs:m,y=Number.isFinite(r.pollIntervalMs)?r.pollIntervalMs:P,R=`${p}/executions/${encodeURIComponent(s)}`,v=Date.now()+E,a="accepted",S=0;for(;Date.now()<v;){await new Promise(e=>setTimeout(e,y)),S+=1;let n=await fetch(R,{headers:{Authorization:`Bearer ${$}`}});if(!n.ok){if(n.status>=500){d.warn(`[sub-graph] status poll for ${s} returned ${n.status}, will retry`);continue}throw new Error(`Sub-graph status poll failed for ${s}: ${n.status}`)}let i=await n.json(),u=i?.data||i?.execution||i;if(a=u?.status||a,O.has(a)){if(a!=="completed"){let I=new Error(`Sub-graph '${t}' (${s}) ended in status '${a}'`);throw I.subgraphJobId=s,I.subgraphStatus=a,I}let e=u?.finalState||u?.state||{},c=C(e,r.output);return d.info(`[sub-graph] '${t}' (${s}) completed after ${S} polls`),c}}let f=new Error(`Sub-graph '${t}' (${s}) timed out after ${Math.round(E/1e3)}s (last status: ${a})`);throw f.subgraphJobId=s,f.subgraphStatus=a,f}export{M as dispatchSubgraph};
|