feat(agent): tools on by default, personal-folder cwd, streamed final answer

- GUI: agent mode (tools) enabled by default; the 🤖 toggle still turns it off.
- shell_exec defaults to the session's personal folder (~/.strata-ctl/sessions/<name>)
  when the model omits cwd, so the agent's home base matches its identity reminder;
  full host access retained (cd / absolute paths). run_loop gains a LoopContext
  (runner/store/default_cwd/max_iters/progress) to stay within the arg-count lint.
- Agent final answer now streams live: the model step uses service.stream, emits
  AgentEvent::Delta (reasoning + content) while generating, and returns the
  accumulated response for tool detection + persistence. The GUI renders a live
  assistant bubble (thinking auto-opens, folds when the answer starts) instead of
  receiving the whole final answer at once.
- Verified live: 76 delta events over 10.75s, pwd -> sessions/<name>, correct answer.
- 122 tests + 2 ignored; clippy/fmt clean.
This commit is contained in:
adm committed 2026-11-25 17:01:06 +01:00
1 parent ebbd9e72e8
commit 5117ecad7a
5 files changed
+259 -40

No files matched your search

+55 -3
View File
@@ -5,7 +5,7 @@
const $ = (sel) => document.querySelector(sel);
const state = { sessions: [], current: null, busy: false, agent: false, attachments: [] };
const state = { sessions: [], current: null, busy: false, agent: true, attachments: [] };
// ---- i18n: new user-facing strings go through t(key, params) --------------
// Catalogs are served at /assets/locales/<lang>.json; language follows the
@@ -405,6 +405,30 @@ async function agentTurn(content, t, attachments) {
const dec = new TextDecoder();
let buf = "";
const toolNodes = {};
let live = null; // the assistant bubble currently being streamed
function ensureLive() {
if (live) return live;
const wrap = el("div", "msg assistant");
wrap.appendChild(el("div", "role", "Assistente"));
const bubble = el("div", "bubble");
const det = el("details", "thinking");
det.appendChild(el("summary", null, "Pensiero"));
const reasoningNode = el("div", "reasoning", "");
det.appendChild(reasoningNode);
det.hidden = true;
const contentNode = el("div", "content", "");
bubble.appendChild(det);
bubble.appendChild(contentNode);
wrap.appendChild(bubble);
t.appendChild(wrap);
live = { bubble, det, reasoningNode, contentNode, reasoning: "", text: "" };
return live;
}
function closeLive() {
if (live) { live.det.open = false; live = null; }
}
for (;;) {
const { done, value } = await reader.read();
if (done) break;
@@ -419,7 +443,21 @@ async function agentTurn(content, t, attachments) {
if (data === "[DONE]") return;
let ev;
try { ev = JSON.parse(data); } catch { continue; }
if (ev.type === "tool_call") {
if (ev.type === "delta") {
const L = ensureLive();
if (ev.reasoning) {
L.reasoning += ev.reasoning;
L.reasoningNode.textContent = L.reasoning;
L.det.hidden = false;
L.det.open = true;
}
if (ev.content) {
if (!L.text) L.det.open = false;
L.text += ev.content;
L.contentNode.textContent = L.text;
}
} else if (ev.type === "tool_call") {
closeLive();
const n = toolCallNode(ev.name, ev.arguments);
toolNodes[ev.id] = n;
t.appendChild(n);
@@ -427,9 +465,19 @@ async function agentTurn(content, t, attachments) {
const n = toolNodes[ev.id];
if (n) n._out.textContent = ev.output;
} else if (ev.type === "final") {
const L = ensureLive();
const m = ev.response.choices[0].message;
t.appendChild(messageNode("assistant", m.content || "", m.reasoning_content, usageText(ev.response.usage)));
if (!L.text && m.content) { L.text = m.content; L.contentNode.textContent = m.content; }
if (!L.reasoning && m.reasoning_content) {
L.reasoning = m.reasoning_content;
L.reasoningNode.textContent = m.reasoning;
L.det.hidden = false;
}
L.det.open = false;
if (ev.response.usage) L.bubble.appendChild(el("div", "usage", usageText(ev.response.usage)));
live = null;
} else if (ev.type === "error") {
closeLive();
t.appendChild(messageNode("assistant", `Errore agent: ${ev.message}`));
}
scrollDown();
@@ -652,6 +700,10 @@ function wire() {
b.classList.toggle("active", state.agent);
b.setAttribute("aria-pressed", String(state.agent));
});
// Tools are on by default: the framework is meant to act, not just chat.
const ab = $("#agent-toggle");
ab.classList.toggle("active", state.agent);
ab.setAttribute("aria-pressed", String(state.agent));
$("#show-stats").addEventListener("click", showStats);
$("#close-stats").addEventListener("click", () => { $("#stats-modal").hidden = true; });
$("#show-settings").addEventListener("click", () => openSettings("session"));