A plugin whose provider's `api` is on a vendor's preset host (a Mistral Vibe plugin's https://api.mistral.ai/v1) got none of what magpie does for that vendor, since its Host() is the plugin's id. An earlier turn's thinking went to the plugin's fetch as reasoning_content, and Mistral turned the whole request away (422 extra_forbidden); the plugin had to rewrite the body itself. provider.VendorHost and VendorPreset now resolve a plugin's declared `api` host to the preset that names it (every Chat, Responses and Anthropic base of every preset and region; local and remote magpie presets excluded, and an api on loopback, a private address, .local or .localhost names no vendor). For every other provider, and a moved subscription, they are Host() and Preset as before. The gateway's vendor checks read them: 38 p.Host() sites, Mistral's, OpenRouter's, DeepSeek's, Kimi's and DashScope's preset checks. Host() stays the plugin's id where calls are kept and shown; requests still go only to the plugin's fetch. Search and the video plan check are unchanged. No community plugin's api is on a preset host today (checked at plugins 7b52e3fc), so none changes. Any plugin gets this with no new field; documented in docs/subsystems/provider-plugins.md and the plugin docs (en, zh, ja). Run (mote): go vet ./... darwin, GOOS=linux -tags nogui, GOOS=windows; go test -count=1 -timeout 45m ./internal/provider ./internal/plugin ./internal/gateway: ok (provider 209s, plugin 182s, gateway 1002s). gofmt -l clean. Broken by hand: pluginVendor resolving nothing (the old behaviour) turns TestPluginVendor red, and TestPluginInFrontOfMistralIsMistral red on both halves: the translated Claude Code turn is answered Mistral's 422 extra_forbidden, and the relayed Chat request carries reasoning_content. Not tried: a real Mistral Vibe key through the reporter's plugin; Windows and Linux runs of the tests (no OS-specific code).
108 lines
3.5 KiB
Go
108 lines
3.5 KiB
Go
package provider
|
|
|
|
import (
|
|
"net"
|
|
"strings"
|
|
"sync"
|
|
)
|
|
|
|
// A plugin's provider in front of a vendor magpie has a preset for is that
|
|
// vendor to the gateway (#5464): what magpie does for the vendor's API —
|
|
// Mistral's thinking parts, DeepSeek's reasoning replay and token field,
|
|
// xAI's schema rules, OpenRouter's shared free pool — is done for the
|
|
// plugin's requests too, before they reach its fetch. The plugin says
|
|
// where its requests go with its provider's `api` (its config hook's
|
|
// cfg.provider[id].api, plugin.Provider.API), the base URL it already
|
|
// gives OpenCode; nothing else is asked of it. A base on no preset's host
|
|
// (the vendor's own app API, a local helper) leaves the plugin as it was.
|
|
|
|
// vendorPresets is each preset's API host to its id, the first preset in
|
|
// the list taking a host two share (Qwen's DashScope ones): every base a
|
|
// preset names, its regions' too. A local server's or another magpie's
|
|
// preset is no vendor's (Ollama's localhost would make every plugin
|
|
// whose fetch talks to a helper on this computer Ollama).
|
|
var vendorPresets = sync.OnceValue(func() map[string]string {
|
|
out := map[string]string{}
|
|
add := func(id string, bases ...string) {
|
|
for _, b := range bases {
|
|
if h := HostOf(b); h != "" && out[h] == "" {
|
|
out[h] = id
|
|
}
|
|
}
|
|
}
|
|
for _, d := range presets {
|
|
if d.Kind == KindLocal || d.Kind == KindRemote {
|
|
continue
|
|
}
|
|
add(d.ID, d.Chat, d.Responses, d.Anthropic)
|
|
for _, r := range d.Regions {
|
|
add(d.ID, r.Chat, r.Responses, r.Anthropic)
|
|
}
|
|
}
|
|
return out
|
|
})
|
|
|
|
// PluginAPI is the base URL the plugin's provider says p's requests go to
|
|
// (its `api`): https://api.mistral.ai/v1 behind a Mistral Vibe plugin. ""
|
|
// for a provider that isn't a plugin's.
|
|
func (p Provider) PluginAPI() string {
|
|
if !p.IsPlugin() {
|
|
return ""
|
|
}
|
|
return p.Account.plugin.API
|
|
}
|
|
|
|
// pluginVendor is the preset, and its host, of the vendor a plugin's
|
|
// provider says its requests go to; "" for any other provider, a moved
|
|
// subscription (whose Host is the built-in's already), and a plugin whose
|
|
// base is on no preset's host or on this computer or network.
|
|
func (p Provider) pluginVendor() (preset, host string) {
|
|
if !p.IsPlugin() || p.Account.moved {
|
|
return "", ""
|
|
}
|
|
h := HostOf(p.PluginAPI())
|
|
if h == "" || onThisNetwork(h) {
|
|
return "", ""
|
|
}
|
|
if id := vendorPresets()[h]; id != "" {
|
|
return id, h
|
|
}
|
|
return "", ""
|
|
}
|
|
|
|
// onThisNetwork is a host on this computer or its network.
|
|
func onThisNetwork(h string) bool {
|
|
if s, _, err := net.SplitHostPort(h); err == nil {
|
|
h = s
|
|
}
|
|
h = strings.Trim(h, "[]")
|
|
if h == "localhost" || strings.HasSuffix(h, ".localhost") || strings.HasSuffix(h, ".local") {
|
|
return true
|
|
}
|
|
ip := net.ParseIP(h)
|
|
return ip != nil && (ip.IsLoopback() || ip.IsPrivate() || ip.IsLinkLocalUnicast() || ip.IsUnspecified())
|
|
}
|
|
|
|
// VendorHost is the host of the vendor's API p's requests are shaped for:
|
|
// Host(), and for a plugin's provider in front of a vendor magpie has a
|
|
// preset for, that vendor's API host (api.mistral.ai), where Host() is
|
|
// the plugin's id. The gateway's checks of whose API a request goes to
|
|
// read this; Host() stays where calls are kept and shown.
|
|
func (p Provider) VendorHost() string {
|
|
if _, h := p.pluginVendor(); h != "" {
|
|
return h
|
|
}
|
|
return p.Host()
|
|
}
|
|
|
|
// VendorPreset is the preset of the vendor p's requests are shaped for:
|
|
// p.Preset, and for a plugin's provider in front of a vendor magpie has a
|
|
// preset for, that preset (mistral).
|
|
func (p Provider) VendorPreset() string {
|
|
if p.Preset != "" {
|
|
return p.Preset
|
|
}
|
|
id, _ := p.pluginVendor()
|
|
return id
|
|
}
|