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

app.ts

449 lines12.9 KB

1import { Hono } from 'hono'
2import {
3 chat,
4 chatParamsFromRequestBody,
5 EventType,
6 maxIterations,
7 mergeAgentTools,
8 toolDefinition,
9 toHttpResponse,
10 toServerSentEventsResponse,
11} from '@tanstack/ai'
12import { openaiText, OPENAI_CHAT_MODELS } from '@tanstack/ai-openai'
13import type { ModelMessage, StreamChunk, UIMessage } from '@tanstack/ai'
14import type { OpenAIChatModel } from '@tanstack/ai-openai'
15​
16const DEFAULT_OPENAI_MODEL = 'gpt-5.2'
17const OPENAI_CHAT_MODEL_NAMES: ReadonlySet<string> = new Set(OPENAI_CHAT_MODELS)
18​
19export const LIVE_RECIPE_SERVER_ERROR =
20 'OPENAI_API_KEY is required to run the live recipe example'
21​
22type RecipePrompt = {
23 messages: Array<ModelMessage | UIMessage>
24 systemPrompts: Array<string>
26​
27const recipeOutputSchema = {
28 type: 'object',
29 properties: {
30 title: { type: 'string' },
31 summary: { type: 'string' },
32 servings: { type: 'number' },
33 totalMinutes: { type: 'number' },
34 tags: { type: 'array', items: { type: 'string' } },
35 ingredients: {
36 type: 'array',
37 items: {
38 type: 'object',
39 properties: {
40 name: { type: 'string' },
41 amount: { type: 'string' },
42 pantry: { type: 'boolean' },
43 },
44 required: ['name', 'amount', 'pantry'],
45 additionalProperties: false,
46 },
47 },
48 steps: {
49 type: 'array',
50 items: {
51 type: 'object',
52 properties: {
53 title: { type: 'string' },
54 text: { type: 'string' },
55 minutes: { type: 'number' },
56 },
57 required: ['title', 'text', 'minutes'],
58 additionalProperties: false,
59 },
60 },
61 tips: { type: 'array', items: { type: 'string' } },
62 warnings: { type: 'array', items: { type: 'string' } },
63 revision: { type: 'number' },
64 fromPrompt: { type: 'array', items: { type: 'string' } },
65 },
66 required: [
67 'title',
68 'summary',
69 'servings',
70 'totalMinutes',
71 'tags',
72 'ingredients',
73 'steps',
74 'tips',
75 'warnings',
76 'revision',
77 'fromPrompt',
78 ],
79 additionalProperties: false,
81​
82const RECIPE_SYSTEM_PROMPT = `You are a live recipe assistant for a React Native streaming demo.
83​
84Always return exactly one complete recipe matching the provided structured output schema.
85Use the full conversation history. When the latest user message is a follow-up such as "make it vegan", "remove mushrooms", "less spicy", or "for 4 people", revise the previous assistant recipe instead of starting from scratch.
86Create a new recipe on every user turn, and make the revision field one higher than the latest previous recipe revision when one is present.
87Call useful server tools when pantry details, substitutions, nutrition, or allergen constraints would improve the recipe. Incorporate tool results into the final recipe, but do not mention internal tool mechanics to the user.
88Respect explicit constraints from the latest user message over earlier recipe details. If the user asks to remove an ingredient, do not include that ingredient. If the user changes serving count, update servings and ingredient amounts.
89Keep titles, summaries, steps, tips, and warnings concise.`
90​
91function isOpenAIChatModel(value: string): value is OpenAIChatModel {
92 return OPENAI_CHAT_MODEL_NAMES.has(value)
94​
95function getOpenAIModel(): OpenAIChatModel {
96 const configuredModel = process.env.OPENAI_MODEL ?? DEFAULT_OPENAI_MODEL
97 if (!isOpenAIChatModel(configuredModel)) {
98 throw new Error(
99 `OPENAI_MODEL must be one of the known @tanstack/ai-openai chat models. Received: ${configuredModel}`,
100 )
101 }
102 return configuredModel
104​
105function requireOpenAIApiKey(): string {
106 const apiKey = process.env.OPENAI_API_KEY
107 if (!apiKey) {
108 throw new Error(LIVE_RECIPE_SERVER_ERROR)
109 }
110 return apiKey
112​
113function supportsReasoningOptions(model: OpenAIChatModel) {
114 return (
115 model.startsWith('gpt-5') ||
116 model.startsWith('o1') ||
117 model.startsWith('o3') ||
118 model.startsWith('o4')
119 )
121​
122function getOpenAIModelOptions(model: OpenAIChatModel) {
123 if (!supportsReasoningOptions(model)) return undefined
124 return {
125 reasoning: {
126 summary: 'auto',
127 },
128 } as const
130​
131export function createRecipePrompt(
132 messages: Array<ModelMessage | UIMessage>,
133): RecipePrompt {
134 return {
135 messages,
136 systemPrompts: [RECIPE_SYSTEM_PROMPT],
137 }
139​
140function isRecord(value: unknown): value is Record<string, unknown> {
141 return !!value && typeof value === 'object'
143​
144function readString(value: unknown, key: string): string {
145 if (!isRecord(value)) return ''
146 const field = value[key]
147 return typeof field === 'string' ? field : ''
149​
150function readNumber(value: unknown, key: string): number {
151 if (!isRecord(value)) return 0
152 const field = value[key]
153 return typeof field === 'number' ? field : 0
155​
156function readStringArray(value: unknown, key: string): Array<string> {
157 if (!isRecord(value)) return []
158 const field = value[key]
159 if (!Array.isArray(field)) return []
160 return field.filter((item): item is string => typeof item === 'string')
162​
163const getPantrySnapshot = toolDefinition({
164 name: 'getPantrySnapshot',
165 description:
166 'Return available pantry ingredients, fresh ingredients, and household dietary notes for recipe planning.',
167 inputSchema: {
168 type: 'object',
169 properties: {
170 focus: {
171 type: 'string',
172 description: 'The recipe planning focus for the pantry lookup.',
173 },
174 },
175 required: ['focus'],
176 additionalProperties: false,
177 },
178 outputSchema: {
179 type: 'object',
180 properties: {
181 staples: { type: 'array', items: { type: 'string' } },
182 fresh: { type: 'array', items: { type: 'string' } },
183 allergens: { type: 'array', items: { type: 'string' } },
184 notes: { type: 'string' },
185 },
186 required: ['staples', 'fresh', 'allergens', 'notes'],
187 additionalProperties: false,
188 },
189}).server((args) => {
190 const focus = readString(args, 'focus') || 'recipe planning'
191 return {
192 staples: [
193 'canned tomatoes',
194 'spaghetti',
195 'olive oil',
196 'chili flakes',
197 'white beans',
198 'brown rice',
199 'coconut milk',
200 ],
201 fresh: ['garlic', 'parsley', 'lemon', 'mushrooms', 'spinach'],
202 allergens: ['gluten'],
203 notes: `Pantry lookup for: ${focus}. Ask the model to adapt if the user removes an ingredient or changes servings.`,
204 }
205})
206​
207const suggestIngredientSubstitutions = toolDefinition({
208 name: 'suggestIngredientSubstitutions',
209 description:
210 'Suggest practical substitutions when the user removes ingredients or asks for dietary changes.',
211 inputSchema: {
212 type: 'object',
213 properties: {
214 ingredient: {
215 type: 'string',
216 description: 'Ingredient to replace or avoid.',
217 },
218 dietaryGoal: {
219 type: 'string',
220 description:
221 'Dietary goal such as vegan, gluten-free, mild, or high-protein.',
222 },
223 },
224 required: ['ingredient', 'dietaryGoal'],
225 additionalProperties: false,
226 },
227 outputSchema: {
228 type: 'object',
229 properties: {
230 replacements: { type: 'array', items: { type: 'string' } },
231 guidance: { type: 'string' },
232 },
233 required: ['replacements', 'guidance'],
234 additionalProperties: false,
235 },
236}).server((args) => {
237 const ingredient = readString(args, 'ingredient') || 'the removed ingredient'
238 const dietaryGoal = readString(args, 'dietaryGoal') || 'the requested diet'
239 return {
240 replacements: [
241 'white beans',
242 'spinach',
243 'roasted peppers',
244 'zucchini',
245 'chickpeas',
246 ],
247 guidance: `For ${dietaryGoal}, omit ${ingredient} entirely and choose replacements that preserve texture and protein without violating the latest user request.`,
248 }
249})
250​
251const estimateRecipeNutrition = toolDefinition({
252 name: 'estimateRecipeNutrition',
253 description:
254 'Provide rough nutrition guidance for a planned recipe and serving count.',
255 inputSchema: {
256 type: 'object',
257 properties: {
258 recipeIdea: {
259 type: 'string',
260 description: 'Short description of the recipe being planned.',
261 },
262 servings: {
263 type: 'number',
264 description: 'Number of servings requested by the user.',
265 },
266 },
267 required: ['recipeIdea', 'servings'],
268 additionalProperties: false,
269 },
270 outputSchema: {
271 type: 'object',
272 properties: {
273 perServing: {
274 type: 'object',
275 properties: {
276 calories: { type: 'number' },
277 proteinGrams: { type: 'number' },
278 fiberGrams: { type: 'number' },
279 },
280 required: ['calories', 'proteinGrams', 'fiberGrams'],
281 additionalProperties: false,
282 },
283 note: { type: 'string' },
284 },
285 required: ['perServing', 'note'],
286 additionalProperties: false,
287 },
288}).server((args) => {
289 const recipeIdea = readString(args, 'recipeIdea') || 'the planned recipe'
290 const servings = readNumber(args, 'servings') || 2
291 return {
292 perServing: {
293 calories: 420,
294 proteinGrams: 18,
295 fiberGrams: 9,
296 },
297 note: `Rough estimate for ${servings} servings of ${recipeIdea}; keep nutrition language approximate.`,
298 }
299})
300​
301const checkAllergenGuidance = toolDefinition({
302 name: 'checkAllergenGuidance',
303 description:
304 'Return allergen and safety guidance for ingredients or dietary constraints.',
305 inputSchema: {
306 type: 'object',
307 properties: {
308 ingredients: {
309 type: 'array',
310 items: { type: 'string' },
311 description: 'Ingredients the model is considering.',
312 },
313 dietaryConstraints: {
314 type: 'array',
315 items: { type: 'string' },
316 description: 'Constraints from the conversation.',
317 },
318 },
319 required: ['ingredients', 'dietaryConstraints'],
320 additionalProperties: false,
321 },
322 outputSchema: {
323 type: 'object',
324 properties: {
325 warnings: { type: 'array', items: { type: 'string' } },
326 guidance: { type: 'string' },
327 },
328 required: ['warnings', 'guidance'],
329 additionalProperties: false,
330 },
331}).server((args) => {
332 const ingredients = readStringArray(args, 'ingredients')
333 const dietaryConstraints = readStringArray(args, 'dietaryConstraints')
334 const warnings = ingredients.some((ingredient) =>
335 ingredient.toLowerCase().includes('spaghetti'),
336 )
337 ? ['Spaghetti usually contains gluten; use gluten-free pasta if needed.']
338 : []
339​
340 return {
341 warnings,
342 guidance: `Apply constraints: ${dietaryConstraints.join(', ') || 'none'}. Keep warnings practical and visible in the final recipe.`,
343 }
344})
345​
346const serverTools = [
347 getPantrySnapshot,
348 suggestIngredientSubstitutions,
349 estimateRecipeNutrition,
350 checkAllergenGuidance,
352​
353async function createChatStream(
354 body: unknown,
355): Promise<AsyncIterable<StreamChunk>> {
356 const params = await chatParamsFromRequestBody(body)
357 const recipePrompt = createRecipePrompt(params.messages)
358 requireOpenAIApiKey()
359 const model = getOpenAIModel()
360 const tools = mergeAgentTools(serverTools, params.tools)
361 const modelOptions = getOpenAIModelOptions(model)
362​
363 return chat({
364 adapter: openaiText(model),
365 tools,
366 messages: recipePrompt.messages,
367 systemPrompts: recipePrompt.systemPrompts,
368 threadId: params.threadId,
369 runId: params.runId,
370 parentRunId: params.parentRunId,
371 agentLoopStrategy: maxIterations(6),
372 outputSchema: recipeOutputSchema,
373 stream: true,
374 ...(modelOptions ? { modelOptions: modelOptions as never } : {}),
375 }) as AsyncIterable<StreamChunk>
377​
378function getErrorMessage(error: unknown): string {
379 return error instanceof Error ? error.message : 'Bad request'
381​
382function getRequestRunContext(body: unknown) {
383 if (!body || typeof body !== 'object') return {}
384​
385 const threadId =
386 'threadId' in body && typeof body.threadId === 'string'
387 ? body.threadId
388 : undefined
389 const runId =
390 'runId' in body && typeof body.runId === 'string' ? body.runId : undefined
391​
392 return {
393 ...(threadId ? { threadId } : {}),
394 ...(runId ? { runId } : {}),
395 }
397​
398async function* createErrorStream(
399 error: unknown,
400 body: unknown,
401): AsyncIterable<StreamChunk> {
402 const message = getErrorMessage(error)
403​
404 yield {
405 type: EventType.RUN_ERROR,
406 ...getRequestRunContext(body),
407 message,
408 code: 'REACT_NATIVE_EXAMPLE_SERVER_ERROR',
409 error: {
410 message,
411 code: 'REACT_NATIVE_EXAMPLE_SERVER_ERROR',
412 },
413 }
415​
416export const app = new Hono()
417​
418app.get('/health', (c) => c.json({ ok: true }))
419​
420app.post('/chat/http', async (c) => {
421 let body: unknown
422 try {
423 body = await c.req.json()
424 const stream = await createChatStream(body)
425 return toHttpResponse(stream, {
426 headers: {
427 'Content-Type': 'application/x-ndjson',
428 'Cache-Control': 'no-cache',
429 },
430 })
431 } catch (error) {
432 return toHttpResponse(createErrorStream(error, body), {
433 headers: {
434 'Content-Type': 'application/x-ndjson',
435 'Cache-Control': 'no-cache',
436 },
437 })
438 }
439})
440​
441app.post('/chat/sse', async (c) => {
442 let body: unknown
443 try {
444 body = await c.req.json()
445 return toServerSentEventsResponse(await createChatStream(body))
446 } catch (error) {
447 return toServerSentEventsResponse(createErrorStream(error, body))
448 }
449})