codex-go

module
v0.0.0-...-ac495b1 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2025 License: Apache-2.0

README

Codex Go

A complete Go rewrite of Codex TUI, implementing a feature-complete terminal UI using Bubble Tea while maintaining compatibility with the Codex protocol.

Architecture

Built in layers from core to UI:

┌─────────────────────────────────────┐
│         TUI (Bubble Tea)            │  ← cmd/codex, internal/tui
├─────────────────────────────────────┤
│      SDK (Public Go API)            │  ← pkg/sdk
├─────────────────────────────────────┤
│    Tools & MCP Integration          │  ← internal/tools, internal/mcp
├─────────────────────────────────────┤
│  Conversation & History Manager     │  ← internal/conversation, internal/history
├─────────────────────────────────────┤
│      API Client & Streaming         │  ← internal/client, internal/tokencount
├─────────────────────────────────────┤
│   Protocol, Config, Errors          │  ← internal/protocol, internal/config
└─────────────────────────────────────┘

Development

Prerequisites
  • Go 1.21+
  • Make
Setup
# Install development tools
make install-tools

# Download dependencies
make deps
Testing
# Run all tests with coverage
make test

# Run unit tests only (fast)
make test-unit

# Update golden test files
make golden-update
Building
# Build the TUI binary
make build

# Run the binary
./bin/codex
Linting
make lint
make fmt

Project Structure

  • cmd/codex/ - TUI binary entry point
  • internal/ - Internal packages (not for external use)
    • protocol/ - Protocol types (Op, Event, Message)
    • config/ - Configuration schema and loading
    • errors/ - Error types with context
    • client/ - API client with streaming
    • tokencount/ - Token counting and usage tracking
    • conversation/ - Session and turn state management
    • history/ - Conversation persistence
    • tools/ - Tool runtime system and implementations
    • sandbox/ - Process isolation and sandboxing
    • mcp/ - Model Context Protocol client
    • tui/ - Bubble Tea UI components
  • pkg/sdk/ - Public SDK for embedding
  • test/ - Test utilities and fixtures
    • testdata/ - Test data files
    • golden/ - Golden file snapshots
    • fixtures/ - Reusable test fixtures

Development Approach

This project follows Test-Driven Development (TDD):

  1. Write tests first for each package
  2. Implement functionality to pass tests
  3. Refactor while keeping tests green
  4. Use golden files for complex outputs (JSON, ANSI)
  5. Mock external dependencies (HTTP, filesystem, exec)

Feature Parity Checklist

  • Streaming assistant messages and reasoning
  • Exec command output with live deltas
  • Patch diffs with approval workflow
  • Command approval workflow
  • Token usage tracking
  • MCP tool calls and discovery
  • Web search event rendering
  • Plan updates (todo panel)
  • Session resume and history
  • Image attachment and viewing
  • Error handling and display
  • Status panels and indicators

Day 5 Features (Go)

  • Multi-turn streaming with tool-results feedback and cumulative token usage.
  • Approval workflow integration (auto/manual/semi-auto) with protocol events.
  • Enhanced history persistence and complete state reconstruction on resume.
Multi-Turn Streaming
  • Tool calls emitted by the model are executed and their results are fed back to the model to generate a final response.
  • Safety guard: a configurable limit prevents infinite multi-turn loops (default: 10).

Example (integration test): see test/integration/day6_additional_test.go: TestMultiTurn_ThreeRounds.

Approval Workflow
  • Session-aware approval via SessionApprovalHandler bridging orchestrator requests to session state.
  • Emits tool_call_approval_needed with risk assessment details.
  • Supports auto/manual/semi-auto policies; manual blocks until approval or cancellation.

Examples:

  • Manual approval happy path: test/integration/integration_test.go: TestManualApprovalWorkflow.
  • Cancellation/timeout via context: test/integration/day6_additional_test.go: TestApprovalCancellationByContext.
Persistence & Resume
  • All submissions and events are persisted when history is enabled.
  • ReconstructStateFromHistory rebuilds conversation, usage, and turn context.
  • Sessions can be resumed and validated before accepting new turns.

Examples:

  • End-to-end persistence: test/integration/integration_test.go: TestFullSessionWithPersistence.
  • Interrupted turn resume: test/integration/day6_additional_test.go: TestResumeFromInterruptedTurn.
Validation Commands
# Build
make build

# Run integration tests (subset)
go test ./test/integration -v

# All packages (can take several minutes)
go test ./... -count=1

# Race detector (targeted)
go test -race ./internal/conversation/manager -v

CI/CD

GitHub Actions workflow runs on every push:

  • Lint with golangci-lint
  • Test with race detector
  • Build for multiple platforms
  • Coverage reporting

License

Same as parent Codex project (see root LICENSE file)

Directories

