codex

package
v0.6.113 Latest Latest
Warning

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

Go to latest
Published: May 5, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package codex drives the codex CLI as a subprocess and parses its JSON-lines output into a plain text reply with optional file-change events.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseFinalMessage

func ParseFinalMessage(jsonlOutput string) (string, error)

Types

type ExecPolicyConfig

type ExecPolicyConfig struct {
	Sandbox        string
	AskForApproval string
	AddDirs        []string
}

ExecPolicyConfig controls codex sandbox and approval settings.

type LoginStatusReport

type LoginStatusReport struct {
	Command   string
	CodexHome string
	LoggedIn  bool
	Output    string
}

LoginStatusReport holds the result of a Codex CLI login status check.

func CheckLogin

func CheckLogin(command, codexHome string, timeout time.Duration) (LoginStatusReport, error)

CheckLogin runs "codex login status" for the given codexHome directory. If codexHome is empty, it falls back to $CODEX_HOME or ~/.codex. If timeout is zero or negative, a 15-second default is used.

type Runner

type Runner struct {
	Command                string
	Timeout                time.Duration
	IdleTimeout            time.Duration
	DefaultIdleTimeout     time.Duration
	HighIdleTimeout        time.Duration
	XHighIdleTimeout       time.Duration
	DefaultModel           string
	DefaultReasoningEffort string
	Env                    map[string]string
	WorkspaceDir           string
}

Runner executes the codex CLI for a single request.

func (Runner) Run

func (r Runner) Run(ctx context.Context, userText string) (string, error)

Run is a convenience wrapper that runs without thread resumption or progress callbacks.

func (Runner) RunWithThreadAndProgress

func (r Runner) RunWithThreadAndProgress(
	ctx context.Context,
	threadID string,
	userText string,
	policy ExecPolicyConfig,
	model string,
	profile string,
	reasoningEffort string,
	personality string,
	env map[string]string,
	onThinking func(step string),
) (string, string, error)

RunWithThreadAndProgress runs the codex CLI and returns the final reply and next thread ID. It is a convenience wrapper around RunWithThreadAndProgressAndUsage.

func (Runner) RunWithThreadAndProgressAndUsage

func (r Runner) RunWithThreadAndProgressAndUsage(
	ctx context.Context,
	threadID string,
	userText string,
	policy ExecPolicyConfig,
	model string,
	profile string,
	reasoningEffort string,
	personality string,
	env map[string]string,
	onThinking func(step string),
	onRawEvent func(kind, line, detail string),
) (string, string, Usage, error)

RunWithThreadAndProgressAndUsage runs the codex CLI and returns the final reply, next thread ID, and token usage.

  • threadID: resume an existing session when non-empty.
  • userText: the fully assembled prompt.
  • policy: sandbox and approval settings.
  • model, profile, reasoningEffort, personality: forwarded as CLI flags.
  • env: merged over the process environment.
  • onThinking: receives intermediate messages and file-change notifications.
  • onRawEvent: optional callback for raw stdout events (kind, line, detail); nil disables raw event delivery.

type Usage

type Usage struct {
	InputTokens       int64
	CachedInputTokens int64
	OutputTokens      int64
}

Usage holds token counts reported by the codex CLI.

func (Usage) HasUsage

func (u Usage) HasUsage() bool

HasUsage reports whether any token counts were captured.

func (Usage) TotalTokens

func (u Usage) TotalTokens() int64

TotalTokens returns InputTokens + OutputTokens.

Jump to

Keyboard shortcuts

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