codex

package
v1.0.0-alpha.32 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 24, 2026 License: Apache-2.0 Imports: 25 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package codex reads `codex exec --json` runs.

The stream reports usage once, at turn.completed, as a running total for the thread, and one prompt is one turn. So the budget is watched in the rollout file Codex writes during the turn, which also gives per-request rows. The stream stays as the check on the file and as the record when it cannot be read.

Index

Constants

View Source
const SDKName = "codex-sdk"

SDKName is the costroid/agent tag of a program that runs Codex through the Codex SDK.

View Source
const (

	// UnknownModel stands in when neither the command nor the config names
	// a model. It prices at the catalog's fallback tier.
	UnknownModel = "unknown"
)

Variables

This section is empty.

Functions

func ReadHomeAnswer

func ReadHomeAnswer(home string, since time.Time) (string, error)

ReadHomeAnswer returns the answer a Codex run that already ended left in its Codex home: the last agent message of the latest completed turn, from the rollout files of threads no other thread started. A subagent's answer went to the thread that started it, not to the run. A file last written before since belongs to an earlier job and is not read; a zero since reads every file.

func ReadStreamAnswer

func ReadStreamAnswer(stream io.Reader) (string, error)

ReadStreamAnswer returns the answer a codex exec --json run ended with, as codex exec keeps its final message: the last agent message completed in a turn that completed. A turn that failed leaves no answer, and nothing else the stream carries is kept.

Types

type Agent

type Agent struct{}

Agent is the Codex CLI.

func (Agent) GatewayArgs

func (Agent) GatewayArgs(base string) []string

GatewayArgs points Codex's built-in OpenAI provider at the gateway's OpenAI routes, and selects that provider, so a provider set in a configuration file cannot send requests around the gateway. Codex reads its base URL from configuration alone, and a --config value overrides every configuration file.

func (Agent) GatewayBypass

func (Agent) GatewayBypass(argv []string) (string, error)

GatewayBypass names the first flag of the command that would send Codex's requests somewhere other than the gateway: a local provider, or a --config value for a key that chooses the provider or its base URL. It reads the command as Codex does, up to a "--" that ends the options.

func (Agent) GatewayEnv

func (Agent) GatewayEnv(_, key string) []string

GatewayEnv gives Codex the run's key as its API key. An OpenAI key beside it would let a request skip the gateway, so it is removed.

func (Agent) Name

func (Agent) Name() string

Name returns the costroid/agent tag.

func (Agent) NewSession

func (Agent) NewSession(plan adapter.Plan) adapter.Session

NewSession starts reading a run of the planned command.

func (Agent) Plan

func (Agent) Plan(argv []string, _ decimal.Decimal) (adapter.Plan, error)

Plan adds --json. Codex has no budget flag, so the wrapper watches the rollout file, and a run that writes none has nothing holding it to the budget.

func (Agent) ReadHome

func (Agent) ReadHome(home string, since time.Time) (adapter.Report, error)

ReadHome reads a Codex run that already ended from the Codex home it wrote to: every rollout file under sessions/, the run's own threads and their subagents alike, each read as the wrapper reads a rollout file. The home must be one made for the job, as codex-action's codex-home input makes one. A file last written before since belongs to an earlier job and is not read; a file started before since and written after it holds an earlier job's spend, so the reading is refused. A zero since reads every file.

Costroid did not start this run, so no budget held it: the grade is none. The files do not say how the run billed, but openai/codex-action runs Codex only on an API key, its openai-api-key input, so the rows read measured.

type SDKAgent

type SDKAgent struct {
	// Dir is the run's working directory. Empty means the process's own.
	Dir string
}

SDKAgent is a program that runs Codex through the Codex SDK. The program's stdout is its own, so the run reads none of it. Every thread whose rollout file appears under CODEX_HOME while the program runs is the run's, with the subagent threads it starts: in CI a thread in any working directory, outside CI one whose working directory is the run's. The run watches those files as it watches codex exec's, and stops the program at the cap. It cannot point Codex at a gateway: Codex reads its base URL from its configuration alone, and no command line reaches the Codex the program starts.

func (SDKAgent) Name

func (SDKAgent) Name() string

Name returns the costroid/agent tag.

func (SDKAgent) NewSession

func (a SDKAgent) NewSession(plan adapter.Plan) adapter.Session

NewSession starts reading a run. It notes every rollout file the Codex home already holds, so a thread the program starts is told from an earlier one.

func (SDKAgent) Plan

func (SDKAgent) Plan(argv []string, _ decimal.Decimal) (adapter.Plan, error)

Plan runs the program as it is, watched through the rollout files.

type SDKSession

type SDKSession struct {
	// contains filtered or unexported fields
}

SDKSession reads the Codex threads of one program's run. Tail and Finish run on separate goroutines, so the lock covers everything below it.

func (*SDKSession) Feed

func (*SDKSession) Feed([]byte, time.Time) error

Feed passes over a line of the program's stdout. The program prints what it chooses, so nothing in it is read as usage.

func (*SDKSession) Finish

func (s *SDKSession) Finish() (adapter.Report, error)

Finish returns the run's usage. Each thread the program started reports under its own id, with the subagents it started, and the first names the run. A rollout file that failed a check refuses the run, because no other record of its spend exists.

func (*SDKSession) Tail

func (s *SDKSession) Tail(ctx context.Context, settled func([]adapter.Request))

Tail follows every rollout file of the run until ctx ends, then reads them once more for the lines Codex wrote last. It calls settled each time a new request settles.

type Session

type Session struct {
	// contains filtered or unexported fields
}

Session reads one Codex run. Feed and Tail run on separate goroutines, so the lock covers everything below it.

func (*Session) BilledThroughGateway

func (s *Session) BilledThroughGateway()

BilledThroughGateway reads the run as key-billed: Codex starts on the run's key, which the gateway bills per token upstream, whatever costroid's own environment holds.

func (*Session) Feed

func (s *Session) Feed(line []byte, at time.Time) error

Feed reads one stdout line. Its type is read first, then the fields that type carries, so a field this reader never uses, such as message, can take any shape. A line whose shape this reader does not know is skipped and counted. A turn.completed line that cannot be read stops the run's reading at Finish, because it holds the run's figures.

func (*Session) Finish

func (s *Session) Finish() (adapter.Report, error)

Finish returns the run's usage and the grade it earned. Per-request rows from the rollout files stand unless the stream's total contradicts them. A run with neither source reported nothing, and whatever it spent is unknown.

func (*Session) RunWindow

func (s *Session) RunWindow(start, _ time.Time)

RunWindow records when the agent's process ran.

func (*Session) Tail

func (s *Session) Tail(ctx context.Context, settled func([]adapter.Request))

Tail follows the thread's rollout file until ctx ends, then reads it once more for the lines the agent wrote last.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL