From ef155a5ca226b69fb3de220f42d9f103045f446e Mon Sep 17 00:00:00 2001 From: yetone Date: Tue, 27 Oct 2026 18:13:22 +0800 Subject: [PATCH] provider: a new partner provider starts at the region where the user is, Kimi Code's Global plan abroad (Kimi) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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). --- internal/provider/partners.go | 71 ++++++++++- internal/provider/partners_region_test.go | 117 ++++++++++++++++++ .../testdata/partners-2026-10-27.json | 1 + internal/provider/zone_other.go | 6 + internal/provider/zone_windows.go | 19 +++ 5 files changed, 212 insertions(+), 2 deletions(-) create mode 100644 internal/provider/partners_region_test.go create mode 100644 internal/provider/testdata/partners-2026-10-27.json create mode 100644 internal/provider/zone_other.go create mode 100644 internal/provider/zone_windows.go diff --git a/internal/provider/partners.go b/internal/provider/partners.go index a3a470168..163e8a1f6 100644 --- a/internal/provider/partners.go +++ b/internal/provider/partners.go @@ -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") diff --git a/internal/provider/partners_region_test.go b/internal/provider/partners_region_test.go new file mode 100644 index 000000000..7d61777c0 --- /dev/null +++ b/internal/provider/partners_region_test.go @@ -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) + } + } +} diff --git a/internal/provider/testdata/partners-2026-10-27.json b/internal/provider/testdata/partners-2026-10-27.json new file mode 100644 index 000000000..79b2ecf43 --- /dev/null +++ b/internal/provider/testdata/partners-2026-10-27.json @@ -0,0 +1 @@ +{"partners":[{"id":"kimi","name":"Kimi Code","regionLabel":"Plan","regions":[{"id":"cn","name":"China","chat":"https://api.kimi.com/coding/v1","anthropic":"https://api.kimi.com/coding","catalog":"kimi-code-plan-cn","keysUrl":"https://usemagpie.ai/go/kimi/keys/cn","website":"https://usemagpie.ai/go/kimi/site/cn","siteHost":"www.kimi.com"},{"id":"global","name":"Global","chat":"https://api.kimi.ai/coding/v1","anthropic":"https://api.kimi.ai/coding","catalog":"kimi-code-plan-global","keysUrl":"https://usemagpie.ai/go/kimi/keys/global","website":"https://usemagpie.ai/go/kimi/site/global","siteHost":"www.kimi.ai"},{"id":"api-cn","name":"Pay as you go · China","chat":"https://api.moonshot.cn/v1","anthropic":"https://api.moonshot.cn/anthropic","catalog":"moonshotai","keysUrl":"https://usemagpie.ai/go/kimi/keys/api-cn","website":"https://usemagpie.ai/go/kimi/site/api-cn","siteHost":"platform.kimi.com"},{"id":"api","name":"Pay as you go · Global","chat":"https://api.moonshot.ai/v1","anthropic":"https://api.moonshot.ai/anthropic","catalog":"moonshotai","keysUrl":"https://usemagpie.ai/go/kimi/keys/api","website":"https://usemagpie.ai/go/kimi/site/api","siteHost":"platform.kimi.ai"}],"catalog":"kimi-code-plan-cn","keysUrl":"https://usemagpie.ai/go/kimi/keys","website":"https://usemagpie.ai/go/kimi/site","iconUrl":"https://usemagpie.ai/img/partner-kimi.png","notes":{"en":"One subscription, one K3 quota shared by every agent","zh":"一份订阅,所有 agent 共享 K3 额度","zh-TW":"一份訂閱,所有 agent 共享 K3 額度","ja":"1つのサブスクリプションで、すべての agent が K3 の枠を共有","de":"Ein Abo, ein K3-Kontingent für alle Agents"},"from":"2026-10-23T00:00:00+08:00","siteHost":"www.kimi.com"},{"id":"apikeyfan","name":"APIKEY.FAN","regions":[{"id":"main","name":"Default","chat":"https://api.apikey.fan/v1","responses":"https://api.apikey.fan/v1","anthropic":"https://api.apikey.fan"},{"id":"slb","name":"Direct","chat":"https://slb.apikey.fan/v1","responses":"https://slb.apikey.fan/v1","anthropic":"https://slb.apikey.fan"}],"chat":"https://api.apikey.fan/v1","responses":"https://api.apikey.fan/v1","anthropic":"https://api.apikey.fan","keysUrl":"https://usemagpie.ai/go/apikeyfan/keys","website":"https://usemagpie.ai/go/apikeyfan/site","iconUrl":"https://usemagpie.ai/img/partner-apikeyfan.png","notes":{"en":"Enterprise-grade AI API gateway to the world's leading models","zh":"企业级 AI 中转站,连接全球主流大模型","zh-TW":"企業級 AI 中轉站,連接全球主流大模型"},"langs":["zh","en"],"from":"2026-10-24T00:00:00+08:00","until":"2026-11-24T00:00:00+08:00","siteHost":"apikey.fan"},{"id":"wandapi","name":"WandAPI","chat":"https://api.wandapi.ai/v1","responses":"https://api.wandapi.ai/v1","anthropic":"https://api.wandapi.ai","keysUrl":"https://usemagpie.ai/go/wandapi/keys","website":"https://usemagpie.ai/go/wandapi/site","iconUrl":"https://usemagpie.ai/img/partner-wandapi.png","notes":{"en":"Enterprise-grade AI gateway, seamless access to global AI models","zh":"企业级 AI 网关,畅连全球 AI 大模型","zh-TW":"企業級 AI 閘道,暢連全球 AI 大模型"},"from":"2026-10-25T00:00:00+08:00","until":"2026-11-25T00:00:00+08:00","siteHost":"wandapi.ai"},{"id":"corecode","name":"CoreCode","anthropic":"https://corecode.live","keysUrl":"https://usemagpie.ai/go/corecode/keys","website":"https://usemagpie.ai/go/corecode/site","iconUrl":"https://usemagpie.ai/img/partner-corecode.png","notes":{"en":"Full-strength Claude, undiluted","zh":"纯血 Claude,一滴不掺","zh-TW":"純血 Claude,一滴不摻"},"from":"2026-10-26T00:00:00+08:00","until":"2026-11-26T00:00:00+08:00","siteHost":"corecode.live"},{"id":"sidrune","name":"Sidrune AI","chat":"https://sidrune.ai/v1","responses":"https://sidrune.ai/v1","anthropic":"https://sidrune.ai","keysUrl":"https://usemagpie.ai/go/sidrune/keys","website":"https://usemagpie.ai/go/sidrune/site","iconUrl":"https://usemagpie.ai/img/partner-sidrune.png","notes":{"en":"Stable Claude API Access with 85% off; $3 free credit upon registration","zh":"低至1.5折,稳定使用满血Claude;注册送$3,可开发票。","zh-TW":"低至1.5折,穩定使用滿血Claude;註冊送$3,可開發票。"},"langs":["zh","en"],"from":"2026-10-26T00:00:00+08:00","until":"2026-11-26T00:00:00+08:00","siteHost":"sidrune.ai"},{"id":"fastai","name":"FastAIToken","chat":"https://www.fastaitoken.com/v1","responses":"https://www.fastaitoken.com/v1","anthropic":"https://www.fastaitoken.com","keysUrl":"https://usemagpie.ai/go/fastai/keys","website":"https://usemagpie.ai/go/fastai/site","iconUrl":"https://usemagpie.ai/img/partner-fastai.png","notes":{"en":"One-stop access to Claude, GPT and DeepSeek, from 6% of list price","zh":"Claude / GPT / Deepseek 一站式接入方案,最低0.6折,支持对公","zh-TW":"Claude / GPT / Deepseek 一站式接入方案,最低0.6折,支援對公"},"from":"2026-10-26T00:00:00+08:00","until":"2026-11-26T00:00:00+08:00","siteHost":"www.fastaitoken.com"}]} \ No newline at end of file diff --git a/internal/provider/zone_other.go b/internal/provider/zone_other.go new file mode 100644 index 000000000..78bc471dd --- /dev/null +++ b/internal/provider/zone_other.go @@ -0,0 +1,6 @@ +//go:build !windows + +package provider + +// systemZoneName is "" off Windows, where LocalZone reads the zone. +func systemZoneName() string { return "" } diff --git a/internal/provider/zone_windows.go b/internal/provider/zone_windows.go new file mode 100644 index 000000000..f09dba19e --- /dev/null +++ b/internal/provider/zone_windows.go @@ -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 +}