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()] }