@goscribe/server 1.3.4 → 1.6.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 (383) 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/context.d.ts +14 -1
  6. package/dist/context.js +23 -2
  7. package/dist/controllers/admin.controller.d.ts +715 -0
  8. package/dist/controllers/admin.controller.js +9 -0
  9. package/dist/controllers/annotations.controller.d.ts +439 -0
  10. package/dist/controllers/annotations.controller.js +9 -0
  11. package/dist/controllers/app-router.controller.d.ts +3011 -0
  12. package/dist/controllers/app-router.controller.js +38 -0
  13. package/dist/controllers/app-router.controller.test.d.ts +1 -0
  14. package/dist/controllers/app-router.controller.test.js +36 -0
  15. package/dist/controllers/auth.controller.d.ts +323 -0
  16. package/dist/controllers/auth.controller.js +9 -0
  17. package/dist/controllers/base.controller.d.ts +4 -0
  18. package/dist/controllers/base.controller.js +5 -0
  19. package/dist/controllers/chat.controller.d.ts +341 -0
  20. package/dist/controllers/chat.controller.js +9 -0
  21. package/dist/controllers/copilot.controller.d.ts +397 -0
  22. package/dist/controllers/copilot.controller.js +9 -0
  23. package/dist/controllers/flashcards.controller.d.ts +651 -0
  24. package/dist/controllers/flashcards.controller.js +9 -0
  25. package/dist/controllers/members.controller.d.ts +339 -0
  26. package/dist/controllers/members.controller.js +9 -0
  27. package/dist/controllers/notifications.controller.d.ts +199 -0
  28. package/dist/controllers/notifications.controller.js +9 -0
  29. package/dist/controllers/payment.controller.d.ts +181 -0
  30. package/dist/controllers/payment.controller.js +9 -0
  31. package/dist/controllers/podcast.controller.d.ts +575 -0
  32. package/dist/controllers/podcast.controller.js +9 -0
  33. package/dist/controllers/router-module.controller.d.ts +5 -0
  34. package/dist/controllers/router-module.controller.js +6 -0
  35. package/dist/controllers/studyguide.controller.d.ts +73 -0
  36. package/dist/controllers/studyguide.controller.js +9 -0
  37. package/dist/controllers/worksheets.controller.d.ts +829 -0
  38. package/dist/controllers/worksheets.controller.js +9 -0
  39. package/dist/controllers/workspace.controller.d.ts +1207 -0
  40. package/dist/controllers/workspace.controller.js +9 -0
  41. package/dist/lib/activity_human_description.test.js +16 -15
  42. package/dist/lib/activity_log_service.test.js +28 -23
  43. package/dist/lib/ai/config.d.ts +20 -0
  44. package/dist/lib/ai/config.js +31 -0
  45. package/dist/lib/ai/embedding-client.d.ts +8 -0
  46. package/dist/lib/ai/embedding-client.js +30 -0
  47. package/dist/lib/ai/index.d.ts +48 -0
  48. package/dist/lib/ai/index.js +29 -0
  49. package/dist/lib/ai/inference-backend/client.d.ts +28 -0
  50. package/dist/lib/ai/inference-backend/client.js +301 -0
  51. package/dist/lib/ai/inference-backend/mocks.d.ts +12 -0
  52. package/dist/lib/ai/inference-backend/mocks.js +133 -0
  53. package/dist/lib/ai/inference-backend/types.d.ts +44 -0
  54. package/dist/lib/ai/inference-backend/types.js +1 -0
  55. package/dist/lib/ai/json-parse.d.ts +2 -0
  56. package/dist/lib/ai/json-parse.js +34 -0
  57. package/dist/lib/ai/llm-client.d.ts +7 -0
  58. package/dist/lib/ai/llm-client.js +36 -0
  59. package/dist/lib/ai/mock.d.ts +2 -0
  60. package/dist/lib/ai/mock.js +10 -0
  61. package/dist/lib/ai/types.d.ts +9 -0
  62. package/dist/lib/ai/types.js +1 -0
  63. package/dist/lib/chunking.d.ts +19 -0
  64. package/dist/lib/chunking.js +47 -0
  65. package/dist/lib/curated-kb-seed.d.ts +12 -0
  66. package/dist/lib/curated-kb-seed.js +155 -0
  67. package/dist/lib/email.js +67 -108
  68. package/dist/lib/embeddings.d.ts +2 -0
  69. package/dist/lib/embeddings.js +1 -0
  70. package/dist/lib/ensure-curated-kb-catalog.d.ts +6 -0
  71. package/dist/lib/ensure-curated-kb-catalog.js +53 -0
  72. package/dist/lib/env.d.ts +1 -5
  73. package/dist/lib/env.js +2 -7
  74. package/dist/lib/inference.d.ts +1 -8
  75. package/dist/lib/inference.js +1 -19
  76. package/dist/lib/kb-meta.d.ts +8 -0
  77. package/dist/lib/kb-meta.js +77 -0
  78. package/dist/lib/note-text.d.ts +1 -0
  79. package/dist/lib/note-text.js +47 -0
  80. package/dist/lib/notification-service.test.js +37 -36
  81. package/dist/lib/pdf.d.ts +11 -0
  82. package/dist/lib/pdf.js +11 -0
  83. package/dist/lib/usage_service.d.ts +2 -1
  84. package/dist/lib/usage_service.js +30 -12
  85. package/dist/lib/worksheet-generation.js +4 -4
  86. package/dist/lib/worksheet-generation.test.js +32 -17
  87. package/dist/lib/workspace-kb.d.ts +5 -0
  88. package/dist/lib/workspace-kb.js +7 -0
  89. package/dist/models/controller-context.model.d.ts +8 -0
  90. package/dist/models/controller-context.model.js +1 -0
  91. package/dist/repositories/artifact.repository.d.ts +60 -0
  92. package/dist/repositories/artifact.repository.js +40 -0
  93. package/dist/repositories/base.repository.d.ts +14 -0
  94. package/dist/repositories/base.repository.js +14 -0
  95. package/dist/repositories/invitation.repository.d.ts +94 -0
  96. package/dist/repositories/invitation.repository.js +44 -0
  97. package/dist/repositories/notification.repository.d.ts +72 -0
  98. package/dist/repositories/notification.repository.js +44 -0
  99. package/dist/repositories/router-module.repository.d.ts +10 -0
  100. package/dist/repositories/router-module.repository.js +14 -0
  101. package/dist/repositories/user.repository.d.ts +74 -0
  102. package/dist/repositories/user.repository.js +37 -0
  103. package/dist/repositories/workspace-member.repository.d.ts +31 -0
  104. package/dist/repositories/workspace-member.repository.js +31 -0
  105. package/dist/repositories/workspace.repository.d.ts +97 -0
  106. package/dist/repositories/workspace.repository.js +79 -0
  107. package/dist/routers/_app.d.ts +566 -111
  108. package/dist/routers/_app.js +4 -0
  109. package/dist/routers/admin.d.ts +0 -4
  110. package/dist/routers/admin.js +21 -549
  111. package/dist/routers/annotations.js +12 -170
  112. package/dist/routers/artifactVersions.d.ts +65 -0
  113. package/dist/routers/artifactVersions.js +14 -0
  114. package/dist/routers/auth.d.ts +0 -6
  115. package/dist/routers/auth.js +37 -422
  116. package/dist/routers/chat.js +15 -229
  117. package/dist/routers/copilot.d.ts +14 -13
  118. package/dist/routers/copilot.js +13 -532
  119. package/dist/routers/flashcards.d.ts +17 -6
  120. package/dist/routers/flashcards.js +23 -349
  121. package/dist/routers/knowledgeBase.d.ts +421 -0
  122. package/dist/routers/knowledgeBase.js +118 -0
  123. package/dist/routers/members.d.ts +0 -41
  124. package/dist/routers/members.js +22 -710
  125. package/dist/routers/notes.d.ts +94 -0
  126. package/dist/routers/notes.js +37 -0
  127. package/dist/routers/notifications.js +7 -109
  128. package/dist/routers/payment.d.ts +2 -12
  129. package/dist/routers/payment.js +11 -393
  130. package/dist/routers/podcast.d.ts +1 -1
  131. package/dist/routers/podcast.js +11 -784
  132. package/dist/routers/studyguide.js +3 -129
  133. package/dist/routers/worksheets.d.ts +29 -14
  134. package/dist/routers/worksheets.js +49 -628
  135. package/dist/routers/workspace.d.ts +27 -71
  136. package/dist/routers/workspace.js +29 -922
  137. package/dist/scripts/purge-deleted-users.js +2 -2
  138. package/dist/server.js +10 -3
  139. package/dist/services/activity/activity-human-description.service.d.ts +13 -0
  140. package/dist/services/activity/activity-human-description.service.js +221 -0
  141. package/dist/services/activity/activity-human-description.service.test.d.ts +1 -0
  142. package/dist/services/activity/activity-human-description.service.test.js +16 -0
  143. package/dist/services/activity/activity-log.service.d.ts +87 -0
  144. package/dist/services/activity/activity-log.service.js +276 -0
  145. package/dist/services/activity/activity-log.service.test.d.ts +1 -0
  146. package/dist/services/activity/activity-log.service.test.js +27 -0
  147. package/dist/services/activity-human-description.service.d.ts +13 -0
  148. package/dist/services/activity-human-description.service.js +221 -0
  149. package/dist/services/activity-human-description.service.test.d.ts +1 -0
  150. package/dist/services/activity-human-description.service.test.js +16 -0
  151. package/dist/services/activity-log.service.d.ts +87 -0
  152. package/dist/services/activity-log.service.js +276 -0
  153. package/dist/services/activity-log.service.test.d.ts +1 -0
  154. package/dist/services/activity-log.service.test.js +27 -0
  155. package/dist/services/admin/admin.service.d.ts +270 -0
  156. package/dist/services/admin/admin.service.js +476 -0
  157. package/dist/services/admin.service.d.ts +270 -0
  158. package/dist/services/admin.service.js +476 -0
  159. package/dist/services/ai/ai-session.service.d.ts +5 -0
  160. package/dist/services/ai/ai-session.service.js +4 -0
  161. package/dist/services/ai-session.service.d.ts +60 -0
  162. package/dist/services/ai-session.service.js +561 -0
  163. package/dist/services/annotation.service.d.ts +177 -0
  164. package/dist/services/annotation.service.js +154 -0
  165. package/dist/services/artifact-notification.service.d.ts +14 -0
  166. package/dist/services/artifact-notification.service.js +20 -0
  167. package/dist/services/artifact-version.service.d.ts +38 -0
  168. package/dist/services/artifact-version.service.js +129 -0
  169. package/dist/services/artifacts/annotation.service.d.ts +177 -0
  170. package/dist/services/artifacts/annotation.service.js +154 -0
  171. package/dist/services/artifacts/artifact-version.service.d.ts +38 -0
  172. package/dist/services/artifacts/artifact-version.service.js +129 -0
  173. package/dist/services/artifacts/chat.service.d.ts +127 -0
  174. package/dist/services/artifacts/chat.service.js +182 -0
  175. package/dist/services/artifacts/study-guide.service.d.ts +18 -0
  176. package/dist/services/artifacts/study-guide.service.js +65 -0
  177. package/dist/services/auth/auth.service.d.ts +94 -0
  178. package/dist/services/auth/auth.service.js +368 -0
  179. package/dist/services/auth.service.d.ts +94 -0
  180. package/dist/services/auth.service.js +368 -0
  181. package/dist/services/base.service.d.ts +14 -0
  182. package/dist/services/base.service.js +14 -0
  183. package/dist/services/billing/payment.service.d.ts +44 -0
  184. package/dist/services/billing/payment.service.js +365 -0
  185. package/dist/services/billing/subscription.service.d.ts +37 -0
  186. package/dist/services/billing/subscription.service.js +654 -0
  187. package/dist/services/billing/usage.service.d.ts +47 -0
  188. package/dist/services/billing/usage.service.js +149 -0
  189. package/dist/services/chat.service.d.ts +127 -0
  190. package/dist/services/chat.service.js +182 -0
  191. package/dist/services/content/copilot.service.d.ts +113 -0
  192. package/dist/services/content/copilot.service.js +439 -0
  193. package/dist/services/content/flashcard-progress.service.d.ts +159 -0
  194. package/dist/services/content/flashcard-progress.service.js +432 -0
  195. package/dist/services/content/flashcard.service.d.ts +184 -0
  196. package/dist/services/content/flashcard.service.js +339 -0
  197. package/dist/services/content/media-analysis.service.d.ts +23 -0
  198. package/dist/services/content/media-analysis.service.js +404 -0
  199. package/dist/services/content/podcast.service.d.ts +267 -0
  200. package/dist/services/content/podcast.service.js +653 -0
  201. package/dist/services/content/worksheet-content.service.d.ts +37 -0
  202. package/dist/services/content/worksheet-content.service.js +84 -0
  203. package/dist/services/content/worksheet-content.service.test.d.ts +1 -0
  204. package/dist/services/content/worksheet-content.service.test.js +69 -0
  205. package/dist/services/content/worksheet-generation.service.d.ts +91 -0
  206. package/dist/services/content/worksheet-generation.service.js +95 -0
  207. package/dist/services/content/worksheet-generation.service.test.d.ts +1 -0
  208. package/dist/services/content/worksheet-generation.service.test.js +20 -0
  209. package/dist/services/content/worksheet.service.d.ts +347 -0
  210. package/dist/services/content/worksheet.service.js +599 -0
  211. package/dist/services/copilot.service.d.ts +116 -0
  212. package/dist/services/copilot.service.js +447 -0
  213. package/dist/services/flashcard-progress.service.d.ts +2 -2
  214. package/dist/services/flashcard-progress.service.js +3 -2
  215. package/dist/services/flashcard.service.d.ts +140 -0
  216. package/dist/services/flashcard.service.js +325 -0
  217. package/dist/services/invitation.service.d.ts +66 -0
  218. package/dist/services/invitation.service.js +348 -0
  219. package/dist/services/knowledge/knowledge-base.service.d.ts +316 -0
  220. package/dist/services/knowledge/knowledge-base.service.js +544 -0
  221. package/dist/services/knowledge-base.service.d.ts +316 -0
  222. package/dist/services/knowledge-base.service.js +536 -0
  223. package/dist/services/media-analysis.service.d.ts +23 -0
  224. package/dist/services/media-analysis.service.js +384 -0
  225. package/dist/services/member.service.d.ts +36 -0
  226. package/dist/services/member.service.js +193 -0
  227. package/dist/services/members/invitation.service.d.ts +66 -0
  228. package/dist/services/members/invitation.service.js +348 -0
  229. package/dist/services/members/member.service.d.ts +36 -0
  230. package/dist/services/members/member.service.js +193 -0
  231. package/dist/services/note.service.d.ts +55 -0
  232. package/dist/services/note.service.js +111 -0
  233. package/dist/services/notification.service.d.ts +214 -0
  234. package/dist/services/notification.service.js +550 -0
  235. package/dist/services/notification.service.test.d.ts +1 -0
  236. package/dist/services/notification.service.test.js +87 -0
  237. package/dist/services/notifications/notification.service.d.ts +214 -0
  238. package/dist/services/notifications/notification.service.js +550 -0
  239. package/dist/services/notifications/notification.service.test.d.ts +1 -0
  240. package/dist/services/notifications/notification.service.test.js +87 -0
  241. package/dist/services/payment.service.d.ts +55 -0
  242. package/dist/services/payment.service.js +368 -0
  243. package/dist/services/podcast.service.d.ts +267 -0
  244. package/dist/services/podcast.service.js +654 -0
  245. package/dist/services/router-module.service.d.ts +7 -0
  246. package/dist/services/router-module.service.js +10 -0
  247. package/dist/services/study-guide.service.d.ts +18 -0
  248. package/dist/services/study-guide.service.js +65 -0
  249. package/dist/services/subscription.service.d.ts +37 -0
  250. package/dist/services/subscription.service.js +654 -0
  251. package/dist/services/usage-limit-policy.service.d.ts +12 -0
  252. package/dist/services/usage-limit-policy.service.js +22 -0
  253. package/dist/services/usage-limit-policy.service.test.d.ts +1 -0
  254. package/dist/services/usage-limit-policy.service.test.js +46 -0
  255. package/dist/services/usage.service.d.ts +27 -0
  256. package/dist/services/usage.service.js +77 -0
  257. package/dist/services/worksheet-content.service.d.ts +42 -0
  258. package/dist/services/worksheet-content.service.js +84 -0
  259. package/dist/services/worksheet-content.service.test.d.ts +1 -0
  260. package/dist/services/worksheet-content.service.test.js +69 -0
  261. package/dist/services/worksheet-generation.service.d.ts +91 -0
  262. package/dist/services/worksheet-generation.service.js +95 -0
  263. package/dist/services/worksheet-generation.service.test.d.ts +1 -0
  264. package/dist/services/worksheet-generation.service.test.js +20 -0
  265. package/dist/services/worksheet.service.d.ts +385 -0
  266. package/dist/services/worksheet.service.js +596 -0
  267. package/dist/services/workspace/workspace-analytics.service.d.ts +24 -0
  268. package/dist/services/workspace/workspace-analytics.service.js +95 -0
  269. package/dist/services/workspace/workspace-kb.service.d.ts +40 -0
  270. package/dist/services/workspace/workspace-kb.service.js +184 -0
  271. package/dist/services/workspace/workspace.service.d.ts +263 -0
  272. package/dist/services/workspace/workspace.service.js +401 -0
  273. package/dist/services/workspace-analytics.service.d.ts +24 -0
  274. package/dist/services/workspace-analytics.service.js +95 -0
  275. package/dist/services/workspace-kb.service.d.ts +40 -0
  276. package/dist/services/workspace-kb.service.js +184 -0
  277. package/dist/services/workspace-progress.service.d.ts +27 -0
  278. package/dist/services/workspace-progress.service.js +56 -0
  279. package/dist/services/workspace-progress.service.test.d.ts +1 -0
  280. package/dist/services/workspace-progress.service.test.js +49 -0
  281. package/dist/services/workspace.service.d.ts +307 -0
  282. package/dist/services/workspace.service.js +390 -0
  283. package/dist/trpc.d.ts +12 -4
  284. package/dist/trpc.js +7 -13
  285. package/package.json +5 -6
  286. package/prisma/migrations/20260509000001_add_knowledge_base/migration.sql +99 -0
  287. package/prisma/migrations/20260509000002_curate_knowledge_base/migration.sql +52 -0
  288. package/prisma/migrations/20260522000000_add_notes/migration.sql +27 -0
  289. package/prisma/migrations/20260524000000_remove_notes/migration.sql +3 -0
  290. package/prisma/schema.prisma +150 -48
  291. package/prisma/seed.mjs +67 -0
  292. package/scripts/debug/README.md +4 -0
  293. package/src/README.md +63 -0
  294. package/src/context.ts +33 -3
  295. package/src/lib/ai/config.ts +34 -0
  296. package/src/lib/ai/embedding-client.ts +47 -0
  297. package/src/lib/ai/index.ts +65 -0
  298. package/src/lib/ai/inference-backend/client.ts +479 -0
  299. package/src/lib/ai/inference-backend/mocks.ts +171 -0
  300. package/src/lib/ai/inference-backend/types.ts +50 -0
  301. package/src/lib/ai/json-parse.ts +35 -0
  302. package/src/lib/ai/llm-client.ts +54 -0
  303. package/src/lib/ai/mock.ts +12 -0
  304. package/src/lib/ai/types.ts +11 -0
  305. package/src/lib/chunking.ts +81 -0
  306. package/src/lib/curated-kb-seed.ts +164 -0
  307. package/src/lib/email.ts +77 -115
  308. package/src/lib/embeddings.ts +9 -0
  309. package/src/lib/ensure-curated-kb-catalog.ts +60 -0
  310. package/src/lib/env.ts +2 -7
  311. package/src/lib/inference.ts +1 -21
  312. package/src/lib/kb-meta.ts +81 -0
  313. package/src/lib/pdf.ts +23 -0
  314. package/src/lib/workspace-kb.ts +7 -0
  315. package/src/repositories/artifact.repository.ts +55 -0
  316. package/src/repositories/base.repository.ts +19 -0
  317. package/src/repositories/invitation.repository.ts +53 -0
  318. package/src/repositories/notification.repository.ts +53 -0
  319. package/src/repositories/user.repository.ts +44 -0
  320. package/src/repositories/workspace-member.repository.ts +38 -0
  321. package/src/repositories/workspace.repository.ts +89 -0
  322. package/src/routers/_app.ts +4 -0
  323. package/src/routers/admin.ts +124 -692
  324. package/src/routers/annotations.ts +25 -203
  325. package/src/routers/artifactVersions.ts +32 -0
  326. package/src/routers/auth.ts +82 -520
  327. package/src/routers/chat.ts +42 -245
  328. package/src/routers/copilot.ts +41 -666
  329. package/src/routers/flashcards.ts +108 -404
  330. package/src/routers/knowledgeBase.ts +216 -0
  331. package/src/routers/members.ts +60 -782
  332. package/src/routers/notifications.ts +15 -117
  333. package/src/routers/payment.ts +37 -446
  334. package/src/routers/podcast.ts +36 -898
  335. package/src/routers/studyguide.ts +5 -144
  336. package/src/routers/worksheets.ts +171 -735
  337. package/src/routers/workspace.ts +141 -1108
  338. package/src/scripts/purge-deleted-users.ts +2 -2
  339. package/src/server.ts +10 -3
  340. package/src/{lib/activity_human_description.test.ts → services/activity/activity-human-description.service.test.ts} +1 -1
  341. package/src/{lib/activity_log_service.test.ts → services/activity/activity-log.service.test.ts} +1 -1
  342. package/src/{lib/activity_log_service.ts → services/activity/activity-log.service.ts} +2 -2
  343. package/src/services/admin/admin.service.ts +612 -0
  344. package/src/services/ai/ai-session.service.ts +5 -0
  345. package/src/services/artifacts/annotation.service.ts +189 -0
  346. package/src/services/artifacts/artifact-version.service.ts +151 -0
  347. package/src/services/artifacts/chat.service.ts +197 -0
  348. package/src/services/artifacts/study-guide.service.ts +72 -0
  349. package/src/services/auth/auth.service.ts +473 -0
  350. package/src/services/base.service.ts +19 -0
  351. package/src/services/billing/payment.service.ts +433 -0
  352. package/src/{lib/subscription_service.ts → services/billing/subscription.service.ts} +5 -5
  353. package/src/services/billing/usage.service.ts +207 -0
  354. package/src/services/content/copilot.service.ts +587 -0
  355. package/src/services/{flashcard-progress.service.ts → content/flashcard-progress.service.ts} +18 -12
  356. package/src/services/content/flashcard.service.ts +417 -0
  357. package/src/services/content/media-analysis.service.ts +561 -0
  358. package/src/services/content/podcast.service.ts +777 -0
  359. package/src/services/content/worksheet-content.service.test.ts +83 -0
  360. package/src/services/content/worksheet-content.service.ts +117 -0
  361. package/src/{lib/worksheet-generation.test.ts → services/content/worksheet-generation.service.test.ts} +3 -3
  362. package/src/services/content/worksheet.service.ts +751 -0
  363. package/src/services/knowledge/knowledge-base.service.ts +705 -0
  364. package/src/services/members/invitation.service.ts +427 -0
  365. package/src/services/members/member.service.ts +241 -0
  366. package/src/{lib/notification-service.test.ts → services/notifications/notification.service.test.ts} +2 -2
  367. package/src/{lib/notification-service.ts → services/notifications/notification.service.ts} +102 -1
  368. package/src/services/workspace/workspace-analytics.service.ts +107 -0
  369. package/src/services/workspace/workspace-kb.service.ts +273 -0
  370. package/src/services/workspace/workspace.service.ts +488 -0
  371. package/src/trpc.ts +7 -15
  372. package/src/lib/ai-session.ts +0 -704
  373. package/src/lib/usage_service.ts +0 -74
  374. package/src/lib/workspace-access.ts +0 -13
  375. /package/{check-difficulty.cjs → scripts/debug/check-difficulty.cjs} +0 -0
  376. /package/{check-questions.cjs → scripts/debug/check-questions.cjs} +0 -0
  377. /package/{db-summary.cjs → scripts/debug/db-summary.cjs} +0 -0
  378. /package/{mcq-test.cjs → scripts/debug/mcq-test.cjs} +0 -0
  379. /package/{test-generate.js → scripts/debug/test-generate.js} +0 -0
  380. /package/{test-ratio.cjs → scripts/debug/test-ratio.cjs} +0 -0
  381. /package/{zod-test.cjs → scripts/debug/zod-test.cjs} +0 -0
  382. /package/src/{lib/activity_human_description.ts → services/activity/activity-human-description.service.ts} +0 -0
  383. /package/src/{lib/worksheet-generation.ts → services/content/worksheet-generation.service.ts} +0 -0
