@magpie-community/opencode-workbuddy-auth 0.1.4 → 0.1.5
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.
- package/index.mjs +32 -13
- package/package.json +1 -1
package/index.mjs
CHANGED
|
@@ -86,10 +86,11 @@ const api = (site) => site.endpoint + "/v2"
|
|
|
86
86
|
// ---- WorkBuddy's API ------------------------------------------------------
|
|
87
87
|
|
|
88
88
|
class WBError extends Error {
|
|
89
|
-
constructor(message, code, status) {
|
|
89
|
+
constructor(message, code, status, said) {
|
|
90
90
|
super(message)
|
|
91
91
|
this.code = code
|
|
92
92
|
this.status = status
|
|
93
|
+
this.said = said // as magpie's built-in (wbCall) worded it
|
|
93
94
|
}
|
|
94
95
|
}
|
|
95
96
|
|
|
@@ -112,11 +113,13 @@ async function call(site, method, path, headers = {}, body) {
|
|
|
112
113
|
try {
|
|
113
114
|
env = JSON.parse(text)
|
|
114
115
|
} catch {}
|
|
116
|
+
const code = Number.parseInt(env.code, 10) || 0
|
|
115
117
|
if (!res.ok) {
|
|
116
118
|
const why = env.msg || env.message || env.error?.message || text.slice(0, 200) || res.statusText
|
|
117
|
-
|
|
119
|
+
const said = code ? env.msg || `error ${code}` : env.msg || statusText(res.status)
|
|
120
|
+
throw new WBError(`${site.name}: ${why} (HTTP ${res.status}${env.code ? ", code " + env.code : ""})`, env.code, res.status, said)
|
|
118
121
|
}
|
|
119
|
-
if (env.code) throw new WBError(`${site.name}: ${env.msg || "error"} (${env.code})`, env.code, res.status)
|
|
122
|
+
if (env.code) throw new WBError(`${site.name}: ${env.msg || "error"} (${env.code})`, env.code, res.status, env.msg || `error ${code}`)
|
|
120
123
|
return env.data
|
|
121
124
|
}
|
|
122
125
|
|
|
@@ -145,13 +148,14 @@ function stale(a) {
|
|
|
145
148
|
|
|
146
149
|
// fresh is the sign-in with an access token that isn't about to end:
|
|
147
150
|
// refreshed (and saved) when it is. A refresh that fails, or a refresh
|
|
148
|
-
// token that has ended, leaves the access token there is.
|
|
151
|
+
// token that has ended, leaves the access token there is. Its failures
|
|
152
|
+
// are worded as the built-in's (wbFresh), "WorkBuddy" on both sites.
|
|
149
153
|
async function fresh(site, client, a) {
|
|
150
154
|
if (!stale(a)) return a
|
|
151
155
|
const refreshable = a.refresh && !(a.refreshExpiresAt && Date.now() >= a.refreshExpiresAt)
|
|
152
156
|
if (!refreshable) {
|
|
153
157
|
if (a.access) return a
|
|
154
|
-
throw new Error(
|
|
158
|
+
throw new Error("this WorkBuddy account is signed out; sign in again")
|
|
155
159
|
}
|
|
156
160
|
try {
|
|
157
161
|
const got = await call(site, "POST", "/v2/plugin/auth/token/refresh", {
|
|
@@ -159,13 +163,14 @@ async function fresh(site, client, a) {
|
|
|
159
163
|
"X-Auth-Refresh-Source": "plugin",
|
|
160
164
|
"X-Domain": domainOf(site, a),
|
|
161
165
|
}, {})
|
|
162
|
-
if (!got?.accessToken) throw new Error(
|
|
166
|
+
if (!got?.accessToken) throw Object.assign(new Error("WorkBuddy gave no refreshed token"), { bare: true })
|
|
163
167
|
const next = merge(a, got)
|
|
164
168
|
await client?.auth?.set?.({ path: { id: site.id }, body: next })
|
|
165
169
|
return next
|
|
166
170
|
} catch (e) {
|
|
167
171
|
if (a.access) return a
|
|
168
|
-
|
|
172
|
+
if (e?.bare) throw e
|
|
173
|
+
throw new Error(`WorkBuddy token refresh: ${e?.said ?? e?.message ?? e}`)
|
|
169
174
|
}
|
|
170
175
|
}
|
|
171
176
|
|
|
@@ -570,20 +575,22 @@ function makePlugin(site) {
|
|
|
570
575
|
let body = init?.body
|
|
571
576
|
if (body === undefined && req) body = await req.clone().text()
|
|
572
577
|
const res = await fetch(req ? req.url : input, { ...init, method: init?.method ?? req?.method, headers, body: withSystem(body) })
|
|
573
|
-
return res.status >= 400 ? explained(res) : res
|
|
578
|
+
return res.status >= 400 ? explained(res) : kept(res)
|
|
574
579
|
},
|
|
575
580
|
}
|
|
576
581
|
},
|
|
577
|
-
// the account's credits, as magpie's built-in showed them
|
|
582
|
+
// the account's credits, as magpie's built-in showed them; each
|
|
583
|
+
// read keeps the sign-in (signIn), as the built-in's neither marked
|
|
584
|
+
// an account lapsed nor cleared it
|
|
578
585
|
async usage(getAuth) {
|
|
579
586
|
const auth = await getAuth()
|
|
580
|
-
if (auth?.type !== "oauth" || !(auth.access || auth.source)) return { error: "not signed in" }
|
|
587
|
+
if (auth?.type !== "oauth" || !(auth.access || auth.source)) return { error: "not signed in", signIn: "kept" }
|
|
581
588
|
try {
|
|
582
589
|
const a = await current(site, client, auth)
|
|
583
590
|
// the meter has no /v2 prefix, on either site
|
|
584
|
-
return usageOf(await meter(site, a, "/billing/meter/get-user-resource-summary"), auth.plan)
|
|
591
|
+
return { ...usageOf(await meter(site, a, "/billing/meter/get-user-resource-summary"), auth.plan), signIn: "kept" }
|
|
585
592
|
} catch (e) {
|
|
586
|
-
return { error: e?.message ?? String(e) }
|
|
593
|
+
return { error: e?.message ?? String(e), signIn: "kept" }
|
|
587
594
|
}
|
|
588
595
|
},
|
|
589
596
|
methods: [
|
|
@@ -611,13 +618,24 @@ const REFUSED_HINT =
|
|
|
611
618
|
"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"
|
|
612
619
|
const REFUSED = /unapproved channel|illegal api invocation/i
|
|
613
620
|
|
|
621
|
+
// kept is an answer that went through, saying the sign-in is kept: the
|
|
622
|
+
// built-in never cleared a WorkBuddy account's mark, as it never set one.
|
|
623
|
+
function kept(res) {
|
|
624
|
+
const headers = new Headers(res.headers)
|
|
625
|
+
headers.set("X-Magpie-Sign-In", "kept")
|
|
626
|
+
return new Response(res.body, { status: res.status, statusText: res.statusText, headers })
|
|
627
|
+
}
|
|
628
|
+
|
|
614
629
|
// explained is res with REFUSED_HINT added to that refusal's message.
|
|
630
|
+
// Each refusal says the sign-in is kept: the built-in passed WorkBuddy's
|
|
631
|
+
// 401 on and never marked a WorkBuddy account lapsed.
|
|
615
632
|
async function explained(res) {
|
|
616
633
|
const text = await res.text()
|
|
617
634
|
const again = (b) => {
|
|
618
635
|
const headers = new Headers(res.headers)
|
|
619
636
|
headers.delete("content-length")
|
|
620
637
|
headers.delete("content-encoding")
|
|
638
|
+
headers.set("X-Magpie-Sign-In", "kept")
|
|
621
639
|
return new Response(b, { status: res.status, statusText: res.statusText, headers })
|
|
622
640
|
}
|
|
623
641
|
if (!REFUSED.test(text)) return again(text)
|
|
@@ -635,6 +653,7 @@ async function explained(res) {
|
|
|
635
653
|
headers.set("content-type", "application/json")
|
|
636
654
|
headers.delete("content-length")
|
|
637
655
|
headers.delete("content-encoding")
|
|
656
|
+
headers.set("X-Magpie-Sign-In", "kept")
|
|
638
657
|
return new Response(JSON.stringify({ error: { message: msg, type: "permission_error", code: null } }), { status: res.status, statusText: res.statusText, headers })
|
|
639
658
|
}
|
|
640
659
|
|
|
@@ -642,4 +661,4 @@ export const WorkBuddyAuthPlugin = makePlugin(SITES.workbuddy)
|
|
|
642
661
|
export const WorkBuddyAIAuthPlugin = makePlugin(SITES["workbuddy-ai"])
|
|
643
662
|
|
|
644
663
|
// for tests
|
|
645
|
-
export const _internal = { withSystem, usageOf, desktopHeld, explained, REFUSED_HINT, freeCredits }
|
|
664
|
+
export const _internal = { withSystem, usageOf, desktopHeld, explained, REFUSED_HINT, freeCredits, fresh, SITES }
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@magpie-community/opencode-workbuddy-auth",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
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",
|