Archived
Add native Responses API upstream proxy
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
8ffd369448
commit
0ccb99ad83
2 files changed
+241
-2
No files matched your search
@@ -19,15 +19,35 @@ import (
|
||||
type fakeUpstream struct {
|
||||
lastModel string
|
||||
lastAuth string
|
||||
lastPath string
|
||||
lastRequest map[string]any
|
||||
}
|
||||
|
||||
func (f *fakeUpstream) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
f.lastPath = r.URL.Path
|
||||
if r.Method == http.MethodGet && r.URL.Path == "/models" {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = io.WriteString(w, `{"object":"list","data":[{"id":"ds-chat"},{"id":"ds-haiku"}]}`)
|
||||
return
|
||||
}
|
||||
if r.Method == http.MethodPost && r.URL.Path == "/v1/responses" {
|
||||
f.lastAuth = r.Header.Get("Authorization")
|
||||
body, _ := io.ReadAll(r.Body)
|
||||
_ = json.Unmarshal(body, &f.lastRequest)
|
||||
f.lastModel, _ = f.lastRequest["model"].(string)
|
||||
stream, _ := f.lastRequest["stream"].(bool)
|
||||
if stream {
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
_, _ = io.WriteString(w, "event: response.completed\ndata: {\"type\":\"response.completed\"}\n\n")
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{
|
||||
"id": "resp_native", "object": "response", "status": "completed", "model": f.lastModel,
|
||||
"output": []any{}, "usage": map[string]any{"input_tokens": 9, "output_tokens": 2, "total_tokens": 11},
|
||||
})
|
||||
return
|
||||
}
|
||||
if r.URL.Path != "/chat/completions" {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
@@ -290,6 +310,74 @@ func TestResponsesStream(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestNativeResponsesProtocol(t *testing.T) {
|
||||
for _, stream := range []bool{false, true} {
|
||||
name := "non-streaming"
|
||||
if stream {
|
||||
name = "streaming"
|
||||
}
|
||||
t.Run(name, func(t *testing.T) {
|
||||
env := newCodexTestEnv(t)
|
||||
provider, err := env.store.AddProvider("native-responses", "", env.upstreamURL+"/v1", "up-key",
|
||||
config.Models{Default: "native-model"}, config.TargetCodex, config.ProtocolOpenAIResponses)
|
||||
if err != nil {
|
||||
t.Fatalf("AddProvider: %v", err)
|
||||
}
|
||||
if _, err := env.store.ActivateCodex(provider.ID); err != nil {
|
||||
t.Fatalf("ActivateCodex: %v", err)
|
||||
}
|
||||
|
||||
body := map[string]any{
|
||||
"model": "gpt-5-codex",
|
||||
"input": "What model are you?",
|
||||
"stream": stream,
|
||||
"reasoning": map[string]any{"effort": "high"},
|
||||
"tools": []any{
|
||||
map[string]any{"type": "web_search_preview"},
|
||||
map[string]any{"type": "function", "name": "read_file", "parameters": map[string]any{"type": "object"}},
|
||||
},
|
||||
}
|
||||
resp := env.post(t, "/v1/responses", body, env.store.Config().RelayToken)
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
data, _ := io.ReadAll(resp.Body)
|
||||
t.Fatalf("status = %d, body = %s", resp.StatusCode, data)
|
||||
}
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("read response: %v", err)
|
||||
}
|
||||
if stream {
|
||||
if !strings.Contains(string(data), "event: response.completed") {
|
||||
t.Fatalf("stream response = %q", data)
|
||||
}
|
||||
} else if !strings.Contains(string(data), `"id":"resp_native"`) {
|
||||
t.Fatalf("native response = %s", data)
|
||||
}
|
||||
|
||||
if env.upstream.lastPath != "/v1/responses" {
|
||||
t.Errorf("upstream path = %q, want /v1/responses", env.upstream.lastPath)
|
||||
}
|
||||
if env.upstream.lastAuth != "Bearer up-key" {
|
||||
t.Errorf("upstream auth = %q", env.upstream.lastAuth)
|
||||
}
|
||||
if env.upstream.lastModel != "native-model" {
|
||||
t.Errorf("upstream model = %q, want model mapping", env.upstream.lastModel)
|
||||
}
|
||||
if env.upstream.lastRequest["input"] != "What model are you?" || env.upstream.lastRequest["messages"] != nil {
|
||||
t.Errorf("request was converted instead of passed through: %v", env.upstream.lastRequest)
|
||||
}
|
||||
if env.upstream.lastRequest["reasoning"] == nil {
|
||||
t.Errorf("native reasoning field was dropped: %v", env.upstream.lastRequest)
|
||||
}
|
||||
tools := env.upstream.lastRequest["tools"].([]any)
|
||||
if len(tools) != 2 || tools[0].(map[string]any)["type"] != "web_search_preview" {
|
||||
t.Errorf("native tools were changed: %v", tools)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestResponsesRejectsBadToken(t *testing.T) {
|
||||
env := newCodexTestEnv(t)
|
||||
resp := env.post(t, "/v1/responses", responsesBody("gpt-5-codex", false), "crl_wrong")
|
||||
|
||||
Reference in new issue
Block a user