web app: attach text files - drop them on the chat or use the attach button (issue #30)
Text files (code, notes, logs, data; up to 512 KB) become chips next to the message and are sent as "File: name" plus a fenced block (a longer fence when the file contains one). Binary files are refused with a toast; pictures work as before (when images are on). The attach button is always there now. Kept small on purpose: the web app is for quick tests and settings.
This commit is contained in:
1 parent
9ffd6c9461
commit
6255c8fd14
3 files changed
+76
-13
No files matched your search
@@ -58,6 +58,7 @@ main { flex: 1; min-height: 0; display: flex; }
|
||||
.st-btn--send { width: 42px; padding: 0; }
|
||||
.composer-hint { font-size: var(--st-fs-xs); color: var(--st-ink-muted); margin-right: var(--st-s-2); font-variant-numeric: tabular-nums; }
|
||||
.attachments { display: flex; flex-wrap: wrap; gap: var(--st-s-2); padding: 2px 4px 0; }
|
||||
.st-composer.dragging { outline: 2px dashed var(--st-accent); outline-offset: 3px; }
|
||||
.chip { display: inline-flex; align-items: center; gap: 6px; height: 30px; padding: 0 6px 0 10px; border-radius: var(--st-r-pill);
|
||||
background: var(--st-surface-2); border: 1px solid var(--st-line); font-size: var(--st-fs-xs); color: var(--st-ink-soft); }
|
||||
.chip .st-btn--icon { width: 22px; height: 22px; border-radius: 50%; }
|
||||
|
||||
+73
-11
@@ -98,7 +98,8 @@ let health = {model: "strata", images: false, max_context: 0};
|
||||
async function loadHealth() {
|
||||
try {
|
||||
health = await (await fetch("/health")).json();
|
||||
$("attach-btn").hidden = !health.images;
|
||||
$("attach-btn").title = health.images ? "Attach a text file or a picture (or drop it here)"
|
||||
: "Attach a text file (or drop it here)";
|
||||
$("chat-empty-sub").textContent = `${health.model} runs on this PC. Nothing leaves it.`;
|
||||
} catch (e) {
|
||||
setTimeout(loadHealth, 2000);
|
||||
@@ -394,7 +395,8 @@ let attachments = []; // {name, url}
|
||||
let busy = null; // {controller, msg}
|
||||
|
||||
function saveChat() {
|
||||
store.set("chat", messages.map((m) => ({...m, images: (m.images || []).map((i) => ({name: i.name}))})));
|
||||
store.set("chat", messages.map((m) => ({...m, images: (m.images || []).map((i) => ({name: i.name})),
|
||||
files: (m.files || []).map((f) => ({name: f.name}))})));
|
||||
}
|
||||
function timeStr(t) { return new Date(t).toLocaleTimeString([], {hour: "2-digit", minute: "2-digit"}); }
|
||||
|
||||
@@ -403,6 +405,18 @@ function msgEl(m, i) {
|
||||
el.className = `st-msg st-msg--${m.role}`;
|
||||
el.dataset.i = i;
|
||||
if (m.role === "user") {
|
||||
if (m.files && m.files.length) {
|
||||
const wrap = document.createElement("div");
|
||||
wrap.className = "msg-images";
|
||||
for (const f of m.files) {
|
||||
const c = document.createElement("span");
|
||||
c.className = "chip";
|
||||
c.innerHTML = icon("attach", "st-icon st-icon--sm");
|
||||
c.append(f.name);
|
||||
wrap.appendChild(c);
|
||||
}
|
||||
el.appendChild(wrap);
|
||||
}
|
||||
if (m.images && m.images.length) {
|
||||
const wrap = document.createElement("div");
|
||||
wrap.className = "msg-images";
|
||||
@@ -486,8 +500,9 @@ function apiMessages() {
|
||||
for (const m of messages) {
|
||||
if (m.role === "user") {
|
||||
const imgs = (m.images || []).filter((i) => i.url);
|
||||
out.push({role: "user", content: imgs.length ? [{type: "text", text: m.text},
|
||||
...imgs.map((i) => ({type: "image_url", image_url: {url: i.url}}))] : m.text});
|
||||
const text = userText(m);
|
||||
out.push({role: "user", content: imgs.length ? [{type: "text", text},
|
||||
...imgs.map((i) => ({type: "image_url", image_url: {url: i.url}}))] : text});
|
||||
} else if (m.text && !m.error) {
|
||||
out.push({role: "assistant", content: m.text});
|
||||
}
|
||||
@@ -504,7 +519,8 @@ function setBusy(on) {
|
||||
async function send() {
|
||||
const text = $("input").value.trim();
|
||||
if ((!text && !attachments.length) || busy) return;
|
||||
messages.push({role: "user", text, images: attachments, time: Date.now()});
|
||||
messages.push({role: "user", text, images: attachments.filter((a) => a.kind !== "file"),
|
||||
files: attachments.filter((a) => a.kind === "file"), time: Date.now()});
|
||||
attachments = [];
|
||||
renderAttachments();
|
||||
$("input").value = "";
|
||||
@@ -615,16 +631,45 @@ $("export-btn").onclick = () => {
|
||||
setTimeout(() => URL.revokeObjectURL(a.href), 5000);
|
||||
};
|
||||
|
||||
// pictures: the attach button, or paste into the message box
|
||||
// pictures and text files: the attach button, dropping them on the chat, or pasting a picture (issue #30)
|
||||
const TEXT_EXT = /\.(txt|md|markdown|rst|tex|py|pyi|ipynb|js|mjs|cjs|ts|tsx|jsx|vue|svelte|json|jsonl|csv|tsv|log|ya?ml|toml|ini|cfg|conf|env|xml|html?|css|scss|less|c|cc|cpp|cxx|h|hh|hpp|cu|cuh|rs|go|java|kt|kts|swift|rb|php|pl|lua|r|jl|scala|sql|sh|bash|zsh|fish|ps1|psm1|bat|cmd|diff|patch|gradle|cmake|mk|dockerfile|gitignore|proto|graphql)$/i;
|
||||
const MAX_TEXT_FILE = 512 * 1024;
|
||||
function isTextFile(f) {
|
||||
return f.type.startsWith("text/") || /json|xml|javascript|yaml|toml|x-sh|x-python/.test(f.type) ||
|
||||
TEXT_EXT.test(f.name) || /(^|[\\/])(makefile|dockerfile|readme|license)$/i.test(f.name);
|
||||
}
|
||||
function addFiles(files) {
|
||||
for (const f of files) {
|
||||
if (!f.type.startsWith("image/")) continue;
|
||||
if (f.size > 20e6) { toast("warn", "Picture too large", `${f.name} is over 20 MB.`); continue; }
|
||||
if (f.type.startsWith("image/")) {
|
||||
if (!health.images) { toast("warn", "Pictures are off", "This model was set up for text only."); continue; }
|
||||
if (f.size > 20e6) { toast("warn", "Picture too large", `${f.name} is over 20 MB.`); continue; }
|
||||
const r = new FileReader();
|
||||
r.onload = () => { attachments.push({kind: "image", name: f.name || "pasted image", url: r.result}); renderAttachments(); };
|
||||
r.readAsDataURL(f);
|
||||
continue;
|
||||
}
|
||||
if (!isTextFile(f)) { toast("warn", "Not a text file", `${f.name}: attach text files (code, notes, logs, data)${health.images ? " or pictures" : ""}.`); continue; }
|
||||
if (f.size > MAX_TEXT_FILE) { toast("warn", "File too large", `${f.name} is over 512 KB.`); continue; }
|
||||
const r = new FileReader();
|
||||
r.onload = () => { attachments.push({name: f.name || "pasted image", url: r.result}); renderAttachments(); };
|
||||
r.readAsDataURL(f);
|
||||
r.onload = () => {
|
||||
const text = String(r.result);
|
||||
if (text.includes("\u0000")) { toast("warn", "Not a text file", `${f.name} looks like a binary file.`); return; }
|
||||
attachments.push({kind: "file", name: f.name, text});
|
||||
renderAttachments();
|
||||
};
|
||||
r.readAsText(f);
|
||||
}
|
||||
}
|
||||
// a file's text in the message, fenced with more backticks than it contains itself
|
||||
function fileBlock(f) {
|
||||
const longest = Math.max(2, ...(f.text.match(/`+/g) || []).map((s) => s.length));
|
||||
const fence = "`".repeat(longest + 1);
|
||||
return `File: ${f.name}\n${fence}\n${f.text}\n${fence}`;
|
||||
}
|
||||
function userText(m) {
|
||||
const files = (m.files || []).filter((f) => f.text != null);
|
||||
return [m.text, ...files.map(fileBlock)].filter((s) => s).join("\n\n");
|
||||
}
|
||||
function renderAttachments() {
|
||||
const box = $("attachments");
|
||||
box.hidden = !attachments.length;
|
||||
@@ -632,7 +677,7 @@ function renderAttachments() {
|
||||
attachments.forEach((a, i) => {
|
||||
const c = document.createElement("span");
|
||||
c.className = "chip";
|
||||
c.innerHTML = icon("image", "st-icon st-icon--sm");
|
||||
c.innerHTML = icon(a.kind === "file" ? "attach" : "image", "st-icon st-icon--sm");
|
||||
c.append(a.name);
|
||||
const x = document.createElement("button");
|
||||
x.type = "button"; x.className = "st-btn st-btn--icon"; x.setAttribute("aria-label", "Remove");
|
||||
@@ -643,6 +688,23 @@ function renderAttachments() {
|
||||
});
|
||||
}
|
||||
$("attach-btn").onclick = () => $("file").click();
|
||||
// drop files on the chat or the message box
|
||||
for (const id of ["chat", "composer"]) {
|
||||
const el = $(id);
|
||||
el.addEventListener("dragover", (e) => {
|
||||
if (![...(e.dataTransfer || {}).types || []].includes("Files")) return;
|
||||
e.preventDefault();
|
||||
$("composer").classList.add("dragging");
|
||||
});
|
||||
el.addEventListener("dragleave", () => $("composer").classList.remove("dragging"));
|
||||
el.addEventListener("drop", (e) => {
|
||||
$("composer").classList.remove("dragging");
|
||||
if (!e.dataTransfer || !e.dataTransfer.files.length) return;
|
||||
e.preventDefault();
|
||||
addFiles(e.dataTransfer.files);
|
||||
$("input").focus();
|
||||
});
|
||||
}
|
||||
$("file").onchange = () => { addFiles($("file").files); $("file").value = ""; };
|
||||
$("input").addEventListener("paste", (e) => {
|
||||
if (!health.images) return;
|
||||
|
||||
@@ -50,9 +50,9 @@
|
||||
<div class="attachments" id="attachments" hidden></div>
|
||||
<textarea id="input" rows="2" placeholder="Ask anything…" aria-label="Message"></textarea>
|
||||
<div class="st-composer__bar">
|
||||
<button type="button" class="st-btn st-btn--icon" id="attach-btn" title="Attach a picture" aria-label="Attach a picture" hidden>
|
||||
<button type="button" class="st-btn st-btn--icon" id="attach-btn" title="Attach a text file (or drop it here)" aria-label="Attach a file">
|
||||
<svg class="st-icon"><use href="/web/sprite.svg#i-attach"/></svg></button>
|
||||
<input type="file" id="file" accept="image/*" multiple hidden>
|
||||
<input type="file" id="file" multiple hidden>
|
||||
<button type="button" class="st-btn st-btn--icon" id="new-btn" title="New chat" aria-label="New chat">
|
||||
<svg class="st-icon"><use href="/web/sprite.svg#i-new-chat"/></svg></button>
|
||||
<button type="button" class="st-btn st-btn--icon" id="export-btn" title="Save this chat as Markdown" aria-label="Save this chat">
|
||||
|
||||
Reference in new issue
Block a user