Skip to content

Conversation

@CaddyGlow
Copy link
Owner

Changelog: Add OpenAI Codex Provider Support

Added OpenAI Codex Provider with Full Proxy Support

Overview

Implemented comprehensive support for OpenAI Codex CLI integration, enabling users to proxy requests through their OpenAI subscription via the ChatGPT backend API. This feature provides an alternative to the Claude provider while maintaining full compatibility with the existing proxy architecture. The implementation uses the OpenAI Responses API endpoint as documented at https://platform.openai.com/docs/api-reference/responses/get.

Key Features

Complete Codex API Proxy

  • Full reverse proxy to https://chatgpt.com/backend-api/codex
  • Support for both /codex/responses and /codex/{session_id}/responses endpoints
  • Compatible with Codex CLI 0.21.0 and authentication flow
  • Implements OpenAI Responses API protocol

OAuth PKCE Authentication Flow

  • Implements complete OpenAI OAuth 2.0 PKCE flow matching official Codex CLI
  • Local callback server on port 1455 for authorization code exchange
  • Token refresh and credential management with persistent storage
  • Support for ~/.openai.toml configuration file format

Intelligent Request/Response Handling

  • Automatic detection and injection of Codex CLI instructions field
  • Smart streaming behavior based on user's explicit stream parameter
  • Session management with flexible session ID handling (auto-generated, persistent, header-forwarded)
  • Request transformation preserving Codex CLI identity headers

Advanced Configuration

  • Environment variable support: CODEX__BASE_URL
  • Configurable via TOML: [codex] section in configuration files
  • Debug logging with request/response capture capabilities
  • Comprehensive error handling with proper HTTP status codes
  • Enabled by default

Technical Implementation

New Components Added:

  • ccproxy/auth/openai.py - OAuth token management and credential storage
  • ccproxy/core/codex_transformers.py - Request/response transformation for Codex format
  • ccproxy/api/routes/codex.py - FastAPI routes for Codex endpoints
  • ccproxy/models/detection.py - Codex CLI detection and header management
  • ccproxy/services/codex_detection_service.py - Runtime detection of Codex CLI requests

Enhanced Proxy Service:

  • Extended ProxyService.handle_codex_request() with full Codex support
  • Intelligent streaming response conversion when user doesn't explicitly request streaming
  • Comprehensive request/response logging for debugging
  • Error handling with proper OpenAI-compatible error responses

Streaming Behavior Fix

Problem Resolved: Fixed issue where requests without explicit stream field were incorrectly returning streaming responses.

Solution Implemented:

  • When "stream" field is missing: Inject "stream": true for upstream (Codex requirement) but return JSON response to client
  • When "stream": true explicitly set: Return streaming response to client
  • When "stream": false explicitly set: Return JSON response to client
  • Smart response conversion: collects streaming data and converts to single JSON response when user didn't request streaming

Usage Examples

Basic Request (JSON Response):

curl -X POST "http://127.0.0.1:8000/codex/responses" \
  -H "Content-Type: application/json" \
  -d '{
    "input": [{"type": "message", "role": "user", "content": [{"type": "input_text", "text": "Hello!"}]}],
    "model": "gpt-5",
    "store": false
  }'

Streaming Request:

curl -X POST "http://127.0.0.1:8000/codex/responses" \
  -H "Content-Type: application/json" \
  -d '{
    "input": [{"type": "message", "role": "user", "content": [{"type": "input_text", "text": "Hello!"}]}],
    "model": "gpt-5",
    "stream": true,
    "store": false
  }'

Authentication Setup

Environment Variables:

export CODEX__BASE_URL="https://chatgpt.com/backend-api/codex"

Configuration File (~/.ccproxy.toml):

[codex]
base_url = "https://chatgpt.com/backend-api/codex"

Compatibility

  • Codex CLI: Full compatibility with codex-cli 0.21.0
  • OpenAI OAuth: Complete PKCE flow implementation
  • Session Management: Supports persistent and auto-generated sessions
  • Model Support: All Codex-supported models (gpt-5, gpt-4, etc.)
  • Streaming: Both streaming and non-streaming responses
  • Error Handling: Proper HTTP status codes and OpenAI-compatible errors
  • API Compliance: Follows OpenAI Responses API specification

Files Modified/Added