@@ -2,12 +2,12 @@ import { PrismaClient } from '@prisma/client';
2
2
  import { supabaseClient } from '../lib/storage.js';
3
3
  import { logger } from '../lib/logger.js';
4
4
  import { env } from '../lib/env.js';
5
- import { notifyAdminsAccountPermanentlyDeleted } from '../lib/notification-service.js';
5
+ import { notifyAdminsAccountPermanentlyDeleted } from '../services/notifications/notification.service.js';
6
6
  import {
7
7
  ActivityLogCategory,
8
8
  ActivityLogStatus,
9
9
  } from '@prisma/client';
10
- import { recordExplicitActivity } from '../lib/activity_log_service.js';
10
+ import { recordExplicitActivity } from '../services/activity/activity-log.service.js';
11
11
 
12
12
  const db = new PrismaClient();
13
13
 
package/src/server.ts CHANGED
@@ -15,7 +15,7 @@ import {
15
15
  ActivityLogCategory,
16
16
  ActivityLogStatus,
17
17
  } from '@prisma/client';
18
- import { recordExplicitActivity } from './lib/activity_log_service.js';
18
+ import { recordExplicitActivity } from './services/activity/activity-log.service.js';
19
19
 
20
20
  const PORT = process.env.PORT ? Number(process.env.PORT) : 3001;
