Files
plugins/packages/factory/gemini.test.mjs
T
William WANG b83e3dee36 factory 0.1.41: ZCode's environment paragraph reaches Factory as runtime context, not refused (#211, William-AIdev)
ZCode embeds its generated environment paragraph, with unindented bullets, inside a larger system prompt. No runtime-context adapter matched it, so "You have been invoked in the following environment:" and "You are powered by the model named" reached Factory unchanged in the system prompt (403). This renames a complete standalone paragraph in system prompts, later system messages and Gemini's system instruction, keeping every value. Also releases factory 0.1.41, which carries #213.

Reviewed at d4ef7d4e; the maintainer commits on top (eb016a4d) merge main b76a577, keep main's Gemini escape test beside this one, expect main's #258 escape in the Gemini user part, add a fenced-paragraph case and bump to 0.1.41.
- On main b76a577, a probe through _internal.droidBody still sends the paragraph unchanged on Responses and Messages; the PR's new tests fail there.
- At eb016a4d: `bun test` in packages/factory (sandbox HOME, on mote): 189 pass, 0 fail. `bun scripts/check.mjs factory`: ok.
- Broken by hand: zcodeRuntimeContext returning its input turns 5 tests red (Responses, Chat, Messages, Gemini, no-Shell). Dropping the insideFence check passed every original test (the fenced inputs never matched); the added fenced case goes red with it, green with it back.
- Not tried: a live Factory account (the PR body records the 403 on all four routes).
2026-10-29 21:09:00 +08:00

105 lines
7.2 KiB
JavaScript

