Open History to see the last commit
1import { useState } from 'react'
2import { createFileRoute } from '@tanstack/react-router'
3import { ChefHat, Loader2, Clock, Users, Gauge } from 'lucide-react'
4import ReactMarkdown from 'react-markdown'
5import remarkGfm from 'remark-gfm'
6
7import type { Recipe } from './api.structured'
8
9type Provider =
10 | 'openai'
11 | 'anthropic'
12 | 'gemini'
13 | 'ollama'
14 | 'grok'
15 | 'openrouter'
16type Mode = 'structured' | 'oneshot'
17
18interface StructuredProvider {
19 id: Provider
20 name: string
21}
22
23const PROVIDERS: Array<StructuredProvider> = [
24 { id: 'openai', name: 'OpenAI (GPT-4o)' },
25 { id: 'anthropic', name: 'Anthropic (Claude Sonnet)' },
26 { id: 'gemini', name: 'Gemini (2.5 Flash)' },
27 { id: 'grok', name: 'Grok (Grok 3 Mini)' },
28 { id: 'ollama', name: 'Ollama (Mistral 7B)' },
29 { id: 'openrouter', name: 'OpenRouter (GPT-4o)' },
30]
31
32const SAMPLE_RECIPES = [
33 'Homemade Margherita Pizza',
34 'Thai Green Curry',
35 'Classic Beef Bourguignon',
36 'Chocolate Lava Cake',
37 'Crispy Korean Fried Chicken',
38 'Fresh Spring Rolls with Peanut Sauce',
39 'Creamy Mushroom Risotto',
40 'Authentic Pad Thai',
41]
42
43function RecipeCard({ recipe }: { recipe: Recipe }) {
44 const difficultyColors = {
45 easy: 'bg-green-500/20 text-green-400',
46 medium: 'bg-yellow-500/20 text-yellow-400',
47 hard: 'bg-red-500/20 text-red-400',
48 }
49
50 return (
51 <div className="space-y-6">
52 {/* Header */}
53 <div>
54 <h3 className="text-2xl font-bold text-white mb-2">{recipe.name}</h3>
55 <p className="text-gray-400">{recipe.description}</p>
56 </div>
57
58 {/* Meta info */}
59 <div className="flex flex-wrap gap-4">
60 <div className="flex items-center gap-2 text-gray-300">
61 <Clock className="w-4 h-4 text-orange-400" />
62 <span className="text-sm">Prep: {recipe.prepTime}</span>
63 </div>
64 <div className="flex items-center gap-2 text-gray-300">
65 <Clock className="w-4 h-4 text-orange-400" />
66 <span className="text-sm">Cook: {recipe.cookTime}</span>
67 </div>
68 <div className="flex items-center gap-2 text-gray-300">
69 <Users className="w-4 h-4 text-orange-400" />
70 <span className="text-sm">{recipe.servings} servings</span>
71 </div>
72 <div
73 className={`flex items-center gap-2 px-2 py-1 rounded-full ${difficultyColors[recipe.difficulty]}`}
74 >
75 <Gauge className="w-4 h-4" />
76 <span className="text-sm capitalize">{recipe.difficulty}</span>
77 </div>
78 </div>
79
80 {/* Ingredients */}
81 <div>
82 <h4 className="text-lg font-semibold text-white mb-3">Ingredients</h4>
83 <ul className="grid grid-cols-1 md:grid-cols-2 gap-2">
84 {recipe.ingredients.map((ing, idx) => (
85 <li key={idx} className="flex items-start gap-2 text-gray-300">
86 <span className="text-orange-400">•</span>
87 <span>
88 <span className="font-medium">{ing.amount}</span> {ing.item}
89 {ing.notes && (
90 <span className="text-gray-500 text-sm"> ({ing.notes})</span>
91 )}
92 </span>
93 </li>
94 ))}
95 </ul>
96 </div>
97
98 {/* Instructions */}
99 <div>
100 <h4 className="text-lg font-semibold text-white mb-3">Instructions</h4>
101 <ol className="space-y-3">
102 {recipe.instructions.map((step, idx) => (
103 <li key={idx} className="flex gap-3 text-gray-300">
104 <span className="flex-shrink-0 w-6 h-6 bg-orange-500/20 text-orange-400 rounded-full flex items-center justify-center text-sm font-medium">
105 {idx + 1}
106 </span>
107 <span>{step}</span>
108 </li>
109 ))}
110 </ol>
111 </div>
112
113 {/* Tips */}
114 {recipe.tips && recipe.tips.length > 0 && (
115 <div>
116 <h4 className="text-lg font-semibold text-white mb-3">Tips</h4>
117 <ul className="space-y-2">
118 {recipe.tips.map((tip, idx) => (
119 <li key={idx} className="flex items-start gap-2 text-gray-300">
120 <span className="text-yellow-400">💡</span>
121 <span>{tip}</span>
122 </li>
123 ))}
124 </ul>
125 </div>
126 )}
127
128 {/* Nutrition */}
129 {recipe.nutritionPerServing && (
130 <div>
131 <h4 className="text-lg font-semibold text-white mb-3">
132 Nutrition (per serving)
133 </h4>
134 <div className="flex flex-wrap gap-4 text-sm">
135 {recipe.nutritionPerServing.calories && (
136 <span className="px-3 py-1 bg-gray-700 rounded-full text-gray-300">
137 {recipe.nutritionPerServing.calories} cal
138 </span>
139 )}
140 {recipe.nutritionPerServing.protein && (
141 <span className="px-3 py-1 bg-gray-700 rounded-full text-gray-300">
142 Protein: {recipe.nutritionPerServing.protein}
143 </span>
144 )}
145 {recipe.nutritionPerServing.carbs && (
146 <span className="px-3 py-1 bg-gray-700 rounded-full text-gray-300">
147 Carbs: {recipe.nutritionPerServing.carbs}
148 </span>
149 )}
150 {recipe.nutritionPerServing.fat && (
151 <span className="px-3 py-1 bg-gray-700 rounded-full text-gray-300">
152 Fat: {recipe.nutritionPerServing.fat}
153 </span>
154 )}
155 </div>
156 </div>
157 )}
158 </div>
159 )
160}
161
162function StructuredPage() {
163 const [recipeName, setRecipeName] = useState('')
164 const [provider, setProvider] = useState<Provider>('openai')
165 const [mode, setMode] = useState<Mode>('structured')
166 const [result, setResult] = useState<{
167 mode: Mode
168 recipe?: Recipe
169 markdown?: string
170 provider: string
171 model: string
172 } | null>(null)
173 const [isLoading, setIsLoading] = useState(false)
174 const [error, setError] = useState<string | null>(null)
175
176 const handleGenerate = async () => {
177 if (!recipeName.trim()) return
178
179 setIsLoading(true)
180 setError(null)
181 setResult(null)
182
183 try {
184 const response = await fetch('/api/structured', {
185 method: 'POST',
186 headers: { 'Content-Type': 'application/json' },
187 body: JSON.stringify({ recipeName, provider, mode }),
188 })
189
190 const data = await response.json()
191
192 if (!response.ok) {
193 throw new Error(data.error || 'Failed to generate recipe')
194 }
195
196 setResult(data)
197 } catch (err: any) {
198 setError(err.message)
199 } finally {
200 setIsLoading(false)
201 }
202 }
203
204 return (
205 <div className="min-h-[calc(100vh-72px)] bg-gray-900 p-6">
206 <div className="max-w-6xl mx-auto">
207 <div className="flex items-center gap-3 mb-6">
208 <ChefHat className="w-8 h-8 text-orange-500" />
209 <h1 className="text-2xl font-bold text-white">
210 One-Shot & Structured Output
211 </h1>
212 </div>
213
214 <p className="text-gray-400 mb-6">
215 Compare two output modes:{' '}
216 <strong className="text-orange-400">One-Shot</strong> returns freeform
217 markdown, while{' '}
218 <strong className="text-orange-400">Structured</strong> returns
219 validated JSON conforming to a Zod schema.
220 </p>
221
222 <div className="grid grid-cols-1 lg:grid-cols-3 gap-6">
223 {/* Input Panel */}
224 <div className="space-y-4">
225 <div>
226 <label className="block text-sm font-medium text-gray-300 mb-2">
227 Provider
228 </label>
229 <select
230 value={provider}
231 onChange={(e) => setProvider(e.target.value as Provider)}
232 disabled={isLoading}
233 className="w-full rounded-lg border border-orange-500/20 bg-gray-800 px-3 py-2 text-sm text-white focus:outline-none focus:ring-2 focus:ring-orange-500/50"
234 >
235 {PROVIDERS.map((p) => (
236 <option key={p.id} value={p.id}>
237 {p.name}
238 </option>
239 ))}
240 </select>
241 </div>
242
243 <div>
244 <label className="block text-sm font-medium text-gray-300 mb-2">
245 Output Mode
246 </label>
247 <div className="grid grid-cols-2 gap-2">
248 <button
249 onClick={() => setMode('oneshot')}
250 disabled={isLoading}
251 className={`px-4 py-2 rounded-lg text-sm font-medium transition-colors ${
252 mode === 'oneshot'
253 ? 'bg-orange-500 text-white'
254 : 'bg-gray-800 text-gray-400 hover:text-white border border-orange-500/20'
255 }`}
256 >
257 One-Shot (Markdown)
258 </button>
259 <button
260 onClick={() => setMode('structured')}
261 disabled={isLoading}
262 className={`px-4 py-2 rounded-lg text-sm font-medium transition-colors ${
263 mode === 'structured'
264 ? 'bg-orange-500 text-white'
265 : 'bg-gray-800 text-gray-400 hover:text-white border border-orange-500/20'
266 }`}
267 >
268 Structured (JSON)
269 </button>
270 </div>
271 </div>
272
273 <div>
274 <label className="block text-sm font-medium text-gray-300 mb-2">
275 Recipe Name
276 </label>
277 <input
278 type="text"
279 value={recipeName}
280 onChange={(e) => setRecipeName(e.target.value)}
281 disabled={isLoading}
282 placeholder="e.g., Chocolate Chip Cookies"
283 className="w-full rounded-lg border border-orange-500/20 bg-gray-800 px-3 py-2 text-sm text-white focus:outline-none focus:ring-2 focus:ring-orange-500/50"
284 />
285 </div>
286
287 <div>
288 <label className="block text-sm font-medium text-gray-300 mb-2">
289 Quick Picks
290 </label>
291 <div className="flex flex-wrap gap-2">
292 {SAMPLE_RECIPES.map((name) => (
293 <button
294 key={name}
295 onClick={() => setRecipeName(name)}
296 disabled={isLoading}
297 className="px-2 py-1 text-xs bg-gray-800 hover:bg-gray-700 text-gray-300 rounded-lg border border-gray-700 transition-colors"
298 >
299 {name}
300 </button>
301 ))}
302 </div>
303 </div>
304
305 <button
306 onClick={handleGenerate}
307 disabled={isLoading || !recipeName.trim()}
308 className="w-full px-4 py-3 bg-orange-600 hover:bg-orange-700 disabled:bg-gray-600 text-white rounded-lg font-medium transition-colors flex items-center justify-center gap-2"
309 >
310 {isLoading ? (
311 <>
312 <Loader2 className="w-5 h-5 animate-spin" />
313 Generating...
314 </>
315 ) : (
316 'Generate Recipe'
317 )}
318 </button>
319
320 {/* Schema Preview */}
321 <div className="mt-6 p-4 bg-gray-800/50 rounded-lg border border-gray-700">
322 <h4 className="text-sm font-medium text-gray-400 mb-2">
323 Structured Output Schema
324 </h4>
325 <pre className="text-xs text-gray-500 overflow-x-auto">
326 {`z.object({
327 name: z.string(),
328 description: z.string(),
329 prepTime: z.string(),
330 cookTime: z.string(),
331 servings: z.number(),
332 difficulty: z.enum(['easy', 'medium', 'hard']),
333 ingredients: z.array(z.object({
334 item: z.string(),
335 amount: z.string(),
336 notes: z.string().optional(),
337 })),
338 instructions: z.array(z.string()),
339 tips: z.array(z.string()).optional(),
340 nutritionPerServing: z.object({...}).optional(),
341})`}
342 </pre>
343 </div>
344 </div>
345
346 {/* Output Panel */}
347 <div className="lg:col-span-2 bg-gray-800 rounded-lg p-6 border border-orange-500/20">
348 <div className="flex items-center justify-between mb-4">
349 <h2 className="text-lg font-semibold text-white">
350 Generated Recipe
351 </h2>
352 {result && (
353 <span
354 className={`px-2 py-1 rounded text-xs font-medium ${
355 result.mode === 'structured'
356 ? 'bg-purple-500/20 text-purple-400'
357 : 'bg-blue-500/20 text-blue-400'
358 }`}
359 >
360 {result.mode === 'structured'
361 ? 'Structured JSON'
362 : 'Markdown'}
363 </span>
364 )}
365 </div>
366
367 {error && (
368 <div className="p-4 bg-red-500/10 border border-red-500/20 rounded-lg text-red-400 mb-4">
369 {error}
370 </div>
371 )}
372
373 {result ? (
374 <div className="space-y-4">
375 {result.mode === 'structured' && result.recipe ? (
376 <RecipeCard recipe={result.recipe} />
377 ) : result.markdown ? (
378 <div className="prose prose-invert max-w-none">
379 <ReactMarkdown remarkPlugins={[remarkGfm]}>
380 {result.markdown}
381 </ReactMarkdown>
382 </div>
383 ) : null}
384
385 <div className="pt-4 border-t border-gray-700 text-sm text-gray-400">
386 <p>
387 Provider:{' '}
388 <span className="text-orange-400">{result.provider}</span>
389 </p>
390 <p>
391 Model:{' '}
392 <span className="text-orange-400">{result.model}</span>
393 </p>
394 </div>
395 </div>
396 ) : !error && !isLoading ? (
397 <div className="flex flex-col items-center justify-center h-64 text-gray-500">
398 <ChefHat className="w-16 h-16 mb-4 opacity-50" />
399 <p>
400 Enter a recipe name and click "Generate Recipe" to get
401 started.
402 </p>
403 </div>
404 ) : null}
405 </div>
406 </div>
407 </div>
408 </div>
409 )
410}
411
412export const Route = createFileRoute('/structured')({
413 component: StructuredPage,
414})