Integration: Switch to real opencode CLI calls for agent 'think' phase
This commit is contained in:
@@ -11,6 +11,7 @@ use chrs_discovery::{SwarmManager, BusHandle, BusMessage};
|
||||
use chrs_election::ElectionManager;
|
||||
use chrono::{Utc, DateTime};
|
||||
use std::path::Path;
|
||||
use std::process::Command;
|
||||
use std::time::Duration;
|
||||
use tokio::time::sleep;
|
||||
use tokio::sync::mpsc;
|
||||
@@ -122,25 +123,48 @@ impl CHORUSAgent {
|
||||
});
|
||||
let _ = self.graph.insert_node("api_call_log", log_entry);
|
||||
|
||||
// 3. Perform the actual API call (simulated opencode call for now)
|
||||
// If the command returned an exit code indicating rate limit (e.g. 429)
|
||||
let success = true;
|
||||
// 3. Perform the actual API call to opencode
|
||||
let output = Command::new("opencode")
|
||||
.args(&[
|
||||
"run",
|
||||
"-m", "resetdata/openai/gpt-oss-120b",
|
||||
&format!("{} \n\n Task: {}", self.system_prompt, message)
|
||||
])
|
||||
.output();
|
||||
|
||||
if success {
|
||||
let _ = self.graph.commit(&format!("API Call Success: {}", call_id));
|
||||
return format!("Reasoning based on: {}", self.system_prompt);
|
||||
} else {
|
||||
attempts += 1;
|
||||
if attempts >= max_attempts {
|
||||
return "Error: Maximum thinking attempts reached.".into();
|
||||
match output {
|
||||
Ok(out) if out.status.success() => {
|
||||
let reasoning = String::from_utf8_lossy(&out.stdout).trim().to_string();
|
||||
let _ = self.graph.insert_node("api_call_log", serde_json::json!({
|
||||
"id": Uuid::new_v4().to_string(),
|
||||
"agent_id": self.id,
|
||||
"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
|
||||
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);
|
||||
|
||||
println!("[AGENT {}] Rate limit hit. Backing off for {:?}...", self.id, delay);
|
||||
sleep(delay).await;
|
||||
// 4. Jittered Exponential Backoff
|
||||
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);
|
||||
|
||||
println!("[AGENT {}] Backing off for {:?}...", self.id, delay);
|
||||
sleep(delay).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user