@magpie-community/opencode-factory-auth 0.1.37 → 0.1.38

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +24 -0
  2. package/index.mjs +42 -8
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -115,6 +115,30 @@ magpie plugin add @magpie-community/opencode-factory-auth
115
115
  magpie plugin login factory
116
116
  ```
117
117
 
118
+ ## Standard usage only (optional)
119
+
120
+ In magpie, open the Factory provider, turn on **Standard usage only** under
121
+ **Plugin options**, and save (or set `"pluginOptions": {"factory":
122
+ {"standard_only": true}}` in magpie's settings.json). It is off by default,
123
+ applies to every Factory account, and takes effect at each account's next
124
+ usage reading.
125
+
126
+ When on, the plugin reports the Standard 5 hours, 7 days and 30 days
127
+ windows as counting every model, Droid Core's open models too, whether or
128
+ not the account has Extra usage. Droid Core's windows and Extra usage are
129
+ still shown but never keep an account in routing.
130
+
131
+ magpie leaves an account out before sending only when it is held at a
132
+ usage cap, so pair the option with a cap on each account, for example
133
+ `magpie provider account-cap factory <account> 99`. An account is then
134
+ skipped once any Standard window reaches the cap, the request goes to the
135
+ next account with the same model, and when every account is held magpie
136
+ answers 429 without sending. Without a cap, an account whose Standard
137
+ usage is gone is still tried, and Factory may serve a Droid Core model
138
+ from Core or Extra usage. Readings lag, so the last requests before a
139
+ window fills still go out; to never spend Extra usage, turn it off in
140
+ Factory.
141
+
118
142
  ## Claude Code through magpie
119
143
 
120
144
  Factory's Anthropic route requires Droid's fixed client preamble and
package/index.mjs CHANGED
@@ -983,11 +983,19 @@ function dollars(cents) {
983
983
  return "$" + ((lo % 2 === 0 ? lo : lo + 1) / 100).toFixed(2)
984
984
  }
985
985
 
986
+ // STANDARD_ONLY is the switch (magpie's plugin options, off by default)
987
+ // that keeps an account in routing only while its Standard usage lasts
988
+ // (magpie-community/plugins#349)
989
+ const STANDARD_ONLY = "standard_only"
990
+
986
991
  // limitWindows turns the limits into windows: standard's by their span,
987
992
  // then Droid Core's, then the extra usage left. For routing, standard counts
988
993
  // the vendors' models and Core the open ones Factory hosts; with extra usage
989
- // to spend, a window used up stops neither.
990
- function limitWindows(l) {
994
+ // to spend, a window used up stops neither. standardOnly (STANDARD_ONLY on)
995
+ // makes standard's windows count every model, Core's too, extra usage or
996
+ // not, and leaves Droid Core's and the extra usage to be shown only: an
997
+ // account stops when any of standard's windows is used up.
998
+ function limitWindows(l, standardOnly = false) {
991
999
  const out = []
992
1000
  const cents = typeof l.extraUsageBalanceCents === "number" ? l.extraUsageBalanceCents : null
993
1001
  const extra = l.extraUsageAllowed === true && cents !== null && cents > 0
@@ -1003,9 +1011,16 @@ function limitWindows(l) {
1003
1011
  const win = { name: prefix + name, used: Math.min(Math.max(used, 0), 100), span }
1004
1012
  const end = windowEnd(w.windowEnd)
1005
1013
  if (end) win.resetsAt = end
1006
- if (extra) win.aside = true
1007
- if (core) win.models = CORE
1008
- else win.notModels = CORE
1014
+ if (standardOnly) {
1015
+ if (core) {
1016
+ win.aside = true
1017
+ win.models = CORE
1018
+ }
1019
+ } else {
1020
+ if (extra) win.aside = true
1021
+ if (core) win.models = CORE
1022
+ else win.notModels = CORE
1023
+ }
1009
1024
  out.push(win)
1010
1025
  }
1011
1026
  }
@@ -1676,7 +1691,16 @@ function anthropicBody(body) {
1676
1691
  return changed ? JSON.stringify(request) : body
1677
1692
  }
1678
1693
 
1679
- export const FactoryAuthPlugin = async ({ client }) => {
1694
+ export const FactoryAuthPlugin = async ({ client, magpie }) => {
1695
+ // standardOnly is the user's STANDARD_ONLY as magpie has it now, read at
1696
+ // each usage reading so a change needs no restart; false outside magpie
1697
+ const standardOnly = () => {
1698
+ try {
1699
+ return magpie?.option?.(PROVIDER, STANDARD_ONLY) === true
1700
+ } catch {
1701
+ return false
1702
+ }
1703
+ }
1680
1704
  // the session id this process's requests carry
1681
1705
  const session = crypto.randomUUID()
1682
1706
  // serializes checking, rotating and saving tokens: WorkOS rotates the
@@ -1870,7 +1894,7 @@ export const FactoryAuthPlugin = async ({ client }) => {
1870
1894
  l = await limits()
1871
1895
  }
1872
1896
  if (!l?.limits?.standard) return { error: "Factory: the account reported no limits", signIn }
1873
- return { windows: limitWindows(l), signIn }
1897
+ return { windows: limitWindows(l, standardOnly()), signIn }
1874
1898
  } catch (e) {
1875
1899
  if (e?.lapsed) signIn = "expired"
1876
1900
  return { error: e?.message ?? String(e), signIn }
@@ -1924,6 +1948,16 @@ export const FactoryAuthPlugin = async ({ client }) => {
1924
1948
  // droid's FACTORY_API_KEY: the key is the bearer, never renewed
1925
1949
  { type: "api", label: "Factory API key (fk-…)", placeholder: "fk-…" },
1926
1950
  ],
1951
+ // magpie's own switches (input.magpie.option), read at each usage
1952
+ // reading
1953
+ options: [
1954
+ {
1955
+ key: STANDARD_ONLY,
1956
+ label: "Standard usage only",
1957
+ description:
1958
+ "Keeps an account in routing only while its Standard usage lasts: once any Standard window (5 hours, 7 days, 30 days) is used up, magpie moves every model, Droid Core's too, to the next account until the window resets. Droid Core and Extra usage are shown but never keep an account in routing.",
1959
+ },
1960
+ ],
1927
1961
  // magpie renews the sign-in MAGPIE_LEAD before its end, before its
1928
1962
  // requests and usage ask for it, and saves what this gives. Under
1929
1963
  // the same lock as the renewal before a request, so the two never
@@ -2068,4 +2102,4 @@ export const FactoryAuthPlugin = async ({ client }) => {
2068
2102
  }
2069
2103
 
2070
2104
  // for tests
2071
- export const _internal = { contentRefused, explain, fastBody, FAST_BETA, limitWindows, windowEnd, dollars, vendorError, CORE, droidBody, DROID_LINE, geminiBody, leaveOutAttachments, LEFT_OUT, TOO_LARGE_HINT, MODELS, PROMOTIONS, rateOf, configModels, providerModel, providerModels }
2105
+ export const _internal = { STANDARD_ONLY, contentRefused, explain, fastBody, FAST_BETA, limitWindows, windowEnd, dollars, vendorError, CORE, droidBody, DROID_LINE, geminiBody, leaveOutAttachments, LEFT_OUT, TOO_LARGE_HINT, MODELS, PROMOTIONS, rateOf, configModels, providerModel, providerModels }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-factory-auth",
3
- "version": "0.1.37",
3
+ "version": "0.1.38",
4
4
  "description": "Sign in to a Factory (Droid) subscription from OpenCode or magpie",
5
5
  "type": "module",
6
6
  "main": "./index.mjs",