import{b as $t,d as fr,c as $e,a as n,f as u,t as ut}from"../chunks/nUxvHMgL.js";import{o as pr}from"../chunks/DPMBEg2i.js";import{t as z,o as kt,v as Ct,Z as mr,aN as hr,C as St,ap as gr,aE as _r,aL as xr,aF as yr,U as wr,ah as qe,f as I,p as Ye,d as s,r as o,a as Qe,g as e,h as oe,u as pt,c as a,s as P,e as d,b as He,n as Ve,ag as br}from"../chunks/CNYbWU2B.js";import{s as ie,d as $r,e as Xe}from"../chunks/BWpKPX3N.js";import{l as et,s as tt,p as Ut,i as w,a as kr,b as Cr}from"../chunks/CxfCsaSi.js";import{I as rt,s as at,d as mt,e as Ge,a as Tt,p as Mt,k as Pt,l as Sr,f as Ze,h as Rt}from"../chunks/D9xfsXh9.js";import{L as Ke,b as Tr}from"../chunks/DcWGgsJS.js";import{b as vt}from"../chunks/DXz5Io-F.js";import{p as Mr}from"../chunks/DlJfzioE.js";import{r as Pr}from"../chunks/CbhfpbUc.js";import{e as jt,f as At,g as Ft,h as zt,i as It,S as Rr,B as Ae,C as ft,b as Et,E as jr}from"../chunks/BQ-_QVTP.js";import{c as ze}from"../chunks/BWQjEkJe.js";import{C as Bt}from"../chunks/BCcEFCgH.js";import"../chunks/CiMb2DjI.js";import{W as Jt,X as Nt}from"../chunks/B2MmRDq8.js";import{U as Er}from"../chunks/BrRL0KsS.js";function Nr(V,p,S=!1,T=!1,D=!1){var U=V,R="";z(()=>{var m=mr;if(R===(R=p()??"")){kt&&Ct();return}if(m.nodes!==null&&(hr(m.nodes.start,m.nodes.end),m.nodes=null),R!==""){if(kt){St.data;for(var M=Ct(),j=M;M!==null&&(M.nodeType!==gr||M.data!=="");)j=M,M=_r(M);if(M===null)throw xr(),yr;$t(St,j),U=wr(M);return}var E=R+"";S?E=`<svg>${E}</svg>`:T&&(E=`<math>${E}</math>`);var B=fr(E);if((S||T)&&(B=qe(B)),$t(qe(B),B.lastChild),S||T)for(;qe(B);)U.before(qe(B));else U.before(B)}})}function Fe(V,p){const S=et(p,["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 T=[["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"}]];rt(V,tt({name:"bot"},()=>S,{get iconNode(){return T},children:(D,U)=>{var R=$e(),m=I(R);at(m,p,"default",{}),n(D,R)},$$slots:{default:!0}}))}function Lr(V,p){const S=et(p,["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 T=[["path",{d:"M12 5a3 3 0 1 0-5.997.125 4 4 0 0 0-2.526 5.77 4 4 0 0 0 .556 6.588A4 4 0 1 0 12 18Z"}],["path",{d:"M12 5a3 3 0 1 1 5.997.125 4 4 0 0 1 2.526 5.77 4 4 0 0 1-.556 6.588A4 4 0 1 1 12 18Z"}],["path",{d:"M15 13a4.5 4.5 0 0 1-3-4 4.5 4.5 0 0 1-3 4"}],["path",{d:"M17.599 6.5a3 3 0 0 0 .399-1.375"}],["path",{d:"M6.003 5.125A3 3 0 0 0 6.401 6.5"}],["path",{d:"M3.477 10.896a4 4 0 0 1 .585-.396"}],["path",{d:"M19.938 10.5a4 4 0 0 1 .585.396"}],["path",{d:"M6 18a4 4 0 0 1-1.967-.516"}],["path",{d:"M19.967 17.484A4 4 0 0 1 18 18"}]];rt(V,tt({name:"brain"},()=>S,{get iconNode(){return T},children:(D,U)=>{var R=$e(),m=I(R);at(m,p,"default",{}),n(D,R)},$$slots:{default:!0}}))}function Or(V,p){const S=et(p,["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 T=[["path",{d:"M13.234 20.252 21 12.3"}],["path",{d:"m16 6-8.414 8.586a2 2 0 0 0 0 2.828 2 2 0 0 0 2.828 0l8.414-8.586a4 4 0 0 0 0-5.656 4 4 0 0 0-5.656 0l-8.415 8.585a6 6 0 1 0 8.486 8.486"}]];rt(V,tt({name:"paperclip"},()=>S,{get iconNode(){return T},children:(D,U)=>{var R=$e(),m=I(R);at(m,p,"default",{}),n(D,R)},$$slots:{default:!0}}))}function Dr(V,p){const S=et(p,["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 T=[["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"}]];rt(V,tt({name:"send"},()=>S,{get iconNode(){return T},children:(D,U)=>{var R=$e(),m=I(R);at(m,p,"default",{}),n(D,R)},$$slots:{default:!0}}))}var Ur=u("<div><!></div>");function Lt(V,p){Ye(p,!0);let S=Ut(p,"class",3,"");jt.setOptions({breaks:!0,gfm:!0});const T=oe(()=>jt.parse(p.content));var D=Ur(),U=s(D);Nr(U,()=>e(T)),o(D),z(()=>mt(D,1,`prose prose-sm dark:prose-invert max-w-none ${S()??""}`)),n(V,D),Qe()}var Ar=u("<!> <!> <span> </span>",1),Fr=u('<div class="mt-2 pl-6 border-l-2 border-muted text-sm text-muted-foreground italic whitespace-pre-wrap"> </div>'),zr=u("<!> <!>",1),Ir=u('<div class="my-2"><!></div>');function Ot(V,p){Ye(p,!0);let S=Ut(p,"streaming",3,!1),T=P(!1);pt(()=>{S()?a(T,!0):p.duration!==void 0&&a(T,!1)});const D=oe(()=>S()?"Thinking...":p.duration!==void 0?`Thought for ${p.duration.toFixed(1)}s`:"Thinking");var U=Ir(),R=s(U);ze(R,()=>It,(m,M)=>{M(m,{get open(){return e(T)},set open(j){a(T,j,!0)},children:(j,E)=>{var B=zr(),pe=I(B);ze(pe,()=>At,(ve,me)=>{me(ve,{class:"flex items-center gap-2 text-sm text-muted-foreground hover:text-foreground transition-colors",children:(J,te)=>{var A=Ar(),O=I(A);{var re=q=>{Bt(q,{class:"w-4 h-4"})},xe=q=>{zt(q,{class:"w-4 h-4"})};w(O,q=>{e(T)?q(re):q(xe,!1)})}var se=d(O,2);{var K=q=>{Ke(q,{class:"w-4 h-4 animate-spin"})},fe=q=>{Lr(q,{class:"w-4 h-4"})};w(se,q=>{S()?q(K):q(fe,!1)})}var W=d(se,2),he=s(W,!0);o(W),z(()=>ie(he,e(D))),n(J,A)},$$slots:{default:!0}})});var X=d(pe,2);ze(X,()=>Ft,(ve,me)=>{me(ve,{children:(J,te)=>{var A=Fr(),O=s(A,!0);o(A),z(()=>ie(O,p.content)),n(J,A)},$$slots:{default:!0}})}),n(j,B)},$$slots:{default:!0}})}),o(U),n(V,U),Qe()}var Br=u("<!> <!> <span> </span>",1),Jr=u('<div class="mt-1 text-xs text-muted-foreground">Result:</div> <pre class="mt-0.5 p-2 rounded bg-green-50 dark:bg-green-950/30 text-xs font-mono overflow-x-auto whitespace-pre-wrap text-green-700 dark:text-green-300"> </pre>',1),Wr=u('<div class="mt-1 text-xs text-red-600 dark:text-red-400"> </div>'),qr=u('<div class="text-sm"><div class="font-medium text-foreground"> </div> <pre class="mt-1 p-2 rounded bg-muted text-xs font-mono overflow-x-auto whitespace-pre-wrap"> </pre> <!> <!></div>'),Hr=u('<div class="mt-2 pl-6 border-l-2 border-amber-300 dark:border-amber-700 space-y-3"></div>'),Vr=u("<!> <!>",1),Xr=u('<div class="my-2"><!></div>');function Dt(V,p){Ye(p,!0);let S=P(!1);const T=oe(()=>p.calls.length===1?`Tool: ${p.calls[0].name}`:`Tool Calls: ${p.calls.length}`);function D(m){try{return JSON.stringify(JSON.parse(m),null,2)}catch{return m}}var U=Xr(),R=s(U);ze(R,()=>It,(m,M)=>{M(m,{get open(){return e(S)},set open(j){a(S,j,!0)},children:(j,E)=>{var B=Vr(),pe=I(B);ze(pe,()=>At,(ve,me)=>{me(ve,{class:"flex items-center gap-2 text-sm text-muted-foreground hover:text-foreground transition-colors",children:(J,te)=>{var A=Br(),O=I(A);{var re=W=>{Bt(W,{class:"w-4 h-4"})},xe=W=>{zt(W,{class:"w-4 h-4"})};w(O,W=>{e(S)?W(re):W(xe,!1)})}var se=d(O,2);Jt(se,{class:"w-4 h-4"});var K=d(se,2),fe=s(K,!0);o(K),z(()=>ie(fe,e(T))),n(J,A)},$$slots:{default:!0}})});var X=d(pe,2);ze(X,()=>Ft,(ve,me)=>{me(ve,{children:(J,te)=>{var A=Hr();Ge(A,21,()=>p.calls,O=>O.id,(O,re)=>{var xe=qr(),se=s(xe),K=s(se,!0);o(se);var fe=d(se,2),W=s(fe,!0);o(fe);var he=d(fe,2);{var q=le=>{var Te=Jr(),ke=d(I(Te),2),ot=s(ke,!0);o(ke),z(()=>ie(ot,e(re).result)),n(le,Te)};w(he,le=>{e(re).result&&le(q)})}var Le=d(he,2);{var H=le=>{var Te=Wr(),ke=s(Te,!0);o(Te),z(()=>ie(ke,e(re).error)),n(le,Te)};w(Le,le=>{e(re).error&&le(H)})}o(xe),z(le=>{ie(K,e(re).name),ie(W,le)},[()=>D(e(re).arguments)]),n(O,xe)}),o(A),n(J,A)},$$slots:{default:!0}})}),n(j,B)},$$slots:{default:!0}})}),o(U),n(V,U),Qe()}var Zr=u("<option> </option>"),Gr=u("<option>Select a running server...</option> <!>",1),Kr=u('<div class="text-center"><!> <p class="text-muted-foreground mb-4">No servers running.</p> <!></div>'),Yr=u('<div class="text-center"><!> <p class="text-muted-foreground">Select a running server to start chatting.</p></div>'),Qr=u('<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>'),ea=u('<div class="flex gap-3 opacity-60"><div class="flex-shrink-0 w-8 h-8 rounded-full bg-muted flex items-center justify-center"><!></div> <div class="flex-1 min-w-0"><p class="text-xs text-muted-foreground mb-1 italic">System Prompt</p> <div class="text-sm text-muted-foreground italic whitespace-pre-wrap"> </div></div></div>'),ta=u('<div class="flex items-center gap-2 text-xs text-muted-foreground bg-muted/50 rounded-lg px-3 py-2" id="system-prompt-hint"><!> <span>No system prompt set.</span> <a class="text-primary hover:underline">Set one in profile settings</a> <button class="ml-auto text-muted-foreground hover:text-foreground" type="button"><!></button></div>'),ra=u('<div class="w-8 h-8 rounded-full bg-primary/10 flex items-center justify-center flex-shrink-0"><!></div>'),aa=u("<!> <!> <!>",1),oa=u('<p class="whitespace-pre-wrap"> </p>'),sa=u('<div class="w-8 h-8 rounded-full bg-primary flex items-center justify-center flex-shrink-0"><!></div>'),na=u("<div><!> <div><!></div> <!></div>"),ia=u('<div class="flex items-center gap-2 text-sm text-muted-foreground animate-pulse px-4 py-2"><!> <span> </span></div>'),la=u('<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="max-w-[80%] rounded-lg px-4 py-2 bg-muted"><!> <!> <!></div></div>'),da=u('<div class="flex gap-3"><div class="w-8 h-8 rounded-full bg-destructive/10 flex items-center justify-center flex-shrink-0"><!></div> <div class="max-w-[80%]"><!></div></div>'),ca=u('<div class="flex justify-center py-2"><!></div>'),ua=u("<!> <!> <!> <!> <!> <!>",1),va=u('<img alt="Attachment" class="w-16 h-16 object-cover rounded-lg border"/>'),fa=u('<video class="w-16 h-16 object-cover rounded-lg border"><track kind="captions"/></video>',2),pa=u('<div class="w-16 h-16 flex flex-col items-center justify-center rounded-lg border bg-muted text-muted-foreground text-xs p-2"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="mb-1"><path d="M14.5 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7.5L14.5 2z"></path><polyline points="14 2 14 8 20 8"></polyline></svg> <span class="truncate w-full text-center"> </span></div>'),ma=u('<div class="relative group"><!> <button type="button" class="absolute -top-2 -right-2 w-5 h-5 bg-destructive text-destructive-foreground rounded-full flex items-center justify-center opacity-0 group-hover:opacity-100 transition-opacity"><!></button></div>'),ha=u('<div class="px-4 pt-4 flex gap-2 flex-wrap"></div>'),ga=u('<div class="flex-1 overflow-y-auto p-4 space-y-4"><!></div> <form class="p-4 border-t"><!> <div class="flex gap-2"><!> <!> <textarea placeholder="Type a message..." class="flex-1 resize-none overflow-hidden rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50" rows="1"></textarea> <!></div> <input type="file" multiple class="hidden"/></form>',1),_a=u('<div class="flex-1 overflow-hidden" role="region" aria-label="Chat message area with drag-drop file upload"><!></div>'),xa=u('<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 Da(V,p){Ye(p,!0);const S=()=>Cr(Mr,"$page",T),[T,D]=kr(),U=new Set(["txt","md","csv","json","xml","yaml","yml","log","py","js","ts","html","css","sh","sql","conf","ini","toml"]),R=new Set(["readme","makefile","dockerfile","license","procfile","gemfile","brewfile","vagrantfile","rakefile","guardfile","podfile","fastfile","dangerfile","berksfile","thorfile","capfile","puppetfile","appraisals","codeowners","changelog","contributing","authors","todo","copying","notice","patents","version","manifest",".gitignore",".dockerignore",".editorconfig",".env.example",".npmrc",".nvmrc",".prettierrc",".eslintrc",".babelrc",".gitattributes",".mailmap",".env",".env.local",".env.development",".env.production",".env.test"]);let m=P(He([])),M=P(""),j=P(!1),E=P(null),B=P(void 0),pe=P(null),X=P(He([])),ve=P(!1),me=P(void 0),J=P(""),te=P(""),A=P(void 0),O=P(He([])),re=P(0),xe=3,se=P(!1),K=P(null),fe=P(null),W=P(!1),he=P(He([])),q=P(!1);const Le=oe(()=>{const t=new Set(Tt.servers.map(r=>r.profile_id));return Mt.profiles.filter(r=>t.has(r.id))}),H=oe(()=>e(Le).find(t=>t.id===e(pe))),le=oe(()=>{var t;return((t=e(H))==null?void 0:t.model_type)==="multimodal"}),Te=oe(()=>e(le)?"image/*,video/*,.txt,.md,.csv,.json,.xml,.yaml,.yml,.log,.py,.js,.ts,.html,.css,.sh,.sql,.conf,.ini,.toml":".txt,.md,.csv,.json,.xml,.yaml,.yml,.log,.py,.js,.ts,.html,.css,.sh,.sql,.conf,.ini,.toml");let ke=P(null);pr(async()=>{Mt.refresh(),Tt.refresh();const t=S().url.searchParams.get("profile");t&&a(ke,parseInt(t,10),!0);try{a(he,await Pt.listTools(),!0),a(q,!0)}catch{}}),pt(()=>{e(ke)!==null&&e(pe)===null&&e(Le).some(r=>r.id===e(ke))&&a(pe,e(ke),!0)}),pt(()=>{!e(M)&&e(fe)&&(e(fe).style.height="auto")});async function ot(t){return new Promise(r=>{const i=document.createElement("video");i.preload="metadata",i.onloadedmetadata=()=>{URL.revokeObjectURL(i.src),r(i.duration<=120)},i.onerror=()=>{URL.revokeObjectURL(i.src),r(!1)},i.src=URL.createObjectURL(t)})}async function ht(t){var h;if(e(X).length>=3){a(E,{summary:"Maximum 3 attachments per message"},!0);return}const r=t.type.startsWith("video/"),i=t.type.startsWith("image/"),l=t.name.split("."),v=l.length>1,g=v&&((h=l.pop())==null?void 0:h.toLowerCase())||"",_=v?U.has(g):R.has(t.name.toLowerCase());if(!_&&!r&&!i){const Y=e(le)?"images, videos, and text files (.txt, .md, .py, .js, .json, .csv, .log, etc.)":"text files (.txt, .md, .py, .js, .json, .csv, .log, .yaml, .xml, etc.)";a(E,{summary:`Unsupported file type. Accepted: ${Y}`},!0);return}if(!e(le)&&(i||r)){a(E,{summary:"This model only supports text file attachments"},!0);return}if(r&&!await ot(t)){a(E,{summary:"Video must be 2 minutes or less"},!0);return}const Z=_?t.name:URL.createObjectURL(t);e(X).push({file:t,preview:Z,type:_?"text":r?"video":"image"})}function Wt(t){const r=e(X)[t];r.type!=="text"&&URL.revokeObjectURL(r.preview),e(X).splice(t,1)}async function qt(t){const r=t.target,i=Array.from(r.files||[]);for(const l of i)await ht(l);r.value=""}function Ht(t){var i;t.preventDefault(),a(ve,!1);const r=Array.from(((i=t.dataTransfer)==null?void 0:i.files)||[]);for(const l of r)ht(l)}function Vt(t){t.preventDefault(),a(ve,!0)}function Xt(t){t.preventDefault(),a(ve,!1)}async function Zt(t){return new Promise((r,i)=>{const l=new FileReader;l.onload=()=>r(l.result),l.onerror=i,l.readAsDataURL(t)})}async function Gt(t){return new Promise((r,i)=>{const l=new FileReader;l.onload=()=>r(l.result),l.onerror=i,l.readAsText(t)})}async function Kt(t,r){if(r.length===0)return t;const i=[{type:"text",text:t}];for(const l of r)if(l.type==="text"){const v=await Gt(l.file);i.push({type:"text",text:`[File: ${l.file.name}]
${v}`})}else{const v=await Zt(l.file);i.push({type:"image_url",image_url:{url:v}})}return i}async function gt(t){var g,_,Z,h;const r={content:"",thinking:"",thinkingDur:void 0,toolCalls:[],toolCallsDone:!1,error:null},i=(g=t.body)==null?void 0:g.getReader();if(!i)return r;const l=new TextDecoder;let v="";for(;;){const{done:Y,value:ye}=await i.read();if(Y)break;v+=l.decode(ye,{stream:!0});const ae=v.split(`
`);v=ae.pop()||"";for(const ne of ae)if(ne.startsWith("data: "))try{const $=JSON.parse(ne.slice(6));switch($.type){case"thinking":r.thinking+=$.content,a(J,e(J)+$.content);break;case"thinking_done":r.thinkingDur=$.duration,a(A,$.duration,!0);break;case"response":r.content+=$.content,a(te,e(te)+$.content);break;case"tool_call":{const Q=$.tool_call,Oe=r.toolCalls.find(Re=>Re.id===Q.id);Oe?Oe.arguments+=((_=Q.function)==null?void 0:_.arguments)||"":Q.id&&r.toolCalls.push({id:Q.id,name:((Z=Q.function)==null?void 0:Z.name)||"",arguments:((h=Q.function)==null?void 0:h.arguments)||""});break}case"tool_calls_done":r.toolCallsDone=!0;break;case"error":$.content.includes("Failed to connect")?r.error={summary:"Connection failed",details:$.content}:$.content.includes("timed out")?r.error={summary:"Request timed out",details:$.content}:r.error={summary:"Server error",details:$.content};break;case"done":break}}catch{}}return r}async function Be(t,r,i=1){if(a(re,i,!0),a(se,i>1),!e(H))return!1;const l=[];e(H).system_prompt&&l.push({role:"system",content:e(H).system_prompt}),e(m).slice(0,-1).forEach(v=>{l.push({role:v.role,content:v.content})}),l.push({role:"user",content:t});try{const v={profile_id:e(H).id,messages:l};e(W)&&e(he).length>0&&(v.tools=e(he),v.tool_choice="auto");const g=await fetch("/api/chat/completions",{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${Rt.token}`},body:JSON.stringify(v)});if(!g.ok){const ae=g.status>=500&&g.status<600,ne=[502,503,504].includes(g.status);if((ae||ne)&&i<xe){const $=i*2e3;return await new Promise(Q=>setTimeout(Q,$)),Be(t,r,i+1)}throw new Error(`Server error: ${g.status}`)}const _=await gt(g);if(_.error)return a(E,_.error,!0),!1;const Z=3;let h=_,Y=0,ye=h.content;for(;h.toolCallsDone&&h.toolCalls.length>0&&Y<Z;){Y++;for(const $ of h.toolCalls){const Q={id:$.id,name:$.name,arguments:$.arguments};try{const Oe=JSON.parse($.arguments),Re=await Pt.executeTool($.name,Oe);Q.result=JSON.stringify(Re)}catch{Q.error="Tool execution failed"}a(O,[...e(O),Q],!0),l.push({role:"assistant",content:"",tool_calls:[{id:$.id,type:"function",function:{name:$.name,arguments:$.arguments}}]}),l.push({role:"tool",tool_call_id:$.id,content:Q.result||Q.error||""})}const ae={profile_id:e(H).id,messages:l};e(W)&&e(he).length>0&&(ae.tools=e(he),ae.tool_choice="auto");const ne=await fetch("/api/chat/completions",{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${Rt.token}`},body:JSON.stringify(ae)});if(!ne.ok)break;if(h=await gt(ne),ye+=h.content,a(te,ye,!0),h.error){a(E,h.error,!0);break}}if(Y>=Z&&h.toolCallsDone&&(ye+=`

*[Tool call limit reached (3 rounds). Stopping automatic execution.]*`,a(te,ye,!0)),e(te)||e(J)||e(O).length>0){const ae=e(J)?`<think>${e(J)}</think>${e(te)}`:e(te);e(m).push({role:"assistant",content:ae,toolCalls:e(O).length>0?[...e(O)]:void 0})}return a(re,0),a(se,!1),a(K,null),!0}catch(v){const g=v instanceof TypeError,_=v instanceof Error?v.message:"Failed to send message",Z=_.toLowerCase().includes("failed to fetch")||_.toLowerCase().includes("network")||_.toLowerCase().includes("connection");if((g||Z)&&i<xe){const h=i*2e3;return await new Promise(Y=>setTimeout(Y,h)),Be(t,r,i+1)}return a(re,0),a(se,!1),a(K,{content:t,attachments:r},!0),a(E,{summary:"Failed to connect to model after 3 attempts",details:`The model may still be loading. Check the Servers page for status.

Error: ${_}`},!0),!1}}async function Yt(t){var v;if(t.preventDefault(),!e(M).trim()||!e(H)||e(j))return;(v=e(B))==null||v.collapse(),a(E,null);const r=e(M).trim();a(M,"");const i=await Kt(r,e(X)),l=[...e(X)];for(const g of e(X))g.type!=="text"&&URL.revokeObjectURL(g.preview);a(X,[],!0),e(m).push({role:"user",content:r}),a(J,""),a(te,""),a(A,void 0),a(O,[],!0),a(j,!0);try{await Be(i,l,1)||e(m).pop()}finally{a(j,!1)}}function Qt(){a(m,[],!0),a(E,null)}function er(t){const r=t.target;a(pe,r.value?parseInt(r.value,10):null,!0),a(m,[],!0),a(E,null);for(const i of e(X))i.type!=="text"&&URL.revokeObjectURL(i.preview);a(X,[],!0)}function tr(t){const r=t.split("/");return r.length>1?r[r.length-1]:t}function rr(t){if(typeof t!="string")return{thinking:null,response:""};const r=[/<think>([\s\S]*?)<\/think>/,/<thinking>([\s\S]*?)<\/thinking>/,/<reasoning>([\s\S]*?)<\/reasoning>/];for(const i of r){const l=t.match(i);if(l){const v=l[1].trim(),g=t.replace(i,"").trim();return{thinking:v,response:g}}}return{thinking:null,response:t}}var st=xa(),nt=s(st),_t=d(s(nt),2),xt=s(_t);{let t=oe(()=>{var r;return((r=e(pe))==null?void 0:r.toString())??""});Rr(xt,{onchange:er,get value(){return e(t)},children:(r,i)=>{var l=Gr(),v=I(l);v.value=v.__value="";var g=d(v,2);Ge(g,17,()=>e(Le),_=>_.id,(_,Z)=>{var h=Zr(),Y=s(h);o(h);var ye={};z((ae,ne)=>{ie(Y,`${e(Z).name??""} (${ae??""})`),ye!==(ye=ne)&&(h.value=(h.__value=ne)??"")},[()=>tr(e(Z).model_path),()=>e(Z).id.toString()]),n(_,h)}),n(r,l)},$$slots:{default:!0}})}var ar=d(xt,2);{var or=t=>{Ae(t,{variant:"outline",onclick:Qt,children:(r,i)=>{Ve();var l=ut("Clear Chat");n(r,l)},$$slots:{default:!0}})};w(ar,t=>{e(m).length>0&&t(or)})}o(_t),o(nt);var sr=d(nt,2);{var nr=t=>{ft(t,{class:"flex-1 flex items-center justify-center",children:(r,i)=>{var l=Kr(),v=s(l);Fe(v,{class:"w-16 h-16 mx-auto text-muted-foreground mb-4"});var g=d(v,4);Ae(g,{href:"/servers",children:(_,Z)=>{Ve();var h=ut("Go to Servers");n(_,h)},$$slots:{default:!0}}),o(l),n(r,l)},$$slots:{default:!0}})},ir=t=>{var r=$e(),i=I(r);{var l=g=>{ft(g,{class:"flex-1 flex items-center justify-center",children:(_,Z)=>{var h=Yr(),Y=s(h);Fe(Y,{class:"w-16 h-16 mx-auto text-muted-foreground mb-4"}),Ve(2),o(h),n(_,h)},$$slots:{default:!0}})},v=g=>{var _=_a(),Z=s(_);{let h=oe(()=>e(ve)?"ring-2 ring-primary":"");ft(Z,{get class(){return`h-full overflow-hidden flex flex-col ${e(h)??""}`},children:(Y,ye)=>{var ae=ga(),ne=I(ae),$=s(ne);{var Q=f=>{var k=Qr(),de=s(k),ee=s(de);Fe(ee,{class:"w-12 h-12 mx-auto mb-4"});var we=d(ee,2),ge=d(s(we)),Ce=s(ge,!0);o(ge),o(we);var ce=d(we,2),De=s(ce);o(ce),o(de),o(k),z(()=>{ie(Ce,e(H).name),ie(De,`Model: ${e(H).model_path??""}`)}),n(f,k)},Oe=f=>{var k=ua(),de=I(k);{var ee=b=>{var c=ea(),x=s(c),C=s(x);Fe(C,{class:"w-4 h-4 text-muted-foreground"}),o(x);var N=d(x,2),L=d(s(N),2),G=s(L,!0);o(L),o(N),o(c),z(()=>ie(G,e(H).system_prompt)),n(b,c)},we=b=>{var c=$e(),x=I(c);{var C=N=>{var L=ta(),G=s(L);Et(G,{class:"w-3 h-3"});var _e=d(G,4),be=d(_e,2);be.__click=Se=>{var F;(F=Se.currentTarget.closest("#system-prompt-hint"))==null||F.remove()};var Ee=s(be);Nt(Ee,{class:"w-3 h-3"}),o(be),o(L),z(Se=>Ze(_e,"href",Se),[()=>Pr(`/profiles/${e(H).id}`)]),n(N,L)};w(x,N=>{e(H)&&!e(H).system_prompt&&N(C)},!0)}n(b,c)};w(de,b=>{var c;(c=e(H))!=null&&c.system_prompt?b(ee):b(we,!1)})}var ge=d(de,2);Ge(ge,16,()=>e(m),b=>b,(b,c)=>{var x=na(),C=s(x);{var N=F=>{var y=ra(),ue=s(y);Fe(ue,{class:"w-5 h-5 text-primary"}),o(y),n(F,y)};w(C,F=>{c.role==="assistant"&&F(N)})}var L=d(C,2),G=s(L);{var _e=F=>{const y=oe(()=>rr(c.content));var ue=aa(),We=I(ue);{var ct=Ne=>{Ot(Ne,{get content(){return e(y).thinking}})};w(We,Ne=>{e(y).thinking&&Ne(ct)})}var Ue=d(We,2);{var ur=Ne=>{Dt(Ne,{get calls(){return c.toolCalls}})};w(Ue,Ne=>{c.toolCalls&&c.toolCalls.length>0&&Ne(ur)})}var vr=d(Ue,2);Lt(vr,{get content(){return e(y).response}}),n(F,ue)},be=F=>{var y=oa(),ue=s(y,!0);o(y),z(()=>ie(ue,typeof c.content=="string"?c.content:"")),n(F,y)};w(G,F=>{c.role==="assistant"?F(_e):F(be,!1)})}o(L);var Ee=d(L,2);{var Se=F=>{var y=sa(),ue=s(y);Er(ue,{class:"w-5 h-5 text-primary-foreground"}),o(y),n(F,y)};w(Ee,F=>{c.role==="user"&&F(Se)})}o(x),z(()=>{mt(x,1,`flex gap-3 ${c.role==="user"?"justify-end":""}`),mt(L,1,`max-w-[80%] rounded-lg px-4 py-2 ${c.role==="user"?"bg-primary text-primary-foreground":"bg-muted"}`)}),n(b,x)});var Ce=d(ge,2);{var ce=b=>{var c=ia(),x=s(c);Ke(x,{class:"w-4 h-4 animate-spin"});var C=d(x,2),N=s(C);o(C),o(c),z(()=>ie(N,`Connecting to model... (attempt ${e(re)??""}/3)`)),n(b,c)};w(Ce,b=>{e(se)&&b(ce)})}var De=d(Ce,2);{var Ie=b=>{var c=la(),x=s(c),C=s(x);Fe(C,{class:"w-5 h-5 text-primary"}),o(x);var N=d(x,2),L=s(N);{var G=y=>{{let ue=oe(()=>e(A)===void 0);Ot(y,{get content(){return e(J)},get duration(){return e(A)},get streaming(){return e(ue)}})}};w(L,y=>{e(J)&&y(G)})}var _e=d(L,2);{var be=y=>{Dt(y,{get calls(){return e(O)}})};w(_e,y=>{e(O).length>0&&y(be)})}var Ee=d(_e,2);{var Se=y=>{Lt(y,{get content(){return e(te)}})},F=y=>{var ue=$e(),We=I(ue);{var ct=Ue=>{Ke(Ue,{class:"w-5 h-5 animate-spin"})};w(We,Ue=>{!e(J)&&e(O).length===0&&Ue(ct)},!0)}n(y,ue)};w(Ee,y=>{e(te)?y(Se):y(F,!1)})}o(N),o(c),n(b,c)};w(De,b=>{e(j)&&b(Ie)})}var Je=d(De,2);{var Pe=b=>{var c=da(),x=s(c),C=s(x);Et(C,{class:"w-5 h-5 text-destructive"}),o(x);var N=d(x,2),L=s(N);vt(jr(L,{get summary(){return e(E).summary},get details(){return e(E).details}}),G=>a(B,G,!0),()=>e(B)),o(N),o(c),n(b,c)};w(Je,b=>{e(E)&&b(Pe)})}var je=d(Je,2);{var dt=b=>{var c=ca(),x=s(c);Ae(x,{variant:"outline",size:"sm",onclick:async()=>{var C,N;if(e(K)&&e(H)){(C=e(B))==null||C.collapse(),a(E,null);const L=typeof e(K).content=="string"?e(K).content:((N=e(K).content.find(G=>G.type==="text"))==null?void 0:N.text)||"";e(m).push({role:"user",content:L}),a(J,""),a(te,""),a(A,void 0),a(j,!0);try{await Be(e(K).content,e(K).attachments,1)||e(m).pop()}finally{a(j,!1)}}},get disabled(){return e(j)},children:(C,N)=>{Ve();var L=ut("Retry");n(C,L)},$$slots:{default:!0}}),o(c),n(b,c)};w(je,b=>{e(K)&&b(dt)})}n(f,k)};w($,f=>{e(m).length===0?f(Q):f(Oe,!1)})}o(ne);var Re=d(ne,2),yt=s(Re);{var lr=f=>{var k=ha();Ge(k,23,()=>e(X),de=>de.preview,(de,ee,we)=>{var ge=ma(),Ce=s(ge);{var ce=Pe=>{var je=va();z(()=>Ze(je,"src",e(ee).preview)),n(Pe,je)},De=Pe=>{var je=$e(),dt=I(je);{var b=x=>{var C=fa();C.muted=!0,z(()=>Ze(C,"src",e(ee).preview)),n(x,C)},c=x=>{var C=$e(),N=I(C);{var L=G=>{var _e=pa(),be=d(s(_e),2),Ee=s(be,!0);o(be),o(_e),z(Se=>ie(Ee,Se),[()=>e(ee).preview.split(".").pop()]),n(G,_e)};w(N,G=>{e(ee).type==="text"&&G(L)},!0)}n(x,C)};w(dt,x=>{e(ee).type==="video"?x(b):x(c,!1)},!0)}n(Pe,je)};w(Ce,Pe=>{e(ee).type==="image"?Pe(ce):Pe(De,!1)})}var Ie=d(Ce,2);Ie.__click=()=>Wt(e(we));var Je=s(Ie);Nt(Je,{class:"w-3 h-3"}),o(Ie),o(ge),n(de,ge)}),o(k),n(f,k)};w(yt,f=>{e(X).length>0&&f(lr)})}var it=d(yt,2),wt=s(it);{var dr=f=>{{let k=oe(()=>e(W)?"default":"ghost"),de=oe(()=>e(W)?`Tools enabled (${e(he).length})`:"Enable tools");Ae(f,{type:"button",get variant(){return e(k)},size:"icon",onclick:()=>a(W,!e(W)),get title(){return e(de)},children:(ee,we)=>{Jt(ee,{class:"w-4 h-4"})},$$slots:{default:!0}})}};w(wt,f=>{e(q)&&f(dr)})}var bt=d(wt,2);{let f=oe(()=>e(j)||e(X).length>=3);Ae(bt,{type:"button",variant:"ghost",size:"icon",onclick:()=>{var k;return(k=e(me))==null?void 0:k.click()},get disabled(){return e(f)},children:(k,de)=>{Or(k,{class:"w-4 h-4"})},$$slots:{default:!0}})}var Me=d(bt,2);br(Me),Me.__input=f=>{const k=f.currentTarget;k.style.height="auto",k.style.height=Math.min(k.scrollHeight,150)+"px"},Me.__keydown=f=>{if(f.key==="Enter"&&!f.shiftKey){f.preventDefault();const k=f.currentTarget.closest("form");k&&e(M).trim()&&k.requestSubmit()}},vt(Me,f=>a(fe,f),()=>e(fe));var cr=d(Me,2);{let f=oe(()=>e(j)||!e(M).trim());Ae(cr,{type:"submit",get disabled(){return e(f)},children:(k,de)=>{var ee=$e(),we=I(ee);{var ge=ce=>{Ke(ce,{class:"w-4 h-4 animate-spin"})},Ce=ce=>{Dr(ce,{class:"w-4 h-4"})};w(we,ce=>{e(j)?ce(ge):ce(Ce,!1)})}n(k,ee)},$$slots:{default:!0}})}o(it);var lt=d(it,2);lt.__change=qt,vt(lt,f=>a(me,f),()=>e(me)),o(Re),z(f=>{Me.disabled=e(j),Sr(Me,`max-height: 150px; overflow-y: ${f??""}`),Ze(lt,"accept",e(Te))},[()=>e(M).split(`
`).length>4?"auto":"hidden"]),Xe("submit",Re,Yt),Tr(Me,()=>e(M),f=>a(M,f)),n(Y,ae)},$$slots:{default:!0}})}o(_),Xe("dragover",_,Vt),Xe("dragleave",_,Xt),Xe("drop",_,Ht),n(g,_)};w(i,g=>{e(H)?g(v,!1):g(l)},!0)}n(t,r)};w(sr,t=>{e(Le).length===0?t(nr):t(ir,!1)})}o(st),n(V,st),Qe(),D()}$r(["click","input","keydown","change"]);export{Da as component};
