From 32d8c859b138c453cabaef525c377b38180f6241 Mon Sep 17 00:00:00 2001 From: Ettore Di Giacinto Date: Fri, 30 May 2025 18:40:01 +0200 Subject: [PATCH] show preview of files Signed-off-by: Ettore Di Giacinto --- core/http/static/chat.js | 64 +++++++++++++++++++++++++++++++++++++++ core/http/views/chat.html | 36 +++++++++++++++++++--- 2 files changed, 96 insertions(+), 4 deletions(-) diff --git a/core/http/static/chat.js b/core/http/static/chat.js index 4100a9e8..8653689c 100644 --- a/core/http/static/chat.js +++ b/core/http/static/chat.js @@ -364,3 +364,67 @@ marked.setOptions({ return hljs.highlightAuto(code).value; }, }); + +document.addEventListener("alpine:init", () => { + Alpine.store("chat", { + history: [], + languages: [undefined], + systemPrompt: "", + clear() { + this.history.length = 0; + }, + add(role, content, image, audio) { + const N = this.history.length - 1; + if (this.history.length && this.history[N].role === role) { + this.history[N].content += content; + this.history[N].html = DOMPurify.sanitize( + marked.parse(this.history[N].content) + ); + // Merge new images and audio with existing ones + if (image && image.length > 0) { + this.history[N].image = [...(this.history[N].image || []), ...image]; + } + if (audio && audio.length > 0) { + this.history[N].audio = [...(this.history[N].audio || []), ...audio]; + } + } else { + let c = ""; + const lines = content.split("\n"); + lines.forEach((line) => { + c += DOMPurify.sanitize(marked.parse(line)); + }); + this.history.push({ + role, + content, + html: c, + image: image || [], + audio: audio || [] + }); + } + document.getElementById('messages').scrollIntoView(false); + const parser = new DOMParser(); + const html = parser.parseFromString( + this.history[this.history.length - 1].html, + "text/html" + ); + const code = html.querySelectorAll("pre code"); + if (!code.length) return; + code.forEach((el) => { + const language = el.className.split("language-")[1]; + if (this.languages.includes(language)) return; + const script = document.createElement("script"); + script.src = `https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.8.0/build/languages/${language}.min.js`; + document.head.appendChild(script); + this.languages.push(language); + }); + }, + messages() { + return this.history.map((message) => ({ + role: message.role, + content: message.content, + image: message.image, + audio: message.audio, + })); + }, + }); +}); diff --git a/core/http/views/chat.html b/core/http/views/chat.html index 01d396f1..4198c2aa 100644 --- a/core/http/views/chat.html +++ b/core/http/views/chat.html @@ -237,8 +237,22 @@ SOFTWARE.
You
-