zed 0.1.12: a string message content goes to Zed's cloud as a text block

Zed's cloud parses an Anthropic body's message content as blocks only, and
answers a string, which Anthropic's own API takes, with 400 "failed to parse
Anthropic request: invalid type: string \"hi\", expected a sequence".
magpie's Test models sends {"role":"user","content":"hi"}, so every Claude
model failed its test in Zed's provider page (Been on Discord, 0.1.11). The
anthropic wire now turns each turn's string content into one text block; a
top-level system string is left as sent (Zed takes it either way).

Not here: Gemini's "missing field contents" in the same test is magpie
sending a non-Gemini body to a plugin's @ai-sdk/google model; fixed in
magpie.

Run (mote-run, bun 1.4.2): packages/zed bun test, 49 pass 0 fail.
Broken by hand: with index.mjs reverted, string-content.test.mjs is 0
pass 2 fail. system-role.test.mjs's effort test now expects blocks.
This commit is contained in:
yetone
2026-10-31 22:03:06 +08:00
parent 21cbeeefe0
commit 3e940bc81b
4 changed files with 85 additions and 4 deletions
+7
View File
@@ -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": {
+1 -1
View File
@@ -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",
+73
View File
@@ -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.")
})
+4 -3
View File
@@ -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" })
})