import{r as o,f as W,g as B,h as $,j as e,d as N,L as q,E as S,T as G,i as K,k as w,l as v,m as U,n as j,o as Q,p as k,S as V,I as T,e as z,q as J,s as Y,t as X,v as Z,w as ee,B as y,x as te,y as _,C as se,z as I,D as ne,F as ae,G as re,H as ie,J as oe,K as le}from"./index-BO5sQl1x.js";import{u as ce}from"./startWorkflowDraft-DmEyPLiI.js";function de(t){return t.isTerminal?"ended":t.attemptsReady?t.liveAttemptCount>0?"live":"awaiting":"connecting"}function ue(t){return t.at(-1)??null}const me={live:"Agent is working — messages interrupt…",awaiting:"Agent finished — send to continue…",connecting:"Connecting to session…",ended:"Session ended"},xe={live:"live",awaiting:"healthy",connecting:"idle",ended:"idle"};function pe(t){return[...t].sort((s,n)=>n.started_at.localeCompare(s.started_at))}const he=50;function fe({namespace:t,client:s}){const n=o.useMemo(W,[]),l=o.useMemo(()=>({limit:he}),[]),a=B({apiClient:s,filter:n,page:l});return $({filter:n,page:l,isFirstPage:!0}),t===null?e.jsx(N,{description:"Select a namespace to list assistant sessions.",title:"No namespace selected"}):a.isPending?e.jsx(q,{label:"Loading assistant sessions",rows:4}):a.isError?e.jsx(S,{error:a.error,onRetry:()=>{a.refetch()},title:"Could not load assistant sessions"}):e.jsx(ge,{sessions:a.data?.items??[]})}function ge({sessions:t}){return t.length===0?e.jsx(N,{description:"Start one above — the session appears here the moment the server confirms it.",title:"No assistant sessions yet"}):e.jsxs(G,{"data-testid":"assistant-session-list",children:[e.jsx(K,{children:e.jsxs(w,{children:[e.jsx(v,{children:"Session"}),e.jsx(v,{children:"Status"}),e.jsx(v,{children:"Started"}),e.jsx(v,{children:"Ended"})]})}),e.jsx(U,{children:pe(t).map(s=>e.jsxs(w,{children:[e.jsx(j,{className:"font-mono text-xs",children:e.jsx(Q,{className:"text-primary underline-offset-4 hover:underline",to:k(s.workflow_id),children:s.workflow_id})}),e.jsx(j,{children:e.jsx(V,{status:s.status})}),e.jsx(j,{children:e.jsx(T,{value:s.started_at})}),e.jsx(j,{children:e.jsx(T,{value:s.ended_at})})]},s.workflow_id))})]})}function be(t){const{namespace:s,workflowId:n}=t,[l,a]=o.useState("idle"),[d,u]=o.useState(null);return{submit:o.useCallback(async(c,m)=>{if(s===null){const i=new Error("cannot signal without a namespace scope");a("error"),u(i);return}const x=t.apiClient??z({namespace:s});a("submitting"),u(null);try{await x.sendSignal(n,c,{namespace:s},m),a("settled")}catch(i){const r=i instanceof Error?i:new Error(String(i));throw u(r),a("error"),r}},[s,n,t.apiClient]),submitState:l,error:d}}function Ie({namespace:t,workflowId:s}){const n=J({workflowId:s}),l=Y({enabled:n.isSuccess,history:n.data??[],workflowId:s}),{attempts:a,loadState:d,loadError:u,refresh:h}=X({workflowId:s,namespace:t}),c=n.isSuccess,m=l.events.length,x=o.useRef(null);o.useEffect(()=>{c&&(x.current!==null&&m!==x.current&&h(),x.current=m)},[c,m,h]);const i=de({isTerminal:l.isTerminal,attemptsReady:d==="ready"||d==="refreshing",liveAttemptCount:a.length}),r=ue(a),g=o.useMemo(()=>r===null||t===null?null:{namespace:t,workflowId:s,runId:r.runId,activityId:r.activityId,attempt:r.attempt},[r,t,s]),[L,R]=o.useState(null);o.useEffect(()=>{g!==null&&R(g)},[g]);const P=g??L,f=Z({namespace:t}),b=be({namespace:t,workflowId:s}),[F,C]=o.useState(!1),E=o.useRef(null);if(ee(re.assistantFocusChat,()=>{C(!0),requestAnimationFrame(()=>{E.current?.querySelector("textarea")?.focus()})}),t===null)return e.jsx(N,{description:"Select a namespace to open this assistant session.",title:"No namespace selected"});if(n.isError)return e.jsx(S,{error:n.error,onRetry:()=>{n.refetch()},title:"Could not load this assistant session"});if(d==="error"&&i!=="ended")return e.jsx(S,{error:u,onRetry:h,title:"Could not enumerate this session's live attempts"});const A=()=>{r!==null&&f.submit({workflowId:s,runId:r.runId,activityId:r.activityId,attempt:r.attempt,kind:{kind:"Cancel",reason:"operator cancelled from assistant panel"}}).catch(()=>{})},O=p=>{if(i==="live"&&r!==null){f.submit({workflowId:s,runId:r.runId,activityId:r.activityId,attempt:r.attempt,kind:{kind:"InjectMessage",text:p,priority:{priority:"Interrupt"}}}).catch(()=>{});return}i==="awaiting"&&b.submit(I,ae(p)).catch(()=>{})},M=p=>{(p==="shutdown"||p==="kill")&&A()},D=()=>{b.submit(I,ne()).catch(()=>{})},H=r?.capabilities.supported.some(p=>p.primitive==="Cancel")??!1;return e.jsxs("section",{className:"flex flex-col gap-4","data-testid":"assistant-session",children:[e.jsxs("header",{className:"flex flex-wrap items-center justify-between gap-3",children:[e.jsxs("div",{className:"min-w-0 space-y-1",children:[e.jsx("h1",{className:"font-semibold text-foreground text-lg",children:"Assistant session"}),e.jsx("p",{className:"font-mono text-muted-foreground text-xs",children:s})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(Se,{mode:i}),H?e.jsx(y,{className:"h-7 px-3 text-xs",disabled:f.submitState==="submitting",onClick:A,type:"button",variant:"destructive",children:"Cancel attempt"}):null,e.jsx(y,{className:"h-7 px-3 text-xs",disabled:i==="ended"||b.submitState==="submitting",onClick:D,type:"button",variant:"outline",children:"End session"})]})]}),e.jsx(te,{target:P}),e.jsx("div",{ref:E,children:e.jsx(ve,{chatExpanded:F,mode:i,onEscalate:M,onExpandedChange:C,onSubmit:O,workflowId:s})}),e.jsx(ye,{interveneError:f.error,outcome:f.lastOutcome,signalError:b.error})]})}function ve({mode:t,workflowId:s,chatExpanded:n,onExpandedChange:l,onSubmit:a,onEscalate:d}){return t==="connecting"?e.jsx("p",{className:"text-muted-foreground text-xs",children:"Connecting to session…"}):t==="ended"?e.jsx("p",{className:"text-muted-foreground text-xs","data-testid":"assistant-session-ended",children:"Session ended — start a new one from the assistant list."}):e.jsx(se,{capabilities:t==="live"?["interrupt"]:["continue"],draftKey:`assistant:${s}`,expanded:n,onEscalate:d,onExpandedChange:l,onSubmit:a,placeholder:me[t],status:xe[t],streaming:t==="live"})}const je={live:{label:"Agent working",className:"border-live/30 bg-live-glow text-live"},awaiting:{label:"Awaiting continuation",className:"border-success/30 bg-success-glow text-success"},connecting:{label:"Connecting",className:"border-border bg-surface-hover text-muted-foreground"},ended:{label:"Ended",className:"border-border bg-surface-hover text-muted-foreground"}};function Se({mode:t}){const s=je[t];return e.jsx(_,{className:s.className,"data-mode":t,variant:"outline",children:s.label})}function ye({outcome:t,interveneError:s,signalError:n}){const l=s??n;if(l!==null)return e.jsx("p",{className:"text-destructive text-xs","data-testid":"assistant-delivery-error",role:"alert",children:l.message});if(t===null||t.outcome==="Applied")return null;const a=t.outcome==="CapabilityNotSupported"?`Not supported: ${t.primitive.primitive}`:`Too late: ${t.detail}`;return e.jsx(_,{className:"self-start border-warning/40 bg-warning-glow text-warning","data-testid":"assistant-delivery-outcome",variant:"outline",children:a})}const Ne="h-9 rounded-md border border-input bg-transparent px-3 text-sm outline-none focus:border-muted-foreground",Ce="min-h-20 rounded-md border border-input bg-transparent px-3 py-2 text-sm outline-none focus:border-muted-foreground";function Ee({namespace:t,apiClient:s}){const n=Ae(),l=ie(),[a,d]=o.useState(""),[u,h]=o.useState(""),c=ce({namespace:t,apiClient:s}),m=t!==null&&a.trim().length>0&&!c.isPending;function x(i){i.preventDefault(),m&&c.mutateAsync({workflowType:oe,input:le(a.trim(),u.trim())}).then(r=>l(k(r.workflowId))).catch(()=>{})}return e.jsxs("div",{className:"space-y-3",children:[e.jsxs("form",{className:"grid gap-3 rounded-lg border border-border p-4",onSubmit:x,children:[e.jsxs("label",{className:"flex flex-col gap-2 font-medium text-sm",htmlFor:n.objective,children:["Objective",e.jsx("textarea",{className:Ce,id:n.objective,onChange:i=>d(i.currentTarget.value),placeholder:"What should the assistant work on?",value:a})]}),e.jsxs("label",{className:"flex flex-col gap-2 font-medium text-sm",htmlFor:n.repoPath,children:["Repository path (optional)",e.jsx("input",{className:Ne,id:n.repoPath,onChange:i=>h(i.currentTarget.value),placeholder:"/path/to/repo — blank pins no repo",value:u})]}),e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(y,{disabled:!m,type:"submit",children:c.isPending?"Starting…":"Start session"}),t===null?e.jsx("p",{className:"text-muted-foreground text-xs",children:"Select a namespace to start a session."}):null]})]}),c.isError?e.jsx(S,{error:c.error,title:"Could not start the session"}):null]})}function Ae(){const t=o.useId();return{objective:`${t}-objective`,repoPath:`${t}-repo-path`}}function ke({namespace:t,client:s}){return e.jsxs("section",{className:"space-y-6",children:[e.jsxs("header",{className:"space-y-1",children:[e.jsx("h1",{className:"font-semibold text-foreground text-lg",children:"Assistant"}),e.jsx("p",{className:"text-muted-foreground text-sm",children:"Round-based agent sessions on the assistant workflow — start one, watch it work, steer it, continue or end it."})]}),e.jsx(Ee,{apiClient:s,namespace:t}),e.jsx(fe,{client:s,namespace:t})]})}export{I as ASSISTANT_CONTINUE_SIGNAL,oe as ASSISTANT_WORKFLOW_TYPE,fe as AssistantSessionList,ge as AssistantSessionRows,Ie as AssistantSessionView,ke as AssistantView,me as MODE_PLACEHOLDER,xe as MODE_STATUS,Se as ModeChip,Ee as NewAssistantSessionForm,ve as SessionDock,ne as assistantEndPayload,W as assistantSessionsFilter,le as assistantStartInput,de as deriveAssistantMode,ue as selectAssistantAttempt,pe as sortSessionsNewestFirst,be as useAssistantSignal};