|
| 1 | +use anyhow::{anyhow, Result}; |
| 2 | +use fluent_core::traits::Engine; |
| 3 | +use fluent_core::types::Request; |
| 4 | +use std::path::Path; |
| 5 | +use std::process::Stdio; |
| 6 | +use tokio::fs; |
| 7 | +use tokio::process::Command; |
| 8 | + |
| 9 | +/// Simple agent that keeps a history of prompt/response pairs. |
| 10 | +pub struct Agent { |
| 11 | + engine: Box<dyn Engine>, |
| 12 | + history: Vec<(String, String)>, |
| 13 | +} |
| 14 | + |
| 15 | +impl Agent { |
| 16 | + /// Create a new agent from an engine. |
| 17 | + pub fn new(engine: Box<dyn Engine>) -> Self { |
| 18 | + Self { engine, history: Vec::new() } |
| 19 | + } |
| 20 | + |
| 21 | + /// Send a prompt to the engine and store the response in history. |
| 22 | + pub async fn send(&mut self, prompt: &str) -> Result<String> { |
| 23 | + let request = Request { flowname: "agent".to_string(), payload: prompt.to_string() }; |
| 24 | + let response = self.engine.execute(&request).await?; |
| 25 | + let content = response.content.clone(); |
| 26 | + self.history.push((prompt.to_string(), content.clone())); |
| 27 | + Ok(content) |
| 28 | + } |
| 29 | + |
| 30 | + /// Read a file asynchronously. |
| 31 | + pub async fn read_file(&self, path: &Path) -> Result<String> { |
| 32 | + Ok(fs::read_to_string(path).await?) |
| 33 | + } |
| 34 | + |
| 35 | + /// Write a file asynchronously. |
| 36 | + pub async fn write_file(&self, path: &Path, content: &str) -> Result<()> { |
| 37 | + fs::write(path, content).await.map_err(Into::into) |
| 38 | + } |
| 39 | + |
| 40 | + /// Run a shell command and capture stdout and stderr. |
| 41 | + pub async fn run_command(&self, cmd: &str, args: &[&str]) -> Result<String> { |
| 42 | + let output = Command::new(cmd) |
| 43 | + .args(args) |
| 44 | + .stdout(Stdio::piped()) |
| 45 | + .stderr(Stdio::piped()) |
| 46 | + .output() |
| 47 | + .await?; |
| 48 | + let mut result = String::from_utf8_lossy(&output.stdout).to_string(); |
| 49 | + if !output.status.success() { |
| 50 | + result.push_str(&String::from_utf8_lossy(&output.stderr)); |
| 51 | + } |
| 52 | + Ok(result) |
| 53 | + } |
| 54 | + |
| 55 | + /// Commit changes in the current git repository. |
| 56 | + pub async fn git_commit(&self, message: &str) -> Result<()> { |
| 57 | + self.run_command("git", &["add", "."]).await?; |
| 58 | + let status = Command::new("git") |
| 59 | + .args(["commit", "-m", message]) |
| 60 | + .status() |
| 61 | + .await?; |
| 62 | + if !status.success() { |
| 63 | + return Err(anyhow!("git commit failed")); |
| 64 | + } |
| 65 | + Ok(()) |
| 66 | + } |
| 67 | + |
| 68 | + /// Run a simple plan -> generate -> test -> commit cycle using the engine. |
| 69 | + pub async fn run_cycle(&mut self, prompt: &str) -> Result<()> { |
| 70 | + let plan = self.send(&format!("Plan: {}", prompt)).await?; |
| 71 | + let _generation = self.send(&format!("Generate code based on plan:\n{}", plan)).await?; |
| 72 | + |
| 73 | + let test_output = self.run_command("cargo", &["test", "--quiet"]).await?; |
| 74 | + if !test_output.contains("0 failed") { |
| 75 | + return Err(anyhow!("tests failed")); |
| 76 | + } |
| 77 | + |
| 78 | + self.git_commit(prompt).await?; |
| 79 | + Ok(()) |
| 80 | + } |
| 81 | +} |
| 82 | + |
0 commit comments