diff options
| author | salmonik <salmonik@tuta.io> | 2026-11-15 02:54:21 +0100 |
|---|---|---|
| committer | salmonik <salmonik@tuta.io> | 2026-11-15 02:54:21 +0100 |
| commit | 7175543e00caa9ce35487fa7f8b1b479c1ba00a2 (patch) | |
| tree | 10f89f85ca35d6ac7515bbe88518255788d6dc22 /src/agent.rs | |
| download | agent686-7175543e00caa9ce35487fa7f8b1b479c1ba00a2.tar.gz agent686-7175543e00caa9ce35487fa7f8b1b479c1ba00a2.zip | |
M0: streaming provider, SSE decoder, mock provider, i686 build gate
Diffstat (limited to 'src/agent.rs')
| -rw-r--r-- | src/agent.rs | 67 |
1 files changed, 67 insertions, 0 deletions
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 @@ | |||
| 1 | use std::sync::atomic::AtomicBool; | ||
| 2 | |||
| 3 | use serde_json::{json, Value}; | ||
| 4 | |||
| 5 | use crate::config::Config; | ||
| 6 | use crate::provider::{self, Error, Outcome, Sink}; | ||
| 7 | |||
| 8 | /// Frozen for the life of the process. Anything session-varying added here | ||
| 9 | /// invalidates the provider's prompt cache, so keep it boring. | ||
| 10 | pub fn system_message() -> Value { | ||
| 11 | json!({ | ||
| 12 | "role": "system", | ||
| 13 | "content": "You are agent686, a coding agent working in a Unix terminal. \ | ||
| 14 | You edit files and run commands. Be terse and do not explain \ | ||
| 15 | what you are about to do before doing it." | ||
| 16 | }) | ||
| 17 | } | ||
| 18 | |||
| 19 | /// Appends the user message, runs one provider round trip, and appends the | ||
| 20 | /// assistant message to the history whether or not it asked for tools. | ||
| 21 | pub fn run_turn( | ||
| 22 | cfg: &Config, | ||
| 23 | history: &mut Vec<Value>, | ||
| 24 | prompt: &str, | ||
| 25 | cancel: &AtomicBool, | ||
| 26 | sink: &mut dyn Sink, | ||
| 27 | ) -> Result<Outcome, Error> { | ||
| 28 | history.push(json!({ "role": "user", "content": prompt })); | ||
| 29 | |||
| 30 | let outcome = provider::chat(cfg, history, cancel, sink)?; | ||
| 31 | |||
| 32 | let mut message = serde_json::Map::new(); | ||
| 33 | message.insert("role".into(), json!("assistant")); | ||
| 34 | message.insert( | ||
| 35 | "content".into(), | ||
| 36 | if outcome.content.is_empty() { | ||
| 37 | Value::Null | ||
| 38 | } else { | ||
| 39 | json!(outcome.content) | ||
| 40 | }, | ||
| 41 | ); | ||
| 42 | if !outcome.tool_calls.is_empty() { | ||
| 43 | let calls: Vec<Value> = outcome | ||
| 44 | .tool_calls | ||
| 45 | .iter() | ||
| 46 | .map(|call| { | ||
| 47 | json!({ | ||
| 48 | "id": call.id, | ||
| 49 | "type": "function", | ||
| 50 | "function": { | ||
| 51 | "name": call.name, | ||
| 52 | // Byte-for-byte what the model sent. | ||
| 53 | "arguments": call.arguments, | ||
| 54 | } | ||
| 55 | }) | ||
| 56 | }) | ||
| 57 | .collect(); | ||
| 58 | message.insert("tool_calls".into(), Value::Array(calls)); | ||
| 59 | } | ||
| 60 | history.push(Value::Object(message)); | ||
| 61 | |||
| 62 | Ok(outcome) | ||
| 63 | } | ||
| 64 | |||
| 65 | pub fn new_history() -> Vec<Value> { | ||
| 66 | vec![system_message()] | ||
| 67 | } | ||
