proxy

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2026 License: MIT Imports: 14 Imported by: 0 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

Overview

Package proxy is the loopback Responses proxy: POST /v1/responses and GET /v1/models go to the fixed ChatGPT backend with no redirects; SSE streams through unchanged except for the stripped model headers. It asks a Decider for a rewrite before the upstream call and reports usage to an Observer after it. Authorization and ChatGPT-Account-ID are forwarded upstream only and never logged. Nothing is re-sent once upstream has started.

Index

Constants

View Source
const (
	PathResponses = "/v1/responses"
	PathModels    = "/v1/models"
	PathHealth    = "/healthz"
)

Paths the listener serves; anything else is 404.

View Source
const (
	LoopbackHost = "127.0.0.1"
	DefaultAddr  = LoopbackHost + ":0"
)

LoopbackHost is the only host the listener binds; DefaultAddr picks an ephemeral port on it.

View Source
const DefaultDecideTimeout = 1700 * time.Millisecond

DefaultDecideTimeout bounds Decide when Options.DecideTimeout is zero: the default classifier deadline (1500 ms) plus 200 ms, as config.DecideTimeout computes for a classifier without a timeout.

View Source
const HeaderToken = "X-Codex-Router-Token"

HeaderToken carries the per-session secret that Options.Token checks. It never goes upstream.

View Source
const MaxBodyBytes = 32 << 20

MaxBodyBytes caps a request body; larger bodies get 413.

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

Upstream is the only backend the proxy talks to.

Variables

This section is empty.

Functions

This section is empty.

Types

type Decider

type Decider interface {
	Decide(ctx context.Context, req *responses.Request, meta responses.Meta) (responses.Rewrite, bool)
}

Decider returns the rewrite for a user step, or false to forward the original. It must respect ctx's deadline; the proxy forwards the original once the deadline passes, whatever Decide returns later.

type Observer

type Observer interface {
	Observe(meta responses.Meta, served string, usage responses.Usage, status int)
}

Observer learns the served model, usage and status of each response: once per response.completed event of a 2xx stream, or once with zero usage for a non-2xx upstream response. Requests that never got an upstream response are not observed.

type Options

type Options struct {
	Addr          string
	Token         string
	Upstream      string
	Client        *http.Client
	Decider       Decider
	Observer      Observer
	OnCatalog     func(catalog.Catalog)
	Logger        *slog.Logger
	DecideTimeout time.Duration
}

Options configure a Server. Upstream overrides the backend in tests only. Addr must be on LoopbackHost; empty means DefaultAddr. Client's CheckRedirect is always replaced with one that refuses redirects. OnCatalog receives every /models 200 body that parses (launcher passes catalog.Store.Set). DecideTimeout bounds each Decide call. Token, when set, must arrive in HeaderToken on /v1/responses and /v1/models, or the request gets 401 unread: any local uid can reach the loopback port. Empty disables the check.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server is the loopback proxy. Call Listen, then Serve.

func New

func New(o Options) *Server

New returns a Server; nothing is bound until Listen.

func (*Server) Listen

func (s *Server) Listen() (int, error)

Listen validates the options and binds the loopback listener. It returns the bound port.

func (*Server) Serve

func (s *Server) Serve(ctx context.Context) error

Serve serves until ctx is done, then shuts down: in-flight requests see ctx cancelled, which cancels their upstream calls.

Jump to

Keyboard shortcuts

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