@@ -4,7 +4,8 @@ import { getModelClient } from "@/lib/models"
44import { toEnhancedPrompt } from "@/lib/enhanced-prompt"
55import ratelimit from "@/lib/ratelimit"
66import { fragmentSchema as schema } from "@/lib/schema"
7- import type { TemplatesDataObject } from "@/lib/templates"
7+ import type { TemplatesDataObject , TemplateId } from "@/lib/templates" // Import TemplateId
8+ import templatesDataFromFile from "@/lib/templates" // Import the actual templates data
89import { ProjectAnalyzer , type ProjectStructure } from "@/lib/project-analyzer"
910import { streamObject , type LanguageModel , type CoreMessage } from "ai"
1011import { logError , generateRequestId , validateRequestData } from "@/lib/debug"
@@ -46,7 +47,7 @@ export async function POST(req: Request) {
4647 messages,
4748 userID,
4849 teamID,
49- template ,
50+ selectedTemplateId , // Changed from 'template' to 'selectedTemplateId'
5051 model,
5152 config,
5253 uploadedFiles,
@@ -55,7 +56,7 @@ export async function POST(req: Request) {
5556 messages : CoreMessage [ ]
5657 userID : string
5758 teamID : string
58- template : TemplatesDataObject
59+ selectedTemplateId : TemplateId // Type is now TemplateId (string)
5960 model : LLMModel
6061 config : LLMModelConfig
6162 uploadedFiles ?: File [ ]
@@ -167,10 +168,12 @@ export async function POST(req: Request) {
167168 try {
168169 if ( projectStructure && userPrompt ) {
169170 console . log ( `[Chat API ${ requestId } ] Generating enhanced prompt with project context` )
170- systemPrompt = toEnhancedPrompt ( template , userPrompt , projectStructure )
171+ // Pass the full templatesDataFromFile to toEnhancedPrompt
172+ systemPrompt = toEnhancedPrompt ( templatesDataFromFile , userPrompt , projectStructure )
171173 } else {
172174 console . log ( `[Chat API ${ requestId } ] Using standard prompt generation` )
173- systemPrompt = generateFallbackPrompt ( template )
175+ // Pass the full templatesDataFromFile to generateFallbackPrompt
176+ systemPrompt = generateFallbackPrompt ( templatesDataFromFile )
174177 }
175178
176179 console . log ( `[Chat API ${ requestId } ] System prompt generated` )
@@ -302,21 +305,21 @@ export async function POST(req: Request) {
302305 }
303306}
304307
305- // Fallback prompt generation function
306308function generateFallbackPrompt ( template : TemplatesDataObject ) : string {
307- const validTemplates = Object . entries ( template )
308- . filter ( ( [ _ , t ] ) => typeof t === 'object' && t !== null && 'instructions' in t && 'lib' in t )
309- . map ( ( [ id , t ] , index ) => {
310- // Now t is known to be an object with at least instructions and lib
311- const templateObject = t as { instructions : string ; file ?: string | null ; lib : string [ ] ; port ?: number | null } ;
312- return `${ index + 1 } . ${ id } : "${ templateObject . instructions } ". File: ${ templateObject . file || 'none' } . Dependencies: ${ templateObject . lib . join ( ', ' ) } . Port: ${ templateObject . port || 'none' } .` ;
313- } ) ;
314-
315309 return `You are an expert software engineer with deep knowledge of modern web development, programming languages, frameworks, and best practices.
316310
317311Generate production-ready code based on the user's requirements using the following templates:
318312
319- ${ validTemplates . join ( '\n' ) }
313+ ${ Object . entries ( template ) . map ( ( [ id , t ] , index ) => {
314+ const instructions = "instructions" in t
315+ ? t . instructions
316+ : ( t . files as any ) . instructions ;
317+ const port = "port" in t
318+ ? t . port
319+ : ( t . files as any ) . port ;
320+ const fileNames = Object . keys ( t . files ) . join ( ', ' ) ;
321+ return `${ index + 1 } . ${ id } : "${ instructions } ". Files: ${ fileNames || 'none' } . Dependencies: ${ t . lib . join ( ', ' ) } . Port: ${ port ?? 'none' } .` ;
322+ } ) . join ( '\n' ) }
320323
321324IMPORTANT GUIDELINES:
322325- Write clean, maintainable, and well-documented code
0 commit comments