Files
magpie/internal/provider/test.go
T
yetone c35471cf5b provider: Azure OpenAI, asked at the user's own resource on its v1 API with the key in api-key, for Codex, Claude Code and chat clients alike, and brought in from other apps instead of left out — Azure OpenAI had no preset, and importing an Azure entry from Alma was skipped with "magpie doesn't support Azure OpenAI yet". A new Azure OpenAI preset (vendor) has no URL of its own: its editor asks for the resource's endpoint in a field above the key, focused first, with https://<resource>.openai.azure.com as the placeholder and a hint on where the portal shows it and that the model ids are the deployments' names (en and zh); adding without one says "Your resource's endpoint is needed" and sends nothing, and Save refuses one without an endpoint too. Whatever is pasted — the bare endpoint, …/openai, …/openai/v1/, a classic deployment URL with its api-version, a *.cognitiveservices.azure.com or *.services.ai.azure.com host, or only the resource's name — is saved as https://<host>/openai/v1 for both chat completions and Responses. magpie uses the v1 GA API rather than the classic /openai/deployments/<name>/…?api-version= paths: the paths match OpenAI's, the body's model is the deployment's name, and no api-version is needed (the reasoning is in internal/provider/azure.go). The key goes in api-key, never as a Bearer, which Azure takes as an Entra ID token; nothing sends Authorization or x-api-key there. Deployments named after GPT/o/codex models go to Responses first, as OpenAI's do, and others to chat completions. Chat bodies ask max_completion_tokens instead of max_tokens and drop thinking/enable_thinking, both in passthrough and in Claude Code's translated requests, and the Test probe asks max_completion_tokens as well. The model list is the resource's deployments (/openai/deployments?api-version=2022-12-01), falling back to /openai/v1/models, with the key in api-key; when neither answers, the error says to type the deployment names in as model ids. Before a list, no catalog models are offered, since a model the resource hasn't deployed would be turned away. An Alma azure entry, and any imported entry at a resource's host, becomes the preset at its v1 base; an Alma azure entry with no endpoint is skipped as "it has no Azure OpenAI endpoint", and one at a non-Azure host (an API Management gateway) keeps its URL and is still signed with api-key. Adds the lobe-icons Azure icon. Tested in Go against fake resources (httptest): TestAzureRoutes checks that Codex /v1/responses, a chat client's /v1/chat/completions and Claude Code's /v1/messages on both a GPT-named and another deployment reach /openai/v1/responses or /openai/v1/chat/completions with api-key set, no Authorization or x-api-key, the deployment name as the model, max_completion_tokens and no thinking; TestAzureBase covers each pasted form and the hosts that aren't resources; TestAzurePresetSaved covers the endpoint required, both URLs on the v1 base, api-key-only headers, Native, the probe's URL and body, and nothing available before a list; TestAzureModels covers deployments first, the v1 fallback and a refused key's message; TestImportAzure covers Alma rows with an endpoint, with a bare resource name and with none, plus a classic URL from another app. azure-endpoint.test.cjs, in Chromium and WebKit, en and zh, checks the field, placeholder, hint and focus, the warning without an endpoint with nothing sent and the page not scrolled, the endpoint sent as chat and responses with the key, and that OpenAI's editor has no such field. add-sheet and old-webkit pass. go build -tags nogui (and GOOS=linux, GOOS=windows), go vet and go test -tags nogui ./... pass (TestClaudeAccountNamedAsItsLogin failed under load and passes alone). Not tried against a real Azure resource: whether a key still lists deployments with api-version 2022-12-01, whether Azure's Responses API accepts every field Codex sends, Claude models on Azure AI Foundry's Anthropic endpoint (not wired), Entra ID tokens, and API Management custom domains beyond keeping their URL. Not tried in the real Wails window.
2026-09-30 02:17:15 +08:00

425 lines
13 KiB
Go

