import{isObject}from"#shared/guards.js";import{loadContext}from"#context/container.js";import{tool}from"ai";import{isAuthorizationSignal,redactSignalResume}from"#harness/authorization.js";import{isCodeModeToolExecutionOptions}from"#runtime/framework-tools/code-mode-connection-auth.js";import{ASK_QUESTION_TOOL_NAME}from"#runtime/framework-tools/ask-question.js";import{WEB_SEARCH_TOOL_DEFINITION}from"#runtime/framework-tools/web-search.js";import{resolveWebSearchBackend,resolveWebSearchProviderTool}from"#harness/provider-tools.js";import{stashToolInterrupt}from"#harness/tool-interrupts.js";function buildToolSet(e){let t={},r=e.capabilities?.requestInput===!0,i=e.disabledProviderTools;for(let a of e.tools.values())a.name===ASK_QUESTION_TOOL_NAME&&!r||i?.has(a.name)||(t[a.name]=tool({description:a.description,execute:wrapToolExecute(a),inputSchema:a.inputSchema,needsApproval:buildNeedsApprovalFn(a,e),outputSchema:a.outputSchema,...a.toModelOutput===void 0?{}:{toModelOutput:({output:e})=>a.toModelOutput(e)}}));return t}function wrapToolExecute(e){let n=e.execute;if(n!==void 0)return async(e,o)=>{let s=await n(e);return!isAuthorizationSignal(s)||isCodeModeToolExecutionOptions(o)?s:(stashToolInterrupt(loadContext(),o.toolCallId,s),redactSignalResume(s))}}async function buildToolSetWithProviderTools(e){let t=e.disabledProviderTools,n={...buildToolSet({approvedTools:e.approvedTools,capabilities:e.capabilities,disabledProviderTools:t,tools:e.tools})};if(!t?.has(WEB_SEARCH_TOOL_DEFINITION.name)){let t=e.tools.get(WEB_SEARCH_TOOL_DEFINITION.name);if(t!==void 0&&t.execute===void 0){let t=resolveWebSearchBackend(e.modelReference);t===null?delete n[WEB_SEARCH_TOOL_DEFINITION.name]:n[WEB_SEARCH_TOOL_DEFINITION.name]=await resolveWebSearchProviderTool(t)}}return n}function buildNeedsApprovalFn(t,n){return async r=>{if(t.needsApproval===void 0)return!1;let i=isObject(r)?r:void 0;return t.needsApproval({approvedTools:n.approvedTools??new Set,toolInput:i,toolName:t.name})}}export{buildToolSet,buildToolSetWithProviderTools,wrapToolExecute};