devin 0.1.4: errors no longer start with "Devin: ", which magpie adds (it read "Devin: Devin: usage limit reached: …"), a stream error before anything included; an error Devin gives no words for is worded as Go's status text ("Payment Required") as the built-in's devinFailure does, where it read "HTTP 402"; an empty reply is the built-in's 502 "the reply broke off: EOF". Statuses were already the built-in's (unauthenticated 401, resource_exhausted/quota/rate limit 429, too long 400, invalid_argument 400, unavailable 503); errors.test.mjs checks them

This commit is contained in:
yetone
2026-10-01 03:43:41 +08:00
parent 3d94a667f6
commit e39f4f4dc0
3 changed files with 90 additions and 12 deletions
+73
View File
@@ -0,0 +1,73 @@
// A request Devin fails is answered with the status and words magpie's
// built-in Devin (internal/gateway/devin.go, devinFailure) gave it, so the
// gateway moves on to the next account or member as it did. magpie puts
// "Devin: " before the message itself.
import { afterEach, expect, test } from "bun:test"
import { _internal } from "./index.mjs"
const real = globalThis.fetch
afterEach(() => (globalThis.fetch = real))
const enc = new TextEncoder()
const end = (obj) => {
const b = enc.encode(JSON.stringify(obj))
const out = new Uint8Array(5 + b.length)
out[0] = 2
new DataView(out.buffer).setUint32(1, b.length)
out.set(b, 5)
return out
}
const join = (...parts) => {
const out = new Uint8Array(parts.reduce((n, p) => n + p.length, 0))
let i = 0
for (const p of parts) out.set(p, (i += p.length) - p.length)
return out
}
const ask = async (reply, stream = false) => {
globalThis.fetch = async () => reply()
const res = await _internal.complete(
{ key: "k", server: "https://devin.test", families: _internal.familiesOf(_internal.SNAPSHOT) },
{ model: "swe-2", stream, messages: [{ role: "user", content: "hi" }] },
)
return { status: res.status, body: await res.text() }
}
const errOf = (r) => JSON.parse(r.body).error.message
test("an HTTP error that says nothing is worded as Go's status text", async () => {
const r = await ask(() => new Response("", { status: 402 }))
expect(r.status).toBe(402)
expect(errOf(r)).toBe("Payment Required")
})
test("a stream that ends on resource_exhausted before anything is a 429 without a second Devin:", async () => {
const r = await ask(() => new Response(end({ error: { code: "resource_exhausted", message: "daily quota used up" } })), true)
expect(r.status).toBe(429)
expect(errOf(r)).toBe("usage limit reached: daily quota used up")
})
test("a too-long conversation is the built-in's 400", async () => {
const r = await ask(() => new Response(end({ error: { code: "invalid_argument", message: "prompt is too long" } })))
expect(r.status).toBe(400)
expect(errOf(r)).toBe("input is too long for the model's context: prompt is too long")
})
test("a refused key is the 401 magpie marks the account lapsed on", async () => {
const r = await ask(() => new Response(JSON.stringify({ code: "unauthenticated", message: "bad key" }), { status: 401 }))
expect(r.status).toBe(401)
expect(errOf(r)).toBe("bad key — sign in to Devin again")
})
test("an empty reply is the built-in's 502 of a broken-off one", async () => {
const r = await ask(() => new Response(""))
expect(r.status).toBe(502)
expect(errOf(r)).toBe("the reply broke off: EOF")
})
test("an error after a frame with nothing in it is streamed in the built-in's words", async () => {
const empty = new Uint8Array(5) // a message frame with no fields
const r = await ask(() => new Response(join(empty, end({ error: { code: "resource_exhausted", message: "rate limit" } }))), true)
expect(r.status).toBe(200)
const ev = r.body.split("\n\n").map((l) => l.replace(/^data: /, "")).find((l) => l.includes('"error"'))
expect(JSON.parse(ev).error.message).toBe("usage limit reached: rate limit")
})
+16 -11
View File
@@ -6,7 +6,7 @@
// (internal/provider/devin*.go, internal/gateway/devin.go).
import { createHash, randomBytes, randomUUID } from "node:crypto"
import { createServer } from "node:http"
import { createServer, STATUS_CODES } from "node:http"
import { gunzipSync } from "node:zlib"
import { spawn } from "node:child_process"
import { existsSync, readFileSync, statSync } from "node:fs"
@@ -902,6 +902,11 @@ function encodeMsg(m) {
// ---- the reply -----------------------------------------------------------------------
// statusText is a status as magpie's built-in words an error that says
// nothing (Go's http.StatusText).
const statusText = (s) =>
({ 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] ?? ""
// failure is the status and message for a Connect error: {"code",
// "message"}, or at a stream's end {"error": {"code", "message"}}.
function failure(status, text) {
@@ -913,7 +918,7 @@ function failure(status, text) {
else if (e?.code) [code, msg] = [e.code, e.message ?? ""]
msg ||= code
} catch {}
msg ||= `HTTP ${status}`
msg ||= statusText(status) || `HTTP ${status}`
const low = msg.toLowerCase()
if (code === "unauthenticated") return { status: 401, message: msg + " — sign in to Devin again" }
if (code === "resource_exhausted" || low.includes("quota") || low.includes("rate limit")) return { status: 429, message: "usage limit reached: " + msg }
@@ -941,7 +946,7 @@ async function* events(it) {
try {
r = await it.next()
} catch (e) {
yield { error: { status: 502, message: "Devin: the reply broke off: " + e.message } }
yield { error: { status: 502, message: "the reply broke off: " + e.message } }
return
}
if (r.done) break
@@ -949,7 +954,7 @@ async function* events(it) {
if (f.end) {
const e = failure(200, dec.decode(f.data))
if (e.status >= 300) {
yield { error: { status: e.status, message: "Devin: " + e.message } }
yield { error: e }
return
}
break
@@ -1018,11 +1023,11 @@ async function complete({ key, server, families }, chat, signal) {
signal,
})
} catch (e) {
return errorResponse({ status: 502, message: "Devin: " + e.message })
return errorResponse({ status: 502, message: e.message })
}
if (!res.ok) {
const e = failure(res.status, (await res.text()).slice(0, 1 << 20))
return errorResponse({ status: e.status, message: "Devin: " + e.message })
return errorResponse(e)
}
// an error comes as the stream's end, before anything else: read that
// far so it is answered with its own status
@@ -1031,12 +1036,12 @@ async function complete({ key, server, families }, chat, signal) {
try {
first = await fr.next()
} catch (e) {
return errorResponse({ status: 502, message: "Devin: the reply broke off: " + e.message })
return errorResponse({ status: 502, message: "the reply broke off: " + e.message })
}
if (first.done) return errorResponse({ status: 502, message: "Devin: an empty reply" })
if (first.done) return errorResponse({ status: 502, message: "the reply broke off: EOF" })
if (first.value.end) {
const e = failure(200, dec.decode(first.value.data))
return errorResponse(e.status < 300 ? { status: 502, message: "Devin: an empty reply" } : { status: e.status, message: "Devin: " + e.message })
return errorResponse(e.status < 300 ? { status: 502, message: "an empty reply" } : e)
}
let replay = first
const it = events({
@@ -1187,13 +1192,13 @@ export async function DevinAuthPlugin() {
const now = await getAuth()
if (now?.type !== "api" || !now.key) return errorResponse({ status: 401, message: "Devin isn't signed in" })
const url = typeof input === "string" ? input : input instanceof URL ? input.href : input.url
if (!/\/chat\/completions$/.test(new URL(url).pathname)) return errorResponse({ status: 404, message: "Devin: only chat completions are served" })
if (!/\/chat\/completions$/.test(new URL(url).pathname)) return errorResponse({ status: 404, message: "only chat completions are served" })
let chat
try {
const b = init.body ?? (input instanceof Request ? await input.clone().text() : undefined)
chat = JSON.parse(typeof b === "string" ? b : dec.decode(b))
} catch {
return errorResponse({ status: 400, message: "Devin: a request that isn't JSON" })
return errorResponse({ status: 400, message: "a request that isn't JSON" })
}
const { key, server } = await live(now)
const families = await familiesFor(key, server)
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-devin-auth",
"version": "0.1.3",
"version": "0.1.4",
"description": "Devin subscriptions (the devin CLI's account) in OpenCode and magpie",
"type": "module",
"main": "./index.mjs",