provider: a new partner provider starts at the region where the user is, Kimi Code's Global plan abroad (Kimi)

Kimi (a sponsor) reported: "伙伴卡编辑器默认区为 China,建议按用户区域分流".
A partner given only by regions started at its first one, and Kimi
Code's partner card lists China first, so a user abroad was offered the
China plan's endpoints, key page and catalog.

Now, when a partner's first region is China's or Global's and the other
is listed too, a new provider of it starts at the one where the user is:
China's in mainland China, Global's elsewhere. The place is read from
this computer's time zone: LocalZone ($TZ, /etc/localtime) on macOS and
Linux, the registry's TimeZoneKeyName on Windows. Asia/Shanghai,
Asia/Urumqi and their old names (Chongqing, Harbin, Kashgar, PRC) and
Windows' "China Standard Time" are mainland China; Hong Kong, Macau and
Taipei have zones of their own and get Global. Windows' China Standard
Time also covers Hong Kong, which Windows doesn't tell apart. A zone
that can't be read keeps the partner's first region, as before.

It is decided in validPartners, where the partner's default endpoints
are filled in, so every add path takes it: the GUI editor (its region
picker marks the region at the preset's endpoints, and Get a key and the
site link follow), the TUI's and CLI's add (FromPreset). The default
region's key page, site, siteHost and catalog become the preset's with
its endpoints, as atRegionOf does for a provider.

Which regions count: only one with id cn/china/global/intl or named
China, China Mainland, Mainland China, Global or International. Checked
against each partner in site/partners.js: Kimi (China, Global, "Pay as
you go · China", "Pay as you go · Global") switches between its two
plans only; APIKEY.FAN (Default, Direct) gives its own top-level
endpoints, so no region is defaulted; WandAPI, CoreCode, Sidrune and
FastAIToken have no regions. yylx (a built-in preset, not a partner)
lists Auto first and would stay at Auto anyway.

Providers already added keep their region: a provider stores its own
endpoints when it is added, and its region is read from them
(regionOf), so a user who moves keeps theirs. The list kept on disk
from before is fixed at the next fetch, which runs at start.

Tests:
- TestPartnerStartsAtTheUsersRegion, from usemagpie.ai/api/partners as
  served on 2026-10-27 (testdata/partners-2026-10-27.json): abroad Kimi
  Code starts at api.kimi.ai, keys /go/kimi/keys/global, site host
  www.kimi.ai, catalog kimi-code-plan-global; in China and with no zone
  at api.kimi.com and China's; a provider added in China stays there
  after the user is abroad and the list is fetched again; APIKEY.FAN
  starts at its own endpoints either way.
- TestHomeRegion: Kimi's four regions, yylx's Auto first, Global first,
  plans only, no Global; chinaZone for Shanghai, Urumqi, PRC, China
  Standard Time, Hong Kong, Taipei, Singapore, New York, UTC and "".
- Broken by hand: with the region back at p.Regions[0], the test fails
  "abroad: a new Kimi Code provider is at {chat:https://api.kimi.com/
  coding/v1 … catalog:kimi-code-plan-cn}, want {… api.kimi.ai …
  kimi-code-plan-global}" and "the preset's endpoints … aren't its home
  region's". With the key page/site/catalog line dropped, it fails on
  keys https://usemagpie.ai/go/kimi/keys (not …/keys/cn) in China.
