Files
yetone aea9438f90 devin 0.1.40: a typed API key (cog_…) is a sign-in method
wuxudongxd on X: the plugin offered only the browser and CLI sign-ins,
while a hand-written {type:"api", key:"cog_…"} entry (a service user's
key) worked for chat and usage. Add "Devin API key (cog_…)" as a v1 api
method (no authorize, so the host saves the key as typed) and as an
OpenCode 2 key method; v2Auth already reads a key credential. The key is
sent as a sign-in's token is (Basic on GetChatMessage, apiKey on
GetUserStatus), and the first usage read names the account (syncOrg).

Run: bun test in packages/devin (214 pass) and the whole repo's bun test
with a temp HOME on mote (8542 pass, 20 skip, 0 fail).
Broken by hand: with both methods removed, apikey.test.mjs's method and
OpenCode 2 tests go red (the chat and usage tests pass either way: those
paths already took any api key).
Not tried: a real cog_ key (none on hand).
2026-11-13 15:02:14 +08:00

85 lines
3.9 KiB
JavaScript

// A key typed in (a service user's cog_… from Devin's settings) is a sign-in
// method of its own, saved as OpenCode saves any API key: {type:"api", key},
// no metadata. The chat and usage paths take it as they take a browser
// sign-in's token, and the first usage read names the account.
import { afterEach, expect, test } from "bun:test"
import { DevinAuthPlugin, _internal } from "./index.mjs"
const real = globalThis.fetch
afterEach(() => (globalThis.fetch = real))
const LABEL = "Devin API key (cog_…)"
const configs = () => new Response(new _internal.PB().bytes(1, new _internal.PB().bytes(22, "swe-2")).done())
test("the API key method is offered after the two sign-ins, with nothing to authorize", async () => {
const { auth } = await DevinAuthPlugin({})
expect(auth.methods.map((m) => [m.type, m.label])).toEqual([
["oauth", "Devin (browser)"],
["oauth", "Devin CLI's sign-in"],
["api", LABEL],
])
// the host saves the key as typed only when there is no authorize
expect(auth.methods[2].authorize).toBeUndefined()
})
test("a typed key is sent to GetChatMessage as a sign-in's token is", async () => {
const seen = []
globalThis.fetch = async (url, init) => {
if (String(url).endsWith("/GetCliModelConfigs")) return configs()
seen.push({ url: String(url), auth: new Headers(init.headers).get("Authorization") })
return new Response("", { status: 402 })
}
const key = { type: "api", key: "cog_chat_test" }
const { auth } = await DevinAuthPlugin({})
const opts = await auth.loader(async () => key)
expect(opts.apiKey).toBe("cog_chat_test")
const res = await opts.fetch("https://devin.invalid/v1/chat/completions", {
method: "POST",
body: JSON.stringify({ model: "swe-2", messages: [{ role: "user", content: "hi" }] }),
})
expect(res.status).toBe(402)
const chat = seen.find((s) => s.url.endsWith("/GetChatMessage"))
expect(chat?.url).toBe("https://server.codeium.com/exa.api_server_pb.ApiServerService/GetChatMessage")
expect(chat.auth).toBe("Basic cog_chat_test")
})
test("usage reads a typed key's plan and names its account", async () => {
const seen = []
globalThis.fetch = async (url, init) => {
if (String(url).endsWith("/GetCliModelConfigs")) return configs()
seen.push(JSON.parse(init.body))
return Response.json({
userStatus: {
email: "svc@example.com",
teamId: "team-1",
planStatus: { planInfo: { planName: "Teams", devinInfo: { orgId: "org-1" } }, planEnd: "2026-12-01T00:00:00Z", dailyQuotaRemainingPercent: 80 },
},
})
}
const sets = []
const key = { type: "api", key: "cog_usage_test" }
const { auth } = await DevinAuthPlugin({ client: { auth: { set: async (a) => sets.push(a) } } })
const u = await auth.usage(async () => key)
expect(u.signIn).toBe("renewed")
expect(u.error).toBeUndefined()
expect(seen[0].metadata.apiKey).toBe("cog_usage_test")
expect(sets).toEqual([{ path: { id: "devin" }, body: { type: "api", key: "cog_usage_test", metadata: { email: "svc@example.com", orgId: "org-1", teamId: "team-1" } } }])
})
test("OpenCode 2 offers a key method, and its credential is read as an API key", async () => {
const methods = []
const ctx = {
integration: {
transform: async (cb) => cb({ method: { update: (m) => methods.push(m) } }),
connection: { active: async () => null, resolve: async () => null },
},
provider: { transform: async () => {}, reload: async () => {} },
model: { transform: async () => {}, reload: async () => {} },
aisdk: { hook: async () => {} },
event: { subscribe: () => ({ [Symbol.asyncIterator]: () => ({ next: () => new Promise(() => {}) }) }) },
}
await _internal.v2Setup(ctx)
expect(methods.filter((m) => m.method?.type === "key").map((m) => [m.integrationID, m.method.label])).toEqual([["devin", LABEL]])
expect(_internal.v2Auth({ type: "key", key: "cog_v2" })).toEqual({ type: "api", key: "cog_v2", metadata: {} })
})