v0.48.1 deaab59

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

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