cli: provider set resolve.<host>=…, the editor's Connect to from the CLI (quantum_lsfc on X)

v0.1.4012 added Advanced › Connect to (Provider.Resolve) in the GUI
only; the reporter asked for it from the CLI. `magpie provider set <id>
resolve.<host>=<IP or dns:name>[,…]` sets a host's connect addresses,
an empty value takes the host out, and `resolve=` clears them all (any
other resolve= value is refused, never saved). provider.Save checks
them as the GUI's do (netproxy.CheckResolve, TakesResolve), so a bad
place or a vendor's own host is refused with nothing written. `magpie
provider <id>` lists them as "connect". Usage and docs/reference.md say
how.

Run on mote: go test -count=1 ./internal/cli ok (166s);
TestProviderSetResolve ok; go vet ./internal/cli for darwin, linux
(-tags nogui) and windows ok; gofmt -l clean. A nogui build in a
sandbox HOME on mote: provider add + set resolve.api.corp.example=
10.0.0.5,dns:gw.corp.example saved and shown as connect; resolve=oops
and a bad place refused; resolve= cleared it; provider set --help
printed usage.
Broken by hand: not deleting the host on an empty value, and taking a
non-empty resolve=, each turn TestProviderSetResolve red.
Rebased on origin/main 9572a2b4c and run again on mote: the resolve,
Pair and Help tests of internal/cli ok.
Not tried: a request actually dialled through the rule from the CLI
(the dial is the GUI's, covered by netproxy's and provider's tests),
nor the reporter's managed network; Windows and Linux beyond go vet.
This commit is contained in:
yetone
2026-11-13 16:39:09 +08:00
parent a4cd61f77f
commit 0fda3a1c12
3 changed files with 136 additions and 0 deletions
+10
View File
@@ -392,6 +392,16 @@ agent's; any other of yours is always sent. The beta of the agent's own
sign-in (`oauth-2025-04-20`, which Claude Code signed in to claude.ai asks)
never goes, as the sign-in never does; one you set yourself does.
`magpie provider set <id> resolve.<host>=<place>[,<place>…]` connects a
host name of the provider's own base URLs to the places listed, as curl's
`--resolve` does (the provider editor's Advanced › Connect to): a place is
an IP address (with a port if it has its own) or `dns:<name>`, looked up at
each connection, tried in order, and the URL's name stays for TLS and Host.
An empty value takes that host out; `resolve=` clears them all. Only a
provider you added with a base URL of its own, or a preset you pointed at a
server of your own, takes them. `magpie provider <id>` lists them as
`connect`.
A key+URL provider is sent the User-Agent the agent sent magpie, as it
sent it, on every API (chat, Anthropic, Responses, Gemini, count_tokens,
embeddings, images, videos): a coding plan that serves only the agents it
+89
View File
@@ -0,0 +1,89 @@
package cli
import (
"slices"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// The provider editor's Advanced › Connect to from the CLI (quantum_lsfc on
// X: "是否可以支持cli去配置"): resolve.<host>= sets a host's connect
// addresses on a saved provider, an empty value takes the host out,
// resolve= clears them all, and a bad place, a made-up resolve= value or a
// vendor's preset is refused with nothing saved.
func TestProviderSetResolve(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
t.Setenv("XDG_DATA_HOME", t.TempDir())
t.Setenv("APPDATA", t.TempDir())
t.Setenv("LOCALAPPDATA", t.TempDir())
if err := provider.Save(provider.Provider{ID: "corp", Name: "Corp", Chat: "https://api.corp.example/v1", Key: "k", Models: []string{"m"}}); err != nil {
t.Fatal(err)
}
saved := func() map[string][]string {
t.Helper()
p, err := provider.Find("corp")
if err != nil {
t.Fatal(err)
}
return p.Resolve
}
set := func(pairs ...string) error {
return providerCmd(append([]string{"provider", "set", "corp"}, pairs...))
}
if err := set("resolve.API.corp.example=10.0.0.5, dns:gw.corp.example,10.0.0.5"); err != nil {
t.Fatal(err)
}
if got := saved()["api.corp.example"]; !slices.Equal(got, []string{"10.0.0.5", "dns:gw.corp.example"}) {
t.Fatalf("saved %v", saved())
}
if err := set("resolve.other.corp.example=[fd00::5]:8443"); err != nil {
t.Fatal(err)
}
if len(saved()) != 2 {
t.Fatalf("a second host: %v", saved())
}
if err := set("resolve.other.corp.example="); err != nil {
t.Fatal(err)
}
if r := saved(); len(r) != 1 || r["other.corp.example"] != nil {
t.Fatalf("an empty value kept the host: %v", r)
}
for _, bad := range [][]string{
{"resolve.api.corp.example=not an address"},
{"resolve.10.0.0.9=10.0.0.5"},
{"resolve=10.0.0.5"},
{"resolve=--help"},
} {
if err := set(bad...); err == nil {
t.Fatalf("%v was taken", bad)
}
if got := saved()["api.corp.example"]; !slices.Equal(got, []string{"10.0.0.5", "dns:gw.corp.example"}) || len(saved()) != 1 {
t.Fatalf("%v changed what was saved: %v", bad, saved())
}
}
if err := set("resolve="); err != nil {
t.Fatal(err)
}
if r := saved(); len(r) != 0 {
t.Fatalf("resolve= kept %v", r)
}
// a vendor's own host is never moved
if err := provider.Save(provider.Provider{ID: "deepseek", Name: "DeepSeek", Preset: "deepseek", Chat: "https://api.deepseek.com/v1", Key: "k", Models: []string{"deepseek-chat"}}); err != nil {
t.Fatal(err)
}
err := providerCmd([]string{"provider", "set", "deepseek", "resolve.api.deepseek.com=10.0.0.5"})
if err == nil || !strings.Contains(err.Error(), "connect address") {
t.Fatalf("a preset at the vendor's host: %v", err)
}
}
+37
View File
@@ -55,6 +55,9 @@ const providerUsage = `usage:
keeps the cache); OpenAI's API and a ChatGPT (Codex) account do so by
default; additional-tools=off puts it with the other tools,
additional-tools=default is magpie's
resolve.<host>=<IP>[,dns:<name>…] connects a host of its base URLs there
(curl --resolve: the URL's name stays for TLS and Host); empty takes the
host's out, resolve= clears them all
image-files=off sends DeepSeek's API an agent's images inline on every
turn; image-files=on, the default, uploads each once (for a day)
and names it by its file id
@@ -178,6 +181,9 @@ const providerUsage = `usage:
magpie provider set my-relay models.url=https://relay.example.com/api/models catalog=
magpie provider set volcengine list=off models=ep-20261108-abcde
(its model list is never asked for: only the models given by id are served)
magpie provider set my-relay resolve.relay.example.com=10.0.0.5,dns:gw.corp.example
(Advanced › Connect to: connects to 10.0.0.5, then gw.corp.example's
address, while TLS and Host still say relay.example.com)
magpie provider set my-relay search=yes
(the relay answers Claude Code's WebSearch and Codex's web_search itself:
those go to it as sent, not through magpie's own search)
@@ -1042,6 +1048,9 @@ func showProviderAsking(p provider.Provider, ask bool) error {
for _, k := range slices.Sorted(maps.Keys(p.Headers)) {
kv("header", k+": "+muted.Render(p.Headers[k]))
}
for _, h := range slices.Sorted(maps.Keys(p.Resolve)) {
kv("connect", h+muted.Render(" → ")+strings.Join(p.Resolve[h], ", "))
}
if d := p.RefreshInterval(); d > 0 {
kv("refresh", "every "+p.RefreshSchedule()+muted.Render(" the gateway fetches its list by itself"))
}
@@ -1442,6 +1451,12 @@ func applyPairs(p *provider.Provider, pairs []string) error {
if err := setContext(p, "*", v); err != nil {
return err
}
case "resolve":
// resolve= takes every connect address out
if strings.TrimSpace(v) != "" {
return fmt.Errorf("resolve.<host>=<IP or dns:name>[,…] gives a host's connect addresses; resolve= alone clears them all, not %q", v)
}
p.Resolve = nil
case "family", "tag":
p.Family = strings.TrimSpace(v)
case "routing", "strategy":
@@ -1490,6 +1505,28 @@ func applyPairs(p *provider.Provider, pairs []string) error {
p.Headers[name] = v
continue
}
if len(k) > len("resolve.") && strings.EqualFold(k[:len("resolve.")], "resolve.") {
// resolve.<host>=<IP or dns:name>[,…] connects the host
// of its base URLs to those places, as curl --resolve
// (quantum_lsfc on X); empty takes the host's out. Save
// checks the host and each place (netproxy.CheckResolve)
host := strings.ToLower(strings.TrimSuffix(strings.TrimSpace(k[len("resolve."):]), "."))
delete(p.Resolve, host)
var places []string
for pl := range strings.SplitSeq(v, ",") {
if pl = strings.TrimSpace(pl); pl != "" {
places = append(places, pl)
}
}
if len(places) == 0 {
continue
}
if p.Resolve == nil {
p.Resolve = map[string][]string{}
}
p.Resolve[host] = places
continue
}
if len(k) > len("context.") && strings.EqualFold(k[:len("context.")], "context.") {
if err := setContext(p, k[len("context."):], v); err != nil {
return err