@studious-lms/server 1.4.3 → 1.4.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.vscode/settings.json +3 -0
- package/dist/index.js +4 -18
- package/dist/index.js.map +1 -1
- package/dist/models/conversation.d.ts +22 -0
- package/dist/models/conversation.d.ts.map +1 -1
- package/dist/models/conversation.js +13 -2
- package/dist/models/conversation.js.map +1 -1
- package/dist/models/labChat.d.ts +3 -0
- package/dist/models/labChat.d.ts.map +1 -1
- package/dist/models/message.d.ts +2 -0
- package/dist/models/message.d.ts.map +1 -1
- package/dist/models/newtonChat.d.ts +1 -0
- package/dist/models/newtonChat.d.ts.map +1 -1
- package/dist/models/worksheet.d.ts +1 -0
- package/dist/models/worksheet.d.ts.map +1 -1
- package/dist/models/worksheet.js +2 -2
- package/dist/models/worksheet.js.map +1 -1
- package/dist/pipelines/pdfToWorksheet.d.ts +12 -0
- package/dist/pipelines/pdfToWorksheet.d.ts.map +1 -0
- package/dist/pipelines/pdfToWorksheet.js +155 -0
- package/dist/pipelines/pdfToWorksheet.js.map +1 -0
- package/dist/routers/_app.d.ts +252 -0
- package/dist/routers/_app.d.ts.map +1 -1
- package/dist/routers/_app.js +4 -2
- package/dist/routers/_app.js.map +1 -1
- package/dist/routers/conversation.d.ts +14 -0
- package/dist/routers/conversation.d.ts.map +1 -1
- package/dist/routers/conversation.js +6 -3
- package/dist/routers/conversation.js.map +1 -1
- package/dist/routers/labChat.d.ts +2 -0
- package/dist/routers/labChat.d.ts.map +1 -1
- package/dist/routers/search.d.ts +41 -0
- package/dist/routers/search.d.ts.map +1 -0
- package/dist/routers/search.js +15 -0
- package/dist/routers/search.js.map +1 -0
- package/dist/routers/worksheet.d.ts +71 -0
- package/dist/routers/worksheet.d.ts.map +1 -1
- package/dist/routers/worksheet.js +110 -3
- package/dist/routers/worksheet.js.map +1 -1
- package/dist/services/conversation.d.ts +8 -0
- package/dist/services/conversation.d.ts.map +1 -1
- package/dist/services/conversation.js +24 -6
- package/dist/services/conversation.js.map +1 -1
- package/dist/services/labChat.d.ts +2 -0
- package/dist/services/labChat.d.ts.map +1 -1
- package/dist/services/message.d.ts.map +1 -1
- package/dist/services/message.js +9 -2
- package/dist/services/message.js.map +1 -1
- package/dist/services/search.d.ts +21 -0
- package/dist/services/search.d.ts.map +1 -0
- package/dist/services/search.js +352 -0
- package/dist/services/search.js.map +1 -0
- package/dist/services/worksheet.d.ts +21 -0
- package/dist/services/worksheet.d.ts.map +1 -1
- package/dist/services/worksheet.js +50 -2
- package/dist/services/worksheet.js.map +1 -1
- package/dist/utils/inference.d.ts +2 -1
- package/dist/utils/inference.d.ts.map +1 -1
- package/dist/utils/inference.js +53 -5
- package/dist/utils/inference.js.map +1 -1
- package/package.json +1 -1
- package/prisma/migrations/20260417083000_add_conversation_member_hidden_at/migration.sql +2 -0
- package/prisma/schema.prisma +1 -0
- package/src/index.ts +4 -19
- package/src/models/conversation.ts +12 -0
- package/src/models/worksheet.ts +1 -0
- package/src/pipelines/pdfToWorksheet.ts +177 -0
- package/src/routers/_app.ts +2 -0
- package/src/routers/conversation.ts +7 -0
- package/src/routers/search.ts +14 -0
- package/src/routers/worksheet.ts +143 -0
- package/src/services/conversation.ts +27 -3
- package/src/services/message.ts +8 -0
- package/src/services/search.ts +397 -0
- package/src/services/worksheet.ts +61 -0
- package/src/utils/inference.ts +77 -7
- package/tests/pipelines/pdfToWorksheet.test.ts +176 -0
- package/tests/routers/conversation.test.ts +114 -2
- package/tests/routers/search.test.ts +109 -0
- package/tests/utils/inference.test.ts +37 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"worksheet.js","sources":["models/worksheet.ts"],"sourceRoot":"/","sourcesContent":["/**\n * Worksheet model – worksheets, questions, submissions, student progress, grading.\n */\nimport { prisma } from \"../lib/prisma.js\";\nimport type { WorksheetQuestionType, GenerationStatus } from \"@prisma/client\";\n\nconst worksheetClassMemberWhere = (userId: string) => ({\n class: {\n OR: [\n { teachers: { some: { id: userId } } },\n { students: { some: { id: userId } } },\n ],\n },\n});\n\nconst worksheetTeacherWhere = (userId: string) => ({\n class: {\n teachers: { some: { id: userId } },\n },\n});\n\n/** @returns Worksheet with questions and class. */\nexport function findWorksheetById(id: string) {\n return prisma.worksheet.findUnique({\n where: { id },\n include: {\n questions: { orderBy: { createdAt: \"asc\" as const } },\n class: true,\n },\n });\n}\n\n/** @returns Worksheet by ID (minimal). */\nexport function findWorksheetByIdMinimal(id: string) {\n return prisma.worksheet.findUnique({\n where: { id },\n });\n}\n\n/** @returns Worksheet by ID for an authorized class member. */\nexport function findWorksheetByIdForClassMember(id: string, userId: string) {\n return prisma.worksheet.findFirst({\n where: {\n id,\n ...worksheetClassMemberWhere(userId),\n },\n include: {\n questions: { orderBy: { createdAt: \"asc\" as const } },\n class: true,\n },\n });\n}\n\n/** @returns Worksheet by ID for an authorized teacher. */\nexport function findWorksheetByIdForTeacher(id: string, userId: string) {\n return prisma.worksheet.findFirst({\n where: {\n id,\n ...worksheetTeacherWhere(userId),\n },\n include: {\n questions: { orderBy: { createdAt: \"asc\" as const } },\n class: true,\n },\n });\n}\n\n/** @returns Worksheets for a class. */\nexport function findWorksheetsByClassId(classId: string) {\n return prisma.worksheet.findMany({\n where: { classId },\n include: {\n questions: { select: { id: true } },\n },\n orderBy: { createdAt: \"desc\" },\n });\n}\n\n/** @returns Worksheet question by ID for an authorized teacher. */\nexport function findWorksheetQuestionByIdForTeacher(id: string, userId: string) {\n return prisma.worksheetQuestion.findFirst({\n where: {\n id,\n worksheet: worksheetTeacherWhere(userId),\n },\n });\n}\n\n/** @returns Worksheet question scoped to a worksheet. */\nexport function findWorksheetQuestionByIdInWorksheet(\n id: string,\n worksheetId: string\n) {\n return prisma.worksheetQuestion.findFirst({\n where: { id, worksheetId },\n });\n}\n\n/** Create a worksheet. */\nexport function createWorksheet(data: { classId: string; name: string }) {\n return prisma.worksheet.create({\n data,\n });\n}\n\n/** Update worksheet name. */\nexport function updateWorksheet(id: string, data: { name?: string }) {\n return prisma.worksheet.update({\n where: { id },\n data,\n });\n}\n\n/** Delete a worksheet. */\nexport function deleteWorksheet(id: string) {\n return prisma.worksheet.delete({\n where: { id },\n });\n}\n\n/** @returns Questions for a worksheet. */\nexport function findQuestionsByWorksheetId(worksheetId: string) {\n return prisma.worksheetQuestion.findMany({\n where: { worksheetId },\n orderBy: { order: \"asc\" },\n });\n}\n\n/** Create a worksheet question. */\nexport function createWorksheetQuestion(data: {\n worksheetId: string;\n type: WorksheetQuestionType;\n question: string;\n answer: string;\n points?: number;\n options?: object;\n markScheme?: object;\n}) {\n return prisma.worksheetQuestion.create({\n data,\n });\n}\n\n/** Update question order. */\nexport function updateWorksheetQuestionOrder(id: string, order: number) {\n return prisma.worksheetQuestion.update({\n where: { id },\n data: { order },\n });\n}\n\n/** Update a worksheet question. */\nexport function updateWorksheetQuestion(\n id: string,\n data: {\n question?: string;\n answer?: string;\n points?: number;\n options?: object;\n markScheme?: object;\n type?: WorksheetQuestionType;\n }\n) {\n return prisma.worksheetQuestion.update({\n where: { id },\n data,\n });\n}\n\n/** Delete a worksheet question. */\nexport function deleteWorksheetQuestion(id: string) {\n return prisma.worksheetQuestion.delete({\n where: { id },\n });\n}\n\n/** @returns Submission by ID. */\nexport function findSubmissionById(id: string) {\n return prisma.submission.findUnique({\n where: { id },\n });\n}\n\n/** @returns Submission if requester is the owning student or a class teacher for the worksheet. */\nexport function findSubmissionByIdForWorksheetAccess(\n id: string,\n worksheetId: string,\n worksheetClassId: string,\n userId: string\n) {\n return prisma.submission.findFirst({\n where: {\n id,\n assignment: {\n classId: worksheetClassId,\n worksheets: { some: { id: worksheetId } },\n },\n OR: [\n {\n studentId: userId,\n assignment: {\n class: {\n students: { some: { id: userId } },\n },\n },\n },\n {\n assignment: {\n class: {\n teachers: { some: { id: userId } },\n },\n },\n },\n ],\n },\n select: {\n id: true,\n studentId: true,\n },\n });\n}\n\nconst worksheetResponseInclude = {\n responses: {\n include: {\n comments: { select: { id: true } },\n },\n },\n};\n\n/** @returns Worksheet response for submission and worksheet. */\nexport function findWorksheetResponseBySubmissionAndWorksheet(\n submissionId: string,\n worksheetId: string\n) {\n return prisma.studentWorksheetResponse.findFirst({\n where: { submissionId, worksheetId },\n include: worksheetResponseInclude,\n });\n}\n\n/** Get or create worksheet response for student. */\nexport async function findOrCreateWorksheetResponse(\n submissionId: string,\n worksheetId: string,\n studentId: string\n) {\n return prisma.$transaction(async (tx) => {\n const existing = await tx.studentWorksheetResponse.findFirst({\n where: { submissionId, worksheetId },\n include: worksheetResponseInclude,\n });\n if (existing) return existing;\n return tx.studentWorksheetResponse.create({\n data: { worksheetId, submissionId, studentId },\n include: worksheetResponseInclude,\n });\n });\n}\n\n/** Create a worksheet response. */\nexport function createWorksheetResponse(data: {\n worksheetId: string;\n submissionId: string;\n studentId: string;\n}) {\n return prisma.studentWorksheetResponse.create({\n data,\n include: {\n responses: {\n include: {\n comments: { select: { id: true } },\n },\n },\n },\n });\n}\n\n/** @returns Worksheet response by ID. */\nexport function findWorksheetResponseById(id: string) {\n return prisma.studentWorksheetResponse.findUnique({\n where: { id },\n include: { responses: true },\n });\n}\n\n/** @returns Worksheet response if requester is the owning student. */\nexport function findWorksheetResponseByIdForStudent(id: string, userId: string) {\n return prisma.studentWorksheetResponse.findFirst({\n where: {\n id,\n studentId: userId,\n worksheet: worksheetClassMemberWhere(userId),\n },\n include: { responses: true, worksheet: true },\n });\n}\n\n/** @returns Worksheet response if requester is a class teacher. */\nexport function findWorksheetResponseByIdForTeacher(id: string, userId: string) {\n return prisma.studentWorksheetResponse.findFirst({\n where: {\n id,\n worksheet: worksheetTeacherWhere(userId),\n },\n include: { responses: true, worksheet: true },\n });\n}\n\n/** @returns Worksheet response with question responses. */\nexport function findWorksheetResponseWithResponses(\n id: string,\n questionId?: string\n) {\n return prisma.studentWorksheetResponse.findUnique({\n where: { id },\n include: {\n responses: questionId\n ? { where: { questionId } }\n : true,\n },\n });\n}\n\n/** @returns Worksheet response with scoped responses if requester is the owning student. */\nexport function findWorksheetResponseWithResponsesForStudent(\n id: string,\n userId: string,\n questionId?: string\n) {\n return prisma.studentWorksheetResponse.findFirst({\n where: {\n id,\n studentId: userId,\n worksheet: worksheetClassMemberWhere(userId),\n },\n include: {\n worksheet: true,\n responses: questionId ? { where: { questionId } } : true,\n },\n });\n}\n\n/** @returns Worksheet response with scoped responses if requester is a class teacher. */\nexport function findWorksheetResponseWithResponsesForTeacher(\n id: string,\n userId: string,\n questionId?: string\n) {\n return prisma.studentWorksheetResponse.findFirst({\n where: {\n id,\n worksheet: worksheetTeacherWhere(userId),\n },\n include: {\n worksheet: true,\n responses: questionId ? { where: { questionId } } : true,\n },\n });\n}\n\n/** @returns Worksheet question by ID. */\nexport function findWorksheetQuestionById(id: string) {\n return prisma.worksheetQuestion.findUnique({\n where: { id },\n });\n}\n\n/** @returns Student question progress if requester is a class teacher. */\nexport function findStudentQuestionProgressByIdForTeacher(\n id: string,\n userId: string\n) {\n return prisma.studentQuestionProgress.findFirst({\n where: {\n id,\n question: {\n worksheet: worksheetTeacherWhere(userId),\n },\n },\n include: {\n question: true,\n studentWorksheetResponse: true,\n },\n });\n}\n\n/** @returns Student question progress by id and worksheet response if requester is a class teacher. */\nexport function findStudentQuestionProgressByIdForTeacherAndResponse(\n id: string,\n studentWorksheetResponseId: string,\n userId: string\n) {\n return prisma.studentQuestionProgress.findFirst({\n where: {\n id,\n studentWorksheetResponseId,\n question: {\n worksheet: worksheetTeacherWhere(userId),\n },\n },\n include: {\n question: true,\n studentWorksheetResponse: true,\n },\n });\n}\n\n/** Update student's question progress. */\nexport function updateStudentQuestionProgress(\n id: string,\n data: {\n response?: string;\n isCorrect?: boolean;\n points?: number;\n status?: GenerationStatus;\n }\n) {\n return prisma.studentQuestionProgress.update({\n where: { id },\n data,\n });\n}\n\n/** Create student question progress. */\nexport function createStudentQuestionProgress(data: {\n studentId: string;\n questionId: string;\n studentWorksheetResponseId: string;\n response: string;\n isCorrect?: boolean;\n points?: number;\n}) {\n return prisma.studentQuestionProgress.create({\n data,\n });\n}\n\n/** @returns Student's progress for a question. */\nexport function findStudentQuestionProgress(\n studentId: string,\n questionId: string,\n studentWorksheetResponseId: string\n) {\n return prisma.studentQuestionProgress.findFirst({\n where: {\n studentId,\n questionId,\n studentWorksheetResponseId,\n },\n });\n}\n\n/** Update progress for grading (AI feedback, marks). */\nexport function updateStudentQuestionProgressForGrading(\n id: string,\n data: {\n isCorrect?: boolean;\n feedback?: string;\n markschemeState?: object;\n points?: number;\n response?: string;\n }\n) {\n return prisma.studentQuestionProgress.update({\n where: { id },\n data,\n });\n}\n\n/** Create progress record for grading pipeline. */\nexport function createStudentQuestionProgressForGrading(data: {\n studentId: string;\n questionId: string;\n studentWorksheetResponseId: string;\n response: string;\n isCorrect: boolean;\n feedback?: string;\n markschemeState?: object;\n points?: number;\n}) {\n return prisma.studentQuestionProgress.create({\n data,\n });\n}\n\n/** Create a comment on a worksheet response. */\nexport function createComment(data: {\n studentQuestionProgressId: string;\n content: string;\n authorId: string;\n}) {\n return prisma.comment.create({\n data,\n });\n}\n"],"names":[],"mappings":"AAAA;;GAEG;;;AACH,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAG1C,MAAM,yBAAyB,GAAG,CAAC,MAAc,EAAE,EAAE,CAAC,CAAC;IACrD,KAAK,EAAE;QACL,EAAE,EAAE;YACF,EAAE,QAAQ,EAAE,EAAE,IAAI,EAAE,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE;YACtC,EAAE,QAAQ,EAAE,EAAE,IAAI,EAAE,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE;SACvC;KACF;CACF,CAAC,CAAC;AAEH,MAAM,qBAAqB,GAAG,CAAC,MAAc,EAAE,EAAE,CAAC,CAAC;IACjD,KAAK,EAAE;QACL,QAAQ,EAAE,EAAE,IAAI,EAAE,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE;KACnC;CACF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K,EAAE;YACL,SAAS;YACT,UAAU;YACV,0BAA0B;SAC3B;KACF,CAAC,CAAC;AACL,CAAC;AAED,wDAAwD;AACxD,MAAM,UAAU,uCAAuC,CACrD,EAAU,EACV,IAMC;IAED,OAAO,MAAM,CAAC,uBAAuB,CAAC,MAAM,CAAC;QAC3C,KAAK,EAAE,EAAE,EAAE,EAAE;QACb,IAAI;KACL,CAAC,CAAC;AACL,CAAC;AAED,mDAAmD;AACnD,MAAM,UAAU,uCAAuC,CAAC,IASvD;IACC,OAAO,MAAM,CAAC,uBAAuB,CAAC,MAAM,CAAC;QAC3C,IAAI;KACL,CAAC,CAAC;AACL,CAAC;AAED,gDAAgD;AAChD,MAAM,UAAU,aAAa,CAAC,IAI7B;IACC,OAAO,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC;QAC3B,IAAI;KACL,CAAC,CAAC;AACL,CAAC","debug_id":"5edbab9e-02d0-5d22-a1f0-c2cfdcd85f9a"}
|
|
1
|
+
{"version":3,"file":"worksheet.js","sources":["models/worksheet.ts"],"sourceRoot":"/","sourcesContent":["/**\n * Worksheet model – worksheets, questions, submissions, student progress, grading.\n */\nimport { prisma } from \"../lib/prisma.js\";\nimport type { WorksheetQuestionType, GenerationStatus } from \"@prisma/client\";\n\nconst worksheetClassMemberWhere = (userId: string) => ({\n class: {\n OR: [\n { teachers: { some: { id: userId } } },\n { students: { some: { id: userId } } },\n ],\n },\n});\n\nconst worksheetTeacherWhere = (userId: string) => ({\n class: {\n teachers: { some: { id: userId } },\n },\n});\n\n/** @returns Worksheet with questions and class. */\nexport function findWorksheetById(id: string) {\n return prisma.worksheet.findUnique({\n where: { id },\n include: {\n questions: { orderBy: { createdAt: \"asc\" as const } },\n class: true,\n },\n });\n}\n\n/** @returns Worksheet by ID (minimal). */\nexport function findWorksheetByIdMinimal(id: string) {\n return prisma.worksheet.findUnique({\n where: { id },\n });\n}\n\n/** @returns Worksheet by ID for an authorized class member. */\nexport function findWorksheetByIdForClassMember(id: string, userId: string) {\n return prisma.worksheet.findFirst({\n where: {\n id,\n ...worksheetClassMemberWhere(userId),\n },\n include: {\n questions: { orderBy: { createdAt: \"asc\" as const } },\n class: true,\n },\n });\n}\n\n/** @returns Worksheet by ID for an authorized teacher. */\nexport function findWorksheetByIdForTeacher(id: string, userId: string) {\n return prisma.worksheet.findFirst({\n where: {\n id,\n ...worksheetTeacherWhere(userId),\n },\n include: {\n questions: { orderBy: { createdAt: \"asc\" as const } },\n class: true,\n },\n });\n}\n\n/** @returns Worksheets for a class. */\nexport function findWorksheetsByClassId(classId: string) {\n return prisma.worksheet.findMany({\n where: { classId },\n include: {\n questions: { select: { id: true } },\n },\n orderBy: { createdAt: \"desc\" },\n });\n}\n\n/** @returns Worksheet question by ID for an authorized teacher. */\nexport function findWorksheetQuestionByIdForTeacher(id: string, userId: string) {\n return prisma.worksheetQuestion.findFirst({\n where: {\n id,\n worksheet: worksheetTeacherWhere(userId),\n },\n });\n}\n\n/** @returns Worksheet question scoped to a worksheet. */\nexport function findWorksheetQuestionByIdInWorksheet(\n id: string,\n worksheetId: string\n) {\n return prisma.worksheetQuestion.findFirst({\n where: { id, worksheetId },\n });\n}\n\n/** Create a worksheet. */\nexport function createWorksheet(data: { classId: string; name: string }) {\n return prisma.worksheet.create({\n data,\n });\n}\n\n/** Update worksheet name. */\nexport function updateWorksheet(id: string, data: { name?: string }) {\n return prisma.worksheet.update({\n where: { id },\n data,\n });\n}\n\n/** Delete a worksheet. */\nexport function deleteWorksheet(id: string) {\n return prisma.worksheet.delete({\n where: { id },\n });\n}\n\n/** @returns Questions for a worksheet. */\nexport function findQuestionsByWorksheetId(worksheetId: string) {\n return prisma.worksheetQuestion.findMany({\n where: { worksheetId },\n orderBy: { order: \"asc\" },\n });\n}\n\n/** Create a worksheet question. */\nexport function createWorksheetQuestion(data: {\n worksheetId: string;\n type: WorksheetQuestionType;\n question: string;\n answer: string;\n points?: number;\n options?: object;\n markScheme?: object;\n order?: number;\n}) {\n return prisma.worksheetQuestion.create({\n data,\n });\n}\n\n/** Update question order. */\nexport function updateWorksheetQuestionOrder(id: string, order: number) {\n return prisma.worksheetQuestion.update({\n where: { id },\n data: { order },\n });\n}\n\n/** Update a worksheet question. */\nexport function updateWorksheetQuestion(\n id: string,\n data: {\n question?: string;\n answer?: string;\n points?: number;\n options?: object;\n markScheme?: object;\n type?: WorksheetQuestionType;\n }\n) {\n return prisma.worksheetQuestion.update({\n where: { id },\n data,\n });\n}\n\n/** Delete a worksheet question. */\nexport function deleteWorksheetQuestion(id: string) {\n return prisma.worksheetQuestion.delete({\n where: { id },\n });\n}\n\n/** @returns Submission by ID. */\nexport function findSubmissionById(id: string) {\n return prisma.submission.findUnique({\n where: { id },\n });\n}\n\n/** @returns Submission if requester is the owning student or a class teacher for the worksheet. */\nexport function findSubmissionByIdForWorksheetAccess(\n id: string,\n worksheetId: string,\n worksheetClassId: string,\n userId: string\n) {\n return prisma.submission.findFirst({\n where: {\n id,\n assignment: {\n classId: worksheetClassId,\n worksheets: { some: { id: worksheetId } },\n },\n OR: [\n {\n studentId: userId,\n assignment: {\n class: {\n students: { some: { id: userId } },\n },\n },\n },\n {\n assignment: {\n class: {\n teachers: { some: { id: userId } },\n },\n },\n },\n ],\n },\n select: {\n id: true,\n studentId: true,\n },\n });\n}\n\nconst worksheetResponseInclude = {\n responses: {\n include: {\n comments: { select: { id: true } },\n },\n },\n};\n\n/** @returns Worksheet response for submission and worksheet. */\nexport function findWorksheetResponseBySubmissionAndWorksheet(\n submissionId: string,\n worksheetId: string\n) {\n return prisma.studentWorksheetResponse.findFirst({\n where: { submissionId, worksheetId },\n include: worksheetResponseInclude,\n });\n}\n\n/** Get or create worksheet response for student. */\nexport async function findOrCreateWorksheetResponse(\n submissionId: string,\n worksheetId: string,\n studentId: string\n) {\n return prisma.$transaction(async (tx) => {\n const existing = await tx.studentWorksheetResponse.findFirst({\n where: { submissionId, worksheetId },\n include: worksheetResponseInclude,\n });\n if (existing) return existing;\n return tx.studentWorksheetResponse.create({\n data: { worksheetId, submissionId, studentId },\n include: worksheetResponseInclude,\n });\n });\n}\n\n/** Create a worksheet response. */\nexport function createWorksheetResponse(data: {\n worksheetId: string;\n submissionId: string;\n studentId: string;\n}) {\n return prisma.studentWorksheetResponse.create({\n data,\n include: {\n responses: {\n include: {\n comments: { select: { id: true } },\n },\n },\n },\n });\n}\n\n/** @returns Worksheet response by ID. */\nexport function findWorksheetResponseById(id: string) {\n return prisma.studentWorksheetResponse.findUnique({\n where: { id },\n include: { responses: true },\n });\n}\n\n/** @returns Worksheet response if requester is the owning student. */\nexport function findWorksheetResponseByIdForStudent(id: string, userId: string) {\n return prisma.studentWorksheetResponse.findFirst({\n where: {\n id,\n studentId: userId,\n worksheet: worksheetClassMemberWhere(userId),\n },\n include: { responses: true, worksheet: true },\n });\n}\n\n/** @returns Worksheet response if requester is a class teacher. */\nexport function findWorksheetResponseByIdForTeacher(id: string, userId: string) {\n return prisma.studentWorksheetResponse.findFirst({\n where: {\n id,\n worksheet: worksheetTeacherWhere(userId),\n },\n include: { responses: true, worksheet: true },\n });\n}\n\n/** @returns Worksheet response with question responses. */\nexport function findWorksheetResponseWithResponses(\n id: string,\n questionId?: string\n) {\n return prisma.studentWorksheetResponse.findUnique({\n where: { id },\n include: {\n responses: questionId\n ? { where: { questionId } }\n : true,\n },\n });\n}\n\n/** @returns Worksheet response with scoped responses if requester is the owning student. */\nexport function findWorksheetResponseWithResponsesForStudent(\n id: string,\n userId: string,\n questionId?: string\n) {\n return prisma.studentWorksheetResponse.findFirst({\n where: {\n id,\n studentId: userId,\n worksheet: worksheetClassMemberWhere(userId),\n },\n include: {\n worksheet: true,\n responses: questionId ? { where: { questionId } } : true,\n },\n });\n}\n\n/** @returns Worksheet response with scoped responses if requester is a class teacher. */\nexport function findWorksheetResponseWithResponsesForTeacher(\n id: string,\n userId: string,\n questionId?: string\n) {\n return prisma.studentWorksheetResponse.findFirst({\n where: {\n id,\n worksheet: worksheetTeacherWhere(userId),\n },\n include: {\n worksheet: true,\n responses: questionId ? { where: { questionId } } : true,\n },\n });\n}\n\n/** @returns Worksheet question by ID. */\nexport function findWorksheetQuestionById(id: string) {\n return prisma.worksheetQuestion.findUnique({\n where: { id },\n });\n}\n\n/** @returns Student question progress if requester is a class teacher. */\nexport function findStudentQuestionProgressByIdForTeacher(\n id: string,\n userId: string\n) {\n return prisma.studentQuestionProgress.findFirst({\n where: {\n id,\n question: {\n worksheet: worksheetTeacherWhere(userId),\n },\n },\n include: {\n question: true,\n studentWorksheetResponse: true,\n },\n });\n}\n\n/** @returns Student question progress by id and worksheet response if requester is a class teacher. */\nexport function findStudentQuestionProgressByIdForTeacherAndResponse(\n id: string,\n studentWorksheetResponseId: string,\n userId: string\n) {\n return prisma.studentQuestionProgress.findFirst({\n where: {\n id,\n studentWorksheetResponseId,\n question: {\n worksheet: worksheetTeacherWhere(userId),\n },\n },\n include: {\n question: true,\n studentWorksheetResponse: true,\n },\n });\n}\n\n/** Update student's question progress. */\nexport function updateStudentQuestionProgress(\n id: string,\n data: {\n response?: string;\n isCorrect?: boolean;\n points?: number;\n status?: GenerationStatus;\n }\n) {\n return prisma.studentQuestionProgress.update({\n where: { id },\n data,\n });\n}\n\n/** Create student question progress. */\nexport function createStudentQuestionProgress(data: {\n studentId: string;\n questionId: string;\n studentWorksheetResponseId: string;\n response: string;\n isCorrect?: boolean;\n points?: number;\n}) {\n return prisma.studentQuestionProgress.create({\n data,\n });\n}\n\n/** @returns Student's progress for a question. */\nexport function findStudentQuestionProgress(\n studentId: string,\n questionId: string,\n studentWorksheetResponseId: string\n) {\n return prisma.studentQuestionProgress.findFirst({\n where: {\n studentId,\n questionId,\n studentWorksheetResponseId,\n },\n });\n}\n\n/** Update progress for grading (AI feedback, marks). */\nexport function updateStudentQuestionProgressForGrading(\n id: string,\n data: {\n isCorrect?: boolean;\n feedback?: string;\n markschemeState?: object;\n points?: number;\n response?: string;\n }\n) {\n return prisma.studentQuestionProgress.update({\n where: { id },\n data,\n });\n}\n\n/** Create progress record for grading pipeline. */\nexport function createStudentQuestionProgressForGrading(data: {\n studentId: string;\n questionId: string;\n studentWorksheetResponseId: string;\n response: string;\n isCorrect: boolean;\n feedback?: string;\n markschemeState?: object;\n points?: number;\n}) {\n return prisma.studentQuestionProgress.create({\n data,\n });\n}\n\n/** Create a comment on a worksheet response. */\nexport function createComment(data: {\n studentQuestionProgressId: string;\n content: string;\n authorId: string;\n}) {\n return prisma.comment.create({\n data,\n });\n}\n"],"names":[],"mappings":"AAAA;;GAEG;;;AACH,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAG1C,MAAM,yBAAyB,GAAG,CAAC,MAAc,EAAE,EAAE,CAAC,CAAC;IACrD,KAAK,EAAE;QACL,EAAE,EAAE;YACF,EAAE,QAAQ,EAAE,EAAE,IAAI,EAAE,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE;YACtC,EAAE,QAAQ,EAAE,EAAE,IAAI,EAAE,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE;SACvC;KACF;CACF,CAAC,CAAC;AAEH,MAAM,qBAAqB,GAAG,CAAC,MAAc,EAAE,EAAE,CAAC,CAAC;IACjD,KAAK,EAAE;QACL,QAAQ,EAAE,EAAE,IAAI,EAAE,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE;KACnC;CACF,CAAC,CAAC;AAEH,mDAAmD;AACnD,MAAM,UAAU,iBAAiB,CAAC,EAAU;IAC1C,OAAO,MAAM,CAAC,SAAS,CAAC,UAAU,CAAC;QACjC,KAAK,EAAE,EAAE,EAAE,EAAE;QACb,OAAO,EAAE;YACP,SAAS,EAAE,EAAE,OAAO,EAAE,EAAE,SAAS,EAAE,KAAc,EAAE,EAAE;YACrD,KAAK,EAAE,IAAI;SACZ;KACF,CAAC,CAAC;AACL,CAAC;AAED,0CAA0C;AAC1C,MAAM,UAAU,wBAAwB,CAAC,EAAU;IACjD,OAAO,MAAM,CAAC,SAAS,CAAC,UAAU,CAAC;QACjC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AC;AAED,gDAAgD;AAChD,MAAM,UAAU,aAAa,CAAC,IAI7B;IACC,OAAO,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC;QAC3B,IAAI;KACL,CAAC,CAAC;AACL,CAAC","debug_id":"ac1564ba-a589-57e4-8f58-f282c5648ea5"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sends a PDF URL to the model for remote retrieval, extracts a worksheet via
|
|
3
|
+
* structured inference, and inserts worksheet + questions in one DB transaction.
|
|
4
|
+
*/
|
|
5
|
+
export declare function pdfToWorksheet(gcsUrl: string, classId: string): Promise<{
|
|
6
|
+
name: string;
|
|
7
|
+
id: string;
|
|
8
|
+
createdAt: Date;
|
|
9
|
+
classId: string;
|
|
10
|
+
updatedAt: Date;
|
|
11
|
+
} | null>;
|
|
12
|
+
//# sourceMappingURL=pdfToWorksheet.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pdfToWorksheet.d.ts","sourceRoot":"/","sources":["pipelines/pdfToWorksheet.ts"],"names":[],"mappings":"AA0GA;;;GAGG;AACH,wBAAsB,cAAc,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;;;;;;UAkEnE"}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PDF URL → worksheet: send a remote PDF URL to structured inference,
|
|
3
|
+
* then persist a Worksheet and WorksheetQuestion rows.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="ea4c1689-45bc-59df-b76c-6c749341cffe")}catch(e){}}();
|
|
7
|
+
import { z } from "zod";
|
|
8
|
+
import { prisma } from "../lib/prisma.js";
|
|
9
|
+
import { inference } from "../utils/inference.js";
|
|
10
|
+
import { logger } from "../utils/logger.js";
|
|
11
|
+
/** Restrict remote fetches to Google-hosted object URLs. */
|
|
12
|
+
function isProbablyGoogleCloudUrl(url) {
|
|
13
|
+
try {
|
|
14
|
+
const host = new URL(url).hostname;
|
|
15
|
+
return (host.endsWith("storage.googleapis.com") ||
|
|
16
|
+
host.endsWith("googleusercontent.com") ||
|
|
17
|
+
host.includes("storage.cloud.google.com"));
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
function getFilenameFromUrl(url) {
|
|
24
|
+
try {
|
|
25
|
+
const parsed = new URL(url);
|
|
26
|
+
const lastSegment = parsed.pathname.split("/").filter(Boolean).pop();
|
|
27
|
+
return lastSegment && lastSegment.includes(".") ? lastSegment : "document.pdf";
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return "document.pdf";
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
function sanitizeUrlForLogs(url) {
|
|
34
|
+
try {
|
|
35
|
+
const parsed = new URL(url);
|
|
36
|
+
return `${parsed.origin}${parsed.pathname}`;
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
return 'invalid-url';
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/** LLM output shape (validated by inference). */
|
|
43
|
+
const worksheetDraftSchema = z.object({
|
|
44
|
+
title: z.string(),
|
|
45
|
+
questions: z
|
|
46
|
+
.array(z.object({
|
|
47
|
+
type: z.enum([
|
|
48
|
+
"MULTIPLE_CHOICE",
|
|
49
|
+
"TRUE_FALSE",
|
|
50
|
+
"SHORT_ANSWER",
|
|
51
|
+
"LONG_ANSWER",
|
|
52
|
+
"MATH_EXPRESSION",
|
|
53
|
+
"ESSAY",
|
|
54
|
+
]),
|
|
55
|
+
question: z.string(),
|
|
56
|
+
answer: z.string(),
|
|
57
|
+
options: z
|
|
58
|
+
.array(z.object({
|
|
59
|
+
id: z.string(),
|
|
60
|
+
text: z.string(),
|
|
61
|
+
isCorrect: z.boolean(),
|
|
62
|
+
}))
|
|
63
|
+
.optional()
|
|
64
|
+
.default([]),
|
|
65
|
+
markScheme: z
|
|
66
|
+
.array(z.object({
|
|
67
|
+
id: z.string(),
|
|
68
|
+
points: z.number().transform((value) => Math.round(value)),
|
|
69
|
+
description: z.string(),
|
|
70
|
+
}))
|
|
71
|
+
.optional()
|
|
72
|
+
.default([]),
|
|
73
|
+
points: z.number().optional().default(0),
|
|
74
|
+
order: z.number().transform((value) => Math.round(value)),
|
|
75
|
+
}))
|
|
76
|
+
.default([]),
|
|
77
|
+
});
|
|
78
|
+
const schemaInstructions = `Return ONLY valid JSON matching this schema:
|
|
79
|
+
{
|
|
80
|
+
"title": string,
|
|
81
|
+
"questions": [
|
|
82
|
+
{
|
|
83
|
+
"type": "MULTIPLE_CHOICE" | "TRUE_FALSE" | "SHORT_ANSWER" | "LONG_ANSWER" | "MATH_EXPRESSION" | "ESSAY",
|
|
84
|
+
"question": string,
|
|
85
|
+
"answer": string,
|
|
86
|
+
"options": [ { "id": string, "text": string, "isCorrect": boolean } ],
|
|
87
|
+
"markScheme": [ { "id": string, "points": number, "description": string } ],
|
|
88
|
+
"points": number,
|
|
89
|
+
"order": number
|
|
90
|
+
}
|
|
91
|
+
]
|
|
92
|
+
}
|
|
93
|
+
Rules: "questions" is an array ([] if none). Non-MCQ "options" can be []. Use "order" 1,2,3,… in reading order. No markdown or extra keys.
|
|
94
|
+
If no answer is provided for a multiple-choice or true/false question, determine the answer from the options.`;
|
|
95
|
+
/**
|
|
96
|
+
* Sends a PDF URL to the model for remote retrieval, extracts a worksheet via
|
|
97
|
+
* structured inference, and inserts worksheet + questions in one DB transaction.
|
|
98
|
+
*/
|
|
99
|
+
export async function pdfToWorksheet(gcsUrl, classId) {
|
|
100
|
+
const sanitizedUrl = sanitizeUrlForLogs(gcsUrl);
|
|
101
|
+
if (!isProbablyGoogleCloudUrl(gcsUrl)) {
|
|
102
|
+
logger.warn("pdfToWorksheet: rejected unsupported PDF URL", {
|
|
103
|
+
sanitizedUrl,
|
|
104
|
+
});
|
|
105
|
+
throw new Error("Only Google Cloud Storage URLs are supported");
|
|
106
|
+
}
|
|
107
|
+
const filename = getFilenameFromUrl(gcsUrl);
|
|
108
|
+
const prompt = `Read the attached PDF and convert it into a single worksheet JSON object.\n\n${schemaInstructions}`;
|
|
109
|
+
// Force Responses API input types so we can pass a remote PDF via `file_url`.
|
|
110
|
+
const input = [
|
|
111
|
+
{
|
|
112
|
+
role: "user",
|
|
113
|
+
content: [
|
|
114
|
+
{ type: "input_text", text: prompt },
|
|
115
|
+
{ type: "input_file", file_url: gcsUrl },
|
|
116
|
+
],
|
|
117
|
+
},
|
|
118
|
+
];
|
|
119
|
+
const draft = await inference(input, worksheetDraftSchema, {
|
|
120
|
+
reason: 'pdf_to_worksheet_draft',
|
|
121
|
+
metadata: {
|
|
122
|
+
classId,
|
|
123
|
+
filename,
|
|
124
|
+
},
|
|
125
|
+
});
|
|
126
|
+
if (!draft) {
|
|
127
|
+
logger.warn("pdfToWorksheet: no model output", { filename });
|
|
128
|
+
return null;
|
|
129
|
+
}
|
|
130
|
+
return prisma.$transaction(async (tx) => {
|
|
131
|
+
const worksheet = await tx.worksheet.create({
|
|
132
|
+
data: { classId, name: draft.title },
|
|
133
|
+
});
|
|
134
|
+
if (draft.questions.length > 0) {
|
|
135
|
+
await tx.worksheetQuestion.createMany({
|
|
136
|
+
data: draft.questions.map((question) => ({
|
|
137
|
+
worksheetId: worksheet.id,
|
|
138
|
+
type: question.type,
|
|
139
|
+
question: question.question,
|
|
140
|
+
answer: question.answer,
|
|
141
|
+
points: Math.round(question.points ?? 0),
|
|
142
|
+
options: question.options ?? [],
|
|
143
|
+
markScheme: (question.markScheme ?? []).map((item) => ({
|
|
144
|
+
...item,
|
|
145
|
+
points: Math.round(item.points),
|
|
146
|
+
})),
|
|
147
|
+
order: Math.round(question.order),
|
|
148
|
+
})),
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
return worksheet;
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
//# sourceMappingURL=pdfToWorksheet.js.map
|
|
155
|
+
//# debugId=ea4c1689-45bc-59df-b76c-6c749341cffe
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pdfToWorksheet.js","sources":["pipelines/pdfToWorksheet.ts"],"sourceRoot":"/","sourcesContent":["/**\n * PDF URL → worksheet: send a remote PDF URL to structured inference,\n * then persist a Worksheet and WorksheetQuestion rows.\n */\nimport { z } from \"zod\";\nimport type { ResponseInput } from \"openai/resources/responses/responses.js\";\nimport { prisma } from \"../lib/prisma.js\";\nimport { inference } from \"../utils/inference.js\";\nimport { logger } from \"../utils/logger.js\";\n\n/** Restrict remote fetches to Google-hosted object URLs. */\nfunction isProbablyGoogleCloudUrl(url: string): boolean {\n try {\n const host = new URL(url).hostname;\n return (\n host.endsWith(\"storage.googleapis.com\") ||\n host.endsWith(\"googleusercontent.com\") ||\n host.includes(\"storage.cloud.google.com\")\n );\n } catch {\n return false;\n }\n}\n\nfunction getFilenameFromUrl(url: string): string {\n try {\n const parsed = new URL(url);\n const lastSegment = parsed.pathname.split(\"/\").filter(Boolean).pop();\n return lastSegment && lastSegment.includes(\".\") ? lastSegment : \"document.pdf\";\n } catch {\n return \"document.pdf\";\n }\n}\n\nfunction sanitizeUrlForLogs(url: string): string {\n try {\n const parsed = new URL(url);\n return `${parsed.origin}${parsed.pathname}`;\n } catch {\n return 'invalid-url';\n }\n}\n\n/** LLM output shape (validated by inference). */\nconst worksheetDraftSchema = z.object({\n title: z.string(),\n questions: z\n .array(\n z.object({\n type: z.enum([\n \"MULTIPLE_CHOICE\",\n \"TRUE_FALSE\",\n \"SHORT_ANSWER\",\n \"LONG_ANSWER\",\n \"MATH_EXPRESSION\",\n \"ESSAY\",\n ]),\n question: z.string(),\n answer: z.string(),\n options: z\n .array(\n z.object({\n id: z.string(),\n text: z.string(),\n isCorrect: z.boolean(),\n })\n )\n .optional()\n .default([]),\n markScheme: z\n .array(\n z.object({\n id: z.string(),\n points: z.number().transform((value) => Math.round(value)),\n description: z.string(),\n })\n )\n .optional()\n .default([]),\n points: z.number().optional().default(0),\n order: z.number().transform((value) => Math.round(value)),\n })\n )\n .default([]),\n});\n\ntype WorksheetDraft = z.infer<typeof worksheetDraftSchema>;\n\nconst schemaInstructions = `Return ONLY valid JSON matching this schema:\n{\n \"title\": string,\n \"questions\": [\n {\n \"type\": \"MULTIPLE_CHOICE\" | \"TRUE_FALSE\" | \"SHORT_ANSWER\" | \"LONG_ANSWER\" | \"MATH_EXPRESSION\" | \"ESSAY\",\n \"question\": string,\n \"answer\": string,\n \"options\": [ { \"id\": string, \"text\": string, \"isCorrect\": boolean } ],\n \"markScheme\": [ { \"id\": string, \"points\": number, \"description\": string } ],\n \"points\": number,\n \"order\": number\n }\n ]\n}\nRules: \"questions\" is an array ([] if none). Non-MCQ \"options\" can be []. Use \"order\" 1,2,3,… in reading order. No markdown or extra keys.\nIf no answer is provided for a multiple-choice or true/false question, determine the answer from the options.`;\n\n/**\n * Sends a PDF URL to the model for remote retrieval, extracts a worksheet via\n * structured inference, and inserts worksheet + questions in one DB transaction.\n */\nexport async function pdfToWorksheet(gcsUrl: string, classId: string) {\n const sanitizedUrl = sanitizeUrlForLogs(gcsUrl);\n\n if (!isProbablyGoogleCloudUrl(gcsUrl)) {\n logger.warn(\"pdfToWorksheet: rejected unsupported PDF URL\", {\n sanitizedUrl,\n });\n throw new Error(\"Only Google Cloud Storage URLs are supported\");\n }\n\n const filename = getFilenameFromUrl(gcsUrl);\n const prompt = `Read the attached PDF and convert it into a single worksheet JSON object.\\n\\n${schemaInstructions}`;\n\n // Force Responses API input types so we can pass a remote PDF via `file_url`.\n const input: ResponseInput = [\n {\n role: \"user\",\n content: [\n { type: \"input_text\", text: prompt },\n { type: \"input_file\", file_url: gcsUrl },\n ],\n },\n ];\n\n const draft = await inference<WorksheetDraft>(\n input,\n worksheetDraftSchema,\n {\n reason: 'pdf_to_worksheet_draft',\n metadata: {\n classId,\n filename,\n },\n }\n );\n\n if (!draft) {\n logger.warn(\"pdfToWorksheet: no model output\", { filename });\n return null;\n }\n\n return prisma.$transaction(async (tx) => {\n const worksheet = await tx.worksheet.create({\n data: { classId, name: draft.title },\n });\n\n if (draft.questions.length > 0) {\n await tx.worksheetQuestion.createMany({\n data: draft.questions.map((question) => ({\n worksheetId: worksheet.id,\n type: question.type,\n question: question.question,\n answer: question.answer,\n points: Math.round(question.points ?? 0),\n options: question.options ?? [],\n markScheme: (question.markScheme ?? []).map((item) => ({\n ...item,\n points: Math.round(item.points),\n })),\n order: Math.round(question.order),\n })),\n });\n }\n\n return worksheet;\n });\n}\n"],"names":[],"mappings":"AAAA;;;GAGG;;;AACH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC1C,OAAO,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAClD,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAE5C,4DAA4D;AAC5D,SAAS,wBAAwB,CAAC,GAAW;IAC3C,IAAI,CAAC;QACH,MAAM,IAAI,GAAG,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC,QAAQ,CAAC;QACnC,OAAO,CACL,IAAI,CAAC,QAAQ,CAAC,wBAAwB,CAAC;YACvC,IAAI,CAAC,QAAQ,CAAC,uBAAuB,CAAC;YACtC,IAAI,CAAC,QAAQ,CAAC,0BAA0B,CAAC,CAC1C,CAAC;IACJ,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC;AAED,SAAS,kBAAkB,CAAC,GAAW;IACrC,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC;QAC5B,MAAM,WAAW,GAAG,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC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