devin 0.1.1: usage shows as magpie's built-in did — plan, period, windows and what they count, balancedevin
This commit is contained in:
@@ -1059,6 +1059,50 @@ async function complete({ key, server, families }, chat, signal) {
|
||||
|
||||
// serverOf is the API server an account's requests go to:
|
||||
// WINDSURF_API_SERVER_URL moves it, as it does the CLI's.
|
||||
// ---- usage -----------------------------------------------------------------
|
||||
|
||||
// usage is the account's plan and how much of its quota is gone, as
|
||||
// GetUserStatus (what the CLI's /usage reads) tells them: the plan's name
|
||||
// and end, the daily and weekly quotas a quota-billed plan has (the share
|
||||
// left of each, and when it comes back), the ACUs a plan with a limit has
|
||||
// used this cycle, and the extra usage balance. JSON leaves out what is
|
||||
// zero, so a quota with a reset and no share left is used up. magpie's
|
||||
// built-in Devin account showed none of this.
|
||||
async function usage(key, server) {
|
||||
const res = await fetch(server + "/exa.seat_management_pb.SeatManagementService/GetUserStatus", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json", "Connect-Protocol-Version": "1" },
|
||||
body: JSON.stringify({
|
||||
metadata: { ideName: "devin-cli", ideVersion: CLI_VERSION, extensionName: "devin-cli", extensionVersion: CLI_VERSION, apiKey: key, locale: "en", os: osName() },
|
||||
}),
|
||||
signal: AbortSignal.timeout(15_000),
|
||||
})
|
||||
const text = await res.text()
|
||||
if (!res.ok) return { error: failure(res.status, text).message, windows: [] }
|
||||
const st = JSON.parse(text || "{}")?.userStatus?.planStatus ?? {}
|
||||
const info = st.planInfo ?? {}
|
||||
const num = (v) => (typeof v === "number" ? v : typeof v === "string" && v.trim() && !isNaN(Number(v)) ? Number(v) : 0)
|
||||
const out = { windows: [] }
|
||||
if (info.planName) out.plan = String(info.planName)
|
||||
if (st.planEnd) out.until = st.planEnd
|
||||
for (const [name, span, left, reset, hide] of [
|
||||
["Daily", 24 * 3600, st.dailyQuotaRemainingPercent, st.dailyQuotaResetAtUnix, info.hideDailyQuota],
|
||||
["Weekly", 7 * 24 * 3600, st.weeklyQuotaRemainingPercent, st.weeklyQuotaResetAtUnix, info.hideWeeklyQuota],
|
||||
]) {
|
||||
if (hide || (left === undefined && !num(reset))) continue
|
||||
const at = num(reset)
|
||||
out.windows.push({ name, used: Math.min(100, Math.max(0, 100 - num(left))), span, ...(at > 0 ? { resetsAt: at } : {}) })
|
||||
}
|
||||
const limit = num(st.acuLimit)
|
||||
if (limit > 0) {
|
||||
const used = num(st.acuConsumed)
|
||||
out.windows.push({ name: "ACUs", used: Math.min(100, (100 * used) / limit), display: `${+used.toFixed(2)} / ${+limit.toFixed(2)} ACUs`, ...(st.planEnd ? { resetsAt: st.planEnd } : {}), aside: true })
|
||||
}
|
||||
const extra = num(st.overageBalanceMicros)
|
||||
if (extra > 0) out.balance = "$" + (extra / 1e6).toFixed(2)
|
||||
return out
|
||||
}
|
||||
|
||||
const serverOf = (auth) => (process.env.WINDSURF_API_SERVER_URL || auth?.metadata?.server || SERVER).replace(/\/+$/, "")
|
||||
|
||||
export async function DevinAuthPlugin() {
|
||||
@@ -1093,6 +1137,12 @@ export async function DevinAuthPlugin() {
|
||||
{ type: "oauth", label: "Devin (browser)", authorize: browserSignIn },
|
||||
{ type: "oauth", label: "Devin CLI's sign-in", authorize: cliSignIn },
|
||||
],
|
||||
// magpie's: the plan and how much of its quota is gone
|
||||
async usage(getAuth) {
|
||||
const auth = await getAuth()
|
||||
if (auth?.type !== "api" || !auth.key) return { error: "Devin isn't signed in" }
|
||||
return usage(auth.key, serverOf(auth))
|
||||
},
|
||||
},
|
||||
async config(config) {
|
||||
config.provider ??= {}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@magpie-community/opencode-devin-auth",
|
||||
"version": "0.1.0",
|
||||
"version": "0.1.1",
|
||||
"description": "Devin subscriptions (the devin CLI's account) in OpenCode and magpie",
|
||||
"type": "module",
|
||||
"main": "./index.mjs",
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
// auth.usage tells the plan and quota GetUserStatus says, against a reply
|
||||
// shaped as a live Teams account's (magpie's built-in Devin account showed
|
||||
// none of it).
|
||||
import { afterEach, expect, test } from "bun:test"
|
||||
import { DevinAuthPlugin } from "./index.mjs"
|
||||
|
||||
const real = globalThis.fetch
|
||||
afterEach(() => (globalThis.fetch = real))
|
||||
|
||||
const auth = { type: "api", key: "devin-key", metadata: { email: "d@x.dev", plan: "Teams" } }
|
||||
|
||||
const status = (plan = {}) => ({
|
||||
userStatus: {
|
||||
planStatus: {
|
||||
planInfo: { planName: "Teams", teamsTier: "TEAMS_TIER_DEVIN_TEAMS", billingStrategy: "BILLING_STRATEGY_QUOTA", monthlyPromptCredits: -1, isDevin: true },
|
||||
planStart: "2026-09-26T21:45:55Z",
|
||||
planEnd: "2026-10-26T21:45:55Z",
|
||||
availablePromptCredits: -1,
|
||||
dailyQuotaRemainingPercent: 100,
|
||||
weeklyQuotaRemainingPercent: 99,
|
||||
overageBalanceMicros: "46807746",
|
||||
dailyQuotaResetAtUnix: "1790841600",
|
||||
weeklyQuotaResetAtUnix: "1791100800",
|
||||
...plan,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
async function run(a, reply) {
|
||||
const seen = []
|
||||
globalThis.fetch = async (url, init) => {
|
||||
seen.push({ url: String(url), headers: init.headers, body: JSON.parse(init.body) })
|
||||
return reply()
|
||||
}
|
||||
const hooks = await DevinAuthPlugin()
|
||||
return { u: await hooks.auth.usage(async () => a), seen }
|
||||
}
|
||||
|
||||
test("a Teams plan: its end, its day and week, the extra usage balance", async () => {
|
||||
const { u, seen } = await run(auth, () => Response.json(status()))
|
||||
expect(u).toEqual({
|
||||
plan: "Teams",
|
||||
until: "2026-10-26T21:45:55Z",
|
||||
balance: "$46.81",
|
||||
windows: [
|
||||
{ name: "Daily", used: 0, span: 86400, resetsAt: 1790841600 },
|
||||
{ name: "Weekly", used: 1, span: 604800, resetsAt: 1791100800 },
|
||||
],
|
||||
})
|
||||
expect(seen[0].url).toBe("https://server.codeium.com/exa.seat_management_pb.SeatManagementService/GetUserStatus")
|
||||
expect(seen[0].headers).toEqual({ "Content-Type": "application/json", "Connect-Protocol-Version": "1" })
|
||||
expect(seen[0].body.metadata).toMatchObject({ ideName: "devin-cli", extensionName: "devin-cli", apiKey: "devin-key", locale: "en" })
|
||||
})
|
||||
|
||||
test("a quota with no share left is used up; a hidden one isn't shown; ACUs with a limit are", async () => {
|
||||
const s = status({ dailyQuotaRemainingPercent: undefined, overageBalanceMicros: undefined, acuConsumed: 12.5, acuLimit: 50 })
|
||||
s.userStatus.planStatus.planInfo.hideWeeklyQuota = true
|
||||
const { u } = await run({ ...auth, metadata: { ...auth.metadata, server: "https://eu.example" } }, () => Response.json(s))
|
||||
expect(u).toEqual({
|
||||
plan: "Teams",
|
||||
until: "2026-10-26T21:45:55Z",
|
||||
windows: [
|
||||
{ name: "Daily", used: 100, span: 86400, resetsAt: 1790841600 },
|
||||
{ name: "ACUs", used: 25, display: "12.5 / 50 ACUs", resetsAt: "2026-10-26T21:45:55Z", aside: true },
|
||||
],
|
||||
})
|
||||
})
|
||||
|
||||
test("a plan without quotas is its name alone", async () => {
|
||||
const s = { userStatus: { planStatus: { planInfo: { planName: "Free" } } } }
|
||||
expect((await run(auth, () => Response.json(s))).u).toEqual({ plan: "Free", windows: [] })
|
||||
})
|
||||
|
||||
test("a refused key says to sign in again", async () => {
|
||||
const { u } = await run(auth, () => Response.json({ code: "unauthenticated", message: "invalid api key" }, { status: 401 }))
|
||||
expect(u).toEqual({ error: "invalid api key — sign in to Devin again", windows: [] })
|
||||
expect((await run({ type: "oauth" }, () => Response.json({}))).u).toEqual({ error: "Devin isn't signed in" })
|
||||
})
|
||||
Reference in New Issue
Block a user