import{contextStorage}from"#context/container.js";import"ai";import{CODE_MODE_RUNTIME_ACTION_INTERRUPT_KIND}from"#harness/code-mode-runtime-action-state.js";import{loadCodeModeModule}from"#shared/code-mode.js";import{isAuthorizationSignal}from"#harness/authorization.js";import{buildDynamicTools}from"#context/build-dynamic-tools.js";import{CODE_MODE_CONNECTION_AUTH_INTERRUPT_KIND,markCodeModeToolExecutionOptions,toCodeModeConnectionAuthArgs}from"#runtime/framework-tools/code-mode-connection-auth.js";import{ALL_SANDBOX_SURFACES}from"#harness/sandbox-surface.js";import{LOAD_SKILL_TOOL_NAME}from"#runtime/skills/fragment-context.js";import{buildToolSet}from"#harness/tools.js";const NEVER_SANDBOXED_TOOL_NAMES=new Set([LOAD_SKILL_TOOL_NAME]);function createEveCodeModeOptions(e={}){let t={approval:{mode:`interrupt`}};return e.lifecycle!==void 0&&(t.lifecycle=e.lifecycle),t}async function applySandboxToolSet(e){let t={},n=new Map(e.surfaces.map(e=>[e,{}]));for(let[r,i]of Object.entries(e.tools)){if(NEVER_SANDBOXED_TOOL_NAMES.has(r)){t[r]=i;continue}let a=e.harnessTools.get(r),o=a?.runtimeAction!==void 0;o&&(t[r]=i);let s=e.surfaces.filter(e=>e.claims(a,i));if(s.length===0){o||(t[r]=i);continue}let c=o&&a!==void 0?createRuntimeActionHostTool(a):wrapHostToolForCodeMode(i);for(let e of s)n.get(e)[r]=c}let r={};for(let i of e.surfaces){let a=n.get(i);Object.keys(a).length!==0&&(Object.assign(r,a),t[i.toolName]=await createSandboxTool(i,a,e.lifecycle))}return{hostTools:r,modelTools:t}}async function createSandboxTool(e,t,r){let{createCodeModeTool:i}=await loadCodeModeModule(),a=i(t,createEveCodeModeOptions({lifecycle:r}));if(e.describe===void 0)return a;let o=typeof a.description==`string`?a.description:``;return{...a,description:e.describe({generated:o,toolNames:Object.keys(t)})}}async function buildSandboxHostTools(t){let n=buildToolSet({approvedTools:t.approvedTools,capabilities:t.capabilities,tools:t.tools}),r=contextStorage.getStore();if(r!==void 0){let e=buildDynamicTools(r);for(let t of e)n[t.name]??={description:t.description,inputSchema:t.inputSchema,execute:t.execute,outputSchema:t.outputSchema}}return(await applySandboxToolSet({harnessTools:t.tools,tools:n,surfaces:ALL_SANDBOX_SURFACES})).hostTools}function createRuntimeActionHostTool(e){return{description:e.description,inputSchema:e.inputSchema,execute:async(r,i)=>{let a=i?.codeModeInterrupt;if(a?.resolution!==void 0)return a.resolution;let{requestCodeModeInterrupt:o}=await loadCodeModeModule();return o({kind:CODE_MODE_RUNTIME_ACTION_INTERRUPT_KIND,runtimeAction:e.runtimeAction,toolInput:r,toolName:e.name})}}}function wrapHostToolForCodeMode(e){let t=e.execute;if(t===void 0)return e;let invoke=async(e,i)=>{let c=await resolveExecuteOutput(t(e,markCodeModeToolExecutionOptions(i)));if(isAuthorizationSignal(c)){let{requestCodeModeInterrupt:t}=await loadCodeModeModule(),r=c.challenges[0]?.name;r&&t({args:toCodeModeConnectionAuthArgs(e),challenges:c.challenges,connectionName:r,kind:CODE_MODE_CONNECTION_AUTH_INTERRUPT_KIND,toolName:``})}return c};return{...e,execute:(e,t)=>invoke(e,t)}}async function resolveExecuteOutput(e){if(isAsyncIterable(e)){let t;for await(let n of e)t=n;return t}return await e}function isAsyncIterable(e){return typeof e==`object`&&!!e&&Symbol.asyncIterator in e&&typeof e[Symbol.asyncIterator]==`function`}export{applySandboxToolSet,buildSandboxHostTools,createEveCodeModeOptions};