diff --git a/app/api/ai/workspace/route.ts b/app/api/ai/workspace/route.ts index 158a2a0..530fb8a 100644 --- a/app/api/ai/workspace/route.ts +++ b/app/api/ai/workspace/route.ts @@ -5,10 +5,10 @@ import { generateWorkspace } from "@/lib/ai/workspace-service"; import { WorkspaceAction } from "@/types/ai"; import { getPrimaryKey } from "@/lib/ai/key-manager"; import { trackMetric } from "@/lib/ai/metrics"; +import { getTopicById } from "@/lib/content"; interface WorkspaceBody { - topic?: string; - module?: string; + topicId?: string; subjectCode?: string; action?: WorkspaceAction; forceRefresh?: boolean; @@ -163,9 +163,21 @@ export async function POST(request: NextRequest) { try { const body = (await request.json()) as WorkspaceBody; - const topic = body.topic?.trim(); - const moduleTitle = body.module?.trim(); + const topicId = body.topicId?.trim(); const subjectCode = body.subjectCode?.trim().toUpperCase(); + const topicData = + topicId && subjectCode + ? await getTopicById( + "common", + "semester-1", + subjectCode.toLowerCase(), + topicId + ) + : null; + + const topic = topicData?.topic.title; + + const moduleTitle = topicData?.module.title; const forceRefresh = body.forceRefresh ?? false; const question = body.question?.trim(); const messages = body.messages || []; diff --git a/app/rgpv/[branch]/[semester]/[subject]/ai/page.tsx b/app/rgpv/[branch]/[semester]/[subject]/ai/page.tsx index 0187a25..e5fc97e 100644 --- a/app/rgpv/[branch]/[semester]/[subject]/ai/page.tsx +++ b/app/rgpv/[branch]/[semester]/[subject]/ai/page.tsx @@ -1,5 +1,6 @@ import { BrainCircuit } from "lucide-react"; import WorkspaceChat from "@/components/ai/workspace-chat"; +import { getTopicById } from "@/lib/content"; interface AIPageProps { params: Promise<{ @@ -9,17 +10,24 @@ interface AIPageProps { }>; searchParams: Promise<{ - topic?: string; - module?: string; + topicId?: string; }>; } export default async function AIPage({ params, searchParams }: AIPageProps) { const { branch, semester, subject } = await params; - const { topic, module } = await searchParams; + const { topicId } = await searchParams; - const isTopicMode = Boolean(topic) && Boolean(module); + const topicData = topicId + ? await getTopicById(branch, semester, subject, topicId) + : null; + + const topicTitle = topicData?.topic.title ?? ""; + + const moduleTitle = topicData?.module.title ?? ""; + + const isTopicMode = topicData !== null; // Suggested prompts based on topic const getSuggestedPrompts = (mainTopic: string) => { @@ -41,51 +49,57 @@ export default async function AIPage({ params, searchParams }: AIPageProps) { ]; }; - const suggestedPrompts = - isTopicMode && topic - ? getSuggestedPrompts(topic) - : [ - "Explain the concept", - "Generate notes", - "Create revision sheet", - "Generate exam questions", - "Explain with examples", - ]; + const suggestedPrompts = isTopicMode + ? getSuggestedPrompts(topicTitle) + : [ + "Explain the concept", + "Generate notes", + "Create revision sheet", + "Generate exam questions", + "Explain with examples", + ]; // Initial prompts for WorkspaceChat - const initialPrompts = - isTopicMode && topic && module - ? [ - { - prompt: `Explain ${topic} in detail`, - action: "explain", - topic: topic, - module: module, - }, - { - prompt: `Generate 5 mark answer on ${topic}`, - action: "generate", - topic: topic, - module: module, - }, - { - prompt: `Create revision sheet for ${topic}`, - action: "summarize", - topic: topic, - module: module, - }, - ] - : [ - { prompt: "Explain the concept", action: "explain" }, - { prompt: "Generate notes", action: "generate" }, - { prompt: "Create revision sheet", action: "summarize" }, - ]; + const initialPrompts = isTopicMode + ? [ + { + prompt: `Explain ${topicTitle} in detail`, + action: "explain", + topic: topicTitle, + module: moduleTitle, + }, + { + prompt: `Generate 5 mark answer on ${topicTitle}`, + action: "generate", + topic: topicTitle, + module: moduleTitle, + }, + { + prompt: `Create revision sheet for ${topicTitle}`, + action: "summarize", + topic: topicTitle, + module: moduleTitle, + }, + ] + : [ + { + prompt: "Explain the concept", + action: "explain", + }, + { + prompt: "Generate notes", + action: "generate", + }, + { + prompt: "Create revision sheet", + action: "summarize", + }, + ]; // Build welcome message with topic context - const welcomeMessage = - isTopicMode && topic - ? `Ask me anything about **${topic}**. I can help with explanations, examples, exam preparation, and suggest related topics based on your learning needs.` - : "Ask me anything about your subject. I can help with explanations, examples, and exam preparation."; + const welcomeMessage = isTopicMode + ? `Ask me anything about **${topicTitle}**. I can help with explanations, examples, exam preparation, and suggest related topics based on your learning needs.` + : "Ask me anything about your subject. I can help with explanations, examples, and exam preparation."; return (
@@ -106,11 +120,11 @@ export default async function AIPage({ params, searchParams }: AIPageProps) { {isTopicMode ? ( <>

- {topic} + {topicTitle}

- {module} + {moduleTitle}

@@ -150,14 +164,13 @@ export default async function AIPage({ params, searchParams }: AIPageProps) {

{/* Main Chat Area */} -
+
diff --git a/app/rgpv/[branch]/[semester]/[subject]/syllabus/page.tsx b/app/rgpv/[branch]/[semester]/[subject]/syllabus/page.tsx index d1367f5..9c86ac0 100644 --- a/app/rgpv/[branch]/[semester]/[subject]/syllabus/page.tsx +++ b/app/rgpv/[branch]/[semester]/[subject]/syllabus/page.tsx @@ -3,13 +3,14 @@ import { BookOpen } from "lucide-react"; import { getSyllabus, getPYQs } from "@/lib/content"; import { getQuestionsForModule } from "@/lib/content/question-mapper"; import ModuleCard from "@/components/syllabus/module-card"; +import type { Topic } from "@/types/topic"; interface Module { id: string; number: number; title: string; hours: number; - topics?: string[]; + topics?: Topic[]; questionIds?: string[]; predictedQuestionIds?: string[]; } diff --git a/components/ai/workspace-chat.tsx b/components/ai/workspace-chat.tsx index d92bc8f..01e656c 100644 --- a/components/ai/workspace-chat.tsx +++ b/components/ai/workspace-chat.tsx @@ -18,15 +18,13 @@ interface WorkspaceChatProps { subjectCode: string; initialPrompts?: Array<{ prompt: string; - topic?: string; - module?: string; + topicId?: string; action?: string; }>; welcomeMessage?: string; inputPlaceholder?: string; apiEndpoint?: string; - topic?: string; - module?: string; + topicId?: string; } export default function WorkspaceChat({ @@ -35,8 +33,7 @@ export default function WorkspaceChat({ welcomeMessage = "Ask me anything about your subject. I can help with explanations, examples, and exam preparation.", inputPlaceholder = "Type your question here...", apiEndpoint = API_ENDPOINTS.AI_WORKSPACE, - topic, - module, + topicId, }: WorkspaceChatProps) { const [messages, setMessages] = useState([]); const [input, setInput] = useState(""); @@ -95,8 +92,7 @@ export default function WorkspaceChat({ body: JSON.stringify({ question: content.trim(), subjectCode, - topic, - module, + topicId, messages: messages.map((m) => ({ role: m.role, content: m.content, @@ -223,7 +219,7 @@ export default function WorkspaceChat({

{initialPrompts.map((item, index) => { const promptText = - item.prompt || `Ask about ${item.topic || "this topic"}`; + item.prompt || `Ask about ${item.topicId || "this topic"}`; return (