diff --git a/packages/devin/README.md b/packages/devin/README.md index 4951547..dbc62ed 100644 --- a/packages/devin/README.md +++ b/packages/devin/README.md @@ -6,8 +6,8 @@ Provider id: `devin`. ## Signing in -An account is the CLI's session token (`devin-session-token$…`). There are -two ways to get one: +An account is the CLI's session token (`devin-session-token$…`) or a key +made in Devin's settings. There are three ways to add one: - **Devin (browser)**: the CLI's own `devin auth login`, run by the plugin. It is a PKCE sign-in through `app.devin.ai/auth/cli/continue` @@ -17,6 +17,9 @@ two ways to get one: its `credentials.toml` (`$XDG_DATA_HOME/devin/`, by default `~/.local/share/devin/`; `%APPDATA%\devin\` on Windows). The file is only read, never changed. +- **Devin API key (cog_…)**: a key typed in, such as a service user's + `cog_…` key. It is sent as a sign-in's token is (chat and usage), and is + never renewed. The first usage read names the account. The plugin names the account as Devin does when asked with the token (`GetUserStatus`: its email, organization when named, and tier). When Devin can't tell, and the CLI diff --git a/packages/devin/apikey.test.mjs b/packages/devin/apikey.test.mjs new file mode 100644 index 0000000..842b3c3 --- /dev/null +++ b/packages/devin/apikey.test.mjs @@ -0,0 +1,84 @@ +// 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: {} }) +}) diff --git a/packages/devin/index.mjs b/packages/devin/index.mjs index 1a10d2a..00cac4d 100644 --- a/packages/devin/index.mjs +++ b/packages/devin/index.mjs @@ -2868,6 +2868,8 @@ async function v2Setup(ctx) { }, label: (c) => c?.metadata?.email || "Devin", }) + // a key typed in, as the v1 "api" method saves it (v2Auth reads it) + editor.method.update({ integrationID: ID, method: { type: "key", label: "Devin API key (cog_…)" } }) }) // authFor resolves the active connection's key each call, so a chat goes @@ -3153,6 +3155,10 @@ export async function DevinAuthPlugin({ client, magpie } = {}, options = {}) { methods: [ { type: "oauth", label: "Devin (browser)", authorize: browserSignIn }, { type: "oauth", label: "Devin CLI's sign-in", authorize: cliSignIn }, + // a key made in Devin's settings (a service user's cog_…, or a + // session token): sent as the sign-ins' token is, never renewed; + // the first usage read names the account (syncOrg) + { type: "api", label: "Devin API key (cog_…)", placeholder: "cog_…" }, ], // magpie's own switches (OpenCode never reads auth.options) options: [ diff --git a/packages/devin/package.json b/packages/devin/package.json index fb90798..db76630 100644 --- a/packages/devin/package.json +++ b/packages/devin/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-devin-auth", - "version": "0.1.39", + "version": "0.1.40", "description": "Devin subscriptions (the devin CLI's account) in OpenCode and magpie", "type": "module", "main": "./index.mjs",