"支持多个ghcp账号轮换和平均负载均衡吗,每个ghcp账号有的模型不一样。"
Several Copilot accounts and the Routing strategies (In turn, by usage)
worked already. What didn't: the copilot provider's list was the first
account's /models alone, so a model only another account's plan has was
nowhere to pick, and the gateway took every account to have every model.
Now each Copilot account's /models is kept as its own (accountModels,
read by Account.Lists), and the provider's list is their union
(copilotPoolModels over poolModels, the body codexPoolModels had). A
fetch of the first account's list also asks the other accounts on: one
with no list at once, of the rest the stalest once its list is a day
old, one per fetch. Only a failed ask is remembered (retried after an
hour). A model Copilot refused one account (model_not_supported) stays
in the list while another account on lists it (copilotOthersList).
Run (WSL magpie-desktop at 425195bf3; mote refused SSH all session):
- go test ./internal/provider/ ./internal/gateway/ -timeout 45m: ok
- go vet linux, GOOS=windows, GOOS=darwin CGO_ENABLED=0 for both: ok
- gofmt -l: clean
- TestCopilotAccountsWithOtherModels: octocat lists gpt-4.1 and
claude-opus-9, hubot gpt-4.1 and gemini-9-pro; the list is the union,
each model goes only to its account under In turn, gpt-4.1 to both.
- TestCopilotRefusedModelGoesToTheAccountServedIt: octocat's list has
gemini-9-pro but Copilot refuses it; it then goes to hubot alone and
stays listed.
Broken by hand:
- copilotPoolModels call removed: the first test red ("lists [gpt-4.1
claude-opus-9 auto], without gemini-9-pro").
- models.go's union check dropped: the second test red (gemini-9-pro
left the list).
- A refusal check in Account.Lists made no difference (the gateway
already keeps a refused account off that model), so it isn't in.
Not tried: real Copilot accounts on two different plans; the full
suite (runs after the tag).
131 lines
4.5 KiB
Go
131 lines
4.5 KiB
Go
package provider
|
|
|
|
// Several Copilot accounts, each with its own plan's models (applemacssma
|
|
// on X: "每个ghcp账号有的模型不一样"). Copilot's /models answers each
|
|
// GitHub account with what its plan has: a Pro+ account lists models a Pro
|
|
// or Student one doesn't. The copilot provider's list was the first
|
|
// account's alone, fetched with its sign-in, so a model only another
|
|
// account had was nowhere to pick, and a model only the first had was
|
|
// sent to the others too, which answer it 400 model_not_supported. Each
|
|
// account's list is now kept of its own (accountModels), the provider's
|
|
// is what they have together, and the gateway sends a model only to the
|
|
// accounts whose list has it (Account.Lists), as Codex's ChatGPT accounts
|
|
// are served (codexPoolModels).
|
|
|
|
import (
|
|
"context"
|
|
"sort"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/catalog"
|
|
)
|
|
|
|
// copilotSavedListAge is how old another account's list is before it is
|
|
// asked for again, and copilotSavedAtOnce how many such lists one fetch of
|
|
// the first account's asks. An account with no list yet is asked at once.
|
|
// A plan's models change seldom, and N /models calls from one address for
|
|
// accounts no editor uses is a pattern GitHub's own clients never make.
|
|
const (
|
|
copilotSavedListAge = 24 * time.Hour
|
|
copilotSavedAtOnce = 1
|
|
copilotSavedRetry = time.Hour
|
|
)
|
|
|
|
// copilotSavedAsked is when each other account's list was last asked for
|
|
// and not saved, so one that keeps failing waits copilotSavedRetry; an ask
|
|
// in flight is held here too, so two fetches don't both make it.
|
|
var copilotSavedAsked = struct {
|
|
sync.Mutex
|
|
m map[string]time.Time
|
|
}{m: map[string]time.Time{}}
|
|
|
|
// copilotSavedNow is copilotFetchSaved's clock; a var so tests can move it.
|
|
var copilotSavedNow = time.Now
|
|
|
|
// copilotPoolModels is ms, the list of the Copilot account in use first,
|
|
// user's, kept as that account's own, with the other accounts on's lists
|
|
// fetched as they are due (copilotFetchSaved) and joined to it.
|
|
func copilotPoolModels(ctx context.Context, user string, ms []catalog.Model) []catalog.Model {
|
|
_ = catalog.SaveLive(accountModels("copilot", user), copilotBase, ms)
|
|
copilotFetchSaved(ctx, user)
|
|
return poolModels("copilot", ms)
|
|
}
|
|
|
|
// copilotFetchSaved asks for the models of the Copilot accounts on behind
|
|
// first, each with its own sign-in and through its own proxy. One that
|
|
// can't be asked now keeps what it listed last.
|
|
func copilotFetchSaved(ctx context.Context, first string) {
|
|
now := copilotSavedNow()
|
|
type due struct {
|
|
c copilotLoginApp
|
|
at time.Time
|
|
}
|
|
var fresh, stale []due
|
|
copilotSavedAsked.Lock()
|
|
for _, c := range copilotLogins(copilotConfigDir()) {
|
|
if c.Active || !c.On || strings.EqualFold(c.User, first) || c.app.Token == "" {
|
|
continue
|
|
}
|
|
asked := copilotSavedAsked.m[strings.ToLower(c.User)]
|
|
_, at, ok := catalog.Live(accountModels("copilot", c.User))
|
|
if !ok {
|
|
if now.Sub(asked) >= copilotSavedRetry {
|
|
fresh = append(fresh, due{c, at})
|
|
}
|
|
continue
|
|
}
|
|
if asked.After(at) {
|
|
at = asked
|
|
}
|
|
if now.Sub(at) >= copilotSavedListAge {
|
|
stale = append(stale, due{c, at})
|
|
}
|
|
}
|
|
sort.SliceStable(stale, func(i, j int) bool { return stale[i].at.Before(stale[j].at) })
|
|
ask := append(fresh, stale[:min(len(stale), copilotSavedAtOnce)]...)
|
|
for _, d := range ask {
|
|
copilotSavedAsked.m[strings.ToLower(d.c.User)] = now
|
|
}
|
|
copilotSavedAsked.Unlock()
|
|
for _, d := range ask {
|
|
ms, err := copilotModels(ViaLogin(ctx, "copilot", d.c.User), d.c.app)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
if catalog.SaveLive(accountModels("copilot", d.c.User), copilotBase, ms) == nil {
|
|
// the list saved says when it was asked; only a failed ask
|
|
// is held here, so a list gone (a new HOME, its file
|
|
// removed) is asked for again at once
|
|
copilotSavedAsked.Lock()
|
|
if copilotSavedAsked.m[strings.ToLower(d.c.User)].Equal(now) {
|
|
delete(copilotSavedAsked.m, strings.ToLower(d.c.User))
|
|
}
|
|
copilotSavedAsked.Unlock()
|
|
}
|
|
}
|
|
}
|
|
|
|
// copilotOthersList are the models the Copilot accounts on other than
|
|
// user have in their own lists, less those Copilot refused them: a model
|
|
// Copilot refused user stays the provider's while another serves it.
|
|
func copilotOthersList(user string) map[string]bool {
|
|
out := map[string]bool{}
|
|
for _, c := range copilotLogins(copilotConfigDir()) {
|
|
if !c.On || strings.EqualFold(c.User, user) {
|
|
continue
|
|
}
|
|
live, _, ok := catalog.Live(accountModels("copilot", c.User))
|
|
if !ok {
|
|
continue
|
|
}
|
|
for _, m := range live {
|
|
if !copilotRefuses(c.app.Token, m.ID) {
|
|
out[m.ID] = true
|
|
}
|
|
}
|
|
}
|
|
return out
|
|
}
|