provider: a typed base that serves chat is never given a /v1 (JNDliuliuM on X)
A custom provider whose API has no /v1 listed its models under base/v1/models only; every models fetch then rewrote the saved chat and responses URLs to …/v1 (fixV1), so the user's removal of /v1 came back each time and requests went to /v1/chat/completions. fixV1 now POSTs an empty body to base/chat/completions (or /responses) first; any answer but 404/405 (a 400 for the model, a 401) means the URL as typed has its endpoint and it is kept. Only a base whose endpoint is missing and whose models sit under /v1 gets it. Run (mote): new TestFetchKeepsTypedBaseThatServesChat red on the old code (chat changed to …/v1), green after; go test ./internal/provider ok; TestFetchAddsMissingV1 and TestArkBaseKeepsItsPath unchanged; go vet darwin, linux, windows. Not tried: a real vendor; the GUI is unchanged (respellURL only fills an empty URL), so no GUI run.
This commit is contained in:
@@ -117,7 +117,7 @@ func TestArkBaseKeepsItsPath(t *testing.T) {
|
||||
t.Errorf("chat base changed to %q", q.Chat)
|
||||
}
|
||||
// even told /v1/models answered, a versioned base is left as it is
|
||||
if got := p.fixV1(base, base+"/v1/models"); got != base {
|
||||
if got := p.fixV1(context.Background(), base, base+"/v1/models"); got != base {
|
||||
t.Errorf("fixV1: %q", got)
|
||||
}
|
||||
// the ids typed in are what agents see
|
||||
@@ -126,3 +126,40 @@ func TestArkBaseKeepsItsPath(t *testing.T) {
|
||||
t.Errorf("exposed %+v", ms)
|
||||
}
|
||||
}
|
||||
|
||||
// JNDliuliuM on X: a custom provider whose API has no /v1 kept getting one
|
||||
// added. A vendor that lists its models under /v1 but serves chat at the
|
||||
// base as typed is kept as typed: the models fetch must not rewrite it.
|
||||
func TestFetchKeepsTypedBaseThatServesChat(t *testing.T) {
|
||||
isolate(t)
|
||||
h := t.TempDir()
|
||||
t.Setenv("HOME", h)
|
||||
t.Setenv("USERPROFILE", h)
|
||||
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
|
||||
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
|
||||
t.Setenv("PATH", h)
|
||||
for _, v := range agentenv.Vars {
|
||||
t.Setenv(v, "")
|
||||
}
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch {
|
||||
case r.URL.Path == "/v1/models":
|
||||
w.Write([]byte(`{"data":[{"id":"m1"}]}`))
|
||||
case r.URL.Path == "/chat/completions" && r.Method == http.MethodPost:
|
||||
http.Error(w, `{"error":"model required"}`, http.StatusBadRequest)
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
defer srv.Close()
|
||||
if err := Save(Provider{ID: "nov1", Name: "Nov1", Chat: srv.URL, Key: "sk-x"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
p, _ := Find("nov1")
|
||||
if _, err := p.Fetch(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if q, _ := Find("nov1"); q.Chat != srv.URL {
|
||||
t.Errorf("chat changed to %q; the URL was typed without /v1 and serves chat there", q.Chat)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,8 +6,10 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"math"
|
||||
"net/http"
|
||||
"slices"
|
||||
"sort"
|
||||
"strings"
|
||||
@@ -956,7 +958,7 @@ func (p Provider) fetchOne(ctx context.Context) (fetched, error) {
|
||||
// list is fetched beside it, and it is saved with the models
|
||||
base = protoBase
|
||||
if proto != Anthropic {
|
||||
base = p.fixV1(base, u)
|
||||
base = p.fixV1(ctx, base, u)
|
||||
}
|
||||
// the base as it will be asked next time, /v1 and all, so
|
||||
// this side is found again by the fetch after this one
|
||||
@@ -1363,14 +1365,55 @@ func (p Provider) planMedia(ids func(*Region) []string, draws, films bool, serve
|
||||
// version in its path (Ark's …/api/plan/v3, Zhipu's …/api/paas/v4) is
|
||||
// the vendor's API as written and is never given a /v1: no list is asked
|
||||
// for under one there (catalog.FetchAt), and none is added here.
|
||||
func (p Provider) fixV1(base, at string) string {
|
||||
func (p Provider) fixV1(ctx context.Context, base, at string) string {
|
||||
base = strings.TrimRight(strings.TrimSpace(base), "/")
|
||||
if base == "" || at != base+"/v1/models" && at != catalog.RelayOpenAIList(base) || catalog.Versioned(base) {
|
||||
return base
|
||||
}
|
||||
if p.servesAtBase(ctx, base) {
|
||||
// a URL the user typed is kept as typed: a vendor that lists its
|
||||
// models under /v1 but serves chat at the base has no /v1 to add
|
||||
// (JNDliuliuM on X)
|
||||
return base
|
||||
}
|
||||
return p.setV1(base)
|
||||
}
|
||||
|
||||
// servesAtBase reports whether p's OpenAI URL that is base has its
|
||||
// endpoint there: a POST of an empty body to base/chat/completions (or
|
||||
// /responses) that is answered with anything but 404 or 405 — a 400 for the
|
||||
// missing model, a 401 for the key — names a route that exists. A request
|
||||
// that fails to connect says nothing, and is taken as the URL being right.
|
||||
func (p Provider) servesAtBase(ctx context.Context, base string) bool {
|
||||
for _, e := range []struct{ at, end string }{{p.Chat, "/chat/completions"}, {p.Responses, "/responses"}} {
|
||||
if strings.TrimRight(strings.TrimSpace(e.at), "/") != base {
|
||||
continue
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, base+e.end, strings.NewReader("{}"))
|
||||
if err != nil {
|
||||
cancel()
|
||||
return true
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
for k, v := range AuthHeaders(p, Chat) {
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
res, err := p.Do(http.DefaultClient, req)
|
||||
if err != nil {
|
||||
cancel()
|
||||
return true
|
||||
}
|
||||
io.Copy(io.Discard, io.LimitReader(res.Body, 1<<16))
|
||||
res.Body.Close()
|
||||
cancel()
|
||||
if res.StatusCode != http.StatusNotFound && res.StatusCode != http.StatusMethodNotAllowed {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// setV1 is base with the /v1 it was given without, set on p's OpenAI
|
||||
// URLs that were base, as saved; base when p can't be saved.
|
||||
func (p Provider) setV1(base string) string {
|
||||
|
||||
@@ -2,6 +2,7 @@ package provider
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -113,7 +114,7 @@ func TestFixV1KeepsInvalidFile(t *testing.T) {
|
||||
}
|
||||
base := "https://original.example.invalid"
|
||||
p := Provider{ID: "original", Chat: base}
|
||||
if got := p.fixV1(base, base+"/v1/models"); got != base {
|
||||
if got := p.fixV1(context.Background(), base, base+"/v1/models"); got != base {
|
||||
t.Errorf("failed correction returned %q, want %q", got, base)
|
||||
}
|
||||
after, err := os.ReadFile(path)
|
||||
|
||||
Reference in New Issue
Block a user