@goscribe/server 1.3.4 → 1.5.0
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/.env.example +12 -0
- package/.vscode/settings.json +3 -0
- package/REFACTOR_NOTES.md +60 -0
- package/TESTING_PROMPT.md +225 -0
- package/dist/controllers/admin.controller.d.ts +715 -0
- package/dist/controllers/admin.controller.js +9 -0
- package/dist/controllers/annotations.controller.d.ts +439 -0
- package/dist/controllers/annotations.controller.js +9 -0
- package/dist/controllers/app-router.controller.d.ts +3011 -0
- package/dist/controllers/app-router.controller.js +38 -0
- package/dist/controllers/app-router.controller.test.d.ts +1 -0
- package/dist/controllers/app-router.controller.test.js +36 -0
- package/dist/controllers/auth.controller.d.ts +323 -0
- package/dist/controllers/auth.controller.js +9 -0
- package/dist/controllers/base.controller.d.ts +4 -0
- package/dist/controllers/base.controller.js +5 -0
- package/dist/controllers/chat.controller.d.ts +341 -0
- package/dist/controllers/chat.controller.js +9 -0
- package/dist/controllers/copilot.controller.d.ts +397 -0
- package/dist/controllers/copilot.controller.js +9 -0
- package/dist/controllers/flashcards.controller.d.ts +651 -0
- package/dist/controllers/flashcards.controller.js +9 -0
- package/dist/controllers/members.controller.d.ts +339 -0
- package/dist/controllers/members.controller.js +9 -0
- package/dist/controllers/notifications.controller.d.ts +199 -0
- package/dist/controllers/notifications.controller.js +9 -0
- package/dist/controllers/payment.controller.d.ts +181 -0
- package/dist/controllers/payment.controller.js +9 -0
- package/dist/controllers/podcast.controller.d.ts +575 -0
- package/dist/controllers/podcast.controller.js +9 -0
- package/dist/controllers/router-module.controller.d.ts +5 -0
- package/dist/controllers/router-module.controller.js +6 -0
- package/dist/controllers/studyguide.controller.d.ts +73 -0
- package/dist/controllers/studyguide.controller.js +9 -0
- package/dist/controllers/worksheets.controller.d.ts +829 -0
- package/dist/controllers/worksheets.controller.js +9 -0
- package/dist/controllers/workspace.controller.d.ts +1207 -0
- package/dist/controllers/workspace.controller.js +9 -0
- package/dist/lib/activity_human_description.test.js +16 -15
- package/dist/lib/activity_log_service.test.js +28 -23
- package/dist/lib/ai/config.d.ts +20 -0
- package/dist/lib/ai/config.js +31 -0
- package/dist/lib/ai/embedding-client.d.ts +8 -0
- package/dist/lib/ai/embedding-client.js +30 -0
- package/dist/lib/ai/index.d.ts +47 -0
- package/dist/lib/ai/index.js +28 -0
- package/dist/lib/ai/inference-backend/client.d.ts +28 -0
- package/dist/lib/ai/inference-backend/client.js +301 -0
- package/dist/lib/ai/inference-backend/mocks.d.ts +12 -0
- package/dist/lib/ai/inference-backend/mocks.js +133 -0
- package/dist/lib/ai/inference-backend/types.d.ts +44 -0
- package/dist/lib/ai/inference-backend/types.js +1 -0
- package/dist/lib/ai/json-parse.d.ts +2 -0
- package/dist/lib/ai/json-parse.js +34 -0
- package/dist/lib/ai/llm-client.d.ts +6 -0
- package/dist/lib/ai/llm-client.js +19 -0
- package/dist/lib/ai/mock.d.ts +2 -0
- package/dist/lib/ai/mock.js +10 -0
- package/dist/lib/ai/types.d.ts +9 -0
- package/dist/lib/ai/types.js +1 -0
- package/dist/lib/chunking.d.ts +19 -0
- package/dist/lib/chunking.js +47 -0
- package/dist/lib/curated-kb-seed.d.ts +12 -0
- package/dist/lib/curated-kb-seed.js +155 -0
- package/dist/lib/email.js +67 -108
- package/dist/lib/embeddings.d.ts +2 -0
- package/dist/lib/embeddings.js +1 -0
- package/dist/lib/ensure-curated-kb-catalog.d.ts +6 -0
- package/dist/lib/ensure-curated-kb-catalog.js +53 -0
- package/dist/lib/env.d.ts +1 -5
- package/dist/lib/env.js +2 -7
- package/dist/lib/inference.d.ts +1 -8
- package/dist/lib/inference.js +1 -19
- package/dist/lib/kb-meta.d.ts +8 -0
- package/dist/lib/kb-meta.js +77 -0
- package/dist/lib/note-text.d.ts +1 -0
- package/dist/lib/note-text.js +47 -0
- package/dist/lib/notification-service.test.js +37 -36
- package/dist/lib/pdf.d.ts +11 -0
- package/dist/lib/pdf.js +11 -0
- package/dist/lib/usage_service.d.ts +2 -1
- package/dist/lib/usage_service.js +30 -12
- package/dist/lib/worksheet-generation.js +4 -4
- package/dist/lib/worksheet-generation.test.js +32 -17
- package/dist/lib/workspace-kb.d.ts +5 -0
- package/dist/lib/workspace-kb.js +7 -0
- package/dist/models/controller-context.model.d.ts +8 -0
- package/dist/models/controller-context.model.js +1 -0
- package/dist/repositories/artifact.repository.d.ts +60 -0
- package/dist/repositories/artifact.repository.js +40 -0
- package/dist/repositories/base.repository.d.ts +14 -0
- package/dist/repositories/base.repository.js +14 -0
- package/dist/repositories/invitation.repository.d.ts +94 -0
- package/dist/repositories/invitation.repository.js +44 -0
- package/dist/repositories/notification.repository.d.ts +72 -0
- package/dist/repositories/notification.repository.js +44 -0
- package/dist/repositories/router-module.repository.d.ts +10 -0
- package/dist/repositories/router-module.repository.js +14 -0
- package/dist/repositories/user.repository.d.ts +74 -0
- package/dist/repositories/user.repository.js +37 -0
- package/dist/repositories/workspace-member.repository.d.ts +31 -0
- package/dist/repositories/workspace-member.repository.js +31 -0
- package/dist/repositories/workspace.repository.d.ts +97 -0
- package/dist/repositories/workspace.repository.js +79 -0
- package/dist/routers/_app.d.ts +528 -33
- package/dist/routers/_app.js +4 -0
- package/dist/routers/admin.d.ts +0 -4
- package/dist/routers/admin.js +21 -549
- package/dist/routers/annotations.js +12 -170
- package/dist/routers/artifactVersions.d.ts +65 -0
- package/dist/routers/artifactVersions.js +14 -0
- package/dist/routers/auth.d.ts +0 -6
- package/dist/routers/auth.js +36 -421
- package/dist/routers/chat.js +15 -229
- package/dist/routers/copilot.d.ts +14 -13
- package/dist/routers/copilot.js +13 -532
- package/dist/routers/flashcards.d.ts +5 -5
- package/dist/routers/flashcards.js +23 -349
- package/dist/routers/knowledgeBase.d.ts +421 -0
- package/dist/routers/knowledgeBase.js +118 -0
- package/dist/routers/members.d.ts +0 -41
- package/dist/routers/members.js +22 -710
- package/dist/routers/notes.d.ts +94 -0
- package/dist/routers/notes.js +37 -0
- package/dist/routers/notifications.js +7 -109
- package/dist/routers/payment.d.ts +3 -2
- package/dist/routers/payment.js +11 -393
- package/dist/routers/podcast.d.ts +1 -1
- package/dist/routers/podcast.js +11 -784
- package/dist/routers/studyguide.js +3 -129
- package/dist/routers/worksheets.d.ts +29 -14
- package/dist/routers/worksheets.js +49 -628
- package/dist/routers/workspace.d.ts +0 -4
- package/dist/routers/workspace.js +28 -922
- package/dist/scripts/purge-deleted-users.js +2 -2
- package/dist/server.js +10 -3
- package/dist/services/activity/activity-human-description.service.d.ts +13 -0
- package/dist/services/activity/activity-human-description.service.js +221 -0
- package/dist/services/activity/activity-human-description.service.test.d.ts +1 -0
- package/dist/services/activity/activity-human-description.service.test.js +16 -0
- package/dist/services/activity/activity-log.service.d.ts +87 -0
- package/dist/services/activity/activity-log.service.js +276 -0
- package/dist/services/activity/activity-log.service.test.d.ts +1 -0
- package/dist/services/activity/activity-log.service.test.js +27 -0
- package/dist/services/activity-human-description.service.d.ts +13 -0
- package/dist/services/activity-human-description.service.js +221 -0
- package/dist/services/activity-human-description.service.test.d.ts +1 -0
- package/dist/services/activity-human-description.service.test.js +16 -0
- package/dist/services/activity-log.service.d.ts +87 -0
- package/dist/services/activity-log.service.js +276 -0
- package/dist/services/activity-log.service.test.d.ts +1 -0
- package/dist/services/activity-log.service.test.js +27 -0
- package/dist/services/admin/admin.service.d.ts +270 -0
- package/dist/services/admin/admin.service.js +476 -0
- package/dist/services/admin.service.d.ts +270 -0
- package/dist/services/admin.service.js +476 -0
- package/dist/services/ai/ai-session.service.d.ts +5 -0
- package/dist/services/ai/ai-session.service.js +4 -0
- package/dist/services/ai-session.service.d.ts +60 -0
- package/dist/services/ai-session.service.js +561 -0
- package/dist/services/annotation.service.d.ts +177 -0
- package/dist/services/annotation.service.js +154 -0
- package/dist/services/artifact-notification.service.d.ts +14 -0
- package/dist/services/artifact-notification.service.js +20 -0
- package/dist/services/artifact-version.service.d.ts +38 -0
- package/dist/services/artifact-version.service.js +129 -0
- package/dist/services/artifacts/annotation.service.d.ts +177 -0
- package/dist/services/artifacts/annotation.service.js +154 -0
- package/dist/services/artifacts/artifact-version.service.d.ts +38 -0
- package/dist/services/artifacts/artifact-version.service.js +129 -0
- package/dist/services/artifacts/chat.service.d.ts +127 -0
- package/dist/services/artifacts/chat.service.js +182 -0
- package/dist/services/artifacts/study-guide.service.d.ts +18 -0
- package/dist/services/artifacts/study-guide.service.js +65 -0
- package/dist/services/auth/auth.service.d.ts +94 -0
- package/dist/services/auth/auth.service.js +368 -0
- package/dist/services/auth.service.d.ts +94 -0
- package/dist/services/auth.service.js +368 -0
- package/dist/services/base.service.d.ts +14 -0
- package/dist/services/base.service.js +14 -0
- package/dist/services/billing/payment.service.d.ts +55 -0
- package/dist/services/billing/payment.service.js +368 -0
- package/dist/services/billing/subscription.service.d.ts +37 -0
- package/dist/services/billing/subscription.service.js +654 -0
- package/dist/services/billing/usage.service.d.ts +27 -0
- package/dist/services/billing/usage.service.js +77 -0
- package/dist/services/chat.service.d.ts +127 -0
- package/dist/services/chat.service.js +182 -0
- package/dist/services/content/copilot.service.d.ts +113 -0
- package/dist/services/content/copilot.service.js +453 -0
- package/dist/services/content/flashcard-progress.service.d.ts +159 -0
- package/dist/services/content/flashcard-progress.service.js +432 -0
- package/dist/services/content/flashcard.service.d.ts +140 -0
- package/dist/services/content/flashcard.service.js +326 -0
- package/dist/services/content/media-analysis.service.d.ts +23 -0
- package/dist/services/content/media-analysis.service.js +404 -0
- package/dist/services/content/podcast.service.d.ts +267 -0
- package/dist/services/content/podcast.service.js +653 -0
- package/dist/services/content/worksheet-content.service.d.ts +37 -0
- package/dist/services/content/worksheet-content.service.js +84 -0
- package/dist/services/content/worksheet-content.service.test.d.ts +1 -0
- package/dist/services/content/worksheet-content.service.test.js +69 -0
- package/dist/services/content/worksheet-generation.service.d.ts +91 -0
- package/dist/services/content/worksheet-generation.service.js +95 -0
- package/dist/services/content/worksheet-generation.service.test.d.ts +1 -0
- package/dist/services/content/worksheet-generation.service.test.js +20 -0
- package/dist/services/content/worksheet.service.d.ts +347 -0
- package/dist/services/content/worksheet.service.js +599 -0
- package/dist/services/copilot.service.d.ts +116 -0
- package/dist/services/copilot.service.js +447 -0
- package/dist/services/flashcard-progress.service.d.ts +2 -2
- package/dist/services/flashcard-progress.service.js +3 -2
- package/dist/services/flashcard.service.d.ts +140 -0
- package/dist/services/flashcard.service.js +325 -0
- package/dist/services/invitation.service.d.ts +66 -0
- package/dist/services/invitation.service.js +348 -0
- package/dist/services/knowledge/knowledge-base.service.d.ts +316 -0
- package/dist/services/knowledge/knowledge-base.service.js +544 -0
- package/dist/services/knowledge-base.service.d.ts +316 -0
- package/dist/services/knowledge-base.service.js +536 -0
- package/dist/services/media-analysis.service.d.ts +23 -0
- package/dist/services/media-analysis.service.js +384 -0
- package/dist/services/member.service.d.ts +36 -0
- package/dist/services/member.service.js +193 -0
- package/dist/services/members/invitation.service.d.ts +66 -0
- package/dist/services/members/invitation.service.js +348 -0
- package/dist/services/members/member.service.d.ts +36 -0
- package/dist/services/members/member.service.js +193 -0
- package/dist/services/note.service.d.ts +55 -0
- package/dist/services/note.service.js +111 -0
- package/dist/services/notification.service.d.ts +214 -0
- package/dist/services/notification.service.js +550 -0
- package/dist/services/notification.service.test.d.ts +1 -0
- package/dist/services/notification.service.test.js +87 -0
- package/dist/services/notifications/notification.service.d.ts +214 -0
- package/dist/services/notifications/notification.service.js +550 -0
- package/dist/services/notifications/notification.service.test.d.ts +1 -0
- package/dist/services/notifications/notification.service.test.js +87 -0
- package/dist/services/payment.service.d.ts +55 -0
- package/dist/services/payment.service.js +368 -0
- package/dist/services/podcast.service.d.ts +267 -0
- package/dist/services/podcast.service.js +654 -0
- package/dist/services/router-module.service.d.ts +7 -0
- package/dist/services/router-module.service.js +10 -0
- package/dist/services/study-guide.service.d.ts +18 -0
- package/dist/services/study-guide.service.js +65 -0
- package/dist/services/subscription.service.d.ts +37 -0
- package/dist/services/subscription.service.js +654 -0
- package/dist/services/usage-limit-policy.service.d.ts +12 -0
- package/dist/services/usage-limit-policy.service.js +22 -0
- package/dist/services/usage-limit-policy.service.test.d.ts +1 -0
- package/dist/services/usage-limit-policy.service.test.js +46 -0
- package/dist/services/usage.service.d.ts +27 -0
- package/dist/services/usage.service.js +77 -0
- package/dist/services/worksheet-content.service.d.ts +42 -0
- package/dist/services/worksheet-content.service.js +84 -0
- package/dist/services/worksheet-content.service.test.d.ts +1 -0
- package/dist/services/worksheet-content.service.test.js +69 -0
- package/dist/services/worksheet-generation.service.d.ts +91 -0
- package/dist/services/worksheet-generation.service.js +95 -0
- package/dist/services/worksheet-generation.service.test.d.ts +1 -0
- package/dist/services/worksheet-generation.service.test.js +20 -0
- package/dist/services/worksheet.service.d.ts +385 -0
- package/dist/services/worksheet.service.js +596 -0
- package/dist/services/workspace/workspace-analytics.service.d.ts +24 -0
- package/dist/services/workspace/workspace-analytics.service.js +95 -0
- package/dist/services/workspace/workspace-kb.service.d.ts +40 -0
- package/dist/services/workspace/workspace-kb.service.js +184 -0
- package/dist/services/workspace/workspace.service.d.ts +307 -0
- package/dist/services/workspace/workspace.service.js +394 -0
- package/dist/services/workspace-analytics.service.d.ts +24 -0
- package/dist/services/workspace-analytics.service.js +95 -0
- package/dist/services/workspace-kb.service.d.ts +40 -0
- package/dist/services/workspace-kb.service.js +184 -0
- package/dist/services/workspace-progress.service.d.ts +27 -0
- package/dist/services/workspace-progress.service.js +56 -0
- package/dist/services/workspace-progress.service.test.d.ts +1 -0
- package/dist/services/workspace-progress.service.test.js +49 -0
- package/dist/services/workspace.service.d.ts +307 -0
- package/dist/services/workspace.service.js +390 -0
- package/dist/trpc.js +2 -2
- package/package.json +5 -6
- package/prisma/migrations/20260509000001_add_knowledge_base/migration.sql +99 -0
- package/prisma/migrations/20260509000002_curate_knowledge_base/migration.sql +52 -0
- package/prisma/migrations/20260522000000_add_notes/migration.sql +27 -0
- package/prisma/migrations/20260524000000_remove_notes/migration.sql +3 -0
- package/prisma/schema.prisma +150 -48
- package/prisma/seed.mjs +67 -0
- package/scripts/debug/README.md +4 -0
- package/src/README.md +63 -0
- package/src/lib/ai/config.ts +34 -0
- package/src/lib/ai/embedding-client.ts +47 -0
- package/src/lib/ai/index.ts +62 -0
- package/src/lib/ai/inference-backend/client.ts +479 -0
- package/src/lib/ai/inference-backend/mocks.ts +171 -0
- package/src/lib/ai/inference-backend/types.ts +50 -0
- package/src/lib/ai/json-parse.ts +35 -0
- package/src/lib/ai/llm-client.ts +31 -0
- package/src/lib/ai/mock.ts +12 -0
- package/src/lib/ai/types.ts +11 -0
- package/src/lib/chunking.ts +81 -0
- package/src/lib/curated-kb-seed.ts +164 -0
- package/src/lib/email.ts +77 -115
- package/src/lib/embeddings.ts +9 -0
- package/src/lib/ensure-curated-kb-catalog.ts +60 -0
- package/src/lib/env.ts +2 -7
- package/src/lib/inference.ts +1 -21
- package/src/lib/kb-meta.ts +81 -0
- package/src/lib/pdf.ts +23 -0
- package/src/lib/workspace-kb.ts +7 -0
- package/src/repositories/artifact.repository.ts +55 -0
- package/src/repositories/base.repository.ts +19 -0
- package/src/repositories/invitation.repository.ts +53 -0
- package/src/repositories/notification.repository.ts +53 -0
- package/src/repositories/user.repository.ts +44 -0
- package/src/repositories/workspace-member.repository.ts +38 -0
- package/src/repositories/workspace.repository.ts +89 -0
- package/src/routers/_app.ts +4 -0
- package/src/routers/admin.ts +124 -692
- package/src/routers/annotations.ts +25 -203
- package/src/routers/artifactVersions.ts +32 -0
- package/src/routers/auth.ts +81 -519
- package/src/routers/chat.ts +42 -245
- package/src/routers/copilot.ts +41 -666
- package/src/routers/flashcards.ts +108 -404
- package/src/routers/knowledgeBase.ts +216 -0
- package/src/routers/members.ts +60 -782
- package/src/routers/notifications.ts +15 -117
- package/src/routers/payment.ts +37 -446
- package/src/routers/podcast.ts +36 -898
- package/src/routers/studyguide.ts +5 -144
- package/src/routers/worksheets.ts +171 -735
- package/src/routers/workspace.ts +138 -1109
- package/src/scripts/purge-deleted-users.ts +2 -2
- package/src/server.ts +10 -3
- package/src/{lib/activity_human_description.test.ts → services/activity/activity-human-description.service.test.ts} +1 -1
- package/src/{lib/activity_log_service.test.ts → services/activity/activity-log.service.test.ts} +1 -1
- package/src/{lib/activity_log_service.ts → services/activity/activity-log.service.ts} +2 -2
- package/src/services/admin/admin.service.ts +612 -0
- package/src/services/ai/ai-session.service.ts +5 -0
- package/src/services/artifacts/annotation.service.ts +189 -0
- package/src/services/artifacts/artifact-version.service.ts +151 -0
- package/src/services/artifacts/chat.service.ts +197 -0
- package/src/services/artifacts/study-guide.service.ts +72 -0
- package/src/services/auth/auth.service.ts +473 -0
- package/src/services/base.service.ts +19 -0
- package/src/services/billing/payment.service.ts +436 -0
- package/src/{lib/subscription_service.ts → services/billing/subscription.service.ts} +5 -5
- package/src/{lib/usage_service.ts → services/billing/usage.service.ts} +32 -12
- package/src/services/content/copilot.service.ts +596 -0
- package/src/services/{flashcard-progress.service.ts → content/flashcard-progress.service.ts} +6 -3
- package/src/services/content/flashcard.service.ts +394 -0
- package/src/services/content/media-analysis.service.ts +556 -0
- package/src/services/content/podcast.service.ts +777 -0
- package/src/services/content/worksheet-content.service.test.ts +83 -0
- package/src/services/content/worksheet-content.service.ts +117 -0
- package/src/{lib/worksheet-generation.test.ts → services/content/worksheet-generation.service.test.ts} +1 -1
- package/src/services/content/worksheet.service.ts +751 -0
- package/src/services/knowledge/knowledge-base.service.ts +705 -0
- package/src/services/members/invitation.service.ts +427 -0
- package/src/services/members/member.service.ts +241 -0
- package/src/{lib/notification-service.test.ts → services/notifications/notification.service.test.ts} +2 -2
- package/src/{lib/notification-service.ts → services/notifications/notification.service.ts} +102 -1
- package/src/services/workspace/workspace-analytics.service.ts +107 -0
- package/src/services/workspace/workspace-kb.service.ts +273 -0
- package/src/services/workspace/workspace.service.ts +481 -0
- package/src/trpc.ts +2 -2
- package/src/lib/ai-session.ts +0 -704
- package/src/lib/workspace-access.ts +0 -13
- /package/{check-difficulty.cjs → scripts/debug/check-difficulty.cjs} +0 -0
- /package/{check-questions.cjs → scripts/debug/check-questions.cjs} +0 -0
- /package/{db-summary.cjs → scripts/debug/db-summary.cjs} +0 -0
- /package/{mcq-test.cjs → scripts/debug/mcq-test.cjs} +0 -0
- /package/{test-generate.js → scripts/debug/test-generate.js} +0 -0
- /package/{test-ratio.cjs → scripts/debug/test-ratio.cjs} +0 -0
- /package/{zod-test.cjs → scripts/debug/zod-test.cjs} +0 -0
- /package/src/{lib/activity_human_description.ts → services/activity/activity-human-description.service.ts} +0 -0
- /package/src/{lib/worksheet-generation.ts → services/content/worksheet-generation.service.ts} +0 -0
|
@@ -0,0 +1,751 @@
|
|
|
1
|
+
import type { PrismaClient } from '@prisma/client';
|
|
2
|
+
import { TRPCError } from '@trpc/server';
|
|
3
|
+
import { BaseService } from '../base.service.js';
|
|
4
|
+
import { ArtifactType } from '../../lib/constants.js';
|
|
5
|
+
import { workspaceAccessWhere } from '../../repositories/workspace.repository.js';
|
|
6
|
+
import PusherService from '../../lib/pusher.js';
|
|
7
|
+
import { ai } from '../../lib/ai/index.js';
|
|
8
|
+
import { workspaceKbService } from '../workspace/workspace-kb.service.js';
|
|
9
|
+
import { notifyArtifactFailed, notifyArtifactReady } from '../notifications/notification.service.js';
|
|
10
|
+
import {
|
|
11
|
+
mergeWorksheetGenerationConfig,
|
|
12
|
+
normalizeWorksheetProblemForDb,
|
|
13
|
+
parsePresetConfig,
|
|
14
|
+
type WorksheetMode,
|
|
15
|
+
type WorksheetPresetConfig,
|
|
16
|
+
type WorksheetDifficultyInput,
|
|
17
|
+
} from './worksheet-generation.service.js';
|
|
18
|
+
import { mergeWorksheetQuestionProgress } from './worksheet-content.service.js';
|
|
19
|
+
|
|
20
|
+
const Difficulty = { EASY: 'EASY', MEDIUM: 'MEDIUM', HARD: 'HARD' } as const;
|
|
21
|
+
const QuestionType = {
|
|
22
|
+
MULTIPLE_CHOICE: 'MULTIPLE_CHOICE',
|
|
23
|
+
TEXT: 'TEXT',
|
|
24
|
+
NUMERIC: 'NUMERIC',
|
|
25
|
+
TRUE_FALSE: 'TRUE_FALSE',
|
|
26
|
+
MATCHING: 'MATCHING',
|
|
27
|
+
FILL_IN_THE_BLANK: 'FILL_IN_THE_BLANK',
|
|
28
|
+
} as const;
|
|
29
|
+
|
|
30
|
+
type DifficultyUpper = 'EASY' | 'MEDIUM' | 'HARD';
|
|
31
|
+
|
|
32
|
+
export interface CreateWorksheetInput {
|
|
33
|
+
workspaceId: string;
|
|
34
|
+
title: string;
|
|
35
|
+
description?: string;
|
|
36
|
+
difficulty?: DifficultyUpper;
|
|
37
|
+
estimatedTime?: string;
|
|
38
|
+
problems?: Array<{
|
|
39
|
+
question: string;
|
|
40
|
+
answer: string;
|
|
41
|
+
type: keyof typeof QuestionType;
|
|
42
|
+
options?: string[];
|
|
43
|
+
}>;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface UpdateWorksheetInput {
|
|
47
|
+
id: string;
|
|
48
|
+
title?: string;
|
|
49
|
+
description?: string;
|
|
50
|
+
difficulty?: DifficultyUpper;
|
|
51
|
+
estimatedTime?: string;
|
|
52
|
+
problems?: Array<{
|
|
53
|
+
id?: string;
|
|
54
|
+
question: string;
|
|
55
|
+
answer: string;
|
|
56
|
+
type: keyof typeof QuestionType;
|
|
57
|
+
options?: string[];
|
|
58
|
+
}>;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export interface GenerateWorksheetInput {
|
|
62
|
+
workspaceId: string;
|
|
63
|
+
prompt: string;
|
|
64
|
+
numQuestions: number;
|
|
65
|
+
difficulty: WorksheetDifficultyInput;
|
|
66
|
+
mode?: WorksheetMode;
|
|
67
|
+
presetId?: string;
|
|
68
|
+
configOverride?: Partial<WorksheetPresetConfig>;
|
|
69
|
+
title?: string;
|
|
70
|
+
estimatedTime?: string;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export class WorksheetService extends BaseService {
|
|
74
|
+
constructor(db: PrismaClient) {
|
|
75
|
+
super(db);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async list(userId: string, workspaceId: string) {
|
|
79
|
+
const worksheets = await this.db.artifact.findMany({
|
|
80
|
+
where: { workspaceId, type: ArtifactType.WORKSHEET },
|
|
81
|
+
include: {
|
|
82
|
+
versions: { orderBy: { version: 'desc' }, take: 1 },
|
|
83
|
+
questions: true,
|
|
84
|
+
},
|
|
85
|
+
orderBy: { updatedAt: 'desc' },
|
|
86
|
+
});
|
|
87
|
+
if (!worksheets) throw new TRPCError({ code: 'NOT_FOUND' });
|
|
88
|
+
|
|
89
|
+
const allQuestionIds = worksheets.flatMap((w) => w.questions.map((q) => q.id));
|
|
90
|
+
if (allQuestionIds.length === 0) return worksheets;
|
|
91
|
+
|
|
92
|
+
const progress = await this.db.worksheetQuestionProgress.findMany({
|
|
93
|
+
where: { userId, worksheetQuestionId: { in: allQuestionIds } },
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
return worksheets.map((w) => ({
|
|
97
|
+
...w,
|
|
98
|
+
questions: mergeWorksheetQuestionProgress(w.questions, progress),
|
|
99
|
+
}));
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async listPresets(userId: string, workspaceId: string) {
|
|
103
|
+
const ws = await this.db.workspace.findFirst({
|
|
104
|
+
where: { id: workspaceId, ...workspaceAccessWhere(userId) },
|
|
105
|
+
});
|
|
106
|
+
if (!ws) throw new TRPCError({ code: 'NOT_FOUND' });
|
|
107
|
+
|
|
108
|
+
return this.db.worksheetPreset.findMany({
|
|
109
|
+
where: {
|
|
110
|
+
OR: [
|
|
111
|
+
{ isSystem: true },
|
|
112
|
+
{
|
|
113
|
+
userId,
|
|
114
|
+
OR: [{ workspaceId }, { workspaceId: null }],
|
|
115
|
+
},
|
|
116
|
+
],
|
|
117
|
+
},
|
|
118
|
+
orderBy: [{ isSystem: 'desc' }, { name: 'asc' }],
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async createPreset(
|
|
123
|
+
userId: string,
|
|
124
|
+
input: { workspaceId?: string; name: string; config: Record<string, unknown> },
|
|
125
|
+
) {
|
|
126
|
+
if (input.workspaceId) {
|
|
127
|
+
const ws = await this.db.workspace.findFirst({
|
|
128
|
+
where: { id: input.workspaceId, ...workspaceAccessWhere(userId) },
|
|
129
|
+
});
|
|
130
|
+
if (!ws) throw new TRPCError({ code: 'NOT_FOUND' });
|
|
131
|
+
}
|
|
132
|
+
const config = parsePresetConfig(input.config);
|
|
133
|
+
return this.db.worksheetPreset.create({
|
|
134
|
+
data: {
|
|
135
|
+
userId,
|
|
136
|
+
workspaceId: input.workspaceId ?? null,
|
|
137
|
+
name: input.name,
|
|
138
|
+
isSystem: false,
|
|
139
|
+
config: config as object,
|
|
140
|
+
},
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
async updatePreset(
|
|
145
|
+
userId: string,
|
|
146
|
+
input: { id: string; name?: string; config?: Record<string, unknown> },
|
|
147
|
+
) {
|
|
148
|
+
const existing = await this.db.worksheetPreset.findFirst({
|
|
149
|
+
where: { id: input.id, userId, isSystem: false },
|
|
150
|
+
});
|
|
151
|
+
if (!existing) {
|
|
152
|
+
throw new TRPCError({ code: 'NOT_FOUND', message: 'Preset not found or read-only' });
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const data: { name?: string; config?: object } = {};
|
|
156
|
+
if (input.name !== undefined) data.name = input.name;
|
|
157
|
+
if (input.config !== undefined) data.config = parsePresetConfig(input.config) as object;
|
|
158
|
+
|
|
159
|
+
return this.db.worksheetPreset.update({
|
|
160
|
+
where: { id: input.id },
|
|
161
|
+
data,
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
async deletePreset(userId: string, id: string) {
|
|
166
|
+
const existing = await this.db.worksheetPreset.findFirst({
|
|
167
|
+
where: { id, userId, isSystem: false },
|
|
168
|
+
});
|
|
169
|
+
if (!existing) {
|
|
170
|
+
throw new TRPCError({ code: 'NOT_FOUND', message: 'Preset not found or read-only' });
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
await this.db.worksheetPreset.delete({ where: { id } });
|
|
174
|
+
return true;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
async create(userId: string, input: CreateWorksheetInput) {
|
|
178
|
+
const workspace = await this.db.workspace.findFirst({
|
|
179
|
+
where: { id: input.workspaceId, ownerId: userId },
|
|
180
|
+
});
|
|
181
|
+
if (!workspace) throw new TRPCError({ code: 'NOT_FOUND' });
|
|
182
|
+
|
|
183
|
+
const { problems } = input;
|
|
184
|
+
|
|
185
|
+
return this.db.artifact.create({
|
|
186
|
+
data: {
|
|
187
|
+
workspaceId: input.workspaceId,
|
|
188
|
+
type: ArtifactType.WORKSHEET,
|
|
189
|
+
title: input.title,
|
|
190
|
+
difficulty: input.difficulty as any,
|
|
191
|
+
estimatedTime: input.estimatedTime,
|
|
192
|
+
createdById: userId,
|
|
193
|
+
questions: problems
|
|
194
|
+
? {
|
|
195
|
+
create: problems.map((problem, index) => ({
|
|
196
|
+
prompt: problem.question,
|
|
197
|
+
answer: problem.answer,
|
|
198
|
+
type: problem.type as any,
|
|
199
|
+
order: index,
|
|
200
|
+
meta: problem.options ? { options: problem.options } : undefined,
|
|
201
|
+
})),
|
|
202
|
+
}
|
|
203
|
+
: undefined,
|
|
204
|
+
},
|
|
205
|
+
include: { questions: true },
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
async get(userId: string, worksheetId: string) {
|
|
210
|
+
const worksheet = await this.db.artifact.findFirst({
|
|
211
|
+
where: {
|
|
212
|
+
id: worksheetId,
|
|
213
|
+
type: ArtifactType.WORKSHEET,
|
|
214
|
+
workspace: workspaceAccessWhere(userId),
|
|
215
|
+
},
|
|
216
|
+
include: { questions: true },
|
|
217
|
+
orderBy: { updatedAt: 'desc' },
|
|
218
|
+
});
|
|
219
|
+
if (!worksheet) throw new TRPCError({ code: 'NOT_FOUND' });
|
|
220
|
+
|
|
221
|
+
const questionIds = worksheet.questions.map((q) => q.id);
|
|
222
|
+
const progress =
|
|
223
|
+
questionIds.length === 0
|
|
224
|
+
? []
|
|
225
|
+
: await this.db.worksheetQuestionProgress.findMany({
|
|
226
|
+
where: { userId, worksheetQuestionId: { in: questionIds } },
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
return {
|
|
230
|
+
...worksheet,
|
|
231
|
+
questions: mergeWorksheetQuestionProgress(worksheet.questions, progress),
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
async createWorksheetQuestion(
|
|
236
|
+
userId: string,
|
|
237
|
+
input: {
|
|
238
|
+
worksheetId: string;
|
|
239
|
+
prompt: string;
|
|
240
|
+
answer?: string;
|
|
241
|
+
type?: keyof typeof QuestionType;
|
|
242
|
+
difficulty?: DifficultyUpper;
|
|
243
|
+
order?: number;
|
|
244
|
+
meta?: Record<string, unknown>;
|
|
245
|
+
},
|
|
246
|
+
) {
|
|
247
|
+
const worksheet = await this.db.artifact.findFirst({
|
|
248
|
+
where: {
|
|
249
|
+
id: input.worksheetId,
|
|
250
|
+
type: ArtifactType.WORKSHEET,
|
|
251
|
+
workspace: workspaceAccessWhere(userId),
|
|
252
|
+
},
|
|
253
|
+
});
|
|
254
|
+
if (!worksheet) throw new TRPCError({ code: 'NOT_FOUND' });
|
|
255
|
+
return this.db.worksheetQuestion.create({
|
|
256
|
+
data: {
|
|
257
|
+
artifactId: input.worksheetId,
|
|
258
|
+
prompt: input.prompt,
|
|
259
|
+
answer: input.answer,
|
|
260
|
+
type: (input.type ?? QuestionType.TEXT) as any,
|
|
261
|
+
difficulty: (input.difficulty ?? Difficulty.MEDIUM) as any,
|
|
262
|
+
order: input.order ?? 0,
|
|
263
|
+
meta: input.meta as any,
|
|
264
|
+
},
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
async updateWorksheetQuestion(
|
|
269
|
+
userId: string,
|
|
270
|
+
input: {
|
|
271
|
+
worksheetQuestionId: string;
|
|
272
|
+
prompt?: string;
|
|
273
|
+
answer?: string;
|
|
274
|
+
type?: keyof typeof QuestionType;
|
|
275
|
+
difficulty?: DifficultyUpper;
|
|
276
|
+
order?: number;
|
|
277
|
+
meta?: Record<string, unknown>;
|
|
278
|
+
},
|
|
279
|
+
) {
|
|
280
|
+
const q = await this.db.worksheetQuestion.findFirst({
|
|
281
|
+
where: {
|
|
282
|
+
id: input.worksheetQuestionId,
|
|
283
|
+
artifact: {
|
|
284
|
+
type: ArtifactType.WORKSHEET,
|
|
285
|
+
workspace: workspaceAccessWhere(userId),
|
|
286
|
+
},
|
|
287
|
+
},
|
|
288
|
+
});
|
|
289
|
+
if (!q) throw new TRPCError({ code: 'NOT_FOUND' });
|
|
290
|
+
return this.db.worksheetQuestion.update({
|
|
291
|
+
where: { id: input.worksheetQuestionId },
|
|
292
|
+
data: {
|
|
293
|
+
prompt: input.prompt ?? q.prompt,
|
|
294
|
+
answer: input.answer ?? q.answer,
|
|
295
|
+
type: (input.type ?? q.type) as any,
|
|
296
|
+
difficulty: (input.difficulty ?? q.difficulty) as any,
|
|
297
|
+
order: input.order ?? q.order,
|
|
298
|
+
meta: (input.meta ?? q.meta) as any,
|
|
299
|
+
},
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
async deleteWorksheetQuestion(userId: string, worksheetQuestionId: string) {
|
|
304
|
+
const q = await this.db.worksheetQuestion.findFirst({
|
|
305
|
+
where: {
|
|
306
|
+
id: worksheetQuestionId,
|
|
307
|
+
artifact: { workspace: workspaceAccessWhere(userId) },
|
|
308
|
+
},
|
|
309
|
+
});
|
|
310
|
+
if (!q) throw new TRPCError({ code: 'NOT_FOUND' });
|
|
311
|
+
await this.db.worksheetQuestion.delete({ where: { id: worksheetQuestionId } });
|
|
312
|
+
return true;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
async updateProblemStatus(
|
|
316
|
+
userId: string,
|
|
317
|
+
input: { problemId: string; completed: boolean; answer?: string; correct?: boolean },
|
|
318
|
+
) {
|
|
319
|
+
const question = await this.db.worksheetQuestion.findFirst({
|
|
320
|
+
where: {
|
|
321
|
+
id: input.problemId,
|
|
322
|
+
artifact: {
|
|
323
|
+
type: ArtifactType.WORKSHEET,
|
|
324
|
+
workspace: workspaceAccessWhere(userId),
|
|
325
|
+
},
|
|
326
|
+
},
|
|
327
|
+
});
|
|
328
|
+
if (!question) throw new TRPCError({ code: 'NOT_FOUND' });
|
|
329
|
+
|
|
330
|
+
return this.db.worksheetQuestionProgress.upsert({
|
|
331
|
+
where: {
|
|
332
|
+
worksheetQuestionId_userId: {
|
|
333
|
+
worksheetQuestionId: input.problemId,
|
|
334
|
+
userId,
|
|
335
|
+
},
|
|
336
|
+
},
|
|
337
|
+
create: {
|
|
338
|
+
worksheetQuestionId: input.problemId,
|
|
339
|
+
userId,
|
|
340
|
+
modified: input.completed,
|
|
341
|
+
userAnswer: input.answer,
|
|
342
|
+
correct: input.correct,
|
|
343
|
+
completedAt: input.completed ? new Date() : null,
|
|
344
|
+
attempts: 1,
|
|
345
|
+
},
|
|
346
|
+
update: {
|
|
347
|
+
modified: input.completed,
|
|
348
|
+
userAnswer: input.answer,
|
|
349
|
+
completedAt: input.completed ? new Date() : null,
|
|
350
|
+
attempts: { increment: 1 },
|
|
351
|
+
correct: input.correct,
|
|
352
|
+
},
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
async getProgress(userId: string, worksheetId: string) {
|
|
357
|
+
const worksheet = await this.db.artifact.findFirst({
|
|
358
|
+
where: {
|
|
359
|
+
id: worksheetId,
|
|
360
|
+
type: ArtifactType.WORKSHEET,
|
|
361
|
+
workspace: workspaceAccessWhere(userId),
|
|
362
|
+
},
|
|
363
|
+
});
|
|
364
|
+
if (!worksheet) throw new TRPCError({ code: 'NOT_FOUND' });
|
|
365
|
+
|
|
366
|
+
const questions = await this.db.worksheetQuestion.findMany({
|
|
367
|
+
where: { artifactId: worksheetId },
|
|
368
|
+
select: { id: true },
|
|
369
|
+
});
|
|
370
|
+
const questionIds = questions.map((q) => q.id);
|
|
371
|
+
|
|
372
|
+
return this.db.worksheetQuestionProgress.findMany({
|
|
373
|
+
where: { userId, worksheetQuestionId: { in: questionIds } },
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
async update(userId: string, input: UpdateWorksheetInput) {
|
|
378
|
+
const { id, problems, ...updateData } = input;
|
|
379
|
+
|
|
380
|
+
const existingWorksheet = await this.db.artifact.findFirst({
|
|
381
|
+
where: {
|
|
382
|
+
id,
|
|
383
|
+
type: ArtifactType.WORKSHEET,
|
|
384
|
+
workspace: workspaceAccessWhere(userId),
|
|
385
|
+
},
|
|
386
|
+
});
|
|
387
|
+
if (!existingWorksheet) throw new TRPCError({ code: 'NOT_FOUND' });
|
|
388
|
+
|
|
389
|
+
if (problems) {
|
|
390
|
+
await this.db.worksheetQuestion.deleteMany({ where: { artifactId: id } });
|
|
391
|
+
|
|
392
|
+
await this.db.worksheetQuestion.createMany({
|
|
393
|
+
data: problems.map((problem, index) => ({
|
|
394
|
+
artifactId: id,
|
|
395
|
+
prompt: problem.question,
|
|
396
|
+
answer: problem.answer,
|
|
397
|
+
type: problem.type as any,
|
|
398
|
+
order: index,
|
|
399
|
+
meta: problem.options ? { options: problem.options } : undefined,
|
|
400
|
+
})),
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
const processedUpdateData = {
|
|
405
|
+
...updateData,
|
|
406
|
+
difficulty: updateData.difficulty as any,
|
|
407
|
+
};
|
|
408
|
+
|
|
409
|
+
return this.db.artifact.update({
|
|
410
|
+
where: { id },
|
|
411
|
+
data: processedUpdateData,
|
|
412
|
+
include: {
|
|
413
|
+
questions: { orderBy: { order: 'asc' } },
|
|
414
|
+
},
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
async delete(userId: string, id: string) {
|
|
419
|
+
const deleted = await this.db.artifact.deleteMany({
|
|
420
|
+
where: {
|
|
421
|
+
id,
|
|
422
|
+
type: ArtifactType.WORKSHEET,
|
|
423
|
+
workspace: workspaceAccessWhere(userId),
|
|
424
|
+
},
|
|
425
|
+
});
|
|
426
|
+
if (deleted.count === 0) throw new TRPCError({ code: 'NOT_FOUND' });
|
|
427
|
+
return true;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
async generateFromPrompt(userId: string, input: GenerateWorksheetInput) {
|
|
431
|
+
const workspace = await this.db.workspace.findFirst({
|
|
432
|
+
where: { id: input.workspaceId, ...workspaceAccessWhere(userId) },
|
|
433
|
+
});
|
|
434
|
+
if (!workspace) throw new TRPCError({ code: 'NOT_FOUND' });
|
|
435
|
+
|
|
436
|
+
let presetRow: Awaited<ReturnType<typeof this.db.worksheetPreset.findFirst>> = null;
|
|
437
|
+
if (input.presetId) {
|
|
438
|
+
presetRow = await this.db.worksheetPreset.findFirst({
|
|
439
|
+
where: {
|
|
440
|
+
id: input.presetId,
|
|
441
|
+
OR: [
|
|
442
|
+
{ isSystem: true },
|
|
443
|
+
{
|
|
444
|
+
userId,
|
|
445
|
+
OR: [{ workspaceId: input.workspaceId }, { workspaceId: null }],
|
|
446
|
+
},
|
|
447
|
+
],
|
|
448
|
+
},
|
|
449
|
+
});
|
|
450
|
+
if (!presetRow) throw new TRPCError({ code: 'NOT_FOUND', message: 'Preset not found' });
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
const presetConfig = presetRow?.config ? parsePresetConfig(presetRow.config) : undefined;
|
|
454
|
+
|
|
455
|
+
const resolved = mergeWorksheetGenerationConfig(presetConfig, input.configOverride ?? undefined, {
|
|
456
|
+
numQuestions: input.numQuestions,
|
|
457
|
+
difficulty: input.difficulty,
|
|
458
|
+
mode: input.mode,
|
|
459
|
+
});
|
|
460
|
+
|
|
461
|
+
const difficultyUpper = resolved.difficulty.toUpperCase() as DifficultyUpper;
|
|
462
|
+
|
|
463
|
+
console.log('[DEBUG TRPC PAYLOAD] input =', input);
|
|
464
|
+
console.log('[DEBUG TRPC PAYLOAD] presetConfig =', presetConfig);
|
|
465
|
+
console.log('[DEBUG TRPC PAYLOAD] legacy merged legacy values:', {
|
|
466
|
+
numQuestions: input.numQuestions,
|
|
467
|
+
difficulty: input.difficulty,
|
|
468
|
+
mode: input.mode,
|
|
469
|
+
});
|
|
470
|
+
console.log('[DEBUG TRPC PAYLOAD] RESOLVED =', resolved);
|
|
471
|
+
|
|
472
|
+
const worksheetConfigSnapshot = {
|
|
473
|
+
mode: resolved.mode,
|
|
474
|
+
presetId: input.presetId ?? null,
|
|
475
|
+
presetName: presetRow?.name ?? null,
|
|
476
|
+
numQuestions: resolved.numQuestions,
|
|
477
|
+
difficulty: resolved.difficulty,
|
|
478
|
+
mcqRatio: resolved.mcqRatio ?? null,
|
|
479
|
+
questionTypes: resolved.questionTypes ?? null,
|
|
480
|
+
};
|
|
481
|
+
|
|
482
|
+
await PusherService.emitWorksheetGenerationStart(input.workspaceId);
|
|
483
|
+
|
|
484
|
+
const artifact = await this.db.artifact.create({
|
|
485
|
+
data: {
|
|
486
|
+
workspaceId: input.workspaceId,
|
|
487
|
+
type: ArtifactType.WORKSHEET,
|
|
488
|
+
title:
|
|
489
|
+
input.title ||
|
|
490
|
+
(resolved.mode === 'quiz'
|
|
491
|
+
? `Quiz - ${new Date().toLocaleString()}`
|
|
492
|
+
: `Worksheet - ${new Date().toLocaleString()}`),
|
|
493
|
+
createdById: userId,
|
|
494
|
+
difficulty: difficultyUpper as any,
|
|
495
|
+
estimatedTime: input.estimatedTime,
|
|
496
|
+
generating: true,
|
|
497
|
+
generatingMetadata: {
|
|
498
|
+
quantity: resolved.numQuestions,
|
|
499
|
+
difficulty: resolved.difficulty,
|
|
500
|
+
mode: resolved.mode,
|
|
501
|
+
presetId: input.presetId ?? null,
|
|
502
|
+
},
|
|
503
|
+
worksheetConfig: worksheetConfigSnapshot as object,
|
|
504
|
+
},
|
|
505
|
+
});
|
|
506
|
+
|
|
507
|
+
await PusherService.emitTaskComplete(input.workspaceId, 'worksheet_info', {
|
|
508
|
+
contentLength: resolved.numQuestions,
|
|
509
|
+
});
|
|
510
|
+
await PusherService.emitWorksheetNew(input.workspaceId, artifact);
|
|
511
|
+
|
|
512
|
+
const workspaceId = input.workspaceId;
|
|
513
|
+
const promptText = input.prompt;
|
|
514
|
+
const estTime = input.estimatedTime;
|
|
515
|
+
|
|
516
|
+
(async () => {
|
|
517
|
+
try {
|
|
518
|
+
const ragContext = await workspaceKbService.retrieveContext(
|
|
519
|
+
workspaceId,
|
|
520
|
+
promptText ?? 'worksheet practice questions concepts problems',
|
|
521
|
+
8,
|
|
522
|
+
);
|
|
523
|
+
|
|
524
|
+
const content = await ai.backend.generateWorksheetQuestions(
|
|
525
|
+
workspaceId,
|
|
526
|
+
userId,
|
|
527
|
+
resolved.numQuestions,
|
|
528
|
+
difficultyUpper,
|
|
529
|
+
{
|
|
530
|
+
mode: resolved.mode,
|
|
531
|
+
mcqRatio: resolved.mcqRatio,
|
|
532
|
+
questionTypes: resolved.questionTypes,
|
|
533
|
+
prompt: promptText,
|
|
534
|
+
ragContext,
|
|
535
|
+
},
|
|
536
|
+
);
|
|
537
|
+
|
|
538
|
+
let problems: any[] = [];
|
|
539
|
+
try {
|
|
540
|
+
const worksheetData = JSON.parse(content);
|
|
541
|
+
let actualWorksheetData: any = worksheetData;
|
|
542
|
+
if (worksheetData.last_response) {
|
|
543
|
+
try {
|
|
544
|
+
actualWorksheetData = JSON.parse(worksheetData.last_response);
|
|
545
|
+
} catch {
|
|
546
|
+
/* noop */
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
problems =
|
|
550
|
+
actualWorksheetData.problems ||
|
|
551
|
+
actualWorksheetData.questions ||
|
|
552
|
+
actualWorksheetData ||
|
|
553
|
+
[];
|
|
554
|
+
if (!Array.isArray(problems)) problems = [];
|
|
555
|
+
} catch (parseError) {
|
|
556
|
+
this.logger.error('Failed to parse worksheet JSON', parseError);
|
|
557
|
+
throw new Error('Failed to parse worksheet JSON');
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
const forceMcq = resolved.mode === 'quiz';
|
|
561
|
+
const questionsToCreate = [];
|
|
562
|
+
for (let i = 0; i < Math.min(problems.length, resolved.numQuestions); i++) {
|
|
563
|
+
const problem =
|
|
564
|
+
problems[i] && typeof problems[i] === 'object'
|
|
565
|
+
? (problems[i] as Record<string, unknown>)
|
|
566
|
+
: {};
|
|
567
|
+
const row = normalizeWorksheetProblemForDb(problem, i, difficultyUpper, forceMcq);
|
|
568
|
+
const metaPayload: Record<string, unknown> = {};
|
|
569
|
+
if (row.meta.options?.length) metaPayload.options = row.meta.options;
|
|
570
|
+
if (row.meta.markScheme != null) metaPayload.markScheme = row.meta.markScheme;
|
|
571
|
+
|
|
572
|
+
questionsToCreate.push({
|
|
573
|
+
artifactId: artifact.id,
|
|
574
|
+
prompt: row.prompt,
|
|
575
|
+
answer: row.answer ?? '',
|
|
576
|
+
difficulty: row.difficulty as any,
|
|
577
|
+
order: row.order,
|
|
578
|
+
type: row.type as any,
|
|
579
|
+
meta: Object.keys(metaPayload).length ? (metaPayload as object) : undefined,
|
|
580
|
+
});
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
if (questionsToCreate.length > 0) {
|
|
584
|
+
await this.db.worksheetQuestion.createMany({ data: questionsToCreate });
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
let parsedTitle: string | undefined;
|
|
588
|
+
let parsedDescription: string | undefined;
|
|
589
|
+
let parsedEstimated: string | undefined;
|
|
590
|
+
try {
|
|
591
|
+
const worksheetData = JSON.parse(content);
|
|
592
|
+
let actualWorksheetData: any = worksheetData;
|
|
593
|
+
if (worksheetData.last_response) {
|
|
594
|
+
try {
|
|
595
|
+
actualWorksheetData = JSON.parse(worksheetData.last_response);
|
|
596
|
+
} catch {
|
|
597
|
+
/* noop */
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
parsedTitle =
|
|
601
|
+
typeof actualWorksheetData.title === 'string' ? actualWorksheetData.title : undefined;
|
|
602
|
+
parsedDescription =
|
|
603
|
+
typeof actualWorksheetData.description === 'string'
|
|
604
|
+
? actualWorksheetData.description
|
|
605
|
+
: undefined;
|
|
606
|
+
parsedEstimated =
|
|
607
|
+
typeof actualWorksheetData.estimatedTime === 'string'
|
|
608
|
+
? actualWorksheetData.estimatedTime
|
|
609
|
+
: undefined;
|
|
610
|
+
} catch {
|
|
611
|
+
/* noop */
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
await this.db.artifact.update({
|
|
615
|
+
where: { id: artifact.id },
|
|
616
|
+
data: {
|
|
617
|
+
generating: false,
|
|
618
|
+
title: parsedTitle || artifact.title,
|
|
619
|
+
description: parsedDescription ?? undefined,
|
|
620
|
+
estimatedTime: estTime || parsedEstimated || undefined,
|
|
621
|
+
worksheetConfig: worksheetConfigSnapshot as object,
|
|
622
|
+
},
|
|
623
|
+
});
|
|
624
|
+
|
|
625
|
+
const updatedWorksheet = await this.db.artifact.findFirst({
|
|
626
|
+
where: { id: artifact.id },
|
|
627
|
+
include: { questions: true },
|
|
628
|
+
});
|
|
629
|
+
|
|
630
|
+
await PusherService.emitWorksheetGenerationComplete(workspaceId, updatedWorksheet);
|
|
631
|
+
await PusherService.emitWorksheetComplete(workspaceId, artifact);
|
|
632
|
+
|
|
633
|
+
await notifyArtifactReady(this.db, {
|
|
634
|
+
userId,
|
|
635
|
+
workspaceId,
|
|
636
|
+
artifactId: artifact.id,
|
|
637
|
+
artifactType: ArtifactType.WORKSHEET,
|
|
638
|
+
title: updatedWorksheet?.title ?? artifact.title,
|
|
639
|
+
}).catch(() => {});
|
|
640
|
+
} catch (error) {
|
|
641
|
+
this.logger.error(`Background generation failed for artifact ${artifact.id}:`, error);
|
|
642
|
+
await notifyArtifactFailed(this.db, {
|
|
643
|
+
userId,
|
|
644
|
+
workspaceId,
|
|
645
|
+
artifactId: artifact.id,
|
|
646
|
+
artifactType: ArtifactType.WORKSHEET,
|
|
647
|
+
title: artifact.title,
|
|
648
|
+
message: `Failed to generate worksheet: ${error instanceof Error ? error.message : 'Unknown error'}`,
|
|
649
|
+
}).catch(() => {});
|
|
650
|
+
|
|
651
|
+
await this.db.artifact.deleteMany({ where: { id: artifact.id } }).catch(() => {});
|
|
652
|
+
|
|
653
|
+
await PusherService.emitError(
|
|
654
|
+
workspaceId,
|
|
655
|
+
`Failed to generate worksheet: ${error instanceof Error ? error.message : 'Unknown error'}`,
|
|
656
|
+
'worksheet_generation',
|
|
657
|
+
);
|
|
658
|
+
}
|
|
659
|
+
})();
|
|
660
|
+
|
|
661
|
+
return { artifact };
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
async checkAnswer(
|
|
665
|
+
userId: string,
|
|
666
|
+
input: { worksheetId: string; questionId: string; answer: string },
|
|
667
|
+
) {
|
|
668
|
+
const worksheet = await this.db.artifact.findFirst({
|
|
669
|
+
where: {
|
|
670
|
+
id: input.worksheetId,
|
|
671
|
+
type: ArtifactType.WORKSHEET,
|
|
672
|
+
workspace: workspaceAccessWhere(userId),
|
|
673
|
+
},
|
|
674
|
+
include: { workspace: true },
|
|
675
|
+
});
|
|
676
|
+
if (!worksheet) throw new TRPCError({ code: 'NOT_FOUND' });
|
|
677
|
+
const question = await this.db.worksheetQuestion.findFirst({
|
|
678
|
+
where: { id: input.questionId, artifactId: input.worksheetId },
|
|
679
|
+
});
|
|
680
|
+
if (!question) throw new TRPCError({ code: 'NOT_FOUND' });
|
|
681
|
+
|
|
682
|
+
const questionMeta = question.meta
|
|
683
|
+
? typeof question.meta === 'object'
|
|
684
|
+
? (question.meta as any)
|
|
685
|
+
: JSON.parse(question.meta as any)
|
|
686
|
+
: ({} as any);
|
|
687
|
+
const markScheme = questionMeta.markScheme;
|
|
688
|
+
|
|
689
|
+
let isCorrect = false;
|
|
690
|
+
let userMarkScheme: any = null;
|
|
691
|
+
|
|
692
|
+
if (markScheme && markScheme.points && markScheme.points.length > 0) {
|
|
693
|
+
try {
|
|
694
|
+
userMarkScheme = await ai.backend.checkWorksheetQuestions(
|
|
695
|
+
worksheet.workspace.id,
|
|
696
|
+
userId,
|
|
697
|
+
question.prompt,
|
|
698
|
+
input.answer,
|
|
699
|
+
markScheme,
|
|
700
|
+
);
|
|
701
|
+
|
|
702
|
+
const achievedTotal = userMarkScheme.points.reduce(
|
|
703
|
+
(sum: number, p: any) => sum + (p.achievedPoints || 0),
|
|
704
|
+
0,
|
|
705
|
+
);
|
|
706
|
+
isCorrect = achievedTotal === markScheme.totalPoints;
|
|
707
|
+
} catch (error) {
|
|
708
|
+
this.logger.error(
|
|
709
|
+
'Failed to check answer with AI',
|
|
710
|
+
error instanceof Error ? error.message : 'Unknown error',
|
|
711
|
+
);
|
|
712
|
+
isCorrect = question.answer === input.answer;
|
|
713
|
+
}
|
|
714
|
+
} else {
|
|
715
|
+
throw new TRPCError({ code: 'BAD_REQUEST', message: 'No mark scheme found for question' });
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
const progress = await this.db.worksheetQuestionProgress.upsert({
|
|
719
|
+
where: {
|
|
720
|
+
worksheetQuestionId_userId: {
|
|
721
|
+
worksheetQuestionId: input.questionId,
|
|
722
|
+
userId,
|
|
723
|
+
},
|
|
724
|
+
},
|
|
725
|
+
create: {
|
|
726
|
+
worksheetQuestionId: input.questionId,
|
|
727
|
+
userId,
|
|
728
|
+
modified: true,
|
|
729
|
+
userAnswer: input.answer,
|
|
730
|
+
correct: isCorrect,
|
|
731
|
+
completedAt: new Date(),
|
|
732
|
+
attempts: 1,
|
|
733
|
+
meta: userMarkScheme
|
|
734
|
+
? { userMarkScheme: JSON.parse(JSON.stringify(userMarkScheme)) }
|
|
735
|
+
: { userMarkScheme: null },
|
|
736
|
+
},
|
|
737
|
+
update: {
|
|
738
|
+
modified: true,
|
|
739
|
+
userAnswer: input.answer,
|
|
740
|
+
correct: isCorrect,
|
|
741
|
+
completedAt: new Date(),
|
|
742
|
+
attempts: { increment: 1 },
|
|
743
|
+
meta: userMarkScheme
|
|
744
|
+
? { userMarkScheme: JSON.parse(JSON.stringify(userMarkScheme)) }
|
|
745
|
+
: { userMarkScheme: null },
|
|
746
|
+
},
|
|
747
|
+
});
|
|
748
|
+
|
|
749
|
+
return { isCorrect, userMarkScheme, progress };
|
|
750
|
+
}
|
|
751
|
+
}
|