@magpie-community/opencode-workbuddy-auth 0.1.17 → 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 +8 -1
  2. package/index.mjs +46 -3
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -24,7 +24,8 @@ The package exports `WorkBuddyAuthPlugin` (`workbuddy`) and
24
24
  - Windows: `~/AppData/Local/CodeBuddyExtension/…`
25
25
  - Linux: `~/.local/share/CodeBuddyExtension/…`
26
26
 
27
- A sign-in the app keeps encrypted can't be read, and this way fails.
27
+ WorkBuddy 5.6 and later keep the sign-in encrypted, which only the app
28
+ opens, so this way fails there and says so: use the browser sign-in.
28
29
 
29
30
  A browser sign-in is kept as an OpenCode `oauth` auth: the access and
30
31
  refresh tokens, their expiry, the user id and the domain; desktop's as a
@@ -133,6 +134,12 @@ client reads. The daily Free Plan slice isn't counted: it ends at the same
133
134
  midnight on every account. magpie's smart routing spends the account whose
134
135
  credits run out first.
135
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
+
136
143
  ## Not here
137
144
 
138
145
  magpie's built-in WorkBuddy does more than this package:
package/index.mjs CHANGED
@@ -931,6 +931,19 @@ function readDesktop(site) {
931
931
  }
932
932
  }
933
933
 
934
+ // notDesktop says why there is no desktop sign-in to use: WorkBuddy 5.6
935
+ // and later seal its tokens at rest ({$wbEncrypted: 1, envelope}), which
936
+ // only the app itself opens (plugins#18), so the browser sign-in is the way;
937
+ // else the app isn't signed in.
938
+ function notDesktop(site) {
939
+ try {
940
+ const t = JSON.parse(readFileSync(desktopFile(site), "utf8"))?.auth ?? {}
941
+ if (t.accessToken?.$wbEncrypted || t.refreshToken?.$wbEncrypted)
942
+ return `${site.name} desktop keeps its sign-in encrypted (WorkBuddy 5.6 and later), which magpie can't read; sign in with "${site.name} account (browser)" instead`
943
+ } catch {}
944
+ return `${site.name} desktop isn't signed in`
945
+ }
946
+
934
947
  // desktopSignIn uses the account WorkBuddy desktop is signed in to: nothing
935
948
  // is copied (a copied refresh token would be the app's and the plugin's
936
949
  // both, and the first to use it would sign the other out); its file is
@@ -942,7 +955,7 @@ function desktopSignIn(site) {
942
955
  method: "auto",
943
956
  async callback() {
944
957
  const d = readDesktop(site)
945
- if (!d) return { type: "failed", error: `${site.name} desktop isn't signed in` }
958
+ if (!d) return { type: "failed", error: notDesktop(site) }
946
959
  return { type: "success", refresh: "", access: "", expires: 0, source: "desktop", accountId: d.name || site.name, uid: d.uid }
947
960
  },
948
961
  }
@@ -957,7 +970,7 @@ const desktopHeld = new Map()
957
970
  async function current(site, client, auth) {
958
971
  if (auth?.source !== "desktop") return fresh(site, client, auth)
959
972
  const d = readDesktop(site)
960
- if (!d) throw new Error(`${site.name} desktop isn't signed in`)
973
+ if (!d) throw new Error(notDesktop(site))
961
974
  const k = site.id + "|" + d.uid
962
975
  const h = desktopHeld.get(k)
963
976
  const a = await fresh(site, null, h && h.expires >= d.expires ? h : d)
@@ -1043,6 +1056,25 @@ function usageOf(sum, plan) {
1043
1056
  return out
1044
1057
  }
1045
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
+
1046
1078
  // The packages' codes, as WorkBuddy's own client lists them when it asks
1047
1079
  // for each package's credits (its COMMODITY_CODES): the API refuses a
1048
1080
  // detail list without them (10001 "PackageCodes required").
@@ -1312,6 +1344,17 @@ function makePlugin(site) {
1312
1344
  // the meter has no /v2 prefix, on either site
1313
1345
  const sum = await meter(site, a, "/billing/meter/get-user-resource-summary")
1314
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
+ }
1315
1358
  // when the credits left run out, for routing to spend them first;
1316
1359
  // a list that can't be read leaves it unknown, the card stands
1317
1360
  const credits = out.windows.find((w) => w.name === "Credits")
@@ -1425,4 +1468,4 @@ export const WorkBuddyAuthPlugin = makePlugin(SITES.workbuddy)
1425
1468
  export const WorkBuddyAIAuthPlugin = makePlugin(SITES["workbuddy-ai"])
1426
1469
 
1427
1470
  // for tests
1428
- export const _internal = { 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.17",
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",