diff --git a/packages/opendesign/README.md b/packages/opendesign/README.md index e195e21..58312d8 100644 --- a/packages/opendesign/README.md +++ b/packages/opendesign/README.md @@ -28,7 +28,7 @@ Open Design's cloud is two planes: | Plane | Host | Key | Used for | | --- | --- | --- | --- | | Control | `amr-api.open-design.ai` | `vela_ctrl_…` | plan, wallet balance, the Design Plan's allowance windows, the workspace | -| Model | `amr-link.open-design.ai/v1` | `sk-amr-…` | `/models`, `/responses` (the OpenAI Responses API) | +| Model | `amr-link.open-design.ai/v1` | `sk-amr-…` | `/models`, `/chat/completions`, `/responses`, `/messages` | A sign-in is the same device authorization the Open Design CLI (`vela login`) runs: @@ -120,6 +120,13 @@ for `claude-haiku-5.5` and `deepseek-v4-pro`, and Anthropic's messages (`@ai-sdk/anthropic`) for `glm-5.3-flash`. Every model reasons at `low`, `high` and `max`. +That API is each model's default, not the only one: amr-link serves every +model on all three (keyblues, plugins#888). A request for a model on another +of them is signed as that API takes the key — `x-api-key` on `/messages`, +`Authorization: Bearer` on `/chat/completions` and `/responses` — whatever +the model is declared on, so a model set to another API in magpie's model +editor works there too. + ## Where the sign-in is kept magpie's `plugin-auth.json` (mode 600), as an oauth account: diff --git a/packages/opendesign/index.mjs b/packages/opendesign/index.mjs index 9e3a451..0e33463 100644 --- a/packages/opendesign/index.mjs +++ b/packages/opendesign/index.mjs @@ -3,7 +3,8 @@ // (amr-api.open-design.ai), which answers with two keys — a control key // (vela_ctrl_…) for the account's plan and its Design Plan allowance, and a // runtime key (sk-amr-…) that the model plane (amr-link.open-design.ai) -// takes. Model requests are the OpenAI Responses API at the runtime key; +// takes. Model requests go there at the runtime key, on each model's own API +// (WIRE below) or any other of the three amr-link serves it on; // the model list, the plan and the 5-hour and 7-day allowance windows all // come from the control plane. // @@ -41,6 +42,37 @@ const MODEL_WIRE = { "glm-5.3-flash": "anthropic", } const wireOf = (id) => WIRE[MODEL_WIRE[id]] ?? WIRE.chat +// The API a model is declared on is only its default: amr-link serves +// every model on all three (keyblues, plugins#888: each of the fourteen +// answered on chat completions and Responses at /v1 and on Anthropic's +// messages at the bare host, which the AI SDK asks at /v1/messages). A +// request can reach the plugin on another API than the declared one — magpie +// sends it there when the model is set to be asked on it — and the key +// rides as that API takes it, not as the declared package sends it: the +// host signs with the declared package's header (x-api-key for +// @ai-sdk/anthropic, Bearer for the others), so a chat model asked on +// /messages went with a Bearer and glm-5.3-flash asked on /chat/completions +// with an x-api-key. signFor reads the API off the request's path. +const MESSAGES = /\/messages(\/count_tokens)?\/?$/ +const signFor = (key) => async (input, init = {}) => { + const url = input instanceof Request ? input.url : String(input) + const h = new Headers(init.headers ?? (input instanceof Request ? input.headers : undefined)) + let path = "" + try { + path = new URL(url).pathname + } catch {} + if (MESSAGES.test(path)) { + h.delete("authorization") + h.set("x-api-key", key) + if (!h.has("anthropic-version")) h.set("anthropic-version", "2023-06-01") + } else { + h.delete("x-api-key") + h.set("authorization", `Bearer ${key}`) + } + // the global fetch as it is when the request is sent: the host's own, + // with its proxy and redirect rules, never one captured at load + return globalThis.fetch(input, { ...init, headers: h }) +} // the provider's own default: a model this plugin doesn't name is asked on // chat completions, where ten of the eleven thought const NPM = WIRE.chat @@ -528,7 +560,7 @@ export const OpenDesignAuthPlugin = async () => { loader: async (getAuth) => { const key = runtimeKeyOf(await getAuth().catch(() => null)) if (!key) return {} - return { baseURL: LINK, apiKey: key } + return { baseURL: LINK, apiKey: key, fetch: signFor(key) } }, methods: [ { @@ -566,4 +598,4 @@ export const OpenDesignAuthPlugin = async () => { // _internal is what the tests read through; magpie calls every exported // function as a plugin, so only the plugin itself is exported bare. -export const _internal = { authOf, allowances, controlKeyOf, credits, modelsOf, money, rateOf, runtimeKeyOf, said, startDevice, thePlan, usedPercent, waitForDevice, windowName, when, workspaceOf } +export const _internal = { authOf, signFor, allowances, controlKeyOf, credits, modelsOf, money, rateOf, runtimeKeyOf, said, startDevice, thePlan, usedPercent, waitForDevice, windowName, when, workspaceOf } diff --git a/packages/opendesign/index.test.mjs b/packages/opendesign/index.test.mjs index 03c2c8b..e180ceb 100644 --- a/packages/opendesign/index.test.mjs +++ b/packages/opendesign/index.test.mjs @@ -405,11 +405,48 @@ test("the plugin declares the provider and answers its methods", async () => { test("the loader hands the runtime key to the model plane", async () => { const hooks = await OpenDesignAuthPlugin({}) - expect(await hooks.auth.loader(async () => ({ access: "sk-amr-b", control: "vela_ctrl_a" }))) - .toEqual({ baseURL: "https://amr-link.open-design.ai/v1", apiKey: "sk-amr-b" }) + const o = await hooks.auth.loader(async () => ({ access: "sk-amr-b", control: "vela_ctrl_a" })) + expect(o).toMatchObject({ baseURL: "https://amr-link.open-design.ai/v1", apiKey: "sk-amr-b" }) + expect(typeof o.fetch).toBe("function") expect(await hooks.auth.loader(async () => null)).toEqual({}) }) +// keyblues (plugins#888): amr-link serves every model on chat completions, +// Responses and Anthropic's messages. The host signs a request with the +// header of the package the model is declared on, so a model asked on +// another API than its own must be signed for the API it is asked on. +test("a model asked on any of the three APIs is signed as that API takes the key (plugins#888)", async () => { + const seen = [] + globalThis.fetch = async (url, init) => { + const h = new Headers(init?.headers) + seen.push({ url: url instanceof Request ? url.url : String(url), auth: h.get("authorization"), xkey: h.get("x-api-key"), version: h.get("anthropic-version"), body: init?.body }) + return new Response("{}", { status: 200 }) + } + const hooks = await OpenDesignAuthPlugin({}) + const { fetch: send } = await hooks.auth.loader(async () => ({ access: "sk-amr-b", control: "vela_ctrl_a" })) + const base = "https://amr-link.open-design.ai/v1" + // kimi-k2.7-code is declared on chat completions, so the host sent the + // Bearer header: asked on Anthropic's messages it goes with x-api-key + await send(base + "/messages", { method: "POST", headers: { authorization: "Bearer sk-amr-b", "content-type": "application/json" }, body: '{"model":"kimi-k2.7-code"}' }) + await send(base + "/messages/count_tokens", { method: "POST", headers: { authorization: "Bearer sk-amr-b" }, body: "{}" }) + // glm-5.3-flash is declared on Anthropic's messages: asked on chat + // completions or Responses it goes with the Bearer + await send(base + "/chat/completions", { method: "POST", headers: { "x-api-key": "sk-amr-b", "anthropic-version": "2023-06-01" }, body: '{"model":"glm-5.3-flash"}' }) + await send(new Request(base + "/responses", { method: "POST", headers: { "x-api-key": "sk-amr-b" }, body: "{}" })) + expect(seen.map(({ url, auth, xkey }) => [url.replace(base, ""), auth, xkey])).toEqual([ + ["/messages", null, "sk-amr-b"], + ["/messages/count_tokens", null, "sk-amr-b"], + ["/chat/completions", "Bearer sk-amr-b", null], + ["/responses", "Bearer sk-amr-b", null], + ]) + // Anthropic's messages carry a version, the client's own when it sent one + expect(seen[0].version).toBe("2023-06-01") + expect(seen[0].body).toBe('{"model":"kimi-k2.7-code"}') + // the declared API's own requests go as they did + await send(base + "/messages", { method: "POST", headers: { "x-api-key": "sk-amr-b", "anthropic-version": "2024-01-01" }, body: "{}" }) + expect([seen[4].auth, seen[4].xkey, seen[4].version]).toEqual([null, "sk-amr-b", "2024-01-01"]) +}) + test("the account's models are read at its own key", async () => { const hooks = await OpenDesignAuthPlugin({}) const seen = [] diff --git a/packages/opendesign/package.json b/packages/opendesign/package.json index acd6b4f..fe8f801 100644 --- a/packages/opendesign/package.json +++ b/packages/opendesign/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-opendesign-auth", - "version": "0.1.11", + "version": "0.1.12", "description": "Open Design Cloud (Design Plan) in OpenCode and magpie: the account's device sign-in, its models, and the plan's 5-hour and 7-day allowance", "type": "module", "main": "./opencode.mjs",