Files
magpie/internal/provider/importurl.go
T
yetone 209dc69b59 providers: Tencent Cloud is one preset, its regions Plan · China, Pay as you go · China and Pay as you go · Global (Discord, Jorben: Tencent Cloud's Token Plan, TokenHub and TokenHub China were three tiles of one vendor) — tencent-token-plan, tencent-tokenhub-cn and tencent-tokenhub become tencent-cloud's regions plan, cn and intl, as Xiaomi MiMo's are, each made as its own preset made it: the plan's /plan endpoints with no Responses, its own models and no catalog; TokenHub's hosts with Responses, models.dev's tencent-tokenhub and its own keys page and docs. A region now carries its Website, Catalog and Models, and a saved provider's region is found by its endpoints (regionOf, by exact URL before path, since TokenHub China and global share their paths): normalize gives it that region's keys page, docs and catalog (a catalog the user picked is kept), and planModels its models. The old ids are aliases, as qianfan-token-plan is of baidu-qianfan: Preset and FromPreset take them, FromPreset builds at their region, normalize rewrites a saved provider's preset to tencent-cloud, and a provider's own id never changes, so its key, models, routing groups, usage and prices stay where they were; magpie://import links by an old id land at its region, and region= now also gives a region's docs and catalog. Imports from other apps match a region's endpoints. The editor's docs link follows the region picked with the segmented control, en and zh ("按量 · 中国", "按量 · 国际"); the add sheet has one Tencent Cloud tile; the site's import docs list tencent-cloud with its regions and import.html still names the old ids. TestTencentCloud (each region, old ids, FromPreset, imports, import links) and TestTencentCloudOldPresetsKeepWorking (a providers.json saved by an older magpie under all three old presets, with a routing group over them, read, re-saved as the editor saves and switched between regions) fail on the old code; TestTencentTokenHubRoutes routes through old ids and tencent-cloud alike. tencent-cloud.test.cjs (WebKit en and zh: one tile, the three regions, no select, the docs and Get a key links following the region, an old id's provider opening at its region and saved under its id) fails on the old app.js; add-sheet, zhipu-team, system-one, strip-scrollbar and click-scroll pass (Chromium doesn't launch in this sandbox). Not tried with a Tencent account. go vet, builds (darwin, GOOS=linux, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass.
2026-10-04 19:38:14 +08:00

198 lines
6.3 KiB
Go

package provider
import (
"cmp"
"errors"
"net"
"net/url"
"slices"
"strings"
)
// Import links let a vendor or a relay hand its users a ready-made provider:
//
// magpie://import?preset=deepseek&key=sk-…
// magpie://import?name=My%20Relay&chat=https://relay.example/v1&key=sk-…
//
// Parameters (every value URL-encoded):
//
// preset a preset id (magpie presets); the preset's endpoints are used
// region with a preset that has regions, which one
// name the provider's name; required without a preset
// id its id; derived from the name when absent
// key the API key; the user can paste one when absent
// chat OpenAI Chat Completions base URL (…/v1)
// responses OpenAI Responses base URL (…/v1)
// anthropic Anthropic Messages base URL (the root)
// models model ids to show agents, comma separated
// catalog models.dev provider id, for names and reasoning levels
// website the vendor's site; keys: where keys are made
// icon an https picture of the vendor's own, fetched only after the
// user confirms; falls back to the generic mark when absent
//
// A link never saves anything by itself: the app shows what it would add
// and the user says yes.
// Scheme is the URL scheme magpie registers with the system.
const Scheme = "magpie"
// ParseImport reads an import link into the provider it describes.
func ParseImport(link string) (Provider, error) {
u, err := url.Parse(strings.TrimSpace(link))
if err != nil || !strings.EqualFold(u.Scheme, Scheme) {
return Provider{}, errors.New("not a magpie:// link")
}
action := u.Host
if action == "" { // magpie:import?… or magpie:///import?…
action = strings.Trim(u.Opaque+u.Path, "/")
}
if action != "import" {
return Provider{}, errorf("magpie://%s is not something magpie knows; links start magpie://import?", action)
}
q := u.Query()
get := func(k string) string { return strings.TrimSpace(q.Get(k)) }
var p Provider
if id := get("preset"); id != "" {
if p, err = FromPreset(strings.ToLower(id)); err != nil {
return Provider{}, err
}
if r := get("region"); r != "" {
found := false
pr := Preset(p.Preset)
for _, reg := range pr.Regions {
if reg.ID == r {
p.Chat, p.Responses, p.Anthropic, found = reg.Chat, reg.Responses, reg.Anthropic, true
// a region with a key page of its own (Qianfan's pay
// as you go, on the IAM page) shows it before the
// provider is saved
if reg.KeysURL != "" {
p.KeysURL = reg.KeysURL
}
// and its docs and prices (Tencent Cloud's TokenHub)
p.Website = cmp.Or(reg.Website, pr.Website)
if slices.ContainsFunc(pr.Regions, func(x Region) bool { return x.Catalog != "" }) {
p.Catalog = cmp.Or(reg.Catalog, pr.Catalog)
}
}
}
if !found {
return Provider{}, errorf("%s has no region %q", p.Name, r)
}
}
if n := get("name"); n != "" {
p.Name = n
}
} else {
p = Provider{Name: get("name"), Catalog: strings.ToLower(get("catalog"))}
if p.Name == "" {
return Provider{}, errors.New("the link names no provider: it needs preset= or name=")
}
for k, dst := range map[string]*string{"chat": &p.Chat, "responses": &p.Responses, "anthropic": &p.Anthropic} {
if *dst, err = endpoint(k, get(k)); err != nil {
return Provider{}, err
}
}
if p.Chat == "" && p.Responses == "" && p.Anthropic == "" {
return Provider{}, errors.New("the link gives no base URL: it needs chat=, responses= or anthropic=")
}
if p.Catalog != "" && p.Catalog != Slug(p.Catalog) {
p.Catalog = ""
}
if p.Catalog != "" {
p.Icon = IconForCatalog(p.Catalog)
}
p.Website = page(get("website"))
p.KeysURL = page(get("keys"))
}
if raw := get("icon"); raw != "" {
u, err := iconURL(raw)
if err != nil {
return Provider{}, err
}
p.IconURL = u
}
if id := get("id"); id != "" {
p.ID = Slug(id)
} else if get("preset") == "" {
p.ID = Slug(p.Name)
}
if p.ID == "" {
return Provider{}, errors.New("the link's name has no letters or digits to make an id from")
}
if p.ID == "magpie" {
return Provider{}, errors.New(`"magpie" is what agents call the gateway itself; the link needs another id`)
}
if len(p.Name) > 80 {
p.Name = p.Name[:80]
}
p.Key = get("key")
if len(p.Key) > 4096 || strings.ContainsFunc(p.Key, func(r rune) bool { return r < ' ' || r == 0x7f }) {
return Provider{}, errors.New("the link's key is not a key")
}
for _, m := range strings.Split(q.Get("models"), ",") {
if m = strings.TrimSpace(m); m != "" && len(p.Models) < 200 {
p.Models = append(p.Models, m)
}
}
return p, nil
}
// endpoint accepts an https base URL, or plain http to this machine or the
// local network, where model servers usually run without TLS.
func endpoint(name, raw string) (string, error) {
if raw == "" {
return "", nil
}
u, err := url.Parse(raw)
if err != nil || u.Host == "" || u.User != nil || u.RawQuery != "" || u.Fragment != "" {
return "", errorf("the link's %s= is not a base URL: %q", name, raw)
}
switch u.Scheme {
case "https":
case "http":
if !local(u.Hostname()) {
return "", errorf("the link's %s= must use https: %q", name, raw)
}
default:
return "", errorf("the link's %s= must use https: %q", name, raw)
}
return strings.TrimRight(u.String(), "/"), nil
}
func local(host string) bool {
if host == "localhost" || strings.HasSuffix(host, ".local") {
return true
}
ip := net.ParseIP(host)
return ip != nil && (ip.IsLoopback() || ip.IsPrivate())
}
// page keeps a web page link only when it is https.
func page(raw string) string {
if u, err := url.Parse(raw); err == nil && u.Scheme == "https" && u.Host != "" && u.User == nil {
return u.String()
}
return ""
}
// iconURL accepts the picture a link names: an https URL on a public host,
// which the app fetches only after the user confirms the import. A plain
// http or a bare host is rejected rather than guessed at, since this is the
// one parameter that makes magpie reach out on the link's behalf.
func iconURL(raw string) (string, error) {
u, err := url.Parse(raw)
if err != nil || u.Host == "" || u.User != nil || u.Fragment != "" {
return "", errorf("the link's icon= is not a picture URL: %q", raw)
}
if u.Scheme != "https" {
return "", errorf("the link's icon= must use https: %q", raw)
}
if local(u.Hostname()) {
return "", errorf("the link's icon= must be on a public host: %q", raw)
}
return u.String(), nil
}