import{i as x,a as v,f as b,s as H}from"../chunks/3MMtouT7.js";import{p as ge,s as g,o as we,q as ye,v as be,y as _e,b as o,g as e,$ as Se,c as w,r as y,w as R,d as _,z as Q,t as X}from"../chunks/Ce0VRwE1.js";import{h as $e}from"../chunks/DFPEaIHz.js";import{d as Y,B as ke}from"../chunks/BTu-2gkj.js";import{p as L}from"../chunks/CBvZimn-.js";import{g as Ie,s as Z,S as W}from"../chunks/Dqgg-a0I.js";import"../chunks/BCwT4WOg.js";import{C as je}from"../chunks/BsNWiLas.js";var Ce=b('<meta name="description" content="Session conversation view"/>'),Oe=b('<div class="loading-container"><!> <div class="chat-container"><!> <!> <!> <!></div></div>'),Me=b('<div class="session-back-row svelte-1a2wdfb"><a class="back-link">← Back</a></div> <p class="error-text svelte-1a2wdfb"> </p>',1),Ne=b('<span class="resume-status svelte-1a2wdfb">Connecting...</span>'),Ae=b('<span class="resume-status svelte-1a2wdfb">Resuming session...</span>'),Pe=b('<span class="connection-dot connected svelte-1a2wdfb"></span>'),Te=b('<span class="connection-dot reconnecting svelte-1a2wdfb"></span>'),Re=b('<div class="load-earlier-row svelte-1a2wdfb"><!></div>'),We=b('<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),De=b('<main class="main session-page-main svelte-1a2wdfb"><!></main>');function qe(ee,te){ge(te,!0);let p=g(null),a=g(we([])),I=g(!0),N=g(null),D=g(!1),A=g(!1),P=g(200),j=g(null),k=g("idle"),c=g("idle"),S=null,$=null,C=null,O=null,u=!1;const B=R(()=>L.params.id),M=R(()=>L.url.searchParams.get("project")||""),se=R(()=>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 q(){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 E(t,s){if(u||C&&(C.readyState===WebSocket.OPEN||C.readyState===WebSocket.CONNECTING))return;const n=await q();if(!n||u)return;o(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}o(k,"connected"),r.send(JSON.stringify({sessionId:s,type:"subscribe"}))},r.onmessage=d=>{if(!u)try{const f=JSON.parse(d.data);ne(f)}catch{}},r.onclose=()=>{u||(o(k,"disconnected"),C=null,e(j)&&(O&&clearTimeout(O),O=setTimeout(()=>{O=null,!u&&e(j)&&E(`/ws/session/${e(j)}`,e(j))},2e3)))},r.onerror=()=>{u||o(k,"disconnected")},C=r}async function F(t){if(u)return;const s=await q();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 ne(t){if(t.type==="history"){const s=t.messages||[];o(a,s.map(n=>({id:n.id,parts:n.content,role:n.role,timestamp:n.timestamp})),!0),o(D,!1),e(c)==="resuming"&&(o(c,"ready"),S&&$?.readyState===WebSocket.OPEN&&($.send(JSON.stringify({data:`${S}
`,type:"input"})),S=null))}else if(t.type==="message")o(a,[...e(a),{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(a).length>0?e(a)[e(a).length-1]:null;n?.role==="assistant"?o(a,[...e(a).slice(0,-1),{...n,parts:[...n.parts,s]}],!0):o(a,[...e(a),{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(a).length>0?e(a)[e(a).length-1]:null;n?.role==="system"?o(a,[...e(a).slice(0,-1),{...n,parts:[...n.parts,s]}],!0):o(a,[...e(a),{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(a).length>0?e(a)[e(a).length-1]:null;n?.role==="assistant"?o(a,[...e(a).slice(0,-1),{...n,parts:[...n.parts,s]}],!0):o(a,[...e(a),{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"&&o(c,"idle")}async function oe(){if(!e(p)||u){o(c,"idle");return}const t=T();if(!t){o(c,"idle");return}o(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(B)}),headers:{Authorization:`Bearer ${t.apiKey}`,"Content-Type":"application/json"},method:"POST"});if(!n.ok||u){o(c,"idle");return}const i=await n.json();o(j,i.terminalId,!0),o(c,"resuming"),E(i.sessionWs,i.terminalId),F(i.ws)}catch{o(c,"idle")}}function ae(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 ie(){e(a).length===0&&o(I,!0);try{const t=T();if(!t){o(N,"No configuration found. Please configure settings first."),o(I,!1);return}const s=e(B)||"",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 h=await fetch("/api/sessions",{headers:{Authorization:`Bearer ${t.apiKey}`}});if(h.ok){const K=await h.json();for(const U of K.projects||[])if((U.sessions||[]).find(z=>z.id===s)){r=await fetch(`/api/sessions?id=${encodeURIComponent(s)}&project=${encodeURIComponent(U.id)}`,{headers:{Authorization:`Bearer ${t.apiKey}`}});break}}}if(!r.ok){o(N,"Session not found"),o(I,!1);return}const d=await r.json();o(p,d.session,!0);const f=Array.isArray(d.messages)?d.messages:[];o(a,f,!0),o(D,f.length>=e(P)),Z(`shooter_session_${s}`,{messages:f,session:d.session})}catch{o(N,"Failed to load session")}o(I,!1)}async function re(){if(e(A))return;const t=T();if(t){o(A,!0);try{const s=e(P)+200,n=e(B)||"",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(),h=Array.isArray(f.messages)?f.messages:[];o(a,h,!0),o(P,s),o(D,h.length>=s),Z(`shooter_session_${n}`,{messages:h,session:f.session})}catch{}finally{o(A,!1)}}}ye(()=>{const t=Ie(`shooter_session_${L.params.id}`);return t&&(o(p,t.session,!0),o(a,Array.isArray(t.messages)?t.messages:[],!0),o(I,!1)),ie().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(B)}),headers:{Authorization:`Bearer ${s.apiKey}`,"Content-Type":"application/json"},method:"POST"});if(i.ok&&!u){const r=await i.json();o(j,r.terminalId,!0),o(c,"resuming"),E(r.sessionWs,r.terminalId),F(r.ws)}}catch{}}),()=>{u=!0,O&&clearTimeout(O),C?.close(),$?.close()}});var J=De();$e("1a2wdfb",t=>{var s=Ce();_e(()=>{Se.title=`${(e(p)?.title||"Session")??""} - Shooter`}),v(t,s)});var ce=w(J);{var le=t=>{var s=Oe(),n=w(s);W(n,{classes:"shimmer-header"});var i=_(n,2),r=w(i);W(r,{classes:"shimmer-bubble shimmer-bubble-user"});var d=_(r,2);W(d,{classes:"shimmer-bubble shimmer-bubble-assistant"});var f=_(d,2);W(f,{classes:"shimmer-bubble shimmer-bubble-user-short"});var h=_(f,2);W(h,{classes:"shimmer-bubble shimmer-bubble-assistant-wide"}),y(i),y(s),v(t,s)},de=t=>{var s=Me(),n=Q(s),i=w(n);y(n);var r=_(n,2),d=w(r,!0);y(r),X(()=>{Y(i,"href",e(M)?`/project?id=${e(M)}`:"/"),H(d,e(N))}),v(t,s)},ue=t=>{var s=We(),n=Q(s),i=w(n),r=w(i),d=_(r,2);{var f=l=>{var m=Ne();v(l,m)},h=l=>{var m=Ae();v(l,m)},K=l=>{var m=Pe();v(l,m)},U=l=>{var m=Te();v(l,m)};x(d,l=>{e(c)==="connecting"?l(f):e(c)==="resuming"?l(h,1):e(k)==="connected"?l(K,2):e(k)==="reconnecting"&&l(U,3)})}y(i);var z=_(i,2),fe=w(z,!0);y(z),y(n);var G=_(n,2),V=w(G);{var pe=l=>{var m=Re(),he=w(m);{let ve=R(()=>e(A)?"Loading...":"Load earlier messages");ke(he,{get text(){return e(ve)},classes:"btn-ghost btn-sm",get disabled(){return e(A)},onclick:re})}y(m),v(l,m)};x(V,l=>{e(D)&&l(pe)})}var me=_(V,2);{let l=R(()=>[...e(a)].reverse());je(me,{get messages(){return e(l)},newestFirst:!0,get connectionState(){return e(k)},showInput:!0,get sendDisabled(){return e(se)},onSendInput:ae,sessionEnded:!1})}y(G),X(()=>{Y(r,"href",e(M)?`/project?id=${e(M)}`:"/"),H(fe,e(p).title)}),v(t,s)};x(ce,t=>{e(I)&&e(a).length===0?t(le):e(N)?t(de,1):e(p)&&t(ue,2)})}y(J),v(ee,J),be()}export{qe as component};
