@goscribe/server 1.3.3 → 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.
Files changed (378) hide show
  1. package/.env.example +12 -0
  2. package/.vscode/settings.json +3 -0
  3. package/REFACTOR_NOTES.md +60 -0
  4. package/TESTING_PROMPT.md +225 -0
  5. package/dist/controllers/admin.controller.d.ts +715 -0
  6. package/dist/controllers/admin.controller.js +9 -0
  7. package/dist/controllers/annotations.controller.d.ts +439 -0
  8. package/dist/controllers/annotations.controller.js +9 -0
  9. package/dist/controllers/app-router.controller.d.ts +3011 -0
  10. package/dist/controllers/app-router.controller.js +38 -0
  11. package/dist/controllers/app-router.controller.test.d.ts +1 -0
  12. package/dist/controllers/app-router.controller.test.js +36 -0
  13. package/dist/controllers/auth.controller.d.ts +323 -0
  14. package/dist/controllers/auth.controller.js +9 -0
  15. package/dist/controllers/base.controller.d.ts +4 -0
  16. package/dist/controllers/base.controller.js +5 -0
  17. package/dist/controllers/chat.controller.d.ts +341 -0
  18. package/dist/controllers/chat.controller.js +9 -0
  19. package/dist/controllers/copilot.controller.d.ts +397 -0
  20. package/dist/controllers/copilot.controller.js +9 -0
  21. package/dist/controllers/flashcards.controller.d.ts +651 -0
  22. package/dist/controllers/flashcards.controller.js +9 -0
  23. package/dist/controllers/members.controller.d.ts +339 -0
  24. package/dist/controllers/members.controller.js +9 -0
  25. package/dist/controllers/notifications.controller.d.ts +199 -0
  26. package/dist/controllers/notifications.controller.js +9 -0
  27. package/dist/controllers/payment.controller.d.ts +181 -0
  28. package/dist/controllers/payment.controller.js +9 -0
  29. package/dist/controllers/podcast.controller.d.ts +575 -0
  30. package/dist/controllers/podcast.controller.js +9 -0
  31. package/dist/controllers/router-module.controller.d.ts +5 -0
  32. package/dist/controllers/router-module.controller.js +6 -0
  33. package/dist/controllers/studyguide.controller.d.ts +73 -0
  34. package/dist/controllers/studyguide.controller.js +9 -0
  35. package/dist/controllers/worksheets.controller.d.ts +829 -0
  36. package/dist/controllers/worksheets.controller.js +9 -0
  37. package/dist/controllers/workspace.controller.d.ts +1207 -0
  38. package/dist/controllers/workspace.controller.js +9 -0
  39. package/dist/lib/activity_human_description.test.js +16 -15
  40. package/dist/lib/activity_log_service.test.js +28 -23
  41. package/dist/lib/ai/config.d.ts +20 -0
  42. package/dist/lib/ai/config.js +31 -0
  43. package/dist/lib/ai/embedding-client.d.ts +8 -0
  44. package/dist/lib/ai/embedding-client.js +30 -0
  45. package/dist/lib/ai/index.d.ts +47 -0
  46. package/dist/lib/ai/index.js +28 -0
  47. package/dist/lib/ai/inference-backend/client.d.ts +28 -0
  48. package/dist/lib/ai/inference-backend/client.js +301 -0
  49. package/dist/lib/ai/inference-backend/mocks.d.ts +12 -0
  50. package/dist/lib/ai/inference-backend/mocks.js +133 -0
  51. package/dist/lib/ai/inference-backend/types.d.ts +44 -0
  52. package/dist/lib/ai/inference-backend/types.js +1 -0
  53. package/dist/lib/ai/json-parse.d.ts +2 -0
  54. package/dist/lib/ai/json-parse.js +34 -0
  55. package/dist/lib/ai/llm-client.d.ts +6 -0
  56. package/dist/lib/ai/llm-client.js +19 -0
  57. package/dist/lib/ai/mock.d.ts +2 -0
  58. package/dist/lib/ai/mock.js +10 -0
  59. package/dist/lib/ai/types.d.ts +9 -0
  60. package/dist/lib/ai/types.js +1 -0
  61. package/dist/lib/chunking.d.ts +19 -0
  62. package/dist/lib/chunking.js +47 -0
  63. package/dist/lib/curated-kb-seed.d.ts +12 -0
  64. package/dist/lib/curated-kb-seed.js +155 -0
  65. package/dist/lib/email.js +67 -108
  66. package/dist/lib/embeddings.d.ts +2 -0
  67. package/dist/lib/embeddings.js +1 -0
  68. package/dist/lib/ensure-curated-kb-catalog.d.ts +6 -0
  69. package/dist/lib/ensure-curated-kb-catalog.js +53 -0
  70. package/dist/lib/env.d.ts +1 -5
  71. package/dist/lib/env.js +2 -7
  72. package/dist/lib/inference.d.ts +1 -8
  73. package/dist/lib/inference.js +1 -19
  74. package/dist/lib/kb-meta.d.ts +8 -0
  75. package/dist/lib/kb-meta.js +77 -0
  76. package/dist/lib/note-text.d.ts +1 -0
  77. package/dist/lib/note-text.js +47 -0
  78. package/dist/lib/notification-service.test.js +37 -36
  79. package/dist/lib/pdf.d.ts +11 -0
  80. package/dist/lib/pdf.js +11 -0
  81. package/dist/lib/usage_service.d.ts +2 -1
  82. package/dist/lib/usage_service.js +30 -12
  83. package/dist/lib/worksheet-generation.js +4 -4
  84. package/dist/lib/worksheet-generation.test.js +32 -17
  85. package/dist/lib/workspace-kb.d.ts +5 -0
  86. package/dist/lib/workspace-kb.js +7 -0
  87. package/dist/models/controller-context.model.d.ts +8 -0
  88. package/dist/models/controller-context.model.js +1 -0
  89. package/dist/repositories/artifact.repository.d.ts +60 -0
  90. package/dist/repositories/artifact.repository.js +40 -0
  91. package/dist/repositories/base.repository.d.ts +14 -0
  92. package/dist/repositories/base.repository.js +14 -0
  93. package/dist/repositories/invitation.repository.d.ts +94 -0
  94. package/dist/repositories/invitation.repository.js +44 -0
  95. package/dist/repositories/notification.repository.d.ts +72 -0
  96. package/dist/repositories/notification.repository.js +44 -0
  97. package/dist/repositories/router-module.repository.d.ts +10 -0
  98. package/dist/repositories/router-module.repository.js +14 -0
  99. package/dist/repositories/user.repository.d.ts +74 -0
  100. package/dist/repositories/user.repository.js +37 -0
  101. package/dist/repositories/workspace-member.repository.d.ts +31 -0
  102. package/dist/repositories/workspace-member.repository.js +31 -0
  103. package/dist/repositories/workspace.repository.d.ts +97 -0
  104. package/dist/repositories/workspace.repository.js +79 -0
  105. package/dist/routers/_app.d.ts +528 -33
  106. package/dist/routers/_app.js +4 -0
  107. package/dist/routers/admin.d.ts +0 -4
  108. package/dist/routers/admin.js +21 -549
  109. package/dist/routers/annotations.js +12 -170
  110. package/dist/routers/artifactVersions.d.ts +65 -0
  111. package/dist/routers/artifactVersions.js +14 -0
  112. package/dist/routers/auth.d.ts +0 -6
  113. package/dist/routers/auth.js +36 -421
  114. package/dist/routers/chat.js +15 -229
  115. package/dist/routers/copilot.d.ts +14 -13
  116. package/dist/routers/copilot.js +13 -532
  117. package/dist/routers/flashcards.d.ts +5 -5
  118. package/dist/routers/flashcards.js +23 -349
  119. package/dist/routers/knowledgeBase.d.ts +421 -0
  120. package/dist/routers/knowledgeBase.js +118 -0
  121. package/dist/routers/members.d.ts +0 -41
  122. package/dist/routers/members.js +22 -710
  123. package/dist/routers/notes.d.ts +94 -0
  124. package/dist/routers/notes.js +37 -0
  125. package/dist/routers/notifications.js +7 -109
  126. package/dist/routers/payment.d.ts +3 -2
  127. package/dist/routers/payment.js +11 -393
  128. package/dist/routers/podcast.d.ts +1 -1
  129. package/dist/routers/podcast.js +11 -784
  130. package/dist/routers/studyguide.js +3 -129
  131. package/dist/routers/worksheets.d.ts +29 -14
  132. package/dist/routers/worksheets.js +49 -628
  133. package/dist/routers/workspace.d.ts +0 -4
  134. package/dist/routers/workspace.js +28 -922
  135. package/dist/scripts/purge-deleted-users.js +2 -2
  136. package/dist/server.js +10 -3
  137. package/dist/services/activity/activity-human-description.service.d.ts +13 -0
  138. package/dist/services/activity/activity-human-description.service.js +221 -0
  139. package/dist/services/activity/activity-human-description.service.test.d.ts +1 -0
  140. package/dist/services/activity/activity-human-description.service.test.js +16 -0
  141. package/dist/services/activity/activity-log.service.d.ts +87 -0
  142. package/dist/services/activity/activity-log.service.js +276 -0
  143. package/dist/services/activity/activity-log.service.test.d.ts +1 -0
  144. package/dist/services/activity/activity-log.service.test.js +27 -0
  145. package/dist/services/activity-human-description.service.d.ts +13 -0
  146. package/dist/services/activity-human-description.service.js +221 -0
  147. package/dist/services/activity-human-description.service.test.d.ts +1 -0
  148. package/dist/services/activity-human-description.service.test.js +16 -0
  149. package/dist/services/activity-log.service.d.ts +87 -0
  150. package/dist/services/activity-log.service.js +276 -0
  151. package/dist/services/activity-log.service.test.d.ts +1 -0
  152. package/dist/services/activity-log.service.test.js +27 -0
  153. package/dist/services/admin/admin.service.d.ts +270 -0
  154. package/dist/services/admin/admin.service.js +476 -0
  155. package/dist/services/admin.service.d.ts +270 -0
  156. package/dist/services/admin.service.js +476 -0
  157. package/dist/services/ai/ai-session.service.d.ts +5 -0
  158. package/dist/services/ai/ai-session.service.js +4 -0
  159. package/dist/services/ai-session.service.d.ts +60 -0
  160. package/dist/services/ai-session.service.js +561 -0
  161. package/dist/services/annotation.service.d.ts +177 -0
  162. package/dist/services/annotation.service.js +154 -0
  163. package/dist/services/artifact-notification.service.d.ts +14 -0
  164. package/dist/services/artifact-notification.service.js +20 -0
  165. package/dist/services/artifact-version.service.d.ts +38 -0
  166. package/dist/services/artifact-version.service.js +129 -0
  167. package/dist/services/artifacts/annotation.service.d.ts +177 -0
  168. package/dist/services/artifacts/annotation.service.js +154 -0
  169. package/dist/services/artifacts/artifact-version.service.d.ts +38 -0
  170. package/dist/services/artifacts/artifact-version.service.js +129 -0
  171. package/dist/services/artifacts/chat.service.d.ts +127 -0
  172. package/dist/services/artifacts/chat.service.js +182 -0
  173. package/dist/services/artifacts/study-guide.service.d.ts +18 -0
  174. package/dist/services/artifacts/study-guide.service.js +65 -0
  175. package/dist/services/auth/auth.service.d.ts +94 -0
  176. package/dist/services/auth/auth.service.js +368 -0
  177. package/dist/services/auth.service.d.ts +94 -0
  178. package/dist/services/auth.service.js +368 -0
  179. package/dist/services/base.service.d.ts +14 -0
  180. package/dist/services/base.service.js +14 -0
  181. package/dist/services/billing/payment.service.d.ts +55 -0
  182. package/dist/services/billing/payment.service.js +368 -0
  183. package/dist/services/billing/subscription.service.d.ts +37 -0
  184. package/dist/services/billing/subscription.service.js +654 -0
  185. package/dist/services/billing/usage.service.d.ts +27 -0
  186. package/dist/services/billing/usage.service.js +77 -0
  187. package/dist/services/chat.service.d.ts +127 -0
  188. package/dist/services/chat.service.js +182 -0
  189. package/dist/services/content/copilot.service.d.ts +113 -0
  190. package/dist/services/content/copilot.service.js +453 -0
  191. package/dist/services/content/flashcard-progress.service.d.ts +159 -0
  192. package/dist/services/content/flashcard-progress.service.js +432 -0
  193. package/dist/services/content/flashcard.service.d.ts +140 -0
  194. package/dist/services/content/flashcard.service.js +326 -0
  195. package/dist/services/content/media-analysis.service.d.ts +23 -0
  196. package/dist/services/content/media-analysis.service.js +404 -0
  197. package/dist/services/content/podcast.service.d.ts +267 -0
  198. package/dist/services/content/podcast.service.js +653 -0
  199. package/dist/services/content/worksheet-content.service.d.ts +37 -0
  200. package/dist/services/content/worksheet-content.service.js +84 -0
  201. package/dist/services/content/worksheet-content.service.test.d.ts +1 -0
  202. package/dist/services/content/worksheet-content.service.test.js +69 -0
  203. package/dist/services/content/worksheet-generation.service.d.ts +91 -0
  204. package/dist/services/content/worksheet-generation.service.js +95 -0
  205. package/dist/services/content/worksheet-generation.service.test.d.ts +1 -0
  206. package/dist/services/content/worksheet-generation.service.test.js +20 -0
  207. package/dist/services/content/worksheet.service.d.ts +347 -0
  208. package/dist/services/content/worksheet.service.js +599 -0
  209. package/dist/services/copilot.service.d.ts +116 -0
  210. package/dist/services/copilot.service.js +447 -0
  211. package/dist/services/flashcard-progress.service.d.ts +2 -2
  212. package/dist/services/flashcard-progress.service.js +3 -2
  213. package/dist/services/flashcard.service.d.ts +140 -0
  214. package/dist/services/flashcard.service.js +325 -0
  215. package/dist/services/invitation.service.d.ts +66 -0
  216. package/dist/services/invitation.service.js +348 -0
  217. package/dist/services/knowledge/knowledge-base.service.d.ts +316 -0
  218. package/dist/services/knowledge/knowledge-base.service.js +544 -0
  219. package/dist/services/knowledge-base.service.d.ts +316 -0
  220. package/dist/services/knowledge-base.service.js +536 -0
  221. package/dist/services/media-analysis.service.d.ts +23 -0
  222. package/dist/services/media-analysis.service.js +384 -0
  223. package/dist/services/member.service.d.ts +36 -0
  224. package/dist/services/member.service.js +193 -0
  225. package/dist/services/members/invitation.service.d.ts +66 -0
  226. package/dist/services/members/invitation.service.js +348 -0
  227. package/dist/services/members/member.service.d.ts +36 -0
  228. package/dist/services/members/member.service.js +193 -0
  229. package/dist/services/note.service.d.ts +55 -0
  230. package/dist/services/note.service.js +111 -0
  231. package/dist/services/notification.service.d.ts +214 -0
  232. package/dist/services/notification.service.js +550 -0
  233. package/dist/services/notification.service.test.d.ts +1 -0
  234. package/dist/services/notification.service.test.js +87 -0
  235. package/dist/services/notifications/notification.service.d.ts +214 -0
  236. package/dist/services/notifications/notification.service.js +550 -0
  237. package/dist/services/notifications/notification.service.test.d.ts +1 -0
  238. package/dist/services/notifications/notification.service.test.js +87 -0
  239. package/dist/services/payment.service.d.ts +55 -0
  240. package/dist/services/payment.service.js +368 -0
  241. package/dist/services/podcast.service.d.ts +267 -0
  242. package/dist/services/podcast.service.js +654 -0
  243. package/dist/services/router-module.service.d.ts +7 -0
  244. package/dist/services/router-module.service.js +10 -0
  245. package/dist/services/study-guide.service.d.ts +18 -0
  246. package/dist/services/study-guide.service.js +65 -0
  247. package/dist/services/subscription.service.d.ts +37 -0
  248. package/dist/services/subscription.service.js +654 -0
  249. package/dist/services/usage-limit-policy.service.d.ts +12 -0
  250. package/dist/services/usage-limit-policy.service.js +22 -0
  251. package/dist/services/usage-limit-policy.service.test.d.ts +1 -0
  252. package/dist/services/usage-limit-policy.service.test.js +46 -0
  253. package/dist/services/usage.service.d.ts +27 -0
  254. package/dist/services/usage.service.js +77 -0
  255. package/dist/services/worksheet-content.service.d.ts +42 -0
  256. package/dist/services/worksheet-content.service.js +84 -0
  257. package/dist/services/worksheet-content.service.test.d.ts +1 -0
  258. package/dist/services/worksheet-content.service.test.js +69 -0
  259. package/dist/services/worksheet-generation.service.d.ts +91 -0
  260. package/dist/services/worksheet-generation.service.js +95 -0
  261. package/dist/services/worksheet-generation.service.test.d.ts +1 -0
  262. package/dist/services/worksheet-generation.service.test.js +20 -0
  263. package/dist/services/worksheet.service.d.ts +385 -0
  264. package/dist/services/worksheet.service.js +596 -0
  265. package/dist/services/workspace/workspace-analytics.service.d.ts +24 -0
  266. package/dist/services/workspace/workspace-analytics.service.js +95 -0
  267. package/dist/services/workspace/workspace-kb.service.d.ts +40 -0
  268. package/dist/services/workspace/workspace-kb.service.js +184 -0
  269. package/dist/services/workspace/workspace.service.d.ts +307 -0
  270. package/dist/services/workspace/workspace.service.js +394 -0
  271. package/dist/services/workspace-analytics.service.d.ts +24 -0
  272. package/dist/services/workspace-analytics.service.js +95 -0
  273. package/dist/services/workspace-kb.service.d.ts +40 -0
  274. package/dist/services/workspace-kb.service.js +184 -0
  275. package/dist/services/workspace-progress.service.d.ts +27 -0
  276. package/dist/services/workspace-progress.service.js +56 -0
  277. package/dist/services/workspace-progress.service.test.d.ts +1 -0
  278. package/dist/services/workspace-progress.service.test.js +49 -0
  279. package/dist/services/workspace.service.d.ts +307 -0
  280. package/dist/services/workspace.service.js +390 -0
  281. package/dist/trpc.js +2 -2
  282. package/package.json +5 -6
  283. package/prisma/migrations/20260509000001_add_knowledge_base/migration.sql +99 -0
  284. package/prisma/migrations/20260509000002_curate_knowledge_base/migration.sql +52 -0
  285. package/prisma/migrations/20260522000000_add_notes/migration.sql +27 -0
  286. package/prisma/migrations/20260524000000_remove_notes/migration.sql +3 -0
  287. package/prisma/schema.prisma +150 -48
  288. package/prisma/seed.mjs +67 -0
  289. package/scripts/debug/README.md +4 -0
  290. package/src/README.md +63 -0
  291. package/src/lib/ai/config.ts +34 -0
  292. package/src/lib/ai/embedding-client.ts +47 -0
  293. package/src/lib/ai/index.ts +62 -0
  294. package/src/lib/ai/inference-backend/client.ts +479 -0
  295. package/src/lib/ai/inference-backend/mocks.ts +171 -0
  296. package/src/lib/ai/inference-backend/types.ts +50 -0
  297. package/src/lib/ai/json-parse.ts +35 -0
  298. package/src/lib/ai/llm-client.ts +31 -0
  299. package/src/lib/ai/mock.ts +12 -0
  300. package/src/lib/ai/types.ts +11 -0
  301. package/src/lib/chunking.ts +81 -0
  302. package/src/lib/curated-kb-seed.ts +164 -0
  303. package/src/lib/email.ts +77 -115
  304. package/src/lib/embeddings.ts +9 -0
  305. package/src/lib/ensure-curated-kb-catalog.ts +60 -0
  306. package/src/lib/env.ts +2 -7
  307. package/src/lib/inference.ts +1 -21
  308. package/src/lib/kb-meta.ts +81 -0
  309. package/src/lib/pdf.ts +23 -0
  310. package/src/lib/workspace-kb.ts +7 -0
  311. package/src/repositories/artifact.repository.ts +55 -0
  312. package/src/repositories/base.repository.ts +19 -0
  313. package/src/repositories/invitation.repository.ts +53 -0
  314. package/src/repositories/notification.repository.ts +53 -0
  315. package/src/repositories/user.repository.ts +44 -0
  316. package/src/repositories/workspace-member.repository.ts +38 -0
  317. package/src/repositories/workspace.repository.ts +89 -0
  318. package/src/routers/_app.ts +4 -0
  319. package/src/routers/admin.ts +124 -692
  320. package/src/routers/annotations.ts +25 -203
  321. package/src/routers/artifactVersions.ts +32 -0
  322. package/src/routers/auth.ts +81 -519
  323. package/src/routers/chat.ts +42 -245
  324. package/src/routers/copilot.ts +41 -666
  325. package/src/routers/flashcards.ts +108 -404
  326. package/src/routers/knowledgeBase.ts +216 -0
  327. package/src/routers/members.ts +60 -782
  328. package/src/routers/notifications.ts +15 -117
  329. package/src/routers/payment.ts +37 -446
  330. package/src/routers/podcast.ts +36 -898
  331. package/src/routers/studyguide.ts +5 -144
  332. package/src/routers/worksheets.ts +171 -735
  333. package/src/routers/workspace.ts +138 -1109
  334. package/src/scripts/purge-deleted-users.ts +2 -2
  335. package/src/server.ts +10 -3
  336. package/src/{lib/activity_human_description.test.ts → services/activity/activity-human-description.service.test.ts} +1 -1
  337. package/src/{lib/activity_log_service.test.ts → services/activity/activity-log.service.test.ts} +1 -1
  338. package/src/{lib/activity_log_service.ts → services/activity/activity-log.service.ts} +2 -2
  339. package/src/services/admin/admin.service.ts +612 -0
  340. package/src/services/ai/ai-session.service.ts +5 -0
  341. package/src/services/artifacts/annotation.service.ts +189 -0
  342. package/src/services/artifacts/artifact-version.service.ts +151 -0
  343. package/src/services/artifacts/chat.service.ts +197 -0
  344. package/src/services/artifacts/study-guide.service.ts +72 -0
  345. package/src/services/auth/auth.service.ts +473 -0
  346. package/src/services/base.service.ts +19 -0
  347. package/src/services/billing/payment.service.ts +436 -0
  348. package/src/{lib/subscription_service.ts → services/billing/subscription.service.ts} +5 -5
  349. package/src/{lib/usage_service.ts → services/billing/usage.service.ts} +32 -12
  350. package/src/services/content/copilot.service.ts +596 -0
  351. package/src/services/{flashcard-progress.service.ts → content/flashcard-progress.service.ts} +6 -3
  352. package/src/services/content/flashcard.service.ts +394 -0
  353. package/src/services/content/media-analysis.service.ts +556 -0
  354. package/src/services/content/podcast.service.ts +777 -0
  355. package/src/services/content/worksheet-content.service.test.ts +83 -0
  356. package/src/services/content/worksheet-content.service.ts +117 -0
  357. package/src/{lib/worksheet-generation.test.ts → services/content/worksheet-generation.service.test.ts} +1 -1
  358. package/src/services/content/worksheet.service.ts +751 -0
  359. package/src/services/knowledge/knowledge-base.service.ts +705 -0
  360. package/src/services/members/invitation.service.ts +427 -0
  361. package/src/services/members/member.service.ts +241 -0
  362. package/src/{lib/notification-service.test.ts → services/notifications/notification.service.test.ts} +2 -2
  363. package/src/{lib/notification-service.ts → services/notifications/notification.service.ts} +102 -1
  364. package/src/services/workspace/workspace-analytics.service.ts +107 -0
  365. package/src/services/workspace/workspace-kb.service.ts +273 -0
  366. package/src/services/workspace/workspace.service.ts +481 -0
  367. package/src/trpc.ts +2 -2
  368. package/src/lib/ai-session.ts +0 -704
  369. package/src/lib/workspace-access.ts +0 -13
  370. /package/{check-difficulty.cjs → scripts/debug/check-difficulty.cjs} +0 -0
  371. /package/{check-questions.cjs → scripts/debug/check-questions.cjs} +0 -0
  372. /package/{db-summary.cjs → scripts/debug/db-summary.cjs} +0 -0
  373. /package/{mcq-test.cjs → scripts/debug/mcq-test.cjs} +0 -0
  374. /package/{test-generate.js → scripts/debug/test-generate.js} +0 -0
  375. /package/{test-ratio.cjs → scripts/debug/test-ratio.cjs} +0 -0
  376. /package/{zod-test.cjs → scripts/debug/zod-test.cjs} +0 -0
  377. /package/src/{lib/activity_human_description.ts → services/activity/activity-human-description.service.ts} +0 -0
  378. /package/src/{lib/worksheet-generation.ts → services/content/worksheet-generation.service.ts} +0 -0
