Files
magpie/internal/provider/factory.go
T
yetone 1cdc349145 factory: X-Factory-Org-Id carries Factory's own id for the org, as droid sends it, not WorkOS's, and a refused org is mended once (#242 tasselx: "403 permission_error: Factory: Requested active organization is not accessible by this user") — read again from droid 0.229.0 (still the latest @factory/cli-darwin-arm64): its header builder (Ad) sends X-Factory-Org-Id only from the stored active_organization_id, left off when null; that field is only ever filled with Factory's (Firestore) org ids, whoami's orgId or /api/app/auth/me's memberships, never WorkOS's org_… ids, which go only to WorkOS's refresh; moving a token that names no org into the first of /api/cli/org's workosOrgIds (gt) saves active_organization_id null; routine refresh names no org. magpie sent the WorkOS org id as the header, which Factory looks up as one of its own and refuses. Now factoryCreds keeps Active (activeOrganizationId) from whoami's orgId, asked without an org header, and only it is sent; Org, the WorkOS id, is kept but never sent; logins saved by v0.1.432 have no Active and so send none; refresh names no org. The sign-in checks the JWT's external_org_id then org_id, carries on as droid does when /api/cli/org fails or lists none, and when whoami fails but the token says whose it is. On that 403 an account mends itself once (Account.retry, Provider.Retries/Retry, asked by gateway.forward and by the usage read): an active org sent is dropped and the request goes again without the header; with none sent, the token is put in the first org /api/cli/org lists (droid does this at sign-in, not on a 403). Other 403s and 401s are passed on as they were. Tested in factory_test.go against fake WorkOS and Factory servers that answer droid's way: TestFactoryActiveOrg (a token WorkOS put in org_A keeps fac_A and gets a 200; a v0.1.432-format login sends no header and gets a 200) and TestFactoryOrgRefused (a left active org dropped and retried with no refresh; a left token org refreshed into org_A and retried; the usage read the same; other 403s and 401s not retried) fail on main with the reporter's 403; the existing tests now check whoami and /api/cli/org get no org header and refresh sends no organization_id. go vet ./internal/..., go build -tags nogui (and GOOS=linux, GOOS=windows) and go test -tags nogui ./... pass. Not tried against a real Factory account: that the server reads the header as a Factory org id is from droid's code, not seen on the wire; the gateway's retry is covered only by a provider-level test; accounts with several orgs take the first, with no picker.
2026-09-30 09:34:21 +08:00

554 lines
20 KiB
Go

package provider
// A Factory subscription is the account Factory's Droid CLI signs in to
// (factory.ai: Pro, Plus, Max). Its models are served under Factory's own
// API, each on the wire it speaks natively: Claude on Anthropic's Messages
// at /api/llm/a, GPT and Grok on OpenAI's Responses at /api/llm/o/v1, the
// open models Factory hosts on chat completions beside it. The model field
// is Factory's own model id; the server picks the vendor behind it.
//
// The sign-in is droid's own: WorkOS's device flow under droid's client, run
// by magpie and kept in logins.json. droid's own login is encrypted with a
// key in the keychain and its refresh token rotates, so magpie never reads
// or shares it; each account magpie signs in is its own.
//
// Read from droid 0.229.0 (the npm package @factory/cli-darwin-arm64).
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"sync"
"time"
"github.com/yetone/magpie/internal/catalog"
)
// Where Factory and its WorkOS sign-in are; vars so tests can point them
// elsewhere.
var (
factoryWorkOS = "https://api.workos.com/user_management"
factoryAPI = "https://api.factory.ai"
factoryAPIEU = "https://api.eu.factory.ai"
)
const (
// factoryClientID is droid's WorkOS client, production.
factoryClientID = "client_01HNM792M5G5G1A2THWPXKFMXB"
// factoryVersion is the droid release magpie's requests say they are.
factoryVersion = "0.229.0"
// factoryRefreshLead is how long before an access token lapses it is
// renewed; droid renews a minute ahead, magpie a little more.
factoryRefreshLead = 2 * time.Minute
)
var (
factoryClient = &http.Client{Timeout: 30 * time.Second}
// factoryMu serializes checking, rotating and saving tokens: WorkOS
// rotates the refresh token, so two refreshes would spend it twice.
factoryMu sync.Mutex
// factorySession is the session id this magpie's requests carry.
factorySession = randomUUID()
)
// factoryCreds is what magpie keeps of a Factory account.
type factoryCreds struct {
Access string `json:"accessToken"`
Refresh string `json:"refreshToken"`
ExpiresAt int64 `json:"expiresAt"` // unix ms, from the token's exp
// Org is the WorkOS organization (org_…) the token was put in, kept
// for the record: it is never sent to Factory's API.
Org string `json:"orgId,omitempty"`
// Active is droid's active_organization_id: Factory's own id for the
// org, as /api/cli/whoami answers it, sent as X-Factory-Org-Id. Empty
// sends none and Factory takes the token's org, as droid does before
// it has asked whoami.
Active string `json:"activeOrganizationId,omitempty"`
Email string `json:"email,omitempty"`
UserID string `json:"userId,omitempty"`
Region string `json:"region,omitempty"` // "eu" for an org served from Factory's EU region
}
// base is the Factory API the account's org is served from.
func (c factoryCreds) base() string {
if c.Region == "eu" {
return factoryAPIEU
}
return factoryAPI
}
type factoryLogin struct {
Login
creds factoryCreds
}
func factorySaved(l savedLogin) (factoryCreds, bool) {
var c factoryCreds
if json.Unmarshal(l.Auth, &c) != nil || c.Access == "" {
return factoryCreds{}, false
}
return c, true
}
// factoryLogins is every Factory account signed in, the first in use first.
func factoryLogins() []factoryLogin {
var out []factoryLogin
for _, l := range sideLogins("factory", "", func(l savedLogin) bool {
_, ok := factorySaved(l)
return ok
}) {
c, _ := factorySaved(l.saved)
a := factoryLogin{Login: l.Login, creds: c}
a.Lapsed = l.saved.Lapsed
out = append(out, a)
}
return out
}
func factorySide() []sideLogin {
var out []sideLogin
for _, l := range factoryLogins() {
out = append(out, sideLogin{Login: l.Login})
}
return out
}
func factoryLoginList() []Login { return loginsOf(factorySide()) }
func switchFactoryLogin(user string) error {
return switchSideLogin("factory", user, factorySide())
}
func setFactoryLoginOn(user string, on bool) error {
return setSideLoginOn("factory", user, on, factorySide())
}
func forgetFactoryLogin(user string) error {
return forgetSideLogin("factory", user, factorySide(), nil)
}
// ---- keeping the sign-in --------------------------------------------------
// factoryStatus is an answer Factory or WorkOS gave with an error status.
type factoryStatus struct {
Code int
Msg string
}
func (e *factoryStatus) Error() string { return e.Msg }
// factoryRefused is WorkOS turning the refresh token away for good: any
// 4xx but a rate limit, as droid reads it.
func factoryRefused(err error) bool {
var st *factoryStatus
return errors.As(err, &st) && st.Code >= 400 && st.Code < 500 && st.Code != http.StatusTooManyRequests
}
// factoryTokens is WorkOS's answer to /authenticate.
type factoryTokens struct {
Access string `json:"access_token"`
Refresh string `json:"refresh_token"`
Org string `json:"organization_id"`
User struct {
ID string `json:"id"`
Email string `json:"email"`
} `json:"user"`
Error string `json:"error"`
Desc string `json:"error_description"`
status int
}
// factoryPost posts a form to WorkOS: the answer and its status.
func factoryPost(ctx context.Context, path string, form url.Values) ([]byte, int, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodPost, factoryWorkOS+path, strings.NewReader(form.Encode()))
if err != nil {
return nil, 0, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Accept", "application/json")
res, err := factoryClient.Do(req)
if err != nil {
return nil, 0, err
}
defer res.Body.Close()
b, _ := io.ReadAll(io.LimitReader(res.Body, 1<<20))
return b, res.StatusCode, nil
}
// factoryAuthenticate asks WorkOS for tokens. A 400 carrying an OAuth error
// is an answer, not a failure: the device flow's polls are told to wait so.
func factoryAuthenticate(ctx context.Context, form url.Values) (factoryTokens, error) {
var t factoryTokens
b, code, err := factoryPost(ctx, "/authenticate", form)
if err != nil {
return t, err
}
_ = json.Unmarshal(b, &t)
t.status = code
if code != http.StatusOK && t.Error == "" {
return t, &factoryStatus{code, "Factory sign-in: " + APIError(b, http.StatusText(code))}
}
return t, nil
}
// factoryRenew trades a refresh token for a new pair, in the WorkOS org
// when one is named (droid's way of putting a token that names no org in
// one); droid's routine refresh names none, and WorkOS keeps the org.
func factoryRenew(ctx context.Context, refresh, org string) (factoryTokens, error) {
form := url.Values{"grant_type": {"refresh_token"}, "refresh_token": {refresh}, "client_id": {factoryClientID}}
if org != "" {
form.Set("organization_id", org)
}
t, err := factoryAuthenticate(ctx, form)
if err == nil && t.Error != "" {
err = &factoryStatus{t.status, "Factory: " + strings.TrimSpace(t.Error+" "+t.Desc)}
}
if err == nil && t.Access == "" {
err = errors.New("Factory: the refresh gave no access token")
}
return t, err
}
// factoryExpiry is when an access token lapses, from its exp; zero when it
// doesn't say.
func factoryExpiry(access string) int64 {
if exp, _ := jwtClaims(access)["exp"].(float64); exp > 0 {
return int64(exp) * 1000
}
return 0
}
// factoryEdit saves c over the account named user.
func factoryEdit(user string, c factoryCreds, renewed bool) error {
return editSideLogin("factory", user, func(ls []savedLogin, i int) ([]savedLogin, error) {
b, err := json.Marshal(c)
if err != nil {
return nil, err
}
ls[i].Auth, ls[i].Seen = b, time.Now().UTC()
if renewed {
ls[i].Renewed, ls[i].Lapsed = time.Now().UTC(), ""
}
return ls, nil
})
}
// factoryLapse records that WorkOS refused an account's refresh token.
func factoryLapse(user string, err error) error {
if !factoryRefused(err) {
return err
}
msg := user + "'s Factory sign-in has expired — sign in again"
_ = editSideLogin("factory", user, func(ls []savedLogin, i int) ([]savedLogin, error) {
ls[i].Lapsed = msg
return ls, nil
})
return fmt.Errorf("%s (%w)", msg, err)
}
func factoryLookup(user string) (savedLogin, bool) {
loginsMu.Lock()
defer loginsMu.Unlock()
for _, l := range readLogins() {
if l.Agent == "factory" && strings.EqualFold(l.User, user) {
return l, true
}
}
return savedLogin{}, false
}
// factoryFresh answers with a live token for one account, renewing it near
// its end. The renewal runs on a context of its own: once WorkOS has rotated
// the pair, its reply must be kept even if the request that asked is gone.
func factoryFresh(ctx context.Context, user string) (factoryCreds, error) {
factoryMu.Lock()
defer factoryMu.Unlock()
l, ok := factoryLookup(user)
if !ok {
return factoryCreds{}, fmt.Errorf("no Factory account %q", user)
}
c, valid := factorySaved(l)
if !valid {
return factoryCreds{}, errors.New("Factory: unreadable sign-in")
}
if c.ExpiresAt > 0 && time.Now().UnixMilli() < c.ExpiresAt-factoryRefreshLead.Milliseconds() {
return c, nil
}
if c.Refresh == "" {
return c, nil // nothing to renew it with; let the request try what there is
}
rctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 20*time.Second)
t, err := factoryRenew(rctx, c.Refresh, "")
cancel()
if err != nil {
// a hiccup while the token still runs: go on with it
if !factoryRefused(err) && c.ExpiresAt > 0 && time.Now().UnixMilli() < c.ExpiresAt {
return c, nil
}
return factoryCreds{}, factoryLapse(l.User, err)
}
c.Access, c.ExpiresAt = t.Access, factoryExpiry(t.Access)
if t.Refresh != "" {
c.Refresh = t.Refresh
}
return c, factoryEdit(l.User, c, true)
}
// ---- Factory's API ----------------------------------------------------------
// factoryHeaders are what droid sends on every call to Factory's API.
func factoryHeaders(h http.Header, c factoryCreds) {
h.Set("Authorization", "Bearer "+c.Access)
h.Set("X-Factory-Client", "cli")
h.Set("X-Client-Version", factoryVersion)
h.Set("User-Agent", "factory-cli/"+factoryVersion)
if c.Active != "" {
h.Set("X-Factory-Org-Id", c.Active)
}
}
// factoryOrgRefused is Factory's 403 for an X-Factory-Org-Id the user can't
// reach: "Requested active organization is not accessible by this user".
func factoryOrgRefused(status int, body []byte) bool {
return status == http.StatusForbidden && strings.Contains(strings.ToLower(string(body)), "active organization is not accessible")
}
// factoryMendOrg answers Factory refusing an account's org: the active org
// it named is dropped, so the request goes again without the header and
// Factory takes the token's own (droid's org picker retries "without the
// active-org header" so); with no header sent, the token is put in the
// first org /api/cli/org lists, as droid does for a token with none. True
// when there was something to change and the request is worth resending.
func factoryMendOrg(ctx context.Context, user string, status int, body []byte) bool {
if !factoryOrgRefused(status, body) {
return false
}
factoryMu.Lock()
defer factoryMu.Unlock()
l, ok := factoryLookup(user)
if !ok {
return false
}
c, valid := factorySaved(l)
if !valid {
return false
}
if c.Active != "" {
c.Active = ""
return factoryEdit(l.User, c, false) == nil
}
if c.Refresh == "" {
return false
}
rctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 20*time.Second)
defer cancel()
org, err := factoryFirstOrg(rctx, c)
if err != nil || org == "" {
return false
}
t, err := factoryRenew(rctx, c.Refresh, org)
if err != nil {
return false
}
c.Access, c.ExpiresAt, c.Org = t.Access, factoryExpiry(t.Access), org
if t.Refresh != "" {
c.Refresh = t.Refresh
}
return factoryEdit(l.User, c, true) == nil
}
// factoryFirstOrg is the first WorkOS org /api/cli/org says the account is
// in, "" for none. droid asks it with the bearer token alone.
func factoryFirstOrg(ctx context.Context, c factoryCreds) (string, error) {
var orgs struct {
IDs []string `json:"workosOrgIds"`
}
c.Active = ""
if err := factoryGet(ctx, c, "/api/cli/org", nil, &orgs); err != nil {
return "", err
}
if len(orgs.IDs) == 0 {
return "", nil
}
return orgs.IDs[0], nil
}
// factoryGet reads a JSON answer from Factory's API.
func factoryGet(ctx context.Context, c factoryCreds, path string, extra map[string]string, v any) error {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.base()+path, nil)
if err != nil {
return err
}
factoryHeaders(req.Header, c)
for k, x := range extra {
req.Header.Set(k, x)
}
req.Header.Set("Accept", "application/json")
res, err := factoryClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
b, _ := io.ReadAll(io.LimitReader(res.Body, 1<<20))
if res.StatusCode != http.StatusOK {
return &factoryStatus{res.StatusCode, "Factory: " + APIError(b, res.Status)}
}
return json.Unmarshal(b, v)
}
// ---- models ---------------------------------------------------------------
// factoryModel is one of the models Factory serves a subscription, as
// droid's registry lists it.
type factoryModel struct {
id, name string
api Protocol // the wire droid sends it on
upstream string // the vendor droid names in x-api-provider
context int
output int
efforts []string
images bool
}
// factoryModels are the ones droid's /model picker offers, less Gemini's
// (sent on a route of Factory's own) and auto (droid picks it client side).
var factoryModels = []factoryModel{
{"claude-fable-5.1", "Fable 5.1", Anthropic, "anthropic", 867000, 128000, []string{"low", "medium", "high", "xhigh", "max"}, true},
{"claude-fable-5", "Fable 5", Anthropic, "anthropic", 867000, 128000, []string{"low", "medium", "high", "xhigh", "max"}, true},
{"claude-opus-5-5", "Opus 5.5", Anthropic, "anthropic", 872000, 128000, []string{"low", "medium", "high", "xhigh", "max"}, true},
{"claude-opus-5", "Opus 5", Anthropic, "anthropic", 867000, 128000, []string{"low", "medium", "high", "xhigh", "max"}, true},
{"claude-opus-4-8", "Opus 4.8", Anthropic, "anthropic", 867000, 128000, []string{"low", "medium", "high", "xhigh", "max"}, true},
{"claude-sonnet-5-5", "Sonnet 5.5", Anthropic, "anthropic", 872000, 128000, []string{"low", "medium", "high", "xhigh", "max"}, true},
{"claude-sonnet-5", "Sonnet 5", Anthropic, "anthropic", 872000, 128000, []string{"low", "medium", "high", "xhigh", "max"}, true},
{"claude-sonnet-4-6", "Sonnet 4.6", Anthropic, "anthropic", 931000, 64000, []string{"low", "medium", "high", "max"}, true},
{"claude-haiku-4-5-20251001", "Haiku 4.5", Anthropic, "anthropic", 0, 0, []string{"low", "medium", "high"}, true},
{"gpt-6-sol", "GPT-6 Sol", Responses, "openai", 1050000, 128000, []string{"none", "low", "medium", "high", "xhigh", "max"}, true},
{"gpt-6-astra", "GPT-6 Astra", Responses, "openai", 1050000, 128000, []string{"low", "medium", "high", "xhigh", "max"}, true},
{"gpt-6-luna", "GPT-6 Luna", Responses, "openai", 1050000, 128000, []string{"none", "low", "medium", "high", "xhigh", "max"}, true},
{"gpt-5.6-sol", "GPT-5.6 Sol", Responses, "openai", 1050000, 128000, []string{"none", "low", "medium", "high", "xhigh", "max"}, true},
{"gpt-5.6-terra", "GPT-5.6 Terra", Responses, "openai", 1050000, 128000, []string{"none", "low", "medium", "high", "xhigh", "max"}, true},
{"gpt-5.6-luna", "GPT-5.6 Luna", Responses, "openai", 1050000, 128000, []string{"none", "low", "medium", "high", "xhigh", "max"}, true},
{"gpt-5.5", "GPT-5.5", Responses, "openai", 1050000, 128000, []string{"low", "medium", "high", "xhigh"}, true},
{"gpt-5.4", "GPT-5.4", Responses, "openai", 1050000, 128000, []string{"low", "medium", "high", "xhigh"}, true},
{"gpt-5.3-codex", "GPT-5.3-Codex", Responses, "openai", 400000, 128000, []string{"low", "medium", "high", "xhigh"}, true},
{"grok-4.7", "Grok 4.7", Responses, "xai", 500000, 63356, []string{"low", "medium", "high", "xhigh"}, true},
{"grok-4.6", "Grok 4.6", Responses, "xai", 200000, 63356, []string{"low", "medium", "high", "xhigh"}, true},
{"glm-5.3", "GLM-5.3", Chat, "fireworks", 1040000, 131072, []string{"low", "high", "max"}, false},
{"glm-5.3-flash", "GLM-5.3-Flash", Chat, "fireworks", 1048576, 131072, []string{"low", "high", "max"}, false},
{"glm-5.2", "GLM-5.2", Chat, "baseten", 1040000, 131072, []string{"high", "max"}, false},
{"kimi-k3", "Kimi K3", Chat, "fireworks", 262144, 65536, []string{"low", "high", "max"}, true},
{"deepseek-v4.1-flash", "DeepSeek V4.1 Flash", Chat, "fireworks", 1040000, 131072, []string{"low", "high", "max"}, true},
{"qwen3.8-max", "Qwen3.8 Max", Chat, "fireworks", 262144, 131072, []string{"low", "medium", "xhigh"}, false},
{"minimax-m3", "MiniMax M3", Chat, "fireworks", 512000, 64000, []string{"high"}, true},
{"minimax-m2.7", "MiniMax M2.7", Anthropic, "fireworks", 196600, 64000, []string{"high"}, false},
{"mistral-medium-3.5", "Mistral Medium 3.5", Chat, "mistral", 256000, 64000, []string{"high"}, true},
{"nemotron-3-ultra", "Nemotron 3 Ultra", Chat, "baseten", 202000, 65536, []string{"high"}, false},
}
// factoryCore is whether a model is one of the open ones Factory hosts,
// billed to Droid Core; droid's registry has every one of them as provider
// "factory", served by Fireworks, Baseten or Mistral.
func factoryCore(id string) bool {
m, ok := factoryModelOf(id)
return ok && m.upstream != "anthropic" && m.upstream != "openai" && m.upstream != "xai"
}
func factoryModelOf(id string) (factoryModel, bool) {
for _, m := range factoryModels {
if m.id == id {
return m, true
}
}
return factoryModel{}, false
}
// factoryCatalog is the models as the picker lists them, each with the one
// API Factory serves it on.
func factoryCatalog() []catalog.Model {
out := make([]catalog.Model, 0, len(factoryModels))
for _, m := range factoryModels {
out = append(out, catalog.Model{ID: m.id, Name: m.name, Provider: "factory", Efforts: m.efforts,
APIs: []string{string(m.api)}, Images: m.images, Context: m.context, Output: m.output})
}
return out
}
// factoryAPIs is the API a model is served on; nil for one droid didn't
// list, which may be tried on any.
func factoryAPIs(model string) []Protocol {
if m, ok := factoryModelOf(model); ok {
return []Protocol{m.api}
}
return nil
}
// ---- the provider ---------------------------------------------------------
func factoryProvider(a factoryLogin) Provider {
user := a.User
acct := &Account{Agent: "factory", User: user, Plan: a.Plan}
acct.sign = func(ctx context.Context, req *http.Request, body []byte) error {
c, err := factoryFresh(ctx, user)
if err != nil {
return err
}
// an EU org is served from Factory's EU region: the request goes there
if base := c.base(); base != factoryAPI && strings.HasPrefix(req.URL.String(), factoryAPI) {
if u, err := url.Parse(base + strings.TrimPrefix(req.URL.String(), factoryAPI)); err == nil {
req.URL, req.Host = u, u.Host
}
}
factoryHeaders(req.Header, c)
model := bodyModel(body)
upstream := "anthropic"
if m, ok := factoryModelOf(model); ok {
upstream = m.upstream
} else if strings.Contains(req.URL.Path, "/llm/o/") {
upstream = "openai"
}
req.Header.Set("x-api-provider", upstream)
req.Header.Set("x-session-id", factorySession)
req.Header.Set("x-assistant-message-id", randomUUID())
req.Header.Set("x-provider-routing-source", "registry_default")
if upstream == "openai" {
req.Header.Set("OpenAI-Platform", "org-bHuLtG1fGmYk5YaOihAAXFBw")
}
return nil
}
acct.retry = func(ctx context.Context, status int, body []byte) bool {
return factoryMendOrg(ctx, user, status, body)
}
acct.models = factoryCatalog
acct.fetch = func(ctx context.Context) ([]catalog.Model, error) {
ms := factoryCatalog()
return ms, catalog.SaveLive("factory", factoryAPI+"/api/llm/o/v1", ms)
}
return Provider{ID: "factory", Name: "Factory", Icon: "factory",
Anthropic: factoryAPI + "/api/llm/a", Responses: factoryAPI + "/api/llm/o/v1", Chat: factoryAPI + "/api/llm/o/v1",
Website: "https://factory.ai", Account: acct}
}
func factoryAccount() (Provider, bool) {
ls := factoryLogins()
if len(ls) == 0 {
return Provider{}, false
}
return factoryProvider(ls[0]), true
}
// factoryAlsoOn is the Factory accounts in use behind the first.
func factoryAlsoOn() []Provider {
var out []Provider
for _, l := range factoryLogins() {
if !l.Active && l.On {
out = append(out, factoryProvider(l))
}
}
return out
}