@@ -121,29 +121,51 @@ const buildSystemPromptFromTemplate = async (templateContent: string, data: {
121121// System prompt builder function
122122export const buildSystemPrompt = cache ( async ( ) : Promise < string > => {
123123 try {
124+ console . log ( 'Starting system prompt build process...' )
125+
124126 // Read the main template file
125127 const templatePath = path . join ( process . cwd ( ) , 'content/system-prompt.md' )
126- const templateContent = fs . readFileSync ( templatePath , 'utf8' )
128+ console . log ( `Reading main template from: ${ templatePath } ` )
129+
130+ let templateContent : string
131+ try {
132+ templateContent = fs . readFileSync ( templatePath , 'utf8' )
133+ console . log ( `Successfully read main template (${ templateContent . length } characters)` )
134+ } catch ( error ) {
135+ console . error ( `Failed to read main template file: ${ error } ` )
136+ throw new Error ( `Failed to read system prompt template: ${ error instanceof Error ? error . message : 'Unknown error' } ` )
137+ }
127138
128139 // Read the additional system prompt file if it exists
129140 const additionalTemplatePath = path . join ( process . cwd ( ) , 'content/system-prompt-extended.md' )
130141 let additionalContent = ''
142+ console . log ( `Checking for extended template at: ${ additionalTemplatePath } ` )
131143 try {
132144 additionalContent = fs . readFileSync ( additionalTemplatePath , 'utf8' )
145+ console . log ( `Successfully read extended template (${ additionalContent . length } characters)` )
133146 } catch ( error ) {
134147 console . log ( 'No extended system prompt file found, continuing with base content only' )
135148 }
136149
137- // Fetch all content
138- const [ profile , about , education , experiences , references , blogPosts , projects ] = await Promise . all ( [
139- getProfileInfo ( ) ,
140- getAboutContent ( ) ,
141- getEducation ( ) ,
142- getExperiences ( ) ,
143- getReferences ( ) ,
144- getBlogPosts ( ) ,
145- getProjects ( ) ,
146- ] )
150+ // Fetch all content with individual error handling
151+ console . log ( 'Fetching all content sources...' )
152+ let profile , about , education , experiences , references , blogPosts , projects
153+
154+ try {
155+ [ profile , about , education , experiences , references , blogPosts , projects ] = await Promise . all ( [
156+ getProfileInfo ( ) ,
157+ getAboutContent ( ) ,
158+ getEducation ( ) ,
159+ getExperiences ( ) ,
160+ getReferences ( ) ,
161+ getBlogPosts ( ) ,
162+ getProjects ( ) ,
163+ ] )
164+ console . log ( 'Successfully fetched all content sources' )
165+ } catch ( error ) {
166+ console . error ( `Failed to fetch content sources: ${ error } ` )
167+ throw new Error ( `Failed to load content: ${ error instanceof Error ? error . message : 'Unknown error' } ` )
168+ }
147169
148170 // Format work experience
149171 const workExperience = experiences . map ( ( exp , index ) => `
@@ -256,18 +278,35 @@ ${post.content.replace(/<[^>]*>/g, '').slice(0, 500).trim()}...
256278 }
257279
258280 // Build the main system prompt
259- const mainPrompt = await buildSystemPromptFromTemplate ( templateContent , templateData )
281+ console . log ( 'Building main system prompt from template...' )
282+ let mainPrompt : string
283+ try {
284+ mainPrompt = await buildSystemPromptFromTemplate ( templateContent , templateData )
285+ console . log ( `Successfully built main prompt (${ mainPrompt . length } characters)` )
286+ } catch ( error ) {
287+ console . error ( `Failed to build main prompt from template: ${ error } ` )
288+ throw new Error ( `Template processing failed: ${ error instanceof Error ? error . message : 'Unknown error' } ` )
289+ }
260290
261291 // Build and append extended content if available
262292 let finalPrompt = mainPrompt
263293 if ( additionalContent ) {
264- const extendedPrompt = await buildSystemPromptFromTemplate ( additionalContent , templateData )
265- finalPrompt = `${ mainPrompt } \n\n## EXTENDED CONTEXT\n${ extendedPrompt } `
294+ console . log ( 'Processing extended template content...' )
295+ try {
296+ const extendedPrompt = await buildSystemPromptFromTemplate ( additionalContent , templateData )
297+ finalPrompt = `${ mainPrompt } \n\n## EXTENDED CONTEXT\n${ extendedPrompt } `
298+ console . log ( `Successfully built extended prompt (${ finalPrompt . length } total characters)` )
299+ } catch ( error ) {
300+ console . error ( `Failed to process extended template: ${ error } ` )
301+ console . log ( 'Continuing with main prompt only' )
302+ }
266303 }
267304
305+ console . log ( 'System prompt build completed successfully' )
268306 return finalPrompt
269307 } catch ( error ) {
270308 console . error ( 'Error building system prompt:' , error )
271- throw new Error ( 'Failed to build system prompt' )
309+ const errorMessage = error instanceof Error ? error . message : 'Unknown error occurred'
310+ throw new Error ( `Failed to build system prompt: ${ errorMessage } ` )
272311 }
273312} )
0 commit comments