Add GUI agent mode: the model acts via a universal shell_exec tool

- agent:🐚 native shell_exec (bash -lc, cwd, timeout, 64 KiB output cap)
  behind a ShellRunner seam — base/host mode now; an isolated Docker-MCP runner
  plugs in later without touching the loop. OpenAI function declaration included.
- agent::run_loop: bounded model<->tool orchestration, generic over the model
  step so it is unit-testable without the engine. Persists the full trajectory
  (assistant tool-call turns + role:tool results + final answer) and emits
  tool_call / tool_result / final / error progress events.
- api::sessions: POST .../chat with agent:true drives the loop and streams the
  events as SSE; the GUI shows commands + output inline (observability, no gate).
- store: messages gains tool_call_id; an idempotent migration adds the column to
  pre-existing databases (CREATE TABLE IF NOT EXISTS does not alter them).
- web: robot agent toggle, tool-call/output blocks, history rendering of tool
  turns; inline favicon to silence the favicon 404.

Validated LIVE on engine 0.1.67: the model called shell_exec('uname -a'), the
host executed it, and the model reported the output; trajectory persisted; GUI
verified by browser (toggle, tool block, output, final answer). 106 tests +
2 ignored green, clippy/fmt clean. Isolated mode (user-provided Docker container)
is the next step.
This commit is contained in:
adm committed 2026-11-25 02:06:45 +01:00
1 parent e29878bb3d
commit 8979201139
10 files changed
+994 -43

No files matched your search

+45 -2
View File
@@ -35,6 +35,7 @@ pub struct StoredMessage {
pub content: String,
pub reasoning_content: Option<String>,
pub tool_calls_json: Option<String>,
pub tool_call_id: Option<String>,
pub prompt_tokens: u32,
pub completion_tokens: u32,
pub created_at: i64,
@@ -46,6 +47,7 @@ pub struct NewMessage<'a> {
pub content: &'a str,
pub reasoning_content: Option<&'a str>,
pub tool_calls_json: Option<&'a str>,
pub tool_call_id: Option<&'a str>,
pub prompt_tokens: u32,
pub completion_tokens: u32,
}
@@ -110,6 +112,7 @@ CREATE TABLE IF NOT EXISTS messages (
content TEXT NOT NULL,
reasoning_content TEXT,
tool_calls_json TEXT,
tool_call_id TEXT,
prompt_tokens INTEGER NOT NULL DEFAULT 0,
completion_tokens INTEGER NOT NULL DEFAULT 0,
created_at INTEGER NOT NULL
@@ -146,6 +149,7 @@ impl SqliteStore {
conn.pragma_update(None, "synchronous", "NORMAL")?;
conn.pragma_update(None, "foreign_keys", "ON")?;
conn.execute_batch(SCHEMA)?;
migrate(&conn)?;
Ok(Self {
conn: Mutex::new(conn),
})
@@ -156,6 +160,19 @@ impl SqliteStore {
}
}
/// Idempotent schema migrations for databases created by older versions.
/// `CREATE TABLE IF NOT EXISTS` never alters an existing table, so new columns
/// must be added explicitly when missing.
fn migrate(conn: &Connection) -> Result<()> {
let has_tool_call_id: i64 = conn
.prepare("SELECT COUNT(*) FROM pragma_table_info('messages') WHERE name = 'tool_call_id'")?
.query_row([], |r| r.get(0))?;
if has_tool_call_id == 0 {
conn.execute_batch("ALTER TABLE messages ADD COLUMN tool_call_id TEXT;")?;
}
Ok(())
}
fn row_to_session(row: &rusqlite::Row<'_>) -> rusqlite::Result<Session> {
Ok(Session {
id: row.get("id")?,
@@ -233,6 +250,7 @@ impl Store for SqliteStore {
content: row.get("content")?,
reasoning_content: row.get("reasoning_content")?,
tool_calls_json: row.get("tool_calls_json")?,
tool_call_id: row.get("tool_call_id")?,
prompt_tokens: row.get::<_, i64>("prompt_tokens")? as u32,
completion_tokens: row.get::<_, i64>("completion_tokens")? as u32,
created_at: row.get("created_at")?,
@@ -247,14 +265,15 @@ impl Store for SqliteStore {
conn.execute(
"INSERT INTO messages
(session_id, role, content, reasoning_content, tool_calls_json,
prompt_tokens, completion_tokens, created_at)
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)",
tool_call_id, prompt_tokens, completion_tokens, created_at)
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)",
params![
session_id,
msg.role,
msg.content,
msg.reasoning_content,
msg.tool_calls_json,
msg.tool_call_id,
msg.prompt_tokens as i64,
msg.completion_tokens as i64,
now
@@ -271,6 +290,7 @@ impl Store for SqliteStore {
content: msg.content.to_string(),
reasoning_content: msg.reasoning_content.map(str::to_string),
tool_calls_json: msg.tool_calls_json.map(str::to_string),
tool_call_id: msg.tool_call_id.map(str::to_string),
prompt_tokens: msg.prompt_tokens,
completion_tokens: msg.completion_tokens,
created_at: now,
@@ -318,6 +338,7 @@ mod tests {
content: "ciao",
reasoning_content: None,
tool_calls_json: None,
tool_call_id: None,
prompt_tokens: 3,
completion_tokens: 0,
},
@@ -330,6 +351,7 @@ mod tests {
content: "ciao!",
reasoning_content: Some("think"),
tool_calls_json: None,
tool_call_id: None,
prompt_tokens: 3,
completion_tokens: 5,
},
@@ -366,4 +388,25 @@ mod tests {
let b = new_id("sess");
assert_ne!(a, b);
}
#[test]
fn migration_adds_tool_call_id_to_old_schema() {
// Simulate a pre-existing database without the tool_call_id column.
let conn = Connection::open_in_memory().unwrap();
conn.execute_batch(
"CREATE TABLE messages (id INTEGER PRIMARY KEY, session_id TEXT, role TEXT,
content TEXT, reasoning_content TEXT, tool_calls_json TEXT,
prompt_tokens INTEGER, completion_tokens INTEGER, created_at INTEGER);",
)
.unwrap();
migrate(&conn).unwrap();
let n: i64 = conn
.prepare("SELECT COUNT(*) FROM pragma_table_info('messages') WHERE name='tool_call_id'")
.unwrap()
.query_row([], |r| r.get(0))
.unwrap();
assert_eq!(n, 1);
// Idempotent: running again does not error.
migrate(&conn).unwrap();
}
}