import{i as K,a as h,f as g,s as V}from"../chunks/BPDiEZo0.js";import{p as ge,s as v,q as we,v as ye,w as be,z as Se,b as s,g as e,$ as _e,c as _,r as $,x as P,A as H,d as D,t as Q}from"../chunks/DWmC0QM7.js";import{h as $e}from"../chunks/2rBV5OkJ.js";import{d as X,B as ke}from"../chunks/B1bOvemT.js";import{p as x}from"../chunks/BjYr_-Ss.js";import{g as Ie,s as Y}from"../chunks/xs1Xl3_e.js";import"../chunks/BDcFu3l7.js";import{s as Z}from"../chunks/BdtLzPpO.js";import{C as je}from"../chunks/i5iZvmIH.js";var Ce=g('<meta name="description" content="Session conversation view"/>'),Oe=g('<div class="loading-container" aria-live="polite" aria-busy="true"><div class="shimmer shimmer-header" aria-hidden="true"></div> <div class="chat-container"><div class="shimmer shimmer-bubble shimmer-bubble-user" aria-hidden="true"></div> <div class="shimmer shimmer-bubble shimmer-bubble-assistant" aria-hidden="true"></div> <div class="shimmer shimmer-bubble shimmer-bubble-user-short" aria-hidden="true"></div> <div class="shimmer shimmer-bubble shimmer-bubble-assistant-wide" aria-hidden="true"></div></div></div>'),Me=g('<div class="session-back-row svelte-1a2wdfb"><a class="back-link">← Back</a></div> <p class="error-text svelte-1a2wdfb"> </p>',1),Ne=g('<span class="resume-status svelte-1a2wdfb">Connecting...</span>'),Ae=g('<span class="resume-status svelte-1a2wdfb">Resuming session...</span>'),Te=g('<span class="connection-dot connected svelte-1a2wdfb"></span>'),Pe=g('<span class="connection-dot reconnecting svelte-1a2wdfb"></span>'),De=g('<div class="load-earlier-row svelte-1a2wdfb"><!></div>'),Re=g('<div class="session-header svelte-1a2wdfb"><div class="session-header-row svelte-1a2wdfb"><a class="back-link">← Back</a> <!></div> <h1 class="session-title svelte-1a2wdfb"> </h1></div> <div class="session-chat-container svelte-1a2wdfb"><!> <!></div>',1),We=g('<main class="main session-page-main svelte-1a2wdfb"><!></main>');function Fe(ee,te){ge(te,!0);let f=v(null),i=v(we([])),w=v(!0),I=v(null),R=v(!1),N=v(!1),A=v(200),j=v(null),k=v("idle"),c=v("idle"),y=null,b=null,C=null,O=null,d=!1;const W=P(()=>x.params.id),M=P(()=>x.url.searchParams.get("project")||""),se=P(()=>e(c)==="connecting"||e(c)==="resuming");function T(){try{const t=localStorage.getItem("shooter_config");return t?JSON.parse(t):null}catch{return null}}async function L(){const t=T();if(!t)return null;try{const n=await fetch("/api/ws-ticket",{headers:{Authorization:`Bearer ${t.apiKey}`},method:"POST"});return n.ok?(await n.json()).ticket:null}catch{return null}}async function z(t,n){if(d||C&&(C.readyState===WebSocket.OPEN||C.readyState===WebSocket.CONNECTING))return;const a=await L();if(!a||d)return;s(k,"connecting");const o=window.location.protocol==="https:"?"wss:":"ws:",r=new WebSocket(`${o}//${window.location.host}${t}?ticket=${a}`);r.onopen=()=>{if(d){r.close();return}s(k,"connected"),r.send(JSON.stringify({sessionId:n,type:"subscribe"}))},r.onmessage=u=>{if(!d)try{const p=JSON.parse(u.data);ne(p)}catch{}},r.onclose=()=>{d||(s(k,"disconnected"),C=null,e(j)&&(O&&clearTimeout(O),O=setTimeout(()=>{O=null,!d&&e(j)&&z(`/ws/session/${e(j)}`,e(j))},2e3)))},r.onerror=()=>{d||s(k,"disconnected")},C=r}async function q(t){if(d)return;const n=await L();if(!n||d)return;const a=window.location.protocol==="https:"?"wss:":"ws:",o=new WebSocket(`${a}//${window.location.host}${t}?ticket=${n}`);o.onopen=()=>{if(d){o.close();return}b=o,e(c)==="ready"&&y&&(o.send(JSON.stringify({data:`${y}
`,type:"input"})),y=null)},o.onclose=()=>{b===o&&(b=null)}}function ne(t){if(t.type==="history"){const n=t.messages||[];s(i,n.map(a=>({id:a.id,parts:a.content,role:a.role,timestamp:a.timestamp})),!0),s(R,!1),e(c)==="resuming"&&(s(c,"ready"),y&&b?.readyState===WebSocket.OPEN&&(b.send(JSON.stringify({data:`${y}
`,type:"input"})),y=null))}else if(t.type==="message")s(i,[...e(i),{id:`msg-${Date.now()}-${Math.random().toString(36).slice(2,8)}`,parts:t.content||[],role:t.role||"assistant",timestamp:t.timestamp||new Date().toISOString()}],!0);else if(t.type==="tool-use"){const n={id:t.id||`tool-${Date.now()}`,input:t.input||{},toolName:t.name,type:"tool_use"},a=e(i).length>0?e(i)[e(i).length-1]:null;a?.role==="assistant"?s(i,[...e(i).slice(0,-1),{...a,parts:[...a.parts,n]}],!0):s(i,[...e(i),{id:`tool-${Date.now()}-${Math.random().toString(36).slice(2,8)}`,parts:[n],role:"assistant",timestamp:new Date().toISOString()}],!0)}else if(t.type==="tool-result"){const n={isError:t.isError||!1,output:t.output||"",toolUseId:t.id,type:"tool_result"},a=e(i).length>0?e(i)[e(i).length-1]:null;a?.role==="system"?s(i,[...e(i).slice(0,-1),{...a,parts:[...a.parts,n]}],!0):s(i,[...e(i),{id:`result-${Date.now()}-${Math.random().toString(36).slice(2,8)}`,parts:[n],role:"system",timestamp:new Date().toISOString()}],!0)}else if(t.type==="thinking"){const n={content:t.text||"",type:"thinking"},a=e(i).length>0?e(i)[e(i).length-1]:null;a?.role==="assistant"?s(i,[...e(i).slice(0,-1),{...a,parts:[...a.parts,n]}],!0):s(i,[...e(i),{id:`think-${Date.now()}-${Math.random().toString(36).slice(2,8)}`,parts:[n],role:"assistant",timestamp:new Date().toISOString()}],!0)}else t.type==="session-end"&&s(c,"idle")}async function ae(){if(!e(f)||d){s(c,"idle");return}const t=T();if(!t){s(c,"idle");return}s(c,"connecting");try{const n=Z(e(f).source),a=await fetch("/api/sessions/connect",{body:JSON.stringify({command:n,cwd:e(f).projectPath,sessionId:e(W)}),headers:{Authorization:`Bearer ${t.apiKey}`,"Content-Type":"application/json"},method:"POST"});if(!a.ok||d){s(c,"idle");return}const o=await a.json();s(j,o.terminalId,!0),s(c,"resuming"),z(o.sessionWs,o.terminalId),q(o.ws)}catch{s(c,"idle")}}function ie(t){if(t.trim()){if(e(c)==="ready"&&b?.readyState===WebSocket.OPEN){b.send(JSON.stringify({data:`${t}
`,type:"input"}));return}if(e(c)==="connecting"||e(c)==="resuming"){y=t;return}y=t,ae()}}async function oe(){e(i).length===0&&s(w,!0);try{const t=T();if(!t){s(I,"No configuration found. Please configure settings first."),s(w,!1);return}const n=e(W)??"",a=e(M);if(!n){s(I,"No session ID"),s(w,!1);return}const o=a?`id=${encodeURIComponent(n)}&project=${encodeURIComponent(a)}&limit=${e(A)}`:`id=${encodeURIComponent(n)}&limit=${e(A)}`;let r=await fetch(`/api/sessions?${o}`,{headers:{Authorization:`Bearer ${t.apiKey}`}});if(r.status===404&&!a){const S=await fetch("/api/sessions",{headers:{Authorization:`Bearer ${t.apiKey}`}});if(S.ok){const J=await S.json();for(const B of J.projects||[])if((B.sessions||[]).find(U=>U.id===n)){r=await fetch(`/api/sessions?id=${encodeURIComponent(n)}&project=${encodeURIComponent(B.id)}`,{headers:{Authorization:`Bearer ${t.apiKey}`}});break}}}if(!r.ok){s(I,"Session not found"),s(w,!1);return}const u=await r.json(),p=Array.isArray(u.messages)?u.messages:[];Y(`shooter_session_${n}`,{messages:p,session:u.session}),s(f,u.session,!0),s(i,p,!0),s(R,p.length>=e(A)),s(w,!1);return}catch{s(I,"Failed to load session")}s(w,!1)}async function re(){if(e(N))return;const t=T();if(t){s(N,!0);try{const n=e(A)+200,a=e(W)??"",o=e(M);if(!a)return;const r=[`id=${encodeURIComponent(a)}`,`limit=${n}`];o&&r.push(`project=${encodeURIComponent(o)}`);const u=await fetch(`/api/sessions?${r.join("&")}`,{headers:{Authorization:`Bearer ${t.apiKey}`}});if(!u.ok)return;const p=await u.json(),S=Array.isArray(p.messages)?p.messages:[];s(i,S,!0),s(A,n),s(R,S.length>=n),Y(`shooter_session_${a}`,{messages:S,session:p.session})}catch{}finally{s(N,!1)}}}ye(()=>{const t=Ie(`shooter_session_${x.params.id}`);return t&&(s(f,t.session,!0),s(i,Array.isArray(t.messages)?t.messages:[],!0),s(w,!1)),oe().then(async()=>{if(!e(f)||d)return;const n=T();if(n)try{const a=Z(e(f).source),o=await fetch("/api/sessions/connect",{body:JSON.stringify({command:a,cwd:e(f).projectPath,noCreate:!0,sessionId:e(W)}),headers:{Authorization:`Bearer ${n.apiKey}`,"Content-Type":"application/json"},method:"POST"});if(o.ok&&!d){const r=await o.json();s(j,r.terminalId,!0),s(c,"resuming"),z(r.sessionWs,r.terminalId),q(r.ws)}}catch{}}),()=>{d=!0,O&&clearTimeout(O),C?.close(),b?.close()}});var E=We();$e("1a2wdfb",t=>{var n=Ce();Se(()=>{_e.title=`${(e(f)?.title||"Session")??""} - Shooter`}),h(t,n)});var ce=_(E);{var le=t=>{var n=Oe();h(t,n)},de=t=>{var n=Me(),a=H(n),o=_(a);$(a);var r=D(a,2),u=_(r,!0);$(r),Q(()=>{X(o,"href",e(M)?`/project?id=${e(M)}`:"/"),V(u,e(I))}),h(t,n)},ue=t=>{var n=Re(),a=H(n),o=_(a),r=_(o),u=D(r,2);{var p=l=>{var m=Ne();h(l,m)},S=l=>{var m=Ae();h(l,m)},J=l=>{var m=Te();h(l,m)},B=l=>{var m=Pe();h(l,m)};K(u,l=>{e(c)==="connecting"?l(p):e(c)==="resuming"?l(S,1):e(k)==="connected"?l(J,2):e(k)==="reconnecting"&&l(B,3)})}$(o);var U=D(o,2),fe=_(U,!0);$(U),$(a);var F=D(a,2),G=_(F);{var pe=l=>{var m=De(),he=_(m);{let ve=P(()=>e(N)?"Loading...":"Load earlier messages");ke(he,{get text(){return e(ve)},classes:"btn-ghost btn-sm",get disabled(){return e(N)},onclick:re})}$(m),h(l,m)};K(G,l=>{e(R)&&l(pe)})}var me=D(G,2);{let l=P(()=>[...e(i)].reverse());je(me,{get messages(){return e(l)},newestFirst:!0,get connectionState(){return e(k)},showInput:!0,get sendDisabled(){return e(se)},onSendInput:ie,sessionEnded:!1})}$(F),Q(()=>{X(r,"href",e(M)?`/project?id=${e(M)}`:"/"),V(fe,e(f).title)}),h(t,n)};K(ce,t=>{e(w)&&e(i).length===0?t(le):e(I)?t(de,1):e(f)&&t(ue,2)})}$(E),h(ee,E),be()}export{Fe as component};
