Open History to see the last commit
1import { createFileRoute } from '@tanstack/react-router'
2import { chat, maxIterations, toServerSentEventsResponse } from '@tanstack/ai'
3import { openaiText } from '@tanstack/ai-openai'
4import {
5 getAvailableAddOnsToolDef,
6 selectAddOnsToolDef,
7 unselectAddOnsToolDef,
8} from '@/lib/addon-tools'
9
10const SYSTEM_PROMPT = `You are an AI assistant that helps users configure their project by selecting add-ons.
11
12IMPORTANT WORKFLOW:
131. When the user asks about available add-ons or wants to configure their project, FIRST call getAvailableAddOns to see what's available and their current selection state.
142. Use selectAddOns to enable add-ons the user wants.
153. Use unselectAddOns to disable add-ons the user doesn't want.
16
17CRITICAL RULES:
18- ALWAYS call getAvailableAddOns FIRST to see current state before making any selections.
19- When selecting/unselecting add-ons, you MUST call the appropriate tool - do NOT just describe what you would do.
20- You can select or unselect MULTIPLE add-ons in a single tool call by passing an array of IDs.
21- After making changes, briefly confirm what was changed.
22
23Available add-on categories:
24- authentication: User auth solutions (clerk)
25- payments: Payment processing (stripe)
26- database: Database ORMs (drizzle, prisma)
27- styling: CSS frameworks (tailwind)
28- ui-components: Component libraries (shadcn)
29- monitoring: Error tracking (sentry)
30- analytics: Product analytics (posthog)
31
32Example interactions:
33- "Add authentication" → Call getAvailableAddOns, then selectAddOns(["clerk"])
34- "I want payments and analytics" → Call getAvailableAddOns, then selectAddOns(["stripe", "posthog"])
35- "Remove the database ORM" → Call getAvailableAddOns to check which is selected, then unselectAddOns with the appropriate ID
36`
37
38export const Route = createFileRoute('/api/addon-chat')({
39 server: {
40 handlers: {
41 POST: async ({ request }) => {
42 // Capture request signal before reading body
43 const requestSignal = request.signal
44
45 // If request is already aborted, return early
46 if (requestSignal.aborted) {
47 return new Response(null, { status: 499 })
48 }
49
50 const abortController = new AbortController()
51
52 const body = await request.json()
53 const messages = body.messages
54
55 try {
56 const stream = chat({
57 adapter: openaiText('gpt-4o'),
58 tools: [
59 // Just the definitions - client will handle execution
60 getAvailableAddOnsToolDef,
61 selectAddOnsToolDef,
62 unselectAddOnsToolDef,
63 ],
64 systemPrompts: [SYSTEM_PROMPT],
65 agentLoopStrategy: maxIterations(10),
66 messages,
67 abortController,
68 })
69
70 return toServerSentEventsResponse(stream, { abortController })
71 } catch (error: any) {
72 console.error('[API Route] Error in addon-chat request:', error)
73
74 if (error.name === 'AbortError' || abortController.signal.aborted) {
75 return new Response(null, { status: 499 })
76 }
77
78 return new Response(
79 JSON.stringify({
80 error: error.message || 'An error occurred',
81 }),
82 {
83 status: 500,
84 headers: { 'Content-Type': 'application/json' },
85 },
86 )
87 }
88 },
89 },
90 },
91})