mirror of
https://github.com/openai/codex.git
synced 2026-04-26 15:45:02 +00:00
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.
38 lines
1.2 KiB
Rust
38 lines
1.2 KiB
Rust
use codex_common::summarize_sandbox_policy;
|
|
use codex_core::WireApi;
|
|
use codex_core::config::Config;
|
|
use codex_core::model_supports_reasoning_summaries;
|
|
use codex_core::protocol::Event;
|
|
|
|
pub(crate) trait EventProcessor {
|
|
/// Print summary of effective configuration and user prompt.
|
|
fn print_config_summary(&mut self, config: &Config, prompt: &str);
|
|
|
|
/// Handle a single event emitted by the agent.
|
|
fn process_event(&mut self, event: Event);
|
|
}
|
|
|
|
pub(crate) fn create_config_summary_entries(config: &Config) -> Vec<(&'static str, String)> {
|
|
let mut entries = vec![
|
|
("workdir", config.cwd.display().to_string()),
|
|
("model", config.model.clone()),
|
|
("provider", config.model_provider_id.clone()),
|
|
("approval", format!("{:?}", config.approval_policy)),
|
|
("sandbox", summarize_sandbox_policy(&config.sandbox_policy)),
|
|
];
|
|
if config.model_provider.wire_api == WireApi::Responses
|
|
&& model_supports_reasoning_summaries(config)
|
|
{
|
|
entries.push((
|
|
"reasoning effort",
|
|
config.model_reasoning_effort.to_string(),
|
|
));
|
|
entries.push((
|
|
"reasoning summaries",
|
|
config.model_reasoning_summary.to_string(),
|
|
));
|
|
}
|
|
|
|
entries
|
|
}
|