@evolvingmachines/sdk 0.0.4 → 0.0.7
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/LICENSE +193 -16
- package/dist/index.cjs +39 -35
- package/dist/index.d.cts +11 -7
- package/dist/index.d.ts +11 -7
- package/dist/index.js +39 -35
- package/package.json +7 -2
package/dist/index.cjs
CHANGED
|
@@ -1,28 +1,32 @@
|
|
|
1
|
-
'use strict';var events=require('events'),
|
|
2
|
-
`),
|
|
3
|
-
`)].filter(
|
|
1
|
+
'use strict';var events=require('events'),Xt=require('ajv'),It=require('zod-to-json-schema'),R=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 Xt__default=/*#__PURE__*/_interopDefault(Xt);var It__default=/*#__PURE__*/_interopDefault(It);var R__namespace=/*#__PURE__*/_interopNamespace(R);var Y__namespace=/*#__PURE__*/_interopNamespace(Y);var St={CLAUDE:"claude",CODEX:"codex",GEMINI:"gemini",QWEN:"qwen"},ge={strict:{coerceTypes:false,removeAdditional:false,useDefaults:false,allErrors:true},loose:{coerceTypes:"array",removeAdditional:false,useDefaults:true,allErrors:true}};var he={claude:{image:"evolve-all",apiKeyEnv:"ANTHROPIC_API_KEY",oauthEnv:"CLAUDE_CODE_OAUTH_TOKEN",baseUrlEnv:"ANTHROPIC_BASE_URL",defaultModel:"opus",models:[{alias:"opus",modelId:"claude-opus-4-5-20251101",description:"Complex reasoning, R&D, architecting"},{alias:"sonnet",modelId:"claude-sonnet-4-5-20250929",description:"Daily coding, features, tests"},{alias:"haiku",modelId:"claude-haiku-4-5-20251001",description:"Quick tasks, syntax correction"}],systemPromptFile:"CLAUDE.md",mcpConfig:{settingsDir:"~/.claude",filename:"settings.json",format:"json",projectConfig:true},availableBetas:{CONTEXT_1M:"context-1m-2025-08-07"},skillsConfig:{sourceDir:"/home/user/.evolve/skills",targetDir:"/home/user/.claude/skills"},buildCommand:({prompt:c,model:e,isResume:n,betas:s})=>{let a=n?"--continue ":"",o=s?.length?`--betas ${s.join(",")} `:"";return `echo "${c}" | claude -p ${a}${o}--model ${e} --output-format stream-json --verbose --dangerously-skip-permissions`}},codex:{image:"evolve-all",apiKeyEnv:"OPENAI_API_KEY",baseUrlEnv:"OPENAI_BASE_URL",defaultModel:"gpt-5.2",models:[{alias:"gpt-5.2",modelId:"gpt-5.2",description:"Base model"},{alias:"gpt-5.2-codex",modelId:"gpt-5.2-codex",description:"Code-optimized"},{alias:"gpt-5.1-codex-max",modelId:"gpt-5.1-codex-max",description:"Best reasoning (xhigh effort)"},{alias:"gpt-5.1-mini",modelId:"gpt-5.1-mini",description:"Smaller/faster"}],systemPromptFile:"AGENTS.md",mcpConfig:{settingsDir:"~/.codex",filename:"config.toml",format:"toml"},skillsConfig:{sourceDir:"/home/user/.evolve/skills",targetDir:"/home/user/.codex/skills"},setupCommand:`printf '%s\\n' "$OPENAI_API_KEY" | codex login --with-api-key`,buildCommand:({prompt:c,model:e,isResume:n,reasoningEffort:s})=>{let a=s?` -c model_reasoning_effort="${s}"`:"";return `printf '%s' "${c}" | codex exec --model ${e}${a} --dangerously-bypass-approvals-and-sandbox --skip-git-repo-check --json${n?" resume --last":""}`}},gemini:{image:"evolve-all",apiKeyEnv:"GEMINI_API_KEY",baseUrlEnv:"GOOGLE_GEMINI_BASE_URL",defaultModel:"gemini-3-flash-preview",models:[{alias:"gemini-3-pro-preview",modelId:"gemini-3-pro-preview",description:"Latest pro preview"},{alias:"gemini-3-flash-preview",modelId:"gemini-3-flash-preview",description:"Latest flash preview"},{alias:"gemini-2.5-pro",modelId:"gemini-2.5-pro",description:"Complex tasks, deep reasoning"},{alias:"gemini-2.5-flash",modelId:"gemini-2.5-flash",description:"Balance speed/reasoning"},{alias:"gemini-2.5-flash-lite",modelId:"gemini-2.5-flash-lite",description:"Simple tasks, fastest"}],systemPromptFile:"GEMINI.md",mcpConfig:{settingsDir:"~/.gemini",filename:"settings.json",format:"json"},skillsConfig:{sourceDir:"/home/user/.evolve/skills",targetDir:"/home/user/.gemini/skills"},usePassthroughGateway:true,buildCommand:({prompt:c,model:e,isResume:n})=>`gemini "${c}" ${n?"--resume latest ":""}--model ${e} --yolo --output-format stream-json`},qwen:{image:"evolve-all",apiKeyEnv:"OPENAI_API_KEY",baseUrlEnv:"OPENAI_BASE_URL",defaultModel:"qwen3-coder-plus",models:[{alias:"qwen3-coder-plus",modelId:"qwen3-coder-plus",description:"Code generation, debugging"},{alias:"qwen3-vl-plus",modelId:"qwen3-vl-plus",description:"Vision + language, multimodal"}],systemPromptFile:"QWEN.md",mcpConfig:{settingsDir:"~/.qwen",filename:"settings.json",format:"json"},skillsConfig:{sourceDir:"/home/user/.evolve/skills",targetDir:"/home/user/.qwen/skills",enableFlag:"--experimental-skills"},defaultBaseUrl:"https://dashscope-intl.aliyuncs.com/compatible-mode/v1",buildCommand:({prompt:c,model:e,isResume:n,isDirectMode:s,skills:a})=>{let o=n?"--continue ":"",i=a?.length?"--experimental-skills ":"",l=s||e.startsWith("dashscope/")?e:`dashscope/${e}`;return `qwen "${c}" ${o}${i}--model ${l} --yolo --output-format stream-json`}}};function M(c){let e=he[c];if(!e)throw new Error(`Unknown agent type: ${c}`);return e}function kt(c){return c in he}function ye(c){return c.replace(/^~/,"/home/user")}function F(c){let e=M(c);return `${ye(e.mcpConfig.settingsDir)}/${e.mcpConfig.filename}`}function U(c){let e=M(c);return ye(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 ve(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))ve(e,n);}function be(c){return c.type?c.type:c.command?"stdio":"sse"}function Tt(c){let e=be(c);return {...c,type:e}}function _t(c){let{type:e,url:n,...s}=c;return be(c)==="http"&&n?{httpUrl:n,...s}:n?{url:n,...s}:s}function Pt(c){return {type:be(c),...c}}async function Be(c,e,n,s){xe(n);let a=U(e),o=F(e);await c.files.makeDir(a);let i={};try{let r=await c.files.read(o);typeof r=="string"&&(i=JSON.parse(r));}catch(r){if(!V(r))throw r}let l=Object.fromEntries(Object.entries(n).map(([r,p])=>[r,s(p)]));await c.files.write(o,JSON.stringify({...i,mcpServers:l},null,2));}async function Q(c,e,n){xe(n);let s=U("claude"),a=F("claude"),o=Object.fromEntries(Object.entries(n).map(([l,r])=>[l,Pt(r)]));await c.files.write(`${e}/.mcp.json`,JSON.stringify({mcpServers:o},null,2)),await c.files.makeDir(s);let i={};try{let l=await c.files.read(a);typeof l=="string"&&(i=JSON.parse(l));}catch(l){if(!V(l))throw l}i.enableAllProjectMcpServers=true,await c.files.write(a,JSON.stringify(i,null,2));}async function X(c,e){await Be(c,"gemini",e,Tt);}async function ee(c,e){await Be(c,"qwen",e,_t);}function _(c){return typeof c=="string"?`"${c.replace(/\\/g,"\\\\").replace(/"/g,'\\"')}"`:Array.isArray(c)?`[${c.map(e=>_(e)).join(", ")}]`:typeof c=="object"&&c!==null?`{ ${Object.entries(c).map(([n,s])=>`${n} = ${_(s)}`).join(", ")} }`:typeof c=="boolean"?c?"true":"false":String(c)}async function te(c,e){for(let[r,p]of Object.entries(e))ve(r,p);let n=U("codex"),s=F("codex");await c.files.makeDir(n);let a="";try{let r=await c.files.read(s);typeof r=="string"&&(a=r);}catch(r){if(!V(r))throw r}let o=[],i=[];a.includes("experimental_use_rmcp_client")||(o.push("# Enable improved RMCP client (recommended)"),o.push("experimental_use_rmcp_client = true")),a.includes("[mcp_servers]")||i.push("[mcp_servers]","");for(let[r,p]of Object.entries(e)){if(a.includes(`[mcp_servers.${r}]`))continue;i.push(`[mcp_servers.${r}]`);let t=p.type??(p.command?"stdio":"http");if(t==="http"||t==="sse"||!!p.url){if(!p.url)throw new Error(`MCP server "${r}" is missing url for ${t} transport`);i.push(`url = ${_(p.url)}`),p.bearerTokenEnvVar&&i.push(`bearer_token_env_var = ${_(p.bearerTokenEnvVar)}`);let d=p.httpHeaders??p.headers;d&&Object.keys(d).length>0&&i.push(`http_headers = ${_(d)}`),p.envHttpHeaders&&Object.keys(p.envHttpHeaders).length>0&&i.push(`env_http_headers = ${_(p.envHttpHeaders)}`);}else i.push(`command = ${_(p.command)}`),p.args&&p.args.length>0&&i.push(`args = ${_(p.args)}`),p.cwd&&i.push(`cwd = ${_(p.cwd)}`);p.env&&Object.keys(p.env).length>0&&i.push(`env = ${_(p.env)}`),p.envVars&&p.envVars.length>0&&i.push(`env_vars = ${_(p.envVars)}`),i.push("");}let l=[a.trim(),o.join(`
|
|
2
|
+
`),i.join(`
|
|
3
|
+
`)].filter(r=>r.length>0);await c.files.write(s,l.join(`
|
|
4
4
|
|
|
5
5
|
`)+`
|
|
6
|
-
`);}async function
|
|
7
|
-
`+
|
|
6
|
+
`);}async function we(c,e,n,s){if(!(!s||Object.keys(s).length===0))switch(c){case "claude":await Q(e,n,s);break;case "codex":await te(e,s);break;case "gemini":await X(e,s);break;case "qwen":await ee(e,s);break;default:throw new Error(`Unknown agent type for MCP config: ${c}`)}}function ne(){let c={};return function(p){let t;try{t=JSON.parse(p);}catch{return null}if(t._meta||t._prompt)return null;let u=t.session_id;switch(t.type){case "system":return null;case "assistant":return e(t.message?.content,"assistant",u);case "user":{let d=t.message?.content;return typeof d=="string"||Array.isArray(d)&&d.length===1&&d[0].type==="text"?null:e(d,"user",u)}case "result":return null;default:return null}};function e(r,p,t){if(typeof r=="string")return p==="user"?null:[{sessionId:t,update:{sessionUpdate:"agent_message_chunk",content:{type:"text",text:r}}}];if(!r||!Array.isArray(r))return null;let u=[];for(let d of r){let m=null;switch(d.type){case "text":case "text_delta":if(p==="user")break;m={sessionUpdate:"agent_message_chunk",content:{type:"text",text:d.text}};break;case "image":if(p==="user")break;m={sessionUpdate:"agent_message_chunk",content:{type:"image",data:d.source?.type==="base64"?d.source.data:"",mimeType:d.source?.type==="base64"?d.source.media_type:"",uri:d.source?.type==="url"?d.source.url:void 0}};break;case "thinking":case "thinking_delta":m={sessionUpdate:"agent_thought_chunk",content:{type:"text",text:d.thinking}};break;case "tool_use":case "server_tool_use":case "mcp_tool_use":{if(c[d.id]=d,d.name==="TodoWrite")Array.isArray(d.input?.todos)&&(m={sessionUpdate:"plan",entries:i(d.input)});else {let f=n(d);m={sessionUpdate:"tool_call",toolCallId:d.id,status:"pending",...f};}break}case "tool_result":case "tool_search_tool_result":case "web_fetch_tool_result":case "web_search_tool_result":case "code_execution_tool_result":case "bash_code_execution_tool_result":case "text_editor_code_execution_tool_result":case "mcp_tool_result":{let f=c[d.tool_use_id];if(!f||(delete c[d.tool_use_id],f.name==="TodoWrite"))break;m={sessionUpdate:"tool_call_update",toolCallId:d.tool_use_id,status:"is_error"in d&&d.is_error?"failed":"completed",...s(d,f)};break}}m&&u.push({sessionId:t,update:m});}return u.length>0?u:null}function n(r){let p=r.name,t=r.input||{};switch(p){case "Task":return {title:t.description||"Task",kind:"think",content:t.prompt?[{type:"content",content:{type:"text",text:t.prompt}}]:[],rawInput:t};case "NotebookRead":return {title:t.notebook_path?`Read Notebook ${t.notebook_path}`:"Read Notebook",kind:"read",content:[],locations:t.notebook_path?[{path:t.notebook_path}]:[],rawInput:t};case "NotebookEdit":return {title:t.notebook_path?`Edit Notebook ${t.notebook_path}`:"Edit Notebook",kind:"edit",content:t.new_source?[{type:"content",content:{type:"text",text:t.new_source}}]:[],locations:t.notebook_path?[{path:t.notebook_path}]:[],rawInput:t};case "Bash":return {title:t.command?"`"+t.command.replaceAll("`","\\`")+"`":"Terminal",kind:"execute",content:t.description?[{type:"content",content:{type:"text",text:t.description}}]:[],rawInput:t};case "BashOutput":return {title:"Tail Logs",kind:"execute",content:[],rawInput:t};case "KillShell":return {title:"Kill Process",kind:"execute",content:[],rawInput:t};case "Read":{let u="";return t.limit?u=" ("+((t.offset??0)+1)+" - "+((t.offset??0)+t.limit)+")":t.offset&&(u=" (from line "+(t.offset+1)+")"),{title:"Read "+(t.file_path??"File")+u,kind:"read",content:[],locations:t.file_path?[{path:t.file_path,line:t.offset??0}]:[],rawInput:t}}case "LS":return {title:t.path?`List \`${t.path}\` directory`:"List current directory",kind:"search",content:[],locations:[],rawInput:t};case "Edit":{let u=t.file_path;return {title:u?`Edit \`${u}\``:"Edit",kind:"edit",content:u?[{type:"diff",path:u,oldText:t.old_string??null,newText:t.new_string??""}]:[],locations:u?[{path:u}]:[],rawInput:t}}case "Write":return {title:t.file_path?`Write ${t.file_path}`:"Write",kind:"edit",content:t.file_path?[{type:"diff",path:t.file_path,oldText:null,newText:t.content??""}]:[],locations:t.file_path?[{path:t.file_path}]:[],rawInput:t};case "Glob":{let u="Find";return t.path&&(u+=` \`${t.path}\``),t.pattern&&(u+=` \`${t.pattern}\``),{title:u,kind:"search",content:[],locations:t.path?[{path:t.path}]:[],rawInput:t}}case "Grep":{let u="grep";return t["-i"]&&(u+=" -i"),t["-n"]&&(u+=" -n"),t["-A"]!==void 0&&(u+=` -A ${t["-A"]}`),t["-B"]!==void 0&&(u+=` -B ${t["-B"]}`),t["-C"]!==void 0&&(u+=` -C ${t["-C"]}`),t.output_mode==="files_with_matches"?u+=" -l":t.output_mode==="count"&&(u+=" -c"),t.head_limit!==void 0&&(u+=` | head -${t.head_limit}`),t.glob&&(u+=` --include="${t.glob}"`),t.type&&(u+=` --type=${t.type}`),t.multiline&&(u+=" -P"),u+=` "${t.pattern??""}"`,t.path&&(u+=` ${t.path}`),{title:u,kind:"search",content:[],rawInput:t}}case "WebFetch":return {title:t.url?`Fetch ${t.url}`:"Fetch",kind:"fetch",content:t.prompt?[{type:"content",content:{type:"text",text:t.prompt}}]:[],rawInput:t};case "WebSearch":{let u=`"${t.query??""}"`;return t.allowed_domains?.length>0&&(u+=` (allowed: ${t.allowed_domains.join(", ")})`),t.blocked_domains?.length>0&&(u+=` (blocked: ${t.blocked_domains.join(", ")})`),{title:u,kind:"fetch",content:[],rawInput:t}}case "TodoWrite":return {title:Array.isArray(t.todos)?`Update TODOs: ${t.todos.map(u=>u.content).join(", ")}`:"Update TODOs",kind:"think",content:[],rawInput:t};case "ExitPlanMode":return {title:"Ready to code?",kind:"switch_mode",content:t.plan?[{type:"content",content:{type:"text",text:t.plan}}]:[],rawInput:t};default:if(p?.startsWith("mcp__")){let u=p.split("__"),d=u[1]||"mcp",m=u.slice(2).join("__")||p;return {title:`${d}: ${m}`,kind:"other",content:[],rawInput:t}}return {title:p||"Unknown Tool",kind:"other",content:[],rawInput:t}}}function s(r,p){let t=r.content;switch(p?.name){case "Read":return Array.isArray(t)&&t.length>0?{content:t.map(u=>({type:"content",content:u.type==="text"?{type:"text",text:l(u.text)}:o(u,false)}))}:typeof t=="string"&&t.length>0?{content:[{type:"content",content:{type:"text",text:l(t)}}]}:{};case "Edit":case "Write":case "Bash":return r.is_error&&t?a(t,true):{};case "ExitPlanMode":return {title:"Exited Plan Mode"};default:return a(t,"is_error"in r?r.is_error:false)}}function a(r,p=false){return Array.isArray(r)&&r.length>0?{content:r.map(t=>({type:"content",content:o(t,p)}))}:typeof r=="string"&&r.length>0?{content:[{type:"content",content:{type:"text",text:p?"```\n"+r+"\n```":r}}]}:{}}function o(r,p){if(r.type==="text")return p?{type:"text",text:"```\n"+r.text+"\n```"}:{type:"text",text:r.text};if(r.type==="image"){if(r.data&&r.mimeType)return {type:"image",data:r.data,mimeType:r.mimeType};if(r.source?.type==="base64")return {type:"image",data:r.source.data||"",mimeType:r.source.media_type||""};if(r.source?.type==="url")return {type:"image",data:"",mimeType:"",uri:r.source.url}}return r}function i(r){return r.todos.map(p=>({content:p.content,status:p.status||"pending",priority:"medium"}))}function l(r){let p="```",t=Array.from(r.matchAll(/^```+/gm));for(let[u]of t)for(;u.length>=p.length;)p+="`";return p+`
|
|
7
|
+
`+r+(r.endsWith(`
|
|
8
8
|
`)?"":`
|
|
9
|
-
`)+p}}function se(){return function(
|
|
9
|
+
`)+p}}function se(){return function(r){let p;try{p=JSON.parse(r);}catch{return null}if(p._meta||p._prompt)return null;let t=[];switch(p.type){case "thread.started":case "turn.started":case "turn.completed":return null;case "item.started":{let u=p.item;if(!u)return null;let d=e(u);d&&t.push({update:d});break}case "item.updated":{let u=p.item;if(!u)return null;let d=n(u);d&&t.push({update:d});break}case "item.completed":{let u=p.item;if(!u)return null;let d=s(u);d&&t.push({update:d});break}default:return null}return t.length>0?t:null};function e(l){let r=l.id;switch(l.type){case "mcp_tool_call":return {type:"mcp_tool_call",name:`${l.server}:${l.tool}`},{sessionUpdate:"tool_call",toolCallId:r,title:`${l.server}: ${l.tool}`,kind:"other",status:"in_progress",rawInput:l.arguments,content:[]};case "command_execution":return {type:"command_execution",name:l.command},{sessionUpdate:"tool_call",toolCallId:r,title:l.command?`\`${l.command}\``:"Execute Command",kind:"execute",status:"in_progress",rawInput:{command:l.command},content:[]};case "web_search":return {type:"web_search",name:l.query},{sessionUpdate:"tool_call",toolCallId:r,title:`Search: ${l.query??""}`,kind:"fetch",status:"in_progress",content:[]};case "todo_list":return {sessionUpdate:"plan",entries:i(l.items)};default:return null}}function n(l){return l.type==="todo_list"?{sessionUpdate:"plan",entries:i(l.items)}:null}function s(l){let r=l.id;switch(l.type){case "reasoning":return {sessionUpdate:"agent_thought_chunk",content:{type:"text",text:l.text??l.summary?.join(`
|
|
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=
|
|
12
|
-
${
|
|
13
|
-
\`\`\``:
|
|
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:i(l.items)};default:return null}}function a(l){return l?.content?Array.isArray(l.content)?l.content.map(r=>({type:"content",content:o(r)})):typeof l.content=="string"?[{type:"content",content:{type:"text",text:l.content}}]:[]:[]}function o(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 i(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 Rt={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 oe(){return function(i){let l;try{l=JSON.parse(i);}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=s(l);t&&p.push({sessionId:r,update:t});break}default:return null}return p.length>0?p:null};function c(o){let i=o.role,l=o.content;return l?i==="assistant"?{sessionUpdate:"agent_message_chunk",content:{type:"text",text:l}}:i==="user"?{sessionUpdate:"user_message_chunk",content:{type:"text",text:l}}:null:null}function e(o){let i=o.tool_id,l=o.tool_name,r=o.parameters||{},{title:p,kind:t,content:u,locations:d}=a(l,r);return {sessionUpdate:"tool_call",toolCallId:i,title:p,kind:t,status:"pending",rawInput:r,content:u,locations:d}}function n(o){let i=o.tool_id,l=o.status,r=o.output,p=o.error,t=[];return r&&typeof r=="string"&&r.length>0&&t.push({type:"content",content:{type:"text",text:l==="error"?`\`\`\`
|
|
12
|
+
${r}
|
|
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:i,status:l==="success"?"completed":"failed",content:t}}function s(o){let i=o.severity,l=o.message;return l?{sessionUpdate:"agent_message_chunk",content:{type:"text",text:`${i==="warning"?"\u26A0\uFE0F Warning":"\u274C Error"}: ${l}`}}:null}function a(o,i){let l=Rt[o]||"other",r=[],p=[],t=o;switch(o){case "read_file":t=`Read ${i.absolute_path||i.file_path||"file"}`,(i.absolute_path||i.file_path)&&p.push({path:i.absolute_path||i.file_path});break;case "write_file":t=`Write ${i.file_path||"file"}`,i.file_path&&(p.push({path:i.file_path}),r.push({type:"diff",path:i.file_path,oldText:null,newText:i.content||""}));break;case "edit_file":t=`Edit ${i.file_path||"file"}`,i.file_path&&p.push({path:i.file_path});break;case "replace":t=`Edit ${i.file_path||"file"}`,i.file_path&&(p.push({path:i.file_path}),(i.old_string!==void 0||i.new_string!==void 0)&&r.push({type:"diff",path:i.file_path,oldText:i.old_string||"",newText:i.new_string||""}));break;case "run_shell_command":case "shell":t=i.command?`\`${i.command}\``:"Run command",i.description&&r.push({type:"content",content:{type:"text",text:i.description}});break;case "brave_web_search":case "web_search":case "google_web_search":t=i.query?`"${i.query}"`:"Web search";break;case "web_fetch":t=i.prompt?`Fetch: ${i.prompt.substring(0,50)}...`:"Web fetch";break;case "glob":t=`Find ${i.pattern||"files"}`,i.dir_path&&p.push({path:i.dir_path});break;case "grep":case "search_file_content":t=`grep "${i.pattern||i.query||""}"`;break;case "list_directory":t=`List ${i.dir_path||i.path||"directory"}`,(i.dir_path||i.path)&&p.push({path:i.dir_path||i.path});break;case "read_many_files":t=`Read ${i.include?.length||"multiple"} files`,Array.isArray(i.include)&&i.include.forEach(u=>p.push({path:u}));break;case "delegate_to_agent":t=i.agent_name?`Agent: ${i.agent_name}`:"Delegate to agent";break;case "write_todos":t="Update todos";break;case "save_memory":t=i.fact?`Remember: "${i.fact.substring(0,40)}..."`:"Save memory";break;case "activate_skill":t=i.name?`Activate skill: ${i.name}`:"Activate skill";break;default:t=o;}return {title:t,kind:l,content:r,locations:p}}}var Et={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 ie(){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=s(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=o(d);t.push(...m);}return t}function s(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=i(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 o(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=i(p);return t?[t]:[]}case "tool_result":{let t=l(p);return t?[t]:[]}}}function i(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
|
|
20
|
-
`),
|
|
21
|
-
Try reinstalling: npm install @evolvingmachines/sdk`):
|
|
22
|
-
Try
|
|
23
|
-
|
|
19
|
+
\`\`\``:h}});}}return {sessionUpdate:"tool_call_update",toolCallId:t,status:u?"failed":"completed",content:m}}function r(p,t){let u=Et[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 ie()(c)}function O(c){switch(c){case "claude":return ne();case "codex":return se();case "gemini":return oe();case "qwen":return ie();default:return ()=>null}}function Mt(c,e){let n=e.trim();return n?O(c)(n):null}function Ot(c,e){let n=[],s=e.split(`
|
|
20
|
+
`),a=O(c);for(let o of s){let i=o.trim();if(!i)continue;let l=a(i);l&&n.push(...l);}return n}function K(){return process.env.EVOLVE_GATEWAY_URL||"https://swarmkit-gateway-692833842999.us-central1.run.app"}function Le(){return `${K()}`}function je(){return `${K()}/e2b`}function Ve(c){return {"browser-use":{type:"http",url:`${K()}/browser_use/mcp`,headers:{"x-litellm-api-key":`Bearer ${c}`}}}}var I="EVOLVE_API_KEY",re="E2B_API_KEY",ae="DAYTONA_API_KEY",ce="MODAL_TOKEN_ID",Ce="MODAL_TOKEN_SECRET",Ke="claude",W=36e5,We=4,Se=100,Je="/home/user/workspace",J="https://dashboard.evolvingmachines.ai",Ye=".evolve-sdk/observability/sessions",Ge=100,ze=5e3,le=3,ke=1e3;function P(c){return c!==null&&typeof c=="object"&&"safeParse"in c&&typeof c.safeParse=="function"}function B(c){return JSON.stringify(It__default.default(c,{target:"jsonSchema7"}),null,2)}function N(c){return JSON.stringify(c,null,2)}function qe(c,e=false){let n={};function s(a,o){for(let i of R__namespace.readdirSync(a)){let l=Y__namespace.join(a,i),r=o?`${o}/${i}`:i;R__namespace.statSync(l).isDirectory()?e&&s(l,r):n[r]=R__namespace.readFileSync(l);}}return s(c,""),n}function He(c,e){for(let[n,s]of Object.entries(e)){let a=Y__namespace.join(c,n),o=Y__namespace.dirname(a);R__namespace.mkdirSync(o,{recursive:true});let i;s instanceof ArrayBuffer||s instanceof Uint8Array?i=Buffer.from(s):i=s,R__namespace.writeFileSync(a,i);}}function Te(c){let e=c?.type??Ke,n=M(e);if(c?.oauthToken){if(e!=="claude")throw new Error(`oauthToken is only supported for claude agent (Claude Max subscription), not ${e}. Use providerApiKey for ${e} instead.`);return {type:e,apiKey:c.oauthToken,isDirectMode:true,isOAuth:true,model:c.model,reasoningEffort:c.reasoningEffort,betas:c.betas}}if(c?.providerApiKey){let i=c.providerBaseUrl??process.env[n.baseUrlEnv]??n.defaultBaseUrl;return {type:e,apiKey:c.providerApiKey,baseUrl:i,isDirectMode:true,model:c.model,reasoningEffort:c.reasoningEffort,betas:c.betas}}if(c?.apiKey)return {type:e,apiKey:c.apiKey,isDirectMode:false,model:c.model,reasoningEffort:c.reasoningEffort,betas:c.betas};let s=process.env[I];if(s)return {type:e,apiKey:s,isDirectMode:false,model:c?.model,reasoningEffort:c?.reasoningEffort,betas:c?.betas};let a=process.env[n.apiKeyEnv];if(a){let i=process.env[n.baseUrlEnv]??n.defaultBaseUrl;return {type:e,apiKey:a,baseUrl:i,isDirectMode:true,model:c?.model,reasoningEffort:c?.reasoningEffort,betas:c?.betas}}if(n.oauthEnv){let i=process.env[n.oauthEnv];if(i)return {type:e,apiKey:i,isDirectMode:true,isOAuth:true,model:c?.model,reasoningEffort:c?.reasoningEffort,betas:c?.betas}}let o=n.oauthEnv?`, oauthToken (Claude Max), or ${n.oauthEnv}`:"";throw new Error(`No API key found for ${e}. Set apiKey (gateway), providerApiKey (direct)${o}, or ${I} / ${n.apiKeyEnv} env var.`)}async function _e(){let c=process.env[re];if(c)try{let{createE2BProvider:o}=await import('@evolvingmachines/e2b');return o({apiKey:c})}catch(o){let i=o;throw i.message?.includes("Cannot find module")||i.message?.includes("MODULE_NOT_FOUND")?new Error(`${re} is set but @evolvingmachines/e2b failed to load.
|
|
21
|
+
Try reinstalling: npm install @evolvingmachines/sdk`):i}let e=process.env[ae];if(e)try{let{createDaytonaProvider:o}=await import('@evolvingmachines/daytona');return o({apiKey:e})}catch(o){let i=o;throw i.message?.includes("Cannot find module")||i.message?.includes("MODULE_NOT_FOUND")?new Error(`${ae} is set but @evolvingmachines/daytona failed to load.
|
|
22
|
+
Try installing: npm install @evolvingmachines/daytona`):i}let n=process.env[ce],s=process.env[Ce];if(n&&s)try{let{createModalProvider:o}=await import('@evolvingmachines/modal');return o()}catch(o){let i=o;throw i.message?.includes("Cannot find module")||i.message?.includes("MODULE_NOT_FOUND")?new Error(`${ce} is set but @evolvingmachines/modal failed to load.
|
|
23
|
+
Try installing: npm install @evolvingmachines/modal`):i}let a=process.env[I];if(a)try{let{createE2BProvider:o}=await import('@evolvingmachines/e2b');return process.env.E2B_API_URL=je(),o({apiKey:a})}catch(o){let i=o;throw i.message?.includes("Cannot find module")||i.message?.includes("MODULE_NOT_FOUND")?new Error(`${I} is set but @evolvingmachines/e2b failed to load.
|
|
24
|
+
Try reinstalling: npm install @evolvingmachines/sdk`):i}throw new Error(`No sandbox provider configured. Either:
|
|
25
|
+
1. Set ${I} environment variable (recommended, get key at https://dashboard.evolvingmachines.ai)
|
|
24
26
|
2. Set ${re} environment variable (direct E2B access, get key at https://e2b.dev)
|
|
25
|
-
3.
|
|
27
|
+
3. Set ${ae} environment variable (direct Daytona access, get key at https://app.daytona.io)
|
|
28
|
+
4. Set ${ce} and ${Ce} environment variables (direct Modal access, get tokens at https://modal.com/settings/tokens)
|
|
29
|
+
5. Pass sandbox explicitly: .withSandbox(provider)`)}function At(c){return new Promise(e=>setTimeout(e,c))}async function T(c,e,n=0){let s=Dt(e),a=e?.onItemRetry,o=null,i=0,l=s.backoffMs;for(;i<s.maxAttempts;){if(i++,o=await c(i),!s.retryOn(o))return o;if(i>=s.maxAttempts)break;if(a){let r=o.error??"Unknown error";a(n,i,r);}await At(l),l*=s.backoffMultiplier;}return o}function Dt(c){return {maxAttempts:c?.maxAttempts??3,backoffMs:c?.backoffMs??1e3,backoffMultiplier:c?.backoffMultiplier??2,retryOn:c?.retryOn??(e=>e.status==="error")}}var Ze=`## FILESYSTEM INSTRUCTIONS:
|
|
26
30
|
|
|
27
31
|
You are running in a sandbox environment.
|
|
28
32
|
|
|
@@ -48,7 +52,7 @@ IMPORTANT - Directory structure:
|
|
|
48
52
|
- The user might be building a long-running agent which could be interrupted and resumed later.
|
|
49
53
|
- The user might pass the outputs to another agent for verification or further processing.
|
|
50
54
|
- etc.
|
|
51
|
-
`;var
|
|
55
|
+
`;var Qe=`## FILESYSTEM INSTRUCTIONS:
|
|
52
56
|
|
|
53
57
|
You are running in a sandbox environment.
|
|
54
58
|
|
|
@@ -75,9 +79,9 @@ IMPORTANT - Directory structure:
|
|
|
75
79
|
- The user might be building a long-running agent which could be interrupted and resumed later.
|
|
76
80
|
- The user might pass the outputs to another agent for verification or further processing.
|
|
77
81
|
- etc.
|
|
78
|
-
`;var
|
|
82
|
+
`;var Xe=`## SYSTEM PROMPT
|
|
79
83
|
|
|
80
|
-
{{systemPrompt}}`;var
|
|
84
|
+
{{systemPrompt}}`;var et=`## STRUCTURED OUTPUT
|
|
81
85
|
|
|
82
86
|
Your final result MUST be saved to \`output/result.json\` following this schema:
|
|
83
87
|
|
|
@@ -95,7 +99,7 @@ You are free to:
|
|
|
95
99
|
But your final \`output/result.json\` MUST conform to the schema above.
|
|
96
100
|
|
|
97
101
|
### OUTPUT RESULTS (DELIVERABLES) MUST BE WRITTEN to \`output/result.json\` as files.
|
|
98
|
-
### Never just state results as text.`;var
|
|
102
|
+
### Never just state results as text.`;var tt=`### 1. YOUR ROLE: BEST OF N JUDGE
|
|
99
103
|
|
|
100
104
|
You are a judge. {{candidateCount}} AI workers attempted the same task independently. Your job is to analyze their solution attempts and pick the best one based on the evaluation criteria below.
|
|
101
105
|
|
|
@@ -125,7 +129,7 @@ You must judge their work based on:
|
|
|
125
129
|
5. Pick the best candidate (0-indexed)
|
|
126
130
|
|
|
127
131
|
**IMPORTANT:** Be thorough. Do not skip steps. Your judgment must be evidence-based \u2014 cite specific files, outputs, or discrepancies to justify your decision.
|
|
128
|
-
`;var
|
|
132
|
+
`;var nt=`### 1. YOUR ROLE: OUTPUT VERIFIER
|
|
129
133
|
|
|
130
134
|
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.
|
|
131
135
|
|
|
@@ -158,14 +162,14 @@ The output must satisfy:
|
|
|
158
162
|
**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.
|
|
159
163
|
|
|
160
164
|
If failing, provide specific, actionable feedback explaining what needs to be fixed.
|
|
161
|
-
`;var
|
|
165
|
+
`;var st=`### CONTEXT STRUCTURE
|
|
162
166
|
|
|
163
167
|
\`\`\`
|
|
164
168
|
{{fileTree}}
|
|
165
169
|
\`\`\`
|
|
166
170
|
|
|
167
171
|
Examine all files within each item folder before producing your output.
|
|
168
|
-
`;var
|
|
172
|
+
`;var ot="Evaluate the candidates and select the best one. You MUST save your decision to the file `output/result.json`.\n";var it="Verify the worker output against the criteria. You MUST save your decision to the file `output/result.json`.\n";var rt=`{{originalPrompt}}
|
|
169
173
|
|
|
170
174
|
## IMPORTANT: Previous Attempt Failed Verification
|
|
171
175
|
|
|
@@ -174,24 +178,24 @@ Your previous attempt did not pass quality verification. Please address this fee
|
|
|
174
178
|
{{feedback}}
|
|
175
179
|
|
|
176
180
|
Make sure your output addresses all the feedback points above.
|
|
177
|
-
`;var
|
|
181
|
+
`;var at=Ze,ct=Qe,lt=Xe,pt=et,Pe=tt,ut=ot,Re=nt,dt=it,mt=st,Ee=rt;function E(c,e){let n=c;for(let[s,a]of Object.entries(e))n=n.replace(new RegExp(`\\{\\{${s}\\}\\}`,"g"),a);return n}function Me(c){let e=c.mode==="swe"?ct:at,n=E(e,{workingDir:c.workingDir}).trim();if(c.systemPrompt&&(n+=`
|
|
178
182
|
|
|
179
183
|
|
|
180
|
-
${E(
|
|
184
|
+
${E(lt,{systemPrompt:c.systemPrompt}).trim()}`),c.schema){let s=P(c.schema)?B(c.schema):N(c.schema);n+=`
|
|
181
185
|
|
|
182
186
|
|
|
183
|
-
${E(
|
|
184
|
-
(empty)`;let n="worker_task/system_prompt.txt"in c,s="worker_task/schema.json"in c,
|
|
185
|
-
`)}var
|
|
187
|
+
${E(pt,{schema:s}).trim()}`;}return n}function pe(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/
|
|
188
|
+
(empty)`;let n="worker_task/system_prompt.txt"in c,s="worker_task/schema.json"in c,a=Object.keys(c).some(r=>r.startsWith("worker_task/input/")),o=[];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"){o.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"}),s&&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 ";o.push({line:`${d}${y}${f.name}`,comment:f.comment});});}else if(r.startsWith("candidate_")){let m=r.replace("candidate_","");o.push({line:`${u}${r}/`,comment:`worker ${m} solution`});}else if(r==="worker_output")o.push({line:`${u}${r}/`,comment:"output to verify"});else if(r.startsWith("item_")){let m=r.replace("item_","");o.push({line:`${u}${r}/`,comment:`input ${m}`});}else o.push({line:`${u}${r}/`,comment:""});});let i=Math.max(...o.map(r=>r.line.length)),l=["context/"];for(let r of o)if(r.comment){let p=" ".repeat(i-r.line.length+3);l.push(`${r.line}${p}# ${r.comment}`);}else l.push(r.line);return l.join(`
|
|
189
|
+
`)}var ft=Y.join(os.homedir(),Ye),G=class{tag;timestamp;provider;agent;model;sandboxId;apiKey;dashboardUrl;localFilePath;observability;parser;isClosed=false;metaWritten=false;localQueue=Promise.resolve();dirReady=promises.mkdir(ft,{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 s=this.timestamp.replace(/[:.]/g,"-"),a=`${this.tag}_${this.provider}_${this.sandboxId}_${this.agent}_${s}.jsonl`;this.localFilePath=Y.join(ft,a),this.parser=O(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 s of n)this.bufferForDashboard(s);}writeEvent(e){if(!this.isClosed&&(this.writeLocalLine(e),this.apiKey)){let n=this.parser(e);if(n)for(let s of n)this.bufferForDashboard(s);}}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(`
|
|
186
190
|
`)?e:e+`
|
|
187
191
|
`;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)+`
|
|
188
192
|
`;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)+`
|
|
189
|
-
`;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>=
|
|
190
|
-
`);
|
|
191
|
-
`),g&&n?.onContent)for(let y of g)n.onContent(y);}},u=m=>{n?.onStderr?.(m);};if(o){let m=await
|
|
192
|
-
`),m&&n?.onContent)for(let f of m)n.onContent(f);}return this.hasRun=true,{sandboxId:
|
|
193
|
-
`).filter(Boolean),l=this.lastRunTimestamp?Math.floor(this.lastRunTimestamp/1e3)-2:0,
|
|
193
|
+
`;this.localQueue=this.localQueue.then(()=>this.appendLocal(n)).catch(()=>{}),this.apiKey&&this.eventBuffer.push(e),this.metaWritten=true;}async appendLocal(e){try{await this.dirReady,await promises.appendFile(this.localFilePath,e,"utf-8");}catch(n){console.debug("[SessionLogger] Local write failed:",n);}}scheduleFlush(){if(this.eventBuffer.length>=Ge){this.flushDashboard();return}!this.flushTimer&&!this.isClosed&&(this.flushTimer=setTimeout(()=>{this.flushTimer=void 0,this.flushDashboard();},ze));}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(ke*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(ke*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 ue="COMPOSIO_API_KEY";function ht(c){return new core.Composio({apiKey:process.env[ue]})}function Oe(c){if(!c?.trim())throw new Error("userId is required and cannot be empty")}async function de(c,e,n){Oe(c);let s=ht(),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 yt(c,e,n,s){Oe(c);let a=ht(),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 Ie(c,e){if(Oe(c),e?.keys)for(let[s,a]of Object.entries(e.keys))try{await yt(c,s,a);}catch(o){throw new Error(`Failed to create API key connection for "${s}": ${o.message}`)}let n=await de(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 vt(c){if(!c?.trim())throw new Error("userId is required and cannot be empty")}function Qt(){return new core.Composio({apiKey:process.env[ue]})}async function xt(c,e){vt(c);let s=await(await de(c,{manageConnections:false})).authorize(e);return {url:s.redirectUrl,connectionId:s.id}}async function bt(c,e){let n=await Ae(c),s={};for(let a of n)s[a.toolkit]=a.connected;return e?s[e]??false:s}async function Ae(c){return vt(c),(await Qt().connectedAccounts.list({userIds:[c],statuses:["ACTIVE"]})).items.map(s=>({toolkit:s.toolkit.slug,connected:s.status==="ACTIVE",accountId:s.id}))}var De={auth:xt,status:bt,connections:Ae};function en(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||Je,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={...ge[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 Xt__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({image:this.registry.image,envs:n,workingDirectory:this.workingDir}),await this.setupAgentAuth(this.sandbox),await this.setupWorkspace(this.sandbox);}return this.sandbox}buildEnvironmentVariables(){let e={},n=this.agentConfig.isOAuth&&this.registry.oauthEnv?this.registry.oauthEnv:this.registry.apiKeyEnv;return e[n]=this.agentConfig.apiKey,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){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=Me({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 Ie(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;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:en(e),model:this.agentConfig.model||this.registry.defaultModel,isResume:this.hasRun,reasoningEffort:this.agentConfig.reasoningEffort,betas:this.agentConfig.betas,isDirectMode:this.agentConfig.isDirectMode,skills:this.skills})}async run(e,n){let{prompt:s,timeoutMs:a=W,background:o=false}=e,i=await this.getSandbox();if(this.lastRunTimestamp=Date.now(),!this.sessionLogger){let m=this.options.sandboxProvider;this.sessionLogger=new G({provider:m?.name||m?.providerType||"unknown",agent:this.agentConfig.type,model:this.agentConfig.model||this.registry.defaultModel,sandboxId:i.sandboxId,tagPrefix:this.options.sessionTagPrefix,apiKey:this.agentConfig.isDirectMode?void 0:this.agentConfig.apiKey,observability:this.options.observability});}this.sessionLogger.writePrompt(s);let l=this.buildCommand(s),r="",p=O(this.agentConfig.type),t=m=>{r+=m;let f=r.split(`
|
|
194
|
+
`);r=f.pop()??"";for(let h of f){if(!h.trim())continue;let g=p(h);if(this.sessionLogger?.writeEventParsed(h,g),n?.onStdout?.(h+`
|
|
195
|
+
`),g&&n?.onContent)for(let y of g)n.onContent(y);}},u=m=>{n?.onStderr?.(m);};if(o){let m=await i.commands.spawn(l,{cwd:this.workingDir,timeoutMs:a,onStdout:t,onStderr:u});return this.hasRun=true,{sandboxId:i.sandboxId,exitCode:0,stdout:`Background process started with ID ${m.processId}`,stderr:""}}let d=await i.commands.run(l,{cwd:this.workingDir,timeoutMs:a,onStdout:t,onStderr:u});if(r.trim()){let m=p(r);if(this.sessionLogger?.writeEventParsed(r,m),n?.onStdout?.(r+`
|
|
196
|
+
`),m&&n?.onContent)for(let f of m)n.onContent(f);}return this.hasRun=true,{sandboxId:i.sandboxId,exitCode:d.exitCode,stdout:d.stdout,stderr:d.stderr}}async executeCommand(e,n={},s){let{timeoutMs:a=W,background:o=false}=n,i=await this.getSandbox();this.lastRunTimestamp=Date.now();let l="",r="",p=d=>{l+=d,s?.onStdout?.(d);},t=d=>{r+=d,s?.onStderr?.(d);};if(o){let d=await i.commands.spawn(e,{cwd:this.workingDir,timeoutMs:a,onStdout:p,onStderr:t});return {sandboxId:i.sandboxId,exitCode:0,stdout:`Background process started with ID ${d.processId}`,stderr:""}}let u=await i.commands.run(e,{cwd:this.workingDir,timeoutMs:a,onStdout:p,onStderr:t});return {sandboxId:i.sandboxId,exitCode:u.exitCode,stdout:l,stderr:r}}async uploadContext(e){let n=await this.getSandbox();await this.uploadContextFiles(n,e);}async uploadFiles(e){let n=await this.getSandbox();await this.uploadWorkspaceFiles(n,e);}async getOutputFiles(e=false){let n=await this.getSandbox(),s=`${this.workingDir}/output`,a=e?"":"-maxdepth 1",i=(await n.commands.run(`find ${s} ${a} -type f -exec stat -c '%n|%Z' {} \\; 2>/dev/null || true`,{timeoutMs:3e4})).stdout.split(`
|
|
197
|
+
`).filter(Boolean),l=this.lastRunTimestamp?Math.floor(this.lastRunTimestamp/1e3)-2:0,r=[],p=`${s}/`;for(let f of i){let[h,g]=f.split("|");if(!h||!g)continue;let y=parseInt(g,10);l>0&&y<l||r.push(h);}let t={},u=await Promise.all(r.map(async f=>{try{let h=await n.files.read(f);return {relativePath:f.startsWith(p)?f.slice(p.length):f.split("/").pop()||f,content:h}}catch{return null}}));for(let f of u)f&&(t[f.relativePath]=f.content);if(!this.zodSchema&&!this.jsonSchema)return {files:t,data:null};let d=t["result.json"];if(!d)return {files:t,data:null,error:"Schema provided but agent did not create output/result.json"};let m=typeof d=="string"?d:new TextDecoder().decode(d);try{let f=JSON.parse(m);if(this.zodSchema){let h=this.zodSchema.safeParse(f);return h.success?{files:t,data:h.data}:{files:t,data:null,error:`Schema validation failed: ${h.error.message}`,rawData:m}}if(this.compiledValidator){if(this.compiledValidator(f))return {files:t,data:f};{let g=this.compiledValidator.errors?.map(y=>`${y.instancePath} ${y.message}`).join(", ")||"Unknown validation error";return {files:t,data:null,error:`Schema validation failed: ${g}`,rawData:m}}}return {files:t,data:null}}catch(f){return {files:t,data:null,error:`Failed to parse result.json: ${f.message}`,rawData:m}}}getSession(){return this.sandbox?.sandboxId||this.options.sandboxId||null}async setSession(e){this.sessionLogger&&(await this.sessionLogger.close(),this.sessionLogger=void 0),this.options.sandboxId=e,this.sandbox=void 0,this.hasRun=true;}async pause(){this.sandbox&&await this.sandbox.pause();}async resume(){this.sandbox&&this.options.sandboxProvider&&(this.sandbox=await this.options.sandboxProvider.connect(this.sandbox.sandboxId));}async kill(){this.sessionLogger&&(await this.sessionLogger.close(),this.sessionLogger=void 0),this.sandbox&&(await this.sandbox.kill(),this.sandbox=void 0,this.hasRun=false);}async getHost(e){return (await this.getSandbox()).getHost(e)}getAgentType(){return this.agentConfig.type}getSessionTag(){return this.sessionLogger?.getTag()||null}getSessionTimestamp(){return this.sessionLogger?.getTimestamp()||null}};var q=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=De;async initializeAgent(){let e=Te(this.config.agent),n=this.config.sandbox??await _e(),s=e.isDirectMode?{}:Ve(e.apiKey),a={sandboxProvider:n,secrets:this.config.secrets,sandboxId:this.config.sandboxId,workingDirectory:this.config.workingDirectory,workspaceMode:this.config.workspaceMode,systemPrompt:this.config.systemPrompt,context:this.config.context,files:this.config.files,mcpServers:{...s,...this.config.mcpServers},skills:this.config.skills,schema:this.config.schema,schemaOptions:this.config.schemaOptions,sessionTagPrefix:this.config.sessionTagPrefix,observability:this.config.observability,composio:this.config.composio};this.agent=new z(e,a);}createStreamCallbacks(){let e=this.listenerCount("stdout")>0,n=this.listenerCount("stderr")>0,s=this.listenerCount("content")>0;return {onStdout:e?a=>this.emit("stdout",a):void 0,onStderr:n?a=>this.emit("stderr",a):void 0,onContent:s?a=>this.emit("content",a):void 0}}async run({prompt:e,timeoutMs:n,background:s}){this.agent||await this.initializeAgent();let a=this.createStreamCallbacks();return this.agent.run({prompt:e,timeoutMs:n,background:s},a)}async executeCommand(e,n={}){this.agent||await this.initializeAgent();let s=this.createStreamCallbacks();return this.agent.executeCommand(e,n,s)}async uploadContext(e){return this.agent||await this.initializeAgent(),this.agent.uploadContext(e)}async uploadFiles(e){return this.agent||await this.initializeAgent(),this.agent.uploadFiles(e)}async getOutputFiles(e=false){if(!this.agent)throw new Error("Agent not initialized. Call run() first.");return this.agent.getOutputFiles(e)}getSession(){return this.agent?this.agent.getSession():this.config.sandboxId??null}async setSession(e){this.agent&&await this.agent.setSession(e),this.config.sandboxId=e;}async pause(){if(this.agent)return this.agent.pause();this.config.sandboxId&&this.config.sandbox&&await(await this.config.sandbox.connect(this.config.sandboxId)).pause();}async resume(){if(this.agent)return this.agent.resume();this.config.sandboxId&&this.config.sandbox&&await this.config.sandbox.connect(this.config.sandboxId);}async kill(){if(this.agent)return this.agent.kill();this.config.sandboxId&&this.config.sandbox&&await(await this.config.sandbox.connect(this.config.sandboxId)).kill();}async getHost(e){return this.agent||await this.initializeAgent(),this.agent.getHost(e)}getSessionTag(){return this.agent?.getSessionTag()||null}getSessionTimestamp(){return this.agent?.getSessionTimestamp()||null}};var L=class{permits;queue=[];constructor(e){if(e<1)throw new Error("Semaphore max must be >= 1");this.permits=e;}async use(e){await this.acquire();try{return await e()}finally{this.release();}}acquire(){return this.permits>0?(this.permits--,Promise.resolve()):new Promise(e=>this.queue.push(e))}release(){this.queue.length>0?this.queue.shift()():this.permits++;}};var A=Symbol.for("evolve.SwarmResult"),j=class c extends Array{get success(){return this.filter(e=>e.status==="success")}get filtered(){return this.filter(e=>e.status==="filtered")}get error(){return this.filter(e=>e.status==="error")}static from(e){let n=new c;return n.push(...e),n}};var $e=class c{config;semaphore;constructor(e={}){let n=e.concurrency??We;if(n>Se)throw new Error(`concurrency=${n} exceeds max ${Se}. For higher parallelism, scale horizontally with multiple processes.`);this.config={agent:e.agent,sandbox:e.sandbox,tag:e.tag??"swarm",concurrency:n,timeoutMs:e.timeoutMs??W,workspaceMode:e.workspaceMode??"knowledge",retry:e.retry,mcpServers:e.mcpServers,skills:e.skills,composio:e.composio},this.semaphore=new L(this.config.concurrency);}async map(e){let{items:n,prompt:s,bestOf:a,verify:o}=e,i=e.retry??this.config.retry,l=this.generateOperationId(),r=e.timeoutMs??this.config.timeoutMs;if(a&&o)throw new Error("map() cannot use both bestOf and verify options simultaneously");let p=await Promise.all(n.map((t,u)=>a?this.executeMapItemWithBestOf(t,s,u,l,e,r,i):o?this.executeMapItemWithVerify(t,s,u,l,e,r,i):i?T(d=>this.executeMapItem(t,s,u,l,e,r,d),i,u):this.executeMapItem(t,s,u,l,e,r)));return j.from(p)}async filter(e){let{items:n,prompt:s,condition:a,verify:o}=e,i=e.retry??this.config.retry,l=this.generateOperationId(),r=e.timeoutMs??this.config.timeoutMs,p=await Promise.all(n.map((u,d)=>o?this.executeFilterItemWithVerify(u,s,d,l,e,r,i):i?T(m=>this.executeFilterItem(u,s,d,l,e,r,m),i,d):this.executeFilterItem(u,s,d,l,e,r))),t=[];for(let u of p)if(u.status==="error")t.push(u);else if(u.data!==null)try{a(u.data)?t.push(u):t.push({...u,status:"filtered"});}catch(d){t.push({...u,status:"error",data:null,error:`Condition function threw: ${d.message}`});}return j.from(t)}async reduce(e){let{items:n,prompt:s,verify:a}=e,o=e.retry??this.config.retry,i=this.generateOperationId(),l=e.timeoutMs??this.config.timeoutMs,r=[],p=[];n.forEach((x,C)=>{r.push(this.getFiles(x)),p.push(this.getIndex(x,C));});let t={};r.forEach((x,C)=>{Object.entries(x).forEach(([w,S])=>{t[`item_${p[C]}/${w}`]=S;});});let u=pe(t),d=E(mt,{fileTree:u}),m=e.systemPrompt?`${d}
|
|
194
198
|
|
|
195
|
-
${e.systemPrompt}`:d,f=(x,C,w)=>({operationId:a,operation:"reduce",tag:x.tag,sandboxId:x.sandboxId,swarmName:this.config.tag,operationName:e.name,inputCount:n.length,inputIndices:p,errorRetry:C,verifyRetry:w,...this.pipelineContextToMeta(e._pipelineContext)}),h=e.mcpServers??this.config.mcpServers,g=e.skills??this.config.skills,y=e.composio??this.config.composio,v=async(x,C,w,S)=>{let k=await this.semaphore.use(()=>this.execute(t,x,{systemPrompt:m,schema:e.schema,schemaOptions:e.schemaOptions,agent:e.agent,mcpServers:h,skills:g,composio:y,tagPrefix:C,timeoutMs:l,observability:{swarmName:this.config.tag,operationName:e.name,operationId:a,operation:"reduce",role:"worker",errorRetry:w,verifyRetry:S&&S>1?S-1:void 0,...this.pipelineContextToObservability(e._pipelineContext)}})),F=f(k,w,S&&S>1?S-1:void 0);return k.error?{status:"error",data:null,files:k.files,meta:F,error:k.error,rawData:k.rawData}:{status:"success",data:k.data,files:k.files,meta:F}},b=`${this.config.tag}-reduce`;return r?this.runWithVerification((x,C,w)=>v(x,C,void 0,w),{originalPrompt:s,inputFiles:t,verifyConfig:r,timeoutMs:l,systemPrompt:m,schema:e.schema,mcpServers:h,skills:g,composio:y,operationId:a,baseTag:b,retry:o,operation:"reduce",_pipelineContext:e._pipelineContext}):o?T(x=>{let C=x>1?x-1:void 0,w=C?`${b}-er${C}`:b;return v(s,w,C)},o):v(s,b)}async bestOf(e){let{item:n,prompt:s,config:r}=e,o=e.retry??this.config.retry,a=r.n??r.taskAgents?.length;if(a===void 0)throw new Error("bestOf requires n or taskAgents");if(a<2)throw new Error("bestOf requires n >= 2");let l=this.generateOperationId(),i=e.timeoutMs??this.config.timeoutMs,p=this.getFiles(n),t=r.mcpServers??this.config.mcpServers,u=r.judgeMcpServers??r.mcpServers??this.config.mcpServers,d=r.skills??this.config.skills,m=r.judgeSkills??r.skills??this.config.skills,f=r.composio??this.config.composio,h=r.judgeComposio??r.composio??this.config.composio,g=await Promise.all(Array.from({length:a},async(C,w)=>{let S=o?await T(k=>this.executeBestOfCandidate({inputFiles:p,prompt:s,candidateIndex:w,operationId:l,config:r,systemPrompt:e.systemPrompt,schema:e.schema,schemaOptions:e.schemaOptions,mcpServers:t,skills:d,composio:f,timeoutMs:i,attempt:k}),o,w):await this.executeBestOfCandidate({inputFiles:p,prompt:s,candidateIndex:w,operationId:l,config:r,systemPrompt:e.systemPrompt,schema:e.schema,schemaOptions:e.schemaOptions,mcpServers:t,skills:d,composio:f,timeoutMs:i});return r.onCandidateComplete?.(0,w,S.status==="success"?"success":"error"),S})),y=o?await T(C=>this.executeBestOfJudge({inputFiles:p,taskPrompt:s,candidates:g,config:r,timeoutMs:i,operationId:l,systemPrompt:e.systemPrompt,schema:e.schema,schemaOptions:e.schemaOptions,mcpServers:u,skills:m,composio:h,attempt:C}),{...o,retryOn:void 0},0):await this.executeBestOfJudge({inputFiles:p,taskPrompt:s,candidates:g,config:r,timeoutMs:i,operationId:l,systemPrompt:e.systemPrompt,schema:e.schema,schemaOptions:e.schemaOptions,mcpServers:u,skills:m,composio:h}),v=g.findIndex(C=>C.status==="success"),b=y.decision?.winner??(v>=0?v:0);r.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:a};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 r=null,o="",a=s.tagPrefix,l={},i=null,p,t,u={...this.config.agent,...s.agent};try{r=new q().withAgent(u).withSandbox(this.config.sandbox).withWorkspaceMode(this.config.workspaceMode).withSessionTagPrefix(s.tagPrefix),s.schema&&r.withSchema(s.schema,s.schemaOptions),s.systemPrompt&&r.withSystemPrompt(s.systemPrompt),s.mcpServers&&r.withMcpServers(s.mcpServers),s.skills?.length&&r.withSkills(s.skills),s.composio&&r.withComposio(s.composio.userId,s.composio.config),s.observability&&r.withObservability(s.observability),Object.keys(e).length>0&&r.withContext(e);let d=await r.run({prompt:n,timeoutMs:s.timeoutMs});o=d.sandboxId,a=r.getSessionTag()??s.tagPrefix;let m=null;try{m=await r.getOutputFiles(!0),l=m.files;}catch{}d.exitCode!==0?p=`Agent exited with code ${d.exitCode}`:s.schema?m?(i=m.data,m.error&&(p=m.error),m.rawData&&(t=m.rawData)):p="Failed to read output files from sandbox":i=l;}catch(d){if(p=d.message,r){a=r.getSessionTag()??s.tagPrefix;try{l=(await r.getOutputFiles(!0)).files;}catch{}}}finally{r&&await r.kill().catch(()=>{});}return {files:l,data:i,tag:a,sandboxId:o,error:p,rawData:t}}async executeMapItem(e,n,s,r,o,a,l=1){let i=this.getFiles(e),p=l>1?`${this.config.tag}-map-${s}-er${l-1}`:`${this.config.tag}-map-${s}`,t=this.evaluatePrompt(n,i,s);if(t instanceof Error)return this.buildErrorResult(`Prompt function threw: ${t.message}`,{operationId:r,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(i,t,{systemPrompt:o.systemPrompt,schema:o.schema,schemaOptions:o.schemaOptions,agent:o.agent,mcpServers:u,skills:d,composio:m,tagPrefix:p,timeoutMs:a,observability:{swarmName:this.config.tag,operationName:o.name,operationId:r,operation:"map",itemIndex:s,role:"worker",errorRetry:l>1?l-1:void 0,...this.pipelineContextToObservability(o._pipelineContext)}})),h={operationId:r,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,r,o,a,l){let i=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,i,s);if(f instanceof Error)return this.buildErrorResult(`Prompt function threw: ${f.message}`,{operationId:r,operation:"map",tag:p,sandboxId:"",itemIndex:s});let h=async(g,y,v)=>{let b=await this.semaphore.use(()=>this.execute(i,g,{systemPrompt:o.systemPrompt,schema:o.schema,schemaOptions:o.schemaOptions,agent:o.agent,mcpServers:u,skills:d,composio:m,tagPrefix:y,timeoutMs:a,observability:{swarmName:this.config.tag,operationName:o.name,operationId:r,operation:"map",itemIndex:s,role:"worker",verifyRetry:v&&v>1?v-1:void 0,...this.pipelineContextToObservability(o._pipelineContext)}})),x={operationId:r,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:i,verifyConfig:t,timeoutMs:a,systemPrompt:o.systemPrompt,schema:o.schema,mcpServers:u,skills:d,composio:m,operationId:r,baseTag:p,retry:l,itemIndex:s,operation:"map",_pipelineContext:o._pipelineContext})}async executeMapItemWithBestOf(e,n,s,r,o,a,l){let i=this.getFiles(e),p=`${this.config.tag}-map-${s}`,t=this.evaluatePrompt(n,i,s);if(t instanceof Error)return this.buildErrorResult(`Prompt function threw: ${t.message}`,{operationId:r,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:r,operation:"map",tag:p,sandboxId:"",itemIndex:s});let m=o.mcpServers??this.config.mcpServers,f=u.mcpServers??m,h=u.judgeMcpServers??u.mcpServers??m,g=o.skills??this.config.skills,y=u.skills??g,v=u.judgeSkills??u.skills??g,b=o.composio??this.config.composio,x=u.composio??b,C=u.judgeComposio??u.composio??b,w=await Promise.all(Array.from({length:d},async(H,Z)=>{let De=l?await T(xt=>this.executeBestOfCandidate({inputFiles:i,prompt:t,candidateIndex:Z,operationId:r,config:u,systemPrompt:o.systemPrompt,schema:o.schema,schemaOptions:o.schemaOptions,mcpServers:f,skills:y,composio:x,timeoutMs:a,parentIndex:s,attempt:xt,_pipelineContext:o._pipelineContext}),l,Z):await this.executeBestOfCandidate({inputFiles:i,prompt:t,candidateIndex:Z,operationId:r,config:u,systemPrompt:o.systemPrompt,schema:o.schema,schemaOptions:o.schemaOptions,mcpServers:f,skills:y,composio:x,timeoutMs:a,parentIndex:s,_pipelineContext:o._pipelineContext});return u.onCandidateComplete?.(s,Z,De.status==="success"?"success":"error"),De})),S=l?await T(H=>this.executeBestOfJudge({inputFiles:i,taskPrompt:t,candidates:w,config:u,timeoutMs:a,operationId:r,systemPrompt:o.systemPrompt,schema:o.schema,schemaOptions:o.schemaOptions,mcpServers:h,skills:v,composio:C,parentIndex:s,attempt:H,_pipelineContext:o._pipelineContext}),{...l,retryOn:void 0},0):await this.executeBestOfJudge({inputFiles:i,taskPrompt:t,candidates:w,config:u,timeoutMs:a,operationId:r,systemPrompt:o.systemPrompt,schema:o.schema,schemaOptions:o.schemaOptions,mcpServers:h,skills:v,composio:C,parentIndex:s,_pipelineContext:o._pipelineContext}),k=w.findIndex(H=>H.status==="success"),F=S.decision?.winner??(k>=0?k:0),Ae=w[F]??w[0];u.onJudgeComplete?.(s,F,S.decision?.reasoning??"Judge failed to provide reasoning");let vt={operationId:r,operation:"bestof-judge",tag:S.tag,sandboxId:S.sandboxId,swarmName:this.config.tag,operationName:o.name,candidateCount:d,...this.pipelineContextToMeta(o._pipelineContext)};return {...Ae,meta:{...Ae.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:vt,candidates:w}}}async executeFilterItem(e,n,s,r,o,a,l=1){let i=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(i,n,{systemPrompt:o.systemPrompt,schema:o.schema,schemaOptions:o.schemaOptions,agent:o.agent,mcpServers:t,skills:u,composio:d,tagPrefix:p,timeoutMs:a,observability:{swarmName:this.config.tag,operationName:o.name,operationId:r,operation:"filter",itemIndex:s,role:"worker",errorRetry:l>1?l-1:void 0,...this.pipelineContextToObservability(o._pipelineContext)}})),f={operationId:r,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,i)}async executeFilterItemWithVerify(e,n,s,r,o,a,l){let i=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(i,h,{systemPrompt:o.systemPrompt,schema:o.schema,schemaOptions:o.schemaOptions,agent:o.agent,mcpServers:u,skills:d,composio:m,tagPrefix:g,timeoutMs:a,observability:{swarmName:this.config.tag,operationName:o.name,operationId:r,operation:"filter",itemIndex:s,role:"worker",verifyRetry:y&&y>1?y-1:void 0,...this.pipelineContextToObservability(o._pipelineContext)}})),b={operationId:r,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,i)};return this.runWithVerification(f,{originalPrompt:n,inputFiles:i,verifyConfig:t,timeoutMs:a,systemPrompt:o.systemPrompt,schema:o.schema,mcpServers:u,skills:d,composio:m,operationId:r,baseTag:p,retry:l,itemIndex:s,operation:"filter",_pipelineContext:o._pipelineContext})}async executeBestOfCandidate(e){let{inputFiles:n,prompt:s,candidateIndex:r,operationId:o,config:a,timeoutMs:l,parentIndex:i,attempt:p=1,_pipelineContext:t}=e,u=i!==void 0?`${this.config.tag}-map-${i}-bestof-cand-${r}`:`${this.config.tag}-bestof-cand-${r}`,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:a.taskAgents?.[r],mcpServers:e.mcpServers,skills:e.skills,composio:e.composio,tagPrefix:d,timeoutMs:l,observability:{swarmName:this.config.tag,operationId:o,operation:"map",itemIndex:i,role:"candidate",candidateIndex:r,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:r,errorRetry:p>1?p-1:void 0,candidateIndex:r,...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,r)=>{s.status==="error"&&(n[`candidate_${r}/_failed.txt`]=`STATUS: FAILED
|
|
199
|
+
${e.systemPrompt}`:d,f=(x,C,w)=>({operationId:i,operation:"reduce",tag:x.tag,sandboxId:x.sandboxId,swarmName:this.config.tag,operationName:e.name,inputCount:n.length,inputIndices:p,errorRetry:C,verifyRetry:w,...this.pipelineContextToMeta(e._pipelineContext)}),h=e.mcpServers??this.config.mcpServers,g=e.skills??this.config.skills,y=e.composio??this.config.composio,v=async(x,C,w,S)=>{let k=await this.semaphore.use(()=>this.execute(t,x,{systemPrompt:m,schema:e.schema,schemaOptions:e.schemaOptions,agent:e.agent,mcpServers:h,skills:g,composio:y,tagPrefix:C,timeoutMs:l,observability:{swarmName:this.config.tag,operationName:e.name,operationId:i,operation:"reduce",role:"worker",errorRetry:w,verifyRetry:S&&S>1?S-1:void 0,...this.pipelineContextToObservability(e._pipelineContext)}})),$=f(k,w,S&&S>1?S-1:void 0);return k.error?{status:"error",data:null,files:k.files,meta:$,error:k.error,rawData:k.rawData}:{status:"success",data:k.data,files:k.files,meta:$}},b=`${this.config.tag}-reduce`;return a?this.runWithVerification((x,C,w)=>v(x,C,void 0,w),{originalPrompt:s,inputFiles:t,verifyConfig:a,timeoutMs:l,systemPrompt:m,schema:e.schema,mcpServers:h,skills:g,composio:y,operationId:i,baseTag:b,retry:o,operation:"reduce",_pipelineContext:e._pipelineContext}):o?T(x=>{let C=x>1?x-1:void 0,w=C?`${b}-er${C}`:b;return v(s,w,C)},o):v(s,b)}async bestOf(e){let{item:n,prompt:s,config:a}=e,o=e.retry??this.config.retry,i=a.n??a.taskAgents?.length;if(i===void 0)throw new Error("bestOf requires n or taskAgents");if(i<2)throw new Error("bestOf requires n >= 2");let l=this.generateOperationId(),r=e.timeoutMs??this.config.timeoutMs,p=this.getFiles(n),t=a.mcpServers??this.config.mcpServers,u=a.judgeMcpServers??a.mcpServers??this.config.mcpServers,d=a.skills??this.config.skills,m=a.judgeSkills??a.skills??this.config.skills,f=a.composio??this.config.composio,h=a.judgeComposio??a.composio??this.config.composio,g=await Promise.all(Array.from({length:i},async(C,w)=>{let S=o?await T(k=>this.executeBestOfCandidate({inputFiles:p,prompt:s,candidateIndex:w,operationId:l,config:a,systemPrompt:e.systemPrompt,schema:e.schema,schemaOptions:e.schemaOptions,mcpServers:t,skills:d,composio:f,timeoutMs:r,attempt:k}),o,w):await this.executeBestOfCandidate({inputFiles:p,prompt:s,candidateIndex:w,operationId:l,config:a,systemPrompt:e.systemPrompt,schema:e.schema,schemaOptions:e.schemaOptions,mcpServers:t,skills:d,composio:f,timeoutMs:r});return a.onCandidateComplete?.(0,w,S.status==="success"?"success":"error"),S})),y=o?await T(C=>this.executeBestOfJudge({inputFiles:p,taskPrompt:s,candidates:g,config:a,timeoutMs:r,operationId:l,systemPrompt:e.systemPrompt,schema:e.schema,schemaOptions:e.schemaOptions,mcpServers:u,skills:m,composio:h,attempt:C}),{...o,retryOn:void 0},0):await this.executeBestOfJudge({inputFiles:p,taskPrompt:s,candidates:g,config:a,timeoutMs:r,operationId:l,systemPrompt:e.systemPrompt,schema:e.schema,schemaOptions:e.schemaOptions,mcpServers:u,skills:m,composio:h}),v=g.findIndex(C=>C.status==="success"),b=y.decision?.winner??(v>=0?v:0);a.onJudgeComplete?.(0,b,y.decision?.reasoning??"Judge failed to provide reasoning");let x={operationId:l,operation:"bestof-judge",tag:y.tag,sandboxId:y.sandboxId,swarmName:this.config.tag,operationName:e.name,candidateCount:i};return {winner:g[b]??g[0],winnerIndex:b,judgeReasoning:y.decision?.reasoning??"Judge failed to provide reasoning",judgeMeta:x,candidates:g}}async execute(e,n,s){let a=null,o="",i=s.tagPrefix,l={},r=null,p,t,u={...this.config.agent,...s.agent};try{a=new q().withAgent(u).withSandbox(this.config.sandbox).withWorkspaceMode(this.config.workspaceMode).withSessionTagPrefix(s.tagPrefix),s.schema&&a.withSchema(s.schema,s.schemaOptions),s.systemPrompt&&a.withSystemPrompt(s.systemPrompt),s.mcpServers&&a.withMcpServers(s.mcpServers),s.skills?.length&&a.withSkills(s.skills),s.composio&&a.withComposio(s.composio.userId,s.composio.config),s.observability&&a.withObservability(s.observability),Object.keys(e).length>0&&a.withContext(e);let d=await a.run({prompt:n,timeoutMs:s.timeoutMs});o=d.sandboxId,i=a.getSessionTag()??s.tagPrefix;let m=null;try{m=await a.getOutputFiles(!0),l=m.files;}catch{}d.exitCode!==0?p=`Agent exited with code ${d.exitCode}`:s.schema?m?(r=m.data,m.error&&(p=m.error),m.rawData&&(t=m.rawData)):p="Failed to read output files from sandbox":r=l;}catch(d){if(p=d.message,a){i=a.getSessionTag()??s.tagPrefix;try{l=(await a.getOutputFiles(!0)).files;}catch{}}}finally{a&&await a.kill().catch(()=>{});}return {files:l,data:r,tag:i,sandboxId:o,error:p,rawData:t}}async executeMapItem(e,n,s,a,o,i,l=1){let r=this.getFiles(e),p=l>1?`${this.config.tag}-map-${s}-er${l-1}`:`${this.config.tag}-map-${s}`,t=this.evaluatePrompt(n,r,s);if(t instanceof Error)return this.buildErrorResult(`Prompt function threw: ${t.message}`,{operationId:a,operation:"map",tag:p,sandboxId:"",itemIndex:s});let u=o.mcpServers??this.config.mcpServers,d=o.skills??this.config.skills,m=o.composio??this.config.composio,f=await this.semaphore.use(()=>this.execute(r,t,{systemPrompt:o.systemPrompt,schema:o.schema,schemaOptions:o.schemaOptions,agent:o.agent,mcpServers:u,skills:d,composio:m,tagPrefix:p,timeoutMs:i,observability:{swarmName:this.config.tag,operationName:o.name,operationId:a,operation:"map",itemIndex:s,role:"worker",errorRetry:l>1?l-1:void 0,...this.pipelineContextToObservability(o._pipelineContext)}})),h={operationId:a,operation:"map",tag:f.tag,sandboxId:f.sandboxId,swarmName:this.config.tag,operationName:o.name,itemIndex:s,errorRetry:l>1?l-1:void 0,...this.pipelineContextToMeta(o._pipelineContext)};return this.buildResult(f,h)}async executeMapItemWithVerify(e,n,s,a,o,i,l){let r=this.getFiles(e),p=`${this.config.tag}-map-${s}`,t=o.verify,u=o.mcpServers??this.config.mcpServers,d=o.skills??this.config.skills,m=o.composio??this.config.composio,f=this.evaluatePrompt(n,r,s);if(f instanceof Error)return this.buildErrorResult(`Prompt function threw: ${f.message}`,{operationId:a,operation:"map",tag:p,sandboxId:"",itemIndex:s});let h=async(g,y,v)=>{let b=await this.semaphore.use(()=>this.execute(r,g,{systemPrompt:o.systemPrompt,schema:o.schema,schemaOptions:o.schemaOptions,agent:o.agent,mcpServers:u,skills:d,composio:m,tagPrefix:y,timeoutMs:i,observability:{swarmName:this.config.tag,operationName:o.name,operationId:a,operation:"map",itemIndex:s,role:"worker",verifyRetry:v&&v>1?v-1:void 0,...this.pipelineContextToObservability(o._pipelineContext)}})),x={operationId:a,operation:"map",tag:b.tag,sandboxId:b.sandboxId,swarmName:this.config.tag,operationName:o.name,itemIndex:s,verifyRetry:v&&v>1?v-1:void 0,...this.pipelineContextToMeta(o._pipelineContext)};return this.buildResult(b,x)};return this.runWithVerification(h,{originalPrompt:f,inputFiles:r,verifyConfig:t,timeoutMs:i,systemPrompt:o.systemPrompt,schema:o.schema,mcpServers:u,skills:d,composio:m,operationId:a,baseTag:p,retry:l,itemIndex:s,operation:"map",_pipelineContext:o._pipelineContext})}async executeMapItemWithBestOf(e,n,s,a,o,i,l){let r=this.getFiles(e),p=`${this.config.tag}-map-${s}`,t=this.evaluatePrompt(n,r,s);if(t instanceof Error)return this.buildErrorResult(`Prompt function threw: ${t.message}`,{operationId:a,operation:"map",tag:p,sandboxId:"",itemIndex:s});let u=o.bestOf,d=u.n??u.taskAgents?.length;if(d===void 0||d<2)return this.buildErrorResult("bestOf requires n >= 2 or taskAgents with at least 2 elements",{operationId:a,operation:"map",tag:p,sandboxId:"",itemIndex:s});let m=o.mcpServers??this.config.mcpServers,f=u.mcpServers??m,h=u.judgeMcpServers??u.mcpServers??m,g=o.skills??this.config.skills,y=u.skills??g,v=u.judgeSkills??u.skills??g,b=o.composio??this.config.composio,x=u.composio??b,C=u.judgeComposio??u.composio??b,w=await Promise.all(Array.from({length:d},async(H,Z)=>{let Ue=l?await T(Ct=>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:Ct,_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,Ue.status==="success"?"success":"error"),Ue})),S=l?await T(H=>this.executeBestOfJudge({inputFiles:r,taskPrompt:t,candidates:w,config:u,timeoutMs:i,operationId:a,systemPrompt:o.systemPrompt,schema:o.schema,schemaOptions:o.schemaOptions,mcpServers:h,skills:v,composio:C,parentIndex:s,attempt:H,_pipelineContext:o._pipelineContext}),{...l,retryOn:void 0},0):await this.executeBestOfJudge({inputFiles:r,taskPrompt:t,candidates:w,config:u,timeoutMs:i,operationId:a,systemPrompt:o.systemPrompt,schema:o.schema,schemaOptions:o.schemaOptions,mcpServers:h,skills:v,composio:C,parentIndex:s,_pipelineContext:o._pipelineContext}),k=w.findIndex(H=>H.status==="success"),$=S.decision?.winner??(k>=0?k:0),Fe=w[$]??w[0];u.onJudgeComplete?.(s,$,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 {...Fe,meta:{...Fe.meta,operation:"map",swarmName:this.config.tag,operationName:o.name,itemIndex:s,...this.pipelineContextToMeta(o._pipelineContext)},bestOf:{winnerIndex:$,judgeReasoning:S.decision?.reasoning??"Judge failed to provide reasoning",judgeMeta:wt,candidates:w}}}async executeFilterItem(e,n,s,a,o,i,l=1){let r=this.getFiles(e),p=l>1?`${this.config.tag}-filter-${s}-er${l-1}`:`${this.config.tag}-filter-${s}`,t=o.mcpServers??this.config.mcpServers,u=o.skills??this.config.skills,d=o.composio??this.config.composio,m=await this.semaphore.use(()=>this.execute(r,n,{systemPrompt:o.systemPrompt,schema:o.schema,schemaOptions:o.schemaOptions,agent:o.agent,mcpServers:t,skills:u,composio:d,tagPrefix:p,timeoutMs:i,observability:{swarmName:this.config.tag,operationName:o.name,operationId:a,operation:"filter",itemIndex:s,role:"worker",errorRetry:l>1?l-1:void 0,...this.pipelineContextToObservability(o._pipelineContext)}})),f={operationId:a,operation:"filter",tag:m.tag,sandboxId:m.sandboxId,swarmName:this.config.tag,operationName:o.name,itemIndex:s,errorRetry:l>1?l-1:void 0,...this.pipelineContextToMeta(o._pipelineContext)};return this.buildResult(m,f,r)}async executeFilterItemWithVerify(e,n,s,a,o,i,l){let r=this.getFiles(e),p=`${this.config.tag}-filter-${s}`,t=o.verify,u=o.mcpServers??this.config.mcpServers,d=o.skills??this.config.skills,m=o.composio??this.config.composio,f=async(h,g,y)=>{let v=await this.semaphore.use(()=>this.execute(r,h,{systemPrompt:o.systemPrompt,schema:o.schema,schemaOptions:o.schemaOptions,agent:o.agent,mcpServers:u,skills:d,composio:m,tagPrefix:g,timeoutMs:i,observability:{swarmName:this.config.tag,operationName:o.name,operationId:a,operation:"filter",itemIndex:s,role:"worker",verifyRetry:y&&y>1?y-1:void 0,...this.pipelineContextToObservability(o._pipelineContext)}})),b={operationId:a,operation:"filter",tag:v.tag,sandboxId:v.sandboxId,swarmName:this.config.tag,operationName:o.name,itemIndex:s,verifyRetry:y&&y>1?y-1:void 0,...this.pipelineContextToMeta(o._pipelineContext)};return this.buildResult(v,b,r)};return this.runWithVerification(f,{originalPrompt:n,inputFiles:r,verifyConfig:t,timeoutMs:i,systemPrompt:o.systemPrompt,schema:o.schema,mcpServers:u,skills:d,composio:m,operationId:a,baseTag:p,retry:l,itemIndex:s,operation:"filter",_pipelineContext:o._pipelineContext})}async executeBestOfCandidate(e){let{inputFiles:n,prompt:s,candidateIndex:a,operationId:o,config:i,timeoutMs:l,parentIndex:r,attempt:p=1,_pipelineContext:t}=e,u=r!==void 0?`${this.config.tag}-map-${r}-bestof-cand-${a}`:`${this.config.tag}-bestof-cand-${a}`,d=p>1?`${u}-er${p-1}`:u,m=await this.semaphore.use(()=>this.execute(n,s,{systemPrompt:e.systemPrompt,schema:e.schema,schemaOptions:e.schemaOptions,agent:i.taskAgents?.[a],mcpServers:e.mcpServers,skills:e.skills,composio:e.composio,tagPrefix:d,timeoutMs:l,observability:{swarmName:this.config.tag,operationId:o,operation:"map",itemIndex:r,role:"candidate",candidateIndex:a,errorRetry:p>1?p-1:void 0,...this.pipelineContextToObservability(t)}})),f={operationId:o,operation:"bestof-cand",tag:m.tag,sandboxId:m.sandboxId,swarmName:this.config.tag,itemIndex:a,errorRetry:p>1?p-1:void 0,candidateIndex:a,...this.pipelineContextToMeta(t)};return this.buildResult(m,f)}buildJudgeContext(e){let n=this.buildEvaluatorContext({inputFiles:e.inputFiles,taskPrompt:e.taskPrompt,systemPrompt:e.systemPrompt,schema:e.schema});return e.candidates.forEach((s,a)=>{s.status==="error"&&(n[`candidate_${a}/_failed.txt`]=`STATUS: FAILED
|
|
196
200
|
|
|
197
|
-
Error: ${s.error??"Unknown error"}`),Object.entries(s.files).forEach(([o,a])=>{n[`candidate_${r}/${o}`]=a;});}),n}async executeBestOfJudge(e){let{candidates:n,config:s,timeoutMs:r,operationId:o,parentIndex:a,attempt:l=1,_pipelineContext:i}=e,p=a!==void 0?`${this.config.tag}-map-${a}-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=ce(u),m=E(ke,{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,ct,{systemPrompt:m,schema:f,agent:s.judgeAgent,mcpServers:e.mcpServers,skills:e.skills,composio:e.composio,tagPrefix:t,timeoutMs:r,observability:{swarmName:this.config.tag,operationId:o,operation:"map",itemIndex:a,role:"judge",errorRetry:l>1?l-1:void 0,...this.pipelineContextToObservability(i)}})),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,r])=>{n[`worker_output/${s}`]=r;}),n}async executeVerify(e){let{config:n,timeoutMs:s,operationId:r,workerTag:o,retryAttempt:a=1,operation:l,itemIndex:i,attemptIndex:p,_pipelineContext:t}=e,u=a>1?`${o}-verifier-er${a-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=ce(h),y=E(Te,{criteria:n.criteria,fileTree:g}),v=await this.semaphore.use(()=>this.execute(h,lt,{systemPrompt:y,schema:c.VerifyDecisionSchema,agent:n.verifierAgent,mcpServers:d,skills:m,composio:f,tagPrefix:u,timeoutMs:s,observability:{swarmName:this.config.tag,operationId:r,operation:l,itemIndex:i,role:"verifier",verifyRetry:p&&p>1?p-1:void 0,errorRetry:a>1?a-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 E(_e,{originalPrompt:e,feedback:n})}async runWithVerification(e,n){let{originalPrompt:s,inputFiles:r,verifyConfig:o,timeoutMs:a,operationId:l,baseTag:i,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?`${i}-vr${g-1}`:i,b=p?await T(w=>e(f,w>1?`${v}-er${w-1}`:v,g),p,t):await e(f,v,g);if(o.onWorkerComplete?.(t,g,b.status==="error"?"error":"success"),b.status==="error")return b;h=b;let x=p?await T(w=>this.executeVerify({inputFiles:r,outputFiles:b.files,taskPrompt:f,config:o,timeoutMs:a,systemPrompt:n.systemPrompt,schema:n.schema,mcpServers:n.mcpServers,skills:n.skills,composio:n.composio,operationId:l,workerTag:v,retryAttempt:w,operation:u,itemIndex:t,attemptIndex:g,_pipelineContext:d}),{...p,retryOn:void 0}):await this.executeVerify({inputFiles:r,outputFiles:b.files,taskPrompt:f,config:o,timeoutMs:a,systemPrompt:n.systemPrompt,schema:n.schema,mcpServers:n.mcpServers,skills:n.skills,composio:n.composio,operationId:l,workerTag:v,operation:u,itemIndex:t,attemptIndex:g,_pipelineContext:d}),C={operationId:l,operation:"verify",tag:x.tag,sandboxId:x.sandboxId,swarmName:this.config.tag,attempts:g,...this.pipelineContextToMeta(d)};if(o.onVerifierComplete?.(t,g,x.decision?.passed??false,x.decision?.feedback),x.decision?.passed)return {...b,verify:{passed:true,reasoning:x.decision.reasoning,verifyMeta:C,attempts:g}};if(g<m){let w=x.decision?.feedback??x.decision?.reasoning??"Output did not meet criteria";f=c.buildRetryPromptWithFeedback(s,w);}}let y=g>1?`${i}-vr${g-1}`:i;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(r){return r}}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)?B(e.schema):N(e.schema)),Object.entries(e.inputFiles).forEach(([s,r])=>{n[`worker_task/input/${s}`]=r;}),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 r=s??e.files;return e.error?{[A]:true,status:"error",data:null,files:r,meta:n,error:e.error,rawData:e.rawData}:{[A]:true,status:"success",data:e.data,files:r,meta:n}}buildErrorResult(e,n){return {[A]:true,status:"error",data:null,files:{},meta:n,error:e}}};var ue=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 de(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=[],r=e,o=Date.now();for(let l=0;l<this.steps.length;l++){let i=this.steps[l],p=i.config.name,t=Date.now();this.events.onStepStart?.({type:i.type,index:l,name:p,itemCount:r.length});let u={pipelineRunId:n,pipelineStepIndex:l};try{let d=await this.executeStep(i,r,l,p,u),m=Date.now()-t;if(s.push({type:i.type,index:l,durationMs:m,results:d.output}),this.events.onStepComplete?.({type:i.type,index:l,name:p,durationMs:m,successCount:d.successCount,errorCount:d.errorCount,filteredCount:d.filteredCount}),i.type==="reduce")return {pipelineRunId:n,steps:s,output:d.output,totalDurationMs:Date.now()-o};r=d.nextItems;}catch(d){throw this.events.onStepError?.({type:i.type,index:l,name:p,error:d instanceof Error?d:new Error(String(d))}),d}}let a=s[s.length-1];return {pipelineRunId:n,steps:s,output:a?.results??[],totalDurationMs:Date.now()-o}}async executeStep(e,n,s,r,o){if(e.type==="map"){let i=e.config,p=await this.swarm.map({items:n,...i,_pipelineContext:o,retry:this.wrapRetry(i.retry,s,r),verify:this.wrapVerify(i.verify,s,r),bestOf:this.wrapBestOf(i.bestOf,s,r)});return {output:[...p],nextItems:p.success,successCount:p.success.length,errorCount:p.error.length,filteredCount:0}}if(e.type==="filter"){let i=e.config,p=await this.swarm.filter({items:n,...i,_pipelineContext:o,retry:this.wrapRetry(i.retry,s,r),verify:this.wrapVerify(i.verify,s,r)}),t=i.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 a=e.config,l=await this.swarm.reduce({items:n,...a,_pipelineContext:o,retry:this.wrapRetry(a.retry,s,r),verify:this.wrapVerify(a.verify,s,r)});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:(r,o,a)=>{e.onItemRetry?.(r,o,a),this.events.onItemRetry?.({stepIndex:n,stepName:s,itemIndex:r,attempt:o,error:a});}}}wrapVerify(e,n,s){if(e)return {...e,onWorkerComplete:(r,o,a)=>{e.onWorkerComplete?.(r,o,a),this.events.onWorkerComplete?.({stepIndex:n,stepName:s,itemIndex:r,attempt:o,status:a});},onVerifierComplete:(r,o,a,l)=>{e.onVerifierComplete?.(r,o,a,l),this.events.onVerifierComplete?.({stepIndex:n,stepName:s,itemIndex:r,attempt:o,passed:a,feedback:l});}}}wrapBestOf(e,n,s){if(e)return {...e,onCandidateComplete:(r,o,a)=>{e.onCandidateComplete?.(r,o,a),this.events.onCandidateComplete?.({stepIndex:n,stepName:s,itemIndex:r,candidateIndex:o,status:a});},onJudgeComplete:(r,o,a)=>{e.onJudgeComplete?.(r,o,a),this.events.onJudgeComplete?.({stepIndex:n,stepName:s,itemIndex:r,winnerIndex:o,reasoning:a});}}}},de=class c extends ue{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")}};exports.AGENT_REGISTRY=fe;exports.AGENT_TYPES=bt;exports.Agent=z;exports.Evolve=q;exports.JUDGE_PROMPT=ke;exports.Pipeline=ue;exports.RETRY_FEEDBACK_PROMPT=_e;exports.SCHEMA_PROMPT=at;exports.SWARM_RESULT_BRAND=A;exports.SYSTEM_PROMPT=rt;exports.Semaphore=j;exports.Swarm=Oe;exports.SwarmResultList=L;exports.TerminalPipeline=de;exports.VALIDATION_PRESETS=me;exports.VERIFY_PROMPT=Te;exports.WORKSPACE_PROMPT=it;exports.WORKSPACE_SWE_PROMPT=ot;exports.applyTemplate=E;exports.buildWorkerSystemPrompt=Re;exports.createAgentParser=I;exports.createClaudeParser=ne;exports.createCodexParser=se;exports.createGeminiParser=ie;exports.executeWithRetry=T;exports.expandPath=ge;exports.getAgentConfig=M;exports.getMcpSettingsDir=U;exports.getMcpSettingsPath=$;exports.isValidAgentType=wt;exports.isZodSchema=R;exports.jsonSchemaToString=N;exports.parseNdjsonLine=Rt;exports.parseNdjsonOutput=Pt;exports.parseQwenOutput=$e;exports.readLocalDir=Ye;exports.saveLocalDir=Ge;exports.writeClaudeMcpConfig=Q;exports.writeCodexMcpConfig=te;exports.writeGeminiMcpConfig=X;exports.writeMcpConfig=xe;exports.writeQwenMcpConfig=ee;exports.zodSchemaToJson=B;
|
|
201
|
+
Error: ${s.error??"Unknown error"}`),Object.entries(s.files).forEach(([o,i])=>{n[`candidate_${a}/${o}`]=i;});}),n}async executeBestOfJudge(e){let{candidates:n,config:s,timeoutMs:a,operationId:o,parentIndex:i,attempt:l=1,_pipelineContext:r}=e,p=i!==void 0?`${this.config.tag}-map-${i}-bestof-judge`:`${this.config.tag}-bestof-judge`,t=l>1?`${p}-er${l-1}`:p,u=this.buildJudgeContext({inputFiles:e.inputFiles,taskPrompt:e.taskPrompt,candidates:n,systemPrompt:e.systemPrompt,schema:e.schema}),d=pe(u),m=E(Pe,{candidateCount:String(n.length),criteria:s.judgeCriteria,fileTree:d}),f=zod.z.object({winner:zod.z.number().min(0).max(n.length-1),reasoning:zod.z.string()}),h=await this.semaphore.use(()=>this.execute(u,ut,{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=pe(h),y=E(Re,{criteria:n.criteria,fileTree:g}),v=await this.semaphore.use(()=>this.execute(h,dt,{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 E(Ee,{originalPrompt:e,feedback:n})}async runWithVerification(e,n){let{originalPrompt:s,inputFiles:a,verifyConfig:o,timeoutMs:i,operationId:l,baseTag:r,retry:p,itemIndex:t=0,operation:u,_pipelineContext:d}=n,m=o.maxAttempts??c.DEFAULT_VERIFY_MAX_ATTEMPTS,f=s,h=null,g=0;for(;g<m;){g++;let v=g>1?`${r}-vr${g-1}`:r,b=p?await T(w=>e(f,w>1?`${v}-er${w-1}`:v,g),p,t):await e(f,v,g);if(o.onWorkerComplete?.(t,g,b.status==="error"?"error":"success"),b.status==="error")return b;h=b;let x=p?await T(w=>this.executeVerify({inputFiles:a,outputFiles:b.files,taskPrompt:f,config:o,timeoutMs:i,systemPrompt:n.systemPrompt,schema:n.schema,mcpServers:n.mcpServers,skills:n.skills,composio:n.composio,operationId:l,workerTag:v,retryAttempt:w,operation:u,itemIndex:t,attemptIndex:g,_pipelineContext:d}),{...p,retryOn:void 0}):await this.executeVerify({inputFiles:a,outputFiles:b.files,taskPrompt:f,config:o,timeoutMs:i,systemPrompt:n.systemPrompt,schema:n.schema,mcpServers:n.mcpServers,skills:n.skills,composio:n.composio,operationId:l,workerTag:v,operation:u,itemIndex:t,attemptIndex:g,_pipelineContext:d}),C={operationId:l,operation:"verify",tag:x.tag,sandboxId:x.sandboxId,swarmName:this.config.tag,attempts:g,...this.pipelineContextToMeta(d)};if(o.onVerifierComplete?.(t,g,x.decision?.passed??false,x.decision?.feedback),x.decision?.passed)return {...b,verify:{passed:true,reasoning:x.decision.reasoning,verifyMeta:C,attempts:g}};if(g<m){let w=x.decision?.feedback??x.decision?.reasoning??"Output did not meet criteria";f=c.buildRetryPromptWithFeedback(s,w);}}let y=g>1?`${r}-vr${g-1}`:r;return {...h,status:"error",verify:{passed:false,reasoning:"Max verification retries exceeded",verifyMeta:{operationId:l,operation:"verify",tag:`${y}-verifier`,sandboxId:"",swarmName:this.config.tag,attempts:g,...this.pipelineContextToMeta(d)},attempts:g}}}generateOperationId(){return crypto.randomBytes(8).toString("hex")}pipelineContextToObservability(e){return e?{pipelineRunId:e.pipelineRunId,pipelineStepIndex:e.pipelineStepIndex}:{}}pipelineContextToMeta(e){return {pipelineRunId:e?.pipelineRunId,pipelineStepIndex:e?.pipelineStepIndex}}evaluatePrompt(e,n,s){if(typeof e=="string")return e;try{return e(n,s)}catch(a){return a}}buildEvaluatorContext(e){let n={};return e.systemPrompt&&(n["worker_task/system_prompt.txt"]=e.systemPrompt),n["worker_task/user_prompt.txt"]=e.taskPrompt,e.schema&&(n["worker_task/schema.json"]=P(e.schema)?B(e.schema):N(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 me=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 fe(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});}}}},fe=class c extends me{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=he;exports.AGENT_TYPES=St;exports.Agent=z;exports.Evolve=q;exports.JUDGE_PROMPT=Pe;exports.Pipeline=me;exports.RETRY_FEEDBACK_PROMPT=Ee;exports.SCHEMA_PROMPT=pt;exports.SWARM_RESULT_BRAND=A;exports.SYSTEM_PROMPT=lt;exports.Semaphore=L;exports.Swarm=$e;exports.SwarmResultList=j;exports.TerminalPipeline=fe;exports.VALIDATION_PRESETS=ge;exports.VERIFY_PROMPT=Re;exports.WORKSPACE_PROMPT=at;exports.WORKSPACE_SWE_PROMPT=ct;exports.applyTemplate=E;exports.buildWorkerSystemPrompt=Me;exports.createAgentParser=O;exports.createClaudeParser=ne;exports.createCodexParser=se;exports.createGeminiParser=oe;exports.executeWithRetry=T;exports.expandPath=ye;exports.getAgentConfig=M;exports.getMcpSettingsDir=U;exports.getMcpSettingsPath=F;exports.isValidAgentType=kt;exports.isZodSchema=P;exports.jsonSchemaToString=N;exports.parseNdjsonLine=Mt;exports.parseNdjsonOutput=Ot;exports.parseQwenOutput=Ne;exports.readLocalDir=qe;exports.saveLocalDir=He;exports.writeClaudeMcpConfig=Q;exports.writeCodexMcpConfig=te;exports.writeGeminiMcpConfig=X;exports.writeMcpConfig=we;exports.writeQwenMcpConfig=ee;exports.zodSchemaToJson=B;
|