diff --git a/packages/qoder/index.mjs b/packages/qoder/index.mjs index 396e650..1e984da 100644 --- a/packages/qoder/index.mjs +++ b/packages/qoder/index.mjs @@ -892,6 +892,13 @@ const TASKS = new Map() const TASK_MAX = 512 const TASK_IDLE = 2 * 60 * 60 * 1000 +// taskName is the business record's name for a task whose prompt is +// prompt, as Qoder's client names an agent task: the prompt's first ten +// characters (qoder-agent-sdk's AgentLifecycle, hdr → C6s: type +// "code_review" takes the session id, every other the prompt, sliced to +// 10 when longer). +const taskName = (prompt) => (prompt.length > 10 ? prompt.slice(0, 10) : prompt) + // taskOf is the task chat's call belongs to, in session sid: made on its // first call, the same one on every call after. function taskOf(sid, chat, now = Date.now()) { @@ -905,7 +912,7 @@ function taskOf(sid, chat, now = Date.now()) { let t = TASKS.get(key) if (t && now - t.used > TASK_IDLE) t = undefined TASKS.delete(key) - t ??= { id: randomUUID(), begin: now, answered: false } + t ??= { id: randomUUID(), begin: now, answered: false, name: taskName(at < 0 ? "" : textOf(msgs[at].content)) } t.used = now TASKS.set(key, t) while (TASKS.size > TASK_MAX) TASKS.delete(TASKS.keys().next().value) @@ -936,7 +943,7 @@ function qoderBody(chat, m, named = "", task) { chat_record_id: request, session_id: session, parameters: params, - business: { product: "app", version: COSY_VERSION, type: "agent", id: task.id, name: "magpie session", begin_at: task.begin, stage: task.answered ? "processing" : "start" }, + business: { product: "app", version: COSY_VERSION, type: "agent", id: task.id, name: task.name ?? "", begin_at: task.begin, stage: task.answered ? "processing" : "start" }, agent_id: "agent_common", task_id: "common", session_type: "app", diff --git a/packages/qoder/no-magpie.test.mjs b/packages/qoder/no-magpie.test.mjs new file mode 100644 index 0000000..8ad82e9 --- /dev/null +++ b/packages/qoder/no-magpie.test.mjs @@ -0,0 +1,71 @@ +// 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("继续修复这个构建失败") +}) diff --git a/packages/qoder/package.json b/packages/qoder/package.json index be7a5c4..de22c23 100644 --- a/packages/qoder/package.json +++ b/packages/qoder/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-qoder-auth", - "version": "0.2.32", + "version": "0.2.33", "description": "Qoder subscriptions in OpenCode and magpie: Qoder (qoder.com) and Qoder CN (qoder.cn)", "type": "module", "main": "./index.mjs",