1+ const formatHistory = ( history : { from : string ; text : string } [ ] = [ ] ) =>
2+ history
3+ . map ( ( m ) => `${ m . from === "user" ? "User" : "Assistant" } : ${ m . text } ` )
4+ . join ( "\n" ) ;
5+
6+ export function buildPrompt ( {
7+ path,
8+ ragData,
9+ userAnswer,
10+ questionContext,
11+ history = [ ] ,
12+ } : {
13+ path : string ;
14+ ragData : any ;
15+ userAnswer : string ;
16+ questionContext ?: any ;
17+ history ?: { from : string ; text : string } [ ] ;
18+ } ) {
19+ const baseRole = `
20+ You are Cortex, an intelligent AI analyst for "Call of Code".
21+ CRITICAL: Do NOT start a mock interview. Do NOT introduce yourself as Alex.
22+ Your ONLY job is to answer the user's question using the DATASET provided below.
23+ If the DATASET is empty, tell the user you don't see any interviews yet.
24+ Rules:
25+ - Always start with a helpful hint.
26+ - Do NOT give full solution unless user explicitly asks.
27+ - Be concise, friendly, and use markdown formatting.
28+ - Do NOT act as an interviewer.
29+ - Do NOT introduce yourself as "Alex" or start a mock session.
30+ - Be concise and use markdown.
31+ ` ;
32+
33+ console . log ( "🧠 QUESTION CONTEXT IN chatBrain 👉" , questionContext ) ;
34+
35+ // --- CASE 1: DSA TOPIC ONLY ---
36+ if ( questionContext ?. type === "DSA" && questionContext . isTopicOnly ) {
37+ return `
38+ ${ baseRole }
39+ User is currently browsing the topic: ${ questionContext . topicTitle } .
40+
41+ Conversation so far:
42+ ${ formatHistory ( history ) }
43+
44+ Instructions:
45+ - Provide a brief overview of ${ questionContext . topicTitle } .
46+ - Mention common interview patterns.
47+
48+ User Query: ${ userAnswer }
49+ ` ;
50+ }
51+
52+ // --- CASE 2: SPECIFIC DSA QUESTION ---
53+ if ( questionContext ?. type === "DSA" && questionContext . questionId ) {
54+ const context = ragData ? `QUESTION: ${ ragData . question } \nCONCEPT: ${ ragData . concept } ` : "No specific details available." ;
55+
56+ return `
57+ ${ baseRole }
58+ Conversation so far:
59+ ${ formatHistory ( history ) }
60+
61+ CURRENT QUESTION: ${ questionContext . questionName } (${ questionContext . topicTitle } )
62+ ${ context }
63+
64+ USER ANSWER / CODE:
65+ ${ userAnswer }
66+
67+ Instructions:
68+ - Give ONLY a hint first.
69+ - Point out logical mistakes.
70+ ` ;
71+ }
72+
73+ // --- CASE 3: INTERVIEW ANALYST (COLLECTION) ---
74+ if ( questionContext ?. type === "INTERVIEW_COLLECTION" ) {
75+ // 🛡️ SAFETY CHECK: Ensure ragData is an array before mapping
76+ const allExperiences = Array . isArray ( ragData )
77+ ? ragData . map ( ( exp : any ) => `
78+ Company: ${ exp . company }
79+ Verdict: ${ exp . verdict }
80+ Experience: ${ exp . content }
81+ ` ) . join ( "\n---\n" )
82+ : "No interview data available to analyze." ;
83+
84+ return `
85+ ${ baseRole }
86+ You are an Interview Analyst. You have access to ${ questionContext . count || 0 } interview stories.
87+
88+ DATASET:
89+ ${ allExperiences }
90+
91+ User Question: ${ userAnswer }
92+
93+ Instructions:
94+ - Analyze the common patterns in the stories provided.
95+ - Identify common mistakes leading to "Rejected" verdicts.
96+ - Determine which companies focused more on DSA.
97+ ` ;
98+ }
99+
100+ // --- CASE 4: SINGLE INTERVIEW EXPERIENCE ---
101+ // chatBrain.ts update for CASE 4
102+ // --- CASE 4: SINGLE INTERVIEW EXPERIENCE ---
103+ if ( questionContext ?. type === "INTERVIEW_EXPERIENCE" ) {
104+ const interview = ragData || questionContext ;
105+ // Added a check to see if content is actually there
106+ const content = ragData ?. content || "No database content found. Request might have failed." ;
107+
108+ // LOGGING: Check your backend terminal to see what is being packed into the prompt
109+ console . log ( "🛠️ PROMPT BUILDING WITH CONTENT:" , content . substring ( 0 , 50 ) + "..." ) ;
110+
111+ return `
112+ ${ baseRole }
113+ CURRENT CONTEXT:
114+ Company: ${ interview . company }
115+ Role: ${ interview . role }
116+ Verdict: ${ interview . verdict }
117+ Full Experience: ${ content }
118+
119+ User Query: ${ userAnswer }
120+ ` ;
121+ }
122+
123+ // --- CASE 5: DEFAULT FALLBACK ---
124+ return `
125+ ${ baseRole }
126+ Conversation so far:
127+ ${ formatHistory ( history ) }
128+ User message: ${ userAnswer }
129+ ` ;
130+ }
0 commit comments