Files
plugins/packages/zed/gemini-enums.test.mjs
yetoneandump45nose 3618bf9f22 zed 0.1.14: a Gemini model's effort variants are keyed lowercase, and its thinking level reaches Zed uppercase (#215)
Zed's cloud parses a Google request with its own serde enums (zed
crates/google_ai): ThinkingLevel is rename_all UPPERCASE (MINIMAL, LOW,
MEDIUM, HIGH) and FunctionCallingMode rename_all lowercase (auto, any,
none). A thinking budget reached it as thinkingLevel "low" and was
answered 400 "failed to parse Google AI request: unknown variant `low`,
expected one of `MINIMAL`, `LOW`, `MEDIUM`, `HIGH`" (iron-water, #215).

- The gemini wire in providerRequest() now goes through geminiEnums(),
  from ump45nose's #587: the thinking level is uppercased (an empty one
  left out) and the tool-calling mode lowercased, as 0.1.13 already did,
  with VALIDATED, which Zed has no variant for, asked as auto.
- Zed's /models now lists a Gemini model's supported_effort_levels
  uppercase (LOW/MEDIUM/HIGH; Yezike-yyy's Student-plan list on #215), so
  the variants were keyed LOW and carried thinkingLevel "LOW". magpie's pi
  profile, which maps efforts by key, matched none of them, and
  @ai-sdk/google (thinkingLevel z.enum minimal/low/medium/high) refuses
  an uppercase level. effortsOf() now lowercases the values whatever case
  /models writes, once each, so a variant keeps the AI SDK's lowercase
  spelling (the owner's review on #587) and only the wire is uppercase.
  This covers every vendor's efforts, not only Google's.
- _internal exports model() so the test checks provider.models' variants.

Tests: gemini-enums.test.mjs builds its model list from the
supported_effort_levels Yezike-yyy pasted on #215, byte for byte, and its
fake /completions refuses both enums with Zed's error text.

Run on mote at f4e2ac3 plus this change: packages/zed `bun test` 61 pass,
0 fail; root `HOME=$(mktemp -d) bun test` 4545 pass, 0 fail; `bun
scripts/check.mjs` ok. (A root run with a HOME outside tmpdir gives the
same 20 devin/isolated-home reds on origin/main as with this change.)
Broken by hand:
- origin/main's index.mjs under the new test: 7 of 7 fail;
- effortsOf without the lowercase: 4 fail;
- the wire level not uppercased: 2 fail;
- VALIDATED not mapped to auto: 1 fail.
Not tried: a live request to cloud.zed.dev and OpenCode with the real
@ai-sdk/google (no Zed account here). Yezike-yyy reports the same two
changes, patched into 0.1.13, answered 200 for low/medium/high on
zed/gemini-4-argon through magpie on Windows with pi.

Thanks to Yezike-yyy for the uppercase /models finding and its check
through pi.

Co-authored-by: ump45nose <52391318+ump45nose@users.noreply.github.com>
2026-11-01 23:44:10 +08:00

141 lines
8.2 KiB
JavaScript

// Zed's cloud parses a Google request with its own serde enums (zed
// crates/google_ai): ThinkingLevel UPPERCASE only, FunctionCallingMode
// lowercase only. A thinking budget reached it as thinkingLevel "low" and was
// answered 400 "failed to parse Google AI request: unknown variant `low`,
// expected one of `MINIMAL`, `LOW`, `MEDIUM`, `HIGH`" (#215, iron-water).
// Zed's /models then came to list a Gemini model's efforts uppercase, so its
// variants were keyed LOW/MEDIUM/HIGH and hosts that map efforts by key found
// none (#215, Yezike-yyy). A variant stays as @ai-sdk/google takes it
// (lowercase); only the wire is uppercase (ump45nose, #587).
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" }
// supported_effort_levels as Zed's /models lists them on a Student plan on
// 2026-11-01, byte for byte from #215 (gemini-3.5-flash's list was cut off
// after LOW there).
const LISTED = {
"gemini-4-argon": `[{"name":"Low","value":"LOW"},{"name":"Medium","value":"MEDIUM","is_default":true},{"name":"High","value":"HIGH"}]`,
"gemini-3.1-pro-preview": `[{"name":"Low","value":"LOW"},{"name":"Medium","value":"MEDIUM"},{"name":"High","value":"HIGH","is_default":true}]`,
"gemini-3.8-flash": `[{"name":"Low","value":"LOW"},{"name":"Medium","value":"MEDIUM","is_default":true},{"name":"High","value":"HIGH"}]`,
"gemini-3.5-flash": `[{"name":"Minimal","value":"MINIMAL"},{"name":"Low","value":"LOW"}]`,
}
const listed = (id) => ({ provider: "google", id, supports_thinking: true, supported_effort_levels: JSON.parse(LISTED[id]) })
// @ai-sdk/google's providerOptions.google.thinkingLevel:
// z.enum(['minimal','low','medium','high']) (google-language-model-options.ts)
const AI_SDK_LEVELS = ["minimal", "low", "medium", "high"]
test("a Gemini model Zed lists with uppercase efforts declares lowercase variants", () => {
for (const [id, want] of [
["gemini-4-argon", ["low", "medium", "high"]],
["gemini-3.1-pro-preview", ["low", "medium", "high"]],
["gemini-3.8-flash", ["low", "medium", "high"]],
["gemini-3.5-flash", ["minimal", "low"]],
]) {
const e = _internal.entry(listed(id))
expect(e.reasoning).toBe(true)
expect(Object.keys(e.variants)).toEqual(want)
for (const k of want) {
expect(e.variants[k]).toEqual({ thinkingConfig: { includeThoughts: true, thinkingLevel: k } })
expect(AI_SDK_LEVELS).toContain(e.variants[k].thinkingConfig.thinkingLevel)
}
// OpenCode's provider.models hands on the same variants
expect(Object.keys(_internal.model(listed(id)).variants)).toEqual(want)
}
})
test("lowercase efforts, as Zed listed them before, are unchanged; a doubled one is declared once", () => {
const e = _internal.entry({ provider: "google", id: "gemini-3.8-flash", supports_thinking: true, supported_effort_levels: [{ value: "low" }, { value: "medium" }, { value: "high" }] })
expect(Object.keys(e.variants)).toEqual(["low", "medium", "high"])
expect(e.variants.high.thinkingConfig.thinkingLevel).toBe("high")
const d = _internal.entry({ provider: "google", id: "g", supported_effort_levels: [{ value: "LOW" }, { value: "low" }, { value: "" }, {}, null] })
expect(Object.keys(d.variants)).toEqual(["low"])
})
test("another vendor's uppercase effort is keyed and given lowercase too", () => {
const a = _internal.entry({ provider: "anthropic", id: "claude-sonnet-5", supported_effort_levels: [{ value: "HIGH" }] })
expect(a.variants).toEqual({ high: { effort: "high" } })
const o = _internal.entry({ provider: "open_ai", id: "gpt-5.5", supported_effort_levels: [{ value: "Low" }] })
expect(o.variants).toEqual({ low: { reasoningEffort: "low" } })
})
// sent is the provider_request the plugin hands Zed for a Gemini body, from
// an upstream that parses both enums as Zed's cloud does.
async function sent(body, model = "gemini-4-argon") {
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 level = got.provider_request.generationConfig?.thinkingConfig?.thinkingLevel
if (level !== undefined && !["MINIMAL", "LOW", "MEDIUM", "HIGH"].includes(level)) {
return new Response(`failed to parse Google AI request: unknown variant \`${level}\`, expected one of \`MINIMAL\`, \`LOW\`, \`MEDIUM\`, \`HIGH\``, { status: 400 })
}
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: "Hi" }] }, finishReason: "STOP" }], usageMetadata: { promptTokenCount: 1, candidatesTokenCount: 1 } }),
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/${model}:streamGenerateContent?alt=sse`, { method: "POST", body: JSON.stringify(body) })
return { req: got.provider_request, res, text: await res.text() }
}
const tools = [{ functionDeclarations: [{ name: "Bash", description: "run a command", parameters: { type: "object", properties: { command: { type: "string" } }, required: ["command"] } }] }]
const ask = (extra = {}) => ({ contents: [{ role: "user", parts: [{ text: "hi" }] }], generationConfig: { maxOutputTokens: 64, ...extra.generationConfig }, ...(extra.toolConfig && { tools, toolConfig: extra.toolConfig }) })
test("each declared variant, as the AI SDK writes it, reaches Zed uppercase and is answered", async () => {
const e = _internal.entry(listed("gemini-4-argon"))
for (const [k, want] of [["low", "LOW"], ["medium", "MEDIUM"], ["high", "HIGH"]]) {
// @ai-sdk/google writes providerOptions.google.thinkingConfig into
// generationConfig as given
const { req, res, text } = await sent(ask({ generationConfig: { thinkingConfig: e.variants[k].thinkingConfig }, toolConfig: { functionCallingConfig: { mode: "AUTO" } } }))
expect(text).not.toContain("unknown variant")
expect(res.status).toBe(200)
expect(req.model).toBe("models/gemini-4-argon")
expect(req.generationConfig.thinkingConfig).toEqual({ includeThoughts: true, thinkingLevel: want })
expect(req.toolConfig.functionCallingConfig.mode).toBe("auto")
}
})
test("a thinking level in any case goes to Zed uppercase", async () => {
for (const [given, want] of [["minimal", "MINIMAL"], ["low", "LOW"], ["medium", "MEDIUM"], ["high", "HIGH"], ["HIGH", "HIGH"], ["High", "HIGH"]]) {
const { req, res } = await sent(ask({ generationConfig: { thinkingConfig: { includeThoughts: true, thinkingLevel: given } } }))
expect(res.status).toBe(200)
expect(req.generationConfig.thinkingConfig).toEqual({ includeThoughts: true, thinkingLevel: want })
}
})
test("an empty thinking level is left out; a budget alone is sent as it was", async () => {
let { req, res } = await sent(ask({ generationConfig: { thinkingConfig: { includeThoughts: true, thinkingLevel: "" } } }))
expect(res.status).toBe(200)
expect(req.generationConfig.thinkingConfig).toEqual({ includeThoughts: true })
;({ req, res } = await sent(ask({ generationConfig: { thinkingConfig: { thinkingBudget: 2000 } } })))
expect(res.status).toBe(200)
expect(req.generationConfig.thinkingConfig).toEqual({ thinkingBudget: 2000 })
})
test("VALIDATED, which Zed has no mode for, is asked as auto", async () => {
const { req, res } = await sent(ask({ toolConfig: { functionCallingConfig: { mode: "VALIDATED", allowedFunctionNames: ["Bash"] } } }))
expect(res.status).toBe(200)
expect(req.toolConfig.functionCallingConfig).toEqual({ mode: "auto", allowedFunctionNames: ["Bash"] })
})