codex

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package codex implements the Agent interface for OpenAI's Codex CLI.

Index

Constants

View Source
const (
	HookNameSessionStart     = "session-start"
	HookNameUserPromptSubmit = "user-prompt-submit"
	HookNameStop             = "stop"
	HookNamePreToolUse       = "pre-tool-use"
)

Codex hook names — these become subcommands under `entire hooks codex`

View Source
const HooksFileName = "hooks.json"

HooksFileName is the hooks config file used by Codex.

Variables

This section is empty.

Functions

func NewCodexAgent

func NewCodexAgent() agent.Agent

NewCodexAgent creates a new Codex agent instance.

Types

type CodexAgent

type CodexAgent struct{}

CodexAgent implements the Agent interface for OpenAI's Codex CLI.

func (*CodexAgent) AreHooksInstalled

func (c *CodexAgent) AreHooksInstalled(ctx context.Context) bool

AreHooksInstalled checks if Entire hooks are installed in Codex hooks.json.

func (*CodexAgent) CalculateTokenUsage

func (c *CodexAgent) CalculateTokenUsage(transcriptData []byte, fromOffset int) (*agent.TokenUsage, error)

CalculateTokenUsage computes token usage from the transcript starting at the given line offset. Codex reports cumulative total_token_usage, so we compute the delta between the last token_count at/before the offset and the last token_count after the offset.

func (*CodexAgent) ChunkTranscript

func (c *CodexAgent) ChunkTranscript(_ context.Context, content []byte, maxSize int) ([][]byte, error)

ChunkTranscript splits a JSONL transcript at line boundaries.

func (*CodexAgent) Description

func (c *CodexAgent) Description() string

Description returns a human-readable description.

func (*CodexAgent) DetectPresence

func (c *CodexAgent) DetectPresence(ctx context.Context) (bool, error)

DetectPresence checks if Codex is configured in the repository.

func (*CodexAgent) ExtractModifiedFilesFromOffset

func (c *CodexAgent) ExtractModifiedFilesFromOffset(path string, startOffset int) (files []string, currentPosition int, err error)

ExtractModifiedFilesFromOffset extracts files modified since a given line offset.

func (*CodexAgent) ExtractPrompts

func (c *CodexAgent) ExtractPrompts(sessionRef string, fromOffset int) ([]string, error)

ExtractPrompts returns user prompts from the transcript starting at the given offset.

func (*CodexAgent) FormatResumeCommand

func (c *CodexAgent) FormatResumeCommand(sessionID string) string

FormatResumeCommand returns the command to resume a Codex session.

func (*CodexAgent) GetSessionDir

func (c *CodexAgent) GetSessionDir(_ string) (string, error)

GetSessionDir returns the directory where Codex stores session transcripts. Codex stores transcripts (rollout files) in its home directory.

func (*CodexAgent) GetSessionID

func (c *CodexAgent) GetSessionID(input *agent.HookInput) string

GetSessionID extracts the session ID from hook input.

func (*CodexAgent) GetTranscriptPosition

func (c *CodexAgent) GetTranscriptPosition(path string) (int, error)

GetTranscriptPosition returns the current line count of a Codex rollout transcript.

func (*CodexAgent) HookNames

func (c *CodexAgent) HookNames() []string

HookNames returns the hook verbs Codex supports.

func (*CodexAgent) InstallHooks

func (c *CodexAgent) InstallHooks(ctx context.Context, localDev bool, force bool) (int, error)

InstallHooks installs Codex hooks in .codex/hooks.json.

func (*CodexAgent) IsPreview

func (c *CodexAgent) IsPreview() bool

IsPreview returns true because this is a new integration.

func (*CodexAgent) Name

func (c *CodexAgent) Name() types.AgentName

Name returns the agent registry key.

func (*CodexAgent) ParseHookEvent

func (c *CodexAgent) ParseHookEvent(_ context.Context, hookName string, stdin io.Reader) (*agent.Event, error)

ParseHookEvent translates a Codex hook into a normalized lifecycle Event. Returns nil if the hook has no lifecycle significance.

func (*CodexAgent) ProtectedDirs

func (c *CodexAgent) ProtectedDirs() []string

ProtectedDirs returns directories that Codex uses for config/state.

func (*CodexAgent) ReadSession

func (c *CodexAgent) ReadSession(input *agent.HookInput) (*agent.AgentSession, error)

ReadSession reads a session from Codex's storage (JSONL rollout file).

func (*CodexAgent) ReadTranscript

func (c *CodexAgent) ReadTranscript(sessionRef string) ([]byte, error)

ReadTranscript reads the raw JSONL transcript bytes for a session.

func (*CodexAgent) ReassembleTranscript

func (c *CodexAgent) ReassembleTranscript(chunks [][]byte) ([]byte, error)

ReassembleTranscript concatenates JSONL chunks with newlines.

func (*CodexAgent) ResolveSessionFile

func (c *CodexAgent) ResolveSessionFile(sessionDir, agentSessionID string) string

ResolveSessionFile returns the path to a Codex session transcript file. Codex provides the transcript path directly in hook payloads as an absolute path. When only a session ID is available, we construct a best-effort path.

func (*CodexAgent) Type

func (c *CodexAgent) Type() types.AgentType

Type returns the agent type identifier.

func (*CodexAgent) UninstallHooks

func (c *CodexAgent) UninstallHooks(ctx context.Context) error

UninstallHooks removes Entire hooks from Codex hooks.json.

func (*CodexAgent) WriteHookResponse

func (c *CodexAgent) WriteHookResponse(message string) error

WriteHookResponse outputs a JSON hook response to stdout. Codex reads the systemMessage field and displays it to the user.

func (*CodexAgent) WriteSession

func (c *CodexAgent) WriteSession(_ context.Context, session *agent.AgentSession) error

WriteSession writes a session to Codex's storage (JSONL rollout file).

type HookEntry

type HookEntry struct {
	Type    string `json:"type"`
	Command string `json:"command"`
	Timeout int    `json:"timeout,omitempty"`
}

HookEntry represents a single hook command in the config.

type HookEvents

type HookEvents struct {
	SessionStart     []MatcherGroup `json:"SessionStart,omitempty"`
	UserPromptSubmit []MatcherGroup `json:"UserPromptSubmit,omitempty"`
	Stop             []MatcherGroup `json:"Stop,omitempty"`
	PreToolUse       []MatcherGroup `json:"PreToolUse,omitempty"`
}

HookEvents contains the hook configurations by event type.

type HooksFile

type HooksFile struct {
	Hooks HookEvents `json:"hooks"`
}

HooksFile represents the .codex/hooks.json structure.

type MatcherGroup

type MatcherGroup struct {
	Matcher *string     `json:"matcher"`
	Hooks   []HookEntry `json:"hooks"`
}

MatcherGroup groups hooks under an optional matcher pattern.

Jump to

Keyboard shortcuts

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