]> git.giacomolli.uk - onechat/blobdiff - responses.go
Move shared code to a file of its own
[onechat] / responses.go
index f887e6d34a455a5da01700ee973e4056ce897b8b..9d6b75f6c05ce2e55335e77b44877b69ea56df5f 100644 (file)
@@ -10,12 +10,19 @@ import (
        "io"
        "net/http"
        "os"
+       "path/filepath"
        "strings"
        "sync"
        "time"
 )
 
 const (
+       // chatgptURL is the Responses endpoint of the ChatGPT backend.
+       chatgptURL = "https://chatgpt.com/backend-api/codex/responses"
+
+       // apiURL is the Responses endpoint of the OpenAI API.
+       apiURL = "https://api.openai.com/v1/responses"
+
        // quotaPrefix starts the names of the quota headers of the ChatGPT
        // backend, in canonical form.
        quotaPrefix = "X-Codex-"
@@ -145,6 +152,23 @@ type outputItem struct {
        } `json:"content"`
 }
 
+// modelEndpoint returns the URL and the authorizer of the endpoint in config.
+// The ChatGPT backend uses the token file in dir. The OpenAI API uses the key in
+// OPENAI_API_KEY, and there is no fallback from one to the other. All clients
+// share the authorizer, so one refresh runs at a time.
+func modelEndpoint(config Config, dir string) (string, authorizer, error) {
+       if config.Endpoint == endpointAPI {
+               key := os.Getenv("OPENAI_API_KEY")
+               if key == "" {
+                       return "", nil, errors.New("endpoint api needs OPENAI_API_KEY")
+               }
+
+               return apiURL, apiKey(key), nil
+       }
+
+       return chatgptURL, newChatGPTAuth(filepath.Join(dir, tokenFileName), authIssuer), nil
+}
+
 // newClient returns a client for the Responses endpoint at url. It records
 // each call in the usage file at usagePath.
 func newClient(url string, auth authorizer, cacheKey, usagePath string) *Client {