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

ImageGenUI.tsx

150 lines4.1 KB

1import { useState } from 'react'
2import {
3 useGenerateImage,
4 fetchServerSentEvents,
5 fetchHttpStream,
6} from '@tanstack/ai-react'
7import { generateImageFn } from '@/lib/server-functions'
8import type { ImageGenerationResult, MediaPrompt } from '@tanstack/ai'
9import type { Mode, Provider } from '@/lib/types'
10​
11interface ImageGenUIProps {
12 provider: Provider
13 mode: Mode
14 testId?: string
15 aimockPort?: number
16 /** Show a file input and send the prompt as multimodal parts (image-to-image). */
17 withImageInput?: boolean
19​
20function fileToBase64(file: File): Promise<string> {
21 return new Promise((resolve, reject) => {
22 const reader = new FileReader()
23 reader.onload = () => {
24 const result = reader.result
25 if (typeof result !== 'string') {
26 reject(new Error('Unexpected FileReader result'))
27 return
28 }
29 resolve(result.split(',')[1] ?? '')
30 }
31 reader.onerror = () => reject(new Error('Failed to read file'))
32 reader.readAsDataURL(file)
33 })
35​
36export function ImageGenUI({
37 provider,
38 mode,
39 testId,
40 aimockPort,
41 withImageInput,
42}: ImageGenUIProps) {
43 const [prompt, setPrompt] = useState('')
44 const [imageFile, setImageFile] = useState<File | null>(null)
45​
46 const connectionOptions = () => {
47 const body = { provider, numberOfImages: 1, testId, aimockPort }
48​
49 if (mode === 'sse') {
50 return { connection: fetchServerSentEvents('/api/image'), body }
51 }
52 if (mode === 'http-stream') {
53 return { connection: fetchHttpStream('/api/image/stream'), body }
54 }
55 return {
56 fetcher: async (input: { prompt: MediaPrompt }) => {
57 return generateImageFn({
58 data: {
59 prompt: input.prompt,
60 provider,
61 numberOfImages: 1,
62 aimockPort,
63 testId,
64 },
65 }) as Promise<ImageGenerationResult>
66 },
67 }
68 }
69​
70 const { generate, result, isLoading, error, status } =
71 useGenerateImage(connectionOptions())
72​
73 const handleGenerate = async () => {
74 if (!imageFile) {
75 await generate({ prompt })
76 return
77 }
78 const base64 = await fileToBase64(imageFile)
79 await generate({
80 prompt: [
81 { type: 'text', content: prompt },
82 {
83 type: 'image',
84 source: { type: 'data', value: base64, mimeType: imageFile.type },
85 },
86 ],
87 })
88 }
89​
90 return (
91 <div className="p-4 space-y-4">
92 <div className="flex gap-2">
93 <input
94 data-testid="prompt-input"
95 type="text"
96 value={prompt}
97 onChange={(e) => setPrompt(e.target.value)}
98 placeholder="Describe the image..."
99 className="flex-1 bg-gray-800 border border-gray-700 rounded px-3 py-2 text-sm"
100 />
101 <button
102 data-testid="generate-button"
103 onClick={handleGenerate}
104 disabled={!prompt.trim() || isLoading}
105 className="px-4 py-2 bg-orange-500 text-white rounded text-sm font-medium disabled:opacity-50"
106 >
107 Generate
108 </button>
109 </div>
110 {withImageInput && (
111 <input
112 data-testid="image-input"
113 type="file"
114 accept="image/*"
115 onChange={(e) => setImageFile(e.target.files?.[0] ?? null)}
116 className="text-sm text-gray-400"
117 />
118 )}
119 <div data-testid="generation-status">
120 {status === 'idle'
121 ? 'idle'
122 : isLoading
123 ? 'loading'
124 : error
125 ? 'error'
126 : result
127 ? 'complete'
128 : 'idle'}
129 </div>
130 {error && (
131 <div data-testid="generation-error" className="text-red-400 text-sm">
132 {error.message}
133 </div>
134 )}
135 {result && (
136 <div className="grid grid-cols-2 gap-4">
137 {result.images.map((img, i) => (
138 <img
139 key={i}
140 data-testid="generated-image"
141 src={img.url || `data:image/png;base64,${img.b64Json}`}
142 alt={`Generated ${i + 1}`}
143 className="rounded border border-gray-700"
144 />
145 ))}
146 </div>
147 )}
148 </div>
149 )