diff --git a/docs/subsystems/providers-accounts.md b/docs/subsystems/providers-accounts.md index 58454894a..488867fae 100644 --- a/docs/subsystems/providers-accounts.md +++ b/docs/subsystems/providers-accounts.md @@ -46,7 +46,7 @@ keys and accounts per request is in [Gateway routing and fallback](gateway-routi | Account names and seats | `AccountNames` (by the account, in lower case, like `AccountCaps`) are names the user gives accounts, set by `SetAccountName` (GUI `provider/accountname`, `magpie accounts alias`); they live in providers.json only, never in a vendor's or plugin's sign-in. A GLM Coding Plan read with a key (`glmPlan`) and a ZCode account (built-in or `zcode-plugin`) whose windows' resets agree are one seat (`seatMatch`, #90/#953), shown once, on the account's card (`seatCards`, used by `quotas`, `Allotted` and `CachedCards`). A match is remembered in `seats.json` beside `quotas.json`: a failed read or a window with no reset yet is unknown and keeps the key's card off while the account is signed in; windows that disagree forget it (#1515). Cards carry `Seat` (the key provider's name, and the key's when it has several) and `Alias` (the user's name, else `Seat`), as `/v1/magpie/quotas`, `magpie quota` and `magpie accounts` do. | [`account_names.go`](../../internal/provider/account_names.go), [`account_seats.go`](../../internal/provider/account_seats.go) | | Last served | `served.json` records when each account, plan or key last answered, shown as `lastServedAt` (#570) on `/v1/magpie/quotas` and on the Usage page's own cards (`Quotas` → `cardsServed`, this computer's cards only, the caches left alone). The Usage page and the tray panel show the last-answered account in full, and a menu bar card that follows the account in use (`codex|*`) takes the one that answered within the last hour, else `InUseLogin`'s guess, else the one that answered last at all (a plugin's accounts, #1516) (`trayInUseCard`; okingkee on X: A spent, B answering, the menu bar stayed on A). `NoteServed` calls `OnServedMoved` when a provider's answers move to another account or key, and `SwitchLogin` after a switch, so the menu bar reads its cards again at once. | [`served.go`](../../internal/provider/served.go), [`trayusage.go`](../../internal/gui/trayusage.go) | | Partners | Services that pay to be listed first in the add sheet, under a heading of their own ("Partners · magpie's sponsors"). The list is `site/partners.js`, served at `usemagpie.ai/api/partners`: a change reaches users with a deploy, not a release. `Partners` never waits: it returns what was fetched last and fetches again behind the call every 6 hours (10 minutes after a failure), keeping the list in `partners.json` in the cache folder. Each entry is checked again on every read (`validPartners`): its own id, never a built-in preset's, a name, an endpoint, https only, at most 12; what only magpie's presets may set (a decision API, no key, a user endpoint) is cleared. Its logo is fetched through `FetchIcon` and kept as an uploaded icon. A partner is a preset to the rest of magpie (`Preset`, `FromPreset`, the editor), and one no longer listed still is to the providers added from it (`Seen`), without the sponsored mark. The window lists a partner in the languages it names (`langs`) only and shows its tagline in the page's language (`notes`, else `en`). `MAGPIE_PARTNERS` points it elsewhere, `off` lists none; tests list none unless they name a list. How often each listed partner was shown, opened (its row, its key page) and added is counted per UTC day (`CountPartner`, capped at 20 of each and 5 adds, kept in `partner-counts.json` in the cache folder) and sent with the next day's stats event as `magpie partner` events (`internal/stats`); nothing is counted with the stats or their usage part off. A listed partner the add sheet (or the TUI's, or `magpie providers presets`) hasn't shown yet is new (`NoticePartners` keeps those shown, as `noticed` in `partners.json`, whatever the stats); the window's add button carries a small dot for it, with its name in the title, until the sheet shows it. | [`partners.go`](../../internal/provider/partners.go), [`partner_events.go`](../../internal/provider/partner_events.go), [`site/partners.js`](../../site/partners.js) | -| Local servers | A preset of `KindLocal` (Ollama, LM Studio, oMLX, MLX-Serve) is at its server's default port on this machine. Another address — a port, or another computer — is given in the window's editor, under Server address, or in the TUI (asked on adding, `w` after); on the command line the URLs themselves (`url=`, `anthropic=`, `responses=`). `AtAddress` replaces only each URL's scheme, host and port, each API keeping its path; `AddressOf` is the address an editor starts from. The window sends the URLs moved only when the address was changed, so a Save doesn't move URLs set apart (another app's import). | [`local_address.go`](../../internal/provider/local_address.go) | +| Local servers | A preset of `KindLocal` (Ollama, LM Studio, oMLX, MLX-Serve) is at its server's default port on this machine. Another address — a port, or another computer — is given in the window's editor, under Server address, or in the TUI (asked on adding, `w` after); on the command line the URLs themselves (`url=`, `anthropic=`, `responses=`). `AtAddress` replaces only each URL's scheme, host and port, each API keeping its path; `AddressOf` is the address an editor starts from. The window sends the URLs moved only when the address was changed, so a Save doesn't move URLs set apart (another app's import). A provider of the user's own (no preset) has `w` in the TUI too: each base URL it has is asked in turn, typed in as it is, Enter alone keeping one (`customURLsAsk`). | [`local_address.go`](../../internal/provider/local_address.go) | | Protocol detection | Works out which of Chat, Responses, Anthropic Messages and Gemini generateContent a base URL speaks when a provider is added. Each is asked at the URL as it takes it (`DetectBase`); Gemini's at the `…/v1beta` beside a typed `…/v1` or bare host, a Gemini model from the list picked for it, with the key in `x-goog-api-key` (#1346). Detection, endpoint tests and model tests share `probe`, which adds `anthropic-version` only for Anthropic Messages; OpenAI probes omit that default so a relay that refuses it can answer. User-supplied headers still apply through `Sign`. The editor asks with the form as it stands, before any Save (#1545): the URL, key and models typed (`picked`, else the saved picks, else the vendor list); a key not typed again is the saved provider's, or for an unsaved copy the copied one's (`copyOf`). The probe writes nothing and adds no provider, and the route stays behind `magpie web`'s key. | [`detect.go`](../../internal/provider/detect.go), [`test.go`](../../internal/provider/test.go) | ## Remote magpie diff --git a/internal/tui/custom_provider_test.go b/internal/tui/custom_provider_test.go index 6bc133520..a2718598a 100644 --- a/internal/tui/custom_provider_test.go +++ b/internal/tui/custom_provider_test.go @@ -106,3 +106,70 @@ func TestTUIAddsACustomProvider(t *testing.T) { } } } + +// w on a provider of the user's own asks each base URL it has, the one +// now typed in, as magpie provider set url=… changes it +// (luluxiu_1_4392 on Discord: on Linux, the TUI had no way to): enter +// alone keeps one, a URL that isn't one is refused, and the others stay. +func TestTUIEditsACustomProvidersBaseURL(t *testing.T) { + home(t) + if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Key: "sk-r", Models: []string{"m"}, + Chat: "https://old.example.com/v1", Anthropic: "https://old.example.com"}); err != nil { + t.Fatal(err) + } + m := press(t, model{w: 140, h: 40}, "2") + at := func(m model, id string) model { + t.Helper() + m.reloadProviders() + i := slices.IndexFunc(m.provs, func(p provider.Provider) bool { return p.ID == id }) + if i < 0 { + t.Fatalf("no provider %s", id) + } + m.prow = i + return m + } + + // one base URL: asked, typed in as it is now, and changed + m = at(m, "a") + m = press(t, m, "w") + if m.mode != modeAsk || m.ask.input.Value() != "http://127.0.0.1:1/v1" { + t.Fatalf("w opened %q (mode %v), not the base URL now", m.ask.input.Value(), m.mode) + } + m = press(t, m, "ctrl+u") + m = typeIn(m, "https://new.example.com/v1/") + m = press(t, m, "enter") + wantFlash(t, m, true, "A base URL https://new.example.com/v1") + if p, _ := provider.Find("a"); p.Chat != "https://new.example.com/v1" || p.Key != "ka" || len(p.Models) != 2 { + t.Fatalf("a after w: %+v", p) + } + + // not an address: refused, nothing saved + m = press(t, m, "w", "ctrl+u") + m = typeIn(m, "new.example.com") + m = press(t, m, "enter") + wantFlash(t, m, false, "http:// or https://") + if p, _ := provider.Find("a"); p.Chat != "https://new.example.com/v1" { + t.Fatalf("a refused URL left %q", p.Chat) + } + + // two: each asked in turn, the one kept as it was left alone + m = at(m, "relay") + m = press(t, m, "w") + if m.ask.input.Value() != "https://old.example.com/v1" || !strings.Contains(m.ask.hint, "Chat Completions") { + t.Fatalf("first asked %q (%s)", m.ask.input.Value(), m.ask.hint) + } + m = press(t, m, "enter") + if m.mode != modeAsk || m.ask.input.Value() != "https://old.example.com" || !strings.Contains(m.ask.hint, "Anthropic") { + t.Fatalf("second asked %q (mode %v, %s)", m.ask.input.Value(), m.mode, m.ask.hint) + } + m = press(t, m, "ctrl+u") + m = typeIn(m, "https://claude.example.com") + m = press(t, m, "enter") + wantFlash(t, m, true, "Relay base URL https://claude.example.com") + if p, _ := provider.Find("relay"); p.Chat != "https://old.example.com/v1" || p.Anthropic != "https://claude.example.com" || p.Responses != "" { + t.Fatalf("relay after w: chat %q anthropic %q responses %q", p.Chat, p.Anthropic, p.Responses) + } + // both left as they are + m = press(t, m, "w", "enter", "enter") + wantFlash(t, m, true, "Relay base URL unchanged") +} diff --git a/internal/tui/pages.go b/internal/tui/pages.go index 4651442df..7547d4e3d 100644 --- a/internal/tui/pages.go +++ b/internal/tui/pages.go @@ -168,6 +168,13 @@ func (m model) updateProviders(msg tea.KeyMsg) (tea.Model, tea.Cmd) { })) return m, nil } + if pr == nil && p.Account == nil && len(customURLs(p)) > 0 { + // a provider of the user's own: its base URLs, one by one + // (luluxiu_1_4392 on Discord: on Linux, the TUI had no way to + // change one) + m.openAsk(customURLsAsk(p, customURLs(p), nil)) + return m, nil + } if pr == nil || pr.Endpoint == "" { m.flash, m.flashOK = p.Name+" is asked at its vendor's address · magpie provider set "+p.ID+" url=… changes a custom one's", false return m, nil @@ -551,6 +558,73 @@ func customURLAsk(api customAPI) ask { }} } +// customURLs are the APIs a provider of the user's own has a base URL +// for, in customAPIs' order. +func customURLs(p provider.Provider) []customAPI { + var out []customAPI + for _, c := range customAPIs { + if customURLOf(p, c) != "" { + out = append(out, c) + } + } + return out +} + +// customURLOf is p's base URL on api. +func customURLOf(p provider.Provider, api customAPI) string { + switch api.id { + case "custom-openai": + return p.Chat + case "custom-responses": + return p.Responses + case "custom-anthropic": + return p.Anthropic + } + return p.Gemini +} + +// customURLsAsk asks for each of p's base URLs in turn, the one it has +// now typed in, and saves those changed once all are answered: enter +// alone keeps one, as typed or emptied. +func customURLsAsk(p provider.Provider, apis []customAPI, typed []string) ask { + api := apis[len(typed)] + in := newInput(api.example) + in.SetValue(customURLOf(p, api)) + return ask{crumbs: []string{"providers", p.Name, "base URL"}, input: in, empty: true, + hint: api.name + " · enter keeps it", + onEnter: func(v string) tea.Cmd { + return func() tea.Msg { + if v != "" { + u, err := url.Parse(v) + if err != nil || (u.Scheme != "http" && u.Scheme != "https") || u.Host == "" { + return flashMsg{text: "the base URL is an http:// or https:// address, e.g. " + api.example} + } + } + typed := append(slices.Clone(typed), strings.TrimRight(v, "/")) + if len(typed) < len(apis) { + return askMsg{customURLsAsk(p, apis, typed)} + } + var set []int + var said []string + for i, c := range apis { + if typed[i] != "" && typed[i] != customURLOf(p, c) { + set = append(set, i) + said = append(said, typed[i]) + } + } + if len(set) == 0 { + return flashMsg{text: p.Name + " base URL unchanged", ok: true} + } + return saveProvider(p.ID, func(q *provider.Provider) { + for _, i := range set { + apis[i].set(q, typed[i]) + } + provider.ForgetBalances() + }, p.Name+" base URL "+strings.Join(said, " · "))() + } + }} +} + // addKeyAsk asks for the key of p, a preset's provider, and adds it. func addKeyAsk(p provider.Provider) ask { in := newInput("the API key") diff --git a/internal/tui/remote_magpie_test.go b/internal/tui/remote_magpie_test.go index f20315145..5493fb4df 100644 --- a/internal/tui/remote_magpie_test.go +++ b/internal/tui/remote_magpie_test.go @@ -73,16 +73,20 @@ func TestTUIAddsARemoteMagpieWithItsAddress(t *testing.T) { t.Fatalf("after w: %q %q key %q", p.Chat, p.Anthropic, p.Key) } - // a vendor of its own address has none to change + // a vendor of its own address has none to change (a provider of the + // user's own does: TestTUIEditsACustomProvidersBaseURL) + if err := provider.Save(provider.Provider{ID: "deepseek", Name: "DeepSeek", Preset: "deepseek", Key: "kd", Chat: "https://api.deepseek.com/v1"}); err != nil { + t.Fatal(err) + } m.reloadProviders() for i, q := range m.provs { - if q.ID == "a" { + if q.ID == "deepseek" { m.prow = i } } m = press(t, m, "w") if m.mode == modeAsk { - t.Fatal("w asked a custom provider's address") + t.Fatal("w asked a vendor's address") } }