@evolvingmachines/sdk 0.0.20 → 0.0.21
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 +31 -31
- package/dist/index.d.cts +66 -8
- package/dist/index.d.ts +66 -8
- package/dist/index.js +31 -31
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -1,32 +1,32 @@
|
|
|
1
|
-
'use strict';var events=require('events'),tn=require('ajv'),At=require('zod-to-json-schema'),
|
|
2
|
-
`),
|
|
3
|
-
`)].filter(r=>r.length>0);await c.files.write(
|
|
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-6",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:i})=>{let a=n?"--continue ":"",s=i?.length?`--betas ${i.join(",")} `:"";return `echo "${c}" | claude -p ${a}${s}--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:i})=>{let a=i?` -c model_reasoning_effort="${i}"`:"";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:i,skills:a})=>{let s=n?"--continue ":"",o=a?.length?"--experimental-skills ":"",l=i||e.startsWith("dashscope/")?e:`dashscope/${e}`;return `qwen "${c}" ${s}${o}--auth-type openai --model ${l} --yolo --output-format stream-json`}}};function O(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=O(c);return `${ve(e.mcpConfig.settingsDir)}/${e.mcpConfig.filename}`}function U(c){let e=O(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 be(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 xe(c){for(let[e,n]of Object.entries(c))be(e,n);}function Se(c){return c.type?c.type:c.command?"stdio":"sse"}function Tt(c){let e=Se(c);return {...c,type:e}}function Rt(c){let{type:e,url:n,...i}=c;return Se(c)==="http"&&n?{httpUrl:n,...i}:n?{url:n,...i}:i}function Pt(c){return {type:Se(c),...c}}async function Be(c,e,n,i){xe(n);let a=U(e),s=$(e);await c.files.makeDir(a);let o={};try{let r=await c.files.read(s);typeof r=="string"&&(o=JSON.parse(r));}catch(r){if(!V(r))throw r}let l=Object.fromEntries(Object.entries(n).map(([r,p])=>[r,i(p)]));await c.files.write(s,JSON.stringify({...o,mcpServers:l},null,2));}async function Q(c,e,n){xe(n);let i=U("claude"),a=$("claude"),s=Object.fromEntries(Object.entries(n).map(([l,r])=>[l,Pt(r)]));await c.files.write(`${e}/.mcp.json`,JSON.stringify({mcpServers:s},null,2)),await c.files.makeDir(i);let o={};try{let l=await c.files.read(a);typeof l=="string"&&(o=JSON.parse(l));}catch(l){if(!V(l))throw l}o.enableAllProjectMcpServers=true,await c.files.write(a,JSON.stringify(o,null,2));}async function X(c,e){await Be(c,"gemini",e,Tt);}async function ee(c,e){await Be(c,"qwen",e,Rt);}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,i])=>`${n} = ${T(i)}`).join(", ")} }`:typeof c=="boolean"?c?"true":"false":String(c)}async function te(c,e){for(let[r,p]of Object.entries(e))be(r,p);let n=U("codex"),i=$("codex");await c.files.makeDir(n);let a="";try{let r=await c.files.read(i);typeof r=="string"&&(a=r);}catch(r){if(!V(r))throw r}let s=[],o=[];a.includes("experimental_use_rmcp_client")||(s.push("# Enable improved RMCP client (recommended)"),s.push("experimental_use_rmcp_client = true")),a.includes("[mcp_servers]")||o.push("[mcp_servers]","");for(let[r,p]of Object.entries(e)){if(a.includes(`[mcp_servers.${r}]`))continue;o.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`);o.push(`url = ${T(p.url)}`),p.bearerTokenEnvVar&&o.push(`bearer_token_env_var = ${T(p.bearerTokenEnvVar)}`);let d=p.httpHeaders??p.headers;d&&Object.keys(d).length>0&&o.push(`http_headers = ${T(d)}`),p.envHttpHeaders&&Object.keys(p.envHttpHeaders).length>0&&o.push(`env_http_headers = ${T(p.envHttpHeaders)}`);}else o.push(`command = ${T(p.command)}`),p.args&&p.args.length>0&&o.push(`args = ${T(p.args)}`),p.cwd&&o.push(`cwd = ${T(p.cwd)}`);p.env&&Object.keys(p.env).length>0&&o.push(`env = ${T(p.env)}`),p.envVars&&p.envVars.length>0&&o.push(`env_vars = ${T(p.envVars)}`),o.push("");}let l=[a.trim(),s.join(`
|
|
2
|
+
`),o.join(`
|
|
3
|
+
`)].filter(r=>r.length>0);await c.files.write(i,l.join(`
|
|
4
4
|
|
|
5
5
|
`)+`
|
|
6
|
-
`);}async function
|
|
6
|
+
`);}async function Ce(c,e,n,i){if(!(!i||Object.keys(i).length===0))switch(c){case "claude":await Q(e,n,i);break;case "codex":await te(e,i);break;case "gemini":await X(e,i);break;case "qwen":await ee(e,i);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:o(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",...i(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 i(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)}:s(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:s(t,p)}))}:typeof r=="string"&&r.length>0?{content:[{type:"content",content:{type:"text",text:p?"```\n"+r+"\n```":r}}]}:{}}function s(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 o(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
|
-
`)+p}}function
|
|
9
|
+
`)+p}}function ie(){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=i(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:o(l.items)};default:return null}}function n(l){return l.type==="todo_list"?{sessionUpdate:"plan",entries:o(l.items)}:null}function i(l){let r=l.id;switch(l.type){case "reasoning":return {sessionUpdate:"agent_thought_chunk",content:{type:"text",text:l.text??l.summary?.join(`
|
|
10
10
|
`)??l.content?.join(`
|
|
11
|
-
`)??""}};case "agent_message":return {sessionUpdate:"agent_message_chunk",content:{type:"text",text:l.text??""}};case "mcp_tool_call":{let t=a(l.result),u=l.status==="failed"||l.error?"failed":"completed";return {sessionUpdate:"tool_call_update",toolCallId:r,status:u,content:t}}case "command_execution":{let t=l.aggregated_output?[{type:"content",content:{type:"text",text:l.aggregated_output}}]:[],u=l.status==="completed"||l.exit_code===0?"completed":"failed";return {sessionUpdate:"tool_call_update",toolCallId:r,status:u,content:t}}case "file_change":{let t=l.changes??[],u=t.map(m=>({path:m.path})),d=t.length===1?`${t[0].kind==="add"?"Create":"Edit"} ${t[0].path}`:`Edit ${t.length} files`;return {sessionUpdate:"tool_call",toolCallId:r,title:d,kind:"edit",status:l.status==="completed"?"completed":"failed",content:t.map(m=>({type:"diff",path:m.path,oldText:m.kind==="add"?null:"",newText:""})),locations:u}}case "web_search":return {sessionUpdate:"tool_call",toolCallId:r,title:`Search: ${l.query??""}`,kind:"fetch",status:"completed",content:[]};case "todo_list":return {sessionUpdate:"plan",entries:
|
|
11
|
+
`)??""}};case "agent_message":return {sessionUpdate:"agent_message_chunk",content:{type:"text",text:l.text??""}};case "mcp_tool_call":{let t=a(l.result),u=l.status==="failed"||l.error?"failed":"completed";return {sessionUpdate:"tool_call_update",toolCallId:r,status:u,content:t}}case "command_execution":{let t=l.aggregated_output?[{type:"content",content:{type:"text",text:l.aggregated_output}}]:[],u=l.status==="completed"||l.exit_code===0?"completed":"failed";return {sessionUpdate:"tool_call_update",toolCallId:r,status:u,content:t}}case "file_change":{let t=l.changes??[],u=t.map(m=>({path:m.path})),d=t.length===1?`${t[0].kind==="add"?"Create":"Edit"} ${t[0].path}`:`Edit ${t.length} files`;return {sessionUpdate:"tool_call",toolCallId:r,title:d,kind:"edit",status:l.status==="completed"?"completed":"failed",content:t.map(m=>({type:"diff",path:m.path,oldText:m.kind==="add"?null:"",newText:""})),locations:u}}case "web_search":return {sessionUpdate:"tool_call",toolCallId:r,title:`Search: ${l.query??""}`,kind:"fetch",status:"completed",content:[]};case "todo_list":return {sessionUpdate:"plan",entries:o(l.items)};default:return null}}function a(l){return l?.content?Array.isArray(l.content)?l.content.map(r=>({type:"content",content:s(r)})):typeof l.content=="string"?[{type:"content",content:{type:"text",text:l.content}}]:[]:[]}function s(l){return l.type==="text"?{type:"text",text:l.text??""}:l.type==="image"?{type:"image",data:l.data??"",mimeType:l.mime_type??l.mimeType??"",uri:l.uri}:l}function o(l){if(!Array.isArray(l))return [];let r=false;return l.map(p=>{let t;return p.completed?t="completed":r?t="pending":(t="in_progress",r=true),{content:p.text??"",status:t,priority:"medium"}})}}var Et={read_file:"read",read_many_files:"read",write_file:"edit",replace:"edit",edit_file:"edit",run_shell_command:"execute",shell:"execute",glob:"search",grep:"search",search_file_content:"search",list_directory:"search",brave_web_search:"fetch",web_search:"fetch",google_web_search:"fetch",web_fetch:"fetch",delegate_to_agent:"think",write_todos:"other",save_memory:"other",activate_skill:"other"};function se(){return function(o){let l;try{l=JSON.parse(o);}catch{return null}if(l._meta||l._prompt)return null;let r=l.session_id,p=[];switch(l.type){case "init":case "result":return null;case "message":{let t=c(l);t&&p.push({sessionId:r,update:t});break}case "tool_use":{let t=e(l);t&&p.push({sessionId:r,update:t});break}case "tool_result":{let t=n(l);t&&p.push({sessionId:r,update:t});break}case "error":{let t=i(l);t&&p.push({sessionId:r,update:t});break}default:return null}return p.length>0?p:null};function c(s){let o=s.role,l=s.content;return l?o==="assistant"?{sessionUpdate:"agent_message_chunk",content:{type:"text",text:l}}:o==="user"?{sessionUpdate:"user_message_chunk",content:{type:"text",text:l}}:null:null}function e(s){let o=s.tool_id,l=s.tool_name,r=s.parameters||{},{title:p,kind:t,content:u,locations:d}=a(l,r);return {sessionUpdate:"tool_call",toolCallId:o,title:p,kind:t,status:"pending",rawInput:r,content:u,locations:d}}function n(s){let o=s.tool_id,l=s.status,r=s.output,p=s.error,t=[];return r&&typeof r=="string"&&r.length>0&&t.push({type:"content",content:{type:"text",text:l==="error"?`\`\`\`
|
|
12
12
|
${r}
|
|
13
13
|
\`\`\``:r}}),p?.message&&!r&&t.push({type:"content",content:{type:"text",text:`\`\`\`
|
|
14
14
|
${p.message}
|
|
15
|
-
\`\`\``}}),{sessionUpdate:"tool_call_update",toolCallId:
|
|
15
|
+
\`\`\``}}),{sessionUpdate:"tool_call_update",toolCallId:o,status:l==="success"?"completed":"failed",content:t}}function i(s){let o=s.severity,l=s.message;return l?{sessionUpdate:"agent_message_chunk",content:{type:"text",text:`${o==="warning"?"\u26A0\uFE0F Warning":"\u274C Error"}: ${l}`}}:null}function a(s,o){let l=Et[s]||"other",r=[],p=[],t=s;switch(s){case "read_file":t=`Read ${o.absolute_path||o.file_path||"file"}`,(o.absolute_path||o.file_path)&&p.push({path:o.absolute_path||o.file_path});break;case "write_file":t=`Write ${o.file_path||"file"}`,o.file_path&&(p.push({path:o.file_path}),r.push({type:"diff",path:o.file_path,oldText:null,newText:o.content||""}));break;case "edit_file":t=`Edit ${o.file_path||"file"}`,o.file_path&&p.push({path:o.file_path});break;case "replace":t=`Edit ${o.file_path||"file"}`,o.file_path&&(p.push({path:o.file_path}),(o.old_string!==void 0||o.new_string!==void 0)&&r.push({type:"diff",path:o.file_path,oldText:o.old_string||"",newText:o.new_string||""}));break;case "run_shell_command":case "shell":t=o.command?`\`${o.command}\``:"Run command",o.description&&r.push({type:"content",content:{type:"text",text:o.description}});break;case "brave_web_search":case "web_search":case "google_web_search":t=o.query?`"${o.query}"`:"Web search";break;case "web_fetch":t=o.prompt?`Fetch: ${o.prompt.substring(0,50)}...`:"Web fetch";break;case "glob":t=`Find ${o.pattern||"files"}`,o.dir_path&&p.push({path:o.dir_path});break;case "grep":case "search_file_content":t=`grep "${o.pattern||o.query||""}"`;break;case "list_directory":t=`List ${o.dir_path||o.path||"directory"}`,(o.dir_path||o.path)&&p.push({path:o.dir_path||o.path});break;case "read_many_files":t=`Read ${o.include?.length||"multiple"} files`,Array.isArray(o.include)&&o.include.forEach(u=>p.push({path:u}));break;case "delegate_to_agent":t=o.agent_name?`Agent: ${o.agent_name}`:"Delegate to agent";break;case "write_todos":t="Update todos";break;case "save_memory":t=o.fact?`Remember: "${o.fact.substring(0,40)}..."`:"Save memory";break;case "activate_skill":t=o.name?`Activate skill: ${o.name}`:"Activate skill";break;default:t=s;}return {title:t,kind:l,content:r,locations:p}}}var Ot={Read:"read",read_file:"read",Write:"edit",Edit:"edit",write_file:"edit",edit_file:"edit",Bash:"execute",BashOutput:"execute",KillShell:"execute",shell:"execute",run_shell_command:"execute",WebFetch:"fetch",WebSearch:"fetch",brave_web_search:"fetch",Glob:"search",Grep:"search",LS:"search",list_directory:"search",Task:"think",TodoWrite:"other",ExitPlanMode:"switch_mode"};function oe(){let c=new Map,e=new Map;return function(t){let u;try{u=JSON.parse(t);}catch{return null}if(!u||typeof u!="object")return null;let d=u;if("_meta"in d||"_prompt"in d)return null;let m=d.session_id||void 0,f=[];switch(d.type){case "assistant":{let h=n(d);for(let g of h)f.push({sessionId:m,update:g});break}case "stream_event":{let h=i(d);for(let g of h)f.push({sessionId:m,update:g});break}case "user":{let h=a(d);for(let g of h)f.push({sessionId:m,update:g});break}case "system":case "result":return null;default:return null}return f.length>0?f:null};function n(p){let t=[],u=p.message?.content;if(!Array.isArray(u))return t;for(let d of u){let m=s(d);t.push(...m);}return t}function i(p){let t=[],u=p.event;if(!u)return t;switch(u.type){case "content_block_start":{let d=u.index??0,m=u.content_block;m&&(c.set(d,m),m.type==="tool_use"&&e.set(d,""));break}case "content_block_delta":{let d=u.index??0,m=u.delta,f=c.get(d);if(m?.type==="text_delta"&&m.text)t.push({sessionUpdate:"agent_message_chunk",content:{type:"text",text:m.text}});else if(m?.type==="thinking_delta"&&m.thinking)t.push({sessionUpdate:"agent_thought_chunk",content:{type:"text",text:m.thinking}});else if(m?.type==="input_json_delta"&&m.partial_json&&f?.type==="tool_use"){let h=e.get(d)||"";e.set(d,h+m.partial_json);}break}case "content_block_stop":{let d=u.index??0,m=c.get(d);if(m&&!(m.type==="text"&&m.text)){if(!(m.type==="thinking"&&m.thinking)){if(m.type==="tool_use"){let f=m,h=e.get(d)||"";e.delete(d);let g=f.input;if(h)try{g=JSON.parse(h);}catch{}let y={...f,input:g},v=o(y);v&&t.push(v);}else if(m.type==="tool_result"){let f=l(m);f&&t.push(f);}}}c.delete(d);break}}return t}function a(p){let t=p.message?.content,u=[];if(typeof t=="string")return u.push({sessionUpdate:"user_message_chunk",content:{type:"text",text:t}}),u;if(Array.isArray(t)){for(let d of t)if(d.type==="text")u.push({sessionUpdate:"user_message_chunk",content:{type:"text",text:d.text}});else if(d.type==="tool_result"){let m=l(d);m&&u.push(m);}}return u}function s(p){switch(p.type){case "text":return p.text?[{sessionUpdate:"agent_message_chunk",content:{type:"text",text:p.text}}]:[];case "thinking":return p.thinking?[{sessionUpdate:"agent_thought_chunk",content:{type:"text",text:p.thinking}}]:[];case "tool_use":{let t=o(p);return t?[t]:[]}case "tool_result":{let t=l(p);return t?[t]:[]}}}function o(p){let t=p.id,u=p.name,d=p.input;if(u==="TodoWrite"&&Array.isArray(d?.todos))return {sessionUpdate:"plan",entries:d.todos.map(y=>({content:y.content,status:y.status||"pending",priority:"medium"}))};let{title:m,kind:f,content:h,locations:g}=r(u,d||{});return {sessionUpdate:"tool_call",toolCallId:t,title:m,kind:f,status:"pending",rawInput:d,content:h,locations:g}}function l(p){let t=p.tool_use_id,u=p.is_error??false,d=p.content,m=[];if(typeof d=="string"&&d.length>0)m.push({type:"content",content:{type:"text",text:u?`\`\`\`
|
|
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=
|
|
20
|
-
`),a=
|
|
21
|
-
Try reinstalling: npm install @evolvingmachines/sdk`):
|
|
22
|
-
Try installing: npm install @evolvingmachines/daytona`):
|
|
23
|
-
Try installing: npm install @evolvingmachines/modal`):
|
|
24
|
-
Try reinstalling: npm install @evolvingmachines/sdk`):
|
|
25
|
-
1. Set ${
|
|
19
|
+
\`\`\``:h}});}}return {sessionUpdate:"tool_call_update",toolCallId:t,status:u?"failed":"completed",content:m}}function r(p,t){let u=Ot[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 Ne(c){return oe()(c)}function I(c){switch(c){case "claude":return ne();case "codex":return ie();case "gemini":return se();case "qwen":return oe();default:return ()=>null}}function It(c,e){let n=e.trim();return n?I(c)(n):null}function Mt(c,e){let n=[],i=e.split(`
|
|
20
|
+
`),a=I(c);for(let s of i){let o=s.trim();if(!o)continue;let l=a(o);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 M="EVOLVE_API_KEY",re="E2B_API_KEY",ae="DAYTONA_API_KEY",ce="MODAL_TOKEN_ID",we="MODAL_TOKEN_SECRET",We="claude",W=36e5,Je=4,ke=100,Ye="/home/user/workspace",J="https://dashboard.evolvingmachines.ai",ze=".evolve-sdk/observability/sessions",Ge=100,He=5e3,le=3,_e=1e3;function R(c){return c!==null&&typeof c=="object"&&"safeParse"in c&&typeof c.safeParse=="function"}function L(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 i(a,s){for(let o of P__namespace.readdirSync(a)){let l=Y__namespace.join(a,o),r=s?`${s}/${o}`:o;P__namespace.statSync(l).isDirectory()?e&&i(l,r):n[r]=P__namespace.readFileSync(l);}}return i(c,""),n}function Ze(c,e){for(let[n,i]of Object.entries(e)){let a=Y__namespace.join(c,n),s=Y__namespace.dirname(a);P__namespace.mkdirSync(s,{recursive:true});let o;i instanceof ArrayBuffer||i instanceof Uint8Array?o=Buffer.from(i):o=i,P__namespace.writeFileSync(a,o);}}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 Te(c){let e=c?.type??We,n=O(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 o=c.providerBaseUrl??process.env[n.baseUrlEnv]??n.defaultBaseUrl;return {type:e,apiKey:c.providerApiKey,baseUrl:o,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 i=process.env[M];if(i)return {type:e,apiKey:i,isDirectMode:false,model:c?.model,reasoningEffort:c?.reasoningEffort,betas:c?.betas};let a=process.env[n.apiKeyEnv];if(a){let o=process.env[n.baseUrlEnv]??n.defaultBaseUrl;return {type:e,apiKey:a,baseUrl:o,isDirectMode:true,model:c?.model,reasoningEffort:c?.reasoningEffort,betas:c?.betas}}if(n.oauthEnv){let o=process.env[n.oauthEnv];if(o){if(n.oauthFileName){let l=Dt(o);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:o,isDirectMode:true,isOAuth:true,model:c?.model,reasoningEffort:c?.reasoningEffort,betas:c?.betas}}}let s=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)${s}, or ${M} / ${n.apiKeyEnv} env var.`)}async function Re(){let c=process.env[re];if(c)try{let{createE2BProvider:s}=await import('@evolvingmachines/e2b');return s({apiKey:c})}catch(s){let o=s;throw o.message?.includes("Cannot find module")||o.message?.includes("MODULE_NOT_FOUND")?new Error(`${re} is set but @evolvingmachines/e2b failed to load.
|
|
21
|
+
Try reinstalling: npm install @evolvingmachines/sdk`):o}let e=process.env[ae];if(e)try{let{createDaytonaProvider:s}=await import('@evolvingmachines/daytona');return s({apiKey:e})}catch(s){let o=s;throw o.message?.includes("Cannot find module")||o.message?.includes("MODULE_NOT_FOUND")?new Error(`${ae} is set but @evolvingmachines/daytona failed to load.
|
|
22
|
+
Try installing: npm install @evolvingmachines/daytona`):o}let n=process.env[ce],i=process.env[we];if(n&&i)try{let{createModalProvider:s}=await import('@evolvingmachines/modal');return s({tokenId:n,tokenSecret:i})}catch(s){let o=s;throw o.message?.includes("Cannot find module")||o.message?.includes("MODULE_NOT_FOUND")?new Error(`${ce} is set but @evolvingmachines/modal failed to load.
|
|
23
|
+
Try installing: npm install @evolvingmachines/modal`):o}let a=process.env[M];if(a)try{let{createE2BProvider:s}=await import('@evolvingmachines/e2b');return process.env.E2B_API_URL=Ve(),s({apiKey:a})}catch(s){let o=s;throw o.message?.includes("Cannot find module")||o.message?.includes("MODULE_NOT_FOUND")?new Error(`${M} is set but @evolvingmachines/e2b failed to load.
|
|
24
|
+
Try reinstalling: npm install @evolvingmachines/sdk`):o}throw new Error(`No sandbox provider configured. Either:
|
|
25
|
+
1. Set ${M} environment variable (recommended, get key at https://dashboard.evolvingmachines.ai)
|
|
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
|
-
4. Set ${ce} and ${
|
|
29
|
-
5. Pass sandbox explicitly: .withSandbox(provider)`)}function Ft(c){return new Promise(e=>setTimeout(e,c))}async function _(c,e,n=0){let
|
|
28
|
+
4. Set ${ce} and ${we} 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 _(c,e,n=0){let i=$t(e),a=e?.onItemRetry,s=null,o=0,l=i.backoffMs;for(;o<i.maxAttempts;){if(o++,s=await c(o),!i.retryOn(s))return s;if(o>=i.maxAttempts)break;if(a){let r=s.error??"Unknown error";a(n,o,r);}await Ft(l),l*=i.backoffMultiplier;}return s}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
|
|
|
@@ -143,7 +143,7 @@ You must judge their work based on:
|
|
|
143
143
|
5. Pick the best candidate (0-indexed)
|
|
144
144
|
|
|
145
145
|
**IMPORTANT:** Be thorough. Do not skip steps. Your judgment must be evidence-based \u2014 cite specific files, outputs, or discrepancies to justify your decision.
|
|
146
|
-
`;var
|
|
146
|
+
`;var it=`### 1. YOUR ROLE: OUTPUT VERIFIER
|
|
147
147
|
|
|
148
148
|
You are a quality verifier. An AI worker produced output for a task. Your job is to verify whether the output meets the specified quality criteria.
|
|
149
149
|
|
|
@@ -176,14 +176,14 @@ The output must satisfy:
|
|
|
176
176
|
**IMPORTANT:** Be thorough and fair. Cite specific evidence. If the output generally achieves the goal with minor issues, consider passing. Only fail if there are significant problems that violate the criteria.
|
|
177
177
|
|
|
178
178
|
If failing, provide specific, actionable feedback explaining what needs to be fixed.
|
|
179
|
-
`;var
|
|
179
|
+
`;var st=`### CONTEXT STRUCTURE
|
|
180
180
|
|
|
181
181
|
\`\`\`
|
|
182
182
|
{{fileTree}}
|
|
183
183
|
\`\`\`
|
|
184
184
|
|
|
185
185
|
Examine all files within each item folder before producing your output.
|
|
186
|
-
`;var
|
|
186
|
+
`;var ot="Evaluate the candidates and select the best one. You MUST save your decision to the file `output/result.json`.\n";var rt="Verify the worker output against the criteria. You MUST save your decision to the file `output/result.json`.\n";var at=`{{originalPrompt}}
|
|
187
187
|
|
|
188
188
|
## IMPORTANT: Previous Attempt Failed Verification
|
|
189
189
|
|
|
@@ -192,24 +192,24 @@ Your previous attempt did not pass quality verification. Please address this fee
|
|
|
192
192
|
{{feedback}}
|
|
193
193
|
|
|
194
194
|
Make sure your output addresses all the feedback points above.
|
|
195
|
-
`;var ct=Qe,lt=Xe,pt=et,ut=tt,
|
|
195
|
+
`;var ct=Qe,lt=Xe,pt=et,ut=tt,Pe=nt,dt=ot,Ee=it,mt=rt,ft=st,Oe=at;function E(c,e){let n=c;for(let[i,a]of Object.entries(e))n=n.replace(new RegExp(`\\{\\{${i}\\}\\}`,"g"),a);return n}function Ie(c){let e=c.mode==="swe"?lt:ct,n=E(e,{workingDir:c.workingDir}).trim();if(c.systemPrompt&&(n+=`
|
|
196
196
|
|
|
197
197
|
|
|
198
|
-
${
|
|
198
|
+
${E(pt,{systemPrompt:c.systemPrompt}).trim()}`),c.schema){let i=R(c.schema)?L(c.schema):B(c.schema);n+=`
|
|
199
199
|
|
|
200
200
|
|
|
201
|
-
${
|
|
202
|
-
(empty)`;let n="worker_task/system_prompt.txt"in c,
|
|
203
|
-
`)}var gt=Y.join(os.homedir(),
|
|
201
|
+
${E(ut,{schema:i}).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/
|
|
202
|
+
(empty)`;let n="worker_task/system_prompt.txt"in c,i="worker_task/schema.json"in c,a=Object.keys(c).some(r=>r.startsWith("worker_task/input/")),s=[];e.forEach((r,p)=>{let t=p===e.length-1,u=t?"\u2514\u2500\u2500 ":"\u251C\u2500\u2500 ",d=t?" ":"\u2502 ";if(r==="worker_task"){s.push({line:`${u}${r}/`,comment:"task given to worker"});let m=[];n&&m.push({name:"system_prompt.txt",comment:"worker system prompt"}),m.push({name:"user_prompt.txt",comment:"worker task prompt"}),i&&m.push({name:"schema.json",comment:"expected output schema"}),a&&m.push({name:"input/",comment:"worker input files"}),m.forEach((f,h)=>{let y=h===m.length-1?"\u2514\u2500\u2500 ":"\u251C\u2500\u2500 ";s.push({line:`${d}${y}${f.name}`,comment:f.comment});});}else if(r.startsWith("candidate_")){let m=r.replace("candidate_","");s.push({line:`${u}${r}/`,comment:`worker ${m} solution`});}else if(r==="worker_output")s.push({line:`${u}${r}/`,comment:"output to verify"});else if(r.startsWith("item_")){let m=r.replace("item_","");s.push({line:`${u}${r}/`,comment:`input ${m}`});}else s.push({line:`${u}${r}/`,comment:""});});let o=Math.max(...s.map(r=>r.line.length)),l=["context/"];for(let r of s)if(r.comment){let p=" ".repeat(o-r.line.length+3);l.push(`${r.line}${p}# ${r.comment}`);}else l.push(r.line);return l.join(`
|
|
203
|
+
`)}var gt=Y.join(os.homedir(),ze),z=class{tag;timestamp;provider;agent;model;sandboxId;apiKey;dashboardUrl;localFilePath;observability;parser;isClosed=false;metaWritten=false;localQueue=Promise.resolve();dirReady=promises.mkdir(gt,{recursive:true}).catch(()=>{});eventBuffer=[];dashboardQueue=Promise.resolve();flushTimer;constructor(e){this.validateConfig(e),this.provider=e.provider,this.agent=e.agent,this.model=e.model,this.sandboxId=e.sandboxId,this.apiKey=e.apiKey,this.dashboardUrl=J,this.observability=e.observability;let n=e.tagPrefix||"evolve";this.tag=`${n}-${crypto.randomBytes(8).toString("hex")}`,this.timestamp=new Date().toISOString();let i=this.timestamp.replace(/[:.]/g,"-"),a=`${this.tag}_${this.provider}_${this.sandboxId}_${this.agent}_${i}.jsonl`;this.localFilePath=Y.join(gt,a),this.parser=I(e.agent);}getTag(){return this.tag}getTimestamp(){return this.timestamp}writePrompt(e){this.isClosed||this.write({_prompt:{text:e}});}writeEventParsed(e,n){if(!this.isClosed&&(this.writeLocalLine(e),this.apiKey&&n))for(let i of n)this.bufferForDashboard(i);}writeEvent(e){if(!this.isClosed&&(this.writeLocalLine(e),this.apiKey)){let n=this.parser(e);if(n)for(let i of n)this.bufferForDashboard(i);}}async flush(){await this.localQueue,await this.dashboardQueue,this.apiKey&&this.eventBuffer.length>0&&(this.flushDashboard(),await this.dashboardQueue);}async close(){this.isClosed||(this.isClosed=true,this.flushTimer&&(clearTimeout(this.flushTimer),this.flushTimer=void 0),this.write({_sessionEnd:{timestamp:new Date().toISOString()}}),await this.flush());}writeLocalLine(e){this.metaWritten||this.writeMeta();let n=e.endsWith(`
|
|
204
204
|
`)?e:e+`
|
|
205
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)+`
|
|
206
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)+`
|
|
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(`
|
|
208
|
-
`);r=
|
|
209
|
-
`),
|
|
210
|
-
`),
|
|
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}
|
|
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>=Ge){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 i=1;i<=le;i++)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(i===le){console.debug(`[SessionLogger] Dashboard ${a.status} after ${i} retries, requeueing`),this.requeueEvents(e);return}await this.delay(_e*i);continue}console.debug(`[SessionLogger] Dashboard ${a.status}, dropping events`);return}catch(a){i===le?(console.debug("[SessionLogger] Dashboard sync failed after retries, requeueing:",a),this.requeueEvents(e)):await this.delay(_e*i);}}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 Me(c){if(!c?.trim())throw new Error("userId is required and cannot be empty")}async function me(c,e,n){Me(c);let i=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 i.create(c,Object.keys(a).length>0?a:void 0)}async function vt(c,e,n,i){Me(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(Me(c),e?.keys)for(let[i,a]of Object.entries(e.keys))try{await vt(c,i,a);}catch(s){throw new Error(`Failed to create API key connection for "${i}": ${s.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 bt(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 xt(c,e){bt(c);let i=await(await me(c,{manageConnections:false})).authorize(e);return {url:i.redirectUrl,connectionId:i.id}}async function St(c,e){let n=await De(c),i={};for(let a of n)i[a.toolkit]=a.connected;return e?i[e]??false:i}async function De(c){return bt(c),(await en().connectedAccounts.list({userIds:[c],statuses:["ACTIVE"]})).items.map(i=>({toolkit:i.toolkit.slug,connected:i.status==="ACTIVE",accountId:i.id}))}var Fe={auth:xt,status:St,connections:De};function nn(c){return c.replace(/\\/g,"\\\\").replace(/"/g,'\\"').replace(/\$/g,"\\$").replace(/`/g,"\\`")}var G=class{agentConfig;options;sandbox;hasRun=false;workingDir;lastRunTimestamp;registry;sessionLogger;activeCommand;activeProcessId=null;activeOperationId=null;activeOperationKind=null;nextOperationId=0;interruptedOperations=new Set;sandboxState;agentState="idle";skills;zodSchema;jsonSchema;schemaOptions;compiledValidator;constructor(e,n={}){if(this.agentConfig=e,this.options=n,this.workingDir=n.workingDirectory||Ye,this.sandboxState=n.sandboxId?"ready":"stopped",this.skills=n.skills,n.schema)if(R(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 i=this.createAjvValidator();this.compiledValidator=i.compile(this.jsonSchema);}catch(i){throw new Error(`Invalid JSON Schema: ${i.message}`)}}this.registry=O(e.type);}emitLifecycle(e,n){e?.onLifecycle?.({sandboxId:this.getSession(),sandbox:this.sandboxState,agent:this.agentState,timestamp:new Date().toISOString(),reason:n});}invalidateActiveOperation(){this.activeCommand=void 0,this.activeProcessId=null,this.activeOperationId=null,this.activeOperationKind=null;}beginOperation(e,n,i,a){let s=++this.nextOperationId;return this.activeOperationId=s,this.activeOperationKind=e,this.activeCommand=n,this.activeProcessId=n.processId,this.sandboxState="running",this.agentState="running",this.emitLifecycle(i,a),s}finalizeOperation(e,n,i,a="idle",s="ready"){return this.activeOperationId!==e?false:(this.invalidateActiveOperation(),this.sandboxState=s,this.agentState=a,this.emitLifecycle(n,i),true)}watchBackgroundOperation(e,n,i,a){let s=n==="run"?"run_background_complete":"command_background_complete",o=n==="run"?"run_background_failed":"command_background_failed",l=n==="run"?"run_interrupted":"command_interrupted";i.wait().then(r=>{if(this.interruptedOperations.delete(e)||r.exitCode===130){this.finalizeOperation(e,a,l,"interrupted");return}let t=r.exitCode===0?s:o,u=r.exitCode===0?"idle":"error";this.finalizeOperation(e,a,t,u);}).catch(()=>{this.interruptedOperations.delete(e),this.finalizeOperation(e,a,o,"error");});}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(e){if(this.sandbox)return this.sandbox;if(!this.options.sandboxProvider)throw new Error("No sandbox provider configured");let n=this.options.sandboxProvider;this.sandboxState="booting",this.emitLifecycle(e,"sandbox_boot");try{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 n.connect(this.options.sandboxId),this.hasRun=!0,this.sandboxState="ready",this.agentState="idle",this.emitLifecycle(e,"sandbox_connected");else {let i=this.buildEnvironmentVariables();this.sandbox=await n.create({envs:i,workingDirectory:this.workingDir}),await this.setupAgentAuth(this.sandbox),await this.setupWorkspace(this.sandbox),this.sandboxState="ready",this.agentState="idle",this.emitLifecycle(e,"sandbox_ready");}}catch(i){throw this.sandboxState="error",this.agentState="error",this.emitLifecycle(e,"sandbox_error"),i}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",i=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 ${i}`,{timeoutMs:3e4});let a=Ie({workingDir:this.workingDir,systemPrompt:this.options.systemPrompt,schema:this.zodSchema||this.jsonSchema,mode:n}),s=`${this.workingDir}/${this.registry.systemPromptFile}`;await e.files.write(s,a),this.options.context&&await this.uploadContextFiles(e,this.options.context),this.options.files&&await this.uploadWorkspaceFiles(e,this.options.files);let o={...this.options.mcpServers};if(this.options.composio){let l=await Ae(this.options.composio.userId,this.options.composio.config);o={...o,composio:{type:"http",url:l.url,headers:l.headers}};}Object.keys(o).length>0&&await Ce(this.agentConfig.type,e,this.workingDir,o),this.skills?.length&&await this.setupSkills(e);}async setupSkills(e){if(!this.skills?.length)return;let{skillsConfig:n}=this.registry,{sourceDir:i,targetDir:a}=n;await e.files.makeDir(a);for(let s of this.skills){let o=`cp -r ${i}/${s} ${a}/ 2>/dev/null || true`;await e.commands.run(o,{timeoutMs:3e4});}}async uploadContextFiles(e,n){let i=Object.entries(n).map(([s,o])=>({path:`${this.workingDir}/context/${s}`,data:o}));if(i.length===0)return;let a=new Set(i.map(s=>s.path.substring(0,s.path.lastIndexOf("/"))).filter(Boolean));a.size>0&&await e.commands.run(`mkdir -p ${Array.from(a).join(" ")}`,{timeoutMs:3e4}),await e.files.writeBatch(i);}async uploadWorkspaceFiles(e,n){let i=Object.entries(n).map(([s,o])=>({path:s.startsWith("/")?s:`${this.workingDir}/${s}`,data:o}));if(i.length===0)return;let a=new Set(i.map(s=>s.path.substring(0,s.path.lastIndexOf("/"))).filter(Boolean));a.size>0&&await e.commands.run(`mkdir -p ${Array.from(a).join(" ")}`,{timeoutMs:3e4}),await e.files.writeBatch(i);}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:i,timeoutMs:a=W,background:s=false}=e;if(this.activeCommand)throw new Error("Agent is already running. Call interrupt(), wait for the active/background run to finish, or create a new Evolve instance.");let o=await this.getSandbox(n);if(this.lastRunTimestamp=Date.now(),!this.sessionLogger){let g=this.options.sandboxProvider;this.sessionLogger=new z({provider:g?.name||g?.providerType||"unknown",agent:this.agentConfig.type,model:this.agentConfig.model||this.registry.defaultModel,sandboxId:o.sandboxId,tagPrefix:this.options.sessionTagPrefix,apiKey:this.agentConfig.isDirectMode?void 0:this.agentConfig.apiKey,observability:this.options.observability});}this.sessionLogger.writePrompt(i);let l=this.buildCommand(i),r="",p=I(this.agentConfig.type),t=g=>{r+=g;let y=r.split(`
|
|
208
|
+
`);r=y.pop()??"";for(let v of y){if(!v.trim())continue;let x=p(v);if(this.sessionLogger?.writeEventParsed(v,x),n?.onStdout?.(v+`
|
|
209
|
+
`),x&&n?.onContent)for(let b of x)n.onContent(b);}},u=g=>{n?.onStderr?.(g);},d=await o.commands.spawn(l,{cwd:this.workingDir,timeoutMs:a,onStdout:t,onStderr:u}),m=this.beginOperation("run",d,n,"run_start");if(this.hasRun=true,s)return this.watchBackgroundOperation(m,"run",d,n),{sandboxId:o.sandboxId,exitCode:0,stdout:`Background process started with ID ${d.processId}`,stderr:""};let f;try{f=await d.wait();}catch(g){throw this.interruptedOperations.delete(m),this.finalizeOperation(m,n,"run_failed","error"),g}if(this.interruptedOperations.delete(m)||f.exitCode===130?this.finalizeOperation(m,n,"run_interrupted","interrupted"):f.exitCode===0?this.finalizeOperation(m,n,"run_complete","idle"):this.finalizeOperation(m,n,"run_failed","error"),r.trim()){let g=p(r);if(this.sessionLogger?.writeEventParsed(r,g),n?.onStdout?.(r+`
|
|
210
|
+
`),g&&n?.onContent)for(let y of g)n.onContent(y);}return {sandboxId:o.sandboxId,exitCode:f.exitCode,stdout:f.stdout,stderr:f.stderr}}async executeCommand(e,n={},i){let{timeoutMs:a=W,background:s=false}=n;if(this.activeCommand)throw new Error("Agent is already running. Call interrupt(), wait for the active/background command to finish, or create a new Evolve instance.");let o=await this.getSandbox(i);this.lastRunTimestamp=Date.now();let l="",r="",p=h=>{l+=h,i?.onStdout?.(h);},t=h=>{r+=h,i?.onStderr?.(h);},u=await o.commands.spawn(e,{cwd:this.workingDir,timeoutMs:a,onStdout:p,onStderr:t}),d=this.beginOperation("command",u,i,"command_start");if(s)return this.watchBackgroundOperation(d,"command",u,i),{sandboxId:o.sandboxId,exitCode:0,stdout:`Background process started with ID ${u.processId}`,stderr:""};let m;try{m=await u.wait();}catch(h){throw this.interruptedOperations.delete(d),this.finalizeOperation(d,i,"command_failed","error"),h}return this.interruptedOperations.delete(d)||m.exitCode===130?this.finalizeOperation(d,i,"command_interrupted","interrupted"):m.exitCode===0?this.finalizeOperation(d,i,"command_complete","idle"):this.finalizeOperation(d,i,"command_failed","error"),{sandboxId:o.sandboxId,exitCode:m.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(),i=`${this.workingDir}/output`,a=e?"":"-maxdepth 1",o=(await n.commands.run(`find ${i} ${a} -type f -exec stat -c '%n|%Z' {} \\; 2>/dev/null || true`,{timeoutMs:3e4})).stdout.split(`
|
|
211
|
+
`).filter(Boolean),l=this.lastRunTimestamp?Math.floor(this.lastRunTimestamp/1e3)-2:0,r=[],p=`${i}/`;for(let f of o){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){if(this.activeCommand&&!await this.interrupt())throw new Error("Cannot switch session while an active process is running and could not be interrupted.");this.sessionLogger&&(await this.sessionLogger.close(),this.sessionLogger=void 0),this.options.sandboxId=e,this.sandbox=void 0,this.interruptedOperations.clear(),this.invalidateActiveOperation(),this.sandboxState="ready",this.agentState="idle",this.hasRun=true;}async pause(e){this.sandbox&&(this.activeCommand&&await this.interrupt(e),await this.sandbox.pause(),this.sandboxState="paused",this.agentState="idle",this.emitLifecycle(e,"sandbox_pause"));}async resume(e){this.sandbox&&this.options.sandboxProvider&&(this.sandbox=await this.options.sandboxProvider.connect(this.sandbox.sandboxId),this.sandboxState="ready",this.agentState="idle",this.emitLifecycle(e,"sandbox_resume"));}async interrupt(e){if(!this.activeCommand&&!this.activeProcessId)return false;let n=this.activeOperationId,i=this.activeOperationKind,a=false;try{this.activeCommand?a=await this.activeCommand.kill():this.sandbox&&this.activeProcessId&&(a=await this.sandbox.commands.kill(this.activeProcessId));}catch{a=false;}if(!a)return this.sandboxState="running",this.agentState="running",false;n!==null&&this.interruptedOperations.add(n),this.invalidateActiveOperation(),this.sandboxState="ready",this.agentState="interrupted";let s=i==="run"?"run_interrupted":"command_interrupted";return this.emitLifecycle(e,s),a}status(){return {sandboxId:this.getSession(),sandbox:this.sandboxState,agent:this.agentState,activeProcessId:this.activeProcessId,hasRun:this.hasRun,timestamp:new Date().toISOString()}}async kill(e){this.sessionLogger&&(await this.sessionLogger.close(),this.sessionLogger=void 0),this.activeCommand&&await this.interrupt(e),this.sandbox&&(await this.sandbox.kill(),this.sandbox=void 0),this.options.sandboxId=void 0,this.interruptedOperations.clear(),this.invalidateActiveOperation(),this.sandboxState="stopped",this.agentState="idle",this.hasRun=false,this.emitLifecycle(e,"sandbox_killed");}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;fallbackSandboxState="stopped";fallbackAgentState="idle";fallbackHasRun=false;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.agent||(this.fallbackSandboxState="ready",this.fallbackAgentState="idle",this.fallbackHasRun=true),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&&(R(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 Re(),i=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:{...i,...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 G(e,a);}createStreamCallbacks(){let e=this.listenerCount("stdout")>0,n=this.listenerCount("stderr")>0,i=this.listenerCount("content")>0,a=this.listenerCount("lifecycle")>0;return {onStdout:e?s=>this.emit("stdout",s):void 0,onStderr:n?s=>this.emit("stderr",s):void 0,onContent:i?s=>this.emit("content",s):void 0,onLifecycle:a?s=>this.emit("lifecycle",s):void 0}}emitLifecycleFromStatus(e){if(this.listenerCount("lifecycle")===0)return;let n=this.status();this.emit("lifecycle",{sandboxId:n.sandboxId,sandbox:n.sandbox,agent:n.agent,timestamp:new Date().toISOString(),reason:e});}async run({prompt:e,timeoutMs:n,background:i}){this.agent||await this.initializeAgent();let a=this.createStreamCallbacks();return this.agent.run({prompt:e,timeoutMs:n,background:i},a)}async executeCommand(e,n={}){this.agent||await this.initializeAgent();let i=this.createStreamCallbacks();return this.agent.executeCommand(e,n,i)}async interrupt(){if(!this.agent)return false;let e=this.createStreamCallbacks();return this.agent.interrupt(e)}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.fallbackSandboxState="ready",this.fallbackAgentState="idle",this.fallbackHasRun=true),this.config.sandboxId=e;}status(){return this.agent?this.agent.status():{sandboxId:this.config.sandboxId??null,sandbox:this.fallbackSandboxState,agent:this.fallbackAgentState,activeProcessId:null,hasRun:this.fallbackHasRun,timestamp:new Date().toISOString()}}async pause(){if(this.agent){let e=this.createStreamCallbacks();await this.agent.pause(e);return}this.config.sandboxId&&this.config.sandbox&&(await(await this.config.sandbox.connect(this.config.sandboxId)).pause(),this.fallbackSandboxState="paused",this.fallbackAgentState="idle",this.emitLifecycleFromStatus("sandbox_pause"));}async resume(){if(this.agent){let e=this.createStreamCallbacks();await this.agent.resume(e);return}this.config.sandboxId&&this.config.sandbox&&(await this.config.sandbox.connect(this.config.sandboxId),this.fallbackSandboxState="ready",this.fallbackAgentState="idle",this.emitLifecycleFromStatus("sandbox_resume"));}async kill(){if(this.agent){let e=this.createStreamCallbacks();await this.agent.kill(e),this.config.sandboxId=void 0,this.fallbackSandboxState="stopped",this.fallbackAgentState="idle",this.fallbackHasRun=false;return}this.config.sandboxId&&this.config.sandbox&&(await(await this.config.sandbox.connect(this.config.sandboxId)).kill(),this.fallbackSandboxState="stopped",this.fallbackAgentState="idle",this.fallbackHasRun=false,this.emitLifecycleFromStatus("sandbox_killed"),this.config.sandboxId=void 0);}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 N=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 N(this.config.concurrency);}async map(e){let{items:n,prompt:i,bestOf:a,verify:s}=e,o=e.retry??this.config.retry,l=this.generateOperationId(),r=e.timeoutMs??this.config.timeoutMs;if(a&&s)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,i,u,l,e,r,o):s?this.executeMapItemWithVerify(t,i,u,l,e,r,o):o?_(d=>this.executeMapItem(t,i,u,l,e,r,d),o,u):this.executeMapItem(t,i,u,l,e,r)));return j.from(p)}async filter(e){let{items:n,prompt:i,condition:a,verify:s}=e,o=e.retry??this.config.retry,l=this.generateOperationId(),r=e.timeoutMs??this.config.timeoutMs,p=await Promise.all(n.map((u,d)=>s?this.executeFilterItemWithVerify(u,i,d,l,e,r,o):o?_(m=>this.executeFilterItem(u,i,d,l,e,r,m),o,d):this.executeFilterItem(u,i,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:i,verify:a}=e,s=e.retry??this.config.retry,o=this.generateOperationId(),l=e.timeoutMs??this.config.timeoutMs,r=[],p=[];n.forEach((b,C)=>{r.push(this.getFiles(b)),p.push(this.getIndex(b,C));});let t={};r.forEach((b,C)=>{Object.entries(b).forEach(([S,w])=>{t[`item_${p[C]}/${S}`]=w;});});let u=ue(t),d=E(ft,{fileTree:u}),m=e.systemPrompt?`${d}
|
|
212
212
|
|
|
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
|
|
213
|
+
${e.systemPrompt}`:d,f=(b,C,S)=>({operationId:o,operation:"reduce",tag:b.tag,sandboxId:b.sandboxId,swarmName:this.config.tag,operationName:e.name,inputCount:n.length,inputIndices:p,errorRetry:C,verifyRetry:S,...this.pipelineContextToMeta(e._pipelineContext)}),h=e.mcpServers??this.config.mcpServers,g=e.skills??this.config.skills,y=e.composio??this.config.composio,v=async(b,C,S,w)=>{let k=await this.semaphore.use(()=>this.execute(t,b,{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:o,operation:"reduce",role:"worker",errorRetry:S,verifyRetry:w&&w>1?w-1:void 0,...this.pipelineContextToObservability(e._pipelineContext)}})),F=f(k,S,w&&w>1?w-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}},x=`${this.config.tag}-reduce`;return a?this.runWithVerification((b,C,S)=>v(b,C,void 0,S),{originalPrompt:i,inputFiles:t,verifyConfig:a,timeoutMs:l,systemPrompt:m,schema:e.schema,mcpServers:h,skills:g,composio:y,operationId:o,baseTag:x,retry:s,operation:"reduce",_pipelineContext:e._pipelineContext}):s?_(b=>{let C=b>1?b-1:void 0,S=C?`${x}-er${C}`:x;return v(i,S,C)},s):v(i,x)}async bestOf(e){let{item:n,prompt:i,config:a}=e,s=e.retry??this.config.retry,o=a.n??a.taskAgents?.length;if(o===void 0)throw new Error("bestOf requires n or taskAgents");if(o<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:o},async(C,S)=>{let w=s?await _(k=>this.executeBestOfCandidate({inputFiles:p,prompt:i,candidateIndex:S,operationId:l,config:a,systemPrompt:e.systemPrompt,schema:e.schema,schemaOptions:e.schemaOptions,mcpServers:t,skills:d,composio:f,timeoutMs:r,attempt:k}),s,S):await this.executeBestOfCandidate({inputFiles:p,prompt:i,candidateIndex:S,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,S,w.status==="success"?"success":"error"),w})),y=s?await _(C=>this.executeBestOfJudge({inputFiles:p,taskPrompt:i,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}),{...s,retryOn:void 0},0):await this.executeBestOfJudge({inputFiles:p,taskPrompt:i,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"),x=y.decision?.winner??(v>=0?v:0);a.onJudgeComplete?.(0,x,y.decision?.reasoning??"Judge failed to provide reasoning");let b={operationId:l,operation:"bestof-judge",tag:y.tag,sandboxId:y.sandboxId,swarmName:this.config.tag,operationName:e.name,candidateCount:o};return {winner:g[x]??g[0],winnerIndex:x,judgeReasoning:y.decision?.reasoning??"Judge failed to provide reasoning",judgeMeta:b,candidates:g}}async execute(e,n,i){let a=null,s="",o=i.tagPrefix,l={},r=null,p,t,u={...this.config.agent,...i.agent};try{a=new H().withAgent(u).withSandbox(this.config.sandbox).withWorkspaceMode(this.config.workspaceMode).withSessionTagPrefix(i.tagPrefix),i.schema&&a.withSchema(i.schema,i.schemaOptions),i.systemPrompt&&a.withSystemPrompt(i.systemPrompt),i.mcpServers&&a.withMcpServers(i.mcpServers),i.skills?.length&&a.withSkills(i.skills),i.composio&&a.withComposio(i.composio.userId,i.composio.config),i.observability&&a.withObservability(i.observability),Object.keys(e).length>0&&a.withContext(e);let d=await a.run({prompt:n,timeoutMs:i.timeoutMs});s=d.sandboxId,o=a.getSessionTag()??i.tagPrefix;let m=null;try{m=await a.getOutputFiles(!0),l=m.files;}catch{}d.exitCode!==0?p=`Agent exited with code ${d.exitCode}`:i.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){o=a.getSessionTag()??i.tagPrefix;try{l=(await a.getOutputFiles(!0)).files;}catch{}}}finally{a&&await a.kill().catch(()=>{});}return {files:l,data:r,tag:o,sandboxId:s,error:p,rawData:t}}async executeMapItem(e,n,i,a,s,o,l=1){let r=this.getFiles(e),p=l>1?`${this.config.tag}-map-${i}-er${l-1}`:`${this.config.tag}-map-${i}`,t=this.evaluatePrompt(n,r,i);if(t instanceof Error)return this.buildErrorResult(`Prompt function threw: ${t.message}`,{operationId:a,operation:"map",tag:p,sandboxId:"",itemIndex:i});let u=s.mcpServers??this.config.mcpServers,d=s.skills??this.config.skills,m=s.composio??this.config.composio,f=await this.semaphore.use(()=>this.execute(r,t,{systemPrompt:s.systemPrompt,schema:s.schema,schemaOptions:s.schemaOptions,agent:s.agent,mcpServers:u,skills:d,composio:m,tagPrefix:p,timeoutMs:o,observability:{swarmName:this.config.tag,operationName:s.name,operationId:a,operation:"map",itemIndex:i,role:"worker",errorRetry:l>1?l-1:void 0,...this.pipelineContextToObservability(s._pipelineContext)}})),h={operationId:a,operation:"map",tag:f.tag,sandboxId:f.sandboxId,swarmName:this.config.tag,operationName:s.name,itemIndex:i,errorRetry:l>1?l-1:void 0,...this.pipelineContextToMeta(s._pipelineContext)};return this.buildResult(f,h)}async executeMapItemWithVerify(e,n,i,a,s,o,l){let r=this.getFiles(e),p=`${this.config.tag}-map-${i}`,t=s.verify,u=s.mcpServers??this.config.mcpServers,d=s.skills??this.config.skills,m=s.composio??this.config.composio,f=this.evaluatePrompt(n,r,i);if(f instanceof Error)return this.buildErrorResult(`Prompt function threw: ${f.message}`,{operationId:a,operation:"map",tag:p,sandboxId:"",itemIndex:i});let h=async(g,y,v)=>{let x=await this.semaphore.use(()=>this.execute(r,g,{systemPrompt:s.systemPrompt,schema:s.schema,schemaOptions:s.schemaOptions,agent:s.agent,mcpServers:u,skills:d,composio:m,tagPrefix:y,timeoutMs:o,observability:{swarmName:this.config.tag,operationName:s.name,operationId:a,operation:"map",itemIndex:i,role:"worker",verifyRetry:v&&v>1?v-1:void 0,...this.pipelineContextToObservability(s._pipelineContext)}})),b={operationId:a,operation:"map",tag:x.tag,sandboxId:x.sandboxId,swarmName:this.config.tag,operationName:s.name,itemIndex:i,verifyRetry:v&&v>1?v-1:void 0,...this.pipelineContextToMeta(s._pipelineContext)};return this.buildResult(x,b)};return this.runWithVerification(h,{originalPrompt:f,inputFiles:r,verifyConfig:t,timeoutMs:o,systemPrompt:s.systemPrompt,schema:s.schema,mcpServers:u,skills:d,composio:m,operationId:a,baseTag:p,retry:l,itemIndex:i,operation:"map",_pipelineContext:s._pipelineContext})}async executeMapItemWithBestOf(e,n,i,a,s,o,l){let r=this.getFiles(e),p=`${this.config.tag}-map-${i}`,t=this.evaluatePrompt(n,r,i);if(t instanceof Error)return this.buildErrorResult(`Prompt function threw: ${t.message}`,{operationId:a,operation:"map",tag:p,sandboxId:"",itemIndex:i});let u=s.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:i});let m=s.mcpServers??this.config.mcpServers,f=u.mcpServers??m,h=u.judgeMcpServers??u.mcpServers??m,g=s.skills??this.config.skills,y=u.skills??g,v=u.judgeSkills??u.skills??g,x=s.composio??this.config.composio,b=u.composio??x,C=u.judgeComposio??u.composio??x,S=await Promise.all(Array.from({length:d},async(q,Z)=>{let Le=l?await _(wt=>this.executeBestOfCandidate({inputFiles:r,prompt:t,candidateIndex:Z,operationId:a,config:u,systemPrompt:s.systemPrompt,schema:s.schema,schemaOptions:s.schemaOptions,mcpServers:f,skills:y,composio:b,timeoutMs:o,parentIndex:i,attempt:wt,_pipelineContext:s._pipelineContext}),l,Z):await this.executeBestOfCandidate({inputFiles:r,prompt:t,candidateIndex:Z,operationId:a,config:u,systemPrompt:s.systemPrompt,schema:s.schema,schemaOptions:s.schemaOptions,mcpServers:f,skills:y,composio:b,timeoutMs:o,parentIndex:i,_pipelineContext:s._pipelineContext});return u.onCandidateComplete?.(i,Z,Le.status==="success"?"success":"error"),Le})),w=l?await _(q=>this.executeBestOfJudge({inputFiles:r,taskPrompt:t,candidates:S,config:u,timeoutMs:o,operationId:a,systemPrompt:s.systemPrompt,schema:s.schema,schemaOptions:s.schemaOptions,mcpServers:h,skills:v,composio:C,parentIndex:i,attempt:q,_pipelineContext:s._pipelineContext}),{...l,retryOn:void 0},0):await this.executeBestOfJudge({inputFiles:r,taskPrompt:t,candidates:S,config:u,timeoutMs:o,operationId:a,systemPrompt:s.systemPrompt,schema:s.schema,schemaOptions:s.schemaOptions,mcpServers:h,skills:v,composio:C,parentIndex:i,_pipelineContext:s._pipelineContext}),k=S.findIndex(q=>q.status==="success"),F=w.decision?.winner??(k>=0?k:0),Ue=S[F]??S[0];u.onJudgeComplete?.(i,F,w.decision?.reasoning??"Judge failed to provide reasoning");let Ct={operationId:a,operation:"bestof-judge",tag:w.tag,sandboxId:w.sandboxId,swarmName:this.config.tag,operationName:s.name,candidateCount:d,...this.pipelineContextToMeta(s._pipelineContext)};return {...Ue,meta:{...Ue.meta,operation:"map",swarmName:this.config.tag,operationName:s.name,itemIndex:i,...this.pipelineContextToMeta(s._pipelineContext)},bestOf:{winnerIndex:F,judgeReasoning:w.decision?.reasoning??"Judge failed to provide reasoning",judgeMeta:Ct,candidates:S}}}async executeFilterItem(e,n,i,a,s,o,l=1){let r=this.getFiles(e),p=l>1?`${this.config.tag}-filter-${i}-er${l-1}`:`${this.config.tag}-filter-${i}`,t=s.mcpServers??this.config.mcpServers,u=s.skills??this.config.skills,d=s.composio??this.config.composio,m=await this.semaphore.use(()=>this.execute(r,n,{systemPrompt:s.systemPrompt,schema:s.schema,schemaOptions:s.schemaOptions,agent:s.agent,mcpServers:t,skills:u,composio:d,tagPrefix:p,timeoutMs:o,observability:{swarmName:this.config.tag,operationName:s.name,operationId:a,operation:"filter",itemIndex:i,role:"worker",errorRetry:l>1?l-1:void 0,...this.pipelineContextToObservability(s._pipelineContext)}})),f={operationId:a,operation:"filter",tag:m.tag,sandboxId:m.sandboxId,swarmName:this.config.tag,operationName:s.name,itemIndex:i,errorRetry:l>1?l-1:void 0,...this.pipelineContextToMeta(s._pipelineContext)};return this.buildResult(m,f,r)}async executeFilterItemWithVerify(e,n,i,a,s,o,l){let r=this.getFiles(e),p=`${this.config.tag}-filter-${i}`,t=s.verify,u=s.mcpServers??this.config.mcpServers,d=s.skills??this.config.skills,m=s.composio??this.config.composio,f=async(h,g,y)=>{let v=await this.semaphore.use(()=>this.execute(r,h,{systemPrompt:s.systemPrompt,schema:s.schema,schemaOptions:s.schemaOptions,agent:s.agent,mcpServers:u,skills:d,composio:m,tagPrefix:g,timeoutMs:o,observability:{swarmName:this.config.tag,operationName:s.name,operationId:a,operation:"filter",itemIndex:i,role:"worker",verifyRetry:y&&y>1?y-1:void 0,...this.pipelineContextToObservability(s._pipelineContext)}})),x={operationId:a,operation:"filter",tag:v.tag,sandboxId:v.sandboxId,swarmName:this.config.tag,operationName:s.name,itemIndex:i,verifyRetry:y&&y>1?y-1:void 0,...this.pipelineContextToMeta(s._pipelineContext)};return this.buildResult(v,x,r)};return this.runWithVerification(f,{originalPrompt:n,inputFiles:r,verifyConfig:t,timeoutMs:o,systemPrompt:s.systemPrompt,schema:s.schema,mcpServers:u,skills:d,composio:m,operationId:a,baseTag:p,retry:l,itemIndex:i,operation:"filter",_pipelineContext:s._pipelineContext})}async executeBestOfCandidate(e){let{inputFiles:n,prompt:i,candidateIndex:a,operationId:s,config:o,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,i,{systemPrompt:e.systemPrompt,schema:e.schema,schemaOptions:e.schemaOptions,agent:o.taskAgents?.[a],mcpServers:e.mcpServers,skills:e.skills,composio:e.composio,tagPrefix:d,timeoutMs:l,observability:{swarmName:this.config.tag,operationId:s,operation:"map",itemIndex:r,role:"candidate",candidateIndex:a,errorRetry:p>1?p-1:void 0,...this.pipelineContextToObservability(t)}})),f={operationId:s,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((i,a)=>{i.status==="error"&&(n[`candidate_${a}/_failed.txt`]=`STATUS: FAILED
|
|
214
214
|
|
|
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;
|
|
215
|
+
Error: ${i.error??"Unknown error"}`),Object.entries(i.files).forEach(([s,o])=>{n[`candidate_${a}/${s}`]=o;});}),n}async executeBestOfJudge(e){let{candidates:n,config:i,timeoutMs:a,operationId:s,parentIndex:o,attempt:l=1,_pipelineContext:r}=e,p=o!==void 0?`${this.config.tag}-map-${o}-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=E(Pe,{candidateCount:String(n.length),criteria:i.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:i.judgeAgent,mcpServers:e.mcpServers,skills:e.skills,composio:e.composio,tagPrefix:t,timeoutMs:a,observability:{swarmName:this.config.tag,operationId:s,operation:"map",itemIndex:o,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(([i,a])=>{n[`worker_output/${i}`]=a;}),n}async executeVerify(e){let{config:n,timeoutMs:i,operationId:a,workerTag:s,retryAttempt:o=1,operation:l,itemIndex:r,attemptIndex:p,_pipelineContext:t}=e,u=o>1?`${s}-verifier-er${o-1}`:`${s}-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=E(Ee,{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:i,observability:{swarmName:this.config.tag,operationId:a,operation:l,itemIndex:r,role:"verifier",verifyRetry:p&&p>1?p-1:void 0,errorRetry:o>1?o-1:void 0,...this.pipelineContextToObservability(t)}})),x=null;if(v.data&&!v.error)x=v.data;else if(v.rawData)try{let b=JSON.parse(v.rawData);x={passed:!!b.passed,reasoning:b.reasoning??"Verification completed",feedback:b.feedback};}catch{console.warn(`Verify validation failed: ${v.error}`);}return {status:x?"success":"error",decision:x,tag:v.tag,sandboxId:v.sandboxId,error:x?void 0:"Verifier failed to produce valid decision"}}static buildRetryPromptWithFeedback(e,n){return E(Oe,{originalPrompt:e,feedback:n})}async runWithVerification(e,n){let{originalPrompt:i,inputFiles:a,verifyConfig:s,timeoutMs:o,operationId:l,baseTag:r,retry:p,itemIndex:t=0,operation:u,_pipelineContext:d}=n,m=s.maxAttempts??c.DEFAULT_VERIFY_MAX_ATTEMPTS,f=i,h=null,g=0;for(;g<m;){g++;let v=g>1?`${r}-vr${g-1}`:r,x=p?await _(S=>e(f,S>1?`${v}-er${S-1}`:v,g),p,t):await e(f,v,g);if(s.onWorkerComplete?.(t,g,x.status==="error"?"error":"success"),x.status==="error")return x;h=x;let b=p?await _(S=>this.executeVerify({inputFiles:a,outputFiles:x.files,taskPrompt:f,config:s,timeoutMs:o,systemPrompt:n.systemPrompt,schema:n.schema,mcpServers:n.mcpServers,skills:n.skills,composio:n.composio,operationId:l,workerTag:v,retryAttempt:S,operation:u,itemIndex:t,attemptIndex:g,_pipelineContext:d}),{...p,retryOn:void 0}):await this.executeVerify({inputFiles:a,outputFiles:x.files,taskPrompt:f,config:s,timeoutMs:o,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:b.tag,sandboxId:b.sandboxId,swarmName:this.config.tag,attempts:g,...this.pipelineContextToMeta(d)};if(s.onVerifierComplete?.(t,g,b.decision?.passed??false,b.decision?.feedback),b.decision?.passed)return {...x,verify:{passed:true,reasoning:b.decision.reasoning,verifyMeta:C,attempts:g}};if(g<m){let S=b.decision?.feedback??b.decision?.reasoning??"Output did not meet criteria";f=c.buildRetryPromptWithFeedback(i,S);}}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,i){if(typeof e=="string")return e;try{return e(n,i)}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"]=R(e.schema)?L(e.schema):B(e.schema)),Object.entries(e.inputFiles).forEach(([i,a])=>{n[`worker_task/input/${i}`]=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,i){let a=i??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=[],i={}){this.swarm=e,this.steps=n,this.events=i;}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 i=`on${e.charAt(0).toUpperCase()}${e.slice(1)}`;return new c(this.swarm,this.steps,{...this.events,[i]:n})}return new c(this.swarm,this.steps,{...this.events,...e})}async run(e){let n=crypto.randomBytes(8).toString("hex"),i=[],a=e,s=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(i.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:i,output:d.output,totalDurationMs:Date.now()-s};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 o=i[i.length-1];return {pipelineRunId:n,steps:i,output:o?.results??[],totalDurationMs:Date.now()-s}}async executeStep(e,n,i,a,s){if(e.type==="map"){let r=e.config,p=await this.swarm.map({items:n,...r,_pipelineContext:s,retry:this.wrapRetry(r.retry,i,a),verify:this.wrapVerify(r.verify,i,a),bestOf:this.wrapBestOf(r.bestOf,i,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:s,retry:this.wrapRetry(r.retry,i,a),verify:this.wrapVerify(r.verify,i,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 o=e.config,l=await this.swarm.reduce({items:n,...o,_pipelineContext:s,retry:this.wrapRetry(o.retry,i,a),verify:this.wrapVerify(o.verify,i,a)});return {output:l,nextItems:[],successCount:l.status==="success"?1:0,errorCount:l.status==="error"?1:0,filteredCount:0}}wrapRetry(e,n,i){if(e)return {...e,onItemRetry:(a,s,o)=>{e.onItemRetry?.(a,s,o),this.events.onItemRetry?.({stepIndex:n,stepName:i,itemIndex:a,attempt:s,error:o});}}}wrapVerify(e,n,i){if(e)return {...e,onWorkerComplete:(a,s,o)=>{e.onWorkerComplete?.(a,s,o),this.events.onWorkerComplete?.({stepIndex:n,stepName:i,itemIndex:a,attempt:s,status:o});},onVerifierComplete:(a,s,o,l)=>{e.onVerifierComplete?.(a,s,o,l),this.events.onVerifierComplete?.({stepIndex:n,stepName:i,itemIndex:a,attempt:s,passed:o,feedback:l});}}}wrapBestOf(e,n,i){if(e)return {...e,onCandidateComplete:(a,s,o)=>{e.onCandidateComplete?.(a,s,o),this.events.onCandidateComplete?.({stepIndex:n,stepName:i,itemIndex:a,candidateIndex:s,status:o});},onJudgeComplete:(a,s,o)=>{e.onJudgeComplete?.(a,s,o),this.events.onJudgeComplete?.({stepIndex:n,stepName:i,itemIndex:a,winnerIndex:s,reasoning:o});}}}},ge=class c extends fe{constructor(e,n,i){super(e,n,i);}on(e,n){if(typeof e=="string"){let i=`on${e.charAt(0).toUpperCase()}${e.slice(1)}`;return new c(this.swarm,this.steps,{...this.events,[i]: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=G;exports.Evolve=H;exports.JUDGE_PROMPT=Pe;exports.Pipeline=fe;exports.RETRY_FEEDBACK_PROMPT=Oe;exports.SCHEMA_PROMPT=ut;exports.SWARM_RESULT_BRAND=A;exports.SYSTEM_PROMPT=pt;exports.Semaphore=N;exports.Swarm=$e;exports.SwarmResultList=j;exports.TerminalPipeline=ge;exports.VALIDATION_PRESETS=he;exports.VERIFY_PROMPT=Ee;exports.WORKSPACE_PROMPT=ct;exports.WORKSPACE_SWE_PROMPT=lt;exports.applyTemplate=E;exports.buildWorkerSystemPrompt=Ie;exports.createAgentParser=I;exports.createClaudeParser=ne;exports.createCodexParser=ie;exports.createGeminiParser=se;exports.executeWithRetry=_;exports.expandPath=ve;exports.getAgentConfig=O;exports.getMcpSettingsDir=U;exports.getMcpSettingsPath=$;exports.isValidAgentType=_t;exports.isZodSchema=R;exports.jsonSchemaToString=B;exports.parseNdjsonLine=It;exports.parseNdjsonOutput=Mt;exports.parseQwenOutput=Ne;exports.readLocalDir=qe;exports.saveLocalDir=Ze;exports.writeClaudeMcpConfig=Q;exports.writeCodexMcpConfig=te;exports.writeGeminiMcpConfig=X;exports.writeMcpConfig=Ce;exports.writeQwenMcpConfig=ee;exports.zodSchemaToJson=L;
|