Codex.CLI.Session (Codex SDK v0.14.0)

Copy Markdown View Source

Raw codex CLI subprocess session.

Sessions are useful for interactive or long-running commands such as:

  • codex
  • codex resume
  • codex fork
  • codex app-server
  • codex mcp-server

The caller process receives raw erlexec messages for the spawned process:

  • {:stdout, os_pid, binary}
  • {:stderr, os_pid, binary}
  • {:DOWN, os_pid, :process, pid, reason}

Use collect/2 to accumulate output until the process exits.

Summary

Functions

Closes the subprocess stdin by sending EOF.

Collects stdout/stderr until the subprocess exits.

Interrupts the subprocess.

Sends input bytes to the subprocess stdin.

Starts a raw subprocess session for binary_path and args.

Stops the subprocess.

Types

result()

@type result() :: %{
  command: [String.t()],
  args: [String.t()],
  stdout: String.t(),
  stderr: String.t(),
  exit_code: integer(),
  success: boolean()
}

t()

@type t() :: %Codex.CLI.Session{
  args: [String.t()],
  command: [String.t()],
  os_pid: non_neg_integer(),
  pid: pid(),
  pty?: boolean(),
  receiver: pid(),
  stdin?: boolean()
}

Functions

close_input(session)

@spec close_input(t()) :: :ok | {:error, term()}

Closes the subprocess stdin by sending EOF.

collect(session, timeout_ms \\ Defaults.exec_timeout_ms())

@spec collect(t(), timeout()) :: {:ok, result()} | {:error, term()}

Collects stdout/stderr until the subprocess exits.

interrupt(session)

@spec interrupt(t()) :: :ok | {:error, term()}

Interrupts the subprocess.

send_input(session, data)

@spec send_input(t(), iodata()) :: :ok | {:error, term()}

Sends input bytes to the subprocess stdin.

start(binary_path, args, opts \\ [])

@spec start(String.t(), [String.t()], keyword()) :: {:ok, t()} | {:error, term()}

Starts a raw subprocess session for binary_path and args.

stop(session)

@spec stop(t()) :: :ok | {:error, term()}

Stops the subprocess.