New Files:

  • ccproxy/auth/openai.py - OpenAI authentication management
  • ccproxy/core/codex_transformers.py - Codex request/response transformation
  • ccproxy/api/routes/codex.py - Codex API endpoints
  • ccproxy/models/detection.py - Codex detection models
  • ccproxy/services/codex_detection_service.py - Codex CLI detection service

Modified Files:

  • ccproxy/services/proxy_service.py - Added handle_codex_request() method
  • ccproxy/config/settings.py - Added Codex configuration section
  • ccproxy/api/app.py - Integrated Codex routes
  • ccproxy/api/routes/health.py - Added Codex health checks

Breaking Changes

None. This is a purely additive feature that doesn't affect existing Claude provider functionality.

Migration Notes

For users wanting to use Codex provider:

  1. Authenticate: Use existing OpenAI credentials or run Codex CLI login
  2. Update endpoints: Change from /v1/messages to /codex/responses

This implementation provides a complete, production-ready OpenAI Codex proxy solution that maintains the same standards as the existing Claude provider while offering users choice in their AI provider preferences.

@CaddyGlow CaddyGlow force-pushed the feature/codex branch 3 times, most recently from 3bc1f2e to bda734c Compare August 13, 2025 06:27
@CaddyGlow
Copy link
Owner Author

#7

Implemented comprehensive support for OpenAI Codex CLI integration,
enabling users to proxy requests through their OpenAI subscription via
the ChatGPT backend API. This feature provides an alternative to the
Claude provider while maintaining full compatibility with the existing
proxy architecture. The implementation uses the OpenAI Responses API
endpoint as documented at
https://platform.openai.com/docs/api-reference/responses/get.

**Complete Codex API Proxy**
- Full reverse proxy to `https://chatgpt.com/backend-api/codex`
- Support for both `/codex/responses` and
`/codex/{session_id}/responses` endpoints
- Compatible with Codex CLI 0.21.0 and authentication flow
- Implements OpenAI Responses API protocol

**OAuth PKCE Authentication Flow**
- Implements complete OpenAI OAuth 2.0 PKCE flow matching official Codex
CLI
- Local callback server on port 1455 for authorization code exchange
- Token refresh and credential management with persistent storage
- Support for `~/.openai.toml` configuration file format

**Intelligent Request/Response Handling**
- Automatic detection and injection of Codex CLI instructions field
- Smart streaming behavior based on user's explicit `stream` parameter
- Session management with flexible session ID handling (auto-generated,
persistent, header-forwarded)
- Request transformation preserving Codex CLI identity headers

**Advanced Configuration**
- Environment variable support: `CODEX__BASE_URL`
- Configurable via TOML: `[codex]` section in configuration files
- Debug logging with request/response capture capabilities
- Comprehensive error handling with proper HTTP status codes
- Enabled by default

**New Components Added:**
- `ccproxy/auth/openai.py` - OAuth token management and credential
storage
- `ccproxy/core/codex_transformers.py` - Request/response transformation
for Codex format
- `ccproxy/api/routes/codex.py` - FastAPI routes for Codex endpoints
- `ccproxy/models/detection.py` - Codex CLI detection and header
management
- `ccproxy/services/codex_detection_service.py` - Runtime detection of
Codex CLI requests

**Enhanced Proxy Service:**
- Extended `ProxyService.handle_codex_request()` with full Codex support
- Intelligent streaming response conversion when user doesn't explicitly
request streaming
- Comprehensive request/response logging for debugging
- Error handling with proper OpenAI-compatible error responses

**Problem Resolved:** Fixed issue where requests without explicit
`stream` field were incorrectly returning streaming responses.

**Solution Implemented:**
- When `"stream"` field is missing: Inject `"stream": true` for upstream
(Codex requirement) but return JSON response to client
- When `"stream": true` explicitly set: Return streaming response to
client
- When `"stream": false` explicitly set: Return JSON response to client
- Smart response conversion: collects streaming data and converts to
single JSON response when user didn't request streaming

**Basic Request (JSON Response):**
```bash
curl -X POST "http://127.0.0.1:8000/codex/responses" \
  -H "Content-Type: application/json" \
  -d '{
    "input": [{"type": "message", "role": "user", "content": [{"type":
"input_text", "text": "Hello!"}]}],
    "model": "gpt-5",
    "store": false
  }'
```

