From 7175543e00caa9ce35487fa7f8b1b479c1ba00a2 Mon Sep 17 00:00:00 2001 From: salmonik Date: Sun, 15 Nov 2026 02:54:21 +0100 Subject: M0: streaming provider, SSE decoder, mock provider, i686 build gate --- src/agent.rs | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 src/agent.rs (limited to 'src/agent.rs') diff --git a/src/agent.rs b/src/agent.rs new file mode 100644 index 0000000..2d84566 --- /dev/null +++ b/src/agent.rs @@ -0,0 +1,67 @@ +use std::sync::atomic::AtomicBool; + +use serde_json::{json, Value}; + +use crate::config::Config; +use crate::provider::{self, Error, Outcome, Sink}; + +/// Frozen for the life of the process. Anything session-varying added here +/// invalidates the provider's prompt cache, so keep it boring. +pub fn system_message() -> Value { + json!({ + "role": "system", + "content": "You are agent686, a coding agent working in a Unix terminal. \ + You edit files and run commands. Be terse and do not explain \ + what you are about to do before doing it." + }) +} + +/// Appends the user message, runs one provider round trip, and appends the +/// assistant message to the history whether or not it asked for tools. +pub fn run_turn( + cfg: &Config, + history: &mut Vec, + prompt: &str, + cancel: &AtomicBool, + sink: &mut dyn Sink, +) -> Result { + history.push(json!({ "role": "user", "content": prompt })); + + let outcome = provider::chat(cfg, history, cancel, sink)?; + + let mut message = serde_json::Map::new(); + message.insert("role".into(), json!("assistant")); + message.insert( + "content".into(), + if outcome.content.is_empty() { + Value::Null + } else { + json!(outcome.content) + }, + ); + if !outcome.tool_calls.is_empty() { + let calls: Vec = outcome + .tool_calls + .iter() + .map(|call| { + json!({ + "id": call.id, + "type": "function", + "function": { + "name": call.name, + // Byte-for-byte what the model sent. + "arguments": call.arguments, + } + }) + }) + .collect(); + message.insert("tool_calls".into(), Value::Array(calls)); + } + history.push(Value::Object(message)); + + Ok(outcome) +} + +pub fn new_history() -> Vec { + vec![system_message()] +} -- cgit v1.2.3