import{o as e}from"./rolldown-runtime-iuO8RvI4.js";import{At as t}from"./vendor-all-0-fRSUBfCr.js";import{$a as o,Ia as i}from"./shared-0-C4kdVF7Y.js";import{Mi as n}from"./vendor-frame-CwiylG4Y.js";import{cl as s}from"./shared-5-UQAFGbWu.js";import{Ja as a,To as r}from"./shared-3-D-Ux4aGn.js";import{Cc as d,yc as u,zl as l}from"./shared-9-D_WZeX6S.js";var p=e(n());function m(e){if(!e)return;const t="AskUserQuestion"===e.tool_name?"ask_user_question":"ExitPlanMode"===e.tool_name?"exit_plan_mode":"tool_permission",n={requestId:e.request_id||void 0,toolUseId:e.tool_use_id||void 0,toolName:e.tool_name||void 0,input:e.input?a(e.input):e.input},r=(e.input?.questions)?.[0]?.header;switch(t){case"ask_user_question":return{kind:t,headline:("string"==typeof r?s(r):"")||s(e.action_description??"")||"Question",...n};case"exit_plan_mode":return{kind:t,headline:s(e.action_description??"")||"Ready to start?",detail:"string"==typeof e.input?.plan?s(e.input.plan):void 0,...n};case"tool_permission":return{kind:t,headline:s(e.action_description??"")||(d=e.tool_name,o(d).replace(/_/g," ").replace(/\b\w/g,e=>e.toUpperCase())),detail:e.input&&Object.keys(e.input).length>0?void 0!==i(e.tool_name)&&"string"==typeof e.input.command?e.input.command:JSON.stringify(e.input,null,2):void 0,...n}}var d}var c=new Map;function _(e,t){let o=c.get(e);o||(o=new Set,c.set(e,o)),o.add(t)}function f(e,o){const i=u(e),{mutateAsync:n}=r(),s=t(),a=o?.id,f=o?.session_status,g=o?.external_metadata?.pending_action;(0,p.useEffect)(()=>{a&&("requires_action"===f||g||c.delete(a))},[a,f,g]);const I=e?.id;(0,p.useEffect)(()=>{I&&g&&l.getState().wakeSession(I)},[I,g]);const h=(0,p.useMemo)(()=>{if(i)return function(e){const{tool:t}=e;return m({tool_name:t.name,action_description:"",request_id:t.id,tool_use_id:t.id,input:t.input})}(i);const e=m(g);return e?.toolUseId&&a&&c.get(a)?.has(e.toolUseId)?void 0:e},[i,g,a]),v=(0,p.useCallback)(async(e,t)=>{if(i)return a&&_(a,i.tool.id),void i.decide(e,t);a&&h?.requestId&&h.toolUseId&&h.toolName&&(l.getState().wakeSession(a,{userInitiated:!0}),await n({sessionId:a,requestId:h.requestId,toolUseId:h.toolUseId,toolName:h.toolName,decision:e,input:t??h.input}),_(a,h.toolUseId),l.getState().beginPendingTurn(a),s.invalidateQueries({queryKey:d.transcript({id:a,type:"remote"})}))},[i,h,n,a,s]);return(0,p.useMemo)(()=>{if(h)return{...h,tool:i?.tool??{id:h.toolUseId??h.requestId??"",name:h.toolName??"",input:h.input??{},status:"awaiting_approval"},hasAlwaysAllow:i?.hasAlwaysAllow??!1,queueDepth:i?.queueDepth??0,decide:v}},[h,i,v])}export{f as t};