**Streaming Request:**
```bash
curl -X POST "http://127.0.0.1:8000/codex/responses" \
  -H "Content-Type: application/json" \
  -d '{
    "input": [{"type": "message", "role": "user", "content": [{"type":
"input_text", "text": "Hello!"}]}],
    "model": "gpt-5",
    "stream": true,
    "store": false
  }'
```

**Environment Variables:**
```bash
export CODEX__BASE_URL="https://chatgpt.com/backend-api/codex"
```

**Configuration File (`~/.ccproxy.toml`):**
```toml
[codex]
base_url = "https://chatgpt.com/backend-api/codex"
```

- Codex CLI: Full compatibility with `codex-cli 0.21.0`
- OpenAI OAuth: Complete PKCE flow implementation
- Session Management: Supports persistent and auto-generated sessions
- Model Support: All Codex-supported models (`gpt-5`, `gpt-4`, etc.)
- Streaming: Both streaming and non-streaming responses
- Error Handling: Proper HTTP status codes and OpenAI-compatible errors
- API Compliance: Follows OpenAI Responses API specification

**New Files:**
- `ccproxy/auth/openai.py` - OpenAI authentication management
- `ccproxy/core/codex_transformers.py` - Codex request/response
transformation
- `ccproxy/api/routes/codex.py` - Codex API endpoints
- `ccproxy/models/detection.py` - Codex detection models
- `ccproxy/services/codex_detection_service.py` - Codex CLI detection
service

**Modified Files:**
- `ccproxy/services/proxy_service.py` - Added `handle_codex_request()`
method
- `ccproxy/config/settings.py` - Added Codex configuration section
- `ccproxy/api/app.py` - Integrated Codex routes
- `ccproxy/api/routes/health.py` - Added Codex health checks

None. This is a purely additive feature that doesn't affect existing
Claude provider functionality.

For users wanting to use Codex provider:
1. Authenticate: Use existing OpenAI credentials or run Codex CLI login
2. Update endpoints: Change from `/v1/messages` to `/codex/responses`

This implementation provides a complete, production-ready OpenAI Codex
proxy solution that maintains the same standards as the existing Claude
provider while offering users choice in their AI provider preferences.
Add complete test coverage for OpenAI Codex integration including API endpoints,
authentication flows, and proxy service functionality.

**Test Infrastructure Added:**
- OpenAI Codex API mocking fixtures with httpx_mock for external HTTP calls
- Codex-specific test clients with credential mocking
- Response format validation helpers for Codex API compliance
- Test data structures for various Codex request scenarios

**API Endpoint Tests:**
- `/codex/responses` endpoint with success and error scenarios
- `/codex/{session_id}/responses` session-based endpoint testing
- Streaming response handling and SSE format compliance
- Request validation for missing/malformed input handling

**Authentication Tests:**
- OpenAI OAuth PKCE flow testing with token management
- Credential storage and retrieval file operations
- JWT token validation and expiration handling
- Error scenarios for authentication failures

**Proxy Service Tests:**
- Request transformation between Codex and OpenAI formats
- Streaming to non-streaming response conversion logic
- Session ID handling and forwarding
- Error propagation and HTTP status code mapping

**Coverage:** 1031 lines across 7 test files providing comprehensive coverage
of Codex functionality including edge cases, error conditions, and integration points.
…authentication flow

Extends CCProxy with full OpenAI Codex Response API support, providing ChatGPT Plus users
programmatic access to GPT models through the chatgpt.com backend. Improves authentication
flow to automatically detect and reuse existing Codex CLI credentials.

## Documentation Updates

### README.md
- Clarified Codex as "Response API" throughout for accuracy
- Added ChatGPT Plus subscription requirement prominently
- Expanded authentication options with credential reuse explanation
- Added detailed curl examples for Response API usage
- Documented instruction prompt injection and token implications
- Added comprehensive troubleshooting for Codex-specific issues
- Improved endpoint documentation with clear provider separation

### Authentication Guide (docs/user-guide/authentication.md)
- Added dedicated OpenAI/Codex authentication section
- Documented credential storage locations for all providers
- Added `ccproxy auth openai-info` command with example output
- Explained credential reuse from Codex CLI
- Added auto-renewal capabilities documentation

