Overview
TheCodexExecAdapter captures events from the codex exec --json command by spawning a child process and parsing JSONL output.
Class: CodexExecAdapter
Constructor
Ingest pipeline for event processing
Methods
runAndCapture
Spawnscodex exec and captures all events from stdout.
Run configuration options
User prompt to send to agent
Working directory for the agent
Repository ID (auto-generated from cwd if not provided)
Model name to use
Thread ID to resume existing conversation
Callback invoked for each captured event
UUID for the capture session
Codex thread ID extracted from events
Final agent message text
Supporting Functions
parseExecJsonLine
Parses a single JSONL line from codex exec output.Raw JSONL line
buildArgs
Builds command-line arguments for codex exec.Run options
Implementation Details
Command Execution
The adapter spawnscodex as a child process:
Event Flow
- Prompt event: Emits
prompt.submittedimmediately - Process spawn: Starts
codex exec --json - JSONL streaming: Reads stdout line-by-line
- Event parsing: Parses each line and ingests into pipeline
- Error collection: Captures stderr for error reporting
- Exit handling: Waits for process exit and checks exit code
Error Handling
The adapter handles multiple error scenarios:- JSON parse errors: Emits
errorevent with raw line - Non-zero exit: Emits
errorevent with stderr output - Process errors: Propagates as exceptions
Usage Example
Comparison with CodexSdkAdapter
| Feature | CodexSdkAdapter | CodexExecAdapter |
|---|---|---|
| Mechanism | SDK streaming API | Process spawn + JSONL parsing |
| Performance | Lower overhead | Spawns subprocess |
| Dependencies | Requires @openai/codex-sdk | Uses system codex CLI |
| Error details | SDK exceptions | stderr capture |
| Best for | Programmatic integration | CLI-based workflows |
Troubleshooting
codex not found
Ensurecodex is in your PATH:
Parse errors
If you encounter JSON parse errors, check that you’re using a compatible Codex CLI version that supports--json output.