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 image `modelOptions`.
7 *
8 * `geminiNativeImageMount` reads the Interactions request and 400s unless
9 * `safety_settings` is `[{ type: "dangerous_content", threshold:
10 * "block_only_high" }]`, `generation_config.thinking_level` is `"low"`, and
11 * no Imagen or generateContent field is present. A second call chains
12 * `previous_interaction_id` from the first result and must not resend an
13 * image. The mount answers `int_e2e_create` then `int_e2e_edit`.
14 *
15 * A dropped `modelOptions` field, a `thinkingBudget` that is sent instead of
16 * `thinkingLevel`, or a chained turn that re-sends the image makes the mount
17 * reject the request and this route returns `ok: false`.
18 */
19export const Route = createFileRoute('/api/gemini-native-image-wire')({
20 server: {
21 handlers: {
22 POST: async () => {
23 const adapter = createImageAdapter('gemini')
24
25 try {
26 const first = await generateImage({
27 threadId: 'gemini-native-image-wire',
28 adapter,
29 prompt: 'a guitar in a music store',
30 stream: false,
31 modelOptions: {
32 safetySettings: [
33 {
34 category: 'HARM_CATEGORY_DANGEROUS_CONTENT',
35 threshold: 'BLOCK_ONLY_HIGH',
36 },
37 ],
38 thinkingConfig: { thinkingLevel: 'LOW' },
39 },
40 })
41 const edit = await generateImage({
42 threadId: 'gemini-native-image-wire',
43 adapter,
44 prompt: 'Make the guitar red',
45 stream: false,
46 modelOptions: { previous_interaction_id: first.id },
47 })
48 return new Response(
49 JSON.stringify({
50 ok: true,
51 images: first.images.length,
52 editImages: edit.images.length,
53 firstId: first.id,
54 editId: edit.id,
55 }),
56 {
57 status: 200,
58 headers: { 'Content-Type': 'application/json' },
59 },
60 )
61 } catch (error) {
62 return new Response(
63 JSON.stringify({
64 ok: false,
65 error: error instanceof Error ? error.message : String(error),
66 }),
67 { status: 200, headers: { 'Content-Type': 'application/json' } },
68 )
69 }
70 },
71 },
72 },
73})