feat: file/image upload + separated framework root (sessions/data/uploads)
- POST /api/uploads: base64 -> shared ~/.strata-ctl/uploads store (uuid+ext), content-sniffed kind (image/text/binary), text token-counted via the model tokenizer; metadata persisted in a new uploads table. - Chat attachments routed by kind: images -> Vision bridge (multipart image_url), small text (<=2000 tokens) inlined, large text/binary -> path + shell_exec reminder; binaries never inlined. Attachment context persisted with the turn. - GUI: attach button + file picker, removable chips, attachments sent with the turn and shown on the user bubble; cleared on session switch. - Framework root ~/.strata-ctl separated by type: sessions/<name>/, data/ (SQLite, moved from ~/.config/strata-ctl), uploads/. Legacy DB copied once on first run. - count_tokens seam on Formatter (real tokenizer override) + ChatService. - i18n: upload errors + attachment markers (backend) and attach.* (GUI), it+en. - 121 tests + 2 ignored; clippy/fmt clean; validated live (upload routing, real tokenizer count, image->Vision answered "Rosso", new layout).
This commit is contained in:
1 parent
498e3dbc1b
commit
65f8111ba2
19 files changed
+672
-47
No files matched your search
@@ -177,6 +177,27 @@ details.thinking .reasoning {
|
||||
}
|
||||
#input:focus { outline: none; border-color: var(--accent); }
|
||||
#send { min-width: 52px; font-size: 1.3rem; }
|
||||
#attach { min-width: 44px; font-size: 1.2rem; }
|
||||
|
||||
/* ---- Attachment chips (composer + sent user turn) ---- */
|
||||
.attachments {
|
||||
display: flex; flex-wrap: wrap; gap: 6px;
|
||||
padding: 8px 12px 0; background: var(--panel);
|
||||
}
|
||||
.attach-chip {
|
||||
display: inline-flex; align-items: center; gap: 6px;
|
||||
max-width: 240px; padding: 4px 8px;
|
||||
background: var(--panel-2); border: 1px solid var(--border); border-radius: 999px;
|
||||
font-size: 0.8rem; color: var(--text);
|
||||
}
|
||||
.attach-chip .attach-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
||||
.attach-chip .attach-kind { color: var(--muted); }
|
||||
.attach-chip button {
|
||||
border: none; background: transparent; color: var(--muted);
|
||||
cursor: pointer; font-size: 0.9rem; line-height: 1; padding: 0 2px;
|
||||
}
|
||||
.attach-chip button:hover { color: var(--danger); }
|
||||
.msg-attachments { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
|
||||
|
||||
/* ---- Stats modal ---- */
|
||||
#stats-modal, #dialog, #settings-modal {
|
||||
|
||||
+80
-11
@@ -5,7 +5,7 @@
|
||||
|
||||
const $ = (sel) => document.querySelector(sel);
|
||||
|
||||
const state = { sessions: [], current: null, busy: false, agent: false };
|
||||
const state = { sessions: [], current: null, busy: false, agent: false, attachments: [] };
|
||||
|
||||
// ---- i18n: new user-facing strings go through t(key, params) --------------
|
||||
// Catalogs are served at /assets/locales/<lang>.json; language follows the
|
||||
@@ -52,11 +52,11 @@ async function sendJSON(url, method, body) {
|
||||
}
|
||||
|
||||
// Stream a chat turn, invoking onDelta for each SSE chunk.
|
||||
async function streamChat(id, content, onDelta) {
|
||||
async function streamChat(id, content, attachments, onDelta) {
|
||||
const r = await fetch(`/api/sessions/${id}/chat`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ content, stream: true }),
|
||||
body: JSON.stringify({ content, stream: true, attachments }),
|
||||
});
|
||||
if (!r.ok) throw new Error(await r.text());
|
||||
const reader = r.body.getReader();
|
||||
@@ -211,6 +211,8 @@ async function loadSessions(selectId) {
|
||||
|
||||
async function selectSession(id) {
|
||||
state.current = id;
|
||||
state.attachments = [];
|
||||
renderAttachments();
|
||||
const sess = state.sessions.find((s) => s.id === id);
|
||||
$("#session-title").textContent = sess ? sess.title : "Conversazione";
|
||||
$("#model-name").textContent = sess ? sess.model : "";
|
||||
@@ -265,24 +267,83 @@ async function deleteSessionById(id) {
|
||||
}
|
||||
}
|
||||
|
||||
// ---- Attachments (upload -> shared store; routed server-side by kind) -----
|
||||
function attachChip(a, removable) {
|
||||
const chip = el("span", "attach-chip");
|
||||
const icon = a.kind === "image" ? "\u{1F5BC}\u{FE0F}" : a.kind === "binary" ? "\u{1F4E6}" : "\u{1F4C4}";
|
||||
chip.appendChild(el("span", "attach-kind", icon));
|
||||
chip.appendChild(el("span", "attach-name", a.original_name));
|
||||
if (removable) {
|
||||
const x = el("button", null, "\u{00D7}");
|
||||
x.setAttribute("aria-label", t("attach.remove"));
|
||||
x.addEventListener("click", () => {
|
||||
state.attachments = state.attachments.filter((m) => m.id !== a.id);
|
||||
renderAttachments();
|
||||
});
|
||||
chip.appendChild(x);
|
||||
}
|
||||
return chip;
|
||||
}
|
||||
|
||||
function renderAttachments() {
|
||||
const box = $("#attachments");
|
||||
box.textContent = "";
|
||||
box.hidden = state.attachments.length === 0;
|
||||
for (const a of state.attachments) box.appendChild(attachChip(a, true));
|
||||
}
|
||||
|
||||
function readFileAsDataURL(file) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const fr = new FileReader();
|
||||
fr.onload = () => resolve(fr.result);
|
||||
fr.onerror = () => reject(fr.error || new Error("read failed"));
|
||||
fr.readAsDataURL(file);
|
||||
});
|
||||
}
|
||||
|
||||
async function uploadFiles(files) {
|
||||
for (const file of files) {
|
||||
try {
|
||||
const data = await readFileAsDataURL(file);
|
||||
const meta = await sendJSON("/api/uploads", "POST", { name: file.name, data });
|
||||
state.attachments.push(meta);
|
||||
renderAttachments();
|
||||
} catch (e) {
|
||||
const tr = $("#transcript");
|
||||
tr.appendChild(messageNode("assistant", t("attach.error", { msg: e.message })));
|
||||
scrollDown();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function sendMessage() {
|
||||
const input = $("#input");
|
||||
const content = input.value.trim();
|
||||
if (!content || state.busy || !state.current) return;
|
||||
const attachments = state.attachments.slice();
|
||||
if ((!content && !attachments.length) || state.busy || !state.current) return;
|
||||
state.busy = true;
|
||||
$("#send").disabled = true;
|
||||
input.value = "";
|
||||
autoGrow(input);
|
||||
state.attachments = [];
|
||||
renderAttachments();
|
||||
|
||||
const t = $("#transcript");
|
||||
const empty = t.querySelector(".empty");
|
||||
if (empty) empty.remove();
|
||||
t.appendChild(messageNode("user", content));
|
||||
const userNode = messageNode("user", content);
|
||||
if (attachments.length) {
|
||||
const row = el("div", "msg-attachments");
|
||||
for (const a of attachments) row.appendChild(attachChip(a, false));
|
||||
userNode.querySelector(".bubble").appendChild(row);
|
||||
}
|
||||
t.appendChild(userNode);
|
||||
scrollDown();
|
||||
|
||||
const ids = attachments.map((a) => a.id);
|
||||
try {
|
||||
if (state.agent) await agentTurn(content, t);
|
||||
else await chatTurn(content, t);
|
||||
if (state.agent) await agentTurn(content, t, ids);
|
||||
else await chatTurn(content, t, ids);
|
||||
state.sessions = await getJSON("/api/sessions");
|
||||
renderSessions();
|
||||
} catch (e) {
|
||||
@@ -296,7 +357,7 @@ async function sendMessage() {
|
||||
}
|
||||
|
||||
// Plain chat: stream OpenAI chunks into a live assistant bubble.
|
||||
async function chatTurn(content, t) {
|
||||
async function chatTurn(content, t, attachments) {
|
||||
const wrap = el("div", "msg assistant");
|
||||
wrap.appendChild(el("div", "role", "Assistente"));
|
||||
const bubble = el("div", "bubble");
|
||||
@@ -313,7 +374,7 @@ async function chatTurn(content, t) {
|
||||
scrollDown();
|
||||
|
||||
let reasoning = "", text = "", usage = null;
|
||||
await streamChat(state.current, content, (chunk) => {
|
||||
await streamChat(state.current, content, attachments, (chunk) => {
|
||||
const d = chunk.choices && chunk.choices[0] && chunk.choices[0].delta;
|
||||
if (d && d.reasoning_content) {
|
||||
reasoning += d.reasoning_content;
|
||||
@@ -331,11 +392,11 @@ async function chatTurn(content, t) {
|
||||
}
|
||||
|
||||
// Agent mode: stream tool_call / tool_result / final events (observability, no gate).
|
||||
async function agentTurn(content, t) {
|
||||
async function agentTurn(content, t, attachments) {
|
||||
const r = await fetch(`/api/sessions/${state.current}/chat`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ content, agent: true }),
|
||||
body: JSON.stringify({ content, agent: true, attachments }),
|
||||
});
|
||||
if (!r.ok) throw new Error(await r.text());
|
||||
const reader = r.body.getReader();
|
||||
@@ -575,6 +636,14 @@ function wire() {
|
||||
$("#new-session").addEventListener("click", newSession);
|
||||
$("#delete-session").addEventListener("click", deleteSession);
|
||||
$("#send").addEventListener("click", sendMessage);
|
||||
$("#attach").title = t("attach.title");
|
||||
$("#attach").setAttribute("aria-label", t("attach.title"));
|
||||
$("#attach").addEventListener("click", () => $("#file-input").click());
|
||||
$("#file-input").addEventListener("change", (e) => {
|
||||
const files = Array.from(e.target.files || []);
|
||||
e.target.value = "";
|
||||
if (files.length) uploadFiles(files);
|
||||
});
|
||||
$("#agent-toggle").addEventListener("click", () => {
|
||||
state.agent = !state.agent;
|
||||
const b = $("#agent-toggle");
|
||||
|
||||
@@ -17,5 +17,8 @@
|
||||
"settings.hint.general": "General defaults: apply only to future sessions.",
|
||||
"settings.name.locked": "The name is locked after the first interaction.",
|
||||
"settings.name.unlocked": "You can rename until you send the first message.",
|
||||
"settings.error": "Error: {msg}"
|
||||
"settings.error": "Error: {msg}",
|
||||
"attach.title": "Attach files or images",
|
||||
"attach.remove": "Remove attachment",
|
||||
"attach.error": "Upload failed: {msg}"
|
||||
}
|
||||
@@ -17,5 +17,8 @@
|
||||
"settings.hint.general": "Default generali: si applicano solo alle sessioni future.",
|
||||
"settings.name.locked": "Il nome è bloccato dopo la prima interazione.",
|
||||
"settings.name.unlocked": "Puoi rinominare finché non invii il primo messaggio.",
|
||||
"settings.error": "Errore: {msg}"
|
||||
"settings.error": "Errore: {msg}",
|
||||
"attach.title": "Allega file o immagini",
|
||||
"attach.remove": "Rimuovi allegato",
|
||||
"attach.error": "Caricamento fallito: {msg}"
|
||||
}
|
||||
Reference in new issue
Block a user