import{b as xe,d as Qe,c as K,a as o,f as _,t as ye}from"../chunks/vfPE-AD7.js";import{t as V,h as be,a as we,V as We,aI as Xe,I as ke,al as Ze,aA as et,aH as tt,aB as rt,O as at,ab as Z,i as E,p as Ie,e as a,r as s,c as Te,g as e,u as J,s as U,k as st,o as ot,d as x,f as p,n as oe}from"../chunks/DhUiKsxN.js";import{s as Y,e as nt}from"../chunks/Dsv8gxIT.js";import{l as ve,b as ce,p as it,i as T,s as lt,a as dt}from"../chunks/DUil1G3I.js";import{I as fe,a as ue,f as de,e as Pe}from"../chunks/QNXDtpsh.js";import{p as vt}from"../chunks/B2gI4nmm.js";import{s as ne,L as Se}from"../chunks/SjYvl9yQ.js";import{p as Me}from"../chunks/DEcqogKB.js";import{d as Ne,S as ct,B as ie,C as le,I as ft,T as ut}from"../chunks/BWzyK28a.js";import"../chunks/1jpszzm8.js";function pt(N,f,w=!1,k=!1,P=!1){var n=N,i="";V(()=>{var d=We;if(i===(i=f()??"")){be&&we();return}if(d.nodes!==null&&(Xe(d.nodes.start,d.nodes.end),d.nodes=null),i!==""){if(be){ke.data;for(var u=we(),B=u;u!==null&&(u.nodeType!==Ze||u.data!=="");)B=u,u=et(u);if(u===null)throw tt(),rt;xe(ke,B),n=at(u);return}var C=i+"";w?C=`<svg>${C}</svg>`:k&&(C=`<math>${C}</math>`);var h=Qe(C);if((w||k)&&(h=Z(h)),xe(Z(h),h.lastChild),w||k)for(;Z(h);)n.before(Z(h));else n.before(h)}})}function q(N,f){const w=ve(f,["children","$$slots","$$events","$$legacy"]);/**
 * @license lucide-svelte v0.469.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const k=[["path",{d:"M12 8V4H8"}],["rect",{width:"16",height:"12",x:"4",y:"8",rx:"2"}],["path",{d:"M2 14h2"}],["path",{d:"M20 14h2"}],["path",{d:"M15 13v2"}],["path",{d:"M9 13v2"}]];fe(N,ce({name:"bot"},()=>w,{get iconNode(){return k},children:(P,n)=>{var i=K(),d=E(i);ue(d,f,"default",{}),o(P,i)},$$slots:{default:!0}}))}function _t(N,f){const w=ve(f,["children","$$slots","$$events","$$legacy"]);/**
 * @license lucide-svelte v0.469.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const k=[["path",{d:"M14.536 21.686a.5.5 0 0 0 .937-.024l6.5-19a.496.496 0 0 0-.635-.635l-19 6.5a.5.5 0 0 0-.024.937l7.93 3.18a2 2 0 0 1 1.112 1.11z"}],["path",{d:"m21.854 2.147-10.94 10.939"}]];fe(N,ce({name:"send"},()=>w,{get iconNode(){return k},children:(P,n)=>{var i=K(),d=E(i);ue(d,f,"default",{}),o(P,i)},$$slots:{default:!0}}))}function ht(N,f){const w=ve(f,["children","$$slots","$$events","$$legacy"]);/**
 * @license lucide-svelte v0.469.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const k=[["path",{d:"M19 21v-2a4 4 0 0 0-4-4H9a4 4 0 0 0-4 4v2"}],["circle",{cx:"12",cy:"7",r:"4"}]];fe(N,ce({name:"user"},()=>w,{get iconNode(){return k},children:(P,n)=>{var i=K(),d=E(i);ue(d,f,"default",{}),o(P,i)},$$slots:{default:!0}}))}var mt=_("<div><!></div>");function gt(N,f){Ie(f,!0);let w=it(f,"class",3,"");Ne.setOptions({breaks:!0,gfm:!0});const k=J(()=>Ne.parse(f.content));var P=mt(),n=a(P);pt(n,()=>e(k)),s(P),V(()=>de(P,1,`prose prose-sm dark:prose-invert max-w-none ${w()??""}`)),o(N,P),Te()}var $t=_("<option> </option>"),xt=_("<option>Select a running server...</option> <!>",1),yt=_('<div class="text-center"><!> <p class="text-muted-foreground mb-4">No servers running.</p> <!></div>'),bt=_('<div class="text-center"><!> <p class="text-muted-foreground">Select a running server to start chatting.</p></div>'),wt=_('<div class="h-full flex items-center justify-center"><div class="text-center text-muted-foreground"><!> <p>Start a conversation with <strong> </strong></p> <p class="text-sm mt-1"> </p></div></div>'),kt=_('<div class="w-8 h-8 rounded-full bg-primary/10 flex items-center justify-center flex-shrink-0"><!></div>'),Pt=_("<!> <!>",1),St=_('<p class="whitespace-pre-wrap"> </p>'),Mt=_('<div class="w-8 h-8 rounded-full bg-primary flex items-center justify-center flex-shrink-0"><!></div>'),Nt=_("<div><!> <div><!></div> <!></div>"),It=_('<div class="flex gap-3"><div class="w-8 h-8 rounded-full bg-primary/10 flex items-center justify-center flex-shrink-0"><!></div> <div class="bg-muted rounded-lg px-4 py-2"><!></div></div>'),Tt=_("<!> <!>",1),jt=_('<div class="px-4 py-2 bg-red-100 text-red-700 text-sm"> </div>'),Ct=_('<div class="flex-1 overflow-y-auto p-4 space-y-4"><!></div> <!> <form class="p-4 border-t"><div class="flex gap-2"><!> <!></div></form>',1),Ot=_('<div class="space-y-6 h-[calc(100vh-8rem)] flex flex-col"><div class="flex items-center justify-between"><h1 class="text-2xl font-bold">Chat</h1> <div class="flex items-center gap-4"><!> <!></div></div> <!></div>');function Gt(N,f){Ie(f,!0);const w=()=>dt(vt,"$page",k),[k,P]=lt();let n=U(st([])),i=U(""),d=U(!1),u=U(null),B=U(null);const C=J(()=>Me.profiles.filter(t=>ne.isRunning(t.id))),h=J(()=>e(C).find(t=>t.id===e(B)));ot(()=>{Me.refresh(),ne.refresh();const t=w().url.searchParams.get("profile");t&&x(B,parseInt(t,10),!0);const r=setInterval(()=>ne.refresh(),5e3);return()=>clearInterval(r)});async function je(t){var O,m,I;if(t.preventDefault(),!e(i).trim()||!e(h)||e(d))return;const r=e(i).trim();x(i,""),x(u,null),x(n,[...e(n),{role:"user",content:r}],!0),x(d,!0);try{const v=await fetch(`http://${e(h).host}:${e(h).port}/v1/chat/completions`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({model:e(h).model_path,messages:e(n).map(l=>({role:l.role,content:l.content})),stream:!1})});if(!v.ok){const l=await v.text();throw new Error(`HTTP ${v.status}: ${l}`)}const H=((I=(m=(O=(await v.json()).choices)==null?void 0:O[0])==null?void 0:m.message)==null?void 0:I.content)||"No response";x(n,[...e(n),{role:"assistant",content:H}],!0)}catch(v){x(u,v instanceof Error?v.message:"Failed to send message",!0),x(n,e(n).slice(0,-1),!0)}finally{x(d,!1)}}function Ce(){x(n,[],!0),x(u,null)}function Oe(t){const r=t.target;x(B,r.value?parseInt(r.value,10):null,!0),x(n,[],!0),x(u,null)}function Ee(t){const r=t.split("/");return r.length>1?r[r.length-1]:t}function He(t){const r=t.match(/<think>([\s\S]*?)<\/think>/);if(r){const O=r[1].trim(),m=t.replace(/<think>[\s\S]*?<\/think>/,"").trim();return{thinking:O,response:m}}return{thinking:null,response:t}}var ee=Ot(),te=a(ee),pe=p(a(te),2),_e=a(pe);{let t=J(()=>{var r;return((r=e(B))==null?void 0:r.toString())??""});ct(_e,{onchange:Oe,get value(){return e(t)},children:(r,O)=>{var m=xt(),I=E(m);I.value=I.__value="";var v=p(I,2);Pe(v,17,()=>e(C),j=>j.id,(j,H)=>{var l=$t(),D=a(l);s(l);var Q={};V((re,W)=>{Y(D,`${e(H).name??""} (${re??""})`),Q!==(Q=W)&&(l.value=(l.__value=W)??"")},[()=>Ee(e(H).model_path),()=>e(H).id.toString()]),o(j,l)}),o(r,m)},$$slots:{default:!0}})}var Re=p(_e,2);{var ze=t=>{ie(t,{variant:"outline",onclick:Ce,children:(r,O)=>{oe();var m=ye("Clear Chat");o(r,m)},$$slots:{default:!0}})};T(Re,t=>{e(n).length>0&&t(ze)})}s(pe),s(te);var Be=p(te,2);{var De=t=>{le(t,{class:"flex-1 flex items-center justify-center",children:(r,O)=>{var m=yt(),I=a(m);q(I,{class:"w-16 h-16 mx-auto text-muted-foreground mb-4"});var v=p(I,4);ie(v,{href:"/servers",children:(j,H)=>{oe();var l=ye("Go to Servers");o(j,l)},$$slots:{default:!0}}),s(m),o(r,m)},$$slots:{default:!0}})},Ae=t=>{var r=K(),O=E(r);{var m=v=>{le(v,{class:"flex-1 flex items-center justify-center",children:(j,H)=>{var l=bt(),D=a(l);q(D,{class:"w-16 h-16 mx-auto text-muted-foreground mb-4"}),oe(2),s(l),o(j,l)},$$slots:{default:!0}})},I=v=>{le(v,{class:"flex-1 overflow-hidden flex flex-col",children:(j,H)=>{var l=Ct(),D=E(l),Q=a(D);{var re=y=>{var S=wt(),R=a(S),A=a(R);q(A,{class:"w-12 h-12 mx-auto mb-4"});var L=p(A,2),M=p(a(L)),g=a(M,!0);s(M),s(L);var c=p(L,2),F=a(c);s(c),s(R),s(S),V(()=>{Y(g,e(h).name),Y(F,`Model: ${e(h).model_path??""}`)}),o(y,S)},W=y=>{var S=Tt(),R=E(S);Pe(R,16,()=>e(n),M=>M,(M,g)=>{var c=Nt(),F=a(c);{var X=b=>{var $=kt(),z=a($);q(z,{class:"w-5 h-5 text-primary"}),s($),o(b,$)};T(F,b=>{g.role==="assistant"&&b(X)})}var G=p(F,2),Fe=a(G);{var Ge=b=>{const $=J(()=>He(g.content));var z=Pt(),$e=E(z);{var qe=se=>{ut(se,{get content(){return e($).thinking}})};T($e,se=>{e($).thinking&&se(qe)})}var Ke=p($e,2);gt(Ke,{get content(){return e($).response}}),o(b,z)},Je=b=>{var $=St(),z=a($,!0);s($),V(()=>Y(z,g.content)),o(b,$)};T(Fe,b=>{g.role==="assistant"?b(Ge):b(Je,!1)})}s(G);var Ue=p(G,2);{var Ye=b=>{var $=Mt(),z=a($);ht(z,{class:"w-5 h-5 text-primary-foreground"}),s($),o(b,$)};T(Ue,b=>{g.role==="user"&&b(Ye)})}s(c),V(()=>{de(c,1,`flex gap-3 ${g.role==="user"?"justify-end":""}`),de(G,1,`max-w-[80%] rounded-lg px-4 py-2 ${g.role==="user"?"bg-primary text-primary-foreground":"bg-muted"}`)}),o(M,c)});var A=p(R,2);{var L=M=>{var g=It(),c=a(g),F=a(c);q(F,{class:"w-5 h-5 text-primary"}),s(c);var X=p(c,2),G=a(X);Se(G,{class:"w-5 h-5 animate-spin"}),s(X),s(g),o(M,g)};T(A,M=>{e(d)&&M(L)})}o(y,S)};T(Q,y=>{e(n).length===0?y(re):y(W,!1)})}s(D);var he=p(D,2);{var Le=y=>{var S=jt(),R=a(S,!0);s(S),V(()=>Y(R,e(u))),o(y,S)};T(he,y=>{e(u)&&y(Le)})}var ae=p(he,2),me=a(ae),ge=a(me);ft(ge,{placeholder:"Type a message...",get disabled(){return e(d)},class:"flex-1",get value(){return e(i)},set value(y){x(i,y,!0)}});var Ve=p(ge,2);{let y=J(()=>e(d)||!e(i).trim());ie(Ve,{type:"submit",get disabled(){return e(y)},children:(S,R)=>{var A=K(),L=E(A);{var M=c=>{Se(c,{class:"w-4 h-4 animate-spin"})},g=c=>{_t(c,{class:"w-4 h-4"})};T(L,c=>{e(d)?c(M):c(g,!1)})}o(S,A)},$$slots:{default:!0}})}s(me),s(ae),nt("submit",ae,je),o(j,l)},$$slots:{default:!0}})};T(O,v=>{e(h)?v(I,!1):v(m)},!0)}o(t,r)};T(Be,t=>{e(C).length===0?t(De):t(Ae,!1)})}s(ee),o(N,ee),Te(),P()}export{Gt as component};
