From b9404c8f68308520ef48f20d69310f9e4adbdeb1 Mon Sep 17 00:00:00 2001 From: yetone Date: Thu, 1 Oct 2026 03:19:57 +0800 Subject: [PATCH] commandcode 0.1.3: on every plan but Go a model the Provider API serves on Responses and not chat completions is asked on Responses (@ai-sdk/openai at /provider/v1), the first of its APIs in magpie's order (chat, Responses, Messages) as the built-in picks it, where it was asked on chat completions and refused; errors no longer start with "Command Code: ", which magpie adds --- packages/commandcode/index.mjs | 15 +++++++----- packages/commandcode/package.json | 2 +- packages/commandcode/usage.test.mjs | 37 +++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 7 deletions(-) diff --git a/packages/commandcode/index.mjs b/packages/commandcode/index.mjs index bc0cc01..9a14a04 100644 --- a/packages/commandcode/index.mjs +++ b/packages/commandcode/index.mjs @@ -29,6 +29,7 @@ const SIGN_IN_TIMEOUT = 10 * 60 * 1000 const CHAT = "@ai-sdk/openai-compatible" const MESSAGES = "@ai-sdk/anthropic" +const RESPONSES = "@ai-sdk/openai" // the Responses API, which magpie speaks to it on // ---- models ------------------------------------------------------------------- @@ -109,7 +110,7 @@ function runtimeModel(m) { // liveModels is the Provider API's list, with the APIs each model is // served on (its Claude models on /messages alone, the open ones on -// /chat/completions and /responses); the default list's names and +// /chat/completions and /responses, some on /responses alone); the default list's names and // windows fill in what it leaves out. async function liveModels(key) { const res = await fetch(BASE + "/models", { headers: { Authorization: `Bearer ${key}`, "x-api-key": key } }) @@ -122,7 +123,9 @@ async function liveModels(key) { const k = known[m.id] const eps = (m.supported_endpoints ?? []).map((e) => String(e).replace(/\/$/, "").replace(/^\/v1/, "")) let npm = isClaude(m.id) ? MESSAGES : CHAT - if (eps.length) npm = eps.includes("/chat/completions") ? CHAT : eps.includes("/messages") ? MESSAGES : CHAT + // the first of the APIs it is served on, in magpie's order (chat + // completions, Responses, Messages), as the built-in picks it + if (eps.length) npm = eps.includes("/chat/completions") ? CHAT : eps.includes("/responses") ? RESPONSES : eps.includes("/messages") ? MESSAGES : CHAT const ctx = Number(m.context_length) out.push({ id: m.id, @@ -688,7 +691,7 @@ function failure(status, text) { if (status < 400 || status === 500) status = 429 } if (status < 400 || status > 599) status = 502 - return { status, message: "Command Code: " + msg } + return { status, message: msg } } // streamError is an error line's status and message: a string, or @@ -789,7 +792,7 @@ async function* events(body) { return } } - yield { error: { status: 502, message: "Command Code: the reply ended before it was complete" } } + yield { error: { status: 502, message: "the reply ended before it was complete" } } } // generate answers a chat completion through /alpha/generate. @@ -864,7 +867,7 @@ async function generate(key, chat, signal) { // ---- the plugin --------------------------------------------------------------- -export const _internal = { subsSeen, waits, liveKey } +export const _internal = { subsSeen, waits, liveKey, failure } export async function CommandCodePlugin({ client } = {}) { return { @@ -893,7 +896,7 @@ export async function CommandCodePlugin({ client } = {}) { const b = init.body ?? (input instanceof Request ? await input.clone().text() : undefined) chat = JSON.parse(typeof b === "string" ? b : new TextDecoder().decode(b)) } catch { - return errorResponse({ status: 400, message: "Command Code: a request that isn't JSON" }) + return errorResponse({ status: 400, message: "a request that isn't JSON" }) } return generate(key, chat, init.signal) } diff --git a/packages/commandcode/package.json b/packages/commandcode/package.json index a13651b..b8e6365 100644 --- a/packages/commandcode/package.json +++ b/packages/commandcode/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-commandcode-auth", - "version": "0.1.2", + "version": "0.1.3", "description": "Command Code plans (Pro, GOAT, Max, Ultra, Go, Teams) in OpenCode and magpie", "type": "module", "main": "./index.mjs", diff --git a/packages/commandcode/usage.test.mjs b/packages/commandcode/usage.test.mjs index 7d79d67..e6f4664 100644 --- a/packages/commandcode/usage.test.mjs +++ b/packages/commandcode/usage.test.mjs @@ -161,3 +161,40 @@ test("a subscription Command Code couldn't read is unread, not No plan", async ( const hooks = await CommandCodePlugin() expect((await hooks.auth.usage(async () => ({ ...api, metadata: { plan: "GOAT" } }))).plan).toBe("GOAT") }) + +test("a model served on Responses and not chat completions is asked on Responses, relayed as it is", async () => { + const sent = [] + globalThis.fetch = async (url, init = {}) => { + const path = new URL(String(url)).pathname + if (path === "/provider/v1/models") + return Response.json({ data: [ + { id: "gpt-6-sol", supported_endpoints: ["/v1/responses"] }, + { id: "moonshotai/Kimi-K3", supported_endpoints: ["/v1/chat/completions", "/v1/responses"] }, + { id: "claude-sonnet-5", supported_endpoints: ["/v1/messages"] }, + { id: "both", supported_endpoints: ["/v1/responses", "/v1/messages"] }, + ] }) + if (path === "/provider/v1/responses") { + sent.push({ path, key: new Headers(init.headers).get("x-api-key"), body: init.body }) + return Response.json({ id: "resp_1" }) + } + return new Response("", { status: 404 }) + } + const a = { ...api, metadata: { plan: "Max" } } + const hooks = await CommandCodePlugin() + const ms = await hooks.provider.models({ models: {} }, { auth: a }) + expect(Object.fromEntries(Object.entries(ms).map(([k, m]) => [k, m.api.npm]))).toEqual({ + "gpt-6-sol": "@ai-sdk/openai", + "moonshotai/Kimi-K3": "@ai-sdk/openai-compatible", + "claude-sonnet-5": "@ai-sdk/anthropic", + both: "@ai-sdk/openai", + }) + const l = await hooks.auth.loader(async () => a) + const res = await l.fetch("https://api.commandcode.ai/provider/v1/responses", { method: "POST", body: '{"model":"gpt-6-sol"}' }) + expect(await res.json()).toEqual({ id: "resp_1" }) + expect(sent).toEqual([{ path: "/provider/v1/responses", key: "own-key", body: '{"model":"gpt-6-sol"}' }]) +}) + +test("errors don't name Command Code, which magpie adds", () => { + expect(_internal.failure(500, JSON.stringify({ error: { message: "boom" } }))).toEqual({ status: 500, message: "boom" }) + expect(_internal.failure(403, "MODEL_NOT_IN_PLAN: gpt-6-sol")).toEqual({ status: 403, message: "Model not in plan: gpt-6-sol" }) +})