gui: the accounts list says what magpie does with the agent's sign-in
A user with two Claude accounts read the used-up switch as gone. Their "Keep Claude Code signed in to" box was ticked, which turns the switch off, and nothing on the page said so. Under the accounts of a Claude Code or Codex with more than one account, a line now says what the switch does, as NextLogin decides it: - kept: switching is off, the agent stays on the first account (or the one picked) even once it runs out; - gateway: Claude Code's requests go through magpie, which picks an account per request; its own sign-in stays put; - moves: the share it moves at (98%, or used up for In order / Until used up), and whether it comes back to the first (returnsToFirst). provider.SignInMovesOf gives the mode from the same checks NextLogin makes (leftAlone, keep, ClaudeThroughMagpie && !SwitchOnly, SpentShareOf, returnsToFirst). /api/providers carries it as signInMoves on the agent's own provider row. The Routing tab's sign-in note says the gateway case too. Strings in zh, zh-TW, ja and de. Run (mote, through mote-run): - go test -run 'TestSignInMoves|TestClaudeRouted|TestKeepLogin| TestClaudeSwitched|TestNextLogin|TestCodexLogin' ./internal/provider: ok - go test -timeout 45m ./internal/gui ./internal/provider: ok (script size budgets included) - gofmt -l clean; go vet darwin, GOOS=linux -tags nogui, GOOS=windows on provider and gui: ok - GUI: sign-in-moves, keep-login, i18n-keys, i18n-en-no-cjk, routing-signin, account-return in chromium and webkit: 60 pass - whole GUI suite (make test-ui): see below - screenshots in WebKit, en and zh: the line sits inside the list at the rows' inset; zh sentences join with no space Broken by hand: dropping NextLogin's keep case from SignInMovesOf turns TestSignInMovesSaysWhatSwitchDoes red; not appending the line turns sign-in-moves.test.cjs red 4 of 4. Not tried: the real app with the owner's accounts; the user's own keepLogin is left as it is.
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -2864,6 +2864,7 @@ body.window .ag-sk-icon { width: 22px; height: 22px; border-radius: 6px; }
|
||||
.accts > :is(.keep-login, .switch-only, .restart-app, .pool-ends) + .acc { border-top: 1px solid var(--line-2); }
|
||||
.editor .accts .keep-login, .editor .accts .switch-only, .editor .accts .restart-app, .editor .accts .pool-ends { display: flex; align-items: center; flex-wrap: wrap; gap: 6px; min-height: 34px; padding: 6px 11px; font-size: 12px; border-top: 1px solid var(--line-2); }
|
||||
.editor .accts .keep-login .keep-as { max-width: 240px; }
|
||||
.editor .accts .sign-in-moves { margin: 0; padding: 8px 11px; font-size: 12px; line-height: 1.5; color: var(--fg-2); border-top: 1px solid var(--line-2); }
|
||||
/* Claude Code's interactive mode or -p, with why (claudeModeRow) */
|
||||
.claude-mode { display: contents; }
|
||||
.cc-run { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 10px; min-height: 26px; }
|
||||
|
||||
@@ -12233,6 +12233,22 @@ function routingOfDraft(p) {
|
||||
if (draft?.sink !== undefined && draft.sink !== !!p.sink) out.sink = draft.sink;
|
||||
return out;
|
||||
}
|
||||
// signInMovesLine says how magpie moves Codex's or Claude Code's own
|
||||
// sign-in between its accounts (provider.SignInMovesOf), "" when it
|
||||
// doesn't: the subscription is off
|
||||
function signInMovesLine(p, agent) {
|
||||
const m = p.signInMoves;
|
||||
if (!m) return "";
|
||||
if (m.mode === "kept") return keptLogin(p)
|
||||
? t("Automatic switching is off: {agent} stays signed in to {user}, even once it runs out.", { agent, user: keptLogin(p) })
|
||||
: t("Automatic switching is off: {agent} stays signed in to the first account, even once it runs out.", { agent });
|
||||
if (m.mode === "gateway") return t("{agent}'s requests go through magpie, which sends each one to an account with room; its own sign-in stays where you put it.", { agent });
|
||||
if (m.mode !== "moves") return "";
|
||||
const on = m.share >= 100
|
||||
? t("When the account {agent} is signed in to is used up, magpie signs it in to the next ticked account with room.", { agent })
|
||||
: t("When the account {agent} is signed in to reaches {share}%, magpie signs it in to the next ticked account with room.", { agent, share: m.share });
|
||||
return on + (locale === "zh" || locale === "zh-TW" || locale === "ja" ? "" : " ") + (m.back ? t("It goes back to the first once that has room again.") : t("It stays on that one till it runs low too."));
|
||||
}
|
||||
function renderRouting(p) {
|
||||
const routingNow = () => draft.routing ?? (p.routing || "");
|
||||
const affinityNow = () => draft.affinity ?? (p.affinity || "");
|
||||
@@ -12244,7 +12260,9 @@ function renderRouting(p) {
|
||||
const a = p.account;
|
||||
// — at 98%, In order once used up (#530), never when kept on the first (#524)
|
||||
const own = (routing) => !a || (a.agent !== "codex" && a.agent !== "claude") ? ""
|
||||
: " " + (keptLogin(p)
|
||||
: " " + (p.signInMoves?.mode === "gateway" && !keptLogin(p) && !p.keepLogin
|
||||
? t("{agent}'s requests go through magpie, which sends each one to an account with room; its own sign-in stays where you put it.", { agent: a.agentName })
|
||||
: keptLogin(p)
|
||||
? t("Routing picks the account for each request through magpie, in the accounts' order; {agent} on its own stays signed in to {user}, whatever it has left.", { agent: a.agentName, user: keptLogin(p) })
|
||||
: p.keepLogin && !p.switchOnly
|
||||
? t("Routing picks the account for each request through magpie; {agent} on its own stays signed in to the first account, whatever it has left.", { agent: a.agentName })
|
||||
@@ -13561,6 +13579,13 @@ function renderAccounts(a, p) {
|
||||
if (amBox) row.append(amBox);
|
||||
list.append(row);
|
||||
}
|
||||
// what magpie does with the agent's own sign-in, said where the
|
||||
// accounts are: a tick left on, or Claude Code on the gateway, had it
|
||||
// never switch with nothing on the page saying so (the owner, 11-11)
|
||||
if ((a.agent === "codex" || a.agent === "claude") && several && p) {
|
||||
const say = signInMovesLine(p, a.agentName);
|
||||
if (say) list.append(el("div", "hint sign-in-moves", say));
|
||||
}
|
||||
// one account (#209, #408, #524)
|
||||
if ((a.agent === "codex" || a.agent === "claude") && several && p && !p.switchOnly) {
|
||||
const box = el("div", "keep-login");
|
||||
|
||||
@@ -2061,6 +2061,13 @@ const I18N = {
|
||||
"Only requests through magpie use them; {agent} itself stays signed in as it is.": "只有经过 magpie 的请求会用这些账号;{agent} 本身的登录不变。",
|
||||
"Routing picks the account for each request through magpie; {agent} on its own uses the one it is signed in to, which magpie moves to the next ticked account with room once it is 98% used, and back to the first once that has room again.": "经 magpie 的请求由路由挑选账号;{agent} 直连时用它登录的账号,该账号用到 98% 时,magpie 把 {agent} 登录到下一个已勾选且有余量的账号,首选账号恢复后切回。",
|
||||
"Routing picks the account for each request through magpie; {agent} on its own uses the one it is signed in to, which magpie moves to the next ticked account with room once it is 98% used, and keeps it there till that one is 98% used too.": "经 magpie 的请求由路由挑选账号;{agent} 直连时用它登录的账号,该账号用到 98% 时,magpie 把 {agent} 登录到下一个已勾选且有余量的账号,并一直用它,直到它也用到 98%。",
|
||||
"Automatic switching is off: {agent} stays signed in to {user}, even once it runs out.": "自动切换已关闭:{agent} 一直登录 {user},额度用完也不换。",
|
||||
"Automatic switching is off: {agent} stays signed in to the first account, even once it runs out.": "自动切换已关闭:{agent} 一直登录首选账号,额度用完也不换。",
|
||||
"{agent}'s requests go through magpie, which sends each one to an account with room; its own sign-in stays where you put it.": "{agent} 的请求经过 magpie,每个请求都发给有额度的账号;{agent} 自己的登录保持你选的账号不变。",
|
||||
"When the account {agent} is signed in to is used up, magpie signs it in to the next ticked account with room.": "{agent} 登录的账号用完时,magpie 把它登录到下一个已勾选且有额度的账号。",
|
||||
"When the account {agent} is signed in to reaches {share}%, magpie signs it in to the next ticked account with room.": "{agent} 登录的账号用到 {share}% 时,magpie 把它登录到下一个已勾选且有额度的账号。",
|
||||
"It goes back to the first once that has room again.": "首选账号恢复额度后再切回去。",
|
||||
"It stays on that one till it runs low too.": "之后一直用这个账号,直到它的额度也快用完。",
|
||||
"Routing picks the account for each request through magpie; {agent} on its own uses the one it is signed in to, which magpie moves to the next ticked account with room once it is used up, and back to the first once that has room again.": "经 magpie 的请求由路由挑选账号;{agent} 直连时用它登录的账号,该账号额度用完时,magpie 把 {agent} 登录到下一个已勾选且有余量的账号,首选账号恢复后切回。",
|
||||
"Routing picks the account for each request through magpie; {agent} on its own uses the one it is signed in to, which magpie moves to the next ticked account with room once it is used up, and keeps it there till that one is used up too.": "经 magpie 的请求由路由挑选账号;{agent} 直连时用它登录的账号,该账号额度用完时,magpie 把 {agent} 登录到下一个已勾选且有余量的账号,并一直用它,直到它的额度也用完。",
|
||||
"Routing picks the account for each request through magpie; {agent} on its own stays signed in to the first account, whatever it has left.": "经 magpie 的请求由路由挑选账号;{agent} 直连时始终登录首选账号,无论剩余额度多少。",
|
||||
@@ -7076,6 +7083,13 @@ const I18N = {
|
||||
"Only requests through magpie use them; {agent} itself stays signed in as it is.": "只有經過 magpie 的請求會用這些帳號;{agent} 本身的登入不變。",
|
||||
"Routing picks the account for each request through magpie; {agent} on its own uses the one it is signed in to, which magpie moves to the next ticked account with room once it is 98% used, and back to the first once that has room again.": "經 magpie 的請求由路由挑選帳號;{agent} 直連時用它登入的帳號,該帳號用到 98% 時,magpie 把 {agent} 登入到下一個已勾選且有餘量的帳號,首選帳號恢復後切回。",
|
||||
"Routing picks the account for each request through magpie; {agent} on its own uses the one it is signed in to, which magpie moves to the next ticked account with room once it is 98% used, and keeps it there till that one is 98% used too.": "經 magpie 的請求由路由挑選帳號;{agent} 直連時用它登入的帳號,該帳號用到 98% 時,magpie 把 {agent} 登入到下一個已勾選且有餘量的帳號,並一直用它,直到它也用到 98%。",
|
||||
"Automatic switching is off: {agent} stays signed in to {user}, even once it runs out.": "自動切換已關閉:{agent} 一直登入 {user},額度用完也不換。",
|
||||
"Automatic switching is off: {agent} stays signed in to the first account, even once it runs out.": "自動切換已關閉:{agent} 一直登入首選帳號,額度用完也不換。",
|
||||
"{agent}'s requests go through magpie, which sends each one to an account with room; its own sign-in stays where you put it.": "{agent} 的請求經過 magpie,每個請求都發給有額度的帳號;{agent} 自己的登入保持你選的帳號不變。",
|
||||
"When the account {agent} is signed in to is used up, magpie signs it in to the next ticked account with room.": "{agent} 登入的帳號用完時,magpie 把它登入到下一個已勾選且有額度的帳號。",
|
||||
"When the account {agent} is signed in to reaches {share}%, magpie signs it in to the next ticked account with room.": "{agent} 登入的帳號用到 {share}% 時,magpie 把它登入到下一個已勾選且有額度的帳號。",
|
||||
"It goes back to the first once that has room again.": "首選帳號恢復額度後再切回去。",
|
||||
"It stays on that one till it runs low too.": "之後一直用這個帳號,直到它的額度也快用完。",
|
||||
"Routing picks the account for each request through magpie; {agent} on its own uses the one it is signed in to, which magpie moves to the next ticked account with room once it is used up, and back to the first once that has room again.": "經 magpie 的請求由路由挑選帳號;{agent} 直連時用它登入的帳號,該帳號額度用完時,magpie 把 {agent} 登入到下一個已勾選且有餘量的帳號,首選帳號恢復後切回。",
|
||||
"Routing picks the account for each request through magpie; {agent} on its own uses the one it is signed in to, which magpie moves to the next ticked account with room once it is used up, and keeps it there till that one is used up too.": "經 magpie 的請求由路由挑選帳號;{agent} 直連時用它登入的帳號,該帳號額度用完時,magpie 把 {agent} 登入到下一個已勾選且有餘量的帳號,並一直用它,直到它的額度也用完。",
|
||||
"Routing picks the account for each request through magpie; {agent} on its own stays signed in to the first account, whatever it has left.": "經 magpie 的請求由路由挑選帳號;{agent} 直連時始終登入首選帳號,無論剩餘額度多少。",
|
||||
@@ -12067,6 +12081,13 @@ const I18N = {
|
||||
"Only requests through magpie use them; {agent} itself stays signed in as it is.": "これらのアカウントを使うのは magpie を通るリクエストだけです。{agent} 自身のサインインはそのままです。",
|
||||
"Routing picks the account for each request through magpie; {agent} on its own uses the one it is signed in to, which magpie moves to the next ticked account with room once it is 98% used, and back to the first once that has room again.": "magpie 経由のリクエストはルーティングがアカウントを選びます。{agent} 単体ではサインイン中のアカウントを使い、そのアカウントが 98% 使用されると magpie が残量のある次のチェック済みアカウントに切り替え、最初のアカウントに残量が戻ると元に戻します。",
|
||||
"Routing picks the account for each request through magpie; {agent} on its own uses the one it is signed in to, which magpie moves to the next ticked account with room once it is 98% used, and keeps it there till that one is 98% used too.": "magpie 経由のリクエストはルーティングがアカウントを選びます。{agent} 単体ではサインイン中のアカウントを使い、そのアカウントが 98% 使用されると magpie が残量のある次のチェック済みアカウントに切り替え、そのアカウントも 98% 使用されるまでそのまま使います。",
|
||||
"Automatic switching is off: {agent} stays signed in to {user}, even once it runs out.": "自動切り替えはオフです:{agent} は使い切っても {user} にサインインしたままです。",
|
||||
"Automatic switching is off: {agent} stays signed in to the first account, even once it runs out.": "自動切り替えはオフです:{agent} は使い切っても最初のアカウントにサインインしたままです。",
|
||||
"{agent}'s requests go through magpie, which sends each one to an account with room; its own sign-in stays where you put it.": "{agent} のリクエストは magpie を経由し、各リクエストは残量のあるアカウントに送られます。{agent} 自身のサインインは選んだアカウントのままです。",
|
||||
"When the account {agent} is signed in to is used up, magpie signs it in to the next ticked account with room.": "{agent} がサインインしているアカウントを使い切ると、magpie は残量のある次のチェック済みアカウントにサインインし直します。",
|
||||
"When the account {agent} is signed in to reaches {share}%, magpie signs it in to the next ticked account with room.": "{agent} がサインインしているアカウントが {share}% に達すると、magpie は残量のある次のチェック済みアカウントにサインインし直します。",
|
||||
"It goes back to the first once that has room again.": "最初のアカウントに残量が戻ると、そちらに戻します。",
|
||||
"It stays on that one till it runs low too.": "そのアカウントも残り少なくなるまで、そのまま使います。",
|
||||
"Routing picks the account for each request through magpie; {agent} on its own uses the one it is signed in to, which magpie moves to the next ticked account with room once it is used up, and back to the first once that has room again.": "magpie 経由のリクエストはルーティングがアカウントを選びます。{agent} 単体ではサインイン中のアカウントを使い、そのアカウントを使い切ると magpie が残量のある次のチェック済みアカウントに切り替え、最初のアカウントに残量が戻ると元に戻します。",
|
||||
"Routing picks the account for each request through magpie; {agent} on its own uses the one it is signed in to, which magpie moves to the next ticked account with room once it is used up, and keeps it there till that one is used up too.": "magpie 経由のリクエストはルーティングがアカウントを選びます。{agent} 単体ではサインイン中のアカウントを使い、そのアカウントを使い切ると magpie が残量のある次のチェック済みアカウントに切り替え、そのアカウントも使い切るまでそのまま使います。",
|
||||
"Routing picks the account for each request through magpie; {agent} on its own stays signed in to the first account, whatever it has left.": "magpie 経由のリクエストはルーティングがアカウントを選びます。{agent} 単体では残量にかかわらず最初のアカウントにサインインしたままです。",
|
||||
@@ -17089,6 +17110,13 @@ const I18N = {
|
||||
"Only requests through magpie use them; {agent} itself stays signed in as it is.": "Nur Anfragen über magpie verwenden diese Konten; {agent} selbst bleibt angemeldet wie bisher.",
|
||||
"Routing picks the account for each request through magpie; {agent} on its own uses the one it is signed in to, which magpie moves to the next ticked account with room once it is 98% used, and back to the first once that has room again.": "Die Weiterleitung wählt für jede Anfrage über magpie das Konto; {agent} selbst nutzt das Konto, bei dem es angemeldet ist; magpie meldet es beim nächsten angehakten Konto mit freiem Kontingent an, sobald das aktuelle zu 98 % verbraucht ist, und wieder beim ersten, sobald dort wieder Kontingent frei ist.",
|
||||
"Routing picks the account for each request through magpie; {agent} on its own uses the one it is signed in to, which magpie moves to the next ticked account with room once it is 98% used, and keeps it there till that one is 98% used too.": "Die Weiterleitung wählt für jede Anfrage über magpie das Konto; {agent} selbst nutzt das Konto, bei dem es angemeldet ist; magpie meldet es beim nächsten angehakten Konto mit freiem Kontingent an, sobald das aktuelle zu 98 % verbraucht ist, und bleibt dort, bis auch dieses zu 98 % verbraucht ist.",
|
||||
"Automatic switching is off: {agent} stays signed in to {user}, even once it runs out.": "Automatischer Wechsel ist aus: {agent} bleibt bei {user} angemeldet, auch wenn das Kontingent aufgebraucht ist.",
|
||||
"Automatic switching is off: {agent} stays signed in to the first account, even once it runs out.": "Automatischer Wechsel ist aus: {agent} bleibt beim ersten Konto angemeldet, auch wenn das Kontingent aufgebraucht ist.",
|
||||
"{agent}'s requests go through magpie, which sends each one to an account with room; its own sign-in stays where you put it.": "Die Anfragen von {agent} laufen über magpie, das jede an ein Konto mit Kontingent schickt; die eigene Anmeldung von {agent} bleibt, wo du sie gesetzt hast.",
|
||||
"When the account {agent} is signed in to is used up, magpie signs it in to the next ticked account with room.": "Ist das Konto, bei dem {agent} angemeldet ist, aufgebraucht, meldet magpie es beim nächsten angehakten Konto mit Kontingent an.",
|
||||
"When the account {agent} is signed in to reaches {share}%, magpie signs it in to the next ticked account with room.": "Erreicht das Konto, bei dem {agent} angemeldet ist, {share} %, meldet magpie es beim nächsten angehakten Konto mit Kontingent an.",
|
||||
"It goes back to the first once that has room again.": "Hat das erste wieder Kontingent, geht es dorthin zurück.",
|
||||
"It stays on that one till it runs low too.": "Es bleibt dort, bis auch dieses knapp wird.",
|
||||
"Routing picks the account for each request through magpie; {agent} on its own uses the one it is signed in to, which magpie moves to the next ticked account with room once it is used up, and back to the first once that has room again.": "Die Weiterleitung wählt für jede Anfrage über magpie das Konto; {agent} selbst nutzt das Konto, bei dem es angemeldet ist; magpie meldet es beim nächsten angehakten Konto mit freiem Kontingent an, sobald das aktuelle aufgebraucht ist, und wieder beim ersten, sobald dort wieder Kontingent frei ist.",
|
||||
"Routing picks the account for each request through magpie; {agent} on its own uses the one it is signed in to, which magpie moves to the next ticked account with room once it is used up, and keeps it there till that one is used up too.": "Die Weiterleitung wählt für jede Anfrage über magpie das Konto; {agent} selbst nutzt das Konto, bei dem es angemeldet ist; magpie meldet es beim nächsten angehakten Konto mit freiem Kontingent an, sobald das aktuelle aufgebraucht ist, und bleibt dort, bis auch dieses aufgebraucht ist.",
|
||||
"Routing picks the account for each request through magpie; {agent} on its own stays signed in to the first account, whatever it has left.": "Die Weiterleitung wählt für jede Anfrage über magpie das Konto; {agent} selbst bleibt beim ersten Konto angemeldet, egal wie viel dort noch übrig ist.",
|
||||
|
||||
@@ -325,6 +325,9 @@ type providerJSON struct {
|
||||
// Claude's, the accounts are kept to Claude Code's own sign-in and
|
||||
// serve no request through magpie (myduso on Discord)
|
||||
SwitchOnly bool `json:"switchOnly,omitempty"`
|
||||
// SignInMoves: how magpie moves Codex's or Claude Code's own sign-in
|
||||
// between its accounts (provider.SignInMovesOf), said beside them
|
||||
SignInMoves *provider.SignInMoves `json:"signInMoves,omitempty"`
|
||||
// RestartApp: magpie restarts the Codex app's app-server for an
|
||||
// account change it makes by itself (provider.Provider.RestartApp)
|
||||
RestartApp bool `json:"restartApp,omitempty"`
|
||||
@@ -846,6 +849,9 @@ func providerInfoWith(p provider.Provider, ls provider.Listing, agents []agentUs
|
||||
AccountConcurrency: p.AccountConcurrency, AccountWeights: p.AccountWeights, QueueLimit: p.QueueLimit, QueueWait: p.QueueWait, MaxRPM: p.MaxRPM,
|
||||
Outputs: provider.OutputsOf(p.ID), Compacts: provider.CompactsOf(p.ID),
|
||||
}
|
||||
if p.Account != nil && p.Account.Agent == p.ID {
|
||||
out.SignInMoves = provider.SignInMovesOf(p.ID)
|
||||
}
|
||||
if p.Account != nil {
|
||||
for _, r := range p.AccountRefs() {
|
||||
if s := provider.SeatNameOf(p.ID, r); s != "" {
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
// Run with Node's test runner and Playwright on the module path; see README.md.
|
||||
// The owner (11-11): Claude Code stopped switching to an account with room,
|
||||
// and nothing on the page said why: "Keep Claude Code signed in to" was
|
||||
// ticked. Under the accounts a line now says what magpie does with Claude
|
||||
// Code's own sign-in, as the server tells it (signInMoves): moved on at 98%
|
||||
// or once used up, back to the first or not; off while kept on one; left
|
||||
// alone while its requests go through the gateway. Ticking the keep box
|
||||
// changes the line. No line with one account. English and Chinese,
|
||||
// Chromium and WebKit.
|
||||
const assert = require("node:assert/strict");
|
||||
const fs = require("node:fs/promises");
|
||||
const path = require("node:path");
|
||||
const { test } = require("node:test");
|
||||
const { chromium, webkit } = require("playwright");
|
||||
|
||||
const assets = path.resolve(__dirname, "../assets");
|
||||
const claude = ({ signInMoves = { mode: "moves", share: 98 }, keepLogin = false, keepLoginAs = "", spare = true, one = false } = {}) => ({
|
||||
id: "claude", name: "Claude Code", icon: "claude-color", chat: "", responses: "", anthropic: "", catalog: "", routing: "", keepLogin, keepLoginAs, signInMoves,
|
||||
models: [{ id: "claude-opus-5-5", name: "Claude Opus 5.5", on: true }], agents: [], fallback: [], headers: {}, keyList: [],
|
||||
account: {
|
||||
agent: "claude", agentName: "Claude Code", user: "a@example.com", plan: "MAX",
|
||||
logins: [{ user: "a@example.com", plan: "MAX", active: true, on: true }, ...(one ? [] : [{ user: "b@example.com", plan: "MAX", on: spare }])],
|
||||
},
|
||||
});
|
||||
|
||||
function serve(lang, first, posts) {
|
||||
let list = [first];
|
||||
const state = { agents: [], profiles: [], settings: { lang, theme: "light" } };
|
||||
const providers = () => ({ providers: list, presets: [], excluded: [], gateway: { running: true, window: true } });
|
||||
return async (route) => {
|
||||
const url = new URL(route.request().url());
|
||||
const json = (data) => route.fulfill({ json: data });
|
||||
if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:true};` });
|
||||
if (url.pathname === "/wails/runtime.js") return route.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
|
||||
if (url.pathname === "/api/state") return json(state);
|
||||
if (url.pathname === "/api/providers") return json(providers());
|
||||
if (url.pathname === "/api/provider/keeplogin") {
|
||||
const body = route.request().postDataJSON();
|
||||
posts.push(body);
|
||||
const as = body.keepLogin && body.keepLoginAs || "";
|
||||
const logins = list[0].account.logins.map((l) => ({ ...l, active: as ? l.user === as : l.user === "a@example.com" }));
|
||||
// the server says what the switch now does (provider.SignInMovesOf)
|
||||
const signInMoves = body.keepLogin ? { mode: "kept" } : { mode: "moves", share: 98 };
|
||||
list = [{ ...list[0], keepLogin: body.keepLogin, keepLoginAs: as, signInMoves, account: { ...list[0].account, logins } }];
|
||||
return json(providers());
|
||||
}
|
||||
if (url.pathname === "/api/provider/arrange") {
|
||||
posts.push({ arrange: route.request().postDataJSON() });
|
||||
return json(providers());
|
||||
}
|
||||
if (url.pathname === "/api/groups") return json({ groups: [] });
|
||||
if (url.pathname === "/api/plugins") return json({ plugins: [] });
|
||||
if (url.pathname.startsWith("/api/")) return json({});
|
||||
const file = path.join(assets, url.pathname === "/" ? "index.html" : url.pathname);
|
||||
const contentType = { ".html": "text/html", ".js": "text/javascript", ".css": "text/css", ".svg": "image/svg+xml", ".png": "image/png" }[path.extname(file)];
|
||||
await route.fulfill({ body: await fs.readFile(file), contentType });
|
||||
};
|
||||
}
|
||||
|
||||
const words = {
|
||||
en: {
|
||||
at98: "When the account Claude Code is signed in to reaches 98%, magpie signs it in to the next ticked account with room. It stays on that one till it runs low too.",
|
||||
usedUpBack: "When the account Claude Code is signed in to is used up, magpie signs it in to the next ticked account with room. It goes back to the first once that has room again.",
|
||||
kept: "Automatic switching is off: Claude Code stays signed in to the first account, even once it runs out.",
|
||||
keptAs: "Automatic switching is off: Claude Code stays signed in to b@example.com, even once it runs out.",
|
||||
gateway: "Claude Code's requests go through magpie, which sends each one to an account with room; its own sign-in stays where you put it.",
|
||||
},
|
||||
zh: {
|
||||
at98: "Claude Code 登录的账号用到 98% 时,magpie 把它登录到下一个已勾选且有额度的账号。之后一直用这个账号,直到它的额度也快用完。",
|
||||
usedUpBack: "Claude Code 登录的账号用完时,magpie 把它登录到下一个已勾选且有额度的账号。首选账号恢复额度后再切回去。",
|
||||
kept: "自动切换已关闭:Claude Code 一直登录首选账号,额度用完也不换。",
|
||||
keptAs: "自动切换已关闭:Claude Code 一直登录 b@example.com,额度用完也不换。",
|
||||
gateway: "Claude Code 的请求经过 magpie,每个请求都发给有额度的账号;Claude Code 自己的登录保持你选的账号不变。",
|
||||
},
|
||||
};
|
||||
|
||||
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
|
||||
for (const lang of ["en", "zh"]) {
|
||||
const w = words[lang];
|
||||
const open = async (t, opts) => {
|
||||
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
|
||||
t.after(() => browser.close());
|
||||
const page = await (await browser.newContext({ viewport: { width: 900, height: 760 }, reducedMotion: "reduce" })).newPage();
|
||||
page.setDefaultTimeout(5000);
|
||||
page.setDefaultNavigationTimeout(60000);
|
||||
const errors = [], posts = [];
|
||||
page.on("pageerror", (e) => errors.push(e.message));
|
||||
await page.route("**/*", serve(lang, claude(opts), posts));
|
||||
await page.goto("http://magpie.test/?view=providers");
|
||||
await page.locator(".row.provider", { hasText: "Claude Code" }).first().click();
|
||||
await page.locator(".editor .acc").first().waitFor();
|
||||
return { page, errors, posts };
|
||||
};
|
||||
const line = (page) => page.locator(".editor .accts .sign-in-moves");
|
||||
|
||||
test(`${engine} ${lang}: the line says how Claude Code's sign-in moves`, async (t) => {
|
||||
for (const [opts, want] of [
|
||||
[{}, w.at98],
|
||||
[{ signInMoves: { mode: "moves", share: 100, back: true } }, w.usedUpBack],
|
||||
[{ signInMoves: { mode: "gateway" } }, w.gateway],
|
||||
[{ signInMoves: { mode: "kept" }, keepLogin: true, keepLoginAs: "b@example.com" }, w.keptAs],
|
||||
]) {
|
||||
const { page, errors } = await open(t, opts);
|
||||
assert.equal((await line(page).textContent()).trim(), want);
|
||||
assert.equal(await line(page).count(), 1);
|
||||
assert.deepEqual(errors, []);
|
||||
await page.close();
|
||||
}
|
||||
});
|
||||
|
||||
test(`${engine} ${lang}: ticking keep changes the line, and none with one account or off`, async (t) => {
|
||||
const { page, errors, posts } = await open(t, {});
|
||||
const top = await page.evaluate(() => document.scrollingElement.scrollTop);
|
||||
await page.locator(".editor .accts .keep-login input").click();
|
||||
await page.waitForFunction((s) => document.querySelector(".editor .accts .sign-in-moves")?.textContent.trim() === s, w.kept);
|
||||
assert.deepEqual(posts, [{ id: "claude", keepLogin: true }]);
|
||||
await page.locator(".editor .accts .keep-login input").click();
|
||||
await page.waitForFunction((s) => document.querySelector(".editor .accts .sign-in-moves")?.textContent.trim() === s, w.at98);
|
||||
assert.equal(await page.evaluate(() => document.scrollingElement.scrollTop), top, "the page doesn't move");
|
||||
assert.deepEqual(errors, []);
|
||||
for (const opts of [{ one: true }, { signInMoves: { mode: "off" } }]) {
|
||||
const o = await open(t, opts);
|
||||
assert.equal(await line(o.page).count(), 0, JSON.stringify(opts));
|
||||
assert.deepEqual(o.errors, []);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package provider
|
||||
|
||||
import "slices"
|
||||
|
||||
// SignInMoves is how magpie moves an agent's own sign-in between its
|
||||
// accounts, as NextLogin decides it, for the Providers page to say
|
||||
// beside the accounts: a user who left a "keep signed in" tick on, or
|
||||
// whose Claude Code went through the gateway, saw no switch and had no
|
||||
// way to tell why (the owner, 11-11).
|
||||
type SignInMoves struct {
|
||||
// Mode: "kept" (stays on one account, KeepLogin / KeepLoginAs),
|
||||
// "gateway" (Claude Code sends its requests to magpie's gateway,
|
||||
// which moves each to an account with room, its sign-in left
|
||||
// where the user put it), "off" (the subscription is off or hidden:
|
||||
// magpie leaves the sign-in alone) or "moves" (moved on once spent).
|
||||
Mode string `json:"mode"`
|
||||
// Share: the share of a window at which the account it is on counts
|
||||
// spent and is moved off (SpentShareOf); 100 is used up. Only "moves".
|
||||
Share float64 `json:"share,omitempty"`
|
||||
// Back: once the account moved off has room again, the agent is
|
||||
// signed back in to it (returnsToFirst). Only "moves".
|
||||
Back bool `json:"back,omitempty"`
|
||||
}
|
||||
|
||||
// SignInMovesOf is SignInMoves for agent; nil for one whose sign-in magpie
|
||||
// never moves.
|
||||
func SignInMovesOf(agent string) *SignInMoves {
|
||||
if !slices.Contains(switchedAgents, agent) {
|
||||
return nil
|
||||
}
|
||||
if leftAlone(agent) {
|
||||
return &SignInMoves{Mode: "off"}
|
||||
}
|
||||
share, keep := loginSwitching(agent)
|
||||
switch {
|
||||
case keep:
|
||||
return &SignInMoves{Mode: "kept"}
|
||||
case agent == "claude" && ClaudeThroughMagpie() && !SwitchOnly(agent):
|
||||
return &SignInMoves{Mode: "gateway"}
|
||||
}
|
||||
return &SignInMoves{Mode: "moves", Share: share, Back: returnsToFirst(agent)}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// What the Providers page says of Claude Code's sign-in (SignInMovesOf) is
|
||||
// what the switch does: "moves" exactly when a spent account is moved off,
|
||||
// at the share it names. The owner's Claude Code went unswitched for days
|
||||
// under a "keep signed in" tick left on, with nothing on the page saying
|
||||
// automatic switching was off (11-11).
|
||||
func TestSignInMovesSaysWhatSwitchDoes(t *testing.T) {
|
||||
home := claudeHome(t)
|
||||
cred := claudeSignIn(t, home, time.Now().Add(time.Hour)) // a@example.com, sk-ant-oat01-old
|
||||
writeFile(t, home+"/.claude.json", map[string]any{"oauthAccount": map[string]any{"emailAddress": "a@example.com", "accountUuid": "u-a"}})
|
||||
rememberLogins(true)
|
||||
loginsMu.Lock()
|
||||
ls := upsertLogin(readLogins(), savedLogin{Agent: "claude", User: "b@example.com", Plan: "max", On: true, Seen: time.Now().UTC(),
|
||||
Auth: mustJSONRaw(t, map[string]any{"claudeAiOauth": map[string]any{"accessToken": "sk-ant-oat01-b", "refreshToken": "sk-ant-ort01-b",
|
||||
"expiresAt": time.Now().Add(time.Hour).UnixMilli(), "subscriptionType": "max", "scopes": []string{"user:inference", "user:profile"}}}),
|
||||
Profile: mustJSONRaw(t, map[string]any{"emailAddress": "b@example.com", "accountUuid": "u-b"})})
|
||||
if err := writeLogins(ls); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
loginsMu.Unlock()
|
||||
fake := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
t.Errorf("usage endpoint read: %s", r.URL)
|
||||
}))
|
||||
defer fake.Close()
|
||||
claudeBase = fake.URL // isolate puts it back
|
||||
|
||||
routed := true
|
||||
old := ClaudeThroughMagpie
|
||||
ClaudeThroughMagpie = func() bool { return routed }
|
||||
t.Cleanup(func() { ClaudeThroughMagpie = old })
|
||||
|
||||
// a used up, b with room
|
||||
used := map[string]float64{"a@example.com": 100, "b@example.com": 10}
|
||||
switched := func() string {
|
||||
t.Helper()
|
||||
loginUsageCache.Lock()
|
||||
loginUsageCache.m = nil
|
||||
loginUsageCache.Unlock()
|
||||
claudeUsage.Lock()
|
||||
claudeUsage.m = nil
|
||||
claudeUsage.Unlock()
|
||||
for user, u := range used {
|
||||
NoteClaudeLimits(user, []ClaudeLimit{{Kind: "five_hour", Used: u / 100, ResetsAt: time.Now().Add(time.Hour).Unix()}})
|
||||
}
|
||||
to, err := SwitchWhenSpent(context.Background(), "claude")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return to
|
||||
}
|
||||
signedInAs := func() string {
|
||||
t.Helper()
|
||||
b, err := os.ReadFile(cred)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if strings.Contains(string(b), "sk-ant-ort01-b") {
|
||||
return "b@example.com"
|
||||
}
|
||||
return "a@example.com"
|
||||
}
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
routed, only, keep bool
|
||||
routing string
|
||||
mode string
|
||||
share float64
|
||||
back bool
|
||||
}{
|
||||
{name: "not routed", mode: "moves", share: SpentShare},
|
||||
{name: "not routed, in order", routing: Ordered, mode: "moves", share: 100, back: true},
|
||||
{name: "routed", routed: true, mode: "gateway"},
|
||||
{name: "routed, switch only", routed: true, only: true, mode: "moves", share: SpentShare},
|
||||
{name: "kept on the first", keep: true, mode: "kept"},
|
||||
{name: "kept, routed", keep: true, routed: true, mode: "kept"},
|
||||
}
|
||||
for _, c := range cases {
|
||||
routed = c.routed
|
||||
if err := SetRouting("claude", c.routing); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := SetSwitchOnly("claude", c.only); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := SetKeepLogin("claude", c.keep); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := SwitchLogin("claude", "a@example.com"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
setLoginReturn("claude", loginReturn{})
|
||||
m := SignInMovesOf("claude")
|
||||
if m == nil || m.Mode != c.mode || m.Share != c.share || m.Back != c.back {
|
||||
t.Fatalf("%s: says %+v, want mode %s share %g back %v", c.name, m, c.mode, c.share, c.back)
|
||||
}
|
||||
to := switched()
|
||||
if moved := to != ""; moved != (c.mode == "moves") {
|
||||
t.Fatalf("%s: the page says %q but the switch moved to %q", c.name, c.mode, to)
|
||||
}
|
||||
if to != "" && signedInAs() != to {
|
||||
t.Fatalf("%s: switched to %s, signed in as %s", c.name, to, signedInAs())
|
||||
}
|
||||
}
|
||||
if SignInMovesOf("gemini") != nil {
|
||||
t.Fatal("an agent whose sign-in magpie never moves has a line")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user