Integration: Switch to real opencode CLI calls for agent 'think' phase

This commit is contained in:
anthonyrawlins
2026-03-04 09:59:37 +11:00
parent f6846ec163
commit 3a2c33886d
39 changed files with 40 additions and 16 deletions

View File

@@ -11,6 +11,7 @@ use chrs_discovery::{SwarmManager, BusHandle, BusMessage};
use chrs_election::ElectionManager; use chrs_election::ElectionManager;
use chrono::{Utc, DateTime}; use chrono::{Utc, DateTime};
use std::path::Path; use std::path::Path;
use std::process::Command;
use std::time::Duration; use std::time::Duration;
use tokio::time::sleep; use tokio::time::sleep;
use tokio::sync::mpsc; use tokio::sync::mpsc;
@@ -122,25 +123,48 @@ impl CHORUSAgent {
}); });
let _ = self.graph.insert_node("api_call_log", log_entry); let _ = self.graph.insert_node("api_call_log", log_entry);
// 3. Perform the actual API call (simulated opencode call for now) // 3. Perform the actual API call to opencode
// If the command returned an exit code indicating rate limit (e.g. 429) let output = Command::new("opencode")
let success = true; .args(&[
"run",
"-m", "resetdata/openai/gpt-oss-120b",
&format!("{} \n\n Task: {}", self.system_prompt, message)
])
.output();
if success { match output {
let _ = self.graph.commit(&format!("API Call Success: {}", call_id)); Ok(out) if out.status.success() => {
return format!("Reasoning based on: {}", self.system_prompt); let reasoning = String::from_utf8_lossy(&out.stdout).trim().to_string();
} else { let _ = self.graph.insert_node("api_call_log", serde_json::json!({
attempts += 1; "id": Uuid::new_v4().to_string(),
if attempts >= max_attempts { "agent_id": self.id,
return "Error: Maximum thinking attempts reached.".into(); "called_at": Utc::now().to_rfc3339(),
"status": "success"
}));
let _ = self.graph.commit(&format!("API Call Success: {}", call_id));
return reasoning;
} }
_ => {
attempts += 1;
let error_msg = if let Ok(out) = output {
String::from_utf8_lossy(&out.stderr).to_string()
} else {
"Failed to execute opencode".to_string()
};
println!("[AGENT {}] API Call failed (Attempt {}): {}", self.id, attempts, error_msg);
if attempts >= max_attempts {
return format!("Error: Maximum thinking attempts reached. Last error: {}", error_msg);
}
// 4. Jittered Exponential Backoff // 4. Jittered Exponential Backoff
let jitter = rand::thread_rng().gen_range(0..1000); let jitter = rand::thread_rng().gen_range(0..1000);
let delay = base_delay.mul_f64(2.0_f64.powi(attempts - 1)) + Duration::from_millis(jitter); let delay = base_delay.mul_f64(2.0_f64.powi(attempts - 1)) + Duration::from_millis(jitter);
println!("[AGENT {}] Rate limit hit. Backing off for {:?}...", self.id, delay); println!("[AGENT {}] Backing off for {:?}...", self.id, delay);
sleep(delay).await; sleep(delay).await;
}
} }
} }
} }