stram response, like ais

This commit is contained in:
Stefano Zini
2026-11-05 08:50:19 +01:00
parent d3c6816181
commit 19de4654ff
6 changed files with 72 additions and 18 deletions
+2
View File
@@ -23,6 +23,8 @@ button{cursor:pointer;background:var(--soft)}button:hover{border-color:var(--mut
button.pri{background:var(--acc);border-color:var(--acc);color:var(--on)}button.pri:hover{background:var(--acc2)}
/* Search and Q&A result containers. */
#sr,#ar{margin-top:10px}#sr>div,#ar>div{padding:4px 0}#ar>div:first-child{font-size:15px;padding-bottom:8px}
/* Rendered markdown answer: tight spacing; sources are collapsed in <details>. */
.md p,.md ul,.md h4{margin:4px 0}.md ul{padding-left:20px}.md code{background:var(--bg);padding:0 4px;border-radius:4px}#ar details{margin-top:6px}#ar summary{cursor:pointer}#ar details>div{padding:2px 0}
/* Meetings table: full width, rows separated by lines. */
table{border-collapse:collapse;width:100%}td,th{padding:10px 12px;border-bottom:1px solid var(--line);text-align:left;vertical-align:top}tbody.rec:last-child tr:last-child td{border-bottom:0}
/* Alternate background per recording group (tbody.rec); accent bar marks the main row. */
+19 -7
View File
@@ -9,12 +9,24 @@ q.onkeydown=e=>{if(e.key=="Enter")srch()};
qa.onkeydown=e=>{if(e.key=="Enter")ask()};
// Close button appended after results: clears the input and the result box.
const CLOSE=(i,o)=>`<div><button onclick="${i}.value='';${o}.innerHTML=''">✕ chiudi</button></div>`;
// Q&A: /ask answers from the meeting data. The model may take minutes to load on first use.
// 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="<small>…avvio modelli / risposta (può richiedere qualche minuto)</small>";
const r=await fetch("/ask?q="+encodeURIComponent(qa.value));if(!r.ok){ar.textContent=await r.text();return}const j=await r.json();
// Turn citation markers [n] into links to the matching report section; keep the marker if the source is missing.
ar.innerHTML="<div>"+E(j.answer).replace(/\[(\d+)\]/g,(m,n)=>{const s=j.sources[n-1];return s?`<a href="/report?name=${s.name}#${s.anchor}" target=_blank>[${n}]</a>`:m})+"</div>"+
j.sources.map(s=>`<div><small>[${s.n}] ${E(s.date)} ${E(s.meeting)} — ${E(s.topic)} ${fmt(s.t)} (${s.kind}): ${E(s.text)}</small></div>`).join("")+CLOSE("qa","ar")}
const r=await fetch("/ask_stream?q="+encodeURIComponent(qa.value));if(!r.ok){ar.textContent=await r.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=t=>{let h="",ul=false;const il=s=>s.replace(/`([^`]+)`/g,"<code>$1</code>").replace(/\*\*(.+?)\*\*/g,"<b>$1</b>").replace(/(^|\W)\*([^*\s][^*]*?)\*/g,"$1<i>$2</i>");
for(const l of E(t).split("\n")){const m=l.match(/^\s*(?:[-*]|\d+\.) (.*)/);if(m){if(!ul){h+="<ul>";ul=true}h+="<li>"+il(m[1])+"</li>";continue}
if(ul){h+="</ul>";ul=false}const g=l.match(/^#{1,6} (.*)/);if(g)h+="<h4>"+il(g[1])+"</h4>";else if(l.trim())h+="<p>"+il(l)+"</p>"}
return h+(ul?"</ul>":"")};
const lk=n=>{const s=src[n-1];return s?`<a href="/report?name=${encodeURIComponent(s.name)}#${encodeURIComponent(s.anchor)}" target=_blank title="${E(s.meeting)} — ${E(s.topic)}">${n}</a>`:n};
// [n], [1][2] and [1, 2] all become links; each number opens report.html at that source's section.
const draw=()=>{ar.innerHTML=`<div class=md>`+MD(txt).replace(/\[(\d+(?:\s*[,;]\s*\d+)*)\]/g,(m,l)=>"["+l.replace(/\d+/g,lk)+"]")+"</div>"+
(fin?(src.length?`<details><summary><small>Fonti (${src.length})</small></summary>`+src.map(s=>`<div><small>[${lk(s.n)}] ${E(s.date)} ${E(s.meeting)} — ${E(s.topic)} <a href="#" title="riproduci il video da qui" onclick="play('${s.name}',0,'${E(s.meeting+" "+s.date).replace(/'/g,"")}',${+s.t||0});return false">▶ ${fmt(s.t)}</a> (${s.kind}): ${E(s.text)}</small></div>`).join("")+"</details>":"")+CLOSE("qa","ar"):"")};
const rd=r.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 l of ls){const e=JSON.parse(l);if(e.sources)src=e.sources;else if(e.err)txt+="\n[errore: "+e.err+"]";else txt+=e.t}draw()}
fin=true;draw()}
// Search over speech and screen text (/search).
async function srch(){if(!q.value){sr.innerHTML="";return}const j=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.
@@ -68,8 +80,8 @@ async function setTitle(n,cur){const t=prompt("Titolo della riunione (vuoto = au
const r=await fetch("/title?name="+n,{method:"POST",body:t});if(!r.ok)alert(await r.text());tick()}
// "mm:ss" or "h:mm:ss" -> seconds.
const sec=s=>s.trim().split(":").reduce((a,x)=>a*60+(+x),0);
// Open the preview dialog for recording n; the video is streamed from /video.
function play(n,d,t){cv.src="/video?name="+n;ctitle.textContent=t;cs.value="00:00";ce.value=d?fmt(d):"";cmsg.textContent="";cv.onloadedmetadata=()=>{if(!ce.value&&isFinite(cv.duration))ce.value=fmt(cv.duration)};cdlg.showModal()}
// Open the preview dialog for recording n; the video is streamed from /video. Optional at = seconds to start playing from (used by the Q&A sources).
function play(n,d,t,at){cv.src="/video?name="+n;ctitle.textContent=t;cs.value="00:00";ce.value=d?fmt(d):"";cmsg.textContent="";cv.onloadedmetadata=()=>{if(!ce.value&&isFinite(cv.duration))ce.value=fmt(cv.duration);if(at>0){cv.currentTime=at;cv.play().catch(()=>{})}};cdlg.showModal()}
// Stop and unload the video so the file handle is released.
function cclose(){cv.pause();cv.removeAttribute("src");cv.load();cdlg.close()}
// Esc closes the dialog through the same cleanup.