commandcode 0.1.16: no request to Command Code names magpie (#352)
The Go catalog (/provider/v1/models) went with User-Agent "magpie" and every /alpha/generate with x-project-slug "magpie". Both are now what command-code 1.88.0's cli.mjs sends: - the catalog: installModelCatalogManager asks with accept alone under Node's fetch, whose User-Agent is "node"; - x-project-slug: buildCommandAuthHeaders' projectSlug is getCommandCurrentProjectDirName, slugify(basename(cwd)) || "root". The plugin takes the working folder magpie names (x-magpie-work-dir, which it already strips), else the home folder as before. A Windows path splits on "\" as the CLI's win32 basename does. A name past Latin-1 is percent-encoded: Node refuses such a header, so the CLI has no value there to copy and the request would otherwise throw (found by the existing #86 test's "中文" folder). Test: no-magpie.test.mjs records every request (URL, every header, body) for a Go chat with and without a folder, a Pro chat and the catalog, and fails on /magpie/i; red on the old code (3 of 3 fail), green now. Run: bun test in packages/commandcode (64 pass, 1 skip, 0 fail); bun scripts/check.mjs commandcode ok. Live, sandbox HOME, owner's GOAT key: Provider API chat 200, catalog 62 models, /alpha/generate 200 with x-project-slug "shop" (billing answered 503 locally so the saved Go plan routed it); the second saved key is dead (401) but its generate request went out clean too. 0 of 9 recorded requests named magpie. Not tried: a real Go-plan key; Windows.
This commit is contained in:
@@ -23,7 +23,7 @@ import { createServer, STATUS_CODES } from "node:http"
|
||||
import { randomBytes, randomUUID } from "node:crypto"
|
||||
import { readFile } from "node:fs/promises"
|
||||
import { homedir } from "node:os"
|
||||
import { join } from "node:path"
|
||||
import { basename, join } from "node:path"
|
||||
|
||||
const ID = "commandcode-plan"
|
||||
const API = "https://api.commandcode.ai"
|
||||
@@ -315,7 +315,9 @@ async function liveModels(key) {
|
||||
// the plan's (open) is let every model, as the CLI lets it.
|
||||
async function goModels(open = false) {
|
||||
const url = BASE + "/models"
|
||||
const res = await fetch(url, { headers: { Accept: "application/json", "User-Agent": "magpie" }, signal: AbortSignal.timeout(8_000) })
|
||||
// the CLI asks its catalog with accept alone, under Node's fetch, whose
|
||||
// User-Agent is "node" (installModelCatalogManager)
|
||||
const res = await fetch(url, { headers: { Accept: "application/json", "User-Agent": "node" }, signal: AbortSignal.timeout(8_000) })
|
||||
const text = await res.text()
|
||||
// catalog.FetchURL's errors
|
||||
if (res.status !== 200) {
|
||||
@@ -978,14 +980,25 @@ function zdrOn(magpie) {
|
||||
return process.env.CMD_ZDR === "1"
|
||||
}
|
||||
|
||||
// projectSlug is the CLI's x-project-slug for a working directory: its
|
||||
// last segment, "root" when it has none (getProjectDirName's slugify of
|
||||
// basename(cwd)); a Windows folder's too, as the CLI's win32 basename
|
||||
// splits it. A name a header can't carry (past Latin-1: the CLI's own
|
||||
// request fails there, Node refusing the header) goes percent-encoded.
|
||||
function projectSlug(dir) {
|
||||
const name = basename(String(dir || homedir()).replaceAll("\\", "/")) || "root"
|
||||
return /^[\x20-\x7e\xa0-\xff]*$/.test(name) ? name : encodeURIComponent(name)
|
||||
}
|
||||
|
||||
// generateHeaders are the CLI's own (buildCommandAuthHeaders), with its
|
||||
// x-cmd-zdr: 1 when ZDR is on.
|
||||
const generateHeaders = (key, zdr = false) => ({
|
||||
// x-cmd-zdr: 1 when ZDR is on; the project slug is the working folder's
|
||||
// name, as the CLI's is.
|
||||
const generateHeaders = (key, zdr = false, place = {}) => ({
|
||||
"Content-Type": "application/json",
|
||||
"User-Agent": "cli",
|
||||
"x-command-code-version": CLI_VERSION,
|
||||
"x-cli-environment": "production",
|
||||
"x-project-slug": "magpie",
|
||||
"x-project-slug": projectSlug(place.dir),
|
||||
"x-taste-learning": "false",
|
||||
"x-session-id": session,
|
||||
Authorization: `Bearer ${key}`,
|
||||
@@ -1166,7 +1179,7 @@ async function* events(body) {
|
||||
async function generate(key, chat, signal, place, zdr = false) {
|
||||
const res = await fetch(API + "/alpha/generate", {
|
||||
method: "POST",
|
||||
headers: generateHeaders(key, zdr),
|
||||
headers: generateHeaders(key, zdr, place),
|
||||
body: JSON.stringify(generateBody(chat, place)),
|
||||
signal,
|
||||
})
|
||||
@@ -1253,7 +1266,7 @@ function providerBody(chat) {
|
||||
|
||||
// ---- the plugin ---------------------------------------------------------------
|
||||
|
||||
export const _internal = { subsSeen, extraSeen, waits, liveKey, failure, goModels, refuses, GO_MODELS, GO_REFUSED, zdrOn }
|
||||
export const _internal = { projectSlug, subsSeen, extraSeen, waits, liveKey, failure, goModels, refuses, GO_MODELS, GO_REFUSED, zdrOn }
|
||||
|
||||
export async function CommandCodePlugin({ client, magpie } = {}) {
|
||||
// isGo keeps a key as Go's, for its models and endpoint: in the
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
// magpie-community/plugins#352: Command Code was told "magpie" twice, as
|
||||
// the Go catalog's User-Agent and as every /alpha/generate request's
|
||||
// x-project-slug. Nothing that goes upstream names the app: the catalog is
|
||||
// asked as the CLI asks it (Node's fetch, "node") and the slug is the
|
||||
// working folder's name, as the CLI's (basename(cwd) || "root").
|
||||
import { afterEach, expect, test } from "bun:test"
|
||||
import { basename } from "node:path"
|
||||
import { homedir } from "node:os"
|
||||
import { CommandCodePlugin, _internal } from "./index.mjs"
|
||||
|
||||
const real = globalThis.fetch
|
||||
afterEach(() => {
|
||||
globalThis.fetch = real
|
||||
_internal.subsSeen.clear()
|
||||
})
|
||||
|
||||
const finish = JSON.stringify({ type: "finish", finishReason: "stop", totalUsage: { inputTokens: 3, outputTokens: 1 } })
|
||||
|
||||
function record() {
|
||||
const sent = []
|
||||
globalThis.fetch = async (input, init = {}) => {
|
||||
const url = typeof input === "string" ? input : input instanceof URL ? input.href : input.url
|
||||
const headers = new Headers(init.headers ?? (input instanceof Request ? input.headers : undefined))
|
||||
const body = init.body == null ? "" : typeof init.body === "string" ? init.body : new TextDecoder().decode(init.body)
|
||||
sent.push({ url, path: new URL(url).pathname, headers, body })
|
||||
const path = new URL(url).pathname
|
||||
if (path === "/alpha/generate") return new Response(JSON.stringify({ type: "text-delta", text: "hi" }) + "\n" + finish + "\n")
|
||||
if (path.endsWith("/models")) return Response.json({ data: [{ id: "deepseek/deepseek-v4-pro", name: "DeepSeek V4 Pro" }] })
|
||||
if (path.startsWith("/provider/")) return Response.json({ choices: [] })
|
||||
return new Response("", { status: 404 })
|
||||
}
|
||||
return sent
|
||||
}
|
||||
|
||||
// upstream is everything a request carries: its URL, every header's name
|
||||
// and value, and its body
|
||||
const upstream = (s) => [s.url, ...[...s.headers].flat(), s.body].join("\n")
|
||||
|
||||
async function chat(plan, dir) {
|
||||
const hooks = await CommandCodePlugin({ client: { auth: { set: async () => {} } } })
|
||||
const opts = await hooks.auth.loader(async () => ({ type: "api", key: "k", metadata: { plan } }))
|
||||
const headers = dir === undefined ? {} : { "x-magpie-work-dir": encodeURIComponent(dir), "x-magpie-work-git": "yes" }
|
||||
const res = await opts.fetch("https://api.commandcode.ai/provider/v1/chat/completions", {
|
||||
method: "POST",
|
||||
headers,
|
||||
body: JSON.stringify({ model: "deepseek/deepseek-v4-pro", messages: [{ role: "user", content: "hi" }] }),
|
||||
})
|
||||
await res.text()
|
||||
}
|
||||
|
||||
test("no request to Command Code names magpie: Go, Provider API and the catalog (#352)", async () => {
|
||||
const sent = record()
|
||||
await chat("Go", "/Users/me/shop")
|
||||
await chat("Go")
|
||||
await chat("Pro", "/Users/me/shop")
|
||||
await _internal.goModels()
|
||||
expect(sent.some((s) => s.path === "/alpha/generate")).toBe(true)
|
||||
expect(sent.some((s) => s.path.endsWith("/models"))).toBe(true)
|
||||
for (const s of sent) expect(upstream(s)).not.toMatch(/magpie/i)
|
||||
})
|
||||
|
||||
test("the project slug is the working folder's name, as the CLI's", async () => {
|
||||
const sent = record()
|
||||
await chat("Go", "/Users/me/shop")
|
||||
await chat("Go")
|
||||
const slugs = sent.filter((s) => s.path === "/alpha/generate").map((s) => s.headers.get("x-project-slug"))
|
||||
expect(slugs).toEqual(["shop", basename(homedir()) || "root"])
|
||||
expect(_internal.projectSlug("/")).toBe("root")
|
||||
expect(_internal.projectSlug("C:\\Users\\me\\proj")).toBe("proj")
|
||||
expect(_internal.projectSlug("/Users/me/café")).toBe("café")
|
||||
expect(_internal.projectSlug("/Users/me/项目")).toBe(encodeURIComponent("项目"))
|
||||
})
|
||||
|
||||
test("the catalog is asked as the CLI's Node fetch asks it", async () => {
|
||||
const sent = record()
|
||||
await _internal.goModels()
|
||||
expect(sent.find((s) => s.path.endsWith("/models")).headers.get("user-agent")).toBe("node")
|
||||
})
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@magpie-community/opencode-commandcode-auth",
|
||||
"version": "0.1.15",
|
||||
"version": "0.1.16",
|
||||
"description": "Command Code plans (Pro, GOAT, Max, Ultra, Go, Teams) in OpenCode and magpie",
|
||||
"type": "module",
|
||||
"main": "./index.mjs",
|
||||
|
||||
Reference in New Issue
Block a user