feat: add --json flag to codex exec (#1603)
This is designed to facilitate programmatic use of Codex in a more lightweight way than using `codex mcp`. Passing `--json` to `codex exec` will print each event as a line of JSON to stdout. Note that it does not print the individual tokens as they are streamed, only full messages, as this is aimed at programmatic use rather than to power UI. <img width="1348" height="1307" alt="image" src="https://github.com/user-attachments/assets/fc7908de-b78d-46e4-a6ff-c85de28415c7" /> I changed the existing `EventProcessor` into a trait and moved the implementation to `EventProcessorWithHumanOutput`. Then I introduced an alternative implementation, `EventProcessorWithJsonOutput`. The `--json` flag determines which implementation to use.
This commit is contained in:
48
codex-rs/exec/src/event_processor_with_json_output.rs
Normal file
48
codex-rs/exec/src/event_processor_with_json_output.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use codex_core::config::Config;
|
||||
use codex_core::protocol::Event;
|
||||
use codex_core::protocol::EventMsg;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::event_processor::EventProcessor;
|
||||
use crate::event_processor::create_config_summary_entries;
|
||||
|
||||
pub(crate) struct EventProcessorWithJsonOutput;
|
||||
|
||||
impl EventProcessorWithJsonOutput {
|
||||
pub fn new() -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
|
||||
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>>();
|
||||
#[allow(clippy::expect_used)]
|
||||
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}");
|
||||
}
|
||||
|
||||
fn process_event(&mut self, event: Event) {
|
||||
match event.msg {
|
||||
EventMsg::AgentMessageDelta(_) | EventMsg::AgentReasoningDelta(_) => {
|
||||
// Suppress streaming events in JSON mode.
|
||||
}
|
||||
_ => {
|
||||
if let Ok(line) = serde_json::to_string(&event) {
|
||||
println!("{line}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user