21
21
 
@@ -50,7 +50,7 @@ async function main() {
50
50
  const sig = req.headers['stripe-signature'];
51
51
  const { stripe } = await import('./lib/stripe.js');
52
52
  const { env } = await import('./lib/env.js');
53
- const subscriptionService = await import('./lib/subscription_service.js');
53
+ const subscriptionService = await import('./services/billing/subscription.service.js');
54
54
 
55
55
  let event;
56
56
 
@@ -195,9 +195,16 @@ async function main() {
195
195
  }),
196
196
  );
197
197
 
198
- app.listen(PORT, () => {
198
+ app.listen(PORT, async () => {
199
199
  logger.info(`Server ready on http://localhost:${PORT}`, 'SERVER');
200
200
  logger.info(`tRPC endpoint at http://localhost:${PORT}/trpc`, 'SERVER');
201
+
202
+ try {
203
+ const { ensureCuratedKbCatalog } = await import('./lib/ensure-curated-kb-catalog.js');
204
+ await ensureCuratedKbCatalog(prisma);
205
+ } catch (err) {
206
+ logger.error('Failed to seed curated knowledge bases', 'KB', undefined, err instanceof Error ? err : undefined);
207
+ }
201
208
  });
202
209
  }
203
210
 
@@ -3,7 +3,7 @@ import assert from "node:assert/strict";
3
3
  import {
4
4
  getActivityHumanDescription,
5
5
  getTrpcPathsMatchingDescriptionSearch,
6
- } from "./activity_human_description.js";
6
+ } from "./activity-human-description.service.js";
7
7
 
