2025-07-17 15:10:15 -07:00
|
|
|
use std::collections::HashMap;
|
2025-07-23 15:03:26 -07:00
|
|
|
use std::path::PathBuf;
|
2025-07-17 15:10:15 -07:00
|
|
|
|
|
|
|
|
use codex_core::config::Config;
|
|
|
|
|
use codex_core::protocol::Event;
|
|
|
|
|
use codex_core::protocol::EventMsg;
|
2025-07-23 15:03:26 -07:00
|
|
|
use codex_core::protocol::TaskCompleteEvent;
|
2025-07-17 15:10:15 -07:00
|
|
|
use serde_json::json;
|
|
|
|
|
|
2025-07-23 15:03:26 -07:00
|
|
|
use crate::event_processor::CodexStatus;
|
2025-07-17 15:10:15 -07:00
|
|
|
use crate::event_processor::EventProcessor;
|
2025-07-23 15:03:26 -07:00
|
|
|
use crate::event_processor::handle_last_message;
|
2025-08-05 23:57:52 -07:00
|
|
|
use codex_common::create_config_summary_entries;
|
2025-07-17 15:10:15 -07:00
|
|
|
|
2025-07-23 15:03:26 -07:00
|
|
|
pub(crate) struct EventProcessorWithJsonOutput {
|
|
|
|
|
last_message_path: Option<PathBuf>,
|
|
|
|
|
}
|
2025-07-17 15:10:15 -07:00
|
|
|
|
|
|
|
|
impl EventProcessorWithJsonOutput {
|
2025-07-23 15:03:26 -07:00
|
|
|
pub fn new(last_message_path: Option<PathBuf>) -> Self {
|
|
|
|
|
Self { last_message_path }
|
2025-07-17 15:10:15 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl EventProcessor for EventProcessorWithJsonOutput {
|
|
|
|
|
fn print_config_summary(&mut self, config: &Config, prompt: &str) {
|
|
|
|
|
let entries = create_config_summary_entries(config)
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|(key, value)| (key.to_string(), value))
|
|
|
|
|
.collect::<HashMap<String, String>>();
|
2025-08-14 17:59:01 -07:00
|
|
|
#[expect(clippy::expect_used)]
|
2025-07-17 15:10:15 -07:00
|
|
|
let config_json =
|
|
|
|
|
serde_json::to_string(&entries).expect("Failed to serialize config summary to JSON");
|
|
|
|
|
println!("{config_json}");
|
|
|
|
|
|
|
|
|
|
let prompt_json = json!({
|
|
|
|
|
"prompt": prompt,
|
|
|
|
|
});
|
|
|
|
|
println!("{prompt_json}");
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-23 15:03:26 -07:00
|
|
|
fn process_event(&mut self, event: Event) -> CodexStatus {
|
2025-07-17 15:10:15 -07:00
|
|
|
match event.msg {
|
|
|
|
|
EventMsg::AgentMessageDelta(_) | EventMsg::AgentReasoningDelta(_) => {
|
|
|
|
|
// Suppress streaming events in JSON mode.
|
2025-07-23 15:03:26 -07:00
|
|
|
CodexStatus::Running
|
|
|
|
|
}
|
|
|
|
|
EventMsg::TaskComplete(TaskCompleteEvent { last_agent_message }) => {
|
2025-08-04 15:56:32 -07:00
|
|
|
if let Some(output_file) = self.last_message_path.as_deref() {
|
|
|
|
|
handle_last_message(last_agent_message.as_deref(), output_file);
|
|
|
|
|
}
|
2025-07-23 15:03:26 -07:00
|
|
|
CodexStatus::InitiateShutdown
|
2025-07-17 15:10:15 -07:00
|
|
|
}
|
2025-07-23 15:03:26 -07:00
|
|
|
EventMsg::ShutdownComplete => CodexStatus::Shutdown,
|
2025-07-17 15:10:15 -07:00
|
|
|
_ => {
|
|
|
|
|
if let Ok(line) = serde_json::to_string(&event) {
|
|
|
|
|
println!("{line}");
|
|
|
|
|
}
|
2025-07-23 15:03:26 -07:00
|
|
|
CodexStatus::Running
|
2025-07-17 15:10:15 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|