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

+67 -7
View File
@@ -16,7 +16,10 @@ use futures::StreamExt;
use serde::Deserialize;
use super::routes::{AppState, Error};
use super::types::{ChatChunk, ChatMessage, ChatRequest, FunctionCall, Role, ToolCall, Usage};
use super::types::{
completion_id, now_unix, ChatChunk, ChatMessage, ChatRequest, ChatResponse, Choice,
FinishReason, FunctionCall, Role, ToolCall, Usage,
};
use super::uploads;
use crate::agent::shell::{HostShell, ShellRunner};
use crate::agent::{self, AgentEvent};
@@ -433,22 +436,79 @@ pub(crate) async fn session_chat(
let svc = st.service.clone();
let store2 = store.clone();
let sid = id.clone();
let folder = sess.folder.clone();
let tx_model = tx.clone();
tokio::spawn(async move {
// Streaming model step: emit Delta events while generating (so the GUI
// shows thinking + final answer live), then return the accumulated
// response for the loop's tool detection and persistence.
let model = move |r: ChatRequest| {
let svc = svc.clone();
async move { svc.complete(&r).await }
let tx = tx_model.clone();
async move {
let (mut stream, prompt_len) = svc.stream(&r).await?;
let mut acc = Accum::default();
let mut finish = FinishReason::Stop;
while let Some(chunk) = stream.next().await {
if let Some(ch) = chunk.choices.first() {
let d = &ch.delta;
if d.content.is_some() || d.reasoning_content.is_some() {
let _ = tx.send(AgentEvent::Delta {
content: d.content.clone(),
reasoning: d.reasoning_content.clone(),
});
}
if let Some(f) = &ch.finish_reason {
finish = *f;
}
}
accumulate(&mut acc, &chunk);
}
let tools: Vec<ToolCall> = acc.tools.values().cloned().collect();
let has_tools = !tools.is_empty();
let usage = acc.usage.unwrap_or_else(|| Usage::new(prompt_len, 0));
Ok(ChatResponse {
id: completion_id(),
object: "chat.completion",
created: now_unix(),
model: r.model.clone(),
choices: vec![Choice {
index: 0,
message: ChatMessage {
role: Role::Assistant,
content: (!acc.content.is_empty())
.then_some(serde_json::Value::String(acc.content)),
tool_call_id: None,
tool_calls: has_tools.then_some(tools),
name: None,
reasoning_content: (!acc.reasoning.is_empty())
.then_some(acc.reasoning),
},
finish_reason: if has_tools {
FinishReason::ToolCalls
} else {
finish
},
}],
usage,
})
}
};
// Base mode: the host shell. Isolated mode will swap in a Docker-MCP
// runner behind the same ShellRunner seam.
// runner behind the same ShellRunner seam. Commands default to the
// session's personal folder (its home base).
let runner: Arc<dyn ShellRunner> = Arc::new(HostShell);
if let Err(env) = agent::run_loop(
model,
runner,
store2,
agent::LoopContext {
runner,
store: store2,
default_cwd: folder,
max_iters: agent::MAX_ITERS,
progress: Some(tx.clone()),
},
&sid,
req,
agent::MAX_ITERS,
Some(tx.clone()),
)
.await
{