Path Synopsis
cmd
codex command
examples
streaming command
internal
client
Package client provides interfaces and types for interacting with AI model APIs.
Package client provides interfaces and types for interacting with AI model APIs.
client/mocks
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.
client/openai
Package openai provides an OpenAI-compatible client implementation.
Package openai provides an OpenAI-compatible client implementation.
config
Package config provides configuration loading and management for Codex.
Package config provides configuration loading and management for Codex.
conversation/manager
Package manager provides the conversation manager for coordinating AI-assisted coding sessions.
Package manager provides the conversation manager for coordinating AI-assisted coding sessions.
conversation/state
Package state provides conversation state tracking with thread-safe operations.
Package state provides conversation state tracking with thread-safe operations.
errors
Package errors provides error types and utilities for Codex Go.
Package errors provides error types and utilities for Codex Go.
history/compaction
Package compaction provides history compaction for managing conversation context.
Package compaction provides history compaction for managing conversation context.
history/persistence
Package persistence provides history persistence functionality for Codex sessions.
Package persistence provides history persistence functionality for Codex sessions.
models
Package models provides model definitions, registry, and validation for supported AI models.
Package models provides model definitions, registry, and validation for supported AI models.
notify
Package notify provides notification functionality for Codex events.
Package notify provides notification functionality for Codex events.
progress
Package progress provides progress tracking for long-running operations in Codex.
Package progress provides progress tracking for long-running operations in Codex.
protocol
Package protocol defines the core protocol types for Codex sessions.
Package protocol defines the core protocol types for Codex sessions.
sandbox
Package sandbox provides policy enforcement and OS-specific sandbox selection.
Package sandbox provides policy enforcement and OS-specific sandbox selection.
sandbox/docker
Package docker provides Docker container-based command execution sandbox.
Package docker provides Docker container-based command execution sandbox.
sandbox/kubernetes
Package kubernetes provides Kubernetes pod-based command execution sandbox.
Package kubernetes provides Kubernetes pod-based command execution sandbox.
sandbox/landlock
Package landlock provides Linux Landlock LSM (Linux Security Module) support for filesystem access control.
Package landlock provides Linux Landlock LSM (Linux Security Module) support for filesystem access control.
sandbox/native
Package native provides a native (non-isolated) sandbox implementation.
Package native provides a native (non-isolated) sandbox implementation.
sandbox/network
Package network provides Linux network namespace isolation.
Package network provides Linux network namespace isolation.
sandbox/seccomp
Package seccomp provides Linux Seccomp-BPF (Berkeley Packet Filter) syscall filtering.
Package seccomp provides Linux Seccomp-BPF (Berkeley Packet Filter) syscall filtering.
tokencount
Package tokencount provides token counting functionality with caching.
Package tokencount provides token counting functionality with caching.
tools
Package tools provides helper functions for initializing the tool registry with all available tools.
Package tools provides helper functions for initializing the tool registry with all available tools.
tools/file
Package file provides file operation tool runtimes.
Package file provides file operation tool runtimes.
tools/git
Package git provides git repository management tools for Codex Go.
Package git provides git repository management tools for Codex Go.
tools/image
Package image provides the view_image tool for attaching local images to conversations.
Package image provides the view_image tool for attaching local images to conversations.
tools/mcp
Package mcp provides Model Context Protocol (MCP) server integration for Codex.
Package mcp provides Model Context Protocol (MCP) server integration for Codex.
tools/mcp/oauth
Package oauth provides OAuth 2.0 authentication support for MCP servers.
Package oauth provides OAuth 2.0 authentication support for MCP servers.
tools/orchestrator
Package orchestrator provides the core tool orchestration system for Codex.
Package orchestrator provides the core tool orchestration system for Codex.
tools/patch
Package patch provides the apply_patch tool runtime.
Package patch provides the apply_patch tool runtime.
tools/plan
Package plan provides task/todo management functionality for Codex Go.
Package plan provides task/todo management functionality for Codex Go.
tools/runtime
Package runtime provides the core abstraction for tool execution in Codex Go.
Package runtime provides the core abstraction for tool execution in Codex Go.
tools/schema
Package schema provides utilities for converting tool runtime definitions to API client tool schemas (JSON Schema format).
Package schema provides utilities for converting tool runtime definitions to API client tool schemas (JSON Schema format).
tools/shell
Package shell provides the shell command execution tool runtime.
Package shell provides the shell command execution tool runtime.
pkg
sdk
Package sdk provides a high-level, ergonomic API for using Codex programmatically.
Package sdk provides a high-level, ergonomic API for using Codex programmatically.
sdk/client
Package client provides convenience helpers for creating OpenAI-compatible clients for use with the Codex SDK.
Package client provides convenience helpers for creating OpenAI-compatible clients for use with the Codex SDK.

Jump to

Keyboard shortcuts

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