@zibby/core 0.3.12 → 0.3.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +1 -1
- package/dist/package.json +1 -1
- package/dist/register-built-in-strategies.js +1 -1
- package/dist/strategies/claude-strategy.js +2 -2
- package/dist/strategies/index.js +1 -1
- package/dist/templates/browser-test-automation/graph.mjs +14 -2
- package/dist/templates/browser-test-automation/nodes/cache-replay.mjs +213 -0
- package/dist/templates/browser-test-automation/nodes/index.mjs +1 -0
- package/package.json +1 -1
- package/templates/browser-test-automation/graph.mjs +14 -2
- package/templates/browser-test-automation/nodes/cache-replay.mjs +213 -0
- package/templates/browser-test-automation/nodes/index.mjs +1 -0
package/dist/index.js
CHANGED
|
@@ -75,7 +75,7 @@ Stderr: ${p.slice(-1e3)}`:""}${m.trim()?`
|
|
|
75
75
|
Stdout (last 500 chars): ${m.slice(-500)}`:""}`));return}let _=O.getResult(),B=_?JSON.stringify(_,null,2):O.getRawText()||m||"";a({stdout:m||p||"",parsedText:B})}),v.on("error",y=>{if($(),clearTimeout(U),clearInterval(N),E&&clearInterval(E),A&&clearTimeout(A),l&&!l.aborted)try{l.removeEventListener("abort",D)}catch{}u(new Error(`Cursor Agent spawn error: ${y.message}
|
|
76
76
|
Binary: ${e}
|
|
77
77
|
This usually means the binary is not in PATH. Try:
|
|
78
|
-
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.zshrc && source ~/.zshrc`))})})}};import{AgentStrategy as Ln,getSkill as Dn}from"@zibby/agent-workflow";import{query as Un}from"@anthropic-ai/claude-agent-sdk";import{zodToJsonSchema as Bn}from"zod-to-json-schema";import{writeFileSync as mt,mkdirSync as gt,existsSync as sr,readFileSync as or}from"fs";import{homedir as ir}from"os";import{join as ge}from"path";var re=class extends Ln{constructor(){super("claude","Claude (Anthropic API)",50)}canHandle(e){let t=!!process.env.ANTHROPIC_API_KEY||!!process.env.ANTHROPIC_API_KEY_POOL,r=!!process.env.CLAUDE_CODE_OAUTH_TOKEN||!!process.env.CLAUDE_CODE_OAUTH_TOKEN_POOL,n=!!process.env.ANTHROPIC_AUTH_TOKEN||!!process.env.ANTHROPIC_AUTH_TOKEN_POOL,s=t||r||n;return s||f.debug("ClaudeAgentStrategy: no credentials. Set one of ANTHROPIC_API_KEY / CLAUDE_CODE_OAUTH_TOKEN / ANTHROPIC_AUTH_TOKEN (or a *_POOL variant for multi-token rotation)."),s}async invoke(e,t={}){let{model:r,workspace:n=process.cwd(),schema:s=null,images:i=[],skills:c=null,sessionPath:l=null,nodeName:a=null,timeout:u,config:d={},signal:m=null}=t,p=r;(!p||p==="auto")&&(f.debug(`Model is '${p||"undefined"}', using default: ${j.CLAUDE}`),p=j.CLAUDE);let g=pt[p]||p;pt[p]&&p!==g&&f.debug(`Mapped model: ${p} \u2192 ${g}`);let S=[J("ANTHROPIC_API_KEY"),J("CLAUDE_CODE_OAUTH_TOKEN"),J("ANTHROPIC_AUTH_TOKEN")];try{f.debug(`Invoking Claude Agent SDK with model: ${g}, skills: ${JSON.stringify(c)}`);let h=process.env.CLAUDE_CODE_OAUTH_TOKEN,b=process.env.ANTHROPIC_API_KEY,x=process.env.ANTHROPIC_AUTH_TOKEN;process.env.CLAUDE_CODE_CREDENTIALS_JSON?Gn(process.env.CLAUDE_CODE_CREDENTIALS_JSON):h&&jn(h,process.env.__ZIBBY_CLAUDE_PLAN);let T=process.env.__ZIBBY_CLAUDE_PLAN,w=T?T.charAt(0).toUpperCase()+T.slice(1):null,I;h?I=w?`Claude ${w} plan \xB7 ***${h.slice(-4)}`:`Subscription \xB7 ***${h.slice(-4)}`:b?I=`API key \xB7 ***${b.slice(-4)}`:x?I=`Bearer \xB7 ***${x.slice(-4)}`:I="NONE \u2014 request will fail";let $=(await import("chalk")).default;console.log(""),console.log(` ${$.cyan("\u25C6")} ${$.bold(g)}`),console.log(` ${$.dim("Auth: ")}${I}`),console.log(` ${$.dim("Workdir: ")}${n}`),console.log(""),console.log($.bold("Prompt sent to LLM:")),console.log($.dim("\u2500".repeat(60))),console.log($.dim(e)),console.log($.dim("\u2500".repeat(60)));let{allowedTools:C,mcpServers:v}=this._resolveSkills(c,{sessionPath:l,workspace:n,nodeName:a});try{let A={cwd:n,allowedTools:C,
|
|
78
|
+
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.zshrc && source ~/.zshrc`))})})}};import{AgentStrategy as Ln,getSkill as Dn}from"@zibby/agent-workflow";import{query as Un}from"@anthropic-ai/claude-agent-sdk";import{zodToJsonSchema as Bn}from"zod-to-json-schema";import{writeFileSync as mt,mkdirSync as gt,existsSync as sr,readFileSync as or}from"fs";import{homedir as ir}from"os";import{join as ge}from"path";var re=class extends Ln{constructor(){super("claude","Claude (Anthropic API)",50)}canHandle(e){let t=!!process.env.ANTHROPIC_API_KEY||!!process.env.ANTHROPIC_API_KEY_POOL,r=!!process.env.CLAUDE_CODE_OAUTH_TOKEN||!!process.env.CLAUDE_CODE_OAUTH_TOKEN_POOL,n=!!process.env.ANTHROPIC_AUTH_TOKEN||!!process.env.ANTHROPIC_AUTH_TOKEN_POOL,s=t||r||n;return s||f.debug("ClaudeAgentStrategy: no credentials. Set one of ANTHROPIC_API_KEY / CLAUDE_CODE_OAUTH_TOKEN / ANTHROPIC_AUTH_TOKEN (or a *_POOL variant for multi-token rotation)."),s}async invoke(e,t={}){let{model:r,workspace:n=process.cwd(),schema:s=null,images:i=[],skills:c=null,sessionPath:l=null,nodeName:a=null,timeout:u,config:d={},signal:m=null}=t,p=r;(!p||p==="auto")&&(f.debug(`Model is '${p||"undefined"}', using default: ${j.CLAUDE}`),p=j.CLAUDE);let g=pt[p]||p;pt[p]&&p!==g&&f.debug(`Mapped model: ${p} \u2192 ${g}`);let S=[J("ANTHROPIC_API_KEY"),J("CLAUDE_CODE_OAUTH_TOKEN"),J("ANTHROPIC_AUTH_TOKEN")];try{f.debug(`Invoking Claude Agent SDK with model: ${g}, skills: ${JSON.stringify(c)}`);let h=process.env.CLAUDE_CODE_OAUTH_TOKEN,b=process.env.ANTHROPIC_API_KEY,x=process.env.ANTHROPIC_AUTH_TOKEN;process.env.CLAUDE_CODE_CREDENTIALS_JSON?Gn(process.env.CLAUDE_CODE_CREDENTIALS_JSON):h&&jn(h,process.env.__ZIBBY_CLAUDE_PLAN);let T=process.env.__ZIBBY_CLAUDE_PLAN,w=T?T.charAt(0).toUpperCase()+T.slice(1):null,I;h?I=w?`Claude ${w} plan \xB7 ***${h.slice(-4)}`:`Subscription \xB7 ***${h.slice(-4)}`:b?I=`API key \xB7 ***${b.slice(-4)}`:x?I=`Bearer \xB7 ***${x.slice(-4)}`:I="NONE \u2014 request will fail";let $=(await import("chalk")).default;console.log(""),console.log(` ${$.cyan("\u25C6")} ${$.bold(g)}`),console.log(` ${$.dim("Auth: ")}${I}`),console.log(` ${$.dim("Workdir: ")}${n}`),console.log(""),console.log($.bold("Prompt sent to LLM:")),console.log($.dim("\u2500".repeat(60))),console.log($.dim(e)),console.log($.dim("\u2500".repeat(60)));let{allowedTools:C,mcpServers:v}=this._resolveSkills(c,{sessionPath:l,workspace:n,nodeName:a});try{let A={cwd:n,allowedTools:C,settings:{permissions:{allow:["Bash(*)","Read(*)","Write(*)","Edit(*)","MultiEdit(*)","NotebookEdit(*)","Grep(*)","Glob(*)","WebFetch(*)","WebSearch(*)","Task(*)","TodoWrite(*)","Skill(*)"]}},model:g,...Object.keys(v).length>0&&{mcpServers:v}};if(s){let k=typeof s.parse=="function"?Bn(s,{target:"openApi3"}):s;A.outputFormat={type:"json_schema",schema:k},f.debug("Structured output enforced via SDK outputFormat")}f.debug(`Agent SDK options: ${JSON.stringify({cwd:A.cwd,toolCount:C.length,permissionMode:A.permissionMode,model:A.model,hasOutputFormat:!!A.outputFormat})}`);let D="",E=0,R=[];f.debug("Starting Claude Agent SDK query stream");let M;try{M=Un({prompt:e,options:A})}catch(y){throw f.error(`Failed to initialize Claude Agent SDK: ${y.message}`),y}let N=null,U=0,O=3;if(m?.aborted){let y=new Error("Aborted via signal");throw y.name="AbortError",y}try{for await(let y of M){if(m?.aborted){let P=new Error("Aborted via signal");throw P.name="AbortError",P}if(R.push(y),y.type==="error"||y.error){let P=y.error?.message||y.error||y.message||"Unknown API error";try{f.error("SDK error event raw payload",{fullMessage:JSON.stringify(y,null,2)})}catch{}throw new Error(typeof P=="string"?P:JSON.stringify(P))}let k=JSON.stringify(y.message?.content||y.text||"").slice(0,200);if(k===N){if(U++,U>=O){let P=(y.message?.content?.[0]?.text||y.text||"unknown").slice(0,100);throw new Error(`API stuck in loop (${U}x repeated): ${P}`)}}else N=k,U=1;if(y.type==="assistant"||y.constructor?.name==="AssistantMessage"){let P=y.message?.content||y.content||[];for(let _ of P)if(_.type==="thinking"&&_.thinking)console.log(`${_.thinking.substring(0,200)}${_.thinking.length>200?"...":""}`);else if(_.type==="text"&&_.text)D+=_.text,_.text.length<500?console.log(`${_.text}`):console.log(`${_.text.substring(0,200)}... (${_.text.length} chars)`);else if(_.type==="tool_use"){E++,_.name.includes("memory")?W.stepMemory(`Tool: ${_.name}`):W.stepTool(`Tool: ${_.name}`);let L=JSON.stringify(_.input).substring(0,100);console.log(` Input: ${L}${JSON.stringify(_.input).length>100?"...":""}`)}}else if(!(y.type==="user"&&y.tool_use_result)){if(y.type==="result"||y.constructor?.name==="ResultMessage"){let P=y.result||y.text||y.content||D;try{Fn({message:y,sessionPath:l,nodeName:a,model:p,toolCallCount:E})}catch(_){f.debug(`usage.json write failed: ${_.message}`)}if(s){if(y.structured_output){f.debug("Using SDK native structured_output");let B=typeof s.parse=="function"?s.parse(y.structured_output):y.structured_output;return{raw:P,structured:B}}if(P){let _=this._extractJson(P,s);if(_)return{raw:P,structured:_}}f.warn(`Could not extract structured output \u2014 returning raw text (${(P||"").length} chars)`)}return P||""}}}if(f.warn(`Agent SDK ended without result. Collected ${R.length} messages`),D.length>0)return f.debug("Returning accumulated text from messages"),D;throw new Error("Claude Agent SDK query ended without result")}catch(y){let k={message:y?.message,name:y?.name,code:y?.code,cause:y?.cause?.message||y?.cause,stack:y?.stack?.split(`
|
|
79
79
|
`).slice(0,5).join(`
|
|
80
80
|
`),stringified:(()=>{try{return JSON.stringify(y,Object.getOwnPropertyNames(Object(y)))}catch{return String(y)}})()};throw f.error(`Error during query stream: ${y?.message||y}`,k),y}}catch(A){throw f.error("Claude Agent SDK call failed",{message:A?.message,name:A?.name,code:A?.code,cause:A?.cause?.message||A?.cause}),A}}finally{for(let h of S)h.dispose()}}_resolveSkills(e,t){let r=["Read","Write","Edit","Bash","Grep","Glob"];if(e===null)return f.debug("No skills \u2014 pure LLM mode"),{allowedTools:[],mcpServers:{}};if(!Array.isArray(e)||e.length===0)return f.debug("Default IDE skills for code generation"),{allowedTools:[...r],mcpServers:{}};let n=[...r],s={};for(let i of e){let c=Dn(i);if(!c){f.warn(`Unknown skill "${i}" \u2014 skipping`);continue}if(c.allowedTools&&n.push(...c.allowedTools),typeof c.resolve=="function"){let l=c.resolve(t);l&&(s[c.serverName]=l,f.debug(`MCP: ${c.serverName} \u2192 ${l.command} ${l.args[0]}`))}}return{allowedTools:n,mcpServers:s}}_extractJson(e,t){let r=[()=>{if(e.includes("===JSON_START===")){let n=e.indexOf("===JSON_START===")+16,s=e.indexOf("===JSON_END===");return e.substring(n,s).trim()}},()=>e.match(/```json\s*\n([\s\S]*?)\n```/)?.[1]?.trim(),()=>{if(!e.startsWith("{"))return e.match(/```\s*\n([\s\S]*?)\n```/)?.[1]?.trim()},()=>e.trim(),()=>{let n=e.indexOf("{"),s=e.lastIndexOf("}");if(n!==-1&&s>n)return e.substring(n,s+1)}];for(let n of r)try{let s=n();if(!s)continue;let i=JSON.parse(s);if(typeof i!="object"||i===null)continue;return typeof t.parse=="function"?t.parse(i):i}catch{}return null}};function Fn({message:o,sessionPath:e,nodeName:t,model:r,toolCallCount:n}){if(!e||!t)return;let s=o?.usage||o?.message?.usage||{},i=Re(s.input_tokens,s.inputTokens),c=Re(s.output_tokens,s.outputTokens),l=Re(s.cache_read_input_tokens,s.cacheReadInputTokens),a=Re(s.cache_creation_input_tokens,s.cacheCreationInputTokens),u=Re(o?.duration_ms,o?.durationMs),d={model:r||null,input_tokens:i,output_tokens:c,cache_read_tokens:l,cache_creation_tokens:a,tool_call_count:typeof n=="number"?n:null,duration_ms:u,captured_at:new Date().toISOString()},m=ge(e,t);try{gt(m,{recursive:!0})}catch{}mt(ge(m,"usage.json"),JSON.stringify(d,null,2),"utf-8")}function Re(...o){for(let e of o){if(typeof e=="number"&&Number.isFinite(e))return e;if(typeof e=="string"&&e.trim()!==""&&!isNaN(Number(e)))return Number(e)}return null}function jn(o,e){let t=ge(ir(),".claude"),r=ge(t,".credentials.json"),n={claudeAiOauth:{accessToken:o,...e?{subscriptionType:String(e).toLowerCase()}:{}}};try{if(sr(r)&&JSON.parse(or(r,"utf-8"))?.claudeAiOauth?.accessToken===o)return;gt(t,{recursive:!0}),mt(r,JSON.stringify(n,null,2),"utf-8"),f.debug(`Wrote Claude Code OAuth credentials to ${r}`)}catch(s){f.warn(`Could not write ${r}: ${s.message} \u2014 Claude SDK may fail to authenticate.`)}}function Gn(o){let e=ge(ir(),".claude"),t=ge(e,".credentials.json"),r;try{r=JSON.parse(o)}catch(n){f.warn(`CLAUDE_CODE_CREDENTIALS_JSON not valid JSON: ${n.message}`);return}if(!r?.claudeAiOauth?.accessToken){f.warn("CLAUDE_CODE_CREDENTIALS_JSON missing claudeAiOauth.accessToken \u2014 skipping write.");return}try{if(sr(t)){let n=JSON.parse(or(t,"utf-8"));if(n?.claudeAiOauth?.accessToken===r.claudeAiOauth.accessToken&&n?.claudeAiOauth?.expiresAt===r.claudeAiOauth.expiresAt)return}gt(e,{recursive:!0}),mt(t,JSON.stringify(r,null,2),"utf-8"),f.debug(`Wrote full Claude Code credentials blob to ${t}`)}catch(n){f.warn(`Could not write ${t}: ${n.message} \u2014 Claude SDK may fail to authenticate.`)}}import{AgentStrategy as Jn,getSkill as zn}from"@zibby/agent-workflow";import{execSync as Kn}from"node:child_process";import{zodToJsonSchema as Yn}from"zod-to-json-schema";var ne=class extends Jn{constructor(){super("codex","Codex (OpenAI)",75)}canHandle(e){if(!!!(process.env.OPENAI_API_KEY||process.env.OPENAI_API_KEY_POOL||process.env.CODEX_API_KEY||process.env.CODEX_API_KEY_POOL))return f.debug("CodexAgentStrategy: OPENAI_API_KEY or CODEX_API_KEY not set"),!1;try{return Kn("codex --version",{encoding:"utf-8",timeout:5e3,stdio:"pipe"}),!0}catch{return f.warn("[Codex] codex CLI not found. Install: npm install -g @openai/codex"),!1}}async invoke(e,t={}){let{model:r,workspace:n=process.cwd(),schema:s=null,skills:i=null,sessionPath:c=null,nodeName:l=null,timeout:a,config:u={},signal:d=null}=t,m=[J("OPENAI_API_KEY"),J("CODEX_API_KEY")];try{let{Codex:p}=await import("@openai/codex-sdk"),g=r;(!g||g==="auto")&&(f.debug(`Model is '${g||"undefined"}', using default: ${j.CODEX}`),g=j.CODEX);let S=ft[g]||g;ft[g]&&g!==S&&f.debug(`Mapped model: ${g} \u2192 ${S}`),f.debug(`Invoking Codex SDK with model: ${S}, skills: ${JSON.stringify(i)}`);let h=process.env.CODEX_API_KEY||process.env.OPENAI_API_KEY;h&&!process.env.CODEX_API_KEY&&(process.env.CODEX_API_KEY=h);let b=h?` | key: ***${h.slice(-4)}`:" | key: not set";console.log(`
|
|
81
81
|
\u25C6 Model: ${S}${b}
|
package/dist/package.json
CHANGED
|
@@ -75,7 +75,7 @@ Stderr: ${h.slice(-1e3)}`:""}${m.trim()?`
|
|
|
75
75
|
Stdout (last 500 chars): ${m.slice(-500)}`:""}`));return}let d=w.getResult(),D=d?JSON.stringify(d,null,2):w.getRawText()||m||"";p({stdout:m||h||"",parsedText:D})}),E.on("error",u=>{if(C(),clearTimeout(B),clearInterval(I),b&&clearInterval(b),S&&clearTimeout(S),c&&!c.aborted)try{c.removeEventListener("abort",M)}catch{}f(new Error(`Cursor Agent spawn error: ${u.message}
|
|
76
76
|
Binary: ${e}
|
|
77
77
|
This usually means the binary is not in PATH. Try:
|
|
78
|
-
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.zshrc && source ~/.zshrc`))})})}};import{AgentStrategy as Bt,getSkill as Kt}from"@zibby/agent-workflow";import{query as Jt}from"@anthropic-ai/claude-agent-sdk";import{zodToJsonSchema as Gt}from"zod-to-json-schema";import{writeFileSync as Re,mkdirSync as Me,existsSync as tt,readFileSync as ot}from"fs";import{homedir as rt}from"os";import{join as ee}from"path";var ye=class extends Bt{constructor(){super("claude","Claude (Anthropic API)",50)}canHandle(e){let t=!!process.env.ANTHROPIC_API_KEY||!!process.env.ANTHROPIC_API_KEY_POOL,r=!!process.env.CLAUDE_CODE_OAUTH_TOKEN||!!process.env.CLAUDE_CODE_OAUTH_TOKEN_POOL,o=!!process.env.ANTHROPIC_AUTH_TOKEN||!!process.env.ANTHROPIC_AUTH_TOKEN_POOL,s=t||r||o;return s||a.debug("ClaudeAgentStrategy: no credentials. Set one of ANTHROPIC_API_KEY / CLAUDE_CODE_OAUTH_TOKEN / ANTHROPIC_AUTH_TOKEN (or a *_POOL variant for multi-token rotation)."),s}async invoke(e,t={}){let{model:r,workspace:o=process.cwd(),schema:s=null,images:i=[],skills:l=null,sessionPath:c=null,nodeName:p=null,timeout:f,config:_={},signal:m=null}=t,h=r;(!h||h==="auto")&&(a.debug(`Model is '${h||"undefined"}', using default: ${K.CLAUDE}`),h=K.CLAUDE);let O=ke[h]||h;ke[h]&&h!==O&&a.debug(`Mapped model: ${h} \u2192 ${O}`);let L=[G("ANTHROPIC_API_KEY"),G("CLAUDE_CODE_OAUTH_TOKEN"),G("ANTHROPIC_AUTH_TOKEN")];try{a.debug(`Invoking Claude Agent SDK with model: ${O}, skills: ${JSON.stringify(l)}`);let y=process.env.CLAUDE_CODE_OAUTH_TOKEN,x=process.env.ANTHROPIC_API_KEY,$=process.env.ANTHROPIC_AUTH_TOKEN;process.env.CLAUDE_CODE_CREDENTIALS_JSON?Yt(process.env.CLAUDE_CODE_CREDENTIALS_JSON):y&&Ft(y,process.env.__ZIBBY_CLAUDE_PLAN);let v=process.env.__ZIBBY_CLAUDE_PLAN,g=v?v.charAt(0).toUpperCase()+v.slice(1):null,R;y?R=g?`Claude ${g} plan \xB7 ***${y.slice(-4)}`:`Subscription \xB7 ***${y.slice(-4)}`:x?R=`API key \xB7 ***${x.slice(-4)}`:$?R=`Bearer \xB7 ***${$.slice(-4)}`:R="NONE \u2014 request will fail";let C=(await import("chalk")).default;console.log(""),console.log(` ${C.cyan("\u25C6")} ${C.bold(O)}`),console.log(` ${C.dim("Auth: ")}${R}`),console.log(` ${C.dim("Workdir: ")}${o}`),console.log(""),console.log(C.bold("Prompt sent to LLM:")),console.log(C.dim("\u2500".repeat(60))),console.log(C.dim(e)),console.log(C.dim("\u2500".repeat(60)));let{allowedTools:k,mcpServers:E}=this._resolveSkills(l,{sessionPath:c,workspace:o,nodeName:p});try{let S={cwd:o,allowedTools:k,
|
|
78
|
+
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.zshrc && source ~/.zshrc`))})})}};import{AgentStrategy as Bt,getSkill as Kt}from"@zibby/agent-workflow";import{query as Jt}from"@anthropic-ai/claude-agent-sdk";import{zodToJsonSchema as Gt}from"zod-to-json-schema";import{writeFileSync as Re,mkdirSync as Me,existsSync as tt,readFileSync as ot}from"fs";import{homedir as rt}from"os";import{join as ee}from"path";var ye=class extends Bt{constructor(){super("claude","Claude (Anthropic API)",50)}canHandle(e){let t=!!process.env.ANTHROPIC_API_KEY||!!process.env.ANTHROPIC_API_KEY_POOL,r=!!process.env.CLAUDE_CODE_OAUTH_TOKEN||!!process.env.CLAUDE_CODE_OAUTH_TOKEN_POOL,o=!!process.env.ANTHROPIC_AUTH_TOKEN||!!process.env.ANTHROPIC_AUTH_TOKEN_POOL,s=t||r||o;return s||a.debug("ClaudeAgentStrategy: no credentials. Set one of ANTHROPIC_API_KEY / CLAUDE_CODE_OAUTH_TOKEN / ANTHROPIC_AUTH_TOKEN (or a *_POOL variant for multi-token rotation)."),s}async invoke(e,t={}){let{model:r,workspace:o=process.cwd(),schema:s=null,images:i=[],skills:l=null,sessionPath:c=null,nodeName:p=null,timeout:f,config:_={},signal:m=null}=t,h=r;(!h||h==="auto")&&(a.debug(`Model is '${h||"undefined"}', using default: ${K.CLAUDE}`),h=K.CLAUDE);let O=ke[h]||h;ke[h]&&h!==O&&a.debug(`Mapped model: ${h} \u2192 ${O}`);let L=[G("ANTHROPIC_API_KEY"),G("CLAUDE_CODE_OAUTH_TOKEN"),G("ANTHROPIC_AUTH_TOKEN")];try{a.debug(`Invoking Claude Agent SDK with model: ${O}, skills: ${JSON.stringify(l)}`);let y=process.env.CLAUDE_CODE_OAUTH_TOKEN,x=process.env.ANTHROPIC_API_KEY,$=process.env.ANTHROPIC_AUTH_TOKEN;process.env.CLAUDE_CODE_CREDENTIALS_JSON?Yt(process.env.CLAUDE_CODE_CREDENTIALS_JSON):y&&Ft(y,process.env.__ZIBBY_CLAUDE_PLAN);let v=process.env.__ZIBBY_CLAUDE_PLAN,g=v?v.charAt(0).toUpperCase()+v.slice(1):null,R;y?R=g?`Claude ${g} plan \xB7 ***${y.slice(-4)}`:`Subscription \xB7 ***${y.slice(-4)}`:x?R=`API key \xB7 ***${x.slice(-4)}`:$?R=`Bearer \xB7 ***${$.slice(-4)}`:R="NONE \u2014 request will fail";let C=(await import("chalk")).default;console.log(""),console.log(` ${C.cyan("\u25C6")} ${C.bold(O)}`),console.log(` ${C.dim("Auth: ")}${R}`),console.log(` ${C.dim("Workdir: ")}${o}`),console.log(""),console.log(C.bold("Prompt sent to LLM:")),console.log(C.dim("\u2500".repeat(60))),console.log(C.dim(e)),console.log(C.dim("\u2500".repeat(60)));let{allowedTools:k,mcpServers:E}=this._resolveSkills(l,{sessionPath:c,workspace:o,nodeName:p});try{let S={cwd:o,allowedTools:k,settings:{permissions:{allow:["Bash(*)","Read(*)","Write(*)","Edit(*)","MultiEdit(*)","NotebookEdit(*)","Grep(*)","Glob(*)","WebFetch(*)","WebSearch(*)","Task(*)","TodoWrite(*)","Skill(*)"]}},model:O,...Object.keys(E).length>0&&{mcpServers:E}};if(s){let T=typeof s.parse=="function"?Gt(s,{target:"openApi3"}):s;S.outputFormat={type:"json_schema",schema:T},a.debug("Structured output enforced via SDK outputFormat")}a.debug(`Agent SDK options: ${JSON.stringify({cwd:S.cwd,toolCount:k.length,permissionMode:S.permissionMode,model:S.model,hasOutputFormat:!!S.outputFormat})}`);let M="",b=0,P=[];a.debug("Starting Claude Agent SDK query stream");let U;try{U=Jt({prompt:e,options:S})}catch(u){throw a.error(`Failed to initialize Claude Agent SDK: ${u.message}`),u}let I=null,B=0,w=3;if(m?.aborted){let u=new Error("Aborted via signal");throw u.name="AbortError",u}try{for await(let u of U){if(m?.aborted){let A=new Error("Aborted via signal");throw A.name="AbortError",A}if(P.push(u),u.type==="error"||u.error){let A=u.error?.message||u.error||u.message||"Unknown API error";try{a.error("SDK error event raw payload",{fullMessage:JSON.stringify(u,null,2)})}catch{}throw new Error(typeof A=="string"?A:JSON.stringify(A))}let T=JSON.stringify(u.message?.content||u.text||"").slice(0,200);if(T===I){if(B++,B>=w){let A=(u.message?.content?.[0]?.text||u.text||"unknown").slice(0,100);throw new Error(`API stuck in loop (${B}x repeated): ${A}`)}}else I=T,B=1;if(u.type==="assistant"||u.constructor?.name==="AssistantMessage"){let A=u.message?.content||u.content||[];for(let d of A)if(d.type==="thinking"&&d.thinking)console.log(`${d.thinking.substring(0,200)}${d.thinking.length>200?"...":""}`);else if(d.type==="text"&&d.text)M+=d.text,d.text.length<500?console.log(`${d.text}`):console.log(`${d.text.substring(0,200)}... (${d.text.length} chars)`);else if(d.type==="tool_use"){b++,d.name.includes("memory")?Y.stepMemory(`Tool: ${d.name}`):Y.stepTool(`Tool: ${d.name}`);let N=JSON.stringify(d.input).substring(0,100);console.log(` Input: ${N}${JSON.stringify(d.input).length>100?"...":""}`)}}else if(!(u.type==="user"&&u.tool_use_result)){if(u.type==="result"||u.constructor?.name==="ResultMessage"){let A=u.result||u.text||u.content||M;try{jt({message:u,sessionPath:c,nodeName:p,model:h,toolCallCount:b})}catch(d){a.debug(`usage.json write failed: ${d.message}`)}if(s){if(u.structured_output){a.debug("Using SDK native structured_output");let D=typeof s.parse=="function"?s.parse(u.structured_output):u.structured_output;return{raw:A,structured:D}}if(A){let d=this._extractJson(A,s);if(d)return{raw:A,structured:d}}a.warn(`Could not extract structured output \u2014 returning raw text (${(A||"").length} chars)`)}return A||""}}}if(a.warn(`Agent SDK ended without result. Collected ${P.length} messages`),M.length>0)return a.debug("Returning accumulated text from messages"),M;throw new Error("Claude Agent SDK query ended without result")}catch(u){let T={message:u?.message,name:u?.name,code:u?.code,cause:u?.cause?.message||u?.cause,stack:u?.stack?.split(`
|
|
79
79
|
`).slice(0,5).join(`
|
|
80
80
|
`),stringified:(()=>{try{return JSON.stringify(u,Object.getOwnPropertyNames(Object(u)))}catch{return String(u)}})()};throw a.error(`Error during query stream: ${u?.message||u}`,T),u}}catch(S){throw a.error("Claude Agent SDK call failed",{message:S?.message,name:S?.name,code:S?.code,cause:S?.cause?.message||S?.cause}),S}}finally{for(let y of L)y.dispose()}}_resolveSkills(e,t){let r=["Read","Write","Edit","Bash","Grep","Glob"];if(e===null)return a.debug("No skills \u2014 pure LLM mode"),{allowedTools:[],mcpServers:{}};if(!Array.isArray(e)||e.length===0)return a.debug("Default IDE skills for code generation"),{allowedTools:[...r],mcpServers:{}};let o=[...r],s={};for(let i of e){let l=Kt(i);if(!l){a.warn(`Unknown skill "${i}" \u2014 skipping`);continue}if(l.allowedTools&&o.push(...l.allowedTools),typeof l.resolve=="function"){let c=l.resolve(t);c&&(s[l.serverName]=c,a.debug(`MCP: ${l.serverName} \u2192 ${c.command} ${c.args[0]}`))}}return{allowedTools:o,mcpServers:s}}_extractJson(e,t){let r=[()=>{if(e.includes("===JSON_START===")){let o=e.indexOf("===JSON_START===")+16,s=e.indexOf("===JSON_END===");return e.substring(o,s).trim()}},()=>e.match(/```json\s*\n([\s\S]*?)\n```/)?.[1]?.trim(),()=>{if(!e.startsWith("{"))return e.match(/```\s*\n([\s\S]*?)\n```/)?.[1]?.trim()},()=>e.trim(),()=>{let o=e.indexOf("{"),s=e.lastIndexOf("}");if(o!==-1&&s>o)return e.substring(o,s+1)}];for(let o of r)try{let s=o();if(!s)continue;let i=JSON.parse(s);if(typeof i!="object"||i===null)continue;return typeof t.parse=="function"?t.parse(i):i}catch{}return null}};function jt({message:n,sessionPath:e,nodeName:t,model:r,toolCallCount:o}){if(!e||!t)return;let s=n?.usage||n?.message?.usage||{},i=ce(s.input_tokens,s.inputTokens),l=ce(s.output_tokens,s.outputTokens),c=ce(s.cache_read_input_tokens,s.cacheReadInputTokens),p=ce(s.cache_creation_input_tokens,s.cacheCreationInputTokens),f=ce(n?.duration_ms,n?.durationMs),_={model:r||null,input_tokens:i,output_tokens:l,cache_read_tokens:c,cache_creation_tokens:p,tool_call_count:typeof o=="number"?o:null,duration_ms:f,captured_at:new Date().toISOString()},m=ee(e,t);try{Me(m,{recursive:!0})}catch{}Re(ee(m,"usage.json"),JSON.stringify(_,null,2),"utf-8")}function ce(...n){for(let e of n){if(typeof e=="number"&&Number.isFinite(e))return e;if(typeof e=="string"&&e.trim()!==""&&!isNaN(Number(e)))return Number(e)}return null}function Ft(n,e){let t=ee(rt(),".claude"),r=ee(t,".credentials.json"),o={claudeAiOauth:{accessToken:n,...e?{subscriptionType:String(e).toLowerCase()}:{}}};try{if(tt(r)&&JSON.parse(ot(r,"utf-8"))?.claudeAiOauth?.accessToken===n)return;Me(t,{recursive:!0}),Re(r,JSON.stringify(o,null,2),"utf-8"),a.debug(`Wrote Claude Code OAuth credentials to ${r}`)}catch(s){a.warn(`Could not write ${r}: ${s.message} \u2014 Claude SDK may fail to authenticate.`)}}function Yt(n){let e=ee(rt(),".claude"),t=ee(e,".credentials.json"),r;try{r=JSON.parse(n)}catch(o){a.warn(`CLAUDE_CODE_CREDENTIALS_JSON not valid JSON: ${o.message}`);return}if(!r?.claudeAiOauth?.accessToken){a.warn("CLAUDE_CODE_CREDENTIALS_JSON missing claudeAiOauth.accessToken \u2014 skipping write.");return}try{if(tt(t)){let o=JSON.parse(ot(t,"utf-8"));if(o?.claudeAiOauth?.accessToken===r.claudeAiOauth.accessToken&&o?.claudeAiOauth?.expiresAt===r.claudeAiOauth.expiresAt)return}Me(e,{recursive:!0}),Re(t,JSON.stringify(r,null,2),"utf-8"),a.debug(`Wrote full Claude Code credentials blob to ${t}`)}catch(o){a.warn(`Could not write ${t}: ${o.message} \u2014 Claude SDK may fail to authenticate.`)}}import{AgentStrategy as zt,getSkill as Ht}from"@zibby/agent-workflow";import{execSync as Xt}from"node:child_process";import{zodToJsonSchema as Zt}from"zod-to-json-schema";var _e=class extends zt{constructor(){super("codex","Codex (OpenAI)",75)}canHandle(e){if(!!!(process.env.OPENAI_API_KEY||process.env.OPENAI_API_KEY_POOL||process.env.CODEX_API_KEY||process.env.CODEX_API_KEY_POOL))return a.debug("CodexAgentStrategy: OPENAI_API_KEY or CODEX_API_KEY not set"),!1;try{return Xt("codex --version",{encoding:"utf-8",timeout:5e3,stdio:"pipe"}),!0}catch{return a.warn("[Codex] codex CLI not found. Install: npm install -g @openai/codex"),!1}}async invoke(e,t={}){let{model:r,workspace:o=process.cwd(),schema:s=null,skills:i=null,sessionPath:l=null,nodeName:c=null,timeout:p,config:f={},signal:_=null}=t,m=[G("OPENAI_API_KEY"),G("CODEX_API_KEY")];try{let{Codex:h}=await import("@openai/codex-sdk"),O=r;(!O||O==="auto")&&(a.debug(`Model is '${O||"undefined"}', using default: ${K.CODEX}`),O=K.CODEX);let L=Pe[O]||O;Pe[O]&&O!==L&&a.debug(`Mapped model: ${O} \u2192 ${L}`),a.debug(`Invoking Codex SDK with model: ${L}, skills: ${JSON.stringify(i)}`);let y=process.env.CODEX_API_KEY||process.env.OPENAI_API_KEY;y&&!process.env.CODEX_API_KEY&&(process.env.CODEX_API_KEY=y);let x=y?` | key: ***${y.slice(-4)}`:" | key: not set";console.log(`
|
|
81
81
|
\u25C6 Model: ${L}${x}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import{AgentStrategy as te,getSkill as oe}from"@zibby/agent-workflow";import{query as
|
|
1
|
+
import{AgentStrategy as te,getSkill as oe}from"@zibby/agent-workflow";import{query as ne}from"@anthropic-ai/claude-agent-sdk";import{zodToJsonSchema as se}from"zod-to-json-schema";import{writeFileSync as x,mkdirSync as P,existsSync as q,readFileSync as X}from"fs";import{homedir as z}from"os";import{join as A}from"path";import C from"chalk";var f={debug:0,info:1,warn:2,error:3,silent:4},v=class{constructor(){this._level=this._getLogLevel()}_getLogLevel(){if(process.env.ZIBBY_DEBUG==="true")return f.debug;if(process.env.ZIBBY_VERBOSE==="true")return f.info;let e=process.env.LOG_LEVEL?.toLowerCase();return e&&e in f?f[e]:f.info}_shouldLog(e){return f[e]>=this._level}_formatMessage(e,n,r={}){let s=new Date().toISOString(),a=`${this._getPrefix(e)} ${n}`;return Object.keys(r).length>0&&(a+=C.dim(` ${JSON.stringify(r)}`)),a}_getPrefix(e){return{debug:C.gray("[DEBUG]"),info:C.cyan("[INFO]"),warn:C.yellow("[WARN]"),error:C.red("\u274C [ERROR]")}[e]||""}debug(e,n){this._shouldLog("debug")&&console.log(this._formatMessage("debug",e,n))}info(e,n){this._shouldLog("info")&&console.log(this._formatMessage("info",e,n))}warn(e,n){this._shouldLog("warn")&&console.warn(this._formatMessage("warn",e,n))}error(e,n){this._shouldLog("error")&&console.error(this._formatMessage("error",e,n))}setLevel(e){e in f&&(this._level=f[e])}getLevel(){return Object.keys(f).find(e=>f[e]===this._level)}},i=new v;import{timeline as b,Timeline as pe,WORKFLOW_GRAPH_LOG_MARKER_PREFIX as ge}from"@zibby/agent-workflow";var I={ASSISTANT:"gpt-5.4-nano-2026-03-17",CLAUDE:"claude-sonnet-4-6",CURSOR:"auto",CODEX:"o4-mini",GEMINI:"gemini-2.5-pro",OPENAI_POSTPROCESSING:"gpt-4o-mini"};var $={auto:"claude-sonnet-4-6","sonnet-4.6":"claude-sonnet-4-6","sonnet-4-6":"claude-sonnet-4-6","opus-4.6":"claude-opus-4-6","opus-4-6":"claude-opus-4-6","sonnet-4.5":"claude-sonnet-4-5-20250929","sonnet-4-5":"claude-sonnet-4-5-20250929","opus-4.5":"claude-opus-4-20250514","opus-4-5":"claude-opus-4-20250514","claude-sonnet-4-6":"claude-sonnet-4-6","claude-opus-4-6":"claude-opus-4-6","claude-sonnet-4-5-20250929":"claude-sonnet-4-5-20250929","claude-opus-4-20250514":"claude-opus-4-20250514"};var fe={CURSOR_AGENT_DEFAULT:1200*1e3,OPENAI_REQUEST:18e4};function D(l){let e=`${l}_POOL`,n=process.env[e];if(!n||typeof n!="string")return{picked:null,count:0,dispose:()=>{}};let r=n.split(/[,\n]+/).map(a=>a.trim()).filter(Boolean);if(r.length===0)return{picked:null,count:0,dispose:()=>{}};let s=r[Math.floor(Math.random()*r.length)],o=process.env[l];return process.env[l]=s,i.debug(`[auth-pool] ${l}: picked 1 of ${r.length} from pool (***${s.slice(-4)})`),{picked:s,count:r.length,dispose(){o===void 0?delete process.env[l]:process.env[l]=o}}}var Z=class extends te{constructor(){super("claude","Claude (Anthropic API)",50)}canHandle(e){let n=!!process.env.ANTHROPIC_API_KEY||!!process.env.ANTHROPIC_API_KEY_POOL,r=!!process.env.CLAUDE_CODE_OAUTH_TOKEN||!!process.env.CLAUDE_CODE_OAUTH_TOKEN_POOL,s=!!process.env.ANTHROPIC_AUTH_TOKEN||!!process.env.ANTHROPIC_AUTH_TOKEN_POOL,o=n||r||s;return o||i.debug("ClaudeAgentStrategy: no credentials. Set one of ANTHROPIC_API_KEY / CLAUDE_CODE_OAUTH_TOKEN / ANTHROPIC_AUTH_TOKEN (or a *_POOL variant for multi-token rotation)."),o}async invoke(e,n={}){let{model:r,workspace:s=process.cwd(),schema:o=null,images:a=[],skills:d=null,sessionPath:m=null,nodeName:N=null,timeout:R,config:M={},signal:E=null}=n,g=r;(!g||g==="auto")&&(i.debug(`Model is '${g||"undefined"}', using default: ${I.CLAUDE}`),g=I.CLAUDE);let S=$[g]||g;$[g]&&g!==S&&i.debug(`Mapped model: ${g} \u2192 ${S}`);let Q=[D("ANTHROPIC_API_KEY"),D("CLAUDE_CODE_OAUTH_TOKEN"),D("ANTHROPIC_AUTH_TOKEN")];try{i.debug(`Invoking Claude Agent SDK with model: ${S}, skills: ${JSON.stringify(d)}`);let O=process.env.CLAUDE_CODE_OAUTH_TOKEN,U=process.env.ANTHROPIC_API_KEY,K=process.env.ANTHROPIC_AUTH_TOKEN;process.env.CLAUDE_CODE_CREDENTIALS_JSON?ce(process.env.CLAUDE_CODE_CREDENTIALS_JSON):O&&ie(O,process.env.__ZIBBY_CLAUDE_PLAN);let k=process.env.__ZIBBY_CLAUDE_PLAN,J=k?k.charAt(0).toUpperCase()+k.slice(1):null,T;O?T=J?`Claude ${J} plan \xB7 ***${O.slice(-4)}`:`Subscription \xB7 ***${O.slice(-4)}`:U?T=`API key \xB7 ***${U.slice(-4)}`:K?T=`Bearer \xB7 ***${K.slice(-4)}`:T="NONE \u2014 request will fail";let _=(await import("chalk")).default;console.log(""),console.log(` ${_.cyan("\u25C6")} ${_.bold(S)}`),console.log(` ${_.dim("Auth: ")}${T}`),console.log(` ${_.dim("Workdir: ")}${s}`),console.log(""),console.log(_.bold("Prompt sent to LLM:")),console.log(_.dim("\u2500".repeat(60))),console.log(_.dim(e)),console.log(_.dim("\u2500".repeat(60)));let{allowedTools:F,mcpServers:H}=this._resolveSkills(d,{sessionPath:m,workspace:s,nodeName:N});try{let p={cwd:s,allowedTools:F,settings:{permissions:{allow:["Bash(*)","Read(*)","Write(*)","Edit(*)","MultiEdit(*)","NotebookEdit(*)","Grep(*)","Glob(*)","WebFetch(*)","WebSearch(*)","Task(*)","TodoWrite(*)","Skill(*)"]}},model:S,...Object.keys(H).length>0&&{mcpServers:H}};if(o){let h=typeof o.parse=="function"?se(o,{target:"openApi3"}):o;p.outputFormat={type:"json_schema",schema:h},i.debug("Structured output enforced via SDK outputFormat")}i.debug(`Agent SDK options: ${JSON.stringify({cwd:p.cwd,toolCount:F.length,permissionMode:p.permissionMode,model:p.model,hasOutputFormat:!!p.outputFormat})}`);let L="",B=0,G=[];i.debug("Starting Claude Agent SDK query stream");let j;try{j=ne({prompt:e,options:p})}catch(t){throw i.error(`Failed to initialize Claude Agent SDK: ${t.message}`),t}let W=null,w=0,V=3;if(E?.aborted){let t=new Error("Aborted via signal");throw t.name="AbortError",t}try{for await(let t of j){if(E?.aborted){let u=new Error("Aborted via signal");throw u.name="AbortError",u}if(G.push(t),t.type==="error"||t.error){let u=t.error?.message||t.error||t.message||"Unknown API error";try{i.error("SDK error event raw payload",{fullMessage:JSON.stringify(t,null,2)})}catch{}throw new Error(typeof u=="string"?u:JSON.stringify(u))}let h=JSON.stringify(t.message?.content||t.text||"").slice(0,200);if(h===W){if(w++,w>=V){let u=(t.message?.content?.[0]?.text||t.text||"unknown").slice(0,100);throw new Error(`API stuck in loop (${w}x repeated): ${u}`)}}else W=h,w=1;if(t.type==="assistant"||t.constructor?.name==="AssistantMessage"){let u=t.message?.content||t.content||[];for(let c of u)if(c.type==="thinking"&&c.thinking)console.log(`${c.thinking.substring(0,200)}${c.thinking.length>200?"...":""}`);else if(c.type==="text"&&c.text)L+=c.text,c.text.length<500?console.log(`${c.text}`):console.log(`${c.text.substring(0,200)}... (${c.text.length} chars)`);else if(c.type==="tool_use"){B++,c.name.includes("memory")?b.stepMemory(`Tool: ${c.name}`):b.stepTool(`Tool: ${c.name}`);let ee=JSON.stringify(c.input).substring(0,100);console.log(` Input: ${ee}${JSON.stringify(c.input).length>100?"...":""}`)}}else if(!(t.type==="user"&&t.tool_use_result)){if(t.type==="result"||t.constructor?.name==="ResultMessage"){let u=t.result||t.text||t.content||L;try{re({message:t,sessionPath:m,nodeName:N,model:g,toolCallCount:B})}catch(c){i.debug(`usage.json write failed: ${c.message}`)}if(o){if(t.structured_output){i.debug("Using SDK native structured_output");let Y=typeof o.parse=="function"?o.parse(t.structured_output):t.structured_output;return{raw:u,structured:Y}}if(u){let c=this._extractJson(u,o);if(c)return{raw:u,structured:c}}i.warn(`Could not extract structured output \u2014 returning raw text (${(u||"").length} chars)`)}return u||""}}}if(i.warn(`Agent SDK ended without result. Collected ${G.length} messages`),L.length>0)return i.debug("Returning accumulated text from messages"),L;throw new Error("Claude Agent SDK query ended without result")}catch(t){let h={message:t?.message,name:t?.name,code:t?.code,cause:t?.cause?.message||t?.cause,stack:t?.stack?.split(`
|
|
2
2
|
`).slice(0,5).join(`
|
|
3
|
-
`),stringified:(()=>{try{return JSON.stringify(t,Object.getOwnPropertyNames(Object(t)))}catch{return String(t)}})()};throw i.error(`Error during query stream: ${t?.message||t}`,h),t}}catch(p){throw i.error("Claude Agent SDK call failed",{message:p?.message,name:p?.name,code:p?.code,cause:p?.cause?.message||p?.cause}),p}}finally{for(let O of Q)O.dispose()}}_resolveSkills(e,
|
|
3
|
+
`),stringified:(()=>{try{return JSON.stringify(t,Object.getOwnPropertyNames(Object(t)))}catch{return String(t)}})()};throw i.error(`Error during query stream: ${t?.message||t}`,h),t}}catch(p){throw i.error("Claude Agent SDK call failed",{message:p?.message,name:p?.name,code:p?.code,cause:p?.cause?.message||p?.cause}),p}}finally{for(let O of Q)O.dispose()}}_resolveSkills(e,n){let r=["Read","Write","Edit","Bash","Grep","Glob"];if(e===null)return i.debug("No skills \u2014 pure LLM mode"),{allowedTools:[],mcpServers:{}};if(!Array.isArray(e)||e.length===0)return i.debug("Default IDE skills for code generation"),{allowedTools:[...r],mcpServers:{}};let s=[...r],o={};for(let a of e){let d=oe(a);if(!d){i.warn(`Unknown skill "${a}" \u2014 skipping`);continue}if(d.allowedTools&&s.push(...d.allowedTools),typeof d.resolve=="function"){let m=d.resolve(n);m&&(o[d.serverName]=m,i.debug(`MCP: ${d.serverName} \u2192 ${m.command} ${m.args[0]}`))}}return{allowedTools:s,mcpServers:o}}_extractJson(e,n){let r=[()=>{if(e.includes("===JSON_START===")){let s=e.indexOf("===JSON_START===")+16,o=e.indexOf("===JSON_END===");return e.substring(s,o).trim()}},()=>e.match(/```json\s*\n([\s\S]*?)\n```/)?.[1]?.trim(),()=>{if(!e.startsWith("{"))return e.match(/```\s*\n([\s\S]*?)\n```/)?.[1]?.trim()},()=>e.trim(),()=>{let s=e.indexOf("{"),o=e.lastIndexOf("}");if(s!==-1&&o>s)return e.substring(s,o+1)}];for(let s of r)try{let o=s();if(!o)continue;let a=JSON.parse(o);if(typeof a!="object"||a===null)continue;return typeof n.parse=="function"?n.parse(a):a}catch{}return null}};function re({message:l,sessionPath:e,nodeName:n,model:r,toolCallCount:s}){if(!e||!n)return;let o=l?.usage||l?.message?.usage||{},a=y(o.input_tokens,o.inputTokens),d=y(o.output_tokens,o.outputTokens),m=y(o.cache_read_input_tokens,o.cacheReadInputTokens),N=y(o.cache_creation_input_tokens,o.cacheCreationInputTokens),R=y(l?.duration_ms,l?.durationMs),M={model:r||null,input_tokens:a,output_tokens:d,cache_read_tokens:m,cache_creation_tokens:N,tool_call_count:typeof s=="number"?s:null,duration_ms:R,captured_at:new Date().toISOString()},E=A(e,n);try{P(E,{recursive:!0})}catch{}x(A(E,"usage.json"),JSON.stringify(M,null,2),"utf-8")}function y(...l){for(let e of l){if(typeof e=="number"&&Number.isFinite(e))return e;if(typeof e=="string"&&e.trim()!==""&&!isNaN(Number(e)))return Number(e)}return null}function ie(l,e){let n=A(z(),".claude"),r=A(n,".credentials.json"),s={claudeAiOauth:{accessToken:l,...e?{subscriptionType:String(e).toLowerCase()}:{}}};try{if(q(r)&&JSON.parse(X(r,"utf-8"))?.claudeAiOauth?.accessToken===l)return;P(n,{recursive:!0}),x(r,JSON.stringify(s,null,2),"utf-8"),i.debug(`Wrote Claude Code OAuth credentials to ${r}`)}catch(o){i.warn(`Could not write ${r}: ${o.message} \u2014 Claude SDK may fail to authenticate.`)}}function ce(l){let e=A(z(),".claude"),n=A(e,".credentials.json"),r;try{r=JSON.parse(l)}catch(s){i.warn(`CLAUDE_CODE_CREDENTIALS_JSON not valid JSON: ${s.message}`);return}if(!r?.claudeAiOauth?.accessToken){i.warn("CLAUDE_CODE_CREDENTIALS_JSON missing claudeAiOauth.accessToken \u2014 skipping write.");return}try{if(q(n)){let s=JSON.parse(X(n,"utf-8"));if(s?.claudeAiOauth?.accessToken===r.claudeAiOauth.accessToken&&s?.claudeAiOauth?.expiresAt===r.claudeAiOauth.expiresAt)return}P(e,{recursive:!0}),x(n,JSON.stringify(r,null,2),"utf-8"),i.debug(`Wrote full Claude Code credentials blob to ${n}`)}catch(s){i.warn(`Could not write ${n}: ${s.message} \u2014 Claude SDK may fail to authenticate.`)}}export{Z as ClaudeAgentStrategy};
|
package/dist/strategies/index.js
CHANGED
|
@@ -75,7 +75,7 @@ Stderr: ${g.slice(-1e3)}`:""}${m.trim()?`
|
|
|
75
75
|
Stdout (last 500 chars): ${m.slice(-500)}`:""}`));return}let d=w.getResult(),D=d?JSON.stringify(d,null,2):w.getRawText()||m||"";p({stdout:m||g||"",parsedText:D})}),O.on("error",u=>{if(C(),clearTimeout(K),clearInterval(v),b&&clearInterval(b),E&&clearTimeout(E),c&&!c.aborted)try{c.removeEventListener("abort",L)}catch{}f(new Error(`Cursor Agent spawn error: ${u.message}
|
|
76
76
|
Binary: ${e}
|
|
77
77
|
This usually means the binary is not in PATH. Try:
|
|
78
|
-
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.zshrc && source ~/.zshrc`))})})}};import{AgentStrategy as Ut,getSkill as Kt}from"@zibby/agent-workflow";import{query as Bt}from"@anthropic-ai/claude-agent-sdk";import{zodToJsonSchema as Jt}from"zod-to-json-schema";import{writeFileSync as Ne,mkdirSync as Re,existsSync as et,readFileSync as tt}from"fs";import{homedir as ot}from"os";import{join as ee}from"path";var he=class extends Ut{constructor(){super("claude","Claude (Anthropic API)",50)}canHandle(e){let t=!!process.env.ANTHROPIC_API_KEY||!!process.env.ANTHROPIC_API_KEY_POOL,r=!!process.env.CLAUDE_CODE_OAUTH_TOKEN||!!process.env.CLAUDE_CODE_OAUTH_TOKEN_POOL,o=!!process.env.ANTHROPIC_AUTH_TOKEN||!!process.env.ANTHROPIC_AUTH_TOKEN_POOL,n=t||r||o;return n||a.debug("ClaudeAgentStrategy: no credentials. Set one of ANTHROPIC_API_KEY / CLAUDE_CODE_OAUTH_TOKEN / ANTHROPIC_AUTH_TOKEN (or a *_POOL variant for multi-token rotation)."),n}async invoke(e,t={}){let{model:r,workspace:o=process.cwd(),schema:n=null,images:i=[],skills:l=null,sessionPath:c=null,nodeName:p=null,timeout:f,config:_={},signal:m=null}=t,g=r;(!g||g==="auto")&&(a.debug(`Model is '${g||"undefined"}', using default: ${B.CLAUDE}`),g=B.CLAUDE);let S=Ce[g]||g;Ce[g]&&g!==S&&a.debug(`Mapped model: ${g} \u2192 ${S}`);let N=[G("ANTHROPIC_API_KEY"),G("CLAUDE_CODE_OAUTH_TOKEN"),G("ANTHROPIC_AUTH_TOKEN")];try{a.debug(`Invoking Claude Agent SDK with model: ${S}, skills: ${JSON.stringify(l)}`);let h=process.env.CLAUDE_CODE_OAUTH_TOKEN,I=process.env.ANTHROPIC_API_KEY,T=process.env.ANTHROPIC_AUTH_TOKEN;process.env.CLAUDE_CODE_CREDENTIALS_JSON?Ft(process.env.CLAUDE_CODE_CREDENTIALS_JSON):h&&jt(h,process.env.__ZIBBY_CLAUDE_PLAN);let $=process.env.__ZIBBY_CLAUDE_PLAN,y=$?$.charAt(0).toUpperCase()+$.slice(1):null,M;h?M=y?`Claude ${y} plan \xB7 ***${h.slice(-4)}`:`Subscription \xB7 ***${h.slice(-4)}`:I?M=`API key \xB7 ***${I.slice(-4)}`:T?M=`Bearer \xB7 ***${T.slice(-4)}`:M="NONE \u2014 request will fail";let C=(await import("chalk")).default;console.log(""),console.log(` ${C.cyan("\u25C6")} ${C.bold(S)}`),console.log(` ${C.dim("Auth: ")}${M}`),console.log(` ${C.dim("Workdir: ")}${o}`),console.log(""),console.log(C.bold("Prompt sent to LLM:")),console.log(C.dim("\u2500".repeat(60))),console.log(C.dim(e)),console.log(C.dim("\u2500".repeat(60)));let{allowedTools:k,mcpServers:O}=this._resolveSkills(l,{sessionPath:c,workspace:o,nodeName:p});try{let E={cwd:o,allowedTools:k,
|
|
78
|
+
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.zshrc && source ~/.zshrc`))})})}};import{AgentStrategy as Ut,getSkill as Kt}from"@zibby/agent-workflow";import{query as Bt}from"@anthropic-ai/claude-agent-sdk";import{zodToJsonSchema as Jt}from"zod-to-json-schema";import{writeFileSync as Ne,mkdirSync as Re,existsSync as et,readFileSync as tt}from"fs";import{homedir as ot}from"os";import{join as ee}from"path";var he=class extends Ut{constructor(){super("claude","Claude (Anthropic API)",50)}canHandle(e){let t=!!process.env.ANTHROPIC_API_KEY||!!process.env.ANTHROPIC_API_KEY_POOL,r=!!process.env.CLAUDE_CODE_OAUTH_TOKEN||!!process.env.CLAUDE_CODE_OAUTH_TOKEN_POOL,o=!!process.env.ANTHROPIC_AUTH_TOKEN||!!process.env.ANTHROPIC_AUTH_TOKEN_POOL,n=t||r||o;return n||a.debug("ClaudeAgentStrategy: no credentials. Set one of ANTHROPIC_API_KEY / CLAUDE_CODE_OAUTH_TOKEN / ANTHROPIC_AUTH_TOKEN (or a *_POOL variant for multi-token rotation)."),n}async invoke(e,t={}){let{model:r,workspace:o=process.cwd(),schema:n=null,images:i=[],skills:l=null,sessionPath:c=null,nodeName:p=null,timeout:f,config:_={},signal:m=null}=t,g=r;(!g||g==="auto")&&(a.debug(`Model is '${g||"undefined"}', using default: ${B.CLAUDE}`),g=B.CLAUDE);let S=Ce[g]||g;Ce[g]&&g!==S&&a.debug(`Mapped model: ${g} \u2192 ${S}`);let N=[G("ANTHROPIC_API_KEY"),G("CLAUDE_CODE_OAUTH_TOKEN"),G("ANTHROPIC_AUTH_TOKEN")];try{a.debug(`Invoking Claude Agent SDK with model: ${S}, skills: ${JSON.stringify(l)}`);let h=process.env.CLAUDE_CODE_OAUTH_TOKEN,I=process.env.ANTHROPIC_API_KEY,T=process.env.ANTHROPIC_AUTH_TOKEN;process.env.CLAUDE_CODE_CREDENTIALS_JSON?Ft(process.env.CLAUDE_CODE_CREDENTIALS_JSON):h&&jt(h,process.env.__ZIBBY_CLAUDE_PLAN);let $=process.env.__ZIBBY_CLAUDE_PLAN,y=$?$.charAt(0).toUpperCase()+$.slice(1):null,M;h?M=y?`Claude ${y} plan \xB7 ***${h.slice(-4)}`:`Subscription \xB7 ***${h.slice(-4)}`:I?M=`API key \xB7 ***${I.slice(-4)}`:T?M=`Bearer \xB7 ***${T.slice(-4)}`:M="NONE \u2014 request will fail";let C=(await import("chalk")).default;console.log(""),console.log(` ${C.cyan("\u25C6")} ${C.bold(S)}`),console.log(` ${C.dim("Auth: ")}${M}`),console.log(` ${C.dim("Workdir: ")}${o}`),console.log(""),console.log(C.bold("Prompt sent to LLM:")),console.log(C.dim("\u2500".repeat(60))),console.log(C.dim(e)),console.log(C.dim("\u2500".repeat(60)));let{allowedTools:k,mcpServers:O}=this._resolveSkills(l,{sessionPath:c,workspace:o,nodeName:p});try{let E={cwd:o,allowedTools:k,settings:{permissions:{allow:["Bash(*)","Read(*)","Write(*)","Edit(*)","MultiEdit(*)","NotebookEdit(*)","Grep(*)","Glob(*)","WebFetch(*)","WebSearch(*)","Task(*)","TodoWrite(*)","Skill(*)"]}},model:S,...Object.keys(O).length>0&&{mcpServers:O}};if(n){let x=typeof n.parse=="function"?Jt(n,{target:"openApi3"}):n;E.outputFormat={type:"json_schema",schema:x},a.debug("Structured output enforced via SDK outputFormat")}a.debug(`Agent SDK options: ${JSON.stringify({cwd:E.cwd,toolCount:k.length,permissionMode:E.permissionMode,model:E.model,hasOutputFormat:!!E.outputFormat})}`);let L="",b=0,P=[];a.debug("Starting Claude Agent SDK query stream");let U;try{U=Bt({prompt:e,options:E})}catch(u){throw a.error(`Failed to initialize Claude Agent SDK: ${u.message}`),u}let v=null,K=0,w=3;if(m?.aborted){let u=new Error("Aborted via signal");throw u.name="AbortError",u}try{for await(let u of U){if(m?.aborted){let A=new Error("Aborted via signal");throw A.name="AbortError",A}if(P.push(u),u.type==="error"||u.error){let A=u.error?.message||u.error||u.message||"Unknown API error";try{a.error("SDK error event raw payload",{fullMessage:JSON.stringify(u,null,2)})}catch{}throw new Error(typeof A=="string"?A:JSON.stringify(A))}let x=JSON.stringify(u.message?.content||u.text||"").slice(0,200);if(x===v){if(K++,K>=w){let A=(u.message?.content?.[0]?.text||u.text||"unknown").slice(0,100);throw new Error(`API stuck in loop (${K}x repeated): ${A}`)}}else v=x,K=1;if(u.type==="assistant"||u.constructor?.name==="AssistantMessage"){let A=u.message?.content||u.content||[];for(let d of A)if(d.type==="thinking"&&d.thinking)console.log(`${d.thinking.substring(0,200)}${d.thinking.length>200?"...":""}`);else if(d.type==="text"&&d.text)L+=d.text,d.text.length<500?console.log(`${d.text}`):console.log(`${d.text.substring(0,200)}... (${d.text.length} chars)`);else if(d.type==="tool_use"){b++,d.name.includes("memory")?Y.stepMemory(`Tool: ${d.name}`):Y.stepTool(`Tool: ${d.name}`);let R=JSON.stringify(d.input).substring(0,100);console.log(` Input: ${R}${JSON.stringify(d.input).length>100?"...":""}`)}}else if(!(u.type==="user"&&u.tool_use_result)){if(u.type==="result"||u.constructor?.name==="ResultMessage"){let A=u.result||u.text||u.content||L;try{Gt({message:u,sessionPath:c,nodeName:p,model:g,toolCallCount:b})}catch(d){a.debug(`usage.json write failed: ${d.message}`)}if(n){if(u.structured_output){a.debug("Using SDK native structured_output");let D=typeof n.parse=="function"?n.parse(u.structured_output):u.structured_output;return{raw:A,structured:D}}if(A){let d=this._extractJson(A,n);if(d)return{raw:A,structured:d}}a.warn(`Could not extract structured output \u2014 returning raw text (${(A||"").length} chars)`)}return A||""}}}if(a.warn(`Agent SDK ended without result. Collected ${P.length} messages`),L.length>0)return a.debug("Returning accumulated text from messages"),L;throw new Error("Claude Agent SDK query ended without result")}catch(u){let x={message:u?.message,name:u?.name,code:u?.code,cause:u?.cause?.message||u?.cause,stack:u?.stack?.split(`
|
|
79
79
|
`).slice(0,5).join(`
|
|
80
80
|
`),stringified:(()=>{try{return JSON.stringify(u,Object.getOwnPropertyNames(Object(u)))}catch{return String(u)}})()};throw a.error(`Error during query stream: ${u?.message||u}`,x),u}}catch(E){throw a.error("Claude Agent SDK call failed",{message:E?.message,name:E?.name,code:E?.code,cause:E?.cause?.message||E?.cause}),E}}finally{for(let h of N)h.dispose()}}_resolveSkills(e,t){let r=["Read","Write","Edit","Bash","Grep","Glob"];if(e===null)return a.debug("No skills \u2014 pure LLM mode"),{allowedTools:[],mcpServers:{}};if(!Array.isArray(e)||e.length===0)return a.debug("Default IDE skills for code generation"),{allowedTools:[...r],mcpServers:{}};let o=[...r],n={};for(let i of e){let l=Kt(i);if(!l){a.warn(`Unknown skill "${i}" \u2014 skipping`);continue}if(l.allowedTools&&o.push(...l.allowedTools),typeof l.resolve=="function"){let c=l.resolve(t);c&&(n[l.serverName]=c,a.debug(`MCP: ${l.serverName} \u2192 ${c.command} ${c.args[0]}`))}}return{allowedTools:o,mcpServers:n}}_extractJson(e,t){let r=[()=>{if(e.includes("===JSON_START===")){let o=e.indexOf("===JSON_START===")+16,n=e.indexOf("===JSON_END===");return e.substring(o,n).trim()}},()=>e.match(/```json\s*\n([\s\S]*?)\n```/)?.[1]?.trim(),()=>{if(!e.startsWith("{"))return e.match(/```\s*\n([\s\S]*?)\n```/)?.[1]?.trim()},()=>e.trim(),()=>{let o=e.indexOf("{"),n=e.lastIndexOf("}");if(o!==-1&&n>o)return e.substring(o,n+1)}];for(let o of r)try{let n=o();if(!n)continue;let i=JSON.parse(n);if(typeof i!="object"||i===null)continue;return typeof t.parse=="function"?t.parse(i):i}catch{}return null}};function Gt({message:s,sessionPath:e,nodeName:t,model:r,toolCallCount:o}){if(!e||!t)return;let n=s?.usage||s?.message?.usage||{},i=ce(n.input_tokens,n.inputTokens),l=ce(n.output_tokens,n.outputTokens),c=ce(n.cache_read_input_tokens,n.cacheReadInputTokens),p=ce(n.cache_creation_input_tokens,n.cacheCreationInputTokens),f=ce(s?.duration_ms,s?.durationMs),_={model:r||null,input_tokens:i,output_tokens:l,cache_read_tokens:c,cache_creation_tokens:p,tool_call_count:typeof o=="number"?o:null,duration_ms:f,captured_at:new Date().toISOString()},m=ee(e,t);try{Re(m,{recursive:!0})}catch{}Ne(ee(m,"usage.json"),JSON.stringify(_,null,2),"utf-8")}function ce(...s){for(let e of s){if(typeof e=="number"&&Number.isFinite(e))return e;if(typeof e=="string"&&e.trim()!==""&&!isNaN(Number(e)))return Number(e)}return null}function jt(s,e){let t=ee(ot(),".claude"),r=ee(t,".credentials.json"),o={claudeAiOauth:{accessToken:s,...e?{subscriptionType:String(e).toLowerCase()}:{}}};try{if(et(r)&&JSON.parse(tt(r,"utf-8"))?.claudeAiOauth?.accessToken===s)return;Re(t,{recursive:!0}),Ne(r,JSON.stringify(o,null,2),"utf-8"),a.debug(`Wrote Claude Code OAuth credentials to ${r}`)}catch(n){a.warn(`Could not write ${r}: ${n.message} \u2014 Claude SDK may fail to authenticate.`)}}function Ft(s){let e=ee(ot(),".claude"),t=ee(e,".credentials.json"),r;try{r=JSON.parse(s)}catch(o){a.warn(`CLAUDE_CODE_CREDENTIALS_JSON not valid JSON: ${o.message}`);return}if(!r?.claudeAiOauth?.accessToken){a.warn("CLAUDE_CODE_CREDENTIALS_JSON missing claudeAiOauth.accessToken \u2014 skipping write.");return}try{if(et(t)){let o=JSON.parse(tt(t,"utf-8"));if(o?.claudeAiOauth?.accessToken===r.claudeAiOauth.accessToken&&o?.claudeAiOauth?.expiresAt===r.claudeAiOauth.expiresAt)return}Re(e,{recursive:!0}),Ne(t,JSON.stringify(r,null,2),"utf-8"),a.debug(`Wrote full Claude Code credentials blob to ${t}`)}catch(o){a.warn(`Could not write ${t}: ${o.message} \u2014 Claude SDK may fail to authenticate.`)}}import{AgentStrategy as Yt,getSkill as zt}from"@zibby/agent-workflow";import{execSync as Ht}from"node:child_process";import{zodToJsonSchema as Xt}from"zod-to-json-schema";var ye=class extends Yt{constructor(){super("codex","Codex (OpenAI)",75)}canHandle(e){if(!!!(process.env.OPENAI_API_KEY||process.env.OPENAI_API_KEY_POOL||process.env.CODEX_API_KEY||process.env.CODEX_API_KEY_POOL))return a.debug("CodexAgentStrategy: OPENAI_API_KEY or CODEX_API_KEY not set"),!1;try{return Ht("codex --version",{encoding:"utf-8",timeout:5e3,stdio:"pipe"}),!0}catch{return a.warn("[Codex] codex CLI not found. Install: npm install -g @openai/codex"),!1}}async invoke(e,t={}){let{model:r,workspace:o=process.cwd(),schema:n=null,skills:i=null,sessionPath:l=null,nodeName:c=null,timeout:p,config:f={},signal:_=null}=t,m=[G("OPENAI_API_KEY"),G("CODEX_API_KEY")];try{let{Codex:g}=await import("@openai/codex-sdk"),S=r;(!S||S==="auto")&&(a.debug(`Model is '${S||"undefined"}', using default: ${B.CODEX}`),S=B.CODEX);let N=ke[S]||S;ke[S]&&S!==N&&a.debug(`Mapped model: ${S} \u2192 ${N}`),a.debug(`Invoking Codex SDK with model: ${N}, skills: ${JSON.stringify(i)}`);let h=process.env.CODEX_API_KEY||process.env.OPENAI_API_KEY;h&&!process.env.CODEX_API_KEY&&(process.env.CODEX_API_KEY=h);let I=h?` | key: ***${h.slice(-4)}`:" | key: not set";console.log(`
|
|
81
81
|
\u25C6 Model: ${N}${I}
|
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
import { WorkflowAgent, WorkflowGraph } from '@zibby/core';
|
|
9
9
|
import {
|
|
10
10
|
preflightNode,
|
|
11
|
+
cacheReplayNode,
|
|
11
12
|
executeLiveNode,
|
|
12
13
|
generateScriptNode,
|
|
13
14
|
} from './nodes/index.mjs';
|
|
@@ -20,9 +21,10 @@ export class BrowserTestAutomationAgent extends WorkflowAgent {
|
|
|
20
21
|
graph.setStateSchema(browserTestAutomationStateSchema);
|
|
21
22
|
|
|
22
23
|
graph.addNode('preflight', preflightNode);
|
|
24
|
+
graph.addNode('cache_replay', cacheReplayNode);
|
|
23
25
|
graph.addNode('execute_live', executeLiveNode);
|
|
24
26
|
graph.addNode('generate_script', generateScriptNode);
|
|
25
|
-
|
|
27
|
+
|
|
26
28
|
graph.setEntryPoint('preflight');
|
|
27
29
|
|
|
28
30
|
// Short-circuit when preflight produced nothing usable. Triggered when:
|
|
@@ -35,7 +37,17 @@ export class BrowserTestAutomationAgent extends WorkflowAgent {
|
|
|
35
37
|
// ~30s before failing — bad UX and bad bill.
|
|
36
38
|
graph.addConditionalEdges('preflight', (state) => {
|
|
37
39
|
const assertions = state.preflight?.assertions || [];
|
|
38
|
-
return assertions.length > 0 ? '
|
|
40
|
+
return assertions.length > 0 ? 'cache_replay' : 'END';
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
// Lever-#2 fork: cache_replay attempted a Playwright-only replay of
|
|
44
|
+
// a prior successful action sequence. On hit it side-wrote
|
|
45
|
+
// state.execute_live with synthesized output, so we can skip
|
|
46
|
+
// execute_live and jump straight to generate_script — zero LLM
|
|
47
|
+
// tokens. On miss / replay failure / cold cache, fall through to
|
|
48
|
+
// the normal LLM-driven execute_live path.
|
|
49
|
+
graph.addConditionalEdges('cache_replay', (state) => {
|
|
50
|
+
return state.cache_replay?.hit === true ? 'generate_script' : 'execute_live';
|
|
39
51
|
});
|
|
40
52
|
|
|
41
53
|
graph.addConditionalEdges('execute_live', (state) => {
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* cache_replay node — lever-#2 read path inside the workflow.
|
|
3
|
+
*
|
|
4
|
+
* Sits between `preflight` and `execute_live` in the graph. Tries to
|
|
5
|
+
* replay a prior successful run's action sequence via Playwright
|
|
6
|
+
* directly, completely skipping the LLM. On a clean cache hit it
|
|
7
|
+
* populates `state.execute_live` with the result so downstream
|
|
8
|
+
* `generate_script` works exactly as if execute_live had run.
|
|
9
|
+
*
|
|
10
|
+
* Conditional edge after this node:
|
|
11
|
+
* - state.cache_replay.hit === true → skip execute_live → generate_script
|
|
12
|
+
* - state.cache_replay.hit === false → execute_live (LLM-driven path)
|
|
13
|
+
*
|
|
14
|
+
* Not user-configurable per-spec — the cache key derivation handles
|
|
15
|
+
* staleness (page fingerprint drift invalidates) and replay failures
|
|
16
|
+
* fall through cleanly to the LLM path.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { z } from '@zibby/core';
|
|
20
|
+
import { chromium } from 'playwright';
|
|
21
|
+
import { spawn } from 'child_process';
|
|
22
|
+
import { extractDomain, replayActions } from '@zibby/ui-memory';
|
|
23
|
+
import { join } from 'path';
|
|
24
|
+
|
|
25
|
+
const REPLAY_TIMEOUT_MS = 60_000;
|
|
26
|
+
|
|
27
|
+
export const cacheReplayNode = {
|
|
28
|
+
name: 'cache_replay',
|
|
29
|
+
skills: [],
|
|
30
|
+
timeout: 90000,
|
|
31
|
+
outputSchema: z.object({
|
|
32
|
+
hit: z.boolean(),
|
|
33
|
+
elapsed_ms: z.number().nullish(),
|
|
34
|
+
executed: z.number().nullish(),
|
|
35
|
+
total: z.number().nullish(),
|
|
36
|
+
cache_key: z.string().nullish(),
|
|
37
|
+
error: z.string().nullish(),
|
|
38
|
+
// When hit, we also write a synthesized execute_live block so the
|
|
39
|
+
// downstream generate_script node sees what it expects.
|
|
40
|
+
execute_live_synthesized: z.boolean().nullish(),
|
|
41
|
+
}),
|
|
42
|
+
|
|
43
|
+
execute: async (context) => {
|
|
44
|
+
// graph.js builds nodeContext as `{ state, invokeAgent, _coreInvokeAgent,
|
|
45
|
+
// ...state.getAll() }`. So `context.testSpec` works (spread) AND
|
|
46
|
+
// `context.state.get('testSpec')` works (instance). Reading from the
|
|
47
|
+
// spread is the natural shape — `context.state` is reserved for the
|
|
48
|
+
// .set(key, value) side-write below.
|
|
49
|
+
const cwd = context.cwd || context.workspace || process.cwd();
|
|
50
|
+
const testSpec = context.testSpec || '';
|
|
51
|
+
const specPath = context.specPath || '';
|
|
52
|
+
|
|
53
|
+
// Derive domain from the spec text (no DOM access yet — pure parse).
|
|
54
|
+
const domain = extractDomainFromSpec(testSpec);
|
|
55
|
+
if (!domain) {
|
|
56
|
+
return { hit: false, error: 'cannot derive domain from spec' };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Cache key requires page_fingerprint, which is page-state-dependent
|
|
60
|
+
// and only available AFTER navigation. We compute a key WITHOUT
|
|
61
|
+
// fingerprint first and look up by (domain, spec_path) prefix —
|
|
62
|
+
// the persister wrote spec_path too. If we find a candidate, we
|
|
63
|
+
// use its stored fingerprint to compute the full key and verify.
|
|
64
|
+
//
|
|
65
|
+
// Lookup order:
|
|
66
|
+
// 1. Exact (domain, spec_path) match in action_cache.
|
|
67
|
+
// 2. If found, use its actions for replay attempt.
|
|
68
|
+
// 3. On replay success: signal hit, populate state.execute_live.
|
|
69
|
+
// 4. On replay failure (or cache miss): hit=false, fall back to LLM.
|
|
70
|
+
const cached = await lookupCacheByDomainAndSpec({ cwd, domain, specPath });
|
|
71
|
+
if (!cached) {
|
|
72
|
+
return { hit: false, error: 'no cached actions for this spec' };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Run the replay in a freshly-launched Playwright browser. Cleanly
|
|
76
|
+
// independent from the @zibby/mcp-browser path execute_live uses.
|
|
77
|
+
const t0 = Date.now();
|
|
78
|
+
const browser = await chromium.launch({ headless: true });
|
|
79
|
+
const page = await browser.newPage();
|
|
80
|
+
let replayResult;
|
|
81
|
+
try {
|
|
82
|
+
replayResult = await Promise.race([
|
|
83
|
+
replayActions({
|
|
84
|
+
actions: cached.actions,
|
|
85
|
+
page,
|
|
86
|
+
log: (m) => console.log(`[cache_replay] ${m}`),
|
|
87
|
+
}),
|
|
88
|
+
new Promise((_, reject) =>
|
|
89
|
+
setTimeout(() => reject(new Error('replay timeout')), REPLAY_TIMEOUT_MS),
|
|
90
|
+
),
|
|
91
|
+
]);
|
|
92
|
+
} catch (err) {
|
|
93
|
+
replayResult = { success: false, error: err.message, executed: 0, total: cached.actions.length };
|
|
94
|
+
}
|
|
95
|
+
const finalUrl = page.url();
|
|
96
|
+
await browser.close().catch(() => {});
|
|
97
|
+
const elapsedMs = Date.now() - t0;
|
|
98
|
+
|
|
99
|
+
if (!replayResult.success) {
|
|
100
|
+
// Increment failure_count so we can drop chronic misses later.
|
|
101
|
+
await incrementCacheFailure({ cwd, cacheKey: cached.cache_key });
|
|
102
|
+
return {
|
|
103
|
+
hit: false,
|
|
104
|
+
elapsed_ms: elapsedMs,
|
|
105
|
+
executed: replayResult.executed,
|
|
106
|
+
total: replayResult.total,
|
|
107
|
+
cache_key: cached.cache_key,
|
|
108
|
+
error: replayResult.error,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// HIT path. Side-write the synthesized execute_live output via
|
|
113
|
+
// context.state.set so downstream generate_script reads the same
|
|
114
|
+
// shape it expects (actions[], finalUrl, …). The customExecute
|
|
115
|
+
// return-value lands in state.cache_replay; the execute_live slot
|
|
116
|
+
// has to be populated separately.
|
|
117
|
+
if (typeof context.state?.set === 'function') {
|
|
118
|
+
context.state.set('execute_live', {
|
|
119
|
+
success: true,
|
|
120
|
+
steps: cached.actions.map((a) => a.description),
|
|
121
|
+
actions: cached.actions,
|
|
122
|
+
assertions: [],
|
|
123
|
+
finalUrl,
|
|
124
|
+
browserClosed: true,
|
|
125
|
+
notes: 'cache_replay hit — actions replayed via Playwright, no LLM',
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return {
|
|
130
|
+
hit: true,
|
|
131
|
+
elapsed_ms: elapsedMs,
|
|
132
|
+
executed: replayResult.executed,
|
|
133
|
+
total: replayResult.total,
|
|
134
|
+
cache_key: cached.cache_key,
|
|
135
|
+
execute_live_synthesized: true,
|
|
136
|
+
};
|
|
137
|
+
},
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
// ─── helpers ────────────────────────────────────────────────────────────
|
|
141
|
+
|
|
142
|
+
function extractDomainFromSpec(spec) {
|
|
143
|
+
if (!spec) return null;
|
|
144
|
+
// Find the first http(s) URL in the spec and run it through the
|
|
145
|
+
// SAME `extractDomain` the persister uses, so the cache-key lookup
|
|
146
|
+
// matches what was actually written (notably: `www.` is stripped).
|
|
147
|
+
const m = String(spec).match(/https?:\/\/[^\s"'<>]+/);
|
|
148
|
+
if (!m) return null;
|
|
149
|
+
return extractDomain(m[0]);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Find a cached row by (domain, spec_path). Picks the row with
|
|
154
|
+
* highest success_count if multiple match.
|
|
155
|
+
* Uses dolt via subprocess (matching the rest of the codebase's
|
|
156
|
+
* Dolt-access pattern).
|
|
157
|
+
*/
|
|
158
|
+
async function lookupCacheByDomainAndSpec({ cwd, domain, specPath }) {
|
|
159
|
+
const dbDir = join(cwd, '.zibby', 'memory');
|
|
160
|
+
const safeDomain = escapeSql(domain);
|
|
161
|
+
const safeSpec = escapeSql(specPath);
|
|
162
|
+
const sql = `SELECT cache_key, actions_json, page_fingerprint
|
|
163
|
+
FROM action_cache
|
|
164
|
+
WHERE domain = ${safeDomain} AND spec_path = ${safeSpec}
|
|
165
|
+
ORDER BY success_count DESC, last_used_at DESC
|
|
166
|
+
LIMIT 1`;
|
|
167
|
+
const rows = await runDoltJson(dbDir, sql);
|
|
168
|
+
if (!rows || rows.length === 0) return null;
|
|
169
|
+
try {
|
|
170
|
+
const actions = JSON.parse(rows[0].actions_json);
|
|
171
|
+
return { cache_key: rows[0].cache_key, actions, fingerprint: rows[0].page_fingerprint };
|
|
172
|
+
} catch {
|
|
173
|
+
return null;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
async function incrementCacheFailure({ cwd, cacheKey }) {
|
|
178
|
+
const dbDir = join(cwd, '.zibby', 'memory');
|
|
179
|
+
const sql = `UPDATE action_cache
|
|
180
|
+
SET failure_count = failure_count + 1, last_replay_status = 'replay-failed'
|
|
181
|
+
WHERE cache_key = ${escapeSql(cacheKey)}`;
|
|
182
|
+
await runDoltExec(dbDir, sql).catch(() => { /* non-fatal */ });
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function escapeSql(v) {
|
|
186
|
+
if (v == null) return 'NULL';
|
|
187
|
+
return `'${String(v).replace(/'/g, "''")}'`;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function runDoltJson(dir, sql) {
|
|
191
|
+
return new Promise((resolve) => {
|
|
192
|
+
const child = spawn('dolt', ['sql', '-r', 'json', '-q', sql], { cwd: dir });
|
|
193
|
+
let out = '';
|
|
194
|
+
child.stdout.on('data', (d) => { out += d; });
|
|
195
|
+
child.on('close', () => {
|
|
196
|
+
try {
|
|
197
|
+
const parsed = JSON.parse(out);
|
|
198
|
+
resolve(parsed.rows || []);
|
|
199
|
+
} catch {
|
|
200
|
+
resolve([]);
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
child.on('error', () => resolve([]));
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function runDoltExec(dir, sql) {
|
|
208
|
+
return new Promise((resolve, reject) => {
|
|
209
|
+
const child = spawn('dolt', ['sql', '-q', sql], { cwd: dir });
|
|
210
|
+
child.on('close', (code) => (code === 0 ? resolve() : reject(new Error(`dolt exit ${code}`))));
|
|
211
|
+
child.on('error', reject);
|
|
212
|
+
});
|
|
213
|
+
}
|
package/package.json
CHANGED
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
import { WorkflowAgent, WorkflowGraph } from '@zibby/core';
|
|
9
9
|
import {
|
|
10
10
|
preflightNode,
|
|
11
|
+
cacheReplayNode,
|
|
11
12
|
executeLiveNode,
|
|
12
13
|
generateScriptNode,
|
|
13
14
|
} from './nodes/index.mjs';
|
|
@@ -20,9 +21,10 @@ export class BrowserTestAutomationAgent extends WorkflowAgent {
|
|
|
20
21
|
graph.setStateSchema(browserTestAutomationStateSchema);
|
|
21
22
|
|
|
22
23
|
graph.addNode('preflight', preflightNode);
|
|
24
|
+
graph.addNode('cache_replay', cacheReplayNode);
|
|
23
25
|
graph.addNode('execute_live', executeLiveNode);
|
|
24
26
|
graph.addNode('generate_script', generateScriptNode);
|
|
25
|
-
|
|
27
|
+
|
|
26
28
|
graph.setEntryPoint('preflight');
|
|
27
29
|
|
|
28
30
|
// Short-circuit when preflight produced nothing usable. Triggered when:
|
|
@@ -35,7 +37,17 @@ export class BrowserTestAutomationAgent extends WorkflowAgent {
|
|
|
35
37
|
// ~30s before failing — bad UX and bad bill.
|
|
36
38
|
graph.addConditionalEdges('preflight', (state) => {
|
|
37
39
|
const assertions = state.preflight?.assertions || [];
|
|
38
|
-
return assertions.length > 0 ? '
|
|
40
|
+
return assertions.length > 0 ? 'cache_replay' : 'END';
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
// Lever-#2 fork: cache_replay attempted a Playwright-only replay of
|
|
44
|
+
// a prior successful action sequence. On hit it side-wrote
|
|
45
|
+
// state.execute_live with synthesized output, so we can skip
|
|
46
|
+
// execute_live and jump straight to generate_script — zero LLM
|
|
47
|
+
// tokens. On miss / replay failure / cold cache, fall through to
|
|
48
|
+
// the normal LLM-driven execute_live path.
|
|
49
|
+
graph.addConditionalEdges('cache_replay', (state) => {
|
|
50
|
+
return state.cache_replay?.hit === true ? 'generate_script' : 'execute_live';
|
|
39
51
|
});
|
|
40
52
|
|
|
41
53
|
graph.addConditionalEdges('execute_live', (state) => {
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* cache_replay node — lever-#2 read path inside the workflow.
|
|
3
|
+
*
|
|
4
|
+
* Sits between `preflight` and `execute_live` in the graph. Tries to
|
|
5
|
+
* replay a prior successful run's action sequence via Playwright
|
|
6
|
+
* directly, completely skipping the LLM. On a clean cache hit it
|
|
7
|
+
* populates `state.execute_live` with the result so downstream
|
|
8
|
+
* `generate_script` works exactly as if execute_live had run.
|
|
9
|
+
*
|
|
10
|
+
* Conditional edge after this node:
|
|
11
|
+
* - state.cache_replay.hit === true → skip execute_live → generate_script
|
|
12
|
+
* - state.cache_replay.hit === false → execute_live (LLM-driven path)
|
|
13
|
+
*
|
|
14
|
+
* Not user-configurable per-spec — the cache key derivation handles
|
|
15
|
+
* staleness (page fingerprint drift invalidates) and replay failures
|
|
16
|
+
* fall through cleanly to the LLM path.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { z } from '@zibby/core';
|
|
20
|
+
import { chromium } from 'playwright';
|
|
21
|
+
import { spawn } from 'child_process';
|
|
22
|
+
import { extractDomain, replayActions } from '@zibby/ui-memory';
|
|
23
|
+
import { join } from 'path';
|
|
24
|
+
|
|
25
|
+
const REPLAY_TIMEOUT_MS = 60_000;
|
|
26
|
+
|
|
27
|
+
export const cacheReplayNode = {
|
|
28
|
+
name: 'cache_replay',
|
|
29
|
+
skills: [],
|
|
30
|
+
timeout: 90000,
|
|
31
|
+
outputSchema: z.object({
|
|
32
|
+
hit: z.boolean(),
|
|
33
|
+
elapsed_ms: z.number().nullish(),
|
|
34
|
+
executed: z.number().nullish(),
|
|
35
|
+
total: z.number().nullish(),
|
|
36
|
+
cache_key: z.string().nullish(),
|
|
37
|
+
error: z.string().nullish(),
|
|
38
|
+
// When hit, we also write a synthesized execute_live block so the
|
|
39
|
+
// downstream generate_script node sees what it expects.
|
|
40
|
+
execute_live_synthesized: z.boolean().nullish(),
|
|
41
|
+
}),
|
|
42
|
+
|
|
43
|
+
execute: async (context) => {
|
|
44
|
+
// graph.js builds nodeContext as `{ state, invokeAgent, _coreInvokeAgent,
|
|
45
|
+
// ...state.getAll() }`. So `context.testSpec` works (spread) AND
|
|
46
|
+
// `context.state.get('testSpec')` works (instance). Reading from the
|
|
47
|
+
// spread is the natural shape — `context.state` is reserved for the
|
|
48
|
+
// .set(key, value) side-write below.
|
|
49
|
+
const cwd = context.cwd || context.workspace || process.cwd();
|
|
50
|
+
const testSpec = context.testSpec || '';
|
|
51
|
+
const specPath = context.specPath || '';
|
|
52
|
+
|
|
53
|
+
// Derive domain from the spec text (no DOM access yet — pure parse).
|
|
54
|
+
const domain = extractDomainFromSpec(testSpec);
|
|
55
|
+
if (!domain) {
|
|
56
|
+
return { hit: false, error: 'cannot derive domain from spec' };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Cache key requires page_fingerprint, which is page-state-dependent
|
|
60
|
+
// and only available AFTER navigation. We compute a key WITHOUT
|
|
61
|
+
// fingerprint first and look up by (domain, spec_path) prefix —
|
|
62
|
+
// the persister wrote spec_path too. If we find a candidate, we
|
|
63
|
+
// use its stored fingerprint to compute the full key and verify.
|
|
64
|
+
//
|
|
65
|
+
// Lookup order:
|
|
66
|
+
// 1. Exact (domain, spec_path) match in action_cache.
|
|
67
|
+
// 2. If found, use its actions for replay attempt.
|
|
68
|
+
// 3. On replay success: signal hit, populate state.execute_live.
|
|
69
|
+
// 4. On replay failure (or cache miss): hit=false, fall back to LLM.
|
|
70
|
+
const cached = await lookupCacheByDomainAndSpec({ cwd, domain, specPath });
|
|
71
|
+
if (!cached) {
|
|
72
|
+
return { hit: false, error: 'no cached actions for this spec' };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Run the replay in a freshly-launched Playwright browser. Cleanly
|
|
76
|
+
// independent from the @zibby/mcp-browser path execute_live uses.
|
|
77
|
+
const t0 = Date.now();
|
|
78
|
+
const browser = await chromium.launch({ headless: true });
|
|
79
|
+
const page = await browser.newPage();
|
|
80
|
+
let replayResult;
|
|
81
|
+
try {
|
|
82
|
+
replayResult = await Promise.race([
|
|
83
|
+
replayActions({
|
|
84
|
+
actions: cached.actions,
|
|
85
|
+
page,
|
|
86
|
+
log: (m) => console.log(`[cache_replay] ${m}`),
|
|
87
|
+
}),
|
|
88
|
+
new Promise((_, reject) =>
|
|
89
|
+
setTimeout(() => reject(new Error('replay timeout')), REPLAY_TIMEOUT_MS),
|
|
90
|
+
),
|
|
91
|
+
]);
|
|
92
|
+
} catch (err) {
|
|
93
|
+
replayResult = { success: false, error: err.message, executed: 0, total: cached.actions.length };
|
|
94
|
+
}
|
|
95
|
+
const finalUrl = page.url();
|
|
96
|
+
await browser.close().catch(() => {});
|
|
97
|
+
const elapsedMs = Date.now() - t0;
|
|
98
|
+
|
|
99
|
+
if (!replayResult.success) {
|
|
100
|
+
// Increment failure_count so we can drop chronic misses later.
|
|
101
|
+
await incrementCacheFailure({ cwd, cacheKey: cached.cache_key });
|
|
102
|
+
return {
|
|
103
|
+
hit: false,
|
|
104
|
+
elapsed_ms: elapsedMs,
|
|
105
|
+
executed: replayResult.executed,
|
|
106
|
+
total: replayResult.total,
|
|
107
|
+
cache_key: cached.cache_key,
|
|
108
|
+
error: replayResult.error,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// HIT path. Side-write the synthesized execute_live output via
|
|
113
|
+
// context.state.set so downstream generate_script reads the same
|
|
114
|
+
// shape it expects (actions[], finalUrl, …). The customExecute
|
|
115
|
+
// return-value lands in state.cache_replay; the execute_live slot
|
|
116
|
+
// has to be populated separately.
|
|
117
|
+
if (typeof context.state?.set === 'function') {
|
|
118
|
+
context.state.set('execute_live', {
|
|
119
|
+
success: true,
|
|
120
|
+
steps: cached.actions.map((a) => a.description),
|
|
121
|
+
actions: cached.actions,
|
|
122
|
+
assertions: [],
|
|
123
|
+
finalUrl,
|
|
124
|
+
browserClosed: true,
|
|
125
|
+
notes: 'cache_replay hit — actions replayed via Playwright, no LLM',
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return {
|
|
130
|
+
hit: true,
|
|
131
|
+
elapsed_ms: elapsedMs,
|
|
132
|
+
executed: replayResult.executed,
|
|
133
|
+
total: replayResult.total,
|
|
134
|
+
cache_key: cached.cache_key,
|
|
135
|
+
execute_live_synthesized: true,
|
|
136
|
+
};
|
|
137
|
+
},
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
// ─── helpers ────────────────────────────────────────────────────────────
|
|
141
|
+
|
|
142
|
+
function extractDomainFromSpec(spec) {
|
|
143
|
+
if (!spec) return null;
|
|
144
|
+
// Find the first http(s) URL in the spec and run it through the
|
|
145
|
+
// SAME `extractDomain` the persister uses, so the cache-key lookup
|
|
146
|
+
// matches what was actually written (notably: `www.` is stripped).
|
|
147
|
+
const m = String(spec).match(/https?:\/\/[^\s"'<>]+/);
|
|
148
|
+
if (!m) return null;
|
|
149
|
+
return extractDomain(m[0]);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Find a cached row by (domain, spec_path). Picks the row with
|
|
154
|
+
* highest success_count if multiple match.
|
|
155
|
+
* Uses dolt via subprocess (matching the rest of the codebase's
|
|
156
|
+
* Dolt-access pattern).
|
|
157
|
+
*/
|
|
158
|
+
async function lookupCacheByDomainAndSpec({ cwd, domain, specPath }) {
|
|
159
|
+
const dbDir = join(cwd, '.zibby', 'memory');
|
|
160
|
+
const safeDomain = escapeSql(domain);
|
|
161
|
+
const safeSpec = escapeSql(specPath);
|
|
162
|
+
const sql = `SELECT cache_key, actions_json, page_fingerprint
|
|
163
|
+
FROM action_cache
|
|
164
|
+
WHERE domain = ${safeDomain} AND spec_path = ${safeSpec}
|
|
165
|
+
ORDER BY success_count DESC, last_used_at DESC
|
|
166
|
+
LIMIT 1`;
|
|
167
|
+
const rows = await runDoltJson(dbDir, sql);
|
|
168
|
+
if (!rows || rows.length === 0) return null;
|
|
169
|
+
try {
|
|
170
|
+
const actions = JSON.parse(rows[0].actions_json);
|
|
171
|
+
return { cache_key: rows[0].cache_key, actions, fingerprint: rows[0].page_fingerprint };
|
|
172
|
+
} catch {
|
|
173
|
+
return null;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
async function incrementCacheFailure({ cwd, cacheKey }) {
|
|
178
|
+
const dbDir = join(cwd, '.zibby', 'memory');
|
|
179
|
+
const sql = `UPDATE action_cache
|
|
180
|
+
SET failure_count = failure_count + 1, last_replay_status = 'replay-failed'
|
|
181
|
+
WHERE cache_key = ${escapeSql(cacheKey)}`;
|
|
182
|
+
await runDoltExec(dbDir, sql).catch(() => { /* non-fatal */ });
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function escapeSql(v) {
|
|
186
|
+
if (v == null) return 'NULL';
|
|
187
|
+
return `'${String(v).replace(/'/g, "''")}'`;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function runDoltJson(dir, sql) {
|
|
191
|
+
return new Promise((resolve) => {
|
|
192
|
+
const child = spawn('dolt', ['sql', '-r', 'json', '-q', sql], { cwd: dir });
|
|
193
|
+
let out = '';
|
|
194
|
+
child.stdout.on('data', (d) => { out += d; });
|
|
195
|
+
child.on('close', () => {
|
|
196
|
+
try {
|
|
197
|
+
const parsed = JSON.parse(out);
|
|
198
|
+
resolve(parsed.rows || []);
|
|
199
|
+
} catch {
|
|
200
|
+
resolve([]);
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
child.on('error', () => resolve([]));
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function runDoltExec(dir, sql) {
|
|
208
|
+
return new Promise((resolve, reject) => {
|
|
209
|
+
const child = spawn('dolt', ['sql', '-q', sql], { cwd: dir });
|
|
210
|
+
child.on('close', (code) => (code === 0 ? resolve() : reject(new Error(`dolt exit ${code}`))));
|
|
211
|
+
child.on('error', reject);
|
|
212
|
+
});
|
|
213
|
+
}
|