Files
magpie/internal/provider/billing.go
T
yetone dc85a7ca59 providers: AIGoCode is a preset that takes a key (MindfulReturn on X)
MindfulReturn on X asked why magpie's plugins have no AIGoCode sign-in
and whether they have to add the provider by hand. AIGoCode has no
sign-in to plug in: it hands out API keys, each bound to one group. So
the answer is a preset, like 鱼鱼连线's, not a plugin: Add provider ›
AIGoCode asks only for the key.

From AIGoCode's own docs (www.aigocode.net/docs/api, openai-compatible,
anthropic-compatible, responses): one root, https://api.aigocode.dev,
with Chat Completions and Responses at /v1 and Messages at the root.
It runs on sub2api ("Model ... is not available for this group",
API_KEY_REQUIRED from /v1/models and /v1/usage, /api/v1/settings/public
answers), so a key's balance is read at sub2api's /v1/usage by the
relay probe already there; no BalanceURL is needed. A key spends its
plan (Pro/Max/Ultra) and then its balance, so its billing is unsaid,
like MiniMax's Coding Plan keys. Website is the invite AIGoCode
sponsors magpie with (site/sponsors.js has the same one); it is not
badged Sponsored, as 鱼鱼连线 isn't. Keys page: /dashboard/keys.

- presets.go: the aigocode preset (KindRelay, icon aigocode).
- billing.go: aigocode in unsaidPresets.
- icons/aigocode.png and pngIcons: AIGoCode's own logo
  (www.aigocode.net/images/algocode-logo.png), trimmed to 124 in 128.
- core_budget_test.go: +12 for the name in pngIcons; main was at the
  budget to the byte, and the list is read wherever an icon draws.
- site docs (en, zh, ja): import's preset ids and relay's preset list.

Not wired: AIGoCode's Gemini-native endpoint (PresetDef has no Gemini
field). A key serves only its group's models, so a third-party
("可外接") group such as Claude API(AWS) or ChatGPT API(Azure) is
the one to use. If AIGoCode later takes the in-app seat, its partner
entry needs an id other than aigocode (checkPartner).

Tests: TestAIGoCodePresetTakesAKey: kind, key, endpoints as documented,
keys page, balance probed at api.aigocode.dev, billing unsaid. Broken
by hand: without aigocode in unsaidPresets it is red (billing "payg").

Run on mote (mica and mote both over their cores, so no full suite;
CI runs it), at origin/main 24effac0e + this change:
- gofmt -l clean; GOOS=linux go vet ./internal/provider and
  GOOS=windows go vet ./internal/provider ./internal/gui: ok.
- go test -count=1 ./internal/provider: ok (147s);
  go test -count=1 ./internal/gui (whole package, budget included): ok.
- node --test, Chromium and WebKit: icon-tile, icon-one-tone,
  add-sheet, add-sheet-custom, preset-name, icon-builtin-over-address
  (en, zh): 113/113 at a4937f8ff; i18n-keys, gui-de, gui-ja,
  gui-zh-tw, add-sheet, icon-tile: 19/19 each engine.
- Real use, sandbox HOME, built binary: `magpie presets` lists
  "AIGoCode  aigocode  magpie provider add aigocode <key>";
  `magpie provider add aigocode <fake key>` writes the preset's
  chat/responses/anthropic/website/keysUrl, and AIGoCode itself
  answers /v1/models with 401 "Invalid API key".

Not tried: a real AIGoCode key (models listed, a request, the balance).
2026-11-14 12:21:48 +08:00

113 lines
3.8 KiB
Go

package provider
import "strings"
// How a provider bills (EV_John_Liu on X: mark pay as you go apart from a
// token plan on Providers and Routing): on a plan, a subscription or
// token package paid ahead, or pay as you go, each call billed as used.
// The Providers list and a routing group's card say it. The user's word
// (Provider.Billing) stands; else it is read from what magpie knows of
// the provider: a signed-in account is a plan, a preset's plan or pay as
// you go endpoints (its Regions) say theirs, a key of a vendor or relay
// is pay as you go. A provider added by its address, a local server or
// another magpie says nothing until the user sets it.
const (
BillingPlan = "plan"
BillingPAYG = "payg"
BillingMixed = "mixed" // its keys are on a plan and on pay as you go
)
// BillingOK reports whether s is a billing the user may set: plan, pay
// as you go, or "" for magpie to read it.
func BillingOK(s string) bool { return s == "" || s == BillingPlan || s == BillingPAYG }
// planPresets are presets whose keys are a plan's alone, with no pay as
// you go endpoints of their own to tell them apart by.
var planPresets = map[string]bool{
"kimi-code": true, "kimi-code-cn": true,
"qwen-token-plan": true, "bailian-token-plan": true, "alibaba-token-plan": true, "qwencloud-token-plan": true,
"opencode-go": true, "clinepass": true,
}
// unsaidPresets are vendors and relays whose one endpoint takes a plan's
// keys and pay as you go's alike, or that are free, so magpie can't say
// which a key is (MiniMax's Coding Plan keys, ModelScope's daily quota,
// an AIGoCode key spending its plan and then its balance),
// and decision models, which bill as their own provider does.
var unsaidPresets = map[string]bool{
"minimax": true, "minimax-cn": true, "ollama-cloud": true, "modelscope": true,
"nvidia": true, "longcat": true, "commandcode": true,
"typesafe": true, "vercel-jev": true, "cloudflare-jev": true, "aigocode": true,
"bailian-decision": true, "qwen-decision": true, "volcengine-decision": true,
}
// regionBilling is what a preset's region's name says of its billing:
// "Pay as you go …" or "… Plan …"; "" for one named by place.
func regionBilling(r *Region) string {
if r == nil {
return ""
}
n := strings.ToLower(r.Name)
switch {
case strings.HasPrefix(n, "pay as you go"):
return BillingPAYG
case strings.Contains(n, "plan"):
return BillingPlan
}
return ""
}
// BillingKind is how p bills: the user's word, else what magpie reads of
// it (BillingPlan, BillingPAYG, BillingMixed), "" when it can't say.
func (p Provider) BillingKind() string {
if p.Billing != "" {
return p.Billing
}
if p.Account != nil {
return BillingPlan
}
pr := Preset(p.Preset)
if pr == nil || unsaidPresets[pr.ID] || strings.HasSuffix(pr.ID, "-decision") || (pr.Kind != KindVendor && pr.Kind != KindRelay) {
return ""
}
if planPresets[pr.ID] {
return BillingPlan
}
// the region the provider sits at: a plan or pay as you go by its
// name, the vendor's API (billed as used) when named by place; one
// asked at none of its regions' endpoints can't be told
own := BillingPAYG
if len(pr.Regions) > 0 {
r := p.regionOf(pr)
if r == nil {
return ""
}
if b := regionBilling(r); b != "" {
own = b
}
}
// keys on another plan than the provider's (KeyAccount.Plan)
if p.plansMixed() {
for _, plan := range append([]string{p.KeyPlan}, keyPlans(p)...) {
if _, r, _ := p.keyRegion(plan); r != nil {
if b := regionBilling(r); b != "" && b != own {
return BillingMixed
}
}
}
}
return own
}
// keyPlans are the plans p's other keys in use say they are for.
func keyPlans(p Provider) []string {
var out []string
for _, k := range p.Keys {
if k.Off || k.Key == "" || k.Key == p.Key || k.Plan == "" {
continue
}
out = append(out, k.Plan)
}
return out
}