diff --git a/internal/gui/providers.go b/internal/gui/providers.go index 1a7c42959..ddf20ad3e 100644 --- a/internal/gui/providers.go +++ b/internal/gui/providers.go @@ -167,6 +167,10 @@ type providerJSON struct { // the server certificate its own hosts are taken with alone, their // certificate otherwise unchecked (provider.ServerPin) ServerPin string `json:"serverPin,omitempty"` + // Resolve: the connect addresses of its hosts (provider.Resolve); + // TakesResolve: it may have them + Resolve map[string][]string `json:"resolve,omitempty"` + TakesResolve bool `json:"takesResolve,omitempty"` // TakesServerPin: an https:// address of the user's own to pin // (provider.PinHosts), a preset's pointed at their own server too TakesServerPin bool `json:"takesServerPin,omitempty"` @@ -891,7 +895,7 @@ func providerInfoWith(p provider.Provider, ls provider.Listing, agents []agentUs ID: p.ID, Name: p.Name, Icon: p.Icon, IconFrom: p.IconFrom, Preset: p.Preset, Host: p.Host(), Chat: p.Chat, Responses: p.Responses, Anthropic: p.Anthropic, Gemini: p.Gemini, Decide: p.Decide, BaseAPI: p.BaseAPI, ModelTest: p.ModelTest(), TestsAs: p.TestClients(), TestPicks: p.TestPicks(), DecideTest: p.AsksDecideModels(), MarksKinds: p.MarksKinds(), Catalog: p.Catalog, Website: p.Website, KeysURL: p.KeysURL, - Proxy: p.Proxy, AccountProxies: p.AccountProxies, AccountModels: p.AccountModels, AccountCaps: p.AccountCaps, AccountWindowCaps: p.AccountWindowCaps, AccountWindowAmounts: p.AccountWindowAmounts, AccountNames: p.AccountNames, Headers: p.Headers, ExtraBody: p.ExtraBody, ReplyHeaders: p.ReplyHeaders, PassHeaders: p.PassHeaders, Searches: p.Searches, Cline: p.ClinePinnable(), PinUpstream: p.PinUpstream, Unredacted: p.Unredacted, CountFree: p.CountFree, Billing: p.Billing, BillingKind: p.BillingKind(), InsecureTLS: p.InsecureTLS, TakesInsecureTLS: p.TakesInsecureTLS(), HTTP1: p.HTTP1, ClientCert: tilde(p.ClientCert), ClientKey: tilde(p.ClientKey), ServerCA: tilde(p.ServerCA), TakesClientCert: p.TakesClientCert(), ServerPin: p.ServerPin, TakesServerPin: p.TakesServerPin(), Socket: p.Socket, TakesSocket: p.TakesSocket(), LimitRestOff: p.LimitRestOff, UpstreamProviders: p.UpstreamProviders, ImageFilesOff: p.ImageFilesOff, TakesImageFiles: p.TakesImageFiles(), NativeCompaction: p.NativeCompaction, CarriesCompaction: p.CarriesCompaction(), PassUserAgent: !p.MagpieUserAgent, OwnClient: p.OwnClient(), PassErrors: p.PassErrors, TakesPassErrors: p.TakesPassErrors(), AuthHeader: p.AuthHeader, BothAuth: p.BothAuth(), BalanceURL: p.BalanceURL, BalancePath: p.BalancePath, BalanceMethod: p.BalanceMethod, NoBalanceProbe: p.NoBalanceProbe, ModelsURL: p.ModelsURL, ListOff: p.ListsOff(), TakesListOff: p.Account == nil && !p.IsPlugin() && !p.DecideOnly(), TakesModelHeaders: p.Account == nil, DecideAPIKind: p.DecideAPIKind(), + Proxy: p.Proxy, AccountProxies: p.AccountProxies, AccountModels: p.AccountModels, AccountCaps: p.AccountCaps, AccountWindowCaps: p.AccountWindowCaps, AccountWindowAmounts: p.AccountWindowAmounts, AccountNames: p.AccountNames, Headers: p.Headers, ExtraBody: p.ExtraBody, ReplyHeaders: p.ReplyHeaders, PassHeaders: p.PassHeaders, Searches: p.Searches, Cline: p.ClinePinnable(), PinUpstream: p.PinUpstream, Unredacted: p.Unredacted, CountFree: p.CountFree, Billing: p.Billing, BillingKind: p.BillingKind(), InsecureTLS: p.InsecureTLS, TakesInsecureTLS: p.TakesInsecureTLS(), HTTP1: p.HTTP1, ClientCert: tilde(p.ClientCert), ClientKey: tilde(p.ClientKey), ServerCA: tilde(p.ServerCA), TakesClientCert: p.TakesClientCert(), ServerPin: p.ServerPin, TakesServerPin: p.TakesServerPin(), Resolve: p.Resolve, TakesResolve: p.TakesResolve(), Socket: p.Socket, TakesSocket: p.TakesSocket(), LimitRestOff: p.LimitRestOff, UpstreamProviders: p.UpstreamProviders, ImageFilesOff: p.ImageFilesOff, TakesImageFiles: p.TakesImageFiles(), NativeCompaction: p.NativeCompaction, CarriesCompaction: p.CarriesCompaction(), PassUserAgent: !p.MagpieUserAgent, OwnClient: p.OwnClient(), PassErrors: p.PassErrors, TakesPassErrors: p.TakesPassErrors(), AuthHeader: p.AuthHeader, BothAuth: p.BothAuth(), BalanceURL: p.BalanceURL, BalancePath: p.BalancePath, BalanceMethod: p.BalanceMethod, NoBalanceProbe: p.NoBalanceProbe, ModelsURL: p.ModelsURL, ListOff: p.ListsOff(), TakesListOff: p.Account == nil && !p.IsPlugin() && !p.DecideOnly(), TakesModelHeaders: p.Account == nil, DecideAPIKind: p.DecideAPIKind(), Ready: p.Ready(), Chosen: p.PicksOf(avail), Agents: make([]providerAgent, 0, len(agents)), Fallback: p.Fallback, Routing: p.Routing, Sink: p.Sink, UseFirst: p.UseFirst, NoStandIn: p.NoStandIn, Affinity: p.Affinity, KeepLogin: p.KeepLogin, KeepLoginAs: p.KeepLoginAs, SwitchOnly: p.SwitchOnly, RestartApp: p.RestartApp, Self: p.SelfAnswered(), KeepSelf: p.KeepSelf, ThisDevice: p.ThisDevice, CapStop: p.CapStop, CapCareful: p.CapCareful, Unlisted: p.Unlisted, NoModels: p.ServesNone(), BestInFamily: p.BestInFamily && len(p.Models) == 0 && !p.NoModels, Off: p.Off, Contexts: p.Contexts, MaxConcurrency: p.MaxConcurrency, PluginConcurrency: p.PluginConcurrency(), MaxAccounts: p.MaxAccounts, PluginAccounts: p.PluginAtOnce(), PriceRate: p.PriceRate, @@ -1732,6 +1736,9 @@ func providerRoutes(mux *http.ServeMux, w Windows) { // hosts are taken with alone (provider.ServerPin), "" to check // them as usual; a save that leaves it out keeps it ServerPin *string `json:"serverPin"` + // Resolve is the connect addresses of its hosts + // (provider.Resolve); left out keeps what it had + Resolve *map[string][]string `json:"resolve"` // HTTP1 is whether its own hosts are spoken HTTP/1.1 to; a // save that leaves it out keeps it HTTP1 *bool `json:"http1"` @@ -2180,7 +2187,7 @@ func providerRoutes(mux *http.ServeMux, w Windows) { if req.InsecureTLS != nil { in.InsecureTLS = *req.InsecureTLS } - in = withCerts(in, nil, req.ClientCert, req.ClientKey, req.ServerCA, req.ServerPin) + in = withCerts(in, nil, req.ClientCert, req.ClientKey, req.ServerCA, req.ServerPin, req.Resolve) if req.HTTP1 != nil { in.HTTP1 = *req.HTTP1 } @@ -2234,7 +2241,7 @@ func providerRoutes(mux *http.ServeMux, w Windows) { } else if old != nil { in.InsecureTLS = old.InsecureTLS } - in = withCerts(in, old, req.ClientCert, req.ClientKey, req.ServerCA, req.ServerPin) + in = withCerts(in, old, req.ClientCert, req.ClientKey, req.ServerCA, req.ServerPin, req.Resolve) if req.HTTP1 != nil { in.HTTP1 = *req.HTTP1 } else if old != nil { @@ -2844,7 +2851,7 @@ func providerRoutes(mux *http.ServeMux, w Windows) { } saved := *p if req.Typed { - q := withCerts(typed(*p, in, req.Proxy, req.InsecureTLS, req.HTTP1), p, req.ClientCert, req.ClientKey, req.ServerCA, req.ServerPin) + q := withCerts(typed(*p, in, req.Proxy, req.InsecureTLS, req.HTTP1), p, req.ClientCert, req.ClientKey, req.ServerCA, req.ServerPin, req.Resolve) p = &q } if len(req.Test) > 0 { @@ -2881,7 +2888,7 @@ func providerRoutes(mux *http.ServeMux, w Windows) { } p = *saved } - p = withCerts(typed(p, in, req.Proxy, req.InsecureTLS, req.HTTP1), &p, req.ClientCert, req.ClientKey, req.ServerCA, req.ServerPin) + p = withCerts(typed(p, in, req.Proxy, req.InsecureTLS, req.HTTP1), &p, req.ClientCert, req.ClientKey, req.ServerCA, req.ServerPin, req.Resolve) if strings.TrimSpace(in.Decide) != "" { // a decision API is asked its smallest question, at // POST …/systemone (#647) @@ -2972,7 +2979,7 @@ func providerRoutes(mux *http.ServeMux, w Windows) { if old, err := provider.Find(in.ID); err == nil && in.ID != "" { p = *old } - p = withCerts(typed(p, in, req.Proxy, req.InsecureTLS, req.HTTP1), &p, req.ClientCert, req.ClientKey, req.ServerCA, req.ServerPin) + p = withCerts(typed(p, in, req.Proxy, req.InsecureTLS, req.HTTP1), &p, req.ClientCert, req.ClientKey, req.ServerCA, req.ServerPin, req.Resolve) ctx, cancel := context.WithTimeout(r.Context(), 15*time.Second) defer cancel() seen, err := p.PeekServerCert(ctx) @@ -2993,7 +3000,7 @@ func providerRoutes(mux *http.ServeMux, w Windows) { if p.Name == "" { p.Name = cmp.Or(strings.TrimSpace(in.Name), "the vendor") } - p = withCerts(typed(p, in, req.Proxy, req.InsecureTLS, req.HTTP1), &p, req.ClientCert, req.ClientKey, req.ServerCA, req.ServerPin) + p = withCerts(typed(p, in, req.Proxy, req.InsecureTLS, req.HTTP1), &p, req.ClientCert, req.ClientKey, req.ServerCA, req.ServerPin, req.Resolve) ctx, cancel := context.WithTimeout(r.Context(), 20*time.Second) defer cancel() all, err := p.ListAll(ctx) @@ -3037,7 +3044,7 @@ func providerRoutes(mux *http.ServeMux, w Windows) { defer cancel() ask := *p if req.Typed { - ask = withCerts(typed(*p, in, req.Proxy, req.InsecureTLS, req.HTTP1), p, req.ClientCert, req.ClientKey, req.ServerCA, req.ServerPin) + ask = withCerts(typed(*p, in, req.Proxy, req.InsecureTLS, req.HTTP1), p, req.ClientCert, req.ClientKey, req.ServerCA, req.ServerPin, req.Resolve) } ms, dropped, err := ask.Refetch(ctx) if err != nil { @@ -3582,10 +3589,10 @@ func typed(p, in provider.Provider, proxy *string, insecureTLS, http1 *bool) pro } // withCerts is p with the client certificate, key and server CA a save -// or an unsaved test typed (#5596), each one left out kept from old (nil: +// or an unsaved test typed (#5596; the connect addresses, Resolve, with them), each one left out kept from old (nil: // none); a ~ is taken as home, as a save keeps it, so an unsaved test // reads the same files. -func withCerts(p provider.Provider, old *provider.Provider, cert, key, ca, pin *string) provider.Provider { +func withCerts(p provider.Provider, old *provider.Provider, cert, key, ca, pin *string, resolve *map[string][]string) provider.Provider { var kept provider.Provider if old != nil { kept = *old @@ -3607,6 +3614,10 @@ func withCerts(p provider.Provider, old *provider.Provider, cert, key, ca, pin * if pin != nil { p.ServerPin = strings.TrimSpace(*pin) // Save keeps it as netproxy.NormalPin has it, or says why } + p.Resolve = kept.Resolve + if resolve != nil { + p.Resolve = *resolve // Save keeps it as netproxy.CheckResolve has it, or says why + } return p } diff --git a/internal/gui/resolve_test.go b/internal/gui/resolve_test.go new file mode 100644 index 000000000..4a1c025d7 --- /dev/null +++ b/internal/gui/resolve_test.go @@ -0,0 +1,70 @@ +package gui + +import ( + "net/http" + "net/http/httptest" + "os" + "reflect" + "strings" + "testing" + + "github.com/yetone/magpie/internal/provider" +) + +// quantum_lsfc on X: the editor's connect addresses are saved on a +// provider of the user's, normalised as netproxy.CheckResolve keeps them, +// open as saved, are kept by a Save that leaves them out, cleared by an +// empty one, and refused when they can't be used. +func TestProviderSaveResolve(t *testing.T) { + t.Setenv("HOME", t.TempDir()) + t.Setenv("USERPROFILE", os.Getenv("HOME")) + t.Setenv("XDG_CONFIG_HOME", t.TempDir()) + t.Setenv("XDG_CACHE_HOME", t.TempDir()) + mux := http.NewServeMux() + providerRoutes(mux, nil) + post := func(body string) *httptest.ResponseRecorder { + t.Helper() + w := httptest.NewRecorder() + mux.ServeHTTP(w, httptest.NewRequest("POST", "/api/provider/save", strings.NewReader(body))) + return w + } + saved := func() map[string][]string { + t.Helper() + p, err := provider.Find("gw") + if err != nil { + t.Fatal(err) + } + info := providerInfo(*p, nil) + if !info.TakesResolve || !reflect.DeepEqual(info.Resolve, p.Resolve) { + t.Fatalf("the editor is shown %v (takes %v), saved %v", info.Resolve, info.TakesResolve, p.Resolve) + } + return p.Resolve + } + base := `"id":"gw","name":"Gateway","key":"sk-g","anthropic":"https://llm.corp.example/v1","icon":"generic"` + if w := post(`{` + base + `,"new":true,"resolve":{" LLM.corp.example ":["dns:gw.managed.test","10.0.0.1"]}}`); w.Code != 200 { + t.Fatalf("%d %s", w.Code, w.Body.String()) + } + want := map[string][]string{"llm.corp.example": {"dns:gw.managed.test", "10.0.0.1"}} + if got := saved(); !reflect.DeepEqual(got, want) { + t.Fatalf("saved %v, want %v", got, want) + } + keep := `{"id":"gw","from":"gw","name":"Gateway","anthropic":"https://llm.corp.example/v1","icon":"generic","models":[],"headers":{}` + if w := post(keep + `}`); w.Code != 200 { + t.Fatalf("%d %s", w.Code, w.Body.String()) + } + if got := saved(); !reflect.DeepEqual(got, want) { + t.Fatalf("a Save that left them out changed them: %v", got) + } + if w := post(keep + `,"resolve":{"llm.corp.example":["gateway.corp"]}}`); w.Code == 200 || !strings.Contains(w.Body.String(), "resolve: ") { + t.Fatalf("a place that is a bare name was kept: %d %s", w.Code, w.Body.String()) + } + if got := saved(); !reflect.DeepEqual(got, want) { + t.Fatalf("a refused Save changed them: %v", got) + } + if w := post(keep + `,"resolve":{}}`); w.Code != 200 { + t.Fatalf("%d %s", w.Code, w.Body.String()) + } + if got := saved(); len(got) != 0 { + t.Fatalf("emptied, still %v", got) + } +}