- go test -tags nogui -count=1 -timeout 45m ./internal/provider
  ./internal/cli ./internal/tui at 56de2d192 + these two commits: cli
  ok, tui ok. internal/provider has three reds that are already on
  main, and they fail the same way at origin/main 14e4d8040, just
  fetched, run alone -count=3: TestFetchAsksASharedBaseOnce,
  TestFetchAsksOneListOnceForTwoBasesOfIt and
  TestFetchUnionsProtocolListsWithoutChat (#3854). The first run, with
  nothing skipped, also crashed in TestUnreadLoginsWriteErrorsKeepCredentials
  ("close of synctest channel from outside bubble", #3632, red on main),
  so the second run skipped that one test. No full ./... run: the
  machine's disk was full (ENOSPC) from other sessions' caches.
- go build; GOOS=linux go build -tags nogui; GOOS=windows go build.
- Rebased onto 14e4d8040 (45 files changed on main since 56de2d192,
  none of them these): go build, go vet ./internal/provider and
  -run 'TestPartner|TestHomeRegion|TestPreset' ./internal/provider ok
  at the pushed head; the package run above was not repeated there.
- gofmt -l: nothing; go vet, GOOS=linux go vet -tags nogui and
  GOOS=windows go vet on internal/provider: clean; GOOS=windows go build.

Not tried: the Windows registry read on a real Windows machine (the
Windows box didn't answer ssh); the GUI editor with a real partner list
in a sandbox; the GUI suite (no file under internal/gui changed).
This commit is contained in:
yetone
2026-10-27 18:45:45 +08:00
parent adf41acdd8
commit ef155a5ca2
5 changed files with 212 additions and 2 deletions
+69 -2
View File
@@ -1,6 +1,7 @@
package provider
import (
"cmp"
"context"
"encoding/json"
"errors"
@@ -387,9 +388,15 @@ func validPartners(in []Partner) []Partner {
p.Regions[i].Decide = ""
}
if p.Chat == "" && p.Responses == "" && p.Anthropic == "" {
// the first region is the default, as a built-in preset's is
r := p.Regions[0]
// the first region is the default, as a built-in preset's is,
// unless it is China's or Global's and the user is in the
// other (homeRegion): its key page, site and catalog with it
r := p.Regions[homeRegion(p.Regions, inMainlandChina())]
p.Chat, p.Responses, p.Anthropic = r.Chat, r.Responses, r.Anthropic
p.KeysURL, p.Website, p.Catalog = cmp.Or(r.KeysURL, p.KeysURL), cmp.Or(r.Website, p.Website), cmp.Or(r.Catalog, p.Catalog)
if r.Website != "" {
p.SiteHost = r.SiteHost
}
}
p.Note = p.Notes["en"]
if !strings.HasPrefix(p.Icon, "file:") || IconFile(strings.TrimPrefix(p.Icon, "file:")) == "" {
@@ -400,6 +407,66 @@ func validPartners(in []Partner) []Partner {
return out
}
// homeRegion is the region a new provider of a partner starts at: the
// first, unless the first is China's or Global's and the other is listed
// too, when it is the one where the user is (inChina, from this
// computer's time zone). Unknown (0) keeps the first. Kimi Code lists
// China first, and a user abroad started at a plan that isn't theirs.
// Only a region named for China or Global counts: "Pay as you go ·
// China" is another plan, and "Auto" or "Direct" no place.
func homeRegion(rs []Region, inChina int) int {
if len(rs) < 2 || inChina == 0 {
return 0
}
first := placeOf(rs[0])
if first == "" {
return 0
}
want := "global"
if inChina > 0 {
want = "china"
}
if first == want {
return 0
}
if i := slices.IndexFunc(rs, func(r Region) bool { return placeOf(r) == want }); i > 0 {
return i
}
return 0
}
// placeOf says whether a region is China's ("china"), Global's ("global")
// or neither (""), by its id or its name.
func placeOf(r Region) string {
id, name := strings.ToLower(r.ID), strings.ToLower(strings.TrimSpace(r.Name))
switch {
case id == "cn" || id == "china" || name == "china" || name == "china mainland" || name == "mainland china":
return "china"
case id == "global" || id == "intl" || name == "global" || name == "international":
return "global"
}
return ""
}
// inMainlandChina says whether this computer's time zone is mainland
// China's: 1 it is, -1 it isn't, 0 unknown (no zone could be read). A
// var, so a test sets where it is.
var inMainlandChina = func() int { return chinaZone(cmp.Or(LocalZone(), systemZoneName())) }
// chinaZone reads a time zone's name: an IANA name, or Windows' own,
// whose China Standard Time is Beijing's (Hong Kong's too, which Windows
// doesn't tell apart). Hong Kong, Macau and Taipei have zones of their
// own, so they are abroad.
func chinaZone(name string) int {
switch name {
case "":
return 0
case "Asia/Shanghai", "Asia/Urumqi", "Asia/Chongqing", "Asia/Chungking", "Asia/Harbin", "Asia/Kashgar", "PRC", "China Standard Time":
return 1
}
return -1
}
func checkPartner(p Partner) error {
if !partnerID.MatchString(p.ID) {
return errors.New("bad id")
+117
View File
@@ -0,0 +1,117 @@
package provider
import (
"context"
"os"
"sync/atomic"
"testing"
)
// A new provider of a partner whose regions are China's and Global's
// starts at the one where the user is (Kimi, 10-27: "伙伴卡编辑器默认区为
// China,建议按用户区域分流"): Kimi Code's Global plan abroad, its China plan
// in mainland China, with that plan's key page, site and catalog. A zone
// that can't be read keeps the list's first, a partner whose regions are
// no places (APIKEY.FAN's Default and Direct) keeps its own endpoints, and
// a provider added before keeps its region when the user moves. The feed
// is usemagpie.ai/api/partners as served on 2026-10-27.
func TestPartnerStartsAtTheUsersRegion(t *testing.T) {
feed, err := os.ReadFile("testdata/partners-2026-10-27.json")
if err != nil {
t.Fatal(err)
}
t.Setenv("XDG_CACHE_HOME", t.TempDir())
was, wasIcon := inMainlandChina, fetchPartnerIcon
t.Cleanup(func() { inMainlandChina, fetchPartnerIcon = was, wasIcon; resetPartners() })
fetchPartnerIcon = func(context.Context, string) (string, error) { return "", os.ErrNotExist }
var body atomic.Value
body.Store(string(feed))
t.Setenv("MAGPIE_PARTNERS", partnerFeedServer(t, &body).URL)
at := func(where int) Provider {
t.Helper()
inMainlandChina = func() int { return where }
resetPartners()
fetchPartnersNow()
p, err := FromPreset("kimi")
if err != nil {
t.Fatal(err)
}
return p
}
type want struct{ chat, anthropic, keys, site, siteHost, catalog string }
cn := want{"https://api.kimi.com/coding/v1", "https://api.kimi.com/coding", "https://usemagpie.ai/go/kimi/keys/cn", "https://usemagpie.ai/go/kimi/site/cn", "www.kimi.com", "kimi-code-plan-cn"}
global := want{"https://api.kimi.ai/coding/v1", "https://api.kimi.ai/coding", "https://usemagpie.ai/go/kimi/keys/global", "https://usemagpie.ai/go/kimi/site/global", "www.kimi.ai", "kimi-code-plan-global"}
for _, c := range []struct {
name string
where int
want want
}{
{"abroad", -1, global},
{"in mainland China", 1, cn},
{"zone unknown", 0, cn},
} {
p := at(c.where)
pr := Preset("kimi")
got := want{p.Chat, p.Anthropic, p.KeysURL, p.Website, pr.SiteHost, p.Catalog}
if got != c.want {
t.Errorf("%s: a new Kimi Code provider is at %+v, want %+v", c.name, got, c.want)
}
// the editor shows the plan picked by the preset's endpoints
if r := pr.Regions[homeRegion(pr.Regions, c.where)]; r.Chat != pr.Chat {
t.Errorf("%s: the preset's endpoints %q aren't its home region's %q", c.name, pr.Chat, r.Chat)
}
}
// a provider added in China stays at China's plan once the user is
// abroad and the list is fetched again
added := normalize(at(1))
at(-1)
if added = normalize(added); added.Chat != cn.chat || added.KeysURL != cn.keys || added.Catalog != cn.catalog {
t.Errorf("an added provider moved to %q (%s, %s)", added.Chat, added.KeysURL, added.Catalog)
}
// regions that are no places: its own endpoints wherever the user is
for _, where := range []int{-1, 1} {
at(where)
if p, _ := FromPreset("apikeyfan"); p.Chat != "https://api.apikey.fan/v1" {
t.Errorf("APIKEY.FAN (%d) starts at %q", where, p.Chat)
}
}
}
// Only a first region that is China's or Global's gives way, and only to
// the other: yylx's Auto first, a plan named "Pay as you go · China", or
// a list without the other place stay at the first.
func TestHomeRegion(t *testing.T) {
kimi := []Region{{ID: "cn", Name: "China"}, {ID: "global", Name: "Global"}, {ID: "api-cn", Name: "Pay as you go · China"}, {ID: "api", Name: "Pay as you go · Global"}}
yylx := []Region{{ID: "auto", Name: "Auto"}, {ID: "global", Name: "Global"}, {ID: "cn", Name: "China Mainland"}}
globalFirst := []Region{{ID: "intl", Name: "International"}, {ID: "cn", Name: "China Mainland"}}
plans := []Region{{ID: "api-cn", Name: "Pay as you go · China"}, {ID: "api", Name: "Pay as you go · Global"}}
chinaOnly := []Region{{ID: "cn", Name: "China"}, {ID: "hk", Name: "Hong Kong"}}
for _, c := range []struct {
name string
rs []Region
inChina int
want int
}{
{"kimi abroad", kimi, -1, 1},
{"kimi in China", kimi, 1, 0},
{"kimi unknown", kimi, 0, 0},
{"yylx abroad", yylx, -1, 0},
{"yylx in China", yylx, 1, 0},
{"global first, in China", globalFirst, 1, 1},
{"global first, abroad", globalFirst, -1, 0},
{"plans only", plans, -1, 0},
{"no Global", chinaOnly, -1, 0},
} {
if got := homeRegion(c.rs, c.inChina); got != c.want {
t.Errorf("%s: homeRegion = %d, want %d", c.name, got, c.want)
}
}
for zone, want := range map[string]int{"Asia/Shanghai": 1, "Asia/Urumqi": 1, "PRC": 1, "China Standard Time": 1, "Asia/Hong_Kong": -1, "Asia/Taipei": -1, "Asia/Singapore": -1, "America/New_York": -1, "UTC": -1, "": 0} {
if got := chinaZone(zone); got != want {
t.Errorf("chinaZone(%q) = %d, want %d", zone, got, want)
}
}
}
File diff suppressed because one or more lines are too long
+6
View File
@@ -0,0 +1,6 @@
//go:build !windows
package provider
// systemZoneName is "" off Windows, where LocalZone reads the zone.
func systemZoneName() string { return "" }
+19
View File
@@ -0,0 +1,19 @@
package provider
import "golang.org/x/sys/windows/registry"
// systemZoneName is Windows' name of this computer's time zone ("China
// Standard Time"), which LocalZone can't read there; "" when the registry
// doesn't say.
func systemZoneName() string {
k, err := registry.OpenKey(registry.LOCAL_MACHINE, `SYSTEM\CurrentControlSet\Control\TimeZoneInformation`, registry.QUERY_VALUE)
if err != nil {
return ""
}
defer k.Close()
name, _, err := k.GetStringValue("TimeZoneKeyName")
if err != nil {
return ""
}
return name
}