@magpie-community/opencode-workbuddy-auth 0.1.18 → 0.1.19

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 +6 -0
  2. package/index.mjs +31 -1
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -134,6 +134,12 @@ client reads. The daily Free Plan slice isn't counted: it ends at the same
134
134
  midnight on every account. magpie's smart routing spends the account whose
135
135
  credits run out first.
136
136
 
137
+ An enterprise (team) account's resource summary lists no packages. Its
138
+ credits are read from the enterprise meter
139
+ (`/billing/meter/get-enterprise-user-usage`), the one WorkBuddy's own usage
140
+ page reads: the cycle's credits used of its limit, and when the cycle
141
+ resets (magpie #1904).
142
+
137
143
  ## Not here
138
144
 
139
145
  magpie's built-in WorkBuddy does more than this package:
package/index.mjs CHANGED
@@ -1056,6 +1056,25 @@ function usageOf(sum, plan) {
1056
1056
  return out
1057
1057
  }
1058
1058
 
1059
+ // enterpriseUsageOf is an enterprise (team) account's credits, from
1060
+ // /billing/meter/get-enterprise-user-usage, the meter WorkBuddy's own usage
1061
+ // page (www.workbuddy.cn/profile/usage) reads for one (magpie #1904,
1062
+ // CnsMaple): the resource summary answers such an account with no
1063
+ // packages at all. It counts the cycle's credits flat — credit used of
1064
+ // limitNum — and when the cycle began and resets (UTC+8). null when it
1065
+ // grants none.
1066
+ function enterpriseUsageOf(u) {
1067
+ const limit = capacity(u?.limitNum)
1068
+ if (!(limit > 0)) return null
1069
+ const used = capacity(u?.credit)
1070
+ const w = { name: "Credits", used: (100 * used) / limit, display: `${compact(used)} / ${compact(limit)}`, amount: used, limit, unit: "credits" }
1071
+ const at = (v) => (typeof v === "string" && /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/.test(v.trim()) ? Date.parse(v.trim().replace(" ", "T") + "+08:00") : 0)
1072
+ const reset = at(u?.cycleResetTime), start = at(u?.cycleStartTime)
1073
+ if (reset > 0) w.resetsAt = new Date(reset).toISOString()
1074
+ if (reset > start && start > 0) w.span = (reset - start) / 1000
1075
+ return w
1076
+ }
1077
+
1059
1078
  // The packages' codes, as WorkBuddy's own client lists them when it asks
1060
1079
  // for each package's credits (its COMMODITY_CODES): the API refuses a
1061
1080
  // detail list without them (10001 "PackageCodes required").
@@ -1325,6 +1344,17 @@ function makePlugin(site) {
1325
1344
  // the meter has no /v2 prefix, on either site
1326
1345
  const sum = await meter(site, a, "/billing/meter/get-user-resource-summary")
1327
1346
  const out = usageOf(sum, auth.plan)
1347
+ // an enterprise account's summary has no packages: its credits
1348
+ // are the enterprise meter's (#1904). A personal account's
1349
+ // refusal of it (10001 "uid or enterpriseID is empty") leaves
1350
+ // the card as the summary said.
1351
+ if (!out.windows.length) {
1352
+ let ent = null
1353
+ try {
1354
+ ent = enterpriseUsageOf(await meter(site, a, "/billing/meter/get-enterprise-user-usage"))
1355
+ } catch {}
1356
+ if (ent) return { plan: auth.plan || "Enterprise", windows: [ent], signIn: "kept" }
1357
+ }
1328
1358
  // when the credits left run out, for routing to spend them first;
1329
1359
  // a list that can't be read leaves it unknown, the card stands
1330
1360
  const credits = out.windows.find((w) => w.name === "Credits")
@@ -1438,4 +1468,4 @@ export const WorkBuddyAuthPlugin = makePlugin(SITES.workbuddy)
1438
1468
  export const WorkBuddyAIAuthPlugin = makePlugin(SITES["workbuddy-ai"])
1439
1469
 
1440
1470
  // for tests
1441
- export const _internal = { notDesktop, textOnly, runsOutOf, packagesOf, endOf, enterpriseHeaders, enterpriseOf, liveModels, browserSignIn, withSystem, normalizeToolChoice, fitTools, fitted, renamed, unflagged, usageOf, desktopHeld, explained, REFUSED_HINT, RESTRICTED_HINT, EXHAUSTED_HINT, freeCredits, creditsOf, fresh, SITES, renewing, renewed, SESSION, attend, turnKey, conversationKey, signatureOf }
1471
+ export const _internal = { enterpriseUsageOf, notDesktop, textOnly, runsOutOf, packagesOf, endOf, enterpriseHeaders, enterpriseOf, liveModels, browserSignIn, withSystem, normalizeToolChoice, fitTools, fitted, renamed, unflagged, usageOf, desktopHeld, explained, REFUSED_HINT, RESTRICTED_HINT, EXHAUSTED_HINT, freeCredits, creditsOf, fresh, SITES, renewing, renewed, SESSION, attend, turnKey, conversationKey, signatureOf }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-workbuddy-auth",
3
- "version": "0.1.18",
3
+ "version": "0.1.19",
4
4
  "description": "OpenCode provider plugins for WorkBuddy's plans: WorkBuddy (CodeBuddy, copilot.tencent.com) and WorkBuddy AI (www.workbuddy.ai)",
5
5
  "type": "module",
6
6
  "main": "./index.mjs",