Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

api.gemini-native-image-wire.ts

71 lines2.4 KB

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 adapter,
28 prompt: 'a guitar in a music store',
29 stream: false,
30 modelOptions: {
31 safetySettings: [
32 {
33 category: 'HARM_CATEGORY_DANGEROUS_CONTENT',
34 threshold: 'BLOCK_ONLY_HIGH',
35 },
36 ],
37 thinkingConfig: { thinkingLevel: 'LOW' },
38 },
39 })
40 const edit = await generateImage({
41 adapter,
42 prompt: 'Make the guitar red',
43 stream: false,
44 modelOptions: { previous_interaction_id: first.id },
45 })
46 return new Response(
47 JSON.stringify({
48 ok: true,
49 images: first.images.length,
50 editImages: edit.images.length,
51 firstId: first.id,
52 editId: edit.id,
53 }),
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})