codex

package
v0.61.0 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: BSD-2-Clause Imports: 28 Imported by: ? 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

Index

Constants

View Source
const (
	AuthURL     = "https://auth.openai.com/oauth/authorize"
	TokenURL    = "https://auth.openai.com/oauth/token" //nolint:gosec // endpoint URL, not a credential
	ClientID    = "app_EMoamEEZ73f0CkXaXp7hrann"
	CallbackURI = "http://localhost:1455/auth/callback"
	// CallbackAddr is the listen address the registered redirect URI names.
	CallbackAddr = "127.0.0.1:1455"
)

OAuth constants of the ChatGPT Codex login, identical to the Codex CLI's.

View Source
const DefaultUpstream = "https://chatgpt.com/backend-api/codex"

DefaultUpstream is the ChatGPT Codex Responses backend base URL.

Variables

This section is empty.

Functions

func BuildAuthURL

func BuildAuthURL(state string, pkce *PKCECodes) string

BuildAuthURL returns the browser URL that starts the PKCE login.

func Login

func Login(ctx context.Context, client OAuthClient, tokenPath string, out io.Writer) error

Login runs the ChatGPT PKCE browser login: it listens on CallbackAddr, prints (and tries to open) the authorize URL, waits for the redirect, exchanges the code and writes the credential to tokenPath.

func NewTransport

func NewTransport(next http.RoundTripper, upstream *url.URL, tokens TokenSource) http.RoundTripper

NewTransport returns an http.RoundTripper that serves Anthropic /v1/messages requests from the Codex Responses backend: it translates the request body, injects the OAuth bearer and ChatGPT account headers from tokens, calls <upstream>/responses on next, and translates the SSE reply back into Anthropic SSE (client stream=true) or one Anthropic JSON message.

Any other path is answered 404 locally; the Codex backend has no equivalent of /v1/messages/count_tokens or /v1/models.

func WriteTokenFile

func WriteTokenFile(ctx context.Context, path string, ts *TokenStorage) error

WriteTokenFile writes ts to path atomically with mode 0600. Keys already present in an existing file that TokenStorage does not model (CLIProxyAPI bookkeeping such as `siwc`, `use_siwc`) are preserved.

Types

type Credentials

type Credentials struct {
	AccessToken string
	AccountID   string
}

Credentials is what an outbound Codex request needs.

type IDTokenAuthNS

type IDTokenAuthNS struct {
	ChatgptAccountID string `json:"chatgpt_account_id"`
	ChatgptPlanType  string `json:"chatgpt_plan_type"`
}

IDTokenAuthNS is the OpenAI-namespaced claim object.

type IDTokenClaims

type IDTokenClaims struct {
	Email string        `json:"email"`
	Auth  IDTokenAuthNS `json:"https://api.openai.com/auth"`
}

IDTokenClaims is the subset of the OpenAI id_token claims the router reads.

func ParseIDToken

func ParseIDToken(ctx context.Context, token string) (*IDTokenClaims, error)

ParseIDToken decodes (without verifying) the claims of a JWT id_token.

type OAuthClient

type OAuthClient interface {
	// ExchangeCode trades an authorization code for a token set.
	ExchangeCode(ctx context.Context, code string, pkce *PKCECodes) (*TokenStorage, error)
	// Refresh trades a refresh token for a new token set.
	Refresh(ctx context.Context, refreshToken string) (*TokenStorage, error)
}

OAuthClient talks to the OpenAI token endpoint.

func NewOAuthClient

func NewOAuthClient(
	httpClient *http.Client,
	tokenURL string,
	now libtime.CurrentDateTimeGetter,
) OAuthClient

NewOAuthClient returns an OAuthClient posting to tokenURL.

type PKCECodes

type PKCECodes struct {
	CodeVerifier  string
	CodeChallenge string
}

PKCECodes is an RFC 7636 verifier/challenge pair.

func GeneratePKCECodes

func GeneratePKCECodes(ctx context.Context) (*PKCECodes, error)

GeneratePKCECodes returns a fresh S256 PKCE pair.

type TokenSource

type TokenSource interface {
	Credentials(ctx context.Context) (Credentials, error)
	// Invalidate drops the cached credential after the upstream rejected it
	// (401/403), so the next Credentials call re-reads the file and refreshes
	// unless the file already holds a different token.
	Invalidate()
}

TokenSource yields valid Codex credentials, refreshing them when due.

func NewFileTokenSource

func NewFileTokenSource(
	path string,
	client OAuthClient,
	now libtime.CurrentDateTimeGetter,
) TokenSource

NewFileTokenSource returns a TokenSource backed by the credential file at path. The file is re-read on every refresh decision, so a token renewed by another process (cli-proxy-api, a fresh codex-login) is picked up.

func SharedFileTokenSource

func SharedFileTokenSource(
	path string,
	client OAuthClient,
	now libtime.CurrentDateTimeGetter,
) TokenSource

SharedFileTokenSource returns the process-wide TokenSource for path, creating it on first use. Every provider and every config reload that names the same credential file shares one source, so refreshes of the rotating refresh token are serialized. client and now are used only when the source is created.

type TokenStorage

type TokenStorage struct {
	IDToken      string `json:"id_token"`
	AccessToken  string `json:"access_token"`
	RefreshToken string `json:"refresh_token"`
	AccountID    string `json:"account_id"`
	LastRefresh  string `json:"last_refresh"`
	Email        string `json:"email"`
	Type         string `json:"type"`
	Expire       string `json:"expired"`
	PlanType     string `json:"plan_type,omitempty"`
}

TokenStorage is the on-disk Codex credential. The JSON field names match CLIProxyAPI's CodexTokenStorage, so a ~/.cli-proxy-api/codex-*.json file written by `cli-proxy-api --codex-login` is read as-is.

func ReadTokenFile

func ReadTokenFile(ctx context.Context, path string) (*TokenStorage, error)

ReadTokenFile reads a Codex credential file.

Directories

Path Synopsis
Package claude provides request translation functionality for Claude Code API compatibility.
Package claude provides request translation functionality for Claude Code API compatibility.
internal
signature
Claude thinking signature validation.
Claude thinking signature validation.
thinking
Package thinking provides unified thinking configuration processing.
Package thinking provides unified thinking configuration processing.

Jump to

Keyboard shortcuts

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