### API Reference (docs/api-reference.md)
- Added OpenAI Codex Mode section with experimental status
- Listed available ChatGPT Plus models
- Added Codex Response API request format
- Documented session management endpoints
- Added links to official OpenAI Response API documentation

### New Codex API Guide (docs/user-guide/codex-api.md)
- Comprehensive 400+ line guide for Codex Response API
- Detailed authentication flow and OAuth2 PKCE explanation
- Session management strategies with code examples
- Python and JavaScript integration examples
- Instruction prompt injection documentation
- Rate limiting and error handling guidance
- Migration guide from OpenAI API
- Troubleshooting common issues

## CLI Enhancements

### ccproxy auth openai-info Command
- Enhanced JWT token decoding to extract user information
- Display ChatGPT subscription details (plan type, dates)
- Show organization membership and roles
- Added token technical details (algorithm, issuer, audience)
- Improved credential validation feedback

These changes make the Codex Response API integration more discoverable and easier
to use, with clear documentation of requirements, limitations, and best practices.
…andling

This commit enhances the Codex API implementation with several key improvements:

## Response Format Standardization
- Made `usage` field optional in `OpenAIChatCompletionResponse` to match OpenAI API spec
- Improved type safety by returning `OpenAIUsage` objects instead of raw dictionaries
- Added better null checking for response data to prevent crashes on malformed responses

## Reasoning Content Processing
- Simplified reasoning block handling to follow official OpenAI Response API format
- Removed complex signature tracking and custom XML wrapping
- Fixed reasoning content to stream properly without custom modifications
- Improved chunk processing with cleaner state management

## Streaming Response Improvements
- Enhanced streaming response detection with better header checking
- Fixed usage data serialization in streaming chunks using proper model dumping
- Improved error handling for edge cases in streaming responses

## Documentation Updates
- Updated README with comprehensive Codex API documentation
- Added clear limitations section for chat completions endpoint
- Included working model examples (gpt-5) and configuration samples
- Improved formatting consistency throughout documentation

## Model and Proxy Service Updates
- Enhanced model mapping validation and error handling
- Improved proxy service resilience for various response formats
- Better handling of OpenAI parameter restrictions in Codex mode

These changes provide a more reliable and specification-compliant implementation of the Codex chat completions endpoint while maintaining backward compatibility.
… Codex endpoints

Enhanced error handling for non-streaming Codex responses by properly
detecting and
returning error responses with correct HTTP status codes instead of
treating them as
streaming responses. Added comprehensive error response parsing to
handle various error
formats (detail, error, message fields) and improved streaming response
collection to
differentiate between actual streaming content and error responses.

Key improvements:
- Added error response detection in both streaming and non-streaming
paths
- Proper status code forwarding for error responses
- Enhanced JSON error parsing with fallback mechanisms
- Improved streaming response buffering and replay functionality
- Better content-type checking to distinguish between streaming and
error responses
- Added comprehensive error logging for debugging purposes

feat: improve Codex streaming response handling with proper header
management

- Capture and forward upstream response headers from Codex API
- Add comprehensive error handling for HTTP 4xx/5xx responses in
streaming mode
- Convert API errors to streaming-compatible JSON format with proper
error structure
- Filter out conflicting headers (content-length, content-encoding,
date) for streaming responses
- Implement header capture pattern in both codex.py and proxy_service.py
- Ensure streaming responses maintain proper SSE format even for error
conditions
- Add debug logging for response headers and error conditions

refactor: simplify Codex adapter imports and consolidate response
handling

- Removed unused CodexAdapter and CodexResponseChoice from
adapters/codex/__init__.py
- Consolidated Codex models to use centralized request/response models
from ccproxy.models
- Simplified response data extraction in response_adapter.py by removing
redundant type checks
- Enhanced codex chat completions route with improved error handling and
streaming logic
- Added proper type hints and error response handling for better
debugging
- Streamlined SSE event processing with clearer reasoning block
detection
- Fixed response format conversion to maintain consistency with OpenAI
Chat Completions API

The changes consolidate Codex-related code organization, improve error
handling robustness, and enhance the streaming response conversion
between Response API format and Chat Completions format. This reduces
code duplication and improves maintainability while ensuring proper
OpenAI API compatibility.
@CaddyGlow CaddyGlow merged commit 26ed7c0 into main Aug 13, 2025
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants