Skip to content

Conversation

@ChenMel27
Copy link
Collaborator

Summary [Planning to remove fallback to legacy before merging]

This PR updates the frontend and middleware to support the AI server’s new LangChain-based mixed MCQ/True/False generation.
It introduces improved error handling, fallback logic, and normalization for True/False responses while maintaining compatibility with legacy endpoints.


🚀 Motivation

With the AI server now generating richer question formats via LangChain, the frontend needed to:

  • Switch from the deprecated /generate-questions-with-answers endpoint
  • Handle both MCQ and True/False formats
  • Gracefully fallback to the legacy service if LangChain is unavailable

🔄 Backend Proxy Updates (routes/aiRoutes.ts)

Area Change Purpose
Primary endpoint Now calls http://localhost:8000/questions/{graph_id}?use_langchain=true Integrates with LangChain question generator
Fallback Added legacy retry on port 5000/generate Ensures reliability if the new AI server fails
Response mapping Converts { questions: [...] }{ status, qa_pairs } Matches existing frontend format
Normalization Extracts text and correct_answer into simple { question, answer } pairs Keeps UI consistent
Error handling Returns clear 502/500 messages with detailed logs Improves debugging visibility

💬 Frontend Updates (src/pages/Chat.tsx)

  • Normalized TF answers: Converts 'T'/'F' to 'True'/'False' for consistency.
  • Unified data handling: Uses normalizedPairs for rendering and quiz progression.
  • Removed redundant helper: Eliminated getAnswerForQuestion() since QA data is already normalized.
  • Improved display logic: Uses normalizedPairs for accurate total count and first question prompt.

Result

  • Fully compatible with LangChain’s { questions: [...] } format.
  • Robust fallback to legacy AI endpoints.
  • Clean, user-friendly quiz flow for both MCQ and True/False questions.

- Proxy question generation to new LangChain endpoint with a legacy fallback
  ().
- Normalize True/False answers in the frontend and use normalized pairs
  ().
- Remove unused variables / minor cleanup in chat UI.
- Keeps frontend API shape unchanged: returns { status: 'success', qa_pairs: [...] }.
@ChenMel27 ChenMel27 requested a review from Copilot October 16, 2025 13:01
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR updates the application to integrate with a new LangChain-based AI question generation service while maintaining backward compatibility. The changes enable handling of mixed MCQ/True-False question formats and implement graceful fallback to legacy endpoints.

  • Proxy layer now calls LangChain endpoint with fallback to legacy service
  • Frontend normalizes True/False answers and removes redundant code
  • Improved error handling and response mapping for different AI server formats

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/pages/Chat.tsx Normalizes T/F answers to True/False, removes unused helper function, and updates question display logic
server/controllers/graphController.ts Implements LangChain endpoint integration with legacy fallback and response format mapping

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

// If the new AI server is inaccessible or returns 403, try legacy endpoint on port 5000 as a graceful fallback
const status = err?.response?.status;
console.error(`Primary questions endpoint failed (status=${status}). Attempting legacy fallback...`);
if (status === 403 || status === 404 || !response) {
Copy link

Copilot AI Oct 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition !response will always be false here since response is declared but never assigned when the try block fails. This condition should be removed as it's unreachable.

Suggested change
if (status === 403 || status === 404 || !response) {
if (status === 403 || status === 404) {

Copilot uses AI. Check for mistakes.
const qaPair = qaData.find(qa => qa.question === question);
return qaPair?.answer || 'No answer available';
};
// ...existing code... (no local helper required)
Copy link

Copilot AI Oct 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This placeholder comment should be removed as it doesn't provide any meaningful information and clutters the code.

Suggested change
// ...existing code... (no local helper required)

Copilot uses AI. Check for mistakes.
Comment on lines +286 to +289
const normalizedPairs = qaResponseData.qa_pairs.map((p: QAPair) => ({
question: p.question,
answer: (p.answer === 'T' || p.answer === 'True') ? 'True' : (p.answer === 'F' || p.answer === 'False') ? 'False' : p.answer
}));
Copy link

Copilot AI Oct 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The normalization logic is duplicated across lines 286-289 and 709-712. Consider extracting this into a reusable function to avoid code duplication.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants