DocDoeAI / src /lib /api /intelligence.ts
asnannp's picture
deploy: sync backend to Space root (learn-lesson HF cache fix)
65d68f5
Raw History Blame Contribute Delete
6.76 kB
import { apiGet, apiPost, buildQuery } from "@/lib/api/client";
import { shouldUseDemoData, withDemoFallback } from "@/lib/api/mode";
import { delay } from "@/lib/frontend-demo-data";
import type {
DiagnosticQuiz,
DiagnosticResultRequest,
DiagnosticResultResponse,
WhatToStudyResponse,
StudyPathResponse,
ExamIntelligenceResponse,
VideoPlanV2Response,
StudyPathPlanType,
VideoPlanDurationType,
VideoPlanStyle,
StudentLevel,
} from "@/lib/api/types";
export async function getDiagnosticQuiz(documentId: string): Promise<DiagnosticQuiz> {
if (shouldUseDemoData(documentId)) {
return delay(mockDiagnosticQuiz(documentId));
}
return withDemoFallback(
() => apiGet<DiagnosticQuiz>(`/intelligence/diagnostic-quiz/${documentId}`),
() => delay(mockDiagnosticQuiz(documentId)),
);
}
export async function submitDiagnosticResults(payload: DiagnosticResultRequest): Promise<DiagnosticResultResponse> {
if (shouldUseDemoData(payload.document_id)) {
return delay({
student_level: "intermediate",
weak_areas: ["diagram", "answer_writing"],
analysis: "You understand the concepts well but need practice in structuring long answers and drawing neat diagrams.",
});
}
return withDemoFallback(
() => apiPost<DiagnosticResultResponse, DiagnosticResultRequest>("/intelligence/diagnostic-results", payload),
() => delay({
student_level: "intermediate",
weak_areas: ["diagram", "answer_writing"],
analysis: "You understand the concepts well but need practice in structuring long answers and drawing neat diagrams.",
}),
);
}
export async function getWhatToStudy(
documentId: string,
studentLevel: StudentLevel = "intermediate",
goal: string = "good marks",
): Promise<WhatToStudyResponse> {
if (shouldUseDemoData(documentId)) {
return delay(mockWhatToStudy());
}
return withDemoFallback(
() => apiPost<WhatToStudyResponse, Record<string, never>>(
`/intelligence/what-to-study${buildQuery({
document_id: documentId,
student_level: studentLevel,
goal,
})}`,
{},
),
() => delay(mockWhatToStudy()),
);
}
export async function getStudyPath(
documentId: string,
planType: StudyPathPlanType,
studentLevel: StudentLevel = "intermediate",
goal: string = "good marks",
): Promise<StudyPathResponse> {
if (shouldUseDemoData(documentId)) {
return delay(mockStudyPath(planType));
}
return withDemoFallback(
() => apiPost<StudyPathResponse, Record<string, never>>(
`/intelligence/study-path${buildQuery({
document_id: documentId,
plan_type: planType,
student_level: studentLevel,
goal,
})}`,
{},
),
() => delay(mockStudyPath(planType)),
);
}
export async function getExamIntelligence(documentId: string): Promise<ExamIntelligenceResponse> {
if (shouldUseDemoData(documentId)) {
return delay(mockExamIntelligence());
}
return withDemoFallback(
() => apiGet<ExamIntelligenceResponse>(`/intelligence/exam-intelligence/${documentId}`),
() => delay(mockExamIntelligence()),
);
}
export async function getVideoPlanExtended(
documentId: string,
durationType: VideoPlanDurationType,
style: VideoPlanStyle,
): Promise<VideoPlanV2Response> {
if (shouldUseDemoData(documentId)) {
return delay(mockVideoPlan(durationType, style));
}
return withDemoFallback(
() => apiPost<VideoPlanV2Response, Record<string, never>>(
`/intelligence/video-plan-extended${buildQuery({
document_id: documentId,
duration_type: durationType,
style,
})}`,
{},
),
() => delay(mockVideoPlan(durationType, style)),
);
}
// Mock Helpers for Demo Mode
function mockDiagnosticQuiz(docId: string): DiagnosticQuiz {
return {
id: "diag-local",
document_id: docId,
questions: [
{
id: "q1",
category: "concept",
question: "What is the primary principle behind this chapter?",
options: ["Option A", "Option B", "Option C", "Option D"],
correct_answer_index: 0,
},
{
id: "q2",
category: "memory",
question: "Define the core term used in section 2.",
options: ["Def A", "Def B", "Def C", "Def D"],
correct_answer_index: 1,
},
],
};
}
function mockWhatToStudy(): WhatToStudyResponse {
return {
must_study_topics: [{ topic: "Core Concept", reason: "Foundational for 60% of marks" }],
high_weightage_topics: [{ topic: "Section 3.1", reason: "Strong topic in the selected material" }],
repeated_pyq_topics: [{ topic: "Definition X", reason: "Use uploaded verified papers before claiming repetition" }],
low_priority_topics: [{ topic: "Historical Context", reason: "Review after core scoring concepts" }],
skip_for_now_topics: [{ topic: "Advanced Proofs", reason: "Beyond standard syllabus" }],
revision_keywords: ["Keyword 1", "Keyword 2"],
easy_marks: [{ topic: "Diagram Y", reason: "Simple to draw, carries 3 marks" }],
danger_areas: [{ topic: "Sign Convention", reason: "Students lose marks here frequently" }],
study_order: ["Core Concept", "Section 3.1", "Definition X"],
};
}
function mockStudyPath(planType: StudyPathPlanType): StudyPathResponse {
return {
plan_type: planType,
total_estimated_coverage: "85%",
blocks: [
{
time_block: "00:00 - 00:20",
topic: "Fast Recap",
reason: "Based on 1h emergency mode",
task: "Read summary keywords",
output_expected: "Mental map of chapter",
revision_checkpoint: "Recall top 3 laws",
},
],
};
}
function mockExamIntelligence(): ExamIntelligenceResponse {
return {
chapter: "Sample Chapter",
topic_importance: "High",
pyq_pattern: "Focuses on numericals in part B",
likely_question_types: ["MCQ", "Long Answer"],
answers: [
{
question: "Explain Z?",
marks: 4,
answer_structure: "Intro -> Law -> Diagram -> Conclusion",
key_points: ["Point 1", "Point 2"],
diagram_suggested: true,
},
],
common_mistakes: ["Units error", "Formula mismatch"],
keywords_to_underline: ["Specific Term 1", "Specific Term 2"],
};
}
function mockVideoPlan(duration: VideoPlanDurationType, style: VideoPlanStyle): VideoPlanV2Response {
return {
title: "Intelligence Masterclass",
duration_type: duration,
style: style,
hook: "Imagine you could control energy...",
analogy: "Like water flowing through a pipe",
recap: "We covered X, Y and Z today.",
sections: [
{ scene_number: 1, label: "Intro", copy: "Hi, I am DocDoe...", visual_suggestion: "Show mascot waving" },
],
mini_quiz: [],
};
}