commandcode 0.1.10: a Go request says the agent's folder and git repository, not the home's (#86)
Found beside #86. A Go key's chat completion goes to /alpha/generate with the CLI's config, which said workingDir: homedir() and isGitRepo: false whatever folder the agent worked in. It now takes the folder and the git state magpie tells the plugin's fetch (x-magpie-work-dir, x-magpie-work-git). Untold, it is the CLI's config outside a project in the home, as before (a git word without a folder means nothing). The headers are taken off every request, the Provider API's too. Run: bun test in packages/commandcode (33 pass, new workplace.test.mjs), bun scripts/check.mjs commandcode. With the old index.mjs both new tests fail. Not tried with a real Go account.
This commit is contained in:
@@ -731,10 +731,37 @@ function parseArgs(s) {
|
||||
}
|
||||
}
|
||||
|
||||
// WORK_DIR and WORK_GIT are where magpie tells a plugin's fetch the agent
|
||||
// that asked works: the folder, percent-encoded, and "yes" or "no" for a
|
||||
// git repository, each left off when magpie doesn't know it (magpie's
|
||||
// provider.WorkDirHeader and WorkGitHeader). They are for the plugin, not
|
||||
// the vendor.
|
||||
const WORK_DIR = "x-magpie-work-dir"
|
||||
const WORK_GIT = "x-magpie-work-git"
|
||||
|
||||
// workPlace takes WORK_DIR and WORK_GIT off h: {dir?, git?}, what magpie
|
||||
// said of where the agent works.
|
||||
function workPlace(h) {
|
||||
const place = {}
|
||||
const dir = h.get(WORK_DIR)
|
||||
if (dir) {
|
||||
try {
|
||||
place.dir = decodeURIComponent(dir)
|
||||
} catch {}
|
||||
}
|
||||
const git = h.get(WORK_GIT)
|
||||
if (place.dir && (git === "yes" || git === "no")) place.git = git
|
||||
h.delete(WORK_DIR)
|
||||
h.delete(WORK_GIT)
|
||||
return place
|
||||
}
|
||||
|
||||
// generateBody is a chat completion request as the CLI asks
|
||||
// /alpha/generate: no project behind it, the CLI's config outside a git
|
||||
// repository, in the user's home.
|
||||
function generateBody(chat) {
|
||||
// /alpha/generate, with no structure, branch or status behind it. The
|
||||
// working directory and whether it is a git repository are the agent's as
|
||||
// magpie tells them (place, yetone magpie-community/plugins#86); untold,
|
||||
// the CLI's config outside a git repository, in the user's home.
|
||||
function generateBody(chat, place = {}) {
|
||||
const system = []
|
||||
const messages = []
|
||||
const names = {} // a tool call's id → its tool
|
||||
@@ -808,11 +835,11 @@ function generateBody(chat) {
|
||||
const date = `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}-${String(d.getDate()).padStart(2, "0")}`
|
||||
return {
|
||||
config: {
|
||||
workingDir: homedir(),
|
||||
workingDir: place.dir || homedir(),
|
||||
date,
|
||||
environment: process.platform,
|
||||
structure: [],
|
||||
isGitRepo: false,
|
||||
isGitRepo: place.git === "yes",
|
||||
currentBranch: "",
|
||||
mainBranch: "",
|
||||
gitStatus: "",
|
||||
@@ -986,11 +1013,11 @@ async function* events(body) {
|
||||
}
|
||||
|
||||
// generate answers a chat completion through /alpha/generate.
|
||||
async function generate(key, chat, signal) {
|
||||
async function generate(key, chat, signal, place) {
|
||||
const res = await fetch(API + "/alpha/generate", {
|
||||
method: "POST",
|
||||
headers: generateHeaders(key),
|
||||
body: JSON.stringify(generateBody(chat)),
|
||||
body: JSON.stringify(generateBody(chat, place)),
|
||||
signal,
|
||||
})
|
||||
if (!res.ok) return errorResponse(failure(res.status, (await res.text()).slice(0, 1 << 20)))
|
||||
@@ -1091,6 +1118,7 @@ export async function CommandCodePlugin({ client } = {}) {
|
||||
const key = await liveKey((await getAuth()) ?? auth)
|
||||
headers.set("Authorization", `Bearer ${key}`)
|
||||
headers.set("x-api-key", key)
|
||||
const place = workPlace(headers)
|
||||
const chatting = /\/chat\/completions$/.test(new URL(url).pathname)
|
||||
let chat
|
||||
if (chatting) {
|
||||
@@ -1100,7 +1128,7 @@ export async function CommandCodePlugin({ client } = {}) {
|
||||
} catch {
|
||||
return errorResponse({ status: 400, message: "a request that isn't JSON" })
|
||||
}
|
||||
if ((await planNow(key, saved)) === "Go") return kept(await generate(key, chat, init.signal))
|
||||
if ((await planNow(key, saved)) === "Go") return kept(await generate(key, chat, init.signal, place))
|
||||
}
|
||||
// the Provider API's answer goes on as it came, the account
|
||||
// kept: the built-in neither marked one lapsed nor cleared it
|
||||
@@ -1113,7 +1141,7 @@ export async function CommandCodePlugin({ client } = {}) {
|
||||
const text = await res.text()
|
||||
if (GO_NO_API.test(text)) {
|
||||
await isGo(key, (await getAuth()) ?? auth)
|
||||
if (chatting) return kept(await generate(key, chat, init.signal))
|
||||
if (chatting) return kept(await generate(key, chat, init.signal, place))
|
||||
}
|
||||
return kept(new Response(text, { status: res.status, statusText: res.statusText, headers: res.headers }))
|
||||
},
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@magpie-community/opencode-commandcode-auth",
|
||||
"version": "0.1.9",
|
||||
"version": "0.1.10",
|
||||
"description": "Command Code plans (Pro, GOAT, Max, Ultra, Go, Teams) in OpenCode and magpie",
|
||||
"type": "module",
|
||||
"main": "./index.mjs",
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
// magpie-community/plugins#86: a Go request to /alpha/generate said the
|
||||
// user's home as the working directory, outside a git repository, whatever
|
||||
// folder the agent worked in. The folder and the repository are the
|
||||
// agent's as magpie tells them (x-magpie-work-dir, percent-encoded, and
|
||||
// x-magpie-work-git), which go to no vendor; untold, the config is the
|
||||
// CLI's in the user's home, as before.
|
||||
import { afterEach, expect, test } from "bun:test"
|
||||
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 setUp() {
|
||||
const sent = []
|
||||
globalThis.fetch = async (url, init = {}) => {
|
||||
const path = new URL(String(url)).pathname
|
||||
sent.push({ path, headers: new Headers(init.headers), body: init.body })
|
||||
if (path === "/alpha/generate") return new Response(JSON.stringify({ type: "text-delta", text: "hi" }) + "\n" + finish + "\n")
|
||||
if (path.startsWith("/provider/")) return Response.json({ choices: [] })
|
||||
return new Response("", { status: 404 })
|
||||
}
|
||||
return sent
|
||||
}
|
||||
|
||||
async function ask(plan, place) {
|
||||
const hooks = await CommandCodePlugin({ client: { auth: { set: async () => {} } } })
|
||||
const opts = await hooks.auth.loader(async () => ({ type: "api", key: "k", metadata: { plan } }))
|
||||
const headers = {}
|
||||
if (place.dir !== undefined) headers["x-magpie-work-dir"] = place.dir
|
||||
if (place.git !== undefined) headers["x-magpie-work-git"] = place.git
|
||||
return 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" }] }),
|
||||
})
|
||||
}
|
||||
|
||||
const config = (sent) => JSON.parse(sent.find((s) => s.path === "/alpha/generate").body).config
|
||||
|
||||
test("a Go request says the agent's folder and repository as magpie tells them (#86)", async () => {
|
||||
const dir = "C:\\Users\\me\\My Proj 中文 & <x>"
|
||||
let sent = setUp()
|
||||
expect((await ask("Go", { dir: encodeURIComponent(dir), git: "yes" })).status).toBe(200)
|
||||
expect(config(sent)).toMatchObject({ workingDir: dir, isGitRepo: true })
|
||||
for (const s of sent) {
|
||||
expect(s.headers.has("x-magpie-work-dir")).toBe(false)
|
||||
expect(s.headers.has("x-magpie-work-git")).toBe(false)
|
||||
}
|
||||
sent = setUp()
|
||||
await ask("Go", { dir: encodeURIComponent("/Users/me/notes"), git: "no" })
|
||||
expect(config(sent)).toMatchObject({ workingDir: "/Users/me/notes", isGitRepo: false })
|
||||
// a folder whose repository magpie doesn't know isn't said to be one
|
||||
sent = setUp()
|
||||
await ask("Go", { dir: encodeURIComponent("/srv/x") })
|
||||
expect(config(sent)).toMatchObject({ workingDir: "/srv/x", isGitRepo: false })
|
||||
// untold: the CLI's own config outside a project; a git word alone
|
||||
// means nothing
|
||||
sent = setUp()
|
||||
await ask("Go", { git: "yes" })
|
||||
expect(config(sent)).toMatchObject({ workingDir: homedir(), isGitRepo: false })
|
||||
})
|
||||
|
||||
test("magpie's word on the folder goes to no vendor on the Provider API either", async () => {
|
||||
const sent = setUp()
|
||||
await ask("Pro", { dir: "%2Fx", git: "no" })
|
||||
const p = sent.find((s) => s.path.startsWith("/provider/"))
|
||||
expect(p.headers.has("x-magpie-work-dir")).toBe(false)
|
||||
expect(p.headers.has("x-magpie-work-git")).toBe(false)
|
||||
})
|
||||
Reference in New Issue
Block a user