DocDoeAI / src /components /study-chat /study-brain-context.ts
asnannp's picture
deploy: sync backend to Space root (learn-lesson HF cache fix)
65d68f5
Raw History Blame Contribute Delete
3.21 kB
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 };
}