Open History to see the last commit
1import { createFileRoute } from '@tanstack/react-router'
2import { generateImage } from '@tanstack/ai'
3import { createImageAdapter } from '@/lib/media-providers'
4
5/**
6 * Wire-format verification for Gemini-native `modelOptions` on the image
7 * generation path (fix/gemini-native-image-model-options).
8 *
9 * Before that fix, `GeminiImageAdapter`'s `generateWithGeminiApi` only ever
10 * forwarded `modelOptions.seed` into the `generateContent` request —
11 * `safetySettings`, `thinkingConfig`, `imageConfig`, and `systemInstruction`
12 * were silently dropped even though the adapter's provider-options type
13 * (`GeminiNativeImageProviderOptions`) already declared them. This route
14 * drives `generateImage()` against `gemini-2.5-flash-image` with
15 * `modelOptions: { safetySettings, thinkingConfig }` set, hitting
16 * `geminiNativeImageMount` in global-setup.ts — a hand-mocked
17 * `POST /v1beta/models/gemini-2.5-flash-image:generateContent` endpoint that
18 * reads the raw, untranslated request body (aimock's own journal cannot see
19 * these fields for this endpoint — see that mount's comment) and rejects
20 * with 400 unless `safetySettings` is present at the request root and
21 * `generationConfig.thinkingConfig` is present nested, and rejects unless no
22 * Imagen-only field (`personGeneration`, `negativePrompt`, a root-level
23 * `aspectRatio`, …) is present anywhere in the body.
24 *
25 * A regression that stops forwarding `modelOptions` on this path — reverting
26 * to only `seed`, or reverting to a wholesale `...modelOptions` spread that
27 * lets an Imagen field cross over — makes the mount reject the request, the
28 * adapter's `client.models.generateContent()` call throws, and this route
29 * returns `ok: false`. The companion spec asserts `ok: true`.
30 */
31export const Route = createFileRoute('/api/gemini-native-image-wire')({
32 server: {
33 handlers: {
34 POST: async () => {
35 const adapter = createImageAdapter('gemini')
36
37 try {
38 const result = await generateImage({
39 adapter,
40 prompt: 'a guitar in a music store',
41 stream: false,
42 modelOptions: {
43 safetySettings: [
44 {
45 category: 'HARM_CATEGORY_DANGEROUS_CONTENT',
46 threshold: 'BLOCK_ONLY_HIGH',
47 },
48 ],
49 thinkingConfig: { thinkingBudget: 128 },
50 },
51 })
52 return new Response(
53 JSON.stringify({ ok: true, images: result.images.length }),
54 {
55 status: 200,
56 headers: { 'Content-Type': 'application/json' },
57 },
58 )
59 } catch (error) {
60 return new Response(
61 JSON.stringify({
62 ok: false,
63 error: error instanceof Error ? error.message : String(error),
64 }),
65 { status: 200, headers: { 'Content-Type': 'application/json' } },
66 )
67 }
68 },
69 },
70 },
71})