"factory" joins retiredBuiltins: every Factory account is served by
@magpie-community/opencode-factory-auth, and the hourly MoveRetiring
moves the ones still on the built-in.
Removed: the built-in's sign-in (factory_signin.go, WorkOS's device
flow), keeping the sign-in (renew, lapse, org reconcile and mend,
headers, factoryGet), its provider and account (factoryProvider,
factoryAccount, factoryAlsoOn), its login list, switch, on/off and
forget, its usage card (factoryLoginQuota), droid's prompt and body
rewriting (factory_client.go), and its rows in signin.go, logins.go,
logins_on.go, login_usage.go, subscription_usage.go and account.go.
zcodeDecode/Encode, kept in zcode.go only for factory_client.go, go
with it. An API key always goes to the plugin now (factoryAddKey), and
`magpie accounts import factory` keeps saying to add it with accounts
add factory; its dead import branches are gone.
Kept: the mover and what it reads (factoryLogins, factorySaved,
factoryCreds, factoryMu); ImportFactoryKeys, factory_key.go and
factoryWhoami, for "Use an API key instead"; droid's model table with
factoryAPIs and factoryCore, since the plugin's provider is named
"factory" (account.go APIs, FactoryGemini and servedByFactory still
answer for it); factoryAPI for the moved provider's host
(builtinHost); factory_usage.go's limits reader behind
FactoryAllowanceForTest, which the gateway's pool tests use; the
gateway's "factory" lists (estimatedMoved, the account check), as the
Zed commit kept zed's; the parity tests.
Tests:
- git rm'd, built-in only: factory_test.go (sign-in, refresh, accounts,
orgs), factory_client_test.go, gateway factory_agents_test.go
(droid's line on every wire, the generate route to chat) and
factory_think_off_test.go (TestPluginThinkingOff covers the plugin);
TestFactoryAPIKey, TestFactoryKeyAgainReadsUsageAfresh,
TestFactoryServesModelsAddedSinceItsFetch, TestUserAgentAccountUntouched
(TestUserAgentFor still covers an account's User-Agent).
- Changed meaning: TestGeminiEmptyReplyIsAnError,
TestGeminiEmptyRepliesAskedAgain and TestGeminiImageModelReturnsItsImage
used Factory's generate route as a fake-able Gemini upstream; they now
use a Gemini API of the user's own (saveGem), the same decoder, and the
image test checks :streamGenerateContent instead of /api/llm/g/v1/generate.
TestMovedShowsBuiltinHost's first check is now a plain plugin
(fakeco), and a retired Factory with no migration record shows
api.factory.ai. TestFactoryAPIKeyMoved checks factoryLogins() is empty
instead of the removed factoryLookup. factorySite/factoryJSON moved
into factory_key_test.go.
Run in this worktree:
gofmt -l internal: nothing
go vet -tags nogui ./internal/provider ./internal/gateway ./internal/cli
./internal/gui, GOOS=darwin, linux, windows: clean
staticcheck -checks U1000 -tests=false (darwin, windows) on
internal/provider: nothing new
go test -tags nogui -count=1 -run 'Factory|Gemini|PaymentRequired|
ResetsInWords|UserAgent|UsedWindow|Vertex|PayloadTooLarge|UsageCache|
AnthropicImage|ThinkingOff|Moved|Retired|Mover|Migrat|Move|
SayTheirPlugin|Allowance|Discovery|ClaudeModels|GrokPrice|
LoopbackSignIns|AsksOwnBackend|SideLogin|Import|Accounts|ZCode|MiMo'
on the four packages: ok (all four); TestFactoryAPIKeyMoved ran with
Bun (PASS), not skipped.
Not run: whole packages, the full suite, the GUI suite (machine load
~300). Not tried with a real Factory account. docs/subsystems not
changed, as for Zed.
109 lines
3.3 KiB
Go
109 lines
3.3 KiB
Go
package provider
|
|
|
|
// PLUGIN-SERVED (see AGENTS.md): Factory ("factory") is a retired built-in
|
|
// subscription, served by its plugin alone,
|
|
// @magpie-community/opencode-factory-auth (provider.BuiltinRetired). What
|
|
// is left here reads /api/billing/limits for tests of the gateway's pool
|
|
// routing (FactoryAllowanceForTest).
|
|
|
|
// How much of a Factory plan an account has used, as droid's /status asks
|
|
// it: GET /api/billing/limits. Standard usage runs in rolling 5-hour, weekly
|
|
// and monthly windows, each a percent used and when it ends; Droid Core, the
|
|
// open models Factory hosts, has windows of its own; extra usage is a
|
|
// balance in cents.
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
type factoryWindow struct {
|
|
Used float64 `json:"usedPercent"`
|
|
End json.RawMessage `json:"windowEnd"`
|
|
}
|
|
|
|
type factoryPool struct {
|
|
FiveHour *factoryWindow `json:"fiveHour"`
|
|
Weekly *factoryWindow `json:"weekly"`
|
|
Monthly *factoryWindow `json:"monthly"`
|
|
}
|
|
|
|
type factoryLimits struct {
|
|
Limits struct {
|
|
Standard *factoryPool `json:"standard"`
|
|
Core *factoryPool `json:"core"`
|
|
} `json:"limits"`
|
|
Extra *float64 `json:"extraUsageBalanceCents"`
|
|
ExtraAllowed bool `json:"extraUsageAllowed"`
|
|
}
|
|
|
|
// factoryWhen reads a window's end: an ISO time or epoch milliseconds, as
|
|
// droid hands either to new Date.
|
|
func factoryWhen(raw json.RawMessage) *time.Time {
|
|
var s string
|
|
if json.Unmarshal(raw, &s) == nil && s != "" {
|
|
if t, err := time.Parse(time.RFC3339Nano, s); err == nil {
|
|
return &t
|
|
}
|
|
if n, err := strconv.ParseInt(s, 10, 64); err == nil && n > 0 {
|
|
t := time.UnixMilli(n).UTC()
|
|
return &t
|
|
}
|
|
return nil
|
|
}
|
|
var n float64
|
|
if json.Unmarshal(raw, &n) == nil && n > 0 {
|
|
t := time.UnixMilli(int64(n)).UTC()
|
|
return &t
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// factoryQuota turns the limits into windows: standard's by their span,
|
|
// then Droid Core's, then the extra usage left. For routing, standard counts
|
|
// the vendors' models and Core the open ones Factory hosts; with extra usage
|
|
// to spend, a window used up stops neither.
|
|
func factoryQuota(l factoryLimits) []QuotaWindow {
|
|
var out []QuotaWindow
|
|
extra := l.ExtraAllowed && l.Extra != nil && *l.Extra > 0
|
|
add := func(p *factoryPool, prefix string, core bool) {
|
|
if p == nil {
|
|
return
|
|
}
|
|
for _, w := range []struct {
|
|
win *factoryWindow
|
|
name string
|
|
span time.Duration
|
|
}{
|
|
{p.FiveHour, "5 hours", 5 * time.Hour},
|
|
{p.Weekly, "7 days", 7 * 24 * time.Hour},
|
|
{p.Monthly, "30 days", 30 * 24 * time.Hour},
|
|
} {
|
|
if w.win == nil {
|
|
continue
|
|
}
|
|
out = append(out, QuotaWindow{Name: prefix + w.name, Used: min(max(w.win.Used, 0), 100),
|
|
Span: w.span, ResetsAt: factoryWhen(w.win.End), Aside: extra,
|
|
matches: func(model string) bool { return factoryCore(model) == core }})
|
|
}
|
|
}
|
|
add(l.Limits.Standard, "", false)
|
|
add(l.Limits.Core, "Droid Core · ", true)
|
|
if l.Extra != nil && *l.Extra > 0 {
|
|
out = append(out, QuotaWindow{Name: "Extra usage", Display: fmt.Sprintf("$%.2f", *l.Extra/100), Aside: true})
|
|
}
|
|
return out
|
|
}
|
|
|
|
// FactoryAllowanceForTest is the allowance an account's /api/billing/limits
|
|
// answer gives routing, for the gateway's tests.
|
|
func FactoryAllowanceForTest(limits []byte, now time.Time) Allowance {
|
|
var l factoryLimits
|
|
if json.Unmarshal(limits, &l) != nil {
|
|
return nil
|
|
}
|
|
return allowanceOf(factoryQuota(l), now)
|
|
}
|