diff --git a/packages/zed/index.mjs b/packages/zed/index.mjs index 2b30e07..84a2f64 100644 --- a/packages/zed/index.mjs +++ b/packages/zed/index.mjs @@ -630,6 +630,13 @@ function providerRequest(wire, req, model) { } } if (Array.isArray(req.messages)) req.messages = withoutSystemTurns(req.messages) + // Zed's cloud takes a message's content as blocks only: a string, which + // Anthropic's own API takes as one text block, is answered 400 "failed + // to parse Anthropic request: invalid type: string, expected a + // sequence" (magpie's model test sends "hi" so; Been on Discord) + for (const m of Array.isArray(req.messages) ? req.messages : []) { + if (typeof m?.content === "string") m.content = asBlocks(m.content) + } return { req, stream } } case "responses": { diff --git a/packages/zed/package.json b/packages/zed/package.json index a885a4b..50b448d 100644 --- a/packages/zed/package.json +++ b/packages/zed/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-zed-auth", - "version": "0.1.11", + "version": "0.1.12", "description": "Sign in to a Zed subscription (Zed Pro, trial, student, business) from OpenCode or magpie", "type": "module", "main": "./index.mjs", diff --git a/packages/zed/string-content.test.mjs b/packages/zed/string-content.test.mjs new file mode 100644 index 0000000..84a987f --- /dev/null +++ b/packages/zed/string-content.test.mjs @@ -0,0 +1,73 @@ +// Zed's cloud takes an Anthropic message's content as blocks only. A string, +// which Anthropic's own API takes as one text block, is answered 400 "failed +// to parse Anthropic request: invalid type: string \"hi\", expected a +// sequence": magpie's model test sends {"role":"user","content":"hi"}, and +// every Claude model failed its test (Been on Discord, plugin 0.1.11). +import { afterEach, expect, test } from "bun:test" +import { _internal } from "./index.mjs" + +const real = globalThis.fetch +afterEach(() => { + globalThis.fetch = real + _internal.tokens.clear() +}) + +const s = { userId: "4242", access: "plain-access", systemId: "", org: "org-me", plan: "zed_pro", who: "octo" } + +// sent is the provider_request the plugin hands Zed for an Anthropic body. +async function sent(body) { + let got + globalThis.fetch = async (url, init) => { + const path = new URL(String(url)).pathname + if (path === "/client/llm_tokens") return Response.json({ token: "llm-token" }) + if (path === "/completions") { + got = JSON.parse(init.body) + const ev = (e) => JSON.stringify({ event: e }) + return new Response( + [ + ev({ type: "message_start", message: { id: "m1", type: "message", role: "assistant", model: "claude-sonnet-5", content: [], usage: { input_tokens: 1, output_tokens: 0 } } }), + ev({ type: "content_block_start", index: 0, content_block: { type: "text", text: "" } }), + ev({ type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "Hi" } }), + ev({ type: "content_block_stop", index: 0 }), + ev({ type: "message_delta", delta: { stop_reason: "end_turn" }, usage: { output_tokens: 1 } }), + ev({ type: "message_stop" }), + JSON.stringify({ status: "stream_ended" }), + ].join("\n") + "\n", + { headers: { "x-zed-server-supports-status-messages": "true" } }, + ) + } + return new Response("", { status: 404 }) + } + const res = await _internal.complete(s, "https://cloud.zed.dev/v1/messages", { method: "POST", body: JSON.stringify(body) }) + return { req: got.provider_request, res, text: await res.text() } +} + +test("magpie's model test, a string content, goes to Zed as one text block", async () => { + // the body magpie's Test models sends (internal/provider/test.go tinyBody) + const { req, res, text } = await sent({ model: "claude-sonnet-5", max_tokens: 16, messages: [{ role: "user", content: "hi" }] }) + expect(req.messages).toEqual([{ role: "user", content: [{ type: "text", text: "hi" }] }]) + expect(res.status).toBe(200) + expect(JSON.parse(text).content).toEqual([{ type: "text", text: "Hi" }]) +}) + +test("each turn's string content becomes blocks; block content is left as sent", async () => { + const blocks = [{ type: "text", text: "and now?", cache_control: { type: "ephemeral" } }] + const { req } = await sent({ + model: "claude-sonnet-5", + stream: true, + max_tokens: 100, + system: "You are terse.", + messages: [ + { role: "user", content: "hello" }, + { role: "assistant", content: "Hi." }, + { role: "user", content: blocks }, + ], + }) + expect(req.messages).toEqual([ + { role: "user", content: [{ type: "text", text: "hello" }] }, + { role: "assistant", content: [{ type: "text", text: "Hi." }] }, + { role: "user", content: blocks }, + ]) + // the top-level system is as the agent sent it (Zed's StringOrContents) + expect(req.system).toBe("You are terse.") +}) diff --git a/packages/zed/system-role.test.mjs b/packages/zed/system-role.test.mjs index 96da9c1..49219ca 100644 --- a/packages/zed/system-role.test.mjs +++ b/packages/zed/system-role.test.mjs @@ -79,10 +79,11 @@ test("an empty system message that only carries a turn's effort is left out", as { role: "user", content: "again" }, ], }) + // (a string content goes as blocks: string-content.test.mjs) expect(r.messages).toEqual([ - { role: "user", content: "hi" }, - { role: "assistant", content: "hello" }, - { role: "user", content: "again" }, + { role: "user", content: [{ type: "text", text: "hi" }] }, + { role: "assistant", content: [{ type: "text", text: "hello" }] }, + { role: "user", content: [{ type: "text", text: "again" }] }, ]) expect(r.output_config).toEqual({ effort: "high" }) })