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