// yetone/magpie#2080: Gemini's models go to Factory's own route,
// /api/llm/g/v1/generate, as magpie's built-in sends them (buildGemini,
// factoryDroidGoogle in internal/provider/factory_client.go).
import { afterEach, expect, test } from "bun:test"
import { FactoryAuthPlugin } from "./index.mjs"
const real = globalThis.fetch
afterEach(() => (globalThis.fetch = real))
const droid = "You are Droid, an AI software engineering agent built by Factory."
async function loaded(auth = { type: "oauth", access: "factory-token", expires: Date.now() + 3_600_000, activeOrganizationId: "org_A" }) {
const seen = []
globalThis.fetch = async (url, init) => {
seen.push({ url: String(url), ...init })
return new Response('data: {"candidates":[{"content":{"role":"model","parts":[{"text":"ok"}]}}]}\n\n', { headers: { "content-type": "text/event-stream" } })
}
const hooks = await FactoryAuthPlugin({ client: { auth: { set: async () => {} } } })
return { l: await hooks.auth.loader(async () => auth), seen }
}
// the request magpie's gateway hands a plugin's @ai-sdk/google model
const request = {
contents: [{ role: "user", parts: [{ text: "hi" }, { inlineData: { mimeType: "image/png", data: "aW1n" } }] }],
systemInstruction: { role: "user", parts: [{ text: "Be brief." }, { text: "Use tools." }] },
generationConfig: { maxOutputTokens: 512, thinkingConfig: { includeThoughts: true, thinkingLevel: "high" } },
tools: [{ functionDeclarations: [{ name: "Read", parameters: { type: "object" } }] }],
}
test("a Gemini model goes to /api/llm/g/v1/generate, the model in the body, x-api-provider google", async () => {
const { l, seen } = await loaded()
const res = await l.fetch("https://api.factory.ai/api/llm/g/v1/models/gemini-4-argon:streamGenerateContent?alt=sse", {
method: "POST",
headers: { "content-type": "application/json", "x-goog-api-key": "placeholder", "content-length": "1" },
body: JSON.stringify(request),
})
expect(seen[0].url).toBe("https://api.factory.ai/api/llm/g/v1/generate")
const sent = JSON.parse(seen[0].body)
expect(sent.model).toBe("gemini-4-argon")
expect(sent.systemInstruction).toEqual({ parts: [{ text: droid + "\nBe brief.\nUse tools." }] })
for (const k of ["contents", "generationConfig", "tools"]) expect(sent[k]).toEqual(request[k])
const h = seen[0].headers
expect(h.get("x-api-provider")).toBe("google")
expect(h.get("authorization")).toBe("Bearer factory-token")
expect(h.get("x-goog-api-key")).toBeNull()
expect(h.get("x-api-key")).toBeNull()
expect(h.get("content-length")).toBeNull()
expect(h.get("OpenAI-Platform")).toBeNull()
expect(await res.text()).toContain('"text":"ok"')
})
test("a Gemini request with no system instruction gets droid's line, one droid opened already goes on as it is", async () => {
const { l, seen } = await loaded()
const at = "https://api.factory.ai/api/llm/g/v1/models/gemini-3.5-flash:streamGenerateContent?alt=sse"
await l.fetch(at, { method: "POST", body: JSON.stringify({ contents: request.contents }) })
expect(JSON.parse(seen[0].body).systemInstruction).toEqual({ parts: [{ text: droid }] })
const own = { contents: request.contents, systemInstruction: { parts: [{ text: droid + "\nmine" }, { inlineData: { mimeType: "image/png", data: "eA==" } }] } }
await l.fetch(at, { method: "POST", body: JSON.stringify(own) })
expect(JSON.parse(seen[1].body)).toEqual({ ...own, model: "gemini-3.5-flash" })
// a string instruction and parts other than text keep their place after the line
await l.fetch(at, { method: "POST", body: JSON.stringify({ contents: request.contents, systemInstruction: "Plain." }) })
expect(JSON.parse(seen[2].body).systemInstruction).toEqual({ parts: [{ text: droid + "\nPlain." }] })
await l.fetch(at, { method: "POST", body: JSON.stringify({ contents: request.contents, systemInstruction: { parts: [{ text: "A" }, { inlineData: { mimeType: "image/png", data: "eA==" } }] } }) })
expect(JSON.parse(seen[3].body).systemInstruction).toEqual({ parts: [{ text: droid + "\nA" }, { inlineData: { mimeType: "image/png", data: "eA==" } }] })
})
test("an EU org's Gemini request goes to Factory's EU region's generate route", async () => {
const { l, seen } = await loaded({ type: "oauth", access: "factory-token", expires: Date.now() + 3_600_000, activeOrganizationId: "org_A", region: "eu" })
await l.fetch("https://api.factory.ai/api/llm/g/v1/models/gemini-3.8-flash:streamGenerateContent?alt=sse", { method: "POST", body: JSON.stringify(request) })
expect(new URL(seen[0].url).pathname).toBe("/api/llm/g/v1/generate")
expect(seen[0].url.startsWith("https://api.factory.ai/")).toBe(false)
})
// Gemini's route has no tool-output adaptation of its own: every refused
// phrase in its parts is escaped before the request leaves.
test("Gemini tool output and system instruction escape every refused phrase", async () => {
const { l, seen } = await loaded()
const env = "You have been invoked in the following environment:"
const pi = "You are an expert coding assistant operating inside pi, a coding agent harness."
const body = {
contents: [{ role: "user", parts: [{ functionResponse: { name: "Read", response: { text: env + " / " + pi } } }] }],
systemInstruction: { role: "user", parts: [{ text: env }] },
}
await l.fetch("https://api.factory.ai/api/llm/g/v1/models/gemini-4-argon:streamGenerateContent?alt=sse", { method: "POST", body: JSON.stringify(body) })
const sent = JSON.parse(seen[0].body)
const part = sent.contents[0].parts[0].functionResponse.response.text
expect(part).toBe("\\u0059ou have been invoked in the following environment: / \\u0059ou are an expert coding assistant operating inside pi, a coding agent harness.")
expect(sent.systemInstruction.parts[0].text).toContain("\\u0059ou have been invoked")
expect(JSON.stringify(sent)).not.toContain(env)
expect(JSON.stringify(sent)).not.toContain(pi)
})
test("adapts ZCode generated Gemini system context while preserving values and user content", async () => {
const { l, seen } = await loaded()
const context = "# Environment\nYou have been invoked in the following environment:\n- Primary working directory: /tmp/project\n- Is a git repository: no\n- Platform: darwin\n- Shell: zsh\n- OS Version: darwin 27.0.0 arm64\n- You are powered by the model named test-model.\n"
const body = { systemInstruction: { parts: [{ text: context }] }, contents: [{ role: "user", parts: [{ text: context }] }] }
const url = "https://api.factory.ai/api/llm/g/v1/models/gemini-3-flash-preview:generateContent"
await l.fetch(url, { method: "POST", body: JSON.stringify(body) })
const sent = JSON.parse(seen[0].body)
expect(sent.systemInstruction.parts[0].text).toBe(droid + "\n" + context.replace("# Environment", "# Runtime context").replace("You have been invoked in the following environment:", "The session environment is:").replace("- You are powered by the model named", "- Current model name:"))
// user parts are not adapted; the Gemini route escapes the refused phrase in them (#258)
expect(sent.contents).toEqual([{ role: "user", parts: [{ text: context.replace("You have been invoked", "\\u0059ou have been invoked") }] }])
await l.fetch(url, { method: "POST", body: JSON.stringify(sent) })
expect(JSON.parse(seen[1].body)).toEqual(sent)
})