@magpie-community/opencode-workbuddy-auth 0.1.3 → 0.1.4

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 +26 -6
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -194,6 +194,8 @@ function sign(site, a, headers) {
194
194
  // withSystem gives a chat that doesn't open with a system message
195
195
  // WorkBuddy's default one, as its app does.
196
196
  function withSystem(body) {
197
+ // a body magpie or OpenCode hands over as bytes is read as the text it is
198
+ if (body instanceof ArrayBuffer || ArrayBuffer.isView(body)) body = new TextDecoder().decode(body)
197
199
  if (typeof body !== "string") return body
198
200
  try {
199
201
  const b = JSON.parse(body)
@@ -344,10 +346,27 @@ async function current(site, client, auth) {
344
346
 
345
347
  // ---- allowance ----------------------------------------------------------------
346
348
 
347
- // STATUS_TEXT is Go's http.StatusText for the answers WorkBuddy gives.
348
- const STATUS_TEXT = { 400: "Bad Request", 401: "Unauthorized", 403: "Forbidden", 404: "Not Found", 405: "Method Not Allowed",
349
- 408: "Request Timeout", 429: "Too Many Requests", 500: "Internal Server Error", 502: "Bad Gateway", 503: "Service Unavailable",
350
- 504: "Gateway Timeout" }
349
+ // STATUS_TEXT is Go's http.StatusText, which magpie's built-in said a
350
+ // refused meter call with; a status Go has no words for is "HTTP <n>", so
351
+ // the card always has an error.
352
+ const STATUS_TEXT = {
353
+ 100: "Continue", 101: "Switching Protocols", 102: "Processing", 103: "Early Hints",
354
+ 200: "OK", 201: "Created", 202: "Accepted", 203: "Non-Authoritative Information", 204: "No Content", 205: "Reset Content",
355
+ 206: "Partial Content", 207: "Multi-Status", 208: "Already Reported", 226: "IM Used",
356
+ 300: "Multiple Choices", 301: "Moved Permanently", 302: "Found", 303: "See Other", 304: "Not Modified", 305: "Use Proxy",
357
+ 307: "Temporary Redirect", 308: "Permanent Redirect",
358
+ 400: "Bad Request", 401: "Unauthorized", 402: "Payment Required", 403: "Forbidden", 404: "Not Found", 405: "Method Not Allowed",
359
+ 406: "Not Acceptable", 407: "Proxy Authentication Required", 408: "Request Timeout", 409: "Conflict", 410: "Gone",
360
+ 411: "Length Required", 412: "Precondition Failed", 413: "Request Entity Too Large", 414: "Request URI Too Long",
361
+ 415: "Unsupported Media Type", 416: "Requested Range Not Satisfiable", 417: "Expectation Failed", 418: "I'm a teapot",
362
+ 421: "Misdirected Request", 422: "Unprocessable Entity", 423: "Locked", 424: "Failed Dependency", 425: "Too Early",
363
+ 426: "Upgrade Required", 428: "Precondition Required", 429: "Too Many Requests", 431: "Request Header Fields Too Large",
364
+ 451: "Unavailable For Legal Reasons",
365
+ 500: "Internal Server Error", 501: "Not Implemented", 502: "Bad Gateway", 503: "Service Unavailable", 504: "Gateway Timeout",
366
+ 505: "HTTP Version Not Supported", 506: "Variant Also Negotiates", 507: "Insufficient Storage", 508: "Loop Detected",
367
+ 510: "Not Extended", 511: "Network Authentication Required",
368
+ }
369
+ const statusText = (n) => STATUS_TEXT[n] || `HTTP ${n}`
351
370
 
352
371
  // meter asks the billing API as magpie's built-in did, its errors said
353
372
  // the same: WorkBuddy's message, else its code, else the HTTP status.
@@ -368,7 +387,7 @@ async function meter(site, a, path) {
368
387
  if (!res.ok) {
369
388
  if (code) throw new Error(env.msg || `error ${code}`)
370
389
  if (env.msg) throw new Error(env.msg)
371
- throw new Error(STATUS_TEXT[res.status] ?? "")
390
+ throw new Error(statusText(res.status))
372
391
  }
373
392
  if (code) throw new Error(env.msg || `error ${code}`)
374
393
  return env.data ?? null
@@ -610,6 +629,7 @@ async function explained(res) {
610
629
  const at = v?.error && typeof v.error === "object" ? v.error : v && typeof v === "object" ? v : null
611
630
  if (at && typeof at.message === "string") msg = at.message
612
631
  else if (typeof v?.error === "string") msg = v.error
632
+ else if (typeof v?.msg === "string" && v.msg) msg = v.msg // WorkBuddy's own {"code", "msg"}
613
633
  msg = `${msg} — ${REFUSED_HINT}`
614
634
  const headers = new Headers(res.headers)
615
635
  headers.set("content-type", "application/json")
@@ -622,4 +642,4 @@ export const WorkBuddyAuthPlugin = makePlugin(SITES.workbuddy)
622
642
  export const WorkBuddyAIAuthPlugin = makePlugin(SITES["workbuddy-ai"])
623
643
 
624
644
  // for tests
625
- export const _internal = { usageOf, desktopHeld, explained, REFUSED_HINT, freeCredits }
645
+ export const _internal = { withSystem, usageOf, desktopHeld, explained, REFUSED_HINT, freeCredits }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-workbuddy-auth",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
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",