LemonCliRunners.CodexRunner (lemon_cli_runners v0.1.0)

View Source

Codex CLI subprocess runner.

This module wraps the codex CLI tool, spawning it as a subprocess and streaming its JSONL events. It enables using Codex as a subagent with full session persistence and resumption support.

Usage

# Start a new Codex session
{:ok, pid} = CodexRunner.start_link(
  prompt: "Create a new Elixir module that...",
  cwd: "/path/to/project"
)

# Get the event stream
stream = CodexRunner.stream(pid)

# Process events
for event <- LemonAgent.EventStream.events(stream) do
  case event do
    {:cli_event, %StartedEvent{resume: token}} ->
      IO.puts("Session started: #{token.value}")

    {:cli_event, %ActionEvent{action: action, phase: :completed}} ->
      IO.puts("Completed: #{action.title}")

    {:cli_event, %CompletedEvent{ok: true, answer: answer}} ->
      IO.puts("Done: #{answer}")

    _ ->
      :ok
  end
end

Resuming Sessions

# Resume a previous session
token = %ResumeToken{engine: "codex", value: "thread_abc123"}

{:ok, pid} = CodexRunner.start_link(
  prompt: "Continue with the implementation",
  resume: token,
  cwd: "/path/to/project"
)

Configuration

The runner uses the following command by default:

codex exec --json --skip-git-repo-check --color=never

For resuming:

codex exec --json --skip-git-repo-check --color=never resume <session_id> -

The prompt is sent via stdin.

Summary

Functions

Cancel a running runner

Run synchronously and return the final result

Start the runner

Get the event stream from a running runner

Functions

cancel(pid, reason \\ :user_requested)

Cancel a running runner

run(opts)

Run synchronously and return the final result

start_link(opts)

Start the runner

stream(pid)

Get the event stream from a running runner