use async_trait::async_trait;
use std::time::Duration;
use tokio::process::Command;
use super::base::{CliExecutor, ExecutorResponse};
use crate::types::config::ExecutorConfig;
use crate::types::requests::EvaluationRequest;
use crate::types::responses::{ModelVote, Vote};
use crate::{TetradError, TetradResult};
pub struct CodexExecutor {
command_name: String,
args: Vec<String>,
timeout: Duration,
}
impl CodexExecutor {
pub fn new() -> Self {
Self {
command_name: "codex".to_string(),
args: vec!["exec".to_string(), "--json".to_string()],
timeout: Duration::from_secs(60),
}
}
pub fn from_config(config: &ExecutorConfig) -> Self {
Self {
command_name: config.command.clone(),
args: config.args.clone(),
timeout: Duration::from_secs(config.timeout_secs),
}
}
#[must_use]
pub fn with_timeout(mut self, timeout: Duration) -> Self {
self.timeout = timeout;
self
}
fn parse_codex_events(output: &str) -> Option<String> {
for line in output.lines() {
let line = line.trim();
if line.is_empty() {
continue;
}
if let Ok(event) = serde_json::from_str::<serde_json::Value>(line) {
if event.get("type").and_then(|t| t.as_str()) == Some("item.completed") {
if let Some(item) = event.get("item") {
if item.get("type").and_then(|t| t.as_str()) == Some("agent_message") {
if let Some(text) = item.get("text").and_then(|t| t.as_str()) {
return Some(text.to_string());
}
}
}
}
}
}
None
}
fn analyze_text_response(text: &str) -> ExecutorResponse {
let lower = text.to_lowercase();
let vote = if lower.contains("erro crítico")
|| lower.contains("bug grave")
|| lower.contains("vulnerabilidade")
|| lower.contains("falha de segurança")
{
"FAIL"
} else if lower.contains("problema")
|| lower.contains("issue")
|| lower.contains("considere")
|| lower.contains("sugestão")
|| lower.contains("atenção")
|| lower.contains("melhoria")
|| lower.contains("overflow")
{
"WARN"
} else {
"PASS"
};
let score = if vote == "PASS" {
if lower.contains("perfeito") || lower.contains("excelente") {
95
} else if lower.contains("bom") || lower.contains("correto") {
85
} else {
80
}
} else if vote == "WARN" {
if lower.contains("menor") || lower.contains("minor") {
70
} else {
60
}
} else {
35
};
let issues: Vec<String> = text
.lines()
.filter(|line| {
let trimmed = line.trim();
trimmed.starts_with("- ") || trimmed.starts_with("* ")
})
.map(|line| {
line.trim()
.trim_start_matches("- ")
.trim_start_matches("* ")
.to_string()
})
.take(5)
.collect();
ExecutorResponse {
vote: vote.to_string(),
score,
reasoning: text.chars().take(500).collect(),
issues,
suggestions: vec![],
}
}
}
impl Default for CodexExecutor {
fn default() -> Self {
Self::new()
}
}
#[async_trait]
impl CliExecutor for CodexExecutor {
fn name(&self) -> &str {
"Codex"
}
fn command(&self) -> &str {
&self.command_name
}
fn specialization(&self) -> &str {
"syntax"
}
async fn evaluate(&self, request: &EvaluationRequest) -> TetradResult<ModelVote> {
let prompt = self.build_prompt(request);
let mut cmd = Command::new(&self.command_name);
for arg in &self.args {
cmd.arg(arg);
}
cmd.arg(&prompt);
let result = tokio::time::timeout(self.timeout, cmd.output()).await;
match result {
Ok(Ok(output)) => {
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
if !stdout.is_empty() {
if let Some(agent_message) = Self::parse_codex_events(&stdout) {
if let Ok(response) =
ExecutorResponse::parse_from_output(&agent_message, self.name())
{
return Ok(response.into_vote(self.name()));
}
let response = Self::analyze_text_response(&agent_message);
return Ok(response.into_vote(self.name()));
}
}
if !stderr.is_empty() && stderr.contains("Error") {
return Err(TetradError::ExecutorFailed(
self.name().to_string(),
stderr.to_string(),
));
}
if let Ok(response) = ExecutorResponse::parse_from_output(&stdout, self.name()) {
return Ok(response.into_vote(self.name()));
}
Err(TetradError::ExecutorFailed(
self.name().to_string(),
"Não foi possível parsear resposta do Codex".to_string(),
))
}
Ok(Err(e)) => {
if e.kind() == std::io::ErrorKind::NotFound {
Ok(ModelVote::new(self.name(), Vote::Warn, 50)
.with_reasoning("Codex CLI não disponível"))
} else {
Err(TetradError::ExecutorFailed(
self.name().to_string(),
e.to_string(),
))
}
}
Err(_) => Err(TetradError::ExecutorTimeout(self.name().to_string())),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_response_valid() {
let output = r#"
Some text before
{"vote": "PASS", "score": 85, "reasoning": "Good code", "issues": [], "suggestions": []}
Some text after
"#;
let response = ExecutorResponse::parse_from_output(output, "Codex");
assert!(response.is_ok());
let response = response.unwrap();
assert_eq!(response.vote, "PASS");
assert_eq!(response.score, 85);
}
#[test]
fn test_parse_response_invalid() {
let output = "No JSON here";
let response = ExecutorResponse::parse_from_output(output, "Codex");
assert!(response.is_err());
}
#[test]
fn test_specialization() {
let executor = CodexExecutor::new();
assert_eq!(executor.specialization(), "syntax");
}
#[test]
fn test_parse_codex_events() {
let output = r#"{"type":"thread.started","thread_id":"test-123"}
{"type":"turn.started"}
{"type":"item.completed","item":{"id":"item_0","type":"reasoning","text":"Thinking..."}}
{"type":"item.completed","item":{"id":"item_1","type":"agent_message","text":"Código aprovado sem problemas."}}
{"type":"turn.completed","usage":{"input_tokens":100,"output_tokens":50}}"#;
let message = CodexExecutor::parse_codex_events(output);
assert!(message.is_some());
assert_eq!(message.unwrap(), "Código aprovado sem problemas.");
}
#[test]
fn test_parse_codex_events_no_agent_message() {
let output = r#"{"type":"thread.started","thread_id":"test-123"}
{"type":"turn.started"}
{"type":"item.completed","item":{"id":"item_0","type":"reasoning","text":"Thinking..."}}"#;
let message = CodexExecutor::parse_codex_events(output);
assert!(message.is_none());
}
#[test]
fn test_analyze_text_response_pass() {
let text = "O código está correto e bem estruturado. Bom trabalho!";
let response = CodexExecutor::analyze_text_response(text);
assert_eq!(response.vote, "PASS");
assert!(response.score >= 80);
}
#[test]
fn test_analyze_text_response_warn() {
let text = "O código funciona, mas considere adicionar tratamento de overflow.";
let response = CodexExecutor::analyze_text_response(text);
assert_eq!(response.vote, "WARN");
assert!(response.score >= 50 && response.score < 80);
}
#[test]
fn test_analyze_text_response_fail() {
let text = "Erro crítico: vulnerabilidade de segurança detectada.";
let response = CodexExecutor::analyze_text_response(text);
assert_eq!(response.vote, "FAIL");
assert!(response.score < 50);
}
#[test]
fn test_default_args() {
let executor = CodexExecutor::new();
assert_eq!(executor.args, vec!["exec", "--json"]);
}
}