/* rag_rqs Copyright (C) 2024 [Il tuo nome] This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ const VERS="0.1.62 (10-12-2024)";var xlog=console.log,xerror=console.error;const cancelRequest=()=>{confirm("Confermi Cancellazione Richeista ?")&&(HfRequest.cancelRequest(),hideSpinner())},showSpinner=()=>{const a=document.getElementById("spinner");a.classList.add("show-spinner");a.addEventListener("click",cancelRequest)},hideSpinner=()=>{const a=document.getElementById("spinner");a.classList.remove("show-spinner");a.removeEventListener("click",cancelRequest)};var tm; function openApp(){setTimeout(()=>{tm=umgm();wnds.init();Menu.init();TextInput.init();TextOutput.init();Rag.init();document.querySelector(".menu-btn").checked=!1;release();showHistory();getTheme()},10)}function showHistory(){const a=ThreadMgr.getThread();setOutText(a)}function release(){document.querySelector(".release").innerHTML=VERS}const op0=async function(a){a=await requestGet("./help1.html");wnds.wdiv.show(a)};function showQuery(a){wnds.wpre.show(`\n${Rag.ragQuery}`)} function showRagResponse(a){wnds.wpre.show(`\n${Rag.ragAnswer}`)}function showThread(a){a=ThreadMgr.getThread();wnds.wpre.show(a)}function elencoRisposte(a){a=[...Rag.answers];0==a.length&&(a=UaDb.readArray(ID_RESPONSES));0!=a.length&&(a=a.map((b,c)=>`\n[${c+1}]\n ${b.trim()}`).join("\n"),wnds.wpre.show(a))}function showContesto(a){wnds.wpre.show(`${Rag.ragContext}`)} function elencoDati(a){var b=UaDb.getAllIds();a=[];for(var c of b)b=UaDb.read(c).length,a.push(`${c} (${b})`);c=a.join("\n ");wnds.wpre.show(c)}const showT=a=>{wnds.wpre.show(DataMgr.docs[a])};function elencoDocs(){DataMgr.readDbDocs();DataMgr.readDbDocNames();var a=DataMgr.doc_names,b=UaJtfh();let c=0;b.append("");b=b.html();wnds.wdiv.show(`

${b}`)}function calcQuery(){DataMgr.readDbDocs();DataMgr.readDbDocNames();var a=[];let b=0,c=0;a.push("Documento Num.Parti");a.push("==================");for(const d of DataMgr.docs){const e=DataMgr.doc_names[c];c+=1;const f=Math.ceil(d.length/MAX_PROMPT_LENGTH);b+=f;a.push(`${e}   [${f}]`)}a.push("==================");a.push(`Totale num. Parti: ${b}`);a=a.join("\n");wnds.wpre.show(a)} function deleteDati(a){confirm("Confermi cancellazione dati?")&&(DataMgr.deleteJsonDati(),wnds.wdiv.close(),wnds.wpre.close(),TextOutput.clear())}function deleteSttorage(a){confirm("Confermi cancellazione documenti & dati?")&&(DataMgr.deleteJsonDati(),localStorage.clear(),wnds.wdiv.close(),wnds.wpre.close(),TextOutput.clear(),DataMgr.docs=[],DataMgr.doc_names=[])}async function help1(a){a=await requestGet("./data/help_test.html");wnds.wdiv.show(a)} function loadTestoEsempio(a){DataMgr.loadDoc(`data/${a}`);wnds.wdiv.close()}async function help2(a){a=await requestGet("./help2.html");wnds.wdiv.show(a)}const themeKey="theme";function getTheme(){const a=localStorage.getItem(themeKey);a&&"light"==a?(document.body.classList.add("theme-light"),document.documentElement.classList.toggle("invert")):document.body.classList.add("theme-dark")} function setLight(){document.documentElement.classList.toggle("invert");document.body.classList.remove("theme-dark");document.body.classList.add("theme-light");localStorage.setItem(themeKey,"light")}function setDark(){document.documentElement.classList.toggle("invert");document.body.classList.remove("theme-light");document.body.classList.add("theme-dark");localStorage.setItem(themeKey,"dark")} function showPrompts(a){0!=Rag.prompts.length&&(a=Rag.prompts.map((b,c)=>`[${c+1}]${b}\n`).join("\n"),wnds.wpre.show(a))};function removeTag(a){a=a.replace(/<<>>/g," ");return a=a.replace(/<>/g,"")} function cleanDoc(a){try{return a=removeTag(a),a=a.replace(/(\w+)-\s*\n(\w+)/g,"$1$2"),a=a.replace(/[\u00AD\u200B\u200C\u200D\u2060\uFEFF\u0008]/g,""),a=a.replace(/[\u00A0\u2000-\u200A\u202F\u205F\u3000\t\r\f\v]/g," "),a=a.replace(/\\([nrtfb])/g,"$1"),a=a.replace(/\\(u[0-9a-fA-F]{4}|x[0-9a-fA-F]{2})/g,"$1"),a=a.replace(/\\([a-zA-Z]:\\|\\\\[a-zA-Z0-9_]+\\)/g,"\\$1"),a=a.replace(/\\/g,""),a=a.replace("\u201c",'"').replace("\u201d",'"'),a=a.replace(/\n/g," "),a=a.replace(/ +([.,;:!?])/g,"$1"),a=a.split(/(?<=[.?!])\s+/).filter(b=> 5<=b.trim().length).map(b=>b.trim()).join("\n"),a=a.replace(/ +/g," "),a.trim()}catch(b){return console.error(b),"Errore di codifica del documento"}} function cleanResponse(a){try{return a=removeTag(a),a=a.replace(/[\u00AD\u200B\u200C\u200D\u2060\uFEFF]/g,""),a=a.replace(/[\u00A0\u2000-\u200A\u202F\u205F\u3000\t\r\f\v]/g," "),a=a.replace(/\\([nrtfb])/g,"$1"),a=a.replace(/\\(u[0-9a-fA-F]{4}|x[0-9a-fA-F]{2})/g,"$1"),a=a.replace(/\\([a-zA-Z]:\\|\\\\[a-zA-Z0-9_]+\\)/g,"\\\\$1"),a=a.replace(/\\/g,""),a=a.replace(/\n{3,}/g,"\n\n"),a=a.replace(/ +/g," "),a.trim()}catch(b){return console.error(b),`Errore di codifica nella risposta\n${b}`}} function cleanOut(a){return a=a.replace(/\n{3,}/g,"\n\n")};const ID_RESPONSES="id_responses",ID_DOC_NAMES="id_doc_names",ID_DOCS="id_docs;",ID_RAG="id_rag",ID_THREAD="id_thread",UaDb={create(a,b){localStorage.getItem(a)?consoe.error(`ID ${a} already exists.`):localStorage.setItem(a,b)},read(a){const b=localStorage.getItem(a);return null===b?(xlog(`UaDb.read ${a} not found.`),""):b},update(a,b){localStorage.getItem(a)?localStorage.setItem(a,b):xlog(`UaDb.update ${a} not found.`)},delete(a){localStorage.getItem(a)?localStorage.removeItem(a):consoe.error(`ID ${a} not found.`)}, save(a,b){localStorage.setItem(a,b)},getAllIds(){const a=[];for(let b=0;bString.fromCharCode((b.charCodeAt(0)-5+256)%256)).join("")).join("")};const model_name="mistralai/Mistral-7B-Instruct-v0.3";async function requestGet(a){try{var b=await fetch(a,{method:"GET",headers:{"Content-Type":"text/plain;charset=UTF-8"}});if(!b.ok)throw Error(`HTTP error! status: ${b.status}`);const c=await b.arrayBuffer();return(new TextDecoder("utf-8")).decode(c)}catch(c){throw console.error(c),b="AbortError"===c.name?"Request was aborted":c.message.includes("HTTP error! status")?c.message:"An error occurred",alert(`requestGet()\nurl: ${a}\n${b}`),c;}} const ERROR_TOKENS="ERROR_TOKENS",TIMEOUT_ERROR="TIMEOUT_ERROR",getErrorType=(a,b)=>500<=a?"SERVER_ERROR":400<=a&&500>a?"object"===typeof b&&"validation"===b.error_type?b.error.includes("tokens")?ERROR_TOKENS:"ERROR_VALIDATION":"CLIENT_ERROR":"UNKNOWN_ERROR",checkResponse=a=>a&&Array.isArray(a)&&0!==a.length?a[0].generated_text?"ok":"MISSING_GENERATED_TEXT":"INVALID_DATA",createErrorInfo=(a,b,c,d)=>JSON.stringify({status:a,statusText:b,errorType:d,message:c||"Errore sconosciuto"}),HfRequest={controller:null, isCancelled:!1,baseUrl:"https://api-inference.huggingface.co/models",async post(a,b=5E4){this.isCancelled=!1;this.controller=new AbortController;let c;try{const d=setTimeout(()=>{this.controller.abort()},b);c=await fetch(`${this.baseUrl}/${model_name}`,{method:"POST",headers:{Authorization:`Bearer ${tm}`,"Content-Type":"application/json"},body:JSON.stringify(a),signal:this.controller.signal});clearTimeout(d);if(this.isCancelled)return null;const e=await c.json();if(!c.ok){const g=getErrorType(c.status, e),k=createErrorInfo(c.status,c.statusText,e.error,g);throw Error(k);}const f=checkResponse(e);if("ok"!==f){const g=createErrorInfo(c.status,c.statusText,e.error,f);throw Error(g);}return e[0].generated_text.trim()}catch(d){if(this.isCancelled)return null;if("AbortError"===d.name)throw a=createErrorInfo(0,"Timeout","La richiesta \u00e8 scaduta",TIMEOUT_ERROR),Error(a);throw d;}},cancelRequest(){this.controller&&(this.isCancelled=!0,this.controller.abort())}}; function getErrorInfo(a){let b={};try{b=JSON.parse(a.message)}catch(c){a=`getErrorInfo\n${a}`,console.error(a),b={status:"0",statusText:"status_errorInof",errorType:"UNKNOWN_ERROR_errorInfo",message:a}}finally{return b}}function errorToText(a){let b;try{const c=JSON.parse(a.message);b=`Error: Status: ${c.status} Status Text: ${c.statusText} Error Type: ${c.errorType} Message: ${c.message}`}catch(c){console.error(`errorToText\n${c}`),b=`${c}`}finally{return b}};const MAX_PROMPT_LENGTH=76800,PROMPT_DECR=2048,TIMEOUT=6E4;function wait(a){return new Promise(b=>setTimeout(b,1E3*a))} const truncInput=(a,b)=>a.substring(0,a.length-b),setMaxLen=a=>a.substring(0,MAX_PROMPT_LENGTH-1E3),getPartSize=(a,b,c)=>{c=MAX_PROMPT_LENGTH-c;a.length+b.lengthc+100&&(a=c),c=a);return c},getPartDoc=(a,b)=>{const c=a.substring(0,b);a=a.substring(b).trim();return[c,a]},ragLog=(a,b,c,d)=>{const e=MAX_PROMPT_LENGTH;d=d.reduce((f,g)=>f+g.length,0);xlog(`${a} mx:${e} lft:${b} rgt:${c} arr:${d}`);a=formatRow([a,b,c,d],[8,-7,-7,-7]);UaLog.log(a)},Rag= {ragContext:"",ragQuery:"",ragAnswer:"",answers:[],docContextLst:[],prompts:[],doc:"",doc_part:"",init(){this.readRespsFromDb();this.readFromDb()},returnOk(){return 10r)break;[t,u]=getPartDoc(f,r);ragLog(`${c}) ${b},${d}`,t.length,u.length,this.answers);l=promptDoc(t,a,g);const p=getPayloadDoc(l);try{if(m=await HfRequest.post(p,TIMEOUT),!m)return""}catch(n){console.error("RR1)\n",n);const w=getErrorInfo(n);if(w.errorType===ERROR_TOKENS){UaLog.log(`Error tokens Doc ${l.length}`);h+=PROMPT_DECR;continue}else if(w.errorType===TIMEOUT_ERROR){UaLog.log("Error timeout Doc");continue}else UaLog.log(`Error ${n}`),m=`ERROR \n${n}\n`}d++;c++; f=u;m=cleanResponse(m);v.push(m);this.answers.push(`DOCUMENTO : ${g}_${d}\n${m}`)}const x=v.length;let q=v.join("\n\n");for(d="";;){l=promptBuildContext(q,this.ragQuery);const r=getPayloadBuildContext(l);try{if(d=await HfRequest.post(r,TIMEOUT),!d)return""}catch(p){console.error("RR2)",p);const n=getErrorInfo(p);if(n.errorType===ERROR_TOKENS){UaLog.log(`Error tokens build Context ${l.length}`);q=truncInput(q,PROMPT_DECR);q=setMaxLen(q);continue}else if(n.errorType===TIMEOUT_ERROR){UaLog.log("Error timeout build Context"); continue}else throw p;}break}UaLog.log(`context ${x} => ${d.length}`);d=`\n### DOCUMENTO: ${g}\n ${d}`;this.docContextLst.push(d)}}catch(e){throw console.error(e),e;}this.ragContext=this.docContextLst.join("\n\n");this.saveToDb();b="";c=this.ragContext;try{for(;;){let e=promptWithContext(c,a);const f=getPayloadWithContext(e);try{if(b=await HfRequest.post(f,TIMEOUT),!b)return""}catch(g){console.error("RR3)",g);const k=getErrorInfo(g);if(k.errorType===ERROR_TOKENS){UaLog.log(`Error tokens with Context ${e.length}`); c=truncInput(c,PROMPT_DECR);c=setMaxLen(c);continue}else if(k.errorType===TIMEOUT_ERROR){UaLog.log("Error timeout with Context");continue}else throw g;}break}this.ragAnswer=b=cleanResponse(b);this.saveRespToDb();ThreadMgr.init();this.saveToDb();UaLog.log(`Risposta (${this.ragAnswer.length})`)}catch(e){throw console.error(e),b=`${e}`,e;}finally{return b}},async requestContext(a){let b="";if(!this.ragContext){if(!await confirm("Contesto vuoto. Vuoi continuare?"))return"";this.ragContext="Sei un assitente AI dispoibile a soddisfare tutte le mi richieste"}if(ThreadMgr.isFirst()){ThreadMgr.init(); try{var c=this.ragContext;let d=ThreadMgr.getThread();for(;;){prompt=promptThread(c,d,a);const e=getPayloadThread(prompt);try{if(b=await HfRequest.post(e,TIMEOUT),!b)return""}catch(f){console.error("RR4)",f);const g=getErrorInfo(f);if(g.errorType===ERROR_TOKENS){UaLog.log(`Error tokens Thread Init ${prompt.length}`);d=truncInput(d,PROMPT_DECR);c=setMaxLen(c);continue}else if(g.errorType===TIMEOUT_ERROR){UaLog.log("Error timeout Thread Init");continue}else throw f;}break}b=cleanResponse(b);ThreadMgr.add(a, b);b=ThreadMgr.getThread();UaLog.log(`Inizio Conversazione (${prompt.length})`)}catch(d){throw console.error(d),b=`${d}`,d;}finally{return b}}else try{let d=this.ragContext,e=ThreadMgr.getThread();for(c="";;){c=promptThread(d,e,a);const f=getPayloadThread(c);try{if(b=await HfRequest.post(f,TIMEOUT),!b)return""}catch(g){console.error("RR5)",g);const k=getErrorInfo(g);if(k.errorType===ERROR_TOKENS){UaLog.log(`Error tokens Thread ${c.length}`);e=truncInput(e,PROMPT_DECR);continue}else if(k.errorType=== TIMEOUT_ERROR){UaLog.log("Error timeout Thread");continue}else throw g;}break}b=cleanResponse(b);ThreadMgr.add(a,b);b=ThreadMgr.getThread();UaLog.log(`Conversazione (${c.length})`)}catch(d){throw console.error(d),b=`${d}`,d;}finally{return b}}},LLM="## Assistant:",USER="## User:",ThreadMgr={rows:[],init(){this.rows=[];Rag.ragAnswer?this.add(Rag.ragQuery,Rag.ragAnswer):this.add("","")},add(a,b){this.rows.push([a,b]);UaDb.saveArray(ID_THREAD,ThreadMgr.rows)},getThread(){const a=[];for(const b of this.rows)a.push(`${USER}\n${b[0]}\n${LLM}\n${b[1]}\n`); return a.join("\n\n")},isFirst(){return 2>this.rows.length}};function promptDoc(a,b,c){return` SYSTEM: Sei un assistente AI specializzato nell'analisi documentale e nell'estrazione mirata di informazioni. Rispondi esclusivamente in italiano. TASK: Analizza il testo estratto dal documento "${c}" e identifica gli elementi rilevanti per rispondere alla domanda: "${b}". INSTRUCTIONS: 1. Analizza attentamente il testo fornito compreso fra i marker <<>> e <<>> . 2. Identifica gli elementi utili per rispondere alla domanda:"${b}". 3. Per ogni elemento individuato, fornisci un titolo ed una descrizione sintetica ma completa. 4. Riporta dettagli specifici (dati, citazioni, eventi, personaggi, luoghi, ..) se corelati alla domanda. 5. Se non ci sono informazioni rilevanti, rispondi con "NESSUNA INFORMAZIONE RILEVANTE". 6. Assicurati di generare la risposta esattamente secondo il formato di output specificato. DOMANDA: ${b} TESTO DA ANALIZZARE: <<>> ${a} <<>> OUTPUT_FORMAT: Genera una risposta strutturata come un elenco nel quale ogni elemento \u00e8 costituito da un breve titolo e da una descrizione concisa ma completa. RESPONSE: `}function promptBuildContext(a,b=""){return` SYSTEM: Sei un assistente AI esperto nella sintesi e nell'organizzazione mirata di informazioni. Rispondi sempre ed esclusivamente in italiano. TASK: Organizza e sintetizza le informazioni estratte da frammenti di testo di un documento, creando un contesto utile per rispondere alla domanda: "${b}". INSTRUCTIONS: 1. Analizza tutte le informazioni fornite. 2. Seleziona e raggruppa le informazioni simili. 3. Per ogni informazioni, genera una descrizione concisa. 4. Elenca i punti chiave essenziali per comprendere il contesto. 5. Riporta elementi specifici (dati, citazioni, eventi, luoghi, personaggi, ..) se utili. 6. Elabora inferenze logiche basate sulle informazioni, se rilevanti per il contesto. 7. Genera una sintesi finale. 8. Assicurati di strutturare la tua risposta esattamente secondo il formato di output specificato. INFORMAZIONI: <<>> ${a} <<>> OUTPUT_FORMAT: Genera una risposta strutturata come un elenco nel quale ogni elemento \u00e8 costituito da un breve titolo e da una descrizione concisa ma completa, alla fine dell'elenco aggiungi una sintesi globale ed eventuali inferenze logiche e collegamenti fra le varie informazioni. RESPONSE: `}function promptWithContext(a,b){return` SYSTEM: Sei un sistema AI specializzato nell'analisi di informazioni estratte da documenti. TASK: Elabora la risposta alla domanda "${b}" sulla base del contesto fornito. INSTRUCTIONS: 1. Analizza attentamente il contesto fornito, identificando le informazioni pertinenti alla domanda: "${b}". 2. Estrai i concetti chiave e formula inferenze ragionevoli basate sulle informazioni disponibili. 3. Inizia la risposta con una breve introduzione che presenta l'argomento e il contesto. 4. Procedi con un'analisi dettagliata delle informazioni rilevanti trovate nel contesto. 5. Concludi con una sintesi che riassume i punti chiave e fornisce una conclusione generale. 6. Se richiesto, cita le fonti facendo riferimento al "documento fornito" per il contesto dato, distinguendolo chiaramente da eventuali altre fonti citate all'interno del contesto stesso. 7. Mantieni uno stile di scrittura fluido durante tutta la risposta. 8. Assicurati che la risposta sia completa e risponda direttamente alla domanda posta. CONTESTO: <<>> ${a} <<>> DOMANDA: ${b} OUTPUT_FORMAT: Fornisci la risposta in testo semplice e lineare suddiviso in paragrafi. RESPONSE: `}function promptThread(a,b,c){return` SYSTEM: Sei un assistente AI versatile progettato per gestire conversazioni dinamiche e adattarti a varie richieste. Rispondi sempre in italiano. TASK: Elabora la risposta alla richiesta "${c}" sulla base del contesto fornito e della conversazione. INSTRUCTIONS: 1. Analizza attentamente il contesto, la conversazione precedente e la richiesta: "${c}". 2. Interpreta l'intento dell'utente senza limitarti a categorie predefinite. 3. Adatta la tua risposta in base all'intento percepito, sia esso una domanda, una richiesta di azione, un'istruzione specifica o altro. 4. Mantieni una stretta coerenza con il contesto della conversazione. 5. Basa la tua risposta sulle informazioni fornite nel contesto e nella conversazione. 6. Evita divagazioni o argomentazioni non direttamente pertinenti alla richiesta o al contesto. 7. Fai riferimento a informazioni precedenti quando sono pertinenti, citando specificamente la fonte. 8. Se l'intento non \u00e8 chiaro, chiedi gentilmente chiarimenti invece di fare supposizioni. 9. Sii flessibile: se la richiesta implica un'azione specifica, adattati di conseguenza. 10. Se \u00e8 necessario integrare con conoscenze generali, specifica chiaramente quando lo stai facendo. CONTESTO: <<>> ${a} <<>> <<>> ${b} <<>> RICHIESTA: ${c} OUTPUT_FORMAT: Fornisci la risposta in testo semplice e lineare suddiviso in paragrafi. RESPONSE: `}function getPayloadDoc(a){return{inputs:a,parameters:{task:"text2text-generation",max_new_tokens:2E3,num_return_sequences:1,temperature:.4,top_p:.85,top_k:30,do_sample:!1,no_repeat_ngram_size:3,num_beams:4,repetition_penalty:1.2,return_full_text:!1,details:!1,max_time:90,seed:42},options:{use_cache:!1,wait_for_model:!0}}} function getPayloadBuildContext(a){return{inputs:a,parameters:{task:"text2text-generation",max_new_tokens:6E3,num_return_sequences:1,temperature:.7,top_p:.85,top_k:30,do_sample:!1,no_repeat_ngram_size:4,num_beams:6,repetition_penalty:1.2,return_full_text:!1,details:!1,max_time:180,seed:42},options:{use_cache:!1,wait_for_model:!0}}} function getPayloadWithContext(a){return{inputs:a,parameters:{task:"text2text-generation",max_new_tokens:4E3,num_return_sequences:1,temperature:.7,top_p:.85,top_k:30,do_sample:!1,no_repeat_ngram_size:4,num_beams:5,repetition_penalty:1.4,return_full_text:!1,details:!1,max_time:120,seed:42},options:{use_cache:!1,wait_for_model:!0}}} function getPayloadThread(a){return{inputs:a,parameters:{task:"text2text-generation",max_new_tokens:6048,num_return_sequences:1,temperature:.7,top_p:.85,top_k:30,do_sample:!1,no_repeat_ngram_size:4,num_beams:5,repetition_penalty:1.4,return_full_text:!1,details:!1,max_time:120,seed:42},options:{use_cache:!1,wait_for_model:!0}}};const WndPre=a=>({w:UaWindowAdm.create(a),out:null,show(b){wnds.closeAll();b=`
${b}
`;this.w.drag();this.w.setZ(12);this.w.vw_vh().setXY(16.5,10,-1);this.w.setHtml(b);this.w.show();this.w.getElement().querySelector(".btn-copy").addEventListener("click",()=>this.copy())},close(){this.w.close()},async copy(){const b=this.w.getElement().querySelector(".pre-text").textContent;try{await navigator.clipboard.writeText(b)}catch(c){console.error("Errore ",c)}}}),WndDiv=a=>({w:UaWindowAdm.create(a),out:null,show(b){wnds.closeAll();b=`
${b}
`;this.w.drag();this.w.setZ(12);this.w.vw_vh().setXY(16.5,10,-1);this.w.setHtml(b);this.w.show();this.w.getElement().querySelector(".btn-copy").addEventListener("click",()=>this.copy())},close(){this.w.close()},async copy(){const b=this.w.getElement().querySelector(".div-text").textContent;try{await navigator.clipboard.writeText(b)}catch(c){console.error("Errore ",c)}}}),wnds={wdiv:null,wpre:null,wout:null,init(){this.wdiv=WndDiv("id_w0");this.wpre=WndPre("id_w1");this.wout=WndPre("id_out")}, closeAll(){UaWindowAdm.close("id_w0");UaWindowAdm.close("id_w1");UaWindowAdm.close("id_out")}},Menu={init(){const a=document.querySelector("#id-menu-btn");a.addEventListener("change",()=>{document.querySelector("body").classList.toggle("menu-open",a.checked)});const b=document.getElementById("id_log");UaLog.callHide=()=>{b.classList.contains("active")&&b.classList.remove("active")};UaLog.callShow=()=>{b.classList.contains("active")||b.classList.add("active")};UaLog.setXY(54,13).setZ(111).new();UaLog.log_show("")}, close(){const a=document.querySelector("#menu-toggle");document.querySelector("body").classList.remove("menu-open",a.checked);document.querySelector(".menu-btn").checked=!1},async help(){const a=await requestGet("./help0.html");wnds.wdiv.show(a)},upload(){RagUpload.open()},uploadDir(){RagUpload.openDir()},async load(){alert("load")},log(){UaLog.toggle()}},setOutText=a=>{const b=document.querySelector("#id-text-out .pre-text");b.textContent=a;b.scrollTop=b.scrollHeight},TextInput={wnd:null,init(){this.inp= document.querySelector(".text-input");document.addEventListener("keydown",a=>{document.activeElement!==this.inp&&("F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12 Control Alt Shift Meta CapsLock Escape PrintScreen ScrollLock Pause Insert Delete Home End PageUp PageDown ArrowLeft ArrowRight ArrowUp ArrowDown".split(" ").includes(a.key)||a.ctrlKey||a.metaKey||a.preventDefault())});document.querySelector(".clear-button").addEventListener("click",()=>{this.inp.value="";this.inp.focus()});this.inp.addEventListener("keydown", a=>this.handleEnter(a));document.querySelector(".send-input").addEventListener("click",()=>this.send());document.querySelector(".send2-input").addEventListener("click",()=>this.send2());document.querySelector(".clear-input").addEventListener("click",()=>this.clear())},handleEnter(a){"Enter"!==a.key||a.shiftKey||(a.preventDefault(),this.send2())},async send(){if(this.inp.value){if(Rag.ragContext&&!confirm("Vuoi iniziare una nuova elabrazione ?"))return"";showSpinner();setOutText("");var a=this.inp.value.trim(); try{let b=await Rag.requestDocsRAG(a);b=cleanOut(b);setOutText(b);this.inp.value="";UaLog.close()}catch(b){a=`send\n${b}`,console.error(a),alert(a),setOutText(a)}hideSpinner()}else alert("Ricorda di scrivere la Query ")},async send2(){if(this.inp.value){showSpinner();ThreadMgr.isFirst()&&setOutText("");var a=this.inp.value.trim();try{let b=await Rag.requestContext(a);if(""==b){hideSpinner();return}b=cleanOut(b);setOutText(b);this.inp.value=""}catch(b){a=`send2\n${b}`,console.error(a),alert(a),setOutText(a)}hideSpinner()}else alert("Ricorda di scrivere la Query ")}, clear(){confirm("Confermi cancellazione conversazione? ")&&(this.inp.value="",setOutText(""),ThreadMgr.init())}}; TextOutput={init(){this.copyBtn=document.querySelector(".copy-output");this.copyBtn.addEventListener("click",()=>this.copy());document.querySelector(".clear-output").addEventListener("click",()=>this.clear());document.querySelector(".wnd-output").addEventListener("click",()=>this.openWnd())},openWnd(){const a=document.querySelector("#id-text-out .pre-text").textContent;wnds.wout.show(a)},async copy(){const a=document.querySelector("#id-text-out .pre-text"),b=a.textContent;if(!(2>b.trim().length)){a.classList.add("copied"); this.copyBtn.classList.add("copied");try{await navigator.clipboard.writeText(b)}catch(c){console.error("Errore ",c)}setTimeout(()=>{this.copyBtn.classList.remove("copied");a.classList.remove("copied")},5E3)}},clear(){document.querySelector("#id-text-out .pre-text").textContent=""}};const RagUpload={open(){const a=UaWindowAdm.create("id_upload");a.drag();a.setZ(12);a.vw_vh().setXY(16.5,10,-1);a.setHtml('\n
\n
\n \n
\n
\n

Upload file Text / PDF / DOCX

\n
\n \n \n
\n
\n
\n
\n '); a.show()},async upload(){const a=document.getElementById("id_fileupload").files[0];if(a){var b=a.name;if(DataMgr.doc_names.includes(b))alert("Il file \u00e8 gi\u00e0 in archivio");else{var c=document.getElementById("result"),d=a.name.split(".").pop().toLowerCase();showSpinner();try{let e;if("pdf"===d){const f=new PdfHandler;await f.loadPdfJs();e=await f.extractTextFromPDF(a);f.cleanup()}else if("txt"===d)e=await readTextFile(a);else if("docx"===d){const f=new DocxHandler;await f.loadMammoth();e=await f.extractTextFromDocx(a); f.cleanup()}else{alert("Formato file non supportato.");return}DataMgr.addDoc(b,e);c.innerHTML=`

${b}

caricato e salvato nella memoria locale`}catch(e){consoe.error("Error:",e),alert("Errore durante l'estrazione del testo dal file.")}finally{hideSpinner()}}}else alert("Nessun file selezionato.")},openDir(){const a=UaWindowAdm.create("id_upload");a.drag();a.setZ(12);a.vw_vh().setXY(16.5,10,-1);a.setHtml('\n
\n
\n \n
\n
\n

Upload files Text / PDF / DOCX

\n
\n \n \n
\n
\n
\n
\n '); a.show()},async uploadDir(){var a=document.getElementById("id_fileupload").files;if(a&&0!=a.length){var b=[];showSpinner();try{for(const c of a){const d=c.name;UaLog.log_show(d);if(DataMgr.doc_names.includes(d)){UaLog.log_show(d+" : \u00e8 gi\u00e0 in archivio");continue}const e=c.name.split(".").pop().toLowerCase();let f;if("pdf"===e){const g=new PdfHandler;await g.loadPdfJs();f=await g.extractTextFromPDF(c);g.cleanup()}else if("txt"===e)f=await readTextFile(c);else if("docx"===e){const g=new DocxHandler; await g.loadMammoth();f=await g.extractTextFromDocx(c);g.cleanup()}else{alert("Formato file non supportato.");return}DataMgr.addDoc(d,f);b.push(`${d}`)}}catch(c){consoe.error("Error:",c),alert("Errore durante l'estrazione del testo dal file.")}finally{hideSpinner()}a=document.getElementById("result");b=b.join("
");a.innerHTML=b}else alert("Nessun file selezionato.")}}; class PdfHandler{constructor(){this.workerScriptElement=this.scriptElement=this.pdfjsLib=null}async loadPdfJs(){window.pdfjsLib||(this.scriptElement=document.createElement("script"),this.scriptElement.src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.6.347/pdf.min.js",document.body.appendChild(this.scriptElement),await new Promise(a=>{this.scriptElement.onload=()=>{this.workerScriptElement=document.createElement("script");this.workerScriptElement.src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.6.347/pdf.worker.min.js"; document.body.appendChild(this.workerScriptElement);this.workerScriptElement.onload=a}}));this.pdfjsLib=window["pdfjs-dist/build/pdf"];this.pdfjsLib.GlobalWorkerOptions.workerSrc="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.6.347/pdf.worker.min.js"}async extractTextFromPDF(a){a=await a.arrayBuffer();a=await this.pdfjsLib.getDocument({data:a}).promise;let b="";for(let c=1;c<=a.numPages;c++){const d=(await (await a.getPage(c)).getTextContent()).items.map(e=>e.str).join(" ");b+=d+"\n"}return b}cleanup(){this.scriptElement&& (document.body.removeChild(this.scriptElement),this.scriptElement=null);this.workerScriptElement&&(document.body.removeChild(this.workerScriptElement),this.workerScriptElement=null);this.pdfjsLib=null;window.gc&&window.gc()}} class DocxHandler{constructor(){this.scriptElement=this.mammoth=null}async loadMammoth(){window.mammoth?this.mammoth=window.mammoth:(this.scriptElement=document.createElement("script"),this.scriptElement.src="https://cdnjs.cloudflare.com/ajax/libs/mammoth/1.4.11/mammoth.browser.min.js",document.body.appendChild(this.scriptElement),await new Promise(a=>{this.scriptElement.onload=()=>{this.mammoth=window.mammoth;a()}}))}async extractTextFromDocx(a){a=await a.arrayBuffer();return(await this.mammoth.extractRawText({arrayBuffer:a})).value}cleanup(){this.scriptElement&& (document.body.removeChild(this.scriptElement),this.scriptElement=null);this.mammoth=null;window.gc&&window.gc()}}async function readTextFile(a){if(!a||"text/plain"!==a.type)throw Error("Invalid file type. Please select a text file.");return new Promise((b,c)=>{const d=new FileReader;d.onload=e=>b(e.target.result);d.onerror=e=>c(Error("Error reading file: "+e.message));d.readAsText(a)})};const nodrag_tds=["input","select","a"],nodrag_cls="nodrag"; var UaDrag=function(a){return function(b){let c=0,d=0,e=0,f=0;const g=function(h){h=h||window.event;h.preventDefault();c=e-h.clientX;d=f-h.clientY;e=h.clientX;f=h.clientY;b.style.top=b.offsetTop-d+"px";b.style.left=b.offsetLeft-c+"px"},k=function(){document.onmouseup=null;document.onmousemove=null};b.onmousedown=function(h){h=h||window.event;let l=h.target;!(l=l||null)||nodrag_tds.includes(l.tagName.toLowerCase())||l.classList.contains(nodrag_cls)||(h.preventDefault(),e=h.clientX,f=h.clientY,document.onmouseup= k,document.onmousemove=g)}}(a)};const UaJtfh=()=>({rows:[],init(){this.rows=[];return this},insert(a){this.rows.unshift(a);return this},append(a){this.rows.push(a);return this},text(a=""){return this.rows.join(a)},html(a=""){return this.rows.join(a).replace(/\s+|\[rn\]/g," ")}});function formatRow(a,b){return a.map((c,d)=>{d=b[d];return 0>d?c.toString().padStart(Math.abs(d)," "):c.toString().padEnd(d," ")}).join(" ")} var UaLog={callHide:function(){},callShow:function(){},active:!1,wind:null,x:null,y:null,z:null,max_length:2E3,msg_id:"ualogmsg_",new:function(){null==this.wind&&(this.wind=UaWindowAdm.create("ualog_"),this.wind.drag());this.wind.setHtml('\n \n \n
');this.x?this.wind.vw_vh().setXY(this.x,
this.y,-1):this.wind.setCenter(-1);this.z&&this.wind.setZ(this.z);return this},setXY(a,b){this.x=a;this.y=b;return this},setZ(a){this.z=a;return this},prn_(...a){a=a.join("\n");let b=document.getElementById(this.msg_id);b.textContent=b.textContent+a+"\n"},print(...a){null!=this.wind&&this.active&&this.prn_(...a)},log(...a){null!=this.wind&&this.prn_(...a)},log_show(...a){null!=this.wind&&(this.active||this.toggle(),this.prn_(...a))},cls(){if(null!=this.wind)return document.getElementById(this.msg_id).innerHTML=
"",this},close(){null!=this.wind&&(this.wind.hide(),this.active=!1,this.callHide())},toggle(){null!=this.wind&&(this.active?(this.active=!1,this.wind.hide(),this.callHide()):(this.active=!0,this.wind.show(),this.callShow()))}};var UaWindowAdm={ws:{},create(a,b=null){let c=document.getElementById(a);c||(c=document.createElement("div"),b?document.getElementById(b).appendChild(c):document.body.appendChild(c),c.id=a,c.setAttribute("data-name","ua-window"),b=this.newUaWindow(c),this.ws[a]=b);a=this.ws[a];c.style.display="none";return a},get(a){return this.ws[a]?this.ws[a]:null},show(a){this.ws[a]&&this.ws[a].show()},close(a){this.ws[a]&&this.ws[a].close()},toggle(a){this.ws[a]&&this.ws[a].toggle()},hide(a){this.ws[a]&&this.ws[a].hide()},
closeThis(a){a=a.closest('[data-name="ua-window"]').id;this.ws[a].close()},showAll(){for(let a in this.ws)this.ws[a].show()},hideAll(){for(let a in this.ws)this.ws[a].hide()},closeAll(){for(let a in this.ws)this.ws[a].close()},remove(a){this.ws[a]&&(document.getElementById(a).remove(),this.ws[a]=null,delete this.ws[a])},removeAll(){for(let a in this.ws)this.remove(a);this.ws={}},newUaWindow(a){let b={initialize(c){this.w=c;this.wy=this.wx="0px";this.isVisible=this.isOpen=!1;this.firstShow=!0;this.wz=
this.pos=0;this.vh=this.vw="px"},vw_vh(){this.vw="vw";this.vh="vh";return this},addClassStyle(c){this.w.classList.contains(c)||this.w.classList.add(c);return this},removeClassStyle(c){this.w.classList.contains(c)&&this.w.classList.remove(c);return this},getWindow(){alert("getWindow => ??");return this.w},getElement(){return this.w},getId(){return this.w.id},setStyle(c){for(const d in c)this.w.style[d]=c[d];return this},setHtml(c){this.w.innerHTML=c;return this},getHtml(){return this.w.innerHTML},
setXY(c,d,e=0){this.wx=c;this.wy=d;this.pos=e;return this},setCenterY(c,d){this.setXY((window.innerWidth-this.w.clientWidth)/2,c,d);return this},setCenter(c){this.setXY((window.innerWidth-this.w.clientWidth)/2,(window.innerHeight-this.w.clientHeight)/2,c);return this},linkToId(c,d,e,f){c=document.getElementById(c);this.linkToElement(c,d,e,f);return this},linkToElement(c,d,e,f){d=c.offsetLeft+c.offsetWidth+d;c=c.offsetTop+e;0>c&&(c=0);this.setXY(d,c,f);return this},setZ(c){this.wz=c;return this},reset(){this.firstShow=
!0;return this},toggle(){this.isVisible?this.hide():this.show();return this},show(){if(this.firstShow||1==this.pos||0===this.pos&&!1===this.isVisible)this.w.style.position="absolute",this.w.style.marginLeft=0,this.w.style.marginTop=0,this.w.style.top=`${this.wy}${this.vh}`,0<=this.wx?this.w.style.left=`${this.wx}${this.vw}`:this.w.style.right=-`${this.wx}${this.vw}`,0