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