UNPKG

@defikitdotnet/education-module-ai

Version:
256 lines (255 loc) 21.1 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype); return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.AIQuizGenerator = void 0; var jsx_runtime_1 = require("react/jsx-runtime"); var react_1 = require("react"); var CategoryManager_1 = __importDefault(require("./CategoryManager")); var AIQuizGenerator = function (_a) { var apiBaseUrl = _a.apiBaseUrl, teacherId = _a.teacherId, // Changed from agentId to teacherId onComplete = _a.onComplete, onCancel = _a.onCancel; var _b = (0, react_1.useState)(""), subject = _b[0], setSubject = _b[1]; var _c = (0, react_1.useState)(""), topic = _c[0], setTopic = _c[1]; var _d = (0, react_1.useState)("beginner"), level = _d[0], setLevel = _d[1]; var _e = (0, react_1.useState)(5), questionCount = _e[0], setQuestionCount = _e[1]; var _f = (0, react_1.useState)([ "multiple_choice", "true_false", ]), questionTypes = _f[0], setQuestionTypes = _f[1]; var _g = (0, react_1.useState)([]), selectedCategoryIds = _g[0], setSelectedCategoryIds = _g[1]; var _h = (0, react_1.useState)(false), isGenerating = _h[0], setIsGenerating = _h[1]; var _j = (0, react_1.useState)(null), error = _j[0], setError = _j[1]; var _k = (0, react_1.useState)(0), progress = _k[0], setProgress = _k[1]; var _l = (0, react_1.useState)(null), generatedQuiz = _l[0], setGeneratedQuiz = _l[1]; // Subject options var subjects = [ { value: "mathematics", label: "Mathematics" }, { value: "science", label: "Science" }, { value: "history", label: "History" }, { value: "language_arts", label: "Language Arts" }, { value: "computer_science", label: "Computer Science" }, { value: "physics", label: "Physics" }, { value: "chemistry", label: "Chemistry" }, { value: "biology", label: "Biology" }, ]; // Educational level options var levels = [ { value: "beginner", label: "Beginner" }, { value: "elementary", label: "Elementary" }, { value: "middle_school", label: "Middle School" }, { value: "high_school", label: "High School" }, { value: "undergraduate", label: "Undergraduate" }, { value: "graduate", label: "Graduate" }, { value: "professional", label: "Professional" }, ]; // Question type options var availableQuestionTypes = [ { value: "multiple_choice", label: "Multiple Choice" }, { value: "true_false", label: "True/False" }, { value: "short_answer", label: "Short Answer" }, { value: "essay", label: "Essay" }, ]; // Toggle question type selection var toggleQuestionType = function (type) { if (questionTypes.includes(type)) { setQuestionTypes(questionTypes.filter(function (t) { return t !== type; })); } else { setQuestionTypes(__spreadArray(__spreadArray([], questionTypes, true), [type], false)); } }; // Handle categories change var handleCategoriesChange = function (categoryIds) { setSelectedCategoryIds(categoryIds); }; // Generate quiz using AI var handleGenerate = function (e) { return __awaiter(void 0, void 0, void 0, function () { var progressInterval_1, response, errorData, data, err_1; return __generator(this, function (_a) { switch (_a.label) { case 0: e.preventDefault(); if (!subject) { setError("Please select a subject"); return [2 /*return*/]; } if (!topic) { setError("Please enter a topic"); return [2 /*return*/]; } if (questionTypes.length === 0) { setError("Please select at least one question type"); return [2 /*return*/]; } _a.label = 1; case 1: _a.trys.push([1, 6, 7, 8]); setIsGenerating(true); setError(null); setProgress(10); progressInterval_1 = setInterval(function () { setProgress(function (prev) { if (prev >= 90) { clearInterval(progressInterval_1); return 90; } return prev + 10; }); }, 1500); return [4 /*yield*/, fetch("".concat(apiBaseUrl, "/api/educational-contents/generate/quiz"), { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ subject: subject, topic: topic, level: level, questionCount: questionCount, questionTypes: questionTypes, teacherId: teacherId, }), })]; case 2: response = _a.sent(); clearInterval(progressInterval_1); if (!!response.ok) return [3 /*break*/, 4]; return [4 /*yield*/, response.json()]; case 3: errorData = _a.sent(); throw new Error(errorData.error || "Failed to generate quiz"); case 4: setProgress(100); return [4 /*yield*/, response.json()]; case 5: data = _a.sent(); setGeneratedQuiz(data.data); return [3 /*break*/, 8]; case 6: err_1 = _a.sent(); setError(err_1.message || "An error occurred while generating the quiz"); console.error(err_1); return [3 /*break*/, 8]; case 7: setIsGenerating(false); return [7 /*endfinally*/]; case 8: return [2 /*return*/]; } }); }); }; // Save generated quiz var handleSave = function () { return __awaiter(void 0, void 0, void 0, function () { var response, errorData, savedData, err_2; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!generatedQuiz) return [2 /*return*/]; _a.label = 1; case 1: _a.trys.push([1, 6, 7, 8]); setIsGenerating(true); setError(null); return [4 /*yield*/, fetch("".concat(apiBaseUrl, "/api/educational-contents/").concat(teacherId), // Changed from agentId to teacherId { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ title: generatedQuiz.title, content: generatedQuiz.content || generatedQuiz.description || "", type: "quiz", subject: subject, level: level, keywords: generatedQuiz.keywords || [], questions: generatedQuiz.questions || [], categories: selectedCategoryIds, }), })]; case 2: response = _a.sent(); if (!!response.ok) return [3 /*break*/, 4]; return [4 /*yield*/, response.json()]; case 3: errorData = _a.sent(); throw new Error(errorData.error || "Failed to save quiz"); case 4: return [4 /*yield*/, response.json()]; case 5: savedData = _a.sent(); // Call onComplete callback if provided if (onComplete) { onComplete(savedData.data); } return [3 /*break*/, 8]; case 6: err_2 = _a.sent(); setError(err_2.message || "An error occurred while saving the quiz"); console.error(err_2); return [3 /*break*/, 8]; case 7: setIsGenerating(false); return [7 /*endfinally*/]; case 8: return [2 /*return*/]; } }); }); }; // Handle cancel var handleCancel = function () { if (onCancel) { onCancel(); } }; return ((0, jsx_runtime_1.jsx)("div", { className: "ai-quiz-generator", children: !generatedQuiz ? ((0, jsx_runtime_1.jsxs)("form", { onSubmit: handleGenerate, className: "space-y-4", children: [error && ((0, jsx_runtime_1.jsx)("div", { className: "bg-red-50 border border-red-200 text-red-700 px-4 py-3 rounded", children: error })), (0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)("label", { htmlFor: "subject", className: "block text-sm font-medium text-gray-700 mb-1", children: "Subject" }), (0, jsx_runtime_1.jsxs)("select", { id: "subject", value: subject, onChange: function (e) { return setSubject(e.target.value); }, className: "w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500", disabled: isGenerating, children: [(0, jsx_runtime_1.jsx)("option", { value: "", children: "Select a subject" }), subjects.map(function (subj) { return ((0, jsx_runtime_1.jsx)("option", { value: subj.value, children: subj.label }, subj.value)); })] })] }), (0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)("label", { htmlFor: "topic", className: "block text-sm font-medium text-gray-700 mb-1", children: "Topic" }), (0, jsx_runtime_1.jsx)("input", { id: "topic", type: "text", value: topic, onChange: function (e) { return setTopic(e.target.value); }, placeholder: "Enter the quiz topic", className: "w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500", disabled: isGenerating })] }), (0, jsx_runtime_1.jsxs)("div", { className: "grid grid-cols-1 md:grid-cols-2 gap-4", children: [(0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)("label", { htmlFor: "level", className: "block text-sm font-medium text-gray-700 mb-1", children: "Educational Level" }), (0, jsx_runtime_1.jsx)("select", { id: "level", value: level, onChange: function (e) { return setLevel(e.target.value); }, className: "w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500", disabled: isGenerating, children: levels.map(function (lvl) { return ((0, jsx_runtime_1.jsx)("option", { value: lvl.value, children: lvl.label }, lvl.value)); }) })] }), (0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)("label", { htmlFor: "questionCount", className: "block text-sm font-medium text-gray-700 mb-1", children: "Number of Questions" }), (0, jsx_runtime_1.jsx)("input", { id: "questionCount", type: "number", min: "1", max: "20", value: questionCount, onChange: function (e) { return setQuestionCount(parseInt(e.target.value)); }, className: "w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500", disabled: isGenerating })] })] }), (0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)("label", { className: "block text-sm font-medium text-gray-700 mb-1", children: "Question Types" }), (0, jsx_runtime_1.jsx)("div", { className: "grid grid-cols-2 gap-2", children: availableQuestionTypes.map(function (type) { return ((0, jsx_runtime_1.jsxs)("div", { className: "flex items-center", children: [(0, jsx_runtime_1.jsx)("input", { id: "questionType-".concat(type.value), type: "checkbox", checked: questionTypes.includes(type.value), onChange: function () { return toggleQuestionType(type.value); }, className: "h-4 w-4 text-blue-600 focus:ring-blue-500 border-gray-300 rounded", disabled: isGenerating }), (0, jsx_runtime_1.jsx)("label", { htmlFor: "questionType-".concat(type.value), className: "ml-2 block text-sm text-gray-700", children: type.label })] }, type.value)); }) })] }), (0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)("label", { className: "block text-sm font-medium text-gray-700 mb-1", children: "Categories" }), (0, jsx_runtime_1.jsx)("div", { className: "border border-gray-300 rounded-md", children: (0, jsx_runtime_1.jsx)(CategoryManager_1.default, { mode: "select", teacherId: teacherId, onCategoriesChange: handleCategoriesChange, apiBaseUrl: apiBaseUrl, selectedCategories: selectedCategoryIds }) })] }), (0, jsx_runtime_1.jsxs)("div", { className: "flex justify-end space-x-3 pt-2", children: [(0, jsx_runtime_1.jsx)("button", { type: "button", onClick: handleCancel, className: "px-4 py-2 border border-gray-300 text-gray-700 rounded-md hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-blue-500", disabled: isGenerating, children: "Cancel" }), (0, jsx_runtime_1.jsx)("button", { type: "submit", className: "px-4 py-2 bg-green-600 text-white rounded-md hover:bg-green-700 focus:outline-none focus:ring-2 focus:ring-green-500", disabled: isGenerating, children: isGenerating ? "Generating..." : "Generate Quiz" })] }), isGenerating && ((0, jsx_runtime_1.jsxs)("div", { className: "mt-4", children: [(0, jsx_runtime_1.jsx)("div", { className: "w-full bg-gray-200 rounded-full h-2.5", children: (0, jsx_runtime_1.jsx)("div", { className: "bg-blue-600 h-2.5 rounded-full transition-all duration-500", style: { width: "".concat(progress, "%") } }) }), (0, jsx_runtime_1.jsx)("p", { className: "text-sm text-gray-500 mt-2 text-center", children: progress < 100 ? "AI is creating challenging questions for your quiz. This may take a moment..." : "Quiz generation complete!" })] }))] })) : ((0, jsx_runtime_1.jsxs)("div", { className: "space-y-6", children: [(0, jsx_runtime_1.jsxs)("div", { className: "bg-green-50 border border-green-200 rounded-md p-4", children: [(0, jsx_runtime_1.jsx)("h3", { className: "text-lg font-medium text-green-800 mb-2", children: "Quiz Generated Successfully!" }), (0, jsx_runtime_1.jsxs)("p", { className: "text-sm text-green-700", children: ["Your quiz containing ", generatedQuiz.questions.length, " questions has been generated. Review it below and save to your collection."] })] }), (0, jsx_runtime_1.jsxs)("div", { className: "bg-white border border-gray-200 rounded-md p-4", children: [(0, jsx_runtime_1.jsx)("h3", { className: "text-lg font-medium mb-2", children: generatedQuiz.title }), generatedQuiz.description && ((0, jsx_runtime_1.jsxs)("div", { className: "mb-4", children: [(0, jsx_runtime_1.jsx)("h4", { className: "text-md font-medium mb-2", children: "Description" }), (0, jsx_runtime_1.jsx)("p", { className: "text-sm text-gray-700", children: generatedQuiz.description })] })), (0, jsx_runtime_1.jsxs)("div", { className: "mb-4", children: [(0, jsx_runtime_1.jsx)("h4", { className: "text-md font-medium mb-2", children: "Questions Preview" }), (0, jsx_runtime_1.jsxs)("div", { className: "space-y-3 max-h-60 overflow-y-auto", children: [generatedQuiz.questions .slice(0, 3) .map(function (question, index) { return ((0, jsx_runtime_1.jsxs)("div", { className: "border border-gray-100 rounded p-3 bg-gray-50", children: [(0, jsx_runtime_1.jsxs)("p", { className: "text-sm font-medium", children: [index + 1, ". ", question.question] }), question.type === "multiple_choice" && question.options && ((0, jsx_runtime_1.jsx)("div", { className: "mt-2 pl-4", children: question.options.map(function (option, optIndex) { return ((0, jsx_runtime_1.jsxs)("div", { className: "text-xs text-gray-700 mt-1", children: [["A", "B", "C", "D", "E"][optIndex] || "".concat(optIndex + 1), ". ", option] }, optIndex)); }) })), question.type === "true_false" && ((0, jsx_runtime_1.jsxs)("div", { className: "mt-2 pl-4", children: [(0, jsx_runtime_1.jsx)("div", { className: "text-xs text-gray-700", children: "A. True" }), (0, jsx_runtime_1.jsx)("div", { className: "text-xs text-gray-700", children: "B. False" })] }))] }, index)); }), generatedQuiz.questions.length > 3 && ((0, jsx_runtime_1.jsxs)("p", { className: "text-xs text-gray-500 text-center italic", children: ["Plus ", generatedQuiz.questions.length - 3, " more questions..."] }))] })] })] }), (0, jsx_runtime_1.jsxs)("div", { className: "flex space-x-3", children: [(0, jsx_runtime_1.jsx)("button", { onClick: handleSave, className: "flex-1 bg-blue-600 text-white py-2 px-4 rounded hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2", disabled: isGenerating, children: isGenerating ? "Saving..." : "Save Quiz" }), (0, jsx_runtime_1.jsx)("button", { onClick: function () { return setGeneratedQuiz(null); }, className: "flex-1 bg-gray-200 text-gray-800 py-2 px-4 rounded hover:bg-gray-300 focus:outline-none focus:ring-2 focus:ring-gray-500 focus:ring-offset-2", disabled: isGenerating, children: "Generate New Quiz" })] })] })) })); }; exports.AIQuizGenerator = AIQuizGenerator; exports.default = exports.AIQuizGenerator;