gaubeebangeel on X: OpenDesign's Anthropic requests to an Open Design Cloud account were all sent upstream as chat completions. The Open Design plugin declares each model on one AI SDK package (chat completions for most, Responses for deepseek-v4-pro and claude-haiku-5.5, plugins#141), and pluginAPIs gave a plugin model that one API alone, so attempt never relayed a /v1/messages request and translated it, though amr-link serves every model on all three (plugins#888). A DeepSeek API-key provider, the case first suspected, relays to /anthropic/v1/messages as sent: tried with OpenCode's, node's and no User-Agent, bare and prefixed ids, and web search. A plugin model can now list the other APIs its vendor serves it on, as their AI SDK packages (m.also; plugin.Model.Also through host.js, config model and models hook). pluginProtocols gives the model's own API first, then those; ListedAPIs, the catalog's APIs and the provider's bases come from it, so a client's request on any of them is relayed as sent. usable puts a plugin model's own API first, so a request on an API it isn't served on is still translated to the declared one. A model with no also is unchanged. opendesign 0.1.13 (plugins 397da020) sets also on every model. Test: TestPluginModelServedOnMoreAPIsKeepsTheClientAPI (fake plugin, FAKE_ALSO): Anthropic, Responses and Chat requests to the also model reach the upstream on their own path; fake-1 (no also) is still translated to /chat/completions. Red on HEAD's plugins.go: Anthropic and Responses reached /v1/chat/completions, ListedAPIs and usable were [chat]. Run (mote unless said): go test ./internal/provider ./internal/plugin ./internal/catalog ok; go test ./internal/gateway ok (972s); GOOS=linux/windows go vet of the three packages ok; gofmt clean. Not run: the full ./... suite (mote and mica each had full suites queued under suite.lock); not tried against a real amr-link account (keyblues's plugins#888 measurements are the evidence it serves /v1/messages for every model).
99 lines
4.0 KiB
Go
99 lines
4.0 KiB
Go
package gateway
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// alsoUp is an upstream that answers chat completions, Responses and
|
|
// Anthropic's messages, as Open Design's amr-link does for every model
|
|
// (plugins#888), and keeps the path each request came to.
|
|
type alsoUp struct {
|
|
mu sync.Mutex
|
|
paths []string
|
|
}
|
|
|
|
func (u *alsoUp) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
io.ReadAll(r.Body)
|
|
u.mu.Lock()
|
|
u.paths = append(u.paths, r.URL.Path)
|
|
u.mu.Unlock()
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
switch {
|
|
case strings.HasSuffix(r.URL.Path, "/messages"):
|
|
io.WriteString(w, pluginMessagesReply)
|
|
case strings.HasSuffix(r.URL.Path, "/responses"):
|
|
io.WriteString(w, sse(
|
|
`event: response.created`+"\n"+`data: {"type":"response.created","response":{"id":"r1","object":"response","status":"in_progress","output":[]}}`,
|
|
`event: response.output_item.done`+"\n"+`data: {"type":"response.output_item.done","output_index":0,"item":{"type":"message","id":"m1","role":"assistant","status":"completed","content":[{"type":"output_text","text":"ok","annotations":[]}]}}`,
|
|
`event: response.completed`+"\n"+`data: {"type":"response.completed","response":{"id":"r1","object":"response","status":"completed","output":[{"type":"message","id":"m1","role":"assistant","status":"completed","content":[{"type":"output_text","text":"ok","annotations":[]}]}],"usage":{"input_tokens":3,"output_tokens":1,"total_tokens":4}}}`))
|
|
default:
|
|
io.WriteString(w, chatSSE("ok", "", ""))
|
|
}
|
|
}
|
|
|
|
func (u *alsoUp) last(t *testing.T) string {
|
|
t.Helper()
|
|
u.mu.Lock()
|
|
defer u.mu.Unlock()
|
|
if len(u.paths) == 0 {
|
|
t.Fatal("the plugin asked its upstream nothing")
|
|
}
|
|
return u.paths[len(u.paths)-1]
|
|
}
|
|
|
|
// A plugin's model its vendor serves on other APIs too (m.also: Open
|
|
// Design's amr-link serves every model on all three) takes a client's
|
|
// request on any of them as it was sent: OpenDesign's Anthropic request to
|
|
// a model declared on chat completions went upstream as chat completions
|
|
// (gaubeebangeel on X), where amr-link serves /messages as well. A model
|
|
// that says nothing more is still translated to its own API, and a request
|
|
// on an API the model isn't served on goes to its own one.
|
|
func TestPluginModelServedOnMoreAPIsKeepsTheClientAPI(t *testing.T) {
|
|
t.Setenv("FAKE_ALSO", "1")
|
|
up := &alsoUp{}
|
|
pid := besideFake(t, "alsoco", up)
|
|
anthropic := func(model string) string {
|
|
return `{"model":"` + pid + "/" + model + `","max_tokens":64,"stream":true,"system":"You are a designer.","messages":[{"role":"user","content":"make a poster"}]}`
|
|
}
|
|
for _, c := range []struct {
|
|
path, body, want string
|
|
}{
|
|
{"/v1/messages", anthropic("fake-also"), "/messages"},
|
|
{"/v1/responses", `{"model":"` + pid + `/fake-also","stream":true,"input":"hi"}`, "/responses"},
|
|
{"/v1/chat/completions", `{"model":"` + pid + `/fake-also","stream":true,"messages":[{"role":"user","content":"hi"}]}`, "/chat/completions"},
|
|
// a model on chat completions alone is translated to it, as before
|
|
{"/v1/messages", anthropic("fake-1"), "/chat/completions"},
|
|
} {
|
|
rec := httptest.NewRecorder()
|
|
req := httptest.NewRequest("POST", c.path, strings.NewReader(c.body))
|
|
if c.path == "/v1/messages" {
|
|
req.Header.Set("anthropic-version", "2023-06-01")
|
|
}
|
|
New().Handler().ServeHTTP(rec, req)
|
|
if rec.Code != 200 {
|
|
t.Fatalf("%s %s: %d %s", c.path, c.body, rec.Code, rec.Body)
|
|
}
|
|
if got := up.last(t); !strings.HasSuffix(got, c.want) {
|
|
t.Errorf("%s %.80s reached the upstream at %s, want …%s", c.path, c.body, got, c.want)
|
|
}
|
|
}
|
|
p, err := provider.Find(pid)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// translated, it goes to the model's own API first
|
|
if to := New().usable(*p, "fake-also"); len(to) != 3 || to[0] != provider.Chat {
|
|
t.Errorf("usable %v, want chat completions first of the three", to)
|
|
}
|
|
if apis := p.ListedAPIs("fake-also"); len(apis) != 3 || apis[0] != provider.Chat {
|
|
t.Errorf("listed %v, want chat completions first of the three", apis)
|
|
}
|
|
}
|