workbuddy 0.1.4: every refused meter status is an error with words, Go's http.StatusText in full (a 409 was an empty error), "HTTP <n>" for one Go has none for; a refusal in WorkBuddy's own {code, msg} reads its msg before the hint, as wbExplain's; a chat body handed over as bytes is read, so it still gets WorkBuddy's system message
This commit is contained in:
@@ -194,6 +194,8 @@ function sign(site, a, headers) {
|
||||
// withSystem gives a chat that doesn't open with a system message
|
||||
// WorkBuddy's default one, as its app does.
|
||||
function withSystem(body) {
|
||||
// a body magpie or OpenCode hands over as bytes is read as the text it is
|
||||
if (body instanceof ArrayBuffer || ArrayBuffer.isView(body)) body = new TextDecoder().decode(body)
|
||||
if (typeof body !== "string") return body
|
||||
try {
|
||||
const b = JSON.parse(body)
|
||||
@@ -344,10 +346,27 @@ async function current(site, client, auth) {
|
||||
|
||||
// ---- allowance ----------------------------------------------------------------
|
||||
|
||||
// STATUS_TEXT is Go's http.StatusText for the answers WorkBuddy gives.
|
||||
const STATUS_TEXT = { 400: "Bad Request", 401: "Unauthorized", 403: "Forbidden", 404: "Not Found", 405: "Method Not Allowed",
|
||||
408: "Request Timeout", 429: "Too Many Requests", 500: "Internal Server Error", 502: "Bad Gateway", 503: "Service Unavailable",
|
||||
504: "Gateway Timeout" }
|
||||
// STATUS_TEXT is Go's http.StatusText, which magpie's built-in said a
|
||||
// refused meter call with; a status Go has no words for is "HTTP <n>", so
|
||||
// the card always has an error.
|
||||
const STATUS_TEXT = {
|
||||
100: "Continue", 101: "Switching Protocols", 102: "Processing", 103: "Early Hints",
|
||||
200: "OK", 201: "Created", 202: "Accepted", 203: "Non-Authoritative Information", 204: "No Content", 205: "Reset Content",
|
||||
206: "Partial Content", 207: "Multi-Status", 208: "Already Reported", 226: "IM Used",
|
||||
300: "Multiple Choices", 301: "Moved Permanently", 302: "Found", 303: "See Other", 304: "Not Modified", 305: "Use Proxy",
|
||||
307: "Temporary Redirect", 308: "Permanent Redirect",
|
||||
400: "Bad Request", 401: "Unauthorized", 402: "Payment Required", 403: "Forbidden", 404: "Not Found", 405: "Method Not Allowed",
|
||||
406: "Not Acceptable", 407: "Proxy Authentication Required", 408: "Request Timeout", 409: "Conflict", 410: "Gone",
|
||||
411: "Length Required", 412: "Precondition Failed", 413: "Request Entity Too Large", 414: "Request URI Too Long",
|
||||
415: "Unsupported Media Type", 416: "Requested Range Not Satisfiable", 417: "Expectation Failed", 418: "I'm a teapot",
|
||||
421: "Misdirected Request", 422: "Unprocessable Entity", 423: "Locked", 424: "Failed Dependency", 425: "Too Early",
|
||||
426: "Upgrade Required", 428: "Precondition Required", 429: "Too Many Requests", 431: "Request Header Fields Too Large",
|
||||
451: "Unavailable For Legal Reasons",
|
||||
500: "Internal Server Error", 501: "Not Implemented", 502: "Bad Gateway", 503: "Service Unavailable", 504: "Gateway Timeout",
|
||||
505: "HTTP Version Not Supported", 506: "Variant Also Negotiates", 507: "Insufficient Storage", 508: "Loop Detected",
|
||||
510: "Not Extended", 511: "Network Authentication Required",
|
||||
}
|
||||
const statusText = (n) => STATUS_TEXT[n] || `HTTP ${n}`
|
||||
|
||||
// meter asks the billing API as magpie's built-in did, its errors said
|
||||
// the same: WorkBuddy's message, else its code, else the HTTP status.
|
||||
@@ -368,7 +387,7 @@ async function meter(site, a, path) {
|
||||
if (!res.ok) {
|
||||
if (code) throw new Error(env.msg || `error ${code}`)
|
||||
if (env.msg) throw new Error(env.msg)
|
||||
throw new Error(STATUS_TEXT[res.status] ?? "")
|
||||
throw new Error(statusText(res.status))
|
||||
}
|
||||
if (code) throw new Error(env.msg || `error ${code}`)
|
||||
return env.data ?? null
|
||||
@@ -610,6 +629,7 @@ async function explained(res) {
|
||||
const at = v?.error && typeof v.error === "object" ? v.error : v && typeof v === "object" ? v : null
|
||||
if (at && typeof at.message === "string") msg = at.message
|
||||
else if (typeof v?.error === "string") msg = v.error
|
||||
else if (typeof v?.msg === "string" && v.msg) msg = v.msg // WorkBuddy's own {"code", "msg"}
|
||||
msg = `${msg} — ${REFUSED_HINT}`
|
||||
const headers = new Headers(res.headers)
|
||||
headers.set("content-type", "application/json")
|
||||
@@ -622,4 +642,4 @@ export const WorkBuddyAuthPlugin = makePlugin(SITES.workbuddy)
|
||||
export const WorkBuddyAIAuthPlugin = makePlugin(SITES["workbuddy-ai"])
|
||||
|
||||
// for tests
|
||||
export const _internal = { usageOf, desktopHeld, explained, REFUSED_HINT, freeCredits }
|
||||
export const _internal = { withSystem, usageOf, desktopHeld, explained, REFUSED_HINT, freeCredits }
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@magpie-community/opencode-workbuddy-auth",
|
||||
"version": "0.1.3",
|
||||
"version": "0.1.4",
|
||||
"description": "OpenCode provider plugins for WorkBuddy's plans: WorkBuddy (CodeBuddy, copilot.tencent.com) and WorkBuddy AI (www.workbuddy.ai)",
|
||||
"type": "module",
|
||||
"main": "./index.mjs",
|
||||
|
||||
@@ -23,3 +23,20 @@ test("a model of no credits is free, as magpie's built-in read them", () => {
|
||||
for (const c of ["x0.00", "0", 0, " X0 "]) expect(freeCredits(c)).toBe(true)
|
||||
for (const c of ["x0.03", "", null, undefined, "free", 1]) expect(freeCredits(c)).toBe(false)
|
||||
})
|
||||
|
||||
// wbExplain's refusal in WorkBuddy's own envelope reads its msg
|
||||
test("a refusal as {code, msg} says its msg, then the hint", async () => {
|
||||
const res = await explained(new Response(JSON.stringify({ code: 11004, msg: "Illegal API invocation from an unapproved channel" }), { status: 403 }))
|
||||
expect(res.status).toBe(403)
|
||||
expect((await res.json()).error.message).toBe("Illegal API invocation from an unapproved channel — " + REFUSED_HINT)
|
||||
})
|
||||
|
||||
// a body handed over as bytes still gets WorkBuddy's system message
|
||||
test("a chat body in bytes is read, and given the system message", () => {
|
||||
const { withSystem } = _internal
|
||||
const chat = { model: "m", messages: [{ role: "user", content: "hi" }] }
|
||||
const want = { model: "m", messages: [{ role: "system", content: "You are a helpful assistant." }, { role: "user", content: "hi" }] }
|
||||
for (const b of [Buffer.from(JSON.stringify(chat)), new TextEncoder().encode(JSON.stringify(chat)), new TextEncoder().encode(JSON.stringify(chat)).buffer])
|
||||
expect(JSON.parse(withSystem(b))).toEqual(want)
|
||||
expect(JSON.parse(withSystem(JSON.stringify(chat)))).toEqual(want)
|
||||
})
|
||||
|
||||
@@ -84,6 +84,16 @@ test("WorkBuddy's refusals are the card's error", async () => {
|
||||
expect(await usageOf(WorkBuddyAuthPlugin, null)).toEqual({ error: "not signed in" })
|
||||
})
|
||||
|
||||
// Go's http.StatusText has words for every status WorkBuddy may give, and
|
||||
// the card must never get an empty error
|
||||
test("any refused status is an error with words", async () => {
|
||||
const auth = { type: "oauth", access: "a", expires: later(), uid: "u" }
|
||||
for (const [status, said] of [[409, "Conflict"], [402, "Payment Required"], [422, "Unprocessable Entity"], [501, "Not Implemented"], [520, "HTTP 520"], [302, "Found"]]) {
|
||||
serve({ "/billing/meter/get-user-resource-summary": () => new Response("", { status }) })
|
||||
expect([status, await usageOf(WorkBuddyAuthPlugin, auth)]).toEqual([status, { error: said }])
|
||||
}
|
||||
})
|
||||
|
||||
test("a token near its end is renewed and saved, as the loader does", async () => {
|
||||
const sets = []
|
||||
const calls = serve({
|
||||
|
||||
Reference in New Issue
Block a user