Files
yetone da079585d9 qoder 0.2.33: a request's task name is the prompt's first ten characters, as Qoder's own client sends, not "magpie session" (#354)
business.name went upstream as "magpie session" on every request. Qoder.app
0.4.3's worker runtime (qoder-agent-sdk qoder-worker-runtime.obf.mjs) names a
task by its prompt sliced to 10 characters (the session id only for
code_review). The plugin now does the same with the task's first user
message, kept for the task's whole tool loop. Nothing old is read back from
Qoder for this field, so existing sessions are unaffected.

Run:
- no-magpie.test.mjs: qoder and qoder-cn, sessions "", "magpie-…" and
  "dsh-session-1"; URL, headers, wire body and decoded body hold no
  "magpie"; short, long and Chinese prompts; name kept through tool calls.
  All 7 red on the old code, green now.
- bun test (package): 209 pass. bun scripts/check.mjs qoder: ok.
- Live, sandbox HOME, the owner's Qoder account (auth.set stubbed, token
  not refreshed): model "efficient", 200 with a streamed reply; business
  {"name":"Reply with",…}; 11 requests recorded, none naming magpie.
Not tried: qoder-cn live (no CN account).
2026-10-24 17:23:45 +08:00

72 lines
4.1 KiB
JavaScript

// magpie-community/plugins#354: every chat request's business record was
// named "magpie session". Qoder's own client names an agent task by its
// prompt, the first ten characters of it (qoder-agent-sdk's
// AgentLifecycle: hdr → C6s), and so does the plugin now. Nothing that
// goes upstream names the app: the URL, every header, the wire body and
// the body under its encoding.
import { afterEach, beforeEach, expect, test } from "bun:test"
import { QoderAuthPlugin, QoderCNAuthPlugin, _internal } from "./index.mjs"
const { TASKS } = _internal
const real = globalThis.fetch
beforeEach(() => TASKS.clear())
afterEach(() => {
globalThis.fetch = real
TASKS.clear()
})
const json = (v) => new Response(JSON.stringify(v), { headers: { "Content-Type": "application/json" } })
const line = (body) => `data:${JSON.stringify({ headers: {}, body: typeof body === "string" ? body : JSON.stringify(body), statusCodeValue: 200 })}\n\n`
const answered = () =>
new Response(line({ choices: [{ delta: { content: "ok" } }] }) + line({ choices: [{ delta: {}, finish_reason: "stop" }] }) + line("[DONE]"), {
headers: { "Content-Type": "text/event-stream" },
})
async function run(make, id, session) {
const auth = { type: "oauth", access: "jt", refresh: "rt", expires: Date.now() + 3_600_000, accountId: "one@x", uid: "u1", machineId: "m1" }
const sent = []
globalThis.fetch = async (input, init = {}) => {
const url = String(input?.url ?? input)
const wire = init.body == null ? "" : typeof init.body === "string" ? init.body : new TextDecoder().decode(init.body)
sent.push({ url, headers: [...new Headers(init.headers)].flat(), wire })
if (new URL(url).pathname.endsWith("/model/list")) return json({ chat: [{ key: "dfmodel", display_name: "DeepSeek Flash", enable: true }] })
return answered()
}
const hooks = await make({ client: { auth: { set: async () => {} } } })
const l = await hooks.auth.loader(async () => auth)
const out = { headers: {} }
await hooks["chat.headers"]({ sessionID: session, model: { providerID: id, id: "dfmodel" } }, out)
const messages = [{ role: "system", content: "You are a coding agent." }, { role: "user", content: "fix the failing build on main" }]
const res = await l.fetch("https://api3.qoder.sh/v1/chat/completions", { method: "POST", headers: new Headers(out.headers), body: JSON.stringify({ model: "dfmodel", messages }) })
expect(res.status).toBe(200)
await res.text()
return sent
}
for (const [make, id] of [[QoderAuthPlugin, "qoder"], [QoderCNAuthPlugin, "qoder-cn"]])
for (const session of ["", "magpie-0123456789abcdef01234567", "dsh-session-1"])
test(`no request to ${id} names magpie (session ${JSON.stringify(session)}) (#354)`, async () => {
const sent = await run(make, id, session)
const chats = sent.filter((s) => !new URL(s.url).pathname.endsWith("/model/list"))
expect(chats.length).toBe(1)
for (const s of sent) {
expect([s.url, ...s.headers, s.wire].join("\n")).not.toMatch(/magpie/i)
if (s.wire) expect(_internal.decodeBody(s.wire)).not.toMatch(/magpie/i)
}
// named as Qoder's client names an agent task: its prompt's first ten
const body = JSON.parse(_internal.decodeBody(chats[0].wire))
expect(body.business.name).toBe("fix the fa")
})
test("a short prompt is the whole name; a task keeps its name through its tool loop", () => {
const { qoderBody } = _internal
const m = { key: "dfmodel", config: { key: "dfmodel" }, context: 128_000 }
const one = [{ role: "user", content: "hi" }]
const a = qoderBody({ messages: one }, m, "s1")
expect(a.business.name).toBe("hi")
const b = qoderBody({ messages: [...one, { role: "assistant", content: "", tool_calls: [{ id: "c", type: "function", function: { name: "ls", arguments: "{}" } }] }, { role: "tool", tool_call_id: "c", content: "x" }] }, m, "s1")
expect(b.business.id).toBe(a.business.id)
expect(b.business.name).toBe("hi")
expect(qoderBody({ messages: [{ role: "user", content: [{ type: "text", text: "继续修复这个构建失败的问题吧" }] }] }, m, "s2").business.name).toBe("继续修复这个构建失败")
})