v0.31.0 9467429

openai_responses.go RawHistoryFolder

1package provider
2
3// OpenAI Responses API client (api.openai.com/v1/responses).
4//
5// Strategy: reuse codexClient, which already speaks the Responses wire
6// format, via a header-rewriting RoundTripper. The public Responses API on
7// api.openai.com does not need ChatGPT subscription identity headers, so the
8// transport strips them if a shared request path supplied any.
9//
10// This is a separate provider from `openai` (which is Chat Completions);
11// users opt in by passing `--provider openai-responses` or by picking a
12// model whose catalog entry tags it under that provider id.
13//
14// Auth: API key via Authorization: Bearer (set by codexClient itself).
15// Different from openai-codex, which uses OAuth subscription tokens.
16
17import (
18	"net/http"
19	"net/url"
20	"strings"
21)
22
23const openaiResponsesDefaultBaseURL = "https://api.openai.com/v1/responses"
24
25// openaiResponsesTransport rewrites headers so the codex client (which
26// is designed for chatgpt.com OAuth tokens) can target the public
27// OpenAI Responses API with a normal API key.
28type openaiResponsesTransport struct {
29	inner http.RoundTripper
30}
31
32// openaiResponsesHTTPClient preserves the public Responses header boundary
33// when a caller injects a transport, for example to scope insecure TLS.
34func openaiResponsesHTTPClient(client *http.Client) *http.Client {
35	clone := *client
36	inner := clone.Transport
37	if inner == nil {
38		inner = http.DefaultTransport
39	}
40	clone.Transport = &openaiResponsesTransport{inner: inner}
41	return &clone
42}
43
44func (t *openaiResponsesTransport) RoundTrip(req *http.Request) (*http.Response, error) {
45	clone := req.Clone(req.Context())
46	// The codex client always sets chatgpt-account-id; api.openai.com
47	// doesn't need it and may reject the OAuth identity headers, so
48	// strip them.
49	clone.Header.Del("chatgpt-account-id")
50	clone.Header.Del("openai-beta")
51	clone.Header.Del("originator")
52	clone.Header.Del("user-agent")
53	// Keep Authorization: Bearer <key> as set by the codex client.
54	return t.inner.RoundTrip(clone)
55}
56
57// NewOpenAIResponses returns an OpenAI Responses-API client (API-key
58// flow). Uses the same wire format as the ChatGPT Codex backend but
59// with the public api.openai.com endpoint and standard Bearer auth.
60//
61// baseURL may be empty; defaults to https://api.openai.com/v1/responses.
62func NewOpenAIResponses(apiKey, baseURL string) Client {
63	return NewOpenAIResponsesNamed(apiKey, baseURL, "openai-responses")
64}
65
66// NewOpenAIResponsesNamed returns a public OpenAI Responses-API client
67// reporting the supplied provider name. This lets the `openai` provider
68// route Responses-only preview models without changing the visible provider.
69func NewOpenAIResponsesNamed(apiKey, baseURL, name string) Client {
70	baseURL = responsesURL(baseURL)
71	httpClient := openaiResponsesHTTPClient(&http.Client{Timeout: 0})
72	inner := &codexClient{
73		token:        apiKey,
74		accountID:    "", // unused; transport strips the header
75		baseURL:      strings.TrimRight(baseURL, "/"),
76		errorLabel:   "openai",
77		providerName: name,
78		capabilities: responsesCapabilities{
79			// Only the official OpenAI provider declares these extensions.
80			// A Responses-shaped custom endpoint must opt in through its
81			// own client rather than inheriting OpenAI's cache contract.
82			StablePromptCacheKey:          officialOpenAIResponsesEndpoint(name, baseURL),
83			ExplicitPromptCacheBreakpoint: officialOpenAIResponsesEndpoint(name, baseURL),
84		},
85		http: httpClient,
86	}
87	var client Client = inner
88	if officialOpenAIResponsesEndpoint(name, baseURL) {
89		client = newResponsesWebSocketClient(inner)
90	}
91	return &renamedClient{inner: client, name: name}
92}
93
94func officialOpenAIResponsesEndpoint(name, endpoint string) bool {
95	if name != "openai" && name != "openai-responses" {
96		return false
97	}
98	u, err := url.Parse(endpoint)
99	return err == nil && u.Scheme == "https" && strings.EqualFold(u.Hostname(), "api.openai.com") && u.Path == "/v1/responses"
100}
101
102// responsesURL accepts either an API root or a complete Responses endpoint.
103// Catalog model entries generally carry API roots, while explicit overrides
104// may already include /responses.
105func responsesURL(baseURL string) string {
106	baseURL = strings.TrimRight(baseURL, "/")
107	if baseURL == "" {
108		return openaiResponsesDefaultBaseURL
109	}
110	if strings.HasSuffix(baseURL, "/responses") {
111		return baseURL
112	}
113	if versionSegmentSuffix.MatchString(baseURL) {
114		return baseURL + "/responses"
115	}
116	return baseURL + "/v1/responses"
117}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 476

↑↓ moveenter openesc close