@evolvingmachines/sdk 0.0.15 → 0.0.16

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.cjs CHANGED
@@ -1,9 +1,9 @@
1
- 'use strict';var events=require('events'),tn=require('ajv'),At=require('zod-to-json-schema'),P=require('fs'),Y=require('path'),promises=require('fs/promises'),os=require('os'),crypto=require('crypto'),core=require('@composio/core'),zod=require('zod'),e2b=require('@evolvingmachines/e2b'),daytona=require('@evolvingmachines/daytona'),modal=require('@evolvingmachines/modal');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}function _interopNamespace(e){if(e&&e.__esModule)return e;var n=Object.create(null);if(e){Object.keys(e).forEach(function(k){if(k!=='default'){var d=Object.getOwnPropertyDescriptor(e,k);Object.defineProperty(n,k,d.get?d:{enumerable:true,get:function(){return e[k]}});}})}n.default=e;return Object.freeze(n)}var tn__default=/*#__PURE__*/_interopDefault(tn);var At__default=/*#__PURE__*/_interopDefault(At);var P__namespace=/*#__PURE__*/_interopNamespace(P);var Y__namespace=/*#__PURE__*/_interopNamespace(Y);var kt={CLAUDE:"claude",CODEX:"codex",GEMINI:"gemini",QWEN:"qwen"},he={strict:{coerceTypes:false,removeAdditional:false,useDefaults:false,allErrors:true},loose:{coerceTypes:"array",removeAdditional:false,useDefaults:true,allErrors:true}};var ye={claude:{image:"evolve-all",apiKeyEnv:"ANTHROPIC_API_KEY",oauthEnv:"CLAUDE_CODE_OAUTH_TOKEN",baseUrlEnv:"ANTHROPIC_BASE_URL",defaultModel:"opus",models:[{alias:"opus",modelId:"claude-opus-4-5-20251101",description:"Complex reasoning, R&D, architecting"},{alias:"sonnet",modelId:"claude-sonnet-4-5-20250929",description:"Daily coding, features, tests"},{alias:"haiku",modelId:"claude-haiku-4-5-20251001",description:"Quick tasks, syntax correction"}],systemPromptFile:"CLAUDE.md",mcpConfig:{settingsDir:"~/.claude",filename:"settings.json",format:"json",projectConfig:true},availableBetas:{CONTEXT_1M:"context-1m-2025-08-07"},skillsConfig:{sourceDir:"/home/user/.evolve/skills",targetDir:"/home/user/.claude/skills"},buildCommand:({prompt:c,model:e,isResume:n,betas:s})=>{let a=n?"--continue ":"",o=s?.length?`--betas ${s.join(",")} `:"";return `echo "${c}" | claude -p ${a}${o}--model ${e} --output-format stream-json --verbose --dangerously-skip-permissions`}},codex:{image:"evolve-all",apiKeyEnv:"OPENAI_API_KEY",oauthEnv:"CODEX_OAUTH_FILE_PATH",oauthFileName:"auth.json",baseUrlEnv:"OPENAI_BASE_URL",defaultModel:"gpt-5.2",models:[{alias:"gpt-5.2",modelId:"gpt-5.2",description:"Base model"},{alias:"gpt-5.2-codex",modelId:"gpt-5.2-codex",description:"Code-optimized"},{alias:"gpt-5.1-codex-max",modelId:"gpt-5.1-codex-max",description:"Best reasoning (xhigh effort)"},{alias:"gpt-5.1-mini",modelId:"gpt-5.1-mini",description:"Smaller/faster"}],systemPromptFile:"AGENTS.md",mcpConfig:{settingsDir:"~/.codex",filename:"config.toml",format:"toml"},skillsConfig:{sourceDir:"/home/user/.evolve/skills",targetDir:"/home/user/.codex/skills"},setupCommand:`printf '%s\\n' "$OPENAI_API_KEY" | codex login --with-api-key`,buildCommand:({prompt:c,model:e,isResume:n,reasoningEffort:s})=>{let a=s?` -c model_reasoning_effort="${s}"`:"";return `printf '%s' "${c}" | codex exec --model ${e}${a} --dangerously-bypass-approvals-and-sandbox --skip-git-repo-check --json${n?" resume --last":""}`}},gemini:{image:"evolve-all",apiKeyEnv:"GEMINI_API_KEY",oauthEnv:"GEMINI_OAUTH_FILE_PATH",oauthFileName:"oauth_creds.json",oauthActivationEnv:{key:"GOOGLE_GENAI_USE_GCA",value:"true"},baseUrlEnv:"GOOGLE_GEMINI_BASE_URL",defaultModel:"gemini-3-flash-preview",models:[{alias:"gemini-3-pro-preview",modelId:"gemini-3-pro-preview",description:"Latest pro preview"},{alias:"gemini-3-flash-preview",modelId:"gemini-3-flash-preview",description:"Latest flash preview"},{alias:"gemini-2.5-pro",modelId:"gemini-2.5-pro",description:"Complex tasks, deep reasoning"},{alias:"gemini-2.5-flash",modelId:"gemini-2.5-flash",description:"Balance speed/reasoning"},{alias:"gemini-2.5-flash-lite",modelId:"gemini-2.5-flash-lite",description:"Simple tasks, fastest"}],systemPromptFile:"GEMINI.md",mcpConfig:{settingsDir:"~/.gemini",filename:"settings.json",format:"json"},skillsConfig:{sourceDir:"/home/user/.evolve/skills",targetDir:"/home/user/.gemini/skills"},usePassthroughGateway:true,buildCommand:({prompt:c,model:e,isResume:n})=>`gemini "${c}" ${n?"--resume latest ":""}--model ${e} --yolo --output-format stream-json`},qwen:{image:"evolve-all",apiKeyEnv:"OPENAI_API_KEY",baseUrlEnv:"OPENAI_BASE_URL",defaultModel:"qwen3-coder-plus",models:[{alias:"qwen3-coder-plus",modelId:"qwen3-coder-plus",description:"Code generation, debugging"},{alias:"qwen3-vl-plus",modelId:"qwen3-vl-plus",description:"Vision + language, multimodal"}],systemPromptFile:"QWEN.md",mcpConfig:{settingsDir:"~/.qwen",filename:"settings.json",format:"json"},skillsConfig:{sourceDir:"/home/user/.evolve/skills",targetDir:"/home/user/.qwen/skills",enableFlag:"--experimental-skills"},defaultBaseUrl:"https://dashscope-intl.aliyuncs.com/compatible-mode/v1",buildCommand:({prompt:c,model:e,isResume:n,isDirectMode:s,skills:a})=>{let o=n?"--continue ":"",i=a?.length?"--experimental-skills ":"",l=s||e.startsWith("dashscope/")?e:`dashscope/${e}`;return `qwen "${c}" ${o}${i}--model ${l} --yolo --output-format stream-json`}}};function M(c){let e=ye[c];if(!e)throw new Error(`Unknown agent type: ${c}`);return e}function Tt(c){return c in ye}function ve(c){return c.replace(/^~/,"/home/user")}function $(c){let e=M(c);return `${ve(e.mcpConfig.settingsDir)}/${e.mcpConfig.filename}`}function U(c){let e=M(c);return ve(e.mcpConfig.settingsDir)}function V(c){if(c instanceof Error){let e=c.message.toLowerCase();return e.includes("not found")||e.includes("enoent")||e.includes("no such file")||e.includes("does not exist")}return false}function xe(c,e){let n=[e.command,e.url].filter(Boolean);if(n.length===0)throw new Error(`MCP server "${c}" must specify command or url`);if(n.length>1)throw new Error(`MCP server "${c}" cannot specify both command and url`)}function be(c){for(let[e,n]of Object.entries(c))xe(e,n);}function we(c){return c.type?c.type:c.command?"stdio":"sse"}function _t(c){let e=we(c);return {...c,type:e}}function Et(c){let{type:e,url:n,...s}=c;return we(c)==="http"&&n?{httpUrl:n,...s}:n?{url:n,...s}:s}function Pt(c){return {type:we(c),...c}}async function Be(c,e,n,s){be(n);let a=U(e),o=$(e);await c.files.makeDir(a);let i={};try{let r=await c.files.read(o);typeof r=="string"&&(i=JSON.parse(r));}catch(r){if(!V(r))throw r}let l=Object.fromEntries(Object.entries(n).map(([r,p])=>[r,s(p)]));await c.files.write(o,JSON.stringify({...i,mcpServers:l},null,2));}async function Q(c,e,n){be(n);let s=U("claude"),a=$("claude"),o=Object.fromEntries(Object.entries(n).map(([l,r])=>[l,Pt(r)]));await c.files.write(`${e}/.mcp.json`,JSON.stringify({mcpServers:o},null,2)),await c.files.makeDir(s);let i={};try{let l=await c.files.read(a);typeof l=="string"&&(i=JSON.parse(l));}catch(l){if(!V(l))throw l}i.enableAllProjectMcpServers=true,await c.files.write(a,JSON.stringify(i,null,2));}async function X(c,e){await Be(c,"gemini",e,_t);}async function ee(c,e){await Be(c,"qwen",e,Et);}function _(c){return typeof c=="string"?`"${c.replace(/\\/g,"\\\\").replace(/"/g,'\\"')}"`:Array.isArray(c)?`[${c.map(e=>_(e)).join(", ")}]`:typeof c=="object"&&c!==null?`{ ${Object.entries(c).map(([n,s])=>`${n} = ${_(s)}`).join(", ")} }`:typeof c=="boolean"?c?"true":"false":String(c)}async function te(c,e){for(let[r,p]of Object.entries(e))xe(r,p);let n=U("codex"),s=$("codex");await c.files.makeDir(n);let a="";try{let r=await c.files.read(s);typeof r=="string"&&(a=r);}catch(r){if(!V(r))throw r}let o=[],i=[];a.includes("experimental_use_rmcp_client")||(o.push("# Enable improved RMCP client (recommended)"),o.push("experimental_use_rmcp_client = true")),a.includes("[mcp_servers]")||i.push("[mcp_servers]","");for(let[r,p]of Object.entries(e)){if(a.includes(`[mcp_servers.${r}]`))continue;i.push(`[mcp_servers.${r}]`);let t=p.type??(p.command?"stdio":"http");if(t==="http"||t==="sse"||!!p.url){if(!p.url)throw new Error(`MCP server "${r}" is missing url for ${t} transport`);i.push(`url = ${_(p.url)}`),p.bearerTokenEnvVar&&i.push(`bearer_token_env_var = ${_(p.bearerTokenEnvVar)}`);let d=p.httpHeaders??p.headers;d&&Object.keys(d).length>0&&i.push(`http_headers = ${_(d)}`),p.envHttpHeaders&&Object.keys(p.envHttpHeaders).length>0&&i.push(`env_http_headers = ${_(p.envHttpHeaders)}`);}else i.push(`command = ${_(p.command)}`),p.args&&p.args.length>0&&i.push(`args = ${_(p.args)}`),p.cwd&&i.push(`cwd = ${_(p.cwd)}`);p.env&&Object.keys(p.env).length>0&&i.push(`env = ${_(p.env)}`),p.envVars&&p.envVars.length>0&&i.push(`env_vars = ${_(p.envVars)}`),i.push("");}let l=[a.trim(),o.join(`
1
+ 'use strict';var events=require('events'),tn=require('ajv'),At=require('zod-to-json-schema'),E=require('fs'),Y=require('path'),promises=require('fs/promises'),os=require('os'),crypto=require('crypto'),core=require('@composio/core'),zod=require('zod'),e2b=require('@evolvingmachines/e2b'),daytona=require('@evolvingmachines/daytona'),modal=require('@evolvingmachines/modal');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}function _interopNamespace(e){if(e&&e.__esModule)return e;var n=Object.create(null);if(e){Object.keys(e).forEach(function(k){if(k!=='default'){var d=Object.getOwnPropertyDescriptor(e,k);Object.defineProperty(n,k,d.get?d:{enumerable:true,get:function(){return e[k]}});}})}n.default=e;return Object.freeze(n)}var tn__default=/*#__PURE__*/_interopDefault(tn);var At__default=/*#__PURE__*/_interopDefault(At);var E__namespace=/*#__PURE__*/_interopNamespace(E);var Y__namespace=/*#__PURE__*/_interopNamespace(Y);var kt={CLAUDE:"claude",CODEX:"codex",GEMINI:"gemini",QWEN:"qwen"},he={strict:{coerceTypes:false,removeAdditional:false,useDefaults:false,allErrors:true},loose:{coerceTypes:"array",removeAdditional:false,useDefaults:true,allErrors:true}};var ye={claude:{image:"evolve-all",apiKeyEnv:"ANTHROPIC_API_KEY",oauthEnv:"CLAUDE_CODE_OAUTH_TOKEN",baseUrlEnv:"ANTHROPIC_BASE_URL",defaultModel:"opus",models:[{alias:"opus",modelId:"claude-opus-4-5-20251101",description:"Complex reasoning, R&D, architecting"},{alias:"sonnet",modelId:"claude-sonnet-4-5-20250929",description:"Daily coding, features, tests"},{alias:"haiku",modelId:"claude-haiku-4-5-20251001",description:"Quick tasks, syntax correction"}],systemPromptFile:"CLAUDE.md",mcpConfig:{settingsDir:"~/.claude",filename:"settings.json",format:"json",projectConfig:true},availableBetas:{CONTEXT_1M:"context-1m-2025-08-07"},skillsConfig:{sourceDir:"/home/user/.evolve/skills",targetDir:"/home/user/.claude/skills"},buildCommand:({prompt:c,model:e,isResume:n,betas:s})=>{let a=n?"--continue ":"",o=s?.length?`--betas ${s.join(",")} `:"";return `echo "${c}" | claude -p ${a}${o}--model ${e} --output-format stream-json --verbose --dangerously-skip-permissions`}},codex:{image:"evolve-all",apiKeyEnv:"OPENAI_API_KEY",oauthEnv:"CODEX_OAUTH_FILE_PATH",oauthFileName:"auth.json",baseUrlEnv:"OPENAI_BASE_URL",defaultModel:"gpt-5.2",models:[{alias:"gpt-5.2",modelId:"gpt-5.2",description:"Base model"},{alias:"gpt-5.2-codex",modelId:"gpt-5.2-codex",description:"Code-optimized"},{alias:"gpt-5.1-codex-max",modelId:"gpt-5.1-codex-max",description:"Best reasoning (xhigh effort)"},{alias:"gpt-5.1-mini",modelId:"gpt-5.1-mini",description:"Smaller/faster"}],systemPromptFile:"AGENTS.md",mcpConfig:{settingsDir:"~/.codex",filename:"config.toml",format:"toml"},skillsConfig:{sourceDir:"/home/user/.evolve/skills",targetDir:"/home/user/.codex/skills"},setupCommand:`printf '%s\\n' "$OPENAI_API_KEY" | codex login --with-api-key`,buildCommand:({prompt:c,model:e,isResume:n,reasoningEffort:s})=>{let a=s?` -c model_reasoning_effort="${s}"`:"";return `printf '%s' "${c}" | codex exec --model ${e}${a} --dangerously-bypass-approvals-and-sandbox --skip-git-repo-check --json${n?" resume --last":""}`}},gemini:{image:"evolve-all",apiKeyEnv:"GEMINI_API_KEY",oauthEnv:"GEMINI_OAUTH_FILE_PATH",oauthFileName:"oauth_creds.json",oauthActivationEnv:{key:"GOOGLE_GENAI_USE_GCA",value:"true"},baseUrlEnv:"GOOGLE_GEMINI_BASE_URL",defaultModel:"gemini-3-flash-preview",models:[{alias:"gemini-3-pro-preview",modelId:"gemini-3-pro-preview",description:"Latest pro preview"},{alias:"gemini-3-flash-preview",modelId:"gemini-3-flash-preview",description:"Latest flash preview"},{alias:"gemini-2.5-pro",modelId:"gemini-2.5-pro",description:"Complex tasks, deep reasoning"},{alias:"gemini-2.5-flash",modelId:"gemini-2.5-flash",description:"Balance speed/reasoning"},{alias:"gemini-2.5-flash-lite",modelId:"gemini-2.5-flash-lite",description:"Simple tasks, fastest"}],systemPromptFile:"GEMINI.md",mcpConfig:{settingsDir:"~/.gemini",filename:"settings.json",format:"json"},skillsConfig:{sourceDir:"/home/user/.evolve/skills",targetDir:"/home/user/.gemini/skills"},usePassthroughGateway:true,buildCommand:({prompt:c,model:e,isResume:n})=>`gemini "${c}" ${n?"--resume latest ":""}--model ${e} --yolo --output-format stream-json`},qwen:{image:"evolve-all",apiKeyEnv:"OPENAI_API_KEY",baseUrlEnv:"OPENAI_BASE_URL",defaultModel:"qwen3-coder-plus",models:[{alias:"qwen3-coder-plus",modelId:"qwen3-coder-plus",description:"Code generation, debugging"},{alias:"qwen3-vl-plus",modelId:"qwen3-vl-plus",description:"Vision + language, multimodal"}],systemPromptFile:"QWEN.md",mcpConfig:{settingsDir:"~/.qwen",filename:"settings.json",format:"json"},skillsConfig:{sourceDir:"/home/user/.evolve/skills",targetDir:"/home/user/.qwen/skills",enableFlag:"--experimental-skills"},defaultBaseUrl:"https://dashscope-intl.aliyuncs.com/compatible-mode/v1",buildCommand:({prompt:c,model:e,isResume:n,isDirectMode:s,skills:a})=>{let o=n?"--continue ":"",i=a?.length?"--experimental-skills ":"",l=s||e.startsWith("dashscope/")?e:`dashscope/${e}`;return `qwen "${c}" ${o}${i}--model ${l} --yolo --output-format stream-json`}}};function M(c){let e=ye[c];if(!e)throw new Error(`Unknown agent type: ${c}`);return e}function _t(c){return c in ye}function ve(c){return c.replace(/^~/,"/home/user")}function $(c){let e=M(c);return `${ve(e.mcpConfig.settingsDir)}/${e.mcpConfig.filename}`}function U(c){let e=M(c);return ve(e.mcpConfig.settingsDir)}function V(c){if(c instanceof Error){let e=c.message.toLowerCase();return e.includes("not found")||e.includes("enoent")||e.includes("no such file")||e.includes("does not exist")}return false}function xe(c,e){let n=[e.command,e.url].filter(Boolean);if(n.length===0)throw new Error(`MCP server "${c}" must specify command or url`);if(n.length>1)throw new Error(`MCP server "${c}" cannot specify both command and url`)}function be(c){for(let[e,n]of Object.entries(c))xe(e,n);}function Ce(c){return c.type?c.type:c.command?"stdio":"sse"}function Tt(c){let e=Ce(c);return {...c,type:e}}function Pt(c){let{type:e,url:n,...s}=c;return Ce(c)==="http"&&n?{httpUrl:n,...s}:n?{url:n,...s}:s}function Et(c){return {type:Ce(c),...c}}async function Be(c,e,n,s){be(n);let a=U(e),o=$(e);await c.files.makeDir(a);let i={};try{let r=await c.files.read(o);typeof r=="string"&&(i=JSON.parse(r));}catch(r){if(!V(r))throw r}let l=Object.fromEntries(Object.entries(n).map(([r,p])=>[r,s(p)]));await c.files.write(o,JSON.stringify({...i,mcpServers:l},null,2));}async function Q(c,e,n){be(n);let s=U("claude"),a=$("claude"),o=Object.fromEntries(Object.entries(n).map(([l,r])=>[l,Et(r)]));await c.files.write(`${e}/.mcp.json`,JSON.stringify({mcpServers:o},null,2)),await c.files.makeDir(s);let i={};try{let l=await c.files.read(a);typeof l=="string"&&(i=JSON.parse(l));}catch(l){if(!V(l))throw l}i.enableAllProjectMcpServers=true,await c.files.write(a,JSON.stringify(i,null,2));}async function X(c,e){await Be(c,"gemini",e,Tt);}async function ee(c,e){await Be(c,"qwen",e,Pt);}function T(c){return typeof c=="string"?`"${c.replace(/\\/g,"\\\\").replace(/"/g,'\\"')}"`:Array.isArray(c)?`[${c.map(e=>T(e)).join(", ")}]`:typeof c=="object"&&c!==null?`{ ${Object.entries(c).map(([n,s])=>`${n} = ${T(s)}`).join(", ")} }`:typeof c=="boolean"?c?"true":"false":String(c)}async function te(c,e){for(let[r,p]of Object.entries(e))xe(r,p);let n=U("codex"),s=$("codex");await c.files.makeDir(n);let a="";try{let r=await c.files.read(s);typeof r=="string"&&(a=r);}catch(r){if(!V(r))throw r}let o=[],i=[];a.includes("experimental_use_rmcp_client")||(o.push("# Enable improved RMCP client (recommended)"),o.push("experimental_use_rmcp_client = true")),a.includes("[mcp_servers]")||i.push("[mcp_servers]","");for(let[r,p]of Object.entries(e)){if(a.includes(`[mcp_servers.${r}]`))continue;i.push(`[mcp_servers.${r}]`);let t=p.type??(p.command?"stdio":"http");if(t==="http"||t==="sse"||!!p.url){if(!p.url)throw new Error(`MCP server "${r}" is missing url for ${t} transport`);i.push(`url = ${T(p.url)}`),p.bearerTokenEnvVar&&i.push(`bearer_token_env_var = ${T(p.bearerTokenEnvVar)}`);let d=p.httpHeaders??p.headers;d&&Object.keys(d).length>0&&i.push(`http_headers = ${T(d)}`),p.envHttpHeaders&&Object.keys(p.envHttpHeaders).length>0&&i.push(`env_http_headers = ${T(p.envHttpHeaders)}`);}else i.push(`command = ${T(p.command)}`),p.args&&p.args.length>0&&i.push(`args = ${T(p.args)}`),p.cwd&&i.push(`cwd = ${T(p.cwd)}`);p.env&&Object.keys(p.env).length>0&&i.push(`env = ${T(p.env)}`),p.envVars&&p.envVars.length>0&&i.push(`env_vars = ${T(p.envVars)}`),i.push("");}let l=[a.trim(),o.join(`
2
2
  `),i.join(`
3
3
  `)].filter(r=>r.length>0);await c.files.write(s,l.join(`
4
4
 
5
5
  `)+`
6
- `);}async function Ce(c,e,n,s){if(!(!s||Object.keys(s).length===0))switch(c){case "claude":await Q(e,n,s);break;case "codex":await te(e,s);break;case "gemini":await X(e,s);break;case "qwen":await ee(e,s);break;default:throw new Error(`Unknown agent type for MCP config: ${c}`)}}function ne(){let c={};return function(p){let t;try{t=JSON.parse(p);}catch{return null}if(t._meta||t._prompt)return null;let u=t.session_id;switch(t.type){case "system":return null;case "assistant":return e(t.message?.content,"assistant",u);case "user":{let d=t.message?.content;return typeof d=="string"||Array.isArray(d)&&d.length===1&&d[0].type==="text"?null:e(d,"user",u)}case "result":return null;default:return null}};function e(r,p,t){if(typeof r=="string")return p==="user"?null:[{sessionId:t,update:{sessionUpdate:"agent_message_chunk",content:{type:"text",text:r}}}];if(!r||!Array.isArray(r))return null;let u=[];for(let d of r){let m=null;switch(d.type){case "text":case "text_delta":if(p==="user")break;m={sessionUpdate:"agent_message_chunk",content:{type:"text",text:d.text}};break;case "image":if(p==="user")break;m={sessionUpdate:"agent_message_chunk",content:{type:"image",data:d.source?.type==="base64"?d.source.data:"",mimeType:d.source?.type==="base64"?d.source.media_type:"",uri:d.source?.type==="url"?d.source.url:void 0}};break;case "thinking":case "thinking_delta":m={sessionUpdate:"agent_thought_chunk",content:{type:"text",text:d.thinking}};break;case "tool_use":case "server_tool_use":case "mcp_tool_use":{if(c[d.id]=d,d.name==="TodoWrite")Array.isArray(d.input?.todos)&&(m={sessionUpdate:"plan",entries:i(d.input)});else {let f=n(d);m={sessionUpdate:"tool_call",toolCallId:d.id,status:"pending",...f};}break}case "tool_result":case "tool_search_tool_result":case "web_fetch_tool_result":case "web_search_tool_result":case "code_execution_tool_result":case "bash_code_execution_tool_result":case "text_editor_code_execution_tool_result":case "mcp_tool_result":{let f=c[d.tool_use_id];if(!f||(delete c[d.tool_use_id],f.name==="TodoWrite"))break;m={sessionUpdate:"tool_call_update",toolCallId:d.tool_use_id,status:"is_error"in d&&d.is_error?"failed":"completed",...s(d,f)};break}}m&&u.push({sessionId:t,update:m});}return u.length>0?u:null}function n(r){let p=r.name,t=r.input||{};switch(p){case "Task":return {title:t.description||"Task",kind:"think",content:t.prompt?[{type:"content",content:{type:"text",text:t.prompt}}]:[],rawInput:t};case "NotebookRead":return {title:t.notebook_path?`Read Notebook ${t.notebook_path}`:"Read Notebook",kind:"read",content:[],locations:t.notebook_path?[{path:t.notebook_path}]:[],rawInput:t};case "NotebookEdit":return {title:t.notebook_path?`Edit Notebook ${t.notebook_path}`:"Edit Notebook",kind:"edit",content:t.new_source?[{type:"content",content:{type:"text",text:t.new_source}}]:[],locations:t.notebook_path?[{path:t.notebook_path}]:[],rawInput:t};case "Bash":return {title:t.command?"`"+t.command.replaceAll("`","\\`")+"`":"Terminal",kind:"execute",content:t.description?[{type:"content",content:{type:"text",text:t.description}}]:[],rawInput:t};case "BashOutput":return {title:"Tail Logs",kind:"execute",content:[],rawInput:t};case "KillShell":return {title:"Kill Process",kind:"execute",content:[],rawInput:t};case "Read":{let u="";return t.limit?u=" ("+((t.offset??0)+1)+" - "+((t.offset??0)+t.limit)+")":t.offset&&(u=" (from line "+(t.offset+1)+")"),{title:"Read "+(t.file_path??"File")+u,kind:"read",content:[],locations:t.file_path?[{path:t.file_path,line:t.offset??0}]:[],rawInput:t}}case "LS":return {title:t.path?`List \`${t.path}\` directory`:"List current directory",kind:"search",content:[],locations:[],rawInput:t};case "Edit":{let u=t.file_path;return {title:u?`Edit \`${u}\``:"Edit",kind:"edit",content:u?[{type:"diff",path:u,oldText:t.old_string??null,newText:t.new_string??""}]:[],locations:u?[{path:u}]:[],rawInput:t}}case "Write":return {title:t.file_path?`Write ${t.file_path}`:"Write",kind:"edit",content:t.file_path?[{type:"diff",path:t.file_path,oldText:null,newText:t.content??""}]:[],locations:t.file_path?[{path:t.file_path}]:[],rawInput:t};case "Glob":{let u="Find";return t.path&&(u+=` \`${t.path}\``),t.pattern&&(u+=` \`${t.pattern}\``),{title:u,kind:"search",content:[],locations:t.path?[{path:t.path}]:[],rawInput:t}}case "Grep":{let u="grep";return t["-i"]&&(u+=" -i"),t["-n"]&&(u+=" -n"),t["-A"]!==void 0&&(u+=` -A ${t["-A"]}`),t["-B"]!==void 0&&(u+=` -B ${t["-B"]}`),t["-C"]!==void 0&&(u+=` -C ${t["-C"]}`),t.output_mode==="files_with_matches"?u+=" -l":t.output_mode==="count"&&(u+=" -c"),t.head_limit!==void 0&&(u+=` | head -${t.head_limit}`),t.glob&&(u+=` --include="${t.glob}"`),t.type&&(u+=` --type=${t.type}`),t.multiline&&(u+=" -P"),u+=` "${t.pattern??""}"`,t.path&&(u+=` ${t.path}`),{title:u,kind:"search",content:[],rawInput:t}}case "WebFetch":return {title:t.url?`Fetch ${t.url}`:"Fetch",kind:"fetch",content:t.prompt?[{type:"content",content:{type:"text",text:t.prompt}}]:[],rawInput:t};case "WebSearch":{let u=`"${t.query??""}"`;return t.allowed_domains?.length>0&&(u+=` (allowed: ${t.allowed_domains.join(", ")})`),t.blocked_domains?.length>0&&(u+=` (blocked: ${t.blocked_domains.join(", ")})`),{title:u,kind:"fetch",content:[],rawInput:t}}case "TodoWrite":return {title:Array.isArray(t.todos)?`Update TODOs: ${t.todos.map(u=>u.content).join(", ")}`:"Update TODOs",kind:"think",content:[],rawInput:t};case "ExitPlanMode":return {title:"Ready to code?",kind:"switch_mode",content:t.plan?[{type:"content",content:{type:"text",text:t.plan}}]:[],rawInput:t};default:if(p?.startsWith("mcp__")){let u=p.split("__"),d=u[1]||"mcp",m=u.slice(2).join("__")||p;return {title:`${d}: ${m}`,kind:"other",content:[],rawInput:t}}return {title:p||"Unknown Tool",kind:"other",content:[],rawInput:t}}}function s(r,p){let t=r.content;switch(p?.name){case "Read":return Array.isArray(t)&&t.length>0?{content:t.map(u=>({type:"content",content:u.type==="text"?{type:"text",text:l(u.text)}:o(u,false)}))}:typeof t=="string"&&t.length>0?{content:[{type:"content",content:{type:"text",text:l(t)}}]}:{};case "Edit":case "Write":case "Bash":return a(t,r.is_error||false);case "ExitPlanMode":return {title:"Exited Plan Mode"};default:return a(t,"is_error"in r?r.is_error:false)}}function a(r,p=false){return Array.isArray(r)&&r.length>0?{content:r.map(t=>({type:"content",content:o(t,p)}))}:typeof r=="string"&&r.length>0?{content:[{type:"content",content:{type:"text",text:p?"```\n"+r+"\n```":r}}]}:{}}function o(r,p){if(r.type==="text")return p?{type:"text",text:"```\n"+r.text+"\n```"}:{type:"text",text:r.text};if(r.type==="image"){if(r.data&&r.mimeType)return {type:"image",data:r.data,mimeType:r.mimeType};if(r.source?.type==="base64")return {type:"image",data:r.source.data||"",mimeType:r.source.media_type||""};if(r.source?.type==="url")return {type:"image",data:"",mimeType:"",uri:r.source.url}}return r}function i(r){return r.todos.map(p=>({content:p.content,status:p.status||"pending",priority:"medium"}))}function l(r){let p="```",t=Array.from(r.matchAll(/^```+/gm));for(let[u]of t)for(;u.length>=p.length;)p+="`";return p+`
6
+ `);}async function we(c,e,n,s){if(!(!s||Object.keys(s).length===0))switch(c){case "claude":await Q(e,n,s);break;case "codex":await te(e,s);break;case "gemini":await X(e,s);break;case "qwen":await ee(e,s);break;default:throw new Error(`Unknown agent type for MCP config: ${c}`)}}function ne(){let c={};return function(p){let t;try{t=JSON.parse(p);}catch{return null}if(t._meta||t._prompt)return null;let u=t.session_id;switch(t.type){case "system":return null;case "assistant":return e(t.message?.content,"assistant",u);case "user":{let d=t.message?.content;return typeof d=="string"||Array.isArray(d)&&d.length===1&&d[0].type==="text"?null:e(d,"user",u)}case "result":return null;default:return null}};function e(r,p,t){if(typeof r=="string")return p==="user"?null:[{sessionId:t,update:{sessionUpdate:"agent_message_chunk",content:{type:"text",text:r}}}];if(!r||!Array.isArray(r))return null;let u=[];for(let d of r){let m=null;switch(d.type){case "text":case "text_delta":if(p==="user")break;m={sessionUpdate:"agent_message_chunk",content:{type:"text",text:d.text}};break;case "image":if(p==="user")break;m={sessionUpdate:"agent_message_chunk",content:{type:"image",data:d.source?.type==="base64"?d.source.data:"",mimeType:d.source?.type==="base64"?d.source.media_type:"",uri:d.source?.type==="url"?d.source.url:void 0}};break;case "thinking":case "thinking_delta":m={sessionUpdate:"agent_thought_chunk",content:{type:"text",text:d.thinking}};break;case "tool_use":case "server_tool_use":case "mcp_tool_use":{if(c[d.id]=d,d.name==="TodoWrite")Array.isArray(d.input?.todos)&&(m={sessionUpdate:"plan",entries:i(d.input)});else {let f=n(d);m={sessionUpdate:"tool_call",toolCallId:d.id,status:"pending",...f};}break}case "tool_result":case "tool_search_tool_result":case "web_fetch_tool_result":case "web_search_tool_result":case "code_execution_tool_result":case "bash_code_execution_tool_result":case "text_editor_code_execution_tool_result":case "mcp_tool_result":{let f=c[d.tool_use_id];if(!f||(delete c[d.tool_use_id],f.name==="TodoWrite"))break;m={sessionUpdate:"tool_call_update",toolCallId:d.tool_use_id,status:"is_error"in d&&d.is_error?"failed":"completed",...s(d,f)};break}}m&&u.push({sessionId:t,update:m});}return u.length>0?u:null}function n(r){let p=r.name,t=r.input||{};switch(p){case "Task":return {title:t.description||"Task",kind:"think",content:t.prompt?[{type:"content",content:{type:"text",text:t.prompt}}]:[],rawInput:t};case "NotebookRead":return {title:t.notebook_path?`Read Notebook ${t.notebook_path}`:"Read Notebook",kind:"read",content:[],locations:t.notebook_path?[{path:t.notebook_path}]:[],rawInput:t};case "NotebookEdit":return {title:t.notebook_path?`Edit Notebook ${t.notebook_path}`:"Edit Notebook",kind:"edit",content:t.new_source?[{type:"content",content:{type:"text",text:t.new_source}}]:[],locations:t.notebook_path?[{path:t.notebook_path}]:[],rawInput:t};case "Bash":return {title:t.command?"`"+t.command.replaceAll("`","\\`")+"`":"Terminal",kind:"execute",content:t.description?[{type:"content",content:{type:"text",text:t.description}}]:[],rawInput:t};case "BashOutput":return {title:"Tail Logs",kind:"execute",content:[],rawInput:t};case "KillShell":return {title:"Kill Process",kind:"execute",content:[],rawInput:t};case "Read":{let u="";return t.limit?u=" ("+((t.offset??0)+1)+" - "+((t.offset??0)+t.limit)+")":t.offset&&(u=" (from line "+(t.offset+1)+")"),{title:"Read "+(t.file_path??"File")+u,kind:"read",content:[],locations:t.file_path?[{path:t.file_path,line:t.offset??0}]:[],rawInput:t}}case "LS":return {title:t.path?`List \`${t.path}\` directory`:"List current directory",kind:"search",content:[],locations:[],rawInput:t};case "Edit":{let u=t.file_path;return {title:u?`Edit \`${u}\``:"Edit",kind:"edit",content:u?[{type:"diff",path:u,oldText:t.old_string??null,newText:t.new_string??""}]:[],locations:u?[{path:u}]:[],rawInput:t}}case "Write":return {title:t.file_path?`Write ${t.file_path}`:"Write",kind:"edit",content:t.file_path?[{type:"diff",path:t.file_path,oldText:null,newText:t.content??""}]:[],locations:t.file_path?[{path:t.file_path}]:[],rawInput:t};case "Glob":{let u="Find";return t.path&&(u+=` \`${t.path}\``),t.pattern&&(u+=` \`${t.pattern}\``),{title:u,kind:"search",content:[],locations:t.path?[{path:t.path}]:[],rawInput:t}}case "Grep":{let u="grep";return t["-i"]&&(u+=" -i"),t["-n"]&&(u+=" -n"),t["-A"]!==void 0&&(u+=` -A ${t["-A"]}`),t["-B"]!==void 0&&(u+=` -B ${t["-B"]}`),t["-C"]!==void 0&&(u+=` -C ${t["-C"]}`),t.output_mode==="files_with_matches"?u+=" -l":t.output_mode==="count"&&(u+=" -c"),t.head_limit!==void 0&&(u+=` | head -${t.head_limit}`),t.glob&&(u+=` --include="${t.glob}"`),t.type&&(u+=` --type=${t.type}`),t.multiline&&(u+=" -P"),u+=` "${t.pattern??""}"`,t.path&&(u+=` ${t.path}`),{title:u,kind:"search",content:[],rawInput:t}}case "WebFetch":return {title:t.url?`Fetch ${t.url}`:"Fetch",kind:"fetch",content:t.prompt?[{type:"content",content:{type:"text",text:t.prompt}}]:[],rawInput:t};case "WebSearch":{let u=`"${t.query??""}"`;return t.allowed_domains?.length>0&&(u+=` (allowed: ${t.allowed_domains.join(", ")})`),t.blocked_domains?.length>0&&(u+=` (blocked: ${t.blocked_domains.join(", ")})`),{title:u,kind:"fetch",content:[],rawInput:t}}case "TodoWrite":return {title:Array.isArray(t.todos)?`Update TODOs: ${t.todos.map(u=>u.content).join(", ")}`:"Update TODOs",kind:"think",content:[],rawInput:t};case "ExitPlanMode":return {title:"Ready to code?",kind:"switch_mode",content:t.plan?[{type:"content",content:{type:"text",text:t.plan}}]:[],rawInput:t};default:if(p?.startsWith("mcp__")){let u=p.split("__"),d=u[1]||"mcp",m=u.slice(2).join("__")||p;return {title:`${d}: ${m}`,kind:"other",content:[],rawInput:t}}return {title:p||"Unknown Tool",kind:"other",content:[],rawInput:t}}}function s(r,p){let t=r.content;switch(p?.name){case "Read":return Array.isArray(t)&&t.length>0?{content:t.map(u=>({type:"content",content:u.type==="text"?{type:"text",text:l(u.text)}:o(u,false)}))}:typeof t=="string"&&t.length>0?{content:[{type:"content",content:{type:"text",text:l(t)}}]}:{};case "Edit":case "Write":case "Bash":return a(t,r.is_error||false);case "ExitPlanMode":return {title:"Exited Plan Mode"};default:return a(t,"is_error"in r?r.is_error:false)}}function a(r,p=false){return Array.isArray(r)&&r.length>0?{content:r.map(t=>({type:"content",content:o(t,p)}))}:typeof r=="string"&&r.length>0?{content:[{type:"content",content:{type:"text",text:p?"```\n"+r+"\n```":r}}]}:{}}function o(r,p){if(r.type==="text")return p?{type:"text",text:"```\n"+r.text+"\n```"}:{type:"text",text:r.text};if(r.type==="image"){if(r.data&&r.mimeType)return {type:"image",data:r.data,mimeType:r.mimeType};if(r.source?.type==="base64")return {type:"image",data:r.source.data||"",mimeType:r.source.media_type||""};if(r.source?.type==="url")return {type:"image",data:"",mimeType:"",uri:r.source.url}}return r}function i(r){return r.todos.map(p=>({content:p.content,status:p.status||"pending",priority:"medium"}))}function l(r){let p="```",t=Array.from(r.matchAll(/^```+/gm));for(let[u]of t)for(;u.length>=p.length;)p+="`";return p+`
7
7
  `+r+(r.endsWith(`
8
8
  `)?"":`
9
9
  `)+p}}function se(){return function(r){let p;try{p=JSON.parse(r);}catch{return null}if(p._meta||p._prompt)return null;let t=[];switch(p.type){case "thread.started":case "turn.started":case "turn.completed":return null;case "item.started":{let u=p.item;if(!u)return null;let d=e(u);d&&t.push({update:d});break}case "item.updated":{let u=p.item;if(!u)return null;let d=n(u);d&&t.push({update:d});break}case "item.completed":{let u=p.item;if(!u)return null;let d=s(u);d&&t.push({update:d});break}default:return null}return t.length>0?t:null};function e(l){let r=l.id;switch(l.type){case "mcp_tool_call":return {type:"mcp_tool_call",name:`${l.server}:${l.tool}`},{sessionUpdate:"tool_call",toolCallId:r,title:`${l.server}: ${l.tool}`,kind:"other",status:"in_progress",rawInput:l.arguments,content:[]};case "command_execution":return {type:"command_execution",name:l.command},{sessionUpdate:"tool_call",toolCallId:r,title:l.command?`\`${l.command}\``:"Execute Command",kind:"execute",status:"in_progress",rawInput:{command:l.command},content:[]};case "web_search":return {type:"web_search",name:l.query},{sessionUpdate:"tool_call",toolCallId:r,title:`Search: ${l.query??""}`,kind:"fetch",status:"in_progress",content:[]};case "todo_list":return {sessionUpdate:"plan",entries:i(l.items)};default:return null}}function n(l){return l.type==="todo_list"?{sessionUpdate:"plan",entries:i(l.items)}:null}function s(l){let r=l.id;switch(l.type){case "reasoning":return {sessionUpdate:"agent_thought_chunk",content:{type:"text",text:l.text??l.summary?.join(`
@@ -16,8 +16,8 @@ ${p.message}
16
16
  ${d}
17
17
  \`\`\``:d}});else if(Array.isArray(d)){for(let f of d)if(f.type==="text"){let h=f.text;m.push({type:"content",content:{type:"text",text:u?`\`\`\`
18
18
  ${h}
19
- \`\`\``:h}});}}return {sessionUpdate:"tool_call_update",toolCallId:t,status:u?"failed":"completed",content:m}}function r(p,t){let u=Mt[p]||"other",d=[],m=[],f=p;switch(p){case "Read":case "read_file":f=`Read ${t.file_path||t.absolute_path||"file"}`,(t.file_path||t.absolute_path)&&m.push({path:t.file_path||t.absolute_path});break;case "Write":case "write_file":f=`Write ${t.file_path||"file"}`,t.file_path&&(m.push({path:t.file_path}),d.push({type:"diff",path:t.file_path,oldText:null,newText:t.content||""}));break;case "Edit":case "edit_file":f=`Edit ${t.file_path||"file"}`,t.file_path&&m.push({path:t.file_path});break;case "Bash":case "shell":case "run_shell_command":f=t.command?`\`${t.command}\``:"Run command",t.description&&d.push({type:"content",content:{type:"text",text:t.description}});break;case "WebFetch":f=t.url?`Fetch ${t.url}`:"Web fetch";break;case "WebSearch":case "brave_web_search":f=t.query?`"${t.query}"`:"Web search";break;case "Glob":f=`Find ${t.pattern||"files"}`,t.path&&m.push({path:t.path});break;case "Grep":f=`grep "${t.pattern||""}"`;break;case "LS":case "list_directory":f=`List ${t.path||"directory"}`,t.path&&m.push({path:t.path});break;case "Task":f=t.description||"Subagent task";break;case "TodoWrite":f="Update todos";break;case "ExitPlanMode":f="Exit plan mode";break;default:f=p;}return {title:f,kind:u,content:d,locations:m}}}function Le(c){return ie()(c)}function O(c){switch(c){case "claude":return ne();case "codex":return se();case "gemini":return oe();case "qwen":return ie();default:return ()=>null}}function Ot(c,e){let n=e.trim();return n?O(c)(n):null}function It(c,e){let n=[],s=e.split(`
20
- `),a=O(c);for(let o of s){let i=o.trim();if(!i)continue;let l=a(i);l&&n.push(...l);}return n}function K(){return process.env.EVOLVE_GATEWAY_URL||"https://swarmkit-gateway-692833842999.us-central1.run.app"}function je(){return `${K()}`}function Ve(){return `${K()}/e2b`}function Ke(c){return {"browser-use":{type:"http",url:`${K()}/browser_use/mcp`,headers:{"x-litellm-api-key":`Bearer ${c}`}}}}var I="EVOLVE_API_KEY",re="E2B_API_KEY",ae="DAYTONA_API_KEY",ce="MODAL_TOKEN_ID",Se="MODAL_TOKEN_SECRET",We="claude",W=36e5,Je=4,ke=100,Ye="/home/user/workspace",J="https://dashboard.evolvingmachines.ai",Ge=".evolve-sdk/observability/sessions",ze=100,He=5e3,le=3,Te=1e3;function E(c){return c!==null&&typeof c=="object"&&"safeParse"in c&&typeof c.safeParse=="function"}function N(c){return JSON.stringify(At__default.default(c,{target:"jsonSchema7"}),null,2)}function B(c){return JSON.stringify(c,null,2)}function qe(c,e=false){let n={};function s(a,o){for(let i of P__namespace.readdirSync(a)){let l=Y__namespace.join(a,i),r=o?`${o}/${i}`:i;P__namespace.statSync(l).isDirectory()?e&&s(l,r):n[r]=P__namespace.readFileSync(l);}}return s(c,""),n}function Ze(c,e){for(let[n,s]of Object.entries(e)){let a=Y__namespace.join(c,n),o=Y__namespace.dirname(a);P__namespace.mkdirSync(o,{recursive:true});let i;s instanceof ArrayBuffer||s instanceof Uint8Array?i=Buffer.from(s):i=s,P__namespace.writeFileSync(a,i);}}function Dt(c){let e=c.replace(/^~/,process.env.HOME||"");if(!P__namespace.existsSync(e))throw new Error(`OAuth file not found: ${e}`);return P__namespace.readFileSync(e,"utf-8")}function _e(c){let e=c?.type??We,n=M(e);if(c?.oauthToken){if(e!=="claude")throw new Error(`oauthToken is only supported for claude agent (Claude Max subscription), not ${e}. Use providerApiKey for ${e} instead.`);return {type:e,apiKey:c.oauthToken,isDirectMode:true,isOAuth:true,model:c.model,reasoningEffort:c.reasoningEffort,betas:c.betas}}if(c?.providerApiKey){let i=c.providerBaseUrl??process.env[n.baseUrlEnv]??n.defaultBaseUrl;return {type:e,apiKey:c.providerApiKey,baseUrl:i,isDirectMode:true,model:c.model,reasoningEffort:c.reasoningEffort,betas:c.betas}}if(c?.apiKey)return {type:e,apiKey:c.apiKey,isDirectMode:false,model:c.model,reasoningEffort:c.reasoningEffort,betas:c.betas};let s=process.env[I];if(s)return {type:e,apiKey:s,isDirectMode:false,model:c?.model,reasoningEffort:c?.reasoningEffort,betas:c?.betas};let a=process.env[n.apiKeyEnv];if(a){let i=process.env[n.baseUrlEnv]??n.defaultBaseUrl;return {type:e,apiKey:a,baseUrl:i,isDirectMode:true,model:c?.model,reasoningEffort:c?.reasoningEffort,betas:c?.betas}}if(n.oauthEnv){let i=process.env[n.oauthEnv];if(i){if(n.oauthFileName){let l=Dt(i);return {type:e,apiKey:"__oauth_file__",isDirectMode:true,isOAuth:true,oauthFileContent:l,model:c?.model,reasoningEffort:c?.reasoningEffort,betas:c?.betas}}return {type:e,apiKey:i,isDirectMode:true,isOAuth:true,model:c?.model,reasoningEffort:c?.reasoningEffort,betas:c?.betas}}}let o=n.oauthEnv?n.oauthFileName?`, or ${n.oauthEnv}`:`, oauthToken, or ${n.oauthEnv}`:"";throw new Error(`No API key found for ${e}. Set apiKey (gateway), providerApiKey (direct)${o}, or ${I} / ${n.apiKeyEnv} env var.`)}async function Ee(){let c=process.env[re];if(c)try{let{createE2BProvider:o}=await import('@evolvingmachines/e2b');return o({apiKey:c})}catch(o){let i=o;throw i.message?.includes("Cannot find module")||i.message?.includes("MODULE_NOT_FOUND")?new Error(`${re} is set but @evolvingmachines/e2b failed to load.
19
+ \`\`\``:h}});}}return {sessionUpdate:"tool_call_update",toolCallId:t,status:u?"failed":"completed",content:m}}function r(p,t){let u=Mt[p]||"other",d=[],m=[],f=p;switch(p){case "Read":case "read_file":f=`Read ${t.file_path||t.absolute_path||"file"}`,(t.file_path||t.absolute_path)&&m.push({path:t.file_path||t.absolute_path});break;case "Write":case "write_file":f=`Write ${t.file_path||"file"}`,t.file_path&&(m.push({path:t.file_path}),d.push({type:"diff",path:t.file_path,oldText:null,newText:t.content||""}));break;case "Edit":case "edit_file":f=`Edit ${t.file_path||"file"}`,t.file_path&&m.push({path:t.file_path});break;case "Bash":case "shell":case "run_shell_command":f=t.command?`\`${t.command}\``:"Run command",t.description&&d.push({type:"content",content:{type:"text",text:t.description}});break;case "WebFetch":f=t.url?`Fetch ${t.url}`:"Web fetch";break;case "WebSearch":case "brave_web_search":f=t.query?`"${t.query}"`:"Web search";break;case "Glob":f=`Find ${t.pattern||"files"}`,t.path&&m.push({path:t.path});break;case "Grep":f=`grep "${t.pattern||""}"`;break;case "LS":case "list_directory":f=`List ${t.path||"directory"}`,t.path&&m.push({path:t.path});break;case "Task":f=t.description||"Subagent task";break;case "TodoWrite":f="Update todos";break;case "ExitPlanMode":f="Exit plan mode";break;default:f=p;}return {title:f,kind:u,content:d,locations:m}}}function je(c){return ie()(c)}function O(c){switch(c){case "claude":return ne();case "codex":return se();case "gemini":return oe();case "qwen":return ie();default:return ()=>null}}function Ot(c,e){let n=e.trim();return n?O(c)(n):null}function It(c,e){let n=[],s=e.split(`
20
+ `),a=O(c);for(let o of s){let i=o.trim();if(!i)continue;let l=a(i);l&&n.push(...l);}return n}function K(){return process.env.EVOLVE_GATEWAY_URL||"https://swarmkit-gateway-692833842999.us-central1.run.app"}function Le(){return `${K()}`}function Ve(){return `${K()}/e2b`}function Ke(c){return {"browser-use":{type:"http",url:`${K()}/browser_use/mcp`,headers:{"x-litellm-api-key":`Bearer ${c}`}}}}var I="EVOLVE_API_KEY",re="E2B_API_KEY",ae="DAYTONA_API_KEY",ce="MODAL_TOKEN_ID",Se="MODAL_TOKEN_SECRET",We="claude",W=36e5,Je=4,ke=100,Ye="/home/user/workspace",J="https://dashboard.evolvingmachines.ai",Ge=".evolve-sdk/observability/sessions",ze=100,He=5e3,le=3,_e=1e3;function P(c){return c!==null&&typeof c=="object"&&"safeParse"in c&&typeof c.safeParse=="function"}function N(c){return JSON.stringify(At__default.default(c,{target:"jsonSchema7"}),null,2)}function B(c){return JSON.stringify(c,null,2)}function qe(c,e=false){let n={};function s(a,o){for(let i of E__namespace.readdirSync(a)){let l=Y__namespace.join(a,i),r=o?`${o}/${i}`:i;E__namespace.statSync(l).isDirectory()?e&&s(l,r):n[r]=E__namespace.readFileSync(l);}}return s(c,""),n}function Ze(c,e){for(let[n,s]of Object.entries(e)){let a=Y__namespace.join(c,n),o=Y__namespace.dirname(a);E__namespace.mkdirSync(o,{recursive:true});let i;s instanceof ArrayBuffer||s instanceof Uint8Array?i=Buffer.from(s):i=s,E__namespace.writeFileSync(a,i);}}function Dt(c){let e=c.replace(/^~/,process.env.HOME||"");if(!E__namespace.existsSync(e))throw new Error(`OAuth file not found: ${e}`);return E__namespace.readFileSync(e,"utf-8")}function Te(c){let e=c?.type??We,n=M(e);if(c?.oauthToken){if(e!=="claude")throw new Error(`oauthToken is only supported for claude agent (Claude Max subscription), not ${e}. Use providerApiKey for ${e} instead.`);return {type:e,apiKey:c.oauthToken,isDirectMode:true,isOAuth:true,model:c.model,reasoningEffort:c.reasoningEffort,betas:c.betas}}if(c?.providerApiKey){let i=c.providerBaseUrl??process.env[n.baseUrlEnv]??n.defaultBaseUrl;return {type:e,apiKey:c.providerApiKey,baseUrl:i,isDirectMode:true,model:c.model,reasoningEffort:c.reasoningEffort,betas:c.betas}}if(c?.apiKey)return {type:e,apiKey:c.apiKey,isDirectMode:false,model:c.model,reasoningEffort:c.reasoningEffort,betas:c.betas};let s=process.env[I];if(s)return {type:e,apiKey:s,isDirectMode:false,model:c?.model,reasoningEffort:c?.reasoningEffort,betas:c?.betas};let a=process.env[n.apiKeyEnv];if(a){let i=process.env[n.baseUrlEnv]??n.defaultBaseUrl;return {type:e,apiKey:a,baseUrl:i,isDirectMode:true,model:c?.model,reasoningEffort:c?.reasoningEffort,betas:c?.betas}}if(n.oauthEnv){let i=process.env[n.oauthEnv];if(i){if(n.oauthFileName){let l=Dt(i);return {type:e,apiKey:"__oauth_file__",isDirectMode:true,isOAuth:true,oauthFileContent:l,model:c?.model,reasoningEffort:c?.reasoningEffort,betas:c?.betas}}return {type:e,apiKey:i,isDirectMode:true,isOAuth:true,model:c?.model,reasoningEffort:c?.reasoningEffort,betas:c?.betas}}}let o=n.oauthEnv?n.oauthFileName?`, or ${n.oauthEnv}`:`, oauthToken, or ${n.oauthEnv}`:"";throw new Error(`No API key found for ${e}. Set apiKey (gateway), providerApiKey (direct)${o}, or ${I} / ${n.apiKeyEnv} env var.`)}async function Pe(){let c=process.env[re];if(c)try{let{createE2BProvider:o}=await import('@evolvingmachines/e2b');return o({apiKey:c})}catch(o){let i=o;throw i.message?.includes("Cannot find module")||i.message?.includes("MODULE_NOT_FOUND")?new Error(`${re} is set but @evolvingmachines/e2b failed to load.
21
21
  Try reinstalling: npm install @evolvingmachines/sdk`):i}let e=process.env[ae];if(e)try{let{createDaytonaProvider:o}=await import('@evolvingmachines/daytona');return o({apiKey:e})}catch(o){let i=o;throw i.message?.includes("Cannot find module")||i.message?.includes("MODULE_NOT_FOUND")?new Error(`${ae} is set but @evolvingmachines/daytona failed to load.
22
22
  Try installing: npm install @evolvingmachines/daytona`):i}let n=process.env[ce],s=process.env[Se];if(n&&s)try{let{createModalProvider:o}=await import('@evolvingmachines/modal');return o({tokenId:n,tokenSecret:s})}catch(o){let i=o;throw i.message?.includes("Cannot find module")||i.message?.includes("MODULE_NOT_FOUND")?new Error(`${ce} is set but @evolvingmachines/modal failed to load.
23
23
  Try installing: npm install @evolvingmachines/modal`):i}let a=process.env[I];if(a)try{let{createE2BProvider:o}=await import('@evolvingmachines/e2b');return process.env.E2B_API_URL=Ve(),o({apiKey:a})}catch(o){let i=o;throw i.message?.includes("Cannot find module")||i.message?.includes("MODULE_NOT_FOUND")?new Error(`${I} is set but @evolvingmachines/e2b failed to load.
@@ -26,7 +26,7 @@ Try reinstalling: npm install @evolvingmachines/sdk`):i}throw new Error(`No sand
26
26
  2. Set ${re} environment variable (direct E2B access, get key at https://e2b.dev)
27
27
  3. Set ${ae} environment variable (direct Daytona access, get key at https://app.daytona.io)
28
28
  4. Set ${ce} and ${Se} environment variables (direct Modal access, get tokens at https://modal.com/settings/tokens)
29
- 5. Pass sandbox explicitly: .withSandbox(provider)`)}function Ft(c){return new Promise(e=>setTimeout(e,c))}async function T(c,e,n=0){let s=$t(e),a=e?.onItemRetry,o=null,i=0,l=s.backoffMs;for(;i<s.maxAttempts;){if(i++,o=await c(i),!s.retryOn(o))return o;if(i>=s.maxAttempts)break;if(a){let r=o.error??"Unknown error";a(n,i,r);}await Ft(l),l*=s.backoffMultiplier;}return o}function $t(c){return {maxAttempts:c?.maxAttempts??3,backoffMs:c?.backoffMs??1e3,backoffMultiplier:c?.backoffMultiplier??2,retryOn:c?.retryOn??(e=>e.status==="error")}}var Qe=`## FILESYSTEM INSTRUCTIONS:
29
+ 5. Pass sandbox explicitly: .withSandbox(provider)`)}function Ft(c){return new Promise(e=>setTimeout(e,c))}async function _(c,e,n=0){let s=$t(e),a=e?.onItemRetry,o=null,i=0,l=s.backoffMs;for(;i<s.maxAttempts;){if(i++,o=await c(i),!s.retryOn(o))return o;if(i>=s.maxAttempts)break;if(a){let r=o.error??"Unknown error";a(n,i,r);}await Ft(l),l*=s.backoffMultiplier;}return o}function $t(c){return {maxAttempts:c?.maxAttempts??3,backoffMs:c?.backoffMs??1e3,backoffMultiplier:c?.backoffMultiplier??2,retryOn:c?.retryOn??(e=>e.status==="error")}}var Qe=`## FILESYSTEM INSTRUCTIONS:
30
30
 
31
31
  You are running in a sandbox environment.
32
32
 
@@ -43,15 +43,22 @@ IMPORTANT - Directory structure:
43
43
  \u2514\u2500\u2500 output/ # Where you can save final deliverables
44
44
  \`\`\`
45
45
 
46
- ### OUTPUT RESULTS (DELIVERABLES) MUST BE WRITTEN to \`output/\` as files.
47
- ### Never just state results as text.
46
+ ### OUTPUT RULES:
48
47
 
49
- - The file system is being used as the main communication channel between the agent (you) and the user.
50
- - Hence, all outputs results must be saved to the \`output/\` folder as files. The user will only be able to view the files in the \`output/\` folder.
51
- - There are several reasons why we need to save the results to the \`output/\` folder:
52
- - The user might be building a long-running agent which could be interrupted and resumed later.
53
- - The user might pass the outputs to another agent for verification or further processing.
54
- - etc.
48
+ The \`output/\` folder is how you deliver files to the user\u2014the SDK retrieves everything from there.
49
+
50
+ Use your best judgment:
51
+ - **Questions, explanations, conversation** \u2192 respond in chat
52
+ - **Artifacts** (files, documents, code, charts, data) \u2192 save to \`output/\`
53
+
54
+ Examples:
55
+ - "What is a binary search?" \u2192 chat
56
+ - "Summarize this document" \u2192 chat
57
+ - "Create an Excel report" \u2192 \`output/sales_report.xlsx\`
58
+ - "Build an HTML dashboard" \u2192 \`output/dashboard.html\`
59
+ - "Generate a bar chart" \u2192 \`output/revenue_chart.png\`
60
+ - "Write a Python script" \u2192 \`output/parser.py\`
61
+ - "Convert this to JSON" \u2192 \`output/data.json\`
55
62
  `;var Xe=`## FILESYSTEM INSTRUCTIONS:
56
63
 
57
64
  You are running in a sandbox environment.
@@ -70,15 +77,22 @@ IMPORTANT - Directory structure:
70
77
  \u2514\u2500\u2500 output/ # Final deliverables
71
78
  \`\`\`
72
79
 
73
- ### OUTPUT RESULTS (DELIVERABLES) MUST BE WRITTEN to \`output/\` as files.
74
- ### Never just state results as text.
80
+ ### OUTPUT RULES:
81
+
82
+ The \`output/\` folder is how you deliver files to the user\u2014the SDK retrieves everything from there.
83
+
84
+ Use your best judgment:
85
+ - **Questions, explanations, conversation** \u2192 respond in chat
86
+ - **Artifacts** (files, documents, code, charts, data) \u2192 save to \`output/\`
75
87
 
76
- - The file system is being used as the main communication channel between the agent (you) and the user.
77
- - Hence, all outputs results must be saved to the \`output/\` folder as files. The user will only be able to view the files in the \`output/\` folder.
78
- - There are several reasons why we need to save the results to the \`output/\` folder:
79
- - The user might be building a long-running agent which could be interrupted and resumed later.
80
- - The user might pass the outputs to another agent for verification or further processing.
81
- - etc.
88
+ Examples:
89
+ - "What is a binary search?" \u2192 chat
90
+ - "Summarize this document" \u2192 chat
91
+ - "Create an Excel report" \u2192 \`output/sales_report.xlsx\`
92
+ - "Build an HTML dashboard" \u2192 \`output/dashboard.html\`
93
+ - "Generate a bar chart" \u2192 \`output/revenue_chart.png\`
94
+ - "Write a Python script" \u2192 \`output/parser.py\`
95
+ - "Convert this to JSON" \u2192 \`output/data.json\`
82
96
  `;var et=`## SYSTEM PROMPT
83
97
 
84
98
  {{systemPrompt}}`;var tt=`## STRUCTURED OUTPUT
@@ -178,10 +192,10 @@ Your previous attempt did not pass quality verification. Please address this fee
178
192
  {{feedback}}
179
193
 
180
194
  Make sure your output addresses all the feedback points above.
181
- `;var ct=Qe,lt=Xe,pt=et,ut=tt,Pe=nt,dt=it,Re=st,mt=rt,ft=ot,Me=at;function R(c,e){let n=c;for(let[s,a]of Object.entries(e))n=n.replace(new RegExp(`\\{\\{${s}\\}\\}`,"g"),a);return n}function Oe(c){let e=c.mode==="swe"?lt:ct,n=R(e,{workingDir:c.workingDir}).trim();if(c.systemPrompt&&(n+=`
195
+ `;var ct=Qe,lt=Xe,pt=et,ut=tt,Ee=nt,dt=it,Re=st,mt=rt,ft=ot,Me=at;function R(c,e){let n=c;for(let[s,a]of Object.entries(e))n=n.replace(new RegExp(`\\{\\{${s}\\}\\}`,"g"),a);return n}function Oe(c){let e=c.mode==="swe"?lt:ct,n=R(e,{workingDir:c.workingDir}).trim();if(c.systemPrompt&&(n+=`
182
196
 
183
197
 
184
- ${R(pt,{systemPrompt:c.systemPrompt}).trim()}`),c.schema){let s=E(c.schema)?N(c.schema):B(c.schema);n+=`
198
+ ${R(pt,{systemPrompt:c.systemPrompt}).trim()}`),c.schema){let s=P(c.schema)?N(c.schema):B(c.schema);n+=`
185
199
 
186
200
 
187
201
  ${R(ut,{schema:s}).trim()}`;}return n}function ue(c){let e=[...new Set(Object.keys(c).map(r=>r.split("/")[0]))].sort((r,p)=>r==="worker_task"?-1:p==="worker_task"?1:r.localeCompare(p));if(!e.length)return `context/
@@ -190,12 +204,12 @@ ${R(ut,{schema:s}).trim()}`;}return n}function ue(c){let e=[...new Set(Object.ke
190
204
  `)?e:e+`
191
205
  `;this.localQueue=this.localQueue.then(()=>this.appendLocal(n)).catch(()=>{});}bufferForDashboard(e){this.eventBuffer.push(e),this.scheduleFlush();}write(e){this.metaWritten||this.writeMeta();let n=JSON.stringify(e)+`
192
206
  `;this.localQueue=this.localQueue.then(()=>this.appendLocal(n)).catch(()=>{}),this.apiKey&&(this.eventBuffer.push(e),this.scheduleFlush());}writeMeta(){let e={_meta:{tag:this.tag,provider:this.provider,agent:this.agent,model:this.model,sandbox_id:this.sandboxId,timestamp:this.timestamp,...this.filterUndefined(this.observability)}},n=JSON.stringify(e)+`
193
- `;this.localQueue=this.localQueue.then(()=>this.appendLocal(n)).catch(()=>{}),this.apiKey&&this.eventBuffer.push(e),this.metaWritten=true;}async appendLocal(e){try{await this.dirReady,await promises.appendFile(this.localFilePath,e,"utf-8");}catch(n){console.debug("[SessionLogger] Local write failed:",n);}}scheduleFlush(){if(this.eventBuffer.length>=ze){this.flushDashboard();return}!this.flushTimer&&!this.isClosed&&(this.flushTimer=setTimeout(()=>{this.flushTimer=void 0,this.flushDashboard();},He));}flushDashboard(){if(!this.apiKey||this.eventBuffer.length===0)return;let e=this.eventBuffer.splice(0);this.dashboardQueue=this.dashboardQueue.then(()=>this.sendToDashboard(e)).catch(()=>{});}async sendToDashboard(e){let n={tag:this.tag,provider:this.provider,agent:this.agent,model:this.model,sandboxId:this.sandboxId,timestamp:this.timestamp,...this.filterUndefined(this.observability),events:e};for(let s=1;s<=le;s++)try{let a=await fetch(`${this.dashboardUrl}/api/sessions/ingest`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`},body:JSON.stringify(n),signal:AbortSignal.timeout(1e4)});if(a.ok)return;if(a.status===429||a.status===401||a.status>=500){if(s===le){console.debug(`[SessionLogger] Dashboard ${a.status} after ${s} retries, requeueing`),this.requeueEvents(e);return}await this.delay(Te*s);continue}console.debug(`[SessionLogger] Dashboard ${a.status}, dropping events`);return}catch(a){s===le?(console.debug("[SessionLogger] Dashboard sync failed after retries, requeueing:",a),this.requeueEvents(e)):await this.delay(Te*s);}}requeueEvents(e){this.eventBuffer.unshift(...e);}validateConfig(e){if(e.apiKey&&!J.startsWith("https://")&&!J.includes("localhost")&&!J.includes("127.0.0.1"))throw new Error("Dashboard URL must use HTTPS when API key is provided")}delay(e){return new Promise(n=>setTimeout(n,e))}filterUndefined(e){return e?Object.fromEntries(Object.entries(e).filter(([,n])=>n!==void 0)):{}}};var de="COMPOSIO_API_KEY";function yt(c){return new core.Composio({apiKey:process.env[de]})}function Ie(c){if(!c?.trim())throw new Error("userId is required and cannot be empty")}async function me(c,e,n){Ie(c);let s=yt(),a={};return e?.toolkits&&(a.toolkits=e.toolkits),e?.tools&&(a.tools=e.tools),e?.authConfigs&&(a.authConfigs=e.authConfigs),e?.connectedAccounts&&(a.connectedAccounts=e.connectedAccounts),e?.manageConnections!==void 0&&(a.manageConnections=e.manageConnections),await s.create(c,Object.keys(a).length>0?a:void 0)}async function vt(c,e,n,s){Ie(c);let a=yt(),l=(await a.toolkits.get(e)).authConfigDetails?.find(r=>r.mode==="API_KEY");if(!l)throw new Error(`Toolkit "${e}" does not support API key authentication. Get an OAuth URL via Evolve.composio.auth() instead.`);await a.connectedAccounts.initiate(c,l.name,{config:core.AuthScheme.APIKey({api_key:n})});}async function Ae(c,e){if(Ie(c),e?.keys)for(let[s,a]of Object.entries(e.keys))try{await vt(c,s,a);}catch(o){throw new Error(`Failed to create API key connection for "${s}": ${o.message}`)}let n=await me(c,{toolkits:e?.toolkits,tools:e?.tools,authConfigs:e?.authConfigs,connectedAccounts:e?.connectedAccounts,manageConnections:e?.manageConnections});return {url:n.mcp.url,headers:n.mcp.headers}}function xt(c){if(!c?.trim())throw new Error("userId is required and cannot be empty")}function en(){return new core.Composio({apiKey:process.env[de]})}async function bt(c,e){xt(c);let s=await(await me(c,{manageConnections:false})).authorize(e);return {url:s.redirectUrl,connectionId:s.id}}async function wt(c,e){let n=await De(c),s={};for(let a of n)s[a.toolkit]=a.connected;return e?s[e]??false:s}async function De(c){return xt(c),(await en().connectedAccounts.list({userIds:[c],statuses:["ACTIVE"]})).items.map(s=>({toolkit:s.toolkit.slug,connected:s.status==="ACTIVE",accountId:s.id}))}var Fe={auth:bt,status:wt,connections:De};function nn(c){return c.replace(/\\/g,"\\\\").replace(/"/g,'\\"').replace(/\$/g,"\\$").replace(/`/g,"\\`")}var z=class{agentConfig;options;sandbox;hasRun=false;workingDir;lastRunTimestamp;registry;sessionLogger;skills;zodSchema;jsonSchema;schemaOptions;compiledValidator;constructor(e,n={}){if(this.agentConfig=e,this.options=n,this.workingDir=n.workingDirectory||Ye,this.skills=n.skills,n.schema)if(E(n.schema))this.zodSchema=n.schema,n.schemaOptions&&console.warn("[Evolve] schemaOptions ignored for Zod schemas - use .passthrough(), .strip(), z.coerce instead");else {this.jsonSchema=n.schema,this.schemaOptions=n.schemaOptions;try{let s=this.createAjvValidator();this.compiledValidator=s.compile(this.jsonSchema);}catch(s){throw new Error(`Invalid JSON Schema: ${s.message}`)}}this.registry=M(e.type);}createAjvValidator(){let n={...he[this.schemaOptions?.mode||"loose"],...this.schemaOptions?.coerceTypes!==void 0&&{coerceTypes:this.schemaOptions.coerceTypes},...this.schemaOptions?.removeAdditional!==void 0&&{removeAdditional:this.schemaOptions.removeAdditional},...this.schemaOptions?.useDefaults!==void 0&&{useDefaults:this.schemaOptions.useDefaults},...this.schemaOptions?.allErrors!==void 0&&{allErrors:this.schemaOptions.allErrors}};return new tn__default.default({...n,strict:false})}async getSandbox(){if(this.sandbox)return this.sandbox;if(!this.options.sandboxProvider)throw new Error("No sandbox provider configured");let e=this.options.sandboxProvider;if(this.options.sandboxId)(this.options.mcpServers||this.options.context||this.options.files||this.options.systemPrompt)&&console.warn("[Evolve] Connecting to existing sandbox - ignoring mcpServers, context, files, and systemPrompt"),this.sandbox=await e.connect(this.options.sandboxId),this.hasRun=true;else {let n=this.buildEnvironmentVariables();this.sandbox=await e.create({envs:n,workingDirectory:this.workingDir}),await this.setupAgentAuth(this.sandbox),await this.setupWorkspace(this.sandbox);}return this.sandbox}buildEnvironmentVariables(){let e={};if(this.agentConfig.oauthFileContent)this.registry.oauthActivationEnv&&(e[this.registry.oauthActivationEnv.key]=this.registry.oauthActivationEnv.value);else {let n=this.agentConfig.isOAuth&&this.registry.oauthEnv?this.registry.oauthEnv:this.registry.apiKeyEnv;e[n]=this.agentConfig.apiKey;}return this.agentConfig.isDirectMode&&!this.agentConfig.isOAuth?this.agentConfig.baseUrl&&(e[this.registry.baseUrlEnv]=this.agentConfig.baseUrl):this.agentConfig.isDirectMode||(e[this.registry.baseUrlEnv]=this.registry.usePassthroughGateway?je():K(),e.EVOLVE_API_KEY=this.agentConfig.apiKey),this.options.secrets&&Object.assign(e,this.options.secrets),e}async setupAgentAuth(e){if(this.agentConfig.oauthFileContent&&this.registry.oauthFileName){let n=this.registry.mcpConfig.settingsDir.replace(/^~/,"/home/user");await e.files.makeDir(n),await e.files.write(`${n}/${this.registry.oauthFileName}`,this.agentConfig.oauthFileContent);return}this.registry.setupCommand&&await e.commands.run(this.registry.setupCommand,{timeoutMs:3e4});}async setupWorkspace(e){let n=this.options.workspaceMode||"knowledge",s=n==="swe"?`${this.workingDir}/repo ${this.workingDir}/context ${this.workingDir}/scripts ${this.workingDir}/temp ${this.workingDir}/output`:`${this.workingDir}/context ${this.workingDir}/scripts ${this.workingDir}/temp ${this.workingDir}/output`;await e.commands.run(`mkdir -p ${s}`,{timeoutMs:3e4});let a=Oe({workingDir:this.workingDir,systemPrompt:this.options.systemPrompt,schema:this.zodSchema||this.jsonSchema,mode:n}),o=`${this.workingDir}/${this.registry.systemPromptFile}`;await e.files.write(o,a),this.options.context&&await this.uploadContextFiles(e,this.options.context),this.options.files&&await this.uploadWorkspaceFiles(e,this.options.files);let i={...this.options.mcpServers};if(this.options.composio){let l=await Ae(this.options.composio.userId,this.options.composio.config);i={...i,composio:{type:"http",url:l.url,headers:l.headers}};}Object.keys(i).length>0&&await Ce(this.agentConfig.type,e,this.workingDir,i),this.skills?.length&&await this.setupSkills(e);}async setupSkills(e){if(!this.skills?.length)return;let{skillsConfig:n}=this.registry,{sourceDir:s,targetDir:a}=n;await e.files.makeDir(a);for(let o of this.skills){let i=`cp -r ${s}/${o} ${a}/ 2>/dev/null || true`;await e.commands.run(i,{timeoutMs:3e4});}}async uploadContextFiles(e,n){let s=Object.entries(n).map(([o,i])=>({path:`${this.workingDir}/context/${o}`,data:i}));if(s.length===0)return;let a=new Set(s.map(o=>o.path.substring(0,o.path.lastIndexOf("/"))).filter(Boolean));a.size>0&&await e.commands.run(`mkdir -p ${Array.from(a).join(" ")}`,{timeoutMs:3e4}),await e.files.writeBatch(s);}async uploadWorkspaceFiles(e,n){let s=Object.entries(n).map(([o,i])=>({path:o.startsWith("/")?o:`${this.workingDir}/${o}`,data:i}));if(s.length===0)return;let a=new Set(s.map(o=>o.path.substring(0,o.path.lastIndexOf("/"))).filter(Boolean));a.size>0&&await e.commands.run(`mkdir -p ${Array.from(a).join(" ")}`,{timeoutMs:3e4}),await e.files.writeBatch(s);}buildCommand(e){return this.registry.buildCommand({prompt:nn(e),model:this.agentConfig.model||this.registry.defaultModel,isResume:this.hasRun,reasoningEffort:this.agentConfig.reasoningEffort,betas:this.agentConfig.betas,isDirectMode:this.agentConfig.isDirectMode,skills:this.skills})}async run(e,n){let{prompt:s,timeoutMs:a=W,background:o=false}=e,i=await this.getSandbox();if(this.lastRunTimestamp=Date.now(),!this.sessionLogger){let m=this.options.sandboxProvider;this.sessionLogger=new G({provider:m?.name||m?.providerType||"unknown",agent:this.agentConfig.type,model:this.agentConfig.model||this.registry.defaultModel,sandboxId:i.sandboxId,tagPrefix:this.options.sessionTagPrefix,apiKey:this.agentConfig.isDirectMode?void 0:this.agentConfig.apiKey,observability:this.options.observability});}this.sessionLogger.writePrompt(s);let l=this.buildCommand(s),r="",p=O(this.agentConfig.type),t=m=>{r+=m;let f=r.split(`
207
+ `;this.localQueue=this.localQueue.then(()=>this.appendLocal(n)).catch(()=>{}),this.apiKey&&this.eventBuffer.push(e),this.metaWritten=true;}async appendLocal(e){try{await this.dirReady,await promises.appendFile(this.localFilePath,e,"utf-8");}catch(n){console.debug("[SessionLogger] Local write failed:",n);}}scheduleFlush(){if(this.eventBuffer.length>=ze){this.flushDashboard();return}!this.flushTimer&&!this.isClosed&&(this.flushTimer=setTimeout(()=>{this.flushTimer=void 0,this.flushDashboard();},He));}flushDashboard(){if(!this.apiKey||this.eventBuffer.length===0)return;let e=this.eventBuffer.splice(0);this.dashboardQueue=this.dashboardQueue.then(()=>this.sendToDashboard(e)).catch(()=>{});}async sendToDashboard(e){let n={tag:this.tag,provider:this.provider,agent:this.agent,model:this.model,sandboxId:this.sandboxId,timestamp:this.timestamp,...this.filterUndefined(this.observability),events:e};for(let s=1;s<=le;s++)try{let a=await fetch(`${this.dashboardUrl}/api/sessions/ingest`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`},body:JSON.stringify(n),signal:AbortSignal.timeout(1e4)});if(a.ok)return;if(a.status===429||a.status===401||a.status>=500){if(s===le){console.debug(`[SessionLogger] Dashboard ${a.status} after ${s} retries, requeueing`),this.requeueEvents(e);return}await this.delay(_e*s);continue}console.debug(`[SessionLogger] Dashboard ${a.status}, dropping events`);return}catch(a){s===le?(console.debug("[SessionLogger] Dashboard sync failed after retries, requeueing:",a),this.requeueEvents(e)):await this.delay(_e*s);}}requeueEvents(e){this.eventBuffer.unshift(...e);}validateConfig(e){if(e.apiKey&&!J.startsWith("https://")&&!J.includes("localhost")&&!J.includes("127.0.0.1"))throw new Error("Dashboard URL must use HTTPS when API key is provided")}delay(e){return new Promise(n=>setTimeout(n,e))}filterUndefined(e){return e?Object.fromEntries(Object.entries(e).filter(([,n])=>n!==void 0)):{}}};var de="COMPOSIO_API_KEY";function yt(c){return new core.Composio({apiKey:process.env[de]})}function Ie(c){if(!c?.trim())throw new Error("userId is required and cannot be empty")}async function me(c,e,n){Ie(c);let s=yt(),a={};return e?.toolkits&&(a.toolkits=e.toolkits),e?.tools&&(a.tools=e.tools),e?.authConfigs&&(a.authConfigs=e.authConfigs),e?.connectedAccounts&&(a.connectedAccounts=e.connectedAccounts),e?.manageConnections!==void 0&&(a.manageConnections=e.manageConnections),await s.create(c,Object.keys(a).length>0?a:void 0)}async function vt(c,e,n,s){Ie(c);let a=yt(),l=(await a.toolkits.get(e)).authConfigDetails?.find(r=>r.mode==="API_KEY");if(!l)throw new Error(`Toolkit "${e}" does not support API key authentication. Get an OAuth URL via Evolve.composio.auth() instead.`);await a.connectedAccounts.initiate(c,l.name,{config:core.AuthScheme.APIKey({api_key:n})});}async function Ae(c,e){if(Ie(c),e?.keys)for(let[s,a]of Object.entries(e.keys))try{await vt(c,s,a);}catch(o){throw new Error(`Failed to create API key connection for "${s}": ${o.message}`)}let n=await me(c,{toolkits:e?.toolkits,tools:e?.tools,authConfigs:e?.authConfigs,connectedAccounts:e?.connectedAccounts,manageConnections:e?.manageConnections});return {url:n.mcp.url,headers:n.mcp.headers}}function xt(c){if(!c?.trim())throw new Error("userId is required and cannot be empty")}function en(){return new core.Composio({apiKey:process.env[de]})}async function bt(c,e){xt(c);let s=await(await me(c,{manageConnections:false})).authorize(e);return {url:s.redirectUrl,connectionId:s.id}}async function Ct(c,e){let n=await De(c),s={};for(let a of n)s[a.toolkit]=a.connected;return e?s[e]??false:s}async function De(c){return xt(c),(await en().connectedAccounts.list({userIds:[c],statuses:["ACTIVE"]})).items.map(s=>({toolkit:s.toolkit.slug,connected:s.status==="ACTIVE",accountId:s.id}))}var Fe={auth:bt,status:Ct,connections:De};function nn(c){return c.replace(/\\/g,"\\\\").replace(/"/g,'\\"').replace(/\$/g,"\\$").replace(/`/g,"\\`")}var z=class{agentConfig;options;sandbox;hasRun=false;workingDir;lastRunTimestamp;registry;sessionLogger;skills;zodSchema;jsonSchema;schemaOptions;compiledValidator;constructor(e,n={}){if(this.agentConfig=e,this.options=n,this.workingDir=n.workingDirectory||Ye,this.skills=n.skills,n.schema)if(P(n.schema))this.zodSchema=n.schema,n.schemaOptions&&console.warn("[Evolve] schemaOptions ignored for Zod schemas - use .passthrough(), .strip(), z.coerce instead");else {this.jsonSchema=n.schema,this.schemaOptions=n.schemaOptions;try{let s=this.createAjvValidator();this.compiledValidator=s.compile(this.jsonSchema);}catch(s){throw new Error(`Invalid JSON Schema: ${s.message}`)}}this.registry=M(e.type);}createAjvValidator(){let n={...he[this.schemaOptions?.mode||"loose"],...this.schemaOptions?.coerceTypes!==void 0&&{coerceTypes:this.schemaOptions.coerceTypes},...this.schemaOptions?.removeAdditional!==void 0&&{removeAdditional:this.schemaOptions.removeAdditional},...this.schemaOptions?.useDefaults!==void 0&&{useDefaults:this.schemaOptions.useDefaults},...this.schemaOptions?.allErrors!==void 0&&{allErrors:this.schemaOptions.allErrors}};return new tn__default.default({...n,strict:false})}async getSandbox(){if(this.sandbox)return this.sandbox;if(!this.options.sandboxProvider)throw new Error("No sandbox provider configured");let e=this.options.sandboxProvider;if(this.options.sandboxId)(this.options.mcpServers||this.options.context||this.options.files||this.options.systemPrompt)&&console.warn("[Evolve] Connecting to existing sandbox - ignoring mcpServers, context, files, and systemPrompt"),this.sandbox=await e.connect(this.options.sandboxId),this.hasRun=true;else {let n=this.buildEnvironmentVariables();this.sandbox=await e.create({envs:n,workingDirectory:this.workingDir}),await this.setupAgentAuth(this.sandbox),await this.setupWorkspace(this.sandbox);}return this.sandbox}buildEnvironmentVariables(){let e={};if(this.agentConfig.oauthFileContent)this.registry.oauthActivationEnv&&(e[this.registry.oauthActivationEnv.key]=this.registry.oauthActivationEnv.value);else {let n=this.agentConfig.isOAuth&&this.registry.oauthEnv?this.registry.oauthEnv:this.registry.apiKeyEnv;e[n]=this.agentConfig.apiKey;}return this.agentConfig.isDirectMode&&!this.agentConfig.isOAuth?this.agentConfig.baseUrl&&(e[this.registry.baseUrlEnv]=this.agentConfig.baseUrl):this.agentConfig.isDirectMode||(e[this.registry.baseUrlEnv]=this.registry.usePassthroughGateway?Le():K(),e.EVOLVE_API_KEY=this.agentConfig.apiKey),this.options.secrets&&Object.assign(e,this.options.secrets),e}async setupAgentAuth(e){if(this.agentConfig.oauthFileContent&&this.registry.oauthFileName){let n=this.registry.mcpConfig.settingsDir.replace(/^~/,"/home/user");await e.files.makeDir(n),await e.files.write(`${n}/${this.registry.oauthFileName}`,this.agentConfig.oauthFileContent);return}this.registry.setupCommand&&await e.commands.run(this.registry.setupCommand,{timeoutMs:3e4});}async setupWorkspace(e){let n=this.options.workspaceMode||"knowledge",s=n==="swe"?`${this.workingDir}/repo ${this.workingDir}/context ${this.workingDir}/scripts ${this.workingDir}/temp ${this.workingDir}/output`:`${this.workingDir}/context ${this.workingDir}/scripts ${this.workingDir}/temp ${this.workingDir}/output`;await e.commands.run(`mkdir -p ${s}`,{timeoutMs:3e4});let a=Oe({workingDir:this.workingDir,systemPrompt:this.options.systemPrompt,schema:this.zodSchema||this.jsonSchema,mode:n}),o=`${this.workingDir}/${this.registry.systemPromptFile}`;await e.files.write(o,a),this.options.context&&await this.uploadContextFiles(e,this.options.context),this.options.files&&await this.uploadWorkspaceFiles(e,this.options.files);let i={...this.options.mcpServers};if(this.options.composio){let l=await Ae(this.options.composio.userId,this.options.composio.config);i={...i,composio:{type:"http",url:l.url,headers:l.headers}};}Object.keys(i).length>0&&await we(this.agentConfig.type,e,this.workingDir,i),this.skills?.length&&await this.setupSkills(e);}async setupSkills(e){if(!this.skills?.length)return;let{skillsConfig:n}=this.registry,{sourceDir:s,targetDir:a}=n;await e.files.makeDir(a);for(let o of this.skills){let i=`cp -r ${s}/${o} ${a}/ 2>/dev/null || true`;await e.commands.run(i,{timeoutMs:3e4});}}async uploadContextFiles(e,n){let s=Object.entries(n).map(([o,i])=>({path:`${this.workingDir}/context/${o}`,data:i}));if(s.length===0)return;let a=new Set(s.map(o=>o.path.substring(0,o.path.lastIndexOf("/"))).filter(Boolean));a.size>0&&await e.commands.run(`mkdir -p ${Array.from(a).join(" ")}`,{timeoutMs:3e4}),await e.files.writeBatch(s);}async uploadWorkspaceFiles(e,n){let s=Object.entries(n).map(([o,i])=>({path:o.startsWith("/")?o:`${this.workingDir}/${o}`,data:i}));if(s.length===0)return;let a=new Set(s.map(o=>o.path.substring(0,o.path.lastIndexOf("/"))).filter(Boolean));a.size>0&&await e.commands.run(`mkdir -p ${Array.from(a).join(" ")}`,{timeoutMs:3e4}),await e.files.writeBatch(s);}buildCommand(e){return this.registry.buildCommand({prompt:nn(e),model:this.agentConfig.model||this.registry.defaultModel,isResume:this.hasRun,reasoningEffort:this.agentConfig.reasoningEffort,betas:this.agentConfig.betas,isDirectMode:this.agentConfig.isDirectMode,skills:this.skills})}async run(e,n){let{prompt:s,timeoutMs:a=W,background:o=false}=e,i=await this.getSandbox();if(this.lastRunTimestamp=Date.now(),!this.sessionLogger){let m=this.options.sandboxProvider;this.sessionLogger=new G({provider:m?.name||m?.providerType||"unknown",agent:this.agentConfig.type,model:this.agentConfig.model||this.registry.defaultModel,sandboxId:i.sandboxId,tagPrefix:this.options.sessionTagPrefix,apiKey:this.agentConfig.isDirectMode?void 0:this.agentConfig.apiKey,observability:this.options.observability});}this.sessionLogger.writePrompt(s);let l=this.buildCommand(s),r="",p=O(this.agentConfig.type),t=m=>{r+=m;let f=r.split(`
194
208
  `);r=f.pop()??"";for(let h of f){if(!h.trim())continue;let g=p(h);if(this.sessionLogger?.writeEventParsed(h,g),n?.onStdout?.(h+`
195
209
  `),g&&n?.onContent)for(let y of g)n.onContent(y);}},u=m=>{n?.onStderr?.(m);};if(o){let m=await i.commands.spawn(l,{cwd:this.workingDir,timeoutMs:a,onStdout:t,onStderr:u});return this.hasRun=true,{sandboxId:i.sandboxId,exitCode:0,stdout:`Background process started with ID ${m.processId}`,stderr:""}}let d=await i.commands.run(l,{cwd:this.workingDir,timeoutMs:a,onStdout:t,onStderr:u});if(r.trim()){let m=p(r);if(this.sessionLogger?.writeEventParsed(r,m),n?.onStdout?.(r+`
196
210
  `),m&&n?.onContent)for(let f of m)n.onContent(f);}return this.hasRun=true,{sandboxId:i.sandboxId,exitCode:d.exitCode,stdout:d.stdout,stderr:d.stderr}}async executeCommand(e,n={},s){let{timeoutMs:a=W,background:o=false}=n,i=await this.getSandbox();this.lastRunTimestamp=Date.now();let l="",r="",p=d=>{l+=d,s?.onStdout?.(d);},t=d=>{r+=d,s?.onStderr?.(d);};if(o){let d=await i.commands.spawn(e,{cwd:this.workingDir,timeoutMs:a,onStdout:p,onStderr:t});return {sandboxId:i.sandboxId,exitCode:0,stdout:`Background process started with ID ${d.processId}`,stderr:""}}let u=await i.commands.run(e,{cwd:this.workingDir,timeoutMs:a,onStdout:p,onStderr:t});return {sandboxId:i.sandboxId,exitCode:u.exitCode,stdout:l,stderr:r}}async uploadContext(e){let n=await this.getSandbox();await this.uploadContextFiles(n,e);}async uploadFiles(e){let n=await this.getSandbox();await this.uploadWorkspaceFiles(n,e);}async getOutputFiles(e=false){let n=await this.getSandbox(),s=`${this.workingDir}/output`,a=e?"":"-maxdepth 1",i=(await n.commands.run(`find ${s} ${a} -type f -exec stat -c '%n|%Z' {} \\; 2>/dev/null || true`,{timeoutMs:3e4})).stdout.split(`
197
- `).filter(Boolean),l=this.lastRunTimestamp?Math.floor(this.lastRunTimestamp/1e3)-2:0,r=[],p=`${s}/`;for(let f of i){let[h,g]=f.split("|");if(!h||!g)continue;let y=parseInt(g,10);l>0&&y<l||r.push(h);}let t={},u=await Promise.all(r.map(async f=>{try{let h=await n.files.read(f);return {relativePath:f.startsWith(p)?f.slice(p.length):f.split("/").pop()||f,content:h}}catch{return null}}));for(let f of u)f&&(t[f.relativePath]=f.content);if(!this.zodSchema&&!this.jsonSchema)return {files:t,data:null};let d=t["result.json"];if(!d)return {files:t,data:null,error:"Schema provided but agent did not create output/result.json"};let m=typeof d=="string"?d:new TextDecoder().decode(d);try{let f=JSON.parse(m);if(this.zodSchema){let h=this.zodSchema.safeParse(f);return h.success?{files:t,data:h.data}:{files:t,data:null,error:`Schema validation failed: ${h.error.message}`,rawData:m}}if(this.compiledValidator){if(this.compiledValidator(f))return {files:t,data:f};{let g=this.compiledValidator.errors?.map(y=>`${y.instancePath} ${y.message}`).join(", ")||"Unknown validation error";return {files:t,data:null,error:`Schema validation failed: ${g}`,rawData:m}}}return {files:t,data:null}}catch(f){return {files:t,data:null,error:`Failed to parse result.json: ${f.message}`,rawData:m}}}getSession(){return this.sandbox?.sandboxId||this.options.sandboxId||null}async setSession(e){this.sessionLogger&&(await this.sessionLogger.close(),this.sessionLogger=void 0),this.options.sandboxId=e,this.sandbox=void 0,this.hasRun=true;}async pause(){this.sandbox&&await this.sandbox.pause();}async resume(){this.sandbox&&this.options.sandboxProvider&&(this.sandbox=await this.options.sandboxProvider.connect(this.sandbox.sandboxId));}async kill(){this.sessionLogger&&(await this.sessionLogger.close(),this.sessionLogger=void 0),this.sandbox&&(await this.sandbox.kill(),this.sandbox=void 0,this.hasRun=false);}async getHost(e){return (await this.getSandbox()).getHost(e)}getAgentType(){return this.agentConfig.type}getSessionTag(){return this.sessionLogger?.getTag()||null}getSessionTimestamp(){return this.sessionLogger?.getTimestamp()||null}};var H=class extends events.EventEmitter{config={};agent;constructor(){super();}on(e,n){return super.on(e,n)}off(e,n){return super.off(e,n)}emit(e,...n){return super.emit(e,...n)}withAgent(e){return e&&(this.config.agent=e),this}withSandbox(e){return this.config.sandbox=e,this}withWorkingDirectory(e){return this.config.workingDirectory=e,this}withWorkspaceMode(e){return this.config.workspaceMode=e,this}withSecrets(e){return this.config.secrets={...this.config.secrets,...e},this}withSession(e){return this.config.sandboxId=e,this}withSystemPrompt(e){return this.config.systemPrompt=e,this}withContext(e){return this.config.context={...this.config.context,...e},this}withFiles(e){return this.config.files={...this.config.files,...e},this}withMcpServers(e){return this.config.mcpServers={...this.config.mcpServers,...e},this}withSkills(e){return this.config.skills=e,this}withSchema(e,n){return this.config.schema=e,n&&(E(e)?console.warn("[Evolve] schemaOptions ignored for Zod schemas - use .passthrough(), .strip(), z.coerce instead"):this.config.schemaOptions=n),this}withSessionTagPrefix(e){return this.config.sessionTagPrefix=e,this}withObservability(e){return this.config.observability={...this.config.observability,...e},this}withComposio(e,n){return this.config.composio={userId:e,config:n},this}static composio=Fe;async initializeAgent(){let e=_e(this.config.agent),n=this.config.sandbox??await Ee(),s=e.isDirectMode?{}:Ke(e.apiKey),a={sandboxProvider:n,secrets:this.config.secrets,sandboxId:this.config.sandboxId,workingDirectory:this.config.workingDirectory,workspaceMode:this.config.workspaceMode,systemPrompt:this.config.systemPrompt,context:this.config.context,files:this.config.files,mcpServers:{...s,...this.config.mcpServers},skills:this.config.skills,schema:this.config.schema,schemaOptions:this.config.schemaOptions,sessionTagPrefix:this.config.sessionTagPrefix,observability:this.config.observability,composio:this.config.composio};this.agent=new z(e,a);}createStreamCallbacks(){let e=this.listenerCount("stdout")>0,n=this.listenerCount("stderr")>0,s=this.listenerCount("content")>0;return {onStdout:e?a=>this.emit("stdout",a):void 0,onStderr:n?a=>this.emit("stderr",a):void 0,onContent:s?a=>this.emit("content",a):void 0}}async run({prompt:e,timeoutMs:n,background:s}){this.agent||await this.initializeAgent();let a=this.createStreamCallbacks();return this.agent.run({prompt:e,timeoutMs:n,background:s},a)}async executeCommand(e,n={}){this.agent||await this.initializeAgent();let s=this.createStreamCallbacks();return this.agent.executeCommand(e,n,s)}async uploadContext(e){return this.agent||await this.initializeAgent(),this.agent.uploadContext(e)}async uploadFiles(e){return this.agent||await this.initializeAgent(),this.agent.uploadFiles(e)}async getOutputFiles(e=false){if(!this.agent)throw new Error("Agent not initialized. Call run() first.");return this.agent.getOutputFiles(e)}getSession(){return this.agent?this.agent.getSession():this.config.sandboxId??null}async setSession(e){this.agent&&await this.agent.setSession(e),this.config.sandboxId=e;}async pause(){if(this.agent)return this.agent.pause();this.config.sandboxId&&this.config.sandbox&&await(await this.config.sandbox.connect(this.config.sandboxId)).pause();}async resume(){if(this.agent)return this.agent.resume();this.config.sandboxId&&this.config.sandbox&&await this.config.sandbox.connect(this.config.sandboxId);}async kill(){if(this.agent)return this.agent.kill();this.config.sandboxId&&this.config.sandbox&&await(await this.config.sandbox.connect(this.config.sandboxId)).kill();}async getHost(e){return this.agent||await this.initializeAgent(),this.agent.getHost(e)}getSessionTag(){return this.agent?.getSessionTag()||null}getSessionTimestamp(){return this.agent?.getSessionTimestamp()||null}};var L=class{permits;queue=[];constructor(e){if(e<1)throw new Error("Semaphore max must be >= 1");this.permits=e;}async use(e){await this.acquire();try{return await e()}finally{this.release();}}acquire(){return this.permits>0?(this.permits--,Promise.resolve()):new Promise(e=>this.queue.push(e))}release(){this.queue.length>0?this.queue.shift()():this.permits++;}};var A=Symbol.for("evolve.SwarmResult"),j=class c extends Array{get success(){return this.filter(e=>e.status==="success")}get filtered(){return this.filter(e=>e.status==="filtered")}get error(){return this.filter(e=>e.status==="error")}static from(e){let n=new c;return n.push(...e),n}};var $e=class c{config;semaphore;constructor(e={}){let n=e.concurrency??Je;if(n>ke)throw new Error(`concurrency=${n} exceeds max ${ke}. For higher parallelism, scale horizontally with multiple processes.`);this.config={agent:e.agent,sandbox:e.sandbox,tag:e.tag??"swarm",concurrency:n,timeoutMs:e.timeoutMs??W,workspaceMode:e.workspaceMode??"knowledge",retry:e.retry,mcpServers:e.mcpServers,skills:e.skills,composio:e.composio},this.semaphore=new L(this.config.concurrency);}async map(e){let{items:n,prompt:s,bestOf:a,verify:o}=e,i=e.retry??this.config.retry,l=this.generateOperationId(),r=e.timeoutMs??this.config.timeoutMs;if(a&&o)throw new Error("map() cannot use both bestOf and verify options simultaneously");let p=await Promise.all(n.map((t,u)=>a?this.executeMapItemWithBestOf(t,s,u,l,e,r,i):o?this.executeMapItemWithVerify(t,s,u,l,e,r,i):i?T(d=>this.executeMapItem(t,s,u,l,e,r,d),i,u):this.executeMapItem(t,s,u,l,e,r)));return j.from(p)}async filter(e){let{items:n,prompt:s,condition:a,verify:o}=e,i=e.retry??this.config.retry,l=this.generateOperationId(),r=e.timeoutMs??this.config.timeoutMs,p=await Promise.all(n.map((u,d)=>o?this.executeFilterItemWithVerify(u,s,d,l,e,r,i):i?T(m=>this.executeFilterItem(u,s,d,l,e,r,m),i,d):this.executeFilterItem(u,s,d,l,e,r))),t=[];for(let u of p)if(u.status==="error")t.push(u);else if(u.data!==null)try{a(u.data)?t.push(u):t.push({...u,status:"filtered"});}catch(d){t.push({...u,status:"error",data:null,error:`Condition function threw: ${d.message}`});}return j.from(t)}async reduce(e){let{items:n,prompt:s,verify:a}=e,o=e.retry??this.config.retry,i=this.generateOperationId(),l=e.timeoutMs??this.config.timeoutMs,r=[],p=[];n.forEach((x,C)=>{r.push(this.getFiles(x)),p.push(this.getIndex(x,C));});let t={};r.forEach((x,C)=>{Object.entries(x).forEach(([w,S])=>{t[`item_${p[C]}/${w}`]=S;});});let u=ue(t),d=R(ft,{fileTree:u}),m=e.systemPrompt?`${d}
211
+ `).filter(Boolean),l=this.lastRunTimestamp?Math.floor(this.lastRunTimestamp/1e3)-2:0,r=[],p=`${s}/`;for(let f of i){let[h,g]=f.split("|");if(!h||!g)continue;let y=parseInt(g,10);l>0&&y<l||r.push(h);}let t={},u=await Promise.all(r.map(async f=>{try{let h=await n.files.read(f);return {relativePath:f.startsWith(p)?f.slice(p.length):f.split("/").pop()||f,content:h}}catch{return null}}));for(let f of u)f&&(t[f.relativePath]=f.content);if(!this.zodSchema&&!this.jsonSchema)return {files:t,data:null};let d=t["result.json"];if(!d)return {files:t,data:null,error:"Schema provided but agent did not create output/result.json"};let m=typeof d=="string"?d:new TextDecoder().decode(d);try{let f=JSON.parse(m);if(this.zodSchema){let h=this.zodSchema.safeParse(f);return h.success?{files:t,data:h.data}:{files:t,data:null,error:`Schema validation failed: ${h.error.message}`,rawData:m}}if(this.compiledValidator){if(this.compiledValidator(f))return {files:t,data:f};{let g=this.compiledValidator.errors?.map(y=>`${y.instancePath} ${y.message}`).join(", ")||"Unknown validation error";return {files:t,data:null,error:`Schema validation failed: ${g}`,rawData:m}}}return {files:t,data:null}}catch(f){return {files:t,data:null,error:`Failed to parse result.json: ${f.message}`,rawData:m}}}getSession(){return this.sandbox?.sandboxId||this.options.sandboxId||null}async setSession(e){this.sessionLogger&&(await this.sessionLogger.close(),this.sessionLogger=void 0),this.options.sandboxId=e,this.sandbox=void 0,this.hasRun=true;}async pause(){this.sandbox&&await this.sandbox.pause();}async resume(){this.sandbox&&this.options.sandboxProvider&&(this.sandbox=await this.options.sandboxProvider.connect(this.sandbox.sandboxId));}async kill(){this.sessionLogger&&(await this.sessionLogger.close(),this.sessionLogger=void 0),this.sandbox&&(await this.sandbox.kill(),this.sandbox=void 0,this.hasRun=false);}async getHost(e){return (await this.getSandbox()).getHost(e)}getAgentType(){return this.agentConfig.type}getSessionTag(){return this.sessionLogger?.getTag()||null}getSessionTimestamp(){return this.sessionLogger?.getTimestamp()||null}};var H=class extends events.EventEmitter{config={};agent;constructor(){super();}on(e,n){return super.on(e,n)}off(e,n){return super.off(e,n)}emit(e,...n){return super.emit(e,...n)}withAgent(e){return e&&(this.config.agent=e),this}withSandbox(e){return this.config.sandbox=e,this}withWorkingDirectory(e){return this.config.workingDirectory=e,this}withWorkspaceMode(e){return this.config.workspaceMode=e,this}withSecrets(e){return this.config.secrets={...this.config.secrets,...e},this}withSession(e){return this.config.sandboxId=e,this}withSystemPrompt(e){return this.config.systemPrompt=e,this}withContext(e){return this.config.context={...this.config.context,...e},this}withFiles(e){return this.config.files={...this.config.files,...e},this}withMcpServers(e){return this.config.mcpServers={...this.config.mcpServers,...e},this}withSkills(e){return this.config.skills=e,this}withSchema(e,n){return this.config.schema=e,n&&(P(e)?console.warn("[Evolve] schemaOptions ignored for Zod schemas - use .passthrough(), .strip(), z.coerce instead"):this.config.schemaOptions=n),this}withSessionTagPrefix(e){return this.config.sessionTagPrefix=e,this}withObservability(e){return this.config.observability={...this.config.observability,...e},this}withComposio(e,n){return this.config.composio={userId:e,config:n},this}static composio=Fe;async initializeAgent(){let e=Te(this.config.agent),n=this.config.sandbox??await Pe(),s=e.isDirectMode?{}:Ke(e.apiKey),a={sandboxProvider:n,secrets:this.config.secrets,sandboxId:this.config.sandboxId,workingDirectory:this.config.workingDirectory,workspaceMode:this.config.workspaceMode,systemPrompt:this.config.systemPrompt,context:this.config.context,files:this.config.files,mcpServers:{...s,...this.config.mcpServers},skills:this.config.skills,schema:this.config.schema,schemaOptions:this.config.schemaOptions,sessionTagPrefix:this.config.sessionTagPrefix,observability:this.config.observability,composio:this.config.composio};this.agent=new z(e,a);}createStreamCallbacks(){let e=this.listenerCount("stdout")>0,n=this.listenerCount("stderr")>0,s=this.listenerCount("content")>0;return {onStdout:e?a=>this.emit("stdout",a):void 0,onStderr:n?a=>this.emit("stderr",a):void 0,onContent:s?a=>this.emit("content",a):void 0}}async run({prompt:e,timeoutMs:n,background:s}){this.agent||await this.initializeAgent();let a=this.createStreamCallbacks();return this.agent.run({prompt:e,timeoutMs:n,background:s},a)}async executeCommand(e,n={}){this.agent||await this.initializeAgent();let s=this.createStreamCallbacks();return this.agent.executeCommand(e,n,s)}async uploadContext(e){return this.agent||await this.initializeAgent(),this.agent.uploadContext(e)}async uploadFiles(e){return this.agent||await this.initializeAgent(),this.agent.uploadFiles(e)}async getOutputFiles(e=false){if(!this.agent)throw new Error("Agent not initialized. Call run() first.");return this.agent.getOutputFiles(e)}getSession(){return this.agent?this.agent.getSession():this.config.sandboxId??null}async setSession(e){this.agent&&await this.agent.setSession(e),this.config.sandboxId=e;}async pause(){if(this.agent)return this.agent.pause();this.config.sandboxId&&this.config.sandbox&&await(await this.config.sandbox.connect(this.config.sandboxId)).pause();}async resume(){if(this.agent)return this.agent.resume();this.config.sandboxId&&this.config.sandbox&&await this.config.sandbox.connect(this.config.sandboxId);}async kill(){if(this.agent)return this.agent.kill();this.config.sandboxId&&this.config.sandbox&&await(await this.config.sandbox.connect(this.config.sandboxId)).kill();}async getHost(e){return this.agent||await this.initializeAgent(),this.agent.getHost(e)}getSessionTag(){return this.agent?.getSessionTag()||null}getSessionTimestamp(){return this.agent?.getSessionTimestamp()||null}};var j=class{permits;queue=[];constructor(e){if(e<1)throw new Error("Semaphore max must be >= 1");this.permits=e;}async use(e){await this.acquire();try{return await e()}finally{this.release();}}acquire(){return this.permits>0?(this.permits--,Promise.resolve()):new Promise(e=>this.queue.push(e))}release(){this.queue.length>0?this.queue.shift()():this.permits++;}};var A=Symbol.for("evolve.SwarmResult"),L=class c extends Array{get success(){return this.filter(e=>e.status==="success")}get filtered(){return this.filter(e=>e.status==="filtered")}get error(){return this.filter(e=>e.status==="error")}static from(e){let n=new c;return n.push(...e),n}};var $e=class c{config;semaphore;constructor(e={}){let n=e.concurrency??Je;if(n>ke)throw new Error(`concurrency=${n} exceeds max ${ke}. For higher parallelism, scale horizontally with multiple processes.`);this.config={agent:e.agent,sandbox:e.sandbox,tag:e.tag??"swarm",concurrency:n,timeoutMs:e.timeoutMs??W,workspaceMode:e.workspaceMode??"knowledge",retry:e.retry,mcpServers:e.mcpServers,skills:e.skills,composio:e.composio},this.semaphore=new j(this.config.concurrency);}async map(e){let{items:n,prompt:s,bestOf:a,verify:o}=e,i=e.retry??this.config.retry,l=this.generateOperationId(),r=e.timeoutMs??this.config.timeoutMs;if(a&&o)throw new Error("map() cannot use both bestOf and verify options simultaneously");let p=await Promise.all(n.map((t,u)=>a?this.executeMapItemWithBestOf(t,s,u,l,e,r,i):o?this.executeMapItemWithVerify(t,s,u,l,e,r,i):i?_(d=>this.executeMapItem(t,s,u,l,e,r,d),i,u):this.executeMapItem(t,s,u,l,e,r)));return L.from(p)}async filter(e){let{items:n,prompt:s,condition:a,verify:o}=e,i=e.retry??this.config.retry,l=this.generateOperationId(),r=e.timeoutMs??this.config.timeoutMs,p=await Promise.all(n.map((u,d)=>o?this.executeFilterItemWithVerify(u,s,d,l,e,r,i):i?_(m=>this.executeFilterItem(u,s,d,l,e,r,m),i,d):this.executeFilterItem(u,s,d,l,e,r))),t=[];for(let u of p)if(u.status==="error")t.push(u);else if(u.data!==null)try{a(u.data)?t.push(u):t.push({...u,status:"filtered"});}catch(d){t.push({...u,status:"error",data:null,error:`Condition function threw: ${d.message}`});}return L.from(t)}async reduce(e){let{items:n,prompt:s,verify:a}=e,o=e.retry??this.config.retry,i=this.generateOperationId(),l=e.timeoutMs??this.config.timeoutMs,r=[],p=[];n.forEach((x,w)=>{r.push(this.getFiles(x)),p.push(this.getIndex(x,w));});let t={};r.forEach((x,w)=>{Object.entries(x).forEach(([C,S])=>{t[`item_${p[w]}/${C}`]=S;});});let u=ue(t),d=R(ft,{fileTree:u}),m=e.systemPrompt?`${d}
198
212
 
199
- ${e.systemPrompt}`:d,f=(x,C,w)=>({operationId:i,operation:"reduce",tag:x.tag,sandboxId:x.sandboxId,swarmName:this.config.tag,operationName:e.name,inputCount:n.length,inputIndices:p,errorRetry:C,verifyRetry:w,...this.pipelineContextToMeta(e._pipelineContext)}),h=e.mcpServers??this.config.mcpServers,g=e.skills??this.config.skills,y=e.composio??this.config.composio,v=async(x,C,w,S)=>{let k=await this.semaphore.use(()=>this.execute(t,x,{systemPrompt:m,schema:e.schema,schemaOptions:e.schemaOptions,agent:e.agent,mcpServers:h,skills:g,composio:y,tagPrefix:C,timeoutMs:l,observability:{swarmName:this.config.tag,operationName:e.name,operationId:i,operation:"reduce",role:"worker",errorRetry:w,verifyRetry:S&&S>1?S-1:void 0,...this.pipelineContextToObservability(e._pipelineContext)}})),F=f(k,w,S&&S>1?S-1:void 0);return k.error?{status:"error",data:null,files:k.files,meta:F,error:k.error,rawData:k.rawData}:{status:"success",data:k.data,files:k.files,meta:F}},b=`${this.config.tag}-reduce`;return a?this.runWithVerification((x,C,w)=>v(x,C,void 0,w),{originalPrompt:s,inputFiles:t,verifyConfig:a,timeoutMs:l,systemPrompt:m,schema:e.schema,mcpServers:h,skills:g,composio:y,operationId:i,baseTag:b,retry:o,operation:"reduce",_pipelineContext:e._pipelineContext}):o?T(x=>{let C=x>1?x-1:void 0,w=C?`${b}-er${C}`:b;return v(s,w,C)},o):v(s,b)}async bestOf(e){let{item:n,prompt:s,config:a}=e,o=e.retry??this.config.retry,i=a.n??a.taskAgents?.length;if(i===void 0)throw new Error("bestOf requires n or taskAgents");if(i<2)throw new Error("bestOf requires n >= 2");let l=this.generateOperationId(),r=e.timeoutMs??this.config.timeoutMs,p=this.getFiles(n),t=a.mcpServers??this.config.mcpServers,u=a.judgeMcpServers??a.mcpServers??this.config.mcpServers,d=a.skills??this.config.skills,m=a.judgeSkills??a.skills??this.config.skills,f=a.composio??this.config.composio,h=a.judgeComposio??a.composio??this.config.composio,g=await Promise.all(Array.from({length:i},async(C,w)=>{let S=o?await T(k=>this.executeBestOfCandidate({inputFiles:p,prompt:s,candidateIndex:w,operationId:l,config:a,systemPrompt:e.systemPrompt,schema:e.schema,schemaOptions:e.schemaOptions,mcpServers:t,skills:d,composio:f,timeoutMs:r,attempt:k}),o,w):await this.executeBestOfCandidate({inputFiles:p,prompt:s,candidateIndex:w,operationId:l,config:a,systemPrompt:e.systemPrompt,schema:e.schema,schemaOptions:e.schemaOptions,mcpServers:t,skills:d,composio:f,timeoutMs:r});return a.onCandidateComplete?.(0,w,S.status==="success"?"success":"error"),S})),y=o?await T(C=>this.executeBestOfJudge({inputFiles:p,taskPrompt:s,candidates:g,config:a,timeoutMs:r,operationId:l,systemPrompt:e.systemPrompt,schema:e.schema,schemaOptions:e.schemaOptions,mcpServers:u,skills:m,composio:h,attempt:C}),{...o,retryOn:void 0},0):await this.executeBestOfJudge({inputFiles:p,taskPrompt:s,candidates:g,config:a,timeoutMs:r,operationId:l,systemPrompt:e.systemPrompt,schema:e.schema,schemaOptions:e.schemaOptions,mcpServers:u,skills:m,composio:h}),v=g.findIndex(C=>C.status==="success"),b=y.decision?.winner??(v>=0?v:0);a.onJudgeComplete?.(0,b,y.decision?.reasoning??"Judge failed to provide reasoning");let x={operationId:l,operation:"bestof-judge",tag:y.tag,sandboxId:y.sandboxId,swarmName:this.config.tag,operationName:e.name,candidateCount:i};return {winner:g[b]??g[0],winnerIndex:b,judgeReasoning:y.decision?.reasoning??"Judge failed to provide reasoning",judgeMeta:x,candidates:g}}async execute(e,n,s){let a=null,o="",i=s.tagPrefix,l={},r=null,p,t,u={...this.config.agent,...s.agent};try{a=new H().withAgent(u).withSandbox(this.config.sandbox).withWorkspaceMode(this.config.workspaceMode).withSessionTagPrefix(s.tagPrefix),s.schema&&a.withSchema(s.schema,s.schemaOptions),s.systemPrompt&&a.withSystemPrompt(s.systemPrompt),s.mcpServers&&a.withMcpServers(s.mcpServers),s.skills?.length&&a.withSkills(s.skills),s.composio&&a.withComposio(s.composio.userId,s.composio.config),s.observability&&a.withObservability(s.observability),Object.keys(e).length>0&&a.withContext(e);let d=await a.run({prompt:n,timeoutMs:s.timeoutMs});o=d.sandboxId,i=a.getSessionTag()??s.tagPrefix;let m=null;try{m=await a.getOutputFiles(!0),l=m.files;}catch{}d.exitCode!==0?p=`Agent exited with code ${d.exitCode}`:s.schema?m?(r=m.data,m.error&&(p=m.error),m.rawData&&(t=m.rawData)):p="Failed to read output files from sandbox":r=l;}catch(d){if(p=d.message,a){i=a.getSessionTag()??s.tagPrefix;try{l=(await a.getOutputFiles(!0)).files;}catch{}}}finally{a&&await a.kill().catch(()=>{});}return {files:l,data:r,tag:i,sandboxId:o,error:p,rawData:t}}async executeMapItem(e,n,s,a,o,i,l=1){let r=this.getFiles(e),p=l>1?`${this.config.tag}-map-${s}-er${l-1}`:`${this.config.tag}-map-${s}`,t=this.evaluatePrompt(n,r,s);if(t instanceof Error)return this.buildErrorResult(`Prompt function threw: ${t.message}`,{operationId:a,operation:"map",tag:p,sandboxId:"",itemIndex:s});let u=o.mcpServers??this.config.mcpServers,d=o.skills??this.config.skills,m=o.composio??this.config.composio,f=await this.semaphore.use(()=>this.execute(r,t,{systemPrompt:o.systemPrompt,schema:o.schema,schemaOptions:o.schemaOptions,agent:o.agent,mcpServers:u,skills:d,composio:m,tagPrefix:p,timeoutMs:i,observability:{swarmName:this.config.tag,operationName:o.name,operationId:a,operation:"map",itemIndex:s,role:"worker",errorRetry:l>1?l-1:void 0,...this.pipelineContextToObservability(o._pipelineContext)}})),h={operationId:a,operation:"map",tag:f.tag,sandboxId:f.sandboxId,swarmName:this.config.tag,operationName:o.name,itemIndex:s,errorRetry:l>1?l-1:void 0,...this.pipelineContextToMeta(o._pipelineContext)};return this.buildResult(f,h)}async executeMapItemWithVerify(e,n,s,a,o,i,l){let r=this.getFiles(e),p=`${this.config.tag}-map-${s}`,t=o.verify,u=o.mcpServers??this.config.mcpServers,d=o.skills??this.config.skills,m=o.composio??this.config.composio,f=this.evaluatePrompt(n,r,s);if(f instanceof Error)return this.buildErrorResult(`Prompt function threw: ${f.message}`,{operationId:a,operation:"map",tag:p,sandboxId:"",itemIndex:s});let h=async(g,y,v)=>{let b=await this.semaphore.use(()=>this.execute(r,g,{systemPrompt:o.systemPrompt,schema:o.schema,schemaOptions:o.schemaOptions,agent:o.agent,mcpServers:u,skills:d,composio:m,tagPrefix:y,timeoutMs:i,observability:{swarmName:this.config.tag,operationName:o.name,operationId:a,operation:"map",itemIndex:s,role:"worker",verifyRetry:v&&v>1?v-1:void 0,...this.pipelineContextToObservability(o._pipelineContext)}})),x={operationId:a,operation:"map",tag:b.tag,sandboxId:b.sandboxId,swarmName:this.config.tag,operationName:o.name,itemIndex:s,verifyRetry:v&&v>1?v-1:void 0,...this.pipelineContextToMeta(o._pipelineContext)};return this.buildResult(b,x)};return this.runWithVerification(h,{originalPrompt:f,inputFiles:r,verifyConfig:t,timeoutMs:i,systemPrompt:o.systemPrompt,schema:o.schema,mcpServers:u,skills:d,composio:m,operationId:a,baseTag:p,retry:l,itemIndex:s,operation:"map",_pipelineContext:o._pipelineContext})}async executeMapItemWithBestOf(e,n,s,a,o,i,l){let r=this.getFiles(e),p=`${this.config.tag}-map-${s}`,t=this.evaluatePrompt(n,r,s);if(t instanceof Error)return this.buildErrorResult(`Prompt function threw: ${t.message}`,{operationId:a,operation:"map",tag:p,sandboxId:"",itemIndex:s});let u=o.bestOf,d=u.n??u.taskAgents?.length;if(d===void 0||d<2)return this.buildErrorResult("bestOf requires n >= 2 or taskAgents with at least 2 elements",{operationId:a,operation:"map",tag:p,sandboxId:"",itemIndex:s});let m=o.mcpServers??this.config.mcpServers,f=u.mcpServers??m,h=u.judgeMcpServers??u.mcpServers??m,g=o.skills??this.config.skills,y=u.skills??g,v=u.judgeSkills??u.skills??g,b=o.composio??this.config.composio,x=u.composio??b,C=u.judgeComposio??u.composio??b,w=await Promise.all(Array.from({length:d},async(q,Z)=>{let Ne=l?await T(St=>this.executeBestOfCandidate({inputFiles:r,prompt:t,candidateIndex:Z,operationId:a,config:u,systemPrompt:o.systemPrompt,schema:o.schema,schemaOptions:o.schemaOptions,mcpServers:f,skills:y,composio:x,timeoutMs:i,parentIndex:s,attempt:St,_pipelineContext:o._pipelineContext}),l,Z):await this.executeBestOfCandidate({inputFiles:r,prompt:t,candidateIndex:Z,operationId:a,config:u,systemPrompt:o.systemPrompt,schema:o.schema,schemaOptions:o.schemaOptions,mcpServers:f,skills:y,composio:x,timeoutMs:i,parentIndex:s,_pipelineContext:o._pipelineContext});return u.onCandidateComplete?.(s,Z,Ne.status==="success"?"success":"error"),Ne})),S=l?await T(q=>this.executeBestOfJudge({inputFiles:r,taskPrompt:t,candidates:w,config:u,timeoutMs:i,operationId:a,systemPrompt:o.systemPrompt,schema:o.schema,schemaOptions:o.schemaOptions,mcpServers:h,skills:v,composio:C,parentIndex:s,attempt:q,_pipelineContext:o._pipelineContext}),{...l,retryOn:void 0},0):await this.executeBestOfJudge({inputFiles:r,taskPrompt:t,candidates:w,config:u,timeoutMs:i,operationId:a,systemPrompt:o.systemPrompt,schema:o.schema,schemaOptions:o.schemaOptions,mcpServers:h,skills:v,composio:C,parentIndex:s,_pipelineContext:o._pipelineContext}),k=w.findIndex(q=>q.status==="success"),F=S.decision?.winner??(k>=0?k:0),Ue=w[F]??w[0];u.onJudgeComplete?.(s,F,S.decision?.reasoning??"Judge failed to provide reasoning");let Ct={operationId:a,operation:"bestof-judge",tag:S.tag,sandboxId:S.sandboxId,swarmName:this.config.tag,operationName:o.name,candidateCount:d,...this.pipelineContextToMeta(o._pipelineContext)};return {...Ue,meta:{...Ue.meta,operation:"map",swarmName:this.config.tag,operationName:o.name,itemIndex:s,...this.pipelineContextToMeta(o._pipelineContext)},bestOf:{winnerIndex:F,judgeReasoning:S.decision?.reasoning??"Judge failed to provide reasoning",judgeMeta:Ct,candidates:w}}}async executeFilterItem(e,n,s,a,o,i,l=1){let r=this.getFiles(e),p=l>1?`${this.config.tag}-filter-${s}-er${l-1}`:`${this.config.tag}-filter-${s}`,t=o.mcpServers??this.config.mcpServers,u=o.skills??this.config.skills,d=o.composio??this.config.composio,m=await this.semaphore.use(()=>this.execute(r,n,{systemPrompt:o.systemPrompt,schema:o.schema,schemaOptions:o.schemaOptions,agent:o.agent,mcpServers:t,skills:u,composio:d,tagPrefix:p,timeoutMs:i,observability:{swarmName:this.config.tag,operationName:o.name,operationId:a,operation:"filter",itemIndex:s,role:"worker",errorRetry:l>1?l-1:void 0,...this.pipelineContextToObservability(o._pipelineContext)}})),f={operationId:a,operation:"filter",tag:m.tag,sandboxId:m.sandboxId,swarmName:this.config.tag,operationName:o.name,itemIndex:s,errorRetry:l>1?l-1:void 0,...this.pipelineContextToMeta(o._pipelineContext)};return this.buildResult(m,f,r)}async executeFilterItemWithVerify(e,n,s,a,o,i,l){let r=this.getFiles(e),p=`${this.config.tag}-filter-${s}`,t=o.verify,u=o.mcpServers??this.config.mcpServers,d=o.skills??this.config.skills,m=o.composio??this.config.composio,f=async(h,g,y)=>{let v=await this.semaphore.use(()=>this.execute(r,h,{systemPrompt:o.systemPrompt,schema:o.schema,schemaOptions:o.schemaOptions,agent:o.agent,mcpServers:u,skills:d,composio:m,tagPrefix:g,timeoutMs:i,observability:{swarmName:this.config.tag,operationName:o.name,operationId:a,operation:"filter",itemIndex:s,role:"worker",verifyRetry:y&&y>1?y-1:void 0,...this.pipelineContextToObservability(o._pipelineContext)}})),b={operationId:a,operation:"filter",tag:v.tag,sandboxId:v.sandboxId,swarmName:this.config.tag,operationName:o.name,itemIndex:s,verifyRetry:y&&y>1?y-1:void 0,...this.pipelineContextToMeta(o._pipelineContext)};return this.buildResult(v,b,r)};return this.runWithVerification(f,{originalPrompt:n,inputFiles:r,verifyConfig:t,timeoutMs:i,systemPrompt:o.systemPrompt,schema:o.schema,mcpServers:u,skills:d,composio:m,operationId:a,baseTag:p,retry:l,itemIndex:s,operation:"filter",_pipelineContext:o._pipelineContext})}async executeBestOfCandidate(e){let{inputFiles:n,prompt:s,candidateIndex:a,operationId:o,config:i,timeoutMs:l,parentIndex:r,attempt:p=1,_pipelineContext:t}=e,u=r!==void 0?`${this.config.tag}-map-${r}-bestof-cand-${a}`:`${this.config.tag}-bestof-cand-${a}`,d=p>1?`${u}-er${p-1}`:u,m=await this.semaphore.use(()=>this.execute(n,s,{systemPrompt:e.systemPrompt,schema:e.schema,schemaOptions:e.schemaOptions,agent:i.taskAgents?.[a],mcpServers:e.mcpServers,skills:e.skills,composio:e.composio,tagPrefix:d,timeoutMs:l,observability:{swarmName:this.config.tag,operationId:o,operation:"map",itemIndex:r,role:"candidate",candidateIndex:a,errorRetry:p>1?p-1:void 0,...this.pipelineContextToObservability(t)}})),f={operationId:o,operation:"bestof-cand",tag:m.tag,sandboxId:m.sandboxId,swarmName:this.config.tag,itemIndex:a,errorRetry:p>1?p-1:void 0,candidateIndex:a,...this.pipelineContextToMeta(t)};return this.buildResult(m,f)}buildJudgeContext(e){let n=this.buildEvaluatorContext({inputFiles:e.inputFiles,taskPrompt:e.taskPrompt,systemPrompt:e.systemPrompt,schema:e.schema});return e.candidates.forEach((s,a)=>{s.status==="error"&&(n[`candidate_${a}/_failed.txt`]=`STATUS: FAILED
213
+ ${e.systemPrompt}`:d,f=(x,w,C)=>({operationId:i,operation:"reduce",tag:x.tag,sandboxId:x.sandboxId,swarmName:this.config.tag,operationName:e.name,inputCount:n.length,inputIndices:p,errorRetry:w,verifyRetry:C,...this.pipelineContextToMeta(e._pipelineContext)}),h=e.mcpServers??this.config.mcpServers,g=e.skills??this.config.skills,y=e.composio??this.config.composio,v=async(x,w,C,S)=>{let k=await this.semaphore.use(()=>this.execute(t,x,{systemPrompt:m,schema:e.schema,schemaOptions:e.schemaOptions,agent:e.agent,mcpServers:h,skills:g,composio:y,tagPrefix:w,timeoutMs:l,observability:{swarmName:this.config.tag,operationName:e.name,operationId:i,operation:"reduce",role:"worker",errorRetry:C,verifyRetry:S&&S>1?S-1:void 0,...this.pipelineContextToObservability(e._pipelineContext)}})),F=f(k,C,S&&S>1?S-1:void 0);return k.error?{status:"error",data:null,files:k.files,meta:F,error:k.error,rawData:k.rawData}:{status:"success",data:k.data,files:k.files,meta:F}},b=`${this.config.tag}-reduce`;return a?this.runWithVerification((x,w,C)=>v(x,w,void 0,C),{originalPrompt:s,inputFiles:t,verifyConfig:a,timeoutMs:l,systemPrompt:m,schema:e.schema,mcpServers:h,skills:g,composio:y,operationId:i,baseTag:b,retry:o,operation:"reduce",_pipelineContext:e._pipelineContext}):o?_(x=>{let w=x>1?x-1:void 0,C=w?`${b}-er${w}`:b;return v(s,C,w)},o):v(s,b)}async bestOf(e){let{item:n,prompt:s,config:a}=e,o=e.retry??this.config.retry,i=a.n??a.taskAgents?.length;if(i===void 0)throw new Error("bestOf requires n or taskAgents");if(i<2)throw new Error("bestOf requires n >= 2");let l=this.generateOperationId(),r=e.timeoutMs??this.config.timeoutMs,p=this.getFiles(n),t=a.mcpServers??this.config.mcpServers,u=a.judgeMcpServers??a.mcpServers??this.config.mcpServers,d=a.skills??this.config.skills,m=a.judgeSkills??a.skills??this.config.skills,f=a.composio??this.config.composio,h=a.judgeComposio??a.composio??this.config.composio,g=await Promise.all(Array.from({length:i},async(w,C)=>{let S=o?await _(k=>this.executeBestOfCandidate({inputFiles:p,prompt:s,candidateIndex:C,operationId:l,config:a,systemPrompt:e.systemPrompt,schema:e.schema,schemaOptions:e.schemaOptions,mcpServers:t,skills:d,composio:f,timeoutMs:r,attempt:k}),o,C):await this.executeBestOfCandidate({inputFiles:p,prompt:s,candidateIndex:C,operationId:l,config:a,systemPrompt:e.systemPrompt,schema:e.schema,schemaOptions:e.schemaOptions,mcpServers:t,skills:d,composio:f,timeoutMs:r});return a.onCandidateComplete?.(0,C,S.status==="success"?"success":"error"),S})),y=o?await _(w=>this.executeBestOfJudge({inputFiles:p,taskPrompt:s,candidates:g,config:a,timeoutMs:r,operationId:l,systemPrompt:e.systemPrompt,schema:e.schema,schemaOptions:e.schemaOptions,mcpServers:u,skills:m,composio:h,attempt:w}),{...o,retryOn:void 0},0):await this.executeBestOfJudge({inputFiles:p,taskPrompt:s,candidates:g,config:a,timeoutMs:r,operationId:l,systemPrompt:e.systemPrompt,schema:e.schema,schemaOptions:e.schemaOptions,mcpServers:u,skills:m,composio:h}),v=g.findIndex(w=>w.status==="success"),b=y.decision?.winner??(v>=0?v:0);a.onJudgeComplete?.(0,b,y.decision?.reasoning??"Judge failed to provide reasoning");let x={operationId:l,operation:"bestof-judge",tag:y.tag,sandboxId:y.sandboxId,swarmName:this.config.tag,operationName:e.name,candidateCount:i};return {winner:g[b]??g[0],winnerIndex:b,judgeReasoning:y.decision?.reasoning??"Judge failed to provide reasoning",judgeMeta:x,candidates:g}}async execute(e,n,s){let a=null,o="",i=s.tagPrefix,l={},r=null,p,t,u={...this.config.agent,...s.agent};try{a=new H().withAgent(u).withSandbox(this.config.sandbox).withWorkspaceMode(this.config.workspaceMode).withSessionTagPrefix(s.tagPrefix),s.schema&&a.withSchema(s.schema,s.schemaOptions),s.systemPrompt&&a.withSystemPrompt(s.systemPrompt),s.mcpServers&&a.withMcpServers(s.mcpServers),s.skills?.length&&a.withSkills(s.skills),s.composio&&a.withComposio(s.composio.userId,s.composio.config),s.observability&&a.withObservability(s.observability),Object.keys(e).length>0&&a.withContext(e);let d=await a.run({prompt:n,timeoutMs:s.timeoutMs});o=d.sandboxId,i=a.getSessionTag()??s.tagPrefix;let m=null;try{m=await a.getOutputFiles(!0),l=m.files;}catch{}d.exitCode!==0?p=`Agent exited with code ${d.exitCode}`:s.schema?m?(r=m.data,m.error&&(p=m.error),m.rawData&&(t=m.rawData)):p="Failed to read output files from sandbox":r=l;}catch(d){if(p=d.message,a){i=a.getSessionTag()??s.tagPrefix;try{l=(await a.getOutputFiles(!0)).files;}catch{}}}finally{a&&await a.kill().catch(()=>{});}return {files:l,data:r,tag:i,sandboxId:o,error:p,rawData:t}}async executeMapItem(e,n,s,a,o,i,l=1){let r=this.getFiles(e),p=l>1?`${this.config.tag}-map-${s}-er${l-1}`:`${this.config.tag}-map-${s}`,t=this.evaluatePrompt(n,r,s);if(t instanceof Error)return this.buildErrorResult(`Prompt function threw: ${t.message}`,{operationId:a,operation:"map",tag:p,sandboxId:"",itemIndex:s});let u=o.mcpServers??this.config.mcpServers,d=o.skills??this.config.skills,m=o.composio??this.config.composio,f=await this.semaphore.use(()=>this.execute(r,t,{systemPrompt:o.systemPrompt,schema:o.schema,schemaOptions:o.schemaOptions,agent:o.agent,mcpServers:u,skills:d,composio:m,tagPrefix:p,timeoutMs:i,observability:{swarmName:this.config.tag,operationName:o.name,operationId:a,operation:"map",itemIndex:s,role:"worker",errorRetry:l>1?l-1:void 0,...this.pipelineContextToObservability(o._pipelineContext)}})),h={operationId:a,operation:"map",tag:f.tag,sandboxId:f.sandboxId,swarmName:this.config.tag,operationName:o.name,itemIndex:s,errorRetry:l>1?l-1:void 0,...this.pipelineContextToMeta(o._pipelineContext)};return this.buildResult(f,h)}async executeMapItemWithVerify(e,n,s,a,o,i,l){let r=this.getFiles(e),p=`${this.config.tag}-map-${s}`,t=o.verify,u=o.mcpServers??this.config.mcpServers,d=o.skills??this.config.skills,m=o.composio??this.config.composio,f=this.evaluatePrompt(n,r,s);if(f instanceof Error)return this.buildErrorResult(`Prompt function threw: ${f.message}`,{operationId:a,operation:"map",tag:p,sandboxId:"",itemIndex:s});let h=async(g,y,v)=>{let b=await this.semaphore.use(()=>this.execute(r,g,{systemPrompt:o.systemPrompt,schema:o.schema,schemaOptions:o.schemaOptions,agent:o.agent,mcpServers:u,skills:d,composio:m,tagPrefix:y,timeoutMs:i,observability:{swarmName:this.config.tag,operationName:o.name,operationId:a,operation:"map",itemIndex:s,role:"worker",verifyRetry:v&&v>1?v-1:void 0,...this.pipelineContextToObservability(o._pipelineContext)}})),x={operationId:a,operation:"map",tag:b.tag,sandboxId:b.sandboxId,swarmName:this.config.tag,operationName:o.name,itemIndex:s,verifyRetry:v&&v>1?v-1:void 0,...this.pipelineContextToMeta(o._pipelineContext)};return this.buildResult(b,x)};return this.runWithVerification(h,{originalPrompt:f,inputFiles:r,verifyConfig:t,timeoutMs:i,systemPrompt:o.systemPrompt,schema:o.schema,mcpServers:u,skills:d,composio:m,operationId:a,baseTag:p,retry:l,itemIndex:s,operation:"map",_pipelineContext:o._pipelineContext})}async executeMapItemWithBestOf(e,n,s,a,o,i,l){let r=this.getFiles(e),p=`${this.config.tag}-map-${s}`,t=this.evaluatePrompt(n,r,s);if(t instanceof Error)return this.buildErrorResult(`Prompt function threw: ${t.message}`,{operationId:a,operation:"map",tag:p,sandboxId:"",itemIndex:s});let u=o.bestOf,d=u.n??u.taskAgents?.length;if(d===void 0||d<2)return this.buildErrorResult("bestOf requires n >= 2 or taskAgents with at least 2 elements",{operationId:a,operation:"map",tag:p,sandboxId:"",itemIndex:s});let m=o.mcpServers??this.config.mcpServers,f=u.mcpServers??m,h=u.judgeMcpServers??u.mcpServers??m,g=o.skills??this.config.skills,y=u.skills??g,v=u.judgeSkills??u.skills??g,b=o.composio??this.config.composio,x=u.composio??b,w=u.judgeComposio??u.composio??b,C=await Promise.all(Array.from({length:d},async(q,Z)=>{let Ne=l?await _(St=>this.executeBestOfCandidate({inputFiles:r,prompt:t,candidateIndex:Z,operationId:a,config:u,systemPrompt:o.systemPrompt,schema:o.schema,schemaOptions:o.schemaOptions,mcpServers:f,skills:y,composio:x,timeoutMs:i,parentIndex:s,attempt:St,_pipelineContext:o._pipelineContext}),l,Z):await this.executeBestOfCandidate({inputFiles:r,prompt:t,candidateIndex:Z,operationId:a,config:u,systemPrompt:o.systemPrompt,schema:o.schema,schemaOptions:o.schemaOptions,mcpServers:f,skills:y,composio:x,timeoutMs:i,parentIndex:s,_pipelineContext:o._pipelineContext});return u.onCandidateComplete?.(s,Z,Ne.status==="success"?"success":"error"),Ne})),S=l?await _(q=>this.executeBestOfJudge({inputFiles:r,taskPrompt:t,candidates:C,config:u,timeoutMs:i,operationId:a,systemPrompt:o.systemPrompt,schema:o.schema,schemaOptions:o.schemaOptions,mcpServers:h,skills:v,composio:w,parentIndex:s,attempt:q,_pipelineContext:o._pipelineContext}),{...l,retryOn:void 0},0):await this.executeBestOfJudge({inputFiles:r,taskPrompt:t,candidates:C,config:u,timeoutMs:i,operationId:a,systemPrompt:o.systemPrompt,schema:o.schema,schemaOptions:o.schemaOptions,mcpServers:h,skills:v,composio:w,parentIndex:s,_pipelineContext:o._pipelineContext}),k=C.findIndex(q=>q.status==="success"),F=S.decision?.winner??(k>=0?k:0),Ue=C[F]??C[0];u.onJudgeComplete?.(s,F,S.decision?.reasoning??"Judge failed to provide reasoning");let wt={operationId:a,operation:"bestof-judge",tag:S.tag,sandboxId:S.sandboxId,swarmName:this.config.tag,operationName:o.name,candidateCount:d,...this.pipelineContextToMeta(o._pipelineContext)};return {...Ue,meta:{...Ue.meta,operation:"map",swarmName:this.config.tag,operationName:o.name,itemIndex:s,...this.pipelineContextToMeta(o._pipelineContext)},bestOf:{winnerIndex:F,judgeReasoning:S.decision?.reasoning??"Judge failed to provide reasoning",judgeMeta:wt,candidates:C}}}async executeFilterItem(e,n,s,a,o,i,l=1){let r=this.getFiles(e),p=l>1?`${this.config.tag}-filter-${s}-er${l-1}`:`${this.config.tag}-filter-${s}`,t=o.mcpServers??this.config.mcpServers,u=o.skills??this.config.skills,d=o.composio??this.config.composio,m=await this.semaphore.use(()=>this.execute(r,n,{systemPrompt:o.systemPrompt,schema:o.schema,schemaOptions:o.schemaOptions,agent:o.agent,mcpServers:t,skills:u,composio:d,tagPrefix:p,timeoutMs:i,observability:{swarmName:this.config.tag,operationName:o.name,operationId:a,operation:"filter",itemIndex:s,role:"worker",errorRetry:l>1?l-1:void 0,...this.pipelineContextToObservability(o._pipelineContext)}})),f={operationId:a,operation:"filter",tag:m.tag,sandboxId:m.sandboxId,swarmName:this.config.tag,operationName:o.name,itemIndex:s,errorRetry:l>1?l-1:void 0,...this.pipelineContextToMeta(o._pipelineContext)};return this.buildResult(m,f,r)}async executeFilterItemWithVerify(e,n,s,a,o,i,l){let r=this.getFiles(e),p=`${this.config.tag}-filter-${s}`,t=o.verify,u=o.mcpServers??this.config.mcpServers,d=o.skills??this.config.skills,m=o.composio??this.config.composio,f=async(h,g,y)=>{let v=await this.semaphore.use(()=>this.execute(r,h,{systemPrompt:o.systemPrompt,schema:o.schema,schemaOptions:o.schemaOptions,agent:o.agent,mcpServers:u,skills:d,composio:m,tagPrefix:g,timeoutMs:i,observability:{swarmName:this.config.tag,operationName:o.name,operationId:a,operation:"filter",itemIndex:s,role:"worker",verifyRetry:y&&y>1?y-1:void 0,...this.pipelineContextToObservability(o._pipelineContext)}})),b={operationId:a,operation:"filter",tag:v.tag,sandboxId:v.sandboxId,swarmName:this.config.tag,operationName:o.name,itemIndex:s,verifyRetry:y&&y>1?y-1:void 0,...this.pipelineContextToMeta(o._pipelineContext)};return this.buildResult(v,b,r)};return this.runWithVerification(f,{originalPrompt:n,inputFiles:r,verifyConfig:t,timeoutMs:i,systemPrompt:o.systemPrompt,schema:o.schema,mcpServers:u,skills:d,composio:m,operationId:a,baseTag:p,retry:l,itemIndex:s,operation:"filter",_pipelineContext:o._pipelineContext})}async executeBestOfCandidate(e){let{inputFiles:n,prompt:s,candidateIndex:a,operationId:o,config:i,timeoutMs:l,parentIndex:r,attempt:p=1,_pipelineContext:t}=e,u=r!==void 0?`${this.config.tag}-map-${r}-bestof-cand-${a}`:`${this.config.tag}-bestof-cand-${a}`,d=p>1?`${u}-er${p-1}`:u,m=await this.semaphore.use(()=>this.execute(n,s,{systemPrompt:e.systemPrompt,schema:e.schema,schemaOptions:e.schemaOptions,agent:i.taskAgents?.[a],mcpServers:e.mcpServers,skills:e.skills,composio:e.composio,tagPrefix:d,timeoutMs:l,observability:{swarmName:this.config.tag,operationId:o,operation:"map",itemIndex:r,role:"candidate",candidateIndex:a,errorRetry:p>1?p-1:void 0,...this.pipelineContextToObservability(t)}})),f={operationId:o,operation:"bestof-cand",tag:m.tag,sandboxId:m.sandboxId,swarmName:this.config.tag,itemIndex:a,errorRetry:p>1?p-1:void 0,candidateIndex:a,...this.pipelineContextToMeta(t)};return this.buildResult(m,f)}buildJudgeContext(e){let n=this.buildEvaluatorContext({inputFiles:e.inputFiles,taskPrompt:e.taskPrompt,systemPrompt:e.systemPrompt,schema:e.schema});return e.candidates.forEach((s,a)=>{s.status==="error"&&(n[`candidate_${a}/_failed.txt`]=`STATUS: FAILED
200
214
 
201
- Error: ${s.error??"Unknown error"}`),Object.entries(s.files).forEach(([o,i])=>{n[`candidate_${a}/${o}`]=i;});}),n}async executeBestOfJudge(e){let{candidates:n,config:s,timeoutMs:a,operationId:o,parentIndex:i,attempt:l=1,_pipelineContext:r}=e,p=i!==void 0?`${this.config.tag}-map-${i}-bestof-judge`:`${this.config.tag}-bestof-judge`,t=l>1?`${p}-er${l-1}`:p,u=this.buildJudgeContext({inputFiles:e.inputFiles,taskPrompt:e.taskPrompt,candidates:n,systemPrompt:e.systemPrompt,schema:e.schema}),d=ue(u),m=R(Pe,{candidateCount:String(n.length),criteria:s.judgeCriteria,fileTree:d}),f=zod.z.object({winner:zod.z.number().min(0).max(n.length-1),reasoning:zod.z.string()}),h=await this.semaphore.use(()=>this.execute(u,dt,{systemPrompt:m,schema:f,agent:s.judgeAgent,mcpServers:e.mcpServers,skills:e.skills,composio:e.composio,tagPrefix:t,timeoutMs:a,observability:{swarmName:this.config.tag,operationId:o,operation:"map",itemIndex:i,role:"judge",errorRetry:l>1?l-1:void 0,...this.pipelineContextToObservability(r)}})),g=null;if(h.data&&!h.error)g=h.data;else if(h.rawData)try{let y=JSON.parse(h.rawData);console.warn(`Judge returned invalid winner ${y.winner}, defaulting to candidate 0`),g={winner:0,reasoning:y.reasoning??"Judge failed to provide reasoning"};}catch{console.warn(`Judge validation failed: ${h.error}`);}return {status:g?"success":"error",decision:g,tag:h.tag,sandboxId:h.sandboxId,error:g?void 0:"Judge failed to produce valid decision"}}static DEFAULT_VERIFY_MAX_ATTEMPTS=3;static VerifyDecisionSchema=zod.z.object({passed:zod.z.boolean(),reasoning:zod.z.string(),feedback:zod.z.string().optional()});buildVerifyContext(e){let n=this.buildEvaluatorContext({inputFiles:e.inputFiles,taskPrompt:e.taskPrompt,systemPrompt:e.systemPrompt,schema:e.schema});return Object.entries(e.outputFiles).forEach(([s,a])=>{n[`worker_output/${s}`]=a;}),n}async executeVerify(e){let{config:n,timeoutMs:s,operationId:a,workerTag:o,retryAttempt:i=1,operation:l,itemIndex:r,attemptIndex:p,_pipelineContext:t}=e,u=i>1?`${o}-verifier-er${i-1}`:`${o}-verifier`,d=n.verifierMcpServers??e.mcpServers,m=n.verifierSkills??e.skills,f=n.verifierComposio??e.composio,h=this.buildVerifyContext({inputFiles:e.inputFiles,taskPrompt:e.taskPrompt,outputFiles:e.outputFiles,systemPrompt:e.systemPrompt,schema:e.schema}),g=ue(h),y=R(Re,{criteria:n.criteria,fileTree:g}),v=await this.semaphore.use(()=>this.execute(h,mt,{systemPrompt:y,schema:c.VerifyDecisionSchema,agent:n.verifierAgent,mcpServers:d,skills:m,composio:f,tagPrefix:u,timeoutMs:s,observability:{swarmName:this.config.tag,operationId:a,operation:l,itemIndex:r,role:"verifier",verifyRetry:p&&p>1?p-1:void 0,errorRetry:i>1?i-1:void 0,...this.pipelineContextToObservability(t)}})),b=null;if(v.data&&!v.error)b=v.data;else if(v.rawData)try{let x=JSON.parse(v.rawData);b={passed:!!x.passed,reasoning:x.reasoning??"Verification completed",feedback:x.feedback};}catch{console.warn(`Verify validation failed: ${v.error}`);}return {status:b?"success":"error",decision:b,tag:v.tag,sandboxId:v.sandboxId,error:b?void 0:"Verifier failed to produce valid decision"}}static buildRetryPromptWithFeedback(e,n){return R(Me,{originalPrompt:e,feedback:n})}async runWithVerification(e,n){let{originalPrompt:s,inputFiles:a,verifyConfig:o,timeoutMs:i,operationId:l,baseTag:r,retry:p,itemIndex:t=0,operation:u,_pipelineContext:d}=n,m=o.maxAttempts??c.DEFAULT_VERIFY_MAX_ATTEMPTS,f=s,h=null,g=0;for(;g<m;){g++;let v=g>1?`${r}-vr${g-1}`:r,b=p?await T(w=>e(f,w>1?`${v}-er${w-1}`:v,g),p,t):await e(f,v,g);if(o.onWorkerComplete?.(t,g,b.status==="error"?"error":"success"),b.status==="error")return b;h=b;let x=p?await T(w=>this.executeVerify({inputFiles:a,outputFiles:b.files,taskPrompt:f,config:o,timeoutMs:i,systemPrompt:n.systemPrompt,schema:n.schema,mcpServers:n.mcpServers,skills:n.skills,composio:n.composio,operationId:l,workerTag:v,retryAttempt:w,operation:u,itemIndex:t,attemptIndex:g,_pipelineContext:d}),{...p,retryOn:void 0}):await this.executeVerify({inputFiles:a,outputFiles:b.files,taskPrompt:f,config:o,timeoutMs:i,systemPrompt:n.systemPrompt,schema:n.schema,mcpServers:n.mcpServers,skills:n.skills,composio:n.composio,operationId:l,workerTag:v,operation:u,itemIndex:t,attemptIndex:g,_pipelineContext:d}),C={operationId:l,operation:"verify",tag:x.tag,sandboxId:x.sandboxId,swarmName:this.config.tag,attempts:g,...this.pipelineContextToMeta(d)};if(o.onVerifierComplete?.(t,g,x.decision?.passed??false,x.decision?.feedback),x.decision?.passed)return {...b,verify:{passed:true,reasoning:x.decision.reasoning,verifyMeta:C,attempts:g}};if(g<m){let w=x.decision?.feedback??x.decision?.reasoning??"Output did not meet criteria";f=c.buildRetryPromptWithFeedback(s,w);}}let y=g>1?`${r}-vr${g-1}`:r;return {...h,status:"error",verify:{passed:false,reasoning:"Max verification retries exceeded",verifyMeta:{operationId:l,operation:"verify",tag:`${y}-verifier`,sandboxId:"",swarmName:this.config.tag,attempts:g,...this.pipelineContextToMeta(d)},attempts:g}}}generateOperationId(){return crypto.randomBytes(8).toString("hex")}pipelineContextToObservability(e){return e?{pipelineRunId:e.pipelineRunId,pipelineStepIndex:e.pipelineStepIndex}:{}}pipelineContextToMeta(e){return {pipelineRunId:e?.pipelineRunId,pipelineStepIndex:e?.pipelineStepIndex}}evaluatePrompt(e,n,s){if(typeof e=="string")return e;try{return e(n,s)}catch(a){return a}}buildEvaluatorContext(e){let n={};return e.systemPrompt&&(n["worker_task/system_prompt.txt"]=e.systemPrompt),n["worker_task/user_prompt.txt"]=e.taskPrompt,e.schema&&(n["worker_task/schema.json"]=E(e.schema)?N(e.schema):B(e.schema)),Object.entries(e.inputFiles).forEach(([s,a])=>{n[`worker_task/input/${s}`]=a;}),n}isSwarmResult(e){return typeof e=="object"&&e!==null&&A in e&&e[A]===true}getFiles(e){if(this.isSwarmResult(e)){let n={...e.files};return n["result.json"]&&(n["data.json"]=n["result.json"],delete n["result.json"]),n}return e}getIndex(e,n){return this.isSwarmResult(e)?e.meta.itemIndex:n}buildResult(e,n,s){let a=s??e.files;return e.error?{[A]:true,status:"error",data:null,files:a,meta:n,error:e.error,rawData:e.rawData}:{[A]:true,status:"success",data:e.data,files:a,meta:n}}buildErrorResult(e,n){return {[A]:true,status:"error",data:null,files:{},meta:n,error:e}}};var fe=class c{swarm;steps;events;constructor(e,n=[],s={}){this.swarm=e,this.steps=n,this.events=s;}map(e){return new c(this.swarm,[...this.steps,{type:"map",config:e}],this.events)}filter(e){return new c(this.swarm,[...this.steps,{type:"filter",config:e}],this.events)}reduce(e){return new ge(this.swarm,[...this.steps,{type:"reduce",config:e}],this.events)}on(e,n){if(typeof e=="string"){let s=`on${e.charAt(0).toUpperCase()}${e.slice(1)}`;return new c(this.swarm,this.steps,{...this.events,[s]:n})}return new c(this.swarm,this.steps,{...this.events,...e})}async run(e){let n=crypto.randomBytes(8).toString("hex"),s=[],a=e,o=Date.now();for(let l=0;l<this.steps.length;l++){let r=this.steps[l],p=r.config.name,t=Date.now();this.events.onStepStart?.({type:r.type,index:l,name:p,itemCount:a.length});let u={pipelineRunId:n,pipelineStepIndex:l};try{let d=await this.executeStep(r,a,l,p,u),m=Date.now()-t;if(s.push({type:r.type,index:l,durationMs:m,results:d.output}),this.events.onStepComplete?.({type:r.type,index:l,name:p,durationMs:m,successCount:d.successCount,errorCount:d.errorCount,filteredCount:d.filteredCount}),r.type==="reduce")return {pipelineRunId:n,steps:s,output:d.output,totalDurationMs:Date.now()-o};a=d.nextItems;}catch(d){throw this.events.onStepError?.({type:r.type,index:l,name:p,error:d instanceof Error?d:new Error(String(d))}),d}}let i=s[s.length-1];return {pipelineRunId:n,steps:s,output:i?.results??[],totalDurationMs:Date.now()-o}}async executeStep(e,n,s,a,o){if(e.type==="map"){let r=e.config,p=await this.swarm.map({items:n,...r,_pipelineContext:o,retry:this.wrapRetry(r.retry,s,a),verify:this.wrapVerify(r.verify,s,a),bestOf:this.wrapBestOf(r.bestOf,s,a)});return {output:[...p],nextItems:p.success,successCount:p.success.length,errorCount:p.error.length,filteredCount:0}}if(e.type==="filter"){let r=e.config,p=await this.swarm.filter({items:n,...r,_pipelineContext:o,retry:this.wrapRetry(r.retry,s,a),verify:this.wrapVerify(r.verify,s,a)}),t=r.emit??"success",u=t==="success"?p.success:t==="filtered"?p.filtered:[...p.success,...p.filtered];return {output:[...p],nextItems:u,successCount:p.success.length,errorCount:p.error.length,filteredCount:p.filtered.length}}let i=e.config,l=await this.swarm.reduce({items:n,...i,_pipelineContext:o,retry:this.wrapRetry(i.retry,s,a),verify:this.wrapVerify(i.verify,s,a)});return {output:l,nextItems:[],successCount:l.status==="success"?1:0,errorCount:l.status==="error"?1:0,filteredCount:0}}wrapRetry(e,n,s){if(e)return {...e,onItemRetry:(a,o,i)=>{e.onItemRetry?.(a,o,i),this.events.onItemRetry?.({stepIndex:n,stepName:s,itemIndex:a,attempt:o,error:i});}}}wrapVerify(e,n,s){if(e)return {...e,onWorkerComplete:(a,o,i)=>{e.onWorkerComplete?.(a,o,i),this.events.onWorkerComplete?.({stepIndex:n,stepName:s,itemIndex:a,attempt:o,status:i});},onVerifierComplete:(a,o,i,l)=>{e.onVerifierComplete?.(a,o,i,l),this.events.onVerifierComplete?.({stepIndex:n,stepName:s,itemIndex:a,attempt:o,passed:i,feedback:l});}}}wrapBestOf(e,n,s){if(e)return {...e,onCandidateComplete:(a,o,i)=>{e.onCandidateComplete?.(a,o,i),this.events.onCandidateComplete?.({stepIndex:n,stepName:s,itemIndex:a,candidateIndex:o,status:i});},onJudgeComplete:(a,o,i)=>{e.onJudgeComplete?.(a,o,i),this.events.onJudgeComplete?.({stepIndex:n,stepName:s,itemIndex:a,winnerIndex:o,reasoning:i});}}}},ge=class c extends fe{constructor(e,n,s){super(e,n,s);}on(e,n){if(typeof e=="string"){let s=`on${e.charAt(0).toUpperCase()}${e.slice(1)}`;return new c(this.swarm,this.steps,{...this.events,[s]:n})}return new c(this.swarm,this.steps,{...this.events,...e})}map(){throw new Error("Cannot add steps after reduce")}filter(){throw new Error("Cannot add steps after reduce")}reduce(){throw new Error("Cannot add steps after reduce")}};Object.defineProperty(exports,"E2BProvider",{enumerable:true,get:function(){return e2b.E2BProvider}});Object.defineProperty(exports,"DaytonaProvider",{enumerable:true,get:function(){return daytona.DaytonaProvider}});Object.defineProperty(exports,"ModalProvider",{enumerable:true,get:function(){return modal.ModalProvider}});exports.AGENT_REGISTRY=ye;exports.AGENT_TYPES=kt;exports.Agent=z;exports.Evolve=H;exports.JUDGE_PROMPT=Pe;exports.Pipeline=fe;exports.RETRY_FEEDBACK_PROMPT=Me;exports.SCHEMA_PROMPT=ut;exports.SWARM_RESULT_BRAND=A;exports.SYSTEM_PROMPT=pt;exports.Semaphore=L;exports.Swarm=$e;exports.SwarmResultList=j;exports.TerminalPipeline=ge;exports.VALIDATION_PRESETS=he;exports.VERIFY_PROMPT=Re;exports.WORKSPACE_PROMPT=ct;exports.WORKSPACE_SWE_PROMPT=lt;exports.applyTemplate=R;exports.buildWorkerSystemPrompt=Oe;exports.createAgentParser=O;exports.createClaudeParser=ne;exports.createCodexParser=se;exports.createGeminiParser=oe;exports.executeWithRetry=T;exports.expandPath=ve;exports.getAgentConfig=M;exports.getMcpSettingsDir=U;exports.getMcpSettingsPath=$;exports.isValidAgentType=Tt;exports.isZodSchema=E;exports.jsonSchemaToString=B;exports.parseNdjsonLine=Ot;exports.parseNdjsonOutput=It;exports.parseQwenOutput=Le;exports.readLocalDir=qe;exports.saveLocalDir=Ze;exports.writeClaudeMcpConfig=Q;exports.writeCodexMcpConfig=te;exports.writeGeminiMcpConfig=X;exports.writeMcpConfig=Ce;exports.writeQwenMcpConfig=ee;exports.zodSchemaToJson=N;
215
+ Error: ${s.error??"Unknown error"}`),Object.entries(s.files).forEach(([o,i])=>{n[`candidate_${a}/${o}`]=i;});}),n}async executeBestOfJudge(e){let{candidates:n,config:s,timeoutMs:a,operationId:o,parentIndex:i,attempt:l=1,_pipelineContext:r}=e,p=i!==void 0?`${this.config.tag}-map-${i}-bestof-judge`:`${this.config.tag}-bestof-judge`,t=l>1?`${p}-er${l-1}`:p,u=this.buildJudgeContext({inputFiles:e.inputFiles,taskPrompt:e.taskPrompt,candidates:n,systemPrompt:e.systemPrompt,schema:e.schema}),d=ue(u),m=R(Ee,{candidateCount:String(n.length),criteria:s.judgeCriteria,fileTree:d}),f=zod.z.object({winner:zod.z.number().min(0).max(n.length-1),reasoning:zod.z.string()}),h=await this.semaphore.use(()=>this.execute(u,dt,{systemPrompt:m,schema:f,agent:s.judgeAgent,mcpServers:e.mcpServers,skills:e.skills,composio:e.composio,tagPrefix:t,timeoutMs:a,observability:{swarmName:this.config.tag,operationId:o,operation:"map",itemIndex:i,role:"judge",errorRetry:l>1?l-1:void 0,...this.pipelineContextToObservability(r)}})),g=null;if(h.data&&!h.error)g=h.data;else if(h.rawData)try{let y=JSON.parse(h.rawData);console.warn(`Judge returned invalid winner ${y.winner}, defaulting to candidate 0`),g={winner:0,reasoning:y.reasoning??"Judge failed to provide reasoning"};}catch{console.warn(`Judge validation failed: ${h.error}`);}return {status:g?"success":"error",decision:g,tag:h.tag,sandboxId:h.sandboxId,error:g?void 0:"Judge failed to produce valid decision"}}static DEFAULT_VERIFY_MAX_ATTEMPTS=3;static VerifyDecisionSchema=zod.z.object({passed:zod.z.boolean(),reasoning:zod.z.string(),feedback:zod.z.string().optional()});buildVerifyContext(e){let n=this.buildEvaluatorContext({inputFiles:e.inputFiles,taskPrompt:e.taskPrompt,systemPrompt:e.systemPrompt,schema:e.schema});return Object.entries(e.outputFiles).forEach(([s,a])=>{n[`worker_output/${s}`]=a;}),n}async executeVerify(e){let{config:n,timeoutMs:s,operationId:a,workerTag:o,retryAttempt:i=1,operation:l,itemIndex:r,attemptIndex:p,_pipelineContext:t}=e,u=i>1?`${o}-verifier-er${i-1}`:`${o}-verifier`,d=n.verifierMcpServers??e.mcpServers,m=n.verifierSkills??e.skills,f=n.verifierComposio??e.composio,h=this.buildVerifyContext({inputFiles:e.inputFiles,taskPrompt:e.taskPrompt,outputFiles:e.outputFiles,systemPrompt:e.systemPrompt,schema:e.schema}),g=ue(h),y=R(Re,{criteria:n.criteria,fileTree:g}),v=await this.semaphore.use(()=>this.execute(h,mt,{systemPrompt:y,schema:c.VerifyDecisionSchema,agent:n.verifierAgent,mcpServers:d,skills:m,composio:f,tagPrefix:u,timeoutMs:s,observability:{swarmName:this.config.tag,operationId:a,operation:l,itemIndex:r,role:"verifier",verifyRetry:p&&p>1?p-1:void 0,errorRetry:i>1?i-1:void 0,...this.pipelineContextToObservability(t)}})),b=null;if(v.data&&!v.error)b=v.data;else if(v.rawData)try{let x=JSON.parse(v.rawData);b={passed:!!x.passed,reasoning:x.reasoning??"Verification completed",feedback:x.feedback};}catch{console.warn(`Verify validation failed: ${v.error}`);}return {status:b?"success":"error",decision:b,tag:v.tag,sandboxId:v.sandboxId,error:b?void 0:"Verifier failed to produce valid decision"}}static buildRetryPromptWithFeedback(e,n){return R(Me,{originalPrompt:e,feedback:n})}async runWithVerification(e,n){let{originalPrompt:s,inputFiles:a,verifyConfig:o,timeoutMs:i,operationId:l,baseTag:r,retry:p,itemIndex:t=0,operation:u,_pipelineContext:d}=n,m=o.maxAttempts??c.DEFAULT_VERIFY_MAX_ATTEMPTS,f=s,h=null,g=0;for(;g<m;){g++;let v=g>1?`${r}-vr${g-1}`:r,b=p?await _(C=>e(f,C>1?`${v}-er${C-1}`:v,g),p,t):await e(f,v,g);if(o.onWorkerComplete?.(t,g,b.status==="error"?"error":"success"),b.status==="error")return b;h=b;let x=p?await _(C=>this.executeVerify({inputFiles:a,outputFiles:b.files,taskPrompt:f,config:o,timeoutMs:i,systemPrompt:n.systemPrompt,schema:n.schema,mcpServers:n.mcpServers,skills:n.skills,composio:n.composio,operationId:l,workerTag:v,retryAttempt:C,operation:u,itemIndex:t,attemptIndex:g,_pipelineContext:d}),{...p,retryOn:void 0}):await this.executeVerify({inputFiles:a,outputFiles:b.files,taskPrompt:f,config:o,timeoutMs:i,systemPrompt:n.systemPrompt,schema:n.schema,mcpServers:n.mcpServers,skills:n.skills,composio:n.composio,operationId:l,workerTag:v,operation:u,itemIndex:t,attemptIndex:g,_pipelineContext:d}),w={operationId:l,operation:"verify",tag:x.tag,sandboxId:x.sandboxId,swarmName:this.config.tag,attempts:g,...this.pipelineContextToMeta(d)};if(o.onVerifierComplete?.(t,g,x.decision?.passed??false,x.decision?.feedback),x.decision?.passed)return {...b,verify:{passed:true,reasoning:x.decision.reasoning,verifyMeta:w,attempts:g}};if(g<m){let C=x.decision?.feedback??x.decision?.reasoning??"Output did not meet criteria";f=c.buildRetryPromptWithFeedback(s,C);}}let y=g>1?`${r}-vr${g-1}`:r;return {...h,status:"error",verify:{passed:false,reasoning:"Max verification retries exceeded",verifyMeta:{operationId:l,operation:"verify",tag:`${y}-verifier`,sandboxId:"",swarmName:this.config.tag,attempts:g,...this.pipelineContextToMeta(d)},attempts:g}}}generateOperationId(){return crypto.randomBytes(8).toString("hex")}pipelineContextToObservability(e){return e?{pipelineRunId:e.pipelineRunId,pipelineStepIndex:e.pipelineStepIndex}:{}}pipelineContextToMeta(e){return {pipelineRunId:e?.pipelineRunId,pipelineStepIndex:e?.pipelineStepIndex}}evaluatePrompt(e,n,s){if(typeof e=="string")return e;try{return e(n,s)}catch(a){return a}}buildEvaluatorContext(e){let n={};return e.systemPrompt&&(n["worker_task/system_prompt.txt"]=e.systemPrompt),n["worker_task/user_prompt.txt"]=e.taskPrompt,e.schema&&(n["worker_task/schema.json"]=P(e.schema)?N(e.schema):B(e.schema)),Object.entries(e.inputFiles).forEach(([s,a])=>{n[`worker_task/input/${s}`]=a;}),n}isSwarmResult(e){return typeof e=="object"&&e!==null&&A in e&&e[A]===true}getFiles(e){if(this.isSwarmResult(e)){let n={...e.files};return n["result.json"]&&(n["data.json"]=n["result.json"],delete n["result.json"]),n}return e}getIndex(e,n){return this.isSwarmResult(e)?e.meta.itemIndex:n}buildResult(e,n,s){let a=s??e.files;return e.error?{[A]:true,status:"error",data:null,files:a,meta:n,error:e.error,rawData:e.rawData}:{[A]:true,status:"success",data:e.data,files:a,meta:n}}buildErrorResult(e,n){return {[A]:true,status:"error",data:null,files:{},meta:n,error:e}}};var fe=class c{swarm;steps;events;constructor(e,n=[],s={}){this.swarm=e,this.steps=n,this.events=s;}map(e){return new c(this.swarm,[...this.steps,{type:"map",config:e}],this.events)}filter(e){return new c(this.swarm,[...this.steps,{type:"filter",config:e}],this.events)}reduce(e){return new ge(this.swarm,[...this.steps,{type:"reduce",config:e}],this.events)}on(e,n){if(typeof e=="string"){let s=`on${e.charAt(0).toUpperCase()}${e.slice(1)}`;return new c(this.swarm,this.steps,{...this.events,[s]:n})}return new c(this.swarm,this.steps,{...this.events,...e})}async run(e){let n=crypto.randomBytes(8).toString("hex"),s=[],a=e,o=Date.now();for(let l=0;l<this.steps.length;l++){let r=this.steps[l],p=r.config.name,t=Date.now();this.events.onStepStart?.({type:r.type,index:l,name:p,itemCount:a.length});let u={pipelineRunId:n,pipelineStepIndex:l};try{let d=await this.executeStep(r,a,l,p,u),m=Date.now()-t;if(s.push({type:r.type,index:l,durationMs:m,results:d.output}),this.events.onStepComplete?.({type:r.type,index:l,name:p,durationMs:m,successCount:d.successCount,errorCount:d.errorCount,filteredCount:d.filteredCount}),r.type==="reduce")return {pipelineRunId:n,steps:s,output:d.output,totalDurationMs:Date.now()-o};a=d.nextItems;}catch(d){throw this.events.onStepError?.({type:r.type,index:l,name:p,error:d instanceof Error?d:new Error(String(d))}),d}}let i=s[s.length-1];return {pipelineRunId:n,steps:s,output:i?.results??[],totalDurationMs:Date.now()-o}}async executeStep(e,n,s,a,o){if(e.type==="map"){let r=e.config,p=await this.swarm.map({items:n,...r,_pipelineContext:o,retry:this.wrapRetry(r.retry,s,a),verify:this.wrapVerify(r.verify,s,a),bestOf:this.wrapBestOf(r.bestOf,s,a)});return {output:[...p],nextItems:p.success,successCount:p.success.length,errorCount:p.error.length,filteredCount:0}}if(e.type==="filter"){let r=e.config,p=await this.swarm.filter({items:n,...r,_pipelineContext:o,retry:this.wrapRetry(r.retry,s,a),verify:this.wrapVerify(r.verify,s,a)}),t=r.emit??"success",u=t==="success"?p.success:t==="filtered"?p.filtered:[...p.success,...p.filtered];return {output:[...p],nextItems:u,successCount:p.success.length,errorCount:p.error.length,filteredCount:p.filtered.length}}let i=e.config,l=await this.swarm.reduce({items:n,...i,_pipelineContext:o,retry:this.wrapRetry(i.retry,s,a),verify:this.wrapVerify(i.verify,s,a)});return {output:l,nextItems:[],successCount:l.status==="success"?1:0,errorCount:l.status==="error"?1:0,filteredCount:0}}wrapRetry(e,n,s){if(e)return {...e,onItemRetry:(a,o,i)=>{e.onItemRetry?.(a,o,i),this.events.onItemRetry?.({stepIndex:n,stepName:s,itemIndex:a,attempt:o,error:i});}}}wrapVerify(e,n,s){if(e)return {...e,onWorkerComplete:(a,o,i)=>{e.onWorkerComplete?.(a,o,i),this.events.onWorkerComplete?.({stepIndex:n,stepName:s,itemIndex:a,attempt:o,status:i});},onVerifierComplete:(a,o,i,l)=>{e.onVerifierComplete?.(a,o,i,l),this.events.onVerifierComplete?.({stepIndex:n,stepName:s,itemIndex:a,attempt:o,passed:i,feedback:l});}}}wrapBestOf(e,n,s){if(e)return {...e,onCandidateComplete:(a,o,i)=>{e.onCandidateComplete?.(a,o,i),this.events.onCandidateComplete?.({stepIndex:n,stepName:s,itemIndex:a,candidateIndex:o,status:i});},onJudgeComplete:(a,o,i)=>{e.onJudgeComplete?.(a,o,i),this.events.onJudgeComplete?.({stepIndex:n,stepName:s,itemIndex:a,winnerIndex:o,reasoning:i});}}}},ge=class c extends fe{constructor(e,n,s){super(e,n,s);}on(e,n){if(typeof e=="string"){let s=`on${e.charAt(0).toUpperCase()}${e.slice(1)}`;return new c(this.swarm,this.steps,{...this.events,[s]:n})}return new c(this.swarm,this.steps,{...this.events,...e})}map(){throw new Error("Cannot add steps after reduce")}filter(){throw new Error("Cannot add steps after reduce")}reduce(){throw new Error("Cannot add steps after reduce")}};Object.defineProperty(exports,"E2BProvider",{enumerable:true,get:function(){return e2b.E2BProvider}});Object.defineProperty(exports,"DaytonaProvider",{enumerable:true,get:function(){return daytona.DaytonaProvider}});Object.defineProperty(exports,"ModalProvider",{enumerable:true,get:function(){return modal.ModalProvider}});exports.AGENT_REGISTRY=ye;exports.AGENT_TYPES=kt;exports.Agent=z;exports.Evolve=H;exports.JUDGE_PROMPT=Ee;exports.Pipeline=fe;exports.RETRY_FEEDBACK_PROMPT=Me;exports.SCHEMA_PROMPT=ut;exports.SWARM_RESULT_BRAND=A;exports.SYSTEM_PROMPT=pt;exports.Semaphore=j;exports.Swarm=$e;exports.SwarmResultList=L;exports.TerminalPipeline=ge;exports.VALIDATION_PRESETS=he;exports.VERIFY_PROMPT=Re;exports.WORKSPACE_PROMPT=ct;exports.WORKSPACE_SWE_PROMPT=lt;exports.applyTemplate=R;exports.buildWorkerSystemPrompt=Oe;exports.createAgentParser=O;exports.createClaudeParser=ne;exports.createCodexParser=se;exports.createGeminiParser=oe;exports.executeWithRetry=_;exports.expandPath=ve;exports.getAgentConfig=M;exports.getMcpSettingsDir=U;exports.getMcpSettingsPath=$;exports.isValidAgentType=_t;exports.isZodSchema=P;exports.jsonSchemaToString=B;exports.parseNdjsonLine=Ot;exports.parseNdjsonOutput=It;exports.parseQwenOutput=je;exports.readLocalDir=qe;exports.saveLocalDir=Ze;exports.writeClaudeMcpConfig=Q;exports.writeCodexMcpConfig=te;exports.writeGeminiMcpConfig=X;exports.writeMcpConfig=we;exports.writeQwenMcpConfig=ee;exports.zodSchemaToJson=N;
package/dist/index.js CHANGED
@@ -1,9 +1,9 @@
1
- import {EventEmitter}from'events';import tn from'ajv';import At from'zod-to-json-schema';import*as P from'fs';import*as Y from'path';import {join}from'path';import {mkdir,appendFile}from'fs/promises';import {homedir}from'os';import {randomBytes}from'crypto';import {Composio,AuthScheme}from'@composio/core';import {z as z$1}from'zod';export{E2BProvider}from'@evolvingmachines/e2b';export{DaytonaProvider}from'@evolvingmachines/daytona';export{ModalProvider}from'@evolvingmachines/modal';var kt={CLAUDE:"claude",CODEX:"codex",GEMINI:"gemini",QWEN:"qwen"},he={strict:{coerceTypes:false,removeAdditional:false,useDefaults:false,allErrors:true},loose:{coerceTypes:"array",removeAdditional:false,useDefaults:true,allErrors:true}};var ye={claude:{image:"evolve-all",apiKeyEnv:"ANTHROPIC_API_KEY",oauthEnv:"CLAUDE_CODE_OAUTH_TOKEN",baseUrlEnv:"ANTHROPIC_BASE_URL",defaultModel:"opus",models:[{alias:"opus",modelId:"claude-opus-4-5-20251101",description:"Complex reasoning, R&D, architecting"},{alias:"sonnet",modelId:"claude-sonnet-4-5-20250929",description:"Daily coding, features, tests"},{alias:"haiku",modelId:"claude-haiku-4-5-20251001",description:"Quick tasks, syntax correction"}],systemPromptFile:"CLAUDE.md",mcpConfig:{settingsDir:"~/.claude",filename:"settings.json",format:"json",projectConfig:true},availableBetas:{CONTEXT_1M:"context-1m-2025-08-07"},skillsConfig:{sourceDir:"/home/user/.evolve/skills",targetDir:"/home/user/.claude/skills"},buildCommand:({prompt:c,model:e,isResume:n,betas:s})=>{let a=n?"--continue ":"",o=s?.length?`--betas ${s.join(",")} `:"";return `echo "${c}" | claude -p ${a}${o}--model ${e} --output-format stream-json --verbose --dangerously-skip-permissions`}},codex:{image:"evolve-all",apiKeyEnv:"OPENAI_API_KEY",oauthEnv:"CODEX_OAUTH_FILE_PATH",oauthFileName:"auth.json",baseUrlEnv:"OPENAI_BASE_URL",defaultModel:"gpt-5.2",models:[{alias:"gpt-5.2",modelId:"gpt-5.2",description:"Base model"},{alias:"gpt-5.2-codex",modelId:"gpt-5.2-codex",description:"Code-optimized"},{alias:"gpt-5.1-codex-max",modelId:"gpt-5.1-codex-max",description:"Best reasoning (xhigh effort)"},{alias:"gpt-5.1-mini",modelId:"gpt-5.1-mini",description:"Smaller/faster"}],systemPromptFile:"AGENTS.md",mcpConfig:{settingsDir:"~/.codex",filename:"config.toml",format:"toml"},skillsConfig:{sourceDir:"/home/user/.evolve/skills",targetDir:"/home/user/.codex/skills"},setupCommand:`printf '%s\\n' "$OPENAI_API_KEY" | codex login --with-api-key`,buildCommand:({prompt:c,model:e,isResume:n,reasoningEffort:s})=>{let a=s?` -c model_reasoning_effort="${s}"`:"";return `printf '%s' "${c}" | codex exec --model ${e}${a} --dangerously-bypass-approvals-and-sandbox --skip-git-repo-check --json${n?" resume --last":""}`}},gemini:{image:"evolve-all",apiKeyEnv:"GEMINI_API_KEY",oauthEnv:"GEMINI_OAUTH_FILE_PATH",oauthFileName:"oauth_creds.json",oauthActivationEnv:{key:"GOOGLE_GENAI_USE_GCA",value:"true"},baseUrlEnv:"GOOGLE_GEMINI_BASE_URL",defaultModel:"gemini-3-flash-preview",models:[{alias:"gemini-3-pro-preview",modelId:"gemini-3-pro-preview",description:"Latest pro preview"},{alias:"gemini-3-flash-preview",modelId:"gemini-3-flash-preview",description:"Latest flash preview"},{alias:"gemini-2.5-pro",modelId:"gemini-2.5-pro",description:"Complex tasks, deep reasoning"},{alias:"gemini-2.5-flash",modelId:"gemini-2.5-flash",description:"Balance speed/reasoning"},{alias:"gemini-2.5-flash-lite",modelId:"gemini-2.5-flash-lite",description:"Simple tasks, fastest"}],systemPromptFile:"GEMINI.md",mcpConfig:{settingsDir:"~/.gemini",filename:"settings.json",format:"json"},skillsConfig:{sourceDir:"/home/user/.evolve/skills",targetDir:"/home/user/.gemini/skills"},usePassthroughGateway:true,buildCommand:({prompt:c,model:e,isResume:n})=>`gemini "${c}" ${n?"--resume latest ":""}--model ${e} --yolo --output-format stream-json`},qwen:{image:"evolve-all",apiKeyEnv:"OPENAI_API_KEY",baseUrlEnv:"OPENAI_BASE_URL",defaultModel:"qwen3-coder-plus",models:[{alias:"qwen3-coder-plus",modelId:"qwen3-coder-plus",description:"Code generation, debugging"},{alias:"qwen3-vl-plus",modelId:"qwen3-vl-plus",description:"Vision + language, multimodal"}],systemPromptFile:"QWEN.md",mcpConfig:{settingsDir:"~/.qwen",filename:"settings.json",format:"json"},skillsConfig:{sourceDir:"/home/user/.evolve/skills",targetDir:"/home/user/.qwen/skills",enableFlag:"--experimental-skills"},defaultBaseUrl:"https://dashscope-intl.aliyuncs.com/compatible-mode/v1",buildCommand:({prompt:c,model:e,isResume:n,isDirectMode:s,skills:a})=>{let o=n?"--continue ":"",i=a?.length?"--experimental-skills ":"",l=s||e.startsWith("dashscope/")?e:`dashscope/${e}`;return `qwen "${c}" ${o}${i}--model ${l} --yolo --output-format stream-json`}}};function M(c){let e=ye[c];if(!e)throw new Error(`Unknown agent type: ${c}`);return e}function Tt(c){return c in ye}function ve(c){return c.replace(/^~/,"/home/user")}function $(c){let e=M(c);return `${ve(e.mcpConfig.settingsDir)}/${e.mcpConfig.filename}`}function U(c){let e=M(c);return ve(e.mcpConfig.settingsDir)}function V(c){if(c instanceof Error){let e=c.message.toLowerCase();return e.includes("not found")||e.includes("enoent")||e.includes("no such file")||e.includes("does not exist")}return false}function xe(c,e){let n=[e.command,e.url].filter(Boolean);if(n.length===0)throw new Error(`MCP server "${c}" must specify command or url`);if(n.length>1)throw new Error(`MCP server "${c}" cannot specify both command and url`)}function be(c){for(let[e,n]of Object.entries(c))xe(e,n);}function we(c){return c.type?c.type:c.command?"stdio":"sse"}function _t(c){let e=we(c);return {...c,type:e}}function Et(c){let{type:e,url:n,...s}=c;return we(c)==="http"&&n?{httpUrl:n,...s}:n?{url:n,...s}:s}function Pt(c){return {type:we(c),...c}}async function Be(c,e,n,s){be(n);let a=U(e),o=$(e);await c.files.makeDir(a);let i={};try{let r=await c.files.read(o);typeof r=="string"&&(i=JSON.parse(r));}catch(r){if(!V(r))throw r}let l=Object.fromEntries(Object.entries(n).map(([r,p])=>[r,s(p)]));await c.files.write(o,JSON.stringify({...i,mcpServers:l},null,2));}async function Q(c,e,n){be(n);let s=U("claude"),a=$("claude"),o=Object.fromEntries(Object.entries(n).map(([l,r])=>[l,Pt(r)]));await c.files.write(`${e}/.mcp.json`,JSON.stringify({mcpServers:o},null,2)),await c.files.makeDir(s);let i={};try{let l=await c.files.read(a);typeof l=="string"&&(i=JSON.parse(l));}catch(l){if(!V(l))throw l}i.enableAllProjectMcpServers=true,await c.files.write(a,JSON.stringify(i,null,2));}async function X(c,e){await Be(c,"gemini",e,_t);}async function ee(c,e){await Be(c,"qwen",e,Et);}function _(c){return typeof c=="string"?`"${c.replace(/\\/g,"\\\\").replace(/"/g,'\\"')}"`:Array.isArray(c)?`[${c.map(e=>_(e)).join(", ")}]`:typeof c=="object"&&c!==null?`{ ${Object.entries(c).map(([n,s])=>`${n} = ${_(s)}`).join(", ")} }`:typeof c=="boolean"?c?"true":"false":String(c)}async function te(c,e){for(let[r,p]of Object.entries(e))xe(r,p);let n=U("codex"),s=$("codex");await c.files.makeDir(n);let a="";try{let r=await c.files.read(s);typeof r=="string"&&(a=r);}catch(r){if(!V(r))throw r}let o=[],i=[];a.includes("experimental_use_rmcp_client")||(o.push("# Enable improved RMCP client (recommended)"),o.push("experimental_use_rmcp_client = true")),a.includes("[mcp_servers]")||i.push("[mcp_servers]","");for(let[r,p]of Object.entries(e)){if(a.includes(`[mcp_servers.${r}]`))continue;i.push(`[mcp_servers.${r}]`);let t=p.type??(p.command?"stdio":"http");if(t==="http"||t==="sse"||!!p.url){if(!p.url)throw new Error(`MCP server "${r}" is missing url for ${t} transport`);i.push(`url = ${_(p.url)}`),p.bearerTokenEnvVar&&i.push(`bearer_token_env_var = ${_(p.bearerTokenEnvVar)}`);let d=p.httpHeaders??p.headers;d&&Object.keys(d).length>0&&i.push(`http_headers = ${_(d)}`),p.envHttpHeaders&&Object.keys(p.envHttpHeaders).length>0&&i.push(`env_http_headers = ${_(p.envHttpHeaders)}`);}else i.push(`command = ${_(p.command)}`),p.args&&p.args.length>0&&i.push(`args = ${_(p.args)}`),p.cwd&&i.push(`cwd = ${_(p.cwd)}`);p.env&&Object.keys(p.env).length>0&&i.push(`env = ${_(p.env)}`),p.envVars&&p.envVars.length>0&&i.push(`env_vars = ${_(p.envVars)}`),i.push("");}let l=[a.trim(),o.join(`
1
+ import {EventEmitter}from'events';import tn from'ajv';import At from'zod-to-json-schema';import*as E from'fs';import*as Y from'path';import {join}from'path';import {mkdir,appendFile}from'fs/promises';import {homedir}from'os';import {randomBytes}from'crypto';import {Composio,AuthScheme}from'@composio/core';import {z as z$1}from'zod';export{E2BProvider}from'@evolvingmachines/e2b';export{DaytonaProvider}from'@evolvingmachines/daytona';export{ModalProvider}from'@evolvingmachines/modal';var kt={CLAUDE:"claude",CODEX:"codex",GEMINI:"gemini",QWEN:"qwen"},he={strict:{coerceTypes:false,removeAdditional:false,useDefaults:false,allErrors:true},loose:{coerceTypes:"array",removeAdditional:false,useDefaults:true,allErrors:true}};var ye={claude:{image:"evolve-all",apiKeyEnv:"ANTHROPIC_API_KEY",oauthEnv:"CLAUDE_CODE_OAUTH_TOKEN",baseUrlEnv:"ANTHROPIC_BASE_URL",defaultModel:"opus",models:[{alias:"opus",modelId:"claude-opus-4-5-20251101",description:"Complex reasoning, R&D, architecting"},{alias:"sonnet",modelId:"claude-sonnet-4-5-20250929",description:"Daily coding, features, tests"},{alias:"haiku",modelId:"claude-haiku-4-5-20251001",description:"Quick tasks, syntax correction"}],systemPromptFile:"CLAUDE.md",mcpConfig:{settingsDir:"~/.claude",filename:"settings.json",format:"json",projectConfig:true},availableBetas:{CONTEXT_1M:"context-1m-2025-08-07"},skillsConfig:{sourceDir:"/home/user/.evolve/skills",targetDir:"/home/user/.claude/skills"},buildCommand:({prompt:c,model:e,isResume:n,betas:s})=>{let a=n?"--continue ":"",o=s?.length?`--betas ${s.join(",")} `:"";return `echo "${c}" | claude -p ${a}${o}--model ${e} --output-format stream-json --verbose --dangerously-skip-permissions`}},codex:{image:"evolve-all",apiKeyEnv:"OPENAI_API_KEY",oauthEnv:"CODEX_OAUTH_FILE_PATH",oauthFileName:"auth.json",baseUrlEnv:"OPENAI_BASE_URL",defaultModel:"gpt-5.2",models:[{alias:"gpt-5.2",modelId:"gpt-5.2",description:"Base model"},{alias:"gpt-5.2-codex",modelId:"gpt-5.2-codex",description:"Code-optimized"},{alias:"gpt-5.1-codex-max",modelId:"gpt-5.1-codex-max",description:"Best reasoning (xhigh effort)"},{alias:"gpt-5.1-mini",modelId:"gpt-5.1-mini",description:"Smaller/faster"}],systemPromptFile:"AGENTS.md",mcpConfig:{settingsDir:"~/.codex",filename:"config.toml",format:"toml"},skillsConfig:{sourceDir:"/home/user/.evolve/skills",targetDir:"/home/user/.codex/skills"},setupCommand:`printf '%s\\n' "$OPENAI_API_KEY" | codex login --with-api-key`,buildCommand:({prompt:c,model:e,isResume:n,reasoningEffort:s})=>{let a=s?` -c model_reasoning_effort="${s}"`:"";return `printf '%s' "${c}" | codex exec --model ${e}${a} --dangerously-bypass-approvals-and-sandbox --skip-git-repo-check --json${n?" resume --last":""}`}},gemini:{image:"evolve-all",apiKeyEnv:"GEMINI_API_KEY",oauthEnv:"GEMINI_OAUTH_FILE_PATH",oauthFileName:"oauth_creds.json",oauthActivationEnv:{key:"GOOGLE_GENAI_USE_GCA",value:"true"},baseUrlEnv:"GOOGLE_GEMINI_BASE_URL",defaultModel:"gemini-3-flash-preview",models:[{alias:"gemini-3-pro-preview",modelId:"gemini-3-pro-preview",description:"Latest pro preview"},{alias:"gemini-3-flash-preview",modelId:"gemini-3-flash-preview",description:"Latest flash preview"},{alias:"gemini-2.5-pro",modelId:"gemini-2.5-pro",description:"Complex tasks, deep reasoning"},{alias:"gemini-2.5-flash",modelId:"gemini-2.5-flash",description:"Balance speed/reasoning"},{alias:"gemini-2.5-flash-lite",modelId:"gemini-2.5-flash-lite",description:"Simple tasks, fastest"}],systemPromptFile:"GEMINI.md",mcpConfig:{settingsDir:"~/.gemini",filename:"settings.json",format:"json"},skillsConfig:{sourceDir:"/home/user/.evolve/skills",targetDir:"/home/user/.gemini/skills"},usePassthroughGateway:true,buildCommand:({prompt:c,model:e,isResume:n})=>`gemini "${c}" ${n?"--resume latest ":""}--model ${e} --yolo --output-format stream-json`},qwen:{image:"evolve-all",apiKeyEnv:"OPENAI_API_KEY",baseUrlEnv:"OPENAI_BASE_URL",defaultModel:"qwen3-coder-plus",models:[{alias:"qwen3-coder-plus",modelId:"qwen3-coder-plus",description:"Code generation, debugging"},{alias:"qwen3-vl-plus",modelId:"qwen3-vl-plus",description:"Vision + language, multimodal"}],systemPromptFile:"QWEN.md",mcpConfig:{settingsDir:"~/.qwen",filename:"settings.json",format:"json"},skillsConfig:{sourceDir:"/home/user/.evolve/skills",targetDir:"/home/user/.qwen/skills",enableFlag:"--experimental-skills"},defaultBaseUrl:"https://dashscope-intl.aliyuncs.com/compatible-mode/v1",buildCommand:({prompt:c,model:e,isResume:n,isDirectMode:s,skills:a})=>{let o=n?"--continue ":"",i=a?.length?"--experimental-skills ":"",l=s||e.startsWith("dashscope/")?e:`dashscope/${e}`;return `qwen "${c}" ${o}${i}--model ${l} --yolo --output-format stream-json`}}};function M(c){let e=ye[c];if(!e)throw new Error(`Unknown agent type: ${c}`);return e}function _t(c){return c in ye}function ve(c){return c.replace(/^~/,"/home/user")}function $(c){let e=M(c);return `${ve(e.mcpConfig.settingsDir)}/${e.mcpConfig.filename}`}function U(c){let e=M(c);return ve(e.mcpConfig.settingsDir)}function V(c){if(c instanceof Error){let e=c.message.toLowerCase();return e.includes("not found")||e.includes("enoent")||e.includes("no such file")||e.includes("does not exist")}return false}function xe(c,e){let n=[e.command,e.url].filter(Boolean);if(n.length===0)throw new Error(`MCP server "${c}" must specify command or url`);if(n.length>1)throw new Error(`MCP server "${c}" cannot specify both command and url`)}function be(c){for(let[e,n]of Object.entries(c))xe(e,n);}function Ce(c){return c.type?c.type:c.command?"stdio":"sse"}function Tt(c){let e=Ce(c);return {...c,type:e}}function Pt(c){let{type:e,url:n,...s}=c;return Ce(c)==="http"&&n?{httpUrl:n,...s}:n?{url:n,...s}:s}function Et(c){return {type:Ce(c),...c}}async function Be(c,e,n,s){be(n);let a=U(e),o=$(e);await c.files.makeDir(a);let i={};try{let r=await c.files.read(o);typeof r=="string"&&(i=JSON.parse(r));}catch(r){if(!V(r))throw r}let l=Object.fromEntries(Object.entries(n).map(([r,p])=>[r,s(p)]));await c.files.write(o,JSON.stringify({...i,mcpServers:l},null,2));}async function Q(c,e,n){be(n);let s=U("claude"),a=$("claude"),o=Object.fromEntries(Object.entries(n).map(([l,r])=>[l,Et(r)]));await c.files.write(`${e}/.mcp.json`,JSON.stringify({mcpServers:o},null,2)),await c.files.makeDir(s);let i={};try{let l=await c.files.read(a);typeof l=="string"&&(i=JSON.parse(l));}catch(l){if(!V(l))throw l}i.enableAllProjectMcpServers=true,await c.files.write(a,JSON.stringify(i,null,2));}async function X(c,e){await Be(c,"gemini",e,Tt);}async function ee(c,e){await Be(c,"qwen",e,Pt);}function T(c){return typeof c=="string"?`"${c.replace(/\\/g,"\\\\").replace(/"/g,'\\"')}"`:Array.isArray(c)?`[${c.map(e=>T(e)).join(", ")}]`:typeof c=="object"&&c!==null?`{ ${Object.entries(c).map(([n,s])=>`${n} = ${T(s)}`).join(", ")} }`:typeof c=="boolean"?c?"true":"false":String(c)}async function te(c,e){for(let[r,p]of Object.entries(e))xe(r,p);let n=U("codex"),s=$("codex");await c.files.makeDir(n);let a="";try{let r=await c.files.read(s);typeof r=="string"&&(a=r);}catch(r){if(!V(r))throw r}let o=[],i=[];a.includes("experimental_use_rmcp_client")||(o.push("# Enable improved RMCP client (recommended)"),o.push("experimental_use_rmcp_client = true")),a.includes("[mcp_servers]")||i.push("[mcp_servers]","");for(let[r,p]of Object.entries(e)){if(a.includes(`[mcp_servers.${r}]`))continue;i.push(`[mcp_servers.${r}]`);let t=p.type??(p.command?"stdio":"http");if(t==="http"||t==="sse"||!!p.url){if(!p.url)throw new Error(`MCP server "${r}" is missing url for ${t} transport`);i.push(`url = ${T(p.url)}`),p.bearerTokenEnvVar&&i.push(`bearer_token_env_var = ${T(p.bearerTokenEnvVar)}`);let d=p.httpHeaders??p.headers;d&&Object.keys(d).length>0&&i.push(`http_headers = ${T(d)}`),p.envHttpHeaders&&Object.keys(p.envHttpHeaders).length>0&&i.push(`env_http_headers = ${T(p.envHttpHeaders)}`);}else i.push(`command = ${T(p.command)}`),p.args&&p.args.length>0&&i.push(`args = ${T(p.args)}`),p.cwd&&i.push(`cwd = ${T(p.cwd)}`);p.env&&Object.keys(p.env).length>0&&i.push(`env = ${T(p.env)}`),p.envVars&&p.envVars.length>0&&i.push(`env_vars = ${T(p.envVars)}`),i.push("");}let l=[a.trim(),o.join(`
2
2
  `),i.join(`
3
3
  `)].filter(r=>r.length>0);await c.files.write(s,l.join(`
4
4
 
5
5
  `)+`
6
- `);}async function Ce(c,e,n,s){if(!(!s||Object.keys(s).length===0))switch(c){case "claude":await Q(e,n,s);break;case "codex":await te(e,s);break;case "gemini":await X(e,s);break;case "qwen":await ee(e,s);break;default:throw new Error(`Unknown agent type for MCP config: ${c}`)}}function ne(){let c={};return function(p){let t;try{t=JSON.parse(p);}catch{return null}if(t._meta||t._prompt)return null;let u=t.session_id;switch(t.type){case "system":return null;case "assistant":return e(t.message?.content,"assistant",u);case "user":{let d=t.message?.content;return typeof d=="string"||Array.isArray(d)&&d.length===1&&d[0].type==="text"?null:e(d,"user",u)}case "result":return null;default:return null}};function e(r,p,t){if(typeof r=="string")return p==="user"?null:[{sessionId:t,update:{sessionUpdate:"agent_message_chunk",content:{type:"text",text:r}}}];if(!r||!Array.isArray(r))return null;let u=[];for(let d of r){let m=null;switch(d.type){case "text":case "text_delta":if(p==="user")break;m={sessionUpdate:"agent_message_chunk",content:{type:"text",text:d.text}};break;case "image":if(p==="user")break;m={sessionUpdate:"agent_message_chunk",content:{type:"image",data:d.source?.type==="base64"?d.source.data:"",mimeType:d.source?.type==="base64"?d.source.media_type:"",uri:d.source?.type==="url"?d.source.url:void 0}};break;case "thinking":case "thinking_delta":m={sessionUpdate:"agent_thought_chunk",content:{type:"text",text:d.thinking}};break;case "tool_use":case "server_tool_use":case "mcp_tool_use":{if(c[d.id]=d,d.name==="TodoWrite")Array.isArray(d.input?.todos)&&(m={sessionUpdate:"plan",entries:i(d.input)});else {let f=n(d);m={sessionUpdate:"tool_call",toolCallId:d.id,status:"pending",...f};}break}case "tool_result":case "tool_search_tool_result":case "web_fetch_tool_result":case "web_search_tool_result":case "code_execution_tool_result":case "bash_code_execution_tool_result":case "text_editor_code_execution_tool_result":case "mcp_tool_result":{let f=c[d.tool_use_id];if(!f||(delete c[d.tool_use_id],f.name==="TodoWrite"))break;m={sessionUpdate:"tool_call_update",toolCallId:d.tool_use_id,status:"is_error"in d&&d.is_error?"failed":"completed",...s(d,f)};break}}m&&u.push({sessionId:t,update:m});}return u.length>0?u:null}function n(r){let p=r.name,t=r.input||{};switch(p){case "Task":return {title:t.description||"Task",kind:"think",content:t.prompt?[{type:"content",content:{type:"text",text:t.prompt}}]:[],rawInput:t};case "NotebookRead":return {title:t.notebook_path?`Read Notebook ${t.notebook_path}`:"Read Notebook",kind:"read",content:[],locations:t.notebook_path?[{path:t.notebook_path}]:[],rawInput:t};case "NotebookEdit":return {title:t.notebook_path?`Edit Notebook ${t.notebook_path}`:"Edit Notebook",kind:"edit",content:t.new_source?[{type:"content",content:{type:"text",text:t.new_source}}]:[],locations:t.notebook_path?[{path:t.notebook_path}]:[],rawInput:t};case "Bash":return {title:t.command?"`"+t.command.replaceAll("`","\\`")+"`":"Terminal",kind:"execute",content:t.description?[{type:"content",content:{type:"text",text:t.description}}]:[],rawInput:t};case "BashOutput":return {title:"Tail Logs",kind:"execute",content:[],rawInput:t};case "KillShell":return {title:"Kill Process",kind:"execute",content:[],rawInput:t};case "Read":{let u="";return t.limit?u=" ("+((t.offset??0)+1)+" - "+((t.offset??0)+t.limit)+")":t.offset&&(u=" (from line "+(t.offset+1)+")"),{title:"Read "+(t.file_path??"File")+u,kind:"read",content:[],locations:t.file_path?[{path:t.file_path,line:t.offset??0}]:[],rawInput:t}}case "LS":return {title:t.path?`List \`${t.path}\` directory`:"List current directory",kind:"search",content:[],locations:[],rawInput:t};case "Edit":{let u=t.file_path;return {title:u?`Edit \`${u}\``:"Edit",kind:"edit",content:u?[{type:"diff",path:u,oldText:t.old_string??null,newText:t.new_string??""}]:[],locations:u?[{path:u}]:[],rawInput:t}}case "Write":return {title:t.file_path?`Write ${t.file_path}`:"Write",kind:"edit",content:t.file_path?[{type:"diff",path:t.file_path,oldText:null,newText:t.content??""}]:[],locations:t.file_path?[{path:t.file_path}]:[],rawInput:t};case "Glob":{let u="Find";return t.path&&(u+=` \`${t.path}\``),t.pattern&&(u+=` \`${t.pattern}\``),{title:u,kind:"search",content:[],locations:t.path?[{path:t.path}]:[],rawInput:t}}case "Grep":{let u="grep";return t["-i"]&&(u+=" -i"),t["-n"]&&(u+=" -n"),t["-A"]!==void 0&&(u+=` -A ${t["-A"]}`),t["-B"]!==void 0&&(u+=` -B ${t["-B"]}`),t["-C"]!==void 0&&(u+=` -C ${t["-C"]}`),t.output_mode==="files_with_matches"?u+=" -l":t.output_mode==="count"&&(u+=" -c"),t.head_limit!==void 0&&(u+=` | head -${t.head_limit}`),t.glob&&(u+=` --include="${t.glob}"`),t.type&&(u+=` --type=${t.type}`),t.multiline&&(u+=" -P"),u+=` "${t.pattern??""}"`,t.path&&(u+=` ${t.path}`),{title:u,kind:"search",content:[],rawInput:t}}case "WebFetch":return {title:t.url?`Fetch ${t.url}`:"Fetch",kind:"fetch",content:t.prompt?[{type:"content",content:{type:"text",text:t.prompt}}]:[],rawInput:t};case "WebSearch":{let u=`"${t.query??""}"`;return t.allowed_domains?.length>0&&(u+=` (allowed: ${t.allowed_domains.join(", ")})`),t.blocked_domains?.length>0&&(u+=` (blocked: ${t.blocked_domains.join(", ")})`),{title:u,kind:"fetch",content:[],rawInput:t}}case "TodoWrite":return {title:Array.isArray(t.todos)?`Update TODOs: ${t.todos.map(u=>u.content).join(", ")}`:"Update TODOs",kind:"think",content:[],rawInput:t};case "ExitPlanMode":return {title:"Ready to code?",kind:"switch_mode",content:t.plan?[{type:"content",content:{type:"text",text:t.plan}}]:[],rawInput:t};default:if(p?.startsWith("mcp__")){let u=p.split("__"),d=u[1]||"mcp",m=u.slice(2).join("__")||p;return {title:`${d}: ${m}`,kind:"other",content:[],rawInput:t}}return {title:p||"Unknown Tool",kind:"other",content:[],rawInput:t}}}function s(r,p){let t=r.content;switch(p?.name){case "Read":return Array.isArray(t)&&t.length>0?{content:t.map(u=>({type:"content",content:u.type==="text"?{type:"text",text:l(u.text)}:o(u,false)}))}:typeof t=="string"&&t.length>0?{content:[{type:"content",content:{type:"text",text:l(t)}}]}:{};case "Edit":case "Write":case "Bash":return a(t,r.is_error||false);case "ExitPlanMode":return {title:"Exited Plan Mode"};default:return a(t,"is_error"in r?r.is_error:false)}}function a(r,p=false){return Array.isArray(r)&&r.length>0?{content:r.map(t=>({type:"content",content:o(t,p)}))}:typeof r=="string"&&r.length>0?{content:[{type:"content",content:{type:"text",text:p?"```\n"+r+"\n```":r}}]}:{}}function o(r,p){if(r.type==="text")return p?{type:"text",text:"```\n"+r.text+"\n```"}:{type:"text",text:r.text};if(r.type==="image"){if(r.data&&r.mimeType)return {type:"image",data:r.data,mimeType:r.mimeType};if(r.source?.type==="base64")return {type:"image",data:r.source.data||"",mimeType:r.source.media_type||""};if(r.source?.type==="url")return {type:"image",data:"",mimeType:"",uri:r.source.url}}return r}function i(r){return r.todos.map(p=>({content:p.content,status:p.status||"pending",priority:"medium"}))}function l(r){let p="```",t=Array.from(r.matchAll(/^```+/gm));for(let[u]of t)for(;u.length>=p.length;)p+="`";return p+`
6
+ `);}async function we(c,e,n,s){if(!(!s||Object.keys(s).length===0))switch(c){case "claude":await Q(e,n,s);break;case "codex":await te(e,s);break;case "gemini":await X(e,s);break;case "qwen":await ee(e,s);break;default:throw new Error(`Unknown agent type for MCP config: ${c}`)}}function ne(){let c={};return function(p){let t;try{t=JSON.parse(p);}catch{return null}if(t._meta||t._prompt)return null;let u=t.session_id;switch(t.type){case "system":return null;case "assistant":return e(t.message?.content,"assistant",u);case "user":{let d=t.message?.content;return typeof d=="string"||Array.isArray(d)&&d.length===1&&d[0].type==="text"?null:e(d,"user",u)}case "result":return null;default:return null}};function e(r,p,t){if(typeof r=="string")return p==="user"?null:[{sessionId:t,update:{sessionUpdate:"agent_message_chunk",content:{type:"text",text:r}}}];if(!r||!Array.isArray(r))return null;let u=[];for(let d of r){let m=null;switch(d.type){case "text":case "text_delta":if(p==="user")break;m={sessionUpdate:"agent_message_chunk",content:{type:"text",text:d.text}};break;case "image":if(p==="user")break;m={sessionUpdate:"agent_message_chunk",content:{type:"image",data:d.source?.type==="base64"?d.source.data:"",mimeType:d.source?.type==="base64"?d.source.media_type:"",uri:d.source?.type==="url"?d.source.url:void 0}};break;case "thinking":case "thinking_delta":m={sessionUpdate:"agent_thought_chunk",content:{type:"text",text:d.thinking}};break;case "tool_use":case "server_tool_use":case "mcp_tool_use":{if(c[d.id]=d,d.name==="TodoWrite")Array.isArray(d.input?.todos)&&(m={sessionUpdate:"plan",entries:i(d.input)});else {let f=n(d);m={sessionUpdate:"tool_call",toolCallId:d.id,status:"pending",...f};}break}case "tool_result":case "tool_search_tool_result":case "web_fetch_tool_result":case "web_search_tool_result":case "code_execution_tool_result":case "bash_code_execution_tool_result":case "text_editor_code_execution_tool_result":case "mcp_tool_result":{let f=c[d.tool_use_id];if(!f||(delete c[d.tool_use_id],f.name==="TodoWrite"))break;m={sessionUpdate:"tool_call_update",toolCallId:d.tool_use_id,status:"is_error"in d&&d.is_error?"failed":"completed",...s(d,f)};break}}m&&u.push({sessionId:t,update:m});}return u.length>0?u:null}function n(r){let p=r.name,t=r.input||{};switch(p){case "Task":return {title:t.description||"Task",kind:"think",content:t.prompt?[{type:"content",content:{type:"text",text:t.prompt}}]:[],rawInput:t};case "NotebookRead":return {title:t.notebook_path?`Read Notebook ${t.notebook_path}`:"Read Notebook",kind:"read",content:[],locations:t.notebook_path?[{path:t.notebook_path}]:[],rawInput:t};case "NotebookEdit":return {title:t.notebook_path?`Edit Notebook ${t.notebook_path}`:"Edit Notebook",kind:"edit",content:t.new_source?[{type:"content",content:{type:"text",text:t.new_source}}]:[],locations:t.notebook_path?[{path:t.notebook_path}]:[],rawInput:t};case "Bash":return {title:t.command?"`"+t.command.replaceAll("`","\\`")+"`":"Terminal",kind:"execute",content:t.description?[{type:"content",content:{type:"text",text:t.description}}]:[],rawInput:t};case "BashOutput":return {title:"Tail Logs",kind:"execute",content:[],rawInput:t};case "KillShell":return {title:"Kill Process",kind:"execute",content:[],rawInput:t};case "Read":{let u="";return t.limit?u=" ("+((t.offset??0)+1)+" - "+((t.offset??0)+t.limit)+")":t.offset&&(u=" (from line "+(t.offset+1)+")"),{title:"Read "+(t.file_path??"File")+u,kind:"read",content:[],locations:t.file_path?[{path:t.file_path,line:t.offset??0}]:[],rawInput:t}}case "LS":return {title:t.path?`List \`${t.path}\` directory`:"List current directory",kind:"search",content:[],locations:[],rawInput:t};case "Edit":{let u=t.file_path;return {title:u?`Edit \`${u}\``:"Edit",kind:"edit",content:u?[{type:"diff",path:u,oldText:t.old_string??null,newText:t.new_string??""}]:[],locations:u?[{path:u}]:[],rawInput:t}}case "Write":return {title:t.file_path?`Write ${t.file_path}`:"Write",kind:"edit",content:t.file_path?[{type:"diff",path:t.file_path,oldText:null,newText:t.content??""}]:[],locations:t.file_path?[{path:t.file_path}]:[],rawInput:t};case "Glob":{let u="Find";return t.path&&(u+=` \`${t.path}\``),t.pattern&&(u+=` \`${t.pattern}\``),{title:u,kind:"search",content:[],locations:t.path?[{path:t.path}]:[],rawInput:t}}case "Grep":{let u="grep";return t["-i"]&&(u+=" -i"),t["-n"]&&(u+=" -n"),t["-A"]!==void 0&&(u+=` -A ${t["-A"]}`),t["-B"]!==void 0&&(u+=` -B ${t["-B"]}`),t["-C"]!==void 0&&(u+=` -C ${t["-C"]}`),t.output_mode==="files_with_matches"?u+=" -l":t.output_mode==="count"&&(u+=" -c"),t.head_limit!==void 0&&(u+=` | head -${t.head_limit}`),t.glob&&(u+=` --include="${t.glob}"`),t.type&&(u+=` --type=${t.type}`),t.multiline&&(u+=" -P"),u+=` "${t.pattern??""}"`,t.path&&(u+=` ${t.path}`),{title:u,kind:"search",content:[],rawInput:t}}case "WebFetch":return {title:t.url?`Fetch ${t.url}`:"Fetch",kind:"fetch",content:t.prompt?[{type:"content",content:{type:"text",text:t.prompt}}]:[],rawInput:t};case "WebSearch":{let u=`"${t.query??""}"`;return t.allowed_domains?.length>0&&(u+=` (allowed: ${t.allowed_domains.join(", ")})`),t.blocked_domains?.length>0&&(u+=` (blocked: ${t.blocked_domains.join(", ")})`),{title:u,kind:"fetch",content:[],rawInput:t}}case "TodoWrite":return {title:Array.isArray(t.todos)?`Update TODOs: ${t.todos.map(u=>u.content).join(", ")}`:"Update TODOs",kind:"think",content:[],rawInput:t};case "ExitPlanMode":return {title:"Ready to code?",kind:"switch_mode",content:t.plan?[{type:"content",content:{type:"text",text:t.plan}}]:[],rawInput:t};default:if(p?.startsWith("mcp__")){let u=p.split("__"),d=u[1]||"mcp",m=u.slice(2).join("__")||p;return {title:`${d}: ${m}`,kind:"other",content:[],rawInput:t}}return {title:p||"Unknown Tool",kind:"other",content:[],rawInput:t}}}function s(r,p){let t=r.content;switch(p?.name){case "Read":return Array.isArray(t)&&t.length>0?{content:t.map(u=>({type:"content",content:u.type==="text"?{type:"text",text:l(u.text)}:o(u,false)}))}:typeof t=="string"&&t.length>0?{content:[{type:"content",content:{type:"text",text:l(t)}}]}:{};case "Edit":case "Write":case "Bash":return a(t,r.is_error||false);case "ExitPlanMode":return {title:"Exited Plan Mode"};default:return a(t,"is_error"in r?r.is_error:false)}}function a(r,p=false){return Array.isArray(r)&&r.length>0?{content:r.map(t=>({type:"content",content:o(t,p)}))}:typeof r=="string"&&r.length>0?{content:[{type:"content",content:{type:"text",text:p?"```\n"+r+"\n```":r}}]}:{}}function o(r,p){if(r.type==="text")return p?{type:"text",text:"```\n"+r.text+"\n```"}:{type:"text",text:r.text};if(r.type==="image"){if(r.data&&r.mimeType)return {type:"image",data:r.data,mimeType:r.mimeType};if(r.source?.type==="base64")return {type:"image",data:r.source.data||"",mimeType:r.source.media_type||""};if(r.source?.type==="url")return {type:"image",data:"",mimeType:"",uri:r.source.url}}return r}function i(r){return r.todos.map(p=>({content:p.content,status:p.status||"pending",priority:"medium"}))}function l(r){let p="```",t=Array.from(r.matchAll(/^```+/gm));for(let[u]of t)for(;u.length>=p.length;)p+="`";return p+`
7
7
  `+r+(r.endsWith(`
8
8
  `)?"":`
9
9
  `)+p}}function se(){return function(r){let p;try{p=JSON.parse(r);}catch{return null}if(p._meta||p._prompt)return null;let t=[];switch(p.type){case "thread.started":case "turn.started":case "turn.completed":return null;case "item.started":{let u=p.item;if(!u)return null;let d=e(u);d&&t.push({update:d});break}case "item.updated":{let u=p.item;if(!u)return null;let d=n(u);d&&t.push({update:d});break}case "item.completed":{let u=p.item;if(!u)return null;let d=s(u);d&&t.push({update:d});break}default:return null}return t.length>0?t:null};function e(l){let r=l.id;switch(l.type){case "mcp_tool_call":return {type:"mcp_tool_call",name:`${l.server}:${l.tool}`},{sessionUpdate:"tool_call",toolCallId:r,title:`${l.server}: ${l.tool}`,kind:"other",status:"in_progress",rawInput:l.arguments,content:[]};case "command_execution":return {type:"command_execution",name:l.command},{sessionUpdate:"tool_call",toolCallId:r,title:l.command?`\`${l.command}\``:"Execute Command",kind:"execute",status:"in_progress",rawInput:{command:l.command},content:[]};case "web_search":return {type:"web_search",name:l.query},{sessionUpdate:"tool_call",toolCallId:r,title:`Search: ${l.query??""}`,kind:"fetch",status:"in_progress",content:[]};case "todo_list":return {sessionUpdate:"plan",entries:i(l.items)};default:return null}}function n(l){return l.type==="todo_list"?{sessionUpdate:"plan",entries:i(l.items)}:null}function s(l){let r=l.id;switch(l.type){case "reasoning":return {sessionUpdate:"agent_thought_chunk",content:{type:"text",text:l.text??l.summary?.join(`
@@ -16,8 +16,8 @@ ${p.message}
16
16
  ${d}
17
17
  \`\`\``:d}});else if(Array.isArray(d)){for(let f of d)if(f.type==="text"){let h=f.text;m.push({type:"content",content:{type:"text",text:u?`\`\`\`
18
18
  ${h}
19
- \`\`\``:h}});}}return {sessionUpdate:"tool_call_update",toolCallId:t,status:u?"failed":"completed",content:m}}function r(p,t){let u=Mt[p]||"other",d=[],m=[],f=p;switch(p){case "Read":case "read_file":f=`Read ${t.file_path||t.absolute_path||"file"}`,(t.file_path||t.absolute_path)&&m.push({path:t.file_path||t.absolute_path});break;case "Write":case "write_file":f=`Write ${t.file_path||"file"}`,t.file_path&&(m.push({path:t.file_path}),d.push({type:"diff",path:t.file_path,oldText:null,newText:t.content||""}));break;case "Edit":case "edit_file":f=`Edit ${t.file_path||"file"}`,t.file_path&&m.push({path:t.file_path});break;case "Bash":case "shell":case "run_shell_command":f=t.command?`\`${t.command}\``:"Run command",t.description&&d.push({type:"content",content:{type:"text",text:t.description}});break;case "WebFetch":f=t.url?`Fetch ${t.url}`:"Web fetch";break;case "WebSearch":case "brave_web_search":f=t.query?`"${t.query}"`:"Web search";break;case "Glob":f=`Find ${t.pattern||"files"}`,t.path&&m.push({path:t.path});break;case "Grep":f=`grep "${t.pattern||""}"`;break;case "LS":case "list_directory":f=`List ${t.path||"directory"}`,t.path&&m.push({path:t.path});break;case "Task":f=t.description||"Subagent task";break;case "TodoWrite":f="Update todos";break;case "ExitPlanMode":f="Exit plan mode";break;default:f=p;}return {title:f,kind:u,content:d,locations:m}}}function Le(c){return ie()(c)}function O(c){switch(c){case "claude":return ne();case "codex":return se();case "gemini":return oe();case "qwen":return ie();default:return ()=>null}}function Ot(c,e){let n=e.trim();return n?O(c)(n):null}function It(c,e){let n=[],s=e.split(`
20
- `),a=O(c);for(let o of s){let i=o.trim();if(!i)continue;let l=a(i);l&&n.push(...l);}return n}function K(){return process.env.EVOLVE_GATEWAY_URL||"https://swarmkit-gateway-692833842999.us-central1.run.app"}function je(){return `${K()}`}function Ve(){return `${K()}/e2b`}function Ke(c){return {"browser-use":{type:"http",url:`${K()}/browser_use/mcp`,headers:{"x-litellm-api-key":`Bearer ${c}`}}}}var I="EVOLVE_API_KEY",re="E2B_API_KEY",ae="DAYTONA_API_KEY",ce="MODAL_TOKEN_ID",Se="MODAL_TOKEN_SECRET",We="claude",W=36e5,Je=4,ke=100,Ye="/home/user/workspace",J="https://dashboard.evolvingmachines.ai",Ge=".evolve-sdk/observability/sessions",ze=100,He=5e3,le=3,Te=1e3;function E(c){return c!==null&&typeof c=="object"&&"safeParse"in c&&typeof c.safeParse=="function"}function N(c){return JSON.stringify(At(c,{target:"jsonSchema7"}),null,2)}function B(c){return JSON.stringify(c,null,2)}function qe(c,e=false){let n={};function s(a,o){for(let i of P.readdirSync(a)){let l=Y.join(a,i),r=o?`${o}/${i}`:i;P.statSync(l).isDirectory()?e&&s(l,r):n[r]=P.readFileSync(l);}}return s(c,""),n}function Ze(c,e){for(let[n,s]of Object.entries(e)){let a=Y.join(c,n),o=Y.dirname(a);P.mkdirSync(o,{recursive:true});let i;s instanceof ArrayBuffer||s instanceof Uint8Array?i=Buffer.from(s):i=s,P.writeFileSync(a,i);}}function Dt(c){let e=c.replace(/^~/,process.env.HOME||"");if(!P.existsSync(e))throw new Error(`OAuth file not found: ${e}`);return P.readFileSync(e,"utf-8")}function _e(c){let e=c?.type??We,n=M(e);if(c?.oauthToken){if(e!=="claude")throw new Error(`oauthToken is only supported for claude agent (Claude Max subscription), not ${e}. Use providerApiKey for ${e} instead.`);return {type:e,apiKey:c.oauthToken,isDirectMode:true,isOAuth:true,model:c.model,reasoningEffort:c.reasoningEffort,betas:c.betas}}if(c?.providerApiKey){let i=c.providerBaseUrl??process.env[n.baseUrlEnv]??n.defaultBaseUrl;return {type:e,apiKey:c.providerApiKey,baseUrl:i,isDirectMode:true,model:c.model,reasoningEffort:c.reasoningEffort,betas:c.betas}}if(c?.apiKey)return {type:e,apiKey:c.apiKey,isDirectMode:false,model:c.model,reasoningEffort:c.reasoningEffort,betas:c.betas};let s=process.env[I];if(s)return {type:e,apiKey:s,isDirectMode:false,model:c?.model,reasoningEffort:c?.reasoningEffort,betas:c?.betas};let a=process.env[n.apiKeyEnv];if(a){let i=process.env[n.baseUrlEnv]??n.defaultBaseUrl;return {type:e,apiKey:a,baseUrl:i,isDirectMode:true,model:c?.model,reasoningEffort:c?.reasoningEffort,betas:c?.betas}}if(n.oauthEnv){let i=process.env[n.oauthEnv];if(i){if(n.oauthFileName){let l=Dt(i);return {type:e,apiKey:"__oauth_file__",isDirectMode:true,isOAuth:true,oauthFileContent:l,model:c?.model,reasoningEffort:c?.reasoningEffort,betas:c?.betas}}return {type:e,apiKey:i,isDirectMode:true,isOAuth:true,model:c?.model,reasoningEffort:c?.reasoningEffort,betas:c?.betas}}}let o=n.oauthEnv?n.oauthFileName?`, or ${n.oauthEnv}`:`, oauthToken, or ${n.oauthEnv}`:"";throw new Error(`No API key found for ${e}. Set apiKey (gateway), providerApiKey (direct)${o}, or ${I} / ${n.apiKeyEnv} env var.`)}async function Ee(){let c=process.env[re];if(c)try{let{createE2BProvider:o}=await import('@evolvingmachines/e2b');return o({apiKey:c})}catch(o){let i=o;throw i.message?.includes("Cannot find module")||i.message?.includes("MODULE_NOT_FOUND")?new Error(`${re} is set but @evolvingmachines/e2b failed to load.
19
+ \`\`\``:h}});}}return {sessionUpdate:"tool_call_update",toolCallId:t,status:u?"failed":"completed",content:m}}function r(p,t){let u=Mt[p]||"other",d=[],m=[],f=p;switch(p){case "Read":case "read_file":f=`Read ${t.file_path||t.absolute_path||"file"}`,(t.file_path||t.absolute_path)&&m.push({path:t.file_path||t.absolute_path});break;case "Write":case "write_file":f=`Write ${t.file_path||"file"}`,t.file_path&&(m.push({path:t.file_path}),d.push({type:"diff",path:t.file_path,oldText:null,newText:t.content||""}));break;case "Edit":case "edit_file":f=`Edit ${t.file_path||"file"}`,t.file_path&&m.push({path:t.file_path});break;case "Bash":case "shell":case "run_shell_command":f=t.command?`\`${t.command}\``:"Run command",t.description&&d.push({type:"content",content:{type:"text",text:t.description}});break;case "WebFetch":f=t.url?`Fetch ${t.url}`:"Web fetch";break;case "WebSearch":case "brave_web_search":f=t.query?`"${t.query}"`:"Web search";break;case "Glob":f=`Find ${t.pattern||"files"}`,t.path&&m.push({path:t.path});break;case "Grep":f=`grep "${t.pattern||""}"`;break;case "LS":case "list_directory":f=`List ${t.path||"directory"}`,t.path&&m.push({path:t.path});break;case "Task":f=t.description||"Subagent task";break;case "TodoWrite":f="Update todos";break;case "ExitPlanMode":f="Exit plan mode";break;default:f=p;}return {title:f,kind:u,content:d,locations:m}}}function je(c){return ie()(c)}function O(c){switch(c){case "claude":return ne();case "codex":return se();case "gemini":return oe();case "qwen":return ie();default:return ()=>null}}function Ot(c,e){let n=e.trim();return n?O(c)(n):null}function It(c,e){let n=[],s=e.split(`
20
+ `),a=O(c);for(let o of s){let i=o.trim();if(!i)continue;let l=a(i);l&&n.push(...l);}return n}function K(){return process.env.EVOLVE_GATEWAY_URL||"https://swarmkit-gateway-692833842999.us-central1.run.app"}function Le(){return `${K()}`}function Ve(){return `${K()}/e2b`}function Ke(c){return {"browser-use":{type:"http",url:`${K()}/browser_use/mcp`,headers:{"x-litellm-api-key":`Bearer ${c}`}}}}var I="EVOLVE_API_KEY",re="E2B_API_KEY",ae="DAYTONA_API_KEY",ce="MODAL_TOKEN_ID",Se="MODAL_TOKEN_SECRET",We="claude",W=36e5,Je=4,ke=100,Ye="/home/user/workspace",J="https://dashboard.evolvingmachines.ai",Ge=".evolve-sdk/observability/sessions",ze=100,He=5e3,le=3,_e=1e3;function P(c){return c!==null&&typeof c=="object"&&"safeParse"in c&&typeof c.safeParse=="function"}function N(c){return JSON.stringify(At(c,{target:"jsonSchema7"}),null,2)}function B(c){return JSON.stringify(c,null,2)}function qe(c,e=false){let n={};function s(a,o){for(let i of E.readdirSync(a)){let l=Y.join(a,i),r=o?`${o}/${i}`:i;E.statSync(l).isDirectory()?e&&s(l,r):n[r]=E.readFileSync(l);}}return s(c,""),n}function Ze(c,e){for(let[n,s]of Object.entries(e)){let a=Y.join(c,n),o=Y.dirname(a);E.mkdirSync(o,{recursive:true});let i;s instanceof ArrayBuffer||s instanceof Uint8Array?i=Buffer.from(s):i=s,E.writeFileSync(a,i);}}function Dt(c){let e=c.replace(/^~/,process.env.HOME||"");if(!E.existsSync(e))throw new Error(`OAuth file not found: ${e}`);return E.readFileSync(e,"utf-8")}function Te(c){let e=c?.type??We,n=M(e);if(c?.oauthToken){if(e!=="claude")throw new Error(`oauthToken is only supported for claude agent (Claude Max subscription), not ${e}. Use providerApiKey for ${e} instead.`);return {type:e,apiKey:c.oauthToken,isDirectMode:true,isOAuth:true,model:c.model,reasoningEffort:c.reasoningEffort,betas:c.betas}}if(c?.providerApiKey){let i=c.providerBaseUrl??process.env[n.baseUrlEnv]??n.defaultBaseUrl;return {type:e,apiKey:c.providerApiKey,baseUrl:i,isDirectMode:true,model:c.model,reasoningEffort:c.reasoningEffort,betas:c.betas}}if(c?.apiKey)return {type:e,apiKey:c.apiKey,isDirectMode:false,model:c.model,reasoningEffort:c.reasoningEffort,betas:c.betas};let s=process.env[I];if(s)return {type:e,apiKey:s,isDirectMode:false,model:c?.model,reasoningEffort:c?.reasoningEffort,betas:c?.betas};let a=process.env[n.apiKeyEnv];if(a){let i=process.env[n.baseUrlEnv]??n.defaultBaseUrl;return {type:e,apiKey:a,baseUrl:i,isDirectMode:true,model:c?.model,reasoningEffort:c?.reasoningEffort,betas:c?.betas}}if(n.oauthEnv){let i=process.env[n.oauthEnv];if(i){if(n.oauthFileName){let l=Dt(i);return {type:e,apiKey:"__oauth_file__",isDirectMode:true,isOAuth:true,oauthFileContent:l,model:c?.model,reasoningEffort:c?.reasoningEffort,betas:c?.betas}}return {type:e,apiKey:i,isDirectMode:true,isOAuth:true,model:c?.model,reasoningEffort:c?.reasoningEffort,betas:c?.betas}}}let o=n.oauthEnv?n.oauthFileName?`, or ${n.oauthEnv}`:`, oauthToken, or ${n.oauthEnv}`:"";throw new Error(`No API key found for ${e}. Set apiKey (gateway), providerApiKey (direct)${o}, or ${I} / ${n.apiKeyEnv} env var.`)}async function Pe(){let c=process.env[re];if(c)try{let{createE2BProvider:o}=await import('@evolvingmachines/e2b');return o({apiKey:c})}catch(o){let i=o;throw i.message?.includes("Cannot find module")||i.message?.includes("MODULE_NOT_FOUND")?new Error(`${re} is set but @evolvingmachines/e2b failed to load.
21
21
  Try reinstalling: npm install @evolvingmachines/sdk`):i}let e=process.env[ae];if(e)try{let{createDaytonaProvider:o}=await import('@evolvingmachines/daytona');return o({apiKey:e})}catch(o){let i=o;throw i.message?.includes("Cannot find module")||i.message?.includes("MODULE_NOT_FOUND")?new Error(`${ae} is set but @evolvingmachines/daytona failed to load.
22
22
  Try installing: npm install @evolvingmachines/daytona`):i}let n=process.env[ce],s=process.env[Se];if(n&&s)try{let{createModalProvider:o}=await import('@evolvingmachines/modal');return o({tokenId:n,tokenSecret:s})}catch(o){let i=o;throw i.message?.includes("Cannot find module")||i.message?.includes("MODULE_NOT_FOUND")?new Error(`${ce} is set but @evolvingmachines/modal failed to load.
23
23
  Try installing: npm install @evolvingmachines/modal`):i}let a=process.env[I];if(a)try{let{createE2BProvider:o}=await import('@evolvingmachines/e2b');return process.env.E2B_API_URL=Ve(),o({apiKey:a})}catch(o){let i=o;throw i.message?.includes("Cannot find module")||i.message?.includes("MODULE_NOT_FOUND")?new Error(`${I} is set but @evolvingmachines/e2b failed to load.
@@ -26,7 +26,7 @@ Try reinstalling: npm install @evolvingmachines/sdk`):i}throw new Error(`No sand
26
26
  2. Set ${re} environment variable (direct E2B access, get key at https://e2b.dev)
27
27
  3. Set ${ae} environment variable (direct Daytona access, get key at https://app.daytona.io)
28
28
  4. Set ${ce} and ${Se} environment variables (direct Modal access, get tokens at https://modal.com/settings/tokens)
29
- 5. Pass sandbox explicitly: .withSandbox(provider)`)}function Ft(c){return new Promise(e=>setTimeout(e,c))}async function T(c,e,n=0){let s=$t(e),a=e?.onItemRetry,o=null,i=0,l=s.backoffMs;for(;i<s.maxAttempts;){if(i++,o=await c(i),!s.retryOn(o))return o;if(i>=s.maxAttempts)break;if(a){let r=o.error??"Unknown error";a(n,i,r);}await Ft(l),l*=s.backoffMultiplier;}return o}function $t(c){return {maxAttempts:c?.maxAttempts??3,backoffMs:c?.backoffMs??1e3,backoffMultiplier:c?.backoffMultiplier??2,retryOn:c?.retryOn??(e=>e.status==="error")}}var Qe=`## FILESYSTEM INSTRUCTIONS:
29
+ 5. Pass sandbox explicitly: .withSandbox(provider)`)}function Ft(c){return new Promise(e=>setTimeout(e,c))}async function _(c,e,n=0){let s=$t(e),a=e?.onItemRetry,o=null,i=0,l=s.backoffMs;for(;i<s.maxAttempts;){if(i++,o=await c(i),!s.retryOn(o))return o;if(i>=s.maxAttempts)break;if(a){let r=o.error??"Unknown error";a(n,i,r);}await Ft(l),l*=s.backoffMultiplier;}return o}function $t(c){return {maxAttempts:c?.maxAttempts??3,backoffMs:c?.backoffMs??1e3,backoffMultiplier:c?.backoffMultiplier??2,retryOn:c?.retryOn??(e=>e.status==="error")}}var Qe=`## FILESYSTEM INSTRUCTIONS:
30
30
 
31
31
  You are running in a sandbox environment.
32
32
 
@@ -43,15 +43,22 @@ IMPORTANT - Directory structure:
43
43
  \u2514\u2500\u2500 output/ # Where you can save final deliverables
44
44
  \`\`\`
45
45
 
46
- ### OUTPUT RESULTS (DELIVERABLES) MUST BE WRITTEN to \`output/\` as files.
47
- ### Never just state results as text.
46
+ ### OUTPUT RULES:
48
47
 
49
- - The file system is being used as the main communication channel between the agent (you) and the user.
50
- - Hence, all outputs results must be saved to the \`output/\` folder as files. The user will only be able to view the files in the \`output/\` folder.
51
- - There are several reasons why we need to save the results to the \`output/\` folder:
52
- - The user might be building a long-running agent which could be interrupted and resumed later.
53
- - The user might pass the outputs to another agent for verification or further processing.
54
- - etc.
48
+ The \`output/\` folder is how you deliver files to the user\u2014the SDK retrieves everything from there.
49
+
50
+ Use your best judgment:
51
+ - **Questions, explanations, conversation** \u2192 respond in chat
52
+ - **Artifacts** (files, documents, code, charts, data) \u2192 save to \`output/\`
53
+
54
+ Examples:
55
+ - "What is a binary search?" \u2192 chat
56
+ - "Summarize this document" \u2192 chat
57
+ - "Create an Excel report" \u2192 \`output/sales_report.xlsx\`
58
+ - "Build an HTML dashboard" \u2192 \`output/dashboard.html\`
59
+ - "Generate a bar chart" \u2192 \`output/revenue_chart.png\`
60
+ - "Write a Python script" \u2192 \`output/parser.py\`
61
+ - "Convert this to JSON" \u2192 \`output/data.json\`
55
62
  `;var Xe=`## FILESYSTEM INSTRUCTIONS:
56
63
 
57
64
  You are running in a sandbox environment.
@@ -70,15 +77,22 @@ IMPORTANT - Directory structure:
70
77
  \u2514\u2500\u2500 output/ # Final deliverables
71
78
  \`\`\`
72
79
 
73
- ### OUTPUT RESULTS (DELIVERABLES) MUST BE WRITTEN to \`output/\` as files.
74
- ### Never just state results as text.
80
+ ### OUTPUT RULES:
81
+
82
+ The \`output/\` folder is how you deliver files to the user\u2014the SDK retrieves everything from there.
83
+
84
+ Use your best judgment:
85
+ - **Questions, explanations, conversation** \u2192 respond in chat
86
+ - **Artifacts** (files, documents, code, charts, data) \u2192 save to \`output/\`
75
87
 
76
- - The file system is being used as the main communication channel between the agent (you) and the user.
77
- - Hence, all outputs results must be saved to the \`output/\` folder as files. The user will only be able to view the files in the \`output/\` folder.
78
- - There are several reasons why we need to save the results to the \`output/\` folder:
79
- - The user might be building a long-running agent which could be interrupted and resumed later.
80
- - The user might pass the outputs to another agent for verification or further processing.
81
- - etc.
88
+ Examples:
89
+ - "What is a binary search?" \u2192 chat
90
+ - "Summarize this document" \u2192 chat
91
+ - "Create an Excel report" \u2192 \`output/sales_report.xlsx\`
92
+ - "Build an HTML dashboard" \u2192 \`output/dashboard.html\`
93
+ - "Generate a bar chart" \u2192 \`output/revenue_chart.png\`
94
+ - "Write a Python script" \u2192 \`output/parser.py\`
95
+ - "Convert this to JSON" \u2192 \`output/data.json\`
82
96
  `;var et=`## SYSTEM PROMPT
83
97
 
84
98
  {{systemPrompt}}`;var tt=`## STRUCTURED OUTPUT
@@ -178,10 +192,10 @@ Your previous attempt did not pass quality verification. Please address this fee
178
192
  {{feedback}}
179
193
 
180
194
  Make sure your output addresses all the feedback points above.
181
- `;var ct=Qe,lt=Xe,pt=et,ut=tt,Pe=nt,dt=it,Re=st,mt=rt,ft=ot,Me=at;function R(c,e){let n=c;for(let[s,a]of Object.entries(e))n=n.replace(new RegExp(`\\{\\{${s}\\}\\}`,"g"),a);return n}function Oe(c){let e=c.mode==="swe"?lt:ct,n=R(e,{workingDir:c.workingDir}).trim();if(c.systemPrompt&&(n+=`
195
+ `;var ct=Qe,lt=Xe,pt=et,ut=tt,Ee=nt,dt=it,Re=st,mt=rt,ft=ot,Me=at;function R(c,e){let n=c;for(let[s,a]of Object.entries(e))n=n.replace(new RegExp(`\\{\\{${s}\\}\\}`,"g"),a);return n}function Oe(c){let e=c.mode==="swe"?lt:ct,n=R(e,{workingDir:c.workingDir}).trim();if(c.systemPrompt&&(n+=`
182
196
 
183
197
 
184
- ${R(pt,{systemPrompt:c.systemPrompt}).trim()}`),c.schema){let s=E(c.schema)?N(c.schema):B(c.schema);n+=`
198
+ ${R(pt,{systemPrompt:c.systemPrompt}).trim()}`),c.schema){let s=P(c.schema)?N(c.schema):B(c.schema);n+=`
185
199
 
186
200
 
187
201
  ${R(ut,{schema:s}).trim()}`;}return n}function ue(c){let e=[...new Set(Object.keys(c).map(r=>r.split("/")[0]))].sort((r,p)=>r==="worker_task"?-1:p==="worker_task"?1:r.localeCompare(p));if(!e.length)return `context/
@@ -190,12 +204,12 @@ ${R(ut,{schema:s}).trim()}`;}return n}function ue(c){let e=[...new Set(Object.ke
190
204
  `)?e:e+`
191
205
  `;this.localQueue=this.localQueue.then(()=>this.appendLocal(n)).catch(()=>{});}bufferForDashboard(e){this.eventBuffer.push(e),this.scheduleFlush();}write(e){this.metaWritten||this.writeMeta();let n=JSON.stringify(e)+`
192
206
  `;this.localQueue=this.localQueue.then(()=>this.appendLocal(n)).catch(()=>{}),this.apiKey&&(this.eventBuffer.push(e),this.scheduleFlush());}writeMeta(){let e={_meta:{tag:this.tag,provider:this.provider,agent:this.agent,model:this.model,sandbox_id:this.sandboxId,timestamp:this.timestamp,...this.filterUndefined(this.observability)}},n=JSON.stringify(e)+`
193
- `;this.localQueue=this.localQueue.then(()=>this.appendLocal(n)).catch(()=>{}),this.apiKey&&this.eventBuffer.push(e),this.metaWritten=true;}async appendLocal(e){try{await this.dirReady,await appendFile(this.localFilePath,e,"utf-8");}catch(n){console.debug("[SessionLogger] Local write failed:",n);}}scheduleFlush(){if(this.eventBuffer.length>=ze){this.flushDashboard();return}!this.flushTimer&&!this.isClosed&&(this.flushTimer=setTimeout(()=>{this.flushTimer=void 0,this.flushDashboard();},He));}flushDashboard(){if(!this.apiKey||this.eventBuffer.length===0)return;let e=this.eventBuffer.splice(0);this.dashboardQueue=this.dashboardQueue.then(()=>this.sendToDashboard(e)).catch(()=>{});}async sendToDashboard(e){let n={tag:this.tag,provider:this.provider,agent:this.agent,model:this.model,sandboxId:this.sandboxId,timestamp:this.timestamp,...this.filterUndefined(this.observability),events:e};for(let s=1;s<=le;s++)try{let a=await fetch(`${this.dashboardUrl}/api/sessions/ingest`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`},body:JSON.stringify(n),signal:AbortSignal.timeout(1e4)});if(a.ok)return;if(a.status===429||a.status===401||a.status>=500){if(s===le){console.debug(`[SessionLogger] Dashboard ${a.status} after ${s} retries, requeueing`),this.requeueEvents(e);return}await this.delay(Te*s);continue}console.debug(`[SessionLogger] Dashboard ${a.status}, dropping events`);return}catch(a){s===le?(console.debug("[SessionLogger] Dashboard sync failed after retries, requeueing:",a),this.requeueEvents(e)):await this.delay(Te*s);}}requeueEvents(e){this.eventBuffer.unshift(...e);}validateConfig(e){if(e.apiKey&&!J.startsWith("https://")&&!J.includes("localhost")&&!J.includes("127.0.0.1"))throw new Error("Dashboard URL must use HTTPS when API key is provided")}delay(e){return new Promise(n=>setTimeout(n,e))}filterUndefined(e){return e?Object.fromEntries(Object.entries(e).filter(([,n])=>n!==void 0)):{}}};var de="COMPOSIO_API_KEY";function yt(c){return new Composio({apiKey:process.env[de]})}function Ie(c){if(!c?.trim())throw new Error("userId is required and cannot be empty")}async function me(c,e,n){Ie(c);let s=yt(),a={};return e?.toolkits&&(a.toolkits=e.toolkits),e?.tools&&(a.tools=e.tools),e?.authConfigs&&(a.authConfigs=e.authConfigs),e?.connectedAccounts&&(a.connectedAccounts=e.connectedAccounts),e?.manageConnections!==void 0&&(a.manageConnections=e.manageConnections),await s.create(c,Object.keys(a).length>0?a:void 0)}async function vt(c,e,n,s){Ie(c);let a=yt(),l=(await a.toolkits.get(e)).authConfigDetails?.find(r=>r.mode==="API_KEY");if(!l)throw new Error(`Toolkit "${e}" does not support API key authentication. Get an OAuth URL via Evolve.composio.auth() instead.`);await a.connectedAccounts.initiate(c,l.name,{config:AuthScheme.APIKey({api_key:n})});}async function Ae(c,e){if(Ie(c),e?.keys)for(let[s,a]of Object.entries(e.keys))try{await vt(c,s,a);}catch(o){throw new Error(`Failed to create API key connection for "${s}": ${o.message}`)}let n=await me(c,{toolkits:e?.toolkits,tools:e?.tools,authConfigs:e?.authConfigs,connectedAccounts:e?.connectedAccounts,manageConnections:e?.manageConnections});return {url:n.mcp.url,headers:n.mcp.headers}}function xt(c){if(!c?.trim())throw new Error("userId is required and cannot be empty")}function en(){return new Composio({apiKey:process.env[de]})}async function bt(c,e){xt(c);let s=await(await me(c,{manageConnections:false})).authorize(e);return {url:s.redirectUrl,connectionId:s.id}}async function wt(c,e){let n=await De(c),s={};for(let a of n)s[a.toolkit]=a.connected;return e?s[e]??false:s}async function De(c){return xt(c),(await en().connectedAccounts.list({userIds:[c],statuses:["ACTIVE"]})).items.map(s=>({toolkit:s.toolkit.slug,connected:s.status==="ACTIVE",accountId:s.id}))}var Fe={auth:bt,status:wt,connections:De};function nn(c){return c.replace(/\\/g,"\\\\").replace(/"/g,'\\"').replace(/\$/g,"\\$").replace(/`/g,"\\`")}var z=class{agentConfig;options;sandbox;hasRun=false;workingDir;lastRunTimestamp;registry;sessionLogger;skills;zodSchema;jsonSchema;schemaOptions;compiledValidator;constructor(e,n={}){if(this.agentConfig=e,this.options=n,this.workingDir=n.workingDirectory||Ye,this.skills=n.skills,n.schema)if(E(n.schema))this.zodSchema=n.schema,n.schemaOptions&&console.warn("[Evolve] schemaOptions ignored for Zod schemas - use .passthrough(), .strip(), z.coerce instead");else {this.jsonSchema=n.schema,this.schemaOptions=n.schemaOptions;try{let s=this.createAjvValidator();this.compiledValidator=s.compile(this.jsonSchema);}catch(s){throw new Error(`Invalid JSON Schema: ${s.message}`)}}this.registry=M(e.type);}createAjvValidator(){let n={...he[this.schemaOptions?.mode||"loose"],...this.schemaOptions?.coerceTypes!==void 0&&{coerceTypes:this.schemaOptions.coerceTypes},...this.schemaOptions?.removeAdditional!==void 0&&{removeAdditional:this.schemaOptions.removeAdditional},...this.schemaOptions?.useDefaults!==void 0&&{useDefaults:this.schemaOptions.useDefaults},...this.schemaOptions?.allErrors!==void 0&&{allErrors:this.schemaOptions.allErrors}};return new tn({...n,strict:false})}async getSandbox(){if(this.sandbox)return this.sandbox;if(!this.options.sandboxProvider)throw new Error("No sandbox provider configured");let e=this.options.sandboxProvider;if(this.options.sandboxId)(this.options.mcpServers||this.options.context||this.options.files||this.options.systemPrompt)&&console.warn("[Evolve] Connecting to existing sandbox - ignoring mcpServers, context, files, and systemPrompt"),this.sandbox=await e.connect(this.options.sandboxId),this.hasRun=true;else {let n=this.buildEnvironmentVariables();this.sandbox=await e.create({envs:n,workingDirectory:this.workingDir}),await this.setupAgentAuth(this.sandbox),await this.setupWorkspace(this.sandbox);}return this.sandbox}buildEnvironmentVariables(){let e={};if(this.agentConfig.oauthFileContent)this.registry.oauthActivationEnv&&(e[this.registry.oauthActivationEnv.key]=this.registry.oauthActivationEnv.value);else {let n=this.agentConfig.isOAuth&&this.registry.oauthEnv?this.registry.oauthEnv:this.registry.apiKeyEnv;e[n]=this.agentConfig.apiKey;}return this.agentConfig.isDirectMode&&!this.agentConfig.isOAuth?this.agentConfig.baseUrl&&(e[this.registry.baseUrlEnv]=this.agentConfig.baseUrl):this.agentConfig.isDirectMode||(e[this.registry.baseUrlEnv]=this.registry.usePassthroughGateway?je():K(),e.EVOLVE_API_KEY=this.agentConfig.apiKey),this.options.secrets&&Object.assign(e,this.options.secrets),e}async setupAgentAuth(e){if(this.agentConfig.oauthFileContent&&this.registry.oauthFileName){let n=this.registry.mcpConfig.settingsDir.replace(/^~/,"/home/user");await e.files.makeDir(n),await e.files.write(`${n}/${this.registry.oauthFileName}`,this.agentConfig.oauthFileContent);return}this.registry.setupCommand&&await e.commands.run(this.registry.setupCommand,{timeoutMs:3e4});}async setupWorkspace(e){let n=this.options.workspaceMode||"knowledge",s=n==="swe"?`${this.workingDir}/repo ${this.workingDir}/context ${this.workingDir}/scripts ${this.workingDir}/temp ${this.workingDir}/output`:`${this.workingDir}/context ${this.workingDir}/scripts ${this.workingDir}/temp ${this.workingDir}/output`;await e.commands.run(`mkdir -p ${s}`,{timeoutMs:3e4});let a=Oe({workingDir:this.workingDir,systemPrompt:this.options.systemPrompt,schema:this.zodSchema||this.jsonSchema,mode:n}),o=`${this.workingDir}/${this.registry.systemPromptFile}`;await e.files.write(o,a),this.options.context&&await this.uploadContextFiles(e,this.options.context),this.options.files&&await this.uploadWorkspaceFiles(e,this.options.files);let i={...this.options.mcpServers};if(this.options.composio){let l=await Ae(this.options.composio.userId,this.options.composio.config);i={...i,composio:{type:"http",url:l.url,headers:l.headers}};}Object.keys(i).length>0&&await Ce(this.agentConfig.type,e,this.workingDir,i),this.skills?.length&&await this.setupSkills(e);}async setupSkills(e){if(!this.skills?.length)return;let{skillsConfig:n}=this.registry,{sourceDir:s,targetDir:a}=n;await e.files.makeDir(a);for(let o of this.skills){let i=`cp -r ${s}/${o} ${a}/ 2>/dev/null || true`;await e.commands.run(i,{timeoutMs:3e4});}}async uploadContextFiles(e,n){let s=Object.entries(n).map(([o,i])=>({path:`${this.workingDir}/context/${o}`,data:i}));if(s.length===0)return;let a=new Set(s.map(o=>o.path.substring(0,o.path.lastIndexOf("/"))).filter(Boolean));a.size>0&&await e.commands.run(`mkdir -p ${Array.from(a).join(" ")}`,{timeoutMs:3e4}),await e.files.writeBatch(s);}async uploadWorkspaceFiles(e,n){let s=Object.entries(n).map(([o,i])=>({path:o.startsWith("/")?o:`${this.workingDir}/${o}`,data:i}));if(s.length===0)return;let a=new Set(s.map(o=>o.path.substring(0,o.path.lastIndexOf("/"))).filter(Boolean));a.size>0&&await e.commands.run(`mkdir -p ${Array.from(a).join(" ")}`,{timeoutMs:3e4}),await e.files.writeBatch(s);}buildCommand(e){return this.registry.buildCommand({prompt:nn(e),model:this.agentConfig.model||this.registry.defaultModel,isResume:this.hasRun,reasoningEffort:this.agentConfig.reasoningEffort,betas:this.agentConfig.betas,isDirectMode:this.agentConfig.isDirectMode,skills:this.skills})}async run(e,n){let{prompt:s,timeoutMs:a=W,background:o=false}=e,i=await this.getSandbox();if(this.lastRunTimestamp=Date.now(),!this.sessionLogger){let m=this.options.sandboxProvider;this.sessionLogger=new G({provider:m?.name||m?.providerType||"unknown",agent:this.agentConfig.type,model:this.agentConfig.model||this.registry.defaultModel,sandboxId:i.sandboxId,tagPrefix:this.options.sessionTagPrefix,apiKey:this.agentConfig.isDirectMode?void 0:this.agentConfig.apiKey,observability:this.options.observability});}this.sessionLogger.writePrompt(s);let l=this.buildCommand(s),r="",p=O(this.agentConfig.type),t=m=>{r+=m;let f=r.split(`
207
+ `;this.localQueue=this.localQueue.then(()=>this.appendLocal(n)).catch(()=>{}),this.apiKey&&this.eventBuffer.push(e),this.metaWritten=true;}async appendLocal(e){try{await this.dirReady,await appendFile(this.localFilePath,e,"utf-8");}catch(n){console.debug("[SessionLogger] Local write failed:",n);}}scheduleFlush(){if(this.eventBuffer.length>=ze){this.flushDashboard();return}!this.flushTimer&&!this.isClosed&&(this.flushTimer=setTimeout(()=>{this.flushTimer=void 0,this.flushDashboard();},He));}flushDashboard(){if(!this.apiKey||this.eventBuffer.length===0)return;let e=this.eventBuffer.splice(0);this.dashboardQueue=this.dashboardQueue.then(()=>this.sendToDashboard(e)).catch(()=>{});}async sendToDashboard(e){let n={tag:this.tag,provider:this.provider,agent:this.agent,model:this.model,sandboxId:this.sandboxId,timestamp:this.timestamp,...this.filterUndefined(this.observability),events:e};for(let s=1;s<=le;s++)try{let a=await fetch(`${this.dashboardUrl}/api/sessions/ingest`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${this.apiKey}`},body:JSON.stringify(n),signal:AbortSignal.timeout(1e4)});if(a.ok)return;if(a.status===429||a.status===401||a.status>=500){if(s===le){console.debug(`[SessionLogger] Dashboard ${a.status} after ${s} retries, requeueing`),this.requeueEvents(e);return}await this.delay(_e*s);continue}console.debug(`[SessionLogger] Dashboard ${a.status}, dropping events`);return}catch(a){s===le?(console.debug("[SessionLogger] Dashboard sync failed after retries, requeueing:",a),this.requeueEvents(e)):await this.delay(_e*s);}}requeueEvents(e){this.eventBuffer.unshift(...e);}validateConfig(e){if(e.apiKey&&!J.startsWith("https://")&&!J.includes("localhost")&&!J.includes("127.0.0.1"))throw new Error("Dashboard URL must use HTTPS when API key is provided")}delay(e){return new Promise(n=>setTimeout(n,e))}filterUndefined(e){return e?Object.fromEntries(Object.entries(e).filter(([,n])=>n!==void 0)):{}}};var de="COMPOSIO_API_KEY";function yt(c){return new Composio({apiKey:process.env[de]})}function Ie(c){if(!c?.trim())throw new Error("userId is required and cannot be empty")}async function me(c,e,n){Ie(c);let s=yt(),a={};return e?.toolkits&&(a.toolkits=e.toolkits),e?.tools&&(a.tools=e.tools),e?.authConfigs&&(a.authConfigs=e.authConfigs),e?.connectedAccounts&&(a.connectedAccounts=e.connectedAccounts),e?.manageConnections!==void 0&&(a.manageConnections=e.manageConnections),await s.create(c,Object.keys(a).length>0?a:void 0)}async function vt(c,e,n,s){Ie(c);let a=yt(),l=(await a.toolkits.get(e)).authConfigDetails?.find(r=>r.mode==="API_KEY");if(!l)throw new Error(`Toolkit "${e}" does not support API key authentication. Get an OAuth URL via Evolve.composio.auth() instead.`);await a.connectedAccounts.initiate(c,l.name,{config:AuthScheme.APIKey({api_key:n})});}async function Ae(c,e){if(Ie(c),e?.keys)for(let[s,a]of Object.entries(e.keys))try{await vt(c,s,a);}catch(o){throw new Error(`Failed to create API key connection for "${s}": ${o.message}`)}let n=await me(c,{toolkits:e?.toolkits,tools:e?.tools,authConfigs:e?.authConfigs,connectedAccounts:e?.connectedAccounts,manageConnections:e?.manageConnections});return {url:n.mcp.url,headers:n.mcp.headers}}function xt(c){if(!c?.trim())throw new Error("userId is required and cannot be empty")}function en(){return new Composio({apiKey:process.env[de]})}async function bt(c,e){xt(c);let s=await(await me(c,{manageConnections:false})).authorize(e);return {url:s.redirectUrl,connectionId:s.id}}async function Ct(c,e){let n=await De(c),s={};for(let a of n)s[a.toolkit]=a.connected;return e?s[e]??false:s}async function De(c){return xt(c),(await en().connectedAccounts.list({userIds:[c],statuses:["ACTIVE"]})).items.map(s=>({toolkit:s.toolkit.slug,connected:s.status==="ACTIVE",accountId:s.id}))}var Fe={auth:bt,status:Ct,connections:De};function nn(c){return c.replace(/\\/g,"\\\\").replace(/"/g,'\\"').replace(/\$/g,"\\$").replace(/`/g,"\\`")}var z=class{agentConfig;options;sandbox;hasRun=false;workingDir;lastRunTimestamp;registry;sessionLogger;skills;zodSchema;jsonSchema;schemaOptions;compiledValidator;constructor(e,n={}){if(this.agentConfig=e,this.options=n,this.workingDir=n.workingDirectory||Ye,this.skills=n.skills,n.schema)if(P(n.schema))this.zodSchema=n.schema,n.schemaOptions&&console.warn("[Evolve] schemaOptions ignored for Zod schemas - use .passthrough(), .strip(), z.coerce instead");else {this.jsonSchema=n.schema,this.schemaOptions=n.schemaOptions;try{let s=this.createAjvValidator();this.compiledValidator=s.compile(this.jsonSchema);}catch(s){throw new Error(`Invalid JSON Schema: ${s.message}`)}}this.registry=M(e.type);}createAjvValidator(){let n={...he[this.schemaOptions?.mode||"loose"],...this.schemaOptions?.coerceTypes!==void 0&&{coerceTypes:this.schemaOptions.coerceTypes},...this.schemaOptions?.removeAdditional!==void 0&&{removeAdditional:this.schemaOptions.removeAdditional},...this.schemaOptions?.useDefaults!==void 0&&{useDefaults:this.schemaOptions.useDefaults},...this.schemaOptions?.allErrors!==void 0&&{allErrors:this.schemaOptions.allErrors}};return new tn({...n,strict:false})}async getSandbox(){if(this.sandbox)return this.sandbox;if(!this.options.sandboxProvider)throw new Error("No sandbox provider configured");let e=this.options.sandboxProvider;if(this.options.sandboxId)(this.options.mcpServers||this.options.context||this.options.files||this.options.systemPrompt)&&console.warn("[Evolve] Connecting to existing sandbox - ignoring mcpServers, context, files, and systemPrompt"),this.sandbox=await e.connect(this.options.sandboxId),this.hasRun=true;else {let n=this.buildEnvironmentVariables();this.sandbox=await e.create({envs:n,workingDirectory:this.workingDir}),await this.setupAgentAuth(this.sandbox),await this.setupWorkspace(this.sandbox);}return this.sandbox}buildEnvironmentVariables(){let e={};if(this.agentConfig.oauthFileContent)this.registry.oauthActivationEnv&&(e[this.registry.oauthActivationEnv.key]=this.registry.oauthActivationEnv.value);else {let n=this.agentConfig.isOAuth&&this.registry.oauthEnv?this.registry.oauthEnv:this.registry.apiKeyEnv;e[n]=this.agentConfig.apiKey;}return this.agentConfig.isDirectMode&&!this.agentConfig.isOAuth?this.agentConfig.baseUrl&&(e[this.registry.baseUrlEnv]=this.agentConfig.baseUrl):this.agentConfig.isDirectMode||(e[this.registry.baseUrlEnv]=this.registry.usePassthroughGateway?Le():K(),e.EVOLVE_API_KEY=this.agentConfig.apiKey),this.options.secrets&&Object.assign(e,this.options.secrets),e}async setupAgentAuth(e){if(this.agentConfig.oauthFileContent&&this.registry.oauthFileName){let n=this.registry.mcpConfig.settingsDir.replace(/^~/,"/home/user");await e.files.makeDir(n),await e.files.write(`${n}/${this.registry.oauthFileName}`,this.agentConfig.oauthFileContent);return}this.registry.setupCommand&&await e.commands.run(this.registry.setupCommand,{timeoutMs:3e4});}async setupWorkspace(e){let n=this.options.workspaceMode||"knowledge",s=n==="swe"?`${this.workingDir}/repo ${this.workingDir}/context ${this.workingDir}/scripts ${this.workingDir}/temp ${this.workingDir}/output`:`${this.workingDir}/context ${this.workingDir}/scripts ${this.workingDir}/temp ${this.workingDir}/output`;await e.commands.run(`mkdir -p ${s}`,{timeoutMs:3e4});let a=Oe({workingDir:this.workingDir,systemPrompt:this.options.systemPrompt,schema:this.zodSchema||this.jsonSchema,mode:n}),o=`${this.workingDir}/${this.registry.systemPromptFile}`;await e.files.write(o,a),this.options.context&&await this.uploadContextFiles(e,this.options.context),this.options.files&&await this.uploadWorkspaceFiles(e,this.options.files);let i={...this.options.mcpServers};if(this.options.composio){let l=await Ae(this.options.composio.userId,this.options.composio.config);i={...i,composio:{type:"http",url:l.url,headers:l.headers}};}Object.keys(i).length>0&&await we(this.agentConfig.type,e,this.workingDir,i),this.skills?.length&&await this.setupSkills(e);}async setupSkills(e){if(!this.skills?.length)return;let{skillsConfig:n}=this.registry,{sourceDir:s,targetDir:a}=n;await e.files.makeDir(a);for(let o of this.skills){let i=`cp -r ${s}/${o} ${a}/ 2>/dev/null || true`;await e.commands.run(i,{timeoutMs:3e4});}}async uploadContextFiles(e,n){let s=Object.entries(n).map(([o,i])=>({path:`${this.workingDir}/context/${o}`,data:i}));if(s.length===0)return;let a=new Set(s.map(o=>o.path.substring(0,o.path.lastIndexOf("/"))).filter(Boolean));a.size>0&&await e.commands.run(`mkdir -p ${Array.from(a).join(" ")}`,{timeoutMs:3e4}),await e.files.writeBatch(s);}async uploadWorkspaceFiles(e,n){let s=Object.entries(n).map(([o,i])=>({path:o.startsWith("/")?o:`${this.workingDir}/${o}`,data:i}));if(s.length===0)return;let a=new Set(s.map(o=>o.path.substring(0,o.path.lastIndexOf("/"))).filter(Boolean));a.size>0&&await e.commands.run(`mkdir -p ${Array.from(a).join(" ")}`,{timeoutMs:3e4}),await e.files.writeBatch(s);}buildCommand(e){return this.registry.buildCommand({prompt:nn(e),model:this.agentConfig.model||this.registry.defaultModel,isResume:this.hasRun,reasoningEffort:this.agentConfig.reasoningEffort,betas:this.agentConfig.betas,isDirectMode:this.agentConfig.isDirectMode,skills:this.skills})}async run(e,n){let{prompt:s,timeoutMs:a=W,background:o=false}=e,i=await this.getSandbox();if(this.lastRunTimestamp=Date.now(),!this.sessionLogger){let m=this.options.sandboxProvider;this.sessionLogger=new G({provider:m?.name||m?.providerType||"unknown",agent:this.agentConfig.type,model:this.agentConfig.model||this.registry.defaultModel,sandboxId:i.sandboxId,tagPrefix:this.options.sessionTagPrefix,apiKey:this.agentConfig.isDirectMode?void 0:this.agentConfig.apiKey,observability:this.options.observability});}this.sessionLogger.writePrompt(s);let l=this.buildCommand(s),r="",p=O(this.agentConfig.type),t=m=>{r+=m;let f=r.split(`
194
208
  `);r=f.pop()??"";for(let h of f){if(!h.trim())continue;let g=p(h);if(this.sessionLogger?.writeEventParsed(h,g),n?.onStdout?.(h+`
195
209
  `),g&&n?.onContent)for(let y of g)n.onContent(y);}},u=m=>{n?.onStderr?.(m);};if(o){let m=await i.commands.spawn(l,{cwd:this.workingDir,timeoutMs:a,onStdout:t,onStderr:u});return this.hasRun=true,{sandboxId:i.sandboxId,exitCode:0,stdout:`Background process started with ID ${m.processId}`,stderr:""}}let d=await i.commands.run(l,{cwd:this.workingDir,timeoutMs:a,onStdout:t,onStderr:u});if(r.trim()){let m=p(r);if(this.sessionLogger?.writeEventParsed(r,m),n?.onStdout?.(r+`
196
210
  `),m&&n?.onContent)for(let f of m)n.onContent(f);}return this.hasRun=true,{sandboxId:i.sandboxId,exitCode:d.exitCode,stdout:d.stdout,stderr:d.stderr}}async executeCommand(e,n={},s){let{timeoutMs:a=W,background:o=false}=n,i=await this.getSandbox();this.lastRunTimestamp=Date.now();let l="",r="",p=d=>{l+=d,s?.onStdout?.(d);},t=d=>{r+=d,s?.onStderr?.(d);};if(o){let d=await i.commands.spawn(e,{cwd:this.workingDir,timeoutMs:a,onStdout:p,onStderr:t});return {sandboxId:i.sandboxId,exitCode:0,stdout:`Background process started with ID ${d.processId}`,stderr:""}}let u=await i.commands.run(e,{cwd:this.workingDir,timeoutMs:a,onStdout:p,onStderr:t});return {sandboxId:i.sandboxId,exitCode:u.exitCode,stdout:l,stderr:r}}async uploadContext(e){let n=await this.getSandbox();await this.uploadContextFiles(n,e);}async uploadFiles(e){let n=await this.getSandbox();await this.uploadWorkspaceFiles(n,e);}async getOutputFiles(e=false){let n=await this.getSandbox(),s=`${this.workingDir}/output`,a=e?"":"-maxdepth 1",i=(await n.commands.run(`find ${s} ${a} -type f -exec stat -c '%n|%Z' {} \\; 2>/dev/null || true`,{timeoutMs:3e4})).stdout.split(`
197
- `).filter(Boolean),l=this.lastRunTimestamp?Math.floor(this.lastRunTimestamp/1e3)-2:0,r=[],p=`${s}/`;for(let f of i){let[h,g]=f.split("|");if(!h||!g)continue;let y=parseInt(g,10);l>0&&y<l||r.push(h);}let t={},u=await Promise.all(r.map(async f=>{try{let h=await n.files.read(f);return {relativePath:f.startsWith(p)?f.slice(p.length):f.split("/").pop()||f,content:h}}catch{return null}}));for(let f of u)f&&(t[f.relativePath]=f.content);if(!this.zodSchema&&!this.jsonSchema)return {files:t,data:null};let d=t["result.json"];if(!d)return {files:t,data:null,error:"Schema provided but agent did not create output/result.json"};let m=typeof d=="string"?d:new TextDecoder().decode(d);try{let f=JSON.parse(m);if(this.zodSchema){let h=this.zodSchema.safeParse(f);return h.success?{files:t,data:h.data}:{files:t,data:null,error:`Schema validation failed: ${h.error.message}`,rawData:m}}if(this.compiledValidator){if(this.compiledValidator(f))return {files:t,data:f};{let g=this.compiledValidator.errors?.map(y=>`${y.instancePath} ${y.message}`).join(", ")||"Unknown validation error";return {files:t,data:null,error:`Schema validation failed: ${g}`,rawData:m}}}return {files:t,data:null}}catch(f){return {files:t,data:null,error:`Failed to parse result.json: ${f.message}`,rawData:m}}}getSession(){return this.sandbox?.sandboxId||this.options.sandboxId||null}async setSession(e){this.sessionLogger&&(await this.sessionLogger.close(),this.sessionLogger=void 0),this.options.sandboxId=e,this.sandbox=void 0,this.hasRun=true;}async pause(){this.sandbox&&await this.sandbox.pause();}async resume(){this.sandbox&&this.options.sandboxProvider&&(this.sandbox=await this.options.sandboxProvider.connect(this.sandbox.sandboxId));}async kill(){this.sessionLogger&&(await this.sessionLogger.close(),this.sessionLogger=void 0),this.sandbox&&(await this.sandbox.kill(),this.sandbox=void 0,this.hasRun=false);}async getHost(e){return (await this.getSandbox()).getHost(e)}getAgentType(){return this.agentConfig.type}getSessionTag(){return this.sessionLogger?.getTag()||null}getSessionTimestamp(){return this.sessionLogger?.getTimestamp()||null}};var H=class extends EventEmitter{config={};agent;constructor(){super();}on(e,n){return super.on(e,n)}off(e,n){return super.off(e,n)}emit(e,...n){return super.emit(e,...n)}withAgent(e){return e&&(this.config.agent=e),this}withSandbox(e){return this.config.sandbox=e,this}withWorkingDirectory(e){return this.config.workingDirectory=e,this}withWorkspaceMode(e){return this.config.workspaceMode=e,this}withSecrets(e){return this.config.secrets={...this.config.secrets,...e},this}withSession(e){return this.config.sandboxId=e,this}withSystemPrompt(e){return this.config.systemPrompt=e,this}withContext(e){return this.config.context={...this.config.context,...e},this}withFiles(e){return this.config.files={...this.config.files,...e},this}withMcpServers(e){return this.config.mcpServers={...this.config.mcpServers,...e},this}withSkills(e){return this.config.skills=e,this}withSchema(e,n){return this.config.schema=e,n&&(E(e)?console.warn("[Evolve] schemaOptions ignored for Zod schemas - use .passthrough(), .strip(), z.coerce instead"):this.config.schemaOptions=n),this}withSessionTagPrefix(e){return this.config.sessionTagPrefix=e,this}withObservability(e){return this.config.observability={...this.config.observability,...e},this}withComposio(e,n){return this.config.composio={userId:e,config:n},this}static composio=Fe;async initializeAgent(){let e=_e(this.config.agent),n=this.config.sandbox??await Ee(),s=e.isDirectMode?{}:Ke(e.apiKey),a={sandboxProvider:n,secrets:this.config.secrets,sandboxId:this.config.sandboxId,workingDirectory:this.config.workingDirectory,workspaceMode:this.config.workspaceMode,systemPrompt:this.config.systemPrompt,context:this.config.context,files:this.config.files,mcpServers:{...s,...this.config.mcpServers},skills:this.config.skills,schema:this.config.schema,schemaOptions:this.config.schemaOptions,sessionTagPrefix:this.config.sessionTagPrefix,observability:this.config.observability,composio:this.config.composio};this.agent=new z(e,a);}createStreamCallbacks(){let e=this.listenerCount("stdout")>0,n=this.listenerCount("stderr")>0,s=this.listenerCount("content")>0;return {onStdout:e?a=>this.emit("stdout",a):void 0,onStderr:n?a=>this.emit("stderr",a):void 0,onContent:s?a=>this.emit("content",a):void 0}}async run({prompt:e,timeoutMs:n,background:s}){this.agent||await this.initializeAgent();let a=this.createStreamCallbacks();return this.agent.run({prompt:e,timeoutMs:n,background:s},a)}async executeCommand(e,n={}){this.agent||await this.initializeAgent();let s=this.createStreamCallbacks();return this.agent.executeCommand(e,n,s)}async uploadContext(e){return this.agent||await this.initializeAgent(),this.agent.uploadContext(e)}async uploadFiles(e){return this.agent||await this.initializeAgent(),this.agent.uploadFiles(e)}async getOutputFiles(e=false){if(!this.agent)throw new Error("Agent not initialized. Call run() first.");return this.agent.getOutputFiles(e)}getSession(){return this.agent?this.agent.getSession():this.config.sandboxId??null}async setSession(e){this.agent&&await this.agent.setSession(e),this.config.sandboxId=e;}async pause(){if(this.agent)return this.agent.pause();this.config.sandboxId&&this.config.sandbox&&await(await this.config.sandbox.connect(this.config.sandboxId)).pause();}async resume(){if(this.agent)return this.agent.resume();this.config.sandboxId&&this.config.sandbox&&await this.config.sandbox.connect(this.config.sandboxId);}async kill(){if(this.agent)return this.agent.kill();this.config.sandboxId&&this.config.sandbox&&await(await this.config.sandbox.connect(this.config.sandboxId)).kill();}async getHost(e){return this.agent||await this.initializeAgent(),this.agent.getHost(e)}getSessionTag(){return this.agent?.getSessionTag()||null}getSessionTimestamp(){return this.agent?.getSessionTimestamp()||null}};var L=class{permits;queue=[];constructor(e){if(e<1)throw new Error("Semaphore max must be >= 1");this.permits=e;}async use(e){await this.acquire();try{return await e()}finally{this.release();}}acquire(){return this.permits>0?(this.permits--,Promise.resolve()):new Promise(e=>this.queue.push(e))}release(){this.queue.length>0?this.queue.shift()():this.permits++;}};var A=Symbol.for("evolve.SwarmResult"),j=class c extends Array{get success(){return this.filter(e=>e.status==="success")}get filtered(){return this.filter(e=>e.status==="filtered")}get error(){return this.filter(e=>e.status==="error")}static from(e){let n=new c;return n.push(...e),n}};var $e=class c{config;semaphore;constructor(e={}){let n=e.concurrency??Je;if(n>ke)throw new Error(`concurrency=${n} exceeds max ${ke}. For higher parallelism, scale horizontally with multiple processes.`);this.config={agent:e.agent,sandbox:e.sandbox,tag:e.tag??"swarm",concurrency:n,timeoutMs:e.timeoutMs??W,workspaceMode:e.workspaceMode??"knowledge",retry:e.retry,mcpServers:e.mcpServers,skills:e.skills,composio:e.composio},this.semaphore=new L(this.config.concurrency);}async map(e){let{items:n,prompt:s,bestOf:a,verify:o}=e,i=e.retry??this.config.retry,l=this.generateOperationId(),r=e.timeoutMs??this.config.timeoutMs;if(a&&o)throw new Error("map() cannot use both bestOf and verify options simultaneously");let p=await Promise.all(n.map((t,u)=>a?this.executeMapItemWithBestOf(t,s,u,l,e,r,i):o?this.executeMapItemWithVerify(t,s,u,l,e,r,i):i?T(d=>this.executeMapItem(t,s,u,l,e,r,d),i,u):this.executeMapItem(t,s,u,l,e,r)));return j.from(p)}async filter(e){let{items:n,prompt:s,condition:a,verify:o}=e,i=e.retry??this.config.retry,l=this.generateOperationId(),r=e.timeoutMs??this.config.timeoutMs,p=await Promise.all(n.map((u,d)=>o?this.executeFilterItemWithVerify(u,s,d,l,e,r,i):i?T(m=>this.executeFilterItem(u,s,d,l,e,r,m),i,d):this.executeFilterItem(u,s,d,l,e,r))),t=[];for(let u of p)if(u.status==="error")t.push(u);else if(u.data!==null)try{a(u.data)?t.push(u):t.push({...u,status:"filtered"});}catch(d){t.push({...u,status:"error",data:null,error:`Condition function threw: ${d.message}`});}return j.from(t)}async reduce(e){let{items:n,prompt:s,verify:a}=e,o=e.retry??this.config.retry,i=this.generateOperationId(),l=e.timeoutMs??this.config.timeoutMs,r=[],p=[];n.forEach((x,C)=>{r.push(this.getFiles(x)),p.push(this.getIndex(x,C));});let t={};r.forEach((x,C)=>{Object.entries(x).forEach(([w,S])=>{t[`item_${p[C]}/${w}`]=S;});});let u=ue(t),d=R(ft,{fileTree:u}),m=e.systemPrompt?`${d}
211
+ `).filter(Boolean),l=this.lastRunTimestamp?Math.floor(this.lastRunTimestamp/1e3)-2:0,r=[],p=`${s}/`;for(let f of i){let[h,g]=f.split("|");if(!h||!g)continue;let y=parseInt(g,10);l>0&&y<l||r.push(h);}let t={},u=await Promise.all(r.map(async f=>{try{let h=await n.files.read(f);return {relativePath:f.startsWith(p)?f.slice(p.length):f.split("/").pop()||f,content:h}}catch{return null}}));for(let f of u)f&&(t[f.relativePath]=f.content);if(!this.zodSchema&&!this.jsonSchema)return {files:t,data:null};let d=t["result.json"];if(!d)return {files:t,data:null,error:"Schema provided but agent did not create output/result.json"};let m=typeof d=="string"?d:new TextDecoder().decode(d);try{let f=JSON.parse(m);if(this.zodSchema){let h=this.zodSchema.safeParse(f);return h.success?{files:t,data:h.data}:{files:t,data:null,error:`Schema validation failed: ${h.error.message}`,rawData:m}}if(this.compiledValidator){if(this.compiledValidator(f))return {files:t,data:f};{let g=this.compiledValidator.errors?.map(y=>`${y.instancePath} ${y.message}`).join(", ")||"Unknown validation error";return {files:t,data:null,error:`Schema validation failed: ${g}`,rawData:m}}}return {files:t,data:null}}catch(f){return {files:t,data:null,error:`Failed to parse result.json: ${f.message}`,rawData:m}}}getSession(){return this.sandbox?.sandboxId||this.options.sandboxId||null}async setSession(e){this.sessionLogger&&(await this.sessionLogger.close(),this.sessionLogger=void 0),this.options.sandboxId=e,this.sandbox=void 0,this.hasRun=true;}async pause(){this.sandbox&&await this.sandbox.pause();}async resume(){this.sandbox&&this.options.sandboxProvider&&(this.sandbox=await this.options.sandboxProvider.connect(this.sandbox.sandboxId));}async kill(){this.sessionLogger&&(await this.sessionLogger.close(),this.sessionLogger=void 0),this.sandbox&&(await this.sandbox.kill(),this.sandbox=void 0,this.hasRun=false);}async getHost(e){return (await this.getSandbox()).getHost(e)}getAgentType(){return this.agentConfig.type}getSessionTag(){return this.sessionLogger?.getTag()||null}getSessionTimestamp(){return this.sessionLogger?.getTimestamp()||null}};var H=class extends EventEmitter{config={};agent;constructor(){super();}on(e,n){return super.on(e,n)}off(e,n){return super.off(e,n)}emit(e,...n){return super.emit(e,...n)}withAgent(e){return e&&(this.config.agent=e),this}withSandbox(e){return this.config.sandbox=e,this}withWorkingDirectory(e){return this.config.workingDirectory=e,this}withWorkspaceMode(e){return this.config.workspaceMode=e,this}withSecrets(e){return this.config.secrets={...this.config.secrets,...e},this}withSession(e){return this.config.sandboxId=e,this}withSystemPrompt(e){return this.config.systemPrompt=e,this}withContext(e){return this.config.context={...this.config.context,...e},this}withFiles(e){return this.config.files={...this.config.files,...e},this}withMcpServers(e){return this.config.mcpServers={...this.config.mcpServers,...e},this}withSkills(e){return this.config.skills=e,this}withSchema(e,n){return this.config.schema=e,n&&(P(e)?console.warn("[Evolve] schemaOptions ignored for Zod schemas - use .passthrough(), .strip(), z.coerce instead"):this.config.schemaOptions=n),this}withSessionTagPrefix(e){return this.config.sessionTagPrefix=e,this}withObservability(e){return this.config.observability={...this.config.observability,...e},this}withComposio(e,n){return this.config.composio={userId:e,config:n},this}static composio=Fe;async initializeAgent(){let e=Te(this.config.agent),n=this.config.sandbox??await Pe(),s=e.isDirectMode?{}:Ke(e.apiKey),a={sandboxProvider:n,secrets:this.config.secrets,sandboxId:this.config.sandboxId,workingDirectory:this.config.workingDirectory,workspaceMode:this.config.workspaceMode,systemPrompt:this.config.systemPrompt,context:this.config.context,files:this.config.files,mcpServers:{...s,...this.config.mcpServers},skills:this.config.skills,schema:this.config.schema,schemaOptions:this.config.schemaOptions,sessionTagPrefix:this.config.sessionTagPrefix,observability:this.config.observability,composio:this.config.composio};this.agent=new z(e,a);}createStreamCallbacks(){let e=this.listenerCount("stdout")>0,n=this.listenerCount("stderr")>0,s=this.listenerCount("content")>0;return {onStdout:e?a=>this.emit("stdout",a):void 0,onStderr:n?a=>this.emit("stderr",a):void 0,onContent:s?a=>this.emit("content",a):void 0}}async run({prompt:e,timeoutMs:n,background:s}){this.agent||await this.initializeAgent();let a=this.createStreamCallbacks();return this.agent.run({prompt:e,timeoutMs:n,background:s},a)}async executeCommand(e,n={}){this.agent||await this.initializeAgent();let s=this.createStreamCallbacks();return this.agent.executeCommand(e,n,s)}async uploadContext(e){return this.agent||await this.initializeAgent(),this.agent.uploadContext(e)}async uploadFiles(e){return this.agent||await this.initializeAgent(),this.agent.uploadFiles(e)}async getOutputFiles(e=false){if(!this.agent)throw new Error("Agent not initialized. Call run() first.");return this.agent.getOutputFiles(e)}getSession(){return this.agent?this.agent.getSession():this.config.sandboxId??null}async setSession(e){this.agent&&await this.agent.setSession(e),this.config.sandboxId=e;}async pause(){if(this.agent)return this.agent.pause();this.config.sandboxId&&this.config.sandbox&&await(await this.config.sandbox.connect(this.config.sandboxId)).pause();}async resume(){if(this.agent)return this.agent.resume();this.config.sandboxId&&this.config.sandbox&&await this.config.sandbox.connect(this.config.sandboxId);}async kill(){if(this.agent)return this.agent.kill();this.config.sandboxId&&this.config.sandbox&&await(await this.config.sandbox.connect(this.config.sandboxId)).kill();}async getHost(e){return this.agent||await this.initializeAgent(),this.agent.getHost(e)}getSessionTag(){return this.agent?.getSessionTag()||null}getSessionTimestamp(){return this.agent?.getSessionTimestamp()||null}};var j=class{permits;queue=[];constructor(e){if(e<1)throw new Error("Semaphore max must be >= 1");this.permits=e;}async use(e){await this.acquire();try{return await e()}finally{this.release();}}acquire(){return this.permits>0?(this.permits--,Promise.resolve()):new Promise(e=>this.queue.push(e))}release(){this.queue.length>0?this.queue.shift()():this.permits++;}};var A=Symbol.for("evolve.SwarmResult"),L=class c extends Array{get success(){return this.filter(e=>e.status==="success")}get filtered(){return this.filter(e=>e.status==="filtered")}get error(){return this.filter(e=>e.status==="error")}static from(e){let n=new c;return n.push(...e),n}};var $e=class c{config;semaphore;constructor(e={}){let n=e.concurrency??Je;if(n>ke)throw new Error(`concurrency=${n} exceeds max ${ke}. For higher parallelism, scale horizontally with multiple processes.`);this.config={agent:e.agent,sandbox:e.sandbox,tag:e.tag??"swarm",concurrency:n,timeoutMs:e.timeoutMs??W,workspaceMode:e.workspaceMode??"knowledge",retry:e.retry,mcpServers:e.mcpServers,skills:e.skills,composio:e.composio},this.semaphore=new j(this.config.concurrency);}async map(e){let{items:n,prompt:s,bestOf:a,verify:o}=e,i=e.retry??this.config.retry,l=this.generateOperationId(),r=e.timeoutMs??this.config.timeoutMs;if(a&&o)throw new Error("map() cannot use both bestOf and verify options simultaneously");let p=await Promise.all(n.map((t,u)=>a?this.executeMapItemWithBestOf(t,s,u,l,e,r,i):o?this.executeMapItemWithVerify(t,s,u,l,e,r,i):i?_(d=>this.executeMapItem(t,s,u,l,e,r,d),i,u):this.executeMapItem(t,s,u,l,e,r)));return L.from(p)}async filter(e){let{items:n,prompt:s,condition:a,verify:o}=e,i=e.retry??this.config.retry,l=this.generateOperationId(),r=e.timeoutMs??this.config.timeoutMs,p=await Promise.all(n.map((u,d)=>o?this.executeFilterItemWithVerify(u,s,d,l,e,r,i):i?_(m=>this.executeFilterItem(u,s,d,l,e,r,m),i,d):this.executeFilterItem(u,s,d,l,e,r))),t=[];for(let u of p)if(u.status==="error")t.push(u);else if(u.data!==null)try{a(u.data)?t.push(u):t.push({...u,status:"filtered"});}catch(d){t.push({...u,status:"error",data:null,error:`Condition function threw: ${d.message}`});}return L.from(t)}async reduce(e){let{items:n,prompt:s,verify:a}=e,o=e.retry??this.config.retry,i=this.generateOperationId(),l=e.timeoutMs??this.config.timeoutMs,r=[],p=[];n.forEach((x,w)=>{r.push(this.getFiles(x)),p.push(this.getIndex(x,w));});let t={};r.forEach((x,w)=>{Object.entries(x).forEach(([C,S])=>{t[`item_${p[w]}/${C}`]=S;});});let u=ue(t),d=R(ft,{fileTree:u}),m=e.systemPrompt?`${d}
198
212
 
199
- ${e.systemPrompt}`:d,f=(x,C,w)=>({operationId:i,operation:"reduce",tag:x.tag,sandboxId:x.sandboxId,swarmName:this.config.tag,operationName:e.name,inputCount:n.length,inputIndices:p,errorRetry:C,verifyRetry:w,...this.pipelineContextToMeta(e._pipelineContext)}),h=e.mcpServers??this.config.mcpServers,g=e.skills??this.config.skills,y=e.composio??this.config.composio,v=async(x,C,w,S)=>{let k=await this.semaphore.use(()=>this.execute(t,x,{systemPrompt:m,schema:e.schema,schemaOptions:e.schemaOptions,agent:e.agent,mcpServers:h,skills:g,composio:y,tagPrefix:C,timeoutMs:l,observability:{swarmName:this.config.tag,operationName:e.name,operationId:i,operation:"reduce",role:"worker",errorRetry:w,verifyRetry:S&&S>1?S-1:void 0,...this.pipelineContextToObservability(e._pipelineContext)}})),F=f(k,w,S&&S>1?S-1:void 0);return k.error?{status:"error",data:null,files:k.files,meta:F,error:k.error,rawData:k.rawData}:{status:"success",data:k.data,files:k.files,meta:F}},b=`${this.config.tag}-reduce`;return a?this.runWithVerification((x,C,w)=>v(x,C,void 0,w),{originalPrompt:s,inputFiles:t,verifyConfig:a,timeoutMs:l,systemPrompt:m,schema:e.schema,mcpServers:h,skills:g,composio:y,operationId:i,baseTag:b,retry:o,operation:"reduce",_pipelineContext:e._pipelineContext}):o?T(x=>{let C=x>1?x-1:void 0,w=C?`${b}-er${C}`:b;return v(s,w,C)},o):v(s,b)}async bestOf(e){let{item:n,prompt:s,config:a}=e,o=e.retry??this.config.retry,i=a.n??a.taskAgents?.length;if(i===void 0)throw new Error("bestOf requires n or taskAgents");if(i<2)throw new Error("bestOf requires n >= 2");let l=this.generateOperationId(),r=e.timeoutMs??this.config.timeoutMs,p=this.getFiles(n),t=a.mcpServers??this.config.mcpServers,u=a.judgeMcpServers??a.mcpServers??this.config.mcpServers,d=a.skills??this.config.skills,m=a.judgeSkills??a.skills??this.config.skills,f=a.composio??this.config.composio,h=a.judgeComposio??a.composio??this.config.composio,g=await Promise.all(Array.from({length:i},async(C,w)=>{let S=o?await T(k=>this.executeBestOfCandidate({inputFiles:p,prompt:s,candidateIndex:w,operationId:l,config:a,systemPrompt:e.systemPrompt,schema:e.schema,schemaOptions:e.schemaOptions,mcpServers:t,skills:d,composio:f,timeoutMs:r,attempt:k}),o,w):await this.executeBestOfCandidate({inputFiles:p,prompt:s,candidateIndex:w,operationId:l,config:a,systemPrompt:e.systemPrompt,schema:e.schema,schemaOptions:e.schemaOptions,mcpServers:t,skills:d,composio:f,timeoutMs:r});return a.onCandidateComplete?.(0,w,S.status==="success"?"success":"error"),S})),y=o?await T(C=>this.executeBestOfJudge({inputFiles:p,taskPrompt:s,candidates:g,config:a,timeoutMs:r,operationId:l,systemPrompt:e.systemPrompt,schema:e.schema,schemaOptions:e.schemaOptions,mcpServers:u,skills:m,composio:h,attempt:C}),{...o,retryOn:void 0},0):await this.executeBestOfJudge({inputFiles:p,taskPrompt:s,candidates:g,config:a,timeoutMs:r,operationId:l,systemPrompt:e.systemPrompt,schema:e.schema,schemaOptions:e.schemaOptions,mcpServers:u,skills:m,composio:h}),v=g.findIndex(C=>C.status==="success"),b=y.decision?.winner??(v>=0?v:0);a.onJudgeComplete?.(0,b,y.decision?.reasoning??"Judge failed to provide reasoning");let x={operationId:l,operation:"bestof-judge",tag:y.tag,sandboxId:y.sandboxId,swarmName:this.config.tag,operationName:e.name,candidateCount:i};return {winner:g[b]??g[0],winnerIndex:b,judgeReasoning:y.decision?.reasoning??"Judge failed to provide reasoning",judgeMeta:x,candidates:g}}async execute(e,n,s){let a=null,o="",i=s.tagPrefix,l={},r=null,p,t,u={...this.config.agent,...s.agent};try{a=new H().withAgent(u).withSandbox(this.config.sandbox).withWorkspaceMode(this.config.workspaceMode).withSessionTagPrefix(s.tagPrefix),s.schema&&a.withSchema(s.schema,s.schemaOptions),s.systemPrompt&&a.withSystemPrompt(s.systemPrompt),s.mcpServers&&a.withMcpServers(s.mcpServers),s.skills?.length&&a.withSkills(s.skills),s.composio&&a.withComposio(s.composio.userId,s.composio.config),s.observability&&a.withObservability(s.observability),Object.keys(e).length>0&&a.withContext(e);let d=await a.run({prompt:n,timeoutMs:s.timeoutMs});o=d.sandboxId,i=a.getSessionTag()??s.tagPrefix;let m=null;try{m=await a.getOutputFiles(!0),l=m.files;}catch{}d.exitCode!==0?p=`Agent exited with code ${d.exitCode}`:s.schema?m?(r=m.data,m.error&&(p=m.error),m.rawData&&(t=m.rawData)):p="Failed to read output files from sandbox":r=l;}catch(d){if(p=d.message,a){i=a.getSessionTag()??s.tagPrefix;try{l=(await a.getOutputFiles(!0)).files;}catch{}}}finally{a&&await a.kill().catch(()=>{});}return {files:l,data:r,tag:i,sandboxId:o,error:p,rawData:t}}async executeMapItem(e,n,s,a,o,i,l=1){let r=this.getFiles(e),p=l>1?`${this.config.tag}-map-${s}-er${l-1}`:`${this.config.tag}-map-${s}`,t=this.evaluatePrompt(n,r,s);if(t instanceof Error)return this.buildErrorResult(`Prompt function threw: ${t.message}`,{operationId:a,operation:"map",tag:p,sandboxId:"",itemIndex:s});let u=o.mcpServers??this.config.mcpServers,d=o.skills??this.config.skills,m=o.composio??this.config.composio,f=await this.semaphore.use(()=>this.execute(r,t,{systemPrompt:o.systemPrompt,schema:o.schema,schemaOptions:o.schemaOptions,agent:o.agent,mcpServers:u,skills:d,composio:m,tagPrefix:p,timeoutMs:i,observability:{swarmName:this.config.tag,operationName:o.name,operationId:a,operation:"map",itemIndex:s,role:"worker",errorRetry:l>1?l-1:void 0,...this.pipelineContextToObservability(o._pipelineContext)}})),h={operationId:a,operation:"map",tag:f.tag,sandboxId:f.sandboxId,swarmName:this.config.tag,operationName:o.name,itemIndex:s,errorRetry:l>1?l-1:void 0,...this.pipelineContextToMeta(o._pipelineContext)};return this.buildResult(f,h)}async executeMapItemWithVerify(e,n,s,a,o,i,l){let r=this.getFiles(e),p=`${this.config.tag}-map-${s}`,t=o.verify,u=o.mcpServers??this.config.mcpServers,d=o.skills??this.config.skills,m=o.composio??this.config.composio,f=this.evaluatePrompt(n,r,s);if(f instanceof Error)return this.buildErrorResult(`Prompt function threw: ${f.message}`,{operationId:a,operation:"map",tag:p,sandboxId:"",itemIndex:s});let h=async(g,y,v)=>{let b=await this.semaphore.use(()=>this.execute(r,g,{systemPrompt:o.systemPrompt,schema:o.schema,schemaOptions:o.schemaOptions,agent:o.agent,mcpServers:u,skills:d,composio:m,tagPrefix:y,timeoutMs:i,observability:{swarmName:this.config.tag,operationName:o.name,operationId:a,operation:"map",itemIndex:s,role:"worker",verifyRetry:v&&v>1?v-1:void 0,...this.pipelineContextToObservability(o._pipelineContext)}})),x={operationId:a,operation:"map",tag:b.tag,sandboxId:b.sandboxId,swarmName:this.config.tag,operationName:o.name,itemIndex:s,verifyRetry:v&&v>1?v-1:void 0,...this.pipelineContextToMeta(o._pipelineContext)};return this.buildResult(b,x)};return this.runWithVerification(h,{originalPrompt:f,inputFiles:r,verifyConfig:t,timeoutMs:i,systemPrompt:o.systemPrompt,schema:o.schema,mcpServers:u,skills:d,composio:m,operationId:a,baseTag:p,retry:l,itemIndex:s,operation:"map",_pipelineContext:o._pipelineContext})}async executeMapItemWithBestOf(e,n,s,a,o,i,l){let r=this.getFiles(e),p=`${this.config.tag}-map-${s}`,t=this.evaluatePrompt(n,r,s);if(t instanceof Error)return this.buildErrorResult(`Prompt function threw: ${t.message}`,{operationId:a,operation:"map",tag:p,sandboxId:"",itemIndex:s});let u=o.bestOf,d=u.n??u.taskAgents?.length;if(d===void 0||d<2)return this.buildErrorResult("bestOf requires n >= 2 or taskAgents with at least 2 elements",{operationId:a,operation:"map",tag:p,sandboxId:"",itemIndex:s});let m=o.mcpServers??this.config.mcpServers,f=u.mcpServers??m,h=u.judgeMcpServers??u.mcpServers??m,g=o.skills??this.config.skills,y=u.skills??g,v=u.judgeSkills??u.skills??g,b=o.composio??this.config.composio,x=u.composio??b,C=u.judgeComposio??u.composio??b,w=await Promise.all(Array.from({length:d},async(q,Z)=>{let Ne=l?await T(St=>this.executeBestOfCandidate({inputFiles:r,prompt:t,candidateIndex:Z,operationId:a,config:u,systemPrompt:o.systemPrompt,schema:o.schema,schemaOptions:o.schemaOptions,mcpServers:f,skills:y,composio:x,timeoutMs:i,parentIndex:s,attempt:St,_pipelineContext:o._pipelineContext}),l,Z):await this.executeBestOfCandidate({inputFiles:r,prompt:t,candidateIndex:Z,operationId:a,config:u,systemPrompt:o.systemPrompt,schema:o.schema,schemaOptions:o.schemaOptions,mcpServers:f,skills:y,composio:x,timeoutMs:i,parentIndex:s,_pipelineContext:o._pipelineContext});return u.onCandidateComplete?.(s,Z,Ne.status==="success"?"success":"error"),Ne})),S=l?await T(q=>this.executeBestOfJudge({inputFiles:r,taskPrompt:t,candidates:w,config:u,timeoutMs:i,operationId:a,systemPrompt:o.systemPrompt,schema:o.schema,schemaOptions:o.schemaOptions,mcpServers:h,skills:v,composio:C,parentIndex:s,attempt:q,_pipelineContext:o._pipelineContext}),{...l,retryOn:void 0},0):await this.executeBestOfJudge({inputFiles:r,taskPrompt:t,candidates:w,config:u,timeoutMs:i,operationId:a,systemPrompt:o.systemPrompt,schema:o.schema,schemaOptions:o.schemaOptions,mcpServers:h,skills:v,composio:C,parentIndex:s,_pipelineContext:o._pipelineContext}),k=w.findIndex(q=>q.status==="success"),F=S.decision?.winner??(k>=0?k:0),Ue=w[F]??w[0];u.onJudgeComplete?.(s,F,S.decision?.reasoning??"Judge failed to provide reasoning");let Ct={operationId:a,operation:"bestof-judge",tag:S.tag,sandboxId:S.sandboxId,swarmName:this.config.tag,operationName:o.name,candidateCount:d,...this.pipelineContextToMeta(o._pipelineContext)};return {...Ue,meta:{...Ue.meta,operation:"map",swarmName:this.config.tag,operationName:o.name,itemIndex:s,...this.pipelineContextToMeta(o._pipelineContext)},bestOf:{winnerIndex:F,judgeReasoning:S.decision?.reasoning??"Judge failed to provide reasoning",judgeMeta:Ct,candidates:w}}}async executeFilterItem(e,n,s,a,o,i,l=1){let r=this.getFiles(e),p=l>1?`${this.config.tag}-filter-${s}-er${l-1}`:`${this.config.tag}-filter-${s}`,t=o.mcpServers??this.config.mcpServers,u=o.skills??this.config.skills,d=o.composio??this.config.composio,m=await this.semaphore.use(()=>this.execute(r,n,{systemPrompt:o.systemPrompt,schema:o.schema,schemaOptions:o.schemaOptions,agent:o.agent,mcpServers:t,skills:u,composio:d,tagPrefix:p,timeoutMs:i,observability:{swarmName:this.config.tag,operationName:o.name,operationId:a,operation:"filter",itemIndex:s,role:"worker",errorRetry:l>1?l-1:void 0,...this.pipelineContextToObservability(o._pipelineContext)}})),f={operationId:a,operation:"filter",tag:m.tag,sandboxId:m.sandboxId,swarmName:this.config.tag,operationName:o.name,itemIndex:s,errorRetry:l>1?l-1:void 0,...this.pipelineContextToMeta(o._pipelineContext)};return this.buildResult(m,f,r)}async executeFilterItemWithVerify(e,n,s,a,o,i,l){let r=this.getFiles(e),p=`${this.config.tag}-filter-${s}`,t=o.verify,u=o.mcpServers??this.config.mcpServers,d=o.skills??this.config.skills,m=o.composio??this.config.composio,f=async(h,g,y)=>{let v=await this.semaphore.use(()=>this.execute(r,h,{systemPrompt:o.systemPrompt,schema:o.schema,schemaOptions:o.schemaOptions,agent:o.agent,mcpServers:u,skills:d,composio:m,tagPrefix:g,timeoutMs:i,observability:{swarmName:this.config.tag,operationName:o.name,operationId:a,operation:"filter",itemIndex:s,role:"worker",verifyRetry:y&&y>1?y-1:void 0,...this.pipelineContextToObservability(o._pipelineContext)}})),b={operationId:a,operation:"filter",tag:v.tag,sandboxId:v.sandboxId,swarmName:this.config.tag,operationName:o.name,itemIndex:s,verifyRetry:y&&y>1?y-1:void 0,...this.pipelineContextToMeta(o._pipelineContext)};return this.buildResult(v,b,r)};return this.runWithVerification(f,{originalPrompt:n,inputFiles:r,verifyConfig:t,timeoutMs:i,systemPrompt:o.systemPrompt,schema:o.schema,mcpServers:u,skills:d,composio:m,operationId:a,baseTag:p,retry:l,itemIndex:s,operation:"filter",_pipelineContext:o._pipelineContext})}async executeBestOfCandidate(e){let{inputFiles:n,prompt:s,candidateIndex:a,operationId:o,config:i,timeoutMs:l,parentIndex:r,attempt:p=1,_pipelineContext:t}=e,u=r!==void 0?`${this.config.tag}-map-${r}-bestof-cand-${a}`:`${this.config.tag}-bestof-cand-${a}`,d=p>1?`${u}-er${p-1}`:u,m=await this.semaphore.use(()=>this.execute(n,s,{systemPrompt:e.systemPrompt,schema:e.schema,schemaOptions:e.schemaOptions,agent:i.taskAgents?.[a],mcpServers:e.mcpServers,skills:e.skills,composio:e.composio,tagPrefix:d,timeoutMs:l,observability:{swarmName:this.config.tag,operationId:o,operation:"map",itemIndex:r,role:"candidate",candidateIndex:a,errorRetry:p>1?p-1:void 0,...this.pipelineContextToObservability(t)}})),f={operationId:o,operation:"bestof-cand",tag:m.tag,sandboxId:m.sandboxId,swarmName:this.config.tag,itemIndex:a,errorRetry:p>1?p-1:void 0,candidateIndex:a,...this.pipelineContextToMeta(t)};return this.buildResult(m,f)}buildJudgeContext(e){let n=this.buildEvaluatorContext({inputFiles:e.inputFiles,taskPrompt:e.taskPrompt,systemPrompt:e.systemPrompt,schema:e.schema});return e.candidates.forEach((s,a)=>{s.status==="error"&&(n[`candidate_${a}/_failed.txt`]=`STATUS: FAILED
213
+ ${e.systemPrompt}`:d,f=(x,w,C)=>({operationId:i,operation:"reduce",tag:x.tag,sandboxId:x.sandboxId,swarmName:this.config.tag,operationName:e.name,inputCount:n.length,inputIndices:p,errorRetry:w,verifyRetry:C,...this.pipelineContextToMeta(e._pipelineContext)}),h=e.mcpServers??this.config.mcpServers,g=e.skills??this.config.skills,y=e.composio??this.config.composio,v=async(x,w,C,S)=>{let k=await this.semaphore.use(()=>this.execute(t,x,{systemPrompt:m,schema:e.schema,schemaOptions:e.schemaOptions,agent:e.agent,mcpServers:h,skills:g,composio:y,tagPrefix:w,timeoutMs:l,observability:{swarmName:this.config.tag,operationName:e.name,operationId:i,operation:"reduce",role:"worker",errorRetry:C,verifyRetry:S&&S>1?S-1:void 0,...this.pipelineContextToObservability(e._pipelineContext)}})),F=f(k,C,S&&S>1?S-1:void 0);return k.error?{status:"error",data:null,files:k.files,meta:F,error:k.error,rawData:k.rawData}:{status:"success",data:k.data,files:k.files,meta:F}},b=`${this.config.tag}-reduce`;return a?this.runWithVerification((x,w,C)=>v(x,w,void 0,C),{originalPrompt:s,inputFiles:t,verifyConfig:a,timeoutMs:l,systemPrompt:m,schema:e.schema,mcpServers:h,skills:g,composio:y,operationId:i,baseTag:b,retry:o,operation:"reduce",_pipelineContext:e._pipelineContext}):o?_(x=>{let w=x>1?x-1:void 0,C=w?`${b}-er${w}`:b;return v(s,C,w)},o):v(s,b)}async bestOf(e){let{item:n,prompt:s,config:a}=e,o=e.retry??this.config.retry,i=a.n??a.taskAgents?.length;if(i===void 0)throw new Error("bestOf requires n or taskAgents");if(i<2)throw new Error("bestOf requires n >= 2");let l=this.generateOperationId(),r=e.timeoutMs??this.config.timeoutMs,p=this.getFiles(n),t=a.mcpServers??this.config.mcpServers,u=a.judgeMcpServers??a.mcpServers??this.config.mcpServers,d=a.skills??this.config.skills,m=a.judgeSkills??a.skills??this.config.skills,f=a.composio??this.config.composio,h=a.judgeComposio??a.composio??this.config.composio,g=await Promise.all(Array.from({length:i},async(w,C)=>{let S=o?await _(k=>this.executeBestOfCandidate({inputFiles:p,prompt:s,candidateIndex:C,operationId:l,config:a,systemPrompt:e.systemPrompt,schema:e.schema,schemaOptions:e.schemaOptions,mcpServers:t,skills:d,composio:f,timeoutMs:r,attempt:k}),o,C):await this.executeBestOfCandidate({inputFiles:p,prompt:s,candidateIndex:C,operationId:l,config:a,systemPrompt:e.systemPrompt,schema:e.schema,schemaOptions:e.schemaOptions,mcpServers:t,skills:d,composio:f,timeoutMs:r});return a.onCandidateComplete?.(0,C,S.status==="success"?"success":"error"),S})),y=o?await _(w=>this.executeBestOfJudge({inputFiles:p,taskPrompt:s,candidates:g,config:a,timeoutMs:r,operationId:l,systemPrompt:e.systemPrompt,schema:e.schema,schemaOptions:e.schemaOptions,mcpServers:u,skills:m,composio:h,attempt:w}),{...o,retryOn:void 0},0):await this.executeBestOfJudge({inputFiles:p,taskPrompt:s,candidates:g,config:a,timeoutMs:r,operationId:l,systemPrompt:e.systemPrompt,schema:e.schema,schemaOptions:e.schemaOptions,mcpServers:u,skills:m,composio:h}),v=g.findIndex(w=>w.status==="success"),b=y.decision?.winner??(v>=0?v:0);a.onJudgeComplete?.(0,b,y.decision?.reasoning??"Judge failed to provide reasoning");let x={operationId:l,operation:"bestof-judge",tag:y.tag,sandboxId:y.sandboxId,swarmName:this.config.tag,operationName:e.name,candidateCount:i};return {winner:g[b]??g[0],winnerIndex:b,judgeReasoning:y.decision?.reasoning??"Judge failed to provide reasoning",judgeMeta:x,candidates:g}}async execute(e,n,s){let a=null,o="",i=s.tagPrefix,l={},r=null,p,t,u={...this.config.agent,...s.agent};try{a=new H().withAgent(u).withSandbox(this.config.sandbox).withWorkspaceMode(this.config.workspaceMode).withSessionTagPrefix(s.tagPrefix),s.schema&&a.withSchema(s.schema,s.schemaOptions),s.systemPrompt&&a.withSystemPrompt(s.systemPrompt),s.mcpServers&&a.withMcpServers(s.mcpServers),s.skills?.length&&a.withSkills(s.skills),s.composio&&a.withComposio(s.composio.userId,s.composio.config),s.observability&&a.withObservability(s.observability),Object.keys(e).length>0&&a.withContext(e);let d=await a.run({prompt:n,timeoutMs:s.timeoutMs});o=d.sandboxId,i=a.getSessionTag()??s.tagPrefix;let m=null;try{m=await a.getOutputFiles(!0),l=m.files;}catch{}d.exitCode!==0?p=`Agent exited with code ${d.exitCode}`:s.schema?m?(r=m.data,m.error&&(p=m.error),m.rawData&&(t=m.rawData)):p="Failed to read output files from sandbox":r=l;}catch(d){if(p=d.message,a){i=a.getSessionTag()??s.tagPrefix;try{l=(await a.getOutputFiles(!0)).files;}catch{}}}finally{a&&await a.kill().catch(()=>{});}return {files:l,data:r,tag:i,sandboxId:o,error:p,rawData:t}}async executeMapItem(e,n,s,a,o,i,l=1){let r=this.getFiles(e),p=l>1?`${this.config.tag}-map-${s}-er${l-1}`:`${this.config.tag}-map-${s}`,t=this.evaluatePrompt(n,r,s);if(t instanceof Error)return this.buildErrorResult(`Prompt function threw: ${t.message}`,{operationId:a,operation:"map",tag:p,sandboxId:"",itemIndex:s});let u=o.mcpServers??this.config.mcpServers,d=o.skills??this.config.skills,m=o.composio??this.config.composio,f=await this.semaphore.use(()=>this.execute(r,t,{systemPrompt:o.systemPrompt,schema:o.schema,schemaOptions:o.schemaOptions,agent:o.agent,mcpServers:u,skills:d,composio:m,tagPrefix:p,timeoutMs:i,observability:{swarmName:this.config.tag,operationName:o.name,operationId:a,operation:"map",itemIndex:s,role:"worker",errorRetry:l>1?l-1:void 0,...this.pipelineContextToObservability(o._pipelineContext)}})),h={operationId:a,operation:"map",tag:f.tag,sandboxId:f.sandboxId,swarmName:this.config.tag,operationName:o.name,itemIndex:s,errorRetry:l>1?l-1:void 0,...this.pipelineContextToMeta(o._pipelineContext)};return this.buildResult(f,h)}async executeMapItemWithVerify(e,n,s,a,o,i,l){let r=this.getFiles(e),p=`${this.config.tag}-map-${s}`,t=o.verify,u=o.mcpServers??this.config.mcpServers,d=o.skills??this.config.skills,m=o.composio??this.config.composio,f=this.evaluatePrompt(n,r,s);if(f instanceof Error)return this.buildErrorResult(`Prompt function threw: ${f.message}`,{operationId:a,operation:"map",tag:p,sandboxId:"",itemIndex:s});let h=async(g,y,v)=>{let b=await this.semaphore.use(()=>this.execute(r,g,{systemPrompt:o.systemPrompt,schema:o.schema,schemaOptions:o.schemaOptions,agent:o.agent,mcpServers:u,skills:d,composio:m,tagPrefix:y,timeoutMs:i,observability:{swarmName:this.config.tag,operationName:o.name,operationId:a,operation:"map",itemIndex:s,role:"worker",verifyRetry:v&&v>1?v-1:void 0,...this.pipelineContextToObservability(o._pipelineContext)}})),x={operationId:a,operation:"map",tag:b.tag,sandboxId:b.sandboxId,swarmName:this.config.tag,operationName:o.name,itemIndex:s,verifyRetry:v&&v>1?v-1:void 0,...this.pipelineContextToMeta(o._pipelineContext)};return this.buildResult(b,x)};return this.runWithVerification(h,{originalPrompt:f,inputFiles:r,verifyConfig:t,timeoutMs:i,systemPrompt:o.systemPrompt,schema:o.schema,mcpServers:u,skills:d,composio:m,operationId:a,baseTag:p,retry:l,itemIndex:s,operation:"map",_pipelineContext:o._pipelineContext})}async executeMapItemWithBestOf(e,n,s,a,o,i,l){let r=this.getFiles(e),p=`${this.config.tag}-map-${s}`,t=this.evaluatePrompt(n,r,s);if(t instanceof Error)return this.buildErrorResult(`Prompt function threw: ${t.message}`,{operationId:a,operation:"map",tag:p,sandboxId:"",itemIndex:s});let u=o.bestOf,d=u.n??u.taskAgents?.length;if(d===void 0||d<2)return this.buildErrorResult("bestOf requires n >= 2 or taskAgents with at least 2 elements",{operationId:a,operation:"map",tag:p,sandboxId:"",itemIndex:s});let m=o.mcpServers??this.config.mcpServers,f=u.mcpServers??m,h=u.judgeMcpServers??u.mcpServers??m,g=o.skills??this.config.skills,y=u.skills??g,v=u.judgeSkills??u.skills??g,b=o.composio??this.config.composio,x=u.composio??b,w=u.judgeComposio??u.composio??b,C=await Promise.all(Array.from({length:d},async(q,Z)=>{let Ne=l?await _(St=>this.executeBestOfCandidate({inputFiles:r,prompt:t,candidateIndex:Z,operationId:a,config:u,systemPrompt:o.systemPrompt,schema:o.schema,schemaOptions:o.schemaOptions,mcpServers:f,skills:y,composio:x,timeoutMs:i,parentIndex:s,attempt:St,_pipelineContext:o._pipelineContext}),l,Z):await this.executeBestOfCandidate({inputFiles:r,prompt:t,candidateIndex:Z,operationId:a,config:u,systemPrompt:o.systemPrompt,schema:o.schema,schemaOptions:o.schemaOptions,mcpServers:f,skills:y,composio:x,timeoutMs:i,parentIndex:s,_pipelineContext:o._pipelineContext});return u.onCandidateComplete?.(s,Z,Ne.status==="success"?"success":"error"),Ne})),S=l?await _(q=>this.executeBestOfJudge({inputFiles:r,taskPrompt:t,candidates:C,config:u,timeoutMs:i,operationId:a,systemPrompt:o.systemPrompt,schema:o.schema,schemaOptions:o.schemaOptions,mcpServers:h,skills:v,composio:w,parentIndex:s,attempt:q,_pipelineContext:o._pipelineContext}),{...l,retryOn:void 0},0):await this.executeBestOfJudge({inputFiles:r,taskPrompt:t,candidates:C,config:u,timeoutMs:i,operationId:a,systemPrompt:o.systemPrompt,schema:o.schema,schemaOptions:o.schemaOptions,mcpServers:h,skills:v,composio:w,parentIndex:s,_pipelineContext:o._pipelineContext}),k=C.findIndex(q=>q.status==="success"),F=S.decision?.winner??(k>=0?k:0),Ue=C[F]??C[0];u.onJudgeComplete?.(s,F,S.decision?.reasoning??"Judge failed to provide reasoning");let wt={operationId:a,operation:"bestof-judge",tag:S.tag,sandboxId:S.sandboxId,swarmName:this.config.tag,operationName:o.name,candidateCount:d,...this.pipelineContextToMeta(o._pipelineContext)};return {...Ue,meta:{...Ue.meta,operation:"map",swarmName:this.config.tag,operationName:o.name,itemIndex:s,...this.pipelineContextToMeta(o._pipelineContext)},bestOf:{winnerIndex:F,judgeReasoning:S.decision?.reasoning??"Judge failed to provide reasoning",judgeMeta:wt,candidates:C}}}async executeFilterItem(e,n,s,a,o,i,l=1){let r=this.getFiles(e),p=l>1?`${this.config.tag}-filter-${s}-er${l-1}`:`${this.config.tag}-filter-${s}`,t=o.mcpServers??this.config.mcpServers,u=o.skills??this.config.skills,d=o.composio??this.config.composio,m=await this.semaphore.use(()=>this.execute(r,n,{systemPrompt:o.systemPrompt,schema:o.schema,schemaOptions:o.schemaOptions,agent:o.agent,mcpServers:t,skills:u,composio:d,tagPrefix:p,timeoutMs:i,observability:{swarmName:this.config.tag,operationName:o.name,operationId:a,operation:"filter",itemIndex:s,role:"worker",errorRetry:l>1?l-1:void 0,...this.pipelineContextToObservability(o._pipelineContext)}})),f={operationId:a,operation:"filter",tag:m.tag,sandboxId:m.sandboxId,swarmName:this.config.tag,operationName:o.name,itemIndex:s,errorRetry:l>1?l-1:void 0,...this.pipelineContextToMeta(o._pipelineContext)};return this.buildResult(m,f,r)}async executeFilterItemWithVerify(e,n,s,a,o,i,l){let r=this.getFiles(e),p=`${this.config.tag}-filter-${s}`,t=o.verify,u=o.mcpServers??this.config.mcpServers,d=o.skills??this.config.skills,m=o.composio??this.config.composio,f=async(h,g,y)=>{let v=await this.semaphore.use(()=>this.execute(r,h,{systemPrompt:o.systemPrompt,schema:o.schema,schemaOptions:o.schemaOptions,agent:o.agent,mcpServers:u,skills:d,composio:m,tagPrefix:g,timeoutMs:i,observability:{swarmName:this.config.tag,operationName:o.name,operationId:a,operation:"filter",itemIndex:s,role:"worker",verifyRetry:y&&y>1?y-1:void 0,...this.pipelineContextToObservability(o._pipelineContext)}})),b={operationId:a,operation:"filter",tag:v.tag,sandboxId:v.sandboxId,swarmName:this.config.tag,operationName:o.name,itemIndex:s,verifyRetry:y&&y>1?y-1:void 0,...this.pipelineContextToMeta(o._pipelineContext)};return this.buildResult(v,b,r)};return this.runWithVerification(f,{originalPrompt:n,inputFiles:r,verifyConfig:t,timeoutMs:i,systemPrompt:o.systemPrompt,schema:o.schema,mcpServers:u,skills:d,composio:m,operationId:a,baseTag:p,retry:l,itemIndex:s,operation:"filter",_pipelineContext:o._pipelineContext})}async executeBestOfCandidate(e){let{inputFiles:n,prompt:s,candidateIndex:a,operationId:o,config:i,timeoutMs:l,parentIndex:r,attempt:p=1,_pipelineContext:t}=e,u=r!==void 0?`${this.config.tag}-map-${r}-bestof-cand-${a}`:`${this.config.tag}-bestof-cand-${a}`,d=p>1?`${u}-er${p-1}`:u,m=await this.semaphore.use(()=>this.execute(n,s,{systemPrompt:e.systemPrompt,schema:e.schema,schemaOptions:e.schemaOptions,agent:i.taskAgents?.[a],mcpServers:e.mcpServers,skills:e.skills,composio:e.composio,tagPrefix:d,timeoutMs:l,observability:{swarmName:this.config.tag,operationId:o,operation:"map",itemIndex:r,role:"candidate",candidateIndex:a,errorRetry:p>1?p-1:void 0,...this.pipelineContextToObservability(t)}})),f={operationId:o,operation:"bestof-cand",tag:m.tag,sandboxId:m.sandboxId,swarmName:this.config.tag,itemIndex:a,errorRetry:p>1?p-1:void 0,candidateIndex:a,...this.pipelineContextToMeta(t)};return this.buildResult(m,f)}buildJudgeContext(e){let n=this.buildEvaluatorContext({inputFiles:e.inputFiles,taskPrompt:e.taskPrompt,systemPrompt:e.systemPrompt,schema:e.schema});return e.candidates.forEach((s,a)=>{s.status==="error"&&(n[`candidate_${a}/_failed.txt`]=`STATUS: FAILED
200
214
 
201
- Error: ${s.error??"Unknown error"}`),Object.entries(s.files).forEach(([o,i])=>{n[`candidate_${a}/${o}`]=i;});}),n}async executeBestOfJudge(e){let{candidates:n,config:s,timeoutMs:a,operationId:o,parentIndex:i,attempt:l=1,_pipelineContext:r}=e,p=i!==void 0?`${this.config.tag}-map-${i}-bestof-judge`:`${this.config.tag}-bestof-judge`,t=l>1?`${p}-er${l-1}`:p,u=this.buildJudgeContext({inputFiles:e.inputFiles,taskPrompt:e.taskPrompt,candidates:n,systemPrompt:e.systemPrompt,schema:e.schema}),d=ue(u),m=R(Pe,{candidateCount:String(n.length),criteria:s.judgeCriteria,fileTree:d}),f=z$1.object({winner:z$1.number().min(0).max(n.length-1),reasoning:z$1.string()}),h=await this.semaphore.use(()=>this.execute(u,dt,{systemPrompt:m,schema:f,agent:s.judgeAgent,mcpServers:e.mcpServers,skills:e.skills,composio:e.composio,tagPrefix:t,timeoutMs:a,observability:{swarmName:this.config.tag,operationId:o,operation:"map",itemIndex:i,role:"judge",errorRetry:l>1?l-1:void 0,...this.pipelineContextToObservability(r)}})),g=null;if(h.data&&!h.error)g=h.data;else if(h.rawData)try{let y=JSON.parse(h.rawData);console.warn(`Judge returned invalid winner ${y.winner}, defaulting to candidate 0`),g={winner:0,reasoning:y.reasoning??"Judge failed to provide reasoning"};}catch{console.warn(`Judge validation failed: ${h.error}`);}return {status:g?"success":"error",decision:g,tag:h.tag,sandboxId:h.sandboxId,error:g?void 0:"Judge failed to produce valid decision"}}static DEFAULT_VERIFY_MAX_ATTEMPTS=3;static VerifyDecisionSchema=z$1.object({passed:z$1.boolean(),reasoning:z$1.string(),feedback:z$1.string().optional()});buildVerifyContext(e){let n=this.buildEvaluatorContext({inputFiles:e.inputFiles,taskPrompt:e.taskPrompt,systemPrompt:e.systemPrompt,schema:e.schema});return Object.entries(e.outputFiles).forEach(([s,a])=>{n[`worker_output/${s}`]=a;}),n}async executeVerify(e){let{config:n,timeoutMs:s,operationId:a,workerTag:o,retryAttempt:i=1,operation:l,itemIndex:r,attemptIndex:p,_pipelineContext:t}=e,u=i>1?`${o}-verifier-er${i-1}`:`${o}-verifier`,d=n.verifierMcpServers??e.mcpServers,m=n.verifierSkills??e.skills,f=n.verifierComposio??e.composio,h=this.buildVerifyContext({inputFiles:e.inputFiles,taskPrompt:e.taskPrompt,outputFiles:e.outputFiles,systemPrompt:e.systemPrompt,schema:e.schema}),g=ue(h),y=R(Re,{criteria:n.criteria,fileTree:g}),v=await this.semaphore.use(()=>this.execute(h,mt,{systemPrompt:y,schema:c.VerifyDecisionSchema,agent:n.verifierAgent,mcpServers:d,skills:m,composio:f,tagPrefix:u,timeoutMs:s,observability:{swarmName:this.config.tag,operationId:a,operation:l,itemIndex:r,role:"verifier",verifyRetry:p&&p>1?p-1:void 0,errorRetry:i>1?i-1:void 0,...this.pipelineContextToObservability(t)}})),b=null;if(v.data&&!v.error)b=v.data;else if(v.rawData)try{let x=JSON.parse(v.rawData);b={passed:!!x.passed,reasoning:x.reasoning??"Verification completed",feedback:x.feedback};}catch{console.warn(`Verify validation failed: ${v.error}`);}return {status:b?"success":"error",decision:b,tag:v.tag,sandboxId:v.sandboxId,error:b?void 0:"Verifier failed to produce valid decision"}}static buildRetryPromptWithFeedback(e,n){return R(Me,{originalPrompt:e,feedback:n})}async runWithVerification(e,n){let{originalPrompt:s,inputFiles:a,verifyConfig:o,timeoutMs:i,operationId:l,baseTag:r,retry:p,itemIndex:t=0,operation:u,_pipelineContext:d}=n,m=o.maxAttempts??c.DEFAULT_VERIFY_MAX_ATTEMPTS,f=s,h=null,g=0;for(;g<m;){g++;let v=g>1?`${r}-vr${g-1}`:r,b=p?await T(w=>e(f,w>1?`${v}-er${w-1}`:v,g),p,t):await e(f,v,g);if(o.onWorkerComplete?.(t,g,b.status==="error"?"error":"success"),b.status==="error")return b;h=b;let x=p?await T(w=>this.executeVerify({inputFiles:a,outputFiles:b.files,taskPrompt:f,config:o,timeoutMs:i,systemPrompt:n.systemPrompt,schema:n.schema,mcpServers:n.mcpServers,skills:n.skills,composio:n.composio,operationId:l,workerTag:v,retryAttempt:w,operation:u,itemIndex:t,attemptIndex:g,_pipelineContext:d}),{...p,retryOn:void 0}):await this.executeVerify({inputFiles:a,outputFiles:b.files,taskPrompt:f,config:o,timeoutMs:i,systemPrompt:n.systemPrompt,schema:n.schema,mcpServers:n.mcpServers,skills:n.skills,composio:n.composio,operationId:l,workerTag:v,operation:u,itemIndex:t,attemptIndex:g,_pipelineContext:d}),C={operationId:l,operation:"verify",tag:x.tag,sandboxId:x.sandboxId,swarmName:this.config.tag,attempts:g,...this.pipelineContextToMeta(d)};if(o.onVerifierComplete?.(t,g,x.decision?.passed??false,x.decision?.feedback),x.decision?.passed)return {...b,verify:{passed:true,reasoning:x.decision.reasoning,verifyMeta:C,attempts:g}};if(g<m){let w=x.decision?.feedback??x.decision?.reasoning??"Output did not meet criteria";f=c.buildRetryPromptWithFeedback(s,w);}}let y=g>1?`${r}-vr${g-1}`:r;return {...h,status:"error",verify:{passed:false,reasoning:"Max verification retries exceeded",verifyMeta:{operationId:l,operation:"verify",tag:`${y}-verifier`,sandboxId:"",swarmName:this.config.tag,attempts:g,...this.pipelineContextToMeta(d)},attempts:g}}}generateOperationId(){return randomBytes(8).toString("hex")}pipelineContextToObservability(e){return e?{pipelineRunId:e.pipelineRunId,pipelineStepIndex:e.pipelineStepIndex}:{}}pipelineContextToMeta(e){return {pipelineRunId:e?.pipelineRunId,pipelineStepIndex:e?.pipelineStepIndex}}evaluatePrompt(e,n,s){if(typeof e=="string")return e;try{return e(n,s)}catch(a){return a}}buildEvaluatorContext(e){let n={};return e.systemPrompt&&(n["worker_task/system_prompt.txt"]=e.systemPrompt),n["worker_task/user_prompt.txt"]=e.taskPrompt,e.schema&&(n["worker_task/schema.json"]=E(e.schema)?N(e.schema):B(e.schema)),Object.entries(e.inputFiles).forEach(([s,a])=>{n[`worker_task/input/${s}`]=a;}),n}isSwarmResult(e){return typeof e=="object"&&e!==null&&A in e&&e[A]===true}getFiles(e){if(this.isSwarmResult(e)){let n={...e.files};return n["result.json"]&&(n["data.json"]=n["result.json"],delete n["result.json"]),n}return e}getIndex(e,n){return this.isSwarmResult(e)?e.meta.itemIndex:n}buildResult(e,n,s){let a=s??e.files;return e.error?{[A]:true,status:"error",data:null,files:a,meta:n,error:e.error,rawData:e.rawData}:{[A]:true,status:"success",data:e.data,files:a,meta:n}}buildErrorResult(e,n){return {[A]:true,status:"error",data:null,files:{},meta:n,error:e}}};var fe=class c{swarm;steps;events;constructor(e,n=[],s={}){this.swarm=e,this.steps=n,this.events=s;}map(e){return new c(this.swarm,[...this.steps,{type:"map",config:e}],this.events)}filter(e){return new c(this.swarm,[...this.steps,{type:"filter",config:e}],this.events)}reduce(e){return new ge(this.swarm,[...this.steps,{type:"reduce",config:e}],this.events)}on(e,n){if(typeof e=="string"){let s=`on${e.charAt(0).toUpperCase()}${e.slice(1)}`;return new c(this.swarm,this.steps,{...this.events,[s]:n})}return new c(this.swarm,this.steps,{...this.events,...e})}async run(e){let n=randomBytes(8).toString("hex"),s=[],a=e,o=Date.now();for(let l=0;l<this.steps.length;l++){let r=this.steps[l],p=r.config.name,t=Date.now();this.events.onStepStart?.({type:r.type,index:l,name:p,itemCount:a.length});let u={pipelineRunId:n,pipelineStepIndex:l};try{let d=await this.executeStep(r,a,l,p,u),m=Date.now()-t;if(s.push({type:r.type,index:l,durationMs:m,results:d.output}),this.events.onStepComplete?.({type:r.type,index:l,name:p,durationMs:m,successCount:d.successCount,errorCount:d.errorCount,filteredCount:d.filteredCount}),r.type==="reduce")return {pipelineRunId:n,steps:s,output:d.output,totalDurationMs:Date.now()-o};a=d.nextItems;}catch(d){throw this.events.onStepError?.({type:r.type,index:l,name:p,error:d instanceof Error?d:new Error(String(d))}),d}}let i=s[s.length-1];return {pipelineRunId:n,steps:s,output:i?.results??[],totalDurationMs:Date.now()-o}}async executeStep(e,n,s,a,o){if(e.type==="map"){let r=e.config,p=await this.swarm.map({items:n,...r,_pipelineContext:o,retry:this.wrapRetry(r.retry,s,a),verify:this.wrapVerify(r.verify,s,a),bestOf:this.wrapBestOf(r.bestOf,s,a)});return {output:[...p],nextItems:p.success,successCount:p.success.length,errorCount:p.error.length,filteredCount:0}}if(e.type==="filter"){let r=e.config,p=await this.swarm.filter({items:n,...r,_pipelineContext:o,retry:this.wrapRetry(r.retry,s,a),verify:this.wrapVerify(r.verify,s,a)}),t=r.emit??"success",u=t==="success"?p.success:t==="filtered"?p.filtered:[...p.success,...p.filtered];return {output:[...p],nextItems:u,successCount:p.success.length,errorCount:p.error.length,filteredCount:p.filtered.length}}let i=e.config,l=await this.swarm.reduce({items:n,...i,_pipelineContext:o,retry:this.wrapRetry(i.retry,s,a),verify:this.wrapVerify(i.verify,s,a)});return {output:l,nextItems:[],successCount:l.status==="success"?1:0,errorCount:l.status==="error"?1:0,filteredCount:0}}wrapRetry(e,n,s){if(e)return {...e,onItemRetry:(a,o,i)=>{e.onItemRetry?.(a,o,i),this.events.onItemRetry?.({stepIndex:n,stepName:s,itemIndex:a,attempt:o,error:i});}}}wrapVerify(e,n,s){if(e)return {...e,onWorkerComplete:(a,o,i)=>{e.onWorkerComplete?.(a,o,i),this.events.onWorkerComplete?.({stepIndex:n,stepName:s,itemIndex:a,attempt:o,status:i});},onVerifierComplete:(a,o,i,l)=>{e.onVerifierComplete?.(a,o,i,l),this.events.onVerifierComplete?.({stepIndex:n,stepName:s,itemIndex:a,attempt:o,passed:i,feedback:l});}}}wrapBestOf(e,n,s){if(e)return {...e,onCandidateComplete:(a,o,i)=>{e.onCandidateComplete?.(a,o,i),this.events.onCandidateComplete?.({stepIndex:n,stepName:s,itemIndex:a,candidateIndex:o,status:i});},onJudgeComplete:(a,o,i)=>{e.onJudgeComplete?.(a,o,i),this.events.onJudgeComplete?.({stepIndex:n,stepName:s,itemIndex:a,winnerIndex:o,reasoning:i});}}}},ge=class c extends fe{constructor(e,n,s){super(e,n,s);}on(e,n){if(typeof e=="string"){let s=`on${e.charAt(0).toUpperCase()}${e.slice(1)}`;return new c(this.swarm,this.steps,{...this.events,[s]:n})}return new c(this.swarm,this.steps,{...this.events,...e})}map(){throw new Error("Cannot add steps after reduce")}filter(){throw new Error("Cannot add steps after reduce")}reduce(){throw new Error("Cannot add steps after reduce")}};export{ye as AGENT_REGISTRY,kt as AGENT_TYPES,z as Agent,H as Evolve,Pe as JUDGE_PROMPT,fe as Pipeline,Me as RETRY_FEEDBACK_PROMPT,ut as SCHEMA_PROMPT,A as SWARM_RESULT_BRAND,pt as SYSTEM_PROMPT,L as Semaphore,$e as Swarm,j as SwarmResultList,ge as TerminalPipeline,he as VALIDATION_PRESETS,Re as VERIFY_PROMPT,ct as WORKSPACE_PROMPT,lt as WORKSPACE_SWE_PROMPT,R as applyTemplate,Oe as buildWorkerSystemPrompt,O as createAgentParser,ne as createClaudeParser,se as createCodexParser,oe as createGeminiParser,T as executeWithRetry,ve as expandPath,M as getAgentConfig,U as getMcpSettingsDir,$ as getMcpSettingsPath,Tt as isValidAgentType,E as isZodSchema,B as jsonSchemaToString,Ot as parseNdjsonLine,It as parseNdjsonOutput,Le as parseQwenOutput,qe as readLocalDir,Ze as saveLocalDir,Q as writeClaudeMcpConfig,te as writeCodexMcpConfig,X as writeGeminiMcpConfig,Ce as writeMcpConfig,ee as writeQwenMcpConfig,N as zodSchemaToJson};
215
+ Error: ${s.error??"Unknown error"}`),Object.entries(s.files).forEach(([o,i])=>{n[`candidate_${a}/${o}`]=i;});}),n}async executeBestOfJudge(e){let{candidates:n,config:s,timeoutMs:a,operationId:o,parentIndex:i,attempt:l=1,_pipelineContext:r}=e,p=i!==void 0?`${this.config.tag}-map-${i}-bestof-judge`:`${this.config.tag}-bestof-judge`,t=l>1?`${p}-er${l-1}`:p,u=this.buildJudgeContext({inputFiles:e.inputFiles,taskPrompt:e.taskPrompt,candidates:n,systemPrompt:e.systemPrompt,schema:e.schema}),d=ue(u),m=R(Ee,{candidateCount:String(n.length),criteria:s.judgeCriteria,fileTree:d}),f=z$1.object({winner:z$1.number().min(0).max(n.length-1),reasoning:z$1.string()}),h=await this.semaphore.use(()=>this.execute(u,dt,{systemPrompt:m,schema:f,agent:s.judgeAgent,mcpServers:e.mcpServers,skills:e.skills,composio:e.composio,tagPrefix:t,timeoutMs:a,observability:{swarmName:this.config.tag,operationId:o,operation:"map",itemIndex:i,role:"judge",errorRetry:l>1?l-1:void 0,...this.pipelineContextToObservability(r)}})),g=null;if(h.data&&!h.error)g=h.data;else if(h.rawData)try{let y=JSON.parse(h.rawData);console.warn(`Judge returned invalid winner ${y.winner}, defaulting to candidate 0`),g={winner:0,reasoning:y.reasoning??"Judge failed to provide reasoning"};}catch{console.warn(`Judge validation failed: ${h.error}`);}return {status:g?"success":"error",decision:g,tag:h.tag,sandboxId:h.sandboxId,error:g?void 0:"Judge failed to produce valid decision"}}static DEFAULT_VERIFY_MAX_ATTEMPTS=3;static VerifyDecisionSchema=z$1.object({passed:z$1.boolean(),reasoning:z$1.string(),feedback:z$1.string().optional()});buildVerifyContext(e){let n=this.buildEvaluatorContext({inputFiles:e.inputFiles,taskPrompt:e.taskPrompt,systemPrompt:e.systemPrompt,schema:e.schema});return Object.entries(e.outputFiles).forEach(([s,a])=>{n[`worker_output/${s}`]=a;}),n}async executeVerify(e){let{config:n,timeoutMs:s,operationId:a,workerTag:o,retryAttempt:i=1,operation:l,itemIndex:r,attemptIndex:p,_pipelineContext:t}=e,u=i>1?`${o}-verifier-er${i-1}`:`${o}-verifier`,d=n.verifierMcpServers??e.mcpServers,m=n.verifierSkills??e.skills,f=n.verifierComposio??e.composio,h=this.buildVerifyContext({inputFiles:e.inputFiles,taskPrompt:e.taskPrompt,outputFiles:e.outputFiles,systemPrompt:e.systemPrompt,schema:e.schema}),g=ue(h),y=R(Re,{criteria:n.criteria,fileTree:g}),v=await this.semaphore.use(()=>this.execute(h,mt,{systemPrompt:y,schema:c.VerifyDecisionSchema,agent:n.verifierAgent,mcpServers:d,skills:m,composio:f,tagPrefix:u,timeoutMs:s,observability:{swarmName:this.config.tag,operationId:a,operation:l,itemIndex:r,role:"verifier",verifyRetry:p&&p>1?p-1:void 0,errorRetry:i>1?i-1:void 0,...this.pipelineContextToObservability(t)}})),b=null;if(v.data&&!v.error)b=v.data;else if(v.rawData)try{let x=JSON.parse(v.rawData);b={passed:!!x.passed,reasoning:x.reasoning??"Verification completed",feedback:x.feedback};}catch{console.warn(`Verify validation failed: ${v.error}`);}return {status:b?"success":"error",decision:b,tag:v.tag,sandboxId:v.sandboxId,error:b?void 0:"Verifier failed to produce valid decision"}}static buildRetryPromptWithFeedback(e,n){return R(Me,{originalPrompt:e,feedback:n})}async runWithVerification(e,n){let{originalPrompt:s,inputFiles:a,verifyConfig:o,timeoutMs:i,operationId:l,baseTag:r,retry:p,itemIndex:t=0,operation:u,_pipelineContext:d}=n,m=o.maxAttempts??c.DEFAULT_VERIFY_MAX_ATTEMPTS,f=s,h=null,g=0;for(;g<m;){g++;let v=g>1?`${r}-vr${g-1}`:r,b=p?await _(C=>e(f,C>1?`${v}-er${C-1}`:v,g),p,t):await e(f,v,g);if(o.onWorkerComplete?.(t,g,b.status==="error"?"error":"success"),b.status==="error")return b;h=b;let x=p?await _(C=>this.executeVerify({inputFiles:a,outputFiles:b.files,taskPrompt:f,config:o,timeoutMs:i,systemPrompt:n.systemPrompt,schema:n.schema,mcpServers:n.mcpServers,skills:n.skills,composio:n.composio,operationId:l,workerTag:v,retryAttempt:C,operation:u,itemIndex:t,attemptIndex:g,_pipelineContext:d}),{...p,retryOn:void 0}):await this.executeVerify({inputFiles:a,outputFiles:b.files,taskPrompt:f,config:o,timeoutMs:i,systemPrompt:n.systemPrompt,schema:n.schema,mcpServers:n.mcpServers,skills:n.skills,composio:n.composio,operationId:l,workerTag:v,operation:u,itemIndex:t,attemptIndex:g,_pipelineContext:d}),w={operationId:l,operation:"verify",tag:x.tag,sandboxId:x.sandboxId,swarmName:this.config.tag,attempts:g,...this.pipelineContextToMeta(d)};if(o.onVerifierComplete?.(t,g,x.decision?.passed??false,x.decision?.feedback),x.decision?.passed)return {...b,verify:{passed:true,reasoning:x.decision.reasoning,verifyMeta:w,attempts:g}};if(g<m){let C=x.decision?.feedback??x.decision?.reasoning??"Output did not meet criteria";f=c.buildRetryPromptWithFeedback(s,C);}}let y=g>1?`${r}-vr${g-1}`:r;return {...h,status:"error",verify:{passed:false,reasoning:"Max verification retries exceeded",verifyMeta:{operationId:l,operation:"verify",tag:`${y}-verifier`,sandboxId:"",swarmName:this.config.tag,attempts:g,...this.pipelineContextToMeta(d)},attempts:g}}}generateOperationId(){return randomBytes(8).toString("hex")}pipelineContextToObservability(e){return e?{pipelineRunId:e.pipelineRunId,pipelineStepIndex:e.pipelineStepIndex}:{}}pipelineContextToMeta(e){return {pipelineRunId:e?.pipelineRunId,pipelineStepIndex:e?.pipelineStepIndex}}evaluatePrompt(e,n,s){if(typeof e=="string")return e;try{return e(n,s)}catch(a){return a}}buildEvaluatorContext(e){let n={};return e.systemPrompt&&(n["worker_task/system_prompt.txt"]=e.systemPrompt),n["worker_task/user_prompt.txt"]=e.taskPrompt,e.schema&&(n["worker_task/schema.json"]=P(e.schema)?N(e.schema):B(e.schema)),Object.entries(e.inputFiles).forEach(([s,a])=>{n[`worker_task/input/${s}`]=a;}),n}isSwarmResult(e){return typeof e=="object"&&e!==null&&A in e&&e[A]===true}getFiles(e){if(this.isSwarmResult(e)){let n={...e.files};return n["result.json"]&&(n["data.json"]=n["result.json"],delete n["result.json"]),n}return e}getIndex(e,n){return this.isSwarmResult(e)?e.meta.itemIndex:n}buildResult(e,n,s){let a=s??e.files;return e.error?{[A]:true,status:"error",data:null,files:a,meta:n,error:e.error,rawData:e.rawData}:{[A]:true,status:"success",data:e.data,files:a,meta:n}}buildErrorResult(e,n){return {[A]:true,status:"error",data:null,files:{},meta:n,error:e}}};var fe=class c{swarm;steps;events;constructor(e,n=[],s={}){this.swarm=e,this.steps=n,this.events=s;}map(e){return new c(this.swarm,[...this.steps,{type:"map",config:e}],this.events)}filter(e){return new c(this.swarm,[...this.steps,{type:"filter",config:e}],this.events)}reduce(e){return new ge(this.swarm,[...this.steps,{type:"reduce",config:e}],this.events)}on(e,n){if(typeof e=="string"){let s=`on${e.charAt(0).toUpperCase()}${e.slice(1)}`;return new c(this.swarm,this.steps,{...this.events,[s]:n})}return new c(this.swarm,this.steps,{...this.events,...e})}async run(e){let n=randomBytes(8).toString("hex"),s=[],a=e,o=Date.now();for(let l=0;l<this.steps.length;l++){let r=this.steps[l],p=r.config.name,t=Date.now();this.events.onStepStart?.({type:r.type,index:l,name:p,itemCount:a.length});let u={pipelineRunId:n,pipelineStepIndex:l};try{let d=await this.executeStep(r,a,l,p,u),m=Date.now()-t;if(s.push({type:r.type,index:l,durationMs:m,results:d.output}),this.events.onStepComplete?.({type:r.type,index:l,name:p,durationMs:m,successCount:d.successCount,errorCount:d.errorCount,filteredCount:d.filteredCount}),r.type==="reduce")return {pipelineRunId:n,steps:s,output:d.output,totalDurationMs:Date.now()-o};a=d.nextItems;}catch(d){throw this.events.onStepError?.({type:r.type,index:l,name:p,error:d instanceof Error?d:new Error(String(d))}),d}}let i=s[s.length-1];return {pipelineRunId:n,steps:s,output:i?.results??[],totalDurationMs:Date.now()-o}}async executeStep(e,n,s,a,o){if(e.type==="map"){let r=e.config,p=await this.swarm.map({items:n,...r,_pipelineContext:o,retry:this.wrapRetry(r.retry,s,a),verify:this.wrapVerify(r.verify,s,a),bestOf:this.wrapBestOf(r.bestOf,s,a)});return {output:[...p],nextItems:p.success,successCount:p.success.length,errorCount:p.error.length,filteredCount:0}}if(e.type==="filter"){let r=e.config,p=await this.swarm.filter({items:n,...r,_pipelineContext:o,retry:this.wrapRetry(r.retry,s,a),verify:this.wrapVerify(r.verify,s,a)}),t=r.emit??"success",u=t==="success"?p.success:t==="filtered"?p.filtered:[...p.success,...p.filtered];return {output:[...p],nextItems:u,successCount:p.success.length,errorCount:p.error.length,filteredCount:p.filtered.length}}let i=e.config,l=await this.swarm.reduce({items:n,...i,_pipelineContext:o,retry:this.wrapRetry(i.retry,s,a),verify:this.wrapVerify(i.verify,s,a)});return {output:l,nextItems:[],successCount:l.status==="success"?1:0,errorCount:l.status==="error"?1:0,filteredCount:0}}wrapRetry(e,n,s){if(e)return {...e,onItemRetry:(a,o,i)=>{e.onItemRetry?.(a,o,i),this.events.onItemRetry?.({stepIndex:n,stepName:s,itemIndex:a,attempt:o,error:i});}}}wrapVerify(e,n,s){if(e)return {...e,onWorkerComplete:(a,o,i)=>{e.onWorkerComplete?.(a,o,i),this.events.onWorkerComplete?.({stepIndex:n,stepName:s,itemIndex:a,attempt:o,status:i});},onVerifierComplete:(a,o,i,l)=>{e.onVerifierComplete?.(a,o,i,l),this.events.onVerifierComplete?.({stepIndex:n,stepName:s,itemIndex:a,attempt:o,passed:i,feedback:l});}}}wrapBestOf(e,n,s){if(e)return {...e,onCandidateComplete:(a,o,i)=>{e.onCandidateComplete?.(a,o,i),this.events.onCandidateComplete?.({stepIndex:n,stepName:s,itemIndex:a,candidateIndex:o,status:i});},onJudgeComplete:(a,o,i)=>{e.onJudgeComplete?.(a,o,i),this.events.onJudgeComplete?.({stepIndex:n,stepName:s,itemIndex:a,winnerIndex:o,reasoning:i});}}}},ge=class c extends fe{constructor(e,n,s){super(e,n,s);}on(e,n){if(typeof e=="string"){let s=`on${e.charAt(0).toUpperCase()}${e.slice(1)}`;return new c(this.swarm,this.steps,{...this.events,[s]:n})}return new c(this.swarm,this.steps,{...this.events,...e})}map(){throw new Error("Cannot add steps after reduce")}filter(){throw new Error("Cannot add steps after reduce")}reduce(){throw new Error("Cannot add steps after reduce")}};export{ye as AGENT_REGISTRY,kt as AGENT_TYPES,z as Agent,H as Evolve,Ee as JUDGE_PROMPT,fe as Pipeline,Me as RETRY_FEEDBACK_PROMPT,ut as SCHEMA_PROMPT,A as SWARM_RESULT_BRAND,pt as SYSTEM_PROMPT,j as Semaphore,$e as Swarm,L as SwarmResultList,ge as TerminalPipeline,he as VALIDATION_PRESETS,Re as VERIFY_PROMPT,ct as WORKSPACE_PROMPT,lt as WORKSPACE_SWE_PROMPT,R as applyTemplate,Oe as buildWorkerSystemPrompt,O as createAgentParser,ne as createClaudeParser,se as createCodexParser,oe as createGeminiParser,_ as executeWithRetry,ve as expandPath,M as getAgentConfig,U as getMcpSettingsDir,$ as getMcpSettingsPath,_t as isValidAgentType,P as isZodSchema,B as jsonSchemaToString,Ot as parseNdjsonLine,It as parseNdjsonOutput,je as parseQwenOutput,qe as readLocalDir,Ze as saveLocalDir,Q as writeClaudeMcpConfig,te as writeCodexMcpConfig,X as writeGeminiMcpConfig,we as writeMcpConfig,ee as writeQwenMcpConfig,N as zodSchemaToJson};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@evolvingmachines/sdk",
3
- "version": "0.0.15",
3
+ "version": "0.0.16",
4
4
  "keywords": [
5
5
  "ai",
6
6
  "agents",