diff --git a/packages/zed/gemini-tool-mode.test.mjs b/packages/zed/gemini-tool-mode.test.mjs new file mode 100644 index 0000000..e560ef2 --- /dev/null +++ b/packages/zed/gemini-tool-mode.test.mjs @@ -0,0 +1,64 @@ +// Zed's cloud reads a Gemini request's toolConfig.functionCallingConfig.mode +// in lowercase (auto, any, none). The Gemini API's proto enums, which the AI +// SDK and magpie's Code Assist envelope write (AUTO, ANY, NONE), are answered +// 400 "failed to parse Google AI request: unknown variant `AUTO`, expected one +// of `auto`, `any`, `none`": every Gemini turn that declared a tool failed +// while plain chat worked (atticuskk, magpie#4657). +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 a Gemini body, from +// an upstream that refuses a mode as Zed's cloud does. +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 mode = got.provider_request.toolConfig?.functionCallingConfig?.mode + if (mode !== undefined && !["auto", "any", "none"].includes(mode)) { + return new Response(`failed to parse Google AI request: unknown variant \`${mode}\`, expected one of \`auto\`, \`any\`, \`none\``, { status: 400 }) + } + const ev = (e) => JSON.stringify({ event: e }) + return new Response( + [ + ev({ candidates: [{ content: { role: "model", parts: [{ text: "ok" }] }, finishReason: "STOP" }], modelVersion: "gemini-3.5-flash" }), + 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/v1beta/models/gemini-3.5-flash:generateContent", { method: "POST", body: JSON.stringify(body) }) + return { req: got.provider_request, res, text: await res.text() } +} + +const tools = [{ functionDeclarations: [{ name: "get_weather", description: "Get weather", parameters: { type: "object", properties: { city: { type: "string" } } } }] }] +const ask = (toolConfig) => ({ contents: [{ role: "user", parts: [{ text: "Weather in Paris? Use the tool." }] }], tools, ...(toolConfig && { toolConfig }) }) + +for (const [mode, want] of [["AUTO", "auto"], ["ANY", "any"], ["NONE", "none"], ["auto", "auto"]]) { + test(`a tool-calling mode ${mode} goes to Zed as ${want}`, async () => { + const { req, res } = await sent(ask({ functionCallingConfig: { mode, ...(mode === "ANY" && { allowedFunctionNames: ["get_weather"] }) } })) + expect(res.status).toBe(200) + expect(req.toolConfig.functionCallingConfig.mode).toBe(want) + // nothing else in it moves + if (mode === "ANY") expect(req.toolConfig.functionCallingConfig.allowedFunctionNames).toEqual(["get_weather"]) + expect(req.tools).toEqual(tools) + }) +} + +test("a request with no toolConfig is sent without one", async () => { + const { req, res } = await sent(ask()) + expect(res.status).toBe(200) + expect(req.toolConfig).toBeUndefined() +}) diff --git a/packages/zed/index.mjs b/packages/zed/index.mjs index 84a2f64..672300b 100644 --- a/packages/zed/index.mjs +++ b/packages/zed/index.mjs @@ -658,6 +658,13 @@ function providerRequest(wire, req, model) { delete req.session_id delete req.sessionId req.model = "models/" + model + // Zed's cloud reads the tool-calling mode in lowercase (auto, any, + // none); the Gemini API's proto enums, as the AI SDK and magpie's + // Code Assist envelope write them (AUTO, ANY, NONE), are answered 400 + // "failed to parse Google AI request: unknown variant `AUTO`", so every + // turn that declared a tool failed (atticuskk, magpie#4657) + const fc = req.toolConfig?.functionCallingConfig + if (fc && typeof fc.mode === "string") fc.mode = fc.mode.toLowerCase() return { req } } } diff --git a/packages/zed/package.json b/packages/zed/package.json index 50b448d..ec117af 100644 --- a/packages/zed/package.json +++ b/packages/zed/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-zed-auth", - "version": "0.1.12", + "version": "0.1.13", "description": "Sign in to a Zed subscription (Zed Pro, trial, student, business) from OpenCode or magpie", "type": "module", "main": "./index.mjs",