File size: 3,207 Bytes
65d68f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import type { ApiDocument, ApiUser, BrainAskMode, StudentLevel } from "@/lib/api/types";
import { getStudyProfileId } from "@/lib/onboarding";
import type { StudyIntent } from "@/lib/study-intents";

export type StudyBrainContext = {
  studyProfileId: string | null;
  subject: string | null;
  level: StudentLevel | null;
  languagePreference: string | null;
};

export function buildStudyBrainContext(
  user: ApiUser | null | undefined,
  selectedDocuments: ApiDocument[],
): StudyBrainContext {
  const primaryDoc = selectedDocuments[0];
  return {
    studyProfileId: getStudyProfileId(),
    subject: primaryDoc?.subject ?? null,
    level: inferLevelFromUser(user),
    languagePreference: user?.preferred_language ?? "English",
  };
}

function inferLevelFromUser(user: ApiUser | null | undefined): StudentLevel | null {
  const grade = user?.class_level?.toLowerCase() ?? "";
  if (/(10|sslc|beginner|basics)/.test(grade)) return "beginner";
  if (/(12|\+2|advanced|jee|neet)/.test(grade)) return "advanced";
  if (grade) return "intermediate";
  return null;
}

export function askModeForIntent(intent: StudyIntent, prompt: string): BrainAskMode {
  const lower = prompt.toLowerCase();
  if (intent === "summarize_source" || /\b(summar|brief|key points|bullet)\b/.test(lower)) {
    return "summarize";
  }
  if (
    intent === "generate_exam_answer" ||
    /\b(\d\s*[- ]?mark|exam answer|board style|marks? answer|write answer)\b/.test(lower)
  ) {
    return "exam_answer";
  }
  if (/\b(diagram|draw|label|figure|sketch)\b/.test(lower)) {
    return "diagram_help";
  }
  if (/\b(pyq|previous year|past paper|repeated question)\b/.test(lower)) {
    return "pyq_pattern";
  }
  return "explain_simple";
}

export function shouldStreamExplanation(intent: StudyIntent, prompt = ""): boolean {
  // Stream only plain teach/explain prompts so students see progress quickly.
  // Structured tools still use /ask or their dedicated generation endpoints.
  return (intent === "teach_topic" || intent === "ask_doubt") && askModeForIntent(intent, prompt) === "explain_simple";
}

export function streamIntentForMode(
  responseMode: "general_answer" | "source_grounded" | "tool_ready" | string,
): string | undefined {
  if (responseMode === "source_grounded") return "source_answer";
  if (responseMode === "general_answer") return "general_topic";
  return undefined;
}

export function mapSuggestedActionToIntent(action: string): { intent: StudyIntent; prompt: string } | null {
  const lower = action.toLowerCase();
  if (lower.includes("quiz")) return { intent: "generate_quiz", prompt: action };
  if (lower.includes("note")) return { intent: "generate_notes", prompt: action };
  if (lower.includes("flashcard")) return { intent: "generate_flashcards", prompt: action };
  if (lower.includes("video")) return { intent: "create_video_plan", prompt: action };
  if (lower.includes("diagram")) return { intent: "teach_topic", prompt: action };
  if (lower.includes("exam") || lower.includes("answer")) {
    return { intent: "generate_exam_answer", prompt: action };
  }
  if (lower.includes("upload") || lower.includes("source")) return null;
  return { intent: "teach_topic", prompt: action };
}