Files
magpie/internal/gateway/tools_need_responses_test.go
T
yetone 9bf20dbfc1 gateway: a model a relay serves with tools on Responses alone goes to the provider's Responses URL (#1308, Stevenzc888)
A custom provider with the same relay as its OpenAI compatible and OpenAI
Responses URL: magpie asks a GPT on a relay on Chat Completions first (Pi is
wired on openai-completions for it), and the relay turned Pi's tools away with
400 "gpt-6.1-sol requires Responses for tool calls; this account only supports
Chat Completions". wrongEndpoint didn't know that wording, so the 400 reached
Pi instead of the request going on to the Responses URL. "requires responses"
is now one of its phrases: the model is remembered as not served on that
relay's Chat, and the request (relayed as Pi sent it, or translated from
Claude Code's Messages) is spoken as Responses there, later turns at once.
Where the provider has no Responses URL, the error now says so
("gpt-6.1-sol is served on /v1/responses, which <provider> has no URL for")
rather than the relay's word alone (wrongAPINote reads the error body).

Red before: TestToolsThatNeedResponsesGoThere failed with the user's exact
error ("status 400: ... requires Responses for tool calls; this account only
supports Chat Completions"); TestTranslatedToolsThatNeedResponsesGoThere,
TestToolsThatNeedResponsesNameTheMissingURL and TestWrongEndpoint's new case
failed too. With the phrase but without the note, the missing-URL test fails.
go test -tags nogui ./internal/gateway ./internal/provider pass, and
./internal/agent as a non-root user (as root in Docker its chmod-based
aside_offline tests fail, unrelated); go vet -tags nogui ./internal/gateway
for darwin, linux and windows, and gofmt -l pass (linux, golang:1.26.8 in
Docker: this Mac's syspolicyd is pegged and new binaries hang at launch).
Not tried against the reporter's real relay.
2026-10-08 20:31:01 +08:00

153 lines
6.5 KiB
Go

package gateway
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// toolsOnChatRefused is what a relay answers a Chat Completions request
// with tools for a model it serves with tools on Responses alone (#1308,
// Stevenzc888's relay, as the issue quotes it).
const toolsOnChatRefused = `{"error":{"message":"status_code=400, gpt-6.1-sol requires Responses for tool calls; this account only supports Chat Completions","type":"invalid_request_error"}}`
// toolsRelay is a relay at one base URL serving Chat Completions and
// Responses, which takes gpt-6.1-sol with tools on Responses alone.
type toolsRelay struct {
mu sync.Mutex
chatCalls, resCalls int
resBody []byte
}
func (f *toolsRelay) ServeHTTP(w http.ResponseWriter, r *http.Request) {
b, _ := io.ReadAll(r.Body)
f.mu.Lock()
defer f.mu.Unlock()
switch r.URL.Path {
case "/v1/chat/completions":
f.chatCalls++
var req struct {
Tools []any `json:"tools"`
}
json.Unmarshal(b, &req)
if len(req.Tools) > 0 {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, toolsOnChatRefused)
return
}
w.Header().Set("Content-Type", "text/event-stream")
io.WriteString(w, sse(
`data: {"id":"c1","model":"gpt-6.1-sol","choices":[{"delta":{"role":"assistant","content":"chat ok"}}]}`,
`data: {"id":"c1","choices":[{"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":3,"completion_tokens":2}}`,
`data: [DONE]`))
case "/v1/responses":
f.resCalls++
f.resBody = b
w.Header().Set("Content-Type", "text/event-stream")
io.WriteString(w, sse(
`event: response.created`+"\n"+`data: {"type":"response.created","response":{"id":"r1","model":"gpt-6.1-sol"}}`,
`event: response.output_text.delta`+"\n"+`data: {"type":"response.output_text.delta","delta":"responses ok"}`,
`event: response.completed`+"\n"+`data: {"type":"response.completed","response":{"id":"r1","status":"completed","usage":{"input_tokens":9,"output_tokens":2}}}`))
default:
http.NotFound(w, r)
}
}
// piChatTools is a turn as Pi's openai-completions API sends it (pi-ai's
// openai-completions.js), the API magpie wires Pi on for a custom
// provider with a Chat URL: streamed, its tools, a thinking level.
const piChatTools = `{"model":"relay/gpt-6.1-sol","stream":true,"stream_options":{"include_usage":true},"store":false,
"max_completion_tokens":32000,"reasoning_effort":"high",
"messages":[{"role":"developer","content":"You are Pi."},{"role":"user","content":"list the files"}],
"tools":[{"type":"function","function":{"name":"ls","description":"List files","parameters":{"type":"object","properties":{"path":{"type":"string"}}},"strict":false}}]}`
// A custom provider whose Chat and Responses URLs are the same relay
// (#1308): Pi asks on Chat Completions with tools, the relay says the model
// takes tools on Responses alone, and the request goes to the provider's
// Responses URL instead of the relay's 400 reaching Pi — and later turns
// go there at once.
func TestToolsThatNeedResponsesGoThere(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
f := &toolsRelay{}
up := httptest.NewServer(f)
t.Cleanup(up.Close)
if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Key: "k", Models: []string{"gpt-6.1-sol"},
Chat: up.URL + "/v1", Responses: up.URL + "/v1"}); err != nil {
t.Fatal(err)
}
if n := mustFind(t, "relay").Native("gpt-6.1-sol"); n != provider.Chat {
t.Fatalf("Pi is wired on %q for the relay's model, the test assumes chat", n)
}
handler := New().Handler()
for i, want := range []int{1, 2} {
rec := httptest.NewRecorder()
handler.ServeHTTP(rec, httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(piChatTools)))
if rec.Code != 200 || !strings.Contains(rec.Body.String(), "responses ok") {
t.Fatalf("call %d: status %d: %s", i, rec.Code, rec.Body.String())
}
f.mu.Lock()
chat, res, body := f.chatCalls, f.resCalls, string(f.resBody)
f.mu.Unlock()
if chat != 1 || res != want {
t.Fatalf("call %d: chat=%d responses=%d", i, chat, res)
}
if !strings.Contains(body, `"name":"ls"`) || !strings.Contains(body, `"model":"gpt-6.1-sol"`) {
t.Errorf("call %d: Responses request lost the tools or model: %s", i, body)
}
}
}
// The sibling on a translated request: Claude Code's Messages turn with
// tools, spoken to the relay as Chat Completions, goes on to Responses too.
func TestTranslatedToolsThatNeedResponsesGoThere(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
f := &toolsRelay{}
up := httptest.NewServer(f)
t.Cleanup(up.Close)
if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Key: "k", Models: []string{"gpt-6.1-sol"},
Chat: up.URL + "/v1", Responses: up.URL + "/v1"}); err != nil {
t.Fatal(err)
}
rec := httptest.NewRecorder()
New().Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/messages", strings.NewReader(`{"model":"relay/gpt-6.1-sol","max_tokens":100,"stream":true,
"messages":[{"role":"user","content":"list the files"}],
"tools":[{"name":"ls","description":"List files","input_schema":{"type":"object","properties":{"path":{"type":"string"}}}}]}`)))
if rec.Code != 200 || !strings.Contains(rec.Body.String(), "responses ok") {
t.Fatalf("status %d: %s", rec.Code, rec.Body.String())
}
if f.chatCalls != 1 || f.resCalls != 1 || !strings.Contains(string(f.resBody), `"name":"ls"`) {
t.Fatalf("chat=%d responses=%d: %s", f.chatCalls, f.resCalls, f.resBody)
}
}
// The same relay with no Responses URL set: nothing else to ask, and the
// error tells the user which URL to set rather than the relay's word alone.
func TestToolsThatNeedResponsesNameTheMissingURL(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
f := &toolsRelay{}
up := httptest.NewServer(f)
t.Cleanup(up.Close)
if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Key: "k", Models: []string{"gpt-6.1-sol"},
Chat: up.URL + "/v1"}); err != nil {
t.Fatal(err)
}
rec := httptest.NewRecorder()
New().Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(piChatTools)))
if rec.Code != 400 || !strings.Contains(rec.Body.String(), "gpt-6.1-sol is served on /v1/responses, which Relay has no URL for") {
t.Fatalf("status %d: %s", rec.Code, rec.Body.String())
}
if f.resCalls != 0 {
t.Errorf("Responses asked with no URL for it: %d", f.resCalls)
}
}