@magpie-community/opencode-workbuddy-auth 0.1.9 → 0.1.10

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 +25 -6
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -761,6 +761,15 @@ const REFUSED_HINT =
761
761
  "WorkBuddy refuses chats from Codex and Claude Code (their system prompt); use it from Hermes, OpenCode or Pi, or add another provider to this group"
762
762
  const REFUSED = /unapproved channel|illegal api invocation/i
763
763
 
764
+ // EXHAUSTED_HINT explains WorkBuddy's requirement of an active package
765
+ // even for 0-rate / free models. When an account has no packages or 0 credits,
766
+ // WorkBuddy returns code 14018 ("Credits exhausted...").
767
+ const EXHAUSTED_HINT =
768
+ "WorkBuddy requires an active credit package even for 0-rate free models; check your account package balance"
769
+ // The code is matched whole: a code that only begins with 14018 (140185) is
770
+ // another error, and insufficient_quota would rest the account as out of credit.
771
+ const EXHAUSTED_TEXT = /credits exhausted|额度已用尽|["']code["']\s*:\s*["']?14018(?![\d.])/i
772
+
764
773
  // kept is an answer that went through, saying the sign-in is kept: the
765
774
  // built-in never cleared a WorkBuddy account's mark, as it never set one.
766
775
  function kept(res) {
@@ -769,7 +778,7 @@ function kept(res) {
769
778
  return new Response(res.body, { status: res.status, statusText: res.statusText, headers })
770
779
  }
771
780
 
772
- // explained is res with REFUSED_HINT added to that refusal's message.
781
+ // explained is res with REFUSED_HINT or EXHAUSTED_HINT added to that refusal's message.
773
782
  // Each refusal says the sign-in is kept: the built-in passed WorkBuddy's
774
783
  // 401 on and never marked a WorkBuddy account lapsed.
775
784
  async function explained(res) {
@@ -781,27 +790,37 @@ async function explained(res) {
781
790
  headers.set("X-Magpie-Sign-In", "kept")
782
791
  return new Response(b, { status: res.status, statusText: res.statusText, headers })
783
792
  }
784
- if (!REFUSED.test(text)) return again(text)
785
- let msg = text.trim()
786
793
  let v
787
794
  try {
788
795
  v = JSON.parse(text)
789
796
  } catch {}
790
797
  const at = v?.error && typeof v.error === "object" ? v.error : v && typeof v === "object" ? v : null
798
+ const code = at?.data?.code ?? at?.code ?? v?.code ?? v?.data?.code
799
+ const isRefused = REFUSED.test(text)
800
+ const isExhausted = code === 14018 || Number(code) === 14018 || EXHAUSTED_TEXT.test(text)
801
+ if (!isRefused && !isExhausted) return again(text)
802
+
803
+ let msg = text.trim()
791
804
  if (at && typeof at.message === "string") msg = at.message
805
+ else if (typeof at?.data?.msg === "string") msg = at.data.msg
792
806
  else if (typeof v?.error === "string") msg = v.error
793
807
  else if (typeof v?.msg === "string" && v.msg) msg = v.msg // WorkBuddy's own {"code", "msg"}
794
- msg = `${msg} — ${REFUSED_HINT}`
808
+ else if (typeof v?.data?.msg === "string") msg = v.data.msg
809
+
810
+ const hint = isRefused ? REFUSED_HINT : EXHAUSTED_HINT
811
+ const type = isRefused ? "permission_error" : "insufficient_quota"
812
+ const errCode = isRefused ? null : 14018
813
+ msg = `${msg} — ${hint}`
795
814
  const headers = new Headers(res.headers)
796
815
  headers.set("content-type", "application/json")
797
816
  headers.delete("content-length")
798
817
  headers.delete("content-encoding")
799
818
  headers.set("X-Magpie-Sign-In", "kept")
800
- return new Response(JSON.stringify({ error: { message: msg, type: "permission_error", code: null } }), { status: res.status, statusText: res.statusText, headers })
819
+ return new Response(JSON.stringify({ error: { message: msg, type, code: errCode } }), { status: res.status, statusText: res.statusText, headers })
801
820
  }
802
821
 
803
822
  export const WorkBuddyAuthPlugin = makePlugin(SITES.workbuddy)
804
823
  export const WorkBuddyAIAuthPlugin = makePlugin(SITES["workbuddy-ai"])
805
824
 
806
825
  // for tests
807
- export const _internal = { withSystem, unflagged, usageOf, desktopHeld, explained, REFUSED_HINT, freeCredits, creditsOf, fresh, SITES, renewing, renewed }
826
+ export const _internal = { withSystem, unflagged, usageOf, desktopHeld, explained, REFUSED_HINT, EXHAUSTED_HINT, freeCredits, creditsOf, fresh, SITES, renewing, renewed }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-workbuddy-auth",
3
- "version": "0.1.9",
3
+ "version": "0.1.10",
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",