11import OpenAI from 'openai' ;
2- import fs from 'fs' ;
3- import path from 'path' ;
42import staticRoleMap from './staticRoleEnrichments' ;
53
6- // Ensure the cache directory exists
7- const cacheDir = path . resolve ( process . cwd ( ) , 'app/api/interview-generator/cache' ) ; // Corrected path
8- if ( ! fs . existsSync ( cacheDir ) ) {
9- fs . mkdirSync ( cacheDir , { recursive : true } ) ;
10- }
11-
12- const cachePath = path . resolve ( cacheDir , 'roleEnrichmentCache.json' ) ;
13- let cache : Record < string , string > = { } ;
14-
15- try {
16- if ( fs . existsSync ( cachePath ) ) {
17- cache = JSON . parse ( fs . readFileSync ( cachePath , 'utf8' ) ) ;
18- }
19- } catch ( error ) {
20- console . error ( "Error reading role enrichment cache:" , error ) ;
21- cache = { } ;
22- }
23-
244// Helper function to check if a role title is too vague to be useful without enrichment
255const isVague = ( title : string ) => {
266 const vagueTerms = [ 'developer' , 'engineer' , 'tech lead' , 'programmer' , 'hacker' , 'architect' ] ;
@@ -29,41 +9,56 @@ const isVague = (title: string) => {
299} ;
3010
3111export async function getEnrichedRoleContext ( role : string ) : Promise < string > {
32- const normalized = role . toLowerCase ( ) . trim ( ) ;
12+ try {
13+ // Handle undefined or empty roles
14+ if ( ! role || typeof role !== 'string' ) {
15+ console . warn ( 'Invalid role provided to getEnrichedRoleContext:' , role ) ;
16+ return '' ;
17+ }
3318
34- if ( staticRoleMap [ normalized ] ) return staticRoleMap [ normalized ] ;
35- if ( cache [ normalized ] ) return cache [ normalized ] ;
36- if ( ! isVague ( normalized ) ) return '' ; // Role is detailed enough — skip enrichment
19+ const normalized = role . toLowerCase ( ) . trim ( ) ;
3720
38- const openai = new OpenAI ( { apiKey : process . env . OPENAI_API_KEY } ) ;
21+ // Check static map first (fastest)
22+ if ( staticRoleMap [ normalized ] ) {
23+ return staticRoleMap [ normalized ] ;
24+ }
3925
40- const prompt = `
26+ // If role is detailed enough, skip enrichment
27+ if ( ! isVague ( normalized ) ) {
28+ return '' ;
29+ }
30+
31+ // Initialize OpenAI client
32+ const openai = new OpenAI ( { apiKey : process . env . OPENAI_API_KEY } ) ;
33+
34+ const prompt = `
4135You are an expert technical hiring manager.
4236
4337Describe 3–5 high-context responsibilities or challenges specific to the role of "${ role } " in a modern tech company.
4438Focus on what this role is accountable for, common tradeoffs they face, and types of systems or decisions they lead.
4539Format your response as a clean Markdown list.
4640` ;
4741
48- try {
49- const response = await openai . chat . completions . create ( {
50- model : 'gpt-4o-mini' ,
51- messages : [ { role : 'user' , content : prompt } ] ,
52- temperature : 0.5
53- } ) ;
42+ // Make API call with timeout handling
43+ const response = await Promise . race ( [
44+ openai . chat . completions . create ( {
45+ model : 'gpt-4o-mini' ,
46+ messages : [ { role : 'user' , content : prompt } ] ,
47+ temperature : 0.5
48+ } ) ,
49+ new Promise < never > ( ( _ , reject ) =>
50+ setTimeout ( ( ) => reject ( new Error ( 'OpenAI enrichment timed out' ) ) , 5000 )
51+ )
52+ ] ) as OpenAI . Chat . Completions . ChatCompletion ;
5453
5554 const content = response . choices [ 0 ] ?. message . content ?. trim ( ) || '' ;
56- if ( content ) {
57- cache [ normalized ] = content ;
58- try {
59- fs . writeFileSync ( cachePath , JSON . stringify ( cache , null , 2 ) ) ;
60- } catch ( writeError ) {
61- console . error ( "Error writing role enrichment cache:" , writeError ) ;
62- }
63- }
6455 return content ;
65- } catch ( apiError ) {
66- console . error ( "Error fetching enriched role context from OpenAI:" , apiError ) ;
56+ } catch ( error ) {
57+ // Handle errors gracefully without failing the whole request
58+ const errorMsg = error instanceof Error ? error . message : String ( error ) ;
59+ console . error ( "Error in getEnrichedRoleContext:" , errorMsg ) ;
60+
61+ // Return empty string on error to allow the process to continue
6762 return '' ;
6863 }
6964}
0 commit comments