diff --git a/packages/devin/free.test.mjs b/packages/devin/free.test.mjs index c48dff9..72c083c 100644 --- a/packages/devin/free.test.mjs +++ b/packages/devin/free.test.mjs @@ -47,11 +47,38 @@ test("Devin's Free tier is read from its model configs", () => { test("a used-up day and week don't count SWE-2", async () => { // both used up: JSON leaves the 0% left out serve(status()) - const u = await (await DevinAuthPlugin()).auth.usage(async () => ({ type: "api", key: "key-free-usage" })) + const plugin = await DevinAuthPlugin() + await plugin.provider.models({ models: {} }, { auth: { type: "api", key: "key-free-usage" } }) + const u = await plugin.auth.usage(async () => ({ type: "api", key: "key-free-usage" })) expect(u.windows.map((w) => [w.name, w.used])).toEqual([["1 day", 100], ["7 days", 100]]) for (const w of u.windows) expect([...w.notModels].sort()).toEqual(SWE2) }) +test("a slow model list doesn't hold up quota usage", async () => { + const key = "slow-model-config-key" + let finishConfigs + serve(status(), () => new Promise((resolve) => (finishConfigs = resolve))) + const plugin = await DevinAuthPlugin() + const usage = plugin.auth.usage(async () => ({ type: "api", key })) + let timeout + let u + try { + u = await Promise.race([ + usage, + new Promise((_, reject) => (timeout = setTimeout(() => reject(new Error("usage waited for GetCliModelConfigs")), 1_000))), + ]) + } finally { + clearTimeout(timeout) + } + expect(u.windows.map((w) => [w.name, w.used])).toEqual([["1 day", 100], ["7 days", 100]]) + expect(u.windows.map((w) => w.notModels)).toEqual([undefined, undefined]) + + finishConfigs(new Response(realConfigs)) + await _internal.freeIDs(key, "https://server.codeium.com") + const refreshed = await plugin.auth.usage(async () => ({ type: "api", key })) + for (const w of refreshed.windows) expect([...w.notModels].sort()).toEqual(SWE2) +}) + test("with no word from Devin on its models, the day and week count every model", async () => { serve(status(), () => new Response("no", { status: 503 })) const u = await (await DevinAuthPlugin()).auth.usage(async () => ({ type: "api", key: "key-free-unknown" })) @@ -94,18 +121,24 @@ test("SWE-2 the account's list leaves out goes as SNAPSHOT's variant, not the ba }) test("a model list Devin won't give is said in usage and logged once, not swallowed", async () => { - serve(status(), () => new Response("no", { status: 503 })) + const key = "key-free-error" + let failConfigs + serve(status(), () => new Promise((resolve) => (failConfigs = () => resolve(new Response("no", { status: 503 }))))) const logged = [] const was = console.error console.error = (...a) => logged.push(a.join(" ")) try { const plugin = await DevinAuthPlugin() - const u = await plugin.auth.usage(async () => ({ type: "api", key: "key-free-said" })) - expect(u.windows.slice(0, 2).map((w) => w.notModels)).toEqual([undefined, undefined]) - const note = u.windows.find((w) => w.name === "Free models") + const first = await plugin.auth.usage(async () => ({ type: "api", key })) + expect(first.windows.slice(0, 2).map((w) => w.notModels)).toEqual([undefined, undefined]) + expect(first.windows.find((w) => w.name === "Free models")).toBeUndefined() + failConfigs() + await _internal.freeIDs(key, "https://server.codeium.com") + const second = await plugin.auth.usage(async () => ({ type: "api", key })) + const note = second.windows.find((w) => w.name === "Free models") expect(note?.aside).toBe(true) expect(note.display).toContain("503") - await plugin.auth.usage(async () => ({ type: "api", key: "key-free-said" })) + await plugin.auth.usage(async () => ({ type: "api", key })) } finally { console.error = was } diff --git a/packages/devin/index.mjs b/packages/devin/index.mjs index 5cada6a..ef2eab3 100644 --- a/packages/devin/index.mjs +++ b/packages/devin/index.mjs @@ -3042,12 +3042,14 @@ async function usage(key, server, onStatus) { } // with Devin's word on its models unread, which are free is unknown, so // the day and the week count them all (a used-up quota then holds SWE-2 - // too): a row says so and why, instead of nothing + // too). Ask in the background so a slow model list doesn't hide quota + // GetUserStatus already returned; a later refresh adds free models or + // says why the list couldn't be read. if (out.windows.length) { - const free = await freeIDs(key, server) + const free = await freeIDs(key, server, false) if (free.length) for (const w of out.windows) w.notModels = free else { - const c = await configsFor(key, server) + const c = await configsFor(key, server, false) if (c.error && !c.at) out.windows.push({ name: "Free models", display: "unknown: Devin's model list couldn't be read (" + c.error + ")", aside: true }) } } @@ -3065,8 +3067,8 @@ async function usage(key, server, onStatus) { // may ask for them: each variant's own, and each entry of the list that // stands for free variants alone (swe-2 for swe-2-high, -medium and -max). // None when Devin hasn't said. -async function freeIDs(key, server) { - const c = await configsFor(key, server) +async function freeIDs(key, server, wait = true) { + const c = await configsFor(key, server, wait) if (!c.free?.size) return [] const ids = new Set(c.free) for (const m of listed(c.families ?? [], [], {}, c.free)) if (m.free) ids.add(m.id) diff --git a/packages/devin/package.json b/packages/devin/package.json index ddc3e0e..3afeeac 100644 --- a/packages/devin/package.json +++ b/packages/devin/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-devin-auth", - "version": "0.1.37", + "version": "0.1.38", "description": "Devin subscriptions (the devin CLI's account) in OpenCode and magpie", "type": "module", "main": "./index.mjs",