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; the Decider and Observer get only a hash of the account (Meta.Account). Nothing is re-sent once upstream has started.
Index ¶
Constants ¶
const ( PathResponses = "/v1/responses" PathModels = "/v1/models" PathHealth = "/healthz" )
Paths the listener serves; anything else is 404.
const ( LoopbackHost = "127.0.0.1" DefaultAddr = LoopbackHost + ":0" )
LoopbackHost is the only host the listener binds; DefaultAddr picks an ephemeral port on it.
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.
const HeaderToken = "X-Codex-Router-Token"
HeaderToken carries the per-session secret that Options.Token checks. It never goes upstream.
const MaxBodyBytes = 32 << 20
MaxBodyBytes caps a request body; larger bodies get 413.
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
Listener net.Listener
Health func(context.Context) string
Token string
Upstream string
Client *http.Client
Decider Decider
Observer Observer
OnCatalog func(catalog.Catalog)
Logger *slog.Logger
DecideTimeout time.Duration
// contains filtered or unexported fields
}
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. Listener, when set, is served instead of binding Addr: a socket from the service manager, which must be TCP on LoopbackHost. Health, when set, adds one line to the /healthz body.