import{n as e,o as t}from"./chunk-DR8-3Aex.js";import{Ct as n,F as r,Rn as i,Tt as a,W as o,b as s,t as c,x as l}from"./src-Di342QoJ.js";import{t as u}from"./jsx-runtime-D-D469L8.js";import{Cc as d,Ps as f,yc as p}from"./paths-Ci1iwL5Y.js";import{U as m,z as h}from"./debug-otxXY3j-.js";import{$D as g,Hk as _,Ll as v,Po as y,Rl as b,Uk as x,Uo as S,Wk as C,ck as w,d_ as T,dt as E,eO as D,ft as O,gb as k,hb as A,p_ as j}from"./loadAgentsDir-C_wFhytF.js";import{X as M}from"./rcDebugLog-C42A0pX1.js";import{t as N}from"./useTerminalSize-Du-EamDN.js";import{n as P,r as F}from"./sideQuestion-BHK6dd1q.js";import{i as I,n as L}from"./modalContext-c5Yqx_CQ.js";function R({question:e,context:t,onDone:n}){let[i,c]=(0,H.useState)(null),[u,d]=(0,H.useState)(null),[f,p]=(0,H.useState)(0),m=(0,H.useRef)(null),{rows:_}=I(r());a(()=>p(e=>e+1),i||u?null:80);function v(e){if(e.key===`escape`||e.key===`return`||e.key===` `||e.ctrl&&(e.key===`c`||e.key===`d`)){e.preventDefault(),n(void 0,{display:`skip`});return}(e.key===`up`||e.ctrl&&e.key===`p`)&&(e.preventDefault(),m.current?.scrollBy(-K)),(e.key===`down`||e.ctrl&&e.key===`n`)&&(e.preventDefault(),m.current?.scrollBy(K))}(0,H.useEffect)(()=>{let n=g();async function r(){try{let r=await F({question:e,cacheSafeParams:await B(t)});n.signal.aborted||(r.response?c(r.response):d(`No response received`))}catch(e){n.signal.aborted||d(h(e)||`Failed to get response`)}}return r(),()=>{n.abort()}},[e,t]);let y=Math.max(5,_-W-G);return(0,U.jsxs)(l,{flexDirection:`column`,paddingLeft:2,marginTop:1,tabIndex:0,autoFocus:!0,onKeyDown:v,children:[(0,U.jsxs)(l,{children:[(0,U.jsxs)(s,{color:`warning`,bold:!0,children:[`/btw`,` `]}),(0,U.jsx)(s,{dimColor:!0,children:e})]}),(0,U.jsx)(l,{marginTop:1,marginLeft:2,maxHeight:y,children:(0,U.jsx)(o,{ref:m,flexDirection:`column`,flexGrow:1,children:u?(0,U.jsx)(s,{color:`error`,children:u}):i?(0,U.jsx)(T,{children:i}):(0,U.jsxs)(l,{children:[(0,U.jsx)(A,{frame:f,messageColor:`warning`}),(0,U.jsx)(s,{color:`warning`,children:`Answering...`})]})})}),(i||u)&&(0,U.jsx)(l,{marginTop:1,children:(0,U.jsxs)(s,{dimColor:!0,children:[`↑`,`/`,`↓`,` to scroll · Space, Enter, or Escape to dismiss`]})})]})}function z(e){let t=e.at(-1);return t?.type===`assistant`&&t.message.stop_reason===null?e.slice(0,-1):e}async function B(e){let t=y(z(e.messages)),n=v();if(n)return{systemPrompt:n.systemPrompt,userContext:n.userContext,systemContext:n.systemContext,toolUseContext:e,forkContextMessages:t};let[r,i,a]=await Promise.all([E(e.options.tools,e.options.mainLoopModel,[],e.options.mcpClients),x(),_()]);return{systemPrompt:M(r),userContext:i,systemContext:a,toolUseContext:e,forkContextMessages:t}}async function V(e,t,n){let r=n?.trim();return r?(d(e=>({...e,btwUseCount:e.btwUseCount+1})),(0,U.jsx)(R,{question:r,context:t,onDone:e})):(e(`Usage: /btw <your question>`,{display:`system`}),null)}var H,U,W,G,K;e((()=>{i(),H=t(i(),1),n(),j(),k(),f(),O(),L(),C(),N(),c(),D(),p(),m(),b(),S(),P(),w(),U=u(),W=5,G=6,K=3}))();export{V as call};