8
8
  test("description search resolves paths by label substring", () => {
9
9
  const paths = getTrpcPathsMatchingDescriptionSearch("study streak");
@@ -5,7 +5,7 @@ import {
5
5
  buildActivityLogWhere,
6
6
  inferCategoryFromTrpcPath,
7
7
  redactSensitive,
8
- } from "./activity_log_service.js";
8
+ } from "./activity-log.service.js";
9
9
 
10
10
  test("redactSensitive redacts sensitive keys", () => {
11
11
  const out = redactSensitive({
@@ -7,8 +7,8 @@
7
7
  import type { Prisma, PrismaClient } from "@prisma/client";
8
8
  import { ActivityLogCategory, ActivityLogStatus } from "@prisma/client";
9
9
  import type { IncomingMessage } from "node:http";
10
- import { logger } from "./logger.js";
11
- import { getTrpcPathsMatchingDescriptionSearch } from "./activity_human_description.js";
10
+ import { logger } from "../../lib/logger.js";
11
+ import { getTrpcPathsMatchingDescriptionSearch } from "./activity-human-description.service.js";
12
12
 
13
13
  const SENSITIVE_KEY_FRAGMENTS = [
14
14
  "password",
@@ -0,0 +1,612 @@
1
+ import type { PrismaClient } from '@prisma/client';
2
+ import { ActivityLogCategory, ActivityLogStatus, ArtifactType, InvoiceType } from '@prisma/client';
3
+ import { BaseService } from '../base.service.js';
4
+ import { stripe } from '../../lib/stripe.js';
5
+ import {
6
+ buildActivityLogWhere,
7
+ deleteActivityLogsOlderThan,
8
+ getActivityRetentionDays,
9
+ } from '../activity/activity-log.service.js';
10
+ import { getActivityHumanDescription } from '../activity/activity-human-description.service.js';
11
+
12
+ function csvEscapeCell(value: string | number | null | undefined): string {
13
+ if (value === null || value === undefined) return '';
14
+ const s = String(value);
15
+ if (/[",\n\r]/.test(s)) return `"${s.replace(/"/g, '""')}"`;
16
+ return s;
17
+ }
18
+
19
+ export interface ActivityFilters {
20
+ from?: Date;
21
+ to?: Date;
22
+ actorUserId?: string;
23
+ workspaceId?: string;
24
+ category?: ActivityLogCategory;
25
+ status?: ActivityLogStatus;
26
+ search?: string;
27
+ }
28
+
29
+ export interface ListUsersInput {
30
+ page: number;
31
+ pageSize: number;
32
+ search?: string;
33
+ emailVerified: 'all' | 'yes' | 'no';
34
+ joinedFrom?: Date;
35
+ joinedTo?: Date;
36
+ }
37
+
38
+ export interface ListInvoicesInput {
39
+ page: number;
40
+ pageSize: number;
41
+ search?: string;
42
+ status?: string;
43
+ type?: InvoiceType;
44
+ userId?: string;
45
+ from?: Date;
46
+ to?: Date;
47
+ }
48
+
49
+ export interface UpsertPlanInput {
50
+ id?: string;
51
+ name: string;
52
+ description?: string;
53
+ type: string;
54
+ price: number;
55
+ stripePriceId?: string;
56
+ interval: string | null;
57
+ active: boolean;
58
+ limits: {
59
+ maxStorageBytes: number;
60
+ maxWorksheets: number;
61
+ maxFlashcards: number;
62
+ maxPodcasts: number;
63
+ maxStudyGuides: number;
64
+ };
65
+ }
66
+
67
+ export class AdminService extends BaseService {
68
+ constructor(db: PrismaClient) {
69
+ super(db);
70
+ }
71
+
72
+ async getSystemStats() {
73
+ const totalUsers = await this.db.user.count();
74
+ const totalWorkspaces = await this.db.workspace.count();
75
+ const totalSubscriptions = await this.db.subscription.count({
76
+ where: { status: 'active' },
77
+ });
78
+
79
+ const subRevenueResult = await (this.db as any).invoice.aggregate({
80
+ where: { status: 'paid', type: 'SUBSCRIPTION' },
81
+ _sum: { amountPaid: true },
82
+ });
83
+ const topupRevenueResult = await (this.db as any).invoice.aggregate({
84
+ where: { status: 'paid', type: 'TOPUP' },
85
+ _sum: { amountPaid: true },
86
+ });
87
+
88
+ const subRevenue = Number(subRevenueResult._sum.amountPaid || 0) / 100;
89
+ const topupRevenue = Number(topupRevenueResult._sum.amountPaid || 0) / 100;
90
+
91
+ return {
92
+ totalUsers,
93
+ totalWorkspaces,
94
+ totalSubscriptions,
95
+ revenue: subRevenue + topupRevenue,
96
+ subscriptionRevenue: subRevenue,
97
+ topupRevenue,
98
+ };
99
+ }
100
+
101
+ async listUsers(input: ListUsersInput) {
102
+ const search = input.search?.trim();
103
+ const baseRoleWhere = {
104
+ role: { name: { not: 'System Admin' } },
105
+ } as const;
106
+
107
+ const searchWhere =
108
+ search && search.length > 0
109
+ ? {
110
+ OR: [
111
+ { email: { contains: search, mode: 'insensitive' as const } },
112
+ { name: { contains: search, mode: 'insensitive' as const } },
113
+ { id: { contains: search } },
114
+ ],
115
+ }
116
+ : undefined;
117
+
118
+ const verifiedWhere =
119
+ input.emailVerified === 'yes'
120
+ ? { emailVerified: { not: null } }
121
+ : input.emailVerified === 'no'
122
+ ? { emailVerified: null }
123
+ : undefined;
124
+
125
+ const joinedWhere =
126
+ input.joinedFrom || input.joinedTo
127
+ ? {
128
+ createdAt: {
129
+ ...(input.joinedFrom ? { gte: input.joinedFrom } : {}),
130
+ ...(input.joinedTo ? { lte: input.joinedTo } : {}),
131
+ },
132
+ }
133
+ : undefined;
134
+
135
+ const where = {
136
+ AND: [
137
+ baseRoleWhere,
138
+ ...(searchWhere ? [searchWhere] : []),
139
+ ...(verifiedWhere ? [verifiedWhere] : []),
140
+ ...(joinedWhere ? [joinedWhere] : []),
141
+ ],
142
+ };
143
+
144
+ const totalCount = await this.db.user.count({ where });
145
+ const pageSize = input.pageSize;
146
+ const totalPages = Math.max(1, Math.ceil(totalCount / pageSize));
147
+ const page = Math.min(Math.max(1, input.page), totalPages);
148
+ const skip = (page - 1) * pageSize;
149
+
150
+ const users = await this.db.user.findMany({
151
+ where,
152
+ skip,
153
+ take: pageSize,
154
+ orderBy: { createdAt: 'desc' },
155
+ include: {
156
+ role: true,
157
+ profilePicture: true,
158
+ subscriptions: {
159
+ orderBy: { createdAt: 'desc' },
160
+ take: 1,
161
+ include: { plan: true },
162
+ },
163
+ },
164
+ });
165
+
166
+ return {
167
+ users: users.map((user: any) => ({
168
+ ...user,
169
+ profilePicture: user.profilePicture?.objectKey
170
+ ? `/profile-picture/${user.profilePicture.objectKey}?t=${new Date(user.updatedAt).getTime()}`
171
+ : null,
172
+ })),
173
+ page,
174
+ pageSize,
175
+ totalCount,
176
+ totalPages,
177
+ };
178
+ }
179
+
180
+ async listInvoices(input: ListInvoicesInput) {
181
+ const search = input.search?.trim();
182
+
183
+ const searchWhere =
184
+ search && search.length > 0
185
+ ? {
186
+ OR: [
187
+ { id: { contains: search } },
188
+ { stripeInvoiceId: { contains: search, mode: 'insensitive' as const } },
189
+ { user: { email: { contains: search, mode: 'insensitive' as const } } },
190
+ { user: { name: { contains: search, mode: 'insensitive' as const } } },
191
+ ],
192
+ }
193
+ : undefined;
194
+
195
+ const userIdWhere = input.userId?.trim() ? { userId: input.userId.trim() } : undefined;
196
+ const statusWhere = input.status?.trim() ? { status: input.status.trim() } : undefined;
197
+ const typeWhere = input.type ? { type: input.type } : undefined;
198
+
199
+ const dateWhere =
200
+ input.from || input.to
201
+ ? {
202
+ createdAt: {
203
+ ...(input.from ? { gte: input.from } : {}),
204
+ ...(input.to ? { lte: input.to } : {}),
205
+ },
206
+ }
207
+ : undefined;
208
+
209
+ const andParts = [
210
+ ...(searchWhere ? [searchWhere] : []),
211
+ ...(userIdWhere ? [userIdWhere] : []),
212
+ ...(statusWhere ? [statusWhere] : []),
213
+ ...(typeWhere ? [typeWhere] : []),
214
+ ...(dateWhere ? [dateWhere] : []),
215
+ ];
216
+ const where = andParts.length > 0 ? { AND: andParts } : {};
217
+
218
+ const totalCount = await (this.db as any).invoice.count({ where });
219
+ const pageSize = input.pageSize;
220
+ const totalPages = Math.max(1, Math.ceil(totalCount / pageSize));
221
+ const page = Math.min(Math.max(1, input.page), totalPages);
222
+ const skip = (page - 1) * pageSize;
223
+
224
+ const invoices = await (this.db as any).invoice.findMany({
225
+ where,
226
+ skip,
227
+ take: pageSize,
228
+ orderBy: { createdAt: 'desc' },
229
+ include: {
230
+ user: { include: { profilePicture: true } },
231
+ subscription: { include: { plan: true } },
232
+ },
233
+ });
234
+
235
+ const items = invoices.map((invoice: any) => ({
236
+ ...invoice,
237
+ user: invoice.user
238
+ ? {
239
+ ...invoice.user,
240
+ profilePicture: invoice.user.profilePicture?.objectKey
241
+ ? `/profile-picture/${invoice.user.profilePicture.objectKey}?t=${new Date(invoice.user.updatedAt).getTime()}`
242
+ : null,
243
+ }
244
+ : invoice.user,
245
+ }));
246
+
247
+ return { items, page, pageSize, totalCount, totalPages };
248
+ }
249
+
250
+ async listWorkspaces(input: { limit: number; cursor?: string | null }) {
251
+ const workspaces = await this.db.workspace.findMany({
252
+ take: input.limit + 1,
253
+ cursor: input.cursor ? { id: input.cursor } : undefined,
254
+ orderBy: { createdAt: 'desc' },
255
+ include: {
256
+ owner: { select: { name: true, email: true } },
257
+ },
258
+ });
259
+
260
+ let nextCursor: typeof input.cursor | undefined = undefined;
261
+ if (workspaces.length > input.limit) {
262
+ const nextItem = workspaces.pop();
263
+ nextCursor = nextItem!.id;
264
+ }
265
+
266
+ return { workspaces, nextCursor };
267
+ }
268
+
269
+ async updateUserRole(actorId: string, input: { userId: string; roleName: string }) {
270
+ const role = await this.db.role.findUnique({ where: { name: input.roleName } });
271
+
272
+ if (!role) {
273
+ throw new Error(`Role ${input.roleName} not found`);
274
+ }
275
+
276
+ const updatedUser = await this.db.user.update({
277
+ where: { id: input.userId },
278
+ data: { roleId: role.id },
279
+ });
280
+
281
+ this.logger.info(
282
+ `User ${input.userId} role updated to ${input.roleName} by admin ${actorId}`,
283
+ 'ADMIN',
284
+ );
285
+ return updatedUser;
286
+ }
287
+
288
+ listPlans() {
289
+ return this.db.plan.findMany({
290
+ include: { limit: true },
291
+ orderBy: { price: 'asc' },
292
+ });
293
+ }
294
+
295
+ async upsertPlan(actorId: string, input: UpsertPlanInput) {
296
+ let { limits, id, stripePriceId, ...planData } = input;
297
+
298
+ if (!stripePriceId && !id) {
299
+ if (!stripe) {
300
+ throw new Error('Stripe is not configured on the server');
301
+ }
302
+
303
+ try {
304
+ const product = await stripe.products.create({
305
+ name: planData.name,
306
+ description: planData.description,
307
+ });
308
+
309
+ const price = await stripe.prices.create({
310
+ product: product.id,
311
+ unit_amount: Math.round(planData.price * 100),
312
+ currency: 'usd',
313
+ recurring:
314
+ planData.type === 'subscription'
315
+ ? { interval: (planData.interval as any) || 'month' }
316
+ : undefined,
317
+ });
318
+
319
+ stripePriceId = price.id;
320
+ this.logger.info(
321
+ `Automatically created Stripe Product (${product.id}) and Price (${price.id}) for plan: ${planData.name}`,
322
+ 'STRIPE',
323
+ );
324
+ } catch (err: any) {
325
+ this.logger.error(`Failed to automate Stripe creation: ${err.message}`, 'STRIPE');
326
+ throw new Error(`Stripe error: ${err.message}`);
327
+ }
328
+ }
329
+
330
+ if (id) {
331
+ const updatedPlan = await this.db.plan.update({
332
+ where: { id },
333
+ data: {
334
+ ...planData,
335
+ stripePriceId,
336
+ limit: {
337
+ update: {
338
+ maxStorageBytes: BigInt(limits.maxStorageBytes),
339
+ maxWorksheets: limits.maxWorksheets,
340
+ maxFlashcards: limits.maxFlashcards,
341
+ maxPodcasts: limits.maxPodcasts,
342
+ maxStudyGuides: limits.maxStudyGuides,
343
+ },
344
+ },
345
+ },
346
+ });
347
+ this.logger.info(`Plan ${id} updated by admin ${actorId}`, 'ADMIN');
348
+ return updatedPlan;
349
+ }
350
+
351
+ const newPlan = await this.db.plan.create({
352
+ data: {
353
+ ...planData,
354
+ stripePriceId: stripePriceId || '',
355
+ limit: {
356
+ create: {
357
+ maxStorageBytes: BigInt(limits.maxStorageBytes),
358
+ maxWorksheets: limits.maxWorksheets,
359
+ maxFlashcards: limits.maxFlashcards,
360
+ maxPodcasts: limits.maxPodcasts,
361
+ maxStudyGuides: limits.maxStudyGuides,
362
+ },
363
+ },
364
+ },
365
+ });
366
+ this.logger.info(`New plan ${newPlan.id} created by admin ${actorId}`, 'ADMIN');
367
+ return newPlan;
368
+ }
369
+
370
+ async deletePlan(actorId: string, id: string) {
371
+ const activeSubs = await this.db.subscription.count({
372
+ where: { planId: id, status: 'active' },
373
+ });
374
+
375
+ if (activeSubs > 0) {
376
+ await this.db.plan.update({ where: { id }, data: { active: false } });
377
+ return {
378
+ success: true,
379
+ message: 'Plan deactivated because it has active subscribers.',
380
+ };
381
+ }
382
+
383
+ await this.db.plan.delete({ where: { id } });
384
+ this.logger.info(`Plan ${id} deleted by admin ${actorId}`, 'ADMIN');
385
+ return { success: true, message: 'Plan deleted.' };
386
+ }
387
+
388
+ getUserInvoices(input: { userId: string; limit?: number }) {
389
+ return (this.db as any).invoice.findMany({
390
+ where: { userId: input.userId },
391
+ orderBy: { createdAt: 'desc' },
392
+ take: input.limit,
393
+ include: {
394
+ subscription: { include: { plan: true } },
395
+ },
396
+ });
397
+ }
398
+
399
+ async getUserDetailedInfo(userId: string) {
400
+ const user = await this.db.user.findUnique({
401
+ where: { id: userId },
402
+ include: {
403
+ role: true,
404
+ profilePicture: true,
405
+ subscriptions: {
406
+ orderBy: { createdAt: 'desc' },
407
+ take: 1,
408
+ include: { plan: true },
409
+ },
410
+ _count: {
411
+ select: { workspaces: true, artifacts: true },
412
+ },
413
+ },
414
+ });
415
+
416
+ if (!user) throw new Error('User not found');
417
+
418
+ const totalSpentResult = await (this.db as any).invoice.aggregate({
419
+ where: { userId, status: 'paid' },
420
+ _sum: { amountPaid: true },
421
+ });
422
+ const totalSpent = Number(totalSpentResult._sum.amountPaid || 0) / 100;
423
+
424
+ const purchasedCredits = await this.db.userCredit.groupBy({
425
+ by: ['resourceType'],
426
+ where: { userId },
427
+ _sum: { amount: true },
428
+ });
429
+
430
+ const profilePictureUrl = (user as any).profilePicture?.objectKey
431
+ ? `/profile-picture/${(user as any).profilePicture.objectKey}?t=${new Date(user.updatedAt).getTime()}`
432
+ : null;
433
+
434
+ return {
435
+ ...user,
436
+ totalSpent,
437
+ purchasedCredits: purchasedCredits.map((c) => ({
438
+ type: c.resourceType,
439
+ amount: c._sum.amount || 0,
440
+ })),
441
+ profilePicture: profilePictureUrl,
442
+ };
443
+ }
444
+
445
+ async debugInvoices() {
446
+ const count = await (this.db as any).invoice.count();
447
+ const all = await (this.db as any).invoice.findMany({ take: 10 });
448
+ return { count, all };
449
+ }
450
+
451
+ listResourcePrices() {
452
+ return this.db.resourcePrice.findMany({
453
+ orderBy: { resourceType: 'asc' },
454
+ });
455
+ }
456
+
457
+ async upsertResourcePrice(
458
+ actorId: string,
459
+ input: { resourceType: ArtifactType; priceCents: number },
460
+ ) {
461
+ const price = await this.db.resourcePrice.upsert({
462
+ where: { resourceType: input.resourceType },
463
+ update: { priceCents: input.priceCents },
464
+ create: {
465
+ resourceType: input.resourceType,
466
+ priceCents: input.priceCents,
467
+ },
468
+ });
469
+ this.logger.info(
470
+ `Resource price for ${input.resourceType} updated to ${input.priceCents} by admin ${actorId}`,
471
+ 'ADMIN',
472
+ );
473
+ return price;
474
+ }
475
+
476
+ async listRecentInvoices(limit: number) {
477
+ const invoices = await (this.db as any).invoice.findMany({
478
+ take: limit,
479
+ orderBy: { createdAt: 'desc' },
480
+ include: {
481
+ user: { include: { profilePicture: true } },
482
+ },
483
+ });
484
+
485
+ return invoices.map((invoice: any) => ({
486
+ ...invoice,
487
+ user: {
488
+ ...invoice.user,
489
+ profilePicture: invoice.user.profilePicture?.objectKey
490
+ ? `/profile-picture/${invoice.user.profilePicture.objectKey}?t=${new Date(invoice.user.updatedAt).getTime()}`
491
+ : null,
492
+ },
493
+ }));
494
+ }
495
+
496
+ async activityList(input: ActivityFilters & { page: number; limit: number }) {
497
+ const where = buildActivityLogWhere({
498
+ from: input.from,
499
+ to: input.to,
500
+ actorUserId: input.actorUserId,
501
+ workspaceId: input.workspaceId,
502
+ category: input.category,
503
+ status: input.status,
504
+ search: input.search,
505
+ });
506
+
507
+ const totalCount = await this.db.activityLog.count({ where });
508
+ const totalPages = Math.max(1, Math.ceil(totalCount / input.limit));
509
+ const page = Math.min(Math.max(1, input.page), totalPages);
510
+ const skip = (page - 1) * input.limit;
511
+
512
+ const rows = await this.db.activityLog.findMany({
513
+ where,
514
+ skip,
515
+ take: input.limit,
516
+ orderBy: { createdAt: 'desc' },
517
+ include: {
518
+ actor: { select: { id: true, email: true, name: true } },
519
+ workspace: { select: { id: true, title: true } },
520
+ },
521
+ });
522
+
523
+ return {
524
+ items: rows.map((r) => ({
525
+ id: r.id,
526
+ createdAt: r.createdAt,
527
+ action: r.action,
528
+ description: getActivityHumanDescription(r.trpcPath, r.action),
529
+ category: r.category,
530
+ trpcPath: r.trpcPath,
531
+ status: r.status,
532
+ durationMs: r.durationMs,
533
+ errorCode: r.errorCode,
534
+ ipAddress: r.ipAddress,
535
+ actor: r.actor,
536
+ workspace: r.workspace,
537
+ metadata: r.metadata,
538
+ })),
539
+ page,
540
+ pageSize: input.limit,
541
+ totalCount,
542
+ totalPages,
543
+ };
544
+ }
545
+
546
+ async activityExportCsv(input: ActivityFilters & { maxRows: number }) {
547
+ const where = buildActivityLogWhere({
548
+ from: input.from,
549
+ to: input.to,
550
+ actorUserId: input.actorUserId,
551
+ workspaceId: input.workspaceId,
552
+ category: input.category,
553
+ status: input.status,
554
+ search: input.search,
555
+ });
556
+
557
+ const rows = await this.db.activityLog.findMany({
558
+ where,
559
+ orderBy: { createdAt: 'desc' },
560
+ take: input.maxRows,
561
+ include: {
562
+ actor: { select: { email: true, name: true } },
563
+ workspace: { select: { title: true } },
564
+ },
565
+ });
566
+
567
+ const header = [
568
+ 'createdAt',
569
+ 'actorEmail',
570
+ 'actorName',
571
+ 'description',
572
+ 'trpcPath',
573
+ 'category',
574
+ 'status',
575
+ 'durationMs',
576
+ 'workspaceTitle',
577
+ 'errorCode',
578
+ ].join(',');
579
+
580
+ const lines = rows.map((r) =>
581
+ [
582
+ csvEscapeCell(r.createdAt.toISOString()),
583
+ csvEscapeCell(r.actor?.email),
584
+ csvEscapeCell(r.actor?.name),
585
+ csvEscapeCell(getActivityHumanDescription(r.trpcPath, r.action)),
586
+ csvEscapeCell(r.trpcPath),
587
+ csvEscapeCell(r.category),
588
+ csvEscapeCell(r.status),
589
+ csvEscapeCell(r.durationMs),
590
+ csvEscapeCell(r.workspace?.title),
591
+ csvEscapeCell(r.errorCode),
592
+ ].join(','),
593
+ );
594
+
595
+ return {
596
+ csv: [header, ...lines].join('\n'),
597
+ count: rows.length,
598
+ };
599
+ }
600
+
601
+ async activityPurgeRetention() {
602
+ const days = getActivityRetentionDays();
603
+ const cutoff = new Date();
604
+ cutoff.setDate(cutoff.getDate() - days);
605
+ const { deleted } = await deleteActivityLogsOlderThan(this.db, cutoff);
606
+ this.logger.info(
607
+ `ActivityLog retention purge: deleted ${deleted} rows older than ${days} days (cutoff ${cutoff.toISOString()})`,
608
+ 'ADMIN',
609
+ );
610
+ return { deleted, retentionDays: days, cutoff };
611
+ }
612
+ }
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Back-compat shim — prefer `import { ai } from '../../lib/ai/index.js'`.
3
+ */
4
+ export { inferenceBackend as aiSessionService } from '../../lib/ai/inference-backend/client.js';
5
+ export type { AISession, ProcessFileResult } from '../../lib/ai/inference-backend/types.js';