import{c as D,d as R}from"./chunk-QSHRHSVU.js";import{gd as c,hd as B,id as v,jd as M}from"./chunk-BPZQKZKD.js";import{a as F,b as x}from"./chunk-QGVT6DPT.js";import{j as f,m as b,q as _}from"./chunk-HTXYTVN4.js";import{J as u,Jb as E,Nc as S,Oc as T,Pc as y,Sa as P,Sb as I,T as m,Ub as A,be as w,ia as C}from"./chunk-WL5U2YIT.js";import{e as k}from"./chunk-ADEKKGI2.js";var X,G,$=k(()=>{_();P();x();M();B();R();w();_();X=["messagesContainer"],G=(()=>{let h=class h extends F{get isTyping(){return this.isSending&&!this.isStreaming}get canSend(){return!this.isDisabled&&!this.isSending&&this.pendingReads===0&&(!!this.inputText.trim()||this.pendingFiles.length>0)}get providerLabel(){if(!this.aiConfig||this.aiConfig.aiProvider===c.None)return"";let e=D(this.aiConfig.aiProvider);return this.aiConfig.aiModel?`${e} \xB7 ${this.aiConfig.aiModel}`:e}constructor(e){super(),this.httpClient=e,this.messages=[],this.inputText="",this.isSending=!1,this.isStreaming=!1,this.isDragging=!1,this.pendingFiles=[],this.pendingReads=0,this.fileError=null,this.fileErrorParam=null,this.connectionId="",this.aiConfig=null,this.isDisabled=!1,this.MAX_FILE_SIZE=200*1024,this.MAX_FILES=5,this.MAX_TOTAL_PAYLOAD_SIZE=1024*1024,this.readBatchId=0,this._thinkState="none",this._thinkBuffer=""}sendMessage(){let e=this.inputText.trim();if(!e&&!this.pendingFiles.length||this.isSending)return;let t=[...this.pendingFiles];this.messages.push({role:"user",content:e,files:t.length?t:void 0}),this.inputText="",this.pendingFiles=[],this.readBatchId++,this.fileError=null,this.fileErrorParam=null,this.isSending=!0,this.isStreaming=!1;let i=this.messages.filter(n=>n.role!=="error").map(n=>({role:n.role,content:n.role==="user"?this.buildMessageContent(n):n.content}));if(i.reduce((n,a)=>n+a.content.length,0)>this.MAX_TOTAL_PAYLOAD_SIZE){this.messages.push({role:"error",content:"lblAIAssistantPayloadTooLarge"}),this.isSending=!1,this.scrollToBottom();return}let s=null,o=0,l=!1;this.httpClient.post("api/ai/chat/stream",{connectionId:this.connectionId,chatHistory:i},{responseType:"text",observe:"events",reportProgress:!0,withCredentials:!1}).pipe(u(n=>n.type===f.DownloadProgress||n.type===f.Response),C(this.destroyed$)).subscribe({next:n=>{if(n.type===f.DownloadProgress){let a=n.partialText??"",{content:d,error:p}=this.parseSseChunk(a.slice(o));o=a.length,p?(l=!0,s&&(this.messages=this.messages.filter(L=>L!==s),s=null),this.messages.push({role:"error",content:p}),this.isSending=!1,this.isStreaming=!1,this.scrollToBottom()):d&&!l&&(s||(s={role:"assistant",content:""},this.messages.push(s),this.isStreaming=!0,this._thinkState="none"),s.content+=this.processThinkChunk(d),this.scrollToBottom())}else n.type===f.Response&&(l||this.onResponseComplete(s))},error:n=>{this.onSendError(n),this.isSending=!1,this.isStreaming=!1,this.scrollToBottom()}})}onKeyDown(e){e.key==="Enter"&&!e.shiftKey&&(e.preventDefault(),this.sendMessage())}addFiles(e){this.fileError=null;let t=this.MAX_FILES-this.pendingFiles.length;if(t<=0){this.fileError="lblAIAssistantTooManyFiles",this.fileErrorParam=this.MAX_FILES;return}let i=!1,r=this.readBatchId;Array.from(e).slice(0,t).forEach(s=>{if(s.size>this.MAX_FILE_SIZE){i=!0;return}this.pendingReads++;let o=new FileReader;o.onload=l=>{if(this.pendingReads--,r!==this.readBatchId)return;let n=l.target?.result;if(!n?.trim()){this.fileError="lblAIAssistantEmptyFile";return}this.pendingFiles.push({name:s.name,content:n,size:s.size})},o.onerror=()=>{this.pendingReads--,this.fileError="lblAIAssistantFileReadError"},o.readAsText(s)}),i&&(this.fileError="lblAIAssistantFileTooLarge",this.fileErrorParam=this.MAX_FILE_SIZE/1024)}removeFile(e){this.readBatchId++,this.pendingFiles.splice(e,1),this.fileError=null,this.fileErrorParam=null}onFileInputChange(e){let t=e.target;t.files?.length&&this.addFiles(t.files),t.value=""}onDragOver(e){e.preventDefault(),this.isDragging=!0}onDragLeave(e){e.currentTarget.contains(e.relatedTarget)||(this.isDragging=!1)}onDrop(e){e.preventDefault(),this.isDragging=!1;let t=e.dataTransfer?.files;t?.length&&this.addFiles(t)}loadAiConfig(){this.loadGlobalAiConfig()}onResponseComplete(e){e?.content||(e&&(this.messages=this.messages.filter(t=>t!==e)),this.messages.push({role:"error",content:"lblAIAssistantEmptyResponse"})),this.isSending=!1,this.isStreaming=!1,this.scrollToBottom()}onSendError(e){this.messages.push({role:"error",content:this.extractErrorMessage(e)})}scrollToBottom(){setTimeout(()=>{this.messagesContainer&&(this.messagesContainer.nativeElement.scrollTop=this.messagesContainer.nativeElement.scrollHeight)},100)}extractErrorMessage(e){if(e&&typeof e=="object"){let t=e;if(t.error&&typeof t.error=="object"){let i=t.error;if(typeof i.message=="string")return i.message}if(typeof t.message=="string")return t.message}return"lblAIAssistantError"}buildMessageContent(e){let t=e.files?.filter(r=>!!r.content?.trim());if(!t?.length)return e.content;let i=t.map(r=>`<file name="${r.name}">
${r.content}
</file>`).join(`

`);return e.content?`${i}

${e.content}`:i}loadGlobalAiConfig(){let e=this.connectionId?`api/ai/configuration?connectionId=${encodeURIComponent(this.connectionId)}`:"api/ai/configuration";this.httpClient.get(e).pipe(m(1)).subscribe({next:t=>{let i=t?.data;i&&!i.isEnabled?(this.isDisabled=!0,this.aiConfig=null):i&&i.provider!==c.None?(this.isDisabled=!1,this.aiConfig=new v({aiProvider:i.provider,aiModel:i.model,aiApiKey:""})):(this.isDisabled=!1,this.aiConfig=null)},error:()=>{this.aiConfig=null}})}processThinkChunk(e){e=this._thinkBuffer+e,this._thinkBuffer="";let t="",i=0,r=e.toLowerCase();for(;i<e.length;)if(this._thinkState==="inside"){let s=r.indexOf("</think>",i);if(s===-1)return this._thinkBuffer=this._extractPartialTag(e.slice(i),"</think>"),t;this._thinkState="none",i=s+8}else{let s=r.indexOf("<think>",i);if(s===-1){let o=e.slice(i),l=this._extractPartialTag(o,"<think>");return t+=o.slice(0,o.length-l.length),this._thinkBuffer=l,t}t+=e.slice(i,s),this._thinkState="inside",i=s+7}return t}_extractPartialTag(e,t){let i=e.toLowerCase(),r=t.toLowerCase();for(let s=Math.min(t.length-1,e.length);s>=1;s--)if(r.startsWith(i.slice(i.length-s)))return e.slice(e.length-s);return""}parseSseChunk(e){let t="",i;for(let r of e.split(`

`).filter(s=>s.trim())){let s=r.split(`
`),l=s.find(a=>a.startsWith("event: "))?.slice(7)==="error",n=s.filter(a=>a.startsWith("data: ")).map(a=>a.slice(6)).join(`
`);l?i=n:t+=n}return{content:t,error:i}}};h.\u0275fac=function(t){return new(t||h)(E(b))},h.\u0275dir=I({type:h,viewQuery:function(t,i){if(t&1&&S(X,5),t&2){let r;T(r=y())&&(i.messagesContainer=r.first)}},features:[A]});let g=h;return g})()});export{G as a,$ as b};
