// Web UI logic: polls /status every 3 s, renders the meetings table, and wires search, Q&A, upload, crop and bulk actions to the local server. // Element ids from index.html used as window globals (plus bare addEventListener), declared for ESLint no-undef. /* global ab addEventListener ar bf bn bulk cdlg ce cgi_ cgo_ cmsg cpr cs ctitle cv llm llma llmr omps pgr q qa qs skp sr t ub ubar ubig ucancel up utitle utxt */ // Escape text for innerHTML (&, <, >, "). const E=value=>String(value??"").replace(/[&<>"]/g,char=>({"&":"&","<":"<",">":">",'"':"""}[char])); // Seconds -> mm:ss (minutes can exceed 59; no hour form here). const fmt=seconds=>{seconds=Math.round(seconds);return String(Math.floor(seconds/60)).padStart(2,"0")+":"+String(seconds%60).padStart(2,"0")}; // Enter in the search box runs the search. q.onkeydown=event=>{if(event.key==="Enter")srch()}; // Enter in the Q&A box asks the question. qa.onkeydown=event=>{if(event.key==="Enter")ask()}; // Close button appended after results: clears the input and the result box. const CLOSE=(target,output)=>`
`; // Q&A: /ask_stream sends NDJSON events ({sources} first, then {t} text pieces); the answer is redrawn as it grows. The model may take minutes to load on first use. async function ask(){if(!qa.value)return;ar.innerHTML="…avvio modelli / risposta (può richiedere qualche minuto)"; const response=await fetch("/ask_stream?q="+encodeURIComponent(qa.value));if(!response.ok){ar.textContent=await response.text();return} let src=[],txt="",buf="",fin=false; // Turn citation markers [n] into links to the matching report section; keep the marker if the source is missing. Sources list and close button appear when the stream ends. // Minimal markdown -> HTML (on escaped text): headings, bullet/numbered lists, **bold**, *italic*, `code`; blank-line separated paragraphs. const MD=text=>{let html="",ul=false;const inlineMd=str=>str.replace(/`([^`]+)`/g,"$1").replace(/\*\*(.+?)\*\*/g,"$1").replace(/(^|\W)\*([^*\s][^*]*?)\*/g,"$1$2"); for(const line of E(text).split("\n")){const item=line.match(/^\s*(?:[-*]|\d+\.) (.*)/);if(item){if(!ul){html+="";ul=false}const heading=line.match(/^#{1,6} (.*)/);if(heading)html+="

"+inlineMd(heading[1])+"

";else if(line.trim())html+="

"+inlineMd(line)+"

"} return html+(ul?"":"")}; const lk=num=>{const source=src[num-1];return source?`${num}`:num}; // [n], [1][2] and [1, 2] all become links; each number opens report.html at that source's section. const draw=()=>{ar.innerHTML=`
`+MD(txt).replace(/\[(\d+(?:\s*[,;]\s*\d+)*)\]/g,(match,group)=>"["+group.replace(/\d+/g,lk)+"]")+"
"+ (fin?(src.length?`
Fonti (${src.length})`+src.map(source=>`
[${lk(source.n)}] ${E(source.date)} ${E(source.meeting)} — ${E(source.topic)} ▶ ${fmt(source.t)} (${source.kind}): ${E(source.text)}
`).join("")+"
":"")+CLOSE("qa","ar"):"")}; const rd=response.body.getReader(),dc=new TextDecoder(); for(;;){const {value,done}=await rd.read();if(done)break;buf+=dc.decode(value,{stream:true});const ls=buf.split("\n");buf=ls.pop(); for(const line of ls){const event=JSON.parse(line);if(event.sources)src=event.sources;else if(event.err)txt+="\n[errore: "+event.err+"]";else txt+=event.t}draw()} fin=true;draw()} // Search over speech and screen text (/search). async function srch(){if(!q.value){sr.innerHTML="";return}const results=await (await fetch("/search?q="+encodeURIComponent(q.value))).json(); // Speech hits: [meeting, seconds, speaker, text]. Screen hits: [meeting, seconds, speaker, text, shot file] and link to the screenshot. sr.innerHTML=results.speech.map(hit=>`
${E(hit[0])} ${fmt(hit[1])} ${E(hit[2]||"")}: ${E(hit[3])}
`).join("")+results.screen.map(hit=>`
🖥 ${E(hit[0])} ${fmt(hit[1])} ${E(hit[2]||"")}: ${E(hit[3])}
`).join("")+CLOSE("q","sr")} // Reprocess dialog: pick the first step, then start the pipeline from it (/run); 409 means it is already running. let rname=""; function reprocess(name,mb){rname=name;rstep.innerHTML=["audio","transcribe","diarize","speakers","shots","docs","report","memory","render"].slice(mb?0:5).map(step=>`