@@ -0,0 +1,189 @@
1
+ import type { PrismaClient } from '@prisma/client';
2
+ import { TRPCError } from '@trpc/server';
3
+ import { BaseService } from '../base.service.js';
4
+ import { notifyStudyGuideCommentAdded } from '../notifications/notification.service.js';
5
+
6
+ export class AnnotationService extends BaseService {
7
+ constructor(db: PrismaClient) {
8
+ super(db);
9
+ }
10
+
11
+ listHighlights(artifactVersionId: string) {
12
+ return this.db.studyGuideHighlight.findMany({
13
+ where: { artifactVersionId },
14
+ include: {
15
+ comments: {
16
+ include: {
17
+ user: { select: { id: true, name: true, profilePicture: true } },
18
+ },
19
+ orderBy: { createdAt: 'asc' },
20
+ },
21
+ user: { select: { id: true, name: true, profilePicture: true } },
22
+ },
23
+ orderBy: { startOffset: 'asc' },
24
+ });
25
+ }
26
+
27
+ async createHighlight(
28
+ userId: string,
29
+ input: {
30
+ artifactVersionId: string;
31
+ startOffset: number;
32
+ endOffset: number;
33
+ selectedText: string;
34
+ color?: string;
35
+ },
36
+ ) {
37
+ const version = await this.db.artifactVersion.findUnique({
38
+ where: { id: input.artifactVersionId },
39
+ });
40
+
41
+ if (!version) {
42
+ throw new TRPCError({ code: 'NOT_FOUND', message: 'Artifact version not found' });
43
+ }
44
+
45
+ return this.db.studyGuideHighlight.create({
46
+ data: {
47
+ artifactVersionId: input.artifactVersionId,
48
+ userId,
49
+ startOffset: input.startOffset,
50
+ endOffset: input.endOffset,
51
+ selectedText: input.selectedText,
52
+ ...(input.color && { color: input.color }),
53
+ },
54
+ include: {
55
+ comments: true,
56
+ user: { select: { id: true, name: true, profilePicture: true } },
57
+ },
58
+ });
59
+ }
60
+
61
+ async deleteHighlight(userId: string, highlightId: string) {
62
+ const highlight = await this.db.studyGuideHighlight.findUnique({
63
+ where: { id: highlightId },
64
+ });
65
+
66
+ if (!highlight) {
67
+ throw new TRPCError({ code: 'NOT_FOUND', message: 'Highlight not found' });
68
+ }
69
+
70
+ if (highlight.userId !== userId) {
71
+ throw new TRPCError({
72
+ code: 'FORBIDDEN',
73
+ message: 'You can only delete your own highlights',
74
+ });
75
+ }
76
+
77
+ await this.db.studyGuideHighlight.delete({ where: { id: highlightId } });
78
+
79
+ return { success: true };
80
+ }
81
+
82
+ async addComment(userId: string, input: { highlightId: string; content: string }) {
83
+ const highlight = await this.db.studyGuideHighlight.findUnique({
84
+ where: { id: input.highlightId },
85
+ });
86
+
87
+ if (!highlight) {
88
+ throw new TRPCError({ code: 'NOT_FOUND', message: 'Highlight not found' });
89
+ }
90
+
91
+ const comment = await this.db.studyGuideComment.create({
92
+ data: {
93
+ highlightId: input.highlightId,
94
+ userId,
95
+ content: input.content,
96
+ },
97
+ include: {
98
+ user: { select: { id: true, name: true, profilePicture: true } },
99
+ },
100
+ });
101
+
102
+ const after = await this.db.studyGuideHighlight.findUnique({
103
+ where: { id: input.highlightId },
104
+ include: {
105
+ comments: { select: { userId: true } },
106
+ artifactVersion: {
107
+ include: {
108
+ artifact: {
109
+ select: { id: true, title: true, workspaceId: true, type: true },
110
+ },
111
+ },
112
+ },
113
+ },
114
+ });
115
+
116
+ if (after?.artifactVersion?.artifact) {
117
+ const art = after.artifactVersion.artifact;
118
+ const recipientIds = new Set<string>();
119
+ if (after.userId !== userId) {
120
+ recipientIds.add(after.userId);
121
+ }
122
+ for (const c of after.comments) {
123
+ if (c.userId !== userId) {
124
+ recipientIds.add(c.userId);
125
+ }
126
+ }
127
+ const authorName = comment.user.name?.trim() || 'Someone';
128
+ await notifyStudyGuideCommentAdded(this.db, {
129
+ authorUserId: userId,
130
+ authorName,
131
+ content: input.content,
132
+ highlightId: input.highlightId,
133
+ commentId: comment.id,
134
+ workspaceId: art.workspaceId,
135
+ artifactId: art.id,
136
+ artifactTitle: art.title,
137
+ recipientUserIds: [...recipientIds],
138
+ }).catch(() => {});
139
+ }
140
+
141
+ return comment;
142
+ }
143
+
144
+ async updateComment(userId: string, input: { commentId: string; content: string }) {
145
+ const comment = await this.db.studyGuideComment.findUnique({
146
+ where: { id: input.commentId },
147
+ });
148
+
149
+ if (!comment) {
150
+ throw new TRPCError({ code: 'NOT_FOUND', message: 'Comment not found' });
151
+ }
152
+
153
+ if (comment.userId !== userId) {
154
+ throw new TRPCError({
155
+ code: 'FORBIDDEN',
156
+ message: 'You can only edit your own comments',
157
+ });
158
+ }
159
+
160
+ return this.db.studyGuideComment.update({
161
+ where: { id: input.commentId },
162
+ data: { content: input.content },
163
+ include: {
164
+ user: { select: { id: true, name: true, profilePicture: true } },
165
+ },
166
+ });
167
+ }
168
+
169
+ async deleteComment(userId: string, commentId: string) {
170
+ const comment = await this.db.studyGuideComment.findUnique({
171
+ where: { id: commentId },
172
+ });
173
+
174
+ if (!comment) {
175
+ throw new TRPCError({ code: 'NOT_FOUND', message: 'Comment not found' });
176
+ }
177
+
178
+ if (comment.userId !== userId) {
179
+ throw new TRPCError({
180
+ code: 'FORBIDDEN',
181
+ message: 'You can only delete your own comments',
182
+ });
183
+ }
184
+
185
+ await this.db.studyGuideComment.delete({ where: { id: commentId } });
186
+
187
+ return { success: true };
188
+ }
189
+ }
@@ -0,0 +1,151 @@
1
+ import type { PrismaClient } from '@prisma/client';
2
+ import { TRPCError } from '@trpc/server';
3
+ import { BaseService } from '../base.service.js';
4
+ import { workspaceAccessWhere } from '../../repositories/workspace.repository.js';
5
+
6
+ export class ArtifactVersionService extends BaseService {
7
+ constructor(db: PrismaClient) {
8
+ super(db);
9
+ }
10
+
11
+ private async assertArtifactAccess(userId: string, artifactId: string) {
12
+ const artifact = await this.db.artifact.findFirst({
13
+ where: {
14
+ id: artifactId,
15
+ workspace: workspaceAccessWhere(userId),
16
+ },
17
+ select: { id: true, workspaceId: true },
18
+ });
19
+
20
+ if (!artifact) {
21
+ throw new TRPCError({
22
+ code: 'NOT_FOUND',
23
+ message: 'Artifact not found or access denied',
24
+ });
25
+ }
26
+
27
+ return artifact;
28
+ }
29
+
30
+ private async assertVersionAccess(userId: string, artifactVersionId: string) {
31
+ const version = await this.db.artifactVersion.findUnique({
32
+ where: { id: artifactVersionId },
33
+ include: {
34
+ artifact: {
35
+ select: { id: true, workspaceId: true, workspace: { select: { id: true } } },
36
+ },
37
+ },
38
+ });
39
+
40
+ if (!version) {
41
+ throw new TRPCError({ code: 'NOT_FOUND', message: 'Version not found' });
42
+ }
43
+
44
+ const hasAccess = await this.db.workspace.findFirst({
45
+ where: {
46
+ id: version.artifact.workspaceId,
47
+ ...workspaceAccessWhere(userId),
48
+ },
49
+ select: { id: true },
50
+ });
51
+
52
+ if (!hasAccess) {
53
+ throw new TRPCError({ code: 'FORBIDDEN', message: 'Access denied' });
54
+ }
55
+
56
+ return version;
57
+ }
58
+
59
+ async listVersions(userId: string, artifactId: string) {
60
+ await this.assertArtifactAccess(userId, artifactId);
61
+
62
+ const versions = await this.db.artifactVersion.findMany({
63
+ where: { artifactId },
64
+ orderBy: { version: 'desc' },
65
+ select: {
66
+ id: true,
67
+ version: true,
68
+ createdAt: true,
69
+ data: true,
70
+ createdBy: {
71
+ select: { id: true, name: true },
72
+ },
73
+ },
74
+ });
75
+
76
+ const latestId = versions[0]?.id ?? null;
77
+
78
+ return versions.map((v) => ({
79
+ id: v.id,
80
+ version: v.version,
81
+ createdAt: v.createdAt,
82
+ isCurrent: v.id === latestId,
83
+ label: this.versionLabel(v.data),
84
+ createdBy: v.createdBy,
85
+ }));
86
+ }
87
+
88
+ async getVersion(userId: string, artifactVersionId: string) {
89
+ const version = await this.assertVersionAccess(userId, artifactVersionId);
90
+
91
+ const latest = await this.db.artifactVersion.findFirst({
92
+ where: { artifactId: version.artifactId },
93
+ orderBy: { version: 'desc' },
94
+ select: { id: true },
95
+ });
96
+
97
+ return {
98
+ id: version.id,
99
+ artifactId: version.artifactId,
100
+ version: version.version,
101
+ content: version.content,
102
+ data: version.data,
103
+ createdAt: version.createdAt,
104
+ isCurrent: version.id === latest?.id,
105
+ createdBy: version.createdById,
106
+ };
107
+ }
108
+
109
+ async restoreVersion(userId: string, artifactVersionId: string) {
110
+ const source = await this.assertVersionAccess(userId, artifactVersionId);
111
+
112
+ const latest = await this.db.artifactVersion.findFirst({
113
+ where: { artifactId: source.artifactId },
114
+ orderBy: { version: 'desc' },
115
+ });
116
+
117
+ if (latest?.id === source.id) {
118
+ throw new TRPCError({
119
+ code: 'BAD_REQUEST',
120
+ message: 'This is already the current version',
121
+ });
122
+ }
123
+
124
+ const nextVersion = (latest?.version ?? 0) + 1;
125
+
126
+ const restored = await this.db.artifactVersion.create({
127
+ data: {
128
+ artifactId: source.artifactId,
129
+ version: nextVersion,
130
+ content: source.content,
131
+ data: source.data ?? undefined,
132
+ createdById: userId,
133
+ },
134
+ });
135
+
136
+ await this.db.artifact.update({
137
+ where: { id: source.artifactId },
138
+ data: { updatedAt: new Date() },
139
+ });
140
+
141
+ return restored;
142
+ }
143
+
144
+ private versionLabel(data: unknown): string | null {
145
+ if (!data || typeof data !== 'object') return null;
146
+ const record = data as Record<string, unknown>;
147
+ if (typeof record.title === 'string') return record.title;
148
+ if (typeof record.description === 'string') return record.description;
149
+ return null;
150
+ }
151
+ }
@@ -0,0 +1,197 @@
1
+ import type { PrismaClient } from '@prisma/client';
2
+ import { TRPCError } from '@trpc/server';
3
+ import { BaseService } from '../base.service.js';
4
+ import PusherService from '../../lib/pusher.js';
5
+
6
+ /**
7
+ * NOTE: These procedures currently do not verify that the caller belongs to
8
+ * the target workspace or channel. That's pre-existing behavior and is
9
+ * listed in REFACTOR_NOTES.md as a follow-up.
10
+ */
11
+ export class ChatService extends BaseService {
12
+ constructor(db: PrismaClient) {
13
+ super(db);
14
+ }
15
+
16
+ async getChannels(workspaceId: string) {
17
+ const channels = await this.db.channel.findMany({
18
+ where: { workspaceId },
19
+ include: {
20
+ chats: {
21
+ include: {
22
+ user: {
23
+ select: {
24
+ id: true,
25
+ name: true,
26
+ profilePicture: {
27
+ select: { id: true, name: true, url: true },
28
+ },
29
+ },
30
+ },
31
+ },
32
+ },
33
+ },
34
+ });
35
+ if (!channels) {
36
+ const defaultChannel = await this.db.channel.create({
37
+ data: { workspaceId, name: 'General' },
38
+ });
39
+ return [defaultChannel];
40
+ }
41
+ return channels;
42
+ }
43
+
44
+ async getChannel(input: { workspaceId?: string; channelId?: string }) {
45
+ if (!input.channelId && input.workspaceId) {
46
+ const defaultChannel = await this.db.channel.create({
47
+ data: { workspaceId: input.workspaceId, name: 'General' },
48
+ include: {
49
+ chats: {
50
+ include: {
51
+ user: { select: { id: true, name: true } },
52
+ },
53
+ },
54
+ },
55
+ });
56
+
57
+ await PusherService.emitTaskComplete(input.workspaceId, 'new_channel', {
58
+ channelId: defaultChannel.id,
59
+ workspaceId: input.workspaceId,
60
+ name: 'General',
61
+ createdAt: defaultChannel.createdAt,
62
+ });
63
+
64
+ return defaultChannel;
65
+ }
66
+
67
+ const channel = await this.db.channel.findUnique({
68
+ where: { id: input.channelId },
69
+ include: {
70
+ chats: {
71
+ include: {
72
+ user: { select: { id: true, name: true } },
73
+ },
74
+ },
75
+ },
76
+ });
77
+
78
+ if (!channel) {
79
+ throw new TRPCError({ code: 'NOT_FOUND', message: 'Channel not found' });
80
+ }
81
+
82
+ return channel;
83
+ }
84
+
85
+ async removeChannel(input: { workspaceId: string; channelId: string }) {
86
+ await this.db.channel.delete({ where: { id: input.channelId } });
87
+ await PusherService.emitTaskComplete(input.workspaceId, 'remove_channel', {
88
+ channelId: input.channelId,
89
+ deletedAt: new Date().toISOString(),
90
+ });
91
+ return { success: true };
92
+ }
93
+
94
+ async editChannel(input: { workspaceId: string; channelId: string; name: string }) {
95
+ const channel = await this.db.channel.update({
96
+ where: { id: input.channelId },
97
+ data: { name: input.name },
98
+ include: {
99
+ chats: {
100
+ include: {
101
+ user: { select: { id: true, name: true } },
102
+ },
103
+ },
104
+ },
105
+ });
106
+ await PusherService.emitTaskComplete(input.workspaceId, 'edit_channel', {
107
+ channelId: input.channelId,
108
+ workspaceId: input.workspaceId,
109
+ name: input.name,
110
+ });
111
+ return channel;
112
+ }
113
+
114
+ async createChannel(input: { workspaceId: string; name: string }) {
115
+ const channel = await this.db.channel.create({
116
+ data: { workspaceId: input.workspaceId, name: input.name },
117
+ include: {
118
+ chats: {
119
+ include: {
120
+ user: { select: { id: true, name: true } },
121
+ },
122
+ },
123
+ },
124
+ });
125
+ await PusherService.emitTaskComplete(input.workspaceId, 'new_channel', {
126
+ channelId: channel.id,
127
+ workspaceId: input.workspaceId,
128
+ name: input.name,
129
+ createdAt: channel.createdAt,
130
+ });
131
+ return channel;
132
+ }
133
+
134
+ async postMessage(userId: string, input: { channelId: string; message: string }) {
135
+ const channel = await this.db.channel.findUnique({
136
+ where: { id: input.channelId },
137
+ include: {
138
+ chats: {
139
+ include: {
140
+ user: { select: { id: true, name: true } },
141
+ },
142
+ },
143
+ },
144
+ });
145
+ if (!channel) {
146
+ throw new TRPCError({ code: 'NOT_FOUND', message: 'Channel not found' });
147
+ }
148
+ const chat = await this.db.chat.create({
149
+ data: {
150
+ channelId: input.channelId,
151
+ userId,
152
+ message: input.message,
153
+ },
154
+ include: {
155
+ user: { select: { id: true, name: true } },
156
+ },
157
+ });
158
+ await PusherService.emitChannelEvent(input.channelId, 'new_message', chat);
159
+ return chat;
160
+ }
161
+
162
+ async editMessage(userId: string, input: { chatId: string; message: string }) {
163
+ const chat = await this.db.chat.findUnique({
164
+ where: { id: input.chatId },
165
+ include: { user: { select: { id: true, name: true } } },
166
+ });
167
+ if (!chat) {
168
+ throw new TRPCError({ code: 'NOT_FOUND', message: 'Chat message not found' });
169
+ }
170
+ if (chat.userId !== userId) {
171
+ throw new TRPCError({ code: 'FORBIDDEN', message: 'Not your message to edit' });
172
+ }
173
+ const updatedChat = await this.db.chat.update({
174
+ where: { id: input.chatId },
175
+ data: { message: input.message },
176
+ include: { user: { select: { id: true, name: true } } },
177
+ });
178
+ await PusherService.emitChannelEvent(chat.channelId, 'edit_message', updatedChat);
179
+ return updatedChat;
180
+ }
181
+
182
+ async deleteMessage(userId: string, chatId: string) {
183
+ const chat = await this.db.chat.findUnique({
184
+ where: { id: chatId },
185
+ include: { user: { select: { id: true, name: true } } },
186
+ });
187
+ if (!chat) {
188
+ throw new TRPCError({ code: 'NOT_FOUND', message: 'Chat message not found' });
189
+ }
190
+ if (chat.userId !== userId) {
191
+ throw new TRPCError({ code: 'FORBIDDEN', message: 'Not your message to delete' });
192
+ }
193
+ await this.db.chat.delete({ where: { id: chatId } });
194
+ await PusherService.emitChannelEvent(chat.channelId, 'delete_message', chat);
195
+ return { success: true };
196
+ }
197
+ }
@@ -0,0 +1,72 @@
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 { getUserUsage, getUserPlanLimits } from '../billing/usage.service.js';
6
+ import { workspaceAccessWhere } from '../../repositories/workspace.repository.js';
7
+
8
+ const initializeEditorJsEmptyBlock = () => ({
9
+ time: Date.now(),
10
+ blocks: [
11
+ {
12
+ id: 'initial',
13
+ type: 'paragraph',
14
+ data: { text: 'Upload some files to begin creating your revision workspace...' },
15
+ },
16
+ ],
17
+ version: '2.27.0',
18
+ });
19
+
20
+ export class StudyGuideService extends BaseService {
21
+ constructor(db: PrismaClient) {
22
+ super(db);
23
+ }
24
+
25
+ async get(userId: string, workspaceId: string) {
26
+ let artifact = await this.db.artifact.findFirst({
27
+ where: {
28
+ workspaceId,
29
+ type: ArtifactType.STUDY_GUIDE,
30
+ workspace: workspaceAccessWhere(userId),
31
+ },
32
+ include: {
33
+ versions: { orderBy: { version: 'desc' }, take: 1 },
34
+ },
35
+ });
36
+
37
+ if (!artifact) {
38
+ const [usage, limits] = await Promise.all([
39
+ getUserUsage(userId),
40
+ getUserPlanLimits(userId),
41
+ ]);
42
+ if (limits && usage.studyGuides >= limits.maxStudyGuides) {
43
+ throw new TRPCError({
44
+ code: 'FORBIDDEN',
45
+ message:
46
+ 'Study Guide limit reached. Please upgrade your plan to create more workspaces with study guides.',
47
+ });
48
+ }
49
+
50
+ artifact = await this.db.artifact.create({
51
+ data: {
52
+ workspaceId,
53
+ type: ArtifactType.STUDY_GUIDE,
54
+ title: 'Study Guide',
55
+ createdById: userId,
56
+ versions: {
57
+ create: {
58
+ content: `${JSON.stringify(initializeEditorJsEmptyBlock())}`,
59
+ version: 1,
60
+ createdById: userId,
61
+ },
62
+ },
63
+ },
64
+ include: {
65
+ versions: { orderBy: { version: 'desc' }, take: 1 },
66
+ },
67
+ });
68
+ }
69
+ const latestVersion = artifact.versions[0] ?? null;
70
+ return { artifactId: artifact.id, title: artifact.title, latestVersion };
71
+ }
72
+ }