package provider
import (
"bytes"
"cmp"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"regexp"
"slices"
"strings"
"sync"
"time"
"github.com/yetone/magpie/internal/catalog"
)
// Result is what a probe of one endpoint came back with.
type Result struct {
Protocol Protocol `json:"protocol"`
OK bool `json:"ok"`
Status int `json:"status,omitempty"`
Millis int64 `json:"ms"`
Model string `json:"model,omitempty"`
Error string `json:"error,omitempty"`
}
// Test sends the smallest possible request to each endpoint the vendor
// serves, signed with a key made for it and asking for a model that key
// sees, and reports what came back.
func (p Provider) Test(ctx context.Context) []Result {
ctx = p.Via(ctx)
if p.Decides() {
return p.testDecide(ctx)
}
p.Fetch(ctx)
var out []Result
for _, proto := range p.Speaks() {
q, ok := p.keyFor(proto)
model := p.testModel(q, proto)
if !ok {
out = append(out, Result{Protocol: proto, Model: model, Error: "no key is on for this endpoint"})
continue
}
url, body := tiny(q, proto, model)
out = append(out, probe(ctx, q, proto, url, q.Prepare([]byte(body)), model, testWait))
}
return out
}
// tiny is the smallest request for model on proto's endpoint, streamed
// to a backend that only streams.
func tiny(q Provider, proto Protocol, model string) (url, body string) {
url, body = tinyBody(q, proto, model)
if q.Account != nil && q.Account.Stream && body != "" {
body = strings.TrimSuffix(body, "}") + `,"stream":true}`
}
return url, body
}
func tinyBody(q Provider, proto Protocol, model string) (url, body string) {
switch proto {
case Chat:
if q.IsBedrock() || q.IsAzure() {
return q.Chat + "/chat/completions", fmt.Sprintf(`{"model":%q,"messages":[{"role":"user","content":"hi"}],"max_completion_tokens":16}`, model)
}
return q.Chat + "/chat/completions", fmt.Sprintf(`{"model":%q,"messages":[{"role":"user","content":"hi"}],"max_tokens":16}`, model)
case Responses:
return q.Responses + "/responses", fmt.Sprintf(`{"model":%q,"input":"hi","max_output_tokens":16}`, model)
case Anthropic:
return q.Anthropic + "/v1/messages", fmt.Sprintf(`{"model":%q,"max_tokens":16,"messages":[{"role":"user","content":"hi"}]}`, model)
}
return "", ""
}
// testWait is how long a probe waits for an answer; drawWait, for a
// picture, which takes an images API longer than a word takes chat.
const (
testWait = 20 * time.Second
drawWait = 2 * time.Minute
)
// drawsOnImages is whether model is asked on p's images API, as the
// gateway draws with it there: chat would be turned away, or answered in
// no shape a chat test knows. OpenRouter draws everything in chat.
func (p Provider) drawsOnImages(model string) bool {
return p.Account == nil && p.Chat != "" && HostOf(p.Chat) != "openrouter.ai" && catalog.ImagesAPI(model)
}
// tinyDrawing is the smallest images request for model: one picture of
// next to nothing, at the lowest quality gpt-image offers, at the size
// the vendor draws by default (the smallest one takes differs by model).
func tinyDrawing(q Provider, model string) (url, body string) {
req := map[string]any{"model": model, "prompt": "a dot", "n": 1}
if m := strings.ToLower(model); strings.Contains(m, "gpt-image") || strings.Contains(m, "chatgpt-image") {
req["quality"] = "low"
}
b, _ := json.Marshal(req)
return strings.TrimRight(q.Chat, "/") + "/images/generations", string(b)
}
// TestModels sends each of models the smallest request, a few at a time,
// on the endpoint it's served on and with a key that sees it: whether each
// answers, not only whether the vendor does. Results are in models' order.
func (p Provider) TestModels(ctx context.Context, models []string) []Result {
ctx = p.Via(ctx)
out := make([]Result, len(models))
sem := make(chan struct{}, 4)
var wg sync.WaitGroup
for i, model := range models {
wg.Add(1)
go func() {
defer wg.Done()
sem <- struct{}{}
defer func() { <-sem }()
out[i] = p.testOne(ctx, model)
}()
}
wg.Wait()
return out
}
func (p Provider) testOne(ctx context.Context, model string) Result {
var protos []Protocol
for _, pr := range p.Speaks() {
if pr == Chat || pr == Responses || pr == Anthropic {
protos = append(protos, pr)
}
}
if len(protos) == 0 {
return Result{Model: model, Error: "this provider can't be sent a test request"}
}
// the endpoint the vendor's list says serves it, else Anthropic's for a
// Claude model, else the one it prefers; an image model draws on the
// chat endpoint's images API
proto := protos[0]
draws := p.drawsOnImages(model) && slices.Contains(protos, Chat)
if draws {
proto = Chat
} else if apis := p.APIs(model); apis != nil {
if i := slices.IndexFunc(protos, func(pr Protocol) bool { return slices.Contains(apis, pr) }); i >= 0 {
proto = protos[i]
}
} else if isClaude(model) && slices.Contains(protos, Anthropic) {
proto = Anthropic
}
// a key made for that endpoint, else one made for any, that sees it
q, ok := p, true
if keys := p.KeysOn(); len(keys) > 0 {
ok = false
pick:
for _, want := range []Protocol{proto, ""} {
for _, k := range keys {
if k.Protocol == want && p.Serves(k, model) {
q, ok = p.WithKey(k), true
break pick
}
}
}
}
if !ok {
return Result{Protocol: proto, Model: model, Error: "no key that's on sees this model"}
}
if draws {
// as the gateway does, one the images API doesn't serve is tried
// in chat, and only its answer said when that fails too
url, body := tinyDrawing(q, model)
r := probe(ctx, q, proto, url, []byte(body), model, drawWait)
if !r.OK && (r.Status == 404 || r.Status == 405) {
url, body := tiny(q, proto, model)
if c := probe(ctx, q, proto, url, q.Prepare([]byte(body)), model, testWait); c.OK {
return c
}
}
return r
}
url, body := tiny(q, proto, model)
return probe(ctx, q, proto, url, q.Prepare([]byte(body)), model, testWait)
}
// keyFor is p using the first key on that works with proto: one made for
// it, else one made for any. A provider without keys is left as it is.
func (p Provider) keyFor(proto Protocol) (Provider, bool) {
keys := p.KeysOn()
if len(keys) == 0 {
return p, true
}
for _, want := range []Protocol{proto, ""} {
for _, k := range keys {
if k.Protocol == want {
return p.WithKey(k), true
}
}
}
return p, false
}
// testModel is the model a probe of proto's endpoint asks for: the first
// exposed one q's key sees, else the first it sees at all — preferring a
// Claude model on the Anthropic endpoint, and one that chats to one that
// draws on an images API.
func (p Provider) testModel(q Provider, proto Protocol) string {
k := q.first()
var pools [][]catalog.Model
if ms := p.Exposed(); len(ms) > 0 {
pools = append(pools, ms)
}
pools = append(pools, p.Available())
for _, want := range []func(string) bool{
func(id string) bool {
if p.drawsOnImages(id) {
return false
}
if apis := p.APIs(id); apis != nil {
return slices.Contains(apis, proto)
}
return proto != Anthropic || isClaude(id)
},
func(string) bool { return true },
} {
for _, pool := range pools {
for _, m := range pool {
if want(m.ID) && (k.Key == "" || p.Serves(k, m.ID)) {
return m.ID
}
}
}
}
return ""
}
func isClaude(id string) bool {
id = strings.ToLower(id)
if i := strings.LastIndex(id, "/"); i >= 0 {
id = id[i+1:]
}
return strings.HasPrefix(id, "claude")
}
// AuthHeaders is how a request to the vendor proves who it is. Anthropic's
// own API wants x-api-key alone, and so does Bedrock's, which turns away a
// request with both (#176); other compatible vendors take either, so both.
// Azure OpenAI takes a key in api-key alone: a Bearer there is an Entra ID
// token, and the key sent as one is turned away.
func AuthHeaders(p Provider, proto Protocol) map[string]string {
if p.Key == "" {
return map[string]string{}
}
if p.IsAzure() {
if proto == Anthropic {
return map[string]string{"x-api-key": p.Key}
}
return map[string]string{"api-key": p.Key}
}
if proto == Anthropic {
if strings.HasSuffix(p.Host(), "anthropic.com") || p.IsBedrock() {
return map[string]string{"x-api-key": p.Key}
}
return map[string]string{"x-api-key": p.Key, "Authorization": "Bearer " + p.Key}
}
return map[string]string{"Authorization": "Bearer " + p.Key}
}
func probe(ctx context.Context, p Provider, proto Protocol, url string, body []byte, model string, wait time.Duration) Result {
r := Result{Protocol: proto, Model: model}
if model == "" {
r.Error = "no model to try: expose one, or refresh the model list"
return r
}
ctx, cancel := context.WithTimeout(ctx, wait)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body))
if err != nil {
r.Error = err.Error()
return r
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("anthropic-version", "2023-06-01")
if p.IsOpenCode() {
req.Header.Set("x-opencode-session", "magpie-test-"+randomUUID())
}
if err := p.Sign(ctx, req, proto, body); err != nil {
r.Error = err.Error()
return r
}
start := time.Now()
res, err := http.DefaultClient.Do(req)
r.Millis = time.Since(start).Milliseconds()
if err != nil {
r.Error = strings.TrimPrefix(err.Error(), "Post \""+url+"\": ")
return r
}
defer res.Body.Close()
r.Status = res.StatusCode
if res.StatusCode >= 200 && res.StatusCode < 300 {
r.OK = true
return r
}
b, _ := io.ReadAll(io.LimitReader(res.Body, 4096))
r.Error = APIError(b, res.Status)
return r
}
// APIError pulls the human message out of an error body when there is one.
// Google's "verify your account" refusal also says what to do about it,
// with the link it gave.
func APIError(b []byte, fallback string) string {
if link, ok := Verification(b); ok {
return VerifyMessage(apiError(b, fallback), link)
}
return apiError(b, fallback)
}
// verifyWords is a refusal asking for the account to be verified, put in
// words rather than as VALIDATION_REQUIRED — or already by VerifyMessage.
var verifyWords = regexp.MustCompile(`(?i)verify your account|account verification required|` + verifyAdvice)
// verifyLink is the link in a message VerifyMessage put in words.
var verifyLink = regexp.MustCompile(verifyAdvice + `: open (https://[^\s"\\]+) in a browser`)
// Verification says whether an error body is Google's refusal of an account
// it wants verified first — Cloud Code Assist's 403 whose details say
// VALIDATION_REQUIRED with a validation_url in their metadata (#152) — and
// the https link to verify it at, when the body has one.
func Verification(b []byte) (link string, ok bool) {
var v struct {
Error struct {
Details []struct {
Reason string `json:"reason"`
Metadata map[string]string `json:"metadata"`
Links []struct {
URL string `json:"url"`
} `json:"links"` // google.rpc.Help
} `json:"details"`
} `json:"error"`
}
if json.Unmarshal(b, &v) == nil {
help := ""
for _, d := range v.Error.Details {
if d.Reason == "VALIDATION_REQUIRED" {
ok = true
link = cmp.Or(link, d.Metadata["validation_url"], d.Metadata["validationUrl"])
}
for _, l := range d.Links {
help = cmp.Or(help, l.URL)
}
}
if ok && link == "" {
link = help
}
}
if !ok && !verifyWords.Match(b) {
return "", false
}
if m := verifyLink.FindSubmatch(b); link == "" && m != nil {
link = string(m[1]) // said already, by VerifyMessage
}
if u, err := url.Parse(link); err != nil || u.Scheme != "https" || u.Host == "" {
link = ""
}
return link, true
}
// verifyAdvice marks a message VerifyMessage has already added to.
const verifyAdvice = "this Google account needs to be verified"
// VerifyMessage is a verification refusal as the agent and the app show it:
// the vendor's words, then what to do.
func VerifyMessage(said, link string) string {
said = strings.TrimSpace(said)
if strings.Contains(said, verifyAdvice) {
return said
}
if link != "" {
return said + " — " + verifyAdvice + ": open " + link + " in a browser signed in to it, verify it, then try again"
}
return said + " — " + verifyAdvice + ": open the Antigravity app (or Gemini CLI) signed in to it and do what it asks, then try again"
}
func apiError(b []byte, fallback string) string {
var v struct {
Error json.RawMessage `json:"error"`
Message string `json:"message"`
Msg string `json:"msg"` // Tencent's (WorkBuddy): {code, msg}
Detail json.RawMessage `json:"detail"` // FastAPI's (TypeSafe)
Errors []struct {
Message string `json:"message"`
} `json:"errors"` // Cloudflare's
}
if json.Unmarshal(b, &v) == nil {
if len(v.Detail) > 0 {
v.Error = v.Detail
}
if len(v.Errors) > 0 && v.Errors[0].Message != "" {
return v.Errors[0].Message
}
var e struct {
Message string `json:"message"`
}
if json.Unmarshal(v.Error, &e) == nil && e.Message != "" {
return e.Message
}
var s string
if json.Unmarshal(v.Error, &s) == nil && s != "" {
return s
}
if m := cmp.Or(v.Message, v.Msg); m != "" {
return m
}
}
// a body in no shape known is shown as it is, cut short, so what the
// vendor said isn't lost; an HTML page says nothing worth showing
s := strings.Join(strings.Fields(string(b)), " ")
if s == "" || strings.HasPrefix(s, "<") {
return fallback
}
if r := []rune(s); len(r) > 300 {
s = string(r[:300]) + "…"
}
return fallback + ": " + s
}