openai

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ValidateAccount

func ValidateAccount(account string) error

ValidateAccount is a convenience for tests/callers that want to confirm the account id is the right shape (non-empty, trimmed). It is a no-op helper rather than a constructor check so a Codex provider can be built before the first login completes.

Types

type CodexAccountResolver

type CodexAccountResolver func(ctx context.Context) (accountID string, ok bool, err error)

CodexAccountResolver returns the `chatgpt_account_id` claim for the bearer that is about to be sent on a request. It is invoked once per request (including the 401-refresh retry) so the value can be re-derived from the live OAuth token rather than cached at construction time.

ok=false means "no account id known" — the Codex provider simply omits the header in that case (the request will 401, but that's recoverable: the user re-auths and the next login persists a fresh id).

type CodexOptions

type CodexOptions struct {
	Options
	// Originator is the value of the `originator` header. Empty defaults to
	// "codex_cli_rs" (the same value the openai/codex CLI ships). The Codex
	// backend reads this to attribute traffic; changing it is supported but
	// unusual.
	Originator string
	// UserAgent overrides the openai Options.UserAgent when non-empty. The
	// Codex backend logs the User-Agent for diagnostics, so a "codex_cli_rs"
	// / "zero" branded value is recommended.
	UserAgent string
	// AccountID is a static `chatgpt-account-id` that bypasses the resolver.
	// Leave empty in production wiring so the AccountResolver is consulted on
	// every request — that path reads the live OAuth token from the store and
	// survives a refresh that rotates the bearer (and its account claim).
	// The field exists for tests that want a pinned value without standing up
	// a resolver.
	AccountID string
	// AccountResolver, when set, returns the account id dynamically per
	// request (including the 401-refresh retry). The factory wires this so a
	// refresh that updates the stored token's Account field takes effect on
	// the next outgoing request without restarting the agent.
	//
	// ok=false means "no account id known" — the Codex provider simply omits
	// the header in that case (the request will 401, but that's recoverable:
	// the user re-auths and the next login persists a fresh id).
	AccountResolver CodexAccountResolver
	// RequestTimeout caps each outbound Codex request. 0 => 60s. The Codex
	// backend is hosted behind Cloudflare, so a few seconds is plenty for a
	// healthy connection; the cap is a safety net for the rare case the
	// request hangs past the streaming idle watchdog.
	RequestTimeout time.Duration
}

CodexOptions configures a Codex-flavored provider. It embeds the standard openai.Options so every chat-completions knob (model, custom headers, MaxTokens, parse-think-tags, etc.) is supported unchanged. The Codex-specific fields below add the headers the Codex backend requires.

type CodexProvider

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

CodexProvider is the Codex-flavored variant of the openai provider. It is a thin shim that adds the Codex-specific request headers (`originator`, `chatgpt-account-id`, branded `User-Agent`) on top of a Responses-API transport. The Codex backend at `https://chatgpt.com/backend-api/codex/responses` serves the OpenAI Responses API (not the chat-completions API), so the constructor overrides the endpoint AND the transport — the wrapped Provider is used only for its validated endpoint / auth / retry / timeout config; the actual request body and SSE parser live in codex_responses.go.

func NewCodexProvider

func NewCodexProvider(options CodexOptions) (*CodexProvider, error)

NewCodexProvider builds a CodexProvider. It is a thin wrapper over the openai.New constructor plus the Codex-specific Options.SetRequestExtra callback that injects the Codex headers. The wrapped Provider supplies the validated endpoint / auth / retry / idle-timeout config; the request body and stream parser are Codex-specific and live in codex_responses.go.

func (*CodexProvider) StreamCompletion

func (p *CodexProvider) StreamCompletion(ctx context.Context, request zeroruntime.CompletionRequest) (<-chan zeroruntime.StreamEvent, error)

StreamCompletion builds a Responses-API request and dispatches it via the Codex-specific stream path in codex_responses.go. The request body is the Responses schema (input items, tools, max_output_tokens) and the response is parsed from the typed SSE event stream the Codex backend emits (response.output_text.delta, response.function_call_ arguments.delta, response.completed, ...).

type Options

type Options struct {
	APIKey  string
	BaseURL string
	// Endpoint, when non-empty, overrides the default `{BaseURL}/chat/completions`
	// request URL. Used by flavors (notably the Codex provider) that speak a
	// different path on the same host — e.g. `/responses` on the ChatGPT Codex
	// backend. When empty, the openai provider falls back to its default
	// chat-completions path so every existing caller is unchanged.
	Endpoint        string
	Model           string
	AuthHeader      string
	AuthScheme      string
	AuthHeaderValue string
	CustomHeaders   map[string]string
	HTTPClient      *http.Client
	UserAgent       string
	// OAuthResolver, when set, supplies an OAuth bearer credential per request and
	// is preferred over APIKey; a nil resolver (or one that yields ok=false) uses
	// the API key. See providerio.SendWithAuthRetry.
	OAuthResolver providerio.TokenResolver
	// MaxTokens caps the model's output tokens. Zero omits the cap (the model's
	// own default applies). Resolved from the model registry by the factory.
	MaxTokens int
	// StreamIdleTimeout aborts the stream if no data arrives for this long.
	// When unset, Zero uses providerio.ResolveStreamIdleTimeout — the
	// ZERO_STREAM_IDLE_TIMEOUT override or providerio.DefaultStreamIdleTimeout.
	StreamIdleTimeout time.Duration
	// ParseThinkTags converts streamed <think>...</think> content into reasoning
	// events for OpenAI-compatible models known to emit that legacy format.
	ParseThinkTags bool
	// SetRequestExtra, when non-nil, is invoked on every outgoing request after
	// the provider's built-in headers (Content-Type, User-Agent) are set, so a
	// wrapper (notably the Codex provider) can inject request-specific headers
	// — e.g. an account id resolved from the OAuth token, or a hard-coded
	// "originator" value. It is also called on the 401-refresh retry, so any
	// per-request state must be re-derivable from the live request.
	SetRequestExtra func(*http.Request)
}

Options configures an OpenAI-compatible chat completions provider.

type Provider

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

Provider streams completions from an OpenAI-compatible chat completions API.

func New

func New(options Options) (*Provider, error)

New creates an OpenAI-compatible provider.

func (*Provider) StreamCompletion

func (provider *Provider) StreamCompletion(
	ctx context.Context,
	request zeroruntime.CompletionRequest,
) (<-chan zeroruntime.StreamEvent, error)

StreamCompletion sends one streaming chat completion request.

Jump to

Keyboard shortcuts

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