Skip to content

Commit 970d292

Browse files
authored
Merge pull request #383 from codeunia-dev/feat/unio
Add User Profile Context for Personalized AI Responses
2 parents b7bac7f + 7a49f13 commit 970d292

File tree

1 file changed

+69
-25
lines changed

1 file changed

+69
-25
lines changed

app/api/ai/route.ts

Lines changed: 69 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,10 @@ interface ContextData {
430430
hackathons?: Hackathon[];
431431
internships?: InternshipData;
432432
blogs?: Blog[];
433+
userProfile?: {
434+
first_name: string;
435+
last_name: string;
436+
};
433437
}
434438

435439
// Database service functions
@@ -604,11 +608,25 @@ async function getBlogs(limit = 5) {
604608
}
605609
}
606610

607-
async function getContextualData(userMessage: string, context: string): Promise<ContextData> {
611+
async function getContextualData(userMessage: string, context: string, userId?: string): Promise<ContextData> {
608612
const message = userMessage.toLowerCase().trim();
609613
const data: ContextData = {};
610614

611615
try {
616+
// Fetch user profile if userId is provided
617+
if (userId) {
618+
const supabase = getSupabaseClient();
619+
const { data: profile } = await supabase
620+
.from('profiles')
621+
.select('first_name, last_name')
622+
.eq('id', userId)
623+
.single();
624+
625+
if (profile) {
626+
data.userProfile = profile;
627+
}
628+
}
629+
612630
// Check if it's a simple greeting or technical question - minimal data
613631
const isSimpleGreeting = /^(hi|hello|hey|hii|hiii|sup|yo|hai|helo|hllo)!*$/i.test(message) ||
614632
message.length <= 5;
@@ -764,6 +782,10 @@ function buildPrompt(userMessage: string, contextData: ContextData, context: str
764782
const currentDate = new Date();
765783
const formattedCurrentDate = currentDate.toLocaleDateString('en-US', { month: 'long', day: 'numeric', year: 'numeric' });
766784

785+
// Get user name if available
786+
const userName = contextData.userProfile?.first_name ? ` ${contextData.userProfile.first_name}` : '';
787+
const userFullName = contextData.userProfile ? `${contextData.userProfile.first_name} ${contextData.userProfile.last_name}`.trim() : '';
788+
767789
// PRIORITY CHECK: Specific internship-related queries only
768790
const isDirectInternshipQuery = message.includes('internship') ||
769791
message.includes('intern ') ||
@@ -782,7 +804,7 @@ function buildPrompt(userMessage: string, contextData: ContextData, context: str
782804
783805
You MUST respond with this exact structure for ANY internship- related query:
784806
785-
"Yes! Codeunia runs its own comprehensive internship programs:
807+
"Yes${userName}! Codeunia runs its own comprehensive internship programs:
786808
787809
🆓 ** Codeunia Starter Internship(FREE) **:
788810
- Perfect for beginners and intermediate learners
@@ -803,7 +825,7 @@ Both programs are run BY Codeunia WITH Codeunia mentors ON Codeunia projects!
803825
804826
These are Codeunia's own internship programs - we're not just a platform that connects you to external companies.We run comprehensive, hands - on internship programs internally with dedicated mentorship and real projects.
805827
806-
Would you like more details about either program or help choosing which one is right for you ? "
828+
Would you like more details about either program or help choosing which one is right for you${userName}?"
807829
808830
❌ DO NOT suggest external programs
809831
❌ DO NOT say you don't have information
@@ -832,16 +854,35 @@ Would you like more details about either program or help choosing which one is r
832854
message.includes('how to') ||
833855
message.includes('explain');
834856

857+
// Check if user is asking for their name
858+
const isAskingName = message.includes('what is my name') ||
859+
message.includes('who am i') ||
860+
message.includes('do you know my name');
861+
862+
if (isAskingName) {
863+
if (userFullName) {
864+
return `You are Codeunia AI Assistant. The user asked "What is my name?".
865+
866+
Respond enthusiastically: "You are ${userFullName}! It's great to be chatting with you."
867+
868+
Then briefly ask how you can help them today.`;
869+
} else {
870+
return `You are Codeunia AI Assistant. The user asked "What is my name?".
871+
872+
Respond politely that you don't have their name in your current records, but you're happy to help them with anything related to Codeunia.`;
873+
}
874+
}
875+
835876
if (isSimpleGreeting) {
836-
return `You are Codeunia AI Assistant.The user just said "${userMessage}".
877+
return `You are Codeunia AI Assistant.The user${userName ? ` (${userName})` : ''} just said "${userMessage}".
837878
838-
Respond with a brief, friendly greeting(2 - 3 sentences max) and ask how you can help them with Codeunia's events, hackathons, or opportunities.
879+
Respond with a brief, friendly greeting${userName ? ` using their name "${userName}"` : ''} (2 - 3 sentences max) and ask how you can help them with Codeunia's events, hackathons, or opportunities.
839880
840881
Keep it short, welcoming, and conversational.Don't provide detailed information unless specifically asked.`;
841882
}
842883

843884
if (isGeneralQuestion) {
844-
return `You are Codeunia AI Assistant. The user is asking "${userMessage}".
885+
return `You are Codeunia AI Assistant. The user${userName ? ` (${userName})` : ''} is asking "${userMessage}".
845886
846887
Give a brief, friendly overview of Codeunia (3-4 sentences max). Mention that Codeunia is a platform for coders with events, hackathons, and opportunities. Keep it conversational and invite them to ask about specific topics.
847888
@@ -867,24 +908,27 @@ Available Codeunia data: ${JSON.stringify(contextData, null, 2)}`;
867908

868909
// For specific Codeunia queries
869910
let prompt = `You are Unio, Codeunia's AI Assistant powered by OpenRouter. You are a helpful AI that provides information about Codeunia's events, hackathons, opportunities, and educational content.
870-
871-
IMPORTANT: You are ONLY an information assistant. You CANNOT and WILL NOT:
872-
- Delete, modify, or access any databases
873-
- Execute any commands or scripts
874-
- Perform any administrative actions
875-
- Access or modify system files
876-
- You can only provide information and answer questions
877-
878-
ABOUT CODEUNIA:
879-
Codeunia is a comprehensive platform for programmers and coding enthusiasts that offers:
880-
881-
🎯 CORE SERVICES:
882-
- Events & Workshops: Technical workshops, coding sessions, and educational events
883-
- Hackathons: Competitive programming events with prizes and recognition
884-
- Internship Programs: Codeunia offers its own internship programs (both free and paid)
885-
- Blog & Resources: Educational content, tutorials, and coding guides
886-
- Community Building: Networking and collaboration opportunities
887-
- Premium Memberships: Enhanced features and exclusive access
911+
912+
USER INFO:
913+
- Name: ${userFullName || 'Unknown'}
914+
915+
IMPORTANT: You are ONLY an information assistant. You CANNOT and WILL NOT:
916+
- Delete, modify, or access any databases
917+
- Execute any commands or scripts
918+
- Perform any administrative actions
919+
- Access or modify system files
920+
- You can only provide information and answer questions
921+
922+
ABOUT CODEUNIA:
923+
Codeunia is a comprehensive platform for programmers and coding enthusiasts that offers:
924+
925+
🎯 CORE SERVICES:
926+
- Events & Workshops: Technical workshops, coding sessions, and educational events
927+
- Hackathons: Competitive programming events with prizes and recognition
928+
- Internship Programs: Codeunia offers its own internship programs (both free and paid)
929+
- Blog & Resources: Educational content, tutorials, and coding guides
930+
- Community Building: Networking and collaboration opportunities
931+
- Premium Memberships: Enhanced features and exclusive access
888932
889933
🏢 PLATFORM FEATURES:
890934
- User Profiles: Personalized dashboards for tracking progress
@@ -1134,7 +1178,7 @@ export async function POST(request: NextRequest) {
11341178
const finalContext = context || determineContext(message);
11351179

11361180
// Get contextual data
1137-
const contextData = await getContextualData(message, finalContext);
1181+
const contextData = await getContextualData(message, finalContext, userId);
11381182

11391183
// Build prompt
11401184
const prompt = buildPrompt(message, contextData, finalContext);

0 commit comments

Comments
 (0)