@magpie-community/opencode-grok-auth 0.1.0 → 0.1.1

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 (2) hide show
  1. package/index.mjs +50 -0
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -12,6 +12,7 @@ import { existsSync, mkdirSync, readFileSync, realpathSync, rmSync, statSync } f
12
12
  import { homedir } from "node:os"
13
13
  import { delimiter, dirname, isAbsolute, join } from "node:path"
14
14
  import { randomBytes } from "node:crypto"
15
+ import { STATUS_CODES } from "node:http"
15
16
 
16
17
  const PROVIDER = "grok"
17
18
  const BASE = "https://cli-chat-proxy.grok.com/v1"
@@ -340,6 +341,42 @@ async function current() {
340
341
  }
341
342
  }
342
343
 
344
+ // ---- usage ----------------------------------------------------------------------
345
+
346
+ // How much of the subscription's allowance is gone, as magpie's built-in
347
+ // Grok account shows it (internal/provider/grok_usage.go) and the CLI's own
348
+ // /usage reads it: the credits of the current period, weekly for
349
+ // SuperGrok, and what on-demand spending has used of its cap.
350
+
351
+ const PERIODS = { DAILY: ["1 day", 24 * 3600], WEEKLY: ["7 days", 7 * 24 * 3600], MONTHLY: ["Month", 30 * 24 * 3600] }
352
+ const RFC3339 = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})$/
353
+
354
+ // statusText is a refused request's status as magpie says it (Go's
355
+ // http.StatusText).
356
+ const statusText = (s) =>
357
+ ({ 413: "Request Entity Too Large", 414: "Request URI Too Long", 416: "Requested Range Not Satisfiable", 418: "I'm a teapot", 509: "" })[s] ?? STATUS_CODES[s] ?? ""
358
+
359
+ async function usage(key) {
360
+ const res = await fetch(`${BASE}/billing?format=credits`, {
361
+ headers: { Authorization: `Bearer ${key}`, Accept: "application/json" },
362
+ signal: AbortSignal.timeout(15_000),
363
+ })
364
+ if (!res.ok) return { error: statusText(res.status), windows: [] }
365
+ const cfg = (await res.json())?.config ?? {}
366
+ const num = (v) => (typeof v === "number" ? v : 0)
367
+ let [name, span] = ["Allowance", 0]
368
+ let end = cfg.billingPeriodEnd
369
+ if (cfg.currentPeriod) {
370
+ ;[name, span] = PERIODS[String(cfg.currentPeriod.type ?? "").replace(/^USAGE_PERIOD_TYPE_/, "")] ?? ["Allowance", 0]
371
+ end = cfg.currentPeriod.end
372
+ }
373
+ const at = typeof end === "string" && RFC3339.test(end) && !Number.isNaN(Date.parse(end)) ? { resetsAt: end } : {}
374
+ const out = [{ name, used: num(cfg.creditUsagePercent), ...(span ? { span } : {}), ...at }]
375
+ const cap = num(cfg.onDemandCap?.val)
376
+ if (cap > 0) out.push({ name: "On-demand", used: (100 * num(cfg.onDemandUsed?.val)) / cap, ...at, aside: true })
377
+ return { windows: out }
378
+ }
379
+
343
380
  // ---- the plugin ---------------------------------------------------------------
344
381
 
345
382
  export const GrokAuthPlugin = async ({ client }) => {
@@ -409,6 +446,19 @@ export const GrokAuthPlugin = async ({ client }) => {
409
446
  authorize: current,
410
447
  },
411
448
  ],
449
+ // magpie's: how much of the subscription's allowance is gone
450
+ async usage(getAuth) {
451
+ const auth = await getAuth()
452
+ let c
453
+ try {
454
+ if (!auth || auth.type !== "oauth" || !auth.refresh) throw new Error("Grok is not signed in; run `grok login`")
455
+ c = await token(auth.refresh, false)
456
+ } catch (e) {
457
+ return { error: e.message, windows: [] }
458
+ }
459
+ await remember(auth, c)
460
+ return usage(c.key)
461
+ },
412
462
  },
413
463
 
414
464
  provider: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-grok-auth",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "OpenCode provider plugin: Grok (SuperGrok / X Premium+) through the Grok Build CLI's sign-in",
5
5
  "type": "module",
6
6
  "main": "./index.mjs",