import{a as W}from"./DCp3GJQW.js";import{_ as H}from"./CLsTGSNv.js";import{_ as q}from"./DKIXA6Zt.js";import{u as G}from"./m0N__S0x.js";import{l as Y,J as Q,N as x,i as T,k as O,ah as w,a5 as s,g as M,h as j,f as o,F as X,V as Z,j as y,a0 as S,D as ee,e as N,U as p,C as te,ad as ne}from"./D7LY3N47.js";import{_ as ae}from"./DlAUqK2U.js";import"./DL7l9XXk.js";import"./CYtbVgsc.js";import"./DqW9UKXb.js";const se={class:"run-page"},le={class:"flex items-center justify-between gap-3 flex-wrap"},oe={class:"text-xs text-gray-500 mt-1"},re={class:"actions"},ie={class:"status-bar"},ue={class:"status-text"},ce={key:2,class:"empty"},de="aidev-ui-token",fe=Y({__name:"run",setup(ve){const z=G(),U=ne(),V=N(()=>U.public.aidevCwd),D=[{value:"open",label:"Open",color:"primary"},{value:"pending",label:"Pending",color:"sky"},{value:"review",label:"Review",color:"amber"},{value:"all",label:"All",color:"gray"}],m=p([]),l=p(!1),r=p(!1),i=p(null),g=p(null),u=p(""),R=p(null);let c=null;const I=N(()=>m.value.join(`
`)),J=N(()=>l.value?"running":u.value?"error":g.value?g.value.code===0?"done":"failed":"idle"),L=N(()=>{if(l.value&&i.value)return`Running aidev run ${i.value}…`;if(g.value){const{code:n,signal:e,durationMs:t}=g.value,a=A(t);return e?`Terminated by signal ${e} after ${a}`:`Exited with code ${n??"null"} after ${a}`}return"Idle"});function A(n){if(n<1e3)return`${n}ms`;const e=n/1e3;if(e<60)return`${e.toFixed(1)}s`;const t=Math.floor(e/60),a=Math.round(e-t*60);return`${t}m ${a}s`}function _(n,e){m.value.push(n?`${n} ${e}`:e),te(()=>{const t=R.value;t&&(t.scrollTop=t.scrollHeight)})}function P(n,e){if(n==="stdout")_("",e);else if(n==="stderr")_("[stderr]",e);else if(n==="exit"){try{const t=JSON.parse(e);g.value=t;const a=A(t.durationMs);_("",`--- exit code ${t.code??"null"}${t.signal?`, signal ${t.signal}`:""} (${a}) ---`)}catch{_("",`--- exit ${e} ---`)}l.value=!1,i.value=null,r.value=!1}else n==="error"&&(_("[error]",e||"unknown error"),l.value=!1,i.value=null,r.value=!1)}async function F(n){if(l.value)return;const e=localStorage.getItem(de);if(!e){u.value="Not authenticated — refresh and log in again.";return}u.value="",m.value=[],g.value=null,l.value=!0,i.value=n;const t=new AbortController;c=t;try{const a=await fetch("/api/run",{method:"POST",headers:{Authorization:`Bearer ${e}`,Accept:"text/event-stream","Content-Type":"application/json"},body:JSON.stringify({status:n}),signal:t.signal});if(!a.ok||!a.body){const h=await a.text().catch(()=>"");let C=h;try{const v=JSON.parse(h);C=v.statusMessage||v.message||h}catch{}u.value=`HTTP ${a.status}: ${C||a.statusText}`,l.value=!1,i.value=null,c===t&&(c=null);return}const E=a.body.getReader(),d=new TextDecoder("utf-8");let f="",$="message",k=[];const B=()=>{k.length===0&&$==="message"||(P($,k.join(`
`)),$="message",k=[])};for(;;){const{value:h,done:C}=await E.read();if(C)break;f+=d.decode(h,{stream:!0});let v=f.indexOf(`
`);for(;v!==-1;){const b=f.slice(0,v).replace(/\r$/,"");f=f.slice(v+1),b===""?B():b.startsWith(":")||(b.startsWith("event:")?$=b.slice(6).trim()||"message":b.startsWith("data:")&&k.push(b.slice(5).replace(/^ /,""))),v=f.indexOf(`
`)}}B()}catch(a){a?.name!=="AbortError"&&(u.value=a instanceof Error?a.message:String(a))}finally{l.value=!1,i.value=null,r.value=!1,c===t&&(c=null)}}async function K(){if(!(!l.value||r.value)){r.value=!0;try{await z("/api/run/cancel",{method:"POST"}),_("","--- cancel requested (SIGTERM) ---")}catch(n){r.value=!1,u.value=n instanceof Error?n.message:String(n)}}}return Q(()=>{c&&(c.abort(),c=null)}),(n,e)=>{const t=W,a=H,E=q;return x(),T("div",se,[O(E,null,{header:w(()=>[o("div",le,[o("div",null,[e[5]||(e[5]=o("h1",{class:"text-lg font-semibold"},"Run",-1)),o("p",oe,[e[1]||(e[1]=y(" Spawns ",-1)),e[2]||(e[2]=o("code",null,"aidev run <status>",-1)),e[3]||(e[3]=y(" in ",-1)),o("code",null,S(s(V)||"—"),1),e[4]||(e[4]=y(" and streams output live below. ",-1))])]),s(m).length>0&&!s(l)?(x(),M(t,{key:0,size:"xs",color:"gray",variant:"ghost",onClick:e[0]||(e[0]=d=>m.value=[])},{default:w(()=>[...e[6]||(e[6]=[y(" Clear output ",-1)])]),_:1})):j("",!0)])]),default:w(()=>[s(u)?(x(),M(a,{key:0,color:"red",variant:"soft",title:s(u),class:"mb-3"},null,8,["title"])):j("",!0),o("div",re,[(x(),T(X,null,Z(D,d=>O(t,{key:d.value,size:"md",color:d.color,loading:s(l)&&s(i)===d.value,disabled:s(l),class:"action-btn",onClick:f=>F(d.value)},{default:w(()=>[y(S(d.label),1)]),_:2},1032,["color","loading","disabled","onClick"])),64)),O(t,{size:"md",color:"red",variant:"soft",disabled:!s(l)||s(r),loading:s(r),class:"cancel-btn",onClick:K},{default:w(()=>[...e[7]||(e[7]=[y(" Cancel ",-1)])]),_:1},8,["disabled","loading"])]),o("div",ie,[o("span",{class:ee(["status-dot",`status-dot--${s(J)}`])},null,2),o("span",ue,S(s(L)),1)]),s(m).length>0?(x(),T("pre",{key:1,ref_key:"viewer",ref:R,class:"viewer"},[o("code",null,S(s(I)),1)],512)):(x(),T("p",ce,"No output yet. Click a status above to start a run."))]),_:1})])}}}),$e=ae(fe,[["__scopeId","data-v-3bd8ab3c"]]);export{$e as default};
