Files
magpie/internal/provider/claude_probe.go
T
yetone e698e7dddd claude: adding a Claude account is Claude Code's own claude auth login, and magpie never refreshes a Claude sign-in nor asks Anthropic anything with one (the user: 把 magpie 自己的 Claude 登录改成直接用 claude /login,只要体验上没什么差别就改) — magpie traded the sign-in page's code for tokens with Claude Code's OAuth client, asked Anthropic's profile and model list with them, refreshed saved accounts itself (daily keepalive included) and handed a saved account to Claude Code as CLAUDE_CODE_OAUTH_TOKEN. Now Add account runs claude auth login --claudeai in a config directory of magpie's own: the page Claude Code would open comes to magpie's window (Claude Code is given magpie as its BROWSER, which writes the page down and exits), the browser comes back to Claude Code's own listener, and when the browser can't reach this machine the address it ended on, or the code Claude's page shows, is pasted as before and handed to Claude Code. Who signed in is what Claude Code kept (.claude.json, auth status). A saved account in use beside the signed-in one runs Claude Code with CLAUDE_CONFIG_DIR set to its own directory (claude-accounts/ beside magpie's config), its sign-in put there once from logins.json and read back as Claude Code refreshes it; switching to it moves it into Claude Code's own store and removes the directory, as forgetting it does. Claude's models are the catalog's (models.dev), not asked of Anthropic; an imported Claude login is kept as the file has it, and Claude Code refreshes it on first use. Claude Code is required to add a Claude account. TestClaudeSignInByPaste, TestClaudeSignInByPastedAddress, TestClaudeSavedAccountDir (a fake claude CLI, and a server standing in for Anthropic that fails the test if asked) and the reworked sign-in, import and gateway tests fail without the change. With the real Claude Code 2.1.286 in a sandbox directory, its sign-in page (localhost callback) reached magpie's binary and the sign-in waited for the browser; nothing was left behind once it was canceled. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./... pass; claude-risk, plugin-signin, signin-callback, signin-link, signin-paste-codex, routing-signin, own-signin-remove, click-scroll, old-webkit pass.
2026-10-01 16:37:07 +08:00

60 lines
2.0 KiB
Go

package provider
// A Claude account is tested as it is used: by Claude Code itself, never
// by a request magpie makes to Anthropic's API with the account's sign-in.
import (
"context"
"errors"
"time"
)
// errClaudeViaCLI is what a request straight to Anthropic's API on a
// Claude account comes to: such requests run Claude Code instead.
var errClaudeViaCLI = errors.New("a Claude account is used through Claude Code, not by requests to Anthropic's API")
// claudeCLIProbe has Claude Code answer one "hi" at model, with configDir
// the config directory of a saved account (claude_dirs.go), "" for the one
// Claude Code is signed in to.
// The gateway, which runs Claude Code, sets it (ProbeClaudeVia).
var claudeCLIProbe func(ctx context.Context, configDir, model string) error
// ProbeClaudeVia sets how a Claude account's test runs Claude Code.
func ProbeClaudeVia(f func(ctx context.Context, configDir, model string) error) { claudeCLIProbe = f }
// claudeWait is how long a test waits on Claude Code, which takes a few
// seconds to start before it asks.
const claudeWait = time.Minute
// testClaude is a Claude account's test of model, run by Claude Code.
func (p Provider) testClaude(ctx context.Context, model string) Result {
r := Result{Protocol: Anthropic, Model: model}
if model == "" {
r.Error = "no model to try: expose one, or refresh the model list"
return r
}
if claudeCLIProbe == nil {
r.Error = "Claude Code isn't available to run the test"
return r
}
ctx, cancel := context.WithTimeout(ctx, claudeWait)
defer cancel()
dir, _, err := p.Account.Token(ctx)
if err != nil {
r.Error = err.Error()
return r
}
start := time.Now()
err = claudeCLIProbe(ctx, dir, model)
r.Millis = time.Since(start).Milliseconds()
if err != nil {
r.Error = err.Error()
return r
}
r.OK = true
return r
}
// isClaudeAccount is whether p is a Claude subscription account.
func (p Provider) isClaudeAccount() bool { return p.Account != nil && p.Account.Agent == "claude" }