Open History to see the last commit
1import { createFileRoute } from '@tanstack/react-router'
2import { generateImage } from '@tanstack/ai'
3import { createGeminiImage, GeminiImageModels } from '@tanstack/ai-gemini'
4import type { ImageGenerationResult } from '@tanstack/ai'
5
6const LLMOCK_DEFAULT_BASE = process.env.LLMOCK_URL || 'http://127.0.0.1:4010'
7const IMAGE_BASE = `${LLMOCK_DEFAULT_BASE}/gemini-image`
8const DUMMY_KEY = 'sk-e2e-test-dummy-key'
9
10interface StageResult {
11 imageCount?: number
12 error?: string
13}
14
15async function runStage(
16 generate: () => Promise<ImageGenerationResult>,
17): Promise<StageResult> {
18 try {
19 const result = await generate()
20 return { imageCount: result.images.length }
21 } catch (error) {
22 return { error: error instanceof Error ? error.message : String(error) }
23 }
24}
25
26/**
27 * Regression coverage for #1104 (GA image model ids + per-model sizes).
28 *
29 * Runs each model call as an independent stage (each with its own
30 * try/catch) so a regression in one doesn't mask another — each spec
31 * asserts on only its own stage's result.
32 *
33 * 1. `gaModel`: `gemini-3.1-flash-image` — the GA id for "Nano Banana 2" —
34 * must be listed in the package's exported `GeminiImageModels` runtime
35 * array and must actually route a `generateImage()` call through
36 * the Interactions API to a returned image. Before the fix,
37 * only the shut-down `-preview` id was in that list (the GA id 404s
38 * against the real API otherwise). `GeminiImageModels` is genuine
39 * runtime data (an exported `as const` array), unlike the per-model
40 * `size` typing below, so `.includes()` on it is real, revert-detectable
41 * coverage independent of any TypeScript compile step — nothing in the
42 * adapter's routing logic gates on this array at runtime, so it's the
43 * only way to observe the id addition without a type-checker.
44 *
45 * All models are proxied through `geminiNativeImageMount` (global-setup.ts)
46 * on the `/gemini-image` prefix. aimock's own `/v1beta/interactions` handler
47 * streams text and has no image branch (see that mount's doc comment).
48 */
49export const Route = createFileRoute('/api/gemini-image-ga-models')({
50 server: {
51 handlers: {
52 POST: async () => {
53 const gaModelListed = GeminiImageModels.includes(
54 'gemini-3.1-flash-image',
55 )
56
57 const gaModel = await runStage(() =>
58 generateImage({
59 threadId: 'gemini-image-ga-models',
60 adapter: createGeminiImage('gemini-3.1-flash-image', DUMMY_KEY, {
61 httpOptions: { baseUrl: IMAGE_BASE },
62 }),
63 prompt: 'a guitar in a music store',
64 size: '16:9_2K',
65 }),
66 )
67
68 // #1640: `gemini-nano-banana-2.1` is a native image model only
69 // because it is in `GEMINI_NATIVE_IMAGE_MODELS`. Otherwise it goes
70 // to the Imagen endpoint, which the mock does not serve.
71 const nanoBanana21 = await runStage(() =>
72 generateImage({
73 threadId: 'gemini-image-ga-models',
74 adapter: createGeminiImage('gemini-nano-banana-2.1', DUMMY_KEY, {
75 httpOptions: { baseUrl: IMAGE_BASE },
76 }),
77 prompt: 'a guitar in a music store',
78 size: '16:9_2K',
79 }),
80 )
81
82 return new Response(
83 JSON.stringify({ gaModelListed, gaModel, nanoBanana21 }),
84 {
85 status: 200,
86 headers: { 'Content-Type': 'application/json' },
87 },
88 )
89 },
90 },
91 },
92})